Added support to the MIDI Class drivers for packed data, where multiple MIDI events...
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / XPROG / XPROGProtocol.c
index bccb96c..9c6ee7e 100644 (file)
@@ -1,21 +1,21 @@
 /*\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
-\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
+  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
+  without fee, provided that the above copyright notice appear in \r
+  all 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 without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
 #include "XPROGProtocol.h"\r
 \r
 #if defined(ENABLE_XPROG_PROTOCOL) || defined(__DOXYGEN__)\r
 #include "XPROGProtocol.h"\r
 \r
 #if defined(ENABLE_XPROG_PROTOCOL) || defined(__DOXYGEN__)\r
-/** Base absolute address for the target's NVM controller */\r
+/** Base absolute address for the target's NVM controller for PDI programming */\r
 uint32_t XPROG_Param_NVMBase    = 0x010001C0;\r
 \r
 /** Size in bytes of the target's EEPROM page */\r
 uint32_t XPROG_Param_NVMBase    = 0x010001C0;\r
 \r
 /** Size in bytes of the target's EEPROM page */\r
-uint32_t XPROG_Param_EEPageSize;\r
+uint16_t XPROG_Param_EEPageSize;\r
+\r
+/** Address of the TPI device's NVMCMD register for TPI programming */\r
+uint8_t  XPROG_Param_NVMCMDRegAddr;\r
+\r
+/** Address of the TPI device's NVMCSR register for TPI programming */\r
+uint8_t  XPROG_Param_NVMCSRRegAddr;\r
 \r
 /** Currently selected XPROG programming protocol */\r
 uint8_t  XPROG_SelectedProtocol = XPRG_PROTOCOL_PDI;\r
 \r
 /** Currently selected XPROG programming protocol */\r
 uint8_t  XPROG_SelectedProtocol = XPRG_PROTOCOL_PDI;\r
@@ -74,7 +80,7 @@ void XPROGProtocol_SetMode(void)
 void XPROGProtocol_Command(void)\r
 {\r
        uint8_t XPROGCommand = Endpoint_Read_Byte();\r
 void XPROGProtocol_Command(void)\r
 {\r
        uint8_t XPROGCommand = Endpoint_Read_Byte();\r
-\r
+       \r
        switch (XPROGCommand)\r
        {\r
                case XPRG_CMD_ENTER_PROGMODE:\r
        switch (XPROGCommand)\r
        {\r
                case XPRG_CMD_ENTER_PROGMODE:\r
@@ -118,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
@@ -131,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
@@ -222,7 +236,9 @@ static void XPROGProtocol_Erase(void)
        }\r
        else\r
        {\r
        }\r
        else\r
        {\r
-               // TODO\r
+               /* Erase the target memory, indicate timeout if ocurred */\r
+               if (!(TINYNVM_EraseMemory()))\r
+                 ReturnStatus = XPRG_ERR_TIMEOUT;\r
        }\r
        \r
        Endpoint_Write_Byte(CMD_XPROG);\r
        }\r
        \r
        Endpoint_Write_Byte(CMD_XPROG);\r
@@ -294,18 +310,25 @@ static void XPROGProtocol_WriteMemory(void)
                }\r
                \r
                /* Send the appropriate memory write commands to the device, indicate timeout if occurred */\r
                }\r
                \r
                /* Send the appropriate memory write commands to the device, indicate timeout if occurred */\r
