10 years, 4 months ago.

HW implementation of SPI, I2C, Serial in mbed

Hello all,

I am new to the mbed compiler. I've noticed that with mbed library it is really easy to setup and use SW implementation of SPI, I2C and Serial.

I'd like to go further and use HW resources as those are there just for using them. I know that usage of HW resources is device-specific, but if we have certain set of mbed enabled devices we could implement libraries for each board separately.

Is it the only way for me to write my own lib for that HW interfaces or there is something already out there I haven't found yet?

Does anybody prepared such a library (any of mentioned IF) already so I could see it for reference?

best regards

1 Answer

10 years, 4 months ago.

These are all hardware implementations, which is why it only works on specific pins. The mbed library only adds an abstraction layer on top of it to use the same API for different devices. Of course this won't be as efficient as a direct implementation for your specific device. There are examples of such implementations on the site, but they also generally use some form of abstraction to simplify using them.

For SPI and Serial there are btw real software implementations which can work on any pin. I don't think there is one for I2C.

Accepted Answer

So as I understand mbed library implements SW interfaces without use of HW resources - right? If I am able to specify any of the pin in Serial pc(pX_a, pX_b, ...) it means that it is pure bit banging. If I would use for example Serial pc(MBED_SPI0) defined in PeripheralNames.h then i would think it might be implementation which uses HW resources because of well defined pins...

Do I understand it correctly?

posted by Grzegorz Kaczmarek 08 Jul 2014

The standard mbed library is only ever a HW implementation. If you try to enter

#include <mbed.h>
Serial pc(p1,p20); 

then it will compile but crash on start up and output an error message indicating invalid pins on the USB serial port. (unless your target device happens to have a hardware serial port on those pins)

If you wish to use a software implementation then you need to use a different library e.g. http://mbed.org/users/dtmort/code/SPI_flex/ is an SPI interface which is pure SW and will work on any pins.

posted by Andy A 08 Jul 2014

In addition to Andy's post, on the cookbook there are a bunch of software implementations for different peripherals: http://mbed.org/cookbook/Homepage, but this is not the standard one.

If you look at the platform page for your target, for example: https://mbed.org/platforms/KL25Z/, you see that you can only do Serial, SPI, etc on a limitted number of pins, because it is a real hardware implementation.

What the mbed library does is map the pins you supply to a hardware peripheral. If that is not possible it will return an error. This is also required since pretty much every ARM microcontroller has several pinmap options for a single peripheral. So just defining the peripheral would not be enough to know which pins it should use, but defining which pins it should use is enough to define the peripheral it should use.

posted by Erik - 08 Jul 2014