SPI Master Module

This module provides functions to read and write data using the SPI protocol.

Those functions are used in the SPI encoders REM 14 Encoder Module itself used by REM 16MT Encoder Module to read and write data from the encoders.

The functions should always run over an IF2 Tile so it can access the ports to your SOMANET Drive module.

How to use

Important

We assume that you are using SOMANET Base and your app includes the required board support files for your SOMANET device.

  1. First, add all the SOMANET Motion Control modules to your app Makefile.

    USED_MODULES = configuration_parameters module_adc module_spi_master lib_bldc_torque_control module_board-support module_hall_sensor module_utils module_position_feedback module_pwm module_incremental_encoder module_biss_encoder module_encoder_rem_14 module_serial_encoder module_shared_memory module_spi_master module_watchdog
    

    Note

    Not all modules will be required, but when using a library it is recommended to include always all the contained modules. This will help solving internal dependency issues.

  2. Include the SPI master header spi_master.h in your app.

  3. Instantiate the ports for the SPI.

    It needs a SPIPorts structure containing two clock blocks and 4 1-bit ports for SPI.

  4. At your IF2 tile, You can use the functions to read or write SPI data.
    #include <CoreC2X.bsp>              //Board Support file for SOMANET Core C2X device
    #include <Drive1000-rev-c4.bsp>     //Board Support file for SOMANET Drive module
                                        //(select your board support files according to your device)
    
    // 2. Include the SPI Master header **spi_master.h** in your app.
    #include <spi_master.h>
    
    // 3.Instantiate the ports for the SPI.
    SPIPorts spi_ports = SOMANET_DRIVE_SPI_PORTS;
    
    int main(void)
    {
        par
        {
            on tile[IF2_TILE]:
            {
                // 4. Use the functions to read and write SPI data.
                // initialize the master
                spi_master_init(spi_ports.spi_interface, DEFAULT_SPI_CLOCK_DIV);
    
                // read SPI data
                slave_select(*spi_ports.slave_select);
                short data_in = spi_master_in_short(spi_ports.spi_interface);
                slave_deselect(*spi_ports.slave_select);
    
                //write SPI data
                short data = 0xab
                slave_select(*spi_ports.slave_select);
                spi_master_out_short(spi_ports.spi_interface, data);
                slave_deselect(*spi_ports.slave_select);
            }
        }
    
        return 0;
    }
    

API

Definitions

DEFAULT_SPI_CLOCK_DIV

divisor for SPI clock frequency, (250/DIV)/2 MHz

SPI_MASTER_MODE
SPI_MASTER_SD_CARD_COMPAT

MOSI high during input.

Types

Warning

doxygenstruct: Cannot find struct “spi_master_interface” in doxygen xml output

SPIPorts

Structure for SPI ports and clock blocks.

Structure Members:

spi_master_class spi_class
port *movable slave_select

Functions

void spi_master_init(spi_master_interface &spi_if, unsigned spi_clock_div)

Configure ports and clocks, clearing port buffers.

Must be called before any SPI data input or output functions are used.

Parameters:
  • spi_if – Resources for the SPI class being initialized
  • spi_clock_div – SPI clock frequency is fref/(2*spi_clock_div), where freq defaults to 100MHz

Example: To achieve an sclk frequency of 25MHz, a divider of 2 must be specified, as 100(MHz)/(2*2) = 25(MHz).

Example: To achieve an sclk frequency of 625kHz, a divider of 80 must be specified, as 100(MHz)/(2*80) = 0.625(MHz).

void spi_master_shutdown(spi_master_interface &spi_if)

Stops the clocks running.

Should be called when all SPI input and output is completed.

Parameters:
  • spi_if – Resources for the SPI class being shutdown
unsigned char spi_master_in_byte(spi_master_interface &spi_if)

Receive one byte.

Most significant bit first order. Big endian byte order.

Parameters:
  • spi_if – Resources for the SPI class
Returns:

The received byte

unsigned short spi_master_in_short(spi_master_interface &spi_if)

Receive one short.

Most significant bit first order. Big endian byte order.

Parameters:
  • spi_if – Resources for the SPI class
Returns:

The received short

unsigned int spi_master_in_word(spi_master_interface &spi_if)

Receive one word.

Most significant bit first order. Big endian byte order.

Parameters:
  • spi_if – Resources for the SPI class
Returns:

The received word

void spi_master_in_buffer(spi_master_interface &spi_if, unsigned char buffer[], int num_bytes)

Receive specified number of bytes.

Most significant bit first order. Big endian byte order.

Parameters:
  • spi_if – Resources for the SPI class
  • buffer – The array the received data will be written to
  • num_bytes – The number of bytes to read from the SPI class, this must not be greater than the size of buffer
void spi_master_out_byte(spi_master_interface &spi_if, unsigned char data)

Transmit one byte.

Most significant bit first order. Big endian byte order.

Parameters:
  • spi_if – Resources for the SPI class
  • data – The byte to transmit
void spi_master_out_short(spi_master_interface &spi_if, unsigned short data)

Transmit one short.

Most significant bit first order. Big endian byte order.

Parameters:
  • spi_if – Resources for the SPI class
  • data – The short to transmit
void spi_master_out_word(spi_master_interface &spi_if, unsigned int data)

Transmit one word.

Most significant bit first order. Big endian byte order.

Parameters:
  • spi_if – Resources for the SPI class
  • data – The word to transmit
void spi_master_out_buffer(spi_master_interface &spi_if, const unsigned char buffer[], int num_bytes)

Transmit specified number of bytes.

Most significant bit first order. Big endian byte order.

Parameters:
  • spi_if – Resources for the SPI class
  • buffer – The array of data to transmit
  • num_bytes – The number of bytes to write to the SPI class, this must not be greater than the size of buffer