SPI Library

The SPI module is available with all ARM MCUs. mikroC PRO for ARM provides a library for initializing the Slave mode and initializing and comfortable work with the Master mode. The ARM can easily communicate with other devices via SPI: A/D converters, D/A converters, MAX7219, LTC1290, etc.

  Important :

Library Routines

Generic Routines

SPIx_Init

Prototype

void SPIx_Init();

Description

Configures and initializes the SPI module with default settings.

Default settings for Stellaris MCUs :

  • Master mode.
  • 8-bit data mode.
  • Serial clock set to 2 MHz.
  • Clock idle state low.
  • Serial output data changes on the first clock edge transition.

Default settings for MSP432 MCUs :

  • Master mode.
  • 8-bit data mode.
  • Low-speed subsystem master clock as clock source.
  • Serial output data changes on the first clock edge transition.
  • MSB sent first.

Default settings for ST MCUs :

  • Master mode.
  • 8-bit data mode.
  • Serial clock set to Peripheral clock/64.
  • Clock idle state low.
  • Serial output data changes on the first clock edge transition.
  • Slave Select output disabled.
  • Software Slave Management disabled.

Default settings for CEC1x02 :

  • Master mode.
  • 8-bit data mode.
  • Serial clock set to 1 MHz.
  • Clock idle state low.
  • Serial output data changes on the first clock edge transition.
  • MSB sent first.

Default settings for Kinetis :

  • Master mode.
  • 8-bit data mode.
  • Serial clock set to 8 MHz.
  • Clock idle state low.
  • Serial output data changes on the first clock edge transition.
  • MSB sent first.
Parameters

None.

Returns

Nothing.

Requires

MCU must have the SPI module.

Example
// Initialize the SPI1 module with default settings
SPI1_Init();
Notes
  • SPI library routines require you to specify the module you want to use. To select the desired SPI module, simply change the letter x in the routine prototype for a number from 0 to 8.
  • Number of SPI modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • SPI_Rd_Ptr() and SPI_Wr_Ptr() global function pointers are assigned when a SPI module is initialized. They point to the appropriate read and write routines (determined by the initialized SPI module) so they can be used for SPI read and write operations.
  • Switching between the SPI modules in the SPI library is done by the SPI_Set_Active function (both SPI modules have to be previously initialized).
  • SPI modules are set on the following pins :

    Stellaris

    • SPI0 :
      • PA2/PA4/PA5 - all Cortex M3 and M4 MCUs.
    • SPI1 :
      • PB5/PE4/PE5 - TM4C129x Cortex M4 MCUs.
      • PD0/PD2/PD3 - all other Cortex M4 MCUs not listed before.
      • PE0/PE2/PE3 - all Cortex M3 MCUs.
    • SPI2 :
      • PG7/PG5/PG4 - TM4C129x Cortex M4 MCUs.
      • PH4/PH6/PH7 - LM4F112x, LM4F122x, LM4F132x, LM4F212x and LM4F232x Cortex M4 MCUs.
      • PB4/PB5/PB7 - all other Cortex M4 MCUs not listed before.
    • SPI3 :
      • PQ0/PQ2/PQ3 - TM4C129x Cortex M4 MCUs.
      • PH0/PH2/PH3 - LM4F112x, LM4F122x, LM4F132x, LM4F212x and LM4F232x Cortex M4 MCUs.
      • PD0/PD2/PD3 - all other Cortex M4 MCUs not listed before.

    MSP432

    • SPI1 :
      • PA1/PA2/PA3.
    • SPI2 :
      • PA9/PA10/PA11.
    • SPI3 :
      • PB1/PB2/PB3.
    • SPI4 :
      • PE5/PE6/PE7.
    • SPI5 :
      • PA5/PA6/PA7.
    • SPI6 :
      • PC11/PC13/PC12.
    • SPI7 :
      • PB5/PB6/PB7.
    • SPI8 :
      • PE9/PE10/PE11.

    STM32

    • SPI1 :
      • PA5/PA6/PA7 - all Cortex M0, M3 and M4 MCUs.
    • SPI2 :
      • PB10/PB14/PB15 - STM32F373x/STM32F378x/STM32F383x Cortex M4 MCUs.
      • PB13/PB14/PB15 - all Cortex M0, M3 and other Cortex M4 MCUs not listed before.
    • SPI3 :
      • PB3/PB4/PB5 - all Cortex M3 and M4 MCUs.
    • SPI4 :
      • PE2/PE5/PE6 - all Cortex M3 and M4 MCUs.
    • SPI5 :
      • PB0/PA12/PA10 - STM32F411x Cortex M4 MCUs.
      • PF7/PF8/PF9 - all other Cortex M4 MCUs not listed before.
    • SPI6 :
      • PG12/PG13/PG14 - all Cortex M4 MCUs.

    CEC1302

    • SPI0 :
      • P153/P164/P054

    CEC1702

    • SPI0 :
      • P034/P036/P001

    Kinetis

    • SPI0 :
      • PD1/PD3/PD2
    • SPI1 :
      • PD5/PD7/PD6
    • SPI2 :
      • PB21/PB23/PB22

