1 /******************************************************************************
2 *  Filename:       flash.h
3 *  Revised:        2020-02-14 11:30:20 +0100 (Fri, 14 Feb 2020)
4 *  Revision:       56760
5 *
6 *  Description:    Defines and prototypes for the Flash driver.
7 *
8 *  Copyright (c) 2015 - 2017, 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 //
41 //! \addtogroup system_control_group
42 //! @{
43 //! \addtogroup flash_api
44 //! @{
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_pmctl.h"
69 #include "../inc/hw_fcfg1.h"
70 #include "interrupt.h"
71 #include "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 frequency input to the flash module in number of MHz
155 //
156 //*****************************************************************************
157 #define FLASH_MODULE_CLK_FREQ  48
158 
159 //*****************************************************************************
160 //
161 //! \brief Defined values for Flash State Machine commands
162 //
163 //*****************************************************************************
164 typedef enum
165 {
166     FAPI_PROGRAM_DATA    = 0x0002, //!< Program data.
167     FAPI_ERASE_SECTOR    = 0x0006, //!< Erase sector.
168     FAPI_ERASE_BANK      = 0x0008, //!< Erase bank.
169     FAPI_VALIDATE_SECTOR = 0x000E, //!< Validate sector.
170     FAPI_CLEAR_STATUS    = 0x0010, //!< Clear status.
171     FAPI_PROGRAM_RESUME  = 0x0014, //!< Program resume.
172     FAPI_ERASE_RESUME    = 0x0016, //!< Erase resume.
173     FAPI_CLEAR_MORE      = 0x0018, //!< Clear more.
174     FAPI_PROGRAM_SECTOR  = 0x0020, //!< Program sector.
175     FAPI_ERASE_OTP       = 0x0030  //!< Erase OTP.
176 } tFlashStateCommandsType;
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 period and pump grace period
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 //! \brief Get size of a flash sector in number of bytes.
242 //!
243 //! This function will return the size of a flash sector in number of bytes.
244 //!
245 //! \return Returns size of a flash sector in number of bytes.
246 //
247 //*****************************************************************************
248 __STATIC_INLINE uint32_t
FlashSectorSizeGet(void)249 FlashSectorSizeGet(void)
250 {
251     uint32_t ui32SectorSizeInKbyte;
252 
253     ui32SectorSizeInKbyte = (HWREG(FLASH_BASE + FLASH_O_FCFG_B0_SSIZE0) &
254                              FLASH_FCFG_B0_SSIZE0_B0_SECT_SIZE_M) >>
255                             FLASH_FCFG_B0_SSIZE0_B0_SECT_SIZE_S;
256 
257     // Return flash sector size in number of bytes.
258     return(ui32SectorSizeInKbyte * 1024);
259 }
260 
261 //*****************************************************************************
262 //
263 //! \brief Get the size of the flash.
264 //!
265 //! This function returns the size of the flash main bank in number of bytes.
266 //!
267 //! \return Returns the flash size in number of bytes.
268 //
269 //*****************************************************************************
270 __STATIC_INLINE uint32_t
FlashSizeGet(void)271 FlashSizeGet(void)
272 {
273     uint32_t ui32NoOfSectors;
274 
275     // Get number of flash sectors
276     ui32NoOfSectors = (HWREG(FLASH_BASE + FLASH_O_FLASH_SIZE) &
277                        FLASH_FLASH_SIZE_SECTORS_M) >>
278                       FLASH_FLASH_SIZE_SECTORS_S;
279 
280     // Return flash size in number of bytes
281     return(ui32NoOfSectors * FlashSectorSizeGet());
282 }
283 
284 //*****************************************************************************
285 //
286 //! \brief Set power mode.
287 //!
288 //! This function will set the specified power mode.
289 //!
290 //! Any access to the bank causes a reload of the specified bank grace period
291 //! input value into the bank down counter. After the last access to the
292 //! flash bank, the down counter delays from 0 to 255 prescaled HCLK clock
293 //! cycles before putting the bank into one of the fallback power modes as
294 //! determined by \c ui32PowerMode. This value must be greater than 1 when the
295 //! fallback mode is not \ref FLASH_PWR_ACTIVE_MODE.
296 //!
297 //! Note: The prescaled clock used for the down counter is a clock divided by
298 //! 16 from input HCLK. The \c ui32BankGracePeriod parameter is ignored if
299 //! \c ui32PowerMode is equal to \ref FLASH_PWR_ACTIVE_MODE.
300 //! Any access to flash memory causes the pump grace period down counter to
301 //! reload with value of \c ui32PumpGracePeriod. After the bank has gone to sleep,
302 //! the down counter delays this number of prescaled HCLK clock cycles before
303 //! entering one of the charge pump fallback power modes as determined by
304 //! \c ui32PowerMode. The prescaled clock used for the pump grace period down
305 //! counter is a clock divided by 16 from input HCLK. This parameter is ignored
306 //! if \c ui32PowerMode is equal to \ref FLASH_PWR_ACTIVE_MODE.
307 //!
308 //! Changing the power mode of the flash module must be a part within a
309 //! device power mode transition requiring configuration of multiple modules.
310 //! Refer to documents describing the device power modes.
311 //!
312 //! \param ui32PowerMode is the wanted power mode.
313 //! The defined flash power modes are:
314 //! - \ref FLASH_PWR_ACTIVE_MODE
315 //! - \ref FLASH_PWR_OFF_MODE
316 //! - \ref FLASH_PWR_DEEP_STDBY_MODE
317 //! \param ui32BankGracePeriod is the starting count value for the bank grace
318 //! period down counter.
319 //! \param ui32PumpGracePeriod is the starting count value for the pump grace
320 //! period down counter.
321 //!
322 //! \return None
323 //
324 //*****************************************************************************
325 extern void FlashPowerModeSet(uint32_t ui32PowerMode,
326                               uint32_t ui32BankGracePeriod,
327                               uint32_t ui32PumpGracePeriod);
328 
329 //*****************************************************************************
330 //
331 //! \brief Get current configured power mode.
332 //!
333 //! This function will return the current configured power mode.
334 //!
335 //! \return Returns the current configured power mode.
336 //! The defined power modes are:
337 //! - \ref FLASH_PWR_ACTIVE_MODE
338 //! - \ref FLASH_PWR_OFF_MODE
339 //! - \ref FLASH_PWR_DEEP_STDBY_MODE
340 //
341 //*****************************************************************************
342 extern uint32_t FlashPowerModeGet(void);
343 
344 //*****************************************************************************
345 //
346 //! \brief Set sector protection.
347 //!
348 //! This function will set the specified protection on specified flash bank
349 //! sector. A sector can either have no protection or have write protection
350 //! which guards for both program and erase of that sector.
351 //! Sector protection can only be changed from \ref FLASH_NO_PROTECT to
352 //! \ref FLASH_WRITE_PROTECT! After write protecting a sector this sector can
353 //! only be set back to unprotected by a device reset.
354 //!
355 //! \param ui32SectorAddress is the start address of the sector to protect.
356 //! \param ui32ProtectMode is the enumerated sector protection mode.
357 //! - \ref FLASH_NO_PROTECT
358 //! - \ref FLASH_WRITE_PROTECT
359 //!
360 //! \return None
361 //
362 //*****************************************************************************
363 extern void FlashProtectionSet(uint32_t ui32SectorAddress,
364                                uint32_t ui32ProtectMode);
365 
366 //*****************************************************************************
367 //
368 //! \brief Get sector protection.
369 //!
370 //! This return the protection mode for the specified flash bank sector.
371 //!
372 //! \param ui32SectorAddress is the start address of the desired sector.
373 //!
374 //! \return Returns the sector protection:
375 //! - \ref FLASH_NO_PROTECT
376 //! - \ref FLASH_WRITE_PROTECT
377 //
378 //*****************************************************************************
379 extern uint32_t FlashProtectionGet(uint32_t ui32SectorAddress);
380 
381 //*****************************************************************************
382 //
383 //! \brief Save sector protection to make it permanent.
384 //!
385 //! This function will save the current protection mode for the specified
386 //! flash bank sector.
387 //!
388 //! This function must only be executed from ROM or SRAM.
389 //!
390 //! \note A write protected sector will become permanent write
391 //! protected!! A device reset will not change the write protection!
392 //!
393 //! \param ui32SectorAddress is the start address of the sector to be protected.
394 //!
395 //! \return Returns the status of the sector protection:
396 //! - \ref FAPI_STATUS_SUCCESS : Success.
397 //! - \ref FAPI_STATUS_FSM_ERROR : An erase error is encountered.
398 //
399 //*****************************************************************************
400 extern uint32_t FlashProtectionSave(uint32_t ui32SectorAddress);
401 
402 //*****************************************************************************
403 //
404 //! \brief Checks if the Flash state machine has detected an error.
405 //!
406 //! This function returns the status of the Flash State Machine indicating if
407 //! an error is detected or not. Primary use is to check if an Erase or
408 //! Program operation has failed.
409 //!
410 //! \note Please note that code can not execute in flash while any part of the flash
411 //! is being programmed or erased. This function must be called from ROM or
412 //! SRAM while any part of the flash is being programmed or erased.
413 //!
414 //! \return Returns status of Flash state machine:
415 //! - \ref FAPI_STATUS_FSM_ERROR
416 //! - \ref FAPI_STATUS_SUCCESS
417 //
418 //*****************************************************************************
419 __STATIC_INLINE uint32_t
FlashCheckFsmForError(void)420 FlashCheckFsmForError(void)
421 {
422     if(HWREG(FLASH_BASE + FLASH_O_FMSTAT) & FLASH_FMSTAT_CSTAT)
423     {
424         return(FAPI_STATUS_FSM_ERROR);
425     }
426     else
427     {
428         return(FAPI_STATUS_SUCCESS);
429     }
430 }
431 
432 //*****************************************************************************
433 //
434 //! \brief Checks if the Flash state machine is ready.
435 //!
436 //! This function returns the status of the Flash State Machine indicating if
437 //! it is ready to accept a new command or not. Primary use is to check if an
438 //! Erase or Program operation has finished.
439 //!
440 //! \note Please note that code can not execute in flash while any part of the flash
441 //! is being programmed or erased. This function must be called from ROM or
442 //! SRAMh while any part of the flash is being programmed or erased.
443 //!
444 //! \return Returns readiness status of Flash state machine:
445 //! - \ref FAPI_STATUS_FSM_READY
446 //! - \ref FAPI_STATUS_FSM_BUSY
447 //
448 //*****************************************************************************
449 __STATIC_INLINE uint32_t
FlashCheckFsmForReady(void)450 FlashCheckFsmForReady(void)
451 {
452     if(HWREG(FLASH_BASE + FLASH_O_STAT) & FLASH_STAT_BUSY)
453     {
454         return(FAPI_STATUS_FSM_BUSY);
455     }
456     else
457     {
458         return(FAPI_STATUS_FSM_READY);
459     }
460 }
461 
462 //*****************************************************************************
463 //
464 //! \brief Registers an interrupt handler for the flash interrupt in the dynamic interrupt table.
465 //!
466 //! \note Only use this function if you want to use the dynamic vector table (in SRAM)!
467 //!
468 //! This function registers a function as the interrupt handler for a specific
469 //! interrupt and enables the corresponding interrupt in the interrupt controller.
470 //!
471 //! Specific FLASH interrupts must be enabled via \ref FlashIntEnable(). It is the
472 //! interrupt handler's responsibility to clear the interrupt source.
473 //!
474 //! \param pfnHandler is a pointer to the function to be called when the flash
475 //! interrupt occurs.
476 //!
477 //! \return None
478 //!
479 //! \sa \ref IntRegister() for important information about registering interrupt
480 //! handlers.
481 //
482 //*****************************************************************************
483 __STATIC_INLINE void
FlashIntRegister(void (* pfnHandler)(void))484 FlashIntRegister(void (*pfnHandler)(void))
485 {
486     // Register the interrupt handler.
487     IntRegister(INT_FLASH, pfnHandler);
488 
489     // Enable the flash interrupt.
490     IntEnable(INT_FLASH);
491 }
492 
493 //*****************************************************************************
494 //
495 //! \brief Unregisters the interrupt handler for the flash interrupt in the dynamic interrupt table.
496 //!
497 //! This function does the actual unregistering of the interrupt handler. It
498 //! clears the handler to be called when a FLASH interrupt occurs. This
499 //! function also masks off the interrupt in the interrupt controller so that
500 //! the interrupt handler no longer is called.
501 //!
502 //! \return None
503 //!
504 //! \sa \ref IntRegister() for important information about registering interrupt
505 //! handlers.
506 //
507 //*****************************************************************************
508 __STATIC_INLINE void
FlashIntUnregister(void)509 FlashIntUnregister(void)
510 {
511     // Disable the interrupts.
512     IntDisable(INT_FLASH);
513 
514     // Unregister the interrupt handler.
515     IntUnregister(INT_FLASH);
516 }
517 
518 //*****************************************************************************
519 //
520 //! \brief Enables flash controller interrupt sources.
521 //!
522 //! This function enables the flash controller interrupt sources.
523 //!
524 //! \param ui32IntFlags is the bit mask of the interrupt sources to be enabled.
525 //! The parameter is the bitwise OR of any of the following:
526 //! - \ref FLASH_INT_FSM_DONE : FSM Done interrupt.
527 //! - \ref FLASH_INT_RV       : Read verify error interrupt.
528 //!
529 //! \return None
530 //
531 //*****************************************************************************
532 __STATIC_INLINE void
FlashIntEnable(uint32_t ui32IntFlags)533 FlashIntEnable(uint32_t ui32IntFlags)
534 {
535     HWREG(FLASH_BASE + FLASH_O_FSM_WR_ENA) = FSM_REG_WRT_ENABLE;
536     HWREG(FLASH_BASE + FLASH_O_FSM_ST_MACHINE) |= ui32IntFlags;
537     HWREG(FLASH_BASE + FLASH_O_FSM_WR_ENA) = FSM_REG_WRT_DISABLE;
538 }
539 
540 //*****************************************************************************
541 //
542 //! \brief Disables individual flash controller interrupt sources.
543 //!
544 //! This function disables the flash controller interrupt sources.
545 //!
546 //! \param ui32IntFlags is the bit mask of the interrupt sources to be disabled.
547 //! The parameter is the bitwise OR of any of the following:
548 //! - \ref FLASH_INT_FSM_DONE : FSM Done interrupt.
549 //! - \ref FLASH_INT_RV       : Read verify error interrupt.
550 //!
551 //! \return None
552 //
553 //*****************************************************************************
554 __STATIC_INLINE void
FlashIntDisable(uint32_t ui32IntFlags)555 FlashIntDisable(uint32_t ui32IntFlags)
556 {
557     HWREG(FLASH_BASE + FLASH_O_FSM_WR_ENA) = FSM_REG_WRT_ENABLE;
558     HWREG(FLASH_BASE + FLASH_O_FSM_ST_MACHINE) &= ~ui32IntFlags;
559     HWREG(FLASH_BASE + FLASH_O_FSM_WR_ENA) = FSM_REG_WRT_DISABLE;
560 }
561 
562 //*****************************************************************************
563 //
564 //! \brief Gets the current interrupt status.
565 //!
566 //! This function returns the interrupt status for the Flash.
567 //!
568 //! \return Returns the current interrupt status as values described in
569 //! \ref FlashIntEnable().
570 //
571 //*****************************************************************************
572 __STATIC_INLINE uint32_t
FlashIntStatus(void)573 FlashIntStatus(void)
574 {
575     uint32_t ui32IntFlags;
576 
577     ui32IntFlags = 0;
578 
579     // Check if FSM_DONE interrupt status is set.
580     if(HWREG(FLASH_BASE + FLASH_O_FEDACSTAT) & FLASH_FEDACSTAT_FSM_DONE)
581     {
582         ui32IntFlags = FLASH_INT_FSM_DONE;
583     }
584 
585     // Check if RVF_INT interrupt status is set.
586     if(HWREG(FLASH_BASE + FLASH_O_FEDACSTAT) & FLASH_FEDACSTAT_RVF_INT)
587     {
588         ui32IntFlags |= FLASH_INT_RV;
589     }
590 
591     return(ui32IntFlags);
592 }
593 
594 //*****************************************************************************
595 //
596 //! \brief Clears flash controller interrupt source.
597 //!
598 //! The flash controller interrupt source is cleared, so that it no longer
599 //! asserts. This must be done in the interrupt handler to keep it from being
600 //! called again immediately upon exit.
601 //!
602 //! \note Due to write buffers and synchronizers in the system it may take several
603 //! clock cycles from a register write clearing an event in a module and until the
604 //! event is actually cleared in the NVIC of the system CPU. It is recommended to
605 //! clear the event source early in the interrupt service routine (ISR) to allow
606 //! the event clear to propagate to the NVIC before returning from the ISR.
607 //! At the same time, an early event clear allows new events of the same type to be
608 //! pended instead of ignored if the event is cleared later in the ISR.
609 //! It is the responsibility of the programmer to make sure that enough time has passed
610 //! before returning from the ISR to avoid false re-triggering of the cleared event.
611 //! A simple, although not necessarily optimal, way of clearing an event before
612 //! returning from the ISR is:
613 //! -# Write to clear event (interrupt source). (buffered write)
614 //! -# Dummy read from the event source module. (making sure the write has propagated)
615 //! -# Wait two system CPU clock cycles (user code or two NOPs). (allowing cleared event to propagate through any synchronizers)
616 //!
617 //! \param ui32IntFlags is the bit mask of the interrupt sources to be cleared.
618 //! Can be any of:
619 //! - \ref FLASH_INT_FSM_DONE
620 //! - \ref FLASH_INT_RV
621 //!
622 //! \return None
623 //
624 //*****************************************************************************
625 __STATIC_INLINE void
FlashIntClear(uint32_t ui32IntFlags)626 FlashIntClear(uint32_t ui32IntFlags)
627 {
628     uint32_t ui32TempVal;
629 
630     ui32TempVal = 0;
631 
632     if(ui32IntFlags & FLASH_INT_FSM_DONE)
633     {
634         ui32TempVal = FLASH_FEDACSTAT_FSM_DONE;
635     }
636 
637     if(ui32IntFlags & FLASH_INT_RV)
638     {
639         ui32TempVal |= FLASH_FEDACSTAT_RVF_INT;
640     }
641 
642     // Clear the flash interrupt source.
643     HWREG(FLASH_BASE + FLASH_O_FEDACSTAT) = ui32TempVal;
644 }
645 
646 //*****************************************************************************
647 //
648 //! \brief Erase a flash sector.
649 //!
650 //! This function will erase the specified flash sector. The function will
651 //! not return until the flash sector has been erased or an error condition
652 //! occurred. If flash top sector is erased the function will program the
653 //! the device security data bytes with default values. The device security
654 //! data located in the customer configuration area of the flash top sector,
655 //! must have valid values at all times. These values affect the configuration
656 //! of the device during boot.
657 //!
658 //! \warning Please note that code can not execute in flash while any part of the flash
659 //! is being programmed or erased. The application must disable interrupts that have
660 //! interrupt routines in flash. This function calls a ROM function which handles the
661 //! actual program operation.
662 //!
663 //! \param ui32SectorAddress is the starting address in flash of the sector to be
664 //! erased.
665 //!
666 //! \return Returns the status of the sector erase:
667 //! - \ref FAPI_STATUS_SUCCESS                     : Success.
668 //! - \ref FAPI_STATUS_INCORRECT_DATABUFFER_LENGTH : Invalid argument.
669 //! - \ref FAPI_STATUS_FSM_ERROR                   : A programming error is encountered.
670 //
671 //*****************************************************************************
672 extern uint32_t FlashSectorErase(uint32_t ui32SectorAddress);
673 
674 
675 //*****************************************************************************
676 //
677 //! \brief Programs unprotected flash sectors in the main bank.
678 //!
679 //! This function programs a sequence of bytes into the on-chip flash.
680 //! Programming each location consists of the result of an AND operation
681 //! of the new data and the existing data; in other words bits that contain
682 //! 1 can remain 1 or be changed to 0, but bits that are 0 cannot be changed
683 //! to 1. Therefore, a byte can be programmed multiple times as long as these
684 //! rules are followed; if a program operation attempts to change a 0 bit to
685 //! a 1 bit, that bit will not have its value changed.
686 //!
687 //! This function does not return until the data has been programmed or a
688 //! programming error occurs.
689 //!
690 //! \note It is recommended to disable cache and line buffer before programming the
691 //! flash. Cache and line buffer are not automatically updated if a flash program
692 //! causes a mismatch between new flash content and old content in cache and
693 //! line buffer. Remember to enable cache and line buffer when the program
694 //! operation completes. See \ref VIMSModeSafeSet(), \ref VIMSLineBufDisable(),
695 //! and \ref VIMSLineBufEnable() for more information.
696 //!
697 //! \warning Please note that code can not execute in flash while any part of the flash
698 //! is being programmed or erased. The application must disable interrupts that have
699 //! interrupt routines in flash. This function calls a ROM function which handles the
700 //! actual program operation.
701 //!
702 //! The \c pui8DataBuffer pointer can not point to flash.
703 //!
704 //! \param pui8DataBuffer is a pointer to the data to be programmed.
705 //! \param ui32Address is the starting address in flash to be programmed.
706 //! \param ui32Count is the number of bytes to be programmed.
707 //!
708 //! \return Returns status of the flash programming:
709 //! - \ref FAPI_STATUS_SUCCESS                     : Success.
710 //! - \ref FAPI_STATUS_INCORRECT_DATABUFFER_LENGTH : Too many bytes were requested.
711 //! - \ref FAPI_STATUS_FSM_ERROR                   : A programming error is encountered.
712 //
713 //*****************************************************************************
714 extern uint32_t FlashProgram(uint8_t *pui8DataBuffer,
715                              uint32_t ui32Address, uint32_t ui32Count);
716 
717 //*****************************************************************************
718 //
719 //! \brief Reads efuse data from specified row.
720 //!
721 //! This function will read one efuse row.
722 //! It is assumed that any previous efuse operation has finished.
723 //!
724 //! \param pui32EfuseData is pointer to variable to be updated with efuse data.
725 //! \param ui32RowAddress is the efuse row number to be read. First row is row
726 //! number 0.
727 //!
728 //! \return Returns the status of the efuse read operation.
729 //! - \c false : OK status.
730 //! - \c true  : Error status
731 //
732 //*****************************************************************************
733 extern bool FlashEfuseReadRow(uint32_t *pui32EfuseData,
734                               uint32_t ui32RowAddress);
735 
736 //*****************************************************************************
737 //
738 //! \brief Disables all sectors for erase and programming on the active bank.
739 //!
740 //! This function disables all sectors for erase and programming on the active
741 //! bank and enables the Idle Reading Power reduction mode if no low power
742 //! mode is configured. Furthermore, an additional level of protection from
743 //! erase is enabled.
744 //!
745 //! \note Please note that code can not execute in flash while any part of the flash
746 //! is being programmed or erased.
747 //!
748 //! \return None
749 //
750 //*****************************************************************************
751 extern void FlashDisableSectorsForWrite(void);
752 
753 
754 //*****************************************************************************
755 //
756 // Support for DriverLib in ROM:
757 // Redirect to implementation in ROM when available.
758 //
759 //*****************************************************************************
760 #if !defined(DRIVERLIB_NOROM) && !defined(DOXYGEN)
761     #include "../driverlib/rom.h"
762     #ifdef ROM_FlashPowerModeSet
763         #undef  FlashPowerModeSet
764         #define FlashPowerModeSet               ROM_FlashPowerModeSet
765     #endif
766     #ifdef ROM_FlashPowerModeGet
767         #undef  FlashPowerModeGet
768         #define FlashPowerModeGet               ROM_FlashPowerModeGet
769     #endif
770     #ifdef ROM_FlashProtectionSet
771         #undef  FlashProtectionSet
772         #define FlashProtectionSet              ROM_FlashProtectionSet
773     #endif
774     #ifdef ROM_FlashProtectionGet
775         #undef  FlashProtectionGet
776         #define FlashProtectionGet              ROM_FlashProtectionGet
777     #endif
778     #ifdef ROM_FlashProtectionSave
779         #undef  FlashProtectionSave
780         #define FlashProtectionSave             ROM_FlashProtectionSave
781     #endif
782     #ifdef ROM_FlashSectorErase
783         #undef  FlashSectorErase
784         #define FlashSectorErase                ROM_FlashSectorErase
785     #endif
786     #ifdef ROM_FlashProgram
787         #undef  FlashProgram
788         #define FlashProgram                    ROM_FlashProgram
789     #endif
790     #ifdef ROM_FlashEfuseReadRow
791         #undef  FlashEfuseReadRow
792         #define FlashEfuseReadRow               ROM_FlashEfuseReadRow
793     #endif
794     #ifdef ROM_FlashDisableSectorsForWrite
795         #undef  FlashDisableSectorsForWrite
796         #define FlashDisableSectorsForWrite     ROM_FlashDisableSectorsForWrite
797     #endif
798 #endif
799 
800 //*****************************************************************************
801 //
802 // Mark the end of the C bindings section for C++ compilers.
803 //
804 //*****************************************************************************
805 #ifdef __cplusplus
806 }
807 #endif
808 
809 #endif // __FLASH_H__
810 
811 //*****************************************************************************
812 //
813 //! Close the Doxygen group.
814 //! @}
815 //! @}
816 //
817 //*****************************************************************************
818