CC26xx Driver Library |

- Main Page
- Related Pages
- CC26xx APIs
- Register Descriptions
- Online Resources

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
flashsafe.c
Go to the documentation of this file.
1 /******************************************************************************
2 * Filename: flash.c
3 * Revised: 2014-12-22 14:02:25 +0100 (ma, 22 des 2014)
4 * Revision: 42272
5 *
6 * Description: Thread Safe Driver for the Flash.
7 *
8 * Copyright (c) 2015, Texas Instruments Incorporated
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
13 *
14 * 1) Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 *
17 * 2) Redistributions in binary form must reproduce the above copyright notice,
18 * this list of conditions and the following disclaimer in the documentation
19 * and/or other materials provided with the distribution.
20 *
21 * 3) Neither the name of the ORGANIZATION nor the names of its contributors may
22 * be used to endorse or promote products derived from this software without
23 * specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 *
37 ******************************************************************************/
38
39 #include <inc/hw_types.h>
40 #include <driverlib/flashsafe.h>
41
42 //*****************************************************************************
43 //
44 // Handle support for DriverLib in ROM:
45 // This section will undo prototype renaming made in the header file
46 //
47 //*****************************************************************************
48 #ifndef DRIVERLIB_GENERATE_ROM
49 #undef FlashsafeSizeGet
50 #define FlashsafeSizeGet NOROM_FlashsafeSizeGet
51 #undef FlashsafeSectorSizeGet
52 #define FlashsafeSectorSizeGet NOROM_FlashsafeSectorSizeGet
53 #undef FlashsafePowerModeSet
54 #define FlashsafePowerModeSet NOROM_FlashsafePowerModeSet
55 #undef FlashsafePowerModeGet
56 #define FlashsafePowerModeGet NOROM_FlashsafePowerModeGet
57 #undef FlashsafeProtectionSet
58 #define FlashsafeProtectionSet NOROM_FlashsafeProtectionSet
59 #undef FlashsafeProtectionGet
60 #define FlashsafeProtectionGet NOROM_FlashsafeProtectionGet
61 #undef FlashsafeProtectionSave
62 #define FlashsafeProtectionSave NOROM_FlashsafeProtectionSave
63 #undef FlashsafeCheckFsmForError
64 #define FlashsafeCheckFsmForError NOROM_FlashsafeCheckFsmForError
65 #undef FlashsafeCheckFsmForReady
66 #define FlashsafeCheckFsmForReady NOROM_FlashsafeCheckFsmForReady
67 #undef FlashsafeIntRegister
68 #define FlashsafeIntRegister NOROM_FlashsafeIntRegister
69 #undef FlashsafeIntUnregister
70 #define FlashsafeIntUnregister NOROM_FlashsafeIntUnregister
71 #undef FlashsafeIntEnable
72 #define FlashsafeIntEnable NOROM_FlashsafeIntEnable
73 #undef FlashsafeIntDisable
74 #define FlashsafeIntDisable NOROM_FlashsafeIntDisable
75 #undef FlashsafeIntStatus
76 #define FlashsafeIntStatus NOROM_FlashsafeIntStatus
77 #undef FlashsafeIntClear
78 #define FlashsafeIntClear NOROM_FlashsafeIntClear
79 #undef FlashsafeSectorErase
80 #define FlashsafeSectorErase NOROM_FlashsafeSectorErase
81 #undef FlashsafeProgram
82 #define FlashsafeProgram NOROM_FlashsafeProgram
83 #undef FlashsafeProgramNowait
84 #define FlashsafeProgramNowait NOROM_FlashsafeProgramNowait
85 #undef FlashsafeProgramNowaitRelease
86 #define FlashsafeProgramNowaitRelease NOROM_FlashsafeProgramNowaitRelease
87 #undef FlashsafeDisableSectorsForWrite
88 #define FlashsafeDisableSectorsForWrite NOROM_FlashsafeDisableSectorsForWrite
89 #endif
90
91 //*****************************************************************************
92 //
94 //
95 //*****************************************************************************
96 uint32_t
97 FlashsafeSizeGet(void)
98 {
99 return(FlashSizeGet());
100 }
101
102 //*****************************************************************************
103 //
105 //
106 //*****************************************************************************
107 uint32_t
108 FlashsafeSectorSizeGet(void)
109 {
110 return(FlashSectorSizeGet());
111 }
112
113 //*****************************************************************************
114 //
116 //
117 //*****************************************************************************
118 uint32_t
119 FlashsafePowerModeSet(uint32_t ui32PowerMode, uint32_t ui32BankGracePeriode,
120 uint32_t ui32PumpGracePeriode)
121 {
122 //
123 // Check the arguments.
124 //
125 ASSERT(ui32PowerMode == FLASH_PWR_ACTIVE_MODE ||
126 ui32PowerMode == FLASH_PWR_OFF_MODE ||
127 ui32PowerMode == FLASH_PWR_DEEP_STDBY_MODE);
128 ASSERT(ui32BankGracePeriode <= 0xFF);
129 ASSERT(ui32PumpGracePeriode <= 0xFFFF);
130
131 if(FlashsafeSMPHTryAcquire())
132 {
133 FlashPowerModeSet(ui32PowerMode, ui32BankGracePeriode,
134 ui32PumpGracePeriode);
135 FlashsafeSMPHRelease();
136 return(FLASHSAFE_ACCESS_OK);
137 }
138
139 return(FLASHSAFE_ACCESS_DENIED);
140 }
141
142 //*****************************************************************************
143 //
145 //
146 //*****************************************************************************
147 uint32_t
148 FlashsafePowerModeGet(void)
149 {
150 uint32_t ui32PowerMode;
151
152 if(FlashsafeSMPHTryAcquire())
153 {
154 ui32PowerMode = FlashPowerModeGet();
155 FlashsafeSMPHRelease();
156 return(ui32PowerMode);
157 }
158
159 return(FLASHSAFE_ACCESS_DENIED);
160 }
161
162 //*****************************************************************************
163 //
165 //
166 //*****************************************************************************
167 uint32_t
168 FlashsafeProtectionSet(uint32_t ui32SectorAddress, uint32_t ui32ProtectMode)
169 {
170 //
171 // Check the arguments.
172 //
173 ASSERT(ui32SectorAddress <= (FLASHMEM_BASE + FlashsafeSizeGet() -
174 FlashsafeSectorSizeGet()));
175 ASSERT((ui32SectorAddress & (FlashsafeSectorSizeGet() - 1)) == 00);
176
177 if(FlashsafeSMPHTryAcquire())
178 {
179 FlashProtectionSet(ui32SectorAddress, ui32ProtectMode);
180 FlashsafeSMPHRelease();
181 return(FLASHSAFE_ACCESS_OK);
182 }
183
184 return(FLASHSAFE_ACCESS_DENIED);
185 }
186
187 //*****************************************************************************
188 //
190 //
191 //*****************************************************************************
192 uint32_t
193 FlashsafeProtectionGet(uint32_t ui32SectorAddress)
194 {
195 uint32_t ui32SectorProtect;
196
197 //
198 // Check the arguments.
199 //
200 ASSERT(ui32SectorAddress <= (FLASHMEM_BASE + FlashsafeSizeGet() -
201 FlashsafeSectorSizeGet()));
202 ASSERT((ui32SectorAddress & (FlashsafeSectorSizeGet() - 1)) == 00);
203
204 if(FlashsafeSMPHTryAcquire())
205 {
206 ui32SectorProtect = FlashProtectionGet(ui32SectorAddress);
207 FlashsafeSMPHRelease();
208 return(ui32SectorProtect);
209 }
210
211 return(FLASHSAFE_ACCESS_DENIED);
212 }
213
214 //*****************************************************************************
215 //
217 //
218 //*****************************************************************************
219 uint32_t
220 FlashsafeProtectionSave(uint32_t ui32SectorAddress)
221 {
222 uint32_t ui32ErrorReturn;
223
224 //
225 // Check the arguments.
226 //
227 ASSERT(ui32SectorAddress <= (FLASHMEM_BASE + FlashsafeSizeGet() -
228 FlashsafeSectorSizeGet()));
229 ASSERT((ui32SectorAddress & (FlashsafeSectorSizeGet() - 1)) == 00);
230
231 if(FlashsafeSMPHTryAcquire())
232 {
233 ui32ErrorReturn = FlashProtectionSave(ui32SectorAddress);
234 FlashsafeSMPHRelease();
235 return(ui32ErrorReturn);
236 }
237
238 return(FLASHSAFE_ACCESS_DENIED);
239 }
240
241 //*****************************************************************************
242 //
244 //
245 //*****************************************************************************
246 uint32_t
247 FlashsafeCheckFsmForError(void)
248 {
249 if(HWREG(FLASH_BASE + FLASH_O_FMSTAT) & FLASH_FMSTAT_CSTAT)
250 {
251 return(FAPI_STATUS_FSM_ERROR);
252 }
253 else
254 {
255 return(FAPI_STATUS_SUCCESS);
256 }
257 }
258
259 //*****************************************************************************
260 //
262 //
263 //*****************************************************************************
264 uint32_t
265 FlashsafeCheckFsmForReady(void)
266 {
267 if(HWREG(FLASH_BASE + FLASH_O_STAT) & FLASH_STAT_BUSY)
268 {
269 return(FAPI_STATUS_FSM_BUSY);
270 }
271 else
272 {
273 return(FAPI_STATUS_FSM_READY);
274 }
275 }
276
277 //*****************************************************************************
278 //
280 //
281 //*****************************************************************************
282 uint32_t
283 FlashsafeIntRegister(void (*pfnHandler)(void))
284 {
285 if(FlashsafeSMPHTryAcquire())
286 {
287 //
288 // Register the interrupt handler.
289 //
290 IntRegister(INT_FLASH, pfnHandler);
291
292 //
293 // Enable the flash interrupt.
294 //
295 IntEnable(INT_FLASH);
296
297 FlashsafeSMPHRelease();
298 return(FLASHSAFE_ACCESS_OK);
299 }
300
301 return(FLASHSAFE_ACCESS_DENIED);
302 }
303
304 //*****************************************************************************
305 //
307 //
308 //*****************************************************************************
309 uint32_t
310 FlashsafeIntUnregister(void)
311 {
312 if(FlashsafeSMPHTryAcquire())
313 {
314 //
315 // Disable the interrupts.
316 //
317 IntDisable(INT_FLASH);
318
319 //
320 // Unregister the interrupt handler.
321 //
322 IntUnregister(INT_FLASH);
323
324 FlashsafeSMPHRelease();
325 return(FLASHSAFE_ACCESS_OK);
326 }
327
328 return(FLASHSAFE_ACCESS_DENIED);
329 }
330
331 //*****************************************************************************
332 //
334 //
335 //*****************************************************************************
336 uint32_t
337 FlashsafeIntEnable(uint32_t ui32IntFlags)
338 {
339 if(FlashsafeSMPHTryAcquire())
340 {
341 HWREG(FLASH_BASE + FLASH_O_FSM_WR_ENA) = FSM_REG_WRT_ENABLE;
342 HWREG(FLASH_BASE + FLASH_O_FSM_ST_MACHINE) |= ui32IntFlags;
343 HWREG(FLASH_BASE + FLASH_O_FSM_WR_ENA) = FSM_REG_WRT_DISABLE;
344
345 FlashsafeSMPHRelease();
346 return(FLASHSAFE_ACCESS_OK);
347 }
348
349 return(FLASHSAFE_ACCESS_DENIED);
350 }
351
352 //*****************************************************************************
353 //
355 //
356 //*****************************************************************************
357 uint32_t
358 FlashsafeIntDisable(uint32_t ui32IntFlags)
359 {
360 if(FlashsafeSMPHTryAcquire())
361 {
362 HWREG(FLASH_BASE + FLASH_O_FSM_WR_ENA) = FSM_REG_WRT_ENABLE;
363 HWREG(FLASH_BASE + FLASH_O_FSM_ST_MACHINE) &= ~ui32IntFlags;
364 HWREG(FLASH_BASE + FLASH_O_FSM_WR_ENA) = FSM_REG_WRT_DISABLE;
365 FlashsafeSMPHRelease();
366 return(FLASHSAFE_ACCESS_OK);
367 }
368
369 return(FLASHSAFE_ACCESS_DENIED);
370 }
371
372 //*****************************************************************************
373 //
375 //
376 //*****************************************************************************
377 uint32_t
378 FlashsafeIntStatus(void)
379 {
380 return(FlashIntStatus());
381 }
382
383 //*****************************************************************************
384 //
386 //
387 //*****************************************************************************
388 uint32_t
389 FlashsafeIntClear(uint32_t ui32IntFlags)
390 {
391 if(FlashsafeSMPHTryAcquire())
392 {
393 FlashIntClear(ui32IntFlags);
394 FlashsafeSMPHRelease();
395 return(FLASHSAFE_ACCESS_OK);
396 }
397
398 return(FLASHSAFE_ACCESS_DENIED);
399 }
400
401 //*****************************************************************************
402 //
404 //
405 //*****************************************************************************
406 uint32_t
407 FlashsafeSectorErase(uint32_t ui32SectorAddress)
408 {
409 uint32_t ui32ErrorReturn;
410
411 //
412 // Check the arguments.
413 //
414 ASSERT(ui32SectorAddress <= (FLASHMEM_BASE + FlashsafeSizeGet() -
415 FlashsafeSectorSizeGet()));
416 ASSERT((ui32SectorAddress & (FlashsafeSectorSizeGet() - 1)) == 00);
417
418 if(FlashsafeSMPHTryAcquire())
419 {
420 ui32ErrorReturn = FlashSectorErase(ui32SectorAddress);
421 FlashsafeSMPHRelease();
422 return(ui32ErrorReturn);
423 }
424
425 return(FLASHSAFE_ACCESS_DENIED);
426 }
427
428
429 //*****************************************************************************
430 //
432 //
433 //*****************************************************************************
434 uint32_t
435 FlashsafeProgram(uint8_t *pui8DataBuffer, uint32_t ui32Address, uint32_t ui32Count)
436 {
437 uint32_t ui32ErrorReturn;
438
439 //
440 // Check the arguments.
441 //
442 ASSERT((ui32Address + ui32Count) <= (FLASHMEM_BASE + FlashsafeSizeGet()));
443
444 if(FlashsafeSMPHTryAcquire())
445 {
446 ui32ErrorReturn = FlashProgram(pui8DataBuffer, ui32Address, ui32Count);
447 FlashsafeSMPHRelease();
448 return(ui32ErrorReturn);
449 }
450
451 return(FLASHSAFE_ACCESS_DENIED);
452 }
453
454
455 //*****************************************************************************
456 //
458 //
459 //*****************************************************************************
460 uint32_t
461 FlashsafeDisableSectorsForWrite(void)
462 {
463
464 if(FlashsafeSMPHTryAcquire())
465 {
466 FlashDisableSectorsForWrite();
467 FlashsafeSMPHRelease();
468 return(FLASHSAFE_ACCESS_OK);
469 }
470
471 return(FLASHSAFE_ACCESS_DENIED);
472 }
FlashsafeSMPHTryAcquire
__STATIC_INLINE bool FlashsafeSMPHTryAcquire(void)
Aquire the flash semaphore.
Definition: flashsafe.h:178
FlashsafeSectorSizeGet
uint32_t FlashsafeSectorSizeGet(void)
Get size of a flash sector in no of bytes.
Definition: flashsafe.c:108
FlashsafeSMPHRelease
__STATIC_INLINE void FlashsafeSMPHRelease(void)
Release the flash semaphore.
Definition: flashsafe.h:191
FLASH_PWR_OFF_MODE
#define FLASH_PWR_OFF_MODE
Definition: flash.h:126
FlashsafeSectorErase
uint32_t FlashsafeSectorErase(uint32_t ui32SectorAddress)
Erase a flash sector.
Definition: flashsafe.c:407
FlashSectorSizeGet
__STATIC_INLINE uint32_t FlashSectorSizeGet(void)
Get size of a flash sector in number of bytes.
Definition: flash.h:260
FlashsafeIntEnable
uint32_t FlashsafeIntEnable(uint32_t ui32IntFlags)
Enables flash controller interrupt sources.
Definition: flashsafe.c:337
FlashProtectionGet
uint32_t FlashProtectionGet(uint32_t ui32SectorAddress)
Get sector protection.
Definition: flash.c:267
FlashsafeCheckFsmForError
uint32_t FlashsafeCheckFsmForError(void)
Checks if the Flashsafe state machine has detected an error.
Definition: flashsafe.c:247
FlashsafePowerModeGet
uint32_t FlashsafePowerModeGet(void)
Get current configured power mode.
Definition: flashsafe.c:148
FlashPowerModeSet
void FlashPowerModeSet(uint32_t ui32PowerMode, uint32_t ui32BankGracePeriode, uint32_t ui32PumpGracePeriode)
Set power mode.
Definition: flash.c:103
FlashsafeProgram
uint32_t FlashsafeProgram(uint8_t *pui8DataBuffer, uint32_t ui32Address, uint32_t ui32Count)
Programs unprotected main bank flash sectors.
Definition: flashsafe.c:435
FlashsafeIntClear
uint32_t FlashsafeIntClear(uint32_t ui32IntFlags)
Clears flash controller interrupt source.
Definition: flashsafe.c:389
FLASHSAFE_ACCESS_DENIED
#define FLASHSAFE_ACCESS_DENIED
Definition: flashsafe.h:118
FlashDisableSectorsForWrite
void FlashDisableSectorsForWrite(void)
Disables all sectors for erase and programming on the active bank.
Definition: flash.c:520
ASSERT
#define ASSERT(expr)
Definition: debug.h:65
FlashsafeIntDisable
uint32_t FlashsafeIntDisable(uint32_t ui32IntFlags)
Disables individual flash controller interrupt sources.
Definition: flashsafe.c:358
FlashsafeCheckFsmForReady
uint32_t FlashsafeCheckFsmForReady(void)
Checks if the Flash state machine is ready.
Definition: flashsafe.c:265
FSM_REG_WRT_DISABLE
#define FSM_REG_WRT_DISABLE
Definition: flash.h:185
FlashPowerModeGet
uint32_t FlashPowerModeGet(void)
Get current configured power mode.
Definition: flash.c:194
FlashSizeGet
__STATIC_INLINE uint32_t FlashSizeGet(void)
Get the size of the flash.
Definition: flash.h:284
FlashsafeProtectionSet
uint32_t FlashsafeProtectionSet(uint32_t ui32SectorAddress, uint32_t ui32ProtectMode)
Set sector protection.
Definition: flashsafe.c:168
FAPI_STATUS_SUCCESS
#define FAPI_STATUS_SUCCESS
Definition: flash.h:104
FlashsafePowerModeSet
uint32_t FlashsafePowerModeSet(uint32_t ui32PowerMode, uint32_t ui32BankGracePeriode, uint32_t ui32PumpGracePeriode)
Set power mode.
Definition: flashsafe.c:119
FlashSectorErase
uint32_t FlashSectorErase(uint32_t ui32SectorAddress)
Erase a flash sector.
Definition: flash.c:364
FSM_REG_WRT_ENABLE
#define FSM_REG_WRT_ENABLE
Definition: flash.h:184
FAPI_STATUS_FSM_ERROR
#define FAPI_STATUS_FSM_ERROR
Definition: flash.h:109
FlashIntClear
__STATIC_INLINE void FlashIntClear(uint32_t ui32IntFlags)
Clears flash controller interrupt source.
Definition: flash.h:646
FAPI_STATUS_FSM_READY
#define FAPI_STATUS_FSM_READY
Definition: flash.h:106
FlashsafeDisableSectorsForWrite
uint32_t FlashsafeDisableSectorsForWrite(void)
Disables all sectors for erase and programming on the active bank.
Definition: flashsafe.c:461
FlashsafeProtectionGet
uint32_t FlashsafeProtectionGet(uint32_t ui32SectorAddress)
Get sector protection.
Definition: flashsafe.c:193
FlashsafeProtectionSave
uint32_t FlashsafeProtectionSave(uint32_t ui32SectorAddress)
Save sector protection to make it permanent.
Definition: flashsafe.c:220
FlashsafeIntStatus
uint32_t FlashsafeIntStatus(void)
Gets the current interrupt status.
Definition: flashsafe.c:378
IntUnregister
void IntUnregister(uint32_t ui32Interrupt)
Unregisters the function to be called when an interrupt occurs.
Definition: interrupt.c:205
FlashIntStatus
__STATIC_INLINE uint32_t FlashIntStatus(void)
Gets the current interrupt status.
Definition: flash.h:595
FLASHSAFE_ACCESS_OK
#define FLASHSAFE_ACCESS_OK
Definition: flashsafe.h:119
FlashProtectionSave
uint32_t FlashProtectionSave(uint32_t ui32SectorAddress)
Save sector protection to make it permanent.
Definition: flash.c:310
FLASH_PWR_ACTIVE_MODE
#define FLASH_PWR_ACTIVE_MODE
Definition: flash.h:125
flashsafe.h
FlashProtectionSet
void FlashProtectionSet(uint32_t ui32SectorAddress, uint32_t ui32ProtectMode)
Set sector protection.
Definition: flash.c:227
FLASH_PWR_DEEP_STDBY_MODE
#define FLASH_PWR_DEEP_STDBY_MODE
Definition: flash.h:127
FlashsafeSizeGet
uint32_t FlashsafeSizeGet(void)
Get the size of the flash.
Definition: flashsafe.c:97
FAPI_STATUS_FSM_BUSY
#define FAPI_STATUS_FSM_BUSY
Definition: flash.h:105
FlashsafeIntUnregister
uint32_t FlashsafeIntUnregister(void)
Unregisters the interrupt handler for the flash interrupt.
Definition: flashsafe.c:310
FlashProgram
uint32_t FlashProgram(uint8_t *pui8DataBuffer, uint32_t ui32Address, uint32_t ui32Count)
Programs unprotected main bank flash sectors.
Definition: flash.c:401
FlashsafeIntRegister
uint32_t FlashsafeIntRegister(void(*pfnHandler)(void))
Registers an interrupt handler for the flash interrupt.
Definition: flashsafe.c:283
IntDisable
void IntDisable(uint32_t ui32Interrupt)
Disables an interrupt.
Definition: interrupt.c:383
IntRegister
void IntRegister(uint32_t ui32Interrupt, void(*pfnHandler)(void))
Registers a function to be called when an interrupt occurs.
Definition: interrupt.c:157
IntEnable
void IntEnable(uint32_t ui32Interrupt)
Enables an interrupt.
Definition: interrupt.c:323