MMC_SPI + I2C = stack overflow
Moderators: RoccoMarco, lbednarz, tfAteba
MMC_SPI + I2C = stack overflow
Hello,
I am trying to log some sensor data to SD card. I2C data reading alone is fine as I can see from serial console. MMC access works as well. But when I try to use I2C and MMC_SPI together I get stack overflow in a few seconds. Minimal settings that causes stack overflow is calling "mmcObjectInit" and "mmcStart" (not any other mmc related funcions) and then periodical reading of I2C data. When I remove "mmcStart" or I2C reading no overflow occurs. I suspect some issue with virtual timers as both "mmcStart" and "i2cMasterTransmitTimeout" uses them.
Currently I am using 2.4.3, but when I tried to change "os" folder to version 2.4.2 I can't reproduce stack overflow. Were there any related changes between the two versions? Maybe some bug has been introduced in new version.
Edit: You don't ever need to connect SD card, as there is no SD card communication in functions "mmcObjectInit" and "mmcStart".
Thanks for any help.
I am trying to log some sensor data to SD card. I2C data reading alone is fine as I can see from serial console. MMC access works as well. But when I try to use I2C and MMC_SPI together I get stack overflow in a few seconds. Minimal settings that causes stack overflow is calling "mmcObjectInit" and "mmcStart" (not any other mmc related funcions) and then periodical reading of I2C data. When I remove "mmcStart" or I2C reading no overflow occurs. I suspect some issue with virtual timers as both "mmcStart" and "i2cMasterTransmitTimeout" uses them.
Currently I am using 2.4.3, but when I tried to change "os" folder to version 2.4.2 I can't reproduce stack overflow. Were there any related changes between the two versions? Maybe some bug has been introduced in new version.
Edit: You don't ever need to connect SD card, as there is no SD card communication in functions "mmcObjectInit" and "mmcStart".
Thanks for any help.
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: MMC_SPI + I2C = stack overflow
Hi,
I doubt there is a problem with virtual timers themselves. Are you running with the debug options enabled in chconf.h? it is likely a stack overflow problem triggered by one of the changes to the I2C driver in 2.4.3.
Giovanni
I doubt there is a problem with virtual timers themselves. Are you running with the debug options enabled in chconf.h? it is likely a stack overflow problem triggered by one of the changes to the I2C driver in 2.4.3.
Giovanni
Re: MMC_SPI + I2C = stack overflow
Yes all options in debug section of chconf.h are enabled. Probably there is really some issue with I2C driver. I am using following wrapper for I2C communication.
When I reach unhandled exteption caused by stack overflow I see characters "I2" on serial console. These are from "chprintf((BaseChannel *)&USART, "I2C failed !!!\n");" line. But I have no idea why the i2c communication timeouts (on 2.4.2 I have no communication issues) and why I end in stack overflow.
Code: Select all
inline void i2cTransmit(
I2CDriver *i2cp,
i2caddr_t addr,
const uint8_t *txbuf,
size_t txbytes,
uint8_t *rxbuf,
size_t rxbytes)
{
msg_t msg = i2cMasterTransmitTimeout(i2cp, addr, txbuf, txbytes, rxbuf, rxbytes, I2C_TIMEOUT);
if (msg!=RDY_OK) {
chprintf((BaseChannel *)&USART, "I2C failed !!!\n");
const I2CConfig *config = i2cp->config;
i2cStop(i2cp);
I2C1->CR1 |= I2C_CR1_SWRST;
i2cStart(i2cp, config);
}
}When I reach unhandled exteption caused by stack overflow I see characters "I2" on serial console. These are from "chprintf((BaseChannel *)&USART, "I2C failed !!!\n");" line. But I have no idea why the i2c communication timeouts (on 2.4.2 I have no communication issues) and why I end in stack overflow.
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: MMC_SPI + I2C = stack overflow
If you specify TIME_INFINITE as timeout does something change?
Giovanni
Giovanni
Re: MMC_SPI + I2C = stack overflow
When I set TIME_INFINITE timeout the behavior is exactly the same. Error value returned from i2cMasterTransmitTimeout is always -2.
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: MMC_SPI + I2C = stack overflow
If it returns -2 then one or more errors occurred. About the stack overflow, you should give a bigger stack to the thread invoking the I2C function.
Giovanni
Giovanni
Re: MMC_SPI + I2C = stack overflow
I am quite sure that there is enough stack. If I remove MMC_SPI function calls there is more than 500 bytes of unused stack when reading I2C data.
As well when I break program execution in first few seconds before stack overflow, there is still around 500 bytes of unused stack. But then suddenly I2C transmit function fails and stack overflow occurs.
It takes 2-30 seconds to overflow and it appears to be random. Before that the program executes normally.
As well when I break program execution in first few seconds before stack overflow, there is still around 500 bytes of unused stack. But then suddenly I2C transmit function fails and stack overflow occurs.
It takes 2-30 seconds to overflow and it appears to be random. Before that the program executes normally.
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: MMC_SPI + I2C = stack overflow
Are those called from different threads or from the same thread? in which thread does the overflow happen? is it possible that is something else overflowing but the effect is only triggered under that condition?
I don't have enough info so I am unable to help further, the MMC driver and the I2C driver do not even interact each other.
Giovanni
I don't have enough info so I am unable to help further, the MMC driver and the I2C driver do not even interact each other.
Giovanni
Re: MMC_SPI + I2C = stack overflow
Everything is called from main thread. First there is I2C init, then there is part of MMC_SPI init (only "mmcObjectInit" and "mmcStart" calls) and in main loop there are some I2C data reads. No any other MMC_SPI calls are performed. I will try to strip my code down to minimal and post the whole project here.
Re: MMC_SPI + I2C = stack overflow
Probably I have found the error. I was trying to use I2CDriver.config pointer. Since I initialized I2C in my_init() function and allocated I2C config structure locally on my_init() function stack the I2CDriver.config pointer destination became undefined after the end of my_init() function.