]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Bootloader/main.c
Adding McHCK DFU Bootloader
[kiibohd-controller.git] / Bootloader / main.c
1 /* Copyright (c) 2011,2012 Simon Schubert <2@0x2c.org>.
2  * Modifications by Jacob Alexander 2014 <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 // ----- Local Includes -----
19
20 #include "mchck.h"
21 #include "dfu.desc.h"
22
23
24
25 // ----- Variables -----
26
27 /**
28  * Unfortunately we can't DMA directly to FlexRAM, so we'll have to stage here.
29  */
30 static char staging[FLASH_SECTOR_SIZE];
31
32
33
34 // ----- Functions -----
35
36 static enum dfu_status setup_write(size_t off, size_t len, void **buf)
37 {
38         static int last = 0;
39
40         if (len > sizeof(staging))
41                 return (DFU_STATUS_errADDRESS);
42
43         // We only allow the last write to be less than one sector size.
44         if (off == 0)
45                 last = 0;
46         if (last && len != 0)
47                 return (DFU_STATUS_errADDRESS);
48         if (len != FLASH_SECTOR_SIZE) {
49                 last = 1;
50                 memset(staging, 0xff, sizeof(staging));
51         }
52
53         *buf = staging;
54         return (DFU_STATUS_OK);
55 }
56
57 static enum dfu_status finish_write( void *buf, size_t off, size_t len )
58 {
59         void *target;
60         if (len == 0)
61                 return (DFU_STATUS_OK);
62
63         target = flash_get_staging_area(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE);
64         if (!target)
65                 return (DFU_STATUS_errADDRESS);
66         memcpy(target, buf, len);
67         if (flash_program_sector(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE) != 0)
68                 return (DFU_STATUS_errADDRESS);
69         return (DFU_STATUS_OK);
70 }
71
72
73 static struct dfu_ctx dfu_ctx;
74
75 void init_usb_bootloader( int config )
76 {
77         dfu_init(setup_write, finish_write, &dfu_ctx);
78 }
79
80 void main()
81 {
82         // Enabling LED to indicate we are in the bootloader
83         GPIOA_PDDR |= (1<<19);
84         // Setup pin - A19 - See Lib/pin_map.mchck for more details on pins
85         PORTA_PCR19 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
86         GPIOA_PSOR |= (1<<19);
87
88         flash_prepare_flashing();
89
90         usb_init( &dfu_device );
91         for (;;)
92         {
93                 usb_poll();
94         }
95 }
96