* the project and is responsible for the initial application hardware configuration.\r
*/\r
\r
-// TODO: Fix PROGRAM FLASH and PROGRAM EEPROM command processing\r
// TODO: Add in software SPI for lower programming speeds below 125KHz\r
// TODO: Add in VTARGET detection\r
// TODO: Add reversed target connector checks\r
-// TODO: Add Doxygen comments to functions\r
\r
#include "AVRISP.h"\r
\r
#define INCLUDE_FROM_V2PROTOCOL_C\r
#include "V2Protocol.h"\r
\r
+/** Master V2 Protocol packet handler, for receieved V2 Protocol packets from a connected host.\r
+ * This routine decodes the issued command and passes off the handling of the command to the\r
+ * appropriate function.\r
+ */\r
void V2Protocol_ProcessCommand(void)
{
uint8_t V2Command = Endpoint_Read_Byte();\r
Endpoint_SetEndpointDirection(ENDPOINT_DIR_OUT);
}
\r
+/** Handler for unknown V2 protocol commands. This discards all sent data and returns a\r
+ * STATUS_CMD_UNKNOWN status back to the host.\r
+ *\r
+ * \param V2Command Issued V2 Protocol command byte from the host\r
+ */\r
static void V2Protocol_Command_Unknown(uint8_t V2Command)\r
{\r
/* Discard all incomming data */\r
Endpoint_Write_Byte(STATUS_CMD_UNKNOWN);\r
Endpoint_ClearIN();\r
}\r
-
+\r
+/** Handler for the CMD_SIGN_ON command, returning the programmer ID string to the host. */
static void V2Protocol_Command_SignOn(void)\r
{\r
Endpoint_ClearOUT();\r
Endpoint_ClearIN();\r
}\r
\r
+/** Handler for the CMD_SET_PARAMETER and CMD_GET_PARAMETER commands from the host, setting or\r
+ * getting a device parameter's value from the parameter table.\r
+ *\r
+ * \param V2Command Issued V2 Protocol command byte from the host\r
+ */\r
static void V2Protocol_Command_GetSetParam(uint8_t V2Command)\r
{\r
uint8_t ParamID = Endpoint_Read_Byte();\r
Endpoint_ClearIN();\r
}\r
\r
+/** Handler for the CMD_LOAD_ADDRESS command, loading the given device address into a\r
+ * global storage variable for later use, and issuing LOAD EXTENDED ADDRESS commands\r
+ * to the attached device as required.\r
+ */\r
static void V2Protocol_Command_LoadAddress(void)\r
{\r
Endpoint_Read_Stream_BE(&CurrentAddress, sizeof(CurrentAddress));\r
Endpoint_ClearIN();\r
}\r
\r
+/** Handler for the CMD_RESET_PROTECTION command, currently implemented as a dummy ACK function\r
+ * as no ISP short-circuit protection is currently implemented.\r
+ */\r
static void V2Protocol_Command_ResetProtection(void)\r
{\r
Endpoint_ClearOUT();\r
Endpoint_ClearIN(); \r
}\r
\r
+/** Handler for the CMD_ENTER_PROGMODE_ISP command, which attempts to enter programming mode on\r
+ * the attached device, returning success or failure back to the host.\r
+ */\r
static void V2Protocol_Command_EnterISPMode(void)\r
{\r
struct\r
Endpoint_ClearIN();\r
}\r
\r
+/** Handler for the CMD_LEAVE_ISP command, which releases the target from programming mode. */\r
static void V2Protocol_Command_LeaveISPMode(void)\r
{\r
struct\r
Endpoint_ClearIN();\r
}\r
\r
+/** Handler for the CMD_PROGRAM_FLASH_ISP and CMD_PROGRAM_EEPROM_ISP commands, writing out bytes,\r
+ * words or pages of data to the attached device.\r
+ *\r
+ * \param V2Command Issued V2 Protocol command byte from the host\r
+ */\r
static void V2Protocol_Command_ProgramMemory(uint8_t V2Command)\r
{\r
struct\r
uint8_t ProgrammingCommands[3];\r
uint8_t PollValue1;\r
uint8_t PollValue2;\r
- uint8_t ProgData[256];\r
+ uint8_t ProgData[512];\r
} Write_Memory_Params;\r
\r
uint8_t* NextWriteByte = Write_Memory_Params.ProgData;\r
Endpoint_ClearIN();\r
}\r
\r
+/** Handler for the CMD_READ_FLASH_ISP and CMD_READ_EEPROM_ISP commands, reading in bytes,\r
+ * words or pages of data from the attached device.\r
+ *\r
+ * \param V2Command Issued V2 Protocol command byte from the host\r
+ */\r
static void V2Protocol_Command_ReadMemory(uint8_t V2Command)\r
{\r
struct\r
}\r
}\r
\r
+/** Handler for the CMD_CHI_ERASE_ISP command, clearing the target's FLASH memory. */\r
static void V2Protocol_Command_ChipErase(void)\r
{\r
struct\r
Endpoint_ClearIN();\r
}\r
\r
+/** Handler for the CMD_READ_FUSE_ISP, CMD_READ_LOCK_ISP, CMD_READ_SIGNATURE_ISP and CMD_READ_OSCCAL commands,\r
+ * reading the requested configuration byte from the device.\r
+ *\r
+ * \param V2Command Issued V2 Protocol command byte from the host\r
+ */\r
static void V2Protocol_Command_ReadFuseLockSigOSCCAL(uint8_t V2Command)\r
{\r
struct\r
Endpoint_ClearIN();\r
}\r
\r
+/** Handler for the CMD_WRITE_FUSE_ISP and CMD_WRITE_LOCK_ISP commands, writing the requested configuration\r
+ * byte to the device.\r
+ *\r
+ * \param V2Command Issued V2 Protocol command byte from the host\r
+ */\r
static void V2Protocol_Command_WriteFuseLock(uint8_t V2Command)\r
{\r
struct\r
Endpoint_ClearIN();\r
}\r
\r
+/** Handler for the CMD_SPI_MULTI command, writing and reading arbitrary SPI data to and from the attached device. */\r
static void V2Protocol_Command_SPIMulti(void)\r
{\r
struct\r
/** Programmer ID string, returned to the host during the CMD_SIGN_ON command processing */
#define PROGRAMMER_ID "AVRISP_MK2"\r
\r
+ /** Mask for the reading or writing of the high byte in a FLASH word when issuing a low-level programming command */\r
#define READ_WRITE_HIGH_BYTE_MASK (1 << 3)\r
\r
#define PROG_MODE_PAGED_WRITES_MASK (1 << 0)\r
};\r
\r
\r
+/** Loads saved non-volatile parameter values from the EEPROM into the parameter table, as needed. */\r
void V2Params_LoadEEPROMParamValues(void)\r
{\r
/* Target RESET line polarity is a non-volatile value, retrieve current parameter value from EEPROM */\r
V2Params_GetParamFromTable(PARAM_RESET_POLARITY)->ParamValue = eeprom_read_byte(&EEPROM_Rest_Polarity);\r
}\r
\r
-static ParameterItem_t* V2Params_GetParamFromTable(uint8_t ParamID)\r
-{\r
- /* Find the parameter in the parameter table if present */\r
- for (uint8_t TableIndex = 0; TableIndex < (sizeof(ParameterTable) / sizeof(ParameterTable[0])); TableIndex++)\r
- {\r
- if (ParamID == ParameterTable[TableIndex].ParamID)\r
- return &ParameterTable[TableIndex];\r
- }\r
- \r
- return NULL;\r
-}\r
-\r
+/** Retrieves the host PC read/write privellages for a given parameter in the parameter table. This should\r
+ * be called before calls to \ref V2Params_GetParameterValue() or \ref V2Params_SetParameterValue() when\r
+ * getting or setting parameter values in response to requests from the host.\r
+ *\r
+ * \param ParamID Parameter ID whose privellages are to be retrieved from the table\r
+ *\r
+ * \return Privellages for the requested parameter, as a mask of PARAM_PRIV_* masks\r
+ */ \r
uint8_t V2Params_GetParameterPrivellages(uint8_t ParamID)\r
{\r
ParameterItem_t* ParamInfo = V2Params_GetParamFromTable(ParamID);\r
return ParamInfo->ParamPrivellages;\r
}\r
\r
+/** Retrieves the current value for a given parameter in the parameter table.\r
+ *\r
+ * \param ParamID Parameter ID whose value is to be retrieved from the table\r
+ *\r
+ * \return Current value of the parameter in the table, or 0 if not found\r
+ */ \r
uint8_t V2Params_GetParameterValue(uint8_t ParamID)\r
{\r
ParameterItem_t* ParamInfo = V2Params_GetParamFromTable(ParamID);\r
return ParamInfo->ParamValue;\r
}\r
\r
+/** Sets the value for a given parameter in the parameter table.\r
+ *\r
+ * \param ParamID Parameter ID whose value is to be set in the table\r
+ * \param Value New value to set the parameter to\r
+ *\r
+ * \return Pointer to the associated parameter information from the parameter table if found, NULL otherwise\r
+ */\r
void V2Params_SetParameterValue(uint8_t ParamID, uint8_t Value)\r
{\r
ParameterItem_t* ParamInfo = V2Params_GetParamFromTable(ParamID);\r
if (ParamID == PARAM_RESET_POLARITY)\r
eeprom_write_byte(&EEPROM_Rest_Polarity, Value); \r
}\r
+\r
+/** Retrieves a parameter entry (including ID, value and privellages) from the parameter table that matches the given\r
+ * parameter ID.\r
+ *\r
+ * \param ParamID Parameter ID to find in the table\r
+ *\r
+ * \return Pointer to the associated parameter information from the parameter table if found, NULL otherwise\r
+ */\r
+static ParameterItem_t* V2Params_GetParamFromTable(uint8_t ParamID)\r
+{\r
+ /* Find the parameter in the parameter table if present */\r
+ for (uint8_t TableIndex = 0; TableIndex < (sizeof(ParameterTable) / sizeof(ParameterTable[0])); TableIndex++)\r
+ {\r
+ if (ParamID == ParameterTable[TableIndex].ParamID)\r
+ return &ParameterTable[TableIndex];\r
+ }\r
+ \r
+ return NULL;\r
+}\r
#include "V2ProtocolConstants.h"\r
\r
/* Macros: */\r
- /* Parameter privellage mask to allow the host PC to read the parameter's value */\r
+ /** Parameter privellage mask to allow the host PC to read the parameter's value */\r
#define PARAM_PRIV_READ (1 << 0)\r
\r
- /* Parameter privellage mask to allow the host PC to change the parameter's value */\r
+ /** Parameter privellage mask to allow the host PC to change the parameter's value */\r
#define PARAM_PRIV_WRITE (1 << 1)\r
/* Type Defines: */\r
#include "V2ProtocolTarget.h"\r
\r
/** Current memory address for FLASH/EEPROM memory read/write commands */\r
-uint32_t CurrentAddress; \r
+uint32_t CurrentAddress;\r
\r
+/** Converts the given AVR Studio SCK duration parameter (set by a SET PARAM command from the host) to the nearest\r
+ * possible SPI clock prescaler mask for passing to the SPI_Init() routine.\r
+ *\r
+ * \return Nearest SPI prescaler mask for the given SCK frequency\r
+ */\r
uint8_t V2Protocol_GetSPIPrescalerMask(void)\r
{\r
static const uint8_t SPIMaskFromSCKDuration[TOTAL_PROGRAMMING_SPEEDS] =\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
+ #if (F_CPU == 8000000)\r
+ SPI_SPEED_FCPU_DIV_2, // AVRStudio = 8MHz SPI, Actual = 4MHz SPI\r
+ SPI_SPEED_FCPU_DIV_2, // AVRStudio = 4MHz SPI, Actual = 4MHz SPI\r
+ SPI_SPEED_FCPU_DIV_4, // AVRStudio = 2MHz SPI, Actual = 2MHz SPI\r
+ SPI_SPEED_FCPU_DIV_8, // AVRStudio = 1MHz SPI, Actual = 1MHz SPI\r
+ SPI_SPEED_FCPU_DIV_16, // AVRStudio = 500KHz SPI, Actual = 500KHz SPI\r
+ SPI_SPEED_FCPU_DIV_32, // AVRStudio = 250KHz SPI, Actual = 250KHz SPI\r
+ SPI_SPEED_FCPU_DIV_64 // AVRStudio = 125KHz SPI, Actual = 125KHz SPI \r
+ #if (F_CPU == 16000000)\r
+ SPI_SPEED_FCPU_DIV_2, // AVRStudio = 8MHz SPI, Actual = 8MHz SPI\r
+ SPI_SPEED_FCPU_DIV_4, // AVRStudio = 4MHz SPI, Actual = 4MHz SPI\r
+ SPI_SPEED_FCPU_DIV_8, // AVRStudio = 2MHz SPI, Actual = 2MHz SPI\r
+ SPI_SPEED_FCPU_DIV_16, // AVRStudio = 1MHz SPI, Actual = 1MHz SPI\r
+ SPI_SPEED_FCPU_DIV_32, // AVRStudio = 500KHz SPI, Actual = 500KHz SPI\r
+ SPI_SPEED_FCPU_DIV_64, // AVRStudio = 250KHz SPI, Actual = 250KHz SPI \r
+ SPI_SPEED_FCPU_DIV_128 // AVRStudio = 125KHz SPI, Actual = 125KHz SPI\r
+ #else\r
+ #error No SPI prescaler masks for chosen F_CPU speed.\r
+ #endif\r
};\r
\r
uint8_t SCKDuration = V2Params_GetParameterValue(PARAM_SCK_DURATION);\r
return SPIMaskFromSCKDuration[SCKDuration];\r
}\r
\r
+/** Asserts or deasserts the target's reset line, using the correct polarity as set by the host using a SET PARAM command.\r
+ * When not asserted, the line is tristated so as not to intefere with normal device operation.\r
+ *\r
+ * \param ResetTarget Boolean true when the target should be held in reset, false otherwise\r
+ */\r
void V2Protocol_ChangeTargetResetLine(bool ResetTarget)\r
{\r
if (ResetTarget)\r
}\r
}\r
\r
+/** Waits until the last issued target memory programming command has completed, via the check mode given and using\r
+ * the given parameters.\r
+ *\r
+ * \param ProgrammingMode Programming mode used and completion check to use, a mask of PROG_MODE_* constants\r
+ * \param PollAddress Memory address to poll for completion if polling check mode used\r
+ * \param PollValue Poll value to check against if polling check mode used\r
+ * \param DelayMS Milliseconds to delay before returning if delay check mode used\r
+ * \param ReadMemCommand Device low-level READ MEMORY command to send if value check mode used\r
+ *\r
+ * \return V2 Protocol status \ref STATUS_CMD_OK if the no timeout occurred, \ref STATUS_RDY_BSY_TOUT or\r
+ * \ref STATUS_CMD_TOUT otherwise\r
+ */\r
uint8_t V2Protocol_WaitForProgComplete(uint8_t ProgrammingMode, uint16_t PollAddress, uint8_t PollValue,\r
uint8_t DelayMS, uint8_t ReadMemCommand)\r
{\r
while ((SPI_TransferByte(0x00) != PollValue) && (TCNT0 < TARGET_BUSY_TIMEOUT_MS));\r
\r
if (TCNT0 >= TARGET_BUSY_TIMEOUT_MS)\r
- ProgrammingStatus = STATUS_RDY_BSY_TOUT;\r
+ ProgrammingStatus = STATUS_CMD_TOUT;\r
\r
break; \r
case PROG_MODE_WORD_READYBUSY_MASK:\r
return ProgrammingStatus;\r
}\r
\r
+/** Waits until the target has completed the last operation, by continuously polling the device's\r
+ * BUSY flag until it is cleared, or until the \ref TARGET_BUSY_TIMEOUT_MS timeout period has expired.\r
+ *\r
+ * \return V2 Protocol status \ref STATUS_CMD_OK if the no timeout occurred, \ref STATUS_RDY_BSY_TOUT otherwise\r
+ */\r
uint8_t V2Protocol_WaitWhileTargetBusy(void)\r
{\r
TCNT0 = 0;\r
return STATUS_CMD_OK;\r
}\r
\r
+/** Sends a low-level LOAD EXTENDED ADDRESS command to the target, for addressing of memory beyond the\r
+ * 64KB boundary. This sends the command with the correct address as indicated by the current address\r
+ * pointer variable set by the host when a SET ADDRESS command is issued.\r
+ */\r
void V2Protocol_LoadExtendedAddress(void)\r
{\r
SPI_SendByte(0x4D);\r
extern uint32_t CurrentAddress;\r
\r
/* Inline Functions: */\r
- static inline void V2Protocol_DelayMS(uint8_t MS)\r
+ /** Blocking delay for a given number of milliseconds, via a hardware timer.\r
+ *\r
+ * \param DelayMS Number of milliseconds to delay for\r
+ */\r
+ static inline void V2Protocol_DelayMS(uint8_t DelayMS)\r
{\r
TCNT0 = 0;\r
- while (TCNT0 < MS);\r
+ while (TCNT0 < DelayMS);\r
}\r
\r
/* Function Prototypes: */\r