SPIx_Init_Advanced

Prototype // for Stellaris MCUs :

void SPIx_Init_Advanced(unsigned long bit_rate_Hz, unsigned master_slave_mode, unsigned config);

// for Stellaris MCUs with alternative PORT functions on GPIO pins :

void SPIx_Init_Advanced(unsigned long bit_rate_Hz, unsigned master_slave_mode, unsigned config, const Module_Struct *module);

// for MCP432 MCUs :

void SPIx_Init_Advanced(int mst, int clkDiv, int clkSource, int msbFirst, int clkPhase, int clkPolarity, int spiMode, const Module_Struct *module);

// for ST MCUs :

void SPIx_Init_Advanced(char clock_divider, long config, const Module_Struct *module);

// for CEC1x02 :

void SPI0_Init_Advanced(unsigned long bit_rate_Hz, unsigned lsb_first, unsigned char config);

// for Kinetis :

void SPI0_Init_Advanced(unsigned long baudRate, unsigned long config, Module_Struct* module);

Description

Configures and initializes the SPI module with user defined settings.

Parameters

Stellaris

  • The bit_rate_Hz parameter determines the SPI clock value in Hz.
  • The master_slave_mode parameter determines the working mode for SPI module.
    Master/Slave mode
    Description Predefined library const
    Master mode _SPI_MASTER
    Slave mode _SPI_SLAVE
  • The parameter config determines the data size, clock polarity and clock phase.
    This value is deriven from OR-ing the following constants :
    Data Length Size
    Description Predefined library const
    4-bit data size _SPI_4_BIT
    5-bit data size _SPI_5_BIT
    6-bit data size _SPI_6_BIT
    7-bit data size _SPI_7_BIT
    8-bit data size _SPI_8_BIT
    9-bit data size _SPI_9_BIT
    10-bit data size _SPI_10_BIT
    11-bit data size _SPI_11_BIT
    12-bit data size _SPI_12_BIT
    13-bit data size _SPI_13_BIT
    14-bit data size _SPI_14_BIT
    15-bit data size _SPI_15_BIT
    16-bit data size _SPI_16_BIT

    Clock Polarity
    Description Predefined library const
    IDLE state is Lo, ACTIVE state is Hi _SPI_CLK_IDLE_LOW
    IDLE state is Hi, ACTIVE state is Lo _SPI_CLK_IDLE_HIGH

    Clock Phase
    Description Predefined library const
    Data is sampled on the first clock edge transition _SPI_FIRST_CLK_EDGE_TRANSITION
    Data is sampled on the second clock edge transition _SPI_SECOND_CLK_EDGE_TRANSITION
  • module: appropriate module pinout. Use Code Assistant to list available module pinouts by typing _GPIO_MODULE_SPI and pressing Ctrl + Space.

MSP432

  • The mst parameter determines the working mode for SPI module. Valid values :
    Description Predefined library const
    Master mode _SPI_MASTER
    Slave mode _SPI_SLAVE
  • The clkDiv parameter determines the working clock for SPI module. Valid values are from 1 to 65535.
  • clockSource: clock source for SPI module. Valid values :
    Description Predefined library const
    Auxiliary clock _SPI_CLOCKSOURCE_ACLK
    Low-speed subsystem master clock _SPI_CLOCKSOURCE_SMCLK
  • msbFirst: most/least significant bit first. Valid values :
    Description Predefined library const
    Most significant bit first _SPI_MSB_FIRST
    Least significant bit first _SPI_LSB_FIRST
  • clkPhase: clock phase. Valid values :
    Description Predefined library const
    Data is changed on the first clock edge and captured on the following edge _SPI_PHASE_DATA_CHANGE_FIRST_CAPTURE_NEXT
    Data is captured on the first clock edge and changed on the following edge. _SPI_PHASE_DATA_CAPTURE_FIRST_CHANGE_NEXT
  • clkPolarity: clock polarity. Valid values :
    Description Predefined library const
    The inactive state is low _SPI_CLK_IDLE_LOW
    The inactive state is high _SPI_CLK_IDLE_HIGH
  • spiMode: 3 wire or 4 wire SPI. Valid values :
    Description Predefined library const
    3-pin SPI _SPI_3PIN
    4-pin SPI with UCxSTE active high: Slave enabled when UCxSTE = 1 _SPI_4PIN_UCxSTE_ACTIVE_HIGH
    4-pin SPI with UCxSTE active low: Slave enabled when UCxSTE = 0 _SPI_4PIN_UCxSTE_ACTIVE_LOW
  • module: appropriate module pinout. Use Code Assistant to list available module pinouts by typing _GPIO_MODULE_SPI and pressing Ctrl + Space.

