关于F3Discovery的SPI

时间:2016-09-21 09:44:42

标签: c stm32 cortex-m

我试图为F3Disco写SPI连接,我有以下问题: 当我尝试使用Std库时它可以工作,但只有寄存器不能... 设定:

VAR=

if [ -n $VAR ]
then
        echo "\$VAR is nonzero"
else
        echo "\$VAR is zero"
fi

比主要

void GyroSPIConf(){
RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
RCC->AHBENR |= RCC_AHBENR_GPIOEEN;
RCC->APB2ENR |= RCC_APB2ENR_SPI1EN;
/* Set functions GPIO - SCK, MISO, MOSI */
GPIOA->MODER |= GPIO_MODER_MODER5_1 | GPIO_MODER_MODER6_1 | GPIO_MODER_MODER7_1;
GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR5 | GPIO_OSPEEDER_OSPEEDR6 | GPIO_OSPEEDER_OSPEEDR7;
GPIOA->AFR[0] |= GPIO_AFRL_AFRL5 | GPIO_AFRL_AFRL6 | GPIO_AFRL_AFRL7;

/* Set PE3 as CS - GPIO output */
GPIOE->MODER |= GPIO_MODER_MODER3_0;
GPIOE->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR3;
GPIOE->ODR |= GPIO_ODR_3;

/* Set SPI MASTER 2,25 MHz; 8 bit */
SPI1->CR1 |= SPI_CR1_SSM | SPI_CR1_SSI | SPI_CR1_MSTR | SPI_CR1_BR_1;// | SPI_CR1_BR_0;
SPI1->CR2 |= SPI_CR2_FRXTH;

/* SPI */
SPI1->CR1 |= SPI_CR1_SPE;
GPIOE->BSRR = (1 << 3);
}
void IOInit(void)
{
    __GPIOE_CLK_ENABLE();
    GPIO_InitTypeDef GPIO_InitStructure;

    GPIO_InitStructure.Pin =    GPIO_PIN_12 |                   // LD9  - Blue
                                GPIO_PIN_8  |                   // LD4  - Blue
                                GPIO_PIN_15 |                   // LD6  - Green
                                GPIO_PIN_14 |                   // LD8  - Orange
                                GPIO_PIN_13 |                   // LD10 - Red 
                                GPIO_PIN_10 |                   // LD5  - Orange
                                GPIO_PIN_11 |                   // LD7  - Green
                                GPIO_PIN_9;                     // LD3  - Red

    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
    GPIO_InitStructure.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOE, &GPIO_InitStructure);

    __GPIOA_CLK_ENABLE();                                       // Will be overwritten
}

功能:

int main(void)
{
    uint8_t Data[8];
    HAL_Init();
    IOInit();   
    GyroSPIConf();  
    GyroWriteByte(0x20, 0x0F);
    while (1){
        Data[0] = GyroReadByte(0x8f);
    }
}

我知道为什么我的数据总是为0x00?

更改后:

uint8_t GyroTransferByte(uint8_t val) {
    volatile uint8_t * const ptr = (volatile uint8_t *)&SPI1->DR;
    while (!(SPI1->SR & SPI_SR_TXE)) {};
    *ptr = val;
    while (!(SPI1->SR & SPI_SR_RXNE)) {};
    return *ptr;
}

void GyroWriteByte(uint8_t addr, uint8_t val) {
    uint8_t d;
    GPIOE->BSRR = (1 << 19);
    d = GyroTransferByte(addr);
    d = GyroTransferByte(val);
    GPIOE->BSRR = (1 << 3);
}

uint8_t GyroReadByte(uint8_t addr) {
    GPIOE->BSRR = (1 << 19);
    GPIOE->BSRR = (1 << 24);
    volatile uint8_t val;
    //addr |= R_BIT;
    val = GyroTransferByte(addr);
    val = GyroTransferByte(DUMMY_BYTE);
    GPIOE->BSRR = (1 << 3);
    GPIOE->BSRR = (1 << 8); 
    return val;
}

我得到0xff(实际值是0xD4)

编辑: 改变后,我进入第一次运行:FF 第二次运行后:DF 第三次运行后:D4 - 正确值 然后其余的运行FF ......

所以我需要清除缓冲区吗?

0 个答案:

没有答案