foxBMS  1.4.1
The foxBMS Battery Management System API Documentation
spi_cfg.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2022, Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice, this
12  * list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * We kindly request you to use one or more of the following phrases to refer to
34  * foxBMS in your hardware, software, documentation or advertising materials:
35  *
36  * - ″This product uses parts of foxBMS®″
37  * - ″This product includes parts of foxBMS®″
38  * - ″This product is derived from foxBMS®″
39  *
40  */
41 
42 /**
43  * @file spi_cfg.c
44  * @author foxBMS Team
45  * @date 2020-03-05 (date of creation)
46  * @updated 2022-10-27 (date of last update)
47  * @version v1.4.1
48  * @ingroup DRIVERS_CONFIGURATION
49  * @prefix SPI
50  *
51  * @brief Configuration for the SPI module
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "spi_cfg.h"
57 
58 #include "spi_cfg-helper.h"
59 
60 /*========== Macros and Definitions =========================================*/
61 
62 /*========== Static Constant and Variable Definitions =======================*/
63 
64 /** @defgroup spi_data_format SPI data configuration structs
65  *
66  * These data configuration structs are used below in the
67  * #SPI_INTERFACE_CONFIG_s structures. Each data configuration structs refers
68  * to a format selection (e.g. SPI_FMT_0). These are defined in the HAL for
69  * each SPI channel (1-5). In order to limit potential confusion the data
70  * formats on SPI1 and SPI4 (the SPI interfaces that are routed to the
71  * interface board that contains the AFE) have to be configured the same.
72  * This way, the developer can switch between SPI1 and SPI4 without having to
73  * change the SPI data format.
74  *
75  * AFE | SPI data format
76  * ------- | ---------------
77  * LTC | SPI_FMT_0
78  * MXM | SPI_FMT_1
79  * NXP | SPI_FMT_2
80  * unused | SPI_FMT_3
81  *
82  * For hardware Chip Select, setting one bit to 0 in the CSNR field of the
83  * SPIDAT1 regsiter activates the corresponding Chip Select pin.
84  */
85 
86 /** SPI data configuration struct for LTC communication */
87 static spiDAT1_t spi_kLtcDataConfig[BS_NR_OF_STRINGS] = {
88  { /* struct is implemented in the TI HAL and uses uppercase true and false */
89  .CS_HOLD = TRUE, /* If true, HW chip select kept active between words */
90  .WDEL = FALSE, /* Activation of delay between words */
91  .DFSEL = SPI_FMT_0, /* Data word format selection */
93 };
94 
95 /** SPI data configuration struct for MXM communication */
96 static spiDAT1_t spi_kMxmDataConfig = {
97  .CS_HOLD = TRUE, /* If true, HW chip select kept active */
98  .WDEL = TRUE, /* Activation of delay between words */
99  .DFSEL = SPI_FMT_1, /* Data word format selection */
101 
102 /** SPI data configuration struct for NXP MC33775A communication, Tx part */
104  {.CS_HOLD = TRUE, /* If true, HW chip select kept active */
105  .WDEL = TRUE, /* Activation of delay between words */
106  .DFSEL = SPI_FMT_2, /* Data word format selection */
108 };
109 
110 /** SPI data configuration struct for NXP MC33775A communication, Rx part */
112  {.CS_HOLD = TRUE, /* If true, HW chip select kept active */
113  .WDEL = TRUE, /* Activation of delay between words */
114  .DFSEL = SPI_FMT_2, /* Data word format selection */
116 };
117 
118 /** SPI data configuration struct for FRAM communication */
119 static spiDAT1_t spi_kFramDataConfig = {
120  /* struct is implemented in the TI HAL and uses uppercase true and false */
121  .CS_HOLD = TRUE, /* If true, HW chip select kept active */
122  .WDEL = TRUE, /* Activation of delay between words */
123  .DFSEL = SPI_FMT_1, /* Data word format selection */
125 
126 /** SPI data configuration struct for SPS communication in low speed (4MHz) */
127 static spiDAT1_t spi_kSpsDataConfigLowSpeed = {
128  /* struct is implemented in the TI HAL and uses uppercase true and false */
129  .CS_HOLD = TRUE, /* If true, HW chip select kept active */
130  .WDEL = TRUE, /* Activation of delay between words */
131  .DFSEL = SPI_FMT_1, /* Data word format selection */
133 };
134 
135 /** SPI data configuration struct for SPS communication in high speed (10MHz) */
136 static spiDAT1_t spi_kSpsDataConfigHighSpeed = {
137  /* struct is implemented in the TI HAL and uses uppercase true and false */
138  .CS_HOLD = TRUE, /* If true, HW chip select kept active */
139  .WDEL = TRUE, /* Activation of delay between words */
140  .DFSEL = SPI_FMT_2, /* Data word format selection */
142 
143 /** SPI data configuration struct for ADC communication */
144 static spiDAT1_t spi_kAdc0DataConfig = {
145  /* struct is implemented in the TI HAL and uses uppercase true and false */
146  .CS_HOLD = TRUE, /* If true, HW chip select kept active */
147  .WDEL = TRUE, /* Activation of delay between words */
148  .DFSEL = SPI_FMT_2, /* Data word format selection */
150 
151 /** SPI data configuration struct for ADC communication */
152 static spiDAT1_t spi_kAdc1DataConfig = {
153  /* struct is implemented in the TI HAL and uses uppercase true and false */
154  .CS_HOLD = TRUE, /* If true, HW chip select kept active */
155  .WDEL = TRUE, /* Activation of delay between words */
156  .DFSEL = SPI_FMT_2, /* Data word format selection */
158 
159 /** SPI configuration struct for SBC communication */
160 static spiDAT1_t spi_kSbcDataConfig = {
161  /* struct is implemented in the TI HAL and uses uppercase true and false */
162  .CS_HOLD = TRUE, /* If true, HW chip select kept active */
163  .WDEL = TRUE, /* Activation of delay between words */
164  .DFSEL = SPI_FMT_0, /* Data word format selection */
166 
167 /*========== Extern Constant and Variable Definitions =======================*/
168 
169 /**
170  * SPI interface configuration for LTC communication
171  * This is a list of structs because of multistring
172  */
174  {
175  .pConfig = &spi_kLtcDataConfig[0u],
176  .pNode = spiREG1,
177  .pGioPort = &(spiREG1->PC3),
178  .csPin = 2u,
179  .csType = SPI_CHIP_SELECT_HARDWARE,
180  },
181 };
182 
183 /** SPI interface configuration for MXM communication */
186  .pNode = spiREG4,
187  .pGioPort = &(spiREG4->PC3),
188  .csPin = 0u,
189  .csType = SPI_CHIP_SELECT_HARDWARE,
190 };
191 
192 /** SPI interface configuration for N775 communication Tx part */
194  {
196  .pNode = spiREG1,
197  .pGioPort = &(spiREG1->PC3),
198  .csPin = 1u,
199  .csType = SPI_CHIP_SELECT_HARDWARE,
200  },
201 };
202 
203 /** SPI interface configuration for N775 communication, Rx part */
205  {
207  .pNode = spiREG4,
208  .pGioPort = &(spiREG4->PC3),
209  .csPin = 0u,
210  .csType = SPI_CHIP_SELECT_HARDWARE,
211  },
212 };
213 
214 /** SPI interface configuration for FRAM communication */
217  .pNode = spiREG3,
218  .pGioPort = &(spiREG3->PC3),
219  .csPin = 1u,
220  .csType = SPI_CHIP_SELECT_SOFTWARE,
221 };
222 
223 /** SPI interface configuration for SPS communication */
226  .pNode = spiREG2,
227  .pGioPort = &SPI_SPS_CS_GIOPORT,
228  .csPin = SPI_SPS_CS_PIN,
229  .csType = SPI_CHIP_SELECT_SOFTWARE,
230 };
231 
232 /** SPI interface configuration for ADC communication */
235  .pNode = spiREG3,
236  .pGioPort = &(spiREG3->PC3),
237  .csPin = 4u,
238  .csType = SPI_CHIP_SELECT_SOFTWARE,
239 };
240 
241 /** SPI interface configuration for ADC communication */
244  .pNode = spiREG3,
245  .pGioPort = &(spiREG3->PC3),
246  .csPin = 5u,
247  .csType = SPI_CHIP_SELECT_SOFTWARE,
248 };
249 
250 /** SPI interface configuration for SBC communication */
253  .pNode = spiREG2,
254  .pGioPort = &(spiREG2->PC3),
255  .csPin = 0u,
256  .csType = SPI_CHIP_SELECT_HARDWARE,
257 };
258 
259 /** struct containing the lock state of the SPI interfaces */
261  SPI_IDLE,
262  SPI_IDLE,
263  SPI_IDLE,
264  SPI_IDLE,
265  SPI_IDLE,
266 };
267 
268 /** size of #spi_busyFlags */
269 const uint8_t spi_nrBusyFlags = sizeof(spi_busyFlags) / sizeof(SPI_BUSY_STATE_e);
270 
271 /*========== Static Function Prototypes =====================================*/
272 
273 /*========== Static Function Implementations ================================*/
274 
275 /*========== Extern Function Implementations ================================*/
277  FAS_ASSERT(pSpiSpsInterface != NULL_PTR);
278  pSpiSpsInterface->pConfig = &spi_kSpsDataConfigHighSpeed;
279 }
280 
282  FAS_ASSERT(pSpiSpsInterface != NULL_PTR);
283  pSpiSpsInterface->pConfig = &spi_kSpsDataConfigLowSpeed;
284 }
285 
286 /*========== Externalized Static Function Implementations (Unit Test) =======*/
#define BS_NR_OF_STRINGS
Number of parallel strings in the battery pack.
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:252
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:76
Headers for the configuration for the SPI module.
#define SPI_HARDWARE_CHIP_SELECT_0_ACTIVE
#define SPI_HARDWARE_CHIP_SELECT_1_ACTIVE
#define SPI_HARDWARE_CHIP_SELECT_4_ACTIVE
#define SPI_HARDWARE_CHIP_SELECT_5_ACTIVE
#define SPI_HARDWARE_CHIP_SELECT_2_ACTIVE
#define SPI_HARDWARE_CHIP_SELECT_DISABLE_ALL
void SPI_SpsInterfaceSwitchToLowSpeed(SPI_INTERFACE_CONFIG_s *pSpiSpsInterface)
Switch the SPS spi handle to low speed.
Definition: spi_cfg.c:281
static spiDAT1_t spi_kNxp775DataConfigRx[BS_NR_OF_STRINGS]
Definition: spi_cfg.c:111
SPI_INTERFACE_CONFIG_s spi_mxmInterface
Definition: spi_cfg.c:184
static spiDAT1_t spi_kSbcDataConfig
Definition: spi_cfg.c:160
SPI_INTERFACE_CONFIG_s spi_adc0Interface
Definition: spi_cfg.c:233
SPI_INTERFACE_CONFIG_s spi_kSbcMcuInterface
Definition: spi_cfg.c:251
SPI_INTERFACE_CONFIG_s spi_nxp775InterfaceTx[BS_NR_OF_STRINGS]
Definition: spi_cfg.c:193
static spiDAT1_t spi_kAdc0DataConfig
Definition: spi_cfg.c:144
static spiDAT1_t spi_kNxp775DataConfigTx[BS_NR_OF_STRINGS]
Definition: spi_cfg.c:103
SPI_BUSY_STATE_e spi_busyFlags[]
Definition: spi_cfg.c:260
static spiDAT1_t spi_kAdc1DataConfig
Definition: spi_cfg.c:152
SPI_INTERFACE_CONFIG_s spi_framInterface
Definition: spi_cfg.c:215
static spiDAT1_t spi_kFramDataConfig
Definition: spi_cfg.c:119
static spiDAT1_t spi_kLtcDataConfig[BS_NR_OF_STRINGS]
Definition: spi_cfg.c:87
SPI_INTERFACE_CONFIG_s spi_adc1Interface
Definition: spi_cfg.c:242
void SPI_SpsInterfaceSwitchToHighSpeed(SPI_INTERFACE_CONFIG_s *pSpiSpsInterface)
Switch the SPS spi handle to high speed.
Definition: spi_cfg.c:276
static spiDAT1_t spi_kMxmDataConfig
Definition: spi_cfg.c:96
SPI_INTERFACE_CONFIG_s spi_nxp775InterfaceRx[BS_NR_OF_STRINGS]
Definition: spi_cfg.c:204
static spiDAT1_t spi_kSpsDataConfigHighSpeed
Definition: spi_cfg.c:136
SPI_INTERFACE_CONFIG_s spi_ltcInterface[BS_NR_OF_STRINGS]
Definition: spi_cfg.c:173
const uint8_t spi_nrBusyFlags
Definition: spi_cfg.c:269
SPI_INTERFACE_CONFIG_s spi_spsInterface
Definition: spi_cfg.c:224
static spiDAT1_t spi_kSpsDataConfigLowSpeed
Definition: spi_cfg.c:127
Headers for the configuration for the SPI module.
SPI_BUSY_STATE_e
Definition: spi_cfg.h:108
@ SPI_IDLE
Definition: spi_cfg.h:109
#define SPI_SPS_CS_GIOPORT
Definition: spi_cfg.h:102
@ SPI_CHIP_SELECT_SOFTWARE
Definition: spi_cfg.h:117
@ SPI_CHIP_SELECT_HARDWARE
Definition: spi_cfg.h:116
#define SPI_SPS_CS_PIN
Definition: spi_cfg.h:104
spiDAT1_t * pConfig
Definition: spi_cfg.h:124