I'm writing application for STM32F103 but this topic not about this.
After some time my application craches with exception "not ready" in attemp to read data from I2C. I has think that this is memory overflow, but can't find where.
That is example os my code:
Code: Select all
static const uint8_t sxaddress = 0x90;
static msg_t touchWrite(I2CDriver *i2cd, uint8_t addr, uint8_t data) {
uint8_t buffer[2] = {addr & 0x3F, data};
i2cAcquireBus(i2cd);
msg_t status = i2cMasterTransmitTimeout(i2cd, sxaddress >> 1, buffer, 2, NULL, 0, MS2ST(10));
i2cReleaseBus(i2cd);
return status;
}
static thread_t *TSThread = NULL;
static thread_reference_t *trp = NULL;
static void hapt_timer(void *p)
{
(void) p;
osalSysLockFromISR();
chThdResumeI((thread_reference_t *)&trp, (msg_t)0x1338); /* Resuming the thread with message.*/
osalSysUnlockFromISR();
}
static THD_WORKING_AREA(PollTSThreadWA, 256);
static THD_FUNCTION(PollTSThread, arg)
{
chRegSetThreadName("PollTS");
I2CDriver *i2cd = (I2CDriver *)arg;
virtual_timer_t hapt_vt;
chVTObjectInit(&hapt_vt);
bool inHapt = false;
while (TRUE)
{
/* Waiting for the IRQ to happen.*/
osalSysLock();
msg_t msg = chThdSuspendS((thread_reference_t *)&trp);
osalSysUnlock();
if (i2cd->state != I2C_READY) {
dmesg("not ready");
continue;
}
if ((msg == (msg_t)0x1338) || (inHapt && (!chVTIsArmed(&hapt_vt)))) {
touchWrite(i2cd, TSR_RegHapt2, 0); //OutPut value 0
inHapt = false;
}
if ((msg == (msg_t)0x1337) || (!palReadPad(GPIOC, GPIOC_TOUCH_IRQ)))
{
uint8_t IrqSrcStat[2] = {0, 0};
if (touchRead(i2cd, TSR_RegIrqSrc, IrqSrcStat, 2) != MSG_OK) continue;
if (IrqSrcStat[0] & PENTOUCHIRQ)
{
...
//Some coords calculaton code, clear no float Math
...
if (!chVTIsArmed(&hapt_vt)) {
touchWrite(i2cd, TSR_RegHapt2, 127); //OutPut value 127
osalSysLock();
chVTSetI(&hapt_vt,MS2ST(80),hapt_timer,&trp);
osalSysUnlock();
inHapt = true;
}
}
}
}
dmesg("Stopped");
touchWriteCMD(i2cd, TSC_CmdMan);
}
static void extcb5(EXTDriver *extp, expchannel_t channel) {
(void)extp;
(void)channel;
/* Wakes up the thread.*/
osalSysLockFromISR();
chThdResumeI((thread_reference_t *)&trp, (msg_t)0x1337); /* Resuming the thread with message.*/
osalSysUnlockFromISR();
}
static const EXTConfig extcfg = {
{
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_FALLING_EDGE | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOC, extcb5},
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_DISABLED, NULL},
{EXT_CH_MODE_DISABLED, NULL}
}
};
int main(void) {
halInit();
chSysInit();
....
i2cStart(&I2CD2, &i2cfg2_slow);
extStart(&EXTD1, &extcfg);
TSThread = chThdCreateStatic(PollTSThreadWA, sizeof(PollTSThreadWA), NORMALPRIO / 2, PollTSThread, driver);
....
}Code has next logic:
Thread wait for EXT INTERRUPT from TS Driver and after Interrupt raised read IRQ status from Chip by I2C bus.
If IRQ has TOUCH PRESS flag then begin Coords calculation and make One short Vibrate by writing command to chip.
Vibrate duration must be around 80 ms, so I turn Vibrate On and start timer for 80 ms. After Timer call event he resume Thread with specific code and Thread turn Vibrate off.
All work but one or twice times. After some repeats of this sequence I2CD2 crashes and make herself state NOT READY and my Thread looping in condition at loop begin.
If I disable Start Vibrate and Start Timer block all work Infinity.
I already trying remove all other code from Thread and leave only this blocks, but result is one - NOT READY after some time.
No one thread in application don't USE i2c except this.
What I do wrong?