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 );
4241static void PrintWarning (void );
4342static uint32_t GetResetAddress ();
4443static bool WasLoadedByBootloader ();
4544static void PrintBootloaderRequired (void );
4645static char * ScanINPUT (void );
4746static void DisableProgrammingPort (void );
48- static void InvalidKeyword ( void );
47+ static void PrintErrorMessage ( char * error );
4948static void PrintProgrammingDisabled (void );
5049
5150// Local Variables
5251static 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
5552static bool errorPresent = false;
56- static bool icspEnabled = false;
57-
5853
5954int 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
104101static 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
147150static 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-
203178static 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
218193static 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" );
0 commit comments