Added handler for the V2 Protocol SPI_MULTI command to the AVRISP Programmer project.
[pub/USBasp.git] / Projects / Unfinished / AVRISP / Lib / V2Protocol.c
index 2679ce7..27f6fa7 100644 (file)
@@ -59,6 +59,24 @@ ParameterItem_t ParameterTable[] EEMEM =
                { .ParameterID    = PARAM_DISCHARGEDELAY,\r
                  .ParameterValue = 0x00                    },\r
        };\r
                { .ParameterID    = PARAM_DISCHARGEDELAY,\r
                  .ParameterValue = 0x00                    },\r
        };\r
+       \r
+void V2Protocol_ConfigureHardware(void)\r
+{\r
+#if F_CPU == 8000000\r
+       uint8_t SPIMaskFromSCKDuration[] = {SPI_SPEED_FCPU_DIV_128, SPI_SPEED_FCPU_DIV_128, SPI_SPEED_FCPU_DIV_128,\r
+                                           SPI_SPEED_FCPU_DIV_128, SPI_SPEED_FCPU_DIV_128, SPI_SPEED_FCPU_DIV_128};\r
+#else\r
+       uint8_t SPIMaskFromSCKDuration[] = {SPI_SPEED_FCPU_DIV_128, SPI_SPEED_FCPU_DIV_128, SPI_SPEED_FCPU_DIV_128,\r
+                                           SPI_SPEED_FCPU_DIV_128, SPI_SPEED_FCPU_DIV_128, SPI_SPEED_FCPU_DIV_128};\r
+#endif\r
+\r
+       uint8_t SCKDuration = eeprom_read_byte(&V2Protocol_GetParameterItem(PARAM_SCK_DURATION)->ParameterValue);\r
+\r
+       if (SCKDuration > sizeof(SPIMaskFromSCKDuration))\r
+         SCKDuration = SPIMaskFromSCKDuration;\r
+         \r
+       SPI_Init(SPIMaskFromSCKDuration[SCKDuration], true);    \r
+}\r
 
 void V2Protocol_ProcessCommand(void)
 {
 
 void V2Protocol_ProcessCommand(void)
 {
@@ -73,6 +91,9 @@ void V2Protocol_ProcessCommand(void)
                case CMD_GET_PARAMETER:\r
                        V2Protocol_ProcessCmdGetSetParam(V2Command);\r
                        break;\r
                case CMD_GET_PARAMETER:\r
                        V2Protocol_ProcessCmdGetSetParam(V2Command);\r
                        break;\r
+               case CMD_SPI_MULTI:\r
+                       V2Protocol_ProcessCmdSPIMulti();\r
+                       break;          \r
                default:\r
                        while (Endpoint_BytesInEndpoint() == AVRISP_DATA_EPSIZE)\r
                        {\r
                default:\r
                        while (Endpoint_BytesInEndpoint() == AVRISP_DATA_EPSIZE)\r
                        {\r
@@ -83,6 +104,7 @@ void V2Protocol_ProcessCommand(void)
                        Endpoint_ClearOUT();\r
                        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
 \r
                        Endpoint_ClearOUT();\r
                        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
 \r
+                       Endpoint_Write_Byte(V2Command);\r
                        Endpoint_Write_Byte(STATUS_CMD_UNKNOWN);\r
                        Endpoint_ClearIN();\r
                        break;\r
                        Endpoint_Write_Byte(STATUS_CMD_UNKNOWN);\r
                        Endpoint_ClearIN();\r
                        break;\r
@@ -90,9 +112,7 @@ void V2Protocol_ProcessCommand(void)
        \r
        printf("COMMAND 0x%02x\r\n", V2Command);\r
 \r
        \r
        printf("COMMAND 0x%02x\r\n", V2Command);\r
 \r
-       Endpoint_WaitUntilReady();\r
-       \r
-       /* Reset Endpoint direction to OUT ready for next command */\r
+       Endpoint_WaitUntilReady();      \r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_OUT);        
 }\r
 \r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_OUT);        
 }\r
 \r
