f. Bridging SPI

<< Click to Display Table of Contents >>

Navigation:  Tutorials > Tutorail 20 - CleO Bridging >

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

 

 

/* MAX7219 based macros for configuration and registers */

#define DECODE_MODE                        9

#define INTENSITY                                  0xA

#define SCAN_LIMIT                                0xB

#define SHUTDOWN                                  0xC

#define DISPLAY_TEST                              0xF

 

#define SHDN_NORMAL_OPERATION          1

#define SHDN_SHUTDOWN                          0

#define NO_DECODE_MODE                        0

#define INTENSITY_MAX                          0x0F

#define INTENSITY_MID                        0x04

#define INTENSITY_LOW                          0x01

#define SCAN_ALL                                      0x7 /* enable all 8 digit lines, segments are controlled by the values in the digit register */

#define NORMAL_MODE                            0x00

 

int16_t HandleSPI = -1;

int16_t BytesWritten;

 

void setup()

{

 CleO.begin();

 Serial.begin(115200);

 

/* Open SPI device using bridging commands */

/* LED8x8 Click is connected to Click 1 and SPI configuration is Mode0, Single Channel, 10MHz */

 Bridge::spim_options SpiOptions;  

 SpiOptions.SlaveSelectPin = GPIO_SPIM_SS1;

 SpiOptions.ClockDivider = 0;

 SpiOptions.BusWidth = 1; /* only single SPI transfer supported */

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

 SpiOptions.Speed = 10000000;

 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

 {

  /* Success in opening SPI interface */                  

   delay(1000); /* Sufficient Delay for MAX7219 to bootup */

 

  /* Configure MAX7219 for mode, intensity, scan, shutdown */

   uint8_t d88[2] = { 0, 0 };

 

 /* Configure MAX7219 for no-decode operation */

   d88[0] = DISPLAY_TEST; d88[1] = NORMAL_MODE;

  /* Write SPI device using bridging commands */

   CleO.DeviceWrite(HandleSPI,2,d88,BytesWritten);

   d88[0] = INTENSITY; d88[1] = INTENSITY_LOW;

   CleO.DeviceWrite(HandleSPI,2,d88,BytesWritten);

   d88[0] = DECODE_MODE; d88[1] = NO_DECODE_MODE;

   CleO.DeviceWrite(HandleSPI,2,d88,BytesWritten);

   d88[0] = SCAN_LIMIT;  d88[1] = SCAN_ALL;

   CleO.DeviceWrite(HandleSPI,2,d88,BytesWritten);

   d88[0] = SHUTDOWN; d88[1] = SHDN_NORMAL_OPERATION;

   CleO.DeviceWrite(HandleSPI,2,d88,BytesWritten);

 

  /* Display a 'X' character on 8x8 */

   uint8_t X[8] = {0x00, 0xC6, 0xEE, 0x38, 0x38, 0xEE, 0xC6, 0x00};

 

   for (int i = 0; i <= 7; i++)

   {

     d88[0] = i + 1;   d88[1] = X[i];

     CleO.DeviceWrite(HandleSPI,2,d88,BytesWritten);

   }

 

  /* Close SPI device using bridging commands      

    */

   HandleSPI = CleO.DeviceClose(HandleSPI);

 }  

}

 

void loop()

{

}

 

 

Description

 

This tutorial opens a handle to SPI interface using GPIO_SPIM_SS1 slave select pin. The communication is set for 10MHz in mode 0. The obtained handle is used to communicate to 8x8 LED click.Several DeviceWrite() calls are used to initialize the LED click. Then, character X will be displayed using DeviceWrite().

 

 

Output

 

On successful execution of tutorial, character X will be displayed on 8x8 LED click. If DeviceOpen() fails, an error message will be displayed on to serial monitor window.