foxBMS  1.5.0
The foxBMS Battery Management System API Documentation
spi_cfg.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2023, 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 2023-02-03 (date of last update)
47  * @version v1.5.0
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 #include <stdint.h>
61 
62 /*========== Macros and Definitions =========================================*/
63 
64 /*========== Static Constant and Variable Definitions =======================*/
65 
66 /** @defgroup spi_data_format SPI data configuration structs
67  *
68  * These data configuration structs are used below in the
69  * #SPI_INTERFACE_CONFIG_s structures. Each data configuration structs refers
70  * to a format selection (e.g. SPI_FMT_0). These are defined in the HAL for
71  * each SPI channel (1-5). In order to limit potential confusion the data
72  * formats on SPI1 and SPI4 (the SPI interfaces that are routed to the
73  * interface board that contains the AFE) have to be configured the same.
74  * This way, the developer can switch between SPI1 and SPI4 without having to
75  * change the SPI data format.
76  *
77  * AFE | SPI data format
78  * ------- | ---------------
79  * LTC | SPI_FMT_0
80  * MXM | SPI_FMT_1
81  * NXP | SPI_FMT_2
82  * unused | SPI_FMT_3
83  *
84  * For hardware Chip Select, setting one bit to 0 in the CSNR field of the
85  * SPIDAT1 register activates the corresponding Chip Select pin.
86  */
87 
88 /** SPI data configuration struct for LTC communication */
89 static spiDAT1_t spi_kLtcDataConfig[BS_NR_OF_STRINGS] = {
90  { /* struct is implemented in the TI HAL and uses uppercase true and false */
91  .CS_HOLD = TRUE, /* If true, HW chip select kept active between words */
92  .WDEL = FALSE, /* Activation of delay between words */
93  .DFSEL = SPI_FMT_0, /* Data word format selection */
94  /* Hardware chip select is configured automatically depending on configuration in #SPI_INTERFACE_CONFIG_s */
96 };
97 
98 /** SPI data configuration struct for MXM communication */
99 static spiDAT1_t spi_kMxmDataConfig = {
100  .CS_HOLD = TRUE, /* If true, HW chip select kept active */
101  .WDEL = TRUE, /* Activation of delay between words */
102  .DFSEL = SPI_FMT_1, /* Data word format selection */
103  /* Hardware chip select is configured automatically depending on configuration in #SPI_INTERFACE_CONFIG_s */
105 };
106 
107 /** SPI data configuration struct for NXP MC33775A communication, Tx part */
109  {.CS_HOLD = TRUE, /* If true, HW chip select kept active */
110  .WDEL = TRUE, /* Activation of delay between words */
111  .DFSEL = SPI_FMT_2, /* Data word format selection */
112  /* Hardware chip select is configured automatically depending on configuration in #SPI_INTERFACE_CONFIG_s */
114 };
115 
116 /** SPI data configuration struct for NXP MC33775A communication, Rx part */
118  {.CS_HOLD = TRUE, /* If true, HW chip select kept active */
119  .WDEL = TRUE, /* Activation of delay between words */
120  .DFSEL = SPI_FMT_2, /* Data word format selection */
121  /* Hardware chip select is configured automatically depending on configuration in #SPI_INTERFACE_CONFIG_s */
123 };
124 
125 /** SPI data configuration struct for FRAM communication */
126 static spiDAT1_t spi_kFramDataConfig = {
127  /* struct is implemented in the TI HAL and uses uppercase true and false */
128  .CS_HOLD = TRUE, /* If true, HW chip select kept active */
129  .WDEL = TRUE, /* Activation of delay between words */
130  .DFSEL = SPI_FMT_1, /* Data word format selection */
131  /* Hardware chip select is configured automatically depending on configuration in #SPI_INTERFACE_CONFIG_s */
133 };
134 
135 /** SPI data configuration struct for SPS communication in low speed (4MHz) */
136 static spiDAT1_t spi_kSpsDataConfigLowSpeed = {
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_1, /* Data word format selection */
141  /* Hardware chip select is configured automatically depending on configuration in #SPI_INTERFACE_CONFIG_s */
143 };
144 
145 /** SPI data configuration struct for SPS communication in high speed (10MHz) */
146 static spiDAT1_t spi_kSpsDataConfigHighSpeed = {
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_2, /* Data word format selection */
151  /* Hardware chip select is configured automatically depending on configuration in #SPI_INTERFACE_CONFIG_s */
153 };
154 
155 /** SPI configuration struct for SBC communication */
156 static spiDAT1_t spi_kSbcDataConfig = {
157  /* struct is implemented in the TI HAL and uses uppercase true and false */
158  .CS_HOLD = TRUE, /* If true, HW chip select kept active */
159  .WDEL = TRUE, /* Activation of delay between words */
160  .DFSEL = SPI_FMT_0, /* Data word format selection */
161  /* Hardware chip select is configured automatically depending on configuration in #SPI_INTERFACE_CONFIG_s */
163 };
164 
165 /*========== Extern Constant and Variable Definitions =======================*/
166 
167 /**
168  * SPI interface configuration for LTC communication
169  * This is a list of structs because of multistring
170  */
172  {
173  .pConfig = &spi_kLtcDataConfig[0u],
174  .pNode = spiREG1,
175  .pGioPort = &(spiREG1->PC3),
176  .csPin = SPI_LTC_CHIP_SELECT_PIN,
177  .csType = SPI_CHIP_SELECT_HARDWARE,
178  },
179 };
180 
181 /** SPI interface configuration for MXM communication */
184  .pNode = spiREG4,
185  .pGioPort = &(spiREG4->PC3),
186  .csPin = SPI_MAXIM_CHIP_SELECT_PIN,
187  .csType = SPI_CHIP_SELECT_HARDWARE,
188 };
189 
190 /** SPI interface configuration for N775 communication Tx part */
192  {
194  .pNode = spiREG1,
195  .pGioPort = &(spiREG1->PC3),
197  .csType = SPI_CHIP_SELECT_HARDWARE,
198  },
199 };
200 
201 /** SPI interface configuration for N775 communication, Rx part */
203  {
205  .pNode = spiREG4,
206  .pGioPort = &(spiREG4->PC3),
208  .csType = SPI_CHIP_SELECT_HARDWARE,
209  },
210 };
211 
212 /** SPI interface configuration for FRAM communication */
215  .pNode = spiREG3,
216  .pGioPort = &(spiREG3->PC3),
217  .csPin = SPI_FRAM_CHIP_SELECT_PIN,
218  .csType = SPI_CHIP_SELECT_SOFTWARE,
219 };
220 
221 /** SPI interface configuration for SPS communication */
224  .pNode = spiREG2,
225  .pGioPort = &SPI_SPS_CS_GIOPORT,
226  .csPin = SPI_SPS_CS_PIN,
227  .csType = SPI_CHIP_SELECT_SOFTWARE,
228 };
229 
230 /** SPI interface configuration for SBC communication */
233  .pNode = spiREG2,
234  .pGioPort = &(spiREG2->PC3),
236  .csType = SPI_CHIP_SELECT_HARDWARE,
237 };
238 
239 /** struct containing the lock state of the SPI interfaces */
241  SPI_IDLE,
242  SPI_IDLE,
243  SPI_IDLE,
244  SPI_IDLE,
245  SPI_IDLE,
246 };
247 
248 /** size of #spi_busyFlags */
249 const uint8_t spi_nrBusyFlags = sizeof(spi_busyFlags) / sizeof(SPI_BUSY_STATE_e);
250 
251 /*========== Static Function Prototypes =====================================*/
252 
253 /*========== Static Function Implementations ================================*/
254 
255 /*========== Extern Function Implementations ================================*/
257  FAS_ASSERT(pSpiSpsInterface != NULL_PTR);
258  pSpiSpsInterface->pConfig = &spi_kSpsDataConfigHighSpeed;
259 }
260 
262  FAS_ASSERT(pSpiSpsInterface != NULL_PTR);
263  pSpiSpsInterface->pConfig = &spi_kSpsDataConfigLowSpeed;
264 }
265 
266 /*========== Externalized Static Function Implementations (Unit Test) =======*/
267 #ifdef UNITY_UNIT_TEST
268 #endif
#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:254
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:77
Headers for the configuration for the SPI module.
#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:261
static spiDAT1_t spi_kNxp775DataConfigRx[BS_NR_OF_STRINGS]
Definition: spi_cfg.c:117
SPI_INTERFACE_CONFIG_s spi_mxmInterface
Definition: spi_cfg.c:182
static spiDAT1_t spi_kSbcDataConfig
Definition: spi_cfg.c:156
SPI_INTERFACE_CONFIG_s spi_sbcMcuInterface
Definition: spi_cfg.c:231
SPI_INTERFACE_CONFIG_s spi_nxp775InterfaceTx[BS_NR_OF_STRINGS]
Definition: spi_cfg.c:191
static spiDAT1_t spi_kNxp775DataConfigTx[BS_NR_OF_STRINGS]
Definition: spi_cfg.c:108
SPI_BUSY_STATE_e spi_busyFlags[]
Definition: spi_cfg.c:240
SPI_INTERFACE_CONFIG_s spi_framInterface
Definition: spi_cfg.c:213
static spiDAT1_t spi_kFramDataConfig
Definition: spi_cfg.c:126
static spiDAT1_t spi_kLtcDataConfig[BS_NR_OF_STRINGS]
Definition: spi_cfg.c:89
void SPI_SpsInterfaceSwitchToHighSpeed(SPI_INTERFACE_CONFIG_s *pSpiSpsInterface)
Switch the SPS spi handle to high speed.
Definition: spi_cfg.c:256
static spiDAT1_t spi_kMxmDataConfig
Definition: spi_cfg.c:99
SPI_INTERFACE_CONFIG_s spi_nxp775InterfaceRx[BS_NR_OF_STRINGS]
Definition: spi_cfg.c:202
static spiDAT1_t spi_kSpsDataConfigHighSpeed
Definition: spi_cfg.c:146
SPI_INTERFACE_CONFIG_s spi_ltcInterface[BS_NR_OF_STRINGS]
Definition: spi_cfg.c:171
const uint8_t spi_nrBusyFlags
Definition: spi_cfg.c:249
SPI_INTERFACE_CONFIG_s spi_spsInterface
Definition: spi_cfg.c:222
static spiDAT1_t spi_kSpsDataConfigLowSpeed
Definition: spi_cfg.c:136
Headers for the configuration for the SPI module.
SPI_BUSY_STATE_e
Definition: spi_cfg.h:109
@ SPI_IDLE
Definition: spi_cfg.h:110
#define SPI_NXP_TX_CHIP_SELECT_PIN
Definition: spi_cfg.h:142
#define SPI_NXP_RX_CHIP_SELECT_PIN
Definition: spi_cfg.h:143
#define SPI_MAXIM_CHIP_SELECT_PIN
Definition: spi_cfg.h:139
#define SPI_LTC_CHIP_SELECT_PIN
Definition: spi_cfg.h:136
#define SPI_SPS_CS_GIOPORT
Definition: spi_cfg.h:150
#define SPI_SBC_MCU_CHIP_SELECT_PIN
Definition: spi_cfg.h:106
@ SPI_CHIP_SELECT_SOFTWARE
Definition: spi_cfg.h:118
@ SPI_CHIP_SELECT_HARDWARE
Definition: spi_cfg.h:117
#define SPI_SPS_CS_PIN
Definition: spi_cfg.h:152
#define SPI_FRAM_CHIP_SELECT_PIN
Definition: spi_cfg.h:147
spiDAT1_t * pConfig
Definition: spi_cfg.h:126