foxBMS  1.1.2
The foxBMS Battery Management System API Documentation
sps.c File Reference

Driver for the smart power switches. More...

#include "sps.h"
#include "database.h"
#include "io.h"
#include "mcu.h"
#include "os.h"
#include "spi.h"
Include dependency graph for sps.c:

Go to the source code of this file.

Macros

#define SPS_RESET_WAIT_TIME_TICKS   (5u)
 

Functions

static void SPS_SetCommandTxBuffer (const SPS_ACTION_e action)
 Assemble a general command to the SPS ICs. More...
 
static void SPS_SetContactorsTxBuffer (void)
 Assemble an update to the channel state of the SPS ICs. More...
 
static STD_RETURN_TYPE_e SPS_Transmit (void)
 Transmit all assembled messages for one timeframe. More...
 
static void SPS_GlobalReadCurrent (const uint8_t outputAllDevices)
 Read the current measurements for a specific output for all SPS ICs of the daisy-chain. More...
 
static void SPS_InitializeIo (void)
 
static void SPS_InitializeBuffers (void)
 
static void SPS_GlobalRegisterRead (const uint16_t address, const SPS_READ_TYPE_e controlOrDiagnostic, uint16_t *pSpiTxBuffer)
 Sets the SPI Tx buffer for a register read access for all SPS ICs of the daisy-chain. More...
 
static void SPS_GlobalRegisterWrite (const uint16_t address, uint8_t writeData, uint16_t *pSpiTxBuffer)
 Sets the SPI Tx buffer for a register write access for all SPS ICs of the daisy-chain. More...
 
static void SPS_SingleDeviceRegisterWrite (uint8_t device, const uint16_t address, uint8_t writeData, SPS_WRITE_TYPE_e writeType, uint16_t *pSpiTxBuffer)
 Sets the SPI Tx buffer for a register write access for a specific single SPS ICs in the daisy-chain. More...
 
static void SPS_RequestChannelState (SPS_CHANNEL_INDEX channelIndex, SPS_CHANNEL_FUNCTION_e channelFunction)
 Request the functional state of a channel. More...
 
void SPS_Ctrl (void)
 Control function for the CONT driver state machine. More...
 
void SPS_Initialize (void)
 Initialize IOs for the SPS driver. More...
 
void SPS_RequestContactorState (SPS_CHANNEL_INDEX channelIndex, SPS_CHANNEL_FUNCTION_e channelFunction)
 Request state of a contactor. More...
 
void SPS_RequestGeneralIOState (SPS_CHANNEL_INDEX channelIndex, SPS_CHANNEL_FUNCTION_e channelFunction)
 Request state of a general IO. More...
 
CONT_ELECTRICAL_STATE_TYPE_e SPS_GetChannelFeedback (SPS_CHANNEL_INDEX channelIndex)
 Get feedback value. More...
 
SPS_CHANNEL_AFFILIATION_e SPS_GetChannelAffiliation (SPS_CHANNEL_INDEX channelIndex)
 Returns the channel affiliation. More...
 

Variables

static SPS_STATE_e sps_state = SPS_START
 
static uint8_t sps_timer = 0
 
static uint16_t sps_spiTxRegisterBuffer [SPS_SPI_BUFFERSIZE] = {0}
 
static uint16_t sps_spiRxRegisterBuffer [SPS_SPI_BUFFERSIZE] = {0}
 
static uint16_t sps_spiTxWriteToChannelChannelControlRegister [SPS_SPI_BUFFERSIZE] = {0}
 
static uint16_t sps_spiRxReadAnswerDuringChannelControl [SPS_SPI_BUFFERSIZE] = {0}
 

Detailed Description

Driver for the smart power switches.

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
2020-10-14 (date of creation)
Updated
2021-07-14 (date of last update)
Prefix
SPS

Definition in file sps.c.

Macro Definition Documentation

◆ SPS_RESET_WAIT_TIME_TICKS

