Clean up and add more comments to the AVRISP-MKII project. Make sure the SPI_MULTI...
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / XPROG / XPROGProtocol.c
index 39f82a0..1be159c 100644 (file)
@@ -1,13 +1,13 @@
 /*\r
              LUFA Library\r
 /*\r
              LUFA Library\r
-     Copyright (C) Dean Camera, 2009.\r
+     Copyright (C) Dean Camera, 2010.\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\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
+  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
 \r
   Permission to use, copy, modify, distribute, and sell this \r
   software and its documentation for any purpose is hereby granted\r
 \r
   Permission to use, copy, modify, distribute, and sell this \r
   software and its documentation for any purpose is hereby granted\r
@@ -38,7 +38,7 @@
 \r
 #if defined(ENABLE_XPROG_PROTOCOL) || defined(__DOXYGEN__)\r
 /** Base absolute address for the target's NVM controller for PDI programming */\r
 \r
 #if defined(ENABLE_XPROG_PROTOCOL) || defined(__DOXYGEN__)\r
 /** Base absolute address for the target's NVM controller for PDI programming */\r
-uint32_t XPROG_Param_NVMBase    = 0x010001C0;\r
+uint32_t XPROG_Param_NVMBase = 0x010001C0;\r
 \r
 /** Size in bytes of the target's EEPROM page */\r
 uint16_t XPROG_Param_EEPageSize;\r
 \r
 /** Size in bytes of the target's EEPROM page */\r
 uint16_t XPROG_Param_EEPageSize;\r
@@ -124,6 +124,10 @@ static void XPROGProtocol_EnterXPROGMode(void)
                XPROGTarget_SendByte(PDI_CMD_STCS | PDI_RESET_REG);     \r
                XPROGTarget_SendByte(PDI_RESET_KEY);\r
 \r
                XPROGTarget_SendByte(PDI_CMD_STCS | PDI_RESET_REG);     \r
                XPROGTarget_SendByte(PDI_RESET_KEY);\r
 \r
+               /* Lower direction change guard time to 8 USART bits */\r
+               XPROGTarget_SendByte(PDI_CMD_STCS | PDI_CTRL_REG);      \r
+               XPROGTarget_SendByte(0x04);\r
+\r
                /* Enable access to the XPROG NVM bus by sending the documented NVM access key to the device */\r
                XPROGTarget_SendByte(PDI_CMD_KEY);      \r
                for (uint8_t i = sizeof(PDI_NVMENABLE_KEY); i > 0; i--)\r
                /* Enable access to the XPROG NVM bus by sending the documented NVM access key to the device */\r
                XPROGTarget_SendByte(PDI_CMD_KEY);      \r
                for (uint8_t i = sizeof(PDI_NVMENABLE_KEY); i > 0; i--)\r
@@ -137,6 +141,10 @@ static void XPROGProtocol_EnterXPROGMode(void)
                /* Enable TPI programming mode with the attached target */\r
                XPROGTarget_EnableTargetTPI();\r
                \r
                /* Enable TPI programming mode with the attached target */\r
                XPROGTarget_EnableTargetTPI();\r
                \r
+               /* Lower direction change guard time to 8 USART bits */\r
+               XPROGTarget_SendByte(TPI_CMD_SSTCS | TPI_CTRL_REG);\r
+               XPROGTarget_SendByte(0x04);             \r
+               \r
                /* Enable access to the XPROG NVM bus by sending the documented NVM access key to the device */\r
                XPROGTarget_SendByte(TPI_CMD_SKEY);     \r
                for (uint8_t i = sizeof(TPI_NVMENABLE_KEY); i > 0; i--)\r
                /* Enable access to the XPROG NVM bus by sending the documented NVM access key to the device */\r
                XPROGTarget_SendByte(TPI_CMD_SKEY);     \r
                for (uint8_t i = sizeof(TPI_NVMENABLE_KEY); i > 0; i--)\r
@@ -313,9 +321,14 @@ static void XPROGProtocol_WriteMemory(void)
        }\r
        else\r
        {\r
        }\r
        else\r
        {\r
+               Serial_TxByte((uint8_t)WriteMemory_XPROG_Params.Length);\r
+       \r
                /* Send write command to the TPI device, indicate timeout if occurred */\r
                /* Send write command to the TPI device, indicate timeout if occurred */\r
-               if (!(TINYNVM_WriteMemory(WriteMemory_XPROG_Params.Address, WriteMemory_XPROG_Params.ProgData[0])))\r
-                 ReturnStatus = XPRG_ERR_TIMEOUT;\r
+               if (!(TINYNVM_WriteMemory(WriteMemory_XPROG_Params.Address, WriteMemory_XPROG_Params.ProgData,\r
+                     WriteMemory_XPROG_Params.Length)))\r
+               {\r
+                       ReturnStatus = XPRG_ERR_TIMEOUT;\r
+               }\r
        }\r
        \r
        Endpoint_Write_Byte(CMD_XPROG);\r
        }\r
        \r
        Endpoint_Write_Byte(CMD_XPROG);\r
@@ -355,6 +368,8 @@ static void XPROGProtocol_ReadMemory(void)
        }\r
        else\r
        {\r
        }\r
        else\r
        {\r
+               Serial_TxByte((uint8_t)ReadMemory_XPROG_Params.Length);\r
+\r
                /* Read the TPI target's memory, indicate timeout if occurred */\r
                if (!(TINYNVM_ReadMemory(ReadMemory_XPROG_Params.Address, ReadBuffer, ReadMemory_XPROG_Params.Length)))\r
                  ReturnStatus = XPRG_ERR_TIMEOUT;\r
                /* Read the TPI target's memory, indicate timeout if occurred */\r
                if (!(TINYNVM_ReadMemory(ReadMemory_XPROG_Params.Address, ReadBuffer, ReadMemory_XPROG_Params.Length)))\r
                  ReturnStatus = XPRG_ERR_TIMEOUT;\r
@@ -440,10 +455,10 @@ static void XPROGProtocol_SetParam(void)
                case XPRG_PARAM_EEPPAGESIZE:\r
                        XPROG_Param_EEPageSize = Endpoint_Read_Word_BE();\r
                        break;\r
                case XPRG_PARAM_EEPPAGESIZE:\r
                        XPROG_Param_EEPageSize = Endpoint_Read_Word_BE();\r
                        break;\r
-               case XPRG_PARAM_NVMCMD:\r
+               case XPRG_PARAM_NVMCMD_REG:\r
                        XPROG_Param_NVMCMDRegAddr = Endpoint_Read_Byte();\r
                        break;\r
                        XPROG_Param_NVMCMDRegAddr = Endpoint_Read_Byte();\r
                        break;\r
-               case XPRG_PARAM_NVMCSR:\r
+               case XPRG_PARAM_NVMCSR_REG:\r
                        XPROG_Param_NVMCSRRegAddr = Endpoint_Read_Byte();\r
                        break;\r
                default:\r
                        XPROG_Param_NVMCSRRegAddr = Endpoint_Read_Byte();\r
                        break;\r
                default:\r