X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/f1076ac4d6e56bff7fb6d2126746af1108211370..d26a9ed5fd6fc60a0dfa61d04f5ae2bd7163a85d:/Bootloaders/CDC/BootloaderCDC.c diff --git a/Bootloaders/CDC/BootloaderCDC.c b/Bootloaders/CDC/BootloaderCDC.c index d875b842d..482859bda 100644 --- a/Bootloaders/CDC/BootloaderCDC.c +++ b/Bootloaders/CDC/BootloaderCDC.c @@ -1,21 +1,21 @@ /* LUFA Library - Copyright (C) Dean Camera, 2009. + Copyright (C) Dean Camera, 2010. dean [at] fourwalledcubicle [dot] com www.fourwalledcubicle.com */ /* - Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com) - - Permission to use, copy, modify, and distribute this software - and its documentation for any purpose and without fee is hereby - granted, provided that the above copyright notice appear in all - copies and that both that the copyright notice and this - permission notice and warranty disclaimer appear in supporting - documentation, and that the name of the author not be used in - advertising or publicity pertaining to distribution of the + Copyright 2010 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 + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the software without specific, written prior permission. The author disclaim all warranties with regard to this @@ -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; @@ -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 */ @@ -350,11 +343,16 @@ static void WriteNextResponseByte(const uint8_t Response) /* Select the IN endpoint so that the next data byte can be written */ Endpoint_SelectEndpoint(CDC_TX_EPNUM); - /* If IN endpoint full, clear it and wait util ready for the next packet to the host */ + /* If IN endpoint full, clear it and wait until ready for the next packet to the host */ 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);