Skip to content

Commit 966c729

Browse files
committed
refactor
1 parent 6893c01 commit 966c729

File tree

3 files changed

+79
-93
lines changed

3 files changed

+79
-93
lines changed

secure_boot_and_secure_firmware_upgrade_over_canfd/icsp_inhibit.X/main.c

Lines changed: 67 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -22,44 +22,41 @@
2222
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
2323
#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"
2424

25-
#include <stdio.h>
26-
#include <string.h>
27-
#include <stdbool.h>
2825
#include "mcc_generated_files/flash/flash.h"
2926
#include "mcc_generated_files/flash/flash_types.h"
3027
#include "mcc_generated_files/boot/boot_config.h"
28+
#include "icsp_inhibit.h"
29+
#include "terminal.h"
30+
31+
#include <stdio.h>
32+
#include <string.h>
33+
#include <stdbool.h>
3134

3235
#define USER_INPUT_BUFFER_SIZE 50
3336
#define UNLOCK_COMMAND "LOCKDEVICE"
3437
#define MATCHES 0
3538
#define ENTER '\r'
3639

3740
// Function prototypes
38-
static void ClearTerminalScreen(void);
39-
static void ClearTerminalLine(void);
40-
static void MoveCursor(int row);
41-
static void EnableCursor(bool enable);
4241
static void PrintWarning(void);
4342
static uint32_t GetResetAddress();
4443
static bool WasLoadedByBootloader();
4544
static void PrintBootloaderRequired(void);
4645
static char* ScanINPUT(void);
4746
static void DisableProgrammingPort(void);
48-
static void InvalidKeyword(void);
47+
static void PrintErrorMessage(char* error);
4948
static void PrintProgrammingDisabled(void);
5049

5150
// Local Variables
5251
static char userInput[USER_INPUT_BUFFER_SIZE] = {0};
53-
static bool ICSP_INHIBIT_IsEnabled(void); //temp stub - replace with real version
54-
static bool ICSP_INHIBIT_Enable(); //temp stub - replace with real version
5552
static bool errorPresent = false;
56-
static bool icspEnabled = false;
57-
5853

5954
int main(void)
6055
{
56+
const char* keyword = "LOCKDEVICE";
57+
6158
SYSTEM_Initialize();
62-
59+
6360
// if(WasLoadedByBootloader() == false)
6461
// {
6562
// PrintBootloaderRequired();
@@ -68,44 +65,44 @@ int main(void)
6865
// {
6966
// }
7067
// }
71-
// else
68+
69+
if(ICSP_INHIBIT_IsEnabled())
7270
{
73-
const char* keyword = "LOCKDEVICE";
74-
75-
PrintWarning();
71+
PrintProgrammingDisabled();
7672

77-
while (1)
73+
while(1)
7874
{
79-
char* userInput;
80-
81-
if(ICSP_INHIBIT_IsEnabled())
82-
{
83-
PrintProgrammingDisabled();
84-
85-
while(1)
86-
{
87-
}
88-
}
89-
90-
userInput = ScanINPUT();
75+
}
76+
}
9177

92-
if(strcmp(userInput, keyword) == MATCHES)
93-
{
94-
DisableProgrammingPort();
95-
}
96-
else
78+
PrintWarning();
79+
80+
while (1)
81+
{
82+
char* userInput = ScanINPUT();
83+
84+
if(strcmp(userInput, keyword) == MATCHES)
85+
{
86+
DisableProgrammingPort();
87+
88+
PrintProgrammingDisabled();
89+
90+
while(1)
9791
{
98-
InvalidKeyword();
9992
}
10093
}
94+
else
95+
{
96+
PrintErrorMessage("Invalid keyword entered. Try again.");
97+
}
10198
}
10299
}
103100

104101
static void PrintProgrammingDisabled(void)
105102
{
106-
EnableCursor(false);
107-
MoveCursor(1);
108-
ClearTerminalScreen();
103+
TERMINAL_EnableCursor(false);
104+
TERMINAL_MoveCursor(1);
105+
TERMINAL_ClearScreen();
109106

110107
printf("\r\n");
111108
printf("\r\n");
@@ -115,53 +112,56 @@ static void PrintProgrammingDisabled(void)
115112
printf("Use the bootloader to load all future applications into this board.");
116113
}
117114

118-
//temp stub - replace with real version
119-
static bool ICSP_INHIBIT_IsEnabled(void)
115+
static void DisableProgrammingPort(void)
120116
{
121-
return icspEnabled;
117+
ICSP_INHIBIT_Enable();
122118
}
123119

