amiro-blt / Target / Source / ARMCM3_STM32 / nvm.c @ df2f435c
History | View | Annotate | Download (7.985 KB)
1 |
/************************************************************************************//** |
---|---|
2 |
* \file Source\ARMCM3_STM32\nvm.c
|
3 |
* \brief Bootloader non-volatile memory driver source file.
|
4 |
* \ingroup Target_ARMCM3_STM32
|
5 |
* \internal
|
6 |
*----------------------------------------------------------------------------------------
|
7 |
* C O P Y R I G H T
|
8 |
*----------------------------------------------------------------------------------------
|
9 |
* Copyright (c) 2011 by Feaser http://www.feaser.com All rights reserved
|
10 |
*
|
11 |
*----------------------------------------------------------------------------------------
|
12 |
* L I C E N S E
|
13 |
*----------------------------------------------------------------------------------------
|
14 |
* This file is part of OpenBLT. OpenBLT is free software: you can redistribute it and/or
|
15 |
* modify it under the terms of the GNU General Public License as published by the Free
|
16 |
* Software Foundation, either version 3 of the License, or (at your option) any later
|
17 |
* version.
|
18 |
*
|
19 |
* OpenBLT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
20 |
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
21 |
* PURPOSE. See the GNU General Public License for more details.
|
22 |
*
|
23 |
* You should have received a copy of the GNU General Public License along with OpenBLT.
|
24 |
* If not, see <http://www.gnu.org/licenses/>.
|
25 |
*
|
26 |
* A special exception to the GPL is included to allow you to distribute a combined work
|
27 |
* that includes OpenBLT without being obliged to provide the source code for any
|
28 |
* proprietary components. The exception text is included at the bottom of the license
|
29 |
* file <license.html>.
|
30 |
*
|
31 |
* \endinternal
|
32 |
****************************************************************************************/
|
33 |
|
34 |
/****************************************************************************************
|
35 |
* Include files
|
36 |
****************************************************************************************/
|
37 |
#include "boot.h" /* bootloader generic header */ |
38 |
|
39 |
|
40 |
/****************************************************************************************
|
41 |
* Hook functions
|
42 |
****************************************************************************************/
|
43 |
#if (BOOT_NVM_HOOKS_ENABLE > 0) |
44 |
extern void NvmInitHook(void); |
45 |
extern blt_int8u NvmWriteHook(blt_addr addr, blt_int32u len, blt_int8u *data);
|
46 |
extern blt_int8u NvmEraseHook(blt_addr addr, blt_int32u len);
|
47 |
extern blt_bool NvmDoneHook(void); |
48 |
#endif
|
49 |
|
50 |
#if (BOOT_NVM_CHECKSUM_HOOKS_ENABLE > 0) |
51 |
extern blt_bool NvmWriteChecksumHook(void); |
52 |
extern blt_bool NvmVerifyChecksumHook(void); |
53 |
#endif
|
54 |
|
55 |
|
56 |
|
57 |
/************************************************************************************//** |
58 |
** \brief Initializes the NVM driver.
|
59 |
** \return none.
|
60 |
**
|
61 |
****************************************************************************************/
|
62 |
void NvmInit(void) |
63 |
{ |
64 |
#if (BOOT_NVM_HOOKS_ENABLE > 0) |
65 |
/* give the application a chance to initialize a driver for operating on NVM
|
66 |
* that is not by default supported by this driver.
|
67 |
*/
|
68 |
NvmInitHook(); |
69 |
#endif
|
70 |
|
71 |
/* init the internal driver */
|
72 |
FlashInit(); |
73 |
} /*** end of NvmInit ***/
|
74 |
|
75 |
|
76 |
/************************************************************************************//** |
77 |
** \brief Programs the non-volatile memory.
|
78 |
** \param addr Start address.
|
79 |
** \param len Length in bytes.
|
80 |
** \param data Pointer to the data buffer.
|
81 |
** \return BLT_TRUE if successful, BLT_FALSE otherwise.
|
82 |
**
|
83 |
****************************************************************************************/
|
84 |
blt_bool NvmWrite(blt_addr addr, blt_int32u len, blt_int8u *data) |
85 |
{ |
86 |
#if (BOOT_NVM_HOOKS_ENABLE > 0) |
87 |
blt_int8u result = BLT_NVM_NOT_IN_RANGE; |
88 |
#endif
|
89 |
|
90 |
#if (BOOT_NVM_HOOKS_ENABLE > 0) |
91 |
/* give the application a chance to operate on memory that is not by default supported
|
92 |
* by this driver.
|
93 |
*/
|
94 |
result = NvmWriteHook(addr, len, data); |
95 |
|
96 |
/* process the return code */
|
97 |
if (result == BLT_NVM_OKAY)
|
98 |
{ |
99 |
/* data was within range of the additionally supported memory and succesfully
|
100 |
* programmed, so we are all done.
|
101 |
*/
|
102 |
return BLT_TRUE;
|
103 |
} |
104 |
else if (result == BLT_NVM_ERROR) |
105 |
{ |
106 |
/* data was within range of the additionally supported memory and attempted to be
|
107 |
* programmed, but an error occurred, so we can't continue.
|
108 |
*/
|
109 |
return BLT_FALSE;
|
110 |
} |
111 |
#endif
|
112 |
|
113 |
/* still here so the internal driver should try and perform the program operation */
|
114 |
return FlashWrite(addr, len, data);
|
115 |
} /*** end of NvmWrite ***/
|
116 |
|
117 |
|
118 |
/************************************************************************************//** |
119 |
** \brief Erases the non-volatile memory.
|
120 |
** \param addr Start address.
|
121 |
** \param len Length in bytes.
|
122 |
** \return BLT_TRUE if successful, BLT_FALSE otherwise.
|
123 |
**
|
124 |
****************************************************************************************/
|
125 |
blt_bool NvmErase(blt_addr addr, blt_int32u len) |
126 |
{ |
127 |
#if (BOOT_NVM_HOOKS_ENABLE > 0) |
128 |
blt_int8u result = BLT_NVM_NOT_IN_RANGE; |
129 |
#endif
|
130 |
|
131 |
#if (BOOT_NVM_HOOKS_ENABLE > 0) |
132 |
/* give the application a chance to operate on memory that is not by default supported
|
133 |
* by this driver.
|
134 |
*/
|
135 |
result = NvmEraseHook(addr, len); |
136 |
|
137 |
/* process the return code */
|
138 |
if (result == BLT_NVM_OKAY)
|
139 |
{ |
140 |
/* address was within range of the additionally supported memory and succesfully
|
141 |
* erased, so we are all done.
|
142 |
*/
|
143 |
return BLT_TRUE;
|
144 |
} |
145 |
else if (result == BLT_NVM_ERROR) |
146 |
{ |
147 |
/* address was within range of the additionally supported memory and attempted to be
|
148 |
* erased, but an error occurred, so we can't continue.
|
149 |
*/
|
150 |
return BLT_FALSE;
|
151 |
} |
152 |
#endif
|
153 |
|
154 |
/* still here so the internal driver should try and perform the erase operation */
|
155 |
return FlashErase(addr, len);
|
156 |
} /*** end of NvmErase ***/
|
157 |
|
158 |
|
159 |
/************************************************************************************//** |
160 |
** \brief Verifies the checksum, which indicates that a valid user program is
|
161 |
** present and can be started.
|
162 |
** \return BLT_TRUE if successful, BLT_FALSE otherwise.
|
163 |
**
|
164 |
****************************************************************************************/
|
165 |
blt_bool NvmVerifyChecksum(void)
|
166 |
{ |
167 |
#if (BOOT_NVM_CHECKSUM_HOOKS_ENABLE > 0) |
168 |
/* check checksum using the application specific method. */
|
169 |
return NvmVerifyChecksumHook();
|
170 |
#else
|
171 |
/* check checksum using the interally supported method. */
|
172 |
return FlashVerifyChecksum();
|
173 |
#endif
|
174 |
} /*** end of NvmVerifyChecksum ***/
|
175 |
|
176 |
|
177 |
/************************************************************************************//** |
178 |
** \brief Once all erase and programming operations are completed, this
|
179 |
** function is called, so at the end of the programming session and
|
180 |
** right before a software reset is performed. It is used to calculate
|
181 |
** a checksum and program this into flash. This checksum is later used
|
182 |
** to determine if a valid user program is present in flash.
|
183 |
** \return BLT_TRUE if successful, BLT_FALSE otherwise.
|
184 |
**
|
185 |
****************************************************************************************/
|
186 |
blt_bool NvmDone(void)
|
187 |
{ |
188 |
#if (BOOT_NVM_HOOKS_ENABLE > 0) |
189 |
/* give the application's NVM driver a chance to finish up */
|
190 |
if (NvmDoneHook() == BLT_FALSE)
|
191 |
{ |
192 |
/* error so no need to continue */
|
193 |
return BLT_FALSE;
|
194 |
} |
195 |
#endif
|
196 |
|
197 |
#if (BOOT_NVM_CHECKSUM_HOOKS_ENABLE > 0) |
198 |
/* compute and write checksum, using the application specific method. */
|
199 |
if (NvmWriteChecksumHook() == BLT_FALSE)
|
200 |
{ |
201 |
return BLT_FALSE;
|
202 |
} |
203 |
#else
|
204 |
/* compute and write checksum, which is programmed by the internal driver. */
|
205 |
if (FlashWriteChecksum() == BLT_FALSE)
|
206 |
{ |
207 |
return BLT_FALSE;
|
208 |
} |
209 |
#endif
|
210 |
|
211 |
/* finish up internal driver operations */
|
212 |
return FlashDone();
|
213 |
} /*** end of NvmDone ***/
|
214 |
|
215 |
|
216 |
/*********************************** end of nvm.c **************************************/
|