Add first draft of the TPI NVM commands for reading, writing and erasing a target...
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / XPROG / XPROGProtocol.c
index bccb96c..39f82a0 100644 (file)
@@ -9,13 +9,13 @@
 /*\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
+  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
 #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_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
@@ -74,7 +80,7 @@ void XPROGProtocol_SetMode(void)
 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
@@ -222,7 +228,9 @@ static void XPROGProtocol_Erase(void)
        }\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
@@ -294,18 +302,20 @@ static void XPROGProtocol_WriteMemory(void)
                }\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.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
-               // TODO\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
        }\r
        \r
        Endpoint_Write_Byte(CMD_XPROG);\r
@@ -339,13 +349,15 @@ static void XPROGProtocol_ReadMemory(void)
        \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
-               // TODO\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
@@ -420,13 +432,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
-       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