Moved out target-related V2 protocol commands into a seperate file for the AVRISP...
[pub/lufa.git] / Projects / Incomplete / AVRISP / Lib / V2ProtocolTarget.c
diff --git a/Projects/Incomplete/AVRISP/Lib/V2ProtocolTarget.c b/Projects/Incomplete/AVRISP/Lib/V2ProtocolTarget.c
new file mode 100644 (file)
index 0000000..7bcf0be
--- /dev/null
@@ -0,0 +1,116 @@
+/*\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
+ *  Target-related functions for the V2 Protocol decoder.\r
+ */\r
+\r
+#include "V2ProtocolTarget.h"\r
+\r
+/** Current memory address for FLASH/EEPROM memory read/write commands */\r
+uint32_t CurrentAddress;\r
+\r
+/** Table of masks for SPI_Init() from a given PARAM_SCK_DURATION value */\r
+static const uint8_t SPIMaskFromSCKDuration[] =\r
+       {\r
+               #if (F_CPU == 8000000)\r
+               SPI_SPEED_FCPU_DIV_2,\r
+               #endif\r
+               SPI_SPEED_FCPU_DIV_2, SPI_SPEED_FCPU_DIV_4, SPI_SPEED_FCPU_DIV_8,\r
+               SPI_SPEED_FCPU_DIV_16, SPI_SPEED_FCPU_DIV_32, SPI_SPEED_FCPU_DIV_64\r
+               #if (F_CPU == 16000000)                                                                         \r
+               , SPI_SPEED_FCPU_DIV_128\r
+               #endif\r
+       };\r
+       \r
+\r
+uint8_t V2Protocol_GetSPIPrescalerMask(void)\r
+{\r
+       uint8_t SCKDuration = V2Params_GetParameterValue(PARAM_SCK_DURATION);\r
+\r
+       if (SCKDuration >= sizeof(SPIMaskFromSCKDuration))\r
+         SCKDuration = (sizeof(SPIMaskFromSCKDuration) - 1);\r
+         \r
+       return SPIMaskFromSCKDuration[SCKDuration];\r
+}\r
+\r
+void V2Protocol_ChangeTargetResetLine(bool ResetTarget)\r
+{\r
+       if (ResetTarget)\r
+       {\r
+               RESET_LINE_DDR  |= RESET_LINE_MASK;\r
+               \r
+               if (!(V2Params_GetParameterValue(PARAM_RESET_POLARITY)))\r
+                 RESET_LINE_PORT |= RESET_LINE_MASK;\r
+       }\r
+       else\r
+       {\r
+               RESET_LINE_PORT &= ~RESET_LINE_MASK;    \r
+               RESET_LINE_DDR  &= ~RESET_LINE_MASK;\r
+       }\r
+}\r
+\r
+void V2Protocol_DelayMS(uint8_t MS)\r
+{\r
+       while (MS--)\r
+         _delay_ms(1);\r
+}\r
+\r
+uint8_t V2Protocol_WaitWhileTargetBusy(void)\r
+{\r
+       uint8_t TimeoutMS = TARGET_BUSY_TIMEOUT_MS;\r
+       uint8_t ResponseByte;\r
+       \r
+       do\r
+       {\r
+               V2Protocol_DelayMS(1);\r
+       \r
+               SPI_SendByte(0xF0);\r
+               SPI_SendByte(0x00);\r
+\r
+               SPI_SendByte(0x00);\r
+               ResponseByte = SPI_ReceiveByte();\r
+       }\r
+       while ((ResponseByte & 0x01) && (TimeoutMS--));\r
+\r
+       if (!(TimeoutMS))\r
+         return STATUS_CMD_TOUT;\r
+       else\r
+         return STATUS_CMD_OK;\r
+}\r
+\r
+void V2Protocol_LoadExtendedAddress(void)\r
+{\r
+       SPI_SendByte(0x4D);\r
+       SPI_SendByte(0x00);\r
+       SPI_SendByte((CurrentAddress & 0x00FF0000) >> 16);\r
+       SPI_SendByte(0x00);     \r
+}\r