Fixed broken Magstripe project and TeensyHID bootloader.
[pub/USBasp.git] / Demos / USBtoSerial / USBtoSerial.c
index 7c06007..c7c9e4b 100644 (file)
@@ -72,8 +72,8 @@ int main(void)
        MCUSR &= ~(1 << WDRF);\r
        wdt_disable();\r
 \r
-       /* Disable Clock Division */\r
-       SetSystemClockPrescaler(0);\r
+       /* Disable clock division */\r
+       clock_prescale_set(clock_div_1);\r
 \r
        /* Hardware Initialization */\r
        LEDs_Init();\r
@@ -116,6 +116,10 @@ EVENT_HANDLER(USB_Disconnect)
        /* Stop running CDC and USB management tasks */\r
        Scheduler_SetTaskMode(CDC_Task, TASK_STOP);\r
        Scheduler_SetTaskMode(USB_USBTask, TASK_STOP);\r
+       \r
+       /* Reset Tx and Rx buffers, device disconnected */\r
+       Buffer_Initialize(&Rx_Buffer);\r
+       Buffer_Initialize(&Tx_Buffer);\r
 \r
        /* Indicate USB not ready */\r
        UpdateStatus(Status_USBNotReady);\r
@@ -160,7 +164,7 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
                case REQ_GetLineEncoding:\r
                        if (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))\r
                        {       \r
-                               /* Acknowedge the SETUP packet, ready for data transfer */\r
+                               /* Acknowledge the SETUP packet, ready for data transfer */\r
                                Endpoint_ClearSetupReceived();\r
 \r
                                /* Write the line coding data to the control endpoint */\r
@@ -174,7 +178,7 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
                case REQ_SetLineEncoding:\r
                        if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))\r
                        {\r
-                               /* Acknowedge the SETUP packet, ready for data transfer */\r
+                               /* Acknowledge the SETUP packet, ready for data transfer */\r
                                Endpoint_ClearSetupReceived();\r
 \r
                                /* Read the line coding data in from the host into the global struct */\r
@@ -202,10 +206,11 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
                                // Do something with the given line states in wIndex\r
 #endif\r
                                \r
-                               /* Acknowedge the SETUP packet, ready for data transfer */\r
+                               /* Acknowledge the SETUP packet, ready for data transfer */\r
                                Endpoint_ClearSetupReceived();\r
                                \r
-                               /* Send an empty packet to acknowedge the command */\r
+                               /* Acknowledge status stage */\r
+                               while (!(Endpoint_IsSetupINReady()));\r
                                Endpoint_ClearSetupIN();\r
                        }\r
        \r
@@ -322,8 +327,12 @@ ISR(USART1_TX_vect, ISR_BLOCK)
  */\r
 ISR(USART1_RX_vect, ISR_BLOCK)\r
 {\r
-       /* Character received, store it into the buffer */\r
-       Buffer_StoreElement(&Tx_Buffer, UDR1);\r
+       /* Only store received characters if the USB interface is connected */\r
+       if (USB_IsConnected)\r
+       {\r
+               /* Character received, store it into the buffer */\r
+               Buffer_StoreElement(&Tx_Buffer, UDR1);\r
+       }\r
 }\r
 \r
 /** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to\r