STM32

  • The clock_divider parameter determines the clock for SPI module (ST specifics).
    SPI Clock
    Description Predefined library const
    Peripheral Clock/2 _SPI_FPCLK_DIV2
    Peripheral Clock/4 _SPI_FPCLK_DIV4
    Peripheral Clock/8 _SPI_FPCLK_DIV8
    Peripheral Clock/16 _SPI_FPCLK_DIV16
    Peripheral Clock/32 _SPI_FPCLK_DIV32
    Peripheral Clock/64 _SPI_FPCLK_DIV64
    Peripheral Clock/128 _SPI_FPCLK_DIV128
    Peripheral Clock/256 _SPI_FPCLK_DIV256
  • The parameter config determines the data size, clock polarity and clock phase.
    This value is deriven from OR-ing the following constants :
    Data Length Size
    Description Predefined library const
    8-bit data size _SPI_8_BIT
    16-bit data size _SPI_16_BIT

    Master/Slave mode
    Description Predefined library const
    Master mode _SPI_MASTER
    Slave mode _SPI_SLAVE

    Clock Polarity
    Description Predefined library const
    IDLE state is Lo, ACTIVE state is Hi _SPI_CLK_IDLE_LOW
    IDLE state is Hi, ACTIVE state is Lo _SPI_CLK_IDLE_HIGH

    Clock Phase
    Description Predefined library const
    Data is sampled on the first clock edge transition _SPI_FIRST_CLK_EDGE_TRANSITION
    Data is sampled on the second clock edge transition _SPI_SECOND_CLK_EDGE_TRANSITION

    Bit Sending Hierarchy
    Description Predefined library const
    Most Significant Bit (MSB) sent first _SPI_MSB_FIRST
    Least Significant Bit (MSB) sent first _SPI_LSB_FIRST

    Software Slave Management
    Description Predefined library const
    Software Slave Management Enable _SPI_SSM_ENABLE
    Software Slave Management Disable _SPI_SSM_DISABLE

    Slave Select Output
    Description Predefined library const
    Slave Select Output Enable _SPI_SS_ENABLE
    Slave Select Output Disable _SPI_SS_DISABLE

    Internal Slave Select
    Description Predefined library const
    Internal Slave Select = 0 _SPI_SSI_0
    Internal Slave Select = 1 _SPI_SSI_1
  • module: appropriate module pinout. Use Code Assistant to list available module pinouts by typing _GPIO_MODULE_SPI and pressing Ctrl + Space.

CEC1x02

  • The bit_rate_Hz parameter determines the clock for SPI module in Hz.
  • The parameter lsb_first determines the bit numbering. Valid values:
    Description Predefined library const
    MSB sent first _SPI_MSB_FIRST
    LSB sent first _SPI_LSB_FIRST
  • The parameter config determines the clock polarity and clock phase. This value is deriven from OR-ing the following constants :

    Clock Polarity
    Description Predefined library const
    IDLE state is Lo, ACTIVE state is Hi _SPI_CLK_IDLE_LOW
    IDLE state is Hi, ACTIVE state is Lo _SPI_CLK_IDLE_HIGH

    Clock Phase
    Description Predefined library const
    Data is sampled on the rising clock edge _SPI_SAMPLE_DATA_RISING_EDGE
    Data is sampled on the falling clock edge _SPI_SAMPLE_DATA_FALLING_EDGE

