This commit is contained in:
yul 2022-05-28 23:09:33 +02:00
parent 02cc22e6ae
commit a7f16616f0
2 changed files with 67 additions and 45 deletions

View File

@ -31,7 +31,7 @@ char* argvs[3][10];
cmd_t cmd;
uint32_t* size_store = (uint32_t*) 0x3400;
uint32_t* size_store = (uint32_t*) 0x3100;
crc_t* crc_store = (crc_t*) 0x3000;
uint32_t offset, total_size, offset_pointer;
@ -49,6 +49,7 @@ int is_code_valid(){
crc_t expected_crc = *crc_store;
size_t application_size = *size_store;
crc_t real_crc = crc_init();
if(application_size > MAX_PROG_SIZE) return 0;
real_crc = crc_update(real_crc, (void*)APP_OFFSET, application_size);
real_crc = crc_finalize(real_crc);
error_code is_error = uart_commands_check(real_crc, expected_crc);
@ -58,6 +59,13 @@ int is_code_valid(){
else return 0;
}
int is_b_pressed(){
int tmp = (LPC_GPIO0->FIOPIN)>>19;
int tmp2 = tmp & 1;
if(tmp2 == 0) return 1;
else return 0;
}
void write_app_info(crc_t* checksum, size_t* size){
iap_prepare_sectors(&status, addr_to_sector(0x3000), addr_to_sector(0x3fff));
if(status != ok){
@ -67,26 +75,20 @@ void write_app_info(crc_t* checksum, size_t* size){
if(status != ok){
uart_send_err();
}
iap_prepare_sectors(&status, addr_to_sector(0x3000), addr_to_sector(0x3fff));
if(status != ok){
uart_send_err();
}
iap_copy_to_flash(&status, (long unsigned int *)crc_store, (long unsigned int*)checksum, 0x0400);
iap_copy_to_flash(&status, (long unsigned int *)size_store, (long unsigned int*)size, 0x0400);
iap_copy_to_flash(&status, (long unsigned int *)crc_store, (long unsigned int*)checksum, 0x100);
//0x100 a result of trial and error I initially wanted to put 0x4 but noooo it doesn't work
iap_copy_to_flash(&status, (long unsigned int *)size_store, (long unsigned int*)size, 0x100);
}
int main(void) {
cmd.argv = (char**)argvs;
//verify b is pressed
//checksum is correct (
//bootloader_init();
uart_init(115200);
if(is_code_valid()){
void launch_program(){
SCB->VTOR = APP_OFFSET;
__asm("ldr r0,=0x4000");
__asm("ldr sp,[r0]");
__asm("ldr pc,[r0,#4]");
}else{
}
void manage_bootloader(){
cmd.argv = (char**)argvs;
while(1){
uart_receive_command(buff);
uart_parse_command(buff, &cmd);
@ -114,6 +116,7 @@ int main(void) {
error_code is_error = uart_commands_check(crc_prog, crc_data);
if(is_error == ok){
uart_send_ok();
break;
}else{
uart_send_err();
}
@ -121,6 +124,23 @@ int main(void) {
}
}
}
int main(void) {
//verify b is pressed
//checksum is correct (
//bootloader_init();
uart_init(115200);
LPC_GPIO0->FIODIR &=~(1<<19);
if(is_b_pressed() == 0){
if(is_code_valid()){
launch_program();
}else{
manage_bootloader();
}
}else{
manage_bootloader();
}
launch_program();
while(1);
return 0 ;
}

View File

@ -7,7 +7,9 @@
#include "crc.h"
#define DELIMIERS ","
#define LPC1769_FLASH_SIZE 0x80000
#define APP_OFFSET 0x4000
#define MAX_PROG_SIZE (LPC1769_FLASH_SIZE - APP_OFFSET)