SPI Library

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

Library Routines

Generic Routines

SPIM1_Init

Prototype

void SPIM1_Init();

Description

Configures and initializes the SPI module with default parameters :

  • SPI Clock set to Master clock/64,
  • Clock idle state low,
  • Capture data on falling clock edge,
  • FIFO disabled,
  • Slave select line disabled.
Parameters

None.

Returns

Nothing.

Requires

Routine requires SPI module.

Example
// Initialize the SPI module with default settings
SPIM1_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 3.
  • Number of SPI modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • Switching between the SPI modules in the SPI library is done by the SPIM_Set_Active function (both SPI modules have to be previously initialized).

SPIM1_Init_Advanced

Prototype

void SPIM1_Init_Advanced(char masterClkRatio, unsigned int config, unsigned int ssLine);

Description

Configures and initializes the SPI module with user defined settings.

Parameters
  • masterClkRatio: SPI module clock derived from master clock :
    Value Description
    _SPI_MASTER_CLK_RATIO_2 Master clock divided by 2.
    _SPI_MASTER_CLK_RATIO_4 Master clock divided by 4.
    _SPI_MASTER_CLK_RATIO_8 Master clock divided by 8.
    _SPI_MASTER_CLK_RATIO_16 Master clock divided by 16.
    _SPI_MASTER_CLK_RATIO_32 Master clock divided by 32.
    _SPI_MASTER_CLK_RATIO_64 Master clock divided by 64.
    _SPI_MASTER_CLK_RATIO_128 Master clock divided by 128.
    _SPI_MASTER_CLK_RATIO_256 Master clock divided by 256.
    _SPI_MASTER_CLK_RATIO_512 Master clock divided by 512.
  • config: The parameter config determines the clock polarity, clock phase, FIFO and Slave select settings. The valid value is formed when OR-ing the values below :

    Clock phase parameters :
    Value Description
    _SPI_CFG_PHASE_CAPTURE_RISING Data is captured on the rising edge.
    _SPI_CFG_PHASE_CAPTURE_FALLING Data is captured on the falling edge.

    Clock polarity parameters :
    Value Description
    _SPI_CFG_POLARITY_IDLE_LOW Clock idle state is low, active state is high.
    _SPI_CFG_POLARITY_IDLE_HIGH Clock idle state is high, active state is low.

    Slave select parameters :
    Value Description
    _SPI_CFG_SS_AUTO_ENABLE Auto SS assertions enabled.
    _SPI_CFG_SS_AUTO_DISABLE Auto SS assertions disabled.

    FIFO enable/disable parameters :
    Value Description
    _SPI_CFG_FIFO_ENABLE Enable FIFO mode.
    _SPI_CFG_FIFO_DISABLE Disable FIFO mode.

    FIFO buffer depth parameters :
    Value Description
    _SPI_CFG_FIFO_DEPTH_16 Set FIFO buffer depth to 16 bytes.
    _SPI_CFG_FIFO_DEPTH_64 Set FIFO buffer depth to 64 bytes.
  • ssLine: slave select line used :
    Value Description
    _SPI_SS_LINE_0 Slave select line 0.
    _SPI_SS_LINE_1 Slave select line 1.
    _SPI_SS_LINE_2 Slave select line 2.
    _SPI_SS_LINE_3 Slave select line 3.
    _SPI_SS_LINE_4 Slave select line 4.
    _SPI_SS_LINE_5 Slave select line 5.
    _SPI_SS_LINE_6 Slave select line 6.
    _SPI_SS_LINE_7 Slave select line 7.
    _SPI_SS_LINE_NONE No slave select line is selected.
Returns

Nothing.

Requires

Routine requires SPI module.

Example
SPIM1_Init_Advanced(_SPI_MASTER_CLK_RATIO_64, _SPI_CFG_PHASE_CAPTURE_RISING | _SPI_CFG_POLARITY_IDLE_LOW | _SPI_CFG_SS_AUTO_DISABLE | _SPI_CFG_FIFO_DISABLE, _SPI_SS_LINE_NONE);
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 3.
  • Number of SPI modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • Switching between the SPI modules in the SPI library is done by the SPIM_Set_Active function (both SPI modules have to be previously initialized).

SPIM1_Read

Prototype

char SPIM1_Read(char dummy);

Description

Reads one byte from the SPI bus.

Parameters
  • dummy: dummy data for clock generation.
Returns

Received byte.

Requires

Routine requires SPI module.

