2010-09-27 09:28:45 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup PIOS PIOS Core hardware abstraction layer
|
|
|
|
* @{
|
|
|
|
* @addtogroup PIOS_BOOTLOADER Functions
|
|
|
|
* @brief HAL code to interface to the OpenPilot AHRS module
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file pios_bl_helper.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @brief Bootloader Helper Functions
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2013-04-28 10:19:08 +02:00
|
|
|
#include <pios.h>
|
2013-03-15 19:25:30 +01:00
|
|
|
|
|
|
|
#ifdef PIOS_INCLUDE_BL_HELPER
|
|
|
|
|
2011-05-24 00:15:06 +02:00
|
|
|
#include <pios_board_info.h>
|
2013-04-28 10:19:08 +02:00
|
|
|
#include <stm32f10x_flash.h>
|
|
|
|
#include <stdbool.h>
|
2013-04-27 15:15:28 +02:00
|
|
|
|
2011-04-23 21:11:17 +02:00
|
|
|
uint8_t *PIOS_BL_HELPER_FLASH_If_Read(uint32_t SectorAddress)
|
2010-09-27 09:28:45 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
return (uint8_t *)(SectorAddress);
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
|
|
|
|
2011-04-23 19:40:41 +02:00
|
|
|
#if defined(PIOS_INCLUDE_BL_HELPER_WRITE_SUPPORT)
|
2013-04-27 15:15:28 +02:00
|
|
|
|
|
|
|
static bool erase_flash(uint32_t startAddress, uint32_t endAddress);
|
|
|
|
|
2011-04-23 21:11:17 +02:00
|
|
|
uint8_t PIOS_BL_HELPER_FLASH_Ini()
|
2010-09-27 09:28:45 +02:00
|
|
|
{
|
2013-04-27 15:15:28 +02:00
|
|
|
FLASH_Unlock();
|
|
|
|
return 1;
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
|
|
|
|
2011-04-23 21:11:17 +02:00
|
|
|
uint8_t PIOS_BL_HELPER_FLASH_Start()
|
2010-09-27 09:28:45 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
const struct pios_board_info *bdinfo = &pios_board_info_blob;
|
2013-04-27 15:15:28 +02:00
|
|
|
uint32_t startAddress = bdinfo->fw_base;
|
2013-05-19 16:37:30 +02:00
|
|
|
uint32_t endAddress = bdinfo->fw_base + bdinfo->fw_size + bdinfo->desc_size;
|
2013-04-27 15:15:28 +02:00
|
|
|
|
|
|
|
bool success = erase_flash(startAddress, endAddress);
|
|
|
|
|
|
|
|
return (success) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
uint8_t PIOS_BL_HELPER_FLASH_Erase_Bootloader()
|
|
|
|
{
|
2013-04-27 15:15:28 +02:00
|
|
|
/// Bootloader memory space erase
|
|
|
|
uint32_t startAddress = BL_BANK_BASE;
|
2013-05-19 16:37:30 +02:00
|
|
|
uint32_t endAddress = BL_BANK_BASE + BL_BANK_SIZE;
|
2013-04-27 15:15:28 +02:00
|
|
|
|
|
|
|
bool success = erase_flash(startAddress, endAddress);
|
|
|
|
|
|
|
|
return (success) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
static bool erase_flash(uint32_t startAddress, uint32_t endAddress)
|
|
|
|
{
|
2013-04-28 10:19:08 +02:00
|
|
|
uint32_t pageAddress = startAddress;
|
|
|
|
uint8_t fail = false;
|
2013-05-19 16:37:30 +02:00
|
|
|
|
2013-04-28 10:19:08 +02:00
|
|
|
while ((pageAddress < endAddress) && (fail == false)) {
|
2013-04-27 15:15:28 +02:00
|
|
|
for (int retry = 0; retry < MAX_DEL_RETRYS; ++retry) {
|
2013-04-28 10:19:08 +02:00
|
|
|
if (FLASH_ErasePage(pageAddress) == FLASH_COMPLETE) {
|
|
|
|
fail = false;
|
2013-04-27 15:15:28 +02:00
|
|
|
break;
|
|
|
|
} else {
|
2013-04-28 10:19:08 +02:00
|
|
|
fail = true;
|
2013-04-27 15:15:28 +02:00
|
|
|
}
|
|
|
|
}
|
2010-09-27 09:28:45 +02:00
|
|
|
|
|
|
|
#ifdef STM32F10X_HD
|
2013-04-28 10:19:08 +02:00
|
|
|
pageAddress += 2048;
|
2013-05-19 16:37:30 +02:00
|
|
|
#elif defined(STM32F10X_MD)
|
2013-04-28 10:19:08 +02:00
|
|
|
pageAddress += 1024;
|
2010-09-27 09:28:45 +02:00
|
|
|
#endif
|
2013-04-27 15:15:28 +02:00
|
|
|
}
|
|
|
|
return !fail;
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
2013-04-27 15:15:28 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
#endif /* if defined(PIOS_INCLUDE_BL_HELPER_WRITE_SUPPORT) */
|
2010-09-27 09:28:45 +02:00
|
|
|
|
2011-04-23 21:11:17 +02:00
|
|
|
uint32_t PIOS_BL_HELPER_CRC_Memory_Calc()
|
2010-09-27 09:28:45 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
const struct pios_board_info *bdinfo = &pios_board_info_blob;
|
2011-05-24 00:15:06 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_BL_HELPER_CRC_Ini();
|
|
|
|
CRC_ResetDR();
|
|
|
|
CRC_CalcBlockCRC((uint32_t *)bdinfo->fw_base, (bdinfo->fw_size) >> 2);
|
|
|
|
return CRC_GetCRC();
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
void PIOS_BL_HELPER_FLASH_Read_Description(uint8_t *array, uint8_t size)
|
2010-09-27 09:28:45 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
const struct pios_board_info *bdinfo = &pios_board_info_blob;
|
|
|
|
uint8_t x = 0;
|
|
|
|
|
|
|
|
if (size > bdinfo->desc_size) {
|
|
|
|
size = bdinfo->desc_size;
|
|
|
|
}
|
|
|
|
for (uint32_t i = bdinfo->fw_base + bdinfo->fw_size; i < bdinfo->fw_base + bdinfo->fw_size + size; ++i) {
|
|
|
|
array[x] = *PIOS_BL_HELPER_FLASH_If_Read(i);
|
|
|
|
++x;
|
|
|
|
}
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
|
|
|
|
2011-04-23 21:11:17 +02:00
|
|
|
void PIOS_BL_HELPER_CRC_Ini()
|
2010-09-27 09:28:45 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE);
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
2013-03-15 19:25:30 +01:00
|
|
|
|
|
|
|
#endif /* PIOS_INCLUDE_BL_HELPER */
|