@@ -113,6 +133,8 @@ static void V2Protocol_ProcessCmdSignOn(void)
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
        Endpoint_WaitUntilReady();\r
 \r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
        Endpoint_WaitUntilReady();\r
 \r
+       V2Protocol_ConfigureHardware();\r
+\r
        Endpoint_Write_Byte(CMD_SIGN_ON);\r
        Endpoint_Write_Byte(STATUS_CMD_OK);\r
        Endpoint_Write_Byte(PROGRAMMER_ID_LEN);\r
        Endpoint_Write_Byte(CMD_SIGN_ON);\r
        Endpoint_Write_Byte(STATUS_CMD_OK);\r
        Endpoint_Write_Byte(PROGRAMMER_ID_LEN);\r
@@ -131,9 +153,10 @@ static void V2Protocol_ProcessCmdGetSetParam(uint8_t V2Command)
        \r
        ParameterItem_t* ParameterItem = V2Protocol_GetParameterItem(ParamID);\r
        \r
        \r
        ParameterItem_t* ParameterItem = V2Protocol_GetParameterItem(ParamID);\r
        \r
+       Endpoint_Write_Byte(V2Command);\r
+\r
        if (ParameterItem != NULL)\r
        {\r
        if (ParameterItem != NULL)\r
        {\r
-               Endpoint_Write_Byte(V2Command);\r
                Endpoint_Write_Byte(STATUS_CMD_OK);\r
 \r
                if (V2Command == CMD_SET_PARAMETER)\r
                Endpoint_Write_Byte(STATUS_CMD_OK);\r
 \r
                if (V2Command == CMD_SET_PARAMETER)\r
@@ -148,3 +171,51 @@ static void V2Protocol_ProcessCmdGetSetParam(uint8_t V2Command)
 \r
        Endpoint_ClearIN();\r
 }\r
 \r
        Endpoint_ClearIN();\r
 }\r
+\r
+static void V2Protocol_ProcessCmdSPIMulti(void)\r
+{\r
+       uint8_t TxBytes = Endpoint_Read_Byte();\r
+       uint8_t RxBytes = Endpoint_Read_Byte();\r
+       uint8_t RxStartAddr = Endpoint_Read_Byte();\r
+       uint8_t TxData[255];\r
+       \r
+       Endpoint_Read_Stream_LE(TxData, TxBytes);\r
+       \r
+       Endpoint_ClearOUT();\r
+       Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
+       Endpoint_WaitUntilReady();\r
+       \r
+       Endpoint_Write_Byte(CMD_SPI_MULTI);\r
+       Endpoint_Write_Byte(STATUS_CMD_OK);\r
+\r
+       uint8_t CurrTxPos = 0;\r
+       uint8_t CurrRxPos = 0;\r
+\r
+       while (CurrTxPos < RxStartAddr)\r
+       {\r
+               if (CurrTxPos < TxBytes)\r
+                 SPI_SendByte(TxData[CurrTxPos]);\r
+               else\r
+                 SPI_SendByte(0);\r
+               \r
+               CurrTxPos++;\r
+       }\r
+\r
+       while (CurrRxPos < RxBytes)\r
+       {\r
+               if (CurrTxPos < TxBytes)\r
+               {\r
+                       Endpoint_Write_Byte(SPI_TransferByte(TxData[CurrTxPos]));\r
+                       CurrTxPos++;\r
+               }\r
+               else\r
+               {\r
+                       Endpoint_Write_Byte(SPI_ReceiveByte());\r
+               }\r
+               \r
+               CurrRxPos++;\r
+       }       \r
+       \r
+       Endpoint_Write_Byte(STATUS_CMD_OK);\r
+       Endpoint_ClearIN();     \r
+}\r