a. Bridging GPIO

<< Click to Display Table of Contents >>

Navigation:  Tutorials > Tutorail 20 - CleO Bridging >

a. Bridging GPIO

This tutorial shows how to use bridging commands to access GPIO interface on CleO50. P6/ADC1 pin of CN18 and P8/ADC3 pin of CN15 connectors  are connected in loop-back fashion and signal driven on P6/ADC1 is read at P8/ADC3 and verified.

 

 

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 this case, CLICK2_AN is the P6/ADC1 pin of CN18 and macro defined as 12 in Bridge.h and CLICK1_AN is P8/ADC3 pin of CN15 which is defined to value 0 in Bridge.h. Hence the value of PinMap parameter should be Z_GPIO_PINMAP(CLICK2_AN) and Z_GPIO_PINMAP(CLICK1_AN)  for these 2 pins.

 

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, 2 pins will take the values CLICK2_AN  and CLICK1_AN. 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 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 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

 

 

int16_t HandlePin1 = -1;

int16_t HandlePin2 = -1;

 

void setup()

{    

 CleO.begin();

 Serial.begin(115200);

 Bridge::gpio_options goptions;

 goptions.PinMap = Z_GPIO_PINMAP(CLICK2_AN);

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

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

/* Open GPIO device using bridging commands */

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

 if(HandlePin1 < 0)

 {

  /* Failed to open a handle */

   Serial.print("DeviceOpen on Pin1 failed");

 }

 else

 {      

   goptions.PinMap = Z_GPIO_PINMAP(CLICK1_AN);

   goptions.Direction = Z_GPIO_DIRECTION(CLICK1_AN, Bridge::Input);

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

  /* Open GPIO device using bridging commands */

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

   if(HandlePin2 < 0)

   {

   /* Failed to open a handle */

     Serial.print("DeviceOpen on Pin2 failed");

   }

   else

   {

     uint32_t Pin1Val, Pin2Val = 0;

     int16_t BytesWritten;  

     int LoopCount = 4;

     uint32_t WriteValue = 0x1;                        

     

     while( LoopCount > 0)

     {

       Pin1Val = (WriteValue << CLICK2_AN);  

     /* Write to Pin1 using bridging commands */

       CleO.DeviceWrite(HandlePin1, sizeof(Pin1Val), (uint8_t *)&Pin1Val, BytesWritten);              

       delay(1000);

      /* Read from Pin2 using bridging command */          

       CleO.DeviceRead(HandlePin2, 0, 4, NULL, (uint8_t*)&Pin2Val);                      

       if(Z_BitIsSet(Pin2Val, (MASK_ONE_U32 << CLICK1_AN)) == WriteValue)              

         Serial.println("Successfully Read the value written!");

       else

         Serial.println("Failed to read the value written!");

     

       LoopCount --;

       WriteValue = !WriteValue;

       delay(1000);

     }

   }

 }

}

 

void loop()

{

}

 

 

Description

 

DeviceOpen() is called for CLICK2_AN GPIO pin to output a value on P6/ADC1 pin. Another DeviceOpen() is called for CLICK1_AN GPIO pin  to read from P8/ADC3 pin. The high value written to CLICK2_AN is read from CLICK1_AN and verified.

 

 

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.

 

.