CC26xx Driver Library
flash.h
Go to the documentation of this file.
1 /******************************************************************************
2 * Filename: flash.h
3 * Revised: 2016-07-07 19:12:02 +0200 (Thu, 07 Jul 2016)
4 * Revision: 46848
5 *
6 * Description: Defines and prototypes for the Flash driver.
7 *
8 * Copyright (c) 2015 - 2016, 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 //
246 //
247 //*****************************************************************************
248 __STATIC_INLINE uint32_t
250 {
251  uint32_t ui32SectorSizeInKbyte;
252 
253  ui32SectorSizeInKbyte = (HWREG(FLASH_BASE + FLASH_O_FCFG_B0_SSIZE0) &
256 
257  //
258  // Return flash sector size in number of bytes.
259  //
260  return(ui32SectorSizeInKbyte * 1024);
261 }
262 
263 //*****************************************************************************
264 //
270 //
271 //*****************************************************************************
272 __STATIC_INLINE uint32_t
274 {
275  uint32_t ui32NoOfSectors;
276 
277  //
278  // Get number of flash sectors
279  //
280  ui32NoOfSectors = (HWREG(FLASH_BASE + FLASH_O_FLASH_SIZE) &
283 
284  //
285  // Return flash size in number of bytes
286  //
287  return(ui32NoOfSectors * FlashSectorSizeGet());
288 }
289 
290 //*****************************************************************************
291 //
329 //
330 //*****************************************************************************
331 extern void FlashPowerModeSet(uint32_t ui32PowerMode,
332  uint32_t ui32BankGracePeriode,
333  uint32_t ui32PumpGracePeriode);
334 
335 //*****************************************************************************
336 //
346 //
347 //*****************************************************************************
348 extern uint32_t FlashPowerModeGet(void);
349 
350 //*****************************************************************************
351 //
367 //
368 //*****************************************************************************
369 extern void FlashProtectionSet(uint32_t ui32SectorAddress,
370  uint32_t ui32ProtectMode);
371 
372 //*****************************************************************************
373 //
383 //
384 //*****************************************************************************
385 extern uint32_t FlashProtectionGet(uint32_t ui32SectorAddress);
386 
387 //*****************************************************************************
388 //
404 //
405 //*****************************************************************************
406 extern uint32_t FlashProtectionSave(uint32_t ui32SectorAddress);
407 
408 //*****************************************************************************
409 //
423 //
424 //*****************************************************************************
425 __STATIC_INLINE uint32_t
427 {
429  {
430  return(FAPI_STATUS_FSM_ERROR);
431  }
432  else
433  {
434  return(FAPI_STATUS_SUCCESS);
435  }
436 }
437 
438 //*****************************************************************************
439 //
453 //
454 //*****************************************************************************
455 __STATIC_INLINE uint32_t
457 {
459  {
460  return(FAPI_STATUS_FSM_BUSY);
461  }
462  else
463  {
464  return(FAPI_STATUS_FSM_READY);
465  }
466 }
467 
468 //*****************************************************************************
469 //
484 //
485 //*****************************************************************************
486 __STATIC_INLINE void
487 FlashIntRegister(void (*pfnHandler)(void))
488 {
489  //
490  // Register the interrupt handler.
491  //
492  IntRegister(INT_FLASH, pfnHandler);
493 
494  //
495  // Enable the flash interrupt.
496  //
497  IntEnable(INT_FLASH);
498 }
499 
500 //*****************************************************************************
501 //
513 //
514 //*****************************************************************************
515 __STATIC_INLINE void
517 {
518  //
519  // Disable the interrupts.
520  //
521  IntDisable(INT_FLASH);
522 
523  //
524  // Unregister the interrupt handler.
525  //
526  IntUnregister(INT_FLASH);
527 }
528 
529 //*****************************************************************************
530 //
541 //
542 //*****************************************************************************
543 __STATIC_INLINE void
544 FlashIntEnable(uint32_t ui32IntFlags)
545 {
547  HWREG(FLASH_BASE + FLASH_O_FSM_ST_MACHINE) |= ui32IntFlags;
549 }
550 
551 //*****************************************************************************
552 //
563 //
564 //*****************************************************************************
565 __STATIC_INLINE void
566 FlashIntDisable(uint32_t ui32IntFlags)
567 {
569  HWREG(FLASH_BASE + FLASH_O_FSM_ST_MACHINE) &= ~ui32IntFlags;
571 }
572 
573 //*****************************************************************************
574 //
581 //
582 //*****************************************************************************
583 __STATIC_INLINE uint32_t
585 {
586  uint32_t ui32IntFlags;
587 
588  ui32IntFlags = 0;
589 
590  //
591  // Check if FSM_DONE interrupt status is set.
592  //
594  {
595  ui32IntFlags = FLASH_INT_FSM_DONE;
596  }
597 
598  //
599  // Check if RVF_INT interrupt status is set.
600  //
602  {
603  ui32IntFlags |= FLASH_INT_RV;
604  }
605 
606  return(ui32IntFlags);
607 }
608 
609 //*****************************************************************************
610 //
638 //
639 //*****************************************************************************
640 __STATIC_INLINE void
641 FlashIntClear(uint32_t ui32IntFlags)
642 {
643  uint32_t ui32TempVal;
644 
645  ui32TempVal = 0;
646 
647  if(ui32IntFlags & FLASH_INT_FSM_DONE)
648  {
649  ui32TempVal = FLASH_FEDACSTAT_FSM_DONE;
650  }
651 
652  if(ui32IntFlags & FLASH_INT_RV)
653  {
654  ui32TempVal |= FLASH_FEDACSTAT_RVF_INT;
655  }
656 
657  //
658  // Clear the flash interrupt source.
659  //
660  HWREG(FLASH_BASE + FLASH_O_FEDACSTAT) = ui32TempVal;
661 }
662 
663 //*****************************************************************************
664 //
686 //
687 //*****************************************************************************
688 extern uint32_t FlashSectorErase(uint32_t ui32SectorAddress);
689 
690 
691 //*****************************************************************************
692 //
720 //
721 //*****************************************************************************
722 extern uint32_t FlashProgram(uint8_t *pui8DataBuffer,
723  uint32_t ui32Address, uint32_t ui32Count);
724 
725 //*****************************************************************************
726 //
739 //
740 //*****************************************************************************
741 extern bool FlashEfuseReadRow(uint32_t *pui32EfuseData,
742  uint32_t ui32RowAddress);
743 
744 //*****************************************************************************
745 //
757 //
758 //*****************************************************************************
759 extern void FlashDisableSectorsForWrite(void);
760 
761 
762 //*****************************************************************************
763 //
764 // Support for DriverLib in ROM:
765 // Redirect to implementation in ROM when available.
766 //
767 //*****************************************************************************
768 #if !defined(DRIVERLIB_NOROM) && !defined(DOXYGEN)
769  #include <driverlib/rom.h>
770  #ifdef ROM_FlashPowerModeSet
771  #undef FlashPowerModeSet
772  #define FlashPowerModeSet ROM_FlashPowerModeSet
773  #endif
774  #ifdef ROM_FlashPowerModeGet
775  #undef FlashPowerModeGet
776  #define FlashPowerModeGet ROM_FlashPowerModeGet
777  #endif
778  #ifdef ROM_FlashProtectionSet
779  #undef FlashProtectionSet
780  #define FlashProtectionSet ROM_FlashProtectionSet
781  #endif
782  #ifdef ROM_FlashProtectionGet
783  #undef FlashProtectionGet
784  #define FlashProtectionGet ROM_FlashProtectionGet
785  #endif
786  #ifdef ROM_FlashProtectionSave
787  #undef FlashProtectionSave
788  #define FlashProtectionSave ROM_FlashProtectionSave
789  #endif
790  #ifdef ROM_FlashSectorErase
791  #undef FlashSectorErase
792  #define FlashSectorErase ROM_FlashSectorErase
793  #endif
794  #ifdef ROM_FlashProgram
795  #undef FlashProgram
796  #define FlashProgram ROM_FlashProgram
797  #endif
798  #ifdef ROM_FlashEfuseReadRow
799  #undef FlashEfuseReadRow
800  #define FlashEfuseReadRow ROM_FlashEfuseReadRow
801  #endif
802  #ifdef ROM_FlashDisableSectorsForWrite
803  #undef FlashDisableSectorsForWrite
804  #define FlashDisableSectorsForWrite ROM_FlashDisableSectorsForWrite
805  #endif
806 #endif
807 
808 //*****************************************************************************
809 //
810 // Mark the end of the C bindings section for C++ compilers.
811 //
812 //*****************************************************************************
813 #ifdef __cplusplus
814 }
815 #endif
816 
817 #endif // __FLASH_H__
818 
819 //*****************************************************************************
820 //
824 //
825 //*****************************************************************************
Program data.
Definition: flash.h:166
static uint32_t FlashSectorSizeGet(void)
Get size of a flash sector in number of bytes.
Definition: flash.h:249
Erase resume.
Definition: flash.h:172
uint32_t FlashProtectionGet(uint32_t ui32SectorAddress)
Get sector protection.
Definition: flash.c:278
void FlashPowerModeSet(uint32_t ui32PowerMode, uint32_t ui32BankGracePeriode, uint32_t ui32PumpGracePeriode)
Set power mode.
Definition: flash.c:114
Erase sector.
Definition: flash.h:167
void FlashDisableSectorsForWrite(void)
Disables all sectors for erase and programming on the active bank.
Definition: flash.c:531
static void FlashIntClear(uint32_t ui32IntFlags)
Clears flash controller interrupt source.
Definition: flash.h:641
static uint32_t FlashCheckFsmForReady(void)
Checks if the Flash state machine is ready.
Definition: flash.h:456
static uint32_t FlashIntStatus(void)
Gets the current interrupt status.
Definition: flash.h:584
static void FlashIntUnregister(void)
Unregisters the interrupt handler for the flash interrupt.
Definition: flash.h:516
#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:205
#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:375
Erase OTP.
Definition: flash.h:175
static uint32_t FlashSizeGet(void)
Get the size of the flash.
Definition: flash.h:273
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:446
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:321
#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:544
void FlashProtectionSet(uint32_t ui32SectorAddress, uint32_t ui32ProtectMode)
Set sector protection.
Definition: flash.c:238
static uint32_t FlashCheckFsmForError(void)
Checks if the Flash state machine has detected an error.
Definition: flash.h:426
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:566
uint32_t FlashProgram(uint8_t *pui8DataBuffer, uint32_t ui32Address, uint32_t ui32Count)
Programs unprotected main bank flash sectors.
Definition: flash.c:412
static void FlashIntRegister(void(*pfnHandler)(void))
Registers an interrupt handler for the flash interrupt.
Definition: flash.h:487
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