2 ******************************************************************************
3 * @addtogroup PIOS PIOS Core hardware abstraction layer
5 * @addtogroup PIOS_BOOTLOADER Functions
6 * @brief HAL code to interface to the OpenPilot AHRS module
9 * @file pios_bl_helper.c
10 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
11 * @brief Bootloader Helper Functions
12 * @see The GNU Public License (GPL) Version 3
14 *****************************************************************************/
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 * You should have received a copy of the GNU General Public License along
27 * with this program; if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #ifdef PIOS_INCLUDE_BL_HELPER
35 #include <pios_board_info.h>
36 #include <stm32f4xx_flash.h>
39 uint8_t *PIOS_BL_HELPER_FLASH_If_Read(uint32_t SectorAddress
)
41 return (uint8_t *)(SectorAddress
);
44 #if defined(PIOS_INCLUDE_BL_HELPER_WRITE_SUPPORT)
46 static bool erase_flash(uint32_t startAddress
, uint32_t endAddress
);
48 uint8_t PIOS_BL_HELPER_FLASH_Ini()
51 FLASH_ClearFlag(FLASH_FLAG_EOP
| FLASH_FLAG_OPERR
| FLASH_FLAG_WRPERR
| FLASH_FLAG_PGAERR
| FLASH_FLAG_PGPERR
| FLASH_FLAG_PGSERR
);
55 struct device_flash_sector
{
61 static struct device_flash_sector flash_sectors
[] = {
65 .st_sector
= FLASH_Sector_0
,
70 .st_sector
= FLASH_Sector_1
,
75 .st_sector
= FLASH_Sector_2
,
80 .st_sector
= FLASH_Sector_3
,
85 .st_sector
= FLASH_Sector_4
,
90 .st_sector
= FLASH_Sector_5
,
95 .st_sector
= FLASH_Sector_6
,
100 .st_sector
= FLASH_Sector_7
,
105 .st_sector
= FLASH_Sector_8
,
110 .st_sector
= FLASH_Sector_9
,
115 .st_sector
= FLASH_Sector_10
,
120 .st_sector
= FLASH_Sector_11
,
125 .st_sector
= FLASH_Sector_12
,
130 .st_sector
= FLASH_Sector_13
,
135 .st_sector
= FLASH_Sector_14
,
140 .st_sector
= FLASH_Sector_15
,
145 .st_sector
= FLASH_Sector_16
,
150 .st_sector
= FLASH_Sector_17
,
155 .st_sector
= FLASH_Sector_18
,
160 .st_sector
= FLASH_Sector_19
,
165 .st_sector
= FLASH_Sector_20
,
170 .st_sector
= FLASH_Sector_21
,
175 .st_sector
= FLASH_Sector_22
,
180 .st_sector
= FLASH_Sector_23
,
184 static bool PIOS_BL_HELPER_FLASH_GetSectorInfo(uint32_t address
, uint8_t *sector_number
, uint32_t *sector_start
, uint32_t *sector_size
)
186 for (uint8_t i
= 0; i
< NELEMENTS(flash_sectors
); i
++) {
187 struct device_flash_sector
*sector
= &flash_sectors
[i
];
188 if ((address
>= sector
->start
) &&
189 (address
< (sector
->start
+ sector
->size
))) {
190 /* address lies within this sector */
191 *sector_number
= sector
->st_sector
;
192 *sector_start
= sector
->start
;
193 *sector_size
= sector
->size
;
201 uint8_t PIOS_BL_HELPER_FLASH_Start()
203 const struct pios_board_info
*bdinfo
= &pios_board_info_blob
;
204 uint32_t startAddress
= bdinfo
->fw_base
;
205 uint32_t endAddress
= bdinfo
->fw_base
+ bdinfo
->fw_size
+ bdinfo
->desc_size
;
207 bool success
= erase_flash(startAddress
, endAddress
);
209 return (success
) ? 1 : 0;
213 uint8_t PIOS_BL_HELPER_FLASH_Erase_Bootloader()
215 /// Bootloader memory space erase
216 uint32_t startAddress
= BL_BANK_BASE
;
217 uint32_t endAddress
= BL_BANK_BASE
+ BL_BANK_SIZE
;
219 bool success
= erase_flash(startAddress
, endAddress
);
221 return (success
) ? 1 : 0;
224 static bool erase_flash(uint32_t startAddress
, uint32_t endAddress
)
226 uint32_t pageAddress
= startAddress
;
229 while ((pageAddress
< endAddress
) && (fail
== false)) {
230 uint8_t sector_number
;
231 uint32_t sector_start
;
232 uint32_t sector_size
;
233 if (!PIOS_BL_HELPER_FLASH_GetSectorInfo(pageAddress
,
237 /* We're asking for an invalid flash address */
240 for (int retry
= 0; retry
< MAX_DEL_RETRYS
; ++retry
) {
241 // if erasing area contain whole bank2 area, using bank erase
242 // bank2: sector 12 to sector 23
243 if (sector_start
== flash_sectors
[12].start
&&
244 endAddress
>= (flash_sectors
[23].start
+ flash_sectors
[23].size
)) {
245 if (FLASH_EraseAllBank2Sectors(VoltageRange_3
) == FLASH_COMPLETE
) {
247 // using bank2 total size substitute sector_size
248 sector_size
= flash_sectors
[23].start
- flash_sectors
[12].start
+ flash_sectors
[23].size
;
253 } else if (FLASH_EraseSector(sector_number
, VoltageRange_3
) == FLASH_COMPLETE
) {
260 /* Move to the next sector */
261 pageAddress
+= sector_size
;
266 #endif /* if defined(PIOS_INCLUDE_BL_HELPER_WRITE_SUPPORT) */
268 uint32_t PIOS_BL_HELPER_CRC_Memory_Calc()
270 const struct pios_board_info
*bdinfo
= &pios_board_info_blob
;
272 PIOS_BL_HELPER_CRC_Ini();
274 CRC_CalcBlockCRC((uint32_t *)bdinfo
->fw_base
, (bdinfo
->fw_size
) >> 2);
278 void PIOS_BL_HELPER_FLASH_Read_Description(uint8_t *array
, uint8_t size
)
280 const struct pios_board_info
*bdinfo
= &pios_board_info_blob
;
283 if (size
> bdinfo
->desc_size
) {
284 size
= bdinfo
->desc_size
;
286 for (uint32_t i
= bdinfo
->fw_base
+ bdinfo
->fw_size
; i
< bdinfo
->fw_base
+ bdinfo
->fw_size
+ size
; ++i
) {
287 array
[x
] = *PIOS_BL_HELPER_FLASH_If_Read(i
);
292 void PIOS_BL_HELPER_CRC_Ini()
295 #endif /* PIOS_INCLUDE_BL_HELPER */