foxBMS  1.2.1
The foxBMS Battery Management System API Documentation
spi_cfg.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2021, 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 2021-12-08 (date of last update)
47  * @ingroup DRIVERS_CONFIGURATION
48  * @prefix SPI
49  *
50  * @brief Configuration for the SPI module
51  *
52  *
53  *
54  */
55 
56 /*========== Includes =======================================================*/
57 #include "spi_cfg.h"
58 
59 #include "spi_cfg-helper.h"
60 
61 /*========== Macros and Definitions =========================================*/
62 
63 /*========== Static Constant and Variable Definitions =======================*/
64 
65 /** @defgroup spi_data_format SPI data configuration structs
66  *
67  * These data configuration structs are used below in the
68  * #SPI_INTERFACE_CONFIG_s structures. Each data configuration structs refers
69  * to a format selection (e.g. SPI_FMT_0). These are defined in the HAL for
70  * each SPI channel (1-5). In order to limit potential confusion the data
71  * formats on SPI1 and SPI4 (the SPI interfaces that are routed to the
72  * interface board that contains the AFE) have to be configured the same.
73  * This way, the developer can switch between SPI1 and SPI4 without having to
74  * change the SPI data format.
75  *
76  * AFE | SPI data format
77  * ------- | ---------------
78  * LTC | SPI_FMT_0
79  * MXM | SPI_FMT_1
80  * NXP | SPI_FMT_2
81  * unused | SPI_FMT_3
82  *
83  * For hardware Chip Select, setting one bit to 0 in the CSNR field of the
84  * SPIDAT1 regsiter activates the corresponding Chip Select pin.
85  */
86 
87 /** SPI data configuration struct for LTC communication */
88 static spiDAT1_t spi_kLtcDataConfig[BS_NR_OF_STRINGS] = {
89  { /* struct is implemented in the TI HAL and uses uppercase true and false */
90  .CS_HOLD = TRUE, /* If true, HW chip select kept active between words */
91  .WDEL = FALSE, /* Activation of delay between words */
92  .DFSEL = SPI_FMT_0, /* Data word format selection */
94  { /* struct is implemented in the TI HAL and uses uppercase true and false */
95  .CS_HOLD = TRUE, /* If true, HW chip select kept active */
96  .WDEL = FALSE, /* Activation of delay between words */
97  .DFSEL = SPI_FMT_0, /* Data word format selection */
99  { /* struct is implemented in the TI HAL and uses uppercase true and false */
100  .CS_HOLD = TRUE, /* If true, HW chip select kept active */
101  .WDEL = FALSE, /* Activation of delay between words */
102  .DFSEL = SPI_FMT_0, /* Data word format selection */
104 };
105 
106 /** SPI data configuration struct for MXM communication */
107 static spiDAT1_t spi_kMxmDataConfig = {
108  .CS_HOLD = TRUE, /* If true, HW chip select kept active */
109  .WDEL = TRUE, /* Activation of delay between words */
110  .DFSEL = SPI_FMT_1, /* Data word format selection */
112 
113 /** SPI data configuration struct for NXP MC33775A communication, Tx part */
115  {.CS_HOLD = TRUE, /* If true, HW chip select kept active */
116  .WDEL = TRUE, /* Activation of delay between words */
117  .DFSEL = SPI_FMT_0, /* Data word format selection */
119  {.CS_HOLD = TRUE, /* If true, HW chip select kept active */
120  .WDEL = TRUE, /* Activation of delay between words */
121  .DFSEL = SPI_FMT_0, /* Data word format selection */
123  {.CS_HOLD = TRUE, /* If true, HW chip select kept active */
124  .WDEL = TRUE, /* Activation of delay between words */
125  .DFSEL = SPI_FMT_0, /* Data word format selection */
127 };
128 
129 /** SPI data configuration struct for NXP MC33775A communication, Rx part */
131  {.CS_HOLD = TRUE, /* If true, HW chip select kept active */
132  .WDEL = TRUE, /* Activation of delay between words */
133  .DFSEL = SPI_FMT_0, /* Data word format selection */
135  {.CS_HOLD = TRUE, /* If true, HW chip select kept active */
136  .WDEL = TRUE, /* Activation of delay between words */
137  .DFSEL = SPI_FMT_0, /* Data word format selection */
139  {.CS_HOLD = TRUE, /* If true, HW chip select kept active */
140  .WDEL = TRUE, /* Activation of delay between words */
141  .DFSEL = SPI_FMT_0, /* Data word format selection */
143 };
144 
145 /** SPI data configuration struct for FRAM communication */
146 static spiDAT1_t spi_kFramDataConfig = {
147  /* struct is implemented in the TI HAL and uses uppercase true and false */
148  .CS_HOLD = TRUE, /* If true, HW chip select kept active */
149  .WDEL = TRUE, /* Activation of delay between words */
150  .DFSEL = SPI_FMT_1, /* Data word format selection */
152 
153 /** SPI data configuration struct for SPS communication in low speed (4MHz) */
154 static spiDAT1_t spi_kSpsDataConfigLowSpeed = {
155  /* struct is implemented in the TI HAL and uses uppercase true and false */
156  .CS_HOLD = TRUE, /* If true, HW chip select kept active */
157  .WDEL = TRUE, /* Activation of delay between words */
158  .DFSEL = SPI_FMT_1, /* Data word format selection */
160 };
161 
162 /** SPI data configuration struct for SPS communication in high speed (10MHz) */
163 static spiDAT1_t spi_kSpsDataConfigHighSpeed = {
164  /* struct is implemented in the TI HAL and uses uppercase true and false */
165  .CS_HOLD = TRUE, /* If true, HW chip select kept active */
166  .WDEL = TRUE, /* Activation of delay between words */
167  .DFSEL = SPI_FMT_2, /* Data word format selection */
169 
170 /** SPI data configuration struct for ADC communication */
171 static spiDAT1_t spi_kAdc0DataConfig = {
172  /* struct is implemented in the TI HAL and uses uppercase true and false */
173  .CS_HOLD = TRUE, /* If true, HW chip select kept active */
174  .WDEL = TRUE, /* Activation of delay between words */
175  .DFSEL = SPI_FMT_2, /* Data word format selection */
177 
178 /** SPI data configuration struct for ADC communication */
179 static spiDAT1_t spi_kAdc1DataConfig = {
180  /* struct is implemented in the TI HAL and uses uppercase true and false */
181  .CS_HOLD = TRUE, /* If true, HW chip select kept active */
182  .WDEL = TRUE, /* Activation of delay between words */
183  .DFSEL = SPI_FMT_2, /* Data word format selection */
185 
186 /** SPI configuration struct for SBC communication */
187 static spiDAT1_t spi_kSbcDataConfig = {
188  /* struct is implemented in the TI HAL and uses uppercase true and false */
189  .CS_HOLD = TRUE, /* If true, HW chip select kept active */
190  .WDEL = TRUE, /* Activation of delay between words */
191  .DFSEL = SPI_FMT_0, /* Data word format selection */
193 
194 /*========== Extern Constant and Variable Definitions =======================*/
195 
196 /**
197  * SPI interface configuration for LTC communication
198  * This is a list of structs because of multistring
199  */
201  {
202  .pConfig = &spi_kLtcDataConfig[0u],
203  .pNode = spiREG1,
204  .pGioPort = &(spiREG1->PC3),
205  .csPin = 2u,
206  .csType = SPI_CHIP_SELECT_HARDWARE,
207  },
208  {
209  .pConfig = &spi_kLtcDataConfig[1u],
210  .pNode = spiREG1,
211  .pGioPort = &(spiREG1->PC3),
212  .csPin = 2u,
213  .csType = SPI_CHIP_SELECT_HARDWARE,
214  },
215  {
216  .pConfig = &spi_kLtcDataConfig[2u],
217  .pNode = spiREG1,
218  .pGioPort = &(spiREG1->PC3),
219  .csPin = 2u,
220  .csType = SPI_CHIP_SELECT_HARDWARE,
221  },
222 };
223 
224 /** SPI interface configuration for MXM communication */
227  .pNode = spiREG4,
228  .pGioPort = &(spiREG4->PC3),
229  .csPin = 0u,
230  .csType = SPI_CHIP_SELECT_HARDWARE,
231 };
232 
233 /** SPI interface configuration for N775 communication Tx part */
235  {
237  .pNode = spiREG1,
238  .pGioPort = &(spiREG1->PC3),
239  .csPin = 2u,
240  .csType = SPI_CHIP_SELECT_HARDWARE,
241  },
242  {
243  .pConfig = &spi_kNxp775DataConfigTx[1u],
244  .pNode = spiREG1,
245  .pGioPort = &(spiREG1->PC3),
246  .csPin = 2u,
247  .csType = SPI_CHIP_SELECT_HARDWARE,
248  },
249  {
250  .pConfig = &spi_kNxp775DataConfigTx[2u],
251  .pNode = spiREG1,
252  .pGioPort = &(spiREG1->PC3),
253  .csPin = 2u,
254  .csType = SPI_CHIP_SELECT_HARDWARE,
255  },
256 };
257 
258 /** SPI interface configuration for N775 communication, Rx part */
260  {
262  .pNode = spiREG4,
263  .pGioPort = &(spiREG4->PC3),
264  .csPin = 0u,
265  .csType = SPI_CHIP_SELECT_HARDWARE,
266  },
267  {
268  .pConfig = &spi_kNxp775DataConfigRx[1u],
269  .pNode = spiREG4,
270  .pGioPort = &(spiREG4->PC3),
271  .csPin = 0u,
272  .csType = SPI_CHIP_SELECT_HARDWARE,
273  },
274  {
275  .pConfig = &spi_kNxp775DataConfigRx[2u],
276  .pNode = spiREG4,
277  .pGioPort = &(spiREG4->PC3),
278  .csPin = 0u,
279  .csType = SPI_CHIP_SELECT_HARDWARE,
280  },
281 };
282 
283 /** SPI interface configuration for FRAM communication */
286  .pNode = spiREG3,
287  .pGioPort = &(spiREG3->PC3),
288  .csPin = 1u,
289  .csType = SPI_CHIP_SELECT_SOFTWARE,
290 };
291 
292 /** SPI interface configuration for SPS communication */
295  .pNode = spiREG2,
296  .pGioPort = &SPS_SPI_CS_GIOPORT,
297  .csPin = SPS_SPI_CS_PIN,
298  .csType = SPI_CHIP_SELECT_SOFTWARE,
299 };
300 
301 /** SPI interface configuration for ADC communication */
304  .pNode = spiREG3,
305  .pGioPort = &(spiREG3->PC3),
306  .csPin = 4u,
307  .csType = SPI_CHIP_SELECT_SOFTWARE,
308 };
309 
310 /** SPI interface configuration for ADC communication */
313  .pNode = spiREG3,
314  .pGioPort = &(spiREG3->PC3),
315  .csPin = 5u,
316  .csType = SPI_CHIP_SELECT_SOFTWARE,
317 };
318 
319 /** SPI interface configuration for SBC communication */
322  .pNode = spiREG2,
323  .pGioPort = &(spiREG2->PC3),
324  .csPin = 0u,
325  .csType = SPI_CHIP_SELECT_HARDWARE,
326 };
327 
328 /** struct containing the lock state of the SPI interfaces */
330  SPI_IDLE,
331  SPI_IDLE,
332  SPI_IDLE,
333  SPI_IDLE,
334  SPI_IDLE,
335 };
336 
337 /** size of #spi_busyFlags */
338 const uint8_t spi_nrBusyFlags = sizeof(spi_busyFlags) / sizeof(SPI_BUSY_STATE_e);
339 
340 /*========== Static Function Prototypes =====================================*/
341 
342 /*========== Static Function Implementations ================================*/
343 
344 /*========== Extern Function Implementations ================================*/
346  FAS_ASSERT(pSpiSpsInterface != NULL_PTR);
347  pSpiSpsInterface->pConfig = &spi_kSpsDataConfigHighSpeed;
348 }
349 
351  FAS_ASSERT(pSpiSpsInterface != NULL_PTR);
352  pSpiSpsInterface->pConfig = &spi_kSpsDataConfigLowSpeed;
353 }
354 
355 /*========== Externalized Static Function Implementations (Unit Test) =======*/
#define BS_NR_OF_STRINGS
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:239
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:75
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:350
static spiDAT1_t spi_kNxp775DataConfigRx[BS_NR_OF_STRINGS]
Definition: spi_cfg.c:130
SPI_INTERFACE_CONFIG_s spi_MxmInterface
Definition: spi_cfg.c:225
static spiDAT1_t spi_kSbcDataConfig
Definition: spi_cfg.c:187
SPI_INTERFACE_CONFIG_s spi_adc0Interface
Definition: spi_cfg.c:302
SPI_INTERFACE_CONFIG_s spi_kSbcMcuInterface
Definition: spi_cfg.c:320
SPI_INTERFACE_CONFIG_s spi_nxp775InterfaceTx[BS_NR_OF_STRINGS]
Definition: spi_cfg.c:234
static spiDAT1_t spi_kAdc0DataConfig
Definition: spi_cfg.c:171
static spiDAT1_t spi_kNxp775DataConfigTx[BS_NR_OF_STRINGS]
Definition: spi_cfg.c:114
SPI_BUSY_STATE_e spi_busyFlags[]
Definition: spi_cfg.c:329
static spiDAT1_t spi_kAdc1DataConfig
Definition: spi_cfg.c:179
SPI_INTERFACE_CONFIG_s spi_framInterface
Definition: spi_cfg.c:284
static spiDAT1_t spi_kFramDataConfig
Definition: spi_cfg.c:146
static spiDAT1_t spi_kLtcDataConfig[BS_NR_OF_STRINGS]
Definition: spi_cfg.c:88
SPI_INTERFACE_CONFIG_s spi_adc1Interface
Definition: spi_cfg.c:311
void SPI_SpsInterfaceSwitchToHighSpeed(SPI_INTERFACE_CONFIG_s *pSpiSpsInterface)
Switch the SPS spi handle to high speed.
Definition: spi_cfg.c:345
static spiDAT1_t spi_kMxmDataConfig
Definition: spi_cfg.c:107
SPI_INTERFACE_CONFIG_s spi_nxp775InterfaceRx[BS_NR_OF_STRINGS]
Definition: spi_cfg.c:259
static spiDAT1_t spi_kSpsDataConfigHighSpeed
Definition: spi_cfg.c:163
SPI_INTERFACE_CONFIG_s spi_ltcInterface[BS_NR_OF_STRINGS]
Definition: spi_cfg.c:200
const uint8_t spi_nrBusyFlags
Definition: spi_cfg.c:338
SPI_INTERFACE_CONFIG_s spi_spsInterface
Definition: spi_cfg.c:293
static spiDAT1_t spi_kSpsDataConfigLowSpeed
Definition: spi_cfg.c:154
Headers for the configuration for the SPI module.
@ SPI_IDLE
Definition: spi_cfg.h:125
#define SPS_SPI_CS_PIN
Definition: spi_cfg.h:107
@ SPI_CHIP_SELECT_SOFTWARE
Definition: spi_cfg.h:133
@ SPI_CHIP_SELECT_HARDWARE
Definition: spi_cfg.h:132
enum SPI_BUSY_STATE SPI_BUSY_STATE_e
#define SPS_SPI_CS_GIOPORT
Definition: spi_cfg.h:105
spiDAT1_t * pConfig
Definition: spi_cfg.h:140