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
|
|
|
|
|
|
|
void uart_commands_getid(error_code* status){
|
|
|
|
char hex[15];
|
|
|
|
uint32_t res = iap_read_part_id(status);
|
|
|
|
sprintf(hex, "0x%x\r\n", res);
|
|
|
|
if(*status == 0){
|
2022-05-19 15:37:51 -04:00
|
|
|
uart_send(OK, strlen(OK));
|
|
|
|
uart_send(hex, strlen(hex));
|
2022-05-19 08:57:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void uart_commands_getserial(error_code* status){
|
|
|
|
char hex[40];
|
|
|
|
uint32_t res[4];
|
|
|
|
iap_read_serial(status, res);
|
|
|
|
sprintf(hex, "0x%x%x%x%x\r\n", res[0], res[1], res[2], res[3]);
|
|
|
|
if(*status == 0){
|
2022-05-24 06:48:02 -04:00
|
|
|
uart_send_ok();
|
2022-05-19 08:57:38 -04:00
|
|
|
uart_send(hex, 35);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-24 06:48:02 -04:00
|
|
|
crc_t uart_commands_prog(error_code* status, int size){
|
|
|
|
iap_prepare_sectors(status, APP_OFFSET, APP_OFFSET+size/4095+5);
|
|
|
|
iap_erase_sectors(status, APP_OFFSET, APP_OFFSET+size/4095+5);
|
|
|
|
iap_prepare_sectors(status, APP_OFFSET, APP_OFFSET+size/4095+5);
|
|
|
|
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-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);
|
|
|
|
iap_prepare_sectors(status, APP_OFFSET, APP_OFFSET+size/4095+5);
|
2022-05-22 17:44:17 -04:00
|
|
|
*checksum_global = crc_update(*checksum_global, data, size);
|
2022-05-22 13:52:44 -04:00
|
|
|
while(tmp >= 4096){
|
2022-05-24 06:48:02 -04:00
|
|
|
iap_copy_to_flash(status, offset, checksum_global, 4096);
|
2022-05-19 08:57:38 -04:00
|
|
|
if(*status != ok){
|
2022-05-24 06:48:02 -04:00
|
|
|
uart_send_err();
|
2022-05-22 17:44:17 -04:00
|
|
|
return *status;
|
2022-05-19 08:57:38 -04:00
|
|
|
}
|
|
|
|
tmp = tmp - 4096;
|
|
|
|
}
|
2022-05-22 13:52:44 -04:00
|
|
|
while(tmp >= 1024){
|
2022-05-22 17:44:17 -04:00
|
|
|
iap_copy_to_flash(status, offset, checksum_global, 1024);
|
2022-05-19 08:57:38 -04:00
|
|
|
if(*status != ok){
|
2022-05-24 06:48:02 -04:00
|
|
|
uart_send_err();
|
2022-05-22 17:44:17 -04:00
|
|
|
return *status;
|
2022-05-19 08:57:38 -04:00
|
|
|
}
|
|
|
|
tmp = tmp - 1024;
|
2022-05-24 06:48:02 -04:00
|
|
|
offset_counter += 1024;
|
2022-05-19 08:57:38 -04:00
|
|
|
}
|
2022-05-22 13:52:44 -04:00
|
|
|
while(tmp >= 512){
|
2022-05-24 06:48:02 -04:00
|
|
|
iap_copy_to_flash(status, offset, checksum_global, 512);
|
2022-05-19 08:57:38 -04:00
|
|
|
if(*status != ok){
|
2022-05-24 06:48:02 -04:00
|
|
|
uart_send_err();
|
2022-05-22 17:44:17 -04:00
|
|
|
return *status;
|
2022-05-19 08:57:38 -04:00
|
|
|
}
|
|
|
|
tmp = tmp - 512;
|
|
|
|
}
|
2022-05-22 13:52:44 -04:00
|
|
|
while(tmp >= 256){
|
2022-05-24 06:48:02 -04:00
|
|
|
iap_copy_to_flash(status, offset, checksum_global, 256);
|
2022-05-19 08:57:38 -04:00
|
|
|
if(*status != ok){
|
2022-05-24 06:48:02 -04:00
|
|
|
uart_send_err();
|
2022-05-22 17:44:17 -04:00
|
|
|
return *status;
|
2022-05-19 08:57:38 -04:00
|
|
|
}
|
|
|
|
tmp = tmp - 256;
|
|
|
|
}
|
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-19 15:37:51 -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
|
|
|
}
|