Page 1 of 1

I2C Error handling

Posted: Tue Nov 17, 2015 4:40 pm
by steved
Something I've been meaning to mention for some time!

At present, if an I2C transaction fails, the driver is set into the I2C_LOCKED state. As far as I can see, the only way to recover from this is to do i2cStop() followed by i2cStart() - this needs handling either in every I2c call, or in a wrapper placed round the i2c transfer routines. This seems rather cumbersome to me, especially as there are use cases where a failure might sometimes be expected (e.g. scanning the bus to see what is present).

I suggest the following:
1. A (device dependent) i2cErrorClear() routine which cleans up after errors - possibly defaulting to i2cStop() followed by i2cStart(), but giving the option of doing something simpler.
2. A callback which is taken when an i2c error occurs. This would allow the main code to take a generic action as necessary. The called routine would also determine what action the driver should take - do nothing (i.e. lock), clear errors and return to normal state, etc.

I'm working on the I2C routines at present, so willing to implement the proposal.

Re: I2C Error handling

Posted: Tue Nov 17, 2015 5:29 pm
by Giovanni
The main problem is I2Cv1 which is very critical because HW "limitations".

You may try using I2Cv2 which is much more simple.

About your proposal, the I2C driver is currently callback-less, if possible I would not make an exception for this. What about a low-level setting?

Giovanni

Re: I2C Error handling

Posted: Tue Nov 24, 2015 11:57 am
by steved
Giovanni wrote:About your proposal, the I2C driver is currently callback-less, if possible I would not make an exception for this. What about a low-level setting?
Giovanni

I take your point - by 'low level setting' do you mean a #define somewhere, or a run-time call? If the latter, is there a driver with a suitable example?

Having said that, I have a use case where a callback would be needed on each received character, so that it can be examined to determine whether to stop the transaction.


FYI, I'm working on the I2C2 driver, trying to get slave mode working.

Re: I2C Error handling

Posted: Tue Nov 24, 2015 12:30 pm
by Giovanni
Hi,

Note that the locked state is only reached in case of timeout, other errors do not lock the driver. I agree on introduction of a new API for direct lock->ready transition, it should be optional, the switch exported by the LLD, this way it is not necessary to update all implementations.

In the LLD:

Code: Select all

#define I2C_SUPPORT_BUS_CLEAR TRUE/FALSE


In the HLD:

Code: Select all

#if !defined(I2C_SUPPORT_BUS_CLEAR)
#define I2C_SUPPORT_BUS_CLEAR FALSE
#endif


The callback for each received byte is not compatible with DMA implementations so it cannot be part of the API.

Giovanni

Re: I2C Error handling

Posted: Tue Nov 24, 2015 7:04 pm
by steved
I'll implement it that way in due course.

Giovanni wrote:The callback for each received byte is not compatible with DMA implementations so it cannot be part of the API.
Giovanni

Yes indeed - my plan is to force interrupt-driven receive when the character callback is present; set it to NULL and behaviour doesn't change.

One question I meant to ask earlier - have you tested the multi-block (i.e. >255 byte) transfer code in the I2CV2 driver? I couldn't see anywhere that the total byte count was explicitly decreased on each block transfer, but wondered whether it was inherent in the way the rest of the code worked?

Re: I2C Error handling

Posted: Tue Nov 24, 2015 8:29 pm
by Giovanni
Hi,

I never tested it, I just added that code to the driver but I don't have devices to test. The DMA counter is used so it decreased automatically.

About the callback, it could be added as a low level feature but not in the high level part.

Giovanni

Re: I2C Error handling

Posted: Tue Nov 24, 2015 8:39 pm
by steved
Giovanni wrote:I never tested it, I just added that code to the driver but I don't have devices to test. The DMA counter is used so it decreased automatically.
Giovanni

Thought that might be the case - I'll try and test while I'm playing with I2C.

I've got a little board with a DS1340 clock and a 32Kbyte FRAM on it - would be happy to send you one to help testing with real devices - PM me an address if you'd like one.

Re: I2C Error handling

Posted: Thu Nov 26, 2015 11:04 am
by steved
Have now tested the multi-block transfer, in both interrupt and DMA modes. Found that the RELOAD flag was never getting cleared.

Simple mod to a couple of routines:

Code: Select all

/**
 * @brief   I2C RX transfer setup.
 * @note   Configures transfer count and related flags
 *
 * @param[in] i2cp      pointer to the @p I2CDriver object
 *
 * @notapi
 */
static void i2c_lld_setup_rx_transfer(I2CDriver *i2cp) {
  I2C_TypeDef *dp = i2cp->i2c;
  uint32_t reload;
  size_t n;

  /* The unit can transfer 255 bytes maximum in a single operation (device constraint). */
  n = i2c_lld_get_rxbytes(i2cp);
  if (n > 255U) {
    n = 255U;
    reload = I2C_CR2_RELOAD;
  }
  else {
    reload = 0U;
  }

  /* Configures the CR2 registers with both the calculated and static
     settings. (Nothing much relevant in static settings - just PEC for SMBUS? */
  dp->CR2 = (dp->CR2 & ~(I2C_CR2_NBYTES | I2C_CR2_RELOAD)) | i2cp->config->cr2 | I2C_CR2_RD_WRN |
            (n << 16U) | reload;
}



/**
 * @brief   I2C TX transfer setup.
 * @note   Configures transfer count and related flags
 *
 * @param[in] i2cp      pointer to the @p I2CDriver object
 *
 * @notapi
 */
static void i2c_lld_setup_tx_transfer(I2CDriver *i2cp) {
  I2C_TypeDef *dp = i2cp->i2c;
  uint32_t reload;
  size_t n;

  /* The unit can transfer 255 bytes maximum in a single operation. */
  n = i2c_lld_get_txbytes(i2cp);            // Get transaction size from DMA or buffer as configured
  if (n > 255U) {
    n = 255U;
    reload = I2C_CR2_RELOAD;
  }
  else {
    reload = 0U;
  }

  /* Configures the CR2 registers with both the calculated and static
     settings.*/
  dp->CR2 = (dp->CR2 & ~(I2C_CR2_NBYTES | I2C_CR2_RELOAD)) | i2cp->config->cr2 |
            (n << 16U) | reload;
}

(Its the last line of each routine that's changed)

Have also got slave version of I2CV2 working; code's still very messy, and error handling not tested; happy to provide code if anyone wants to join in the testing.

Re: I2C Error handling

Posted: Thu Nov 26, 2015 11:33 am
by Giovanni
Hi,

Thanks for testing, I applied the patch to both trunk and 3.0.x.

Giovanni