Commit 071e02c6 authored by Dean Camera's avatar Dean Camera
Browse files

Add svn:eol-style property to source files, so that the line endings are...

Add svn:eol-style property to source files, so that the line endings are correctly converted to the target system's native end of line style.
parent e331b531
/* /*
LUFA Library LUFA Library
Copyright (C) Dean Camera, 2010. Copyright (C) Dean Camera, 2010.
dean [at] fourwalledcubicle [dot] com dean [at] fourwalledcubicle [dot] com
www.fourwalledcubicle.com www.fourwalledcubicle.com
*/ */
/* /*
Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, distribute, and sell this Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in without fee, provided that the above copyright notice appear in
all copies and that both that the copyright notice and this all copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the advertising or publicity pertaining to distribution of the
software without specific, written prior permission. software without specific, written prior permission.
The author disclaim all warranties with regard to this The author disclaim all warranties with regard to this
software, including all implied warranties of merchantability software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action, in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of arising out of or in connection with the use or performance of
this software. this software.
*/ */
/** \file /** \file
* *
* Main source file for the CDC class bootloader. This file contains the complete bootloader logic. * Main source file for the CDC class bootloader. This file contains the complete bootloader logic.
*/ */
#define INCLUDE_FROM_BOOTLOADERCDC_C #define INCLUDE_FROM_BOOTLOADERCDC_C
#include "BootloaderCDC.h" #include "BootloaderCDC.h"
/** Line coding options for the virtual serial port. Although the virtual serial port data is never /** 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 * 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 * 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 * current encoding options, including baud rate, character format, parity mode and total number of
* bits in each data chunk. * bits in each data chunk.
*/ */
CDC_Line_Coding_t LineCoding = { .BaudRateBPS = 9600, CDC_Line_Coding_t LineCoding = { .BaudRateBPS = 9600,
.CharFormat = OneStopBit, .CharFormat = OneStopBit,
.ParityType = Parity_None, .ParityType = Parity_None,
.DataBits = 8 }; .DataBits = 8 };
/** Current address counter. This stores the current address of the FLASH or EEPROM as set by the host, /** 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 * and is used when reading or writing to the AVRs memory (either FLASH or EEPROM depending on the issued
* command.) * command.)
*/ */
uint32_t CurrAddress; uint32_t CurrAddress;
/** Flag to indicate if the bootloader should be running, or should exit and allow the application code to run /** 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 * 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. * loop until the AVR restarts and the application runs.
*/ */
bool RunBootloader = true; bool RunBootloader = true;
/** Main program entry point. This routine configures the hardware required by the bootloader, then continuously /** Main program entry point. This routine configures the hardware required by the bootloader, then continuously
* runs the bootloader processing routine until instructed to soft-exit, or hard-reset via the watchdog to start * runs the bootloader processing routine until instructed to soft-exit, or hard-reset via the watchdog to start
* the loaded application code. * the loaded application code.
*/ */
int main(void) int main(void)
{ {
/* Setup hardware required for the bootloader */ /* Setup hardware required for the bootloader */
SetupHardware(); SetupHardware();
/* Enable global interrupts so that the USB stack can function */ /* Enable global interrupts so that the USB stack can function */
sei(); sei();
while (RunBootloader) while (RunBootloader)
{ {
CDC_Task(); CDC_Task();
USB_USBTask(); USB_USBTask();
} }
/* Disconnect from the host - USB interface will be reset later along with the AVR */ /* Disconnect from the host - USB interface will be reset later along with the AVR */
USB_Detach(); USB_Detach();
/* Enable the watchdog and force a timeout to reset the AVR */ /* Enable the watchdog and force a timeout to reset the AVR */
wdt_enable(WDTO_250MS); wdt_enable(WDTO_250MS);
for (;;); for (;;);
} }
/** Configures all hardware required for the bootloader. */ /** Configures all hardware required for the bootloader. */
void SetupHardware(void) void SetupHardware(void)
{ {
/* Disable watchdog if enabled by bootloader/fuses */ /* Disable watchdog if enabled by bootloader/fuses */
MCUSR &= ~(1 << WDRF); MCUSR &= ~(1 << WDRF);
wdt_disable(); wdt_disable();
/* Disable clock division */ /* Disable clock division */
clock_prescale_set(clock_div_1); clock_prescale_set(clock_div_1);
/* Relocate the interrupt vector table to the bootloader section */ /* Relocate the interrupt vector table to the bootloader section */
MCUCR = (1 << IVCE); MCUCR = (1 << IVCE);
MCUCR = (1 << IVSEL); MCUCR = (1 << IVSEL);
/* Initialize USB Subsystem */ /* Initialize USB Subsystem */
USB_Init(); USB_Init();
} }
/** Event handler for the USB_ConfigurationChanged event. This configures the device's endpoints ready /** Event handler for the USB_ConfigurationChanged event. This configures the device's endpoints ready
* to relay data to and from the attached USB host. * to relay data to and from the attached USB host.
*/ */
void EVENT_USB_Device_ConfigurationChanged(void) void EVENT_USB_Device_ConfigurationChanged(void)
{ {
/* Setup CDC Notification, Rx and Tx Endpoints */ /* Setup CDC Notification, Rx and Tx Endpoints */
Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT, Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT,
ENDPOINT_DIR_IN, CDC_NOTIFICATION_EPSIZE, ENDPOINT_DIR_IN, CDC_NOTIFICATION_EPSIZE,
ENDPOINT_BANK_SINGLE); ENDPOINT_BANK_SINGLE);
Endpoint_ConfigureEndpoint(CDC_TX_EPNUM, EP_TYPE_BULK, Endpoint_ConfigureEndpoint(CDC_TX_EPNUM, EP_TYPE_BULK,
ENDPOINT_DIR_IN, CDC_TXRX_EPSIZE, ENDPOINT_DIR_IN, CDC_TXRX_EPSIZE,
ENDPOINT_BANK_SINGLE); ENDPOINT_BANK_SINGLE);
Endpoint_ConfigureEndpoint(CDC_RX_EPNUM, EP_TYPE_BULK, Endpoint_ConfigureEndpoint(CDC_RX_EPNUM, EP_TYPE_BULK,
ENDPOINT_DIR_OUT, CDC_TXRX_EPSIZE, ENDPOINT_DIR_OUT, CDC_TXRX_EPSIZE,
ENDPOINT_BANK_SINGLE); ENDPOINT_BANK_SINGLE);
} }
/** Event handler for the USB_UnhandledControlRequest 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 * control requests that are not handled internally by the USB library, so that they can be handled appropriately
* for the application. * for the application.
*/ */
void EVENT_USB_Device_UnhandledControlRequest(void) void EVENT_USB_Device_UnhandledControlRequest(void)
{ {
uint8_t* LineCodingData = (uint8_t*)&LineCoding; uint8_t* LineCodingData = (uint8_t*)&LineCoding;
/* Process CDC specific control requests */ /* Process CDC specific control requests */
switch (USB_ControlRequest.bRequest) switch (USB_ControlRequest.bRequest)
{ {
case REQ_GetLineEncoding: case REQ_GetLineEncoding:
if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
{ {
Endpoint_ClearSETUP(); Endpoint_ClearSETUP();
for (uint8_t i = 0; i < sizeof(LineCoding); i++) for (uint8_t i = 0; i < sizeof(LineCoding); i++)
Endpoint_Write_Byte(*(LineCodingData++)); Endpoint_Write_Byte(*(LineCodingData++));
Endpoint_ClearIN(); Endpoint_ClearIN();
Endpoint_ClearStatusStage(); Endpoint_ClearStatusStage();
} }
break; break;
case REQ_SetLineEncoding: case REQ_SetLineEncoding:
if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
{ {
Endpoint_ClearSETUP(); Endpoint_ClearSETUP();
while (!(Endpoint_IsOUTReceived())) while (!(Endpoint_IsOUTReceived()))
{ {
if (USB_DeviceState == DEVICE_STATE_Unattached) if (USB_DeviceState == DEVICE_STATE_Unattached)
return; return;
} }
for (uint8_t i = 0; i < sizeof(LineCoding); i++) for (uint8_t i = 0; i < sizeof(LineCoding); i++)
*(LineCodingData++) = Endpoint_Read_Byte(); *(LineCodingData++) = Endpoint_Read_Byte();
Endpoint_ClearOUT(); Endpoint_ClearOUT();
Endpoint_ClearStatusStage(); Endpoint_ClearStatusStage();
} }
break; break;
case REQ_SetControlLineState: case REQ_SetControlLineState:
if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
{ {
Endpoint_ClearSETUP(); Endpoint_ClearSETUP();
Endpoint_ClearStatusStage(); Endpoint_ClearStatusStage();
} }
break; break;
} }
} }
/** Reads or writes a block of EEPROM or FLASH memory to or from the appropriate CDC data endpoint, depending /** 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. * on the AVR910 protocol command issued.
* *
* \param[in] 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) static void ReadWriteMemoryBlock(const uint8_t Command)
{ {
uint16_t BlockSize; uint16_t BlockSize;
char MemoryType; char MemoryType;
bool HighByte = false; bool HighByte = false;
uint8_t LowByte = 0; uint8_t LowByte = 0;
BlockSize = (FetchNextCommandByte() << 8); BlockSize = (FetchNextCommandByte() << 8);
BlockSize |= FetchNextCommandByte(); BlockSize |= FetchNextCommandByte();
MemoryType = FetchNextCommandByte(); MemoryType = FetchNextCommandByte();
if ((MemoryType != 'E') && (MemoryType != 'F')) if ((MemoryType != 'E') && (MemoryType != 'F'))
{ {
/* Send error byte back to the host */ /* Send error byte back to the host */
WriteNextResponseByte('?'); WriteNextResponseByte('?');
return; return;
} }
/* Check if command is to read memory */ /* Check if command is to read memory */
if (Command == 'g') if (Command == 'g')
{ {
/* Re-enable RWW section */ /* Re-enable RWW section */
boot_rww_enable(); boot_rww_enable();
while (BlockSize--) while (BlockSize--)
{ {
if (MemoryType == 'F') if (MemoryType == 'F')
{ {
/* Read the next FLASH byte from the current FLASH page */ /* Read the next FLASH byte from the current FLASH page */
#if (FLASHEND > 0xFFFF) #if (FLASHEND > 0xFFFF)
WriteNextResponseByte(pgm_read_byte_far(CurrAddress | HighByte)); WriteNextResponseByte(pgm_read_byte_far(CurrAddress | HighByte));
#else #else
WriteNextResponseByte(pgm_read_byte(CurrAddress | HighByte)); WriteNextResponseByte(pgm_read_byte(CurrAddress | HighByte));
#endif #endif
/* If both bytes in current word have been read, increment the address counter */ /* If both bytes in current word have been read, increment the address counter */
if (HighByte) if (HighByte)
CurrAddress += 2; CurrAddress += 2;
HighByte = !HighByte; HighByte = !HighByte;
} }
else else
{ {
/* Read the next EEPROM byte into the endpoint */ /* Read the next EEPROM byte into 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 counter after use */ /* Increment the address counter after use */
CurrAddress += 2; CurrAddress += 2;
} }
} }
} }
else else
{ {
uint32_t PageStartAddress = CurrAddress; uint32_t PageStartAddress = CurrAddress;
if (MemoryType == 'F') if (MemoryType == 'F')
{ {
boot_page_erase(PageStartAddress); boot_page_erase(PageStartAddress);
boot_spm_busy_wait(); boot_spm_busy_wait();
} }
while (BlockSize--) while (BlockSize--)
{ {
if (MemoryType == 'F') if (MemoryType == 'F')
{ {
/* If both bytes in current word have been written, increment the address counter */ /* If both bytes in current word have been written, increment the address counter */
if (HighByte) if (HighByte)
{ {
/* Write the next FLASH word to the current FLASH page */ /* Write the next FLASH word to the current FLASH page */
boot_page_fill(CurrAddress, ((FetchNextCommandByte() << 8) | LowByte)); boot_page_fill(CurrAddress, ((FetchNextCommandByte() << 8) | LowByte));
/* Increment the address counter after use */ /* Increment the address counter after use */
CurrAddress += 2; CurrAddress += 2;
HighByte = false; HighByte = false;
} }
else else
{ {
LowByte = FetchNextCommandByte(); LowByte = FetchNextCommandByte();
HighByte = true; HighByte = true;
} }
} }
else else
{ {
/* Write the next EEPROM byte from the endpoint */ /* Write the next EEPROM byte from the endpoint */
eeprom_write_byte((uint8_t*)(uint16_t)(CurrAddress >> 1), FetchNextCommandByte()); eeprom_write_byte((uint8_t*)(uint16_t)(CurrAddress >> 1), FetchNextCommandByte());
/* Increment the address counter after use */ /* Increment the address counter after use */
CurrAddress += 2; CurrAddress += 2;
} }
} }
/* If in FLASH programming mode, commit the page after writing */ /* If in FLASH programming mode, commit the page after writing */
if (MemoryType == 'F') if (MemoryType == 'F')
{ {
/* Commit the flash page to memory */ /* Commit the flash page to memory */
boot_page_write(PageStartAddress); boot_page_write(PageStartAddress);
/* Wait until write operation has completed */ /* Wait until write operation has completed */
boot_spm_busy_wait(); boot_spm_busy_wait();
} }
/* Send response byte back to the host */ /* Send response byte back to the host */
WriteNextResponseByte('\r'); WriteNextResponseByte('\r');
} }
} }
/** Retrieves the next byte from the host in the CDC data OUT endpoint, and clears the endpoint bank if needed /** Retrieves the next byte from the host in the CDC data OUT endpoint, and clears the endpoint bank if needed
* to allow reception of the next data packet from the host. * to allow reception of the next data packet from the host.
* *
* \return Next received byte from the host in the CDC data OUT endpoint * \return Next received byte from the host in the CDC data OUT endpoint
*/ */
static uint8_t FetchNextCommandByte(void) static uint8_t FetchNextCommandByte(void)
{ {
/* Select the OUT endpoint so that the next data byte can be read */ /* Select the OUT endpoint so that the next data byte can be read */
Endpoint_SelectEndpoint(CDC_RX_EPNUM); Endpoint_SelectEndpoint(CDC_RX_EPNUM);
/* If OUT endpoint empty, clear it and wait for the next packet from the host */ /* If OUT endpoint empty, clear it and wait for the next packet from the host */
while (!(Endpoint_IsReadWriteAllowed())) while (!(Endpoint_IsReadWriteAllowed()))
{ {
Endpoint_ClearOUT(); Endpoint_ClearOUT();
while (!(Endpoint_IsOUTReceived())) while (!(Endpoint_IsOUTReceived()))
{ {
if (USB_DeviceState == DEVICE_STATE_Unattached) if (USB_DeviceState == DEVICE_STATE_Unattached)
return 0; return 0;
} }
} }
/* Fetch the next byte from the OUT endpoint */ /* Fetch the next byte from the OUT endpoint */
return Endpoint_Read_Byte(); return Endpoint_Read_Byte();
} }
/** Writes the next response byte to the CDC data IN endpoint, and sends the endpoint back if needed to free up the /** 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. * bank when full ready for the next byte in the packet to the host.
* *
* \param[in] 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) static void WriteNextResponseByte(const uint8_t Response)
{ {
/* Select the IN endpoint so that the next data byte can be written */ /* Select the IN endpoint so that the next data byte can be written */
Endpoint_SelectEndpoint(CDC_TX_EPNUM); Endpoint_SelectEndpoint(CDC_TX_EPNUM);
/* If IN endpoint full, clear it and wait until 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())) if (!(Endpoint_IsReadWriteAllowed()))
{ {
Endpoint_ClearIN(); Endpoint_ClearIN();
while (!(Endpoint_IsINReady())) while (!(Endpoint_IsINReady()))
{