Move AVRISP Programmer project to a new Unfinished subdirectory of Project while...
[pub/lufa.git] / Projects / Unfinished / AVRISP / Lib / V2Protocol.c
diff --git a/Projects/Unfinished/AVRISP/Lib/V2Protocol.c b/Projects/Unfinished/AVRISP/Lib/V2Protocol.c
new file mode 100644 (file)
index 0000000..304eef4
--- /dev/null
@@ -0,0 +1,161 @@
+/*\r
+             LUFA Library\r
+     Copyright (C) Dean Camera, 2009.\r
+              \r
+  dean [at] fourwalledcubicle [dot] com\r
+      www.fourwalledcubicle.com\r
+*/\r
+\r
+/*\r
+  Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
+\r
+  Permission to use, copy, modify, and distribute this software\r
+  and its documentation for any purpose and without fee is hereby\r
+  granted, provided that the above copyright notice appear in all\r
+  copies and that both that the copyright notice and this\r
+  permission notice and warranty disclaimer appear in supporting\r
+  documentation, and that the name of the author not be used in\r
+  advertising or publicity pertaining to distribution of the\r
+  software without specific, written prior permission.\r
+\r
+  The author disclaim all warranties with regard to this\r
+  software, including all implied warranties of merchantability\r
+  and fitness.  In no event shall the author be liable for any\r
+  special, indirect or consequential damages or any damages\r
+  whatsoever resulting from loss of use, data or profits, whether\r
+  in an action of contract, negligence or other tortious action,\r
+  arising out of or in connection with the use or performance of\r
+  this software.\r
+*/\r
+\r
+/** \file\r
+ *\r
+ *  V2Protocol handler, to process V2 Protocol commands used in Atmel programmer devices.\r
+ */\r
+\r
+#define  INCLUDE_FROM_V2PROTOCOL_C\r
+#include "V2Protocol.h"\r
+\r
+ParameterItem_t ParameterTable[] EEMEM = \r
+       {\r
+               { .ParameterID    = PARAM_BUILD_NUMBER_LOW,\r
+                 .ParameterValue = 0x00                    },\r
+               { .ParameterID    = PARAM_BUILD_NUMBER_HIGH,\r
+                 .ParameterValue = 0x00                    },\r
+               { .ParameterID    = PARAM_HW_VER,\r
+                 .ParameterValue = 0x01                    },\r
+               { .ParameterID    = PARAM_SW_MAJOR,\r
+                 .ParameterValue = 0x01                    },\r
+               { .ParameterID    = PARAM_SW_MINOR,\r
+                 .ParameterValue = 0x00                    },\r
+               { .ParameterID    = PARAM_VTARGET,\r
+                 .ParameterValue = 0x00                    },\r
+               { .ParameterID    = PARAM_SCK_DURATION,\r
+                 .ParameterValue = 0x00                    },\r
+               { .ParameterID    = PARAM_RESET_POLARITY,\r
+                 .ParameterValue = 0x00                    },\r
+               { .ParameterID    = PARAM_STATUS_TGT_CONN,\r
+                 .ParameterValue = 0x00                    },\r
+               { .ParameterID    = PARAM_DISCHARGEDELAY,\r
+                 .ParameterValue = 0x00                    },\r
+       };\r
+
+void V2Protocol_ProcessCommand(void)
+{
+       uint8_t V2Command = Endpoint_Read_Byte();\r
+                 \r
+       printf("COMMAND %d\r\n", V2Command);\r
+\r
+       switch (V2Command)\r
+       {\r
+               case CMD_SIGN_ON:\r
+                       V2Protocol_ProcessCmdSignOn();\r
+                       break;\r
+               case CMD_SET_PARAMETER:\r
+                       V2Protocol_ProcessCmdSetParam();\r
+                       break;\r
+               case CMD_GET_PARAMETER:\r
+                       V2Protocol_ProcessCmdGetParam();\r
+                       break;\r
+               default:\r
+                       Endpoint_ClearOUT();\r
+                       Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
+                       Endpoint_Write_Byte(STATUS_CMD_UNKNOWN);\r
+                       Endpoint_ClearIN();\r
+                       break;\r
+       }\r
+       \r
+       /* Reset Endpoint direction to OUT ready for next command */\r
+       Endpoint_SetEndpointDirection(ENDPOINT_DIR_OUT);        
+}\r
+\r
+static ParameterItem_t* V2Protocol_GetParameterItem(uint8_t ParamID)\r
+{\r
+       for (uint8_t TableIndex = 0; TableIndex < (sizeof(ParameterTable) / sizeof(ParameterTable[0])); TableIndex++)\r
+       {\r
+               if (ParamID == eeprom_read_byte(&ParameterTable[TableIndex].ParameterID))\r
+                 return &ParameterTable[TableIndex];\r
+       }\r
+       \r
+       return NULL;\r
+}
+
+static void V2Protocol_ProcessCmdSignOn(void)\r
+{\r
+       Endpoint_ClearOUT();\r
+       Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\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_Stream_LE(PROGRAMMER_ID, PROGRAMMER_ID_LEN);\r
+       Endpoint_ClearIN();\r
+}\r
+\r
+static void V2Protocol_ProcessCmdSetParam(void)\r
+{\r
+       uint8_t ParamID    = Endpoint_Read_Byte();\r
+       uint8_t ParamValue = Endpoint_Read_Byte();\r
+\r
+       ParameterItem_t* ParameterItem = V2Protocol_GetParameterItem(ParamID);\r
+       \r
+       Endpoint_ClearOUT();\r
+       Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
+\r
+       if (ParameterItem != NULL)\r
+       {\r
+               eeprom_write_byte(&ParameterItem->ParameterValue, ParamValue);\r
+\r
+               Endpoint_Write_Byte(CMD_SET_PARAMETER);\r
+               Endpoint_Write_Byte(STATUS_CMD_OK);     \r
+       }\r
+       else\r
+       {\r
+               Endpoint_Write_Byte(STATUS_CMD_FAILED);\r
+       }\r
+\r
+       Endpoint_ClearIN();\r
+}\r
+\r
+static void V2Protocol_ProcessCmdGetParam(void)\r
+{\r
+       uint8_t ParamID    = Endpoint_Read_Byte();\r
+\r
+       ParameterItem_t* ParameterItem = V2Protocol_GetParameterItem(ParamID);\r
+       \r
+       Endpoint_ClearOUT();\r
+       Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
+\r
+       if (ParameterItem != NULL)\r
+       {\r
+               Endpoint_Write_Byte(CMD_GET_PARAMETER);\r
+               Endpoint_Write_Byte(STATUS_CMD_OK);\r
+               Endpoint_Write_Byte(eeprom_read_byte(&ParameterItem->ParameterValue));  \r
+       }\r
+       else\r
+       {\r
+               Endpoint_Write_Byte(STATUS_CMD_FAILED);\r
+       }\r
+\r
+       Endpoint_ClearIN();     \r
+}\r