#define SPS_RESET_WAIT_TIME_TICKS   (5u)

amount of ticks to wait when toggling the reset

Definition at line 104 of file sps.c.

Function Documentation

◆ SPS_Ctrl()

void SPS_Ctrl ( void  )

Control function for the CONT driver state machine.

This function contains the sequence of events in the SPS state machine. It must be called time-triggered, every 10ms. It exits without effect, if the function call is a reentrance.

Definition at line 466 of file sps.c.

Here is the call graph for this function:

◆ SPS_GetChannelAffiliation()

SPS_CHANNEL_AFFILIATION_e SPS_GetChannelAffiliation ( SPS_CHANNEL_INDEX  channelIndex)

Returns the channel affiliation.

Parameters
[in]channelIndexindex of the channel in question
Returns
affiliation of the channel

Definition at line 613 of file sps.c.

◆ SPS_GetChannelFeedback()

CONT_ELECTRICAL_STATE_TYPE_e SPS_GetChannelFeedback ( SPS_CHANNEL_INDEX  channelIndex)

Get feedback value.

This function compares the measured current in the contactor channel with a fixed threshold and assumes the contactor feedback to be closed when this threshold is passed on the channel.

Parameters
[in]channelIndexindex of the channel (contactor) that should be accessed
Returns
state of the channel's feedback

Definition at line 597 of file sps.c.

Here is the call graph for this function:

◆ SPS_GlobalReadCurrent()

static void SPS_GlobalReadCurrent ( const uint8_t  outputAllDevices)
static

Read the current measurements for a specific output for all SPS ICs of the daisy-chain.

A read command must be issued for all SPS ICs in the daisy chain first. There is one register to read for each output. This funcion i used to retrieve the answer on the MISO line after the write command to the contactors output registers was made. The result is written to SPS_CoilCurrent[].

Parameters
[in]outputAllDevicesOutput (1 to 4) to be read. Value between 1-4 instead of 0-3 to match numbering in datasheet.
Returns
TODO

Definition at line 443 of file sps.c.

◆ SPS_GlobalRegisterRead()

static void SPS_GlobalRegisterRead ( const uint16_t  address,
const SPS_READ_TYPE_e  controlOrDiagnostic,
uint16_t *  pSpiTxBuffer 
)
static

Sets the SPI Tx buffer for a register read access for all SPS ICs of the daisy-chain.

The same command is issued for all SPS ICs in the daisy chain. This means that the same register is read to all SPS ICs in the daisy chain.

Parameters
[in]addressaddress of register accessed
[in]controlOrDiagnostictype of read access, SPS_READ_DIAGNOSTIC_REGISTER for diagnostic register, SPS_READ_CONTROL_REGISTER for control register.
[out]pSpiTxBufferTx buffer used for the transmission

Definition at line 256 of file sps.c.

◆ SPS_GlobalRegisterWrite()

static void SPS_GlobalRegisterWrite ( const uint16_t  address,
uint8_t  writeData,
uint16_t *  pSpiTxBuffer 
)
static

Sets the SPI Tx buffer for a register write access for all SPS ICs of the daisy-chain.

The same command is issued for all SPS ICs in the daisy chain. This means that the same value is written to all SPS ICs in the daisy chain. The current value in register is overwritten by writeData.

Parameters
[in]addressaddress of register accessed
[in]writeDatadata to write
[out]pSpiTxBufferTx buffer used for the transmission

Definition at line 277 of file sps.c.

◆ SPS_Initialize()

void SPS_Initialize ( void  )

Initialize IOs for the SPS driver.

This function sets the necessary register (e.g., configure pins as input or output).

Definition at line 583 of file sps.c.

Here is the call graph for this function:

◆ SPS_InitializeBuffers()

static void SPS_InitializeBuffers ( void  )
static

Init TX buffers with 0

Definition at line 369 of file sps.c.

Here is the call graph for this function:

