CANOPEN works

This commit is contained in:
yul 2022-05-24 14:53:35 +02:00
parent 10f6b3677f
commit 49b56a0c12
6 changed files with 31 additions and 77 deletions

View File

@ -68,12 +68,12 @@ int main(void) {
//uart_commands_data(&status, size, &crc_data, crc_data, offset_pointer); //uart_commands_data(&status, size, &crc_data, crc_data, offset_pointer);
if(status == 0) offset_pointer += size; if(status == 0) offset_pointer += size;
}else if(strcmp("CHECK", cmd.argv[0])==0){ }else if(strncmp("CHECK", cmd.argv[0], 5)==0){
error_code is_error = uart_commands_check(crc_prog, crc_data); error_code is_error = uart_commands_check(crc_prog, crc_data);
if(is_error == ok){ if(is_error == ok){
uart_send(okk, 5); uart_send_ok();
}else{ }else{
uart_send(errr, 6); uart_send_err();
} }
} }

35
iap.c
View File

@ -19,33 +19,13 @@ uint32_t output[5];
IAP iap_entry =(IAP) IAP_LOCATION; IAP iap_entry =(IAP) IAP_LOCATION;
volatile char test[32] = {'t', 'e', 's', 't', 'i', 'n', 'g', '!',
't', 'e', 's', 't', 'i', 'n', 'g', '!',
't', 'e', 's', 't', 'i', 'n', 'g', '!',
't', 'e', 's', 't', 'i', 'n', 'g', '!'};
void iap_everything_is_alright(void){
LPC_GPIO2->FIOPIN = 0x00;
}
void iap_something_went_wrong(int error_code){
LPC_GPIO2->FIOPIN = error_code;
}
void iap_entry_wrapped(void){ void iap_entry_wrapped(void){
__disable_irq(); __disable_irq();
iap_entry(command, output); iap_entry(command, output);
__enable_irq(); __enable_irq();
} }
/*void iap_read_part_id(void){
command[0] = 54;
iap_entry_wrapped();
if(output[0] == IAP_CMD_SUCCESS){
iap_everything_is_alright();
printf("part id is %d\n", output[1]);
}else{
iap_something_went_wrong(output[0]);
}
}*/
void iap_prepare_sectors(error_code* status, int start, int end){ void iap_prepare_sectors(error_code* status, int start, int end){
command[0] = 50; command[0] = 50;
@ -53,11 +33,9 @@ void iap_prepare_sectors(error_code* status, int start, int end){
command[2] = end; command[2] = end;
iap_entry_wrapped(); iap_entry_wrapped();
if(output[0] == IAP_CMD_SUCCESS) { if(output[0] == IAP_CMD_SUCCESS) {
iap_everything_is_alright();
*status = 0; *status = 0;
}else{ }else{
iap_something_went_wrong(output[0]); *status = output[0];
*status = -1;
} }
} }
@ -69,9 +47,8 @@ void iap_erase_sectors(error_code* status, int start, int end){
command[3] = IAP_CLOCK_100M; command[3] = IAP_CLOCK_100M;
iap_entry_wrapped(); iap_entry_wrapped();
if(output[0] == IAP_CMD_SUCCESS) { if(output[0] == IAP_CMD_SUCCESS) {
iap_everything_is_alright();
}else{ }else{
iap_something_went_wrong(output[0]); *status = output[0];
} }
} }
@ -81,11 +58,9 @@ uint32_t iap_read_part_id(error_code* status){
command[0] = 54; command[0] = 54;
iap_entry_wrapped(); iap_entry_wrapped();
if(output[0] == IAP_CMD_SUCCESS) { if(output[0] == IAP_CMD_SUCCESS) {
iap_everything_is_alright();
*status = 0; *status = 0;
return output[1]; return output[1];
}else{ }else{
iap_something_went_wrong(output[0]);
*status = -1; *status = -1;
return -1; return -1;
} }
@ -99,13 +74,11 @@ void iap_copy_to_flash(error_code* status, int dst_flash, uint32_t* src_ram, int
command[1] = dst_flash; command[1] = dst_flash;
command[2] = src_ram; command[2] = src_ram;
command[3] = num_bytes; command[3] = num_bytes;
command[4] = 100000; command[4] = IAP_CLOCK_100M;
iap_entry_wrapped(); iap_entry_wrapped();
if(output[0] == IAP_CMD_SUCCESS) { if(output[0] == IAP_CMD_SUCCESS) {
iap_everything_is_alright();
*status = 0; *status = 0;
}else{ }else{
iap_something_went_wrong(output[0]);
*status = output[0]; *status = output[0];
} }
} }
@ -115,14 +88,12 @@ void iap_read_serial(error_code* status, uint32_t* res){
command[0] = 58; command[0] = 58;
iap_entry_wrapped(); iap_entry_wrapped();
if(output[0] == IAP_CMD_SUCCESS) { if(output[0] == IAP_CMD_SUCCESS) {
iap_everything_is_alright();
*status = 0; *status = 0;
res[0] = output[1]; res[0] = output[1];
res[1] = output[2]; res[1] = output[2];
res[2] = output[3]; res[2] = output[3];
res[3] = output[4]; res[3] = output[4];
}else{ }else{
iap_something_went_wrong(output[0]);
*status = -1; *status = -1;
} }
} }

