| 1 |
/* |
| 2 |
LUFA Library |
| 3 |
Copyright (C) Dean Camera, 2011. |
| 4 |
|
| 5 |
dean [at] fourwalledcubicle [dot] com |
| 6 |
www.lufa-lib.org |
| 7 |
*/ |
| 8 |
|
| 9 |
/* |
| 10 |
Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com) |
| 11 |
|
| 12 |
Permission to use, copy, modify, distribute, and sell this |
| 13 |
software and its documentation for any purpose is hereby granted |
| 14 |
without fee, provided that the above copyright notice appear in |
| 15 |
all copies and that both that the copyright notice and this |
| 16 |
permission notice and warranty disclaimer appear in supporting |
| 17 |
documentation, and that the name of the author not be used in |
| 18 |
advertising or publicity pertaining to distribution of the |
| 19 |
software without specific, written prior permission. |
| 20 |
|
| 21 |
The author disclaim all warranties with regard to this |
| 22 |
software, including all implied warranties of merchantability |
| 23 |
and fitness. In no event shall the author be liable for any |
| 24 |
special, indirect or consequential damages or any damages |
| 25 |
whatsoever resulting from loss of use, data or profits, whether |
| 26 |
in an action of contract, negligence or other tortious action, |
| 27 |
arising out of or in connection with the use or performance of |
| 28 |
this software. |
| 29 |
*/ |
| 30 |
|
| 31 |
/** \file |
| 32 |
* |
| 33 |
* Main source file for the CDC class bootloader. This file contains the complete bootloader logic. |
| 34 |
*/ |
| 35 |
|
| 36 |
#define INCLUDE_FROM_CATERINA_C |
| 37 |
#include "Caterina.h" |
| 38 |
|
| 39 |
/** Contains the current baud rate and other settings of the first virtual serial port. This must be retained as some |
| 40 |
* operating systems will not open the port unless the settings can be set successfully. |
| 41 |
*/ |
| 42 |
static CDC_LineEncoding_t LineEncoding = { .BaudRateBPS = 0, |
| 43 |
.CharFormat = CDC_LINEENCODING_OneStopBit, |
| 44 |
.ParityType = CDC_PARITY_None, |
| 45 |
.DataBits = 8 }; |
| 46 |
|
| 47 |
/** Current address counter. This stores the current address of the FLASH or EEPROM as set by the host, |
| 48 |
* and is used when reading or writing to the AVRs memory (either FLASH or EEPROM depending on the issued |
| 49 |
* command.) |
| 50 |
*/ |
| 51 |
static uint32_t CurrAddress; |
| 52 |
|
| 53 |
/** Flag to indicate if the bootloader should be running, or should exit and allow the application code to run |
| 54 |
* via a watchdog reset. When cleared the bootloader will exit, starting the watchdog and entering an infinite |
| 55 |
* loop until the AVR restarts and the application runs. |
| 56 |
*/ |
| 57 |
static bool RunBootloader = true; |
| 58 |
|
| 59 |
/* Pulse generation counters to keep track of the time remaining for each pulse type */ |
| 60 |
#define TX_RX_LED_PULSE_PERIOD 100 |
| 61 |
uint16_t TxLEDPulse = 0; // time remaining for Tx LED pulse |
| 62 |
uint16_t RxLEDPulse = 0; // time remaining for Rx LED pulse |
| 63 |
|
| 64 |
/* Bootloader timeout timer */ |
| 65 |
// MAH 8/15/12- change so timeouts work properly when the chip is running at 8MHz instead of 16. |
| 66 |
#define TIMEOUT_PERIOD 8000 |
| 67 |
#define EXT_RESET_TIMEOUT_PERIOD 750 |
| 68 |
|
| 69 |
|
| 70 |
/********************************************************************************************************* |
| 71 |
LilyPadUSB bootloader code |
| 72 |
The LilyPadUSB bootloader has been changed to remove the 8-second delay after external reset which is in |
| 73 |
the Leonardo. To enter the bootloader, the user should execute TWO external resets within 750 ms; that is, |
| 74 |
press the reset button twice, quickly.\ |
| 75 |
|
| 76 |
Some other changes were made to allow this code to compile tightly enough to fit in the alloted 4k of |
| 77 |
bootloader space. |
| 78 |
*/ |
| 79 |
// MAH 8/15/12- added this flag to replace the bulky program memory reads to check for the presence of a sketch |
| 80 |
// at the top of the memory space. |
| 81 |
static bool sketchPresent = false; |
| 82 |
|
| 83 |
// MAH 8/15/12- make this volatile, since we modify it in one place and read it in another, we want to make |
| 84 |
// sure we're always working on the copy in memory and not an erroneous value stored in a cache somewhere. |
| 85 |
// This variable stores the length of time we've been in the bootloader when waiting for the 8 second delay. |
| 86 |
volatile uint16_t Timeout = 0; |
| 87 |
// MAH 8/15/12- added this for delay during startup. Did not use existing Timeout value b/c it only increments |
| 88 |
// when there's a sketch at the top of the memory. |
| 89 |
volatile uint16_t resetTimeout = 0; |
| 90 |
|
| 91 |
// MAH 8/15/12- let's make this an 8-bit value instead of 16- that saves on memory because 16-bit addition and |
| 92 |
// comparison compiles to bulkier code. Note that this does *not* require a change to the Arduino core- we're |
| 93 |
// just sort of ignoring the extra byte that the Arduino core puts at the next location. |
| 94 |
uint8_t bootKey = 0x77; |
| 95 |
volatile uint8_t *const bootKeyPtr = (volatile uint8_t *)0x0800; |
| 96 |
|
| 97 |
// StartSketch() is called to clean up our mess before passing execution to the sketch. |
| 98 |
void StartSketch(void) |
| 99 |
{ |
| 100 |
cli(); |
| 101 |
|
| 102 |
/* Undo TIMER1 setup and clear the count before running the sketch */ |
| 103 |
TIMSK1 = 0; |
| 104 |
TCCR1B = 0; |
| 105 |
|
| 106 |
/* Relocate the interrupt vector table to the application section */ |
| 107 |
MCUCR = (1 << IVCE); |
| 108 |
MCUCR = 0; |
| 109 |
|
| 110 |
L_LED_OFF(); |
| 111 |
TX_LED_OFF(); |
| 112 |
RX_LED_OFF(); |
| 113 |
|
| 114 |
/* jump to beginning of application space */ |
| 115 |
__asm__ volatile("jmp 0x0000"); |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
uint16_t LLEDPulse; |
| 120 |
|
| 121 |
/** Main program entry point. This routine configures the hardware required by the bootloader, then continuously |
| 122 |
* runs the bootloader processing routine until it times out or is instructed to exit. |
| 123 |
*/ |
| 124 |
int main(void) |
| 125 |
{ |
| 126 |
/* Save the value of the boot key memory before it is overwritten */ |
| 127 |
uint8_t bootKeyPtrVal = *bootKeyPtr; |
| 128 |
*bootKeyPtr = 0; |
| 129 |
|
| 130 |
/* Check the reason for the reset so we can act accordingly */ |
| 131 |
uint8_t mcusr_state = MCUSR; // store the initial state of the Status register |
| 132 |
MCUSR = 0; // clear all reset flags |
| 133 |
|
| 134 |
/* Watchdog may be configured with a 15 ms period so must disable it before going any further */ |
| 135 |
// MAH 8/15/12- I removed this because wdt_disable() is the first thing SetupHardware() does- why |
| 136 |
// do it twice right in a row? |
| 137 |
//wdt_disable(); |
| 138 |
|
| 139 |
/* Setup hardware required for the bootloader */ |
| 140 |
// MAH 8/15/12- Moved this up to before the bootloader go/no-go decision tree so I could use the |
| 141 |
// timer in that decision tree. Removed the USBInit() call from it; if I'm not going to stay in |
| 142 |
// the bootloader, there's no point spending the time initializing the USB. |
| 143 |
// SetupHardware(); |
| 144 |
wdt_disable(); |
| 145 |
|
| 146 |
// Disable clock division |
| 147 |
clock_prescale_set(clock_div_1); |
| 148 |
|
| 149 |
// Relocate the interrupt vector table to the bootloader section |
| 150 |
MCUCR = (1 << IVCE); |
| 151 |
MCUCR = (1 << IVSEL); |
| 152 |
|
| 153 |
LED_SETUP(); |
| 154 |
CPU_PRESCALE(0); |
| 155 |
L_LED_OFF(); |
| 156 |
TX_LED_OFF(); |
| 157 |
RX_LED_OFF(); |
| 158 |
|
| 159 |
// Initialize TIMER1 to handle bootloader timeout and LED tasks. |
| 160 |
// With 16 MHz clock and 1/64 prescaler, timer 1 is clocked at 250 kHz |
| 161 |
// Our chosen compare match generates an interrupt every 1 ms. |
| 162 |
// This interrupt is disabled selectively when doing memory reading, erasing, |
| 163 |
// or writing since SPM has tight timing requirements. |
| 164 |
|
| 165 |
OCR1AH = 0; |
| 166 |
OCR1AL = 250; |
| 167 |
TIMSK1 = (1 << OCIE1A); // enable timer 1 output compare A match interrupt |
| 168 |
TCCR1B = ((1 << CS11) | (1 << CS10)); // 1/64 prescaler on timer 1 input |
| 169 |
|
| 170 |
|
| 171 |
// MAH 8/15/12- this replaces bulky pgm_read_word(0) calls later on, to save memory. |
| 172 |
if (pgm_read_word(0) != 0xFFFF) sketchPresent = true; |
| 173 |
|
| 174 |
// MAH 26 Oct 2012- The "bootload or not?" section has been modified since the code released |
| 175 |
// with Arduino 1.0.1. The simplest modification is the replacement of equivalence checks on |
| 176 |
// the reset bits with masked checks, so if more than one reset occurs before the register is |
| 177 |
// checked, the check doesn't fail and fall through to the bootloader unnecessarily. |
| 178 |
|
| 179 |
// The second, more in depth modification addresses behavior after an external reset (i.e., |
| 180 |
// user pushes the reset button). The Leonardo treats all external resets as requests to |
| 181 |
// re-enter the bootloader and wait for code to be loaded. It remains in bootloader mode for |
| 182 |
// 8 seconds before continuing on to the sketch (if one is present). By defining RESET_DELAY |
| 183 |
// equal to 1, this behavior will persist. |
| 184 |
|
| 185 |
// However, if RESET_DELAY is defined to 0, the reset timeout before loading the sketch drops |
| 186 |
// to 750ms. If, during that 750ms, another external reset occurs, THEN an 8-second delay |
| 187 |
// in the bootloader will occur. |
| 188 |
|
| 189 |
// This is the "no-8-second-delay" code. If this is the first time through the loop, we |
| 190 |
// don't expect to see the bootKey in memory. |
| 191 |
if ( (mcusr_state & (1<<EXTRF)) && (bootKeyPtrVal != bootKey) ) { |
| 192 |
*bootKeyPtr = bootKey; // Put the bootKey in memory so if we get back to this |
| 193 |
// point again, we know to jump into the bootloader |
| 194 |
sei(); // Enable interrupts, so we can use timer1 to track our time in the bootloader |
| 195 |
while (RunBootloader) |
| 196 |
{ |
| 197 |
if (resetTimeout > EXT_RESET_TIMEOUT_PERIOD) // resetTimeout is getting incremeted |
| 198 |
RunBootloader = false; // in the timer1 ISR. |
| 199 |
} |
| 200 |
// If we make it past that while loop, it's sketch loading time! |
| 201 |
*bootKeyPtr = 0; // clear out the bootKey; from now on, we want to treat a reset like |
| 202 |
// a normal reset. |
| 203 |
cli(); // Disable interrupts, in case no sketch is present. |
| 204 |
RunBootloader = true; // We want to hang out in the bootloader if no sketch is present. |
| 205 |
if (sketchPresent) StartSketch(); // If a sketch is present, go! Otherwise, wait around |
| 206 |
// in the bootloader until one is uploaded. |
| 207 |
} |
| 208 |
// On a power-on reset, we ALWAYS want to go to the sketch. If there is one. |
| 209 |
// This is a place where the old code had an equivalence and now there is a mask. |
| 210 |
else if ( (mcusr_state & (1<<PORF)) && sketchPresent) { |
| 211 |
StartSketch(); |
| 212 |
} |
| 213 |
// On a watchdog reset, if the bootKey isn't set, and there's a sketch, we should just |
| 214 |
// go straight to the sketch. |
| 215 |
// This is a place where the old code had an equivalence and now there is a mask. |
| 216 |
else if ( (mcusr_state & (1<<WDRF) ) && (bootKeyPtrVal != bootKey) && sketchPresent) { |
| 217 |
// If it looks like an "accidental" watchdog reset then start the sketch. |
| 218 |
StartSketch(); |
| 219 |
} |
| 220 |
|
| 221 |
/* Initialize USB Subsystem */ |
| 222 |
USB_Init(); |
| 223 |
|
| 224 |
/* Enable global interrupts so that the USB stack can function */ |
| 225 |
sei(); |
| 226 |
|
| 227 |
Timeout = 0; |
| 228 |
|
| 229 |
while (RunBootloader) |
| 230 |
{ |
| 231 |
CDC_Task(); |
| 232 |
USB_USBTask(); |
| 233 |
/* Time out and start the sketch if one is present */ |
| 234 |
if (Timeout > TIMEOUT_PERIOD) |
| 235 |
RunBootloader = false; |
| 236 |
|
| 237 |
// MAH 8/15/12- This used to be a function call- inlining it saves a few bytes. |
| 238 |
LLEDPulse++; |
| 239 |
uint8_t p = LLEDPulse >> 8; |
| 240 |
if (p > 127) |
| 241 |
p = 254-p; |
| 242 |
p += p; |
| 243 |
if (((uint8_t)LLEDPulse) > p) |
| 244 |
L_LED_OFF(); |
| 245 |
else |
| 246 |
L_LED_ON(); |
| 247 |
} |
| 248 |
|
| 249 |
/* Disconnect from the host - USB interface will be reset later along with the AVR */ |
| 250 |
USB_Detach(); |
| 251 |
|
| 252 |
/* Jump to beginning of application space to run the sketch - do not reset */ |
| 253 |
StartSketch(); |
| 254 |
} |
| 255 |
|
| 256 |
// Timer1 is set up to provide periodic interrupts. This is used to flicker the LEDs during |
| 257 |
// programming as well as to generate the clock counts which determine how long the board should |
| 258 |
// remain in bootloading mode. |
| 259 |
|
| 260 |
ISR(TIMER1_COMPA_vect, ISR_BLOCK) |
| 261 |
{ |
| 262 |
/* Reset counter */ |
| 263 |
TCNT1H = 0; |
| 264 |
TCNT1L = 0; |
| 265 |
|
| 266 |
/* Check whether the TX or RX LED one-shot period has elapsed. if so, turn off the LED */ |
| 267 |
if (TxLEDPulse && !(--TxLEDPulse)) |
| 268 |
TX_LED_OFF(); |
| 269 |
if (RxLEDPulse && !(--RxLEDPulse)) |
| 270 |
RX_LED_OFF(); |
| 271 |
resetTimeout++; // Needed for the "short reset delay" mode- governs the time the board waits |
| 272 |
// for a second reset before loading the sketch. |
| 273 |
if (pgm_read_word(0) != 0xFFFF) |
| 274 |
Timeout++; |
| 275 |
} |
| 276 |
|
| 277 |
// MAH 29 Oct 2012 Nothing below this point has to change for the LilyPadUSB support |
| 278 |
|
| 279 |
/** Event handler for the USB_ConfigurationChanged event. This configures the device's endpoints ready |
| 280 |
* to relay data to and from the attached USB host. |
| 281 |
*/ |
| 282 |
void EVENT_USB_Device_ConfigurationChanged(void) |
| 283 |
{ |
| 284 |
/* Setup CDC Notification, Rx and Tx Endpoints */ |
| 285 |
Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT, |
| 286 |
ENDPOINT_DIR_IN, CDC_NOTIFICATION_EPSIZE, |
| 287 |
ENDPOINT_BANK_SINGLE); |
| 288 |
|
| 289 |
Endpoint_ConfigureEndpoint(CDC_TX_EPNUM, EP_TYPE_BULK, |
| 290 |
ENDPOINT_DIR_IN, CDC_TXRX_EPSIZE, |
| 291 |
ENDPOINT_BANK_SINGLE); |
| 292 |
|
| 293 |
Endpoint_ConfigureEndpoint(CDC_RX_EPNUM, EP_TYPE_BULK, |
| 294 |
ENDPOINT_DIR_OUT, CDC_TXRX_EPSIZE, |
| 295 |
ENDPOINT_BANK_SINGLE); |
| 296 |
} |
| 297 |
|
| 298 |
/** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to |
| 299 |
* the device from the USB host before passing along unhandled control requests to the library for processing |
| 300 |
* internally. |
| 301 |
*/ |
| 302 |
void EVENT_USB_Device_ControlRequest(void) |
| 303 |
{ |
| 304 |
/* Ignore any requests that aren't directed to the CDC interface */ |
| 305 |
if ((USB_ControlRequest.bmRequestType & (CONTROL_REQTYPE_TYPE | CONTROL_REQTYPE_RECIPIENT)) != |
| 306 |
(REQTYPE_CLASS | REQREC_INTERFACE)) |
| 307 |
{ |
| 308 |
return; |
| 309 |
} |
| 310 |
|
| 311 |
/* Process CDC specific control requests */ |
| 312 |
switch (USB_ControlRequest.bRequest) |
| 313 |
{ |
| 314 |
case CDC_REQ_GetLineEncoding: |
| 315 |
if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) |
| 316 |
{ |
| 317 |
Endpoint_ClearSETUP(); |
| 318 |
|
| 319 |
/* Write the line coding data to the control endpoint */ |
| 320 |
Endpoint_Write_Control_Stream_LE(&LineEncoding, sizeof(CDC_LineEncoding_t)); |
| 321 |
Endpoint_ClearOUT(); |
| 322 |
} |
| 323 |
|
| 324 |
break; |
| 325 |
case CDC_REQ_SetLineEncoding: |
| 326 |
if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) |
| 327 |
{ |
| 328 |
Endpoint_ClearSETUP(); |
| 329 |
|
| 330 |
/* Read the line coding data in from the host into the global struct */ |
| 331 |
Endpoint_Read_Control_Stream_LE(&LineEncoding, sizeof(CDC_LineEncoding_t)); |
| 332 |
Endpoint_ClearIN(); |
| 333 |
} |
| 334 |
|
| 335 |
break; |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
#if !defined(NO_BLOCK_SUPPORT) |
| 340 |
/** Reads or writes a block of EEPROM or FLASH memory to or from the appropriate CDC data endpoint, depending |
| 341 |
* on the AVR910 protocol command issued. |
| 342 |
* |
| 343 |
* \param[in] Command Single character AVR910 protocol command indicating what memory operation to perform |
| 344 |
*/ |
| 345 |
static void ReadWriteMemoryBlock(const uint8_t Command) |
| 346 |
{ |
| 347 |
uint16_t BlockSize; |
| 348 |
char MemoryType; |
| 349 |
|
| 350 |
bool HighByte = false; |
| 351 |
uint8_t LowByte = 0; |
| 352 |
|
| 353 |
BlockSize = (FetchNextCommandByte() << 8); |
| 354 |
BlockSize |= FetchNextCommandByte(); |
| 355 |
|
| 356 |
MemoryType = FetchNextCommandByte(); |
| 357 |
|
| 358 |
if ((MemoryType != 'E') && (MemoryType != 'F')) |
| 359 |
{ |
| 360 |
/* Send error byte back to the host */ |
| 361 |
WriteNextResponseByte('?'); |
| 362 |
|
| 363 |
return; |
| 364 |
} |
| 365 |
|
| 366 |
/* Disable timer 1 interrupt - can't afford to process nonessential interrupts |
| 367 |
* while doing SPM tasks */ |
| 368 |
TIMSK1 = 0; |
| 369 |
|
| 370 |
/* Check if command is to read memory */ |
| 371 |
if (Command == 'g') |
| 372 |
{ |
| 373 |
/* Re-enable RWW section */ |
| 374 |
boot_rww_enable(); |
| 375 |
|
| 376 |
while (BlockSize--) |
| 377 |
{ |
| 378 |
if (MemoryType == 'F') |
| 379 |
{ |
| 380 |
/* Read the next FLASH byte from the current FLASH page */ |
| 381 |
#if (FLASHEND > 0xFFFF) |
| 382 |
WriteNextResponseByte(pgm_read_byte_far(CurrAddress | HighByte)); |
| 383 |
#else |
| 384 |
WriteNextResponseByte(pgm_read_byte(CurrAddress | HighByte)); |
| 385 |
#endif |
| 386 |
|
| 387 |
/* If both bytes in current word have been read, increment the address counter */ |
| 388 |
if (HighByte) |
| 389 |
CurrAddress += 2; |
| 390 |
|
| 391 |
HighByte = !HighByte; |
| 392 |
} |
| 393 |
else |
| 394 |
{ |
| 395 |
/* Read the next EEPROM byte into the endpoint */ |
| 396 |
WriteNextResponseByte(eeprom_read_byte((uint8_t*)(intptr_t)(CurrAddress >> 1))); |
| 397 |
|
| 398 |
/* Increment the address counter after use */ |
| 399 |
CurrAddress += 2; |
| 400 |
} |
| 401 |
} |
| 402 |
} |
| 403 |
else |
| 404 |
{ |
| 405 |
uint32_t PageStartAddress = CurrAddress; |
| 406 |
|
| 407 |
if (MemoryType == 'F') |
| 408 |
{ |
| 409 |
boot_page_erase(PageStartAddress); |
| 410 |
boot_spm_busy_wait(); |
| 411 |
} |
| 412 |
|
| 413 |
while (BlockSize--) |
| 414 |
{ |
| 415 |
if (MemoryType == 'F') |
| 416 |
{ |
| 417 |
/* If both bytes in current word have been written, increment the address counter */ |
| 418 |
if (HighByte) |
| 419 |
{ |
| 420 |
/* Write the next FLASH word to the current FLASH page */ |
| 421 |
boot_page_fill(CurrAddress, ((FetchNextCommandByte() << 8) | LowByte)); |
| 422 |
|
| 423 |
/* Increment the address counter after use */ |
| 424 |
CurrAddress += 2; |
| 425 |
} |
| 426 |
else |
| 427 |
{ |
| 428 |
LowByte = FetchNextCommandByte(); |
| 429 |
} |
| 430 |
|
| 431 |
HighByte = !HighByte; |
| 432 |
} |
| 433 |
else |
| 434 |
{ |
| 435 |
/* Write the next EEPROM byte from the endpoint */ |
| 436 |
eeprom_write_byte((uint8_t*)((intptr_t)(CurrAddress >> 1)), FetchNextCommandByte()); |
| 437 |
|
| 438 |
/* Increment the address counter after use */ |
| 439 |
CurrAddress += 2; |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
/* If in FLASH programming mode, commit the page after writing */ |
| 444 |
if (MemoryType == 'F') |
| 445 |
{ |
| 446 |
/* Commit the flash page to memory */ |
| 447 |
boot_page_write(PageStartAddress); |
| 448 |
|
| 449 |
/* Wait until write operation has completed */ |
| 450 |
boot_spm_busy_wait(); |
| 451 |
} |
| 452 |
|
| 453 |
/* Send response byte back to the host */ |
| 454 |
WriteNextResponseByte('\r'); |
| 455 |
} |
| 456 |
|
| 457 |
/* Re-enable timer 1 interrupt disabled earlier in this routine */ |
| 458 |
TIMSK1 = (1 << OCIE1A); |
| 459 |
} |
| 460 |
#endif |
| 461 |
|
| 462 |
/** Retrieves the next byte from the host in the CDC data OUT endpoint, and clears the endpoint bank if needed |
| 463 |
* to allow reception of the next data packet from the host. |
| 464 |
* |
| 465 |
* \return Next received byte from the host in the CDC data OUT endpoint |
| 466 |
*/ |
| 467 |
static uint8_t FetchNextCommandByte(void) |
| 468 |
{ |
| 469 |
/* Select the OUT endpoint so that the next data byte can be read */ |
| 470 |
Endpoint_SelectEndpoint(CDC_RX_EPNUM); |
| 471 |
|
| 472 |
/* If OUT endpoint empty, clear it and wait for the next packet from the host */ |
| 473 |
while (!(Endpoint_IsReadWriteAllowed())) |
| 474 |
{ |
| 475 |
Endpoint_ClearOUT(); |
| 476 |
|
| 477 |
while (!(Endpoint_IsOUTReceived())) |
| 478 |
{ |
| 479 |
if (USB_DeviceState == DEVICE_STATE_Unattached) |
| 480 |
return 0; |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
/* Fetch the next byte from the OUT endpoint */ |
| 485 |
return Endpoint_Read_8(); |
| 486 |
} |
| 487 |
|
| 488 |
/** Writes the next response byte to the CDC data IN endpoint, and sends the endpoint back if needed to free up the |
| 489 |
* bank when full ready for the next byte in the packet to the host. |
| 490 |
* |
| 491 |
* \param[in] Response Next response byte to send to the host |
| 492 |
*/ |
| 493 |
static void WriteNextResponseByte(const uint8_t Response) |
| 494 |
{ |
| 495 |
/* Select the IN endpoint so that the next data byte can be written */ |
| 496 |
Endpoint_SelectEndpoint(CDC_TX_EPNUM); |
| 497 |
|
| 498 |
/* If IN endpoint full, clear it and wait until ready for the next packet to the host */ |
| 499 |
if (!(Endpoint_IsReadWriteAllowed())) |
| 500 |
{ |
| 501 |
Endpoint_ClearIN(); |
| 502 |
|
| 503 |
while (!(Endpoint_IsINReady())) |
| 504 |
{ |
| 505 |
if (USB_DeviceState == DEVICE_STATE_Unattached) |
| 506 |
return; |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
/* Write the next byte to the IN endpoint */ |
| 511 |
Endpoint_Write_8(Response); |
| 512 |
|
| 513 |
TX_LED_ON(); |
| 514 |
TxLEDPulse = TX_RX_LED_PULSE_PERIOD; |
| 515 |
} |
| 516 |
|
| 517 |
#define STK_OK 0x10 |
| 518 |
#define STK_INSYNC 0x14 // ' ' |
| 519 |
#define CRC_EOP 0x20 // 'SPACE' |
| 520 |
#define STK_GET_SYNC 0x30 // '0' |
| 521 |
|
| 522 |
#define STK_GET_PARAMETER 0x41 // 'A' |
| 523 |
#define STK_SET_DEVICE 0x42 // 'B' |
| 524 |
#define STK_SET_DEVICE_EXT 0x45 // 'E' |
| 525 |
#define STK_LOAD_ADDRESS 0x55 // 'U' |
| 526 |
#define STK_UNIVERSAL 0x56 // 'V' |
| 527 |
#define STK_PROG_PAGE 0x64 // 'd' |
| 528 |
#define STK_READ_PAGE 0x74 // 't' |
| 529 |
#define STK_READ_SIGN 0x75 // 'u' |
| 530 |
|
| 531 |
/** Task to read in AVR910 commands from the CDC data OUT endpoint, process them, perform the required actions |
| 532 |
* and send the appropriate response back to the host. |
| 533 |
*/ |
| 534 |
void CDC_Task(void) |
| 535 |
{ |
| 536 |
/* Select the OUT endpoint */ |
| 537 |
Endpoint_SelectEndpoint(CDC_RX_EPNUM); |
| 538 |
|
| 539 |
/* Check if endpoint has a command in it sent from the host */ |
| 540 |
if (!(Endpoint_IsOUTReceived())) |
| 541 |
return; |
| 542 |
|
| 543 |
RX_LED_ON(); |
| 544 |
RxLEDPulse = TX_RX_LED_PULSE_PERIOD; |
| 545 |
|
| 546 |
/* Read in the bootloader command (first byte sent from host) */ |
| 547 |
uint8_t Command = FetchNextCommandByte(); |
| 548 |
|
| 549 |
if (Command == 'E') |
| 550 |
{ |
| 551 |
/* We nearly run out the bootloader timeout clock, |
| 552 |
* leaving just a few hundred milliseconds so the |
| 553 |
* bootloder has time to respond and service any |
| 554 |
* subsequent requests */ |
| 555 |
Timeout = TIMEOUT_PERIOD - 500; |
| 556 |
|
| 557 |
/* Re-enable RWW section - must be done here in case |
| 558 |
* user has disabled verification on upload. */ |
| 559 |
boot_rww_enable_safe(); |
| 560 |
|
| 561 |
// Send confirmation byte back to the host |
| 562 |
WriteNextResponseByte('\r'); |
| 563 |
} |
| 564 |
else if (Command == 'T') |
| 565 |
{ |
| 566 |
FetchNextCommandByte(); |
| 567 |
|
| 568 |
// Send confirmation byte back to the host |
| 569 |
WriteNextResponseByte('\r'); |
| 570 |
} |
| 571 |
else if ((Command == 'L') || (Command == 'P')) |
| 572 |
{ |
| 573 |
// Send confirmation byte back to the host |
| 574 |
WriteNextResponseByte('\r'); |
| 575 |
} |
| 576 |
else if (Command == 't') |
| 577 |
{ |
| 578 |
// Return ATMEGA128 part code - this is only to allow AVRProg to use the bootloader |
| 579 |
WriteNextResponseByte(0x44); |
| 580 |
WriteNextResponseByte(0x00); |
| 581 |
} |
| 582 |
else if (Command == 'a') |
| 583 |
{ |
| 584 |
// Indicate auto-address increment is supported |
| 585 |
WriteNextResponseByte('Y'); |
| 586 |
} |
| 587 |
else if (Command == 'A') |
| 588 |
{ |
| 589 |
// Set the current address to that given by the host |
| 590 |
CurrAddress = (FetchNextCommandByte() << 9); |
| 591 |
CurrAddress |= (FetchNextCommandByte() << 1); |
| 592 |
|
| 593 |
// Send confirmation byte back to the host |
| 594 |
WriteNextResponseByte('\r'); |
| 595 |
} |
| 596 |
else if (Command == 'p') |
| 597 |
{ |
| 598 |
// Indicate serial programmer back to the host |
| 599 |
WriteNextResponseByte('S'); |
| 600 |
} |
| 601 |
else if (Command == 'S') |
| 602 |
{ |
| 603 |
// Write the 7-byte software identifier to the endpoint |
| 604 |
for (uint8_t CurrByte = 0; CurrByte < 7; CurrByte++) |
| 605 |
WriteNextResponseByte(SOFTWARE_IDENTIFIER[CurrByte]); |
| 606 |
} |
| 607 |
else if (Command == 'V') |
| 608 |
{ |
| 609 |
WriteNextResponseByte('0' + BOOTLOADER_VERSION_MAJOR); |
| 610 |
WriteNextResponseByte('0' + BOOTLOADER_VERSION_MINOR); |
| 611 |
} |
| 612 |
else if (Command == 's') |
| 613 |
{ |
| 614 |
WriteNextResponseByte(AVR_SIGNATURE_3); |
| 615 |
WriteNextResponseByte(AVR_SIGNATURE_2); |
| 616 |
WriteNextResponseByte(AVR_SIGNATURE_1); |
| 617 |
} |
| 618 |
else if (Command == 'e') |
| 619 |
{ |
| 620 |
// Clear the application section of flash |
| 621 |
for (uint32_t CurrFlashAddress = 0; CurrFlashAddress < BOOT_START_ADDR; CurrFlashAddress += SPM_PAGESIZE) |
| 622 |
{ |
| 623 |
boot_page_erase(CurrFlashAddress); |
| 624 |
boot_spm_busy_wait(); |
| 625 |
boot_page_write(CurrFlashAddress); |
| 626 |
boot_spm_busy_wait(); |
| 627 |
} |
| 628 |
|
| 629 |
// Send confirmation byte back to the host |
| 630 |
WriteNextResponseByte('\r'); |
| 631 |
} |
| 632 |
#if !defined(NO_LOCK_BYTE_WRITE_SUPPORT) |
| 633 |
else if (Command == 'l') |
| 634 |
{ |
| 635 |
// Set the lock bits to those given by the host |
| 636 |
boot_lock_bits_set(FetchNextCommandByte()); |
| 637 |
|
| 638 |
// Send confirmation byte back to the host |
| 639 |
WriteNextResponseByte('\r'); |
| 640 |
} |
| 641 |
#endif |
| 642 |
else if (Command == 'r') |
| 643 |
{ |
| 644 |
WriteNextResponseByte(boot_lock_fuse_bits_get(GET_LOCK_BITS)); |
| 645 |
} |
| 646 |
else if (Command == 'F') |
| 647 |
{ |
| 648 |
WriteNextResponseByte(boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS)); |
| 649 |
} |
| 650 |
else if (Command == 'N') |
| 651 |
{ |
| 652 |
WriteNextResponseByte(boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS)); |
| 653 |
} |
| 654 |
else if (Command == 'Q') |
| 655 |
{ |
| 656 |
WriteNextResponseByte(boot_lock_fuse_bits_get(GET_EXTENDED_FUSE_BITS)); |
| 657 |
} |
| 658 |
#if !defined(NO_BLOCK_SUPPORT) |
| 659 |
else if (Command == 'b') |
| 660 |
{ |
| 661 |
WriteNextResponseByte('Y'); |
| 662 |
|
| 663 |
// Send block size to the host |
| 664 |
WriteNextResponseByte(SPM_PAGESIZE >> 8); |
| 665 |
WriteNextResponseByte(SPM_PAGESIZE & 0xFF); |
| 666 |
} |
| 667 |
else if ((Command == 'B') || (Command == 'g')) |
| 668 |
{ |
| 669 |
// Keep resetting the timeout counter if we're receiving self-programming instructions |
| 670 |
Timeout = 0; |
| 671 |
// Delegate the block write/read to a separate function for clarity |
| 672 |
ReadWriteMemoryBlock(Command); |
| 673 |
} |
| 674 |
#endif |
| 675 |
#if !defined(NO_FLASH_BYTE_SUPPORT) |
| 676 |
else if (Command == 'C') |
| 677 |
{ |
| 678 |
// Write the high byte to the current flash page |
| 679 |
boot_page_fill(CurrAddress, FetchNextCommandByte()); |
| 680 |
|
| 681 |
// Send confirmation byte back to the host |
| 682 |
WriteNextResponseByte('\r'); |
| 683 |
} |
| 684 |
else if (Command == 'c') |
| 685 |
{ |
| 686 |
// Write the low byte to the current flash page |
| 687 |
boot_page_fill(CurrAddress | 0x01, FetchNextCommandByte()); |
| 688 |
|
| 689 |
// Increment the address |
| 690 |
CurrAddress += 2; |
| 691 |
|
| 692 |
// Send confirmation byte back to the host |
| 693 |
WriteNextResponseByte('\r'); |
| 694 |
} |
| 695 |
else if (Command == 'm') |
| 696 |
{ |
| 697 |
// Commit the flash page to memory |
| 698 |
boot_page_write(CurrAddress); |
| 699 |
|
| 700 |
// Wait until write operation has completed |
| 701 |
boot_spm_busy_wait(); |
| 702 |
|
| 703 |
// Send confirmation byte back to the host |
| 704 |
WriteNextResponseByte('\r'); |
| 705 |
} |
| 706 |
else if (Command == 'R') |
| 707 |
{ |
| 708 |
#if (FLASHEND > 0xFFFF) |
| 709 |
uint16_t ProgramWord = pgm_read_word_far(CurrAddress); |
| 710 |
#else |
| 711 |
uint16_t ProgramWord = pgm_read_word(CurrAddress); |
| 712 |
#endif |
| 713 |
|
| 714 |
WriteNextResponseByte(ProgramWord >> 8); |
| 715 |
WriteNextResponseByte(ProgramWord & 0xFF); |
| 716 |
} |
| 717 |
#endif |
| 718 |
#if !defined(NO_EEPROM_BYTE_SUPPORT) |
| 719 |
else if (Command == 'D') |
| 720 |
{ |
| 721 |
// Read the byte from the endpoint and write it to the EEPROM |
| 722 |
eeprom_write_byte((uint8_t*)((intptr_t)(CurrAddress >> 1)), FetchNextCommandByte()); |
| 723 |
|
| 724 |
// Increment the address after use |
| 725 |
CurrAddress += 2; |
| 726 |
|
| 727 |
// Send confirmation byte back to the host |
| 728 |
WriteNextResponseByte('\r'); |
| 729 |
} |
| 730 |
else if (Command == 'd') |
| 731 |
{ |
| 732 |
// Read the EEPROM byte and write it to the endpoint |
| 733 |
WriteNextResponseByte(eeprom_read_byte((uint8_t*)((intptr_t)(CurrAddress >> 1)))); |
| 734 |
|
| 735 |
// Increment the address after use |
| 736 |
CurrAddress += 2; |
| 737 |
} |
| 738 |
#endif |
| 739 |
else if (Command != 27) |
| 740 |
{ |
| 741 |
// Unknown (non-sync) command, return fail code |
| 742 |
WriteNextResponseByte('?'); |
| 743 |
} |
| 744 |
|
| 745 |
|
| 746 |
/* Select the IN endpoint */ |
| 747 |
Endpoint_SelectEndpoint(CDC_TX_EPNUM); |
| 748 |
|
| 749 |
/* Remember if the endpoint is completely full before clearing it */ |
| 750 |
bool IsEndpointFull = !(Endpoint_IsReadWriteAllowed()); |
| 751 |
|
| 752 |
/* Send the endpoint data to the host */ |
| 753 |
Endpoint_ClearIN(); |
| 754 |
|
| 755 |
/* If a full endpoint's worth of data was sent, we need to send an empty packet afterwards to signal end of transfer */ |
| 756 |
if (IsEndpointFull) |
| 757 |
{ |
| 758 |
while (!(Endpoint_IsINReady())) |
| 759 |
{ |
| 760 |
if (USB_DeviceState == DEVICE_STATE_Unattached) |
| 761 |
return; |
| 762 |
} |
| 763 |
|
| 764 |
Endpoint_ClearIN(); |
| 765 |
} |
| 766 |
|
| 767 |
/* Wait until the data has been sent to the host */ |
| 768 |
while (!(Endpoint_IsINReady())) |
| 769 |
{ |
| 770 |
if (USB_DeviceState == DEVICE_STATE_Unattached) |
| 771 |
return; |
| 772 |
} |
| 773 |
|
| 774 |
/* Select the OUT endpoint */ |
| 775 |
Endpoint_SelectEndpoint(CDC_RX_EPNUM); |
| 776 |
|
| 777 |
/* Acknowledge the command from the host */ |
| 778 |
Endpoint_ClearOUT(); |
| 779 |
} |
| 780 |
|