From 7301aa596f0e088826bcbf698ca83686f771c9dc Mon Sep 17 00:00:00 2001 From: cpldcpu Date: Mon, 3 Feb 2014 18:39:10 +0100 Subject: firmware: removed debugging output, clean up logic --- firmware/main.c | 53 +++++++++++++++++++++-------------------------------- 1 file changed, 21 insertions(+), 32 deletions(-) diff --git a/firmware/main.c b/firmware/main.c index 5381cee..f85e9ca 100644 --- a/firmware/main.c +++ b/firmware/main.c @@ -204,7 +204,7 @@ static inline void leaveBootloader(void) { bootLoaderExit(); - _delay_ms(10); // Bus needs to see a few more SOFs before it can be disconnected + // _delay_ms(10); // Bus needs to see a few more SOFs before it can be disconnected usbDeviceDisconnect(); /* Disconnect micronucleus */ USB_INTR_ENABLE = 0; @@ -230,27 +230,26 @@ static inline void leaveBootloader(void) { void USB_INTR_VECTOR(void); int main(void) { - uint8_t ackSent=0; bootLoaderInit(); - - DDRB|=3; - + if (bootLoaderStartCondition()||(pgm_read_byte(BOOTLOADER_ADDRESS - TINYVECTOR_RESET_OFFSET + 1)==0xff)) { initHardware(); LED_INIT(); if (AUTO_EXIT_NO_USB_MS>0) { - idlePolls.b[1]=((AUTO_EXIT_MS-AUTO_EXIT_NO_USB_MS) * 10UL)>>8; + idlePolls.b[1]=((AUTO_EXIT_MS-AUTO_EXIT_NO_USB_MS)/5)>>8; } else { idlePolls.b[1]=0; } + command=cmd_local_nop; + do { // 15 clockcycles per loop. - // adjust fastctr for 1ms timeout + // adjust fastctr for 5ms timeout - uint16_t fastctr=(uint16_t)(F_CPU/(1000.0f*15.0f)); + uint16_t fastctr=(uint16_t)(F_CPU/(1000.0f*15.0f/5.0f)); uint8_t resetctr=20; do { @@ -270,8 +269,15 @@ int main(void) { } } while(--fastctr); - - PORTB|=_BV(PB1); + + // commands are only evaluated after next USB transmission or after 5ms passed + if (command==cmd_exit) break; + if (command==cmd_erase_application) + eraseApplication(); + // Attention: eraseApplication will set command=cmd_write_page! + if (command==cmd_write_page) + writeFlashPage(); + command=cmd_local_nop; { @@ -292,16 +298,15 @@ int main(void) { idlePolls.w++; // Try to execute program when bootloader times out - if (AUTO_EXIT_MS&&(idlePolls.w==AUTO_EXIT_MS)) { + if (AUTO_EXIT_MS&&(idlePolls.w==(AUTO_EXIT_MS/5))) { if (pgm_read_byte(BOOTLOADER_ADDRESS - TINYVECTOR_RESET_OFFSET + 1)!=0xff) break; } LED_MACRO( idlePolls.b[1] ); - // Test whether another interrupt occured during the processing of USBpoll and commands. + // Test whether another interrupt occurred during the processing of USBpoll and commands. // If yes, we missed a data packet on the bus. This is not a big issue, since - // USB seems to allow timeout of up the two packets. (On my machine an USB - // error is triggered after the third missed packet.) + // USB seems to allow time-out of up the two packets. // The most critical situation occurs when a PID IN packet is missed due to // it's short length. Each packet + timeout takes around 45µs, meaning that // usbpoll must take less than 90µs or resyncing is not possible. @@ -319,7 +324,6 @@ int main(void) { if (USB_INTR_PENDING & (1<