124-
static bool ICSP_INHIBIT_Enable()
120+
static void ClearErrorMessage(void)
125121
{
126-
icspEnabled = true;
122+
TERMINAL_MoveCursor(7);
123+
TERMINAL_ClearLine();
124+
125+
errorPresent = false;
127126
}
128127

129-
static void InvalidKeyword(void)
128+
static void ResetPrompt(void)
130129
{
131-
MoveCursor(7);
132-
ClearTerminalLine();
133-
printf("Invalid keyword entered. Try again.");
134-
errorPresent = true;
135-
136-
MoveCursor(5);
137-
ClearTerminalLine();
130+
TERMINAL_MoveCursor(5);
131+
TERMINAL_ClearLine();
138132
printf(">> ");
133+
}
134+
135+
static void PrintErrorMessage(char* errorMessage)
136+
{
137+
ClearErrorMessage();
139138

139+
printf("%s", errorMessage);
140+
errorPresent = true;
141+
142+
ResetPrompt();
140143
}
141144

142-
static void DisableProgrammingPort(void)
145+
static void ClearUserInput(void)
143146
{
144-
ICSP_INHIBIT_Enable();
147+
memset(userInput, 0, sizeof(userInput));
145148
}
146149

147150
static char* ScanINPUT(void)
148151
{
149152
uint8_t offset = 0;
150153
char key;
151154

152-
memset(userInput, 0, sizeof(userInput));
155+
ClearUserInput();
153156

154157
do
155158
{
156159
key = UART1_Read();
157160

158161
if(errorPresent)
159162
{
160-
MoveCursor(7);
161-
ClearTerminalLine();
162-
MoveCursor(5);
163-
printf(">> ");
164-
errorPresent = false;
163+
ClearErrorMessage();
164+
ResetPrompt();
165165
}
166166

167167
if(key != ENTER)
@@ -175,51 +175,26 @@ static char* ScanINPUT(void)
175175
return userInput;
176176
}
177177

178-
static void ClearTerminalScreen(void)
179-
{
180-
printf("\033[2J");
181-
}
182-
183-
static void ClearTerminalLine(void)
184-
{
185-
printf("\33[2K\r");
186-
}
187-
188-
static void MoveCursor(int row)
189-
{
190-
printf("\033[%d;0f", row);
191-
}
192-
193-
194-
/*
195-
* "\033[?25h" for cursor enable
196-
* "\033[?25l" for cursor disable
197-
*/
198-
static void EnableCursor(bool enable)
199-
{
200-
printf("\033[?25%c", enable ? 'h' : 'l');
201-
}
202-
203178
static void PrintWarning(void)
204179
{
205-
EnableCursor(false);
206-
MoveCursor(1);
207-
ClearTerminalScreen();
180+
TERMINAL_EnableCursor(false);
181+
TERMINAL_MoveCursor(1);
182+
TERMINAL_ClearScreen();
208183

209184
printf("Type LOCKDEVICE (plus ENTER) to enable the ICSP Inhibit feature.\r\n");
210185
printf("\r\n");
211186
printf("WARNING: THIS PERMANENTLY DISABLES DIRECT PROGRAMMING OF THE BOARD.\r\n");
212187
printf("\r\n");
213188
printf(">> ");
214189

215-
EnableCursor(true);
190+
TERMINAL_EnableCursor(true);
216191
}
217192

218193
static void PrintBootloaderRequired(void)
219194
{
220-
EnableCursor(false);
221-
MoveCursor(1);
222-
ClearTerminalScreen();
195+
TERMINAL_EnableCursor(false);
196+
TERMINAL_MoveCursor(1);
197+
TERMINAL_ClearScreen();
223198

224199
printf("NO BOOTLOADER DETECTED!\r\n");
225200
printf("\r\n");

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"/>

secure_boot_and_secure_firmware_upgrade_over_canfd/icsp_inhibit.X/nbproject/project.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
<make-project-type>0</make-project-type>
99
<sourceEncoding>ISO-8859-1</sourceEncoding>
1010
<make-dep-projects/>
11-
<sourceRootList/>
11+
<sourceRootList>
12+
<sourceRootElem>.</sourceRootElem>
13+
</sourceRootList>
1214
<confList>
1315
<confElem>
1416
<name>default</name>

0 commit comments

Comments
 (0)