Used SPI module must be initialized before using this function. See the SPIM1_Init and SPIM1_Init_Advanced routines.

Example
// read a byte from the SPI bus 
char take, buffer;
...
take = SPIM1_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 3.

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

SPIM1_Read_Bytes

Prototype

void SPIM1_Read_Bytes(char *bufferIn, unsigned int numBytesToRead);

Description

Reads multiple bytes from the SPI bus.

Parameters
  • bufferIn: destination buffer for data storage.
  • numBytesToRead: number of bytes to be read.
Returns

Received bytes.

Requires

Routine requires SPI module.

Used SPI module must be initialized before using this function. See the SPIM1_Init and SPIM1_Init_Advanced routines.

Example
// read 16 bytes from the SPI bus 
char *buffer[16];
...
SPIM1_Read_Bytes(&buffer, 16);
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 3.

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

SPIM1_Write

Prototype

void SPIM1_Write(char dataOut);

Description

Writes one byte to the SPI bus.

Parameters
  • data_out: data to be written.
Returns

Nothing.

Requires

Routine requires SPI module.

Used SPI module must be initialized before using this function. See the SPIM1_Init and SPIM1_Init_Advanced routines.

Example
// write a buffer to the SPI bus
unsigned char buffer;
...
SPIM1_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 3.

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

SPIM1_Write_Bytes

Prototype

void SPIM1_Write_Bytes(char *bufferOut, unsigned int numBytesToSend);

Description

Writes bytes to the SPI bus.

Parameters
  • bufferOut: source buffer where the data is stored.
  • numBytesToSend: number of bytes to be written.
Returns

Nothing.

Requires

Routine requires SPI module.

Used SPI module must be initialized before using this function. See the SPIM1_Init and SPIM1_Init_Advanced routines.

Example
// write a buffer to the SPI bus
SPIM1_Write_Bytes(&buffer, 16);
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 3.

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

SPIM1_Enable_SS

Prototype

void SPIM1_Enable_SS(char ssLine);

Description

Enables desired Slave Select line.

Parameters
  • ssLine: slave select line used :
    Value Description
    _SPI_SS_LINE_0 Slave select line 0.
    _SPI_SS_LINE_1 Slave select line 1.
    _SPI_SS_LINE_2 Slave select line 2.
    _SPI_SS_LINE_3 Slave select line 3.
    _SPI_SS_LINE_4 Slave select line 4.
    _SPI_SS_LINE_5 Slave select line 5.
    _SPI_SS_LINE_6 Slave select line 6.
    _SPI_SS_LINE_7 Slave select line 7.
    _SPI_SS_LINE_NONE No slave select line is selected.
Returns

Nothing.

Requires

Routine requires SPI module.

Used SPI module must be initialized before using this function. See the SPIM1_Init and SPIM1_Init_Advanced routines.

Example
// enable SS line 0
SPIM1_Enable_SS(_SPI_SS_LINE_0);
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 3.

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

SPIM1_Disable_SS

Prototype

void SPIM1_Disable_SS(char ssLine);

Description

Disables desired Slave Select line.

Parameters
  • ssLine: slave select line used :
    Value Description
    _SPI_SS_LINE_0 Slave select line 0.
    _SPI_SS_LINE_1 Slave select line 1.
    _SPI_SS_LINE_2 Slave select line 2.
    _SPI_SS_LINE_3 Slave select line 3.
    _SPI_SS_LINE_4 Slave select line 4.
    _SPI_SS_LINE_5 Slave select line 5.
    _SPI_SS_LINE_6 Slave select line 6.
    _SPI_SS_LINE_7 Slave select line 7.
    _SPI_SS_LINE_NONE No slave select line is selected.
Returns

Nothing.

Requires

Routine requires SPI module.

Used SPI module must be initialized before using this function. See the SPIM1_Init and SPIM1_Init_Advanced routines.

Example
// disable SS line 0
SPIM1_Disable_SS(_SPI_SS_LINE_0);
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 3.

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

SPIM_Set_Active

Prototype

void SPIM_Set_Active(
  void (*writeBytes_Ptr)(char *bufferOut, unsigned int numBytesToSend),
  void (*write_Ptr)(char dataOut),
  void (*readBytes_Ptr)(char *bufferIn, unsigned int numBytesToRead),
  char (*read_Ptr)(void),
  void (*enableSS_Ptr)(char ssLine),
  void (*disableSS_Ptr)(char ssLine)
);

Description

Sets the active SPI module which will be used by the SPIM1_Read and SPIM1_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 SPIM1_Init and SPIM1_Init_Advanced routines.

