d. Bridging UART

<< Click to Display Table of Contents >>

Navigation:  Tutorials > Tutorail 20 - CleO Bridging >

d. Bridging UART

This tutorial shows how to use bridging commands to access UART device interface.Wifi3 click module (from MikroE) is used as UART slave device.

 

 

Command Prototype

 

int16_t DeviceOpen(uint8_t Slot,  uint8_t Interface,  int16_t bytestowrite,  uint8_t* Buffer)

 

Parameters

Description

Slot

Currently only one Slot is supported.Slot value should be equal to 0.

Interface

Value of this parameter should be BRIDGE_TYPE_UART for UART interface access.

bytestowrite

Value of this parameter is the total number of bytes to be written through this function which in this case is sizeof uart_options structure.

Buffer

Value of this parameter should be a pointer to uart_options structure type in Bridge.h.

 

typedef struct PACK {

   union PACK {

       struct PACK {

           uint32_t BaudRate;

           uint16_t BufferSize;

           uint16_t RxTimeout; /* Timeout for Reads in mS(0xFFFF = infinite, ) */

           uint8_t PortID;

       };

       uint8_t b[7];

   };

}uart_options;

 

Structure member description:

 

BaudRate: The value of this parameter should be one of the enum value as shown below. The definition can be found in Bridge.h

 

typedef enum{

       b2400 = 2400,

       b4800 = 4800,

       b9600 = 9600,

       b19200 = 19200,

       b31250 = 31250,

       b38400 = 38400,

       b57600 = 57600,

       b115200 = 115200,

       b230400 = 230400,

       b460800 = 460800,

       b921600 = 921600,

   }uart_baud;

 

BufferSize: If this value is specified as 0, then the buffer size is assigned to UART_DEFAULT_BUFF_SIZE internally. If this value is a non-zero integer value, then buffer size is set to lower nearest power of 2. This value is capped at UART_DEFAULT_BUFF_SIZE.

 

RxTimeout: This is the timeout value used for UART reads and is in mS (milli seconds). This value can take numbers from 0 to INDEFINITE_BLOCKING and used for only OPT_BLOCKING UART read type.

INDEFINITE_BLOCKING is defined as 0xFFFF in Bridge.h and it means infinite wait.

If the UART read type is OPT_BLOCKING and RxTimeout is INDEFINITE_BLOCKING, then the UART read will block forever until requested number of bytes are read.

If the UART read type is OPT_BLOCKING and RxTimeout is not INDEFINITE_BLOCKING, then UART read will exit after the specified timeout.

 

PortID: The value of this parameter should be one of the enum value as shown below -

 

typedef enum

{

   CleO50_click_1 = 0,  /** Click 1 on Cleo50 board */

   CleO50_click_2 = 1,  /** Click 2 on Cleo50 board */

   CleO35_CN5 = 0,      /** CN5 connector on Cleo35 board */

} interfaces;

 

 

Return Value

Description

int16_t

On success, the function returns a valid handle that can be used in subsequent Device*() calls.

On failure, the function returns a negative value of error code with blue screen.

 

 

int16_t DeviceWrite(uint8_t DevHandle,  int16_t bytestowrite,  uint8_t* Buffer,  int16_t &byteswritten)

 

Parameters

Description

DevHandle

A UART handle number that is obtained using DeviceOpen() call.

bytestowrite

Value of this parameter is the total number of bytes to be written through this function

Buffer

Value of this parameter should be a pointer to type uint8_t and holds the data bytes to be written.

byteswritten

This variable will be updated with the total number of bytes written to CleO when the function returns.

 

Return Value

Description

int16_t

On success, the function returns 1 and on failure the function returns a negative value of error code with blue screen.

 

 

int16_t DeviceRead(uint8_t DevHandle,  int16_t bytestowrite,  int16_t bytestoread,  uint8_t* Buffer,  uint8_t* rBuffer)

 

Parameters

Description

DevHandle

A UART handle number that is obtained using DeviceOpen() call.

bytestowrite

Value of this parameter is the total number of bytes to be written to UART

bytestoread

Value of this parameter is the total number of bytes intend to read from UART

Buffer

Value of this parameter should be a pointer to type uint8_t.

This buffer holds the data to be written to UART. "bytestowrite" parameter refers to the number of bytes in this Buffer. For UART read, type of read whether OPT_NONBLOCKING or OPT_BLOCKING, needs to be mentioned in this parameter.

rBuffer

Value of this parameter should be read buffer pointer to which read result will be updated when the function returns.

 

Return Value

Description

int16_t

On success the function returns the number of bytes read from UART.

On failure the function returns a negative value of error code with blue screen.

 

 

int16_t DeviceClose( uint8_t DevHandle)

 

Parameters

Description

DevHandle

A UART handle that is obtained using DeviceOpen() call.

 

Return Value

Description

int16_t

On success, the function returns 1 and on failure the function returns a negative value of error code with blue screen.

 

 

 

Code

 

 

#define ESP_READ_TIMEOUT  ((uint16_t)1000u)

char GMR[] = "AT+GMR\r\n";

 

int16_t HandleUart = -1;

int16_t BytesWritten;

char Buffer[150];

 

void setup()

{    

 CleO.begin();

 Serial.begin(115200);

 Bridge::uart_options uoptions;  

 uoptions.BaudRate = Bridge::b115200;

 uoptions.BufferSize = UART_DEFAULT_BUFF_SIZE ;

 uoptions.RxTimeout = ESP_READ_TIMEOUT;

 uoptions.PortID = Bridge::CleO50_click_2;

 /* Open UART device using bridging commands */

 HandleUart = CleO.DeviceOpen(0, BRIDGE_TYPE_UART, sizeof(uoptions), (uint8_t*)&uoptions);

 if(HandleUart < 0 )

 {

  /* Failed to open a handle */

   Serial.print("DeviceOpen failed");

 }      

 else

 {  

  /* Write to UART device using bridging command */  

   CleO.DeviceWrite(HandleUart, strlen(GMR), (uint8_t *)GMR, BytesWritten);

   unsigned long start = millis();

   uint16_t rlen = 0;        

   uint8_t ropt = OPT_NONBLOCKING; /* OPT_NONBLOCKING enables reading available bytes from UART without blocking */

   bool IsResponseOK = false;

 

   while ( ((millis() - start) & ~0UL) < 10000)

   {  

    /* Read from UART device using bridging command */

     rlen += CleO.DeviceRead(HandleUart, sizeof(ropt), 0, &ropt, (uint8_t*)Buffer+rlen);                

     if( strstr(Buffer, "OK"))

     {              

       Serial.println(Buffer);

       IsResponseOK = true;              

       break;

     }      

   }      

   if(!IsResponseOK)

     Serial.print("Version Command Response NOT OK ");

   

 /* Close UART device using bridging command      

    */

   HandleUart = CleO.DeviceClose(HandleUart);

 }

}

 

void loop()

{

}

 

 

Description

 

A UART handle is obtained using DeviceOpen() to start communication with CleO WiFi module. An AT command requesting firmware version number is written to CleO WiFi module using UART handle. The response is read using DeviceRead().

 

 

Output

 

Upon executing the tutorial, depending on the success or failure, an appropriate message is displayed on the serial monitor window. For illustration purpose, a success message window is shown here in the picture.