| 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 |
#include "stdarg.h" |
| 44 |
#include "stdlib.h" |
| 45 |
#include "stdio.h" |
| 46 |
#include "console.h" |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
char uart_init(unsigned long baudRate) |
| 62 |
{ |
| 63 |
return 0; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
void uart_write_char(char data) |
| 74 |
{ |
| 75 |
putchar(data); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
void uart_read_char(char * data) |
| 86 |
{ |
| 87 |
*data = getchar(); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
void uart_write_string(const char* string) |
| 98 |
{ |
| 99 |
while(*string) |
| 100 |
{ |
| 101 |
uart_write_char(*string++); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
char *int_to_str(long number, char base) |
| 115 |
{ |
| 116 |
unsigned long pos_number = 0; |
| 117 |
char neg_sign = 0; |
| 118 |
const char digits[17] = "0123456789ABCDEF"; |
| 119 |
static char buffer[17] = " "; |
| 120 |
char* buffer_ptr = &buffer[16]; |
| 121 |
|
| 122 |
if((number < 0) && (base == 10)) |
| 123 |
{ |
| 124 |
neg_sign = 1; |
| 125 |
pos_number = -1 * number; |
| 126 |
} |
| 127 |
else |
| 128 |
{ |
| 129 |
pos_number = (unsigned long)number; |
| 130 |
} |
| 131 |
do |
| 132 |
{ |
| 133 |
*buffer_ptr-- = digits[pos_number % base]; |
| 134 |
pos_number /= base; |
| 135 |
} |
| 136 |
while(pos_number != 0); |
| 137 |
if(neg_sign) |
| 138 |
{ |
| 139 |
*buffer_ptr-- = '-'; |
| 140 |
} |
| 141 |
*buffer_ptr++; |
| 142 |
|
| 143 |
return buffer_ptr; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
void console_print(char* str, ...) |
| 154 |
{ |
| 155 |
char* string_ptr; |
| 156 |
char first_param = 0; |
| 157 |
char second_param = 0; |
| 158 |
unsigned long x_mask = 0; |
| 159 |
unsigned long d_mask = 0; |
| 160 |
char ch_number = 0; |
| 161 |
unsigned long multiplier = 1; |
| 162 |
char* str_arg; |
| 163 |
long long_arg; |
| 164 |
double double_arg; |
| 165 |
va_list argp; |
| 166 |
|
| 167 |
va_start(argp, str); |
| 168 |
for(string_ptr = str; *string_ptr != '\0'; string_ptr++) |
| 169 |
{ |
| 170 |
if(*string_ptr!='%') |
| 171 |
{ |
| 172 |
uart_write_char(*string_ptr); |
| 173 |
continue; |
| 174 |
} |
| 175 |
string_ptr++; |
| 176 |
first_param = 0; |
| 177 |
while((*string_ptr >= 0x30) & (*string_ptr <= 0x39)) |
| 178 |
{ |
| 179 |
first_param *= 10; |
| 180 |
first_param += (*string_ptr - 0x30); |
| 181 |
string_ptr++; |
| 182 |
} |
| 183 |
if(*string_ptr == '.') |
| 184 |
{ |
| 185 |
string_ptr++; |
| 186 |
second_param = 0; |
| 187 |
while((*string_ptr >= 0x30) & (*string_ptr <= 0x39)) |
| 188 |
{ |
| 189 |
second_param *= 10; |
| 190 |
second_param += (*string_ptr - 0x30); |
| 191 |
string_ptr++; |
| 192 |
} |
| 193 |
} |
| 194 |
switch(*string_ptr) |
| 195 |
{ |
| 196 |
case 'c': |
| 197 |
long_arg = va_arg(argp, long); |
| 198 |
uart_write_char((char)long_arg); |
| 199 |
break; |
| 200 |
case 's': |
| 201 |
str_arg = va_arg(argp, char*); |
| 202 |
uart_write_string(str_arg); |
| 203 |
break; |
| 204 |
case 'd': |
| 205 |
long_arg = va_arg(argp, long); |
| 206 |
uart_write_string(int_to_str(long_arg, 10)); |
| 207 |
break; |
| 208 |
case 'x': |
| 209 |
long_arg = va_arg(argp, long); |
| 210 |
x_mask = 268435456; |
| 211 |
ch_number = 8; |
| 212 |
while(x_mask > long_arg) |
| 213 |
{ |
| 214 |
x_mask /= 16; |
| 215 |
ch_number--; |
| 216 |
} |
| 217 |
while(ch_number < first_param) |
| 218 |
{ |
| 219 |
uart_write_char('0'); |
| 220 |
ch_number++; |
| 221 |
} |
| 222 |
if(long_arg != 0) |
| 223 |
{ |
| 224 |
uart_write_string(int_to_str(long_arg, 16)); |
| 225 |
} |
| 226 |
break; |
| 227 |
case 'f': |
| 228 |
double_arg = va_arg(argp, double); |
| 229 |
if(second_param == 0) |
| 230 |
{ |
| 231 |
second_param = 3; |
| 232 |
} |
| 233 |
ch_number = second_param; |
| 234 |
while(ch_number > 0) |
| 235 |
{ |
| 236 |
multiplier *= 10; |
| 237 |
ch_number--; |
| 238 |
} |
| 239 |
double_arg *= multiplier; |
| 240 |
if(double_arg < 0) |
| 241 |
{ |
| 242 |
double_arg *= -1; |
| 243 |
uart_write_char('-'); |
| 244 |
} |
| 245 |
long_arg = (long)double_arg; |
| 246 |
uart_write_string(int_to_str((long_arg / multiplier), 10)); |
| 247 |
uart_write_char('.'); |
| 248 |
d_mask = 1000000000; |
| 249 |
ch_number = 10; |
| 250 |
while(d_mask > (long)(long_arg % multiplier)) |
| 251 |
{ |
| 252 |
d_mask /= 10; |
| 253 |
ch_number--; |
| 254 |
} |
| 255 |
while(ch_number < second_param) |
| 256 |
{ |
| 257 |
uart_write_char('0'); |
| 258 |
ch_number++; |
| 259 |
} |
| 260 |
if((long_arg % multiplier) != 0) |
| 261 |
{ |
| 262 |
uart_write_string(int_to_str((long_arg % multiplier), 10)); |
| 263 |
} |
| 264 |
break; |
| 265 |
} |
| 266 |
} |
| 267 |
va_end(argp); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
char console_init(unsigned long baud_rate) |
| 281 |
{ |
| 282 |
return uart_init(baud_rate); |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
void console_get_command(char* command) |
| 293 |
{ |
| 294 |
char received_char = 0; |
| 295 |
unsigned char char_number = 0; |
| 296 |
|
| 297 |
while((received_char != '\n') && (received_char != '\r')) |
| 298 |
{ |
| 299 |
uart_read_char(&received_char); |
| 300 |
command[char_number++] = received_char; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
int console_check_commands(char* received_cmd, |
| 319 |
const char* expected_cmd, |
| 320 |
double* param, |
| 321 |
char* param_no) |
| 322 |
{ |
| 323 |
int cmd_type = 1; |
| 324 |
unsigned char char_index = 0; |
| 325 |
char param_string[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 326 |
unsigned char param_index = 0; |
| 327 |
unsigned char index = 0; |
| 328 |
const char digits[17] = "0123456789ABCDEF"; |
| 329 |
unsigned char digit_index = 0; |
| 330 |
|
| 331 |
while((expected_cmd[char_index] != '!') && |
| 332 |
(expected_cmd[char_index] != '?') && |
| 333 |
(expected_cmd[char_index] != '=') && |
| 334 |
(cmd_type != UNKNOWN_CMD)) |
| 335 |
{ |
| 336 |
if(expected_cmd[char_index] != received_cmd[char_index]) |
| 337 |
{ |
| 338 |
cmd_type = UNKNOWN_CMD; |
| 339 |
} |
| 340 |
char_index++; |
| 341 |
} |
| 342 |
if(cmd_type != UNKNOWN_CMD) |
| 343 |
{ |
| 344 |
if(expected_cmd[char_index] == '!') |
| 345 |
{ |
| 346 |
if(received_cmd[char_index] == '!') |
| 347 |
{ |
| 348 |
cmd_type = DO_CMD; |
| 349 |
} |
| 350 |
else |
| 351 |
{ |
| 352 |
cmd_type = UNKNOWN_CMD; |
| 353 |
} |
| 354 |
} |
| 355 |
if(expected_cmd[char_index] == '?') |
| 356 |
{ |
| 357 |
if(received_cmd[char_index] == '?') |
| 358 |
{ |
| 359 |
cmd_type = READ_CMD; |
| 360 |
} |
| 361 |
else |
| 362 |
{ |
| 363 |
cmd_type = UNKNOWN_CMD; |
| 364 |
} |
| 365 |
} |
| 366 |
if(expected_cmd[char_index] == '=') |
| 367 |
{ |
| 368 |
if(received_cmd[char_index] == '=') |
| 369 |
{ |
| 370 |
cmd_type = WRITE_CMD; |
| 371 |
} |
| 372 |
else |
| 373 |
{ |
| 374 |
cmd_type = UNKNOWN_CMD; |
| 375 |
} |
| 376 |
} |
| 377 |
if((cmd_type == WRITE_CMD) || (cmd_type == READ_CMD)) |
| 378 |
{ |
| 379 |
char_index++; |
| 380 |
while((received_cmd[char_index] != '\n') && |
| 381 |
(received_cmd[char_index] != '\r')) |
| 382 |
{ |
| 383 |
if((received_cmd[char_index] == 0x20)) |
| 384 |
{ |
| 385 |
*param = 0; |
| 386 |
if((param_string[0] == '0') && (param_string[1] == 'x')) |
| 387 |
{ |
| 388 |
for(index = 2; index < param_index; index++) |
| 389 |
{ |
| 390 |
for(digit_index = 0; digit_index < 16; digit_index++) |
| 391 |
{ |
| 392 |
if(param_string[index] == digits[digit_index]) |
| 393 |
{ |
| 394 |
*param = *param * 16; |
| 395 |
*param = *param + digit_index; |
| 396 |
} |
| 397 |
} |
| 398 |
} |
| 399 |
} |
| 400 |
else |
| 401 |
{ |
| 402 |
if(param_string[0] == '-') |
| 403 |
{ |
| 404 |
*param = atof((const char*)(¶m_string[1])); |
| 405 |
*param *= (-1); |
| 406 |
} |
| 407 |
else |
| 408 |
{ |
| 409 |
*param = atof((const char*)param_string); |
| 410 |
} |
| 411 |
} |
| 412 |
param++; |
| 413 |
*param_no += 1; |
| 414 |
for(param_index = 0; param_index < 10; param_index++) |
| 415 |
{ |
| 416 |
param_string[param_index] = 0; |
| 417 |
} |
| 418 |
param_index = 0; |
| 419 |
char_index++; |
| 420 |
} |
| 421 |
else |
| 422 |
{ |
| 423 |
param_string[param_index] = received_cmd[char_index]; |
| 424 |
char_index++; |
| 425 |
param_index++; |
| 426 |
} |
| 427 |
} |
| 428 |
if(param_index) |
| 429 |
{ |
| 430 |
*param = 0; |
| 431 |
if((param_string[0] == '0') && (param_string[1] == 'x')) |
| 432 |
{ |
| 433 |
for(index = 2; index < param_index; index++) |
| 434 |
{ |
| 435 |
for(digit_index = 0; digit_index < 16; digit_index++) |
| 436 |
{ |
| 437 |
if(param_string[index] == digits[digit_index]) |
| 438 |
{ |
| 439 |
*param *= 16; |
| 440 |
*param += digit_index; |
| 441 |
} |
| 442 |
} |
| 443 |
} |
| 444 |
} |
| 445 |
else |
| 446 |
{ |
| 447 |
if(param_string[0] == '-') |
| 448 |
{ |
| 449 |
*param = atof((const char*)(¶m_string[1])); |
| 450 |
*param *= (-1); |
| 451 |
} |
| 452 |
else |
| 453 |
{ |
| 454 |
*param = atof((const char*)param_string); |
| 455 |
} |
| 456 |
} |
| 457 |
*param_no += 1; |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
return cmd_type; |
| 463 |
} |