foxBMS  1.2.1
The foxBMS Battery Management System API Documentation
spi.c File Reference

Driver for the SPI module. More...

#include "spi.h"
#include "HL_reg_spi.h"
#include "HL_spi.h"
#include "HL_sys_common.h"
#include "dma.h"
#include "fsystem.h"
#include "io.h"
#include "mcu.h"
#include "os.h"
Include dependency graph for spi.c:

Go to the source code of this file.

Macros

#define SPI_FLAG_REGISTER_TRANSMISSION_ERRORS   (0x5Fu)
 

Functions

STD_RETURN_TYPE_e SPI_TransmitDummyByte (SPI_INTERFACE_CONFIG_s *pSpiInterface, uint32_t delay)
 Sends a dummy byte to wake up the SPI interface. More...
 
STD_RETURN_TYPE_e SPI_TransmitData (SPI_INTERFACE_CONFIG_s *pSpiInterface, uint16 *pTxBuff, uint32 frameLength)
 Transmits data on SPI without DMA. More...
 
STD_RETURN_TYPE_e SPI_TransmitReceiveData (SPI_INTERFACE_CONFIG_s *pSpiInterface, uint16 *pTxBuff, uint16 *pRxBuff, uint32 frameLength)
 Transmits and receives data on SPI without DMA. More...
 
void SPI_FramTransmitReceiveData (SPI_INTERFACE_CONFIG_s *pSpiInterface, uint16 *pTxBuff, uint16 *pRxBuff, uint32 frameLength)
 Transmits and receives data on SPI without DMA, wrappe for FRAM. More...
 
STD_RETURN_TYPE_e SPI_TransmitReceiveDataDma (SPI_INTERFACE_CONFIG_s *pSpiInterface, uint16_t *pTxBuff, uint16_t *pRxBuff, uint32_t frameLength)
 Transmits and receives data on SPI with DMA. More...
 
STD_RETURN_TYPE_e SPI_Lock (uint8_t spi)
 Locks SPI interfaces. More...
 
void SPI_Unlock (uint8_t spi)
 Unlocks SPI interfaces. More...
 
void SPI_SetFunctional (spiBASE_t *pNode, enum spiPinSelect bit, bool hardwareControlled)
 Sets the functional of a SPI pin. More...
 
STD_RETURN_TYPE_e SPI_SlaveSetReceiveDataDma (SPI_INTERFACE_CONFIG_s *pSpiInterface, uint16_t *pTxBuff, uint16_t *pRxBuff, uint32_t frameLength)
 Transmits and receives data on SPI with DMA. More...
 
void SPI_DmaSendLastByte (uint8_t spiIndex)
 Used to send last byte per SPI. More...
 
void UNIT_TEST_WEAK_IMPL spiNotification (spiBASE_t *spi, uint32 flags)
 
STD_RETURN_TYPE_e SPI_CheckInterfaceAvailable (spiBASE_t *pNode)
 Returns STD_OK if the SPI interface can be used again. More...
 
uint8_t SPI_GetSpiIndex (spiBASE_t *pNode)
 Returns index of SPI node. More...
 

Variables

static uint32_t spi_txLastWord [DMA_NUMBER_SPI_INTERFACES] = {0}
 

Detailed Description

Driver for the SPI module.

SPDX-License-Identifier: BSD-3-Clause

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

We kindly request you to use one or more of the following phrases to refer to foxBMS in your hardware, software, documentation or advertising materials:

  • ″This product uses parts of foxBMS®″
  • ″This product includes parts of foxBMS®″
  • ″This product is derived from foxBMS®″
Author
foxBMS Team
Date
2019-12-12 (date of creation)
Updated
2021-12-08 (date of last update)
Prefix
SPI

Definition in file spi.c.

Macro Definition Documentation

◆ SPI_FLAG_REGISTER_TRANSMISSION_ERRORS

#define SPI_FLAG_REGISTER_TRANSMISSION_ERRORS   (0x5Fu)

Bitfield to check for transmission errors in SPI FLAG register

Definition at line 71 of file spi.c.

Function Documentation

◆ SPI_CheckInterfaceAvailable()

STD_RETURN_TYPE_e SPI_CheckInterfaceAvailable ( spiBASE_t *  pNode)

