User I/O from mbed with CMSIS-DAP

Following on from my last posting regarding using native C/C++ on the mbed I have found that I currently cannot get output via the standard CMSIS ITM_SendChar function as used in the Cortex-M hard fault handler (I am currently in dialog with the guys at ARM trying to resolve this).

MdebHello

In the standard mbed environment, the mbed can communicate with a host PC through a “USB Virtual Serial Port” over the same USB cable that is used for programming using printf(), e.g.

#include “mbed.h”
int main()
{
printf(“Hello World!\n”);
}

To achieve the same output, an mbed SerialPC object can be defined, e.g.

#include “mbed.h”
Serial pc(USBTX, USBRX); // tx, rx
int main()
{
pc.printf(“Hello World!\n”);
}

Currently, with a native minimal project, the semi-hosting of printf is not supported. This can be overcome by “re-targeting the project”, so I’ll cover that in the future, but for now the is a simple way of getting basic user I/O.

User I/O via UART0

Luckily for us, if we push characters out over the UART0 serial interface they are transmitted via the same channel that the mbed SerialPC uses. To test this out I quickly (using the best agile techniques of course) put together a very basic UART driver.

As with many peripherals most of the work goes into getting the device initially configured. using CMSIS defines, the following code sets UART0 up for 9600 baud, 8–bits, No parity, 1 Stop bit (9600/8/N/1) based on a 25MHz Peripheral Clock.

SerialInit

Once the UART is configured we can then simply send and receive characters:

SerialTxRx

Finally we need a main to test our the code:

SerialInit

Note the use of “\n\r” to generate a carriage-return & line-feed.

My work PC sees the mbed Serial Port as COM8:

Devices

Once I’ve configured Tera Term and run the program, I get the following output:

Teraterm

So there you go, simple user I/O on the mbed-native.

One final thing, if, like me, you use Tera Term in VT mode, then you can use the ‘good old’ VT100 ESC sequences, for example:

putstring0(“33[2J”);        // VT100 Erase screen
putstring0(“33[H”);         // VT100 set cursor to HOME (0,0)
putstring0(“33[32m”);     // VT100 set colour to green – cool

Have fun…

Niall Cooling

Director at Feabhas Limited
Co-Founder and Director of Feabhas since 1995.
Niall has been designing and programming embedded systems for over 30 years. He has worked in different sectors, including aerospace, telecomms, government and banking.
His current interest lie in IoT Security and Agile for Embedded Systems.
Dislike (0)

About Niall Cooling

Co-Founder and Director of Feabhas since 1995. Niall has been designing and programming embedded systems for over 30 years. He has worked in different sectors, including aerospace, telecomms, government and banking. His current interest lie in IoT Security and Agile for Embedded Systems.
This entry was posted in ARM, C/C++ Programming, CMSIS, Cortex. Bookmark the permalink.

3 Responses to User I/O from mbed with CMSIS-DAP

  1. Bryce Schober says:

    Actually, carriage-return is '\r' and line-feed is '\n', so you really should be doing "\r\n".

    Like (0)
    Dislike (0)
  2. Niall says:

    Very true, thanks.

    Like (0)
    Dislike (0)
  3. Pingback: Sticky Bits » Blog Archive » Rehosting ARMCC for the mbed with CMSIS-DAP

Leave a Reply