0

我正在尝试将 SPI 接口与 SPI ADXL345 传感器和 STM32F072 Nucleo 板一起使用。我想做一个非常基本的读取操作。现在读取功能在等待 RXNE 信号时被阻塞。但 RXNE 从未设置。SPI 总是读取 0。我在这里使用 IAR 是我的代码,我也使用这些函数来写入和读取 SPI。

int main()
{

    USART2_Init();
    SPIx_Init();

    while (1) {
        adxl_baslat();
        receiveData[0] = readData(0x32);
        receiveData[1] = readData(0x33);
        receiveData[2] = readData(0x34);
        receiveData[3] = readData(0x35);
        receiveData[4] = readData(0x36);
        receiveData[5] = readData(0x37);

        x = (receiveData[1] << 8) | (receiveData[0]);
        y = (receiveData[3] << 8) | (receiveData[2]);
        z = (receiveData[5] << 8) | (receiveData[4]);
        xg = (float)x * 0.0078;
        yg = (float)x * 0.0078;
        zg = (float)x * 0.0078;
        sprintf(buffer, "xg: %f yg: %f zg: %f \r\n", xg, yg, zg);
        USART_PutString(buffer);
    }
}

void SPIx_Init()
{

    SPI_InitTypeDef SPI_InitStruct;
    GPIO_InitTypeDef GPIO_InitStruct;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
    SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
    SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;
    SPI_InitStruct.SPI_CPOL = SPI_CPOL_High;
    SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
    SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
    SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set;
    SPI_Init(SPI1, &SPI_InitStruct);
    SPI_Cmd(SPI1, ENABLE);
    SPI_DataSizeConfig(SPI1, ENABLE);
    SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF);

    // Step 2: Initialize GPIO
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
    // GPIO pins for MOSI, MISO, and SCK
    GPIO_InitStruct.GPIO_Pin = SPI_PIN_MOSI | SPI_PIN_MISO | SPI_PIN_SCK;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;

    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
    GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_1);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_1);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_1);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
    GPIO_InitStruct.GPIO_Pin = SPI_PIN_SS;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;

    GPIO_Init(GPIOB, &GPIO_InitStruct);
}

uint8_t sendByte(uint8_t byteToSend)
{
    timeout = TIMEOUT_TIME;
    while ((SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET) & (timeout != 0)) {
        timeout--;
    }
    SPI_SendData8(SPI1, byteToSend);
    timeout = TIMEOUT_TIME;
    while ((SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET) & (timeout != 0)) {
        timeout--;
    }
    return (uint8_t)SPI_ReceiveData8(SPI1);
}

void writeData(uint8_t address, uint8_t dataToWrite)
{

    DATA2 = address | 0x40;
    GPIO_ResetBits(GPIOB, GPIO_Pin_9);
    sendByte(DATA2);
    sendByte(dataToWrite);

    GPIO_SetBits(GPIOB, GPIO_Pin_9);
}

uint8_t readData(uint8_t address)
{
    address |= 0x40;
    address |= 0x80;
    GPIO_ResetBits(GPIOB, GPIO_Pin_9);
    sendByte(address);
    tempByte = sendByte(address);
    GPIO_SetBits(GPIOB, GPIO_Pin_9);
    return tempByte;
}

void adxl_baslat(void)
{
    writeData(0x31, 0x01);
    writeData(0x2d, 0x00);
    writeData(0x2d, 0x08);
}
4

2 回答 2

0

您的初始化看起来很完整,但我发现了一个错误:需要将 GPIO 引脚 A5/A6/A7 设置为GPIO_AF_0(而不是GPIO_AF_1)才能在这些引脚上获得 SPI1 SCK/MISO/MOSI(参见 STM32F072 数据表表 15)。这就是为什么您可能从 RX FIFO 中读取始终为零的原因。

不过,我有点想知道没有设置 RXNE 标志。当 SPI 单元处于全双工主模式时,即使 GPIO 引脚配置不正确,它也应该为每个发送的字节接收一个字节。但我认为 STM32F0 处理这种情况的方式与旧的 STM32L1 不同:当我没记错时,我确实注意到在 STM32F0 上,SPI 接收器似乎从 GPIO 引脚获取时钟信号,而不是直接从 SPI 发送器(生成它)。因此,错误配置的 SCK 引脚也可能解释 SPI RX 单元根本没有接收到任何信息。

于 2020-10-02T00:05:08.493 回答
0

您需要清除接收 FIFO 或 DR 才能接收有意义的数据


uint8_t sendByte(uint8_t byteToSend)
{
    timeout = TIMEOUT_TIME;
    while ((SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) != RESET) 
    {
        uint8_t dummy;
        dummy = *(volatile uint8_t *)&SPI1 -> DR;
    }
    timeout = TIMEOUT_TIME;
    while ((SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET) & (timeout != 0)) {
        timeout--;
    }
    SPI_SendData8(SPI1, byteToSend);
    timeout = TIMEOUT_TIME;
    while ((SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET) & (timeout != 0)) {
        timeout--;
    }
    return (uint8_t)SPI_ReceiveData8(SPI1);
}
于 2020-10-02T11:32:36.023 回答