Library Update: Serial buses and ethernet enhancements.

Version 22 of the library is now live. Highlights are:

  • I2C repeated start
  • SPI half-duplex and slave classes
  • Serial half-duplex class
  • Ethernet link setting

I2C Repeated-Start

As requested in and in other places, repeated start is now supported.

The read() and write() methods of the I2C class now have an optional boolean parameter 'repeated'. It defaults to false (no repeated start) so the behaviour remains as before, but if you set this to true, then the stop bit is not set, and a repeated start is possible:

// CMPS03 - magnetic compass example

#include "mbed.h"

I2C compass(p28, p27);

int main() {
    char cmd[] = { 0x02 };
    char buf[2];


    while(1) {
        compass.write(0xC0, cmd, 1, true);   // Repeated start, buf, 2, false);   // No repeated start

        int d = (buf[0] << 8) + buf[1];
        float f = (float) d / 10.0;

        printf("Read: %f\n", f);

SPIHalfDuplex, SPISlave

There are now two new SPI classes: SPIHalfDuplex, and SPISlave.

The SPISlave allows the mbed to act as the slave half of a SPI master-slave relationship. Note that the chip select is required for this to work (pin 8, if using the pin (5, 6, 7) block, and pin 14 for the pin (11, 12, 13) block.

SPIHalfDuplex makes the mbed a master of shared data-in/data-out single line SPI system. This still requires both the MOSI and MISO pins, but they are simply connected together outside of the mbed; no additional tri-state buffers are needed. Also, because some of of these shared data line SPI-like devices return a different number of bits to that in the command, it has an extra method slave_format(int sbits) - which allows you to define the number of data bits in the response. An example of this is the MAXIM MAX6662 SPI thermometer, which is commanded by 8 bits, and replies with 16.


This new Serial class, like SPIHalfDuplex, allows serial communications over a shared single data line. Again, both pins are required for transmit and received, tied together externally, but no additional pulls or buffers are needed. All the usual baud/putc/getc/printf methods are available.

Ethernet Enhancements

By default, an ethernet device auto-negotiates the speed and duplex settings of the link. However, not all networking equipment supports this, so a new method, set_link(), allows you to force the speed/duplex settings of the link (or set it back to auto-negotiate).

API Documentation

There have also been a few minor documenation corrections, including the missing documentation for the "wait" functions (as pointed out in

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