-               if ((PagedMemory && !XMEGANVM_WritePageMemory(WriteBuffCommand, EraseBuffCommand, WriteCommand, \r
+               if ((PagedMemory && !(XMEGANVM_WritePageMemory(WriteBuffCommand, EraseBuffCommand, WriteCommand, \r
                                                                                                           WriteMemory_XPROG_Params.PageMode, WriteMemory_XPROG_Params.Address,\r
                                                                                                           WriteMemory_XPROG_Params.PageMode, WriteMemory_XPROG_Params.Address,\r
-                                                                                                          WriteMemory_XPROG_Params.ProgData, WriteMemory_XPROG_Params.Length)) ||\r
-                  (!PagedMemory && !XMEGANVM_WriteByteMemory(WriteCommand, WriteMemory_XPROG_Params.Address,\r
-                                                                                                          WriteMemory_XPROG_Params.ProgData)))\r
+                                                                                                          WriteMemory_XPROG_Params.ProgData, WriteMemory_XPROG_Params.Length))) ||\r
+                  (!PagedMemory && !(XMEGANVM_WriteByteMemory(WriteCommand, WriteMemory_XPROG_Params.Address,\r
+                                                                                                          WriteMemory_XPROG_Params.ProgData[0]))))\r
                {\r
                        ReturnStatus = XPRG_ERR_TIMEOUT;\r
                }\r
        }\r
        else\r
        {\r
                {\r
                        ReturnStatus = XPRG_ERR_TIMEOUT;\r
                }\r
        }\r
        else\r
        {\r
-               // TODO\r
+               Serial_TxByte((uint8_t)WriteMemory_XPROG_Params.Length);\r
+       \r
+               /* Send write command to the TPI device, indicate timeout if occurred */\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
@@ -339,13 +362,17 @@ static void XPROGProtocol_ReadMemory(void)
        \r
        if (XPROG_SelectedProtocol == XPRG_PROTOCOL_PDI)\r
        {\r
        \r
        if (XPROG_SelectedProtocol == XPRG_PROTOCOL_PDI)\r
        {\r
-               /* Read the target's memory, indicate timeout if occurred */\r
+               /* Read the PDI target's memory, indicate timeout if occurred */\r
                if (!(XMEGANVM_ReadMemory(ReadMemory_XPROG_Params.Address, ReadBuffer, ReadMemory_XPROG_Params.Length)))\r
                  ReturnStatus = XPRG_ERR_TIMEOUT;\r
        }\r
        else\r
        {\r
                if (!(XMEGANVM_ReadMemory(ReadMemory_XPROG_Params.Address, ReadBuffer, ReadMemory_XPROG_Params.Length)))\r
                  ReturnStatus = XPRG_ERR_TIMEOUT;\r
        }\r
        else\r
        {\r
-               // TODO\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
        }\r
 \r
        Endpoint_Write_Byte(CMD_XPROG);\r
        }\r
 \r
        Endpoint_Write_Byte(CMD_XPROG);\r
@@ -420,13 +447,25 @@ static void XPROGProtocol_SetParam(void)
        uint8_t XPROGParam = Endpoint_Read_Byte();\r
        \r
        /* Determine which parameter is being set, store the new parameter value */\r
        uint8_t XPROGParam = Endpoint_Read_Byte();\r
        \r
        /* Determine which parameter is being set, store the new parameter value */\r
-       if (XPROGParam == XPRG_PARAM_NVMBASE)\r
-         XPROG_Param_NVMBase = Endpoint_Read_DWord_BE();\r
-       else if (XPROGParam == XPRG_PARAM_EEPPAGESIZE)\r
-         XPROG_Param_EEPageSize = Endpoint_Read_Word_BE();\r
-       else\r
-         ReturnStatus = XPRG_ERR_FAILED;\r
-       \r
+       switch (XPROGParam)\r
+       {\r
+               case XPRG_PARAM_NVMBASE:\r
+                       XPROG_Param_NVMBase = Endpoint_Read_DWord_BE();\r
+                       break;\r
+               case XPRG_PARAM_EEPPAGESIZE:\r
+                       XPROG_Param_EEPageSize = Endpoint_Read_Word_BE();\r
+                       break;\r
+               case XPRG_PARAM_NVMCMD:\r
+                       XPROG_Param_NVMCMDRegAddr = Endpoint_Read_Byte();\r
+                       break;\r
+               case XPRG_PARAM_NVMCSR:\r
+                       XPROG_Param_NVMCSRRegAddr = Endpoint_Read_Byte();\r
+                       break;\r
+               default:\r
+                       ReturnStatus = XPRG_ERR_FAILED;\r
+                       break;\r
+       }\r
+\r
        Endpoint_ClearOUT();\r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
                  \r
        Endpoint_ClearOUT();\r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
                  \r