Skip to main content
DS.4
Senior
January 1, 2024
Solved

I2C with DMA last byte error written to zero instead value

  • January 1, 2024
  • 5 replies
  • 2378 views

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

 

 

 

 

Best answer by Saket_Om

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

5 replies

DS.4
DS.4Author
Senior
January 1, 2024

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.
January 1, 2024

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

 

DS.4
DS.4Author
Senior
January 1, 2024

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.
January 1, 2024

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
January 29, 2024

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_OmBest answer
ST Technical Moderator
March 19, 2024

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

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Saket_Om