EmbDev.net

Forum: µC & Digital Electronics STM32F407 SPI reads wrong data


Author: Mirko (Guest)
Posted on:
Attached files:

Rate this post
0 useful
not useful
Hey,

I'm encountering a strange problem with the hardware SPI of the 
STM32F407 [2] (discovery board). I'm trying to communicate with an UM7 
IMU [1] over its SPI interface.

When I try to read its firmware revision, I receive ASCII 'T71E' instead 
of the expected 'U71D'. You can see the SPI transmission recorded using 
a logic analyzer in the pictures, with the closeup showing the 
transmission for the last byte, which should be 'D' but is 'E'.

To me at least it looks like the data is transmitted correctly by the 
UM7, but somehow the STM reads the last bit incorrectly.

The behavior is reproducible, i.e. the first and last byte always have 
the last bit flipped.

Are there any pitfalls for the STM SPI I should aware of? Or am I doing 
something else wrong?

Regards,
Mirko

[1] Datasheet UM7: https://www.chrobotics.com/docs/UM7_Datasheet.pdf
[2] Datasheet STM32F407: 
http://www2.st.com/content/ccc/resource/technical/...

Code, in case I just made a dumb error somewhere...
void spi_init(void) {
      hspi.Instance = SPI1;
      hspi.Init.Mode = SPI_MODE_MASTER;
      hspi.Init.Direction = SPI_DIRECTION_2LINES;
      hspi.Init.DataSize = SPI_DATASIZE_8BIT;
      hspi.Init.CLKPolarity = SPI_POLARITY_LOW;
      hspi.Init.CLKPhase = SPI_PHASE_1EDGE;
      hspi.Init.NSS = SPI_NSS_SOFT;
      hspi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
      hspi.Init.FirstBit = SPI_FIRSTBIT_MSB;
      hspi.Init.TIMode = SPI_TIMODE_DISABLE;
      hspi.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
      hspi.Init.CRCPolynomial = 10;
      HAL_SPI_Init(&hspi);
}

uint8_t transceive_byte(uint8_t data) {
    uint8_t recv = 0x00;
    HAL_SPI_TransmitReceive(&hspi, &data, &recv, 1, HAL_MAX_DELAY);
    return recv;
}

uint32_t transceive_packet(uint8_t mode, uint8_t addr, uint8_t* data) {

    HAL_GPIO_WritePin(GPIO_IMU_SS_GPIO_Port, GPIO_IMU_SS_Pin, GPIO_PIN_RESET);
    delay(10); // delay is in uS
    transceive_byte(mode);
    delay(20);
    transceive_byte(addr);

    uint32_t recv_data = 0;
    for(int i = 0; i < 4; i++) {
        delay(20);
        recv_data |= transceive_byte(data[i]) << (i * 8);
    }

    HAL_GPIO_WritePin(GPIO_IMU_SS_GPIO_Port, GPIO_IMU_SS_Pin, GPIO_PIN_SET);

    return recv_data;
}

void imu_spi_test() {
    delay_init();
    spi_init();

    uint32_t cmd = 0;
    char fw_version[5] = {0};
    *((uint32_t*)fw_version)= transceive_packet(CMD_WRITE, 0xAA, &cmd);

    trace("FW: %s\n", fw_version);
}

Author: Mirko (Guest)
Posted on:

Rate this post
0 useful
not useful
Nevermind, using SPI2 works fine. I think the jtag interface shared some 
pins with SPI1, which caused the errors.

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.