4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
33 * \author Stefano Federico <aleph@develer.com>
35 * \brief Empty project.
37 * This is a minimalist project, it just initializes the hardware of the
38 * supported board and proposes an empty main.
41 #include "hw/hw_led.h"
43 #include "hw/hw_boot.h"
45 // Define log settings for cfg/log.h
49 #include <cfg/debug.h>
53 #include <drv/timer.h>
54 #include <drv/lcd_hx8347.h>
55 #include <drv/flash.h>
57 #include <drv/dmac_sam3.h>
59 #include <io/kblock.h>
66 static Flash internal_flash;
67 static KFileBlock flash;
74 uint8_t fw_buf1[4096];
77 * To jump to the application init, we should add this offset plus one.
79 #define FLASH_INIT_OFFSET 0x13
80 #define FLASH_TRIM_START FLASH_BOOT_SIZE / FLASH_PAGE_SIZE_BYTES
82 * Define pointer function to main program.
84 void (*rom_start)(void) NORETURN = (void *)(FLASH_BOOT_SIZE + FLASH_INIT_OFFSET);
86 #define START_APP() rom_start()
88 static void init(void)
98 flash_init(&internal_flash, 0);
100 kprintf("Trim start: %d, blocks: %ld\n", FLASH_TRIM_START, internal_flash.blk.blk_cnt - FLASH_TRIM_START);
101 kblock_trim(&internal_flash.blk, FLASH_TRIM_START, internal_flash.blk.blk_cnt - FLASH_TRIM_START);
102 kfileblock_init(&flash, &internal_flash.blk);
112 if (SD_CARD_PRESENT())
114 // There is an hardware bug, so to flash correctly we access to flash more slowing.
115 EEFC0_FMR = EEFC_FMR_FWS(7);
116 EEFC1_FMR = EEFC_FMR_FWS(7);
119 bool sd_ok = sd_init(&sd, NULL, 0);
122 kprintf("Mount FAT filesystem.\n");
123 result = f_mount(0, &fs);
126 kprintf("Mounting FAT volumes error[%d]\n", result);
134 result = fatfile_open(&fw_file, "firmware.bin", FA_READ);
138 LOG_INFO("Firmware file found, checking for update...\n");
139 size_t fw_len = fw_file.fat_file.fsize;
140 if (fw_len > (kfile_off_t)(FLASH_MEM_SIZE - FLASH_BOOT_SIZE))
142 LOG_ERR("Fw file too large\n");
143 kfile_close(&fw_file.fd);
151 len = MIN(sizeof(fw_buf), fw_len);
153 if (kfile_read(&fw_file.fd, fw_buf, len) != len)
155 LOG_ERR("Error reading fw file\n");
156 kfile_close(&fw_file.fd);
161 if (kfile_read(&flash.fd, fw_buf1, len) != len)
163 LOG_ERR("Error reading from flash\n");
164 kfile_close(&fw_file.fd);
168 if (memcmp(fw_buf, fw_buf1, len))
176 LOG_INFO("Already up-to date\n");
177 kfile_close(&fw_file.fd);
182 LOG_INFO("Firmware file differs from memory, reprogramming...\n");
183 fw_len = fw_file.fat_file.fsize;
184 kfile_seek(&fw_file.fd, 0, KSM_SEEK_SET);
185 kfile_seek(&flash.fd, 0, KSM_SEEK_SET);
189 len = MIN(sizeof(fw_buf), fw_len);
190 if (kfile_read(&fw_file.fd, fw_buf, len) != len)
192 LOG_ERR("Error reading fw file[%d]\n", len);
193 kfile_close(&fw_file.fd);
197 if (kfile_write(&flash.fd, fw_buf, len) != len)
199 LOG_ERR("Error writing flash!\n");
200 kfile_close(&fw_file.fd);
207 kfile_flush(&flash.fd);
208 kfile_close(&fw_file.fd);
216 LOG_INFO("firmware file not found\n");
226 //Shut down all peripheral before to jump to application.
230 EEFC0_FMR = EEFC_FMR_FWS(3);
231 EEFC1_FMR = EEFC_FMR_FWS(3);
233 LOG_INFO("Jump to main application.\n");