foxBMS  1.3.0
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-05-30 (date of last update)
47  * @version v1.3.0
48  * @ingroup DRIVERS_CONFIGURATION
49  * @prefix SPI
50  *
51  * @brief Configuration for the SPI module
52  *
53  *
54  *
55  */
56 
57 /*========== Includes =======================================================*/
58 #include "spi_cfg.h"
59 
60 #include "spi_cfg-helper.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 regsiter 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 */
95 };
96 
97 /** SPI data configuration struct for MXM communication */
98 static spiDAT1_t spi_kMxmDataConfig = {
99  .CS_HOLD = TRUE, /* If true, HW chip select kept active */
100  .WDEL = TRUE, /* Activation of delay between words */
101  .DFSEL = SPI_FMT_1, /* Data word format selection */
103 
104 /** SPI data configuration struct for NXP MC33775A communication, Tx part */
106  {.CS_HOLD = TRUE, /* If true, HW chip select kept active */
107  .WDEL = TRUE, /* Activation of delay between words */
108  .DFSEL = SPI_FMT_0, /* Data word format selection */
110 };
111 
112 /** SPI data configuration struct for NXP MC33775A communication, Rx part */
114  {.CS_HOLD = TRUE, /* If true, HW chip select kept active */
115  .WDEL = TRUE, /* Activation of delay between words */
116  .DFSEL = SPI_FMT_0, /* Data word format selection */
118 };
119 
120 /** SPI data configuration struct for FRAM communication */
121 static spiDAT1_t spi_kFramDataConfig = {
122  /* struct is implemented in the TI HAL and uses uppercase true and false */
123  .CS_HOLD = TRUE, /* If true, HW chip select kept active */
124  .WDEL = TRUE, /* Activation of delay between words */
125  .DFSEL = SPI_FMT_1, /* Data word format selection */
127 
128 /** SPI data configuration struct for SPS communication in low speed (4MHz) */
129 static spiDAT1_t spi_kSpsDataConfigLowSpeed = {
130  /* struct is implemented in the TI HAL and uses uppercase true and false */
131  .CS_HOLD = TRUE, /* If true, HW chip select kept active */
132  .WDEL = TRUE, /* Activation of delay between words */
133  .DFSEL = SPI_FMT_1, /* Data word format selection */
135 };
136 
137 /** SPI data configuration struct for SPS communication in high speed (10MHz) */
138 static spiDAT1_t spi_kSpsDataConfigHighSpeed = {
139  /* struct is implemented in the TI HAL and uses uppercase true and false */
140  .CS_HOLD = TRUE, /* If true, HW chip select kept active */
141  .WDEL = TRUE, /* Activation of delay between words */
142  .DFSEL = SPI_FMT_2, /* Data word format selection */
144 
145 /** SPI data configuration struct for ADC communication */
146 static spiDAT1_t spi_kAdc0DataConfig = {
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 */
152 
153 /** SPI data configuration struct for ADC communication */
154 static spiDAT1_t spi_kAdc1DataConfig = {
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_2, /* Data word format selection */
160 
161 /** SPI configuration struct for SBC communication */
162 static spiDAT1_t spi_kSbcDataConfig = {
163  /* struct is implemented in the TI HAL and uses uppercase true and false */
164  .CS_HOLD = TRUE, /* If true, HW chip select kept active */
165  .WDEL = TRUE, /* Activation of delay between words */
166  .DFSEL = SPI_FMT_0, /* Data word format selection */
168 
169 /*========== Extern Constant and Variable Definitions =======================*/
170 
171 /**
172  * SPI interface configuration for LTC communication
173  * This is a list of structs because of multistring
174  */
176  {
177  .pConfig = &spi_kLtcDataConfig[0u],
178  .pNode = spiREG1,
179  .pGioPort = &(spiREG1->PC3),
180  .csPin = 2u,
181  .csType = SPI_CHIP_SELECT_HARDWARE,
182  },
183 };
184 
185 /** SPI interface configuration for MXM communication */
188  .pNode = spiREG4,
189  .pGioPort = &(spiREG4->PC3),
190  .csPin = 0u,
191  .csType = SPI_CHIP_SELECT_HARDWARE,
192 };
193 
194 /** SPI interface configuration for N775 communication Tx part */
196  {
198  .pNode = spiREG1,
199  .pGioPort = &(spiREG1->PC3),
200  .csPin = 2u,
201  .csType = SPI_CHIP_SELECT_HARDWARE,
202  },
203 };
204 
205 /** SPI interface configuration for N775 communication, Rx part */
207  {
209  .pNode = spiREG4,
210  .pGioPort = &(spiREG4->PC3),
211  .csPin = 0u,
212  .csType = SPI_CHIP_SELECT_HARDWARE,
213  },
214 };
215 
216 /** SPI interface configuration for FRAM communication */
219  .pNode = spiREG3,
220  .pGioPort = &(spiREG3->PC3),
221  .csPin = 1u,
222  .csType = SPI_CHIP_SELECT_SOFTWARE,
223 };
224 
225 /** SPI interface configuration for SPS communication */
228  .pNode = spiREG2,
229  .pGioPort = &SPS_SPI_CS_GIOPORT,
230  .csPin = SPS_SPI_CS_PIN,
231  .csType = SPI_CHIP_SELECT_SOFTWARE,
232 };
233 
234 /** SPI interface configuration for ADC communication */
237  .pNode = spiREG3,
238  .pGioPort = &(spiREG3->PC3),
239  .csPin = 4u,
240  .csType = SPI_CHIP_SELECT_SOFTWARE,
241 };
242 
243 /** SPI interface configuration for ADC communication */
246  .pNode = spiREG3,
247  .pGioPort = &(spiREG3->PC3),
248  .csPin = 5u,
249  .csType = SPI_CHIP_SELECT_SOFTWARE,
250 };
251 
252 /** SPI interface configuration for SBC communication */
255  .pNode = spiREG2,
256  .pGioPort = &(spiREG2->PC3),
257  .csPin = 0u,
258  .csType = SPI_CHIP_SELECT_HARDWARE,
259 };
260 
261 /** struct containing the lock state of the SPI interfaces */
263  SPI_IDLE,
264  SPI_IDLE,
265  SPI_IDLE,
266  SPI_IDLE,
267  SPI_IDLE,
268 };
269 
270 /** size of #spi_busyFlags */
271 const uint8_t spi_nrBusyFlags = sizeof(spi_busyFlags) / sizeof(SPI_BUSY_STATE_e);
272 
273 /*========== Static Function Prototypes =====================================*/
274 
275 /*========== Static Function Implementations ================================*/
276 
277 /*========== Extern Function Implementations ================================*/
279  FAS_ASSERT(pSpiSpsInterface != NULL_PTR);
280  pSpiSpsInterface->pConfig = &spi_kSpsDataConfigHighSpeed;
281 }
282 
284  FAS_ASSERT(pSpiSpsInterface != NULL_PTR);
285  pSpiSpsInterface->pConfig = &spi_kSpsDataConfigLowSpeed;
286 }
287 
288 /*========== 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:241
#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:283
static spiDAT1_t spi_kNxp775DataConfigRx[BS_NR_OF_STRINGS]
Definition: spi_cfg.c:113
SPI_INTERFACE_CONFIG_s spi_MxmInterface
Definition: spi_cfg.c:186
static spiDAT1_t spi_kSbcDataConfig
Definition: spi_cfg.c:162
SPI_INTERFACE_CONFIG_s spi_adc0Interface
Definition: spi_cfg.c:235
SPI_INTERFACE_CONFIG_s spi_kSbcMcuInterface
Definition: spi_cfg.c:253
SPI_INTERFACE_CONFIG_s spi_nxp775InterfaceTx[BS_NR_OF_STRINGS]
Definition: spi_cfg.c:195
static spiDAT1_t spi_kAdc0DataConfig
Definition: spi_cfg.c:146
static spiDAT1_t spi_kNxp775DataConfigTx[BS_NR_OF_STRINGS]
Definition: spi_cfg.c:105
SPI_BUSY_STATE_e spi_busyFlags[]
Definition: spi_cfg.c:262
static spiDAT1_t spi_kAdc1DataConfig
Definition: spi_cfg.c:154
SPI_INTERFACE_CONFIG_s spi_framInterface
Definition: spi_cfg.c:217
static spiDAT1_t spi_kFramDataConfig
Definition: spi_cfg.c:121
static spiDAT1_t spi_kLtcDataConfig[BS_NR_OF_STRINGS]
Definition: spi_cfg.c:89
SPI_INTERFACE_CONFIG_s spi_adc1Interface
Definition: spi_cfg.c:244
void SPI_SpsInterfaceSwitchToHighSpeed(SPI_INTERFACE_CONFIG_s *pSpiSpsInterface)
Switch the SPS spi handle to high speed.
Definition: spi_cfg.c:278
static spiDAT1_t spi_kMxmDataConfig
Definition: spi_cfg.c:98
SPI_INTERFACE_CONFIG_s spi_nxp775InterfaceRx[BS_NR_OF_STRINGS]
Definition: spi_cfg.c:206
static spiDAT1_t spi_kSpsDataConfigHighSpeed
Definition: spi_cfg.c:138
SPI_INTERFACE_CONFIG_s spi_ltcInterface[BS_NR_OF_STRINGS]
Definition: spi_cfg.c:175
const uint8_t spi_nrBusyFlags
Definition: spi_cfg.c:271
SPI_INTERFACE_CONFIG_s spi_spsInterface
Definition: spi_cfg.c:226
static spiDAT1_t spi_kSpsDataConfigLowSpeed
Definition: spi_cfg.c:129
Headers for the configuration for the SPI module.
SPI_BUSY_STATE_e
Definition: spi_cfg.h:125
@ SPI_IDLE
Definition: spi_cfg.h:126
#define SPS_SPI_CS_PIN
Definition: spi_cfg.h:108
#define SPS_SPI_CS_GIOPORT
Definition: spi_cfg.h:106
@ SPI_CHIP_SELECT_SOFTWARE
Definition: spi_cfg.h:134
@ SPI_CHIP_SELECT_HARDWARE
Definition: spi_cfg.h:133
spiDAT1_t * pConfig
Definition: spi_cfg.h:141