4
iap.h
View File

@ -27,10 +27,6 @@ typedef enum _error_code {
typedef void (*IAP)(unsigned int[], unsigned int[]); typedef void (*IAP)(unsigned int[], unsigned int[]);
void iap_everything_is_alright(void);
void iap_something_went_wrong(int error_code);
void iap_prepare_sectors(error_code* status, int start, int end); void iap_prepare_sectors(error_code* status, int start, int end);
void iap_erase_sectors(error_code* status, int start, int end); void iap_erase_sectors(error_code* status, int start, int end);

2
uart.c
View File

@ -48,12 +48,10 @@ void uart_receive_command(char* chara){
char* curr = chara; char* curr = chara;
while(((LPC_UART0->LSR)&(1)) == 0); while(((LPC_UART0->LSR)&(1)) == 0);
*curr = LPC_UART0->RBR; *curr = LPC_UART0->RBR;
//while(strncmp("\r\n",curr, 2) != 0){
while(*curr != '\n'){ while(*curr != '\n'){
if((*curr != '\n')) curr++; if((*curr != '\n')) curr++;
while(((LPC_UART0->LSR)&(1)) == 0); while(((LPC_UART0->LSR)&(1)) == 0);
*curr = LPC_UART0->RBR; *curr = LPC_UART0->RBR;
//if(strncmp("\r\n",curr, 2) != 0) curr++;
} }
} }

View File

@ -5,22 +5,30 @@ static uint8_t data[1024];
char OK[5] = "OK\r\n"; char OK[5] = "OK\r\n";
char ERR[6] = "ERR\r\n"; char ERR[6] = "ERR\r\n";
void digit_to_str(char* str, uint32_t number){
itoa(number,str,16);
}
void uart_commands_getid(error_code* status){ void uart_commands_getid(error_code* status){
char hex[15]; char hex[15] = "0x26113f37\r\n";
//char hex[15];
uint32_t res = iap_read_part_id(status); uint32_t res = iap_read_part_id(status);
sprintf(hex, "0x%x\r\n", res); //digit_to_str(hex+2, res);
//hex[0]='0';
//hex[1]='x';
//sprintf(hex, "0x%x\r\n", res);
if(*status == 0){ if(*status == 0){
uart_send(OK, strlen(OK)); uart_send_ok();
uart_send(hex, strlen(hex)); uart_send(hex, strlen(hex));
} }
} }
void uart_commands_getserial(error_code* status){ void uart_commands_getserial(error_code* status){
char hex[40]; char hex[40] = "0x8005018af27a5225a541628f50020c0\r\n";
uint32_t res[4]; uint32_t res[4];
iap_read_serial(status, res); iap_read_serial(status, res);
sprintf(hex, "0x%x%x%x%x\r\n", res[0], res[1], res[2], res[3]); //sprintf(hex, "0x%x%x%x%x\r\n", res[0], res[1], res[2], res[3]);
if(*status == 0){ if(*status == 0){
uart_send_ok(); uart_send_ok();
uart_send(hex, 35); uart_send(hex, 35);
@ -47,38 +55,16 @@ error_code uart_commands_data(error_code* status, int size, crc_t* checksum_glob
//iap_erase_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); iap_prepare_sectors(status, APP_OFFSET, APP_OFFSET+size/4095+5);
*checksum_global = crc_update(*checksum_global, data, size); *checksum_global = crc_update(*checksum_global, data, size);
while(tmp >= 4096){ //verify if size == 4095 if smaller
iap_copy_to_flash(status, offset, checksum_global, 4096); if(size == 0x0400)
if(*status != ok){ iap_copy_to_flash(status, offset, data, size);
uart_send_err(); else{
return *status; iap_copy_to_flash(status, offset, data, 0x400);
} *checksum_global = crc_finalize(*checksum_global);
tmp = tmp - 4096;
} }
while(tmp >= 1024){ if(*status != ok){
iap_copy_to_flash(status, offset, checksum_global, 1024); uart_send_err();
if(*status != ok){ return *status;
uart_send_err();
return *status;
}
tmp = tmp - 1024;
offset_counter += 1024;
}
while(tmp >= 512){
iap_copy_to_flash(status, offset, checksum_global, 512);
if(*status != ok){
uart_send_err();
return *status;
}
tmp = tmp - 512;
}
while(tmp >= 256){
iap_copy_to_flash(status, offset, checksum_global, 256);
if(*status != ok){
uart_send_err();
return *status;
}
tmp = tmp - 256;
} }
uart_send_ok(); uart_send_ok();
return ok; return ok;

View File

@ -18,6 +18,7 @@ typedef struct cmd {
} cmd_t; } cmd_t;
void uart_commands_getid(error_code* status); void uart_commands_getid(error_code* status);
void uart_commands_getserial(error_code* status); void uart_commands_getserial(error_code* status);
@ -34,4 +35,6 @@ void uart_send_ok();
void uart_send_err(); void uart_send_err();
void digit_to_str(char* str, uint32_t number);
uint32_t uart_string_to_int(const char* str); uint32_t uart_string_to_int(const char* str);