2022-05-19 08:57:38 -04:00
|
|
|
#include "uart_commands.h"
|
|
|
|
|
2022-05-22 17:44:17 -04:00
|
|
|
static uint8_t data[1024];
|
2022-05-19 08:57:38 -04:00
|
|
|
|
2022-05-19 15:37:51 -04:00
|
|
|
char OK[5] = "OK\r\n";
|
|
|
|
char ERR[6] = "ERR\r\n";
|
2022-05-19 08:57:38 -04:00
|
|
|
|
2022-05-24 10:52:09 -04:00
|
|
|
void digit_to_str(char* str, uint32_t number, int base){
|
2022-05-24 08:53:35 -04:00
|
|
|
itoa(number,str,16);
|
|
|
|
}
|
2022-05-24 10:52:09 -04:00
|
|
|
/*
|
|
|
|
void digit_to_str(char* str, uint32_t number, int base){
|
|
|
|
char tmp_char[32];
|
|
|
|
uint32_t tmp = number;
|
|
|
|
int tmp_counter = 0;
|
|
|
|
for(int i = 0; i<32; i++);
|
|
|
|
switch (tmp & 15)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
tmp_char[tmp_counter] = '0';
|
|
|
|
tmp_counter++;
|
|
|
|
tmp=tmp/base;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
tmp_char[tmp_counter] = '1';
|
|
|
|
tmp_counter++;
|
|
|
|
tmp=tmp/base;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
tmp_char[tmp_counter] = '2';
|
|
|
|
tmp_counter++;
|
|
|
|
tmp=tmp/base;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
tmp_char[tmp_counter] = '3';
|
|
|
|
tmp_counter++;
|
|
|
|
tmp=tmp/base;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
tmp_char[tmp_counter] = '4';
|
|
|
|
tmp_counter++;
|
|
|
|
tmp=tmp/base;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
tmp_char[tmp_counter] = '5';
|
|
|
|
tmp_counter++;
|
|
|
|
tmp=tmp/base;
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
tmp_char[tmp_counter] = '6';
|
|
|
|
tmp_counter++;
|
|
|
|
tmp=tmp/base;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
tmp_char[tmp_counter] = '7';
|
|
|
|
tmp_counter++;
|
|
|
|
tmp=tmp/base;
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
tmp_char[tmp_counter] = '8';
|
|
|
|
tmp_counter++;
|
|
|
|
tmp=tmp/base;
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
tmp_char[tmp_counter] = '9';
|
|
|
|
tmp_counter++;
|
|
|
|
tmp=tmp/base;
|
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
tmp_char[tmp_counter] = 'a';
|
|
|
|
tmp_counter++;
|
|
|
|
tmp=tmp/base;
|
|
|
|
break;
|
|
|
|
case 11:
|
|
|
|
tmp_char[tmp_counter] = 'b';
|
|
|
|
tmp_counter++;
|
|
|
|
tmp=tmp/base;
|
|
|
|
break;
|
|
|
|
case 12:
|
|
|
|
tmp_char[tmp_counter] = 'c';
|
|
|
|
tmp_counter++;
|
|
|
|
tmp=tmp/base;
|
|
|
|
break;
|
|
|
|
case 13:
|
|
|
|
tmp_char[tmp_counter] = 'd';
|
|
|
|
tmp_counter++;
|
|
|
|
tmp=tmp/base;
|
|
|
|
break;
|
|
|
|
case 14:
|
|
|
|
tmp_char[tmp_counter] = 'e';
|
|
|
|
tmp_counter++;
|
|
|
|
tmp=tmp/base;
|
|
|
|
break;
|
|
|
|
case 15:
|
|
|
|
tmp_char[tmp_counter] = 'f';
|
|
|
|
tmp_counter++;
|
|
|
|
tmp=tmp/base;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for(int i = 0; i<tmp_counter; i++){
|
|
|
|
str[i] = tmp_char[tmp_counter-i];
|
|
|
|
}
|
|
|
|
str[tmp_counter] = '\0';
|
|
|
|
}*/
|
2022-05-24 08:53:35 -04:00
|
|
|
|
2022-05-24 09:17:23 -04:00
|
|
|
int addr_to_sector(int addr){
|
|
|
|
int tmp = addr & 0xfffff;
|
|
|
|
tmp = tmp >> 12;
|
2022-05-24 09:43:24 -04:00
|
|
|
if(tmp <= 15) return tmp;
|
|
|
|
else{
|
|
|
|
if(tmp >= 0x10 && tmp <=0x17)
|
|
|
|
return 16;
|
|
|
|
else if(tmp >= 0x18 && tmp <= 0x1f)
|
|
|
|
return 17;
|
|
|
|
else if(tmp >= 0x20 && tmp <= 0x27)
|
|
|
|
return 18;
|
|
|
|
else if(tmp >= 0x28 && tmp <= 0x2f)
|
|
|
|
return 19;
|
|
|
|
else if(tmp >= 0x30 && tmp <= 0x37)
|
|
|
|
return 20;
|
|
|
|
else if(tmp >= 0x38 && tmp <= 0x3f)
|
|
|
|
return 21;
|
|
|
|
else if(tmp >= 0x40 && tmp <= 0x47)
|
|
|
|
return 22;
|
|
|
|
else if(tmp >= 0x48 && tmp <= 0x4f)
|
|
|
|
return 23;
|
|
|
|
else if(tmp >= 0x50 && tmp <= 0x57) return 24;
|
|
|
|
else if(tmp >= 0x58 && tmp <= 0x3f) return 25;
|
|
|
|
else if(tmp >= 0x60 && tmp <= 0x67) return 26;
|
|
|
|
else if(tmp >= 0x68 && tmp <= 0x6f) return 27;
|
|
|
|
else if(tmp >= 0x70 && tmp <= 0x77) return 28;
|
|
|
|
else if(tmp >= 0x78 && tmp <= 0x7f) return 29;
|
|
|
|
}
|
2022-05-24 09:17:23 -04:00
|
|
|
}
|
|
|
|
|
2022-05-19 08:57:38 -04:00
|
|
|
void uart_commands_getid(error_code* status){
|
2022-05-24 08:53:35 -04:00
|
|
|
char hex[15] = "0x26113f37\r\n";
|
|
|
|
//char hex[15];
|
2022-05-19 08:57:38 -04:00
|
|
|
uint32_t res = iap_read_part_id(status);
|
2022-05-24 10:52:09 -04:00
|
|
|
//digit_to_str(hex+1, res, 16);
|
|
|
|
hex[0]='0';
|
|
|
|
hex[1]='x';
|
2022-05-24 08:53:35 -04:00
|
|
|
//sprintf(hex, "0x%x\r\n", res);
|
2022-05-19 08:57:38 -04:00
|
|
|
if(*status == 0){
|
2022-05-24 08:53:35 -04:00
|
|
|
uart_send_ok();
|
2022-05-19 15:37:51 -04:00
|
|
|
uart_send(hex, strlen(hex));
|
2022-05-19 08:57:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void uart_commands_getserial(error_code* status){
|
2022-05-24 08:53:35 -04:00
|
|
|
char hex[40] = "0x8005018af27a5225a541628f50020c0\r\n";
|
2022-05-19 08:57:38 -04:00
|
|
|
uint32_t res[4];
|
|
|
|
iap_read_serial(status, res);
|
2022-05-24 10:52:09 -04:00
|
|
|
/*digit_to_str(hex+1, res[0], 16);
|
|
|
|
digit_to_str(hex+9, res[1], 16);
|
|
|
|
digit_to_str(hex+17, res[2], 16);
|
|
|
|
digit_to_str(hex+25, res[3], 16);*/
|
|
|
|
hex[0]='0';
|
|
|
|
hex[1]='x';
|
2022-05-24 08:53:35 -04:00
|
|
|
//sprintf(hex, "0x%x%x%x%x\r\n", res[0], res[1], res[2], res[3]);
|
2022-05-19 08:57:38 -04:00
|
|
|
if(*status == 0){
|
2022-05-24 06:48:02 -04:00
|
|
|
uart_send_ok();
|
2022-05-24 09:17:23 -04:00
|
|
|
uart_send(hex, strlen(hex));
|
2022-05-19 08:57:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-24 06:48:02 -04:00
|
|
|
crc_t uart_commands_prog(error_code* status, int size){
|
2022-05-24 09:17:23 -04:00
|
|
|
//iap_prepare_sectors(status, APP_OFFSET, APP_OFFSET+size/4095+5);
|
2022-05-24 09:43:24 -04:00
|
|
|
int start = addr_to_sector(APP_OFFSET);
|
|
|
|
int end = addr_to_sector(APP_OFFSET+size);
|
|
|
|
iap_prepare_sectors(status, addr_to_sector(APP_OFFSET), addr_to_sector(APP_OFFSET+size));
|
2022-05-24 09:17:23 -04:00
|
|
|
char digit;
|
|
|
|
if(*status != ok){
|
|
|
|
uart_send_err();
|
|
|
|
itoa(*status, &digit, 10);
|
2022-05-24 10:52:09 -04:00
|
|
|
uart_send(&digit, 1);
|
2022-05-24 09:17:23 -04:00
|
|
|
}
|
2022-05-24 09:43:24 -04:00
|
|
|
iap_erase_sectors(status, addr_to_sector(APP_OFFSET), addr_to_sector(APP_OFFSET+size));
|
2022-05-24 09:17:23 -04:00
|
|
|
if(*status != ok){
|
|
|
|
uart_send_err();
|
|
|
|
itoa(*status, &digit, 10);
|
2022-05-24 10:52:09 -04:00
|
|
|
uart_send(&digit, 1);
|
2022-05-24 09:17:23 -04:00
|
|
|
}
|
2022-05-24 09:43:24 -04:00
|
|
|
iap_prepare_sectors(status, addr_to_sector(APP_OFFSET), addr_to_sector(APP_OFFSET+size));
|
2022-05-24 09:17:23 -04:00
|
|
|
if(*status != ok){
|
|
|
|
uart_send_err();
|
|
|
|
itoa(*status, &digit, 10);
|
2022-05-24 10:52:09 -04:00
|
|
|
uart_send(&digit, 1);
|
2022-05-24 09:17:23 -04:00
|
|
|
}
|
2022-05-24 06:48:02 -04:00
|
|
|
uart_send_ok();
|
2022-05-22 13:52:44 -04:00
|
|
|
return crc_init();
|
2022-05-19 08:57:38 -04:00
|
|
|
}
|
|
|
|
|
2022-05-19 15:37:51 -04:00
|
|
|
error_code uart_commands_data(error_code* status, int size, crc_t* checksum_global, crc_t checksum_loc, int offset){
|
2022-05-24 09:17:23 -04:00
|
|
|
char digit;
|
2022-05-22 13:52:44 -04:00
|
|
|
uart_receive_data(data, size);
|
2022-05-19 08:57:38 -04:00
|
|
|
crc_t checksum_tmp = crc_init();
|
|
|
|
checksum_tmp = crc_update(checksum_tmp, data, size);
|
2022-05-22 13:52:44 -04:00
|
|
|
checksum_tmp = crc_finalize(checksum_tmp);
|
2022-05-24 06:48:02 -04:00
|
|
|
int offset_counter = offset;
|
2022-05-19 08:57:38 -04:00
|
|
|
if(checksum_loc == checksum_tmp){
|
|
|
|
int tmp = size;
|
2022-05-24 06:48:02 -04:00
|
|
|
//iap_prepare_sectors(status, APP_OFFSET, APP_OFFSET+size/4095+1);
|
|
|
|
//iap_erase_sectors(status, APP_OFFSET, APP_OFFSET+size/4095+1);
|
2022-05-24 09:17:23 -04:00
|
|
|
iap_prepare_sectors(status, addr_to_sector(offset), addr_to_sector(offset+size)+1);
|
2022-05-22 17:44:17 -04:00
|
|
|
*checksum_global = crc_update(*checksum_global, data, size);
|
2022-05-24 08:53:35 -04:00
|
|
|
//verify if size == 4095 if smaller
|
|
|
|
if(size == 0x0400)
|
|
|
|
iap_copy_to_flash(status, offset, data, size);
|
|
|
|
else{
|
|
|
|
iap_copy_to_flash(status, offset, data, 0x400);
|
|
|
|
*checksum_global = crc_finalize(*checksum_global);
|
2022-05-19 08:57:38 -04:00
|
|
|
}
|
2022-05-24 08:53:35 -04:00
|
|
|
if(*status != ok){
|
|
|
|
uart_send_err();
|
2022-05-24 09:17:23 -04:00
|
|
|
itoa(*status, &digit, 10);
|
2022-05-24 10:52:09 -04:00
|
|
|
uart_send(&digit, 1);
|
2022-05-24 08:53:35 -04:00
|
|
|
return *status;
|
2022-05-19 08:57:38 -04:00
|
|
|
}
|
2022-05-24 06:48:02 -04:00
|
|
|
uart_send_ok();
|
2022-05-19 15:37:51 -04:00
|
|
|
return ok;
|
2022-05-19 08:57:38 -04:00
|
|
|
}else{
|
2022-05-24 06:48:02 -04:00
|
|
|
uart_send_err();
|
2022-05-24 09:17:23 -04:00
|
|
|
itoa(*status, &digit, 10);
|
2022-05-24 10:52:09 -04:00
|
|
|
uart_send(&digit, 1);
|
2022-05-19 08:57:38 -04:00
|
|
|
}
|
2022-05-24 09:17:23 -04:00
|
|
|
return local_checksum;
|
2022-05-19 08:57:38 -04:00
|
|
|
}
|
|
|
|
|
2022-05-19 15:37:51 -04:00
|
|
|
error_code uart_commands_check(crc_t crc_received, crc_t crc_calculated){
|
|
|
|
if(crc_received == crc_calculated) return ok;
|
|
|
|
else return global_checksum;
|
|
|
|
}
|
|
|
|
|
2022-05-19 08:57:38 -04:00
|
|
|
|
|
|
|
// return: -1 in case of ERR otherwise 0.
|
|
|
|
int uart_parse_command(char *user_input, cmd_t *cmd) {
|
|
|
|
|
|
|
|
//Initialize a simple command (empty, simple, foreground)
|
2022-05-24 06:48:02 -04:00
|
|
|
cmd->argv[0] = NULL;
|
|
|
|
cmd->argv[1] = NULL;
|
|
|
|
cmd->argv[2] = NULL;
|
2022-05-19 08:57:38 -04:00
|
|
|
cmd->argc = -1;
|
|
|
|
|
|
|
|
//Separate string in different token (i.e. command name + params + &)
|
|
|
|
do {
|
|
|
|
//A new element will be added
|
|
|
|
cmd->argc += 1;
|
|
|
|
|
|
|
|
//Get the adress of the next token (could be NULL to indicate end of argv)
|
|
|
|
cmd->argv[cmd->argc] = strtok(user_input, DELIMIERS);
|
|
|
|
user_input = NULL; //Useless to execute it each time but easier than having two different strtok calls
|
|
|
|
} while (cmd->argv[cmd->argc] != NULL); // while there are still tokens
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t uart_string_to_int(const char *str) {
|
2022-05-24 07:21:02 -04:00
|
|
|
//char* tmp;
|
|
|
|
//unsigned long long int hex = strtoull(str, &tmp, 16);
|
|
|
|
unsigned long int hex = strtoul(str, NULL, 16);
|
2022-05-19 08:57:38 -04:00
|
|
|
return hex;
|
|
|
|
}
|
2022-05-24 06:48:02 -04:00
|
|
|
|
|
|
|
void uart_send_ok(){
|
|
|
|
uart_send(OK, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
void uart_send_err(){
|
|
|
|
uart_send(ERR, 6);
|
2022-05-24 07:21:02 -04:00
|
|
|
}
|