SPI always read 0

1.1k views Asked by At

I am trying to use the SPI interface with a SPI ADXL345 sensor and the STM32F072 Nucleo board.I want to make a very basic read operation.Now the read function is blocked in waiting the the RXNE signal.But the RXNE is never set . SPI always read 0. I use IAR here is my codes, I use these functions too write and read 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);
}
2

There are 2 answers

1
Blue On

Your initialization looks quite complete, but I spotted one error: GPIO pins A5/A6/A7 need to be set to GPIO_AF_0 (instead of GPIO_AF_1) in order to get SPI1 SCK/MISO/MOSI at these pins (see STM32F072 datasheet table 15). This is a reason, why you might read always zero from the RX FIFO.

I am wondering somewhat about the RXNE flag not being set, though. With the SPI unit in full-duplex master mode it should receive a byte for each byte sent, even if the GPIO pins are not configured correctly. But I think the STM32F0 handles this situation differently to the older STM32L1: When I remember correctly I did notice that on STM32F0 the SPI receiver seems to get its clock signal from the GPIO pin and not directly from the SPI sender (which generates it). Therefore a wrongly configured SCK pin might also explain that the SPI RX unit receives nothing at all.

4
0___________ On

You need to clear receive FIFO or DR before you can receive meaningful data


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);
}