AnsweredAssumed Answered

STM32L051C6 SPI DMA extra ghost byte

Question asked by chiu.shuchun on May 27, 2015
Hi, 
I have discover there is one extra ghost byte is sent in SPI. I have monitor the memory area of DMA TX buffer, the data there is correct, but I will receive one extra byte in the beginning.

Here is my code

/* Buffers used for transmission */
uint8_t amsgHeadBuffer[MPUHEADERSIZE];
uint8_t amsgpayloadBuffer[MPUPAYLOADSIZE];
uint8_t aRxBuffer[BUFFERSIZE];
uint8_t aTxBuffer[BUFFERSIZE];

void spi_handler_init(void)
{     
     SpiHandle.Instance               = SPIx;
  SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
  SpiHandle.Init.Direction         = SPI_DIRECTION_2LINES;
  SpiHandle.Init.CLKPhase          = SPI_PHASE_1EDGE;
  SpiHandle.Init.CLKPolarity       = SPI_POLARITY_LOW;
  SpiHandle.Init.CRCCalculation    = SPI_CRCCALCULATION_DISABLED;
  SpiHandle.Init.CRCPolynomial     = 7;
  SpiHandle.Init.DataSize          = SPI_DATASIZE_8BIT;
  SpiHandle.Init.FirstBit          = SPI_FIRSTBIT_MSB;
  SpiHandle.Init.NSS               = SPI_NSS_HARD_INPUT;
  SpiHandle.Init.TIMode            = SPI_TIMODE_DISABLED;
     SpiHandle.Init.Mode                                = SPI_MODE_SLAVE;
     if(HAL_SPI_Init(&SpiHandle) != HAL_OK) {
    /* Initialization Error */
    Error_Handler();
  }
     if(HAL_SPI_TransmitReceive_DMA(&SpiHandle, (uint8_t *)aTxBuffer, (uint8_t *)aRxBuffer, BUFFERSIZE) != HAL_OK)
     {
          err = 0;
          Error_Handler();
     }     
}
void msg_handler(void)
{     
     int cnt = SpiHandle.hdmarx->Instance->CNDTR;
     rx_index = 0;
     HAL_SPI_DMAStop(&SpiHandle);
     /* 1. Get MPU Message Header */
     memcpy(amsgHeadBuffer, aRxBuffer+rx_index, sizeof(MpuMsgHeader_t));
     rx_index += sizeof(MpuMsgHeader_t);
     msg = mpu_message_header(amsgHeadBuffer);
     len_mpu_payload = msg->payloadLen;
     cmd_type = msg->type;
     test(msg);
     if(HAL_SPI_TransmitReceive_DMA(&SpiHandle, (uint8_t *)aTxBuffer, (uint8_t *)aRxBuffer, BUFFERSIZE) != HAL_OK)
     {
          err = 1;
          Error_Handler();
     }     
}
void spi_write(uint8_t * buff, int size)
{
     int i;
     /* pull GP_nINT_PMU_PIN low for trigger interrupt */
     GP_nINT_PMU_BANK->BSRR = GP_nINT_PMU_PIN ;
     udelay(50);
     GP_nINT_PMU_BANK->BRR = GP_nINT_PMU_PIN ;
     memset(aTxBuffer, 0x00, BUFFERSIZE);
     memcpy(aTxBuffer, buff, size);
}

Outcomes