10 years, 3 months ago.

Can't get I2C working on my EA LPC4088 board

Hello, I'm quite new with mbed devices and I can't get the i2c working on my device. Scanning the i2c bus with the arduino UNO gives response, but my EA LPC4088 board don't seem to be able to connect.

I tried several different codes that should give me an output from the ATtiny slave device but nothing seems to work. So now I've put in a simple program in the ATtiny that I know works, but still no output, except from the arduino.

Can someone please write a simple example, to connect to this code:

#define F_CPU 8000000UL
 
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay_basic.h>
#include "usiTwiSlave.h"
 
#define I2C_SLAVE_ADDR  0x10
#define LED PB1
 
// Somewhere to store the values the master writes to i2c register 2 and 3.
static volatile uint8_t i2cReg2 = 0;
static volatile uint8_t i2cReg3 = 0;
 
// A callback triggered when the i2c master attempts to read from a register.
uint8_t i2cReadFromRegister(uint8_t reg)
{
    switch (reg)
    {
        case 0:
        return 10;
        case 1:
        return i2cReg2;
        case 2:
        return i2cReg3;
        default:
        return 0xff;
    }
}
 
// A callback triggered when the i2c master attempts to write to a register.
void i2cWriteToRegister(uint8_t reg, uint8_t value)
{
    switch (reg)
    {
        case 2:
        i2cReg2 = value;
        break;
        case 3:
        i2cReg3 = value;
        break;
    }
}
 
int main()
{
    // Set the LED pin as output.
    DDRB |= (1 << LED);
 
    usiTwiSlaveInit(I2C_SLAVE_ADDR, i2cReadFromRegister, i2cWriteToRegister);
    
    sei();
 
    while (1)
    {
        // This is a pretty pointless EXAMPLE which allows me to test writing to two i2c registers: the
        // LED is only lit if both registers have the same value.
        if (i2cReg2 == i2cReg3)
        PORTB |= 1 << LED;
        else
        PORTB &= ~(1 << LED);
    }
}

BR Johan

Just a recheck, are you aware that arduino uses 7bit address and mbed 8bit ? Some details for example here https://mbed.org/questions/3712/KL46z-as-I2C-Slave/, answer from Wim

posted by Martin Kojtal 02 Sep 2014

Hi Wim, Yes I've tried 0x14 with:

#include "mbed.h"
 
 
 
I2C i2c(p32, p31);
Serial pc(USBTX, USBRX); // tx, rx
 
const int addr = 0x14; //Slave 7bit adress is 0x10 Adress dont car if read or write, or? 
 
int main() {
    char ret[12]={             };
    while (1) {
        
        i2c.write(addr, 0x00, 1); //Sends command 0x00 which should put 0x0A in ouput buffer, or?  
 
        wait(0.5);
 
        i2c.read(addr, ret, 1);  //reads out 1 byte and puts it in cmd[0], or? (cmd[0]= 0x0A)
        
        pc.printf(ret); //prints value on screen, or?
        
        wait(5.0);
        
    }
}

As I understand it, the start and stop condition is all within that command, or?

BR Johan

posted by Johan Svensson 02 Sep 2014

"cmd[0]" should be ret[0]...

posted by Johan Svensson 02 Sep 2014

2 Answers

10 years, 3 months ago.

The I2C address on mbed side should be 0x20 as pointed out. The methods i2c.read and i2c.write as used above include the start and stop conditions. The 2nd parameter should be a pointer to the data or array of data that will be sent on received. Your example is wrong for the command 0x00. You should declare a byte, give it value 0x00 and use pointer to that value. <</code>> char data=0; i2c.write(address, &data,1); <<code>>

Accepted Answer

Thanks now the write command works, but the read command still gives me a headake. When giving the following read command:

i2c.read(address, &data, 1) // when data=0; should give me "10" or?

But that doesn't work. i get FF in reply. Is the approach totaly different in a read comand?

BR Johan

posted by Johan Svensson 02 Sep 2014

I tried writing and then a qiuck read after and it worked but it gives me a strange output.

char cmd=1;
        char data=1;
        //i2c.read(i, &data[0], 1
        i2c.write(addr, &cmd, 1);
        i2c.read(addr, &data, 1);;  //reads out 1 byte and puts it in cmd[0], or? (cmd[0]= 0x0A)
        wait(0.8);
        pc.printf(&data); //prints value on screen, or?

It somehow worked, but i get a secoundary printout like this " 10 ff" why do I get the ff?

BR JOhan

posted by Johan Svensson 02 Sep 2014

Printf is wrong. It expects a format string telling it how to display your data. Try something like pc.printf("value is 0x%x", data) to show data as hex number.

posted by Wim Huiskamp 02 Sep 2014

Think you ever so much Wim and everybody else.

BR Johan

posted by Johan Svensson 02 Sep 2014
10 years, 3 months ago.

The 8 Bit representation of your 7 Bit slave address 0x10 is not 0x14 but 0x10 << 1 = 0x20. mbed then uses 0x20 for write and 0x21 for read (it just ORs the address-value with 1 for reading).

Thanks, my mistake regarding the adress (By mistake I got the number from RoL of the expected answer), but is the other assumptions in the code correct? Also is the start/stop condition within the read and write command?

BR Johan

posted by Johan Svensson 02 Sep 2014