UART serial port

From openPicus Wiki
Jump to: navigation, search

Contents

What you are going to learn

This tutorial shows how to add and manage UARTs communication on Flyport.

Introduction to UART

The UART (or Serial communication) is an asynchronous serial communication module. Basically it constists of RX and TX pins, and the clock is built-in. For this reason, you have always to know the signal baudrate, otherwise the UART will not correctly read the messages.

How to use it

Flyport provides 4 independent hardware UARTs (3 for Flyport GPRS).
UART ports must be enabled by the IDE wizard. The wizard also allows to set the dimension of the buffers of each UART.

Danger.png the maximum memory used by UART buffers should not exceed 6144 byte.

The UART peripheral can be used to transmit data between devices, but also to send messages to the serial monitor of the IDE.
UART is very useful to debug your code: a debug messages before and after critical code sections could be a good practice in order to check your firmware.
The IDE wizard allows to set the UART1 as “TCP debug on UART1”. This configuration initializes the UART1 module at 19200 baud and turns it on at the startup.

The following example shows how to enable 3 UARTs ports (UART1 is for debug using the serial monitor). It's a simple closed loop: the message is sent by UART2 and received by UART3.
The message is also shown on UART1 for debugging purpose.

Note: You must enable the 3 UARTs in the IDE wizard before compiling the example

FlyportUARTPortWizard.png

Flyport PRO

Code example


The functions used in this tutorial are the following:

Name Function Description
 void IOInit(int io, int putval) Initializes the specified pin with the desired function. Remappable pins allow the user to set the pin not just as input or output, but also with advanced functionalities, as such UART or SPI.
 void UARTInit(int  port, long int  baud) Initializes the specified uart port with the specified baud rate.

Parameters: port The UART port to initialize. baud The desired baudrate.

 void UARTOn(int  port) After the initialization, the UART must be turned on with this command.

Parameters: port The UART port to turn on.

 void UARTWrite(int  port, char * buffer) Writes the specified string on the UART port.

Parameters: port The UART port to write to. buffer The string to write (a NULL terminated char array).

 void UARTFlush(int port) Flushes the buffer of the specified UART port.

Parameters: port The UART port to flush.

 int UARTBufferSize(int  port) Returns the RX buffer size of the specified UART port.

Parameters: port The UART port to read. Returns: The number of characters that can be read from the specified serial port.

 int UARTRead( int  port, char *  towrite, int  count) Reads characters from the UART RX buffer and put them in the char pointer "towrite" . Also returns the report for the operation.

Parameters: port The UART port to read. towrite The char pointer to fill with the read characters. count The number of characters to read Returns: The report for the operation:

  • n0: N characters correctly read.
  • n<0: N characters read, but buffer overflow detected.


Source code for taskFlyport.c:
Note: please remember to connect p10 and p12 to enable UARTs loopback!

#include "taskFlyport.h"
void FlyportTask()
{
    int i;
    // Disable all Analog channels
    for(i = 0; i < 9; i++)
        ADCDetach(i);
    ADCAttach(2);    // Enable only Analog 2 channel (pin p5)
 
    int count = 0, msglen = 0;
    char rcv_uart[200], uart_msg[200];
 
    // First of all: REMAP THE PINS
    IOInit(p10, UART3RX); // remap the p10 pin as UART3RX
    IOInit(p12, UART2TX); // remap the p12 pin as UART2TX
 
    //remember to enable the UARTs in the wizard
 
    UARTInit(2,19200);  //init the UART2 
    UARTOn(2);          //enable the UART2
    UARTInit(3,19200); //init the UART3
    UARTOn(3);         //enable the UART3
 
    while (1)
    {
        vTaskDelay(200);
        count++; 
        sprintf(uart_msg, "Iteration %i\r\n",count);
        UARTWrite(2, uart_msg);//write on UART2
        vTaskDelay(25);
        msglen = UARTBufferSize(3); //check buffer of UART3
        if (msglen > 0)
        {
            UARTRead(3, rcv_uart, msglen); // read from UART3
            rcv_uart[msglen] = '\0';
            UARTWrite(1, rcv_uart); //write on UART1
        }
    }
}


Flyport

Bill of Materials

  • 1x Flyport Module
  • 1x miniUSB programmer


Schematic

Uart schematic.png


Code example


The functions used in this tutorial are the following:

Name Function Description
 void IOInit(int io, int putval) Initializes the specified pin with the desired function. Remappable pins allow the user to set the pin not just as input or output, but also with advanced functionalities, as such UART or SPI.
 void UARTInit(int  port, long int  baud) Initializes the specified uart port with the specified baud rate.

Parameters: port The UART port to initialize. baud The desired baudrate.

 void UARTOn(int  port) After the initialization, the UART must be turned on with this command.

Parameters: port The UART port to turn on.

 void UARTWrite(int  port, char * buffer) Writes the specified string on the UART port.

Parameters: port The UART port to write to. buffer The string to write (a NULL terminated char array).

 void UARTFlush(int port) Flushes the buffer of the specified UART port.

Parameters: port The UART port to flush.

 int UARTBufferSize(int  port) Returns the RX buffer size of the specified UART port.

Parameters: port The UART port to read. Returns: The number of characters that can be read from the specified serial port.

 int UARTRead( int  port, char *  towrite, int  count) Reads characters from the UART RX buffer and put them in the char pointer "towrite" . Also returns the report for the operation.

Parameters: port The UART port to read. towrite The char pointer to fill with the read characters. count The number of characters to read Returns: The report for the operation:

  • n0: N characters correctly read.
  • n<0: N characters read, but buffer overflow detected.


Source code for taskFlyport.c:

#include "taskFlyport.h"
void FlyportTask()
{
    int count = 0,msglen = 0;
    char rcv_uart[200],uart_msg[200];
    // First of all: REMAP THE PINS
    IOInit(p11,UART3RX); // remap the p11 pin as UART3RX
    IOInit(p12,UART2TX); // remap the p12 pin as UART2TX
 
    //remember to enable the UARTs in the wizard
 
    UARTInit(2,19200);  //init the UART2 
    UARTOn(2);          //enable the UART2
    UARTInit(3,19200); //init the UART3
    UARTOn(3);         //enable the UART3
 
    while (1)
    {
	vTaskDelay(200);
       	count++; 
	sprintf(uart_msg, "Iteration %i\r\n",count);
	UARTWrite(2, uart_msg);//write on UART2
	vTaskDelay(25);
	msglen = UARTBufferSize(3); //check buffer of UART3
	if (msglen > 0)
	{
	    UARTRead(3, rcv_uart, msglen); // read from UART3
	    rcv_uart[msglen] = '\0';
	    UARTWrite(1, rcv_uart); //write on UART1
	}
    }
}
Personal tools
Namespaces

Variants
Actions
START HERE
DEVELOPMENT
HARDWARE INFO
RESOURCES
PHASED OUT
Toolbox