#include "fatfs.h" #include "string.h" #define RAM_LOADER_BUFFER_SIZE 15*1024 #define RAM_LOADER_ADDRESS 0x38000000 #define APP_START_ADDRESS RAM_LOADER_ADDRESS uint8_t * ram_loader_buffer = (uint8_t *) RAM_LOADER_ADDRESS; void _app_start(void) { __disable_irq(); // Reset USB //De-init all peripherals //HAL_ADC_DeInit(&hadc1); // Disable Systick SysTick->CTRL = 0; SysTick->LOAD = 0; SysTick->VAL = 0; // Reset clock to default HAL_RCC_DeInit(); // Clear all interrupt bits for (uint8_t i = 0; i < sizeof(NVIC->ICER) / sizeof(NVIC->ICER[0]); i++) { NVIC->ICER[i] = 0xFFFFFFFF; NVIC->ICPR[i] = 0xFFFFFFFF; } __enable_irq(); // set the vector table offset register SCB->VTOR = APP_START_ADDRESS; // get the application stack pointer __set_MSP(((uint32_t *)APP_START_ADDRESS)[0]); // jump to the application ((void (*)(void))((uint32_t *)APP_START_ADDRESS)[1])(); // the application should never return // ULOG_ERROR("Failed to start application"); while (1) {} } static uint8_t buffer[512]; void RamLoader_LoadApplication() { // check if the file "load.bin" exists FIL file; FRESULT res = f_open(&file, "load.bin", FA_READ); if (res != FR_OK) { // ULOG_ERROR("Failed to open file load.bin"); return; } UINT size = f_size(&file); // ensure the file is not larger than the buffer if (size > RAM_LOADER_BUFFER_SIZE ) { // ULOG_ERROR("File load.bin is too large"); return; } // read the file into the buffer UINT bytes_read; uint32_t remaining_bytes = size; uint8_t *destination_ptr = ram_loader_buffer; uint8_t *buffer_ptr = buffer; while (remaining_bytes > 0) { UINT read_size = remaining_bytes > 512 ? 512 : remaining_bytes; res = f_read(&file, buffer_ptr, read_size, &bytes_read); if (res != FR_OK) { // ULOG_ERROR("Failed to read file load.bin"); return; } if (bytes_read != read_size) { // ULOG_ERROR("Failed to read file load.bin completely"); return; } memcpy(destination_ptr, buffer_ptr, read_size); remaining_bytes -= read_size; destination_ptr += read_size; } // close the file f_close(&file); // check that the file to flash exists ("firm.bin") res = f_open(&file, "firm.bin", FA_READ); if (res != FR_OK) { // ULOG_ERROR("Failed to open file firm.bin"); return; } //close the file f_close(&file); _app_start(); }