Child pages
  • PSoC UART Example
Skip to end of metadata
Go to start of metadata

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

  1. Open PSoC Creator
  2. Navigate to File -> New -> Project
  3. Select "Empty PSoC 4 (CY8C42*) Design"
  4. Name the project "UART_Example", select a location to save the project, and click "Ok".
  5. 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

  1. In the Component Catalog window, in the Communications folder, click and drag the "UART (SCB mode)" component onto the page.
  2. 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
  3. Click "Apply" to apply any changes and click "Ok" to close the Configuration window.
Select pins for UART Communication
  1. 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.
  2. 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". 
  3. Save and close the "UART_Example.cydwr" tab.
  4. Build the project.  This will auto-generate functions for the UART component which you can execute from application code.
UART Example Code
  1. In the Workspace Explorer window, open the "main.c" file for the project.
  2. 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
    }
  3. Save and build the project.
Setup hardware connections
  1. Connect Pin10 of J3 to Pin10 of J8 on the Pioneer board.
  2. 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.
  3. Connect the board to an open USB port of the PC using a USB mini-B cable.
Setup PC Terminal Software

  1. Open a Tera Term window.
  2. Navigate to Setup -> Serial Port
  3. Select the COM Port corresponding to the Pioneer board.
  4. Verify the following settings:
    • Baud rate: 115200
    • Data: 8 bit
    • Parity: none
    • Stop: 1 bit
    • Flow control: none
    • Transmit delays should be zero
  5. Click "OK" to close the Serial Port setup window.
Program and Test the Project
  1. In PSoC Creater, program the device (board must be connected to PC).
  2. In the Tera Term window, you should see "Hello World" printed.
  3. 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.  If you have any interesting breakthroughs, questions, or comments, let me know at eewiki@digikey.com.  Thanks for visiting the eewiki!

 

  • No labels