GCC Code Coverage Report


Directory: src/drv/
File: src/drv/flash/src/flash_driver.c
Date: 2024-01-25 15:03:18
Exec Total Coverage
Lines: 0 120 0.0%
Branches: 0 50 0.0%

Line Branch Exec Source
1 /********************************************************************************************************//**
2 * @file flash_driver.c
3 *
4 * @brief File containing the APIs for configuring the FLASH peripheral.
5 *
6 * Public Functions:
7 * - uint8_t Flash_EraseSector(uint8_t sector)
8 * - uint8_t Flash_WriteMemoryByte(uintptr_t address, uint8_t data)
9 * - uint8_t Flash_WriteMemoryHalfWord(uintptr_t address, uint16_t data)
10 * - uint8_t Flash_WriteMemoryWord(uintptr_t address, uint32_t data)
11 * - uint8_t Flash_WriteMemoryDoubleWord(uintptr_t address, uint64_t data)
12 * - uint8_t Flash_EnRWProtection(uint8_t sectors, uint8_t protection_mode)
13 * - uint8_t Flash_DisRWProtection(void)
14 * - void Flash_Unlock(void)
15 * - void Flash_Lock(void)
16 * - void Flash_OPTUnlock(void)
17 * - void Flash_OPTLock(void)
18 * - void Flash_SetPSIZE(flash_psize_t psize)
19 * - uint8_t Flash_Busy(void)
20 * - void Flash_GetOBCfg(OPT_Cfg_t* OPTCfg)
21 * - uint8_t Flash_SetLatency(uint8_t latency)
22 * - void Flash_EnableCache(flash_cache_t cache_options)
23 * - void Flash_DisableCache(flash_cache_t cache_options)
24 *
25 * @note
26 * For further information about functions refer to the corresponding header file.
27 **/
28
29 #include <stdint.h>
30 #include "flash_driver.h"
31
32 /***********************************************************************************************************/
33 /* Public API Definitions */
34 /***********************************************************************************************************/
35
36 uint8_t Flash_EraseSector(uint8_t sector){
37
38 /* Check no flash memory operation is ongoing */
39 if(FLASHINTR->SR & (1 << FLASH_SR_BSY)){
40 return 1;
41 }
42
43 /* Unlock Flash to perform erase operation */
44 Flash_Unlock();
45
46 if(sector != 0xFF){
47 /* Set Sector Erase bit in Flash Control Register */
48 FLASHINTR->CR |= (1 << FLASH_CR_SER);
49 /* Select the sector to be erased in the SNB bit in Flash Control Register */
50 FLASHINTR->CR &= ~(0x0F << FLASH_CR_SNB); /* Clear the SNB bits */
51 FLASHINTR->CR |= ((0x07 & sector) << FLASH_CR_SNB);
52 }
53 else{
54 /* Set Mass Erase bit in Flash Control Register */
55 FLASHINTR->CR |= (1 << FLASH_CR_MER);
56 }
57 /* Set the STRT bit in Flash Control Register */
58 FLASHINTR->CR |= (1 << FLASH_CR_STRT);
59
60 /* Wait for flash memory operation is finished */
61 while(FLASHINTR->SR & (1 << FLASH_SR_BSY));
62
63 /* Lock Flash Control Register */
64 Flash_Lock();
65
66 return 0;
67 }
68
69 uint8_t Flash_WriteMemoryByte(uintptr_t address, uint8_t data){
70
71 /* check no flash memory operation is ongoing */
72 if(FLASHINTR->SR & (1 << FLASH_SR_BSY)){
73 return 1;
74 }
75
76 /* Set PSIZE bits in the Flash Control Register */
77 Flash_SetPSIZE(FLASH_PSIZE_BYTE);
78
79 /* Set Programming bit in the Flash Control Register */
80 FLASHINTR->CR |= (1 << FLASH_CR_PG);
81
82 /* Write data in flash memory */
83 *(uint8_t*)address = data;
84
85 /* Wait for flash memory operation is finished */
86 while(FLASHINTR->SR & (1 << FLASH_SR_BSY));
87
88 /*Check for any error */
89 if(FLASHINTR->SR & ((1 << FLASH_SR_PGSERR) | (1 << FLASH_SR_PGPERR) |
90 (1 << FLASH_SR_PGAERR) | (1 << FLASH_SR_WRPERR))){
91 return 1;
92 }
93
94 return 0;
95 }
96
97 uint8_t Flash_WriteMemoryHalfWord(uintptr_t address, uint16_t data){
98
99 /* check no flash memory operation is ongoing */
100 if(FLASHINTR->SR & (1 << FLASH_SR_BSY)){
101 return 1;
102 }
103
104 /* Set PSIZE bits in the Flash Control Register */
105 Flash_SetPSIZE(FLASH_PSIZE_HALFWORD);
106
107 /* Set Programming bit in the Flash Control Register */
108 FLASHINTR->CR |= (1 << FLASH_CR_PG);
109
110 /* Write data in flash memory */
111 *(uint16_t*)address = data;
112
113 /* Wait for flash memory operation is finished */
114 while(FLASHINTR->SR & (1 << FLASH_SR_BSY));
115
116 /*Check for any error */
117 if(FLASHINTR->SR & ((1 << FLASH_SR_PGSERR) | (1 << FLASH_SR_PGPERR) |
118 (1 << FLASH_SR_PGAERR) | (1 << FLASH_SR_WRPERR))){
119 return 1;
120 }
121
122 return 0;
123 }
124
125 uint8_t Flash_WriteMemoryWord(uintptr_t address, uint32_t data){
126
127 /* check no flash memory operation is ongoing */
128 if(FLASHINTR->SR & (1 << FLASH_SR_BSY)){
129 return 1;
130 }
131
132 /* Set PSIZE bits in the Flash Control Register */
133 Flash_SetPSIZE(FLASH_PSIZE_WORD);
134
135 /* Set Programming bit in the Flash Control Register */
136 FLASHINTR->CR |= (1 << FLASH_CR_PG);
137
138 /* Write data in flash memory */
139 *(uint32_t*)address = data;
140
141 /* Wait for flash memory operation is finished */
142 while(FLASHINTR->SR & (1 << FLASH_SR_BSY));
143
144 /*Check for any error */
145 if(FLASHINTR->SR & ((1 << FLASH_SR_PGSERR) | (1 << FLASH_SR_PGPERR) |
146 (1 << FLASH_SR_PGAERR) | (1 << FLASH_SR_WRPERR))){
147 return 1;
148 }
149
150 return 0;
151 }
152
153 uint8_t Flash_WriteMemoryDoubleWord(uintptr_t address, uint64_t data){
154
155 /* check no flash memory operation is ongoing */
156 if(FLASHINTR->SR & (1 << FLASH_SR_BSY)){
157 return 1;
158 }
159
160 /* Set PSIZE bits in the Flash Control Register */
161 Flash_SetPSIZE(FLASH_PSIZE_DOUBLEWORD);
162
163 /* Set Programming bit in the Flash Control Register */
164 FLASHINTR->CR |= (1 << FLASH_CR_PG);
165
166 /* Write data in flash memory */
167 *(uint32_t*)address = (uint32_t)data;
168 *(uint32_t*)(address + 4) = (uint32_t)(data >> 32);
169
170 /* Wait for flash memory operation is finished */
171 while(FLASHINTR->SR & (1 << FLASH_SR_BSY));
172
173 /*Check for any error */
174 if(FLASHINTR->SR & ((1 << FLASH_SR_PGSERR) | (1 << FLASH_SR_PGPERR) |
175 (1 << FLASH_SR_PGAERR) | (1 << FLASH_SR_WRPERR))){
176 return 1;
177 }
178
179 return 0;
180 }
181
182 uint8_t Flash_EnRWProtection(uint8_t sectors, uint8_t protection_mode){
183
184 if(protection_mode == 1){
185 /* Check no flash memory operation is ongoing */
186 if(FLASHINTR->SR & (1 << FLASH_SR_BSY)){
187 return 1;
188 }
189
190 /* Unlock Flash to perform erase operation */
191 Flash_OPTUnlock();
192
193 /* Set write protection on sectors */
194 /* Reset SPRMOD bit in OPTCR register */
195 FLASHINTR->OPTCR &= ~(1 << FLASH_OPTCR_SPRMOD);
196 /* Set nWRP byte in OPTCR register (0 is protection active) */
197 FLASHINTR->OPTCR &= ~(sectors << FLASH_OPTCR_NWRP);
198
199 /* Set the STRT bit in Flash Control Register */
200 FLASHINTR->CR |= (1 << FLASH_CR_STRT);
201
202 /* Wait for flash memory operation is finished */
203 while(FLASHINTR->SR & (1 << FLASH_SR_BSY));
204
205 /* Lock Flash Control Register */
206 Flash_OPTLock();
207 }
208 else if(protection_mode == 2){
209 /* Check no flash memory operation is ongoing */
210 if(FLASHINTR->SR & (1 << FLASH_SR_BSY)){
211 return 1;
212 }
213
214 /* Unlock Flash to perform erase operation */
215 Flash_OPTUnlock();
216
217 /* Set read/write protection on sectors */
218 /* Set SPRMOD bit in OPTCR register */
219 FLASHINTR->OPTCR |= (1 << FLASH_OPTCR_SPRMOD);
220 /* Clear nWRP byte in OPTCR register */
221 FLASHINTR->OPTCR &= ~(0xFF << FLASH_OPTCR_NWRP);
222 /* Set nWRP byte in OPTCR register (1 is protection active) */
223 FLASHINTR->OPTCR |= (sectors << FLASH_OPTCR_NWRP);
224
225 /* Set the STRT bit in Flash Control Register */
226 FLASHINTR->CR |= (1 << FLASH_CR_STRT);
227
228 /* Wait for flash memory operation is finished */
229 while(FLASHINTR->SR & (1 << FLASH_SR_BSY));
230
231 /* Lock Flash Control Register */
232 Flash_OPTLock();
233 }
234 else{
235 return 1;
236 }
237
238 return 0;
239 }
240
241 uint8_t Flash_DisRWProtection(void){
242
243 /* Check no flash memory operation is ongoing */
244 if(FLASHINTR->SR & (1 << FLASH_SR_BSY)){
245 return 1;
246 }
247
248 /* Unlock Flash to perform erase operation */
249 Flash_OPTUnlock();
250
251 /* Set read/write protection on sectors */
252 /* Reset SPRMOD bit in OPTCR register */
253 FLASHINTR->OPTCR &= ~(1 << FLASH_OPTCR_SPRMOD);
254 /* Set nWRP byte in OPTCR register (1 is disabled) */
255 FLASHINTR->OPTCR |= (0xFF << FLASH_OPTCR_NWRP);
256
257 /* Set the STRT bit in Flash Control Register */
258 FLASHINTR->CR |= (1 << FLASH_CR_STRT);
259
260 /* Wait for flash memory operation is finished */
261 while(FLASHINTR->SR & (1 << FLASH_SR_BSY));
262
263 /* Lock Flash Control Register */
264 Flash_OPTLock();
265
266 return 0;
267 }
268
269 void Flash_Unlock(void){
270
271 /* Write KEY1 and KEY2 in Flash Key Register */
272 FLASHINTR->KEYR = 0x45670123;
273 FLASHINTR->KEYR = 0xCDEF89AB;
274 }
275
276 void Flash_Lock(void){
277
278 /* Set Lock bit in Flash Control Register */
279 FLASHINTR->CR |= (1 << FLASH_CR_LOCK);
280 }
281
282 void Flash_OPTUnlock(void){
283
284 /* Write KEY1 and KEY2 in Flash Option Key Register */
285 FLASHINTR->OPTKEYR = 0x08192A3B;
286 FLASHINTR->OPTKEYR = 0x4C5D6E7F;
287 }
288
289 void Flash_OPTLock(void){
290
291 /* Set Lock bit in Flash Option Control Register */
292 FLASHINTR->OPTCR |= (1 << FLASH_OPTCR_OPTLOCK);
293 }
294
295 void Flash_SetPSIZE(flash_psize_t psize){
296
297 /* Set PSIZE bits in the Flash Control Register */
298 FLASHINTR->CR &= (0x07 << FLASH_CR_PSIZE);
299 FLASHINTR->CR |= (psize << FLASH_CR_PSIZE);
300 }
301
302 uint8_t Flash_Busy(void){
303
304 if(FLASHINTR->SR & (1 << FLASH_SR_BSY)){
305 return 1;
306 }
307
308 return 0;
309 }
310
311 void Flash_GetOBCfg(OPT_Cfg_t* OPTCfg){
312
313 /* Unlock Option Byte Flash area */
314 Flash_OPTUnlock();
315
316 /* Get Option Byte configuration */
317 /* Get Not Write Protect byte */
318 OPTCfg->nWRP = (uint16_t)((FLASHINTR->OPTCR & (0xFFFF << FLASH_OPTCR_NWRP)) >> 16);
319 /*Get Read Protect byte */
320 OPTCfg->RDP = (uint8_t)((FLASHINTR->OPTCR & (0xFF << FLASH_OPTCR_RDP)) >> 8);
321 /* Get User Option bits */
322 OPTCfg->user = (uint8_t)(FLASHINTR->OPTCR & 0xE0);
323 /* Get BOR bits */
324 OPTCfg->BOR = (uint8_t)(FLASHINTR->OPTCR & 0x0C);
325
326 /* Lock Option Byte Flash area */
327 Flash_OPTLock();
328 }
329
330 uint8_t Flash_SetLatency(uint8_t latency){
331
332 /* Check value is allowed */
333 if(latency > 0x0F){
334 return 1;
335 }
336
337 /* Clear and set latency */
338 FLASHINTR->ACR &= ~(0x0F << FLASH_ACR_LATENCY);
339 FLASHINTR->ACR |= (latency << FLASH_ACR_LATENCY);
340
341 return 0;
342 }
343
344 void Flash_EnableCache(flash_cache_t cache_options){
345
346 FLASHINTR->ACR |= cache_options;
347 }
348
349 void Flash_DisableCache(flash_cache_t cache_options){
350
351 FLASHINTR->ACR &= ~cache_options;
352 }
353