◆ SPS_InitializeIo()

static void SPS_InitializeIo ( void  )
static

initialize the IO ports for the contactor driver

Pin to drive reset line of SPS ICs

Pin to drive Chip Select line of SPS ICs

Definition at line 249 of file sps.c.

◆ SPS_RequestChannelState()

static void SPS_RequestChannelState ( SPS_CHANNEL_INDEX  channelIndex,
SPS_CHANNEL_FUNCTION_e  channelFunction 
)
static

Request the functional state of a channel.

Pass a value of SPS_CHANNEL_FUNCTION_e and a channel index number in order to request this state.

Parameters
[in]channelIndexnumber of the channel that should be accessed; do not confuse with contactor channel!
[in]channelFunctionrequested functional state of the channel

Definition at line 456 of file sps.c.

Here is the call graph for this function:

◆ SPS_RequestContactorState()

void SPS_RequestContactorState ( SPS_CHANNEL_INDEX  channelIndex,
SPS_CHANNEL_FUNCTION_e  channelFunction 
)

Request state of a contactor.

This function checks if the requested channel is affiliated to contactors and then passes the arguments on to SPS_RequestChannelState().

Parameters
[in]channelIndexnumber of the channel that should be accessed; This has to be a SPS channel and it has to be affiliated with SPS_AFF_CONTACTOR
[in]channelFunctionrequested functional state of the channel

Definition at line 587 of file sps.c.

Here is the call graph for this function:

◆ SPS_RequestGeneralIOState()

void SPS_RequestGeneralIOState ( SPS_CHANNEL_INDEX  channelIndex,
SPS_CHANNEL_FUNCTION_e  channelFunction 
)

Request state of a general IO.

This function checks if the requested channel is affiliated to general IO and then passes the arguments on to SPS_RequestChannelState().

Parameters
[in]channelIndexnumber of the channel that should be accessed; This has to be a SPS channel and it has to be affiliated with SPS_AFF_GENERAL_IO
[in]channelFunctionrequested functional state of the channel

Definition at line 592 of file sps.c.

Here is the call graph for this function:

◆ SPS_SetCommandTxBuffer()

static void SPS_SetCommandTxBuffer ( const SPS_ACTION_e  action)
static

Assemble a general command to the SPS ICs.

For each call of the SPS_Ctrl() function two transactions are sent. For details please read the documentation of SPS_Transmit().

This function has to be called before the transaction in order to update the TX buffer with a general command to the SPS chips in the daisy-chain. The actual command (and with that registers) that shall be accessed and sent is controlled by the parameter that is passed on with this function. *

Parameters
[in]actiondecides which command should be used

Definition at line 319 of file sps.c.

Here is the call graph for this function:

◆ SPS_SetContactorsTxBuffer()

static void SPS_SetContactorsTxBuffer ( void  )
static

Assemble an update to the channel state of the SPS ICs.

For each call of the SPS_Ctrl() function two transactions are sent. For details please read the documentation of SPS_Transmit().

This function has to be called before the transaction in order to update the TX buffer with a channel configuration to the SPS chips in the daisy-chain. The actual contactor or channel state is taken from the current state of the array sps_channelStatus.

With this computation, the contactor number is linked to the SPS IC position in the daisy-chain which is driving this contactor. Contactor 0-3: SPS IC 0 Contactor 4-7: SPS IC 1 Contactor 8-11: SPS IC 2 ...

Definition at line 381 of file sps.c.

Here is the call graph for this function:

◆ SPS_SingleDeviceRegisterWrite()

static void SPS_SingleDeviceRegisterWrite ( uint8_t  device,
const uint16_t  address,
uint8_t  writeData,
SPS_WRITE_TYPE_e  writeType,
uint16_t *  pSpiTxBuffer 
)
static

Sets the SPI Tx buffer for a register write access for a specific single SPS ICs in the daisy-chain.

