X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/d711e37d2f10f8df9a9ffdf974935c1f1d0a7906..b9b03aadb219d06fbad9d110e508db93e45461af:/Bootloaders/DFU/BootloaderDFU.c diff --git a/Bootloaders/DFU/BootloaderDFU.c b/Bootloaders/DFU/BootloaderDFU.c index 704133f5d..11177c5a6 100644 --- a/Bootloaders/DFU/BootloaderDFU.c +++ b/Bootloaders/DFU/BootloaderDFU.c @@ -32,13 +32,6 @@ * * Main source file for the DFU class bootloader. This file contains the complete bootloader logic. */ - -/** Configuration define. Define this token to true to case the bootloader to reject all memory commands - * until a memory erase has been performed. When used in conjunction with the lockbits of the AVR, this - * can protect the AVR's firmware from being dumped from a secured AVR. When false, memory operations are - * allowed at any time. - */ -#define SECURE_MODE false #define INCLUDE_FROM_BOOTLOADER_C #include "BootloaderDFU.h" @@ -57,7 +50,7 @@ bool RunBootloader = true; /** Flag to indicate if the bootloader is waiting to exit. When the host requests the bootloader to exit and * jump to the application address it specifies, it sends two sequential commands which must be properly - * acknowedged. Upon reception of the first the RunBootloader flag is cleared and the WaitForExit flag is set, + * acknowledged. Upon reception of the first the RunBootloader flag is cleared and the WaitForExit flag is set, * causing the bootloader to wait for the final exit command before shutting down. */ bool WaitForExit = false; @@ -109,8 +102,8 @@ int main (void) MCUSR &= ~(1 << WDRF); wdt_disable(); - /* Disable Clock Division */ - SetSystemClockPrescaler(0); + /* Disable clock division */ + clock_prescale_set(clock_div_1); /* Relocate the interrupt vector table to the bootloader section */ MCUCR = (1 << IVCE); @@ -146,7 +139,7 @@ int main (void) /** Event handler for the USB_Disconnect event. This indicates that the bootloader should exit and the user * application started. */ -EVENT_HANDLER(USB_Disconnect) +void EVENT_USB_Disconnect(void) { /* Upon disconnection, run user application */ RunBootloader = false; @@ -156,21 +149,15 @@ EVENT_HANDLER(USB_Disconnect) * control requests that are not handled internally by the USB library (including the DFU commands, which are * all issued via the control endpoint), so that they can be handled appropriately for the application. */ -EVENT_HANDLER(USB_UnhandledControlPacket) +void EVENT_USB_UnhandledControlPacket(void) { - /* Discard unused wIndex value */ - Endpoint_Discard_Word(); - - /* Discard unused wValue value */ - Endpoint_Discard_Word(); - /* Get the size of the command and data from the wLength value */ - SentCommand.DataSize = Endpoint_Read_Word_LE(); + SentCommand.DataSize = USB_ControlRequest.wLength; - switch (bRequest) + switch (USB_ControlRequest.bRequest) { case DFU_DNLOAD: - Endpoint_ClearSetupReceived(); + Endpoint_ClearSETUP(); /* Check if bootloader is waiting to terminate */ if (WaitForExit) @@ -185,7 +172,7 @@ EVENT_HANDLER(USB_UnhandledControlPacket) /* If the request has a data stage, load it into the command struct */ if (SentCommand.DataSize) { - while (!(Endpoint_IsSetupOUTReceived())); + while (!(Endpoint_IsOUTReceived())); /* First byte of the data stage is the DNLOAD request's command */ SentCommand.Command = Endpoint_Read_Byte(); @@ -232,7 +219,7 @@ EVENT_HANDLER(USB_UnhandledControlPacket) { uint16_t Words[2]; uint32_t Long; - } CurrFlashAddress = {Words: {StartAddr, Flash64KBPage}}; + } CurrFlashAddress = {.Words = {StartAddr, Flash64KBPage}}; uint32_t CurrFlashPageStartAddress = CurrFlashAddress.Long; uint8_t WordsInFlashPage = 0; @@ -242,8 +229,8 @@ EVENT_HANDLER(USB_UnhandledControlPacket) /* Check if endpoint is empty - if so clear it and wait until ready for next packet */ if (!(Endpoint_BytesInEndpoint())) { - Endpoint_ClearSetupOUT(); - while (!(Endpoint_IsSetupOUTReceived())); + Endpoint_ClearOUT(); + while (!(Endpoint_IsOUTReceived())); } /* Write the next word into the current flash page */ @@ -286,8 +273,8 @@ EVENT_HANDLER(USB_UnhandledControlPacket) /* Check if endpoint is empty - if so clear it and wait until ready for next packet */ if (!(Endpoint_BytesInEndpoint())) { - Endpoint_ClearSetupOUT(); - while (!(Endpoint_IsSetupOUTReceived())); + Endpoint_ClearOUT(); + while (!(Endpoint_IsOUTReceived())); } /* Read the byte from the USB interface and write to to the EEPROM */ @@ -303,16 +290,17 @@ EVENT_HANDLER(USB_UnhandledControlPacket) } } - Endpoint_ClearSetupOUT(); + Endpoint_ClearOUT(); - /* Send ZLP to the host to acknowedge the request */ - Endpoint_ClearSetupIN(); + /* Acknowledge status stage */ + while (!(Endpoint_IsINReady())); + Endpoint_ClearIN(); break; case DFU_UPLOAD: - Endpoint_ClearSetupReceived(); + Endpoint_ClearSETUP(); - while (!(Endpoint_IsSetupINReady())); + while (!(Endpoint_IsINReady())); if (DFU_State != dfuUPLOAD_IDLE) { @@ -342,15 +330,15 @@ EVENT_HANDLER(USB_UnhandledControlPacket) { uint16_t Words[2]; uint32_t Long; - } CurrFlashAddress = {Words: {StartAddr, Flash64KBPage}}; + } CurrFlashAddress = {.Words = {StartAddr, Flash64KBPage}}; while (WordsRemaining--) { /* Check if endpoint is full - if so clear it and wait until ready for next packet */ if (Endpoint_BytesInEndpoint() == FIXED_CONTROL_ENDPOINT_SIZE) { - Endpoint_ClearSetupIN(); - while (!(Endpoint_IsSetupINReady())); + Endpoint_ClearIN(); + while (!(Endpoint_IsINReady())); } /* Read the flash word and send it via USB to the host */ @@ -374,8 +362,8 @@ EVENT_HANDLER(USB_UnhandledControlPacket) /* Check if endpoint is full - if so clear it and wait until ready for next packet */ if (Endpoint_BytesInEndpoint() == FIXED_CONTROL_ENDPOINT_SIZE) { - Endpoint_ClearSetupIN(); - while (!(Endpoint_IsSetupINReady())); + Endpoint_ClearIN(); + while (!(Endpoint_IsINReady())); } /* Read the EEPROM byte and send it via USB to the host */ @@ -390,15 +378,15 @@ EVENT_HANDLER(USB_UnhandledControlPacket) DFU_State = dfuIDLE; } - Endpoint_ClearSetupIN(); + Endpoint_ClearIN(); - /* Send ZLP to the host to acknowedge the request */ - while (!(Endpoint_IsSetupOUTReceived())); - Endpoint_ClearSetupOUT(); + /* Acknowledge status stage */ + while (!(Endpoint_IsOUTReceived())); + Endpoint_ClearOUT(); break; case DFU_GETSTATUS: - Endpoint_ClearSetupReceived(); + Endpoint_ClearSETUP(); /* Write 8-bit status value */ Endpoint_Write_Byte(DFU_Status); @@ -413,40 +401,46 @@ EVENT_HANDLER(USB_UnhandledControlPacket) /* Write 8-bit state string ID number */ Endpoint_Write_Byte(0); - Endpoint_ClearSetupIN(); + Endpoint_ClearIN(); - while (!(Endpoint_IsSetupOUTReceived())); - Endpoint_ClearSetupOUT(); + /* Acknowledge status stage */ + while (!(Endpoint_IsOUTReceived())); + Endpoint_ClearOUT(); break; case DFU_CLRSTATUS: - Endpoint_ClearSetupReceived(); + Endpoint_ClearSETUP(); /* Reset the status value variable to the default OK status */ DFU_Status = OK; - - Endpoint_ClearSetupIN(); + /* Acknowledge status stage */ + while (!(Endpoint_IsINReady())); + Endpoint_ClearIN(); + break; case DFU_GETSTATE: - Endpoint_ClearSetupReceived(); + Endpoint_ClearSETUP(); /* Write the current device state to the endpoint */ Endpoint_Write_Byte(DFU_State); - Endpoint_ClearSetupIN(); + Endpoint_ClearIN(); - while (!(Endpoint_IsSetupOUTReceived())); - Endpoint_ClearSetupOUT(); + /* Acknowledge status stage */ + while (!(Endpoint_IsOUTReceived())); + Endpoint_ClearOUT(); break; case DFU_ABORT: - Endpoint_ClearSetupReceived(); + Endpoint_ClearSETUP(); /* Reset the current state variable to the default idle state */ DFU_State = dfuIDLE; - Endpoint_ClearSetupIN(); + /* Acknowledge status stage */ + while (!(Endpoint_IsINReady())); + Endpoint_ClearIN(); break; } @@ -463,10 +457,10 @@ static void DiscardFillerBytes(uint8_t NumberOfBytes) { if (!(Endpoint_BytesInEndpoint())) { - Endpoint_ClearSetupOUT(); + Endpoint_ClearOUT(); /* Wait until next data packet received */ - while (!(Endpoint_IsSetupOUTReceived())); + while (!(Endpoint_IsOUTReceived())); } Endpoint_Discard_Byte(); @@ -531,15 +525,15 @@ static void LoadStartEndAddresses(void) { uint8_t Bytes[2]; uint16_t Word; - } Address[2] = {{Bytes: {SentCommand.Data[2], SentCommand.Data[1]}}, - {Bytes: {SentCommand.Data[4], SentCommand.Data[3]}}}; + } Address[2] = {{.Bytes = {SentCommand.Data[2], SentCommand.Data[1]}}, + {.Bytes = {SentCommand.Data[4], SentCommand.Data[3]}}}; /* Load in the start and ending read addresses from the sent data packet */ StartAddr = Address[0].Word; EndAddr = Address[1].Word; } -/** Handler for a Memory Program command issued by the host. This routine handles the preperations needed +/** Handler for a Memory Program command issued by the host. This routine handles the preparations needed * to write subsequent data from the host into the specified memory. */ static void ProcessMemProgCommand(void) @@ -557,7 +551,7 @@ static void ProcessMemProgCommand(void) { uint16_t Words[2]; uint32_t Long; - } CurrFlashAddress = {Words: {StartAddr, Flash64KBPage}}; + } CurrFlashAddress = {.Words = {StartAddr, Flash64KBPage}}; /* Erase the current page's temp buffer */ boot_page_erase(CurrFlashAddress.Long); @@ -569,7 +563,7 @@ static void ProcessMemProgCommand(void) } } -/** Handler for a Memory Read command issued by the host. This routine handles the preperations needed +/** Handler for a Memory Read command issued by the host. This routine handles the preparations needed * to read subsequent data from the specified memory out to the host, as well as implementing the memory * blank check command. */ @@ -639,7 +633,7 @@ static void ProcessWriteCommand(void) { uint8_t Bytes[2]; AppPtr_t FuncPtr; - } Address = {Bytes: {SentCommand.Data[4], SentCommand.Data[3]}}; + } Address = {.Bytes = {SentCommand.Data[4], SentCommand.Data[3]}}; AppStartPtr = Address.FuncPtr;