]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Bootloader/main.c
Merge branch 'master' of https://github.com/smasher816/controller into smasher816...
[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
25
26 // ----- Variables -----
27
28 /**
29  * Unfortunately we can't DMA directly to FlexRAM, so we'll have to stage here.
30  */
31 static char staging[FLASH_SECTOR_SIZE];
32
33
34
35 // ----- Functions -----
36
37 static enum dfu_status setup_write(size_t off, size_t len, void **buf)
38 {
39         static int last = 0;
40
41         if (len > sizeof(staging))
42                 return (DFU_STATUS_errADDRESS);
43
44         // We only allow the last write to be less than one sector size.
45         if (off == 0)
46                 last = 0;
47         if (last && len != 0)
48                 return (DFU_STATUS_errADDRESS);
49         if (len != FLASH_SECTOR_SIZE) {
50                 last = 1;
51                 memset(staging, 0xff, sizeof(staging));
52         }
53
54         *buf = staging;
55         return (DFU_STATUS_OK);
56 }
57
58 static enum dfu_status finish_write( void *buf, size_t off, size_t len )
59 {
60         void *target;
61         if (len == 0)
62                 return (DFU_STATUS_OK);
63
64         target = flash_get_staging_area(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE);
65         if (!target)
66                 return (DFU_STATUS_errADDRESS);
67         memcpy(target, buf, len);
68         if (flash_program_sector(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE) != 0)
69                 return (DFU_STATUS_errADDRESS);
70         return (DFU_STATUS_OK);
71 }
72
73
74 static struct dfu_ctx dfu_ctx;
75
76 void init_usb_bootloader( int config )
77 {
78         dfu_init(setup_write, finish_write, &dfu_ctx);
79 }
80
81 void main()
82 {
83 #if defined(_mk20dx128vlf5_) // Kiibohd-dfu / Infinity
84         // XXX McHCK uses B16 instead of A19
85
86         // Enabling LED to indicate we are in the bootloader
87         GPIOA_PDDR |= (1<<19);
88         // Setup pin - A19 - See Lib/pin_map.mchck for more details on pins
89         PORTA_PCR19 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
90         GPIOA_PSOR |= (1<<19);
91
92 #elif defined(_mk20dx256vlh7_) // Kiibohd-dfu
93         // Enabling LED to indicate we are in the bootloader
94         GPIOA_PDDR |= (1<<5);
95         // Setup pin - A5 - See Lib/pin_map.mchck for more details on pins
96         PORTA_PCR19 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
97         GPIOA_PSOR |= (1<<5);
98
99 #endif
100
101         flash_prepare_flashing();
102
103         usb_init( &dfu_device );
104         for (;;)
105         {
106                 usb_poll();
107         }
108 }
109