The same command is issued for all SPS ICs in the daisy chain. This means that the same value is written to all SPS ICs in the daisy chain. This function does not set the Tx buffer to 0. This must be done manually.

Parameters
[in]devicedevice number in the daisy-chain to be written to.
[in]addressaddress of register accessed
[in]writeDatadata to write. In case of read access, this field is ignored and can be set to 0u.
[in]writeTypedefines wether the value in register should be replaced by writeData, or or-ed with writeType, or and-ed with writeData
[out]pSpiTxBufferTx buffer used for the transmission

Definition at line 286 of file sps.c.

◆ SPS_Transmit()

static STD_RETURN_TYPE_e SPS_Transmit ( void  )
static

Transmit all assembled messages for one timeframe.

This function leverages the fact that the SPS are connected in a "daisy-chain". This way the chain acts as a large shift register. When sending a message into the chain it is clocked in until every chip in the chain as its message. Then chip-select is released and the messages parsed. The answer is then assembled by the chip in its register and clocked out and received by the master with the next transmission.

This function sends out a general command to each chip that has been assembled with SPS_SetCommandTxBuffer(). The clocked out information that is received of this transaction is stored in a separate buffer and is the answer from the last transaction of the timeframe before the current one.

After this transaction the answer to the general command is retrieved and the transmission is used to clock in an update of the state of the contactor channels. This update has been assembled before the call of this function with the function SPS_SetContactorsTxBuffer().

Returns
returns STD_OK if both transactions have been successful

Definition at line 421 of file sps.c.

Here is the call graph for this function:

Variable Documentation

◆ sps_spiRxReadAnswerDuringChannelControl

uint16_t sps_spiRxReadAnswerDuringChannelControl[SPS_SPI_BUFFERSIZE] = {0}
static

SPI buffer: Tx part used to write to contactor output control register Rx part used to read answer from former read register command

When a read register command was issued on the MOSI line, the answers comes during the next command (read or write) on the MISO line (datasheet figure 6 page 9 in datasheet Rev. 2 - 11 September 2019)

In the SPS driver, there are always two communications with the SPS IC each time the state machine is triggered:

  • first a register access (read or write)
  • then a write access to the channel output control register This way, the channel outputs are switched each time the state machine is triggered, and during the second access, if a read access was done before, the answer comes during the second access.

Definition at line 100 of file sps.c.

◆ sps_spiRxRegisterBuffer

uint16_t sps_spiRxRegisterBuffer[SPS_SPI_BUFFERSIZE] = {0}
static

SPI buffer: Tx part used to issue a write or read register command Rx part is unused

Definition at line 78 of file sps.c.

◆ sps_spiTxRegisterBuffer

uint16_t sps_spiTxRegisterBuffer[SPS_SPI_BUFFERSIZE] = {0}
static

SPI buffer: Tx part used to issue a write or read register command Rx part is unused

Definition at line 77 of file sps.c.

◆ sps_spiTxWriteToChannelChannelControlRegister

uint16_t sps_spiTxWriteToChannelChannelControlRegister[SPS_SPI_BUFFERSIZE] = {0}
static

SPI buffer: Tx part used to write to contactor output control register Rx part used to read answer from former read register command

When a read register command was issued on the MOSI line, the answers comes during the next command (read or write) on the MISO line (datasheet figure 6 page 9 in datasheet Rev. 2 - 11 September 2019)

In the SPS driver, there are always two communications with the SPS IC each time the state machine is triggered:

  • first a register access (read or write)
  • then a write access to the channel output control register This way, the channel outputs are switched each time the state machine is triggered, and during the second access, if a read access was done before, the answer comes during the second access.

Definition at line 99 of file sps.c.

◆ sps_state

SPS_STATE_e sps_state = SPS_START
static

state indicator of the state-machine

Definition at line 67 of file sps.c.

◆ sps_timer

uint8_t sps_timer = 0
static

timer for the state-machine

Definition at line 70 of file sps.c.