Skip to content

Commit 4671a67

Browse files
committed
Pull request #40: Feature/MCU16GITHUB-957 improve the icsp inhibit ui ux
Merge in MCU16CE/dspic33-dsc-bootloader-code-examples from ~C12109/dspic33-dsc-bootloader-code-examples:feature/MCU16GITHUB-957-improve-the-icsp-inhibit-ui-ux to feature/MCU16GITHUB-842-add-the-icsp-inhibit-feature * commit '7f93c5d2a9d8eb1eb4a3c9842cf671567d8f6452': Select correct debugging port. remove .yml files. Add *.yml to gitignore. only allow alphanumeric input keys. disable the ICSP enable for safety during development of demo. refactor and enable bootloader detection refactor update functionality updated, but not working completely. block ICSP inhibit when programmed directly: MCU16GITHUB-959
2 parents f30d30b + 7f93c5d commit 4671a67

File tree

11 files changed

+306
-75
lines changed

11 files changed

+306
-75
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*.ko
1010
*.obj
1111
*.elf
12+
*.yml
1213

1314
# Excluding documentation output directories
1415
#docs/
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
© [2024] Microchip Technology Inc. and its subsidiaries.
3+
4+
Subject to your compliance with these terms, you may use Microchip
5+
software and any derivatives exclusively with Microchip products.
6+
You are responsible for complying with 3rd party license terms
7+
applicable to your use of 3rd party software (including open source
8+
software) that may accompany Microchip software. SOFTWARE IS ?AS IS.?
9+
NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS
10+
SOFTWARE, INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT,
11+
MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
12+
WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
13+
INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY
14+
KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF
15+
MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE
16+
FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP?S
17+
TOTAL LIABILITY ON ALL CLAIMS RELATED TO THE SOFTWARE WILL NOT
18+
EXCEED AMOUNT OF FEES, IF ANY, YOU PAID DIRECTLY TO MICROCHIP FOR
19+
THIS SOFTWARE.
20+
*/
21+
22+
#include <stdbool.h>
23+
24+
static bool icspEnabled = false;
25+
26+
//temp stubs - replace with real implementation
27+
bool ICSP_INHIBIT_IsEnabled(void)
28+
{
29+
return icspEnabled;
30+
}
31+
32+
bool ICSP_INHIBIT_Enable()
33+
{
34+
icspEnabled = true;
35+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
© [2024] Microchip Technology Inc. and its subsidiaries.
3+
4+
Subject to your compliance with these terms, you may use Microchip
5+
software and any derivatives exclusively with Microchip products.
6+
You are responsible for complying with 3rd party license terms
7+
applicable to your use of 3rd party software (including open source
8+
software) that may accompany Microchip software. SOFTWARE IS ?AS IS.?
9+
NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS
10+
SOFTWARE, INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT,
11+
MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
12+
WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
13+
INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY
14+
KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF
15+
MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE
16+
FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP?S
17+
TOTAL LIABILITY ON ALL CLAIMS RELATED TO THE SOFTWARE WILL NOT
18+
EXCEED AMOUNT OF FEES, IF ANY, YOU PAID DIRECTLY TO MICROCHIP FOR
19+
THIS SOFTWARE.
20+
*/
21+
22+
#ifndef ICSP_INHIBIT_H
23+
#define ICSP_INHIBIT_H
24+
25+
#include <stdbool.h>
26+
27+
bool ICSP_INHIBIT_IsEnabled(void);
28+
bool ICSP_INHIBIT_Enable(void);
29+
30+
#endif

secure_boot_and_secure_firmware_upgrade_over_canfd/icsp_inhibit.X/icsp_inhibit.mc3

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

secure_boot_and_secure_firmware_upgrade_over_canfd/icsp_inhibit.X/main.c

Lines changed: 140 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -18,123 +18,194 @@
1818
EXCEED AMOUNT OF FEES, IF ANY, YOU PAID DIRECTLY TO MICROCHIP FOR
1919
THIS SOFTWARE.
2020
*/
21+
22+
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
2123
#warning "!!RUNNING THIS PROGRAM AND FOLLOWING THE STEPS OUTLINED IN THE CONSOLE WILL PERMANENTLY DISABLE DIRECT PROGRAMMING OF THE BOARD. FOR ADDITIONAL INFORMATION, SEE THE README.MD INCLUDED WITH THIS PROJECT AND THE FAMILY DATA SHEET LOCATED AT https://ww1.microchip.com/downloads/aemDocuments/documents/MCU16/ProductDocuments/DataSheets/dsPIC33CK1024MP710-Family-Data-Sheet-DS70005496.pdf"
2224

23-
#include <stdio.h>
24-
#include <string.h>
25-
#include <stdbool.h>
2625
#include "mcc_generated_files/flash/flash.h"
2726
#include "mcc_generated_files/flash/flash_types.h"
2827
#include "mcc_generated_files/boot/boot_config.h"
28+
#include "mcc_generated_files/uart/uart1.h"
29+
#include "mcc_generated_files/system/system.h"
30+
#include "icsp_inhibit.h"
31+
#include "terminal.h"
32+
33+
#include <stdio.h>
34+
#include <string.h>
35+
#include <stdbool.h>
36+
#include <ctype.h>
2937

30-
#define WINDOW_SIZE 10
38+
#define USER_INPUT_BUFFER_SIZE 50
3139
#define UNLOCK_COMMAND "LOCKDEVICE"
40+
#define STRCMP_MATCHES 0
41+
#define ENTER '\r'
42+
#define TERMINAL_LINE_ERROR 7
43+
#define TERMINAL_LINE_INPUT 5
44+
#define TERMINAL_LINE_HOME 1
3245

3346
// Function prototypes
34-
static void ClearTerminalScreen(void);
35-
static void ClearTerminalLine(void);
36-
static void MoveCursor(int row);
37-
static void HideCursor(void);
3847
static void PrintWarning(void);
39-
static void AppendCharToWindow(char receivedChar, char *window, int *windowIndex);
40-
static void ProcessReceivedChar(char receivedChar, char *window, int *windowIndex);
41-
static void ResetWindowOnMismatch(char *window, int *windowIndex);
42-
static void CheckForUnlockCommand(char *window, int *windowIndex);
4348
static uint32_t GetResetAddress();
4449
static bool WasLoadedByBootloader();
50+
static void PrintBootloaderRequired(void);
51+
static char* ScanInput(void);
52+
static void PrintErrorMessage(char* error);
53+
static void PrintProgrammingDisabled(void);
54+
static void ClearUserInput(void);
55+
static void ClearErrorMessage(void);
56+
static void ResetPrompt(void);
57+
58+
// Local Variables
59+
static char userInput[USER_INPUT_BUFFER_SIZE] = {0};
60+
static bool errorPresent = false;
4561

4662
int main(void)
4763
{
48-
char window[WINDOW_SIZE + 1] = {0};
49-
int windowIndex = 0;
64+
const char* keyword = "LOCKDEVICE";
5065

5166
SYSTEM_Initialize();
52-
ClearTerminalScreen();
67+
68+
if(WasLoadedByBootloader() == false)
69+
{
70+
PrintBootloaderRequired();
71+
72+
while(1)
73+
{
74+
}
75+
}
76+
77+
if(ICSP_INHIBIT_IsEnabled())
78+
{
79+
PrintProgrammingDisabled();
80+
81+
while(1)
82+
{
83+
}
84+
}
85+
5386
PrintWarning();
54-
while (1)
87+
88+
while(1)
5589
{
56-
if (UART1_IsRxReady())
90+
char* userInput = ScanInput();
91+
92+
if(strcmp(userInput, keyword) == STRCMP_MATCHES)
93+
{
94+
//ICSP_INHIBIT_Enable(); //Disable during development for safety
95+
96+
PrintProgrammingDisabled();
97+
98+
while(1)
99+
{
100+
}
101+
}
102+
else
57103
{
58-
char receivedChar = UART1_Read();
59-
ProcessReceivedChar(receivedChar, window, &windowIndex);
60-
CheckForUnlockCommand(window, &windowIndex);
104+
PrintErrorMessage("Invalid keyword entered. Try again.");
61105
}
62106
}
63107
}
64108

65-
static void ClearTerminalScreen(void)
109+
static char* ScanInput(void)
66110
{
67-
printf("\033[2J");
68-
}
111+
uint8_t userInputOffset = 0;
112+
char key;
113+
114+
ClearUserInput();
115+
116+
do
117+
{
118+
key = UART1_Read();
119+
120+
/* If there is still an error message after the first user key press,
121+
* clear the error and reset the prompt before printing their input. */
122+
if(errorPresent)
123+
{
124+
ClearErrorMessage();
125+
ResetPrompt();
126+
}
127+
128+
if(isalnum(key) && (userInputOffset < sizeof(userInput)))
129+
{
130+
userInput[userInputOffset++] = key;
131+
printf("%c", key);
132+
}
133+
}
134+
while(key != ENTER);
69135

70-
static void ClearTerminalLine(void)
71-
{
72-
printf("\33[2K\r");
136+
return userInput;
73137
}
74138

75-
static void MoveCursor(int row)
139+
static void PrintProgrammingDisabled(void)
76140
{
77-
printf("\033[%d;0f", row);
141+
TERMINAL_EnableCursor(false);
142+
TERMINAL_MoveCursor(TERMINAL_LINE_HOME);
143+
TERMINAL_ClearScreen();
144+
145+
printf("\r\n");
146+
printf("\r\n");
147+
printf("\r\n");
148+
printf("*** ICSP Programming/Debugging permanently disabled. ***\r\n");
149+
printf("\r\n");
150+
printf("Use the bootloader to load all future applications into this board.");
78151
}
79152

80-
static void HideCursor(void)
153+
static void ClearErrorMessage(void)
81154
{
82-
printf("\033[?25l");
155+
TERMINAL_MoveCursor(TERMINAL_LINE_ERROR);
156+
TERMINAL_ClearLine();
157+
158+
errorPresent = false;
83159
}
84160

85-
static void PrintWarning(void)
161+
static void ResetPrompt(void)
86162
{
87-
MoveCursor(1);
88-
printf("Type LOCKDEVICE to enable the ICSP Inhibit feature.");
89-
MoveCursor(3);
90-
printf("WARNING: THIS PERMANENTLY DISABLES DIRECT PROGRAMMING OF THE BOARD.");
163+
TERMINAL_MoveCursor(TERMINAL_LINE_INPUT);
164+
TERMINAL_ClearLine();
165+
printf(">> ");
91166
}
92167

93-
static void ProcessReceivedChar(char receivedChar, char *window, int *windowIndex)
168+
static void PrintErrorMessage(char* errorMessage)
94169
{
95-
bool isCharValid = (*windowIndex < strlen(UNLOCK_COMMAND)) && (receivedChar == UNLOCK_COMMAND[*windowIndex]);
170+
ClearErrorMessage();
96171

97-
if (isCharValid)
98-
{
99-
AppendCharToWindow(receivedChar, window, windowIndex);
100-
MoveCursor(10);
101-
printf("%s", window);
102-
}
103-
else
104-
{
105-
ResetWindowOnMismatch(window, windowIndex);
106-
}
172+
printf("%s", errorMessage);
173+
errorPresent = true;
174+
175+
ResetPrompt();
107176
}
108177

109-
static void AppendCharToWindow(char receivedChar, char *window, int *windowIndex)
178+
static void ClearUserInput(void)
110179
{
111-
window[(*windowIndex)++] = receivedChar;
112-
window[*windowIndex] = '\0';
180+
memset(userInput, 0, sizeof(userInput));
113181
}
114182

115-
static void ResetWindowOnMismatch(char *window, int *windowIndex)
183+
static void PrintWarning(void)
116184
{
117-
MoveCursor(10);
118-
ClearTerminalLine();
119-
MoveCursor(5);
120-
ClearTerminalLine();
121-
printf("Invalid character entered. Try again.");
122-
*windowIndex = 0;
123-
memset(window, 0, WINDOW_SIZE + 1);
185+
TERMINAL_EnableCursor(false);
186+
TERMINAL_MoveCursor(TERMINAL_LINE_HOME);
187+
TERMINAL_ClearScreen();
188+
189+
printf("Type LOCKDEVICE (plus ENTER) to enable the ICSP Inhibit feature.\r\n");
190+
printf("\r\n");
191+
printf("WARNING: THIS PERMANENTLY DISABLES DIRECT PROGRAMMING OF THE BOARD.\r\n");
192+
printf("\r\n");
193+
printf(">> ");
194+
195+
TERMINAL_EnableCursor(true);
124196
}
125197

126-
static void CheckForUnlockCommand(char *window, int *windowIndex)
198+
static void PrintBootloaderRequired(void)
127199
{
128-
if (strncmp(window, UNLOCK_COMMAND, *windowIndex) == 0 && *windowIndex == strlen(UNLOCK_COMMAND))
129-
{
130-
MoveCursor(5);
131-
ClearTerminalLine();
132-
printf("ICSP Programming/Debugging permanently disabled. \n");
133-
MoveCursor(10);
134-
ClearTerminalLine();
135-
*windowIndex = 0;
136-
memset(window, 0, WINDOW_SIZE + 1);
137-
}
200+
TERMINAL_EnableCursor(false);
201+
TERMINAL_MoveCursor(TERMINAL_LINE_HOME);
202+
TERMINAL_ClearScreen();
203+
204+
printf("NO BOOTLOADER DETECTED!\r\n");
205+
printf("\r\n");
206+
printf("Because programming will be permanently disabled, \r\n");
207+
printf("a bootloader is required to run this demo. \r\n");
208+
printf("Please see the readme.md for more information.\r\n");
138209
}
139210

140211
/* The following code finds the address used by GOTO instruction programmed

secure_boot_and_secure_firmware_upgrade_over_canfd/icsp_inhibit.X/mcc_generated_files/system/src/config_bits.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
#pragma config BISTDIS = DISABLED //Memory BIST Feature Disable->mBIST on reset feature disabled
7575

7676
// FICD
77-
#pragma config ICS = PGD1 //ICD Communication Channel Select bits->Communicate on PGC1 and PGD1
77+
#pragma config ICS = PGD3 //ICD Communication Channel Select bits->Communicate on PGC3 and PGD3
7878
#pragma config JTAGEN = OFF //JTAG Enable bit->JTAG is disabled
7979
#pragma config NOBTSWP = DISABLED //BOOTSWP instruction disable bit->BOOTSWP instruction is disabled
8080

secure_boot_and_secure_firmware_upgrade_over_canfd/icsp_inhibit.X/mcc_generated_files/system/src/pins.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ void PINS_Initialize(void)
9898
* Setting the Analog/Digital Configuration SFR(s)
9999
***************************************************************************/
100100
ANSELA = 0x001F;
101-
ANSELB = 0x009E;
101+
ANSELB = 0x039E;
102102
ANSELC = 0x00CF;
103103
ANSELD = 0x2C00;
104104
ANSELE = 0x000F;

secure_boot_and_secure_firmware_upgrade_over_canfd/icsp_inhibit.X/nbproject/configurations.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
<itemPath>mcc_generated_files/uart/uart1.h</itemPath>
3636
</logicalFolder>
3737
</logicalFolder>
38+
<itemPath>icsp_inhibit.h</itemPath>
39+
<itemPath>terminal.h</itemPath>
3840
</logicalFolder>
3941
<logicalFolder name="ExternalFiles"
4042
displayName="Important Files"
@@ -95,8 +97,13 @@
9597
</logicalFolder>
9698
</logicalFolder>
9799
<itemPath>main.c</itemPath>
100+
<itemPath>icsp_inhibit.c</itemPath>
101+
<itemPath>terminal.c</itemPath>
98102
</logicalFolder>
99103
</logicalFolder>
104+
<sourceRootList>
105+
<Elem>.</Elem>
106+
</sourceRootList>
100107
<projectmakefile>Makefile</projectmakefile>
101108
<confs>
102109
<conf name="default" type="2">
@@ -453,6 +460,8 @@
453460
value="${memories.instruction.ram.ranges}"/>
454461
<property key="memories.programmemory" value="true"/>
455462
<property key="memories.programmemory.ranges" value="0-afeff"/>
463+
<property key="programmerToGoFilePath"
464+
value="C:/work/repos/dspic33-dsc-bootloader-code-examples/secure_boot_and_secure_firmware_upgrade_over_canfd/icsp_inhibit.X/debug/default/icsp_inhibit_ptg"/>
456465
<property key="programoptions.donoteraseauxmem" value="false"/>
457466
<property key="programoptions.eraseb4program" value="true"/>
458467
<property key="programoptions.pgmentry.voltage" value="low"/>

0 commit comments

Comments
 (0)