Kinetis

  • The baudRate parameter determines the clock for SPI module (ST specifics).
  • The parameter config determines the data size, clock polarity and clock phase.
    This value is deriven from OR-ing the following constants :
    Data Length Size
    Description Predefined library const
    4-bit data size _SPI_CFG_FRAME_SIZE_4BITS
    5-bit data size _SPI_CFG_FRAME_SIZE_5BITS
    6-bit data size _SPI_CFG_FRAME_SIZE_6BITS
    7-bit data size _SPI_CFG_FRAME_SIZE_7BITS
    8-bit data size _SPI_CFG_FRAME_SIZE_8BITS
    9-bit data size _SPI_CFG_FRAME_SIZE_9BITS
    10-bit data size _SPI_CFG_FRAME_SIZE_10BITS
    11-bit data size _SPI_CFG_FRAME_SIZE_11BITS
    12-bit data size _SPI_CFG_FRAME_SIZE_12BITS
    13-bit data size _SPI_CFG_FRAME_SIZE_13BITS
    14-bit data size _SPI_CFG_FRAME_SIZE_14BITS
    15-bit data size _SPI_CFG_FRAME_SIZE_15BITS
    16-bit data size _SPI_CFG_FRAME_SIZE_16BITS

    Master/Slave mode
    Description Predefined library const
    Master mode _SPI_CFG_MASTER
    Slave mode _SPI_CFG_SLAVE

    Clock Polarity
    Description Predefined library const
    IDLE state is Lo, ACTIVE state is Hi _SPI_CFG_CLK_IDLE_LOW
    IDLE state is Hi, ACTIVE state is Lo _SPI_CFG_CLK_IDLE_HIGH

    Clock Phase
    Description Predefined library const
    Data is sampled on the first clock edge transition _SPI_CFG_CLK_PHASE_CAPT_LEADING
    Data is sampled on the second clock edge transition _SPI_CFG_CLK_PHASE_CHG_LEADING

    Bit Sending Hierarchy
    Description Predefined library const
    Most Significant Bit (MSB) sent first _SPI_CFG_MSB_FIRST
    Least Significant Bit (MSB) sent first _SPI_CFG_LSB_FIRST

  • module: appropriate module pinout. Use Code Assistant to list available module pinouts by typing _GPIO_MODULE_SPI and pressing Ctrl + Space.
Returns

Nothing.

Requires

MCU must have the SPI module.

Example

Stellaris

// Set SPI1 to the Master Mode, data length is 16-bit, clock = 2MHz, clock IDLE state high and data transmitted at the first clock edge transition :
SPI1_Init_Advanced(2000000, _SPI_MASTER, _SPI_16_BIT | _SPI_CLK_IDLE_HIGH | _SPI_FIRST_CLK_EDGE_TRANSITION);

// Set SPI1 to the Master Mode, data length is 16-bit, clock = 2MHz, clock IDLE state high and data transmitted at the first clock edge transition, on GPIO_PORTE :
SPI1_Init_Advanced(2000000, _SPI_MASTER, _SPI_16_BIT | _SPI_CLK_IDLE_HIGH | _SPI_FIRST_CLK_EDGE_TRANSITION, &_GPIO_MODULE_SPI1_E023);

MSP432

// Set SPI1 to the Master Mode, 3-pin interface, SMCLK divided by 6, MSB transmitted first, Data is changed on the first UCLK edge and captured on the following edge, inactive state is low :
SPI1_Init_Advanced(_SPI_MASTER, 6, _SPI_CLOCKSOURCE_SMCLK, _SPI_MSB_FIRST, _SPI_PHASE_DATA_CHANGE_FIRST_CAPTURE_NEXT, _SPI_CLK_IDLE_LOW, _SPI_3PIN, _GPIO_MODULE_SPIA0_A123);

STM32

// Set SPI1 to the Master Mode, data length is 8-bit, clock = Peripheral/64, clock IDLE state low and data transmitted at the first clock edge transition, MSB transferred first, Slave Select Disabled :
SPI1_Init_Advanced(_SPI_FPCLK_DIV64, _SPI_MASTER | _SPI_8_BIT | _SPI_CLK_IDLE_LOW | _SPI_FIRST_CLK_EDGE_TRANSITION | _SPI_MSB_FIRST | _SPI_SS_DISABLE | _SPI_SSM_ENABLE | _SPI_SSI_1, &_GPIO_MODULE_SPI1_PA567);

CEC1x02

// Set SPI0 with clock = 1MHz, MSB sent first, clock IDLE state low and data transmitted at the rising clock edge transition :
SPI0_Init_Advanced(1000000,  _SPI_MSB_FIRST, _SPI_CLK_IDLE_LOW | _SPI_SAMPLE_DATA_RISING_EDGE);

