cancel
Showing results for 
Search instead for 
Did you mean: 

SPI CRC problem with dma on stm32f0

mjenabzadeh9
Associate III
Posted on December 31, 2016 at 10:06

Hello

I have connected two uc through SPI .

My target is to send data with DAM+CRC.

one of them is slave stm32f030 and the other one  is stm32f429.

I'm using cubeMX for confige spi and dma  registers .

Here is slave config:

* SPI1 init function */

static void MX_SPI1_Init(void)

{

hspi1.Instance = SPI1;

hspi1.Init.Mode = SPI_MODE_SLAVE;

hspi1.Init.Direction = SPI_DIRECTION_2LINES_RXONLY;

hspi1.Init.DataSize = SPI_DATASIZE_16BIT;

hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;

hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;

hspi1.Init.NSS = SPI_NSS_SOFT;

hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;

hspi1.Init.TIMode = SPI_TIMODE_DISABLE;

hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_ENABLE;

hspi1.Init.CRCPolynomial = 50835;

hspi1.Init.CRCLength = SPI_CRC_LENGTH_16BIT;

hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;

if (HAL_SPI_Init(&hspi1) != HAL_OK)

{

Error_Handler();

}

}

And here is master config:

/* SPI4 init function */

static void MX_SPI4_Init(void)

{

hspi4.Instance = SPI4;

hspi4.Init.Mode = SPI_MODE_MASTER;

hspi4.Init.Direction = SPI_DIRECTION_2LINES;

hspi4.Init.DataSize = SPI_DATASIZE_16BIT;

hspi4.Init.CLKPolarity = SPI_POLARITY_LOW;

hspi4.Init.CLKPhase = SPI_PHASE_1EDGE;

hspi4.Init.NSS = SPI_NSS_SOFT;

hspi4.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;

hspi4.Init.FirstBit = SPI_FIRSTBIT_MSB;

hspi4.Init.TIMode = SPI_TIMODE_DISABLE;

hspi4.Init.CRCCalculation = SPI_CRCCALCULATION_ENABLE;

hspi4.Init.CRCPolynomial = 50835;

if (HAL_SPI_Init(&hspi4) != HAL_OK)

{

Error_Handler();

}

}

I use Hal lib to send data :

HAL_SPI_Transmit_DMA(&hspi4,ptr,1);

and call this function in receive side ;

HAL_SPI_Receive_DMA(&hspi1,ptr,1);

data is received as  i expected 

but i have changed CRC POLYNOMIAL in master side  for test crc error in slave side .

but I havn't got any error .

I have checked data transmission by oscilloscope CRC is attached at the end of the data .

when i set breakpoint in DMA receive complete the 'SPI Rx CRC register' was 0 !

and the CRC ERROR bit in status was 0!

i guess the problem is in slave side but i can't find the problem .

I would appreciate all help.

5 REPLIES 5
mjenabzadeh9
Associate III
Posted on January 01, 2017 at 09:45

Thanks Clive 

'

Some how I suspect you have to transmit/receive more than one byte,'

yes you are right :

slave : 

   hspi1.Init.DataSize = SPI_DATASIZE_16BIT;

master :

hspi4.Init.DataSize = SPI_DATASIZE_16BIT;

'What polynomial do you think you are implementing here? For it to work you need specific feed back terms, you can't just use any number in the 16-bit range.'

as i see data sheet and errata  the only limitation is that polynomial must be odd  and of course 16 bits range . 

mjenabzadeh9
Associate III
Posted on January 01, 2017 at 14:39

Is there any kind of sample code for Spi with crc enable and Dma data  transfer .

Posted on January 01, 2017 at 15:53

To see the CRC on the receiving end you'd need to specify a length that included the data + crc bytes or words. If you reset the CRC first, passing the correct one through will result in the remainder being zero, and peripheral flagging it as correct. Seeing the data would allow you to confirm the results manually.

On the transmitting side you need to send the data first, then switch to sending the CRC bytes/words to have them clock over the wire. There would need to be two transactions, only the data portion using DMA

I don't have HAL examples for this.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on December 31, 2016 at 17:51

Some how I suspect you have to transmit/receive more than one byte, and manage/effect the transition between sending data and sending CRC bytes.

What polynomial do you think you are implementing here? For it to work you need specific feed back terms, you can't just use any number in the 16-bit range.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on January 02, 2017 at 10:43

'

To see the CRC on the receiving end you'd need to specify a length that included the data + crc bytes or words.'

i have read this on user manual :

'In receive only mode, the DMA reception channel counter should contain only the amount of

data transferred, excluding the CRC calculation. Then based on the complete transfer from

DMA, all the CRC values must be read back by software from FIFO as it works as a single

buffer in this mode.'

i don't understand this part :

'

Then based on the complete transfer from

DMA, all the CRC values must be read back by software from FIFO as it works as a single

buffer in this mode.''

'On the transmitting side you need to send the data first, then switch to sending the CRC bytes/words to have them clock over the wire. There would need to be two transactions, only the data portion using DMA'

I see data+CRC by oscilloscope master send data correct .