CC13xx Driver Library
flash.h
Go to the documentation of this file.
1 /******************************************************************************
2 * Filename: flash.h
3 * Revised: 2015-09-21 15:19:36 +0200 (Mon, 21 Sep 2015)
4 * Revision: 44629
5 *
6 * Description: Defines and prototypes for the Flash driver.
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 //*****************************************************************************
40 //
45 //
46 //*****************************************************************************
47 
48 #ifndef __FLASH_H__
49 #define __FLASH_H__
50 
51 //*****************************************************************************
52 //
53 // If building with a C++ compiler, make all of the definitions in this header
54 // have a C binding.
55 //
56 //*****************************************************************************
57 #ifdef __cplusplus
58 extern "C"
59 {
60 #endif
61 
62 #include <stdbool.h>
63 #include <stdint.h>
64 #include <inc/hw_types.h>
65 #include <inc/hw_flash.h>
66 #include <inc/hw_memmap.h>
67 #include <inc/hw_ints.h>
68 #include <inc/hw_aon_sysctl.h>
69 #include <inc/hw_fcfg1.h>
70 #include <driverlib/interrupt.h>
71 #include <driverlib/debug.h>
72 
73 //*****************************************************************************
74 //
75 // Support for DriverLib in ROM:
76 // This section renames all functions that are not "static inline", so that
77 // calling these functions will default to implementation in flash. At the end
78 // of this file a second renaming will change the defaults to implementation in
79 // ROM for available functions.
80 //
81 // To force use of the implementation in flash, e.g. for debugging:
82 // - Globally: Define DRIVERLIB_NOROM at project level
83 // - Per function: Use prefix "NOROM_" when calling the function
84 //
85 //*****************************************************************************
86 #if !defined(DOXYGEN)
87  #define FlashPowerModeSet NOROM_FlashPowerModeSet
88  #define FlashPowerModeGet NOROM_FlashPowerModeGet
89  #define FlashProtectionSet NOROM_FlashProtectionSet
90  #define FlashProtectionGet NOROM_FlashProtectionGet
91  #define FlashProtectionSave NOROM_FlashProtectionSave
92  #define FlashSectorErase NOROM_FlashSectorErase
93  #define FlashProgram NOROM_FlashProgram
94  #define FlashEfuseReadRow NOROM_FlashEfuseReadRow
95  #define FlashDisableSectorsForWrite NOROM_FlashDisableSectorsForWrite
96 #endif
97 
98 //*****************************************************************************
99 //
100 // Values that can be returned from the API functions
101 //
102 //*****************************************************************************
103 #define FAPI_STATUS_SUCCESS 0x00000000 // Function completed successfully
104 #define FAPI_STATUS_FSM_BUSY 0x00000001 // FSM is Busy
105 #define FAPI_STATUS_FSM_READY 0x00000002 // FSM is Ready
106 #define FAPI_STATUS_INCORRECT_DATABUFFER_LENGTH \
107  0x00000003 // Incorrect parameter value
108 #define FAPI_STATUS_FSM_ERROR 0x00000004 // Operation failed
109 
110 //*****************************************************************************
111 //
112 // Values passed to FlashIntEnable(), FlashIntDisable() and FlashIntClear() and
113 // returned from FlashIntStatus().
114 //
115 //*****************************************************************************
116 #define FLASH_INT_FSM_DONE 0x00400000 // FSM Done Interrupt Mask
117 #define FLASH_INT_RV 0x00010000 // Read Verify error Interrupt Mask
118 
119 //*****************************************************************************
120 //
121 // Values passed to FlashSetPowerMode() and returned from FlashGetPowerMode().
122 //
123 //*****************************************************************************
124 #define FLASH_PWR_ACTIVE_MODE 0x00000000
125 #define FLASH_PWR_OFF_MODE 0x00000001
126 #define FLASH_PWR_DEEP_STDBY_MODE \
127  0x00000002
128 
129 //*****************************************************************************
130 //
131 // Values passed to FlashSetProtection() and returned from FlashGetProtection().
132 //
133 //*****************************************************************************
134 #define FLASH_NO_PROTECT 0x00000000 // Sector not protected
135 #define FLASH_WRITE_PROTECT 0x00000001 // Sector erase and program
136  // protected
137 
138 //*****************************************************************************
139 //
140 // Define used by the flash programming and erase functions
141 //
142 //*****************************************************************************
143 #define ADDR_OFFSET (0x1F800000 - FLASHMEM_BASE)
144 
145 //*****************************************************************************
146 //
147 // Define used for access to factory configuration area.
148 //
149 //*****************************************************************************
150 #define FCFG1_OFFSET 0x1000
151 
152 //*****************************************************************************
153 //
154 // Define for the clock frequencey input to the flash module in number of MHz
155 //
156 //*****************************************************************************
157 #define FLASH_MODULE_CLK_FREQ 48
158 
159 //*****************************************************************************
160 //
162 //
163 //*****************************************************************************
164 typedef enum
165 {
166  FAPI_PROGRAM_DATA = 0x0002,
167  FAPI_ERASE_SECTOR = 0x0006,
168  FAPI_ERASE_BANK = 0x0008,
170  FAPI_CLEAR_STATUS = 0x0010,
172  FAPI_ERASE_RESUME = 0x0016,
173  FAPI_CLEAR_MORE = 0x0018,
175  FAPI_ERASE_OTP = 0x0030
177 
178 //*****************************************************************************
179 //
180 // Defines for values written to the FLASH_O_FSM_WR_ENA register
181 //
182 //*****************************************************************************
183 #define FSM_REG_WRT_ENABLE 5
184 #define FSM_REG_WRT_DISABLE 2
185 
186 //*****************************************************************************
187 //
188 // Defines for the bank power mode field the FLASH_O_FBFALLBACK register
189 //
190 //*****************************************************************************
191 #define FBFALLBACK_SLEEP 0
192 #define FBFALLBACK_DEEP_STDBY 1
193 #define FBFALLBACK_ACTIVE 3
194 
195 //*****************************************************************************
196 //
197 // Defines for the bank grace periode and pump grace periode
198 //
199 //*****************************************************************************
200 #define FLASH_BAGP 0x14
201 #define FLASH_PAGP 0x14
202 
203 //*****************************************************************************
204 //
205 // Defines used by the FlashProgramPattern() function
206 //
207 //*****************************************************************************
208 #define PATTERN_BITS 0x20 // No of bits in data pattern to program
209 
210 //*****************************************************************************
211 //
212 // Defines for the FW flag bits in the FLASH_O_FWFLAG register
213 //
214 //*****************************************************************************
215 #define FW_WRT_TRIMMED 0x00000001
216 
217 //*****************************************************************************
218 //
219 // Defines used by the flash programming functions
220 //
221 //*****************************************************************************
222 typedef volatile uint8_t tFwpWriteByte;
223 #define FWPWRITE_BYTE_ADDRESS ((tFwpWriteByte *)((FLASH_BASE + FLASH_O_FWPWRITE0)))
224 
225 //*****************************************************************************
226 //
227 // Define for efuse instruction
228 //
229 //*****************************************************************************
230 #define DUMPWORD_INSTR 0x04
231 
232 //*****************************************************************************
233 //
234 // Define for FSM command execution
235 //
236 //*****************************************************************************
237 #define FLASH_CMD_EXEC 0x15
238 
239 //*****************************************************************************
240 //
241 // Defines for accesses to the customer configuration area in flash top sector
242 //
243 //*****************************************************************************
244 #define CCFG_OFFSET_SECURITY 0x00000FD8
245 #define CCFG_OFFSET_SECT_PROT 0x00000FF0
246 #define CCFG_SIZE_SECURITY 0x00000014
247 #define CCFG_SIZE_SECT_PROT 0x00000004
248 
249 //*****************************************************************************
250 //
256 //
257 //*****************************************************************************
258 __STATIC_INLINE uint32_t
260 {
261  uint32_t ui32SectorSizeInKbyte;
262 
263  ui32SectorSizeInKbyte = (HWREG(FLASH_BASE + FLASH_O_FCFG_B0_SSIZE0) &
266 
267  //
268  // Return flash sector size in number of bytes.
269  //
270  return(ui32SectorSizeInKbyte * 1024);
271 }
272 
273 //*****************************************************************************
274 //
280 //
281 //*****************************************************************************
282 __STATIC_INLINE uint32_t
284 {
285  uint32_t ui32NoOfSectors;
286 
287  //
288  // Get number of flash sectors
289  //
290  ui32NoOfSectors = (HWREG(FLASH_BASE + FLASH_O_FLASH_SIZE) &
293 
294  //
295  // Return flash size in number of bytes
296  //
297  return(ui32NoOfSectors * FlashSectorSizeGet());
298 }
299 
300 //*****************************************************************************
301 //
339 //
340 //*****************************************************************************
341 extern void FlashPowerModeSet(uint32_t ui32PowerMode,
342  uint32_t ui32BankGracePeriode,
343  uint32_t ui32PumpGracePeriode);
344 
345 //*****************************************************************************
346 //
356 //
357 //*****************************************************************************
358 extern uint32_t FlashPowerModeGet(void);
359 
360 //*****************************************************************************
361 //
377 //
378 //*****************************************************************************
379 extern void FlashProtectionSet(uint32_t ui32SectorAddress,
380  uint32_t ui32ProtectMode);
381 
382 //*****************************************************************************
383 //
393 //
394 //*****************************************************************************
395 extern uint32_t FlashProtectionGet(uint32_t ui32SectorAddress);
396 
397 //*****************************************************************************
398 //
414 //
415 //*****************************************************************************
416 extern uint32_t FlashProtectionSave(uint32_t ui32SectorAddress);
417 
418 //*****************************************************************************
419 //
433 //
434 //*****************************************************************************
435 __STATIC_INLINE uint32_t
437 {
439  {
440  return(FAPI_STATUS_FSM_ERROR);
441  }
442  else
443  {
444  return(FAPI_STATUS_SUCCESS);
445  }
446 }
447 
448 //*****************************************************************************
449 //
463 //
464 //*****************************************************************************
465 __STATIC_INLINE uint32_t
467 {
469  {
470  return(FAPI_STATUS_FSM_BUSY);
471  }
472  else
473  {
474  return(FAPI_STATUS_FSM_READY);
475  }
476 }
477 
478 //*****************************************************************************
479 //
494 //
495 //*****************************************************************************
496 __STATIC_INLINE void
497 FlashIntRegister(void (*pfnHandler)(void))
498 {
499  //
500  // Register the interrupt handler.
501  //
502  IntRegister(INT_FLASH, pfnHandler);
503 
504  //
505  // Enable the flash interrupt.
506  //
507  IntEnable(INT_FLASH);
508 }
509 
510 //*****************************************************************************
511 //
523 //
524 //*****************************************************************************
525 __STATIC_INLINE void
527 {
528  //
529  // Disable the interrupts.
530  //
531  IntDisable(INT_FLASH);
532 
533  //
534  // Unregister the interrupt handler.
535  //
536  IntUnregister(INT_FLASH);
537 }
538 
539 //*****************************************************************************
540 //
551 //
552 //*****************************************************************************
553 __STATIC_INLINE void
554 FlashIntEnable(uint32_t ui32IntFlags)
555 {
557  HWREG(FLASH_BASE + FLASH_O_FSM_ST_MACHINE) |= ui32IntFlags;
559 }
560 
561 //*****************************************************************************
562 //
573 //
574 //*****************************************************************************
575 __STATIC_INLINE void
576 FlashIntDisable(uint32_t ui32IntFlags)
577 {
579  HWREG(FLASH_BASE + FLASH_O_FSM_ST_MACHINE) &= ~ui32IntFlags;
581 }
582 
583 //*****************************************************************************
584 //
591 //
592 //*****************************************************************************
593 __STATIC_INLINE uint32_t
595 {
596  uint32_t ui32IntFlags;
597 
598  ui32IntFlags = 0;
599 
600  //
601  // Check if FSM_DONE interrupt status is set.
602  //
604  {
605  ui32IntFlags = FLASH_INT_FSM_DONE;
606  }
607 
608  //
609  // Check if RVF_INT interrupt status is set.
610  //
612  {
613  ui32IntFlags |= FLASH_INT_RV;
614  }
615 
616  return(ui32IntFlags);
617 }
618 
619 //*****************************************************************************
620 //
648 //
649 //*****************************************************************************
650 __STATIC_INLINE void
651 FlashIntClear(uint32_t ui32IntFlags)
652 {
653  uint32_t ui32TempVal;
654 
655  ui32TempVal = 0;
656 
657  if(ui32IntFlags & FLASH_INT_FSM_DONE)
658  {
659  ui32TempVal = FLASH_FEDACSTAT_FSM_DONE;
660  }
661 
662  if(ui32IntFlags & FLASH_INT_RV)
663  {
664  ui32TempVal |= FLASH_FEDACSTAT_RVF_INT;
665  }
666 
667  //
668  // Clear the flash interrupt source.
669  //
670  HWREG(FLASH_BASE + FLASH_O_FEDACSTAT) = ui32TempVal;
671 }
672 
673 //*****************************************************************************
674 //
696 //
697 //*****************************************************************************
698 extern uint32_t FlashSectorErase(uint32_t ui32SectorAddress);
699 
700 
701 //*****************************************************************************
702 //
730 //
731 //*****************************************************************************
732 extern uint32_t FlashProgram(uint8_t *pui8DataBuffer,
733  uint32_t ui32Address, uint32_t ui32Count);
734 
735 //*****************************************************************************
736 //
749 //
750 //*****************************************************************************
751 extern bool FlashEfuseReadRow(uint32_t *pui32EfuseData,
752  uint32_t ui32RowAddress);
753 
754 //*****************************************************************************
755 //
767 //
768 //*****************************************************************************
769 extern void FlashDisableSectorsForWrite(void);
770 
771 
772 //*****************************************************************************
773 //
774 // Support for DriverLib in ROM:
775 // Redirect to implementation in ROM when available.
776 //
777 //*****************************************************************************
778 #if !defined(DRIVERLIB_NOROM) && !defined(DOXYGEN)
779  #include <driverlib/rom.h>
780  #ifdef ROM_FlashPowerModeSet
781  #undef FlashPowerModeSet
782  #define FlashPowerModeSet ROM_FlashPowerModeSet
783  #endif
784  #ifdef ROM_FlashPowerModeGet
785  #undef FlashPowerModeGet
786  #define FlashPowerModeGet ROM_FlashPowerModeGet
787  #endif
788  #ifdef ROM_FlashProtectionSet
789  #undef FlashProtectionSet
790  #define FlashProtectionSet ROM_FlashProtectionSet
791  #endif
792  #ifdef ROM_FlashProtectionGet
793  #undef FlashProtectionGet
794  #define FlashProtectionGet ROM_FlashProtectionGet
795  #endif
796  #ifdef ROM_FlashProtectionSave
797  #undef FlashProtectionSave
798  #define FlashProtectionSave ROM_FlashProtectionSave
799  #endif
800  #ifdef ROM_FlashSectorErase
801  #undef FlashSectorErase
802  #define FlashSectorErase ROM_FlashSectorErase
803  #endif
804  #ifdef ROM_FlashProgram
805  #undef FlashProgram
806  #define FlashProgram ROM_FlashProgram
807  #endif
808  #ifdef ROM_FlashEfuseReadRow
809  #undef FlashEfuseReadRow
810  #define FlashEfuseReadRow ROM_FlashEfuseReadRow
811  #endif
812  #ifdef ROM_FlashDisableSectorsForWrite
813  #undef FlashDisableSectorsForWrite
814  #define FlashDisableSectorsForWrite ROM_FlashDisableSectorsForWrite
815  #endif
816 #endif
817 
818 //*****************************************************************************
819 //
820 // Mark the end of the C bindings section for C++ compilers.
821 //
822 //*****************************************************************************
823 #ifdef __cplusplus
824 }
825 #endif
826 
827 #endif // __FLASH_H__
828 
829 //*****************************************************************************
830 //
834 //
835 //*****************************************************************************
Program data.
Definition: flash.h:166
static uint32_t FlashSectorSizeGet(void)
Get size of a flash sector in number of bytes.
Definition: flash.h:259
Erase resume.
Definition: flash.h:172
uint32_t FlashProtectionGet(uint32_t ui32SectorAddress)
Get sector protection.
Definition: flash.c:265
void FlashPowerModeSet(uint32_t ui32PowerMode, uint32_t ui32BankGracePeriode, uint32_t ui32PumpGracePeriode)
Set power mode.
Definition: flash.c:101
Erase sector.
Definition: flash.h:167
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
static uint32_t FlashCheckFsmForReady(void)
Checks if the Flash state machine is ready.
Definition: flash.h:466
static uint32_t FlashIntStatus(void)
Gets the current interrupt status.
Definition: flash.h:594
static void FlashIntUnregister(void)
Unregisters the interrupt handler for the flash interrupt.
Definition: flash.h:526
#define FLASH_INT_FSM_DONE
Definition: flash.h:116
#define FSM_REG_WRT_DISABLE
Definition: flash.h:184
uint32_t FlashPowerModeGet(void)
Get current configured power mode.
Definition: flash.c:192
#define FAPI_STATUS_SUCCESS
Definition: flash.h:103
Program sector.
Definition: flash.h:174
uint32_t FlashSectorErase(uint32_t ui32SectorAddress)
Erase a flash sector.
Definition: flash.c:362
Erase OTP.
Definition: flash.h:175
static uint32_t FlashSizeGet(void)
Get the size of the flash.
Definition: flash.h:283
Clear status.
Definition: flash.h:170
#define FSM_REG_WRT_ENABLE
Definition: flash.h:183
#define FAPI_STATUS_FSM_ERROR
Definition: flash.h:108
#define FAPI_STATUS_FSM_READY
Definition: flash.h:105
bool FlashEfuseReadRow(uint32_t *pui32EfuseData, uint32_t ui32RowAddress)
Reads efuse data from specified row.
Definition: flash.c:433
Erase bank.
Definition: flash.h:168
tFlashStateCommandsType
Defined values for Flash State Machine commands.
Definition: flash.h:164
Validate sector.
Definition: flash.h:169
void IntUnregister(uint32_t ui32Interrupt)
Unregisters the function to be called when an interrupt occurs.
Definition: interrupt.c:200
uint32_t FlashProtectionSave(uint32_t ui32SectorAddress)
Save sector protection to make it permanent.
Definition: flash.c:308
#define FLASH_INT_RV
Definition: flash.h:117
Clear more.
Definition: flash.h:173
Program resume.
Definition: flash.h:171
static void FlashIntEnable(uint32_t ui32IntFlags)
Enables flash controller interrupt sources.
Definition: flash.h:554
void FlashProtectionSet(uint32_t ui32SectorAddress, uint32_t ui32ProtectMode)
Set sector protection.
Definition: flash.c:225
static uint32_t FlashCheckFsmForError(void)
Checks if the Flash state machine has detected an error.
Definition: flash.h:436
volatile uint8_t tFwpWriteByte
Definition: flash.h:222
#define FAPI_STATUS_FSM_BUSY
Definition: flash.h:104
static void FlashIntDisable(uint32_t ui32IntFlags)
Disables individual flash controller interrupt sources.
Definition: flash.h:576
uint32_t FlashProgram(uint8_t *pui8DataBuffer, uint32_t ui32Address, uint32_t ui32Count)
Programs unprotected main bank flash sectors.
Definition: flash.c:399
static void FlashIntRegister(void(*pfnHandler)(void))
Registers an interrupt handler for the flash interrupt.
Definition: flash.h:497
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