Returns STD_OK if the SPI interface can be used again.

This function just checks for SPI_PENDING and groups SPI_READY and SPI_...

Parameters
[in]pNodehandle of the SPI node that should be checked
Returns
STD_RETURN_TYPE_e indicating wether the interface is ok to be used.

Definition at line 445 of file spi.c.

◆ SPI_DmaSendLastByte()

void SPI_DmaSendLastByte ( uint8_t  spiIndex)

Used to send last byte per SPI.

This function is called in the DMA Tx callback. It is used to send the last byte with CSHOLD = 0.

Parameters
spiIndexSPI node in use

Definition at line 436 of file spi.c.

◆ SPI_FramTransmitReceiveData()

void SPI_FramTransmitReceiveData ( SPI_INTERFACE_CONFIG_s pSpiInterface,
uint16 *  pTxBuff,
uint16 *  pRxBuff,
uint32  frameLength 
)

Transmits and receives data on SPI without DMA, wrappe for FRAM.

This function can be used to send and receive data via SPI. SPI communication is performed in blocking mode and chip select is set/reset automatically. It does not drive the Chip Select (neither hardware nor software) as this is done directly in the FRAM functions.

Parameters
pSpiInterfacepointer to SPI interface configuration
pTxBuffpointer to data that is transmitted by the SPI interface
pRxBuffpointer to data that is received by the SPI interface
frameLengthnumber of bytes to be transmitted by the SPI interface
Returns
status of the SPI transfer

Definition at line 201 of file spi.c.

◆ SPI_GetSpiIndex()

uint8_t SPI_GetSpiIndex ( spiBASE_t *  pNode)

Returns index of SPI node.

Parameters
[in]pNodehandle of the SPI node that should be checked
Returns
index of SPI node

Invalid SPI node

Definition at line 455 of file spi.c.

◆ SPI_Lock()

STD_RETURN_TYPE_e SPI_Lock ( uint8_t  spi)

Locks SPI interfaces.

This function is used to change the state of the SPI_busy_flags variable to "locked".

Parameters
spiSPI interface to be locked (0-4 on the TMS570LC4357)
Returns
STD_OK if SPI interface could be locked, STD_NOT_OK otherwise

Definition at line 315 of file spi.c.

Here is the call graph for this function:

◆ SPI_SetFunctional()

void SPI_SetFunctional ( spiBASE_t *  pNode,
enum spiPinSelect  bit,
bool  hardwareControlled 
)

Sets the functional of a SPI pin.

SPI pins in this HAL can have functional state SPI (for when it is controlled by the SPI hardware) and functional state GIO (for when it is controlled as a GIO pin).

Parameters
[in,out]pNodehandle of the SPI node that should be configured
[in]bitbit that should be manipulated, other pins remain unchanged
[in]hardwareControlledWhether the bit should be hardware controlled (true) or not (false)

Definition at line 338 of file spi.c.

◆ SPI_SlaveSetReceiveDataDma()

STD_RETURN_TYPE_e SPI_SlaveSetReceiveDataDma ( SPI_INTERFACE_CONFIG_s pSpiInterface,
uint16_t *  pTxBuff,
uint16_t *  pRxBuff,
uint32_t  frameLength 
)

Transmits and receives data on SPI with DMA.

This function can be used to send and receive data via SPI. SPI communication is performed in blocking mode and chip select is set/reset automatically..

Parameters
pSpiInterfacepointer to SPI interface configuration
pTxBuffpointer to data that is transmitted by the SPI interface
pRxBuffpointer to data that is received by the SPI interface
frameLengthnumber of bytes to be transmitted by the SPI interface
Returns
status of the SPI transfer

SPI receive works only with HW Chip Select

Definition at line 372 of file spi.c.

Here is the call graph for this function:

◆ SPI_TransmitData()

STD_RETURN_TYPE_e SPI_TransmitData ( SPI_INTERFACE_CONFIG_s pSpiInterface,
uint16 *  pTxBuff,
uint32  frameLength 
)

Transmits data on SPI without DMA.

This function can be used to send and receive data via SPI. SPI communication is performed in blocking mode and chip select is set/reset automatically.

