I/O Retargeting  Version 1.2.0
User Code Templates for I/O Retargeting using ARM Compiler
 All Files Macros Pages
Retarget Input and Output via UART

Copy the USB Host Keyboard example application to your PC as described in Retarget Input via Keyboard and Output via Display.

Variant Selection

To change the retarget option for STDIN and STDOUT to USART, open the Manage Run-Time Environment window and set the Variants as shown here:

SelOutput.png
Select USART as Input and Output Channel

This will change the #defines in the RTE_Components.h file and thus execute another code section in retarget_io.c.

Add User Code Templates for the USART

Use the context menu in the Project window to add user code template files to the source code. Right-click on the group Source, select Add new Items to Group. Click on User Code Templates, expand the component Compiler, and add the templates for STDIN and STDOUT via USART.

SelTemplatesUART.png
Select Templates for UART Retargeting

This adds the files stdin_usart.c and stdout_usart.c to the group Source. The files contain predefined functions which need little modifications if you are using CMSIS-Driver for USARTs.

Use CMSIS-Driver USART

But first, enable the CMSIS-Driver in the Manage Run-Time Environment window:

CMSIS_Driver_USART.png
Enable the CMSIS-Driver USART

The Validation Output window might show related component dependencies. Click Resolve to automatically add the required components to your project.

Configure the USART Driver

Under Device, open the file RTE_Device.h and enable the correct USART (here: USART1). Configure the pins according to your target hardware (refer to the board documentation for further details; here PB6/PB7):

RTE_Device_h.png
Device Configuration using RTE_Device.h

Connect the Retargeting Functions to the Correct Driver Instance

Open stdout_USART.c and stdin_USART.c. Use the Configuration Wizard view to set the correct hardware interface (here: Driver_USART1) in both files:

Driver_Config_Wizard.png
Driver Configuration using the Configuration Wizard
Note
To have the full flexibility for retargeting different channels to different target hardware, each user code template contains a function to initialize the underlying hardware. This is fine as long as you are not using the same hardware for multiple channels.

Adapt the Code

In this example, the USART is used for STDIN and STDOUT simultaneously. Thus, one of the initialization functions need to be commented out and the content needs to be merged with the other one. Change to Text Editor mode for both files. In stdout_USART.c comment out the function stdout_init (lines 62 to 83):

//int stdout_init (void) {
// int32_t status;
// status = ptrUSART->Initialize(NULL);
// if (status != ARM_DRIVER_OK) return (-1);
// status = ptrUSART->PowerControl(ARM_POWER_FULL);
// if (status != ARM_DRIVER_OK) return (-1);
// status = ptrUSART->Control(ARM_USART_MODE_ASYNCHRONOUS |
// ARM_USART_DATA_BITS_8 |
// ARM_USART_PARITY_NONE |
// ARM_USART_STOP_BITS_1 |
// ARM_USART_FLOW_CONTROL_NONE,
// USART_BAUDRATE);
// if (status != ARM_DRIVER_OK) return (-1);
// status = ptrUSART->Control(ARM_USART_CONTROL_TX, 1);
// if (status != ARM_DRIVER_OK) return (-1);
// return (0);
//}

Copy the last call to the Control struct of the USART driver to the stdin_init function (line 62) in stdin_USART.c:

int stdin_init (void) {
int32_t status;
status = ptrUSART->Initialize(NULL);
if (status != ARM_DRIVER_OK) return (-1);
status = ptrUSART->PowerControl(ARM_POWER_FULL);
if (status != ARM_DRIVER_OK) return (-1);
status = ptrUSART->Control(ARM_USART_MODE_ASYNCHRONOUS |
ARM_USART_DATA_BITS_8 |
ARM_USART_PARITY_NONE |
ARM_USART_STOP_BITS_1 |
ARM_USART_FLOW_CONTROL_NONE,
USART_BAUDRATE);
if (status != ARM_DRIVER_OK) return (-1);
status = ptrUSART->Control(ARM_USART_CONTROL_RX, 1);
if (status != ARM_DRIVER_OK) return (-1);
status = ptrUSART->Control(ARM_USART_CONTROL_TX, 1);
if (status != ARM_DRIVER_OK) return (-1);
return (0);
}

The stdin_init() function needs to be called in main. Change the following line in the Keyboard.c file:

extern int stdin_init (void);

In main(), replace stdout_init() with stdin_init():

:
SystemClock_Config(); /* Configure the System Clock */
stdout_init(); /* Initialize printf output */
ADC_Initialize (); /* Initialize A/D converter */
:

Remove old Template Files

Before building the project, remove the old user code templates from the target build. Right-click the files stdin_keyboard.c and stdout_display.c, select Options for File, and disable Include in Target Build:

FileOptions.png
Remove Unnecessary User Code Template Files from Target Build

Run the Application

Build the project, configure for debugging, and download the application to the development board. Connect the development board with an RS232 cable to a PC. Open a terminal program with the correct COM port setting and observe that a menu is displayed. Enter a command via your keyboard and test the application:

Terminal_Output.png
Input and Output on the Terminal with MCB1800
Note
If you do not see any output on the terminal, check the baud rate settings (9600 baud), COM port number, or verify that hte correct USB driver is used (in case you have a USB-Serial converter attached to the PC).