CC23x0r2DriverLibrary
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_DISABLE | VIMS_CCHCTRL_CCHPFEN_DISABLE |
197  VIMS_CCHCTRL_CCHEN_DISABLE);
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
228  HWREG(VIMS_BASE + VIMS_O_CCHCTRL) = (VIMS_CCHCTRL_CCHMPEN_DISABLE | VIMS_CCHCTRL_CCHPFEN_DISABLE |
229  VIMS_CCHCTRL_CCHEN_DISABLE);
230 
231  uint32_t retCode = HapiFlashBankErase(FLASH_API_KEY);
232 
233  // Restore configuration
234  HWREG(VIMS_BASE + VIMS_O_CCHCTRL) = cchctrl;
235 
236  return (retCode);
237 }
238 
239 //*****************************************************************************
240 //
270 //
271 //*****************************************************************************
272 __STATIC_INLINE uint32_t FlashProgram(uint8_t *dataBuffer, uint32_t address, uint32_t count)
273 {
274  // Store current configuration
275  uint32_t cchctrl = HWREG(VIMS_BASE + VIMS_O_CCHCTRL);
276 
277  // Clear instruction cache
278  HWREG(VIMS_BASE + VIMS_O_CCHCTRL) = (VIMS_CCHCTRL_CCHMPEN_DISABLE | VIMS_CCHCTRL_CCHPFEN_DISABLE |
279  VIMS_CCHCTRL_CCHEN_DISABLE);
280 
281  uint32_t retCode = HapiFlashProgram(FLASH_API_KEY, dataBuffer, address, count);
282 
283  // Restore configuration
284  HWREG(VIMS_BASE + VIMS_O_CCHCTRL) = cchctrl;
285 
286  return (retCode);
287 }
288 
289 //*****************************************************************************
290 //
291 // Mark the end of the C bindings section for C++ compilers.
292 //
293 //*****************************************************************************
294 #ifdef __cplusplus
295 }
296 #endif
297 
298 //*****************************************************************************
299 //
303 //
304 //*****************************************************************************
305 
306 #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:272
__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:222
#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