Parameters
pSpiInterfacepointer to SPI interface configuration
pTxBuffpointer to data that is transmitted by the SPI interface
frameLengthnumber of bytes to be transmitted by the SPI interface
Returns
status of the SPI transfer

SW Chip Select

Set SPI Chip Select pins as GIOs

Activate Chip Select

HW Chip Select

Activate HW Chip Select according to bitmask register CSNR by setting pins as SPI functional pins

First deactivate all HW Chip Selects

Bitmask = 0 --> HW CS active --> write to PC0 to set pin as SPI pin (and not GIO)

SW Chip Select

Deactivate Chip Select

Definition at line 91 of file spi.c.

Here is the call graph for this function:

◆ SPI_TransmitDummyByte()

STD_RETURN_TYPE_e SPI_TransmitDummyByte ( SPI_INTERFACE_CONFIG_s pSpiInterface,
uint32_t  delay 
)

Sends a dummy byte to wake up the SPI interface.

Parameters
pSpiInterfacepointer to SPI interface configuration
delaydelay to wait after dummy byte transfer
Returns
status of the SPI transfer

Definition at line 83 of file spi.c.

Here is the call graph for this function:

◆ SPI_TransmitReceiveData()

STD_RETURN_TYPE_e SPI_TransmitReceiveData ( SPI_INTERFACE_CONFIG_s pSpiInterface,
uint16 *  pTxBuff,
uint16 *  pRxBuff,
uint32  frameLength 
)

Transmits and receives data on SPI without DMA.

This function can be used to send and receive data via SPI. SPI communication is performed in blocking mode and chip select is set/reset automatically.

Parameters
pSpiInterfacepointer to SPI interface configuration
pTxBuffpointer to data that is transmitted by the SPI interface
pRxBuffpointer to data that is received by the SPI interface
frameLengthnumber of bytes to be transmitted by the SPI interface
Returns
status of the SPI transfer

SW Chip Select

Set SPI Chip Select pins as GIOs

Activate Chip Select

HW Chip Select

Activate HW Chip Select according to bitmask register CSNR by setting pins as SPI functional pins

First deactivate all HW Chip Selects

Bitmask = 0 --> HW CS active --> write to PC0 to set pin as SPI pin (and not GIO)

SW Chip Select

Deactivate Chip Select

Definition at line 143 of file spi.c.

Here is the call graph for this function:

◆ SPI_TransmitReceiveDataDma()

STD_RETURN_TYPE_e SPI_TransmitReceiveDataDma ( SPI_INTERFACE_CONFIG_s pSpiInterface,
uint16_t *  pTxBuff,
uint16_t *  pRxBuff,
uint32_t  frameLength 
)

Transmits and receives data on SPI with DMA.

This function can be used to send and receive data via SPI. SPI communication is performed in blocking mode and chip select is set/reset automatically..

Parameters
pSpiInterfacepointer to SPI interface configuration
pTxBuffpointer to data that is transmitted by the SPI interface
pRxBuffpointer to data that is received by the SPI interface
frameLengthnumber of bytes to be transmitted by the SPI interface
Returns
status of the SPI transfer

SPI over DMA currently only compatible with HW Chip Select

Set number of Tx words to send Last word sent in ISR to set CSHOLD = 0

Send first word without DMA because when writing config to DAT1 the HW CS pin are asserted immediately, even if SPIEN bit in GCR1 is 0. The C2TDELAY is then taken into account before the transmission.

Definition at line 214 of file spi.c.

Here is the call graph for this function:

◆ SPI_Unlock()

void SPI_Unlock ( uint8_t  spi)

Unlocks SPI interfaces.

This function is used to change the state of the SPI_busy_flags variable to "unlocked".

Parameters
spiSPI interface to be unlocked (0-4 on the TMS570LC4357)

Definition at line 330 of file spi.c.

Here is the call graph for this function:

◆ spiNotification()

void UNIT_TEST_WEAK_IMPL spiNotification ( spiBASE_t *  spi,
uint32  flags 
)

Definition at line 442 of file spi.c.

Variable Documentation

◆ spi_txLastWord

uint32_t spi_txLastWord[DMA_NUMBER_SPI_INTERFACES] = {0}
static

Definition at line 67 of file spi.c.