1 /* Copyright (c) 2011,2012 Simon Schubert <2@0x2c.org>.
2 * Modifications by Jacob Alexander 2014-2015 <haata@kiibohd.com>
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.
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.
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/>.
18 // ----- Includes -----
26 // ----- Variables -----
29 * Unfortunately we can't DMA directly to FlexRAM, so we'll have to stage here.
31 static char staging[ FLASH_SECTOR_SIZE ];
35 // ----- Functions -----
37 static enum dfu_status setup_write( size_t off, size_t len, void **buf )
42 if ( len > sizeof(staging) )
43 return (DFU_STATUS_errADDRESS);
45 // We only allow the last write to be less than one sector size.
48 if ( last && len != 0 )
49 return (DFU_STATUS_errADDRESS);
50 if ( len != FLASH_SECTOR_SIZE )
53 memset( staging, 0xff, sizeof(staging) );
57 return (DFU_STATUS_OK);
60 static enum dfu_status finish_write( void *buf, size_t off, size_t len )
64 return (DFU_STATUS_OK);
66 target = flash_get_staging_area(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE);
68 return (DFU_STATUS_errADDRESS);
69 memcpy( target, buf, len );
71 // Depending on the error return a different status
72 switch ( flash_program_sector(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE) )
75 case FTFL_FSTAT_RDCOLERR: // Flash Read Collision Error
76 case FTFL_FSTAT_ACCERR: // Flash Access Error
77 case FTFL_FSTAT_FPVIOL: // Flash Protection Violation Error
78 return (DFU_STATUS_errADDRESS);
79 case FTFL_FSTAT_MGSTAT0: // Memory Controller Command Completion Error
80 return (DFU_STATUS_errADDRESS);
85 return (DFU_STATUS_OK);
90 static struct dfu_ctx dfu_ctx;
92 void init_usb_bootloader( int config )
94 dfu_init(setup_write, finish_write, &dfu_ctx);
99 #if defined(_mk20dx128vlf5_) // Kiibohd-dfu / Infinity
100 // XXX McHCK uses B16 instead of A19
102 // Enabling LED to indicate we are in the bootloader
103 GPIOA_PDDR |= (1<<19);
104 // Setup pin - A19 - See Lib/pin_map.mchck for more details on pins
105 PORTA_PCR19 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
106 GPIOA_PSOR |= (1<<19);
108 #elif defined(_mk20dx256vlh7_) // Kiibohd-dfu
109 // Enabling LED to indicate we are in the bootloader
110 GPIOA_PDDR |= (1<<5);
111 // Setup pin - A5 - See Lib/pin_map.mchck for more details on pins
112 PORTA_PCR5 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
113 GPIOA_PSOR |= (1<<5);
115 #error "Incompatible chip for bootloader"
118 //for (uint8_t c = 0; c < 20; c++)
122 GPIOA_PTOR |= (1<<5);
123 for (uint32_t d = 0; d < 7200000; d++ );
129 GPIOB_PDDR |= (1<<16);
130 PORTB_PCR16 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
131 GPIOB_PSOR |= (1<<16);
134 flash_prepare_flashing();
136 uint32_t *position = &_app_rom;
137 usb_init( &dfu_device );
143 for ( ; position < &_app_rom + 0x201; position++ )
144 //for ( ; position < &_app_rom + 0x800; position++ )
146 if ( *position != 0xFFFFFFFF )
150 GPIOA_PTOR |= (1<<5);
151 for (uint32_t d = 0; d < 7200000; d++ );