CC27xxDriverLibrary
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 //
69 // Values that can be returned from the API functions
70 //
71 //*****************************************************************************
72 #define FAPI_STATUS_SUCCESS 0x00000000
73 #define FAPI_STATUS_FSM_BUSY 0x00000001
74 #define FAPI_STATUS_FSM_READY 0x00000002
75 #define FAPI_STATUS_INCORRECT_DATABUFFER_LENGTH 0x00000003
76 #define FAPI_STATUS_FSM_ERROR 0x00000004
77 #define FAPI_STATUS_ADDRESS_ERROR 0x00000005
78 #define FAPI_STATUS_INVALID_KEY 0x00000010
79 
80 //*****************************************************************************
81 //
82 // Values returned from FlashGetProtection().
83 //
84 //*****************************************************************************
85 #define FLASH_NO_PROTECT 0x00000000
86 #define FLASH_WRITE_PROTECT 0x00000001
87 
88 //*****************************************************************************
89 //
95 //
96 //*****************************************************************************
98 {
99  return (FLASH_MAIN_SECTOR_SIZE);
100 }
101 
102 //*****************************************************************************
103 //
109 //
110 //*****************************************************************************
112 {
113  // Return flash size in number of bytes
114  return (FLASH_MAIN_SIZE);
115 }
116 
117 //*****************************************************************************
118 //
138 //
139 //*****************************************************************************
140 __STATIC_INLINE uint32_t FlashGetProtection(uint32_t address)
141 {
142  uint32_t regOffset;
143  uint32_t mask;
144 
145  if (address >= FLASH_MAIN_SIZE)
146  {
148  }
149 
150  if (address < (32 * FLASH_MAIN_SECTOR_SIZE)) // Check for VIMS WEPRA
151  {
152  mask = (1 << (address / FLASH_MAIN_SECTOR_SIZE));
153  regOffset = VIMS_O_WEPRA;
154  }
155  else if (address < (256 * FLASH_MAIN_SECTOR_SIZE)) // Check for VIMS WEPRB0
156  {
157  mask = (1 << ((address / (8 * FLASH_MAIN_SECTOR_SIZE))));
158  regOffset = VIMS_O_WEPRB0;
159  }
160  else if (address < (512 * FLASH_MAIN_SECTOR_SIZE)) // Check for VIMS WEPRB1
161  {
162  mask = (1 << ((address / (8 * FLASH_MAIN_SECTOR_SIZE)) % 32));
163  regOffset = VIMS_O_WEPRB1;
164  }
165  else
166  {
168  }
169 
170  // VIMS WEPRA, WEPRB0 and WEPRB1 are sticky 0 (0 = active, 1 = inactive).
171  if (HWREG(VIMS_BASE + regOffset) & mask)
172  {
173  return FLASH_NO_PROTECT;
174  }
175 
176  return FLASH_WRITE_PROTECT;
177 }
178 
179 #ifndef DRIVERLIB_NS
180 //*****************************************************************************
181 //
182 // Below functions will only be compiled into the driverlib.a library and not
183 // the driverlib_ns.a library.
184 // Non-secure applications might be able to access some/all of these functions
185 // through veneers declared further below in the #else case.
186 //
187 //*****************************************************************************
188 
189 //*****************************************************************************
190 //
204 //
205 //*****************************************************************************
207 {
208  if (HWREG(FLASH_BASE + FLASH_O_STATCMD) & FLASH_STATCMD_CMDPASS_M)
209  {
210  return (FAPI_STATUS_SUCCESS);
211  }
212  else
213  {
214  return (FAPI_STATUS_FSM_ERROR);
215  }
216 }
217 
218 //*****************************************************************************
219 //
233 //
234 //*****************************************************************************
236 {
237  if (HWREG(FLASH_BASE + FLASH_O_STATCMD) & FLASH_STATCMD_CMDDONE_M)
238  {
239  return (FAPI_STATUS_FSM_READY);
240  }
241  else
242  {
243  return (FAPI_STATUS_FSM_BUSY);
244  }
245 }
246 
247 //*****************************************************************************
248 //
271 //
272 //*****************************************************************************
273 __STATIC_INLINE uint32_t FlashEraseSector(uint32_t sectorAddress)
274 {
275  // Store current configuration
276  uint32_t cchctl = HWREG(VIMS_BASE + VIMS_O_CCHCTL);
277  // Clear instruction cache
278  HWREG(VIMS_BASE + VIMS_O_CCHCTL) = (VIMS_CCHCTL_CCHEN_DIS | VIMS_CCHCTL_LINEN_DIS);
279 
280  uint32_t retCode = HapiFlashSectorErase(FLASH_API_KEY, sectorAddress);
281 
282  // Restore configuration
283  HWREG(VIMS_BASE + VIMS_O_CCHCTL) = cchctl;
284 
285  return (retCode);
286 }
287 
288 //*****************************************************************************
289 //
308 //
309 //*****************************************************************************
310 __STATIC_INLINE uint32_t FlashEraseBank(uint32_t bankSelect)
311 {
312  // Store current configuration
313  uint32_t cchctl = HWREG(VIMS_BASE + VIMS_O_CCHCTL);
314 
315  // Clear instruction cache
316  HWREG(VIMS_BASE + VIMS_O_CCHCTL) = (VIMS_CCHCTL_CCHEN_DIS | VIMS_CCHCTL_LINEN_DIS);
317 
318  uint32_t retCode = HapiFlashBankErase(FLASH_API_KEY, bankSelect);
319 
320  // Restore configuration
321  HWREG(VIMS_BASE + VIMS_O_CCHCTL) = cchctl;
322 
323  return (retCode);
324 }
325 
326 //*****************************************************************************
327 //
357 //
358 //*****************************************************************************
359 __STATIC_INLINE uint32_t FlashProgram(uint8_t *dataBuffer, uint32_t address, uint32_t count)
360 {
361  // Store current configuration
362  uint32_t cchctl = HWREG(VIMS_BASE + VIMS_O_CCHCTL);
363 
364  // Clear instruction cache
365  HWREG(VIMS_BASE + VIMS_O_CCHCTL) = (VIMS_CCHCTL_CCHEN_DIS | VIMS_CCHCTL_LINEN_DIS);
366 
367  uint32_t retCode = HapiFlashProgram(FLASH_API_KEY, dataBuffer, address, count);
368 
369  // Restore configuration
370  HWREG(VIMS_BASE + VIMS_O_CCHCTL) = cchctl;
371 
372  return (retCode);
373 }
374 
375 #else
376  //*****************************************************************************
377  //
378  // Secure-only API functions available from non-secure context through
379  // veneers.
380  //
381  // These functions must be linked in from from the secure veneer library.
382  //
383  //*****************************************************************************
384 
385  // No veneers
386 
387 #endif
388 
389 //*****************************************************************************
390 //
391 // Mark the end of the C bindings section for C++ compilers.
392 //
393 //*****************************************************************************
394 #ifdef __cplusplus
395 }
396 #endif
397 
398 //*****************************************************************************
399 //
403 //
404 //*****************************************************************************
405 
406 #endif // __FLASH_H__
__STATIC_INLINE uint32_t FlashGetProtection(uint32_t address)
Returns the write/erase protection for a given main flash address.
Definition: flash.h:140
#define HapiFlashBankErase(key, bankSelect)
Erase the specified bank.
Definition: hapi.h:253
__STATIC_INLINE uint32_t FlashGetSize(void)
Get the size of the flash.
Definition: flash.h:111
__STATIC_INLINE uint32_t FlashCheckFsmForReady(void)
Checks if the Flash state machine is ready.
Definition: flash.h:235
__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:359
#define HapiFlashSectorErase(key, addr)
Erase a flash sector.
Definition: hapi.h:231
__STATIC_INLINE uint32_t FlashCheckFsmForError(void)
Checks if the Flash state machine has detected an error.
Definition: flash.h:206
#define FLASH_NO_PROTECT
Sector not protected.
Definition: flash.h:85
#define FAPI_STATUS_SUCCESS
Function completed successfully.
Definition: flash.h:72
#define FAPI_STATUS_FSM_ERROR
Flash program/erase operation failed.
Definition: flash.h:76
__STATIC_INLINE uint32_t FlashGetSectorSize(void)
Get size of a flash sector in number of bytes.
Definition: flash.h:97
#define HapiFlashProgram(key, data, addr, nBytes)
Program to flash (FCFG, CCFG or MAIN)
Definition: hapi.h:291
#define FAPI_STATUS_FSM_READY
FSM is Ready.
Definition: flash.h:74
__STATIC_INLINE uint32_t FlashEraseSector(uint32_t sectorAddress)
Erase a flash sector.
Definition: flash.h:273
#define FLASH_WRITE_PROTECT
Sector is write / erase protected.
Definition: flash.h:86
#define FLASH_API_KEY
Definition: hapi.h:45
__STATIC_INLINE uint32_t FlashEraseBank(uint32_t bankSelect)
Erase all unprotected sectors in the selected flash main bank.
Definition: flash.h:310
#define __STATIC_INLINE
Definition: cmsis_gcc.h:47
#define FAPI_STATUS_FSM_BUSY
FSM is Busy.
Definition: flash.h:73
#define FAPI_STATUS_ADDRESS_ERROR
Address range or alignment error.
Definition: flash.h:77