114 lines
2.8 KiB
C
114 lines
2.8 KiB
C
#include "uart_commands.h"
|
|
|
|
static uint8_t data[4096];
|
|
|
|
char OK[5] = "OK\r\n";
|
|
char ERR[6] = "ERR\r\n";
|
|
|
|
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){
|
|
uart_send(OK, strlen(OK));
|
|
uart_send(hex, strlen(hex));
|
|
}
|
|
}
|
|
|
|
|
|
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){
|
|
uart_send(OK, 4);
|
|
uart_send(hex, 35);
|
|
}
|
|
}
|
|
|
|
crc_t uart_commands_prog(void){
|
|
return crc_init();
|
|
uart_send(OK, 5);
|
|
}
|
|
|
|
error_code uart_commands_data(error_code* status, int size, crc_t* checksum_global, crc_t checksum_loc, int offset){
|
|
uart_receive_data(data);
|
|
crc_t checksum_tmp = crc_init();
|
|
checksum_tmp = crc_update(checksum_tmp, data, size);
|
|
if(checksum_loc == checksum_tmp){
|
|
int tmp = size;
|
|
while(tmp > 4096){
|
|
iap_copy_to_flash(status, offset, &checksum_loc, 4096);
|
|
if(*status != ok){
|
|
uart_send(ERR, 6);
|
|
}
|
|
tmp = tmp - 4096;
|
|
}
|
|
while(tmp > 1024){
|
|
iap_copy_to_flash(status, offset, &checksum_loc, 1024);
|
|
if(*status != ok){
|
|
uart_send(ERR, 6);
|
|
}
|
|
tmp = tmp - 1024;
|
|
}
|
|
while(tmp > 512){
|
|
iap_copy_to_flash(status, offset, &checksum_loc, 512);
|
|
if(*status != ok){
|
|
uart_send(ERR, 6);
|
|
}
|
|
tmp = tmp - 512;
|
|
}
|
|
while(tmp > 256){
|
|
iap_copy_to_flash(status, offset, &checksum_loc, 256);
|
|
if(*status != ok){
|
|
uart_send(ERR, 6);
|
|
}
|
|
tmp = tmp - 256;
|
|
}
|
|
*checksum_global = crc_update(*checksum_global, data, size);
|
|
return ok;
|
|
}else{
|
|
uart_send(ERR, 6);
|
|
return local_checksum;
|
|
}
|
|
}
|
|
|
|
error_code uart_commands_check(crc_t crc_received, crc_t crc_calculated){
|
|
if(crc_received == crc_calculated) return ok;
|
|
else return global_checksum;
|
|
}
|
|
|
|
|
|
// 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)
|
|
cmd->argv = NULL;
|
|
cmd->argc = -1;
|
|
|
|
//Separate string in different token (i.e. command name + params + &)
|
|
do {
|
|
//A new element will be added
|
|
cmd->argc += 1;
|
|
|
|
//Allocate a new pointer on char for next argv element
|
|
if((cmd->argv = realloc(cmd->argv, (cmd->argc+1)*sizeof(char*))) == NULL)
|
|
perror("uart_parse_command::realloc");
|
|
|
|
//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) {
|
|
// Convert input in port number
|
|
char* tmp;
|
|
unsigned long int hex = strtoul(str, tmp, 16);
|
|
return hex;
|
|
}
|