Add extra tests to the MassStorage device demo and class driver for validating comman...
[pub/USBasp.git] / Bootloaders / CDC / BootloaderCDC.c
index 708ba35..d875b84 100644 (file)
@@ -67,6 +67,26 @@ bool RunBootloader = true;
  */\r
 int main(void)\r
 {\r
  */\r
 int main(void)\r
 {\r
+       /* Setup hardware required for the bootloader */\r
+       SetupHardware();\r
+\r
+       while (RunBootloader)\r
+       {\r
+               CDC_Task();\r
+               USB_USBTask();\r
+       }\r
+       \r
+       /* Reset all configured hardware to their default states for the user app */\r
+       ResetHardware();\r
+\r
+       /* Start the user application */\r
+       AppPtr_t AppStartPtr = (AppPtr_t)0x0000;\r
+       AppStartPtr();  \r
+}\r
+\r
+/** Configures all hardware required for the bootloader. */\r
+void SetupHardware(void)\r
+{\r
        /* Disable watchdog if enabled by bootloader/fuses */\r
        MCUSR &= ~(1 << WDRF);\r
        wdt_disable();\r
        /* Disable watchdog if enabled by bootloader/fuses */\r
        MCUSR &= ~(1 << WDRF);\r
        wdt_disable();\r
@@ -80,18 +100,11 @@ int main(void)
        \r
        /* Initialize USB Subsystem */\r
        USB_Init();\r
        \r
        /* Initialize USB Subsystem */\r
        USB_Init();\r
+}\r
 \r
 \r
-       while (RunBootloader)\r
-       {\r
-               USB_USBTask();\r
-               CDC_Task();\r
-       }\r
-       \r
-       Endpoint_SelectEndpoint(CDC_TX_EPNUM);\r
-\r
-       /* Wait until any pending transmissions have completed before shutting down */\r
-       while (!(Endpoint_IsINReady()));\r
-       \r
+/** Resets all configured hardware required for the bootloader back to their original states. */\r
+void ResetHardware(void)\r
+{\r
        /* Shut down the USB subsystem */\r
        USB_ShutDown();\r
        \r
        /* Shut down the USB subsystem */\r
        USB_ShutDown();\r
        \r
@@ -99,27 +112,14 @@ int main(void)
        MCUCR = (1 << IVCE);\r
        MCUCR = 0;\r
 \r
        MCUCR = (1 << IVCE);\r
        MCUCR = 0;\r
 \r
-       /* Reset any used hardware ports back to their defaults */\r
-       PORTD = 0;\r
-       DDRD  = 0;\r
-       \r
-       #if defined(PORTE)\r
-       PORTE = 0;\r
-       DDRE  = 0;\r
-       #endif\r
-       \r
        /* Re-enable RWW section */\r
        boot_rww_enable();\r
        /* Re-enable RWW section */\r
        boot_rww_enable();\r
-\r
-       /* Start the user application */\r
-       AppPtr_t AppStartPtr = (AppPtr_t)0x0000;\r
-       AppStartPtr();  \r
 }\r
 \r
 /** Event handler for the USB_Disconnect event. This indicates that the bootloader should exit and the user\r
  *  application started.\r
  */\r
 }\r
 \r
 /** Event handler for the USB_Disconnect event. This indicates that the bootloader should exit and the user\r
  *  application started.\r
  */\r
-EVENT_HANDLER(USB_Disconnect)\r
+void EVENT_USB_Disconnect(void)\r
 {\r
        /* Upon disconnection, run user application */\r
        RunBootloader = false;\r
 {\r
        /* Upon disconnection, run user application */\r
        RunBootloader = false;\r
@@ -128,7 +128,7 @@ EVENT_HANDLER(USB_Disconnect)
 /** Event handler for the USB_ConfigurationChanged event. This configures the device's endpoints ready\r
  *  to relay data to and from the attached USB host.\r
  */\r
 /** Event handler for the USB_ConfigurationChanged event. This configures the device's endpoints ready\r
  *  to relay data to and from the attached USB host.\r
  */\r
-EVENT_HANDLER(USB_ConfigurationChanged)\r
+void EVENT_USB_ConfigurationChanged(void)\r
 {\r
        /* Setup CDC Notification, Rx and Tx Endpoints */\r
        Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT,\r
 {\r
        /* Setup CDC Notification, Rx and Tx Endpoints */\r
        Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT,\r
@@ -148,7 +148,7 @@ EVENT_HANDLER(USB_ConfigurationChanged)
  *  control requests that are not handled internally by the USB library, so that they can be handled appropriately\r
  *  for the application.\r
  */\r
  *  control requests that are not handled internally by the USB library, so that they can be handled appropriately\r
  *  for the application.\r
  */\r
-EVENT_HANDLER(USB_UnhandledControlPacket)\r
+void EVENT_USB_UnhandledControlPacket(void)\r
 {\r
        uint8_t* LineCodingData = (uint8_t*)&LineCoding;\r
 \r
 {\r
        uint8_t* LineCodingData = (uint8_t*)&LineCoding;\r
 \r
@@ -206,7 +206,7 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
 /** Reads or writes a block of EEPROM or FLASH memory to or from the appropriate CDC data endpoint, depending\r
  *  on the AVR910 protocol command issued.\r
  *\r
 /** Reads or writes a block of EEPROM or FLASH memory to or from the appropriate CDC data endpoint, depending\r
  *  on the AVR910 protocol command issued.\r
  *\r
- *  \param Command  Single character AVR910 protocol command indicating what memory operation to perform\r
+ *  \param[in] Command  Single character AVR910 protocol command indicating what memory operation to perform\r
  */\r
 static void ReadWriteMemoryBlock(const uint8_t Command)\r
 {\r
  */\r
 static void ReadWriteMemoryBlock(const uint8_t Command)\r
 {\r
@@ -248,7 +248,7 @@ static void ReadWriteMemoryBlock(const uint8_t Command)
                        else\r
                        {\r
                                /* Read the next FLASH byte from the current FLASH page */\r
                        else\r
                        {\r
                                /* Read the next FLASH byte from the current FLASH page */\r
-                               #if defined(RAMPZ)\r
+                               #if (FLASHEND > 0xFFFF)\r
                                WriteNextResponseByte(pgm_read_byte_far(CurrAddress | HighByte));\r
                                #else\r
                                WriteNextResponseByte(pgm_read_byte(CurrAddress | HighByte));                                   \r
                                WriteNextResponseByte(pgm_read_byte_far(CurrAddress | HighByte));\r
                                #else\r
                                WriteNextResponseByte(pgm_read_byte(CurrAddress | HighByte));                                   \r
@@ -343,14 +343,14 @@ static uint8_t FetchNextCommandByte(void)
 /** Writes the next response byte to the CDC data IN endpoint, and sends the endpoint back if needed to free up the\r
  *  bank when full ready for the next byte in the packet to the host.\r
  *\r
 /** Writes the next response byte to the CDC data IN endpoint, and sends the endpoint back if needed to free up the\r
  *  bank when full ready for the next byte in the packet to the host.\r
  *\r
- *  \param Response  Next response byte to send to the host\r
+ *  \param[in] Response  Next response byte to send to the host\r
  */\r
 static void WriteNextResponseByte(const uint8_t Response)\r
 {\r
        /* Select the IN endpoint so that the next data byte can be written */\r
        Endpoint_SelectEndpoint(CDC_TX_EPNUM);\r
        \r
  */\r
 static void WriteNextResponseByte(const uint8_t Response)\r
 {\r
        /* Select the IN endpoint so that the next data byte can be written */\r
        Endpoint_SelectEndpoint(CDC_TX_EPNUM);\r
        \r
-       /* If OUT endpoint empty, clear it and wait for the next packet from the host */\r
+       /* If IN endpoint full, clear it and wait util ready for the next packet to the host */\r
        if (!(Endpoint_IsReadWriteAllowed()))\r
        {\r
                Endpoint_ClearIN();\r
        if (!(Endpoint_IsReadWriteAllowed()))\r
        {\r
                Endpoint_ClearIN();\r
@@ -364,7 +364,7 @@ static void WriteNextResponseByte(const uint8_t Response)
 /** Task to read in AVR910 commands from the CDC data OUT endpoint, process them, perform the required actions\r
  *  and send the appropriate response back to the host.\r
  */\r
 /** Task to read in AVR910 commands from the CDC data OUT endpoint, process them, perform the required actions\r
  *  and send the appropriate response back to the host.\r
  */\r
-TASK(CDC_Task)\r
+void CDC_Task(void)\r
 {\r
        /* Select the OUT endpoint */\r
        Endpoint_SelectEndpoint(CDC_RX_EPNUM);\r
 {\r
        /* Select the OUT endpoint */\r
        Endpoint_SelectEndpoint(CDC_RX_EPNUM);\r
@@ -424,9 +424,9 @@ TASK(CDC_Task)
                }\r
                else if (Command == 's')\r
                {\r
                }\r
                else if (Command == 's')\r
                {\r
-                       WriteNextResponseByte(SIGNATURE_0);\r
-                       WriteNextResponseByte(SIGNATURE_1);\r
-                       WriteNextResponseByte(SIGNATURE_2);             \r
+                       WriteNextResponseByte(AVR_SIGNATURE_3);         \r
+                       WriteNextResponseByte(AVR_SIGNATURE_2);\r
+                       WriteNextResponseByte(AVR_SIGNATURE_1);\r
                }\r
                else if (Command == 'b')\r
                {\r
                }\r
                else if (Command == 'b')\r
                {\r
@@ -513,7 +513,7 @@ TASK(CDC_Task)
                }\r
                else if (Command == 'R')\r
                {\r
                }\r
                else if (Command == 'R')\r
                {\r
-                       #if defined(RAMPZ)\r
+                       #if (FLASHEND > 0xFFFF)\r
                        uint16_t ProgramWord = pgm_read_word_far(CurrAddress);\r
                        #else\r
                        uint16_t ProgramWord = pgm_read_word(CurrAddress);                      \r
                        uint16_t ProgramWord = pgm_read_word_far(CurrAddress);\r
                        #else\r
                        uint16_t ProgramWord = pgm_read_word(CurrAddress);                      \r
@@ -566,6 +566,9 @@ TASK(CDC_Task)
                        while (!(Endpoint_IsINReady()));\r
                        Endpoint_ClearIN();\r
                }\r
                        while (!(Endpoint_IsINReady()));\r
                        Endpoint_ClearIN();\r
                }\r
+\r
+               /* Wait until the data has been sent to the host */\r
+               while (!(Endpoint_IsINReady()));\r
                \r
                /* Select the OUT endpoint */\r
                Endpoint_SelectEndpoint(CDC_RX_EPNUM);\r
                \r
                /* Select the OUT endpoint */\r
                Endpoint_SelectEndpoint(CDC_RX_EPNUM);\r