X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/475323e4004f6b2410cad85eb103d7a13dfb060f..0da99447d3e88e83f9977501bee56af5c7aa56c0:/Bootloaders/CDC/BootloaderCDC.c?ds=inline diff --git a/Bootloaders/CDC/BootloaderCDC.c b/Bootloaders/CDC/BootloaderCDC.c index 12a755707..af778c76f 100644 --- a/Bootloaders/CDC/BootloaderCDC.c +++ b/Bootloaders/CDC/BootloaderCDC.c @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2010. + Copyright (C) Dean Camera, 2012. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -39,23 +39,72 @@ /** Contains the current baud rate and other settings of the first virtual serial port. This must be retained as some * operating systems will not open the port unless the settings can be set successfully. */ -CDC_Line_Coding_t LineEncoding = { .BaudRateBPS = 0, - .CharFormat = OneStopBit, - .ParityType = Parity_None, - .DataBits = 8 }; +static CDC_LineEncoding_t LineEncoding = { .BaudRateBPS = 0, + .CharFormat = CDC_LINEENCODING_OneStopBit, + .ParityType = CDC_PARITY_None, + .DataBits = 8 }; /** Current address counter. This stores the current address of the FLASH or EEPROM as set by the host, * and is used when reading or writing to the AVRs memory (either FLASH or EEPROM depending on the issued * command.) */ -uint32_t CurrAddress; +static uint32_t CurrAddress; /** Flag to indicate if the bootloader should be running, or should exit and allow the application code to run * via a watchdog reset. When cleared the bootloader will exit, starting the watchdog and entering an infinite * loop until the AVR restarts and the application runs. */ -bool RunBootloader = true; +static bool RunBootloader = true; +/** Magic lock for forced application start. If the HWBE fuse is programmed and BOOTRST is unprogrammed, the bootloader + * will start if the /HWB line of the AVR is held low and the system is reset. However, if the /HWB line is still held + * low when the application attempts to start via a watchdog reset, the bootloader will re-start. If set to the value + * \ref MAGIC_BOOT_KEY the special init function \ref Application_Jump_Check() will force the application to start. + */ +uint16_t MagicBootKey ATTR_NO_INIT; + + +/** Special startup routine to check if the bootloader was started via a watchdog reset, and if the magic application + * start key has been loaded into \ref MagicBootKey. If the bootloader started via the watchdog and the key is valid, + * this will force the user application to start via a software jump. + */ +void Application_Jump_Check(void) +{ + bool JumpToApplication = false; + + #if ((BOARD == BOARD_XPLAIN) || (BOARD == BOARD_XPLAIN_REV1)) + /* Disable JTAG debugging */ + JTAG_DISABLE(); + + /* Enable pull-up on the JTAG TCK pin so we can use it to select the mode */ + PORTF |= (1 << 4); + Delay_MS(10); + + /* If the TCK pin is not jumpered to ground, start the user application instead */ + JumpToApplication |= ((PINF & (1 << 4)) != 0); + + /* Re-enable JTAG debugging */ + JTAG_ENABLE(); + #endif + + /* If the reset source was the bootloader and the key is correct, clear it and jump to the application */ + if ((MCUSR & (1 << WDRF)) && (MagicBootKey == MAGIC_BOOT_KEY)) + JumpToApplication |= true; + + /* If a request has been made to jump to the user application, honor it */ + if (JumpToApplication) + { + /* Turn off the watchdog */ + MCUSR &= ~(1<> 8); - WriteNextResponseByte(SPM_PAGESIZE & 0xFF); - } - else if ((Command == 'B') || (Command == 'g')) - { - /* Delegate the block write/read to a separate function for clarity */ - ReadWriteMemoryBlock(Command); - } - #endif - #if !defined(NO_FLASH_BYTE_SUPPORT) - else if (Command == 'C') - { - /* Write the high byte to the current flash page */ - boot_page_fill(CurrAddress, FetchNextCommandByte()); + /* Send block size to the host */ + WriteNextResponseByte(SPM_PAGESIZE >> 8); + WriteNextResponseByte(SPM_PAGESIZE & 0xFF); + } + else if ((Command == 'B') || (Command == 'g')) + { + /* Delegate the block write/read to a separate function for clarity */ + ReadWriteMemoryBlock(Command); + } + #endif + #if !defined(NO_FLASH_BYTE_SUPPORT) + else if (Command == 'C') + { + /* Write the high byte to the current flash page */ + boot_page_fill(CurrAddress, FetchNextCommandByte()); - /* Send confirmation byte back to the host */ - WriteNextResponseByte('\r'); - } - else if (Command == 'c') - { - /* Write the low byte to the current flash page */ - boot_page_fill(CurrAddress | 0x01, FetchNextCommandByte()); + /* Send confirmation byte back to the host */ + WriteNextResponseByte('\r'); + } + else if (Command == 'c') + { + /* Write the low byte to the current flash page */ + boot_page_fill(CurrAddress | 0x01, FetchNextCommandByte()); - /* Increment the address */ - CurrAddress += 2; + /* Increment the address */ + CurrAddress += 2; - /* Send confirmation byte back to the host */ - WriteNextResponseByte('\r'); - } - else if (Command == 'm') - { - /* Commit the flash page to memory */ - boot_page_write(CurrAddress); + /* Send confirmation byte back to the host */ + WriteNextResponseByte('\r'); + } + else if (Command == 'm') + { + /* Commit the flash page to memory */ + boot_page_write(CurrAddress); - /* Wait until write operation has completed */ - boot_spm_busy_wait(); + /* Wait until write operation has completed */ + boot_spm_busy_wait(); - /* Send confirmation byte back to the host */ - WriteNextResponseByte('\r'); - } - else if (Command == 'R') - { - #if (FLASHEND > 0xFFFF) - uint16_t ProgramWord = pgm_read_word_far(CurrAddress); - #else - uint16_t ProgramWord = pgm_read_word(CurrAddress); - #endif - - WriteNextResponseByte(ProgramWord >> 8); - WriteNextResponseByte(ProgramWord & 0xFF); - } + /* Send confirmation byte back to the host */ + WriteNextResponseByte('\r'); + } + else if (Command == 'R') + { + #if (FLASHEND > 0xFFFF) + uint16_t ProgramWord = pgm_read_word_far(CurrAddress); + #else + uint16_t ProgramWord = pgm_read_word(CurrAddress); #endif - #if !defined(NO_EEPROM_BYTE_SUPPORT) - else if (Command == 'D') - { - /* Read the byte from the endpoint and write it to the EEPROM */ - eeprom_write_byte((uint8_t*)((intptr_t)(CurrAddress >> 1)), FetchNextCommandByte()); - /* Increment the address after use */ - CurrAddress += 2; - - /* Send confirmation byte back to the host */ - WriteNextResponseByte('\r'); - } - else if (Command == 'd') - { - /* Read the EEPROM byte and write it to the endpoint */ - WriteNextResponseByte(eeprom_read_byte((uint8_t*)((intptr_t)(CurrAddress >> 1)))); + WriteNextResponseByte(ProgramWord >> 8); + WriteNextResponseByte(ProgramWord & 0xFF); + } + #endif + #if !defined(NO_EEPROM_BYTE_SUPPORT) + else if (Command == 'D') + { + /* Read the byte from the endpoint and write it to the EEPROM */ + eeprom_write_byte((uint8_t*)((intptr_t)(CurrAddress >> 1)), FetchNextCommandByte()); - /* Increment the address after use */ - CurrAddress += 2; - } - #endif - else if (Command != 27) - { - /* Unknown (non-sync) command, return fail code */ - WriteNextResponseByte('?'); - } + /* Increment the address after use */ + CurrAddress += 2; - /* Select the IN endpoint */ - Endpoint_SelectEndpoint(CDC_TX_EPNUM); + /* Send confirmation byte back to the host */ + WriteNextResponseByte('\r'); + } + else if (Command == 'd') + { + /* Read the EEPROM byte and write it to the endpoint */ + WriteNextResponseByte(eeprom_read_byte((uint8_t*)((intptr_t)(CurrAddress >> 1)))); - /* Remember if the endpoint is completely full before clearing it */ - bool IsEndpointFull = !(Endpoint_IsReadWriteAllowed()); + /* Increment the address after use */ + CurrAddress += 2; + } + #endif + else if (Command != 27) + { + /* Unknown (non-sync) command, return fail code */ + WriteNextResponseByte('?'); + } - /* Send the endpoint data to the host */ - Endpoint_ClearIN(); + /* Select the IN endpoint */ + Endpoint_SelectEndpoint(CDC_TX_EPADDR); - /* If a full endpoint's worth of data was sent, we need to send an empty packet afterwards to signal end of transfer */ - if (IsEndpointFull) - { - while (!(Endpoint_IsINReady())) - { - if (USB_DeviceState == DEVICE_STATE_Unattached) - return; - } + /* Remember if the endpoint is completely full before clearing it */ + bool IsEndpointFull = !(Endpoint_IsReadWriteAllowed()); - Endpoint_ClearIN(); - } + /* Send the endpoint data to the host */ + Endpoint_ClearIN(); - /* Wait until the data has been sent to the host */ + /* If a full endpoint's worth of data was sent, we need to send an empty packet afterwards to signal end of transfer */ + if (IsEndpointFull) + { while (!(Endpoint_IsINReady())) { if (USB_DeviceState == DEVICE_STATE_Unattached) return; } - /* Select the OUT endpoint */ - Endpoint_SelectEndpoint(CDC_RX_EPNUM); + Endpoint_ClearIN(); + } - /* Acknowledge the command from the host */ - Endpoint_ClearOUT(); + /* Wait until the data has been sent to the host */ + while (!(Endpoint_IsINReady())) + { + if (USB_DeviceState == DEVICE_STATE_Unattached) + return; } + + /* Select the OUT endpoint */ + Endpoint_SelectEndpoint(CDC_RX_EPADDR); + + /* Acknowledge the command from the host */ + Endpoint_ClearOUT(); }