X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/e0420015df848f18d5eb1672da4ccac578d454cf..cf610c5c91c3eee02dec7ca848acc8da831ad98a:/Bootloaders/CDC/BootloaderCDC.c diff --git a/Bootloaders/CDC/BootloaderCDC.c b/Bootloaders/CDC/BootloaderCDC.c index 92c2291c9..925385d8c 100644 --- a/Bootloaders/CDC/BootloaderCDC.c +++ b/Bootloaders/CDC/BootloaderCDC.c @@ -36,7 +36,6 @@ #define INCLUDE_FROM_BOOTLOADERCDC_C #include "BootloaderCDC.h" -/* Globals: */ /** Line coding options for the virtual serial port. Although the virtual serial port data is never * sent through a physical serial port, the line encoding data must still be read and preserved from * the host, or the host will detect a problem and fail to open the port. This structure contains the @@ -116,19 +115,10 @@ void ResetHardware(void) boot_rww_enable(); } -/** Event handler for the USB_Disconnect event. This indicates that the bootloader should exit and the user - * application started. - */ -void EVENT_USB_Disconnect(void) -{ - /* Upon disconnection, run user application */ - RunBootloader = false; -} - /** Event handler for the USB_ConfigurationChanged event. This configures the device's endpoints ready * to relay data to and from the attached USB host. */ -void EVENT_USB_ConfigurationChanged(void) +void EVENT_USB_Device_ConfigurationChanged(void) { /* Setup CDC Notification, Rx and Tx Endpoints */ Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT, @@ -144,11 +134,11 @@ void EVENT_USB_ConfigurationChanged(void) ENDPOINT_BANK_SINGLE); } -/** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific +/** Event handler for the USB_UnhandledControlRequest event. This is used to catch standard and class specific * control requests that are not handled internally by the USB library, so that they can be handled appropriately * for the application. */ -void EVENT_USB_UnhandledControlPacket(void) +void EVENT_USB_Device_UnhandledControlRequest(void) { uint8_t* LineCodingData = (uint8_t*)&LineCoding; @@ -165,9 +155,7 @@ void EVENT_USB_UnhandledControlPacket(void) Endpoint_ClearIN(); - /* Acknowledge status stage */ - while (!(Endpoint_IsOUTReceived())); - Endpoint_ClearOUT(); + Endpoint_ClearStatusStage(); } break; @@ -176,16 +164,18 @@ void EVENT_USB_UnhandledControlPacket(void) { Endpoint_ClearSETUP(); - while (!(Endpoint_IsOUTReceived())); - + while (!(Endpoint_IsOUTReceived())) + { + if (USB_DeviceState == DEVICE_STATE_Unattached) + return; + } + for (uint8_t i = 0; i < sizeof(LineCoding); i++) *(LineCodingData++) = Endpoint_Read_Byte(); Endpoint_ClearOUT(); - /* Acknowledge status stage */ - while (!(Endpoint_IsINReady())); - Endpoint_ClearIN(); + Endpoint_ClearStatusStage(); } break; @@ -194,9 +184,7 @@ void EVENT_USB_UnhandledControlPacket(void) { Endpoint_ClearSETUP(); - /* Acknowledge status stage */ - while (!(Endpoint_IsINReady())); - Endpoint_ClearIN(); + Endpoint_ClearStatusStage(); } break; @@ -206,7 +194,7 @@ void EVENT_USB_UnhandledControlPacket(void) /** Reads or writes a block of EEPROM or FLASH memory to or from the appropriate CDC data endpoint, depending * on the AVR910 protocol command issued. * - * \param Command Single character AVR910 protocol command indicating what memory operation to perform + * \param[in] Command Single character AVR910 protocol command indicating what memory operation to perform */ static void ReadWriteMemoryBlock(const uint8_t Command) { @@ -237,15 +225,7 @@ static void ReadWriteMemoryBlock(const uint8_t Command) while (BlockSize--) { - if (MemoryType == 'E') - { - /* Read the next EEPROM byte into the endpoint */ - WriteNextResponseByte(eeprom_read_byte((uint8_t*)(uint16_t)(CurrAddress >> 1))); - - /* Increment the address counter after use */ - CurrAddress += 2; - } - else + if (MemoryType == 'F') { /* Read the next FLASH byte from the current FLASH page */ #if (FLASHEND > 0xFFFF) @@ -260,6 +240,14 @@ static void ReadWriteMemoryBlock(const uint8_t Command) HighByte = !HighByte; } + else + { + /* Read the next EEPROM byte into the endpoint */ + WriteNextResponseByte(eeprom_read_byte((uint8_t*)(uint16_t)(CurrAddress >> 1))); + + /* Increment the address counter after use */ + CurrAddress += 2; + } } } else @@ -333,7 +321,12 @@ static uint8_t FetchNextCommandByte(void) while (!(Endpoint_IsReadWriteAllowed())) { Endpoint_ClearOUT(); - while (!(Endpoint_IsOUTReceived())); + + while (!(Endpoint_IsOUTReceived())) + { + if (USB_DeviceState == DEVICE_STATE_Unattached) + return 0; + } } /* Fetch the next byte from the OUT endpoint */ @@ -343,7 +336,7 @@ static uint8_t FetchNextCommandByte(void) /** Writes the next response byte to the CDC data IN endpoint, and sends the endpoint back if needed to free up the * bank when full ready for the next byte in the packet to the host. * - * \param Response Next response byte to send to the host + * \param[in] Response Next response byte to send to the host */ static void WriteNextResponseByte(const uint8_t Response) { @@ -354,7 +347,12 @@ static void WriteNextResponseByte(const uint8_t Response) if (!(Endpoint_IsReadWriteAllowed())) { Endpoint_ClearIN(); - while (!(Endpoint_IsINReady())); + + while (!(Endpoint_IsINReady())) + { + if (USB_DeviceState == DEVICE_STATE_Unattached) + return; + } } /* Write the next byte to the OUT endpoint */ @@ -525,7 +523,7 @@ void CDC_Task(void) else if (Command == 'D') { /* Read the byte from the endpoint and write it to the EEPROM */ - eeprom_write_byte((uint8_t*)(uint16_t)(CurrAddress >> 1), FetchNextCommandByte()); + eeprom_write_byte((uint8_t*)((uint16_t)(CurrAddress >> 1)), FetchNextCommandByte()); /* Increment the address after use */ CurrAddress += 2; @@ -536,7 +534,7 @@ void CDC_Task(void) else if (Command == 'd') { /* Read the EEPROM byte and write it to the endpoint */ - WriteNextResponseByte(eeprom_read_byte((uint8_t*)(uint16_t)(CurrAddress >> 1))); + WriteNextResponseByte(eeprom_read_byte((uint8_t*)((uint16_t)(CurrAddress >> 1)))); /* Increment the address after use */ CurrAddress += 2; @@ -563,12 +561,21 @@ void CDC_Task(void) /* 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())); + while (!(Endpoint_IsINReady())) + { + if (USB_DeviceState == DEVICE_STATE_Unattached) + return; + } + Endpoint_ClearIN(); } /* Wait until the data has been sent to the host */ - while (!(Endpoint_IsINReady())); + while (!(Endpoint_IsINReady())) + { + if (USB_DeviceState == DEVICE_STATE_Unattached) + return; + } /* Select the OUT endpoint */ Endpoint_SelectEndpoint(CDC_RX_EPNUM);