ACDC Senior Project v1.0.0
Senior Project for Textron Aviation
Loading...
Searching...
No Matches
ACDC_SPI.c File Reference

Implementation of SPI driver. More...

#include "ACDC_SPI.h"
#include "ACDC_GPIO.h"
#include "ACDC_CLOCK.h"

Functions

void SPI_InitCS (SPI_TypeDef *SPIx, bool isMaster, GPIO_TypeDef *GPIOx, uint16_t GPIO_PIN)
 Initializes the SPIx peripheral to master or slave, using the chip select pin of your choosing. (Default Values: SPI_MODE_16Bit, SPI_BAUD_DIV_2, MSB First)
 
void SPI_EnableSoftwareCS (SPI_TypeDef *SPIx, GPIO_TypeDef *GPIOx, uint16_t GPIO_PIN)
 Enables the software CS for SPIx. (Disables Hardware CS)
 
void SPI_EnableRemap (const SPI_TypeDef *SPIx, bool enable)
 Enables pin remapping on SPIx depending on the value of enable (Only SPI1 can be remapped)
 
void SPI_Transmit (SPI_TypeDef *SPIx, uint16_t data)
 Transmits data over the given SPI.
 
void SPI_TransmitCS (SPI_TypeDef *SPIx, uint16_t data, GPIO_TypeDef *GPIOx, uint16_t GPIO_PIN)
 Transmits data over the given SPI, and toggles the CS pins accordingly.
 
uint16_t SPI_Receive (const SPI_TypeDef *SPIx)
 Recieves data from a SPI.
 
uint16_t SPI_TransmitReceive (SPI_TypeDef *SPIx, uint16_t data)
 Transmits and recieves data over the SPI.
 
uint16_t SPI_TransmitReceiveCS (SPI_TypeDef *SPIx, uint16_t data, GPIO_TypeDef *GPIOx, uint16_t GPIO_PIN)
 Transmits and recieves data over the SPI, also takes care of the software chip select.
 
void SPI_SetBaudDivider (SPI_TypeDef *SPIx, SPI_BaudDivider SPI_BAUD_DIV_x)
 Changes the Baud rate divider of the SPIx peripheral.
 
void SPI_CalculateAndSetBaudDivider (SPI_TypeDef *SPIx, uint32_t maxPeripheralClockSpeed)
 Calculates and sets the SPI baud divider to accompany the current peripherals maximum clock speed.
 
void SPI_SetBitMode (SPI_TypeDef *SPIx, SPI_BitMode SPI_MODE_x)
 Changes the SPIx peripheral between 8-bit and 16-bit mode.
 
void SPI_SetLsbFirst (SPI_TypeDef *SPIx, bool LsbFirst)
 Sets SPIx into either MsbFirst or LsbFirst mode.
 
void SPI_SetClockPhaseAndPolarity (SPI_TypeDef *SPIx, bool ClkPhase, bool ClkPolarity)
 Sets the clock phase and polarity for SPIx.
 
void SPI_SetToMaster (SPI_TypeDef *SPIx, bool isMaster)
 Configures SPIx peripheral as master or slave mode.
 
bool SPI_HasDataToRecieve (SPI_TypeDef *SPIx)
 Checks if there is data available in the SPIx recieve buffer.
 

Detailed Description

Implementation of SPI driver.

Author
Liam Bramley, Devin Marx

This file includes functions to initalize the SPI, transfer and recieve data.

Version
0.1
Date
2024-01-30

Function Documentation

◆ SPI_CalculateAndSetBaudDivider()

void SPI_CalculateAndSetBaudDivider ( SPI_TypeDef * SPIx,
uint32_t maxPeripheralClockSpeed )

Calculates and sets the SPI baud divider to accompany the current peripherals maximum clock speed.

Parameters
SPIxSPI Peripheral (Ex. SPI1 or SPI2)
maxPeripheralClockSpeedMaximum clock speed the SPI peripheral can run at

◆ SPI_EnableRemap()

void SPI_EnableRemap ( const SPI_TypeDef * SPIx,
bool enable )

Enables pin remapping on SPIx depending on the value of enable (Only SPI1 can be remapped)

Parameters
SPIxSPI Peripheral (Ex. SPI1 or SPI2)
enableTrue if the system should enable remapping for SPIx, false if it should not remap SPIx

◆ SPI_EnableSoftwareCS()

void SPI_EnableSoftwareCS ( SPI_TypeDef * SPIx,
GPIO_TypeDef * GPIOx,
uint16_t GPIO_PIN )

Enables the software CS for SPIx. (Disables Hardware CS)

Parameters
SPIxSPI Peripheral (Ex. SPI1 or SPI2)
GPIOxPort of the GPIO (Ex. GPIOA, GPIOB, ...)
GPIO_PINDesired pin on port GPIOx (Ex. GPIO_PIN_0, GPIO_PIN_1, ...)

◆ SPI_HasDataToRecieve()

bool SPI_HasDataToRecieve ( SPI_TypeDef * SPIx)

