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

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 http://mbed.org/forum/mbed/topic/586/ 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];

    compass.frequency(100000);

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

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

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

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.

SerialHalfDuplex

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 http://mbed.org/forum/helloworld/topic/788/).

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

03 Jun 2010

:D this is the SPI update ive been waiting for

 

Ive managed to work around the SPI half duplex issue in the past (working with the SP1000 barometric pressure sensor which has a DIO pin) but the slave thing will be very useful, especially for chaining mbeds together

03 Jun 2010

Thank you.  I am actually working on interfacing with a DS2482 I2C to 1-Wire bridge for which the repeated start has a direct application.  Your timing is great!!

Tim

03 Jun 2010

Great! Please tell us if it goes ok.

03 Jun 2010 . Edited: 03 Jun 2010

Hi Simon:

I have removed this as I was giving examples and saying it wasn't working with the repeated start.  I had clicked to update my environment, but not in the specific sheet I was working in.  I have something else to fix that I messed up trying to fix the problem that cropped up because I didn't do the update.

It looks like it's working.

If I still have a problem after I fix my fix, I'll post it.

Tim

05 Jun 2010

Could you post even if it does work? We didn't have that many devices that require I2C repeated start to hand, so knowing which devices worked (and which didn't) would be very useful.

27 Nov 2010 . Edited: 28 Nov 2010

 

I am trying to experiment with SPIHalfDuplex. I am getting...

"Identifier "SPIHalfDuplex" is undefined (E20)" in file "/main.cpp" on Line 5 Col 1

"Expected a ")" (E18)" in file "/main.cpp" on Line 5 Col 21

Has anybody successfully used SPIHalfDuplex?

 

OOPS! I figured it out. I needed to right click on the mbed folder on my project

and do an update library. Voila!

 

// Writes hex over SPI from pc input

#include "mbed.h"

SPIHalfDuplex spi(p11,p12,p13); // mosi, miso, sclk

DigitalOut cs(p23);

Serial pc(USBTX, USBRX); // tx, rx

int main() {
    spi.format(8,0);
    spi.frequency(1000000);
    spi.slave_format(9);

// deleted code

}

Please log in to post a comment.