cancel
Showing results for 
Search instead for 
Did you mean: 

I2C with DMA last byte error written to zero instead value

DS.4
Senior II

hello,

trying to write to a ST25DV tag mailbox, via I2C with DMA.

The chip has a mailbox of 256 bytes.

I use  the function:

HAL_I2C_Mem_Write_DMA

 

 

When I try to write data of size 256 bytes, only 255 is written and the last one is written to 0.

Writing 255 bytes works fine. 

 

Writing without DMA fixes the issue:

 

 

HAL_I2C_Mem_Write

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Saket_Om
ST Employee

Hello DS.4

A similar issue has been reported on GitHub for HAL_I2C_Mem_Read_DMA.

If you are encountering the same issue, you can adjust the priority of DMA and I2C interrupts by setting the DMA interrupt priority higher than the I2C interrupt priority. Here is an example of how you can configure the interrupt priorities.

 

  /* NVIC configuration for DMA transfer complete interrupt (I2C1_TX) */
  HAL_NVIC_SetPriority(I2Cx_DMA_TX_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(I2Cx_DMA_TX_IRQn);

  /* NVIC for I2Cx */
  HAL_NVIC_SetPriority(I2Cx_ER_IRQn, 1, 0);
  HAL_NVIC_EnableIRQ(I2Cx_ER_IRQn);
  HAL_NVIC_SetPriority(I2Cx_EV_IRQn, 2, 0);
  HAL_NVIC_EnableIRQ(I2Cx_EV_IRQn);

 

 If your question is answered, please close this topic by clicking "Accept as Solution".
Thanks

Omar

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar

View solution in original post

6 REPLIES 6
DS.4
Senior II

Found this in hal_i2c:

#define MAX_NBYTE_SIZE 255U

Is it related? I don't think I can increase it to 256, as it must be uint8_t

Pavel A.
Evangelist III

Yes it looks like related. If ST25DV allows, do the write in two (or more) transactions.

 

Thanks, but can I write twice to the mailbox?

Asking because When I write the mailbox the first time ,

I see that its status changes to HostPutMsg = 1 ,

And I am not sure I can write again until the peer side reads...

 

Also not sure speed-wise if it's efficient to write twice just to get that byte..

Pavel A.
Evangelist III

From the code in stm32xxx_i2c.c if the transmit size > MAX_NBYTE_SIZE the function will automatically divide the send operation to several parts. This should work seamlessly. If it does not - please use a logic analyzer to understand what goes after sending first 255 bytes.

 

JL. Lebon
ST Employee

Hi DS.4, 

Concerning multiple writes in the ST25DV mailbox, your understanding is correct. You have to write it in one shot.

Once you have written into it, the HOSTPUTMSG bit is set to 1 at the stop condition of the write command and the mailbox must be read by the peer or reset (MB_EN=0 then MB_EN=1) to allow a new write.

Best regards.

Saket_Om
ST Employee

Hello DS.4

A similar issue has been reported on GitHub for HAL_I2C_Mem_Read_DMA.

If you are encountering the same issue, you can adjust the priority of DMA and I2C interrupts by setting the DMA interrupt priority higher than the I2C interrupt priority. Here is an example of how you can configure the interrupt priorities.

 

  /* NVIC configuration for DMA transfer complete interrupt (I2C1_TX) */
  HAL_NVIC_SetPriority(I2Cx_DMA_TX_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(I2Cx_DMA_TX_IRQn);

  /* NVIC for I2Cx */
  HAL_NVIC_SetPriority(I2Cx_ER_IRQn, 1, 0);
  HAL_NVIC_EnableIRQ(I2Cx_ER_IRQn);
  HAL_NVIC_SetPriority(I2Cx_EV_IRQn, 2, 0);
  HAL_NVIC_EnableIRQ(I2Cx_EV_IRQn);

 

 If your question is answered, please close this topic by clicking "Accept as Solution".
Thanks

Omar

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar