I2C library in LPC11U68

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

I2C library in LPC11U68

2,452 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by rubensm on Sun Jan 04 04:36:48 MST 2015
Hi,

I'm new with LPC devices and I have some doubts about the use I2C protocol on these boards. I've read all the stuff related with I2C in the datasheet, but I don't know if there exists a encapsulated library or it must be the developer who have to handle all the I2C states using software.

I've been looking for info in the forum and searching in the Internet, but I haven't been able to clarify this issue. In the example files there is the file "i2c_11u6x.c", but it is enough to have a communication as master and as slave? Is it so simple as, for example, use the function
Chip_I2C_MasterTransfer(I2C_ID_T id, I2C_XFER_T *xfer)
to write in the slave or
Chip_I2C_MasterRead(I2C_ID_T id, uint8_t slaveAddr, uint8_t *buff, int len)
for a slave writing?

There is very little information about I2C and LPC, so I would be very greatful if someone could help me.

Thank you,
Rubén.
Labels (1)
0 Kudos
8 Replies

1,807 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by rubensm on Thu Jan 08 15:54:34 MST 2015
Just another thing. I've added I2C1 functionality in the 'periph_i2c' example project since it wasn't still implemented.

The new code affects in the functions Init_I2C_PinMux, i2c_set_mode and i2c_app_init and the new one I2C1_IRQHandler


static void Init_I2C_PinMux(I2C_ID_T id)
{
switch(id){
case I2C0:
Chip_SYSCTL_PeriphReset(RESET_I2C0);
Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 4,
(IOCON_FUNC1 | I2C_FASTPLUS_BIT) | IOCON_DIGMODE_EN);
Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 5,
(IOCON_FUNC1 | I2C_FASTPLUS_BIT) | IOCON_DIGMODE_EN);

break;
case I2C1:
Chip_SYSCTL_PeriphReset(RESET_I2C1);
Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 7,
IOCON_FUNC3 | IOCON_DIGMODE_EN);
Chip_IOCON_PinMuxSet(LPC_IOCON, 1, 24,
IOCON_FUNC2 | IOCON_DIGMODE_EN);
break;
}
}


static void i2c_set_mode(I2C_ID_T id, int polling)
{
if (!polling) {
mode_poll &= ~(1 << id);
Chip_I2C_SetMasterEventHandler(id, Chip_I2C_EventHandler);
if(id == I2C0)
NVIC_EnableIRQ(I2C0_IRQn);
else if(id == I2C1)
NVIC_EnableIRQ(I2C1_IRQn);
}
else {
mode_poll |= 1 << id;
if(id == I2C0)
NVIC_DisableIRQ(I2C0_IRQn);
else if(id == I2C1)
NVIC_DisableIRQ(I2C1_IRQn);
Chip_I2C_SetMasterEventHandler(id, Chip_I2C_EventHandlerPolling);
}
}


static void i2c_app_init(I2C_ID_T id, int speed)
{
Init_I2C_PinMux(id);

/* Initialize I2C */
Chip_I2C_Init(id);
Chip_I2C_SetClockRate(id, speed);

/* Set default mode to interrupt */
i2c_set_mode(id, 0);
}


void I2C1_IRQHandler(void)
{
i2c_state_handling(I2C1);
}


I2C0:
[list]
  [*]SDA: PIO0_5
  [*]SCL: PIO0_4
[/list]
I2C1:
[list]
  [*]SDA: PIO1_24
  [*]SCL: PIO0_7
[/list]

I attach my whole "periph_i2c.c". It's been tested with NXP LPC11U68, where the LPC11U68 is the master and Arduino UNO is the slave. Polling mode has not been tested. Please, note that the file is a custom version of i2c_periph without iox emulation nor rom emulation. It just sends through I2C0 or I2C1 pins the bytes defined in the buffer as master. I hope it is useful for someone.


Rubén.
0 Kudos

1,807 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by rubensm on Thu Jan 08 14:07:09 MST 2015
That was the mistake! I did copy and paste and I forgot to change it. I was actually sending zeros, that in ASCII are the NULL character.

