CC13xx Driver Library
flashsafe.c
Go to the documentation of this file.
1 /******************************************************************************
2 * Filename: flash.c
3 * Revised: 2015-02-03 14:18:59 +0100 (Tue, 03 Feb 2015)
4 * Revision: 42551
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 #if !defined(DOXYGEN)
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 FlashsafeDisableSectorsForWrite
84  #define FlashsafeDisableSectorsForWrite NOROM_FlashsafeDisableSectorsForWrite
85 #endif
86 
87 //*****************************************************************************
88 //
90 //
91 //*****************************************************************************
92 uint32_t
94 {
95  return(FlashSizeGet());
96 }
97 
98 //*****************************************************************************
99 //
101 //
102 //*****************************************************************************
103 uint32_t
105 {
106  return(FlashSectorSizeGet());
107 }
108 
109 //*****************************************************************************
110 //
112 //
113 //*****************************************************************************
114 uint32_t
115 FlashsafePowerModeSet(uint32_t ui32PowerMode, uint32_t ui32BankGracePeriode,
116  uint32_t ui32PumpGracePeriode)
117 {
118  //
119  // Check the arguments.
120  //
121  ASSERT(ui32PowerMode == FLASH_PWR_ACTIVE_MODE ||
122  ui32PowerMode == FLASH_PWR_OFF_MODE ||
123  ui32PowerMode == FLASH_PWR_DEEP_STDBY_MODE);
124  ASSERT(ui32BankGracePeriode <= 0xFF);
125  ASSERT(ui32PumpGracePeriode <= 0xFFFF);
126 
128  {
129  FlashPowerModeSet(ui32PowerMode, ui32BankGracePeriode,
130  ui32PumpGracePeriode);
132  return(FLASHSAFE_ACCESS_OK);
133  }
134 
135  return(FLASHSAFE_ACCESS_DENIED);
136 }
137 
138 //*****************************************************************************
139 //
141 //
142 //*****************************************************************************
143 uint32_t
145 {
146  uint32_t ui32PowerMode;
147 
149  {
150  ui32PowerMode = FlashPowerModeGet();
152  return(ui32PowerMode);
153  }
154 
155  return(FLASHSAFE_ACCESS_DENIED);
156 }
157 
158 //*****************************************************************************
159 //
161 //
162 //*****************************************************************************
163 uint32_t
164 FlashsafeProtectionSet(uint32_t ui32SectorAddress, uint32_t ui32ProtectMode)
165 {
166  //
167  // Check the arguments.
168  //
169  ASSERT(ui32SectorAddress <= (FLASHMEM_BASE + FlashsafeSizeGet() -
171  ASSERT((ui32SectorAddress & (FlashsafeSectorSizeGet() - 1)) == 00);
172 
174  {
175  FlashProtectionSet(ui32SectorAddress, ui32ProtectMode);
177  return(FLASHSAFE_ACCESS_OK);
178  }
179 
180  return(FLASHSAFE_ACCESS_DENIED);
181 }
182 
183 //*****************************************************************************
184 //
186 //
187 //*****************************************************************************
188 uint32_t
189 FlashsafeProtectionGet(uint32_t ui32SectorAddress)
190 {
191  uint32_t ui32SectorProtect;
192 
193  //
194  // Check the arguments.
195  //
196  ASSERT(ui32SectorAddress <= (FLASHMEM_BASE + FlashsafeSizeGet() -
198  ASSERT((ui32SectorAddress & (FlashsafeSectorSizeGet() - 1)) == 00);
199 
201  {
202  ui32SectorProtect = FlashProtectionGet(ui32SectorAddress);
204  return(ui32SectorProtect);
205  }
206 
207  return(FLASHSAFE_ACCESS_DENIED);
208 }
209 
210 //*****************************************************************************
211 //
213 //
214 //*****************************************************************************
215 uint32_t
216 FlashsafeProtectionSave(uint32_t ui32SectorAddress)
217 {
218  uint32_t ui32ErrorReturn;
219 
220  //
221  // Check the arguments.
222  //
223  ASSERT(ui32SectorAddress <= (FLASHMEM_BASE + FlashsafeSizeGet() -
225  ASSERT((ui32SectorAddress & (FlashsafeSectorSizeGet() - 1)) == 00);
226 
228  {
229  ui32ErrorReturn = FlashProtectionSave(ui32SectorAddress);
231  return(ui32ErrorReturn);
232  }
233 
234  return(FLASHSAFE_ACCESS_DENIED);
235 }
236 
237 //*****************************************************************************
238 //
240 //
241 //*****************************************************************************
242 uint32_t
244 {
246  {
247  return(FAPI_STATUS_FSM_ERROR);
248  }
249  else
250  {
251  return(FAPI_STATUS_SUCCESS);
252  }
253 }
254 
255 //*****************************************************************************
256 //
258 //
259 //*****************************************************************************
260 uint32_t
262 {
264  {
265  return(FAPI_STATUS_FSM_BUSY);
266  }
267  else
268  {
269  return(FAPI_STATUS_FSM_READY);
270  }
271 }
272 
273 //*****************************************************************************
274 //
276 //
277 //*****************************************************************************
278 uint32_t
279 FlashsafeIntRegister(void (*pfnHandler)(void))
280 {
282  {
283  //
284  // Register the interrupt handler.
285  //
286  IntRegister(INT_FLASH, pfnHandler);
287 
288  //
289  // Enable the flash interrupt.
290  //
291  IntEnable(INT_FLASH);
292 
294  return(FLASHSAFE_ACCESS_OK);
295  }
296 
297  return(FLASHSAFE_ACCESS_DENIED);
298 }
299 
300 //*****************************************************************************
301 //
303 //
304 //*****************************************************************************
305 uint32_t
307 {
309  {
310  //
311  // Disable the interrupts.
312  //
313  IntDisable(INT_FLASH);
314 
315  //
316  // Unregister the interrupt handler.
317  //
318  IntUnregister(INT_FLASH);
319 
321  return(FLASHSAFE_ACCESS_OK);
322  }
323 
324  return(FLASHSAFE_ACCESS_DENIED);
325 }
326 
327 //*****************************************************************************
328 //
330 //
331 //*****************************************************************************
332 uint32_t
333 FlashsafeIntEnable(uint32_t ui32IntFlags)
334 {
336  {
338  HWREG(FLASH_BASE + FLASH_O_FSM_ST_MACHINE) |= ui32IntFlags;
340 
342  return(FLASHSAFE_ACCESS_OK);
343  }
344 
345  return(FLASHSAFE_ACCESS_DENIED);
346 }
347 
348 //*****************************************************************************
349 //
351 //
352 //*****************************************************************************
353 uint32_t
354 FlashsafeIntDisable(uint32_t ui32IntFlags)
355 {
357  {
359  HWREG(FLASH_BASE + FLASH_O_FSM_ST_MACHINE) &= ~ui32IntFlags;
362  return(FLASHSAFE_ACCESS_OK);
363  }
364 
365  return(FLASHSAFE_ACCESS_DENIED);
366 }
367 
368 //*****************************************************************************
369 //
371 //
372 //*****************************************************************************
373 uint32_t
375 {
376  return(FlashIntStatus());
377 }
378 
379 //*****************************************************************************
380 //
382 //
383 //*****************************************************************************
384 uint32_t
385 FlashsafeIntClear(uint32_t ui32IntFlags)
386 {
388  {
389  FlashIntClear(ui32IntFlags);
391  return(FLASHSAFE_ACCESS_OK);
392  }
393 
394  return(FLASHSAFE_ACCESS_DENIED);
395 }
396 
397 //*****************************************************************************
398 //
400 //
401 //*****************************************************************************
402 uint32_t
403 FlashsafeSectorErase(uint32_t ui32SectorAddress)
404 {
405  uint32_t ui32ErrorReturn;
406 
407  //
408  // Check the arguments.
409  //
410  ASSERT(ui32SectorAddress <= (FLASHMEM_BASE + FlashsafeSizeGet() -
412  ASSERT((ui32SectorAddress & (FlashsafeSectorSizeGet() - 1)) == 00);
413 
415  {
416  ui32ErrorReturn = FlashSectorErase(ui32SectorAddress);
418  return(ui32ErrorReturn);
419  }
420 
421  return(FLASHSAFE_ACCESS_DENIED);
422 }
423 
424 
425 //*****************************************************************************
426 //
428 //
429 //*****************************************************************************
430 uint32_t
431 FlashsafeProgram(uint8_t *pui8DataBuffer, uint32_t ui32Address, uint32_t ui32Count)
432 {
433  uint32_t ui32ErrorReturn;
434 
435  //
436  // Check the arguments.
437  //
438  ASSERT((ui32Address + ui32Count) <= (FLASHMEM_BASE + FlashsafeSizeGet()));
439 
441  {
442  ui32ErrorReturn = FlashProgram(pui8DataBuffer, ui32Address, ui32Count);
444  return(ui32ErrorReturn);
445  }
446 
447  return(FLASHSAFE_ACCESS_DENIED);
448 }
449 
450 
451 //*****************************************************************************
452 //
454 //
455 //*****************************************************************************
456 uint32_t
458 {
459 
461  {
464  return(FLASHSAFE_ACCESS_OK);
465  }
466 
467  return(FLASHSAFE_ACCESS_DENIED);
468 }
uint32_t FlashsafeSectorSizeGet(void)
Get size of a flash sector in no of bytes.
Definition: flashsafe.c:104
#define FLASH_PWR_OFF_MODE
Definition: flash.h:125
uint32_t FlashsafeSectorErase(uint32_t ui32SectorAddress)
Erase a flash sector.
Definition: flashsafe.c:403
static uint32_t FlashSectorSizeGet(void)
Get size of a flash sector in number of bytes.
Definition: flash.h:259
uint32_t FlashsafeIntEnable(uint32_t ui32IntFlags)
Enables flash controller interrupt sources.
Definition: flashsafe.c:333
static bool FlashsafeSMPHTryAcquire(void)
Aquire the flash semaphore.
Definition: flashsafe.h:176
uint32_t FlashProtectionGet(uint32_t ui32SectorAddress)
Get sector protection.
Definition: flash.c:265
uint32_t FlashsafeCheckFsmForError(void)
Checks if the Flashsafe state machine has detected an error.
Definition: flashsafe.c:243
uint32_t FlashsafePowerModeGet(void)
Get current configured power mode.
Definition: flashsafe.c:144
void FlashPowerModeSet(uint32_t ui32PowerMode, uint32_t ui32BankGracePeriode, uint32_t ui32PumpGracePeriode)
Set power mode.
Definition: flash.c:101
uint32_t FlashsafeProgram(uint8_t *pui8DataBuffer, uint32_t ui32Address, uint32_t ui32Count)
Programs unprotected main bank flash sectors.
Definition: flashsafe.c:431
uint32_t FlashsafeIntClear(uint32_t ui32IntFlags)
Clears flash controller interrupt source.
Definition: flashsafe.c:385
#define FLASHSAFE_ACCESS_DENIED
Definition: flashsafe.h:116
void FlashDisableSectorsForWrite(void)
Disables all sectors for erase and programming on the active bank.
Definition: flash.c:518
static void FlashIntClear(uint32_t ui32IntFlags)
Clears flash controller interrupt source.
Definition: flash.h:651
uint32_t FlashsafeIntDisable(uint32_t ui32IntFlags)
Disables individual flash controller interrupt sources.
Definition: flashsafe.c:354
uint32_t FlashsafeCheckFsmForReady(void)
Checks if the Flash state machine is ready.
Definition: flashsafe.c:261
static uint32_t FlashIntStatus(void)
Gets the current interrupt status.
Definition: flash.h:594
#define FSM_REG_WRT_DISABLE
Definition: flash.h:184
uint32_t FlashPowerModeGet(void)
Get current configured power mode.
Definition: flash.c:192
uint32_t FlashsafeProtectionSet(uint32_t ui32SectorAddress, uint32_t ui32ProtectMode)
Set sector protection.
Definition: flashsafe.c:164
#define FAPI_STATUS_SUCCESS
Definition: flash.h:103
uint32_t FlashsafePowerModeSet(uint32_t ui32PowerMode, uint32_t ui32BankGracePeriode, uint32_t ui32PumpGracePeriode)
Set power mode.
Definition: flashsafe.c:115
uint32_t FlashSectorErase(uint32_t ui32SectorAddress)
Erase a flash sector.
Definition: flash.c:362
static uint32_t FlashSizeGet(void)
Get the size of the flash.
Definition: flash.h:283
#define FSM_REG_WRT_ENABLE
Definition: flash.h:183
#define ASSERT(expr)
Definition: debug.h:74
#define FAPI_STATUS_FSM_ERROR
Definition: flash.h:108
#define FAPI_STATUS_FSM_READY
Definition: flash.h:105
uint32_t FlashsafeDisableSectorsForWrite(void)
Disables all sectors for erase and programming on the active bank.
Definition: flashsafe.c:457
uint32_t FlashsafeProtectionGet(uint32_t ui32SectorAddress)
Get sector protection.
Definition: flashsafe.c:189
uint32_t FlashsafeProtectionSave(uint32_t ui32SectorAddress)
Save sector protection to make it permanent.
Definition: flashsafe.c:216
uint32_t FlashsafeIntStatus(void)
Gets the current interrupt status.
Definition: flashsafe.c:374
void IntUnregister(uint32_t ui32Interrupt)
Unregisters the function to be called when an interrupt occurs.
Definition: interrupt.c:200
#define FLASHSAFE_ACCESS_OK
Definition: flashsafe.h:117
uint32_t FlashProtectionSave(uint32_t ui32SectorAddress)
Save sector protection to make it permanent.
Definition: flash.c:308
#define FLASH_PWR_ACTIVE_MODE
Definition: flash.h:124
void FlashProtectionSet(uint32_t ui32SectorAddress, uint32_t ui32ProtectMode)
Set sector protection.
Definition: flash.c:225
#define FLASH_PWR_DEEP_STDBY_MODE
Definition: flash.h:126
uint32_t FlashsafeSizeGet(void)
Get the size of the flash.
Definition: flashsafe.c:93
#define FAPI_STATUS_FSM_BUSY
Definition: flash.h:104
static void FlashsafeSMPHRelease(void)
Release the flash semaphore.
Definition: flashsafe.h:189
uint32_t FlashsafeIntUnregister(void)
Unregisters the interrupt handler for the flash interrupt.
Definition: flashsafe.c:306
uint32_t FlashProgram(uint8_t *pui8DataBuffer, uint32_t ui32Address, uint32_t ui32Count)
Programs unprotected main bank flash sectors.
Definition: flash.c:399
uint32_t FlashsafeIntRegister(void(*pfnHandler)(void))
Registers an interrupt handler for the flash interrupt.
Definition: flashsafe.c:279
void IntDisable(uint32_t ui32Interrupt)
Disables an interrupt.
Definition: interrupt.c:378
void IntRegister(uint32_t ui32Interrupt, void(*pfnHandler)(void))
Registers a function to be called when an interrupt occurs.
Definition: interrupt.c:152
void IntEnable(uint32_t ui32Interrupt)
Enables an interrupt.
Definition: interrupt.c:318