1 /**
2   *********************************************************************************
3   *
4   * @file    ald_iap.c
5   * @brief   IAP module driver.
6   *
7   * @version V1.0
8   * @date    04 Dec 2019
9   * @author  AE Team
10   * @note
11   *          Change Logs:
12   *          Date            Author          Notes
13   *          04 Dec 2019     AE Team         The first version
14   *
15   * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
16   *
17   * SPDX-License-Identifier: Apache-2.0
18   *
19   * Licensed under the Apache License, Version 2.0 (the License); you may
20   * not use this file except in compliance with the License.
21   * You may obtain a copy of the License at
22   *
23   * www.apache.org/licenses/LICENSE-2.0
24   *
25   * Unless required by applicable law or agreed to in writing, software
26   * distributed under the License is distributed on an AS IS BASIS, WITHOUT
27   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28   * See the License for the specific language governing permissions and
29   * limitations under the License.
30   **********************************************************************************
31   */
32 
33 #include "ald_conf.h"
34 
35 
36 /** @addtogroup ES32FXXX_ALD
37   * @{
38   */
39 
40 /** @defgroup IAP IAP
41   * @brief IAP module driver
42   * @{
43   */
44 #ifdef ALD_IAP
45 
46 
47 /** @defgroup IAP_Public_Functions IAP Public Functions
48   *
49   * @verbatim
50   ==============================================================================
51               #####  Erase and Program flash functions #####
52   ==============================================================================
53     [..]  This section provides functions allowing to:
54       (+) Erase flash.
55       (+) Program flash.
56 
57     @endverbatim
58   * @{
59   */
60 
61 /**
62   * @brief  Erases a specified page.
63   * @param  addr: The beginning address of the page to be erased.
64   * @retval The result:
65   *           - 0: SUCCESS
66   *           - 1: ERROR
67   */
ald_iap_erase_page(uint32_t addr)68 uint32_t ald_iap_erase_page(uint32_t addr)
69 {
70 	uint32_t status;
71 	IAP_PE iap_pe = (IAP_PE)(*(uint32_t *)IAP_PE_ADDR);
72 
73 	__disable_irq();
74 	status = (*iap_pe)(addr);
75 	__enable_irq();
76 
77 	return !status;
78 }
79 
80 /**
81   * @brief  Programs a word at a specified address.
82   * @param  addr: Specifies the address to be programmed.
83   *         Bit0-1 must be zero.
84   * @param  data: Specifies the data to be programmed.
85   * @retval The result:
86   *           - 0: SUCCESS
87   *           - 1: ERROR
88   */
ald_iap_program_word(uint32_t addr,uint32_t data)89 uint32_t ald_iap_program_word(uint32_t addr, uint32_t data)
90 {
91 	uint32_t status;
92 	IAP_WP iap_wp = (IAP_WP)(*(uint32_t *)IAP_WP_ADDR);
93 
94 	if (addr & 0x3)
95 		return 1;
96 
97 	__disable_irq();
98 	status = (*iap_wp)(addr, data);
99 	__enable_irq();
100 
101 	return !status;
102 }
103 
104 /**
105   * @brief  Programs double words at a specified address.
106   * @param  addr: Specifies the address to be programmed.
107   *         Bit0-1 must be zero.
108   * @param  data_l: Specifies the LSB data to be programmed.
109   * @param  data_h: Specifies the MSB data to be programmed.
110   * @retval The result:
111   *           - 0: SUCCESS
112   *           - 1: ERROR
113   */
ald_iap_program_dword(uint32_t addr,uint32_t data_l,uint32_t data_h)114 uint32_t ald_iap_program_dword(uint32_t addr, uint32_t data_l, uint32_t data_h)
115 {
116 	uint32_t status;
117 	IAP_DWP iap_dwp = (IAP_DWP)(*(uint32_t *)IAP_DWP_ADDR);
118 
119 	if (addr & 0x3)
120 		return 1;
121 
122 	__disable_irq();
123 	status = (*iap_dwp)(addr, data_l, data_h);
124 	__enable_irq();
125 
126 	return !status;
127 }
128 
129 /**
130   * @brief  Programs datas at a specified address.
131   * @param  addr: Specifies the address to be programmed.
132   *         Bit0-1 must be zero.
133   * @param  data: Specifies the data to be programmed.
134   * @param  len: Specifies the data length to be programmed.
135   *         Bit0-1 must be zero.
136   * @param  erase: Erase page flag before programming.
137   * @retval The result:
138   *           - 0: SUCCESS
139   *           - 1: ERROR
140   */
ald_iap_program_words(uint32_t addr,uint8_t * data,uint32_t len,uint32_t erase)141 uint32_t ald_iap_program_words(uint32_t addr, uint8_t *data, uint32_t len, uint32_t erase)
142 {
143 	uint32_t status;
144 	IAP_WSP iap_wsp = (IAP_WSP)(*(uint32_t *)IAP_WSP_ADDR);
145 
146 	if ((addr & 0x3) || (len & 0x3))
147 		return 1;
148 
149 	__disable_irq();
150 	status = (*iap_wsp)(addr, data, len, erase);
151 	__enable_irq();
152 
153 	return !status;
154 }
155 
156 /**
157   * @brief  Erases a specified page of dataflash.
158   * @param  addr: The beginning address of the page to be erased.
159   * @retval The result:
160   *           - 0: SUCCESS
161   *           - 1: ERROR
162   */
ald_iap_erase_page_df(uint32_t addr)163 uint32_t ald_iap_erase_page_df(uint32_t addr)
164 {
165 	uint32_t status;
166 	IAP_PE iap_pe = (IAP_PE)(*(uint32_t *)IAP_PageErase_DF);
167 
168 	__disable_irq();
169 	status = (*iap_pe)(addr);
170 	__enable_irq();
171 
172 	return !status;
173 }
174 
175 /**
176   * @brief  Programs a word at a specified address of dataflash.
177   * @param  addr: Specifies the address to be programmed.
178   *         Bit0-1 must be zero.
179   * @param  data: Specifies the data to be programmed.
180   * @retval The result:
181   *           - 0: SUCCESS
182   *           - 1: ERROR
183   */
ald_iap_program_word_df(uint32_t addr,uint32_t data)184 uint32_t ald_iap_program_word_df(uint32_t addr, uint32_t data)
185 {
186 	uint32_t status;
187 	IAP_WP iap_wp = (IAP_WP)(*(uint32_t *)IAP_WordProgram_DF);
188 
189 	if (addr & 0x3)
190 		return 1;
191 
192 	__disable_irq();
193 	status = (*iap_wp)(addr, data);
194 	__enable_irq();
195 
196 	return !status;
197 }
198 
199 /**
200   * @brief  Programs double words at a specified address of dataflash.
201   * @param  addr: Specifies the address to be programmed.
202   *         Bit0-1 must be zero.
203   * @param  data_l: Specifies the LSB data to be programmed.
204   * @param  data_h: Specifies the MSB data to be programmed.
205   * @retval The result:
206   *           - 0: SUCCESS
207   *           - 1: ERROR
208   */
ald_iap_program_dword_df(uint32_t addr,uint32_t data_l,uint32_t data_h)209 uint32_t ald_iap_program_dword_df(uint32_t addr, uint32_t data_l, uint32_t data_h)
210 {
211 	uint32_t status;
212 	IAP_DWP iap_dwp = (IAP_DWP)(*(uint32_t *)IAP_DWordProgram_DF);
213 
214 	if (addr & 0x3)
215 		return 1;
216 
217 	__disable_irq();
218 	status = (*iap_dwp)(addr, data_l, data_h);
219 	__enable_irq();
220 
221 	return !status;
222 }
223 
224 /**
225   * @brief  Programs datas at a specified address of dataflash.
226   * @param  addr: Specifies the address to be programmed.
227   *         Bit0-1 must be zero.
228   * @param  data: Specifies the data to be programmed.
229   * @param  len: Specifies the data length to be programmed.
230   *         Bit0-1 must be zero.
231   * @param  erase: Erase page flag before programming.
232   * @retval The result:
233   *           - 0: SUCCESS
234   *           - 1: ERROR
235   */
ald_iap_program_words_df(uint32_t addr,uint8_t * data,uint32_t len,uint32_t erase)236 uint32_t ald_iap_program_words_df(uint32_t addr, uint8_t *data, uint32_t len, uint32_t erase)
237 {
238 	uint32_t status;
239 	IAP_WSP iap_wsp = (IAP_WSP)(*(uint32_t *)IAP_WordsProgram_DF);
240 
241 	if ((addr & 0x3) || (len & 0x3))
242 		return 1;
243 
244 	__disable_irq();
245 	status = (*iap_wsp)(addr, data, len, erase);
246 	__enable_irq();
247 
248 	return !status;
249 }
250 /**
251   * @}
252   */
253 #endif /* ALD_IAP */
254 /**
255   * @}
256   */
257 /**
258   * @}
259   */
260