This article will guide you through the steps required in order to communicate using serial protocol via USB on Synergy devices. Several common actions have been described briefly as this guide assumes basic familiarity with e2studio and Synergy toolchain. Feel free to refer to Starting Development section of the <SSP User's Manual> for more detailed information about setting up your workspace and running first project.
Following steps listed in this guide will allow you to send and receive data using Serial USB emulation. Implementing USBX with Communications Framework in your project and using provided APIs will enable your Synergy board to be recognized as serial communications device. Upon completion of this example, the device can be connected to the serial monitor of your choice (i.e. PuTTY) in order to send and receive data.
You must first have the Synergy software tool flow installed and a target Synergy board available to communicate via USB. If you don't, follow the below link to the SSP User's Manual, and read the SSP Overview and Starting Development sections.
<Synergy Software Platform User's Manual>
Create Your Project
If you followed the previous step you should have the Synergy Platform tool flow working and you should have exercised your target board. Now you are ready for a ThreadX based example using the USBX and Communications Framework.
- Open e2 studio and create a new Synergy Project (refer back to the SSP User's Manual if you need a reminder).
- Target your board and select the BSP option.
- Choose BSP template for foundation of this project.
Add required components
In this step you will configure your project to include components necessary for Serial USB emulation on Synergy.
- Open the Threads Tab in the Configuration Window.
- Add new thread to the project. This will enable ThreadX RTOS.
- In your new thread, add following modules:
- USBX on ux (found under Framework/USB)
- USBX Port HS and FS on sf_el_ux (Framework/USB)
- USBX Device Class CDC ACM on ux_device_class_cdc_acm (Framework/USB)
- Communications Framework on sf_el_ux_comms (Framework/Connectivity)
- Select USBX Port HS and FS in module list and navigate to Properties tab. Enable USBFS INT interrupt by selecting priority.
- Press the Generate Project Content button.
- You should now have a file called new_thread_entry.c (or similar, depending on the name given to the created thread) in the src directory.
- Open new_thread_entry.c add following code inside the while loop:
static uint8_t serial_in; g_sf_comms.p_api->read(g_sf_comms.p_ctrl, &serial_in, 1, TX_WAIT_FOREVER); g_sf_comms.p_api->write(g_sf_comms.p_ctrl, (uint8_t *) &"Renesas Synergy!\n\r", 18, TX_WAIT_FOREVER);
- Save the changes to the file and build the debug configuration of your project.
- The design should build without errors. If not, look carefully at your added code.You may have missed or added some characters. When your code builds without errors, you are ready to run serial USB communication on your target board.
Communicate using Serial USB
- Connect your board using 2 USB micro cables. One is used for the programmer and debugger, while the other is for serial communication.
- Initialize the Debugger and select your new project build configuration.
- Click the Debug button to bring up the debug perspective.
- The debug session will halt at the beginning of the design, so you need to press the Resume button twice to get it to begin execution on your board.
- Launch serial monitoring software of your choice. Make sure the valid COM port has been chosen that matches one assigned to your Synergy board (use Device Manager to verify that).
- Typing any character in the terminal window will result in board communicating back.
How Serial USB works
Code generated by Synergy configurator sets up the device to emulate serial transmission via USB. During thread setup (found inside synergy_gen folder), the framework is initialized with default settings for USB CDC. Once user code starts executing, the device awaits incoming data (1 byte) for unlimited time (TX_WAIT_FOREVER). Once the byte is received, it's stored in the serial_in variable and program moves on to write "Renesas Synergy!", followed by line break and carriage return, to the serial monitor. Again, the board waits for receiver to become available (TX_WAIT_FOREVER) before sending off the data.
Since there is no code for input interpretation, the program will proceed to write back regardless of the value received. As user code is placed inside a permanent while loop (while(1)), process described in previous paragraph is recurring indefinitely.
The key elements of Communications Framework API in this example are read and write:
g_sf_comms.p_api->read(g_sf_comms.p_ctrl, p_dest, length, timeout);
g_sf_comms.p_api->write(g_sf_comms.p_ctrl, p_source, length, timeout);
Both functions require similar arguments: pointer to the framework control structure, followed by a pointer to data source/destination, length of the data item (in bytes) and timeout period (eg. TX_NO_WAIT or TX_WAIT_FOREVER). For more detail on how to use this API please refer to Synergy Software Package User Manual page 534.
Now that you have completed this example design here are some suggestions for building on this exercise to extend your knowledge and skills with the Synergy Platform.
- Try extending your solution by adding code responsible for interpreting received data.
- Send back received characters to the monitor to create basic text editor.
- Expand your knowledge of ASCII character table and make use of various control commands (such as backspace, horizontal tab or escape sequences).
Please provide feedback on this article by using the feedback buttons below.