foxBMS-UnitTests  1.0.0
The foxBMS Unit Tests API Documentation
fram.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2021, Fraunhofer-Gesellschaft zur Foerderung der
4  * angewandten Forschung e.V. All rights reserved.
5  *
6  * BSD 3-Clause License
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * We kindly request you to use one or more of the following phrases to refer
31  * to foxBMS in your hardware, software, documentation or advertising
32  * materials:
33  *
34  * ″This product uses parts of foxBMS®″
35  *
36  * ″This product includes parts of foxBMS®″
37  *
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-03-24 (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  retVal = SPI_Lock(SPI_Interface3);
110 
111  if (retVal == STD_OK) {
112  address = (&fram_base_header[0] + blockId)->address;
113 
114  wrt_ptr = (uint8_t *)((&fram_base_header[0] + blockId)->blockptr);
115  size = (&fram_base_header[0] + blockId)->datalength;
116 
117  /* send write enable command */
119  write = FRAM_WRITEENABLECOMMAND;
123 
124  /* send data to write */
125  /* set chip select low to start transmission */
127 
128  /* send write command */
129  write = FRAM_WRITECOMMAND;
131 
132  /* send upper part of address */
133  write = (address & 0x3F0000u) >> 16u;
135 
136  /* send middle part of address */
137  write = (address & 0xFF00u) >> 8u;
139 
140  /* send lower part of address */
141  write = address & 0xFFu;
143 
144  while (size > 0u) {
145  write = (uint16_t)(*wrt_ptr);
147  wrt_ptr++;
148  size--;
149  }
150 
151  /* set chip select high to start transmission */
153 
155  }
156 
157  return retVal;
158 }
159 
161  uint8_t *rd_ptr = NULL_PTR;
162  uint32_t address = 0;
163  uint16_t write = 0;
164  uint16_t read = 0;
165  uint16_t size = 0;
166  STD_RETURN_TYPE_e retVal = STD_NOT_OK;
167 
168  retVal = SPI_Lock(SPI_Interface3);
169 
170  if (retVal == STD_OK) {
171  address = (&fram_base_header[0] + blockId)->address;
172 
173  rd_ptr = (uint8_t *)((&fram_base_header[0] + blockId)->blockptr);
174  size = (&fram_base_header[0] + blockId)->datalength;
175 
176  /* get data to be read */
177  /* set chip select low to start transmission */
179 
180  /* send write command */
181  write = FRAM_READCOMMAND;
183 
184  /* send upper part of address */
185  write = (address & 0x3F0000u) >> 16u;
187 
188  /* send middle part of address */
189  write = (address & 0xFF00u) >> 8u;
191 
192  /* send lower part of address */
193  write = address & 0xFFu;
195 
196  write = 0;
197  while (size > 0u) {
199  *rd_ptr = read;
200  rd_ptr++;
201  size--;
202  }
203 
204  /* set chip select high to start transmission */
206 
208  }
209 
210  return retVal;
211 }
212 
213 /*========== Externalized Static Function Implementations (Unit Test) =======*/
os.h
Implementation of the tasks used by the system, headers.
SPI_INTERFACE_CONFIG::pGioPort
volatile uint32_t * pGioPort
Definition: spi_cfg.h:111
STD_RETURN_TYPE_e
enum STD_RETURN_TYPE STD_RETURN_TYPE_e
spi.h
Headers for the driver for the SPI module.
FRAM_WRITECOMMAND
#define FRAM_WRITECOMMAND
Definition: fram.c:70
SPI_Interface3
@ SPI_Interface3
Definition: spi_cfg.h:101
fram_base_header
FRAM_BASE_HEADER_s fram_base_header[]
Definition: fram_cfg.c:83
FAS_ASSERT
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:233
SPI_Lock
STD_RETURN_TYPE_e SPI_Lock(uint8_t spi)
Locks SPI interfaces.
Definition: spi.c:403
FRAM_BLOCK_MAX
@ FRAM_BLOCK_MAX
Definition: fram_cfg.h:93
MCU_delay_us
void MCU_delay_us(uint32_t delay_us)
Wait blocking a certain time in microseconds.
Definition: mcu.c:80
FRAM_Initialize
void FRAM_Initialize(void)
Initializes the addresses to be written in the FRAM.
Definition: fram.c:88
mcu.h
Headers for the driver for the MCU module.
STD_OK
@ STD_OK
Definition: fstd_types.h:72
SPI_DirectlyTransmitReceiveData
STD_RETURN_TYPE_e SPI_DirectlyTransmitReceiveData(SPI_INTERFACE_CONFIG_s *pSpiInterface, uint16 *pTxBuff, uint16 *pRxBuff, uint32 frameLength)
Transmits and receives data on SPI without DMA.
Definition: spi.c:162
STD_NOT_OK
@ STD_NOT_OK
Definition: fstd_types.h:73
spi_framInterface
SPI_INTERFACE_CONFIG_s spi_framInterface
Definition: spi_cfg.c:145
FRAM_Write
STD_RETURN_TYPE_e FRAM_Write(FRAM_BLOCK_ID_e blockId)
Writes a variable to the FRAM.
Definition: fram.c:101
IO_PinReset
void IO_PinReset(uint32_t *pRegisterAddress, uint32_t pin)
Set pin by writing in pin output register.
Definition: io.c:79
FRAM_READCOMMAND
#define FRAM_READCOMMAND
Definition: fram.c:71
FRAM_MAX_ADDRESS
#define FRAM_MAX_ADDRESS
Definition: fram.c:76
NULL_PTR
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:66
FRAM_Read
STD_RETURN_TYPE_e FRAM_Read(FRAM_BLOCK_ID_e blockId)
Reads a variable from the FRAM.
Definition: fram.c:160
io.h
Header for the driver for the IO module.
FRAM_DELAY_AFTER_WRITE_ENABLE_US
#define FRAM_DELAY_AFTER_WRITE_ENABLE_US
Definition: fram.c:66
SPI_INTERFACE_CONFIG::csPin
uint32_t csPin
Definition: spi_cfg.h:112
FRAM_BLOCK_ID_e
enum FRAM_BLOCK_ID FRAM_BLOCK_ID_e
FRAM_WRITEENABLECOMMAND
#define FRAM_WRITEENABLECOMMAND
Definition: fram.c:72
IO_PinSet
void IO_PinSet(uint32_t *pRegisterAddress, uint32_t pin)
Set pin by writing in pin output register.
Definition: io.c:72
SPI_Unlock
void SPI_Unlock(uint8_t spi)
Unlocks SPI interfaces.
Definition: spi.c:418
fram.h
Header for the driver for the FRAM module.