a. Bridging GPIO

<< Click to Display Table of Contents >>

Navigation:  Tutorials > Tutorial 20 - CleO Bridging >

a. Bridging GPIO

This tutorial shows how to use bridging commands to access GPIO interface on CleO35. The setup uses CleOWifi module (which uses ESP8266MOD) as GPIO device. Chip Enable pin of ESP8266 is driven high using bridge GPIO commands.

 

 

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_GPIO for GPIO interface access.

bytestowrite

Value of this parameter is the total number of bytes to be written to CleO through this function which in this case is size of gpio_options structure.

Buffer

Value of this parameter should be a pointer to gpio_options structure type (as shown below) in Bridge.h.

 

typedef struct PACK {

 union PACK {

  struct PACK {

   uint32_t PinMap;

   uint32_t Direction;

   uint64_t PullUpCfg; // 4 possible states - none, up, down, keeper

  };

  uint8_t b[16];

 };

}gpio_options;

 

Structure member description:

 

PinMap: The value of this member should be in the form of Z_GPIO_PINMAP(gpio), where gpio is the order of the GPIO element in the GPIOMap table which is stored internally in CleO firmware. This order number for each GPIO element is macro defined in Bridge.h. In the below example, CLEO35_CN5_2 is the GPIO element connected to chip enable pin of ESP8266 and macro defined as 20 in Bridge.h. Hence the value of PinMap parameter should be Z_GPIO_PINMAP(CLEO35_CN5_2).

 

Direction: The value of this member should be in the form Z_GPIO_DIRECTION(gpio, direction) where gpio is the order of the GPIO element in the GPIOMap table and direction is one of the enum value as below -

 

typedef enum {

       Output,

       Input

}gpio_dir;.

PullUpCfg: The value of this parameter should be in the form of Z_GPIO_PULL_UP_CFG( gpio, cfg), where gpio parameter is the GPIO element order as defined (macro) in Bridge.h. In this case is CLEO35_CN5_2. Second parameter cfg has to be one of enum as shown below. This is defined in Bridge.h.

 

typedef enum

{

   pad_pull_none,      /**< No pull up or pull down */

   pad_pull_pullup,    /**< Weak pull up enabled */

   pad_pull_pulldown,  /**< Weak pull down enabled */

   pad_pull_keeper     /**< Weak pull up/down reflects output */

} pad_pull_t;

 

 

Return Value

Description

int16_t

On success, the function returns int16_t value which is the handle that can be used in subsequent Device*() calls. On failure the function returns 255.

 

 

 

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

 

Parameters

Description

DevHandle

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

bytestowrite

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

Buffer

Value of this parameter should be a pointer to type uint8_t

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 GPIO handle number that is obtained using DeviceOpen() call.

bytestowrite

Value of this parameter is the total number of bytes to be written to CleO. In this case this parameter value will be 0.

bytestoread

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

Buffer

Value of this parameter should be a pointer to type uint8_t. This buffer holds the data to be written to CleO. "bytestowrite" parameter refers to number of bytes in this Buffer.When "bytestowrite" is 0, this parameter should be NULL.

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

This function returns the number of bytes read from GPIO. On failure the function returns a negative value of error code with blue screen.

 

 

 

int16_t DeviceClose(uint8_t DevHandle)

 

Parameters

Description

DevHandle

A GPIO handle number 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 HandlePwdPin = -1;

int16_t BytesWritten;

char Buffer[150];

 

void setup()

{    

 CleO.begin();

 Serial.begin(115200);

 Bridge::gpio_options goptions;

 goptions.PinMap = Z_GPIO_PINMAP(CLEO35_CN5_2);

 goptions.Direction = Z_GPIO_DIRECTION(CLEO35_CN5_2, Bridge::Output);

 goptions.PullUpCfg = Z_GPIO_PULL_UP_CFG(CLEO35_CN5_2, Bridge::pad_pull_none);

/* Open GPIO device using bridging commands

    Enable Chip Enable pin of ESP8266

  */

 HandlePwdPin = CleO.DeviceOpen(0, BRIDGE_TYPE_GPIO, sizeof(goptions), (uint8_t*)&goptions);

 if(HandlePwdPin < 0)

 {

  /* Failed to open a handle */

   Serial.print("DeviceOpen failed");

 }

 else

 {

   uint32_t GpioVal;

   GpioVal = ((uint32_t)0x01 << CLEO35_CN5_2);  

 /* Write to GPIO device using bridging commands */

   CleO.DeviceWrite(HandlePwdPin, sizeof(GpioVal), (uint8_t *)&GpioVal, BytesWritten);

 }

 Bridge::uart_options uoptions;  

 uoptions.BaudRate = Bridge::b115200;

 uoptions.BufferSize = UART_DEFAULT_BUFF_SIZE ;

 uoptions.RxTimeout = ESP_READ_TIMEOUT;

 uoptions.PortID = Bridge::CleO35_CN5;

/* 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

 {  

   uint16_t rlen = 1;

  /* OPT_BLOCKING enables continuous read from UART until timeout occurs */

   uint8_t ropt = OPT_BLOCKING;

   while(rlen)      

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

   

  /* Write to UART device using bridging command */

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

   unsigned long start = millis();

   bool IsResponseOK = false;

   rlen = 0;        

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

 

   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);

 

  /* Close GPIO device using bridging command      

    */  

   HandlePwdPin = CleO.DeviceClose(HandlePwdPin);

 }

}

 

void loop()

{

}

 

 

Description

 

A GPIO handle is obtained using DeviceOpen() to operate on chip enable pin of CleO WiFi module. Using this handle, a high value is written to this gpio using DeviceWrite(). This enables the CleO WiFi module. A UART handle is obtained using DeviceOpen() to start the 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

 

On successful execution of the tutorial, the firmware version number/information is displayed on the serial monitor.

 

 

If the execution fails, an appropriate error message is displayed on the serial monitor.