Mercurial > louis > kiibohd-controller
comparison Bootloader/main.c @ 334:9ac304aa1ab5
Adding initial dfu-upload code and debugging for Bootloader.
author | Jacob Alexander <haata@kiibohd.com> |
---|---|
date | Mon, 27 Apr 2015 00:57:34 -0700 |
parents | 9e31d92caf12 |
children | 795cc567b018 |
comparison
equal
deleted
inserted
replaced
333:772f9bea482b | 334:9ac304aa1ab5 |
---|---|
19 | 19 |
20 // Local Includes | 20 // Local Includes |
21 #include "mchck.h" | 21 #include "mchck.h" |
22 #include "dfu.desc.h" | 22 #include "dfu.desc.h" |
23 | 23 |
24 #include "debug.h" | |
25 | |
24 | 26 |
25 | 27 |
26 // ----- Variables ----- | 28 // ----- Variables ----- |
27 | 29 |
28 /** | 30 /** |
32 | 34 |
33 | 35 |
34 | 36 |
35 // ----- Functions ----- | 37 // ----- Functions ----- |
36 | 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 | |
37 static enum dfu_status setup_write( size_t off, size_t len, void **buf ) | 91 static enum dfu_status setup_write( size_t off, size_t len, void **buf ) |
38 { | 92 { |
39 GPIOA_PCOR |= (1<<5); | |
40 static int last = 0; | 93 static int last = 0; |
41 | 94 |
42 if ( len > sizeof(staging) ) | 95 if ( len > sizeof(staging) ) |
43 return (DFU_STATUS_errADDRESS); | 96 return (DFU_STATUS_errADDRESS); |
44 | 97 |
65 | 118 |
66 target = flash_get_staging_area(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE); | 119 target = flash_get_staging_area(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE); |
67 if ( !target ) | 120 if ( !target ) |
68 return (DFU_STATUS_errADDRESS); | 121 return (DFU_STATUS_errADDRESS); |
69 memcpy( target, buf, len ); | 122 memcpy( target, buf, len ); |
123 print("BUF: "); | |
124 printHex( off ); | |
125 sector_print( target, 0, 16 ); | |
70 | 126 |
71 // Depending on the error return a different status | 127 // Depending on the error return a different status |
72 switch ( flash_program_sector(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE) ) | 128 switch ( flash_program_sector(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE) ) |
73 { | 129 { |
74 /* | 130 /* |
89 | 145 |
90 static struct dfu_ctx dfu_ctx; | 146 static struct dfu_ctx dfu_ctx; |
91 | 147 |
92 void init_usb_bootloader( int config ) | 148 void init_usb_bootloader( int config ) |
93 { | 149 { |
94 dfu_init(setup_write, finish_write, &dfu_ctx); | 150 dfu_init( setup_read, setup_write, finish_write, &dfu_ctx ); |
95 } | 151 } |
96 | 152 |
97 void main() | 153 void main() |
98 { | 154 { |
99 #if defined(_mk20dx128vlf5_) // Kiibohd-dfu / Infinity | 155 #if defined(_mk20dx128vlf5_) // Kiibohd-dfu / Infinity |
113 GPIOA_PSOR |= (1<<5); | 169 GPIOA_PSOR |= (1<<5); |
114 #else | 170 #else |
115 #error "Incompatible chip for bootloader" | 171 #error "Incompatible chip for bootloader" |
116 #endif | 172 #endif |
117 | 173 |
118 //for (uint8_t c = 0; c < 20; c++) | 174 uart_serial_setup(); |
119 /* | 175 printNL( NL "Bootloader DFU-Mode" ); |
120 while( 1 ) | 176 |
121 { | 177 // TODO REMOVEME |
122 GPIOA_PTOR |= (1<<5); | 178 for ( uint8_t sector = 0; sector < 3; sector++ ) |
123 for (uint32_t d = 0; d < 7200000; d++ ); | 179 sector_print( &_app_rom, sector, 16 ); |
124 } | 180 print( NL ); |
125 */ | |
126 | 181 |
127 // XXX REMOVEME | 182 // XXX REMOVEME |
128 /* | 183 /* |
129 GPIOB_PDDR |= (1<<16); | 184 GPIOB_PDDR |= (1<<16); |
130 PORTB_PCR16 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); | 185 PORTB_PCR16 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); |
131 GPIOB_PSOR |= (1<<16); | 186 GPIOB_PSOR |= (1<<16); |
132 */ | 187 |
133 // RST | 188 // RST |
134 GPIOC_PDDR |= (1<<8); | 189 GPIOC_PDDR |= (1<<8); |
135 PORTC_PCR8 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); | 190 PORTC_PCR8 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); |
136 GPIOC_PSOR |= (1<<8); | 191 GPIOC_PSOR |= (1<<8); |
137 /* | 192 |
138 // CS1B | 193 // CS1B |
139 GPIOC_PDDR |= (1<<4); | 194 GPIOC_PDDR |= (1<<4); |
140 PORTC_PCR4 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); | 195 PORTC_PCR4 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); |
141 GPIOC_PCOR |= (1<<4); | 196 GPIOC_PCOR |= (1<<4); |
142 */ | 197 */ |
149 GPIOC_PCOR |= (1<<2); | 204 GPIOC_PCOR |= (1<<2); |
150 GPIOC_PDDR |= (1<<3); | 205 GPIOC_PDDR |= (1<<3); |
151 PORTC_PCR3 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); | 206 PORTC_PCR3 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); |
152 GPIOC_PCOR |= (1<<3); | 207 GPIOC_PCOR |= (1<<3); |
153 | 208 |
154 | |
155 | |
156 flash_prepare_flashing(); | 209 flash_prepare_flashing(); |
157 | 210 |
158 uint32_t *position = &_app_rom; | 211 //uint32_t *position = &_app_rom; |
159 usb_init( &dfu_device ); | 212 usb_init( &dfu_device ); |
213 | |
160 for (;;) | 214 for (;;) |
161 { | 215 { |
162 usb_poll(); | 216 usb_poll(); |
163 | 217 } |
164 /* | 218 } |
165 for ( ; position < &_app_rom + 0x201; position++ ) | 219 |
166 //for ( ; position < &_app_rom + 0x800; position++ ) | |
167 { | |
168 if ( *position != 0xFFFFFFFF ) | |
169 { | |
170 while( 1 ) | |
171 { | |
172 GPIOA_PTOR |= (1<<5); | |
173 for (uint32_t d = 0; d < 7200000; d++ ); | |
174 } | |
175 } | |
176 } | |
177 */ | |
178 | |
179 } | |
180 } | |
181 |