1 /**
2  * @file xmc1_flash.h
3  * @date 2019-05-04
4  *
5  * @cond
6  *********************************************************************************************************************
7  * XMClib v2.1.24 - XMC Peripheral Driver Library
8  *
9  * Copyright (c) 2015-2019, Infineon Technologies AG
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without modification,are permitted provided that the
13  * following conditions are met:
14  *
15  * Redistributions of source code must retain the above copyright notice, this list of conditions and the following
16  * disclaimer.
17  *
18  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
19  * disclaimer in the documentation and/or other materials provided with the distribution.
20  *
21  * Neither the name of the copyright holders nor the names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE  FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * To improve the quality of the software, users are encouraged to share modifications, enhancements or bug fixes with
33  * Infineon Technologies AG dave@infineon.com).
34  *********************************************************************************************************************
35  *
36  * Change History
37  * --------------
38  *
39  * 2014-12-10:
40  *     - Initial <br>
41  * 2015-02-20:
42  *     - Updated for Documentation related changes<br>
43  * 2015-06-20:
44  *     - Removed version macros and declaration of GetDriverVersion API
45  * 2019-05-04:
46  *     - Moved definitions of XMC_FLASH_ProgramPage() and XMC_FLASH_EraseSector() from xmc_flash.h
47  *     - Changed XMC_FLASH_ProgramPage() and XMC_FLASH_EraseSector() to return status of operation
48  *     - Changed XMC_FLASH_ErasePages(), XMC_FLASH_ErasePage(), XMC_FLASH_ProgramVerifyPage() to return status of operation
49  *
50  * @endcond
51  *
52  */
53 
54 #ifndef XMC1_FLASH_H
55 #define XMC1_FLASH_H
56 
57 /*********************************************************************************************************************
58  * HEADER FILES
59  ********************************************************************************************************************/
60 
61 #include "xmc_common.h"
62 
63 #if UC_FAMILY == XMC1
64 
65 #include <XMC1000_RomFunctionTable.h>
66 
67 /**
68 * @addtogroup XMClib XMC Peripheral Library
69 * @{
70 */
71 
72 /**
73 * @addtogroup FLASH
74 * @{
75 */
76 
77 /*********************************************************************************************************************
78  * MACROS
79  ********************************************************************************************************************/
80 
81 #define XMC_FLASH_PAGES_PER_SECTOR (16U)    /**< Number of pages in a sector. A page consists of 16 blocks.*/
82 #define XMC_FLASH_BLOCKS_PER_PAGE  (16U)    /**< Number of blocks in a page. A block consists of 4 words(16 bytes).*/
83 
84 #define XMC_FLASH_BYTES_PER_SECTOR (4096U)  /**< Number of bytes in a sector. (16 pages * 256 bytes = 4096 bytes)*/
85 #define XMC_FLASH_BYTES_PER_PAGE   (256U)   /**< Number of bytes in a page. (16 blocks * 16 bytes = 256 bytes)*/
86 #define XMC_FLASH_BYTES_PER_BLOCK  (16U)    /**< Number of bytes in a block. (128 bits = 16 bytes)*/
87 
88 #define XMC_FLASH_WORDS_PER_SECTOR (1024U)  /**< Number of words in a sector. (16 pages * 64 words = 1024 words)*/
89 #define XMC_FLASH_WORDS_PER_PAGE   (64U)    /**< Number of words in a page. (16 blocks * 4 words = 64 words) */
90 #define XMC_FLASH_WORDS_PER_BLOCK  (4U)     /**< Number of words in a block. (128 bit / 32 bit = 4 words) */
91 
92 #define FLASH_BLOCK_ADDR_MASK      (15U)    /*   Bitwise AND with block address is done to check the address alignment.
93                                                  Applicable to XMC_FLASH_WriteBlocks() and XMC_FLASH_VerifyBlocks()
94                                                  APIs.*/
95 #define FLASH_PAGE_ADDR_MASK       (255U)   /*   Bitwise AND with page address is done to check the address alignment.
96                                                  Applicable to XMC_FLASH_ErasePages() API.*/
97 #define FLASH_SECTOR_ADDR_MASK     (4095U)  /*   Bitwise AND with sector address is done to check the address alignment.
98                                                  Applicable to XMC_FLASH_EraseSector API.*/
99 
100 #define XMC_FLASH_BASE             (0x10001000U) /**< Starting address of flash for XMC1 family of microcontrollers*/
101 
102 /*********************************************************************************************************************
103  * ENUMS
104  ********************************************************************************************************************/
105 /**
106  *  Defines the status of flash, to verify the flash related API calls. Use type \a XMC_FLASH_STATUS_t for this enum.
107  *  The members defines the respective masked status bits of \a NVMSTATUS register.
108  */
109 typedef enum XMC_FLASH_STATUS
110 {
111   XMC_FLASH_STATUS_OK                   = 0U,                          /**< Flash related operation was successfully
112                                                                             completed*/
113   XMC_FLASH_STATUS_BUSY                 = NVM_NVMSTATUS_BUSY_Msk    ,  /**< Cannot execute the flash request because
114                                                                             another operation is in progress*/
115   XMC_FLASH_STATUS_SLEEP_MODE           = NVM_NVMSTATUS_SLEEP_Msk   ,  /**< Flash is in sleep mode*/
116   XMC_FLASH_STATUS_VERIFY_ERROR         = NVM_NVMSTATUS_VERR_Msk    ,  /**< Flash reported a verification failure*/
117   XMC_FLASH_STATUS_ECC1_READ_ERROR      = NVM_NVMSTATUS_ECC1READ_Msk,  /**< Flash reports a single bit failure, and it
118                                                                             is automatically corrected.*/
119   XMC_FLASH_STATUS_ECC2_READ_ERROR      = NVM_NVMSTATUS_ECC2READ_Msk,  /**< Flash reported at least two bit failure*/
120   XMC_FLASH_STATUS_WRITE_PROTOCOL_ERROR = NVM_NVMSTATUS_WRPERR_Msk  ,  /**< Write/Verify operation on a block is
121                                                                             failed due to protocol violations or write
122                                                                             protected sectors*/
123 } XMC_FLASH_STATUS_t;
124 
125 /**
126  *  Defines NVM ready interrupt event. Use type \a XMC_FLASH_EVENT_t for this enum.
127  */
128 typedef enum XMC_FLASH_EVENT
129 {
130   XMC_FLASH_EVENT_READY = NVM_NVMCONF_INT_ON_Msk  /**< Generates the NVM ready interrupts on flash sequence completion*/
131 } XMC_FLASH_EVENT_t;
132 
133 /**
134  *  Defines hard read levels for strict data verification. Use type \a XMC_FLASH_HARDREAD_LEVEL_t for this enum.
135  *  These \a hardread levels provide some margin to ensure that the data is really programmed with suitably distinct
136  *  levels for written and erased bits.
137  */
138 typedef enum XMC_FLASH_HARDREAD_LEVEL
139 {
140   XMC_FLASH_HARDREAD_LEVEL_NORMAL  = (uint16_t)0x0, /**< No \a hardread level verification enabled (Normal read)*/
141   XMC_FLASH_HARDREAD_LEVEL_WRITTEN = (uint16_t)0x1, /**< Enables strict margin compare for written data cells*/
142   XMC_FLASH_HARDREAD_LEVEL_ERASED  = (uint16_t)0x2  /**< Enables strict margin compare for erased data cells*/
143 } XMC_FLASH_HARDREAD_LEVEL_t;
144 
145 /**********************************************************************************************************************
146  * API PROTOTYPES
147  *********************************************************************************************************************/
148 
149 #ifdef __cplusplus
150 extern "C" {
151 #endif
152 
153 /**
154  *
155  * @param address    Pointer to the starting address of flash page from where the programming starts.
156  * @param data       Pointer to the source address where targeted data is located.
157  *
158  * @return Status of operation (NVM_STATUS)
159  *
160  * \par<b>Description:</b><br>
161  * Programs a single flash page associated with the specified \a address.\n\n XMC1000 Flash can be programmed with one
162  * page (256 bytes) using this API. It calls the Flash Firmware routine \a XMC1000_NvmProgVerify(unsigned long pageAddr)
163  * to perform the programming. Refer XMC1000 reference manual of for more details on flash firmware routines
164  * (Section 25.3). Call XMC_FLASH_GetStatus() API after calling this API, to verify the programming operation.
165  *
166  * \par<b>Note:</b><br>
167  * Flash will be busy state during write is ongoing, hence no operations allowed until it completes.
168  *
169  * \par<b>Related APIs:</b><BR>
170  * None
171  *
172  */
173 int32_t XMC_FLASH_ProgramPage(uint32_t *address, const uint32_t *data);
174 
175 /**
176  *
177  * @param address Pointer to the starting address of the page to be erased.
178  *
179  * @return Status of operation (NVM_STATUS)
180  *
181  * \par<b>Description:</b><br>
182  * Erases a complete sector starting from the \a address specified.\n\n  XMC1000 Flash can be erased with granularity
183  * of one page = 16 blocks of 16 Bytes = 256 Bytes using this API. It internally calls XMC_FLASH_ErasePages API 16
184  * times starting from the first page of the sector.. Call XMC_FLASH_GetStatus() API after calling this API,
185  * to verify the erase operation.\n
186  *
187  * \par<b>Related APIs:</b><BR>
188  * XMC_FLASH_ErasePages() \n\n\n
189  */
190 int32_t XMC_FLASH_EraseSector(uint32_t *address);
191 
192 /**
193  *
194  * @param address   Pointer to the starting address of the flash page from where the erase starts
195  * @param num_pages Number of pages to be erased.<BR> Range: [1 to (flash size / 256)]
196  *
197  * @return Status of operation (NVM_STATUS)
198  *
199  * \par<b>Description:</b><BR>
200  * Erases a set of flash memory pages.<BR><BR>
201  * Erase starts from the  specified \a address.
202  * It erases a maximum number of \a num_pages flash pages. The maximum erasable pages are limited to
203  * microcontroller flash size. It sets NVMPROG register to continuous page erase mode before erase and resets
204  * it action back to normal state on completion. Call XMC_FLASH_GetStatus() after calling this API to verify the erase
205  * operation.\n
206  *
207  * \par<b>Note:</b><BR>
208  * Flash will be in busy state during erase operation. Hence no operations on flash are allowed until it completes.\n
209  *
210  * \par<b>Related APIs:</b><BR>
211  * XMC_FLASH_EraseSector(), XMC_FLASH_ErasePage() \n\n\n
212  *
213  * \par<b>Related APIs:</b><BR>
214  * None
215  *
216  */
217 int32_t XMC_FLASH_ErasePages(uint32_t *address, uint32_t num_pages);
218 
219 /**
220  *
221  * @param address    Pointer to the starting address of flash block from where the write starts.
222  * @param data       Pointer to the source address where targeted data blocks are located.
223  * @param num_blocks Maximum number of flash block writes needed.<BR> Range:  [1 to (flash size / 16)]
224  * @param verify     If \a true, hardware verification after block write is enabled else disabled.
225  *
226  * @return None
227  *
228  * \par<b>Description:</b><br>
229  * Writes a set of data blocks into the flash.\n\n Minimum possible writable area is 16 byte block. It sets the NVMPROG
230  * register to continuous block write mode before write and resets it action back to normal state on completion.
231  * Call XMC_FLASH_GetStatus() API after calling this API to verify the erase operation.
232  *
233  * \par<b>Note</b><br>
234  * Flash will be busy state during write is ongoing, hence no operations allowed until it completes.
235  *
236  * \par<b>Related APIs:</b><BR>
237  * None
238  *
239  */
240 void XMC_FLASH_WriteBlocks(uint32_t *address, const uint32_t *data, uint32_t num_blocks, bool verify);
241 
242 /**
243  *
244  * @param address    Pointer to the starting address of flash block from where the verification starts.
245  * @param data       Pointer to the source address where targeted data blocks are located.
246  * @param num_blocks Maximum number of flash blocks writes needed.<BR> Range:  [1 to (flash size / 16)]
247  *
248  * @return None
249  *
250  * \par<b>Description:</b><br>
251  * Performs verification of written data blocks.\n\n After calling XMC_FLASH_WriteBlocks() API, calling this API will
252  * verify the correctness of written blocks. It sets the \a NVMPROG register into continuous block write mode before
253  * write and resets it action back to normal state on completion. It reads back the written data blocks from the flash
254  * and verify the values against the internal buffer values. Calling XMC_FLASH_GetStatus() API after calling this API
255  * validates the result of verification.
256  *
257  * \par<b>Related APIs:</b><BR>
258  * None
259  *
260  */
261 void XMC_FLASH_VerifyBlocks(uint32_t *address, const uint32_t *data, uint32_t num_blocks);
262 
263 
264 /**
265  *
266  * @param  address    Pointer to the starting address of flash block from where the read starts.
267  * @param  data       Pointer to the destination address, where the read data blocks to be stored.
268  * @param  num_blocks Number of blocks to be read.<BR> Range:  [1 to (flash size / 16)]
269  *
270  * @return None
271  *
272  * \par<b>Description:</b><br>
273  * Reads multiple blocks from flash in one shot, starting from the \a address specified.\n\n The read blocks are stored
274  * into the locations starting from the \a data address. Calling XMC_FLASH_GetStatus() API after calling this API
275  * verifies the read operation.
276  *
277  * \par<b>Related APIs:</b><BR>
278  * None
279  *
280  */
281 void XMC_FLASH_ReadBlocks(uint32_t *address, uint32_t *data, uint32_t num_blocks);
282 
283 
284 /**
285  *
286  * @param address Pointer to the flash word address from where the read is expected
287  *
288  * @return <BR>
289  *   a 32bit data word stored in the specified \a address.
290  *
291  * \par<b>Description:</b><br>
292  * Reads a single word from the specified flash\a address.\n\n Calling XMC_FLASH_GetStatus() API after calling this
293  * API returns the read status.
294  *
295  * \par<b>Related APIs:</b><BR>
296  * XMC_FLASH_ReadBlocks()
297  *
298  */
XMC_FLASH_ReadWord(const uint32_t * const address)299 __STATIC_INLINE uint32_t XMC_FLASH_ReadWord(const uint32_t *const address)
300 {
301   return *address;
302 }
303 
304 /**
305  *
306  * @param address Pointer to the starting address of the page to be erased
307  *
308  * @return Status of operation (NVM_STATUS)
309  *
310  * \par<b>Description:</b><br>
311  * Erases a single flash page associated to the specified \a address.\n\n XMC1000 Flash can be erased with granularity
312  * of one page = 16 blocks of 16 Bytes = 256 Bytes using this API. It internally calls the Flash Firmware routine
313  * \a XMC1000_NvmErasePage(unsigned long pageAddr) to perform the erase operation. Refer XMC1000 reference manual
314  * for more details on flash firmware routines (Section 25.3). Call XMC_FLASH_GetStatus() API after calling this API,
315  * to verify the erase operation.\n
316  *
317  * \par<b>Related APIs:</b><BR>
318  * XMC_FLASH_ErasePages() \n\n\n
319  */
320 int32_t XMC_FLASH_ErasePage(uint32_t *address);
321 
322 /**
323  *
324  * @param address    Pointer to the starting address of flash page from where the programming starts.
325  * @param data       Pointer to the source address where targeted data blocks are located.
326  *
327  * @return Status of operation (NVM_STATUS)
328  *
329  * \par<b>Description:</b><br>
330  * Erases, programs and verifies a single flash page starting from the \a address specified.\n\n XMC1000 Flash can be
331  * programmed with granularity of one page = 16 blocks of 16 Bytes = 256 Bytes using this API. It internally calls the
332  * Flash Firmware routine \a XMC1000_NvmProgVerify(unsigned long pageAddr) to perform the programming. Refer XMC1000
333  * reference manual of for more details on flash firmware routines (Section 25.3). Call XMC_FLASH_GetStatus() API after
334  * calling this API, to verify the erase operation.
335  *
336  * \par<b>Related APIs:</b><BR>
337  * None
338  *
339  */
340 int32_t XMC_FLASH_ProgramVerifyPage(uint32_t *address, const uint32_t *data);
341 
342 /**
343  *
344  * @param None
345  *
346  * @return None
347  *
348  * \par<b>Description:</b><br>
349  * Enables the flash to enter into sleep mode by resetting the NVMCONF register NVM_ON bit.\n\n Flash can wake up from
350  * sleep mode on any flash operation completion ready event trigger. To disable the sleep mode any time during execution
351  * call the API XMC_FLASH_ExitSleepMode().\n
352  *
353  * \par<b>Related APIs:</b><BR>
354  * XMC_FLASH_ExitSleepMode()\n\n\n
355  *
356  */
XMC_FLASH_EnterSleepMode(void)357 __STATIC_INLINE void XMC_FLASH_EnterSleepMode(void)
358 {
359   NVM->NVMCONF &= (uint16_t)(~(uint32_t)NVM_NVMCONF_NVM_ON_Msk);
360 }
361 
362 /**
363  *
364  * @param None
365  *
366  * @return None
367  *
368  * \par<b>Description:</b><br>
369  * Enables the flash to exit from sleep mode by setting the NVMCONF register NVM_ON bit.\n\n Calling the API
370  * XMC_FLASH_EnterSleepMode() allows the flash to renter into sleep mode.\n
371  *
372  * \par<b>Related APIs:</b><BR>
373  * XMC_FLASH_EnterSleepMode()\n\n\n
374  *
375  */
XMC_FLASH_ExitSleepMode(void)376 __STATIC_INLINE void XMC_FLASH_ExitSleepMode(void)
377 {
378   NVM->NVMCONF |= (uint16_t)NVM_NVMCONF_NVM_ON_Msk;
379 }
380 
381 
382 /**
383  *
384  * @param sector Flash sector number for which the address extraction is needed<BR> Range:  [0 to 51]
385  *
386  * @return uint32_t Starting address of the sector specified<BR> Range:  [0x10001000 to 0x10032000]
387  *
388  * \par<b>Description:</b><br>
389  * Finds the starting address of the specified \a sector number.\n\n
390  *
391  * \par<b>Related APIs:</b><BR>
392  * None
393  *
394  */
XMC_FLASH_GetSectorAddress(uint32_t sector)395 __STATIC_INLINE uint32_t XMC_FLASH_GetSectorAddress(uint32_t sector)
396 {
397   return (XMC_FLASH_BASE + (XMC_FLASH_BYTES_PER_SECTOR * sector));
398 }
399 
400 
401 /**
402  *
403  * @param num_sectors    Number of sectors to be protected<BR> Range:  [0 to 51]
404  *
405  * @return None
406  *
407  * \par<b>Description:</b><br>
408  * Protect the flash sectors starting from 0th sector to the specified \a num_sectors.\n\n It sets the NVMCONF register
409  * SECPROT field with the value specified in \a num_sectors. Changing the protection limit can be achieved by calling
410  * this API at runtime with a different value of \a num_sectors.\n\n
411  *
412  * \par<b>Related APIs:</b><BR>
413  * None
414  *
415  */
XMC_FLASH_SetSectorProtection(uint32_t num_sectors)416 __STATIC_INLINE void XMC_FLASH_SetSectorProtection(uint32_t num_sectors)
417 {
418   NVM->NVMCONF &= (~(uint16_t)NVM_NVMCONF_SECPROT_Msk);
419   NVM->NVMCONF |= (uint16_t)((uint16_t)num_sectors << NVM_NVMCONF_SECPROT_Pos);
420 }
421 
422 
423 /**
424  *
425  * @param level Hard read levels specified in \a XMC_FLASH_HARDREAD_LEVEL_t.
426  *
427  * @return None
428  *
429  * \par<b>Description:</b><br>
430  * Sets the hard read level for verification process.\n\n It insists the flash to do a strict margin compare
431  * with the written/erased data against the internal buffer. Sets the NVMCONF register HRLEV field with \a level
432  * value. This hardread level is used until the end of the verification sequence and, may not be changed in between.\n\n
433  *
434  * \par<b>Related APIs:</b><BR>
435  * None
436  *
437  */
XMC_FLASH_SetHardReadLevel(XMC_FLASH_HARDREAD_LEVEL_t level)438 __STATIC_INLINE void XMC_FLASH_SetHardReadLevel(XMC_FLASH_HARDREAD_LEVEL_t level)
439 {
440   NVM->NVMCONF &= (uint16_t)(~(uint16_t)NVM_NVMCONF_HRLEV_Msk);
441   NVM->NVMCONF |= (uint16_t)(level<< (uint16_t)NVM_NVMCONF_HRLEV_Pos);
442 }
443 
444 #ifdef __cplusplus
445 }
446 #endif
447 
448 /**
449  * @}
450  */
451 
452 /**
453  * @}
454  */
455 
456 #endif
457 
458 #endif /* FLASH_H */
459