Checks if there is data available in the SPIx recieve buffer.

Parameters
SPIxSPI Peripheral (Ex. SPI1 or SPI2)
Returns
True if there is data available to recieve, false otherwise.

◆ SPI_InitCS()

void SPI_InitCS ( SPI_TypeDef * SPIx,
bool isMaster,
GPIO_TypeDef * GPIOx,
uint16_t GPIO_PIN )

Initializes the SPIx peripheral to master or slave, using the chip select pin of your choosing. (Default Values: SPI_MODE_16Bit, SPI_BAUD_DIV_2, MSB First)

Parameters
SPIxSPI Peripheral (Ex. SPI1 or SPI2)
isMasterTrue if SPIx should act as the master, false if it should act as the slave
GPIOxGPIO Port for the chip select pin (Ex. GPIOA, GPIOB, ...)
GPIO_PINDesired chip select pin on port GPIOx (Ex. GPIO_PIN_0, GPIO_PIN_1, ...)

◆ SPI_Receive()

uint16_t SPI_Receive ( const SPI_TypeDef * SPIx)

Recieves data from a SPI.

Parameters
SPIxSPI to recieve from
Returns
Data recieved

◆ SPI_SetBaudDivider()

void SPI_SetBaudDivider ( SPI_TypeDef * SPIx,
SPI_BaudDivider SPI_BAUD_DIV_x )

Changes the Baud rate divider of the SPIx peripheral.

Parameters
SPIxSPI Peripheral (Ex. SPI1 or SPI2)
SPI_BAUD_DIV_xSPI Tx Baud rate divider (Ex. SPI_BAUD_DIV_2, SPI_BAUD_DIV_4, ...)

◆ SPI_SetBitMode()

void SPI_SetBitMode ( SPI_TypeDef * SPIx,
SPI_BitMode SPI_MODE_x )

Changes the SPIx peripheral between 8-bit and 16-bit mode.

Parameters
SPIxSPI Peripheral (Ex. SPI1 or SPI2)
SPI_MODE_xBit mode selection for SPIx (Ex. SPI_MODE_8bit or SPI_MODE_16bit)

◆ SPI_SetClockPhaseAndPolarity()

void SPI_SetClockPhaseAndPolarity ( SPI_TypeDef * SPIx,
bool ClkPhase,
bool ClkPolarity )

Sets the clock phase and polarity for SPIx.

Parameters
SPIxSPI Peripheral (Ex. SPI1 or SPI2)
ClkPhaseTrue for second clock transition as first data capture edge, False for first clock transition. {See RM-744}
ClkPolarityTrue to set clock to 1 when idle, else false to set low while idle.

◆ SPI_SetLsbFirst()

void SPI_SetLsbFirst ( SPI_TypeDef * SPIx,
bool LsbFirst )

Sets SPIx into either MsbFirst or LsbFirst mode.

Parameters
SPIxSPI Peripheral (Ex. SPI1 or SPI2)
LsbFirstTrue if the data format should be LsbFirst, else false for MsbFirst.

◆ SPI_SetToMaster()

void SPI_SetToMaster ( SPI_TypeDef * SPIx,
bool isMaster )

Configures SPIx peripheral as master or slave mode.

Parameters
SPIxSPI Peripheral (Ex. SPI1 or SPI2)
isMasterTrue to configure SPI as master, False to configure it as slave.

◆ SPI_Transmit()

void SPI_Transmit ( SPI_TypeDef * SPIx,
uint16_t data )

Transmits data over the given SPI.

Parameters
SPIxSPI to transmit over
dataData to transmit

◆ SPI_TransmitCS()

void SPI_TransmitCS ( SPI_TypeDef * SPIx,
uint16_t data,
GPIO_TypeDef * GPIOx,
uint16_t GPIO_PIN )

Transmits data over the given SPI, and toggles the CS pins accordingly.

Parameters
SPIxSPI to transmit over
dataData to transmit
GPIOxGPIO Port for the chip select pin (Ex. GPIOA, GPIOB, ...)
GPIO_PINDesired chip select pin on port GPIOx (Ex. GPIO_PIN_0, GPIO_PIN_1, ...)

◆ SPI_TransmitReceive()

uint16_t SPI_TransmitReceive ( SPI_TypeDef * SPIx,
uint16_t data )

Transmits and recieves data over the SPI.

Parameters
SPIxSPI to transmit over
dataData to transmit
Returns
Data recieved

◆ SPI_TransmitReceiveCS()

uint16_t SPI_TransmitReceiveCS ( SPI_TypeDef * SPIx,
uint16_t data,
GPIO_TypeDef * GPIOx,
uint16_t GPIO_PIN )

Transmits and recieves data over the SPI, also takes care of the software chip select.

Parameters
SPIxSPI to transmit over
dataData to transmit
GPIOxGPIO Port for the chip select pin (Ex. GPIOA, GPIOB, ...)
GPIO_PINDesired chip select pin on port GPIOx (Ex. GPIO_PIN_0, GPIO_PIN_1, ...)
Returns
Data recieved