]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Bootloader/main.c
Adding initial dfu-upload code and debugging for Bootloader.
[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[ FLASH_SECTOR_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         print( NL );
46         print("Block ");
47         printHex( sector );
48         print(" ");
49         printHex( (size_t)start );
50         print(" -> ");
51         printHex( (size_t)end );
52         print( NL );
53
54         // Display sector
55         for ( size_t line = 0; pos < end - 24; line++ )
56         {
57                 // Each Line
58                 printHex_op( (size_t)pos, 4 );
59                 print(" ");
60
61                 // Each 2 byte chunk
62                 for ( size_t chunk = 0; chunk < chunks; chunk++ )
63                 {
64                         // Print out the two bytes (second one first)
65                         printHex_op( *(pos + 1), 2 );
66                         printHex_op( *pos, 2 );
67                         print(" ");
68                         pos += 2;
69                 }
70
71                 print( NL );
72         }
73 }
74
75 static enum dfu_status setup_read( size_t off, size_t *len, void **buf )
76 {
77         // Calculate starting address from offset
78         *buf = (void*)&_app_rom + (USB_DFU_TRANSFER_SIZE / 4) * off;
79
80         // Calculate length of transfer
81         *len = *buf > (void*)(&_app_rom_end) - USB_DFU_TRANSFER_SIZE
82                 ? 0 : USB_DFU_TRANSFER_SIZE;
83
84         // Check for error
85         if ( *buf > (void*)&_app_rom_end )
86                 return (DFU_STATUS_errADDRESS);
87
88         return (DFU_STATUS_OK);
89 }
90
91 static enum dfu_status setup_write( size_t off, size_t len, void **buf )
92 {
93         static int last = 0;
94
95         if ( len > sizeof(staging) )
96                 return (DFU_STATUS_errADDRESS);
97
98         // We only allow the last write to be less than one sector size.
99         if ( off == 0 )
100                 last = 0;
101         if ( last && len != 0 )
102                 return (DFU_STATUS_errADDRESS);
103         if ( len != FLASH_SECTOR_SIZE )
104         {
105                 last = 1;
106                 memset( staging, 0xff, sizeof(staging) );
107         }
108
109         *buf = staging;
110         return (DFU_STATUS_OK);
111 }
112
113 static enum dfu_status finish_write( void *buf, size_t off, size_t len )
114 {
115         void *target;
116         if ( len == 0 )
117                 return (DFU_STATUS_OK);
118
119         target = flash_get_staging_area(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE);
120         if ( !target )
121                 return (DFU_STATUS_errADDRESS);
122         memcpy( target, buf, len );
123         print("BUF: ");
124         printHex( off );
125         sector_print( target, 0, 16 );
126
127         // Depending on the error return a different status
128         switch ( flash_program_sector(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE) )
129         {
130         /*
131         case FTFL_FSTAT_RDCOLERR: // Flash Read Collision Error
132         case FTFL_FSTAT_ACCERR:   // Flash Access Error
133         case FTFL_FSTAT_FPVIOL:   // Flash Protection Violation Error
134                 return (DFU_STATUS_errADDRESS);
135         case FTFL_FSTAT_MGSTAT0:  // Memory Controller Command Completion Error
136                 return (DFU_STATUS_errADDRESS);
137         */
138
139         case 0:
140         default: // No error
141                 return (DFU_STATUS_OK);
142         }
143 }
144
145
146 static struct dfu_ctx dfu_ctx;
147
148 void init_usb_bootloader( int config )
149 {
150         dfu_init( setup_read, setup_write, finish_write, &dfu_ctx );
151 }
152
153 void main()
154 {
155 #if defined(_mk20dx128vlf5_) // Kiibohd-dfu / Infinity
156         // XXX McHCK uses B16 instead of A19
157
158         // Enabling LED to indicate we are in the bootloader
159         GPIOA_PDDR |= (1<<19);
160         // Setup pin - A19 - See Lib/pin_map.mchck for more details on pins
161         PORTA_PCR19 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
162         GPIOA_PSOR |= (1<<19);
163
164 #elif defined(_mk20dx256vlh7_) // Kiibohd-dfu
165         // Enabling LED to indicate we are in the bootloader
166         GPIOA_PDDR |= (1<<5);
167         // Setup pin - A5 - See Lib/pin_map.mchck for more details on pins
168         PORTA_PCR5 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
169         GPIOA_PSOR |= (1<<5);
170 #else
171 #error "Incompatible chip for bootloader"
172 #endif
173
174         uart_serial_setup();
175         printNL( NL "Bootloader DFU-Mode" );
176
177         // TODO REMOVEME
178         for ( uint8_t sector = 0; sector < 3; sector++ )
179                 sector_print( &_app_rom, sector, 16 );
180         print( NL );
181
182         // XXX REMOVEME
183         /*
184         GPIOB_PDDR |= (1<<16);
185         PORTB_PCR16 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
186         GPIOB_PSOR |= (1<<16);
187
188         // RST
189         GPIOC_PDDR |= (1<<8);
190         PORTC_PCR8 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
191         GPIOC_PSOR |= (1<<8);
192
193         // CS1B
194         GPIOC_PDDR |= (1<<4);
195         PORTC_PCR4 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
196         GPIOC_PCOR |= (1<<4);
197         */
198         // Backlight
199         GPIOC_PDDR |= (1<<1);
200         PORTC_PCR1 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
201         GPIOC_PCOR |= (1<<1);
202         GPIOC_PDDR |= (1<<2);
203         PORTC_PCR2 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
204         GPIOC_PCOR |= (1<<2);
205         GPIOC_PDDR |= (1<<3);
206         PORTC_PCR3 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
207         GPIOC_PCOR |= (1<<3);
208
209         flash_prepare_flashing();
210
211         //uint32_t *position = &_app_rom;
212         usb_init( &dfu_device );
213
214         for (;;)
215         {
216                 usb_poll();
217         }
218 }
219