foxBMS - Unit Tests  1.5.0
The foxBMS Unit Tests API Documentation
spi.h
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.h
44  * @author foxBMS Team
45  * @date 2019-12-12 (date of creation)
46  * @updated 2023-02-03 (date of last update)
47  * @version v1.5.0
48  * @ingroup DRIVERS
49  * @prefix SPI
50  *
51  * @brief Headers for the driver for the SPI module.
52  *
53  */
54 
55 #ifndef FOXBMS__SPI_H_
56 #define FOXBMS__SPI_H_
57 
58 /*========== Includes =======================================================*/
59 #include "spi_cfg.h"
60 
61 #include "fstd_types.h"
62 
63 #include <stdint.h>
64 
65 /*========== Macros and Definitions =========================================*/
66 
67 /*========== Extern Constant and Variable Declarations ======================*/
68 
69 /*========== Extern Function Prototypes =====================================*/
70 /**
71  * @brief Initializes the SPI module
72  * @details First the low level initialization (using the HAL functions) is
73  * done.
74  * After that the chip select setup for the peripherals is configured.
75 */
76 extern void SPI_Initialize(void);
77 
78 /* INCLUDE MARKER FOR THE DOCUMENTATION; DO NOT MOVE spi-documentation-dummy-start-include */
79 /**
80  * @brief Sends a dummy byte to wake up the SPI interface.
81  * @param pSpiInterface pointer to SPI interface configuration
82  * @param delay delay to wait after dummy byte transfer
83  * @return status of the SPI transfer
84  */
85 extern STD_RETURN_TYPE_e SPI_TransmitDummyByte(SPI_INTERFACE_CONFIG_s *pSpiInterface, uint32_t delay);
86 /* INCLUDE MARKER FOR THE DOCUMENTATION; DO NOT MOVE spi-documentation-dummy-stop-include */
87 
88 /* INCLUDE MARKER FOR THE DOCUMENTATION; DO NOT MOVE spi-documentation-transmit-start-include */
89 /**
90  * @brief Transmits data on SPI without DMA.
91  * @details This function can be used to send and receive data via SPI. SPI
92  * communication is performed in blocking mode and chip select is
93  * set/reset automatically.
94  * @param pSpiInterface pointer to SPI interface configuration
95  * @param pTxBuff pointer to data that is transmitted by the SPI interface
96  * @param frameLength number of bytes to be transmitted by the SPI interface
97  * @return status of the SPI transfer
98  */
99 extern STD_RETURN_TYPE_e SPI_TransmitData(SPI_INTERFACE_CONFIG_s *pSpiInterface, uint16 *pTxBuff, uint32 frameLength);
100 /* INCLUDE MARKER FOR THE DOCUMENTATION; DO NOT MOVE spi-documentation-transmit-stop-include */
101 
102 /* INCLUDE MARKER FOR THE DOCUMENTATION; DO NOT MOVE spi-documentation-transmit-receive-start-include */
103 /**
104  * @brief Transmits and receives data on SPI without DMA.
105  * @details This function can be used to send and receive data via SPI. SPI
106  * communication is performed in blocking mode and chip select is
107  * set/reset automatically.
108  * @param pSpiInterface pointer to SPI interface configuration
109  * @param pTxBuff pointer to data that is transmitted by the SPI interface
110  * @param pRxBuff pointer to data that is received by the SPI interface
111  * @param frameLength number of bytes to be transmitted by the SPI interface
112  * @return status of the SPI transfer
113  */
115  SPI_INTERFACE_CONFIG_s *pSpiInterface,
116  uint16 *pTxBuff,
117  uint16 *pRxBuff,
118  uint32 frameLength);
119 /* INCLUDE MARKER FOR THE DOCUMENTATION; DO NOT MOVE spi-documentation-transmit-receive-stop-include */
120 
121 /* INCLUDE MARKER FOR THE DOCUMENTATION; DO NOT MOVE spi-documentation-fram-start-include */
122 /**
123  * @brief Transmits and receives data on SPI without DMA, wrapper for FRAM
124  * @details This function can be used to send and receive data via SPI. SPI
125  * communication is performed in blocking mode and chip select is
126  * set/reset automatically.
127  * It does not drive the Chip Select (neither hardware nor software)
128  * as this is done directly in the FRAM functions.
129  * @param pSpiInterface pointer to SPI interface configuration
130  * @param pTxBuff pointer to data that is transmitted by the SPI interface
131  * @param pRxBuff pointer to data that is received by the SPI interface
132  * @param frameLength number of bytes to be transmitted by the SPI interface
133  * @return status of the SPI transfer
134  */
135 extern void SPI_FramTransmitReceiveData(
136  SPI_INTERFACE_CONFIG_s *pSpiInterface,
137  uint16 *pTxBuff,
138  uint16 *pRxBuff,
139  uint32 frameLength);
140 /* INCLUDE MARKER FOR THE DOCUMENTATION; DO NOT MOVE spi-documentation-fram-stop-include */
141 
142 /* INCLUDE MARKER FOR THE DOCUMENTATION; DO NOT MOVE spi-documentation-transmit-receive-dma-start-include */
143 /**
144  * @brief Transmits and receives data on SPI with DMA.
145  * @details This function can be used to send and receive data via SPI. SPI
146  * communication is performed in blocking mode and chip select is
147  * set/reset automatically..
148  * @param pSpiInterface pointer to SPI interface configuration
149  * @param pTxBuff pointer to data that is transmitted by the SPI interface
150  * @param pRxBuff pointer to data that is received by the SPI interface
151  * @param frameLength number of bytes to be transmitted by the SPI interface
152  * @return status of the SPI transfer
153  */
155  SPI_INTERFACE_CONFIG_s *pSpiInterface,
156  uint16_t *pTxBuff,
157  uint16_t *pRxBuff,
158  uint32_t frameLength);
159 /* INCLUDE MARKER FOR THE DOCUMENTATION; DO NOT MOVE spi-documentation-transmit-receive-dma-stop-include */
160 
161 /* INCLUDE MARKER FOR THE DOCUMENTATION; DO NOT MOVE spi-documentation-slave-receive-dma-start-include */
162 /**
163  * @brief Transmits and receives data on SPI with DMA.
164  * @details This function can be used to send and receive data via SPI. SPI
165  * communication is performed in blocking mode and chip select is
166  * set/reset automatically..
167  * @param pSpiInterface pointer to SPI interface configuration
168  * @param pTxBuff pointer to data that is transmitted by the SPI interface
169  * @param pRxBuff pointer to data that is received by the SPI interface
170  * @param frameLength number of bytes to be transmitted by the SPI interface
171  * @return status of the SPI transfer
172  */
174  SPI_INTERFACE_CONFIG_s *pSpiInterface,
175  uint16_t *pTxBuff,
176  uint16_t *pRxBuff,
177  uint32_t frameLength);
178 /* INCLUDE MARKER FOR THE DOCUMENTATION; DO NOT MOVE spi-documentation-slave-receive-dma-stop-include */
179 
180 /**
181  * @brief Locks SPI interfaces.
182  * @details This function is used to change the state of the SPI_busy_flags
183  * variable to "locked".
184  * @param spi SPI interface to be locked (0-4 on the TMS570LC4357)
185  * @return #STD_OK if SPI interface could be locked, #STD_NOT_OK otherwise
186  */
187 extern STD_RETURN_TYPE_e SPI_Lock(uint8_t spi);
188 
189 /**
190  * @brief Unlocks SPI interfaces.
191  * @details This function is used to change the state of the SPI_busy_flags
192  * variable to "unlocked".
193  * @param spi SPI interface to be unlocked (0-4 on the TMS570LC4357)
194  */
195 extern void SPI_Unlock(uint8_t spi);
196 
197 /**
198  * @brief Sets the functional of a SPI pin
199  * @details SPI pins in this HAL can have functional state SPI (for when it is
200  * controlled by the SPI hardware) and functional state GIO (for when
201  * it is controlled as a GIO pin).
202  * @param[in,out] pNode handle of the SPI node that should be configured
203  * @param[in] bit bit that should be manipulated, other pins remain
204  * unchanged
205  * @param[in] hardwareControlled Whether the bit should be hardware
206  * controlled (true) or not (false)
207  */
208 extern void SPI_SetFunctional(spiBASE_t *pNode, enum spiPinSelect bit, bool hardwareControlled);
209 
210 /**
211  * @brief Used to send last byte per SPI.
212  * @details This function is called in the DMA Tx callback. It is used
213  * to send the last byte with CSHOLD = 0.
214  * @param spiIndex SPI node in use
215  */
216 extern void SPI_DmaSendLastByte(uint8_t spiIndex);
217 
218 /**
219  * @brief Returns #STD_OK if the SPI interface can be used again
220  * @details This function just checks for SPI_PENDING and groups SPI_READY and
221  * SPI_...
222  * @param[in] pNode handle of the SPI node that should be checked
223  * @returns #STD_RETURN_TYPE_e indicating wether the interface is ok to be used.
224  */
225 extern STD_RETURN_TYPE_e SPI_CheckInterfaceAvailable(spiBASE_t *pNode);
226 
227 /**
228  * @brief Returns index of SPI node
229  * @param[in] pNode handle of the SPI node that should be checked
230  * @returns index of SPI node
231  */
232 extern uint8_t SPI_GetSpiIndex(spiBASE_t *pNode);
233 
234 /*========== Externalized Static Functions Prototypes (Unit Test) ===========*/
235 #ifdef UNITY_UNIT_TEST
236 extern void TEST_SPI_InitializeChipSelects(void);
237 extern uint8_t TEST_SPI_GetChipSelectPin(SPI_CHIP_SELECT_TYPE_e csType, uint32_t csPin);
238 extern uint8_t TEST_SPI_GetHardwareChipSelectPin(uint8_t csPin);
239 #endif
240 
241 #endif /* FOXBMS__SPI_H_ */
Definition of foxBMS standard types.
STD_RETURN_TYPE_e
Definition: fstd_types.h:82
uint8_t TEST_SPI_GetHardwareChipSelectPin(uint8_t csPin)
Definition: spi.c:585
STD_RETURN_TYPE_e SPI_TransmitReceiveDataDma(SPI_INTERFACE_CONFIG_s *pSpiInterface, uint16_t *pTxBuff, uint16_t *pRxBuff, uint32_t frameLength)
Transmits and receives data on SPI with DMA.
Definition: spi.c:291
void SPI_SetFunctional(spiBASE_t *pNode, enum spiPinSelect bit, bool hardwareControlled)
Sets the functional of a SPI pin.
Definition: spi.c:428
STD_RETURN_TYPE_e SPI_CheckInterfaceAvailable(spiBASE_t *pNode)
Returns STD_OK if the SPI interface can be used again.
Definition: spi.c:545
void SPI_DmaSendLastByte(uint8_t spiIndex)
Used to send last byte per SPI.
Definition: spi.c:530
STD_RETURN_TYPE_e SPI_TransmitDummyByte(SPI_INTERFACE_CONFIG_s *pSpiInterface, uint32_t delay)
Sends a dummy byte to wake up the SPI interface.
Definition: spi.c:158
void SPI_FramTransmitReceiveData(SPI_INTERFACE_CONFIG_s *pSpiInterface, uint16 *pTxBuff, uint16 *pRxBuff, uint32 frameLength)
Transmits and receives data on SPI without DMA, wrapper for FRAM.
Definition: spi.c:278
STD_RETURN_TYPE_e SPI_Lock(uint8_t spi)
Locks SPI interfaces.
Definition: spi.c:401
STD_RETURN_TYPE_e SPI_SlaveSetReceiveDataDma(SPI_INTERFACE_CONFIG_s *pSpiInterface, uint16_t *pTxBuff, uint16_t *pRxBuff, uint32_t frameLength)
Transmits and receives data on SPI with DMA.
Definition: spi.c:462
void SPI_Unlock(uint8_t spi)
Unlocks SPI interfaces.
Definition: spi.c:418
STD_RETURN_TYPE_e SPI_TransmitData(SPI_INTERFACE_CONFIG_s *pSpiInterface, uint16 *pTxBuff, uint32 frameLength)
Transmits data on SPI without DMA.
Definition: spi.c:168
STD_RETURN_TYPE_e SPI_TransmitReceiveData(SPI_INTERFACE_CONFIG_s *pSpiInterface, uint16 *pTxBuff, uint16 *pRxBuff, uint32 frameLength)
Transmits and receives data on SPI without DMA.
Definition: spi.c:220
void TEST_SPI_InitializeChipSelects(void)
Definition: spi.c:579
uint8_t SPI_GetSpiIndex(spiBASE_t *pNode)
Returns index of SPI node.
Definition: spi.c:555
uint8_t TEST_SPI_GetChipSelectPin(SPI_CHIP_SELECT_TYPE_e csType, uint32_t csPin)
Definition: spi.c:582
void SPI_Initialize(void)
Initializes the SPI module.
Definition: spi.c:153
Headers for the configuration for the SPI module.
SPI_CHIP_SELECT_TYPE_e
Definition: spi_cfg.h:116