Created by Scott Schmit, last modified on Nov 13, 2013
Purpose
The Universal Asynchronous Receiver/Transmitter (UART) is one of the most common forms of communication in embedded applications. The purpose of this page is to provide a simple UART example for PSoC devices. PSoC Creator, which is a free development tool from Cypress for PSoC 3, 4, 5, and 5LP devices, was used for firmware development for this project. The PSoC4 Pioneer Kit was used as the platform for this demo. However, any PSoC device can use the code for this project.
Project Goals
The following goals will be accomplished in this project. If the user simply wants to browse through the application code for this project, feel free to skip ahead.
- Start a project from scratch in PSoC Creator
- Add/Configure a UART (SCB) component to the design
- Select which pins are used for UART communication
- Provide/explain UART application code
- Setup the hardware connections for UART communication with PC
- Setup the terminal software used by the PC
Project Requirements
The PSoC4 Pioneer Kit also includes an on-board PSoC5 device which can serve as a UART/USB bridge for this project. This will allow us to communicate with the PSoC4 device’s UART port using a USB port of the PC. This will not cause additional work on our part, since the firmware for the PSoC5 device is pre-programmed when the board ships. We will need to connect the PSoC4 and PSoC5 devices through the board’s headers using 2 jumper wires. The following tools were used for this project:
- PSoC Creator - Free firmware development software provided by Cypress. Version 2.2, Service pack 1 was used for this project.
- PSoC4 Pioneer Kit - Development platform for PSoC4 devices.
- Tera Term - Free terminal emulator software for PC.
Procedure
Start a project from scratch in PSoC Creator
- Open PSoC Creator
- Navigate to File → New → Project
- Select “Empty PSoC 4 (CY8C42*) Design”
- Name the project “UART_Example”, select a location to save the project, and click “Ok”.
- This will bring you to the schematic view of the project where you can click and drag desired components into your design. If you do not see a blank schematic page, double click “TopDesign.cysch” from the Workspace Explorer window.
Add/Configure a UART (SCB) component
- In the Component Catalog window, in the Communications folder, click and drag the “UART (SCB mode)” component onto the page.
- By default, the component will be named “UART_1”. Double click on the component to bring up the Configuration window. Replace the name “UART_1” with “PC_PSoC_UART”. In the UART Basic tab, verify the following settings:
- Mode: Standard
- Direction: TX + RX
- Baud Rate (kbps): 115200
- Data bits: 8 bits
- Parity: None
- Stop bits: 1 bit
- Oversampling 12
- Click “Apply” to apply any changes and click “Ok” to close the Configuration window.
Select pins for UART Communication
- In the Workspace Explorer window, click on “UART_Example.cydwr”. This is where you can define which pins of the PSoC device will be used for UART communication.
- You should see 2 signals called “PC_PSoC_UART:rx” and “PC_PSoC_UART:tx” with drop-down arrows next to them. The drop-down arrows can be used to select a Port/Pin for each signal. Select P4[0] for “PC_PSoC_UART:rx” and P4[1] for “PC_PSoC_UART:tx”.
- Save and close the “UART_Example.cydwr” tab.
- Build the project. This will auto-generate functions for the UART component which you can execute from application code.
UART Example Code
- In the Workspace Explorer window, open the “main.c” file for the project.
- Copy and Paste the following code into the “main.c” file:
#include <device.h>
void sys_init();
void main() {
uint32 rxData;
char test_string[] = "Hello World\n\r";
sys_init();
PC_PSoC_UART_UartPutString(test_string); // print test_string
for(;;) // endless loop
{
rxData = PC_PSoC_UART_UartGetChar(); // store received characters in temporary variable
if(rxData) { // make sure data is non-zero
PC_PSoC_UART_UartPutChar(rxData); // echo characters in terminal window
// Handle received characters
}
}
}
void sys_init (void) {
PC_PSoC_UART_Start();
// Add additional initialization code as desired
}
- Save and build the project.
Setup hardware connections
- Connect Pin10 of J3 to Pin10 of J8 on the Pioneer board.
- Connect Pin9 of J3 to Pin9 of J8 on the Pioneer board.
- The PSoC4 device is now connected to the PSoC5 device, which will convert the UART signal to a USB signal.
- Connect the board to an open USB port of the PC using a USB mini-B cable.
Setup PC Terminal Software
- Open a Tera Term window.
- Navigate to Setup → Serial Port
- Select the COM Port corresponding to the Pioneer board.
- Verify the following settings:
- Baud rate: 115200
- Data: 8 bit
- Parity: none
- Stop: 1 bit
- Flow control: none
- Transmit delays should be zero
- Click “OK” to close the Serial Port setup window.
Program and Test the Project
- In PSoC Creater, program the device (board must be connected to PC).
- In the Tera Term window, you should see “Hello World” printed.
- You should be able to type characters in the terminal and see them echoed back to you as shown below.
Questions/Comments
UARTs are one of the most common types of communication in embedded applications. I hope this example was helpful for you. I encourage you to modify different settings in the UART component and read through the available functions that PSoC Creator generates for the component. I challenge you to setup some sort of command prompt and execute various functions based on those commands. For questions or feedback about information on this or any other page, please go to the TechForum: TechForum Thanks for visiting the eewiki!