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!

You need to log in to post a comment