About mbed
mbed is a tool for Rapid Prototyping with microcontrollers.
Take the tour...
Back to blog

Library Update: CAN and I2C

Version 24 of the library has gone live. Highlights are:

  • CAN additions
  • I2C Slave class
  • I2C, Ethernet bugfixes

CAN

  • The CAN class now has an attach() method, which is triggered on the reception of a CAN frame. This works in an identical manner to the attach() methods of other classes i.e. takes a function or member function which returns void, and has not parameters.
  • In addition there is a new method called monitor(bool silent), which puts the CAN interface into silent monitor mode. When the interface is in monitor mode, it can receive messages, but it will not ACK them, and cannot transmit.
  • Finally, the sampling point set by the frequency() method has been tweaked.

I2CSlave

A new I2CSlave class has been added. This uses the receive() poll method to see if the slave has been addressed, and can use both the multi-byte and single byte read() and write() methods as the I2C class. It can also distinguish if the slave has been directly addressed, or whether it has received the general call address:
#include 

I2CSlave slave(p9, p10);

int main() {
    char buf[10];
    char msg[] = "Slave!";

    slave.address(0x30);
    while (1) {
        int i = slave.receive();
        switch (i) {
            case I2CSlave::ReadAddressed:             // Master has performed a read on the slave
                slave.write(msg, strlen(msg) + 1);    // Includes null char
                break;
            case I2CSlave::WriteGeneral:              // Master has performed a general call write
                slave.read(buf, 10);
                printf("Read G: %s\n", buf);
                break;
            case I2CSlave::WriteAddressed:            // Master has performed an addressed write
                slave.read(buf, 10);
                printf("Read A: %s\n", buf);
                break;
        }
        for(int i = 0; i < 10; i++) buf[i] = 0;       // Clear buffer
    }
}

I2C bugfix

Version 23 introduced a bug into the reading of multiple bytes on I2C. Version 24 fixes this.

Ethernet bugfix

There was a small bug in re-initialisation of the Ethernet class, which has been fixed.

Updating to the new Library

As usual, to get these updates for existing programs, simply click on the mbed library in your compiler project and choose "Update to latest revision!". New programs will automatically pull it in.

Any problems, suggestions or thumbs ups, please tell us in the Bugs/Suggestions forum!

Comments

14 Jul 2010

These changes look great Jon.

However, as with your previous SPISlave class, the documentation for the new I2CSlave didn't make it into the "Handbook", although I can see it in the compiler when I expand the mbed library. I think there are a couple of others that aren't shown in the Handbook also.

Any chance of an update to the Handbook also, so you don't have to launch the compiler just to see the details of these new classes?

14 Jul 2010

Yes cheers Jon, the last update was great too. CAN worked on the pins that weren't brought out on the MBED! :). And this attach function should come in handy too. Question Is this attach method polling or interrupt based?

29 Aug 2010

Hi Jon,

The latest library version 25, the attach function doesn't work using CAN can(P0_21, P0_22)

but works fine using CAN can(P2_7, P2_8) . Everything else seems ok, I can read and send, but just doesnt generate a attach interrupt apon recieving a message. Yet P2_7,P2_8 works?

Thanks Jason

Please log in to post a comment.