]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Bootloader/main.c
Basic code for mk20dx256vlh7 flashing
[kiibohd-controller.git] / Bootloader / main.c
1 /* Copyright (c) 2011,2012 Simon Schubert <2@0x2c.org>.
2  * Modifications by Jacob Alexander 2014-2015 <haata@kiibohd.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 // ----- Includes -----
19
20 // Local Includes
21 #include "mchck.h"
22 #include "dfu.desc.h"
23
24 #include "debug.h"
25
26
27
28 // ----- Variables -----
29
30 /**
31  * Unfortunately we can't DMA directly to FlexRAM, so we'll have to stage here.
32  */
33 static char staging[ USB_DFU_TRANSFER_SIZE ];
34
35
36
37 // ----- Functions -----
38
39 void sector_print( void* buf, size_t sector, size_t chunks )
40 {
41         uint8_t* start = (uint8_t*)buf + sector * USB_DFU_TRANSFER_SIZE;
42         uint8_t* end = (uint8_t*)buf + (sector + 1) * USB_DFU_TRANSFER_SIZE;
43         uint8_t* pos = start;
44
45         // Verify if sector erased
46         FTFL.fccob.read_1s_section.fcmd = FTFL_FCMD_READ_1s_SECTION;
47         FTFL.fccob.read_1s_section.addr = (uintptr_t)start;
48         FTFL.fccob.read_1s_section.margin = FTFL_MARGIN_NORMAL;
49         FTFL.fccob.read_1s_section.num_words = 250; // 2000 kB / 64 bits
50         int retval = ftfl_submit_cmd();
51
52         print( NL );
53         print("Block ");
54         printHex( sector );
55         print(" ");
56         printHex( (size_t)start );
57         print(" -> ");
58         printHex( (size_t)end );
59         print(" Erased: ");
60         printHex( retval );
61         print( NL );
62
63         // Display sector
64         for ( size_t line = 0; pos < end - 24; line++ )
65         {
66                 // Each Line
67                 printHex_op( (size_t)pos, 4 );
68                 print(": ");
69
70                 // Each 2 byte chunk
71                 for ( size_t chunk = 0; chunk < chunks; chunk++ )
72                 {
73                         // Print out the two bytes (second one first)
74                         printHex_op( *(pos + 1), 2 );
75                         printHex_op( *pos, 2 );
76                         print(" ");
77                         pos += 2;
78                 }
79
80                 print( NL );
81         }
82 }
83
84 static enum dfu_status setup_read( size_t off, size_t *len, void **buf )
85 {
86         // Calculate starting address from offset
87         *buf = (void*)&_app_rom + (USB_DFU_TRANSFER_SIZE / 4) * off;
88
89         // Calculate length of transfer
90         /*
91         *len = *buf > (void*)(&_app_rom_end) - USB_DFU_TRANSFER_SIZE
92                 ? 0 : USB_DFU_TRANSFER_SIZE;
93         */
94
95         // Check for error
96         /*
97         if ( *buf > (void*)&_app_rom_end )
98                 return (DFU_STATUS_errADDRESS);
99         */
100
101         return (DFU_STATUS_OK);
102 }
103
104 static enum dfu_status setup_write( size_t off, size_t len, void **buf )
105 {
106         static int last = 0;
107
108         if ( len > sizeof(staging) )
109                 return (DFU_STATUS_errADDRESS);
110
111         // We only allow the last write to be less than one sector size.
112         if ( off == 0 )
113                 last = 0;
114         if ( last && len != 0 )
115                 return (DFU_STATUS_errADDRESS);
116         if ( len != FLASH_SECTOR_SIZE )
117         {
118                 last = 1;
119                 memset( staging, 0xff, sizeof(staging) );
120         }
121
122         *buf = staging;
123         return (DFU_STATUS_OK);
124 }
125
126 static enum dfu_status finish_write( void *buf, size_t off, size_t len )
127 {
128         void *target;
129         if ( len == 0 )
130                 return (DFU_STATUS_OK);
131
132         target = flash_get_staging_area(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE);
133         if ( !target )
134                 return (DFU_STATUS_errADDRESS);
135         memcpy( target, buf, len );
136         print("BUF: ");
137         printHex( off );
138         sector_print( target, 0, 16 );
139
140         // Depending on the error return a different status
141         switch ( flash_program_sector(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE) )
142         {
143         /*
144         case FTFL_FSTAT_RDCOLERR: // Flash Read Collision Error
145         case FTFL_FSTAT_ACCERR:   // Flash Access Error
146         case FTFL_FSTAT_FPVIOL:   // Flash Protection Violation Error
147                 return (DFU_STATUS_errADDRESS);
148         case FTFL_FSTAT_MGSTAT0:  // Memory Controller Command Completion Error
149                 return (DFU_STATUS_errADDRESS);
150         */
151
152         case 0:
153         default: // No error
154                 return (DFU_STATUS_OK);
155         }
156 }
157
158
159 static struct dfu_ctx dfu_ctx;
160
161 void init_usb_bootloader( int config )
162 {
163         dfu_init( setup_read, setup_write, finish_write, &dfu_ctx );
164 }
165
166 void main()
167 {
168 #if defined(_mk20dx128vlf5_) // Kiibohd-dfu / Infinity
169         // XXX McHCK uses B16 instead of A19
170
171         // Enabling LED to indicate we are in the bootloader
172         GPIOA_PDDR |= (1<<19);
173         // Setup pin - A19 - See Lib/pin_map.mchck for more details on pins
174         PORTA_PCR19 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
175         GPIOA_PSOR |= (1<<19);
176
177 #elif defined(_mk20dx256vlh7_) // Kiibohd-dfu
178         // Enabling LED to indicate we are in the bootloader
179         GPIOA_PDDR |= (1<<5);
180         // Setup pin - A5 - See Lib/pin_map.mchck for more details on pins
181         PORTA_PCR5 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
182         GPIOA_PSOR |= (1<<5);
183 #else
184 #error "Incompatible chip for bootloader"
185 #endif
186
187         uart_serial_setup();
188         printNL( NL "Bootloader DFU-Mode" );
189
190         // XXX REMOVEME
191         /*
192         GPIOB_PDDR |= (1<<16);
193         PORTB_PCR16 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
194         GPIOB_PSOR |= (1<<16);
195
196         // RST
197         GPIOC_PDDR |= (1<<8);
198         PORTC_PCR8 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
199         GPIOC_PSOR |= (1<<8);
200
201         // CS1B
202         GPIOC_PDDR |= (1<<4);
203         PORTC_PCR4 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
204         GPIOC_PCOR |= (1<<4);
205         */
206         // Backlight
207         /*
208         GPIOC_PDDR |= (1<<1);
209         PORTC_PCR1 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
210         GPIOC_PCOR |= (1<<1);
211         GPIOC_PDDR |= (1<<2);
212         PORTC_PCR2 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
213         GPIOC_PCOR |= (1<<2);
214         GPIOC_PDDR |= (1<<3);
215         PORTC_PCR3 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
216         GPIOC_PCOR |= (1<<3);
217         */
218
219         /*
220         // Read Firmware 1 Status
221         FTFL.fccob.read_1s_block.fcmd = FTFL_FCMD_READ_1s_BLOCK;
222         FTFL.fccob.read_1s_block.addr = (uintptr_t)&_app_rom;
223         FTFL.fccob.read_1s_block.margin = FTFL_MARGIN_NORMAL;
224
225         int retval = ftfl_submit_cmd();
226         print("Firmware Erase Status: ");
227         printHex( retval );
228         print( NL );
229
230
231         // Read Bootloader 1 Status
232         FTFL.fccob.read_1s_block.fcmd = FTFL_FCMD_READ_1s_BLOCK;
233         FTFL.fccob.read_1s_block.addr = (uintptr_t)&_bootloader;
234         FTFL.fccob.read_1s_block.margin = FTFL_MARGIN_NORMAL;
235
236         retval = ftfl_submit_cmd();
237         print("Bootloader Erase Status: ");
238         printHex( retval );
239         print( NL );
240         */
241
242         /*
243         // Program First Longword of firmware
244         FTFL.fccob.program_longword.fcmd = FTFL_FCMD_PROGRAM_LONGWORD;
245         FTFL.fccob.program_longword.addr = (uintptr_t)&_app_rom;
246         FTFL.fccob.program_longword.data_be[0] = 0x1;
247         FTFL.fccob.program_longword.data_be[1] = 0x2;
248         FTFL.fccob.program_longword.data_be[2] = 0x4;
249         FTFL.fccob.program_longword.data_be[3] = 0x8;
250         int retval = ftfl_submit_cmd();
251         print("Write Longword Status: ");
252         printHex( retval );
253         print( NL );
254         */
255
256         /*
257         // Erase Sector
258         FTFL.fccob.erase.fcmd = FTFL_FCMD_ERASE_SECTOR;
259         FTFL.fccob.erase.addr = (uintptr_t)&_app_rom;
260         int retval = ftfl_submit_cmd();
261         print("Erase Status: ");
262         printHex( retval );
263         print( NL );
264
265         // Prepare FlexRAM
266         FTFL.fccob.set_flexram.fcmd = FTFL_FCMD_SET_FLEXRAM;
267         FTFL.fccob.set_flexram.flexram_function = FTFL_FLEXRAM_RAM;
268         retval = ftfl_submit_cmd();
269         print("Set FlexRAM Status: ");
270         printHex( retval );
271         print( NL );
272
273         // Write to FlexRAM
274         memset( FlexRAM, 0xB4, 1000 );
275         memset( &FlexRAM[1000], 0xE3, 1000 );
276
277         // Program Sector
278         FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
279         FTFL.fccob.program_section.addr = (uintptr_t)&_app_rom;
280         FTFL.fccob.program_section.num_words = 128;
281         //FTFL.fccob.program_section.num_words = 250; // 2000 kb / 64 bits
282         retval = ftfl_submit_cmd();
283         print("Program Sector1 Status: ");
284         printHex( retval );
285         print( NL );
286
287         FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
288         FTFL.fccob.program_section.addr = (uintptr_t)&_app_rom + 0x400;
289         FTFL.fccob.program_section.num_words = 128;
290         //FTFL.fccob.program_section.num_words = 250; // 2000 kb / 64 bits
291         retval = ftfl_submit_cmd();
292         print("Program Sector2 Status: ");
293         printHex( retval );
294         print( NL );
295
296         for ( uint8_t sector = 0; sector < 1; sector++ )
297                 //sector_print( &_bootloader, sector, 16 );
298                 sector_print( &_app_rom, sector, 16 );
299         print( NL );
300         */
301
302         flash_prepare_flashing();
303
304         //uint32_t *position = &_app_rom;
305         usb_init( &dfu_device );
306
307         for (;;)
308         {
309                 usb_poll();
310         }
311 }
312