X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/071e02c6b6b4837fa9cf0b6d4c749994e02638d7..bea72a8412f99b294c00341fa16a8308bcc66f15:/Bootloaders/CDC/BootloaderCDC.c diff --git a/Bootloaders/CDC/BootloaderCDC.c b/Bootloaders/CDC/BootloaderCDC.c index b2a278ed7..234b5ce88 100644 --- a/Bootloaders/CDC/BootloaderCDC.c +++ b/Bootloaders/CDC/BootloaderCDC.c @@ -36,16 +36,13 @@ #define INCLUDE_FROM_BOOTLOADERCDC_C #include "BootloaderCDC.h" -/** 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 - * current encoding options, including baud rate, character format, parity mode and total number of - * bits in each data chunk. +/** 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 LineCoding = { .BaudRateBPS = 9600, - .CharFormat = OneStopBit, - .ParityType = Parity_None, - .DataBits = 8 }; +CDC_Line_Coding_t LineEncoding = { .BaudRateBPS = 0, + .CharFormat = OneStopBit, + .ParityType = 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 @@ -112,40 +109,35 @@ void EVENT_USB_Device_ConfigurationChanged(void) { /* Setup CDC Notification, Rx and Tx Endpoints */ Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT, - ENDPOINT_DIR_IN, CDC_NOTIFICATION_EPSIZE, + ENDPOINT_DIR_IN, CDC_NOTIFICATION_EPSIZE, ENDPOINT_BANK_SINGLE); Endpoint_ConfigureEndpoint(CDC_TX_EPNUM, EP_TYPE_BULK, - ENDPOINT_DIR_IN, CDC_TXRX_EPSIZE, + ENDPOINT_DIR_IN, CDC_TXRX_EPSIZE, ENDPOINT_BANK_SINGLE); Endpoint_ConfigureEndpoint(CDC_RX_EPNUM, EP_TYPE_BULK, - ENDPOINT_DIR_OUT, CDC_TXRX_EPSIZE, + ENDPOINT_DIR_OUT, CDC_TXRX_EPSIZE, ENDPOINT_BANK_SINGLE); } /** 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. + * control requests that are not handled internally by the USB library (including the CDC control commands, + * which are all issued via the control endpoint), so that they can be handled appropriately for the application. */ void EVENT_USB_Device_UnhandledControlRequest(void) { - uint8_t* LineCodingData = (uint8_t*)&LineCoding; - /* Process CDC specific control requests */ switch (USB_ControlRequest.bRequest) { case REQ_GetLineEncoding: if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) - { + { Endpoint_ClearSETUP(); - for (uint8_t i = 0; i < sizeof(LineCoding); i++) - Endpoint_Write_Byte(*(LineCodingData++)); - - Endpoint_ClearIN(); - - Endpoint_ClearStatusStage(); + /* Write the line coding data to the control endpoint */ + Endpoint_Write_Control_Stream_LE(&LineEncoding, sizeof(CDC_Line_Coding_t)); + Endpoint_ClearOUT(); } break; @@ -154,27 +146,9 @@ void EVENT_USB_Device_UnhandledControlRequest(void) { Endpoint_ClearSETUP(); - 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(); - - Endpoint_ClearStatusStage(); - } - - break; - case REQ_SetControlLineState: - if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) - { - Endpoint_ClearSETUP(); - - Endpoint_ClearStatusStage(); + /* Read the line coding data in from the host into the global struct */ + Endpoint_Read_Control_Stream_LE(&LineEncoding, sizeof(CDC_Line_Coding_t)); + Endpoint_ClearIN(); } break; @@ -233,7 +207,7 @@ static void ReadWriteMemoryBlock(const uint8_t Command) else { /* Read the next EEPROM byte into the endpoint */ - WriteNextResponseByte(eeprom_read_byte((uint8_t*)(uint16_t)(CurrAddress >> 1))); + WriteNextResponseByte(eeprom_read_byte((uint8_t*)(intptr_t)(CurrAddress >> 1))); /* Increment the address counter after use */ CurrAddress += 2; @@ -275,7 +249,7 @@ static void ReadWriteMemoryBlock(const uint8_t Command) else { /* Write the next EEPROM byte from the endpoint */ - eeprom_write_byte((uint8_t*)(uint16_t)(CurrAddress >> 1), FetchNextCommandByte()); + eeprom_write_byte((uint8_t*)((intptr_t)(CurrAddress >> 1)), FetchNextCommandByte()); /* Increment the address counter after use */ CurrAddress += 2; @@ -339,7 +313,7 @@ static void WriteNextResponseByte(const uint8_t Response) Endpoint_ClearIN(); while (!(Endpoint_IsINReady())) - { + { if (USB_DeviceState == DEVICE_STATE_Unattached) return; } @@ -367,7 +341,7 @@ void CDC_Task(void) { if (Command == 'E') RunBootloader = false; - if (Command == 'T') + else if (Command == 'T') FetchNextCommandByte(); /* Send confirmation byte back to the host */ @@ -377,7 +351,6 @@ void CDC_Task(void) { /* Return ATMEGA128 part code - this is only to allow AVRProg to use the bootloader */ WriteNextResponseByte(0x44); - WriteNextResponseByte(0x00); } else if (Command == 'a') @@ -513,7 +486,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*)((intptr_t)(CurrAddress >> 1)), FetchNextCommandByte()); /* Increment the address after use */ CurrAddress += 2; @@ -524,7 +497,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*)((intptr_t)(CurrAddress >> 1)))); /* Increment the address after use */ CurrAddress += 2;