Serial Peripheral Interface Reusable Driver v1.1
This project implements SPI reusable driver that can be adapted to various microcontrollers.
Loading...
Searching...
No Matches
spi.c File Reference

The implementation for the SPI Driver. More...

#include "spi.h"
Include dependency graph for spi.c:

Functions

void SPI_init (const SpiConfig_t *const Config, size_t configSize)
 
void SPI_transfer (const SpiTransferConfig_t *const TransferConfig)
 
void SPI_receive (const SpiTransferConfig_t *const TransferConfig)
 
void SPI_registerWrite (uint32_t address, uint32_t value)
 
uint16_t SPI_registerRead (uint32_t address)
 

Detailed Description

The implementation for the SPI Driver.

Author
Jose Luis Figueroa
Version
1.1
Date
2025-03-11

Function Documentation

◆ SPI_init()

void SPI_init ( const SpiConfig_t *const Config,
size_t configSize )

Description: This function is used to initialize the SPI based on the configuration
table defined in spi_cfg module.

PRE-CONDITION: The MCU clocks must be configured and enabled.
PRE-CONDITION: SPI pins should be configured using GPIO driver.
PRE-CONDITION: Configuration table needs to be populated (sizeof > 0)
PRE-CONDITION: The setting is within the maximum values (SPI_MAX).

POST-CONDITION: The peripheral is set up with the configuration settings.

Parameters
[in]Configis a pointer to the configuration table that contains the initialization for the peripheral.
[in]configSizeis the size of the configuration table.
Returns
void

Example:

const SpiConfig_t * const SpiConfig = SPI_configGet();
size_t configSize = SPI_configSizeGet();
SPI_Init(DioConfig, configSize);
const DioConfig_t DioConfig[]
Definition dio_cfg.c:41
const SpiConfig_t SpiConfig[]
Definition spi_cfg.c:41
size_t SPI_configSizeGet(void)
Definition spi_cfg.c:129
Definition spi_cfg.h:135
See also
SPI_configGet
SPI_getConfigSize
SPI_Init
SPI_Transfer
SPI_RegisterWrite
SPI_RegisterRead
SPI_CallbackRegister

Loop through all the elements of the configuration table.

Set the configuration of the SPI on the control register 1

Set the Clock phase and polarity modes

Set the hierarchy of the device

Set the baud rate of the device

Set the slave select pin management for the device

Set the frame format of the device

Set the data transfer type of the device

Set the data frame format (size) of the device

Enable the SPI module

◆ SPI_receive()

void SPI_receive ( const SpiTransferConfig_t *const TransferConfig)

Description: This function is used to initialize a data reception on the SPI bus. This function is used to receive data specified by the SpiTransferConfig_t structure, which contains the channel, size, and data.

PRE-CONDITION: The MCU clocks must be configured and enabled.
PRE-CONDITION: SPI_Init must be called with valid configuration data.
PRE-CONDITION: SpiTransferConfig_t needs to be populated.
PRE-CONDITION: The Channel is within the maximum SpiChannel_t.
PRE-CONDITION: The size is greater than 0.
PRE-CONDITION: The data is not NULL.

POST-CONDITION: Data transferred based on configuration.

Parameters
[in]SpiTransferConfigA pointer to a structure containing the channel, size, and data to be read.
Returns
void

Example:

uint16_t rxdata[1];
SpiTransferConfig_t ReceiveConfig =
{
.Channel = SPI_CHANNEL1,
.size = sizeof(rxdata)/sizeof(rxdata[0]),
.data = rxdata
};
SPI_receive(&ReceiveConfig);
void SPI_receive(const SpiTransferConfig_t *const TransferConfig)
Definition spi.c:401
@ SPI_CHANNEL1
Definition spi_cfg.h:38
Definition spi.h:42
See also
SPI_configGet
SPI_getConfigSize
SPI_Init
SPI_Transfer
SPI_RegisterWrite
SPI_RegisterRead
SPI_CallbackRegister

◆ SPI_registerRead()

uint16_t SPI_registerRead ( uint32_t address)

Description: This function is used to directly address a SPI register. The function should be used to access specialized functionality in the SPI peripheral that is not exposed by any other function of the interface.

PRE-CONDITION: Address is within the boundaries of the SPI register address space.

POST-CONDITION: The value stored in the register is returned to the caller.

Parameters
[in]addressis the address of the SPI register to read.
Returns
The current value of the SPI register.

Example:

type spiValue = SPI_registerRead(0x1000);
uint16_t SPI_registerRead(uint32_t address)
Definition spi.c:498
See also
SPI_configGet
SPI_configSizeGet
SPI_Init
SPI_Transfer
SPI_RegisterWrite
SPI_RegisterRead
SPI_CallbackRegister

◆ SPI_registerWrite()

void SPI_registerWrite ( uint32_t address,
uint32_t value )

Description: This function is used to directly address and modify a SPI register. The function should be used to access specialized functionality in the SPI peripheral that is not exposed by any other function of the interface.

PRE-CONDITION: Address is within the boundaries of the SPI register address space.

POST-CONDITION: The register located at address with be updated with value.

Parameters
[in]addressis a register address within the SPI peripheral map.
[in]valueis the value to set the SPI register.
Returns
void

Example

SPI_registerWrite(0x1000, 0x15);
void SPI_registerWrite(uint32_t address, uint32_t value)
Definition spi.c:459
See also
SPI_configGet
SPI_configSizeGet
SPI_Init
SPI_Transfer
SPI_RegisterWrite
SPI_RegisterRead
SPI_CallbackRegister

◆ SPI_transfer()

void SPI_transfer ( const SpiTransferConfig_t *const TransferConfig)

Description: This function is used to initialize a data transfer on the SPI bus. This function is used to send data to a slave device specified by the SpiTransferConfig_t structure, which contains the channel, size, and data.

PRE-CONDITION: The MCU clocks must be configured and enabled.
PRE-CONDITION: SPI_Init must be called with valid configuration data.
PRE-CONDITION: SpiTransferConfig_t needs to be populated.
PRE-CONDITION: The Channel is within the maximum SpiChannel_t.
PRE-CONDITION: The size is greater than 0.
PRE-CONDITION: The data is not NULL.

POST-CONDITION: Data transferred based on configuration.

Parameters
[in]SpiTransferConfigA pointer to a structure containing the channel, size, and data to be read.
Returns
void

Example:

uint16_t data[] = {0x56};
SpiTransferConfig_t TransferConfig =
{
.Channel = SPI_CHANNEL1,
.size = sizeof(data)/sizeof(data[0]),
.data = data
};
SPI_Transfer(&TransferConfig);
See also
SPI_configGet
SPI_getConfigSize
SPI_Init
SPI_Transfer
SPI_RegisterWrite
SPI_RegisterRead
SPI_CallbackRegister