1 /**
2  * @file xmc1_flash.c
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  * 2015-02-10:
40  *     - Initial <br>
41  *
42  * 2015-06-20:
43  *     - Removed definition of GetDriverVersion API
44  *
45  * 2015-10-14:
46  *     - Fixed defect in API XMC_FLASH_ErasePages, related to the errata NVM_CM.001
47  *     - NVM ROM user routine XMC1000_NvmErasePage(address) used for erase page.
48  *
49  * 2019-05-04:
50  *     - Changed XMC_FLASH_ErasePage() and XMC_FLASH_ProgramVerifyPage() to return status of operation
51  *     - Changed XMC_FLASH_ErasePages(), XMC_FLASH_EraseSector() and XMC_FLASH_ProgramPage() to return status of operation
52  *
53  * @endcond
54  *
55  */
56 
57 #include "xmc_flash.h"
58 
59 /*********************************************************************************************************************
60  * MACROS
61  ********************************************************************************************************************/
62 #if UC_FAMILY == XMC1
63 
64 /*********************************************************************************************************************
65  * ENUMS
66  ********************************************************************************************************************/
67 /* FLASH programming / erase options */
68 typedef enum FLASH_ACTION
69 {
70   FLASH_ACTION_IDLE                        =  (uint32_t)0x00,
71   FLASH_ACTION_ONESHOT_WRITE_VERIFY        = ((uint32_t)0x51 << NVM_NVMPROG_ACTION_Pos),
72   FLASH_ACTION_ONESHOT_WRITE               = ((uint32_t)0x91 << NVM_NVMPROG_ACTION_Pos),
73   FLASH_ACTION_CONTINUOUS_WRITE_VERIFY     = ((uint32_t)0x61 << NVM_NVMPROG_ACTION_Pos),
74   FLASH_ACTION_CONTINUOUS_WRITE            = ((uint32_t)0xa1 << NVM_NVMPROG_ACTION_Pos),
75   FLASH_ACTION_ONESHOT_PAGE_ERASE          = ((uint32_t)0x92 << NVM_NVMPROG_ACTION_Pos),
76   FLASH_ACTION_CONTINUOUS_PAGE_ERASE       = ((uint32_t)0xa2 << NVM_NVMPROG_ACTION_Pos),
77   FLASH_ACTION_ONESHOT_VERIFY_ONLY         = ((uint32_t)0xd0 << NVM_NVMPROG_ACTION_Pos),
78   FLASH_ACTION_CONTINUOUS_VERIFY_ONLY      = ((uint32_t)0xe0 << NVM_NVMPROG_ACTION_Pos)
79 } FLASH_ACTION_t;
80 
81 
82 /*********************************************************************************************************************
83  * API IMPLEMENTATION
84  ********************************************************************************************************************/
85 
86 /*
87  * This API shall clear the ECC and VERIFICATION error status.
88  */
XMC_FLASH_ClearStatus(void)89 void XMC_FLASH_ClearStatus(void)
90 {
91   NVM->NVMPROG |= (uint16_t)((uint16_t)NVM_NVMPROG_RSTVERR_Msk | (uint16_t)NVM_NVMPROG_RSTECC_Msk);
92 }
93 
94 /*
95  * This API shall return the status of NVM.
96  */
XMC_FLASH_GetStatus(void)97 uint32_t XMC_FLASH_GetStatus(void)
98 {
99   return NVM->NVMSTATUS;
100 }
101 
102 /*
103  * This API shall enable the the flash interrupt event.
104  */
XMC_FLASH_EnableEvent(const uint32_t event_msk)105 void XMC_FLASH_EnableEvent(const uint32_t event_msk)
106 {
107   NVM->NVMCONF |= (uint16_t)event_msk;
108 }
109 
110 /*
111  * This API shall disable the the flash interrupt event.
112  */
XMC_FLASH_DisableEvent(const uint32_t event_msk)113 void XMC_FLASH_DisableEvent(const uint32_t event_msk)
114 {
115   NVM->NVMCONF &= (uint16_t)(~(uint16_t)event_msk);
116 }
117 
XMC_FLASH_ErasePage(uint32_t * address)118 int32_t XMC_FLASH_ErasePage(uint32_t *address)
119 {
120   return XMC1000_NvmErasePage(address);
121 }
122 
XMC_FLASH_ProgramVerifyPage(uint32_t * address,const uint32_t * data)123 int32_t XMC_FLASH_ProgramVerifyPage(uint32_t *address, const uint32_t *data)
124 {
125   return XMC1000_NvmProgVerify(data, address);
126 }
127 
128 /* Write blocks of data into flash*/
XMC_FLASH_WriteBlocks(uint32_t * address,const uint32_t * data,uint32_t num_blocks,bool verify)129 void XMC_FLASH_WriteBlocks(uint32_t *address, const uint32_t *data, uint32_t num_blocks, bool verify)
130 {
131   uint32_t word;
132   uint32_t block;
133 
134   XMC_ASSERT("XMC_FLASH_WriteBlocks: Starting address not aligned to Block",
135                                                                    ((uint32_t)address & FLASH_BLOCK_ADDR_MASK) == 0U)
136 
137   /* Configure the continuous Write option command and reset the NVM error / verification status*/
138   NVM->NVMPROG &= (uint16_t)(~(uint16_t)NVM_NVMPROG_ACTION_Msk);
139   NVM->NVMPROG |= (uint16_t)(NVM_NVMPROG_RSTVERR_Msk | NVM_NVMPROG_RSTECC_Msk);
140 
141   if (verify == true)
142   {
143     NVM->NVMPROG |= (uint16_t)FLASH_ACTION_CONTINUOUS_WRITE_VERIFY;
144   }
145   else
146   {
147     NVM->NVMPROG |= (uint16_t)FLASH_ACTION_CONTINUOUS_WRITE;
148   }
149 
150   for (block = 0U; block < num_blocks; ++block)
151   {
152     for (word = 0U; word < XMC_FLASH_WORDS_PER_BLOCK; ++word)
153     {
154       *address = *data;
155       data++;
156       address++;
157     }
158 
159     while (XMC_FLASH_IsBusy() == true)
160     {
161     }
162   }
163 
164   /* Stop continuous write operation */
165   NVM->NVMPROG &= (uint16_t)(~(uint16_t)NVM_NVMPROG_ACTION_Msk);
166 }
167 
168 /* Erase flash pages */
XMC_FLASH_ErasePages(uint32_t * address,uint32_t num_pages)169 int32_t XMC_FLASH_ErasePages(uint32_t *address, uint32_t num_pages)
170 {
171   uint32_t page;
172 
173   XMC_ASSERT("XMC_FLASH_ErasePages: Starting address not aligned to Page",
174                                                                     ((uint32_t)address & FLASH_PAGE_ADDR_MASK) == 0U)
175 
176   int32_t status;
177   for (page = 0U; page < num_pages; ++page)
178   {
179     status = XMC1000_NvmErasePage(address);
180     if (status != NVM_PASS)
181     {
182       return status;
183     }
184 
185     /* Increment the page address for the next erase */
186     address += XMC_FLASH_WORDS_PER_PAGE;
187   }
188 
189   return status;
190 
191 }
192 
193 /* Write multiple data blocks and verify the written data */
XMC_FLASH_VerifyBlocks(uint32_t * address,const uint32_t * data,uint32_t num_blocks)194 void XMC_FLASH_VerifyBlocks(uint32_t *address, const uint32_t *data, uint32_t num_blocks)
195 {
196   uint32_t word;
197   uint32_t block;
198 
199   XMC_ASSERT("XMC_FLASH_VerifyBlocks: Starting address not aligned to Block",
200                                                                    ((uint32_t)address & FLASH_BLOCK_ADDR_MASK) == 0U)
201 
202   /* Configure the Continuous write with verify option command and reset the NVM error / verification status*/
203   NVM->NVMPROG &= (uint16_t)~NVM_NVMPROG_ACTION_Msk;
204   NVM->NVMPROG |= (uint16_t)((uint16_t)NVM_NVMPROG_RSTVERR_Msk |
205                              (uint16_t)NVM_NVMPROG_RSTECC_Msk |
206                              (uint16_t)FLASH_ACTION_CONTINUOUS_VERIFY_ONLY);
207 
208   for (block = 0U; block < num_blocks; ++block)
209   {
210     for (word = 0U; word < XMC_FLASH_WORDS_PER_BLOCK; ++word)
211     {
212       *address = *data;
213       data++;
214       address++;
215     }
216 
217     while (XMC_FLASH_IsBusy() == true)
218     {
219     }
220   }
221 
222   /* Stop continuous verify operation */
223   NVM->NVMPROG &= (uint16_t)(~(uint16_t)NVM_NVMPROG_ACTION_Msk);
224 }
225 
226 /* Read data blocks from flash */
XMC_FLASH_ReadBlocks(uint32_t * address,uint32_t * data,uint32_t num_blocks)227 void XMC_FLASH_ReadBlocks(uint32_t *address, uint32_t *data, uint32_t num_blocks)
228 {
229   uint32_t word;
230   uint32_t block;
231 
232   XMC_ASSERT("XMC_FLASH_ReadBlocks: Starting address not aligned to Block",
233                                                                   ((uint32_t)address & FLASH_BLOCK_ADDR_MASK) == 0U)
234 
235   for (block = 0U; block < num_blocks; ++block)
236   {
237     for (word = 0U; word < XMC_FLASH_WORDS_PER_BLOCK; ++word)
238     {
239       *data = *address;
240       data++;
241       address++;
242     }
243   }
244 }
245 
246 /* Erase single sector */
XMC_FLASH_EraseSector(uint32_t * address)247 int32_t XMC_FLASH_EraseSector(uint32_t *address)
248 {
249   XMC_ASSERT("XMC_FLASH_EraseSector: Starting address not aligned to Sector",
250                                                                  ((uint32_t)address & FLASH_SECTOR_ADDR_MASK) == 0U)
251   return XMC_FLASH_ErasePages(address, XMC_FLASH_PAGES_PER_SECTOR);
252 }
253 
254 /* Program single page */
XMC_FLASH_ProgramPage(uint32_t * address,const uint32_t * data)255 int32_t XMC_FLASH_ProgramPage(uint32_t *address, const uint32_t *data)
256 {
257   return XMC_FLASH_ProgramVerifyPage(address, data);
258 }
259 
260 #endif
261