Kinetis

// Set SPI1 to the Master Mode, data length is 8-bit, clock = 8 MHz, clock IDLE state low and data transmitted at the first clock edge transition, MSB transferred first :
SPI1_Init_Advanced(8000000, _SPI_CFG_MASTER | _SPI_CFG_FRAME_SIZE_8BITS | _SPI_CFG_CLK_IDLE_LOW | _SPI_CFG_CLK_PHASE_CAPT_LEADING | _SPI_CFG_MSB_FIRST, &_GPIO_Module_SPI0_PD1_3_2);
Notes
  • SPI library routines require you to specify the module you want to use. To select the desired SPI module, simply change the letter x in the routine prototype for a number from 0 to 8.
  • Number of SPI modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • SPI_Rd_Ptr() and SPI_Wr_Ptr() global function pointers are assigned when a SPI module is initialized. They point to the appropriate read and write routines (determined by the initialized SPI module) so they can be used for SPI read and write operations.
  • Switching between the SPI modules in the SPI library is done by the SPI_Set_Active function (both SPI modules have to be previously initialized).
  • For available working modes for a specific MCU please read the appropriate datasheet.

SPIx_Read

Prototype

unsigned SPIx_Read(unsigned long buffer);

Description

Reads one word from the SPI bus.

Parameters
  • buffer: dummy data for clock generation (see device Datasheet for SPI modules implementation details)
Returns

Received data.

Requires

Routine requires at least one SPI module.

Used SPI module must be initialized before using this function. See the SPIx_Init and SPIx_Init_Advanced routines.

Example
// read a byte from the SPI bus 
char take, buffer;
...
take = SPI1_Read(buffer);
Notes
  • SPI library routines require you to specify the module you want to use. To select the desired SPI module, simply change the letter x in the routine prototype for a number from 0 to 8.
  • Number of SPI modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

SPIx_Write

Prototype

void SPIx_Write(unsigned data_out);

Description

Writes one word via the SPI bus.

Parameters
  • data_out: data to be sent
Returns

Nothing.

Requires

Routine requires at least one SPI module.

Used SPI module must be initialized before using this function. See the SPIx_Init and SPIx_Init_Advanced routines.

Example
// write a buffer to the SPI bus
unsigned buffer;
...
SPI1_Write(buffer);
Notes
  • SPI library routines require you to specify the module you want to use. To select the desired SPI module, simply change the letter x in the routine prototype for a number from 0 to 8.
  • Number of SPI modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

SPI_Set_Active

Prototype

void SPI_Set_Active(unsigned (*read_ptr)(unsigned), void(*write_ptr)(unsigned));

Description

Sets the active SPI module which will be used by the SPIx_Read and SPIx_Write routines.

Parameters

Parameters :

Returns

Nothing.

Requires

Routine is available only for MCUs with multiple SPI modules.

Used SPI module must be initialized before using this function. See the SPIx_Init and SPIx_Init_Advanced routines.

Example
SPI_Set_Active(SPI1_Read, SPI1_Write); // Sets the SPI1 module active
Notes

Number of SPI modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

SPI_Read

Prototype

unsigned int SPI_Read(unsigned int buffer);

Description

Reads one word or byte from the SPI bus.

This is a generic routine which uses the active SPI module previously activated by the SPI_Set_Active routine.

Parameters
  • buffer: dummy data for clock generation (see device Datasheet for SPI modules implementation details)
Returns

Received data.

Requires

Routine requires at least one SPI module.

Used SPI module must be initialized before using this function. See the SPIx_Init and SPIx_Init_Advanced routines.

Example
// read a word from the SPI bus 
unsigned int take, buffer;
...
take = SPI_Read(buffer);
Notes

None.

SPI_Write

Prototype

procedure SPI_Write(data_out : word);

Description

Writes one word or byte via the SPI bus.

This is a generic routine which uses the active SPI module previously activated by the SPI_Set_Active routine.

Parameters
  • data_out: data to be sent
Returns

Nothing.

Requires

Routine requires at least one SPI module.

Used SPI module must be initialized before using this function. See the SPIx_Init and SPIx_Init_Advanced routines.

Example
// write a word to the SPI bus
unsigned int buffer;
...
SPI_Write(buffer);
Notes

None.

Copyright (c) 2002-2019 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code