Doubt on I2C LLD

ChibiOS public support forum for topics related to the Atmel AVR family of micro-controllers.

Moderators: utzig, tfAteba

carldong
Posts: 62
Joined: Wed Oct 15, 2014 6:08 pm
Has thanked: 1 time
Been thanked: 1 time

Doubt on I2C LLD

Postby carldong » Fri Mar 24, 2017 12:06 am

I am doubting the implementation of the AVR I2C LLD here. I run the testhal example, where the designated address is 0x50. However, on an oscilloscope, I found that 0xA0 was used as address. The IRQ is kind of big so I need some more time processing, but I do find a suspicious line:

Code: Select all

    TWDR = (i2cp->addr << 1);


Which is exactly what makes 0x50 to be 0xA0. I don't know why it is there (without digging into the TWI docs), but it looks like the source of problems.

BTW, this is a snippet that allows full frequency range of AVR TWI/I2C, I modified from the ChibiOS 16.something.

Code: Select all

void i2c_lld_start(I2CDriver *i2cp) {
  uint32_t clock_speed = 100000;
  // uint32_t clock_speed = 1000;
  /* TODO: Test TWI without external pull-ups (use internal) */


  if (i2cp->config != NULL)
    clock_speed = i2cp->config->clock_speed;

  uint32_t bdiv = ((F_CPU / clock_speed) - 16 ) / 2;
  if (bdiv < 256) {
    /* Configure prescaler to 1 */
    TWSR &= 0xF8;
    TWBR = bdiv;
  }
  else if (bdiv < 1024) {
    /* Prescale 4.*/
    TWSR &= 0xF9;
    TWSR |= 0x01;
    TWBR = bdiv / 4;
  }
  else if (bdiv < 4096) {
    TWSR &= 0xFA;
    TWSR |= 0x02;
    TWBR = bdiv / 16;
  }
  else {
    TWSR &= 0xFB;
    TWSR |= 0x03;
    TWBR = bdiv / 64;
  }
  /* Configure baudrate */
  // TWBR = ((F_CPU / clock_speed) - 16) / 2;
}

utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Location: Brazil
Has thanked: 1 time
Been thanked: 20 times
Contact:

Re: Doubt on I2C LLD

Postby utzig » Fri Mar 24, 2017 12:23 am

In I2C the address goes in the upper 7 bits and the LSb defines if it is a read or a write. So if you're reading 0xA0 you're actually sending to address 0x50.

carldong
Posts: 62
Joined: Wed Oct 15, 2014 6:08 pm
Has thanked: 1 time
Been thanked: 1 time

Re: Doubt on I2C LLD

Postby carldong » Fri Mar 24, 2017 4:36 am

utzig wrote:In I2C the address goes in the upper 7 bits and the LSb defines if it is a read or a write. So if you're reading 0xA0 you're actually sending to address 0x50.


That is exactly the problem. A write should give 0x50, while a read should give 0x51. Note that 0xA0 = 0x50 << 1. Also, there are multiple states in the IRQ, this action is performed on something about repeating(didn't see it for a few hours, so memory is blur). I have not located code for reading and writing data yet, but in my understanding, the left shift operation should not be executed when I am just trying to transmit.

carldong
Posts: 62
Joined: Wed Oct 15, 2014 6:08 pm
Has thanked: 1 time
Been thanked: 1 time

Re: Doubt on I2C LLD

Postby carldong » Wed Mar 29, 2017 12:21 am

I got it. Yeah, ChibiOS uses 7-bit addresses instead of the 8 given on my LCD datasheet. That is why there is the `<<1` operation... So I do need to manually convert address to 0x28.

utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Location: Brazil
Has thanked: 1 time
Been thanked: 20 times
Contact:

Re: Doubt on I2C LLD

Postby utzig » Wed Mar 29, 2017 12:26 am

That is not a ChibiOS thing. That is standard I2C. Your LCD manual may be providing the wrong address than. I'm curious, what LCD model is that? Where is the DS?

carldong
Posts: 62
Joined: Wed Oct 15, 2014 6:08 pm
Has thanked: 1 time
Been thanked: 1 time

Re: Doubt on I2C LLD

Postby carldong » Wed Mar 29, 2017 4:41 am

utzig wrote:That is not a ChibiOS thing. That is standard I2C. Your LCD manual may be providing the wrong address than. I'm curious, what LCD model is that? Where is the DS?


NHD-0420D3Z-NSW-BBW-V3 is the LCD. The specified I2C address ix 0x50. However, it is actually 0x28.

Also, a sample code I used (outside OS, that is) also uses 8-bit address.

Note that the oscilloscope I used also have the option to include R/W bit inside address. So I assume that this is a valid way to specify an address (the LCD DS specifically says the address "must be an even number", and it says "8-bit number").


Return to “AVR Support”

Who is online

Users browsing this forum: No registered users and 9 guests