Add bootloader example for sam3.
[bertos.git] / boards / sam3x-ek / examples / sam3x-ek_sd_bootloader / main.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \author Stefano Federico <aleph@develer.com>
34  *
35  * \brief Empty project.
36  *
37  * This is a minimalist project, it just initializes the hardware of the
38  * supported board and proposes an empty main.
39  */
40
41 #include "hw/hw_led.h"
42 #include "hw/hw_sd.h"
43 #include "hw/hw_boot.h"
44
45 // Define log settings for cfg/log.h
46 #define LOG_LEVEL    3
47 #define LOG_FORMAT   0
48 #include <cfg/log.h>
49 #include <cfg/debug.h>
50
51 #include <cpu/irq.h>
52
53 #include <drv/timer.h>
54 #include <drv/lcd_hx8347.h>
55 #include <drv/flash.h>
56 #include <drv/sd.h>
57 #include <drv/dmac_sam3.h>
58
59 #include <io/kblock.h>
60
61 #include <fs/fat.h>
62
63 #include <string.h>
64
65
66 static Flash internal_flash;
67 static KFileBlock flash;
68 static Sd sd;
69
70 FATFS fs;
71 FatFile fw_file;
72
73 uint8_t fw_buf[4096];
74 uint8_t fw_buf1[4096];
75
76 /*
77  * To jump to the application init, we should add this offset plus one.
78  */
79 #define FLASH_INIT_OFFSET  0x13
80 #define FLASH_TRIM_START   FLASH_BOOT_SIZE / FLASH_PAGE_SIZE_BYTES
81 /*
82  * Define pointer function to main program.
83  */
84 void (*rom_start)(void) NORETURN = (void *)(FLASH_BOOT_SIZE + FLASH_INIT_OFFSET);
85
86 #define START_APP() rom_start()
87
88 static void init(void)
89 {
90         kdbg_init();
91
92         IRQ_ENABLE;
93
94         timer_init();
95         dmac_init();
96         LED_INIT();
97
98         flash_init(&internal_flash, 0);
99
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);
103 }
104
105
106 int main(void)
107 {
108         init();
109
110         LED_ON(LED_RED);
111
112         if (SD_CARD_PRESENT())
113         {
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);
117
118                 FRESULT result;
119                 bool sd_ok = sd_init(&sd, NULL, 0);
120                 if (sd_ok)
121                 {
122                         kprintf("Mount FAT filesystem.\n");
123                         result = f_mount(0, &fs);
124                         if (result != FR_OK)
125                         {
126                                 kprintf("Mounting FAT volumes error[%d]\n", result);
127                                 sd_ok = false;
128                                 f_mount(0, NULL);
129                                 goto end;
130                         }
131
132                         if (sd_ok)
133                         {
134                                 result = fatfile_open(&fw_file, "firmware.bin",  FA_READ);
135                                 if (result == FR_OK)
136                                 {
137
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))
141                                         {
142                                                 LOG_ERR("Fw file too large\n");
143                                                 kfile_close(&fw_file.fd);
144                                                 f_mount(0, NULL);
145                                                 goto end;
146                                         }
147
148                                         size_t len;
149                                         while (fw_len)
150                                         {
151                                                 len = MIN(sizeof(fw_buf), fw_len);
152
153                                                 if (kfile_read(&fw_file.fd, fw_buf, len) != len)
154                                                 {
155                                                         LOG_ERR("Error reading fw file\n");
156                                                         kfile_close(&fw_file.fd);
157                                                         f_mount(0, NULL);
158                                                         goto end;
159                                                 }
160
161                                                 if (kfile_read(&flash.fd, fw_buf1, len) != len)
162                                                 {
163                                                         LOG_ERR("Error reading from flash\n");
164                                                         kfile_close(&fw_file.fd);
165                                                         f_mount(0, NULL);
166                                                         goto end;
167                                                 }
168                                                 if (memcmp(fw_buf, fw_buf1, len))
169                                                         break;
170
171                                                 fw_len -= len;
172                                         }
173
174                                         if (fw_len == 0)
175                                         {
176                                                 LOG_INFO("Already up-to date\n");
177                                                 kfile_close(&fw_file.fd);
178                                                 f_mount(0, NULL);
179                                                 goto end;
180                                         }
181
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);
186
187                                         while (fw_len)
188                                         {
189                                                 len = MIN(sizeof(fw_buf), fw_len);
190                                                 if (kfile_read(&fw_file.fd, fw_buf, len) != len)
191                                                 {
192                                                         LOG_ERR("Error reading fw file[%d]\n", len);
193                                                         kfile_close(&fw_file.fd);
194                                                         f_mount(0, NULL);
195                                                         goto end;
196                                                 }
197                                                 if (kfile_write(&flash.fd, fw_buf, len) != len)
198                                                 {
199                                                         LOG_ERR("Error writing flash!\n");
200                                                         kfile_close(&fw_file.fd);
201                                                         f_mount(0, NULL);
202                                                         goto end;
203                                                 }
204
205                                                 fw_len -= len;
206                                         }
207                                         kfile_flush(&flash.fd);
208                                         kfile_close(&fw_file.fd);
209
210                                         f_mount(0, NULL);
211                                         LOG_INFO("Done!\n");
212
213                                 }
214                                 else
215                                 {
216                                         LOG_INFO("firmware file not found\n");
217                                         f_mount(0, NULL);
218                                         goto end;
219                                 }
220                         }
221                 }
222         }
223
224
225 end:
226         //Shut down all peripheral before to jump to application.
227         timer_hw_exit();
228         IRQ_DISABLE;
229
230         EEFC0_FMR = EEFC_FMR_FWS(3);
231         EEFC1_FMR = EEFC_FMR_FWS(3);
232
233         LOG_INFO("Jump to main application.\n");
234         START_APP();
235
236         return 0;
237 }