Page 1 of 1

i2c "not ready" What I do wrong?

Posted: Wed Sep 09, 2015 9:14 am
by Spider
Hello! At first: Sorry for my English.

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?

Re: i2c "not ready" What I do wrong?

Posted: Wed Sep 09, 2015 9:20 am
by Giovanni
Hi,

Is the state checker enabled in chconf.h?

Note that I2C has limitations on the F1, it is not possible to read less than 2 bytes.

Also make sure to read this article: http://wiki.chibios.org/dokuwiki/doku.p ... e_shooting

Giovanni

Re: i2c "not ready" What I do wrong?

Posted: Wed Sep 09, 2015 9:39 am
by Spider
Thx for quick response!

I2C work perfectly! (I don't know about "2 bytes" issue but now I reading all times more then 2 bytes)
Did I right understand sequience work with timers from threads with blocking "I-code"? may be I do something wrong?

All ChibiOS DEBUG flags is set.
CPU stops here if I don't check this first.

Code: Select all

osalDbgAssert(i2cp->state == I2C_READY, "not ready");

Re: i2c "not ready" What I do wrong?

Posted: Wed Sep 09, 2015 9:45 am
by Giovanni
It which function is that? the assertion tells you that the driver should be in the ready state but it isn't, see the state machine in the I2C driver documentation, that will explain you what brings the driver in the right state.

Giovanni

Re: i2c "not ready" What I do wrong?

Posted: Wed Sep 09, 2015 9:53 am
by Spider
From i2cMasterTransmitTimeout() witch call from touchWrite().
You mean that I must bring driver ro READY state manualy? How so?

Re: i2c "not ready" What I do wrong?

Posted: Wed Sep 09, 2015 9:58 am
by Giovanni
The state machine is here: http://chibios.sourceforge.net/docs3/ha ... _i2_c.html

1) Verify in which state you actually are.
2) Decide why you are not in the right state,

If you are in the locked state then you should read that troubleshooting guide I linked in the previous posts.

Giovanni

Re: i2c "not ready" What I do wrong?

Posted: Wed Sep 09, 2015 2:56 pm
by Spider
Thx.

1. Now state LOCKED with TIMEOUT result but I sure that sometimes state was UNINIT.... Strange...

Re: i2c "not ready" What I do wrong?

Posted: Wed Sep 09, 2015 3:34 pm
by Spider
Change code to:

Code: Select all

static msg_t touchWrite(I2CDriver *i2cd, uint8_t addr, uint8_t data) {
  uint8_t buffer[2] = {addr & 0x3F, data};

  msg_t status = MSG_OK;
  i2cAcquireBus(i2cd);
  while ((status = i2cMasterTransmitTimeout(i2cd, sxaddress >> 1, buffer, 2, NULL, 0, MS2ST(10)))==MSG_TIMEOUT)
  {
     i2cd->state = I2C_READY;
     chThdSleepMicroseconds(500);
  }
  i2cReleaseBus(i2cd);

  return status;
}


"Flight still normal"