diff --git a/devices.c b/devices.c index 369eb10..29e0231 100644 --- a/devices.c +++ b/devices.c @@ -13,7 +13,7 @@ tDevice DEVICES_List[] = 0x1100, 0x1280, 0x1300, - 11 + 9 }, { "mega320x", @@ -25,7 +25,7 @@ tDevice DEVICES_List[] = 0x1100, 0x1280, 0x1300, - 11 + 9 }, { "mega160x", @@ -37,7 +37,7 @@ tDevice DEVICES_List[] = 0x1100, 0x1280, 0x1300, - 11 + 9 }, { "mega80x", @@ -49,7 +49,7 @@ tDevice DEVICES_List[] = 0x1100, 0x1280, 0x1300, - 11 + 9 }, { "tiny321x", @@ -87,7 +87,7 @@ tDevice DEVICES_List[] = 0x1300, 9 }, - { + { "tiny80x", 0x8000, 8 * 1024, @@ -111,7 +111,7 @@ tDevice DEVICES_List[] = 0x1300, 9 }, - { + { "tiny40x", 0x8000, 4 * 1024, @@ -133,7 +133,7 @@ tDevice DEVICES_List[] = 0x1100, 0x1280, 0x1300, - 11 + 9 }, { "tiny20x", diff --git a/devices.h b/devices.h index f3e6cbd..8477e1a 100644 --- a/devices.h +++ b/devices.h @@ -7,6 +7,8 @@ #define DEVICE_UNKNOWN_ID (-1) +#define DEVICE_LOCKBIT_ADDR (0x0A) + typedef struct { char name[DEVICES_NAME_LEN]; diff --git a/main.c b/main.c index 61e9f4e..859c090 100644 --- a/main.c +++ b/main.c @@ -20,8 +20,8 @@ #define COMPORT_LEN (32) #define FUSES_LEN (128) -#define SW_VER_NUMBER "0.6" -#define SW_VER_DATE "06.04.2022" +#define SW_VER_NUMBER "0.7" +#define SW_VER_DATE "09.03.2022" typedef struct { @@ -30,6 +30,8 @@ typedef struct bool read; bool wr_fuses; bool rd_fuses; + bool lock; + bool unlock; bool show_info; uint32_t baudrate; int8_t device; @@ -56,6 +58,8 @@ void help(void) printf(" -e - erase device\n"); printf(" -fw X:0xYY - write fuses (X - fuse number, 0xYY - hex value)\n"); printf(" -fr - read all fuses\n"); + printf(" -ls - lock device\n"); + printf(" -lr - unlock device\n"); printf(" -h - show this help screen\n"); printf(" -mX - set logging level (0-all/1-warnings/2-errors)\n"); printf(" -r FILE.HEX - Hex file to read MCU flash into\n"); @@ -66,7 +70,10 @@ void help(void) for (i = 1; i < DEVICES_GetNumber()+1; i++) { printf("%-14s", DEVICES_GetNameByNumber(i-1)); - if (i % 4 == 0 ? printf("\n "): printf("")); + if (i % 4 == 0) + { + printf("\n "); + } } printf("\n"); } @@ -207,6 +214,21 @@ int main(int argc, char* argv[]) error = true; } break; + case 'l': + /**< lock/unlock device */ + if (argv[i][2] == 's') + { + parameters.lock = true; + } else + if (argv[i][2] == 'r') + { + parameters.unlock = true; + } else + { + printf("%s: wrong or unsupported fuses parameter!\n", argv[i]); + error = true; + } + break; case 'm': /**< level of messaging */ if (argv[i][2] >= '0' && argv[i][2] <= '2') @@ -246,7 +268,8 @@ int main(int argc, char* argv[]) printf("COM port name is missing!\n"); return -1; } - if (!parameters.read && !parameters.write && !parameters.erase && !parameters.rd_fuses && !parameters.wr_fuses) + if (!parameters.read && !parameters.write && !parameters.erase && !parameters.rd_fuses && + !parameters.wr_fuses && !parameters.unlock) { printf("Nothing to do, stopping\n"); return -1; @@ -258,13 +281,23 @@ int main(int argc, char* argv[]) return -1; } + printf("Working with device: %s\n", DEVICES_GetNameByNumber(parameters.device)); + + if (parameters.unlock == true) + { + printf("Unlocking... "); + if (NVM_UnlockDevice() == true) + { + printf("OK\n"); + } + } + if (NVM_EnterProgmode() == false) { printf("Can't enter programming mode, exiting\n"); return -1; } - printf("Working with device: %s\n", DEVICES_GetNameByNumber(parameters.device)); /**< process input parameters */ if (parameters.erase == true) { @@ -325,6 +358,14 @@ int main(int argc, char* argv[]) printf("Reading to file: %s%c%s\n", cwd, ch, parameters.rd_file); NVM_SaveIhex(parameters.rd_file, DEVICES_GetFlashStart(), DEVICES_GetFlashLength()); } + if (parameters.lock == true) + { + printf("Locking MCU... "); + if (NVM_WriteFuse(DEVICE_LOCKBIT_ADDR, 0x00) == true) + { + printf("OK\n"); + } + } NVM_LeaveProgmode(); PHY_Close(); diff --git a/nvm.c b/nvm.c index 7559001..3d4c03c 100644 --- a/nvm.c +++ b/nvm.c @@ -51,17 +51,23 @@ void NVM_LeaveProgmode(void) * \return Nothing * */ -void NVM_UnlockDevice(void) +bool NVM_UnlockDevice(void) { if (NVM_Progmode == true) { LOG_Print(LOG_LEVEL_WARNING, "Device already unlocked"); - return; + } else + { + // Unlock after using the NVM key results in prog mode. + if (APP_Unlock() == true) + { + NVM_Progmode = true; + } else + { + return false; + } } - - // Unlock after using the NVM key results in prog mode. - if (APP_Unlock() == true) - NVM_Progmode = true; + return true; } /** \brief Erase chip flash memory diff --git a/nvm.h b/nvm.h index bbe24b0..42595ed 100644 --- a/nvm.h +++ b/nvm.h @@ -8,6 +8,7 @@ bool NVM_EnterProgmode(void); void NVM_LeaveProgmode(void); +bool NVM_UnlockDevice(void); bool NVM_ChipErase(void); uint8_t NVM_ReadFuse(uint8_t fusenum); bool NVM_WriteFuse(uint8_t fusenum, uint8_t value); diff --git a/updiprog.cbp b/updiprog.cbp index 5ffb49d..e9ad34e 100644 --- a/updiprog.cbp +++ b/updiprog.cbp @@ -11,7 +11,7 @@