CC23x0R5DriverLibrary
flash.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (c) 2022-2023 Texas Instruments Incorporated
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1) Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * 2) Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * 3) Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  ******************************************************************************/
31 
32 #ifndef __FLASH_H__
33 #define __FLASH_H__
34 
35 //*****************************************************************************
36 //
41 //
42 //*****************************************************************************
43 
44 //*****************************************************************************
45 //
46 // If building with a C++ compiler, make all of the definitions in this header
47 // have a C binding.
48 //
49 //*****************************************************************************
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 
54 #include <stdbool.h>
55 #include <stdint.h>
56 
57 #include "../inc/hw_types.h"
58 #include "../inc/hw_flash.h"
59 #include "../inc/hw_memmap.h"
60 #include "../inc/hw_ints.h"
61 #include "../inc/hw_fcfg.h"
62 #include "../inc/hw_vims.h"
63 #include "hapi.h"
64 #include "chipinfo.h"
65 #include "interrupt.h"
66 #include "debug.h"
67 
68 //*****************************************************************************
69 // Values that can be returned from the API functions
70 //*****************************************************************************
71 #define FAPI_STATUS_SUCCESS 0x00000000
72 #define FAPI_STATUS_FSM_BUSY 0x00000001
73 #define FAPI_STATUS_FSM_READY 0x00000002
74 #define FAPI_STATUS_INCORRECT_DATABUFFER_LENGTH 0x00000003
75 #define FAPI_STATUS_FSM_ERROR 0x00000004
76 #define FAPI_STATUS_ADDRESS_ERROR 0x00000005
77 #define FAPI_STATUS_INVALID_KEY 0x00000010
78 
79 //*****************************************************************************
80 //
86 //
87 //*****************************************************************************
89 {
90  return (FLASH_MAIN_SECTOR_SIZE);
91 }
92 
93 //*****************************************************************************
94 //
100 //
101 //*****************************************************************************
103 {
104  // Return flash size in number of bytes
105  return (FLASH_MAIN_SIZE);
106 }
107 
108 //*****************************************************************************
109 //
123 //
124 //*****************************************************************************
126 {
128  {
129  return (FAPI_STATUS_SUCCESS);
130  }
131  else
132  {
133  return (FAPI_STATUS_FSM_ERROR);
134  }
135 }
136 
137 //*****************************************************************************
138 //
152 //
153 //*****************************************************************************
155 {
157  {
158  return (FAPI_STATUS_FSM_READY);
159  }
160  else
161  {
162  return (FAPI_STATUS_FSM_BUSY);
163  }
164 }
165 
166 //*****************************************************************************
167 //
190 //
191 //*****************************************************************************
192 __STATIC_INLINE uint32_t FlashEraseSector(uint32_t sectorAddress)
193 {
194  // Store current configuration
195  uint32_t cchctrl = HWREG(VIMS_BASE + VIMS_O_CCHCTRL);
196  // Clear instruction cache
198 
199  uint32_t retCode = HapiFlashSectorErase(FLASH_API_KEY, sectorAddress);
200 
201  // Restore configuration
202  HWREG(VIMS_BASE + VIMS_O_CCHCTRL) = cchctrl;
203 
204  return (retCode);
205 }
206 
207 /*****************************************************************************
208  * \brief Erase all unprotected sectors in the flash main bank
209  *
210  * This function will erase all unprotected main bank flash sectors. It will
211  * not return until the flash sectors has been erased or an error condition
212  * occurs.
213  *
214  * \warning Please note that code can not execute in flash while any part of
215  * the flash is being programmed or erased. The application must disable
216  * interrupts that have interrupt routines in flash.
217  *
218  * \return Returns the status of the sector erase:
219  * - \ref FAPI_STATUS_SUCCESS (0): Success
220  * - \ref FAPI_STATUS_FSM_ERROR : An erase error is encountered.
221  *****************************************************************************/
223 {
224  // Store current configuration
225  uint32_t cchctrl = HWREG(VIMS_BASE + VIMS_O_CCHCTRL);
226 
227  // Clear instruction cache
229 
230  uint32_t retCode = HapiFlashBankErase(FLASH_API_KEY);
231 
232  // Restore configuration
233  HWREG(VIMS_BASE + VIMS_O_CCHCTRL) = cchctrl;
234 
235  return (retCode);
236 }
237 
238 //*****************************************************************************
239 //
269 //
270 //*****************************************************************************
271 __STATIC_INLINE uint32_t FlashProgram(uint8_t *dataBuffer, uint32_t address, uint32_t count)
272 {
273  // Store current configuration
274  uint32_t cchctrl = HWREG(VIMS_BASE + VIMS_O_CCHCTRL);
275 
276  // Clear instruction cache
278 
279  uint32_t retCode = HapiFlashProgram(FLASH_API_KEY, dataBuffer, address, count);
280 
281  // Restore configuration
282  HWREG(VIMS_BASE + VIMS_O_CCHCTRL) = cchctrl;
283 
284  return (retCode);
285 }
286 
287 //*****************************************************************************
288 //
289 // Mark the end of the C bindings section for C++ compilers.
290 //
291 //*****************************************************************************
292 #ifdef __cplusplus
293 }
294 #endif
295 
296 //*****************************************************************************
297 //
301 //
302 //*****************************************************************************
303 
304 #endif // __FLASH_H__
#define VIMS_BASE
Definition: hw_memmap.h:60
__STATIC_INLINE uint32_t FlashGetSize(void)
Get the size of the flash.
Definition: flash.h:102
__STATIC_INLINE uint32_t FlashCheckFsmForReady(void)
Checks if the Flash state machine is ready.
Definition: flash.h:154
#define HapiFlashBankErase(k)
Definition: hapi.h:219
__STATIC_INLINE uint32_t FlashProgram(uint8_t *dataBuffer, uint32_t address, uint32_t count)
Programs unprotected flash sectors in the main bank.
Definition: flash.h:271
#define HWREG(x)
Definition: hw_types.h:79
#define __STATIC_INLINE
Definition: hw_types.h:58
__STATIC_INLINE uint32_t FlashCheckFsmForError(void)
Checks if the Flash state machine has detected an error.
Definition: flash.h:125
#define FLASH_O_STATCMD
Definition: hw_flash.h:109
#define FLASH_STATCMD_CMDDONE_M
Definition: hw_flash.h:880
#define FLASH_STATCMD_CMDPASS_M
Definition: hw_flash.h:868
__STATIC_INLINE uint32_t FlashEraseBank(void)
Definition: flash.h:222
#define FAPI_STATUS_SUCCESS
Function completed successfully.
Definition: flash.h:71
#define FAPI_STATUS_FSM_ERROR
Flash program/erase operation failed.
Definition: flash.h:75
__STATIC_INLINE uint32_t FlashGetSectorSize(void)
Get size of a flash sector in number of bytes.
Definition: flash.h:88
#define FLASH_MAIN_SECTOR_SIZE
Size of a MAIN flash sector, in number of bytes.
Definition: hw_device.h:54
#define FAPI_STATUS_FSM_READY
FSM is Ready.
Definition: flash.h:73
#define FLASH_BASE
Definition: hw_memmap.h:57
#define VIMS_O_CCHCTRL
Definition: hw_vims.h:82
#define VIMS_CCHCTRL_CCHMPEN_DIS
Definition: hw_vims.h:412
__STATIC_INLINE uint32_t FlashEraseSector(uint32_t sectorAddress)
Erase a flash sector.
Definition: flash.h:192
#define FLASH_API_KEY
Definition: hapi.h:43
#define HapiFlashProgram(k, s, d, n)
Definition: hapi.h:264
#define VIMS_CCHCTRL_CCHEN_DIS
Definition: hw_vims.h:436
#define HapiFlashSectorErase(k, p)
Definition: hapi.h:197
#define FLASH_MAIN_SIZE
Definition: hw_memmap.h:43
#define FAPI_STATUS_FSM_BUSY
FSM is Busy.
Definition: flash.h:72
#define VIMS_CCHCTRL_CCHPFEN_DIS
Definition: hw_vims.h:424