Add LUFA-side channel open/close routines, add signalling handlers for the creation...
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / ISP / ISPProtocol.c
index 33ccbf9..9d0a26c 100644 (file)
@@ -57,18 +57,26 @@ void ISPProtocol_EnterISPMode(void)
        Endpoint_Read_Stream_LE(&Enter_ISP_Params, sizeof(Enter_ISP_Params), NO_STREAM_CALLBACK);\r
 \r
        Endpoint_ClearOUT();\r
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);\r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
 \r
        uint8_t ResponseStatus = STATUS_CMD_FAILED;\r
        \r
        CurrentAddress = 0;\r
+       \r
+       /* Set up the synchronous USART to generate the recovery clock on XCK pin */\r
+       UBRR1  = (F_CPU / 500000UL);\r
+       UCSR1B = (1 << TXEN1);\r
+       UCSR1C = (1 << UMSEL10) | (1 << UPM11) | (1 << USBS1) | (1 << UCSZ11) | (1 << UCSZ10) | (1 << UCPOL1);\r
+       DDRD  |= (1 << 5);\r
 \r
+       /* Perform execution delay, initialize SPI bus */\r
        ISPProtocol_DelayMS(Enter_ISP_Params.ExecutionDelayMS); \r
        SPI_Init(ISPTarget_GetSPIPrescalerMask() | SPI_SCK_LEAD_RISING | SPI_SAMPLE_LEADING | SPI_MODE_MASTER);\r
 \r
        /* Continuously attempt to synchronize with the target until either the number of attempts specified\r
         * by the host has exceeded, or the the device sends back the expected response values */\r
-       while (Enter_ISP_Params.SynchLoops-- && (ResponseStatus == STATUS_CMD_FAILED))\r
+       while (Enter_ISP_Params.SynchLoops-- && (ResponseStatus == STATUS_CMD_FAILED) && TimeoutMSRemaining)\r
        {\r
                uint8_t ResponseBytes[4];\r
 \r
@@ -110,6 +118,7 @@ void ISPProtocol_LeaveISPMode(void)
        Endpoint_Read_Stream_LE(&Leave_ISP_Params, sizeof(Leave_ISP_Params), NO_STREAM_CALLBACK);\r
        \r
        Endpoint_ClearOUT();\r
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);\r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
 \r
        /* Perform pre-exit delay, release the target /RESET, disable the SPI bus and perform the post-exit delay */\r
@@ -118,6 +127,12 @@ void ISPProtocol_LeaveISPMode(void)
        SPI_ShutDown();\r
        ISPProtocol_DelayMS(Leave_ISP_Params.PostDelayMS);\r
 \r
+       /* Turn off the synchronous USART to terminate the recovery clock on XCK pin */\r
+       UBRR1  = (F_CPU / 500000UL);\r
+       UCSR1B = (1 << TXEN1);\r
+       UCSR1C = (1 << UMSEL10) | (1 << UPM11) | (1 << USBS1) | (1 << UCSZ11) | (1 << UCSZ10) | (1 << UCPOL1);\r
+       DDRD  &= ~(1 << 5);\r
+\r
        Endpoint_Write_Byte(CMD_LEAVE_PROGMODE_ISP);\r
        Endpoint_Write_Byte(STATUS_CMD_OK);\r
        Endpoint_ClearIN();\r
@@ -150,6 +165,7 @@ void ISPProtocol_ProgramMemory(uint8_t V2Command)
        if (Write_Memory_Params.BytesToWrite > sizeof(Write_Memory_Params.ProgData))\r
        {\r
                Endpoint_ClearOUT();\r
+               Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);\r
                Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
 \r
                Endpoint_Write_Byte(V2Command);\r
@@ -161,6 +177,7 @@ void ISPProtocol_ProgramMemory(uint8_t V2Command)
        Endpoint_Read_Stream_LE(&Write_Memory_Params.ProgData, Write_Memory_Params.BytesToWrite, NO_STREAM_CALLBACK);\r
 \r
        Endpoint_ClearOUT();\r
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);\r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
 \r
        uint8_t  ProgrammingStatus = STATUS_CMD_OK;     \r
@@ -292,6 +309,7 @@ void ISPProtocol_ReadMemory(uint8_t V2Command)
        Read_Memory_Params.BytesToRead = SwapEndian_16(Read_Memory_Params.BytesToRead);\r
        \r
        Endpoint_ClearOUT();\r
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);\r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
        \r
        Endpoint_Write_Byte(V2Command);\r
@@ -362,6 +380,7 @@ void ISPProtocol_ChipErase(void)
        Endpoint_Read_Stream_LE(&Erase_Chip_Params, sizeof(Erase_Chip_Params), NO_STREAM_CALLBACK);\r
        \r
        Endpoint_ClearOUT();\r
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);\r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
        \r
        uint8_t ResponseStatus = STATUS_CMD_OK;\r
@@ -397,6 +416,7 @@ void ISPProtocol_ReadFuseLockSigOSCCAL(uint8_t V2Command)
        Endpoint_Read_Stream_LE(&Read_FuseLockSigOSCCAL_Params, sizeof(Read_FuseLockSigOSCCAL_Params), NO_STREAM_CALLBACK);\r
 \r
        Endpoint_ClearOUT();\r
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);\r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
 \r
        uint8_t ResponseBytes[4];\r
@@ -427,6 +447,7 @@ void ISPProtocol_WriteFuseLock(uint8_t V2Command)
        Endpoint_Read_Stream_LE(&Write_FuseLockSig_Params, sizeof(Write_FuseLockSig_Params), NO_STREAM_CALLBACK);\r
 \r
        Endpoint_ClearOUT();\r
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);\r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
 \r
        /* Send the Fuse or Lock byte program commands as given by the host to the device */\r
@@ -454,6 +475,7 @@ void ISPProtocol_SPIMulti(void)
        Endpoint_Read_Stream_LE(&SPI_Multi_Params.TxData, SPI_Multi_Params.TxBytes, NO_STREAM_CALLBACK);\r
        \r
        Endpoint_ClearOUT();\r
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);\r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
        \r
        Endpoint_Write_Byte(CMD_SPI_MULTI);\r
@@ -505,4 +527,19 @@ void ISPProtocol_SPIMulti(void)
        }\r
 }\r
 \r
+/** Blocking delay for a given number of milliseconds.\r
+ *\r
+ *  \param[in] DelayMS  Number of milliseconds to delay for\r
+ */\r
+void ISPProtocol_DelayMS(uint8_t DelayMS)\r
+{\r
+       while (DelayMS-- && TimeoutMSRemaining)\r
+       {\r
+               if (TimeoutMSRemaining)\r
+                 TimeoutMSRemaining--;\r
+                 \r
+               _delay_ms(1);\r
+       }\r
+}\r
+\r
 #endif
\ No newline at end of file