|
29 | 29 | #include "mcc_generated_files/flash/flash_types.h" |
30 | 30 | #include "mcc_generated_files/boot/boot_config.h" |
31 | 31 |
|
32 | | -#define WINDOW_SIZE 10 |
| 32 | +#define USER_INPUT_BUFFER_SIZE 50 |
33 | 33 | #define UNLOCK_COMMAND "LOCKDEVICE" |
| 34 | +#define MATCHES 0 |
| 35 | +#define ENTER '\r' |
34 | 36 |
|
35 | 37 | // Function prototypes |
36 | 38 | static void ClearTerminalScreen(void); |
37 | 39 | static void ClearTerminalLine(void); |
38 | 40 | static void MoveCursor(int row); |
39 | 41 | static void HideCursor(void); |
40 | 42 | static void PrintWarning(void); |
41 | | -static void AppendCharToWindow(char receivedChar, char *window, int *windowIndex); |
42 | | -static void ProcessReceivedChar(char receivedChar, char *window, int *windowIndex); |
43 | | -static void ResetWindowOnMismatch(char *window, int *windowIndex); |
44 | | -static void CheckForUnlockCommand(char *window, int *windowIndex); |
45 | 43 | static uint32_t GetResetAddress(); |
46 | 44 | static bool WasLoadedByBootloader(); |
47 | 45 | static void PrintBootloaderRequired(void); |
| 46 | +static char* ScanInput(void); |
| 47 | +static void DisableProgrammingPort(void); |
| 48 | +static void InvalidKeyword(void); |
| 49 | +static char userInput[USER_INPUT_BUFFER_SIZE] = {0}; |
| 50 | +static bool ICSP_INHIBIT_IsEnabled(void); //temp stub - replace with real version |
48 | 51 |
|
49 | 52 | int main(void) |
50 | 53 | { |
51 | | - char window[WINDOW_SIZE + 1] = {0}; |
52 | | - int windowIndex = 0; |
53 | | - |
54 | 54 | SYSTEM_Initialize(); |
55 | 55 | HideCursor(); |
56 | 56 | ClearTerminalScreen(); |
57 | 57 |
|
58 | | - if(WasLoadedByBootloader() == false) |
| 58 | +// if(WasLoadedByBootloader() == false) |
| 59 | +// { |
| 60 | +// PrintBootloaderRequired(); |
| 61 | +// |
| 62 | +// while(1) |
| 63 | +// { |
| 64 | +// } |
| 65 | +// } |
| 66 | +// else |
59 | 67 | { |
60 | | - PrintBootloaderRequired(); |
| 68 | + const char* keyword = "LOCKDEVICE"; |
61 | 69 |
|
62 | | - while(1) |
63 | | - { |
64 | | - } |
65 | | - } |
66 | | - else |
67 | | - { |
68 | 70 | PrintWarning(); |
69 | 71 |
|
70 | 72 | while (1) |
71 | 73 | { |
72 | | - if (UART1_IsRxReady()) |
| 74 | + char* userInput; |
| 75 | + |
| 76 | + if(ICSP_INHIBIT_IsEnabled()) |
| 77 | + { |
| 78 | + printf("ICSP Programming/Debugging permanently disabled."); |
| 79 | + |
| 80 | + while(1) |
| 81 | + { |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + userInput = ScanInput(); |
| 86 | + |
| 87 | + if(strcmp(userInput, keyword) == MATCHES) |
73 | 88 | { |
74 | | - char receivedChar = UART1_Read(); |
75 | | - ProcessReceivedChar(receivedChar, window, &windowIndex); |
76 | | - CheckForUnlockCommand(window, &windowIndex); |
| 89 | + DisableProgrammingPort(); |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + InvalidKeyword(); |
77 | 94 | } |
78 | 95 | } |
79 | 96 | } |
80 | 97 | } |
81 | 98 |
|
| 99 | +//temp stub - replace with real version |
| 100 | +static bool ICSP_INHIBIT_IsEnabled(void) |
| 101 | +{ |
| 102 | + return false; |
| 103 | +} |
| 104 | + |
| 105 | +static void InvalidKeyword(void) |
| 106 | +{ |
| 107 | + MoveCursor(10); |
| 108 | + ClearTerminalLine(); |
| 109 | + MoveCursor(5); |
| 110 | + ClearTerminalLine(); |
| 111 | + printf("Invalid character entered. Try again."); |
| 112 | +} |
| 113 | + |
| 114 | +static void DisableProgrammingPort(void) |
| 115 | +{ |
| 116 | + //ICSP_INHIBIT_Enable(true); |
| 117 | +} |
| 118 | + |
| 119 | +static char* ScanInput(void) |
| 120 | +{ |
| 121 | + uint8_t offset = 0; |
| 122 | + char key; |
| 123 | + |
| 124 | + memset(userInput, 0, sizeof(userInput)); |
| 125 | + |
| 126 | + do |
| 127 | + { |
| 128 | + key = UART1_Read(); |
| 129 | + |
| 130 | + if(key != ENTER) |
| 131 | + { |
| 132 | + userInput[offset++] = key; |
| 133 | + printf("%c", key); |
| 134 | + } |
| 135 | + } |
| 136 | + while((key != ENTER) && (offset < sizeof(userInput))); |
| 137 | + |
| 138 | + return userInput; |
| 139 | +} |
| 140 | + |
82 | 141 | static void ClearTerminalScreen(void) |
83 | 142 | { |
84 | 143 | printf("\033[2J"); |
@@ -113,53 +172,6 @@ static void PrintBootloaderRequired(void) |
113 | 172 | printf("Because programming will be permanently disabled, \r\na bootloader is required to run this demo. \r\nPlease see the readme.md for more information."); |
114 | 173 | } |
115 | 174 |
|
116 | | -static void ProcessReceivedChar(char receivedChar, char *window, int *windowIndex) |
117 | | -{ |
118 | | - bool isCharValid = (*windowIndex < strlen(UNLOCK_COMMAND)) && (receivedChar == UNLOCK_COMMAND[*windowIndex]); |
119 | | - |
120 | | - if (isCharValid) |
121 | | - { |
122 | | - AppendCharToWindow(receivedChar, window, windowIndex); |
123 | | - MoveCursor(10); |
124 | | - printf("%s", window); |
125 | | - } |
126 | | - else |
127 | | - { |
128 | | - ResetWindowOnMismatch(window, windowIndex); |
129 | | - } |
130 | | -} |
131 | | - |
132 | | -static void AppendCharToWindow(char receivedChar, char *window, int *windowIndex) |
133 | | -{ |
134 | | - window[(*windowIndex)++] = receivedChar; |
135 | | - window[*windowIndex] = '\0'; |
136 | | -} |
137 | | - |
138 | | -static void ResetWindowOnMismatch(char *window, int *windowIndex) |
139 | | -{ |
140 | | - MoveCursor(10); |
141 | | - ClearTerminalLine(); |
142 | | - MoveCursor(5); |
143 | | - ClearTerminalLine(); |
144 | | - printf("Invalid character entered. Try again."); |
145 | | - *windowIndex = 0; |
146 | | - memset(window, 0, WINDOW_SIZE + 1); |
147 | | -} |
148 | | - |
149 | | -static void CheckForUnlockCommand(char *window, int *windowIndex) |
150 | | -{ |
151 | | - if (strncmp(window, UNLOCK_COMMAND, *windowIndex) == 0 && *windowIndex == strlen(UNLOCK_COMMAND)) |
152 | | - { |
153 | | - MoveCursor(5); |
154 | | - ClearTerminalLine(); |
155 | | - printf("ICSP Programming/Debugging permanently disabled. \n"); |
156 | | - MoveCursor(10); |
157 | | - ClearTerminalLine(); |
158 | | - *windowIndex = 0; |
159 | | - memset(window, 0, WINDOW_SIZE + 1); |
160 | | - } |
161 | | -} |
162 | | - |
163 | 175 | /* The following code finds the address used by GOTO instruction programmed |
164 | 176 | * at the reset vector in order to determine whether or not the application |
165 | 177 | * was downloaded directly or via the bootloader (i.e. if the address within the |
|
0 commit comments