Now it works perfectly!

Thank you very much both R2D2 and capiman!  :D   :D   :D
0 Kudos

1,807 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by capiman on Thu Jan 08 13:26:23 MST 2015
xfer.slaveAddr = 4; //Slave address 4
xfer.txSz = 10;//10 bytes to send
xfer.[color=#f30]txBuff[/color] = buffer[0];
xfer.rxSz = 0;//0 bytes to send
xfer.[color=#f30]txBuff[/color] = 0;//Flush del buffer
tmp = Chip_I2C_MasterSend(i2cDev, xfer.slaveAddr, xfer.txBuff, xfer.txSz);

Is it correct that you use both times txBuff?
Can you print the 10 bytes in hex?
0 Kudos

1,807 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Thu Jan 08 13:03:18 MST 2015
You are printing (with 9600 bps  :D ) in your receive event  :quest:

Probably that's no LPC problem  :((

Could be a good idea to:

- use an oscilloscope or logic analyser to read your I2C data...

- send a shorter message and use a delay between messages...

- store received data in a buffer instead using a slow printing...

0 Kudos

1,807 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by rubensm on Thu Jan 08 12:45:51 MST 2015
It seems that it's working with the 'periph_i2c', but I have a problem:

I'm sending all the time the same:
buffer[0][0] = 0x48;//H
buffer[0][1] = 0x6F;//o
buffer[0][2] = 0x6C;//l
buffer[0][3] = 0x61;//a
buffer[0][4] = 0x20;//_
buffer[0][5] = 0x6D;//m
buffer[0][6] = 0x75;//u
buffer[0][7] = 0x6E;//n
buffer[0][8] = 0x64;//d
buffer[0][9] = 0x6F;//o

xfer.slaveAddr = 4; //Slave address 4
xfer.txSz = 10;//10 bytes to send
xfer.txBuff = buffer[0];
xfer.rxSz = 0;//0 bytes to send
xfer.txBuff = 0;//Flush del buffer
tmp = Chip_I2C_MasterSend(i2cDev, xfer.slaveAddr, xfer.txBuff, xfer.txSz);


and what I'm receiving in the Arduino is: '     -  q   5 '. They are 10 characters but not the corresponding ones.

I've attached a screenshot for clarity.

Thank you very much for your help
0 Kudos

1,807 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Mon Jan 05 06:16:54 MST 2015

Quote: rubensm
The code is attatched as I am neither able to post it nor post a link to pastebin.



That's a special forum 'feature'  :((


Quote: rubensm
Am I initiliazing improperly the I2C interface?



Is this a LPCOpen sample?

Usually there's a good i2c sample 'periph_i2c' which is showing how to communicate...


Quote: rubensm
I'm trying to communicate with an Arduino UNO in order to test the proper work of the communication but it is being fruitless.



I'm not sure what communication your Arduino is expecting in detail...
0 Kudos

1,807 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by rubensm on Mon Jan 05 05:34:03 MST 2015
Thank you very much for your reply.

I'm trying to communicate with an Arduino UNO in order to test the proper work of the communication but it is being fruitless.
The LPC is the master and Arduino the slave with the address 0x12 and about the hardware, I'm using pull-up resistors (10k) in SDA and SCL.

The code is attatched as I am neither able to post it nor post a link to pastebin.

Am I initiliazing improperly the I2C interface?

Thank you very much for your help!

Rubén

0 Kudos

1,807 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Sun Jan 04 05:04:59 MST 2015
In Chip_I2C_Master... functions your LPC is the master.

Usually there's a Chip_I2C_MasterRead function to read from a slave and a Chip_I2C_MasterSend function to write to a slave. This functions require a correct slave address and a buffer to read / write. They are both using Chip_I2C_MasterTransfer.

To start with I2C it's just necessary to init I2C correct and use Chip_I2C_MasterRead / Chip_I2C_MasterSend functions (as shown in example) and of course a working hardware  :)

0 Kudos