This commit is contained in:
yul 2022-05-24 12:48:02 +02:00
parent a695df73d2
commit 3768522850
4 changed files with 41 additions and 24 deletions

View File

@ -26,9 +26,11 @@ char errr[5] = "ERR\r\n";
char* argvs[3][10];
cmd_t cmd;
uint32_t offset, total_size, offset_pointer;
crc_t crc_prog, crc_data;
@ -41,6 +43,7 @@ crc_t crc_prog, crc_data;
char buff[256];
int main(void) {
cmd.argv = argvs;
uart_init(115200);
while(1){
uart_receive_command(buff);
@ -55,7 +58,7 @@ int main(void) {
crc_prog = uart_string_to_int(cmd.argv[3]);
total_size = uart_string_to_int(cmd.argv[2]);
offset = uart_string_to_int(cmd.argv[1]);
crc_data = uart_commands_prog();
crc_data = uart_commands_prog(&status, total_size);
offset_pointer = offset;
}else if(strcmp("DATA", cmd.argv[0])==0){

2
iap.c
View File

@ -94,7 +94,7 @@ uint32_t iap_read_part_id(error_code* status){
void iap_copy_to_flash(error_code* status, int dst_flash, uint32_t* src_ram, int num_bytes){
//iap_prepare_sectors(status, dst_flash, dst_flash+num_bytes);
//iap_erase_sectors(status, dst_flash, dst_flash+num_bytes);
//iap_prepare_sectors(status, dst_flash, dst_flash+num_bytes);
iap_prepare_sectors(status, dst_flash, dst_flash+num_bytes);
command[0] = 51;
command[1] = dst_flash;
command[2] = src_ram;

View File

@ -22,13 +22,16 @@ void uart_commands_getserial(error_code* status){
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_ok();
uart_send(hex, 35);
}
}
crc_t uart_commands_prog(void){
uart_send(OK, 5);
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();
return crc_init();
}
@ -37,49 +40,50 @@ error_code uart_commands_data(error_code* status, int size, crc_t* checksum_glob
crc_t checksum_tmp = crc_init();
checksum_tmp = crc_update(checksum_tmp, data, size);
checksum_tmp = crc_finalize(checksum_tmp);
int offset_counter = offset;
if(checksum_loc == checksum_tmp){
int tmp = size;
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+1);
//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);
*checksum_global = crc_update(*checksum_global, data, size);
while(tmp >= 4096){
iap_copy_to_flash(status, offset/0x1000, checksum_global, 4096);
iap_copy_to_flash(status, offset, checksum_global, 4096);
if(*status != ok){
uart_send(ERR, 6);
uart_send_err();
return *status;
}
tmp = tmp - 4096;
}
while(tmp >= 1024){
//iap_copy_to_flash(status, offset/0x1000, checksum_global, 1024);
iap_copy_to_flash(status, offset, checksum_global, 1024);
if(*status != ok){
uart_send(ERR, 6);
uart_send_err();
return *status;
}
tmp = tmp - 1024;
offset_counter += 1024;
}
while(tmp >= 512){
iap_copy_to_flash(status, offset/0x1000, checksum_global, 512);
iap_copy_to_flash(status, offset, checksum_global, 512);
if(*status != ok){
uart_send(ERR, 6);
uart_send_err();
return *status;
}
tmp = tmp - 512;
}
while(tmp >= 256){
iap_copy_to_flash(status, offset/0x1000, checksum_global, 256);
iap_copy_to_flash(status, offset, checksum_global, 256);
if(*status != ok){
uart_send(ERR, 6);
uart_send_err();
return *status;
}
tmp = tmp - 256;
}
uart_send(OK, 4);
uart_send_ok();
return ok;
}else{
uart_send(ERR, 6);
uart_send_err();
return local_checksum;
}
}
@ -94,7 +98,9 @@ error_code uart_commands_check(crc_t crc_received, crc_t crc_calculated){
int uart_parse_command(char *user_input, cmd_t *cmd) {
//Initialize a simple command (empty, simple, foreground)
cmd->argv = NULL;
cmd->argv[0] = NULL;
cmd->argv[1] = NULL;
cmd->argv[2] = NULL;
cmd->argc = -1;
//Separate string in different token (i.e. command name + params + &)
@ -102,10 +108,6 @@ int uart_parse_command(char *user_input, cmd_t *cmd) {
//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
@ -121,3 +123,11 @@ uint32_t uart_string_to_int(const char *str) {
unsigned long long int hex = strtoull(str, &tmp, 16);
return hex;
}
void uart_send_ok(){
uart_send(OK, 4);
}
void uart_send_err(){
uart_send(ERR, 6);
}

View File

@ -22,7 +22,7 @@ void uart_commands_getid(error_code* status);
void uart_commands_getserial(error_code* status);
crc_t uart_commands_prog(void);
crc_t uart_commands_prog(error_code* status, int size);
error_code uart_commands_data(error_code* status, int size, crc_t* checksum_global, crc_t checksum_loc, int offset);
@ -30,4 +30,8 @@ error_code uart_commands_check(crc_t crc_received, crc_t crc_calculated);
int uart_parse_command(char *user_input, cmd_t *cmd);
void uart_send_ok();
void uart_send_err();
uint32_t uart_string_to_int(const char* str);