sanguino/firmwares/arduino-usbdfu/Arduino-usbdfu.c

Thu, 07 Jul 2016 12:23:34 +0200

author
mbayer
date
Thu, 07 Jul 2016 12:23:34 +0200
changeset 2
b373b0288715
permissions
-rw-r--r--

added missing sanguino files

2
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1 /*
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
2 LUFA Library
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
3 Copyright (C) Dean Camera, 2010.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
4
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
5 dean [at] fourwalledcubicle [dot] com
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
6 www.fourwalledcubicle.com
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
7 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
8
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
9 /*
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
11
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
12 Permission to use, copy, modify, distribute, and sell this
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
13 software and its documentation for any purpose is hereby granted
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
14 without fee, provided that the above copyright notice appear in
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
15 all copies and that both that the copyright notice and this
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
16 permission notice and warranty disclaimer appear in supporting
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
17 documentation, and that the name of the author not be used in
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
18 advertising or publicity pertaining to distribution of the
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
19 software without specific, written prior permission.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
20
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
21 The author disclaim all warranties with regard to this
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
22 software, including all implied warranties of merchantability
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
23 and fitness. In no event shall the author be liable for any
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
24 special, indirect or consequential damages or any damages
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
25 whatsoever resulting from loss of use, data or profits, whether
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
26 in an action of contract, negligence or other tortious action,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
27 arising out of or in connection with the use or performance of
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
28 this software.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
29 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
30
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
31 /** \file
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
32 *
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
33 * Main source file for the DFU class bootloader. This file contains the complete bootloader logic.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
34 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
35
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
36 #define INCLUDE_FROM_BOOTLOADER_C
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
37 #include "Arduino-usbdfu.h"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
38
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
39 /** Flag to indicate if the bootloader should be running, or should exit and allow the application code to run
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
40 * via a soft reset. When cleared, the bootloader will abort, the USB interface will shut down and the application
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
41 * jumped to via an indirect jump to location 0x0000 (or other location specified by the host).
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
42 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
43 bool RunBootloader = true;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
44
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
45 /** Flag to indicate if the bootloader is waiting to exit. When the host requests the bootloader to exit and
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
46 * jump to the application address it specifies, it sends two sequential commands which must be properly
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
47 * acknowledged. Upon reception of the first the RunBootloader flag is cleared and the WaitForExit flag is set,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
48 * causing the bootloader to wait for the final exit command before shutting down.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
49 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
50 bool WaitForExit = false;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
51
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
52 /** Current DFU state machine state, one of the values in the DFU_State_t enum. */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
53 uint8_t DFU_State = dfuIDLE;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
54
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
55 /** Status code of the last executed DFU command. This is set to one of the values in the DFU_Status_t enum after
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
56 * each operation, and returned to the host when a Get Status DFU request is issued.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
57 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
58 uint8_t DFU_Status = OK;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
59
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
60 /** Data containing the DFU command sent from the host. */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
61 DFU_Command_t SentCommand;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
62
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
63 /** Response to the last issued Read Data DFU command. Unlike other DFU commands, the read command
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
64 * requires a single byte response from the bootloader containing the read data when the next DFU_UPLOAD command
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
65 * is issued by the host.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
66 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
67 uint8_t ResponseByte;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
68
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
69 /** Pointer to the start of the user application. By default this is 0x0000 (the reset vector), however the host
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
70 * may specify an alternate address when issuing the application soft-start command.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
71 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
72 AppPtr_t AppStartPtr = (AppPtr_t)0x0000;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
73
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
74 /** 64-bit flash page number. This is concatenated with the current 16-bit address on USB AVRs containing more than
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
75 * 64KB of flash memory.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
76 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
77 uint8_t Flash64KBPage = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
78
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
79 /** Memory start address, indicating the current address in the memory being addressed (either FLASH or EEPROM
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
80 * depending on the issued command from the host).
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
81 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
82 uint16_t StartAddr = 0x0000;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
83
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
84 /** Memory end address, indicating the end address to read to/write from in the memory being addressed (either FLASH
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
85 * of EEPROM depending on the issued command from the host).
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
86 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
87 uint16_t EndAddr = 0x0000;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
88
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
89
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
90 /** Pulse generation counters to keep track of the number of milliseconds remaining for each pulse type */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
91 volatile struct
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
92 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
93 uint8_t TxLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
94 uint8_t RxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
95 uint8_t PingPongLEDPulse; /**< Milliseconds remaining for enumeration Tx/Rx ping-pong LED pulse */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
96 } PulseMSRemaining;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
97
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
98 /** Main program entry point. This routine configures the hardware required by the bootloader, then continuously
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
99 * runs the bootloader processing routine until instructed to soft-exit, or hard-reset via the watchdog to start
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
100 * the loaded application code.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
101 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
102 int main(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
103 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
104 /* Configure hardware required by the bootloader */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
105 SetupHardware();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
106
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
107 /* Enable global interrupts so that the USB stack can function */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
108 sei();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
109
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
110 /* Run the USB management task while the bootloader is supposed to be running */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
111 while (RunBootloader || WaitForExit)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
112 USB_USBTask();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
113
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
114 /* Reset configured hardware back to their original states for the user application */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
115 ResetHardware();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
116
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
117 /* Start the user application */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
118 AppStartPtr();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
119 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
120
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
121 /** Configures all hardware required for the bootloader. */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
122 void SetupHardware(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
123 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
124 /* Disable watchdog if enabled by bootloader/fuses */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
125 MCUSR &= ~(1 << WDRF);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
126 wdt_disable();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
127
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
128 /* Disable clock division */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
129 // clock_prescale_set(clock_div_1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
130
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
131 /* Relocate the interrupt vector table to the bootloader section */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
132 MCUCR = (1 << IVCE);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
133 MCUCR = (1 << IVSEL);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
134
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
135 LEDs_Init();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
136
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
137 /* Initialize the USB subsystem */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
138 USB_Init();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
139 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
140
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
141 /** Resets all configured hardware required for the bootloader back to their original states. */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
142 void ResetHardware(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
143 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
144 /* Shut down the USB subsystem */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
145 USB_ShutDown();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
146
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
147 /* Relocate the interrupt vector table back to the application section */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
148 MCUCR = (1 << IVCE);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
149 MCUCR = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
150 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
151
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
152 /** Event handler for the USB_UnhandledControlRequest event. This is used to catch standard and class specific
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
153 * control requests that are not handled internally by the USB library (including the DFU commands, which are
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
154 * all issued via the control endpoint), so that they can be handled appropriately for the application.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
155 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
156 void EVENT_USB_Device_UnhandledControlRequest(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
157 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
158 /* Get the size of the command and data from the wLength value */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
159 SentCommand.DataSize = USB_ControlRequest.wLength;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
160
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
161 /* Turn off TX LED(s) once the TX pulse period has elapsed */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
162 if (PulseMSRemaining.TxLEDPulse && !(--PulseMSRemaining.TxLEDPulse))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
163 LEDs_TurnOffLEDs(LEDMASK_TX);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
164
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
165 /* Turn off RX LED(s) once the RX pulse period has elapsed */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
166 if (PulseMSRemaining.RxLEDPulse && !(--PulseMSRemaining.RxLEDPulse))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
167 LEDs_TurnOffLEDs(LEDMASK_RX);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
168
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
169 switch (USB_ControlRequest.bRequest)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
170 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
171 case DFU_DNLOAD:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
172 LEDs_TurnOnLEDs(LEDMASK_RX);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
173 PulseMSRemaining.RxLEDPulse = TX_RX_LED_PULSE_MS;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
174
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
175 Endpoint_ClearSETUP();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
176
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
177 /* Check if bootloader is waiting to terminate */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
178 if (WaitForExit)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
179 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
180 /* Bootloader is terminating - process last received command */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
181 ProcessBootloaderCommand();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
182
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
183 /* Turn off TX/RX status LEDs so that they're not left on when application starts */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
184 LEDs_TurnOffLEDs(LEDMASK_TX);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
185 LEDs_TurnOffLEDs(LEDMASK_RX);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
186
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
187 /* Indicate that the last command has now been processed - free to exit bootloader */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
188 WaitForExit = false;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
189 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
190
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
191 /* If the request has a data stage, load it into the command struct */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
192 if (SentCommand.DataSize)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
193 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
194 while (!(Endpoint_IsOUTReceived()))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
195 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
196 if (USB_DeviceState == DEVICE_STATE_Unattached)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
197 return;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
198 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
199
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
200 /* First byte of the data stage is the DNLOAD request's command */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
201 SentCommand.Command = Endpoint_Read_Byte();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
202
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
203 /* One byte of the data stage is the command, so subtract it from the total data bytes */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
204 SentCommand.DataSize--;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
205
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
206 /* Load in the rest of the data stage as command parameters */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
207 for (uint8_t DataByte = 0; (DataByte < sizeof(SentCommand.Data)) &&
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
208 Endpoint_BytesInEndpoint(); DataByte++)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
209 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
210 SentCommand.Data[DataByte] = Endpoint_Read_Byte();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
211 SentCommand.DataSize--;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
212 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
213
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
214 /* Process the command */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
215 ProcessBootloaderCommand();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
216 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
217
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
218 /* Check if currently downloading firmware */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
219 if (DFU_State == dfuDNLOAD_IDLE)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
220 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
221 if (!(SentCommand.DataSize))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
222 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
223 DFU_State = dfuIDLE;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
224 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
225 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
226 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
227 /* Throw away the filler bytes before the start of the firmware */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
228 DiscardFillerBytes(DFU_FILLER_BYTES_SIZE);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
229
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
230 /* Throw away the packet alignment filler bytes before the start of the firmware */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
231 DiscardFillerBytes(StartAddr % FIXED_CONTROL_ENDPOINT_SIZE);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
232
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
233 /* Calculate the number of bytes remaining to be written */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
234 uint16_t BytesRemaining = ((EndAddr - StartAddr) + 1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
235
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
236 if (IS_ONEBYTE_COMMAND(SentCommand.Data, 0x00)) // Write flash
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
237 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
238 /* Calculate the number of words to be written from the number of bytes to be written */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
239 uint16_t WordsRemaining = (BytesRemaining >> 1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
240
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
241 union
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
242 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
243 uint16_t Words[2];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
244 uint32_t Long;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
245 } CurrFlashAddress = {.Words = {StartAddr, Flash64KBPage}};
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
246
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
247 uint32_t CurrFlashPageStartAddress = CurrFlashAddress.Long;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
248 uint8_t WordsInFlashPage = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
249
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
250 while (WordsRemaining--)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
251 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
252 /* Check if endpoint is empty - if so clear it and wait until ready for next packet */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
253 if (!(Endpoint_BytesInEndpoint()))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
254 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
255 Endpoint_ClearOUT();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
256
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
257 while (!(Endpoint_IsOUTReceived()))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
258 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
259 if (USB_DeviceState == DEVICE_STATE_Unattached)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
260 return;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
261 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
262 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
263
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
264 /* Write the next word into the current flash page */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
265 boot_page_fill(CurrFlashAddress.Long, Endpoint_Read_Word_LE());
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
266
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
267 /* Adjust counters */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
268 WordsInFlashPage += 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
269 CurrFlashAddress.Long += 2;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
270
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
271 /* See if an entire page has been written to the flash page buffer */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
272 if ((WordsInFlashPage == (SPM_PAGESIZE >> 1)) || !(WordsRemaining))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
273 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
274 /* Commit the flash page to memory */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
275 boot_page_write(CurrFlashPageStartAddress);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
276 boot_spm_busy_wait();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
277
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
278 /* Check if programming incomplete */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
279 if (WordsRemaining)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
280 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
281 CurrFlashPageStartAddress = CurrFlashAddress.Long;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
282 WordsInFlashPage = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
283
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
284 /* Erase next page's temp buffer */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
285 boot_page_erase(CurrFlashAddress.Long);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
286 boot_spm_busy_wait();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
287 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
288 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
289 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
290
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
291 /* Once programming complete, start address equals the end address */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
292 StartAddr = EndAddr;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
293
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
294 /* Re-enable the RWW section of flash */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
295 boot_rww_enable();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
296 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
297 else // Write EEPROM
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
298 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
299 while (BytesRemaining--)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
300 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
301 /* Check if endpoint is empty - if so clear it and wait until ready for next packet */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
302 if (!(Endpoint_BytesInEndpoint()))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
303 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
304 Endpoint_ClearOUT();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
305
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
306 while (!(Endpoint_IsOUTReceived()))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
307 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
308 if (USB_DeviceState == DEVICE_STATE_Unattached)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
309 return;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
310 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
311 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
312
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
313 /* Read the byte from the USB interface and write to to the EEPROM */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
314 eeprom_write_byte((uint8_t*)StartAddr, Endpoint_Read_Byte());
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
315
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
316 /* Adjust counters */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
317 StartAddr++;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
318 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
319 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
320
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
321 /* Throw away the currently unused DFU file suffix */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
322 DiscardFillerBytes(DFU_FILE_SUFFIX_SIZE);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
323 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
324 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
325
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
326 Endpoint_ClearOUT();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
327
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
328 Endpoint_ClearStatusStage();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
329
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
330 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
331 case DFU_UPLOAD:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
332 Endpoint_ClearSETUP();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
333
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
334 LEDs_TurnOnLEDs(LEDMASK_TX);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
335 PulseMSRemaining.TxLEDPulse = TX_RX_LED_PULSE_MS;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
336
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
337 while (!(Endpoint_IsINReady()))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
338 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
339 if (USB_DeviceState == DEVICE_STATE_Unattached)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
340 return;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
341 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
342
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
343 if (DFU_State != dfuUPLOAD_IDLE)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
344 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
345 if ((DFU_State == dfuERROR) && IS_ONEBYTE_COMMAND(SentCommand.Data, 0x01)) // Blank Check
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
346 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
347 /* Blank checking is performed in the DFU_DNLOAD request - if we get here we've told the host
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
348 that the memory isn't blank, and the host is requesting the first non-blank address */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
349 Endpoint_Write_Word_LE(StartAddr);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
350 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
351 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
352 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
353 /* Idle state upload - send response to last issued command */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
354 Endpoint_Write_Byte(ResponseByte);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
355 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
356 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
357 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
358 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
359 /* Determine the number of bytes remaining in the current block */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
360 uint16_t BytesRemaining = ((EndAddr - StartAddr) + 1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
361
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
362 if (IS_ONEBYTE_COMMAND(SentCommand.Data, 0x00)) // Read FLASH
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
363 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
364 /* Calculate the number of words to be written from the number of bytes to be written */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
365 uint16_t WordsRemaining = (BytesRemaining >> 1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
366
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
367 union
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
368 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
369 uint16_t Words[2];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
370 uint32_t Long;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
371 } CurrFlashAddress = {.Words = {StartAddr, Flash64KBPage}};
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
372
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
373 while (WordsRemaining--)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
374 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
375 /* Check if endpoint is full - if so clear it and wait until ready for next packet */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
376 if (Endpoint_BytesInEndpoint() == FIXED_CONTROL_ENDPOINT_SIZE)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
377 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
378 Endpoint_ClearIN();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
379
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
380 while (!(Endpoint_IsINReady()))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
381 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
382 if (USB_DeviceState == DEVICE_STATE_Unattached)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
383 return;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
384 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
385 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
386
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
387 /* Read the flash word and send it via USB to the host */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
388 #if (FLASHEND > 0xFFFF)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
389 Endpoint_Write_Word_LE(pgm_read_word_far(CurrFlashAddress.Long));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
390 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
391 Endpoint_Write_Word_LE(pgm_read_word(CurrFlashAddress.Long));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
392 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
393
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
394 /* Adjust counters */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
395 CurrFlashAddress.Long += 2;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
396 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
397
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
398 /* Once reading is complete, start address equals the end address */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
399 StartAddr = EndAddr;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
400 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
401 else if (IS_ONEBYTE_COMMAND(SentCommand.Data, 0x02)) // Read EEPROM
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
402 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
403 while (BytesRemaining--)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
404 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
405 /* Check if endpoint is full - if so clear it and wait until ready for next packet */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
406 if (Endpoint_BytesInEndpoint() == FIXED_CONTROL_ENDPOINT_SIZE)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
407 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
408 Endpoint_ClearIN();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
409
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
410 while (!(Endpoint_IsINReady()))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
411 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
412 if (USB_DeviceState == DEVICE_STATE_Unattached)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
413 return;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
414 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
415 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
416
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
417 /* Read the EEPROM byte and send it via USB to the host */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
418 Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)StartAddr));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
419
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
420 /* Adjust counters */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
421 StartAddr++;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
422 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
423 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
424
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
425 /* Return to idle state */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
426 DFU_State = dfuIDLE;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
427 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
428
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
429 Endpoint_ClearIN();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
430
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
431 Endpoint_ClearStatusStage();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
432 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
433 case DFU_GETSTATUS:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
434 Endpoint_ClearSETUP();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
435
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
436 /* Write 8-bit status value */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
437 Endpoint_Write_Byte(DFU_Status);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
438
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
439 /* Write 24-bit poll timeout value */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
440 Endpoint_Write_Byte(0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
441 Endpoint_Write_Word_LE(0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
442
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
443 /* Write 8-bit state value */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
444 Endpoint_Write_Byte(DFU_State);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
445
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
446 /* Write 8-bit state string ID number */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
447 Endpoint_Write_Byte(0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
448
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
449 Endpoint_ClearIN();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
450
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
451 Endpoint_ClearStatusStage();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
452 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
453 case DFU_CLRSTATUS:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
454 Endpoint_ClearSETUP();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
455
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
456 /* Reset the status value variable to the default OK status */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
457 DFU_Status = OK;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
458
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
459 Endpoint_ClearStatusStage();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
460 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
461 case DFU_GETSTATE:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
462 Endpoint_ClearSETUP();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
463
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
464 /* Write the current device state to the endpoint */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
465 Endpoint_Write_Byte(DFU_State);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
466
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
467 Endpoint_ClearIN();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
468
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
469 Endpoint_ClearStatusStage();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
470 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
471 case DFU_ABORT:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
472 Endpoint_ClearSETUP();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
473
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
474 /* Turn off TX/RX status LEDs so that they're not left on when application starts */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
475 LEDs_TurnOffLEDs(LEDMASK_TX);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
476 LEDs_TurnOffLEDs(LEDMASK_RX);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
477
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
478 /* Reset the current state variable to the default idle state */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
479 DFU_State = dfuIDLE;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
480
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
481 Endpoint_ClearStatusStage();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
482 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
483 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
484 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
485
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
486 /** Routine to discard the specified number of bytes from the control endpoint stream. This is used to
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
487 * discard unused bytes in the stream from the host, including the memory program block suffix.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
488 *
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
489 * \param[in] NumberOfBytes Number of bytes to discard from the host from the control endpoint
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
490 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
491 static void DiscardFillerBytes(uint8_t NumberOfBytes)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
492 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
493 while (NumberOfBytes--)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
494 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
495 if (!(Endpoint_BytesInEndpoint()))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
496 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
497 Endpoint_ClearOUT();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
498
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
499 /* Wait until next data packet received */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
500 while (!(Endpoint_IsOUTReceived()))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
501 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
502 if (USB_DeviceState == DEVICE_STATE_Unattached)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
503 return;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
504 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
505 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
506 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
507 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
508 Endpoint_Discard_Byte();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
509 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
510 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
511 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
512
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
513 /** Routine to process an issued command from the host, via a DFU_DNLOAD request wrapper. This routine ensures
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
514 * that the command is allowed based on the current secure mode flag value, and passes the command off to the
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
515 * appropriate handler function.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
516 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
517 static void ProcessBootloaderCommand(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
518 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
519 /* Check if device is in secure mode */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
520 // if (IsSecure)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
521 // {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
522 // /* Don't process command unless it is a READ or chip erase command */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
523 // if (!(((SentCommand.Command == COMMAND_WRITE) &&
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
524 // IS_TWOBYTE_COMMAND(SentCommand.Data, 0x00, 0xFF)) ||
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
525 // (SentCommand.Command == COMMAND_READ)))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
526 // {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
527 // /* Set the state and status variables to indicate the error */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
528 // DFU_State = dfuERROR;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
529 // DFU_Status = errWRITE;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
530 //
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
531 // /* Stall command */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
532 // Endpoint_StallTransaction();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
533 //
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
534 // /* Don't process the command */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
535 // return;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
536 // }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
537 // }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
538
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
539 /* Dispatch the required command processing routine based on the command type */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
540 switch (SentCommand.Command)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
541 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
542 case COMMAND_PROG_START:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
543 ProcessMemProgCommand();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
544 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
545 case COMMAND_DISP_DATA:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
546 ProcessMemReadCommand();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
547 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
548 case COMMAND_WRITE:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
549 ProcessWriteCommand();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
550 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
551 case COMMAND_READ:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
552 ProcessReadCommand();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
553 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
554 case COMMAND_CHANGE_BASE_ADDR:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
555 if (IS_TWOBYTE_COMMAND(SentCommand.Data, 0x03, 0x00)) // Set 64KB flash page command
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
556 Flash64KBPage = SentCommand.Data[2];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
557 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
558 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
559 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
560
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
561 /** Routine to concatenate the given pair of 16-bit memory start and end addresses from the host, and store them
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
562 * in the StartAddr and EndAddr global variables.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
563 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
564 static void LoadStartEndAddresses(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
565 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
566 union
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
567 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
568 uint8_t Bytes[2];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
569 uint16_t Word;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
570 } Address[2] = {{.Bytes = {SentCommand.Data[2], SentCommand.Data[1]}},
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
571 {.Bytes = {SentCommand.Data[4], SentCommand.Data[3]}}};
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
572
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
573 /* Load in the start and ending read addresses from the sent data packet */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
574 StartAddr = Address[0].Word;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
575 EndAddr = Address[1].Word;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
576 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
577
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
578 /** Handler for a Memory Program command issued by the host. This routine handles the preparations needed
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
579 * to write subsequent data from the host into the specified memory.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
580 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
581 static void ProcessMemProgCommand(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
582 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
583 if (IS_ONEBYTE_COMMAND(SentCommand.Data, 0x00) || // Write FLASH command
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
584 IS_ONEBYTE_COMMAND(SentCommand.Data, 0x01)) // Write EEPROM command
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
585 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
586 /* Load in the start and ending read addresses */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
587 LoadStartEndAddresses();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
588
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
589 /* If FLASH is being written to, we need to pre-erase the first page to write to */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
590 if (IS_ONEBYTE_COMMAND(SentCommand.Data, 0x00))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
591 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
592 union
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
593 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
594 uint16_t Words[2];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
595 uint32_t Long;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
596 } CurrFlashAddress = {.Words = {StartAddr, Flash64KBPage}};
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
597
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
598 /* Erase the current page's temp buffer */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
599 boot_page_erase(CurrFlashAddress.Long);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
600 boot_spm_busy_wait();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
601 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
602
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
603 /* Set the state so that the next DNLOAD requests reads in the firmware */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
604 DFU_State = dfuDNLOAD_IDLE;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
605 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
606 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
607
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
608 /** Handler for a Memory Read command issued by the host. This routine handles the preparations needed
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
609 * to read subsequent data from the specified memory out to the host, as well as implementing the memory
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
610 * blank check command.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
611 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
612 static void ProcessMemReadCommand(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
613 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
614 if (IS_ONEBYTE_COMMAND(SentCommand.Data, 0x00) || // Read FLASH command
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
615 IS_ONEBYTE_COMMAND(SentCommand.Data, 0x02)) // Read EEPROM command
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
616 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
617 /* Load in the start and ending read addresses */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
618 LoadStartEndAddresses();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
619
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
620 /* Set the state so that the next UPLOAD requests read out the firmware */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
621 DFU_State = dfuUPLOAD_IDLE;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
622 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
623 else if (IS_ONEBYTE_COMMAND(SentCommand.Data, 0x01)) // Blank check FLASH command
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
624 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
625 uint32_t CurrFlashAddress = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
626
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
627 while (CurrFlashAddress < BOOT_START_ADDR)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
628 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
629 /* Check if the current byte is not blank */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
630 #if (FLASHEND > 0xFFFF)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
631 if (pgm_read_byte_far(CurrFlashAddress) != 0xFF)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
632 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
633 if (pgm_read_byte(CurrFlashAddress) != 0xFF)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
634 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
635 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
636 /* Save the location of the first non-blank byte for response back to the host */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
637 Flash64KBPage = (CurrFlashAddress >> 16);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
638 StartAddr = CurrFlashAddress;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
639
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
640 /* Set state and status variables to the appropriate error values */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
641 DFU_State = dfuERROR;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
642 DFU_Status = errCHECK_ERASED;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
643
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
644 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
645 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
646
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
647 CurrFlashAddress++;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
648 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
649 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
650 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
651
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
652 /** Handler for a Data Write command issued by the host. This routine handles non-programming commands such as
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
653 * bootloader exit (both via software jumps and hardware watchdog resets) and flash memory erasure.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
654 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
655 static void ProcessWriteCommand(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
656 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
657 if (IS_ONEBYTE_COMMAND(SentCommand.Data, 0x03)) // Start application
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
658 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
659 /* Indicate that the bootloader is terminating */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
660 WaitForExit = true;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
661
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
662 /* Check if data supplied for the Start Program command - no data executes the program */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
663 if (SentCommand.DataSize)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
664 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
665 if (SentCommand.Data[1] == 0x01) // Start via jump
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
666 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
667 union
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
668 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
669 uint8_t Bytes[2];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
670 AppPtr_t FuncPtr;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
671 } Address = {.Bytes = {SentCommand.Data[4], SentCommand.Data[3]}};
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
672
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
673 /* Load in the jump address into the application start address pointer */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
674 AppStartPtr = Address.FuncPtr;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
675 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
676 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
677 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
678 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
679 if (SentCommand.Data[1] == 0x00) // Start via watchdog
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
680 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
681 /* Start the watchdog to reset the AVR once the communications are finalized */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
682 wdt_enable(WDTO_250MS);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
683 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
684 else // Start via jump
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
685 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
686 /* Set the flag to terminate the bootloader at next opportunity */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
687 RunBootloader = false;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
688 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
689 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
690 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
691 else if (IS_TWOBYTE_COMMAND(SentCommand.Data, 0x00, 0xFF)) // Erase flash
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
692 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
693 uint32_t CurrFlashAddress = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
694
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
695 /* Clear the application section of flash */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
696 while (CurrFlashAddress < BOOT_START_ADDR)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
697 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
698 boot_page_erase(CurrFlashAddress);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
699 boot_spm_busy_wait();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
700 boot_page_write(CurrFlashAddress);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
701 boot_spm_busy_wait();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
702
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
703 CurrFlashAddress += SPM_PAGESIZE;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
704 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
705
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
706 /* Re-enable the RWW section of flash as writing to the flash locks it out */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
707 boot_rww_enable();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
708
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
709 /* Memory has been erased, reset the security bit so that programming/reading is allowed */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
710 // IsSecure = false;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
711 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
712 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
713
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
714 /** Handler for a Data Read command issued by the host. This routine handles bootloader information retrieval
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
715 * commands such as device signature and bootloader version retrieval.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
716 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
717 static void ProcessReadCommand(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
718 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
719 const uint8_t BootloaderInfo[3] = {BOOTLOADER_VERSION, BOOTLOADER_ID_BYTE1, BOOTLOADER_ID_BYTE2};
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
720 const uint8_t SignatureInfo[3] = {AVR_SIGNATURE_1, AVR_SIGNATURE_2, AVR_SIGNATURE_3};
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
721
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
722 uint8_t DataIndexToRead = SentCommand.Data[1];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
723
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
724 if (IS_ONEBYTE_COMMAND(SentCommand.Data, 0x00)) // Read bootloader info
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
725 ResponseByte = BootloaderInfo[DataIndexToRead];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
726 else if (IS_ONEBYTE_COMMAND(SentCommand.Data, 0x01)) // Read signature byte
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
727 ResponseByte = SignatureInfo[DataIndexToRead - 0x30];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
728 }

mercurial