Example
SPIM_Set_Active(SPIM1_Write_Bytes, SPIM1_Write, SPIM1_Read_Bytes, SPIM1_Read, SPIM1_Enable_SS, SPIM1_Disable_SS); // Sets the SPIM1 module active
Notes

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

SPIM_Read

Prototype

char SPIM_Read(char dummy);

Description

Reads one byte from the SPI bus.

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

Parameters
  • dummy: dummy data for clock generation.
Returns

Received byte.

Requires

Routine requires SPI module.

Used SPI module must be initialized before using this function. See the SPIM1_Init and SPIM1_Init_Advanced routines.

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

None.

SPIM_Read_Bytes

Prototype

void SPIM_Read_Bytes(char *bufferIn, unsigned int numBytesToRead);

Description

Reads bytes from the SPI bus.

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

Parameters
  • bufferIn: destination buffer for data storage.
  • numBytesToRead: number of bytes to be read.
Returns

Received bytes.

Requires

Routine requires SPI module.

Used SPI module must be initialized before using this function. See the SPIM1_Init and SPIM1_Init_Advanced routines.

Example
// read 16 bytes from the SPI bus 
char *buffer[16];
...
SPIM_Read_Bytes(&buffer, 16);
Notes

None.

SPIM_Write

Prototype

void SPIM_Write(char dataOut);

Description

Writes one byte to the SPI bus.

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

Parameters
  • dataOut: data to be sent.
Returns

Nothing.

Requires

Routine requires SPI module.

Used SPI module must be initialized before using this function. See the SPIM1_Init and SPIM1_Init_Advanced routines.

Example
// write a byte to the SPI bus
unsigned char buffer;
...
SPIM_Write(buffer);
Notes

None.

SPIM_Write_Bytes

Prototype

void SPIM_Write_Bytes(char *bufferOut, unsigned int numBytesToSend);

Description

Writes bytes to the SPI bus.

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

Parameters
  • bufferOut: source buffer where the data is stored.
  • numBytesToSend: number of bytes to be written.
Returns

Nothing.

Requires

Routine requires SPI module.

Used SPI module must be initialized before using this function. See the SPIM1_Init and SPIM1_Init_Advanced routines.

Example
// write a buffer to the SPI bus
SPIM_Write_Bytes(&buffer, 16);
Notes

None.

SPIM_Enable_SS

Prototype

void SPIM_Enable_SS(char ssLine);

Description

Enables desired Slave Select line.

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

Parameters
  • ssLine: slave select line used :
    Value Description
    _SPI_SS_LINE_0 Slave select line 0.
    _SPI_SS_LINE_1 Slave select line 1.
    _SPI_SS_LINE_2 Slave select line 2.
    _SPI_SS_LINE_3 Slave select line 3.
    _SPI_SS_LINE_4 Slave select line 4.
    _SPI_SS_LINE_5 Slave select line 5.
    _SPI_SS_LINE_6 Slave select line 6.
    _SPI_SS_LINE_7 Slave select line 7.
    _SPI_SS_LINE_NONE No slave select line is selected.
Returns

Nothing.

Requires

Routine requires SPI module.

Used SPI module must be initialized before using this function. See the SPIM1_Init and SPIM1_Init_Advanced routines.

Example
// enable SS line 0
SPIM_Enable_SS(_SPI_SS_LINE_0);
Notes

None.

SPIM_Disable_SS

Prototype

void SPIM_Disable_SS(char ssLine);

Description

Disables desired Slave Select line.

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

Parameters
  • ssLine: slave select line used :
    Value Description
    _SPI_SS_LINE_0 Slave select line 0.
    _SPI_SS_LINE_1 Slave select line 1.
    _SPI_SS_LINE_2 Slave select line 2.
    _SPI_SS_LINE_3 Slave select line 3.
    _SPI_SS_LINE_4 Slave select line 4.
    _SPI_SS_LINE_5 Slave select line 5.
    _SPI_SS_LINE_6 Slave select line 6.
    _SPI_SS_LINE_7 Slave select line 7.
    _SPI_SS_LINE_NONE No slave select line is selected.
Returns

Nothing.

Requires

Routine requires SPI module.

Used SPI module must be initialized before using this function. See the SPIM1_Init and SPIM1_Init_Advanced routines.

Example
// disable SS line 0
SPIM_Disable_SS(_SPI_SS_LINE_0);
Notes

None.

Copyright (c) 2002-2015 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