CC26xx Driver Library
 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
98 {
99  return(FlashSizeGet());
100 }
101 
102 //*****************************************************************************
103 //
105 //
106 //*****************************************************************************
107 uint32_t
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 
132  {
133  FlashPowerModeSet(ui32PowerMode, ui32BankGracePeriode,
134  ui32PumpGracePeriode);
136  return(FLASHSAFE_ACCESS_OK);
137  }
138 
139  return(FLASHSAFE_ACCESS_DENIED);
140 }
141 
142 //*****************************************************************************
143 //
145 //
146 //*****************************************************************************
147 uint32_t
149 {
150  uint32_t ui32PowerMode;
151 
153  {
154  ui32PowerMode = FlashPowerModeGet();
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() -
175  ASSERT((ui32SectorAddress & (FlashsafeSectorSizeGet() - 1)) == 00);
176 
178  {
179  FlashProtectionSet(ui32SectorAddress, ui32ProtectMode);
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() -
202  ASSERT((ui32SectorAddress & (FlashsafeSectorSizeGet() - 1)) == 00);
203 
205  {
206  ui32SectorProtect = FlashProtectionGet(ui32SectorAddress);
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() -
229  ASSERT((ui32SectorAddress & (FlashsafeSectorSizeGet() - 1)) == 00);
230 
232  {
233  ui32ErrorReturn = FlashProtectionSave(ui32SectorAddress);
235  return(ui32ErrorReturn);
236  }
237 
238  return(FLASHSAFE_ACCESS_DENIED);
239 }
240 
241 //*****************************************************************************
242 //
244 //
245 //*****************************************************************************
246 uint32_t
248 {
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
266 {
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 {
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 
298  return(FLASHSAFE_ACCESS_OK);
299  }
300 
301  return(FLASHSAFE_ACCESS_DENIED);
302 }
303 
304 //*****************************************************************************
305 //
307 //
308 //*****************************************************************************
309 uint32_t
311 {
313  {
314  //
315  // Disable the interrupts.
316  //
317  IntDisable(INT_FLASH);
318 
319  //
320  // Unregister the interrupt handler.
321  //
322  IntUnregister(INT_FLASH);
323 
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 {
340  {
342  HWREG(FLASH_BASE + FLASH_O_FSM_ST_MACHINE) |= ui32IntFlags;
344 
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 {
361  {
363  HWREG(FLASH_BASE + FLASH_O_FSM_ST_MACHINE) &= ~ui32IntFlags;
366  return(FLASHSAFE_ACCESS_OK);
367  }
368 
369  return(FLASHSAFE_ACCESS_DENIED);
370 }
371 
372 //*****************************************************************************
373 //
375 //
376 //*****************************************************************************
377 uint32_t
379 {
380  return(FlashIntStatus());
381 }
382 
383 //*****************************************************************************
384 //
386 //
387 //*****************************************************************************
388 uint32_t
389 FlashsafeIntClear(uint32_t ui32IntFlags)
390 {
392  {
393  FlashIntClear(ui32IntFlags);
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() -
416  ASSERT((ui32SectorAddress & (FlashsafeSectorSizeGet() - 1)) == 00);
417 
419  {
420  ui32ErrorReturn = FlashSectorErase(ui32SectorAddress);
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 
445  {
446  ui32ErrorReturn = FlashProgram(pui8DataBuffer, ui32Address, ui32Count);
448  return(ui32ErrorReturn);
449  }
450 
451  return(FLASHSAFE_ACCESS_DENIED);
452 }
453 
454 
455 //*****************************************************************************
456 //
458 //
459 //*****************************************************************************
460 uint32_t
462 {
463 
465  {
468  return(FLASHSAFE_ACCESS_OK);
469  }
470 
471  return(FLASHSAFE_ACCESS_DENIED);
472 }
__STATIC_INLINE bool FlashsafeSMPHTryAcquire(void)
Aquire the flash semaphore.
Definition: flashsafe.h:178
uint32_t FlashsafeSectorSizeGet(void)
Get size of a flash sector in no of bytes.
Definition: flashsafe.c:108
__STATIC_INLINE void FlashsafeSMPHRelease(void)
Release the flash semaphore.
Definition: flashsafe.h:191
#define FLASH_PWR_OFF_MODE
Definition: flash.h:126
uint32_t FlashsafeSectorErase(uint32_t ui32SectorAddress)
Erase a flash sector.
Definition: flashsafe.c:407
__STATIC_INLINE uint32_t FlashSectorSizeGet(void)
Get size of a flash sector in number of bytes.
Definition: flash.h:260
uint32_t FlashsafeIntEnable(uint32_t ui32IntFlags)
Enables flash controller interrupt sources.
Definition: flashsafe.c:337
uint32_t FlashProtectionGet(uint32_t ui32SectorAddress)
Get sector protection.
Definition: flash.c:267
uint32_t FlashsafeCheckFsmForError(void)
Checks if the Flashsafe state machine has detected an error.
Definition: flashsafe.c:247
uint32_t FlashsafePowerModeGet(void)
Get current configured power mode.
Definition: flashsafe.c:148
void FlashPowerModeSet(uint32_t ui32PowerMode, uint32_t ui32BankGracePeriode, uint32_t ui32PumpGracePeriode)
Set power mode.
Definition: flash.c:103
uint32_t FlashsafeProgram(uint8_t *pui8DataBuffer, uint32_t ui32Address, uint32_t ui32Count)
Programs unprotected main bank flash sectors.
Definition: flashsafe.c:435
uint32_t FlashsafeIntClear(uint32_t ui32IntFlags)
Clears flash controller interrupt source.
Definition: flashsafe.c:389
#define FLASHSAFE_ACCESS_DENIED
Definition: flashsafe.h:118
void FlashDisableSectorsForWrite(void)
Disables all sectors for erase and programming on the active bank.
Definition: flash.c:520
#define ASSERT(expr)
Definition: debug.h:65
uint32_t FlashsafeIntDisable(uint32_t ui32IntFlags)
Disables individual flash controller interrupt sources.
Definition: flashsafe.c:358
uint32_t FlashsafeCheckFsmForReady(void)
Checks if the Flash state machine is ready.
Definition: flashsafe.c:265
#define FSM_REG_WRT_DISABLE
Definition: flash.h:185
uint32_t FlashPowerModeGet(void)
Get current configured power mode.
Definition: flash.c:194
__STATIC_INLINE uint32_t FlashSizeGet(void)
Get the size of the flash.
Definition: flash.h:284
uint32_t FlashsafeProtectionSet(uint32_t ui32SectorAddress, uint32_t ui32ProtectMode)
Set sector protection.
Definition: flashsafe.c:168
#define FAPI_STATUS_SUCCESS
Definition: flash.h:104
uint32_t FlashsafePowerModeSet(uint32_t ui32PowerMode, uint32_t ui32BankGracePeriode, uint32_t ui32PumpGracePeriode)
Set power mode.
Definition: flashsafe.c:119
uint32_t FlashSectorErase(uint32_t ui32SectorAddress)
Erase a flash sector.
Definition: flash.c:364
#define FSM_REG_WRT_ENABLE
Definition: flash.h:184
#define FAPI_STATUS_FSM_ERROR
Definition: flash.h:109
__STATIC_INLINE void FlashIntClear(uint32_t ui32IntFlags)
Clears flash controller interrupt source.
Definition: flash.h:646
#define FAPI_STATUS_FSM_READY
Definition: flash.h:106
uint32_t FlashsafeDisableSectorsForWrite(void)
Disables all sectors for erase and programming on the active bank.
Definition: flashsafe.c:461
uint32_t FlashsafeProtectionGet(uint32_t ui32SectorAddress)
Get sector protection.
Definition: flashsafe.c:193
uint32_t FlashsafeProtectionSave(uint32_t ui32SectorAddress)
Save sector protection to make it permanent.
Definition: flashsafe.c:220
uint32_t FlashsafeIntStatus(void)
Gets the current interrupt status.
Definition: flashsafe.c:378
void IntUnregister(uint32_t ui32Interrupt)
Unregisters the function to be called when an interrupt occurs.
Definition: interrupt.c:205
__STATIC_INLINE uint32_t FlashIntStatus(void)
Gets the current interrupt status.
Definition: flash.h:595
#define FLASHSAFE_ACCESS_OK
Definition: flashsafe.h:119
uint32_t FlashProtectionSave(uint32_t ui32SectorAddress)
Save sector protection to make it permanent.
Definition: flash.c:310
#define FLASH_PWR_ACTIVE_MODE
Definition: flash.h:125
void FlashProtectionSet(uint32_t ui32SectorAddress, uint32_t ui32ProtectMode)
Set sector protection.
Definition: flash.c:227
#define FLASH_PWR_DEEP_STDBY_MODE
Definition: flash.h:127
uint32_t FlashsafeSizeGet(void)
Get the size of the flash.
Definition: flashsafe.c:97
#define FAPI_STATUS_FSM_BUSY
Definition: flash.h:105
uint32_t FlashsafeIntUnregister(void)
Unregisters the interrupt handler for the flash interrupt.
Definition: flashsafe.c:310
uint32_t FlashProgram(uint8_t *pui8DataBuffer, uint32_t ui32Address, uint32_t ui32Count)
Programs unprotected main bank flash sectors.
Definition: flash.c:401
uint32_t FlashsafeIntRegister(void(*pfnHandler)(void))
Registers an interrupt handler for the flash interrupt.
Definition: flashsafe.c:283
void IntDisable(uint32_t ui32Interrupt)
Disables an interrupt.
Definition: interrupt.c:383
void IntRegister(uint32_t ui32Interrupt, void(*pfnHandler)(void))
Registers a function to be called when an interrupt occurs.
Definition: interrupt.c:157
void IntEnable(uint32_t ui32Interrupt)
Enables an interrupt.
Definition: interrupt.c:323