foxBMS  1.2.1
The foxBMS Battery Management System API Documentation
fram.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 fram.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
48  * @prefix FRAM
49  *
50  * @brief Driver for the FRAM module
51  *
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "fram.h"
57 
58 #include "io.h"
59 #include "mcu.h"
60 #include "os.h"
61 #include "spi.h"
62 
63 /*========== Macros and Definitions =========================================*/
64 
65 /** delay in µs after writing the FRAM */
66 #define FRAM_DELAY_AFTER_WRITE_ENABLE_US (5U)
67 
68 /** control commands for the FRAM */
69 /**@{*/
70 #define FRAM_WRITECOMMAND (0x02u)
71 #define FRAM_READCOMMAND (0x03u)
72 #define FRAM_WRITEENABLECOMMAND (0x06u)
73 /**@}*/
74 
75 /** maximal memory address of the FRAM */
76 #define FRAM_MAX_ADDRESS (0x3FFFFu)
77 
78 /*========== Static Constant and Variable Definitions =======================*/
79 
80 /*========== Extern Constant and Variable Definitions =======================*/
81 
82 /*========== Static Function Prototypes =====================================*/
83 
84 /*========== Static Function Implementations ================================*/
85 
86 /*========== Extern Function Implementations ================================*/
87 
88 extern void FRAM_Initialize(void) {
89  uint32_t address = 0;
90 
91  /* find address of all variables in FRAM by parsing length of data*/
92  for (uint16_t i = 0u; i < FRAM_BLOCK_MAX; i++) {
93  (&fram_base_header[0u] + i)->address = address;
94  address += (&fram_base_header[0u] + i)->datalength;
95  }
96 
97  /* ASSERT that size of variables does not exceed FRAM size */
98  FAS_ASSERT(!(address > FRAM_MAX_ADDRESS));
99 }
100 
102  uint8_t *wrt_ptr = NULL_PTR;
103  uint32_t address = 0;
104  uint16_t write = 0;
105  uint16_t read = 0;
106  uint16_t size = 0;
107  STD_RETURN_TYPE_e retVal = STD_NOT_OK;
108 
109  /* FRAM must use SW Chip Select configuration*/
111 
113 
114  if (retVal == STD_OK) {
115  address = (&fram_base_header[0] + blockId)->address;
116 
117  wrt_ptr = (uint8_t *)((&fram_base_header[0] + blockId)->blockptr);
118  size = (&fram_base_header[0] + blockId)->datalength;
119 
120  /* send write enable command */
122  write = FRAM_WRITEENABLECOMMAND;
123  SPI_FramTransmitReceiveData(&spi_framInterface, &write, &read, 1u);
126 
127  /* send data to write */
128  /* set chip select low to start transmission */
130 
131  /* send write command */
132  write = FRAM_WRITECOMMAND;
133  SPI_FramTransmitReceiveData(&spi_framInterface, &write, &read, 1u);
134 
135  /* send upper part of address */
136  write = (address & 0x3F0000u) >> 16u;
137  SPI_FramTransmitReceiveData(&spi_framInterface, &write, &read, 1u);
138 
139  /* send middle part of address */
140  write = (address & 0xFF00u) >> 8u;
141  SPI_FramTransmitReceiveData(&spi_framInterface, &write, &read, 1u);
142 
143  /* send lower part of address */
144  write = address & 0xFFu;
145  SPI_FramTransmitReceiveData(&spi_framInterface, &write, &read, 1u);
146 
147  while (size > 0u) {
148  write = (uint16_t)(*wrt_ptr);
149  SPI_FramTransmitReceiveData(&spi_framInterface, &write, &read, 1u);
150  wrt_ptr++;
151  size--;
152  }
153 
154  /* set chip select high to start transmission */
156 
158  }
159 
160  return retVal;
161 }
162 
164  uint8_t *rd_ptr = NULL_PTR;
165  uint32_t address = 0;
166  uint16_t write = 0;
167  uint16_t read = 0;
168  uint16_t size = 0;
169  STD_RETURN_TYPE_e retVal = STD_NOT_OK;
170 
171  /* FRAM must use SW Chip Select configuration*/
173 
175 
176  if (retVal == STD_OK) {
177  address = (&fram_base_header[0] + blockId)->address;
178 
179  rd_ptr = (uint8_t *)((&fram_base_header[0] + blockId)->blockptr);
180  size = (&fram_base_header[0] + blockId)->datalength;
181 
182  /* get data to be read */
183  /* set chip select low to start transmission */
185 
186  /* send write command */
187  write = FRAM_READCOMMAND;
188  SPI_FramTransmitReceiveData(&spi_framInterface, &write, &read, 1u);
189 
190  /* send upper part of address */
191  write = (address & 0x3F0000u) >> 16u;
192  SPI_FramTransmitReceiveData(&spi_framInterface, &write, &read, 1u);
193 
194  /* send middle part of address */
195  write = (address & 0xFF00u) >> 8u;
196  SPI_FramTransmitReceiveData(&spi_framInterface, &write, &read, 1u);
197 
198  /* send lower part of address */
199  write = address & 0xFFu;
200  SPI_FramTransmitReceiveData(&spi_framInterface, &write, &read, 1u);
201 
202  write = 0;
203  while (size > 0u) {
204  SPI_FramTransmitReceiveData(&spi_framInterface, &write, &read, 1u);
205  *rd_ptr = read;
206  rd_ptr++;
207  size--;
208  }
209 
210  /* set chip select high to start transmission */
212 
214  }
215 
216  return retVal;
217 }
218 
219 /*========== Externalized Static Function Implementations (Unit Test) =======*/
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:239
void FRAM_Initialize(void)
Initializes the addresses to be written in the FRAM.
Definition: fram.c:88
#define FRAM_DELAY_AFTER_WRITE_ENABLE_US
Definition: fram.c:66
#define FRAM_READCOMMAND
Definition: fram.c:71
STD_RETURN_TYPE_e FRAM_Read(FRAM_BLOCK_ID_e blockId)
Reads a variable from the FRAM.
Definition: fram.c:163
#define FRAM_WRITECOMMAND
Definition: fram.c:70
STD_RETURN_TYPE_e FRAM_Write(FRAM_BLOCK_ID_e blockId)
Writes a variable to the FRAM.
Definition: fram.c:101
#define FRAM_MAX_ADDRESS
Definition: fram.c:76
#define FRAM_WRITEENABLECOMMAND
Definition: fram.c:72
Header for the driver for the FRAM module.
FRAM_BASE_HEADER_s fram_base_header[]
Definition: fram_cfg.c:83
enum FRAM_BLOCK_ID FRAM_BLOCK_ID_e
@ FRAM_BLOCK_MAX
Definition: fram_cfg.h:93
@ STD_NOT_OK
Definition: fstd_types.h:82
@ STD_OK
Definition: fstd_types.h:81
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:75
enum STD_RETURN_TYPE STD_RETURN_TYPE_e
void IO_PinSet(volatile uint32_t *pRegisterAddress, uint32_t pin)
Set pin by writing in pin output register.
Definition: io.c:84
void IO_PinReset(volatile uint32_t *pRegisterAddress, uint32_t pin)
Reset pin by writing in pin output register.
Definition: io.c:91
Header for the driver for the IO module.
void MCU_delay_us(uint32_t delay_us)
Wait blocking a certain time in microseconds.
Definition: mcu.c:80
Headers for the driver for the MCU module.
Declaration of the OS wrapper interface.
void SPI_FramTransmitReceiveData(SPI_INTERFACE_CONFIG_s *pSpiInterface, uint16 *pTxBuff, uint16 *pRxBuff, uint32 frameLength)
Transmits and receives data on SPI without DMA, wrappe for FRAM.
Definition: spi.c:201
STD_RETURN_TYPE_e SPI_Lock(uint8_t spi)
Locks SPI interfaces.
Definition: spi.c:315
void SPI_Unlock(uint8_t spi)
Unlocks SPI interfaces.
Definition: spi.c:330
uint8_t SPI_GetSpiIndex(spiBASE_t *pNode)
Returns index of SPI node.
Definition: spi.c:455
Headers for the driver for the SPI module.
SPI_INTERFACE_CONFIG_s spi_framInterface
Definition: spi_cfg.c:284
@ SPI_CHIP_SELECT_SOFTWARE
Definition: spi_cfg.h:133
SPI_CHIP_SELECT_TYPE_e csType
Definition: spi_cfg.h:144
spiBASE_t * pNode
Definition: spi_cfg.h:141
volatile uint32_t * pGioPort
Definition: spi_cfg.h:142