AnsweredAssumed Answered

STM32F411 SPI2 - MPU6000 - Tx and Rx with DMA

Question asked by elgarbe on May 4, 2015
I'm comunicating a nucleo board with a mpu6000 mems sensor, using CubeMX to make initialization code.
It is working ok, but with just one issue. I have to write 0x3B|0x80 and a 16 0x00 (dummy bytes) and read back 16 bytes from sensor.
I have a uint8_t dataOut[16] = {0x3B|0x80, 0,0,0,0....,0}
The thing is that HAL_SPI_TransmitReceive_DMA send 3 times 0x3B|0x80 and so the data I've read is wrong.
I solve this issue first sending HAL_SPI_Transmit 0x3B|0x80 and then calling to HAL_SPI_TransmitReceive_DMA. But I'm really would like erase SPI_Transmit.
Does anybody know somthing about it?

/* Peripheral DMA init*/
hdma_spi2_tx.Instance = DMA1_Stream4;
hdma_spi2_tx.Init.Channel = DMA_CHANNEL_0;
hdma_spi2_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_spi2_tx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_spi2_tx.Init.MemInc = DMA_MINC_DISABLE;
hdma_spi2_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_spi2_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_spi2_tx.Init.Mode = DMA_NORMAL;
hdma_spi2_tx.Init.Priority = DMA_PRIORITY_LOW;
hdma_spi2_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma_spi2_rx.Instance = DMA1_Stream3;
hdma_spi2_rx.Init.Channel = DMA_CHANNEL_0;
hdma_spi2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_spi2_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_spi2_rx.Init.MemInc = DMA_MINC_ENABLE;
hdma_spi2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_spi2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_spi2_rx.Init.Mode = DMA_NORMAL;
hdma_spi2_rx.Init.Priority = DMA_PRIORITY_LOW;
hdma_spi2_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;

sensor continously make an interrupt at sample rate (200Hz) and on ISR:
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
    uint8_t addr;
//    data[0]=0x3B|0x80;
    HAL_SPI_Transmit(&hspi2, &addr, 1, 10);
    HAL_SPI_Receive_DMA(&hspi2, data, 16);
//    HAL_SPI_TransmitReceive_DMA(&hspi2, &addr, data, 17);
HAL_SPI_Receive_DMA calls to HAL_SPI_TransmitReceive_DMA inside their code.

on SPI_DMA Tx complete I do:
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
    int16_t tmp=0;
    tmp=(data[6]<<8) | data[7];
    tempC = (double)(tmp)/340.0 + 36.53;
    X_accel = (int16_t)((data[0]<<8) | data[1])/16384.0;
    Y_accel = (int16_t)((data[2]<<8) | data[3])/16384.0;
    Z_accel = (int16_t)((data[4]<<8) | data[5])/16384.0;