123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673 |
- #define INCLUDE_FROM_BOOTLOADERCDC_C
- #include "BootloaderCDC.h"
- static CDC_LineEncoding_t LineEncoding = { .BaudRateBPS = 0,
- .CharFormat = CDC_LINEENCODING_OneStopBit,
- .ParityType = CDC_PARITY_None,
- .DataBits = 8 };
- static uint32_t CurrAddress;
- static bool RunBootloader = true;
- uint16_t MagicBootKey ATTR_NO_INIT;
- void Application_Jump_Check(void)
- {
- bool JumpToApplication = false;
- #if (BOARD == BOARD_LEONARDO)
-
- PORTC |= (1 << 7);
- Delay_MS(10);
-
- JumpToApplication = ((PINC & (1 << 7)) != 0);
-
- PORTC &= ~(1 << 7);
- #elif ((BOARD == BOARD_XPLAIN) || (BOARD == BOARD_XPLAIN_REV1))
-
- JTAG_DISABLE();
-
- PORTF |= (1 << 4);
- Delay_MS(10);
-
- JumpToApplication = ((PINF & (1 << 4)) != 0);
-
- JTAG_ENABLE();
- #else
-
- if (boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS) & FUSE_BOOTRST)
- {
-
- if (!(MCUSR & (1 << EXTRF)) || (MagicBootKey == MAGIC_BOOT_KEY))
- JumpToApplication = true;
-
- MCUSR &= ~(1 << EXTRF);
- }
- else
- {
-
- if ((MCUSR & (1 << WDRF)) && (MagicBootKey == MAGIC_BOOT_KEY))
- JumpToApplication = true;
-
- MCUSR &= ~(1 << WDRF);
- }
- #endif
-
- bool ApplicationValid = (pgm_read_word_near(0) != 0xFFFF);
-
- if (JumpToApplication && ApplicationValid)
- {
-
- MCUSR &= ~(1 << WDRF);
- wdt_disable();
-
- MagicBootKey = 0;
-
- ((void (*)(void))0x0000)();
- }
- }
- int main(void)
- {
-
- SetupHardware();
-
- LEDs_SetAllLEDs(LEDS_LED1);
-
- GlobalInterruptEnable();
- while (RunBootloader)
- {
- CDC_Task();
- USB_USBTask();
- }
-
- _delay_us(1000);
-
- USB_Detach();
-
- MagicBootKey = MAGIC_BOOT_KEY;
-
- wdt_enable(WDTO_250MS);
- for (;;);
- }
- static void SetupHardware(void)
- {
-
- MCUSR &= ~(1 << WDRF);
- wdt_disable();
-
- clock_prescale_set(clock_div_1);
-
- MCUCR = (1 << IVCE);
- MCUCR = (1 << IVSEL);
-
- USB_Init();
- LEDs_Init();
-
- TIMSK1 = (1 << TOIE1);
- TCCR1B = ((1 << CS11) | (1 << CS10));
- }
- ISR(TIMER1_OVF_vect, ISR_BLOCK)
- {
- LEDs_ToggleLEDs(LEDS_LED1 | LEDS_LED2);
- }
- void EVENT_USB_Device_ConfigurationChanged(void)
- {
-
- Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPADDR, EP_TYPE_INTERRUPT,
- CDC_NOTIFICATION_EPSIZE, 1);
- Endpoint_ConfigureEndpoint(CDC_TX_EPADDR, EP_TYPE_BULK, CDC_TXRX_EPSIZE, 1);
- Endpoint_ConfigureEndpoint(CDC_RX_EPADDR, EP_TYPE_BULK, CDC_TXRX_EPSIZE, 1);
- }
- void EVENT_USB_Device_ControlRequest(void)
- {
-
- if ((USB_ControlRequest.bmRequestType & (CONTROL_REQTYPE_TYPE | CONTROL_REQTYPE_RECIPIENT)) !=
- (REQTYPE_CLASS | REQREC_INTERFACE))
- {
- return;
- }
-
- LEDs_ToggleLEDs(LEDS_LED1 | LEDS_LED2);
-
- switch (USB_ControlRequest.bRequest)
- {
- case CDC_REQ_GetLineEncoding:
- if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
- {
- Endpoint_ClearSETUP();
-
- Endpoint_Write_Control_Stream_LE(&LineEncoding, sizeof(CDC_LineEncoding_t));
- Endpoint_ClearOUT();
- }
- break;
- case CDC_REQ_SetLineEncoding:
- if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
- {
- Endpoint_ClearSETUP();
-
- Endpoint_Read_Control_Stream_LE(&LineEncoding, sizeof(CDC_LineEncoding_t));
- Endpoint_ClearIN();
- }
- break;
- case CDC_REQ_SetControlLineState:
- if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
- {
- Endpoint_ClearSETUP();
- Endpoint_ClearStatusStage();
- }
- break;
- }
- }
- #if !defined(NO_BLOCK_SUPPORT)
- static void ReadWriteMemoryBlock(const uint8_t Command)
- {
- uint16_t BlockSize;
- char MemoryType;
- uint8_t HighByte = 0;
- uint8_t LowByte = 0;
- BlockSize = (FetchNextCommandByte() << 8);
- BlockSize |= FetchNextCommandByte();
- MemoryType = FetchNextCommandByte();
- if ((MemoryType != MEMORY_TYPE_FLASH) && (MemoryType != MEMORY_TYPE_EEPROM))
- {
-
- WriteNextResponseByte('?');
- return;
- }
-
- if (Command == AVR109_COMMAND_BlockRead)
- {
-
- boot_rww_enable();
- while (BlockSize--)
- {
- if (MemoryType == MEMORY_TYPE_FLASH)
- {
-
- #if (FLASHEND > 0xFFFF)
- WriteNextResponseByte(pgm_read_byte_far(CurrAddress | HighByte));
- #else
- WriteNextResponseByte(pgm_read_byte(CurrAddress | HighByte));
- #endif
-
- if (HighByte)
- CurrAddress += 2;
- HighByte = !HighByte;
- }
- else
- {
-
- WriteNextResponseByte(eeprom_read_byte((uint8_t*)(intptr_t)(CurrAddress >> 1)));
-
- CurrAddress += 2;
- }
- }
- }
- else
- {
- uint32_t PageStartAddress = CurrAddress;
- if (MemoryType == MEMORY_TYPE_FLASH)
- {
- boot_page_erase(PageStartAddress);
- boot_spm_busy_wait();
- }
- while (BlockSize--)
- {
- if (MemoryType == MEMORY_TYPE_FLASH)
- {
-
- if (HighByte)
- {
-
- boot_page_fill(CurrAddress, ((FetchNextCommandByte() << 8) | LowByte));
-
- CurrAddress += 2;
- }
- else
- {
- LowByte = FetchNextCommandByte();
- }
- HighByte = !HighByte;
- }
- else
- {
-
- eeprom_update_byte((uint8_t*)((intptr_t)(CurrAddress >> 1)), FetchNextCommandByte());
-
- CurrAddress += 2;
- }
- }
-
- if (MemoryType == MEMORY_TYPE_FLASH)
- {
-
- boot_page_write(PageStartAddress);
-
- boot_spm_busy_wait();
- }
-
- WriteNextResponseByte('\r');
- }
- }
- #endif
- static uint8_t FetchNextCommandByte(void)
- {
-
- Endpoint_SelectEndpoint(CDC_RX_EPADDR);
-
- while (!(Endpoint_IsReadWriteAllowed()))
- {
- Endpoint_ClearOUT();
- while (!(Endpoint_IsOUTReceived()))
- {
- if (USB_DeviceState == DEVICE_STATE_Unattached)
- return 0;
- }
- }
-
- return Endpoint_Read_8();
- }
- static void WriteNextResponseByte(const uint8_t Response)
- {
-
- Endpoint_SelectEndpoint(CDC_TX_EPADDR);
-
- if (!(Endpoint_IsReadWriteAllowed()))
- {
- Endpoint_ClearIN();
- while (!(Endpoint_IsINReady()))
- {
- if (USB_DeviceState == DEVICE_STATE_Unattached)
- return;
- }
- }
-
- Endpoint_Write_8(Response);
- }
- static void CDC_Task(void)
- {
-
- Endpoint_SelectEndpoint(CDC_RX_EPADDR);
-
- if (!(Endpoint_IsOUTReceived()))
- return;
-
- uint8_t Command = FetchNextCommandByte();
- if (Command == AVR109_COMMAND_ExitBootloader)
- {
- RunBootloader = false;
-
- WriteNextResponseByte('\r');
- }
- else if ((Command == AVR109_COMMAND_SetLED) || (Command == AVR109_COMMAND_ClearLED) ||
- (Command == AVR109_COMMAND_SelectDeviceType))
- {
- FetchNextCommandByte();
-
- WriteNextResponseByte('\r');
- }
- else if ((Command == AVR109_COMMAND_EnterProgrammingMode) || (Command == AVR109_COMMAND_LeaveProgrammingMode))
- {
-
- WriteNextResponseByte('\r');
- }
- else if (Command == AVR109_COMMAND_ReadPartCode)
- {
-
- WriteNextResponseByte(0x44);
- WriteNextResponseByte(0x00);
- }
- else if (Command == AVR109_COMMAND_ReadAutoAddressIncrement)
- {
-
- WriteNextResponseByte('Y');
- }
- else if (Command == AVR109_COMMAND_SetCurrentAddress)
- {
-
- CurrAddress = (FetchNextCommandByte() << 9);
- CurrAddress |= (FetchNextCommandByte() << 1);
-
- WriteNextResponseByte('\r');
- }
- else if (Command == AVR109_COMMAND_ReadBootloaderInterface)
- {
-
- WriteNextResponseByte('S');
- }
- else if (Command == AVR109_COMMAND_ReadBootloaderIdentifier)
- {
-
- for (uint8_t CurrByte = 0; CurrByte < 7; CurrByte++)
- WriteNextResponseByte(SOFTWARE_IDENTIFIER[CurrByte]);
- }
- else if (Command == AVR109_COMMAND_ReadBootloaderSWVersion)
- {
- WriteNextResponseByte('0' + BOOTLOADER_VERSION_MAJOR);
- WriteNextResponseByte('0' + BOOTLOADER_VERSION_MINOR);
- }
- else if (Command == AVR109_COMMAND_ReadSignature)
- {
- WriteNextResponseByte(AVR_SIGNATURE_3);
- WriteNextResponseByte(AVR_SIGNATURE_2);
- WriteNextResponseByte(AVR_SIGNATURE_1);
- }
- else if (Command == AVR109_COMMAND_EraseFLASH)
- {
-
- for (uint32_t CurrFlashAddress = 0; CurrFlashAddress < (uint32_t)BOOT_START_ADDR; CurrFlashAddress += SPM_PAGESIZE)
- {
- boot_page_erase(CurrFlashAddress);
- boot_spm_busy_wait();
- boot_page_write(CurrFlashAddress);
- boot_spm_busy_wait();
- }
-
- WriteNextResponseByte('\r');
- }
- #if !defined(NO_LOCK_BYTE_WRITE_SUPPORT)
- else if (Command == AVR109_COMMAND_WriteLockbits)
- {
-
- boot_lock_bits_set(FetchNextCommandByte());
-
- WriteNextResponseByte('\r');
- }
- #endif
- else if (Command == AVR109_COMMAND_ReadLockbits)
- {
- WriteNextResponseByte(boot_lock_fuse_bits_get(GET_LOCK_BITS));
- }
- else if (Command == AVR109_COMMAND_ReadLowFuses)
- {
- WriteNextResponseByte(boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS));
- }
- else if (Command == AVR109_COMMAND_ReadHighFuses)
- {
- WriteNextResponseByte(boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS));
- }
- else if (Command == AVR109_COMMAND_ReadExtendedFuses)
- {
- WriteNextResponseByte(boot_lock_fuse_bits_get(GET_EXTENDED_FUSE_BITS));
- }
- #if !defined(NO_BLOCK_SUPPORT)
- else if (Command == AVR109_COMMAND_GetBlockWriteSupport)
- {
- WriteNextResponseByte('Y');
-
- WriteNextResponseByte(SPM_PAGESIZE >> 8);
- WriteNextResponseByte(SPM_PAGESIZE & 0xFF);
- }
- else if ((Command == AVR109_COMMAND_BlockWrite) || (Command == AVR109_COMMAND_BlockRead))
- {
-
- ReadWriteMemoryBlock(Command);
- }
- #endif
- #if !defined(NO_FLASH_BYTE_SUPPORT)
- else if (Command == AVR109_COMMAND_FillFlashPageWordHigh)
- {
-
- boot_page_fill(CurrAddress, FetchNextCommandByte());
-
- WriteNextResponseByte('\r');
- }
- else if (Command == AVR109_COMMAND_FillFlashPageWordLow)
- {
-
- boot_page_fill(CurrAddress | 0x01, FetchNextCommandByte());
-
- CurrAddress += 2;
-
- WriteNextResponseByte('\r');
- }
- else if (Command == AVR109_COMMAND_WriteFlashPage)
- {
-
- boot_page_write(CurrAddress);
-
- boot_spm_busy_wait();
-
- WriteNextResponseByte('\r');
- }
- else if (Command == AVR109_COMMAND_ReadFLASHWord)
- {
- #if (FLASHEND > 0xFFFF)
- uint16_t ProgramWord = pgm_read_word_far(CurrAddress);
- #else
- uint16_t ProgramWord = pgm_read_word(CurrAddress);
- #endif
- WriteNextResponseByte(ProgramWord >> 8);
- WriteNextResponseByte(ProgramWord & 0xFF);
- }
- #endif
- #if !defined(NO_EEPROM_BYTE_SUPPORT)
- else if (Command == AVR109_COMMAND_WriteEEPROM)
- {
-
- eeprom_update_byte((uint8_t*)((intptr_t)(CurrAddress >> 1)), FetchNextCommandByte());
-
- CurrAddress += 2;
-
- WriteNextResponseByte('\r');
- }
- else if (Command == AVR109_COMMAND_ReadEEPROM)
- {
-
- WriteNextResponseByte(eeprom_read_byte((uint8_t*)((intptr_t)(CurrAddress >> 1))));
-
- CurrAddress += 2;
- }
- #endif
- else if (Command != AVR109_COMMAND_Sync)
- {
-
- WriteNextResponseByte('?');
- }
-
- Endpoint_SelectEndpoint(CDC_TX_EPADDR);
-
- bool IsEndpointFull = !(Endpoint_IsReadWriteAllowed());
-
- Endpoint_ClearIN();
-
- if (IsEndpointFull)
- {
- while (!(Endpoint_IsINReady()))
- {
- if (USB_DeviceState == DEVICE_STATE_Unattached)
- return;
- }
- Endpoint_ClearIN();
- }
-
- while (!(Endpoint_IsINReady()))
- {
- if (USB_DeviceState == DEVICE_STATE_Unattached)
- return;
- }
-
- Endpoint_SelectEndpoint(CDC_RX_EPADDR);
-
- Endpoint_ClearOUT();
- }
|