-/*\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
-uint32_t CurrentAddress;\r
-\r
-\r
-/* Table of masks for SPI_Init() from a given PARAM_SCK_DURATION value */\r
-static const uint8_t SPIMaskFromSCKDuration[MAX_SPI_SETTINGS] =\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
-static void V2Protocol_ReconfigureSPI(void)\r
-{\r
- uint8_t SCKDuration = V2Params_GetParameterValue(PARAM_SCK_DURATION);\r
-\r
- if (SCKDuration >= MAX_SPI_SETTINGS)\r
- SCKDuration = (MAX_SPI_SETTINGS - 1);\r
- \r
- SPI_Init(SPIMaskFromSCKDuration[SCKDuration], true); \r
-}\r
-\r
-static 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
-
-void V2Protocol_ProcessCommand(void)
-{
- uint8_t V2Command = Endpoint_Read_Byte();\r
- \r
- switch (V2Command)\r
- {\r
- case CMD_SIGN_ON:\r
- V2Protocol_Command_SignOn();\r
- break;\r
- case CMD_SET_PARAMETER:\r
- case CMD_GET_PARAMETER:\r
- V2Protocol_Command_GetSetParam(V2Command);\r
- break;\r
- case CMD_LOAD_ADDRESS:\r
- V2Protocol_Command_LoadAddress();\r
- break;\r
- case CMD_SPI_MULTI:\r
- V2Protocol_Command_SPIMulti();\r
- break; \r
- default:\r
- V2Protocol_Command_Unknown(V2Command);\r
- break;\r
- }\r
- \r
- printf("COMMAND 0x%02x\r\n", V2Command);\r
-\r
- Endpoint_WaitUntilReady(); \r
- Endpoint_SetEndpointDirection(ENDPOINT_DIR_OUT);
-}
-\r
-static void V2Protocol_Command_Unknown(uint8_t V2Command)\r
-{\r
- /* Discard all incomming data */\r
- while (Endpoint_BytesInEndpoint() == AVRISP_DATA_EPSIZE)\r
- {\r
- Endpoint_ClearOUT();\r
- Endpoint_WaitUntilReady();\r
- }\r
-\r
- Endpoint_ClearOUT();\r
- Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
- Endpoint_WaitUntilReady();\r
-\r
- Endpoint_Write_Byte(V2Command);\r
- Endpoint_Write_Byte(STATUS_CMD_UNKNOWN);\r
- Endpoint_ClearIN();\r
-}\r
-
-static void V2Protocol_Command_SignOn(void)\r
-{\r
- Endpoint_ClearOUT();\r
- Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
- Endpoint_WaitUntilReady();\r
-\r
- V2Protocol_ReconfigureSPI();\r
- CurrentAddress = 0;\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_Command_GetSetParam(uint8_t V2Command)\r
-{\r
- uint8_t ParamID = Endpoint_Read_Byte();\r
- uint8_t ParamValue = Endpoint_Read_Byte();\r
-\r
- Endpoint_ClearOUT();\r
- Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
- Endpoint_WaitUntilReady();\r
- \r
- uint8_t ParamPrivs = V2Params_GetParameterPrivellages(ParamID);\r
- \r
- Endpoint_Write_Byte(V2Command);\r
- \r
- if ((V2Command == CMD_SET_PARAMETER) && (ParamPrivs & PARAM_PRIV_WRITE))\r
- {\r
- Endpoint_Write_Byte(STATUS_CMD_OK);\r
- V2Params_SetParameterValue(ParamID, ParamValue);\r
- }\r
- else if ((V2Command == CMD_GET_PARAMETER) && (ParamPrivs & PARAM_PRIV_READ))\r
- {\r
- Endpoint_Write_Byte(STATUS_CMD_OK);\r
- Endpoint_Write_Byte(V2Params_GetParameterValue(ParamID));\r
- }\r
- else\r
- { \r
- Endpoint_Write_Byte(STATUS_CMD_FAILED);\r
- }\r
-\r
- Endpoint_ClearIN();\r
-}\r
-\r
-static void V2Protocol_Command_LoadAddress(void)\r
-{\r
- CurrentAddress = Endpoint_Read_DWord_LE();\r
-\r
- Endpoint_ClearOUT();\r
- Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
- Endpoint_WaitUntilReady();\r
-\r
- Endpoint_Write_Byte(CMD_LOAD_ADDRESS);\r
- Endpoint_Write_Byte(STATUS_CMD_OK);\r
- Endpoint_ClearIN();\r
-}\r
-\r
-static void V2Protocol_Command_SPIMulti(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
- /* Write out bytes to transmit until the start of the bytes to receive is met */\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
- /* Transmit remaining bytes with padding as needed, read in response bytes */\r
- while (CurrRxPos < RxBytes)\r
- {\r
- if (CurrTxPos < TxBytes)\r
- Endpoint_Write_Byte(SPI_TransferByte(TxData[CurrTxPos++]));\r
- else\r
- Endpoint_Write_Byte(SPI_ReceiveByte());\r
- \r
- CurrRxPos++;\r
- } \r
- \r
- Endpoint_Write_Byte(STATUS_CMD_OK);\r
- Endpoint_ClearIN(); \r
-}\r