SPI Library

The SPI Master module is available with all FT90x MCUs. mikroBasic 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

sub procedure 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

sub procedure SPIM1_Init_Advanced(dim masterClkRatio as byte, dim config as word, dim ssLine as word)

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 or _SPI_CFG_POLARITY_IDLE_LOW or _SPI_CFG_SS_AUTO_DISABLE or _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

sub function SPIM1_Read(dim dummy as byte) as byte

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 
dim take, buffer as byte
...
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

sub procedure SPIM1_Read_Bytes(dim bufferIn as ^byte, dim numBytesToRead as word)

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 
dim buffer as byte[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

sub procedure SPIM1_Write(dim dataOut as byte)

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
dim buffer as byte
...
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

sub procedure SPIM1_Write_Bytes(dim bufferOut as ^byte, dim numBytesToSend as word)

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

sub procedure SPIM1_Enable_SS(dim ssLine as byte)

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

sub procedure SPIM1_Disable_SS(dim ssLine as word)

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

sub procedure SPIM_Set_Active(
  dim writeBytes_Ptr as ^TSPI_WrBytes_Ptr,
  dim write_Ptr as ^TSPI_Wr_Ptr,
  dim readBytes_Ptr as ^TSPI_RdBytes_Ptr,
  dim read_Ptr as ^TSPI_Rd_Ptr,
  dim enableSS_Ptr as ^TSPI_EnSS_Ptr,
  dim disableSS_Ptr as ^TSPI_DisSS_Ptr
)

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

sub function SPIM_Read(dim dummy as byte) as byte

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 
dim take, buffer as byte
...
take = SPIM_Read(buffer)
Notes

None.

SPIM_Read_Bytes

Prototype

sub procedure SPIM_Read_Bytes(dim bufferIn as ^byte, dim numBytesToRead as word)

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 
dim buffer as byte[16]
...
SPIM_Read_Bytes(@buffer, 16)
Notes

None.

SPIM_Write

Prototype

sub procedure SPIM_Write(dimdataOut as byte)

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
dim buffer as byte
...
SPIM_Write(buffer)
Notes

None.

SPIM_Write_Bytes

Prototype

sub procedure SPIM_Write_Bytes(dim bufferOut as ^byte, dim numBytesToSend as word)

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

sub procedure SPIM_Enable_SS(dim ssLine as byte)

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

sub procedure SPIM_Disable_SS(dim ssLine as byte)

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