This commit is contained in:
yul 2022-05-19 21:37:51 +02:00
parent 63085ebd4c
commit 3f39d5714e
5 changed files with 37 additions and 22 deletions

View File

@ -24,7 +24,6 @@ error_code status;
char okk[4] = "OK\r\n";
char errr[5] = "ERR\r\n";
char buff[256];
@ -39,37 +38,40 @@ crc_t crc_prog, crc_data;
// TODO: insert other definitions and declarations here
char buff[256];
int main(void) {
uart_init(115200);
//iap_read_part_id();
//iap_copy_to_flash(0x8000,(uint8_t*)test, 4);
while(1){
uart_receive_command(buff);
uart_parse_command(buff, &cmd);
if (strcmp("GETID", cmd.argv[0])==0){
if (strncmp("GETID", cmd.argv[0], 5)==0){
uart_commands_getid(&status);
}else if(strcmp("GETSERIAL", cmd.argv[0])==0){
}else if(strncmp("GETSE", cmd.argv[0], 5)==0){
uart_commands_getserial(&status);
}else if(strcmp("PROG", cmd.argv[0])==0){
}else if(strncmp("PROG", cmd.argv[0], 4)==0){
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();
offset_pointer = offset;
}else if(strcmp("DATA", cmd.argv[0])==0){
int size = uart_string_to_int(cmd.argv[1]);
crc_t local_checksum = uart_string_to_int(cmd.argv[2]);
uart_commands_data(&status, size, &crc_data, local_checksum, offset_pointer);
if(status == 0) offset_pointer += size;
}else if(strcmp("CHECK", cmd.argv[0])==0){
if(crc_prog == crc_data){
error_code is_error = uart_commands_check(crc_prog, crc_data);
if(is_error == ok){
uart_send(okk, 5);
}else{
uart_send(errr, 6);
}
}
//uart_commands_data(&status, 0x00004000, 0x00048f40, 0x4);
}
}
while(1);
return 0 ;

4
iap.c
View File

@ -77,7 +77,7 @@ void iap_erase_sectors(error_code* status, int start, int end){
uint32_t iap_read_part_id(error_code* status){
//iap_prepare_sectors(dst_flash, dst_flash+num_bytes-1);
iap_prepare_sectors(status, 2, 9);
iap_prepare_sectors(status, 2, 10);
command[0] = 54;
iap_entry_wrapped();
if(output[0] == IAP_CMD_SUCCESS) {
@ -109,7 +109,7 @@ void iap_copy_to_flash(error_code* status, int dst_flash, uint32_t* src_ram, int
}
void iap_read_serial(error_code* status, uint32_t* res){
iap_prepare_sectors(status, 2, 9);
//iap_prepare_sectors(status, 2, 9);
command[0] = 58;
iap_entry_wrapped();
if(output[0] == IAP_CMD_SUCCESS) {

7
uart.c
View File

@ -45,14 +45,17 @@ void uart_send(char* buff, uint32_t length){
void uart_receive_command(char* chara){
char* curr = chara;
while(strncmp(curr, "\r\n", 2) != 0){
//while(strncmp("\r\n",curr, 2) != 0){
while(*curr != '\n'){
while(((LPC_UART0->LSR)&(1)) == 0);
*curr = LPC_UART0->RBR;
if(strncmp(curr, "\r\n", 2) != 0) curr++;
//if(strncmp("\r\n",curr, 2) != 0) curr++;
if((*curr != '\n')) curr++;
}
}
void uart_receive_data(uint8_t* chara){
//size parameter add while size != size_given
uint8_t* curr = chara;
while(*curr != '\0'){
while(((LPC_UART0->LSR)&(1)) == 0);

View File

@ -2,16 +2,16 @@
static uint8_t data[4096];
char OK[4] = "OK\r\n";
char ERR[5] = "ERR\r\n";
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, 5);
uart_send(hex, 12);
uart_send(OK, strlen(OK));
uart_send(hex, strlen(hex));
}
}
@ -22,7 +22,7 @@ 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, 5);
uart_send(OK, 4);
uart_send(hex, 35);
}
}
@ -32,7 +32,7 @@ crc_t uart_commands_prog(void){
uart_send(OK, 5);
}
void uart_commands_data(error_code* status, int size, crc_t* checksum_global, crc_t checksum_loc, int offset){
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);
@ -67,11 +67,18 @@ void uart_commands_data(error_code* status, int size, crc_t* checksum_global, cr
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) {
@ -100,6 +107,7 @@ int uart_parse_command(char *user_input, cmd_t *cmd) {
uint32_t uart_string_to_int(const char *str) {
// Convert input in port number
unsigned long int hex = strtoul(str, NULL, 16);
char* tmp;
unsigned long int hex = strtoul(str, tmp, 16);
return hex;
}

View File

@ -6,7 +6,7 @@
#include "iap.h"
#include "crc.h"
#define DELIMIERS ","
#define DELIMIERS ','
@ -23,7 +23,9 @@ void uart_commands_getserial(error_code* status);
crc_t uart_commands_prog(void);
void uart_commands_data(error_code* status, int size, crc_t* checksum_global, crc_t checksum_loc, int offset);
error_code uart_commands_data(error_code* status, int size, crc_t* checksum_global, crc_t checksum_loc, int offset);
error_code uart_commands_check(crc_t crc_received, crc_t crc_calculated);
int uart_parse_command(char *user_input, cmd_t *cmd);