e. Bridging SPI

<< Click to Display Table of Contents >>

Navigation:  Tutorials > Tutorial 20 - CleO Bridging >

e. Bridging SPI

This tutorial shows how to use bridging commands to access SPI device interface. MikroE 8X8 LED Click module is used as SPI 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_SPI for SPI 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 spim_options  structure.

Buffer

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

 

typedef struct PACK {

 union PACK {

  struct PACK {

   uint32_t Speed;  

   uint16_t ClockDivider; / Set to 0 to allow system to auto-select clock divider based on configured speed /  

   uint8_t SlaveSelectPin;

   uint8_t BusWidth;

   uint8_t Mode;

  };

  uint8_t b[9];

 };

} spim_options;

 

Structure member description:

 

SlaveSelectPin: his parameter value should be one of GPIO_SPIM_SS1 or GPIO_SPIM_SS3 definitions in Bridge.h as these are the chip selects supported on CleO50.

 

ClockDivider: If this value is zero then the divisor value is calculated to match the specified speed in Speed parameter of the structure. Else the ClockDivider value is directly used in SPI initialization.

 

BusWidth: This parameter value should be 1 as only single SPI transfer is supported.

 

Mode: This value should be one of the following enums as specified in Bridge.h

 

typedef enum

{

 spi_mode_0,  /**< CPOL = 0, CPHA = 0 */

 spi_mode_1,  /**< CPOL = 0, CPHA = 1 */

 spi_mode_2,  /**< CPOL = 1, CPHA = 0 */

 spi_mode_3   /**< CPOL = 1, CPHA = 1 */

} spi_clock_mode_t;

 

Speed: Maximum SPI data bit rate supported in CleO is 25MHz.The Speed parameter value will only be used to calculate ClockDivider value if ClockDivider is not specified by user in the structure. If ClockDivider value is specified then Speed parameter value will be ignored.

 

 

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 255.

 

 

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

 

Parameters

Description

DevHandle

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

bytestowrite

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

bytestoread

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

Buffer

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

This buffer holds the data to be written to I2C. "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.

 

 

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

 

Parameters

Description

DevHandle

A SPI 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 DeviceClose( uint8_t DevHandle)

 

Parameters

Description

DevHandle

A SPI 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

 

 

int16_t HandleSPI = -1;

 

void setup()

{

 Bridge::spim_options SpiOptions;  

 int16_t BytesWritten;

 uint8_t WriteBuf = 0x33;

 CleO.begin();

 Serial.begin(115200);

 

 SpiOptions.SlaveSelectPin = GPIO_SPIM_SS3;

 SpiOptions.ClockDivider = 0;

 SpiOptions.BusWidth = 1;

 SpiOptions.Mode = Bridge::spi_clock_mode_t::spi_mode_0;

 SpiOptions.Speed = 10000000;

/* Open SPI device using bridging commands */  

 HandleSPI = CleO.DeviceOpen(0, BRIDGE_TYPE_SPI, sizeof(SpiOptions), (uint8_t *)&SpiOptions );      

 if(HandleSPI < 0 )

 {  

  /* Failed to open a handle */

   Serial.print("DeviceOpen failed");

 }  

 else

 {      

 /* Write SPI device using bridging commands.

    * DeviceWrite responds 0 for failure and 1 for success

    */

   int16_t RetCode = CleO.DeviceWrite(HandleSPI,1,&WriteBuf,BytesWritten);

   if(RetCode < 0)

       Serial.println("Write to SPI failed");  

   else

       Serial.println("Write to SPI is successful");

 

 /* Close SPI device using bridging commands.    

    */

   HandleSPI = CleO.DeviceClose(HandleSPI);

   if(HandleSPI < 0 )

     Serial.println("DeviceClose failed");

 }          

}

 

void loop()

{  

}

 

 

Description

 

This tutorial opens a handle to SPI interface using  GPIO_SPIM_SS3 slave select pin. The communication is set for 10MHz in mode 0. The obtained handle is used to write 1 byte of data to SPI using DeviceWrite() call.

 

 

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.