Skip to content

Commit

Permalink
feat: Lock/unlock commands added (-ls/-lr), number of fuses fixed for…
Browse files Browse the repository at this point in the history
… all devices
  • Loading branch information
Alex Kiselev committed Mar 9, 2023
1 parent d05be65 commit d4a7241
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 24 deletions.
14 changes: 7 additions & 7 deletions devices.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ tDevice DEVICES_List[] =
0x1100,
0x1280,
0x1300,
11
9
},
{
"mega320x",
Expand All @@ -25,7 +25,7 @@ tDevice DEVICES_List[] =
0x1100,
0x1280,
0x1300,
11
9
},
{
"mega160x",
Expand All @@ -37,7 +37,7 @@ tDevice DEVICES_List[] =
0x1100,
0x1280,
0x1300,
11
9
},
{
"mega80x",
Expand All @@ -49,7 +49,7 @@ tDevice DEVICES_List[] =
0x1100,
0x1280,
0x1300,
11
9
},
{
"tiny321x",
Expand Down Expand Up @@ -87,7 +87,7 @@ tDevice DEVICES_List[] =
0x1300,
9
},
{
{
"tiny80x",
0x8000,
8 * 1024,
Expand All @@ -111,7 +111,7 @@ tDevice DEVICES_List[] =
0x1300,
9
},
{
{
"tiny40x",
0x8000,
4 * 1024,
Expand All @@ -133,7 +133,7 @@ tDevice DEVICES_List[] =
0x1100,
0x1280,
0x1300,
11
9
},
{
"tiny20x",
Expand Down
2 changes: 2 additions & 0 deletions devices.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#define DEVICE_UNKNOWN_ID (-1)

#define DEVICE_LOCKBIT_ADDR (0x0A)

typedef struct
{
char name[DEVICES_NAME_LEN];
Expand Down
51 changes: 46 additions & 5 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
Expand All @@ -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");
Expand All @@ -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");
}
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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;
Expand All @@ -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)
{
Expand Down Expand Up @@ -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();
Expand Down
18 changes: 12 additions & 6 deletions nvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions nvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 2 additions & 6 deletions updiprog.cbp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Option parameters="-r test.hex -c COM10 -d tiny161x -m2 -fw 8:0x04 -b 57600" />
<Option parameters="-d tiny161x -c COM5 -r test.hex -fr -lr -ls" />
<Compiler>
<Add option="-std=c99" />
<Add option="-g" />
Expand Down Expand Up @@ -78,10 +78,6 @@
</Unit>
<Unit filename="sleep.h" />
<Unit filename="updi.h" />
<Extensions>
<code_completion />
<envvars />
<debugger />
</Extensions>
<Extensions />
</Project>
</CodeBlocks_project_file>

0 comments on commit d4a7241

Please sign in to comment.