1 /**
2   *********************************************************************************
3   *
4   * @file    ald_iap.c
5   * @brief   IAP module driver.
6   *
7   * @version V1.0
8   * @date    04 Dec 2017
9   * @author  AE Team
10   * @note
11   *
12   * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
13   *
14   * SPDX-License-Identifier: Apache-2.0
15   *
16   * Licensed under the Apache License, Version 2.0 (the License); you may
17   * not use this file except in compliance with the License.
18   * You may obtain a copy of the License at
19   *
20   * www.apache.org/licenses/LICENSE-2.0
21   *
22   * Unless required by applicable law or agreed to in writing, software
23   * distributed under the License is distributed on an AS IS BASIS, WITHOUT
24   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25   * See the License for the specific language governing permissions and
26   * limitations under the License.
27   *
28   *********************************************************************************
29   */
30 
31 #include "ald_iap.h"
32 
33 
34 /** @addtogroup ES32FXXX_ALD
35   * @{
36   */
37 
38 /** @defgroup IAP IAP
39   * @brief IAP module driver
40   * @{
41   */
42 #ifdef ALD_IAP
43 
44 
45 /** @defgroup IAP_Public_Functions IAP Public Functions
46   *
47   * @verbatim
48   ==============================================================================
49               #####  Erase and Program flash functions #####
50   ==============================================================================
51     [..]  This section provides functions allowing to:
52       (+) Erase flash.
53       (+) Program flash.
54 
55     @endverbatim
56   * @{
57   */
58 
59 /**
60   * @brief  Erases a specified page.
61   * @param  addr: The beginning address of the page to be erased.
62   * @retval The result:
63   *           - 0: SUCCESS
64   *           - 1: ERROR
65   */
ald_iap_erase_page(uint32_t addr)66 uint32_t ald_iap_erase_page(uint32_t addr)
67 {
68 	uint32_t status;
69 	IAP_PE iap_pe = (IAP_PE)(*(uint32_t *)IAP_PE_ADDR);
70 
71 	__disable_irq();
72 	status = (*iap_pe)(addr);
73 	__enable_irq();
74 
75 	return !status;
76 }
77 
78 /**
79   * @brief  Programs a word at a specified address.
80   * @param  addr: Specifies the address to be programmed.
81   *         Bit0-1 must be zero.
82   * @param  data: Specifies the data to be programmed.
83   * @retval The result:
84   *           - 0: SUCCESS
85   *           - 1: ERROR
86   */
ald_iap_program_word(uint32_t addr,uint32_t data)87 uint32_t ald_iap_program_word(uint32_t addr, uint32_t data)
88 {
89 	uint32_t status;
90 	IAP_WP iap_wp = (IAP_WP)(*(uint32_t *)IAP_WP_ADDR);
91 
92 	if (addr & 0x3)
93 		return 1;
94 
95 	__disable_irq();
96 	status = (*iap_wp)(addr, data);
97 	__enable_irq();
98 
99 	return !status;
100 }
101 
102 /**
103   * @brief  Programs double words at a specified address.
104   * @param  addr: Specifies the address to be programmed.
105   *         Bit0-1 must be zero.
106   * @param  data_l: Specifies the LSB data to be programmed.
107   * @param  data_h: Specifies the MSB data to be programmed.
108   * @retval The result:
109   *           - 0: SUCCESS
110   *           - 1: ERROR
111   */
ald_iap_program_dword(uint32_t addr,uint32_t data_l,uint32_t data_h)112 uint32_t ald_iap_program_dword(uint32_t addr, uint32_t data_l, uint32_t data_h)
113 {
114 	uint32_t status;
115 	IAP_DWP iap_dwp = (IAP_DWP)(*(uint32_t *)IAP_DWP_ADDR);
116 
117 	if (addr & 0x3)
118 		return 1;
119 
120 	__disable_irq();
121 	status = (*iap_dwp)(addr, data_l, data_h);
122 	__enable_irq();
123 
124 	return !status;
125 }
126 
127 /**
128   * @brief  Programs datas at a specified address.
129   * @param  addr: Specifies the address to be programmed.
130   *         Bit0-1 must be zero.
131   * @param  data: Specifies the data to be programmed.
132   * @param  len: Specifies the data length to be programmed.
133   *         Bit0-1 must be zero.
134   * @param  erase: Erase page flag before programming.
135   * @retval The result:
136   *           - 0: SUCCESS
137   *           - 1: ERROR
138   */
ald_iap_program_words(uint32_t addr,uint8_t * data,uint32_t len,uint32_t erase)139 uint32_t ald_iap_program_words(uint32_t addr, uint8_t *data, uint32_t len, uint32_t erase)
140 {
141 	uint32_t status;
142 	IAP_WSP iap_wsp = (IAP_WSP)(*(uint32_t *)IAP_WSP_ADDR);
143 
144 	if ((addr & 0x3) || (len & 0x3))
145 		return 1;
146 
147 	__disable_irq();
148 	status = (*iap_wsp)(addr, data, len, erase);
149 	__enable_irq();
150 
151 	return !status;
152 }
153 /**
154   * @}
155   */
156 #endif /* ALD_IAP */
157 /**
158   * @}
159   */
160 
161 /**
162   * @}
163   */
164