CC23x0R5DriverLibrary
flash.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (c) 2022-2024 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_vims.h"
62 #include "hapi.h"
63 #include "chipinfo.h"
64 #include "interrupt.h"
65 #include "debug.h"
66 
67 //*****************************************************************************
68 // Values that can be returned from the API functions
69 //*****************************************************************************
70 #define FAPI_STATUS_SUCCESS 0x00000000
71 #define FAPI_STATUS_FSM_BUSY 0x00000001
72 #define FAPI_STATUS_FSM_READY 0x00000002
73 #define FAPI_STATUS_INCORRECT_DATABUFFER_LENGTH 0x00000003
74 #define FAPI_STATUS_FSM_ERROR 0x00000004
75 #define FAPI_STATUS_ADDRESS_ERROR 0x00000005
76 #define FAPI_STATUS_INVALID_KEY 0x00000010
77 
78 //*****************************************************************************
79 //
85 //
86 //*****************************************************************************
88 {
89  return (FLASH_MAIN_SECTOR_SIZE);
90 }
91 
92 //*****************************************************************************
93 //
99 //
100 //*****************************************************************************
102 {
103  // Return flash size in number of bytes
104  return (FLASH_MAIN_SIZE);
105 }
106 
107 //*****************************************************************************
108 //
122 //
123 //*****************************************************************************
125 {
126  if (HWREG(FLASH_BASE + FLASH_O_STATCMD) & FLASH_STATCMD_CMDPASS_M)
127  {
128  return (FAPI_STATUS_SUCCESS);
129  }
130  else
131  {
132  return (FAPI_STATUS_FSM_ERROR);
133  }
134 }
135 
136 //*****************************************************************************
137 //
151 //
152 //*****************************************************************************
154 {
155  if (HWREG(FLASH_BASE + FLASH_O_STATCMD) & FLASH_STATCMD_CMDDONE_M)
156  {
157  return (FAPI_STATUS_FSM_READY);
158  }
159  else
160  {
161  return (FAPI_STATUS_FSM_BUSY);
162  }
163 }
164 
165 //*****************************************************************************
166 //
189 //
190 //*****************************************************************************
191 __STATIC_INLINE uint32_t FlashEraseSector(uint32_t sectorAddress)
192 {
193  // Store current configuration
194  uint32_t cchctrl = HWREG(VIMS_BASE + VIMS_O_CCHCTRL);
195  // Clear instruction cache
196  HWREG(VIMS_BASE + VIMS_O_CCHCTRL) = (VIMS_CCHCTRL_CCHMPEN_DIS | VIMS_CCHCTRL_CCHPFEN_DIS | VIMS_CCHCTRL_CCHEN_DIS);
197 
198  uint32_t retCode = HapiFlashSectorErase(FLASH_API_KEY, sectorAddress);
199 
200  // Restore configuration
201  HWREG(VIMS_BASE + VIMS_O_CCHCTRL) = cchctrl;
202 
203  return (retCode);
204 }
205 
206 /*****************************************************************************
207  * \brief Erase all unprotected sectors in the flash main bank
208  *
209  * This function will erase all unprotected main bank flash sectors. It will
210  * not return until the flash sectors has been erased or an error condition
211  * occurs.
212  *
213  * \warning Please note that code can not execute in flash while any part of
214  * the flash is being programmed or erased. The application must disable
215  * interrupts that have interrupt routines in flash.
216  *
217  * \return Returns the status of the sector erase:
218  * - \ref FAPI_STATUS_SUCCESS (0): Success
219  * - \ref FAPI_STATUS_FSM_ERROR : An erase error is encountered.
220  *****************************************************************************/
222 {
223  // Store current configuration
224  uint32_t cchctrl = HWREG(VIMS_BASE + VIMS_O_CCHCTRL);
225 
226  // Clear instruction cache
227  HWREG(VIMS_BASE + VIMS_O_CCHCTRL) = (VIMS_CCHCTRL_CCHMPEN_DIS | VIMS_CCHCTRL_CCHPFEN_DIS | VIMS_CCHCTRL_CCHEN_DIS);
228 
229  uint32_t retCode = HapiFlashBankErase(FLASH_API_KEY);
230 
231  // Restore configuration
232  HWREG(VIMS_BASE + VIMS_O_CCHCTRL) = cchctrl;
233 
234  return (retCode);
235 }
236 
237 //*****************************************************************************
238 //
268 //
269 //*****************************************************************************
270 __STATIC_INLINE uint32_t FlashProgram(uint8_t *dataBuffer, uint32_t address, uint32_t count)
271 {
272  // Store current configuration
273  uint32_t cchctrl = HWREG(VIMS_BASE + VIMS_O_CCHCTRL);
274 
275  // Clear instruction cache
276  HWREG(VIMS_BASE + VIMS_O_CCHCTRL) = (VIMS_CCHCTRL_CCHMPEN_DIS | VIMS_CCHCTRL_CCHPFEN_DIS | VIMS_CCHCTRL_CCHEN_DIS);
277 
278  uint32_t retCode = HapiFlashProgram(FLASH_API_KEY, dataBuffer, address, count);
279 
280  // Restore configuration
281  HWREG(VIMS_BASE + VIMS_O_CCHCTRL) = cchctrl;
282 
283  return (retCode);
284 }
285 
286 //*****************************************************************************
287 //
288 // Mark the end of the C bindings section for C++ compilers.
289 //
290 //*****************************************************************************
291 #ifdef __cplusplus
292 }
293 #endif
294 
295 //*****************************************************************************
296 //
300 //
301 //*****************************************************************************
302 
303 #endif // __FLASH_H__
__STATIC_INLINE uint32_t FlashGetSize(void)
Get the size of the flash.
Definition: flash.h:101
__STATIC_INLINE uint32_t FlashCheckFsmForReady(void)
Checks if the Flash state machine is ready.
Definition: flash.h:153
#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:270
__STATIC_INLINE uint32_t FlashCheckFsmForError(void)
Checks if the Flash state machine has detected an error.
Definition: flash.h:124
__STATIC_INLINE uint32_t FlashEraseBank(void)
Definition: flash.h:221
#define FAPI_STATUS_SUCCESS
Function completed successfully.
Definition: flash.h:70
#define FAPI_STATUS_FSM_ERROR
Flash program/erase operation failed.
Definition: flash.h:74
__STATIC_INLINE uint32_t FlashGetSectorSize(void)
Get size of a flash sector in number of bytes.
Definition: flash.h:87
#define FAPI_STATUS_FSM_READY
FSM is Ready.
Definition: flash.h:72
__STATIC_INLINE uint32_t FlashEraseSector(uint32_t sectorAddress)
Erase a flash sector.
Definition: flash.h:191
#define FLASH_API_KEY
Definition: hapi.h:43
#define HapiFlashProgram(k, s, d, n)
Definition: hapi.h:264
#define __STATIC_INLINE
Definition: cmsis_gcc.h:47
#define HapiFlashSectorErase(k, p)
Definition: hapi.h:197
#define FAPI_STATUS_FSM_BUSY
FSM is Busy.
Definition: flash.h:71