I/O Retargeting
Version 1.2.0
User Code Templates for I/O Retargeting using ARM Compiler
|
Copy the USB Host Keyboard example application to your PC as described in Retarget Input via Keyboard and Output via Display.
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:
This will change the #defines
in the RTE_Components.h file and thus execute another code section in retarget_io.c.
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.
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.
But first, enable the CMSIS-Driver in the Manage Run-Time Environment window:
The Validation Output window might show related component dependencies. Click Resolve to automatically add the required components to your project.
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):
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:
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):
Copy the last call to the Control struct of the USART driver to the stdin_init
function (line 62) in stdin_USART.c:
The stdin_init()
function needs to be called in main
. Change the following line in the Keyboard.c
file:
In main(), replace stdout_init()
with stdin_init()
:
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:
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: