]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Bootloader/main.c
Fixing bugs in mk20dx128vlf5 support
[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 int 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         return retval;
84 }
85
86 static enum dfu_status setup_read( size_t off, size_t *len, void **buf )
87 {
88         // Calculate starting address from offset
89         *buf = (void*)&_app_rom + (USB_DFU_TRANSFER_SIZE / 4) * off;
90
91         // Calculate length of transfer
92         /*
93         *len = *buf > (void*)(&_app_rom_end) - USB_DFU_TRANSFER_SIZE
94                 ? 0 : USB_DFU_TRANSFER_SIZE;
95         */
96
97         // Check for error
98         /*
99         if ( *buf > (void*)&_app_rom_end )
100                 return (DFU_STATUS_errADDRESS);
101         */
102
103         return (DFU_STATUS_OK);
104 }
105
106 static enum dfu_status setup_write( size_t off, size_t len, void **buf )
107 {
108         static int last = 0;
109
110         if ( len > sizeof(staging) )
111                 return (DFU_STATUS_errADDRESS);
112
113         // We only allow the last write to be less than one sector size.
114         if ( off == 0 )
115                 last = 0;
116         if ( last && len != 0 )
117                 return (DFU_STATUS_errADDRESS);
118         if ( len != FLASH_SECTOR_SIZE )
119         {
120                 last = 1;
121                 memset( staging, 0xff, sizeof(staging) );
122         }
123
124         *buf = staging;
125         return (DFU_STATUS_OK);
126 }
127
128 static enum dfu_status finish_write( void *buf, size_t off, size_t len )
129 {
130         void *target;
131         if ( len == 0 )
132                 return (DFU_STATUS_OK);
133
134         target = flash_get_staging_area(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE);
135         if ( !target )
136                 return (DFU_STATUS_errADDRESS);
137         memcpy( target, buf, len );
138
139         // Depending on the error return a different status
140         switch ( flash_program_sector(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE) )
141         {
142         /*
143         case FTFL_FSTAT_RDCOLERR: // Flash Read Collision Error
144         case FTFL_FSTAT_ACCERR:   // Flash Access Error
145         case FTFL_FSTAT_FPVIOL:   // Flash Protection Violation Error
146                 return (DFU_STATUS_errADDRESS);
147         case FTFL_FSTAT_MGSTAT0:  // Memory Controller Command Completion Error
148                 return (DFU_STATUS_errADDRESS);
149         */
150
151         case 0:
152         default: // No error
153                 return (DFU_STATUS_OK);
154         }
155 }
156
157
158 static struct dfu_ctx dfu_ctx;
159
160 void init_usb_bootloader( int config )
161 {
162         dfu_init( setup_read, setup_write, finish_write, &dfu_ctx );
163 }
164
165 void main()
166 {
167 #if defined(_mk20dx128vlf5_) // Kiibohd-dfu / Infinity
168         // XXX McHCK uses B16 instead of A19
169
170         // Enabling LED to indicate we are in the bootloader
171         GPIOA_PDDR |= (1<<19);
172         // Setup pin - A19 - See Lib/pin_map.mchck for more details on pins
173         PORTA_PCR19 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
174         GPIOA_PSOR |= (1<<19);
175
176 #elif defined(_mk20dx256vlh7_) // Kiibohd-dfu
177         // Enabling LED to indicate we are in the bootloader
178         GPIOA_PDDR |= (1<<5);
179         // Setup pin - A5 - See Lib/pin_map.mchck for more details on pins
180         PORTA_PCR5 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
181         GPIOA_PSOR |= (1<<5);
182 #else
183 #error "Incompatible chip for bootloader"
184 #endif
185
186         uart_serial_setup();
187         printNL( NL "Bootloader DFU-Mode" );
188
189         // XXX REMOVEME
190         /*
191         GPIOB_PDDR |= (1<<16);
192         PORTB_PCR16 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
193         GPIOB_PSOR |= (1<<16);
194
195         // RST
196         GPIOC_PDDR |= (1<<8);
197         PORTC_PCR8 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
198         GPIOC_PSOR |= (1<<8);
199
200         // CS1B
201         GPIOC_PDDR |= (1<<4);
202         PORTC_PCR4 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
203         GPIOC_PCOR |= (1<<4);
204         */
205         // Backlight
206         /*
207         GPIOC_PDDR |= (1<<1);
208         PORTC_PCR1 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
209         GPIOC_PCOR |= (1<<1);
210         GPIOC_PDDR |= (1<<2);
211         PORTC_PCR2 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
212         GPIOC_PCOR |= (1<<2);
213         GPIOC_PDDR |= (1<<3);
214         PORTC_PCR3 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
215         GPIOC_PCOR |= (1<<3);
216         */
217
218         /*
219         // Read Firmware 1 Status
220         FTFL.fccob.read_1s_block.fcmd = FTFL_FCMD_READ_1s_BLOCK;
221         FTFL.fccob.read_1s_block.addr = (uintptr_t)&_app_rom;
222         FTFL.fccob.read_1s_block.margin = FTFL_MARGIN_NORMAL;
223
224         int retval = ftfl_submit_cmd();
225         print("Firmware Erase Status: ");
226         printHex( retval );
227         print( NL );
228
229
230         // Read Bootloader 1 Status
231         FTFL.fccob.read_1s_block.fcmd = FTFL_FCMD_READ_1s_BLOCK;
232         FTFL.fccob.read_1s_block.addr = (uintptr_t)&_bootloader;
233         FTFL.fccob.read_1s_block.margin = FTFL_MARGIN_NORMAL;
234
235         retval = ftfl_submit_cmd();
236         print("Bootloader Erase Status: ");
237         printHex( retval );
238         print( NL );
239         */
240
241         /*
242         // Program First Longword of firmware
243         FTFL.fccob.program_longword.fcmd = FTFL_FCMD_PROGRAM_LONGWORD;
244         FTFL.fccob.program_longword.addr = (uintptr_t)&_app_rom;
245         FTFL.fccob.program_longword.data_be[0] = 0x1;
246         FTFL.fccob.program_longword.data_be[1] = 0x2;
247         FTFL.fccob.program_longword.data_be[2] = 0x4;
248         FTFL.fccob.program_longword.data_be[3] = 0x8;
249         int retval = ftfl_submit_cmd();
250         print("Write Longword Status: ");
251         printHex( retval );
252         print( NL );
253         */
254
255         /*
256         // Erase Sector
257         FTFL.fccob.erase.fcmd = FTFL_FCMD_ERASE_SECTOR;
258         FTFL.fccob.erase.addr = (uintptr_t)&_app_rom;
259         int retval = ftfl_submit_cmd();
260         print("Erase Status: ");
261         printHex( retval );
262         print( NL );
263
264         // Prepare FlexRAM
265         FTFL.fccob.set_flexram.fcmd = FTFL_FCMD_SET_FLEXRAM;
266         FTFL.fccob.set_flexram.flexram_function = FTFL_FLEXRAM_RAM;
267         retval = ftfl_submit_cmd();
268         print("Set FlexRAM Status: ");
269         printHex( retval );
270         print( NL );
271
272         // Write to FlexRAM
273         memset( FlexRAM, 0xB4, 1000 );
274         memset( &FlexRAM[1000], 0xE3, 1000 );
275
276         // Program Sector
277         FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
278         FTFL.fccob.program_section.addr = (uintptr_t)&_app_rom;
279         FTFL.fccob.program_section.num_words = 128;
280         //FTFL.fccob.program_section.num_words = 250; // 2000 kb / 64 bits
281         retval = ftfl_submit_cmd();
282         print("Program Sector1 Status: ");
283         printHex( retval );
284         print( NL );
285
286         FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
287         FTFL.fccob.program_section.addr = (uintptr_t)&_app_rom + 0x400;
288         FTFL.fccob.program_section.num_words = 128;
289         //FTFL.fccob.program_section.num_words = 250; // 2000 kb / 64 bits
290         retval = ftfl_submit_cmd();
291         print("Program Sector2 Status: ");
292         printHex( retval );
293         print( NL );
294
295         for ( uint8_t sector = 0; sector < 1; sector++ )
296                 //sector_print( &_bootloader, sector, 16 );
297                 sector_print( &_app_rom, sector, 16 );
298         print( NL );
299         */
300
301         flash_prepare_flashing();
302
303         //uint32_t *position = &_app_rom;
304         usb_init( &dfu_device );
305
306         for (;;)
307         {
308                 usb_poll();
309         }
310 }
311