Skip to content

Commit

Permalink
added bootloader
Browse files Browse the repository at this point in the history
  • Loading branch information
robotsrulz committed Jun 28, 2017
1 parent 7de6b8d commit c08074a
Show file tree
Hide file tree
Showing 10 changed files with 1,689 additions and 49 deletions.
76 changes: 76 additions & 0 deletions Bootloader/boot_conf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
******************************************************************************
* File Name : boot_conf.h
* Description : This file contains firmware configuration
******************************************************************************
*
* COPYRIGHT(c) 2017 Roman Stepanov
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __BOOT_CONF_H
#define __BOOT_CONF_H

#include "stm32f1xx_hal.h"
#include "fatfs.h"

#if defined(STM32F107xC) && defined(MKS_TFT)
/**
* Makerbase MKS-TFT32
*/
#define SPEAKER_Pin GPIO_PIN_2
#define SPEAKER_GPIO_Port GPIOA
#define SDCARD_nCS_Pin GPIO_PIN_11
#define SDCARD_nCS_GPIO_Port GPIOD
#define FLASH_nCS_Pin GPIO_PIN_9
#define FLASH_nCS_GPIO_Port GPIOB

extern FATFS sdFileSystem; // 0:/

#elif defined(STM32F103xE) && defined(CZMINI)

#endif

typedef enum
{
FLASH_RESULT_OK = 0,
FLASH_RESULT_FILE_ERROR,
FLASH_RESULT_FLASH_ERROR,
FLASH_FILE_NOT_EXISTS,
FLASH_FILE_CANNOT_OPEN,
FLASH_FILE_INVALID_HASH,
FLASH_FILE_TOO_BIG
} FlashResult;

FlashResult flash(const char *fname);
extern const uint32_t *mcuFirstPageAddr;

typedef void (*Callable)();

#define MAIN_PR_OFFSET 0x8000

#endif /* __BOOT_CONF_H */
/************************ (C) COPYRIGHT Roman Stepanov *****END OF FILE****/
114 changes: 114 additions & 0 deletions Bootloader/flash.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@

#include "stm32f1xx_hal.h"
#include "boot_conf.h"

extern unsigned int _isr_real;
const uint32_t *mcuFirstPageAddr = (const uint32_t *) 0x8000000 + MAIN_PR_OFFSET;

// uint32_t mcuLastPageAddr = ((uint32_t) &_isr_real) - FLASH_PAGE_SIZE;

uint8_t buffer[FLASH_PAGE_SIZE];
uint32_t bufferLen = 0;

static FIL fil;
static FILINFO info;

// uint32_t getMaxFlashImageSize()
// {
// return (uint32_t) &_isr_real - mcuFirstPageAddr;
// }

HAL_StatusTypeDef erase(uint32_t from, uint32_t to)
{
HAL_StatusTypeDef res = HAL_OK;

for (uint32_t i = from; i <= to; i += FLASH_PAGE_SIZE)
{
FLASH_EraseInitTypeDef erase;

erase.TypeErase = FLASH_TYPEERASE_PAGES;
erase.Banks = FLASH_BANK_1;
erase.PageAddress = i;
erase.NbPages = 1;

uint32_t error = 0;
res = HAL_FLASHEx_Erase(&erase, &error);

if (res != HAL_OK) {
return res;
}
}
return res;
}

FRESULT readNextPage(uint8_t *target, uint32_t *read)
{
uint16_t blocksCount = 16;
uint16_t fileBlock = FLASH_PAGE_SIZE / blocksCount;

*read = 0;
UINT readNow = 0;
FRESULT res = FR_OK;

for (uint16_t i = 0; i<blocksCount; i++)
{
res = f_read(&fil, target, fileBlock, &readNow);

target += readNow;
*read += readNow;

if (res != FR_OK || readNow != fileBlock)
break;
}
return res;
}

HAL_StatusTypeDef flashWrite(uint32_t position, uint8_t *data, uint32_t size)
{
HAL_StatusTypeDef res = HAL_OK;
for (uint32_t i=0; i<size; i+=2)
{
res = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, position + i, *(uint16_t*)&data[i]);
if (res != HAL_OK)
break;
}
return res;
}

FlashResult flash(const char *fname)
{
FRESULT res = f_stat(fname, &info);
if (res != FR_OK)
return FLASH_FILE_NOT_EXISTS;

// Checking file size
// if (info.fsize > getMaxFlashImageSize())
// return FLASH_FILE_TOO_BIG;

res = f_open(&fil, fname, FA_OPEN_EXISTING | FA_READ);

if (res != FR_OK)
return FLASH_RESULT_FILE_ERROR;

uint32_t position = (uint32_t) mcuFirstPageAddr;

if (HAL_OK != HAL_FLASH_Unlock())
return FLASH_RESULT_FLASH_ERROR;

if (HAL_OK != erase((uint32_t) mcuFirstPageAddr, info.fsize + (uint32_t)mcuFirstPageAddr))
return FLASH_RESULT_FLASH_ERROR;

do {
readNextPage(buffer, &bufferLen);
if (HAL_OK != flashWrite(position, buffer, bufferLen))
return FLASH_RESULT_FLASH_ERROR;

position += bufferLen;
} while (bufferLen != 0);

f_close(&fil);
HAL_FLASH_Lock();

return FR_OK;
}

Loading

0 comments on commit c08074a

Please sign in to comment.