A code sample showing the use of the asynchronous Serial APIs.

Dependencies:   mbed

Usage Cautions

When utilizing the asynchronous api, there is one usage of the interrupt callback handler that worth notice when developing an application: timing sensitive commands are not supposedly to be executed within an interrupt

Let's take a look at this short example here:

/media/uploads/lichang/asyncdemo.png

The MCU is set to go into sleep mode when executing an asynchronous procedure.

When function sleep() is called, the current clock state is stored before the clock that is currently running is disabled. Then, a wait for interrupt function WFI() is executed to listen for any incoming interrupt requests while the execution of MCU is halted. These interrupts could be receiving data through serial, or a pin is set to high, etc. See the flow char here: /media/uploads/lichang/sleepfnflowchart.png

When a processor executs an interrupt handler or event callback function, we say that the system is in interrupt context.

For example, the processor is sleeping until it receives some serial data from UART (serial) to light up an LED. Lighting up an LED is done in the serial callback, and as such is in an interrupt context.

/media/uploads/lichang/serialcb.png

An interrupt is asynchronous with respect to the current main process. Interrupt context is time critical because the interrupt handler interrupts other code. So, it should be quick and simple, unless you really know what you are doing and all of the consequences.

Because of its asynchronous nature and the way the sleep API is built, there are certain functions that are not supposed to be called within an interrupt context. The major caveat is that when the interrupt routine calls some timing sensitive functions, the system core clock needs to be re-enabled in order for these functions to work properly. However, as shown in the flowchart above, the processor is still in interrupt context and has not yet gotten to execute the clock re-enable function. As a result, any timing sensitive execution should be avoided here, or you must manually re-enable the clocks. Such timing sensitive commands include but are not limited to: printf("content"), wait(number), serial.getc(),serial.putc(), spi.read(), spi.write(), i2c.read(), i2c.write(), ...


All wikipages