-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart.c
446 lines (412 loc) · 12 KB
/
uart.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
* Copyright (C) 2013 Wiktor Grajkowski <wiktor.grajkowski AT gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/******************************************************************************
* MSP430 command line
*
* This program lets you control your MSP430 in an easy way through terminal
* like commands. Syntax: command param1 param2 ... You can specify the
* commands, number and type of parameters, display simple help with commands'
* descriptions. It can handle string, decimal (signed long),
* hex(only positive signed long) or binary (8 bit) parameters.
*
* For a tutorial and demo visit http://flemingsociety.wordpress.com
*
*
* Wiktor Grajkowski
* wiktor.grajkowski AT gmail.com
* http://flemingsociety.wordpress.com
******************************************************************************/
#include <msp430.h>
#include <legacymsp430.h>
#include "uart.h"
#include "spi.h"
// Receive Data (RXD) at P1.1
#define RXD BIT1
// Transmit Data (TXD) at P1.2
#define TXD BIT2
// commands
char* command_list[NO_OF_COMMANDS] = {"help", "set", "read", "restart"};
// types: n = no param, s - string parameter, d - digits
char* command_list_type[NO_OF_COMMANDS] = {"n", "sd", "s", "n"};
// descriptions of comands for help command
char* command_list_desc[NO_OF_COMMANDS] = {"______list available commannds", "______set [register] [value] via spi", "_____read [register]", "__restart the program"};
// strings to be replaced
char* param_strings[NO_OF_PARAM_STRINGS] = {"val1", "val2", "val3", "register"};
// values to replace strings (can't be '-1' as its reserved for invalid value detection)
char param_strings_replace_vals[NO_OF_PARAM_STRINGS] = {1, 2, 3, 100};
char data[MAX_COMMAND_LENGTH]; // receive buffer
char command[MAX_COMMAND_LENGTH]; // command entered
char payload[MAX_PARAMS][MAX_PARAM_LEN]; // payload parameters
char* pls[MAX_PARAMS]; // pointer to payload parameters (maybe not needed?)
long vals[MAX_PARAMS]; // array for storing processed payload values
void uart_init(void)
{
P1SEL |= RXD + TXD;
P1SEL2 |= RXD + TXD;
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 0x45; // 16MHz 112500 baud
UCA0BR1 = 0; // 16MHz 112500 baud
UCA0MCTL = UCBRS_4 + UCBRF_0;
UCA0CTL1 &= ~UCSWRST; // Initialize USCI state machine
IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt
}
// put char
void uart_putc(unsigned char c)
{
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = c; // TX
}
// put string
void uart_puts(const char *str)
{
while(*str) uart_putc(*str++);
}
// print integer
void uart_printi(long n)
{
int divided; // digit to be printed
long division = 1000000000; // 1000000000 as highest integer can be 2^(16) = 65536
char reached = 0; // used as a flag not to print leading zeros
// if n = 0 don't bother with calculating stuff
if(n == 0)
{
uart_putc('0');
return;
}
// if negative put minus sign and make positive
else if(n<0)
{
uart_putc('-');
n = -n;
}
while(division != 0)
{
divided = n/division; // digit to be printed is the integer part left after division
if(divided != 0) // detect first non zero digit
reached = 1; // set flag to print digits from now on
if(reached)
uart_putc(divided+'0'); // print the digit (see ascii table to know why +'0')
n %= division; // set new value for n (reminder from division)
division /= 10; // set new division value
}
}
// process payload depending on the type of params and store processed values in vals[] array
void command_type_check (char command)
{
char* r = command_list_type[command];
int cnt=0; // counter for parameters, '0' being the first parameter
int i;
for (i=0; i<MAX_PARAMS; i++)
vals[i] = -1; // set all values to invalid (-1)
while (*r) // for all the parameters
{
char* p = pls[cnt]; // temporary pointer to payload parameter string
switch(*r++) // check parameter type
{
case 'n': // just execute if no parameters
{
break;
}
case 's':
{
//uart_puts((char*)"case s: \n\r"); // debug
int i;
for(i=0; i<NO_OF_PARAM_STRINGS; i++)
{
char* s = param_strings[i];
char* p_temp = p;
while(*s)
{
if(*p_temp++ != *s++)
break;
else if (*s == '\0')
{
vals[cnt] = param_strings_replace_vals[i]; //
i = NO_OF_PARAM_STRINGS; // end searching
}
}
}
break;
}
//TODO add enetering too large/small number prevention
case 'd':
{
static signed char hextable[] = {
[0 ... 255] = -1, // bit aligned access into this table is considerably
['0'] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, // faster for most modern processors,
['A'] = 10, 11, 12, 13, 14, 15, // for the space conscious, reduce to
['a'] = 10, 11, 12, 13, 14, 15 // signed char.
};
//uart_puts((char*)"case d: \n\r"); // debug
long number = 0;
char valid = 1;
// hex
if(*p++ == '0' && *p-- == 'x')
{
//uart_puts((char*)"hex: \n\r"); // debug
p+=2;
unsigned long number_ = 0;
char temp_hex[8];
char* hex = temp_hex;
char i=0;
while(*p)
temp_hex[i++] = *p++;
while (i--)
{
number_ = (number_ << 4) | hextable[*hex++];
//uart_printi(number_); // debug
//uart_puts((char*)"\n\r"); // debug
}
if(number_ > ((MAX_NUM_AS_PARAM << 1) - 1)) // this is max signed int value
valid = 0;
else
number = number_;
}
// bin
else if(*p++ == '0' && *p-- == 'b')
{
//uart_puts((char*)"bin: \n\r"); // debug
char temp_bin[8];
p+=2;
char i=0;
while(*p)
{
uart_putc(*p); // debug
if(*p != '0' && *p!= '1')
{
//uart_putc('z'); // debug
valid = 0;
break;
}
else
temp_bin[i++] = *p++ - '0';
}
while (i--)
{
number = (number << 1) | temp_bin[7-i];
uart_printi(number); // debug
uart_puts((char*)"\n\r"); // debug
}
}
// dec
else
{
//uart_puts((char*)"dec: \n\r"); // debug
p-=2;
char positive = 1;
char temp_digits[10];
char i=0;
long scaler = 1;
if(*p == '-')
{
positive = 0;
p++;
}
while(*p)
{
temp_digits[i] = *p++ - '0';
if(temp_digits[i] < 0 || temp_digits[i++] > 9)
valid = 0;
}
while(i>0)
{
number += temp_digits[i-1]*scaler;
scaler *= 10;
i--;
}
if(!positive)
number = -number;
}
if(valid)
vals[cnt] = number;
else
{
uart_puts((char*)"\n\rinvalid number in parameter: ");
uart_printi(cnt);
}
break;
}
}
cnt++;
}
/*
uart_puts((char*)"\n\rvals[0] = "); // debug
uart_printi(vals[0]); // debug
uart_puts((char*)"\n\rvals[1] = "); // debug
uart_printi(vals[1]); // debug
uart_puts((char*)"\n\r"); // debug
*/
command_execution(command);
}
// TODO add payload overflow prevention
void payload_split(char* p)
{
char i = 0;
char cnt = 0;
while(*p)
{
//uart_putc(*p); // debug
if(*p == ' ' )
{
payload[cnt++][i] = '\0';
i = 0;
}
else if(*p == '\r')
{
payload[cnt++][i++] = '\0';
while(cnt < MAX_PARAMS)
payload[cnt++][0] = '\0';
}
else
payload[cnt][i++] = *p;
p++;
}
for(i=0; i<MAX_PARAMS; i++)
pls[i] = payload[i];
/*
uart_puts((char *)"\n\rcommand payload[0]: "); // debug
uart_puts(payload[0]); // debug
uart_puts((char *)"\n\rcommand payload[1]: "); // debug
uart_puts(payload[1]); // debug
uart_puts((char *)"\n\r"); // debug
*/
}
void command_check(char command_[])
{
char i;
for (i = 0; i < NO_OF_COMMANDS; i++)
{
char* p = command_;
char* r = command_list[i];
while(*r)
{
// debug info
/*
uart_putc('\n'); // debug
uart_putc(*r); // debug
uart_putc(' '); // debug
uart_putc(*p); // debug
uart_putc('\n'); // debug
uart_putc('\r'); // debug
*/
if(*p++ != *r++)
break;
else if (*r == '\0')
{
//uart_puts((char *)"valid command entered \n\r"); // debug
payload_split(++p);
command_type_check(i);
return;
}
}
}
command_execution(-1);
}
void command_execution (char command_)
{
switch(command_)
{
case 0:
{
uart_puts((char *)"Available commands: \n\r");
int i;
for (i=0; i<NO_OF_COMMANDS; i++)
{
uart_puts(command_list[i]);
uart_puts(command_list_desc[i]);
uart_puts((char *)"\r\n");
}
uart_puts((char *)"\n");
break;
}
case 1:
{
uart_puts((char *)"set command executed\n\r");
break;
}
case 2:
{
uart_puts((char *)"read command executed\n\r");
break;
}
case 3:
{
WDTCTL = 0xffff; // Set Watchdog Timer interval to ~32ms
break;
}
default :
{
uart_puts((char *)"invalid command. type 'help' for the list of available commands. \n\r");
break;
}
}
int i;
int ii;
for(i=0; i<MAX_PARAMS; i++)
for(ii=0; ii<MAX_PARAM_LEN; ii++)
payload[i][ii] = '\0';
uart_puts((char *)"payloads cleared. \n\r");
}
//receive interrupt
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
static char i = 0;
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
data[i] = UCA0RXBUF; // put incoming char to a buffer
uart_putc(data[i++]); // print incoming char
// check if received command
switch(data[i-1])
{
// enter
case '\r':
{
uart_putc('\n');
i-=2;
command[i+1] = '\0';
while(i+1)
{
command[i] = data[i--];
}
i = 0;
//uart_puts((char *)"string entered: "); // debug
//uart_puts(command); // debug
//uart_puts((char *)"\n\r"); // debug
command_check(command);
//uart_puts("command entered: ");
//uart_putc(command_check(command)+'0');
//uart_puts("\n\r");
break;
}
// backspace
case 0x7F:
{
if(i > 1)
i-=2;
break;
}
default:
{
//data buffer overflow prevention
if (i-1 == MAX_COMMAND_LENGTH-1)
{
uart_putc(0x7F);
i--;
}
}
}
}