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
,
124 static bool PIOS_BL_HELPER_FLASH_GetSectorInfo(uint32_t address
, uint8_t *sector_number
, uint32_t *sector_start
, uint32_t *sector_size
)
126 for (uint8_t i
= 0; i
< NELEMENTS(flash_sectors
); i
++) {
127 struct device_flash_sector
*sector
= &flash_sectors
[i
];
128 if ((address
>= sector
->start
) &&
129 (address
< (sector
->start
+ sector
->size
))) {
130 /* address lies within this sector */
131 *sector_number
= sector
->st_sector
;
132 *sector_start
= sector
->start
;
133 *sector_size
= sector
->size
;
141 uint8_t PIOS_BL_HELPER_FLASH_Start()
143 const struct pios_board_info
*bdinfo
= &pios_board_info_blob
;
144 uint32_t startAddress
= bdinfo
->fw_base
;
145 uint32_t endAddress
= bdinfo
->fw_base
+ bdinfo
->fw_size
+ bdinfo
->desc_size
;
147 bool success
= erase_flash(startAddress
, endAddress
);
149 return (success
) ? 1 : 0;
153 uint8_t PIOS_BL_HELPER_FLASH_Erase_Bootloader()
155 /// Bootloader memory space erase
156 uint32_t startAddress
= BL_BANK_BASE
;
157 uint32_t endAddress
= BL_BANK_BASE
+ BL_BANK_SIZE
;
159 bool success
= erase_flash(startAddress
, endAddress
);
161 return (success
) ? 1 : 0;
164 static bool erase_flash(uint32_t startAddress
, uint32_t endAddress
)
166 uint32_t pageAddress
= startAddress
;
169 while ((pageAddress
< endAddress
) && (fail
== false)) {
170 uint8_t sector_number
;
171 uint32_t sector_start
;
172 uint32_t sector_size
;
173 if (!PIOS_BL_HELPER_FLASH_GetSectorInfo(pageAddress
,
177 /* We're asking for an invalid flash address */
180 for (int retry
= 0; retry
< MAX_DEL_RETRYS
; ++retry
) {
181 if (FLASH_EraseSector(sector_number
, VoltageRange_3
) == FLASH_COMPLETE
) {
188 /* Move to the next sector */
189 pageAddress
+= sector_size
;
194 #endif /* if defined(PIOS_INCLUDE_BL_HELPER_WRITE_SUPPORT) */
196 uint32_t PIOS_BL_HELPER_CRC_Memory_Calc()
198 const struct pios_board_info
*bdinfo
= &pios_board_info_blob
;
200 PIOS_BL_HELPER_CRC_Ini();
202 CRC_CalcBlockCRC((uint32_t *)bdinfo
->fw_base
, (bdinfo
->fw_size
) >> 2);
206 void PIOS_BL_HELPER_FLASH_Read_Description(uint8_t *array
, uint8_t size
)
208 const struct pios_board_info
*bdinfo
= &pios_board_info_blob
;
211 if (size
> bdinfo
->desc_size
) {
212 size
= bdinfo
->desc_size
;
214 for (uint32_t i
= bdinfo
->fw_base
+ bdinfo
->fw_size
; i
< bdinfo
->fw_base
+ bdinfo
->fw_size
+ size
; ++i
) {
215 array
[x
] = *PIOS_BL_HELPER_FLASH_If_Read(i
);
220 void PIOS_BL_HELPER_CRC_Ini()
223 #endif /* PIOS_INCLUDE_BL_HELPER */