Compare commits
15 Commits
6d4ca4fccd
...
master
Author | SHA1 | Date | |
---|---|---|---|
e1ffa61060 | |||
b21b8dbf2d | |||
a94c55d4a6 | |||
917aa733ff | |||
a7f16616f0 | |||
02cc22e6ae | |||
84caa2db3b | |||
ae3e68c12b | |||
41414634b6 | |||
49b56a0c12 | |||
10f6b3677f | |||
3768522850 | |||
a695df73d2 | |||
3672c0f986 | |||
8ee6dff899 |
131
bootloader.c
131
bootloader.c
@ -18,17 +18,33 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "uart_commands.h"
|
#include "uart_commands.h"
|
||||||
|
#include "timer.h"
|
||||||
|
|
||||||
error_code status;
|
error_code status;
|
||||||
|
|
||||||
char okk[4] = "OK\r\n";
|
enum States
|
||||||
char errr[5] = "ERR\r\n";
|
{
|
||||||
|
S1,
|
||||||
|
S2,
|
||||||
|
S3
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
is_even_assembly:
|
||||||
|
ldr sp, [r0]
|
||||||
|
ret
|
||||||
|
*/
|
||||||
|
|
||||||
|
enum States state = S1;
|
||||||
|
|
||||||
|
char* argvs[3][10];
|
||||||
|
|
||||||
cmd_t cmd;
|
cmd_t cmd;
|
||||||
|
|
||||||
|
uint32_t* size_store = (uint32_t*) 0x3100;
|
||||||
|
crc_t* crc_store = (crc_t*) 0x3000;
|
||||||
|
|
||||||
uint32_t offset, total_size, offset_pointer;
|
uint32_t offset, total_size, offset_pointer;
|
||||||
|
|
||||||
crc_t crc_prog, crc_data;
|
crc_t crc_prog, crc_data;
|
||||||
@ -40,40 +56,123 @@ crc_t crc_prog, crc_data;
|
|||||||
|
|
||||||
char buff[256];
|
char buff[256];
|
||||||
|
|
||||||
int main(void) {
|
int is_code_valid(){
|
||||||
uart_init(115200);
|
crc_t expected_crc = *crc_store;
|
||||||
while(1){
|
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);
|
||||||
|
if(is_error == ok){
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
set_colour(RED);//code invalid, red led
|
||||||
|
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){
|
||||||
|
uart_send_err();
|
||||||
|
}
|
||||||
|
iap_erase_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, 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
void launch_program(){
|
||||||
|
SCB->VTOR = APP_OFFSET;
|
||||||
|
__asm("ldr r0,=0x4000");
|
||||||
|
__asm("ldr sp,[r0]");
|
||||||
|
__asm("ldr pc,[r0,#4]");
|
||||||
|
}
|
||||||
|
|
||||||
|
void manage_bootloader(){
|
||||||
|
cmd.argv = (char**)argvs;
|
||||||
|
int var = 1;
|
||||||
|
char digit;
|
||||||
|
set_colour(GREEN);
|
||||||
|
while(var){
|
||||||
uart_receive_command(buff);
|
uart_receive_command(buff);
|
||||||
uart_parse_command(buff, &cmd);
|
uart_parse_command(buff, &cmd);
|
||||||
if (strncmp("GETID", cmd.argv[0], 5)==0){
|
set_colour(GREEN);
|
||||||
|
if ((state == S1) && (strncmp("GETID", cmd.argv[0], 5)==0)){
|
||||||
uart_commands_getid(&status);
|
uart_commands_getid(&status);
|
||||||
|
|
||||||
}else if(strncmp("GETSE", cmd.argv[0], 5)==0){
|
}else if((state == S1) && (strncmp("GETSE", cmd.argv[0], 5)==0)){
|
||||||
uart_commands_getserial(&status);
|
uart_commands_getserial(&status);
|
||||||
|
|
||||||
}else if(strncmp("PROG", cmd.argv[0], 4)==0){
|
}else if((state == S1) && (strncmp("PROG", cmd.argv[0], 4)==0)){
|
||||||
crc_prog = uart_string_to_int(cmd.argv[3]);
|
crc_prog = uart_string_to_int(cmd.argv[3]);
|
||||||
total_size = uart_string_to_int(cmd.argv[2]);
|
total_size = uart_string_to_int(cmd.argv[2]);
|
||||||
offset = uart_string_to_int(cmd.argv[1]);
|
offset = uart_string_to_int(cmd.argv[1]);
|
||||||
crc_data = uart_commands_prog();
|
write_app_info(&crc_prog, (unsigned int *)&total_size);
|
||||||
|
crc_data = uart_commands_prog(&status, total_size);
|
||||||
|
if(status == ok){
|
||||||
|
state = S2;
|
||||||
|
}
|
||||||
offset_pointer = offset;
|
offset_pointer = offset;
|
||||||
|
|
||||||
}else if(strcmp("DATA", cmd.argv[0])==0){
|
}else if(((state == S2) || (state = S3)) && (strcmp("DATA", cmd.argv[0])==0)){
|
||||||
int size = uart_string_to_int(cmd.argv[1]);
|
int size = uart_string_to_int(cmd.argv[1]);
|
||||||
crc_t local_checksum = uart_string_to_int(cmd.argv[2]);
|
crc_t local_checksum = uart_string_to_int(cmd.argv[2]);
|
||||||
//uart_commands_data(&status, size, &crc_data, local_checksum, offset_pointer);
|
uart_commands_data(&status, size, &crc_data, local_checksum, offset_pointer);
|
||||||
uart_commands_data(&status, size, &crc_data, crc_data, offset_pointer);
|
if(status == ok){
|
||||||
if(status == 0) offset_pointer += size;
|
offset_pointer += size;
|
||||||
|
state = S3;
|
||||||
|
}else{
|
||||||
|
state = S2;
|
||||||
|
}
|
||||||
|
|
||||||
}else if(strcmp("CHECK", cmd.argv[0])==0){
|
}else if((state == S3) && (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);
|
state = S1;
|
||||||
|
uart_send_ok();
|
||||||
|
uart_send("bla\r\n", 5);//putting bla here so OK will show up
|
||||||
|
//removing bla for the moment will make ok not show up
|
||||||
|
var = 0;
|
||||||
|
LPC_GPIO2->FIOCLR0 = 0xff;
|
||||||
|
launch_program();
|
||||||
}else{
|
}else{
|
||||||
uart_send(errr, 6);
|
state = S2;
|
||||||
|
uart_send_err();
|
||||||
|
set_colour(RED);
|
||||||
|
itoa(is_error, &digit, 10);
|
||||||
|
uart_send(&digit, 1);
|
||||||
|
//flash LED red, and stays in the bootloader mode
|
||||||
}
|
}
|
||||||
|
}else {
|
||||||
|
uart_send_cmd_illeg();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
uart_init(115200);
|
||||||
|
timer_0();
|
||||||
|
if(is_b_pressed() == 0){
|
||||||
|
if(is_code_valid()){
|
||||||
|
launch_program();
|
||||||
|
}else{
|
||||||
|
manage_bootloader();
|
||||||
}
|
}
|
||||||
|
}else{
|
||||||
|
manage_bootloader();
|
||||||
}
|
}
|
||||||
while(1);
|
while(1);
|
||||||
return 0 ;
|
return 0 ;
|
||||||
|
94
iap.c
94
iap.c
@ -14,38 +14,47 @@
|
|||||||
|
|
||||||
#include "iap.h"
|
#include "iap.h"
|
||||||
|
|
||||||
uint32_t command[5];
|
unsigned int command[5];
|
||||||
uint32_t output[5];
|
unsigned int output[5];
|
||||||
IAP iap_entry =(IAP) IAP_LOCATION;
|
IAP iap_entry =(IAP) IAP_LOCATION;
|
||||||
|
|
||||||
|
int addr_to_sector(unsigned long int addr){
|
||||||
volatile char test[32] = {'t', 'e', 's', 't', 'i', 'n', 'g', '!',
|
int tmp = addr & 0xfffff;
|
||||||
't', 'e', 's', 't', 'i', 'n', 'g', '!',
|
tmp = tmp >> 12;
|
||||||
't', 'e', 's', 't', 'i', 'n', 'g', '!',
|
if(tmp <= 15) return tmp;
|
||||||
't', 'e', 's', 't', 'i', 'n', 'g', '!'};
|
else{
|
||||||
|
if(tmp >= 0x10 && tmp <=0x17)
|
||||||
void iap_everything_is_alright(void){
|
return 16;
|
||||||
LPC_GPIO2->FIOPIN = 0x00;
|
else if(tmp >= 0x18 && tmp <= 0x1f)
|
||||||
}
|
return 17;
|
||||||
void iap_something_went_wrong(int error_code){
|
else if(tmp >= 0x20 && tmp <= 0x27)
|
||||||
LPC_GPIO2->FIOPIN = error_code;
|
return 18;
|
||||||
|
else if(tmp >= 0x28 && tmp <= 0x2f)
|
||||||
|
return 19;
|
||||||
|
else if(tmp >= 0x30 && tmp <= 0x37)
|
||||||
|
return 20;
|
||||||
|
else if(tmp >= 0x38 && tmp <= 0x3f)
|
||||||
|
return 21;
|
||||||
|
else if(tmp >= 0x40 && tmp <= 0x47)
|
||||||
|
return 22;
|
||||||
|
else if(tmp >= 0x48 && tmp <= 0x4f)
|
||||||
|
return 23;
|
||||||
|
else if(tmp >= 0x50 && tmp <= 0x57) return 24;
|
||||||
|
else if(tmp >= 0x58 && tmp <= 0x3f) return 25;
|
||||||
|
else if(tmp >= 0x60 && tmp <= 0x67) return 26;
|
||||||
|
else if(tmp >= 0x68 && tmp <= 0x6f) return 27;
|
||||||
|
else if(tmp >= 0x70 && tmp <= 0x77) return 28;
|
||||||
|
else if(tmp >= 0x78 && tmp <= 0x7f) return 29;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 +62,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 +76,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,31 +87,26 @@ 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void iap_copy_to_flash(error_code* status, int dst_flash, uint32_t* src_ram, int num_bytes){
|
void iap_copy_to_flash(error_code* status, long unsigned int* dst_flash, uint8_t* src_ram, int num_bytes){
|
||||||
//iap_prepare_sectors(status, dst_flash, dst_flash+num_bytes);
|
iap_prepare_sectors(status, addr_to_sector((long unsigned int)dst_flash), addr_to_sector((long unsigned int)dst_flash+num_bytes));
|
||||||
//iap_erase_sectors(status, dst_flash, dst_flash+num_bytes);
|
|
||||||
//iap_prepare_sectors(status, dst_flash, dst_flash+num_bytes);
|
|
||||||
command[0] = 51;
|
command[0] = 51;
|
||||||
command[1] = dst_flash;
|
command[1] = (unsigned int)dst_flash;
|
||||||
command[2] = *src_ram;
|
command[2] = (unsigned int)src_ram;
|
||||||
command[3] = num_bytes;
|
command[3] = num_bytes;
|
||||||
|
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 = -1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,26 +115,13 @@ 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 = output[0];
|
||||||
*status = -1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: insert other include files here
|
|
||||||
|
|
||||||
// TODO: insert other definitions and declarations here
|
|
||||||
/*
|
|
||||||
int main(void) {
|
|
||||||
LPC_PINCON->PINSEL4 &= ~0xFF; //Configure the PORT2 Pins as GPIO;
|
|
||||||
LPC_GPIO2->FIODIR = 0xff; //Configure the PORT2 pins as OUTPUT;
|
|
||||||
iap_read_part_id();
|
|
||||||
iap_erase_sectors(2, 9);
|
|
||||||
iap_copy_to_flash(0x8000,(uint8_t*)test, 4);
|
|
||||||
}*/
|
|
||||||
|
12
iap.h
12
iap.h
@ -26,18 +26,18 @@ typedef enum _error_code {
|
|||||||
|
|
||||||
typedef void (*IAP)(unsigned int[], unsigned int[]);
|
typedef void (*IAP)(unsigned int[], unsigned int[]);
|
||||||
|
|
||||||
|
/*This command must be executed before executing "Copy RAM to Flash" or
|
||||||
void iap_everything_is_alright(void);
|
"Erase Sector(s)" command. Successful execution of the "Copy RAM to Flash" or
|
||||||
void iap_something_went_wrong(int error_code);
|
"Erase Sector(s)" command causes relevant sectors to be protected again. To
|
||||||
|
prepare a single sector use the same "Start" and "End" sector numbers.*/
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
void iap_copy_to_flash(error_code* status, int dst_flash, uint32_t* src_ram, int num_bytes);
|
void iap_copy_to_flash(error_code* status, long unsigned int * dst_flash, uint8_t* src_ram, int num_bytes);
|
||||||
|
|
||||||
uint32_t iap_read_part_id(error_code* status);
|
uint32_t iap_read_part_id(error_code* status);
|
||||||
|
|
||||||
void iap_read_serial(error_code* status, uint32_t* res);
|
void iap_read_serial(error_code* status, uint32_t* res);
|
||||||
|
|
||||||
|
int addr_to_sector(unsigned long int addr);
|
||||||
|
11
uart.c
11
uart.c
@ -32,13 +32,12 @@ void uart_init(uint32_t baudrate){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void uart_send(char* buff, uint32_t length){
|
void uart_send(char* buff, uint32_t length){
|
||||||
int tmp;
|
//int tmp;
|
||||||
while (length-- != 0 ){
|
while (length-- != 0 ){
|
||||||
LPC_UART0->THR = *buff++;
|
//LPC_UART0->THR = *buff++;
|
||||||
while(((LPC_UART0->LSR)&(1<<5)) == 0);//stuck in while when U1THR contains valid data
|
while(((LPC_UART0->LSR)&(1<<5)) == 0);//stuck in while when U1THR contains valid data
|
||||||
tmp = LPC_UART0->RBR;
|
LPC_UART0->THR = *buff++;
|
||||||
LPC_GPIO2->FIOCLR = 0xff;
|
//tmp = LPC_UART0->RBR;
|
||||||
LPC_GPIO2->FIOSET = tmp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -47,12 +46,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++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
145
uart_commands.c
145
uart_commands.c
@ -1,81 +1,114 @@
|
|||||||
#include "uart_commands.h"
|
#include "uart_commands.h"
|
||||||
|
|
||||||
static uint8_t data[4096];
|
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";
|
||||||
|
char CMD_ILLEGAL[13] = "CMD_ILLEGAL\r\n";
|
||||||
|
|
||||||
|
void digit_to_str(char* str, uint32_t number, int base){
|
||||||
|
itoa(number,str,16);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void uart_commands_getid(error_code* status){
|
void uart_commands_getid(error_code* status){
|
||||||
|
//char hex[15] = "0x26113f37\r\n";
|
||||||
char hex[15];
|
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, 16);
|
||||||
|
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));
|
||||||
|
uart_send("\r\n", 2);
|
||||||
|
}else{
|
||||||
|
uart_send_err();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void uart_commands_getserial(error_code* status){
|
void uart_commands_getserial(error_code* status){
|
||||||
|
char digit;
|
||||||
char hex[40];
|
char hex[40];
|
||||||
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]);
|
digit_to_str(hex+2, res[0], 16);
|
||||||
if(*status == 0){
|
hex[0]='0';
|
||||||
uart_send(OK, 4);
|
hex[1]='x';
|
||||||
uart_send(hex, 35);
|
if(*status != ok){
|
||||||
|
uart_send_err();
|
||||||
|
itoa(*status, &digit, 10);
|
||||||
|
uart_send(&digit, 1);
|
||||||
|
}
|
||||||
|
if(*status == ok){
|
||||||
|
uart_send_ok();
|
||||||
|
uart_send(hex, strlen(hex));
|
||||||
|
digit_to_str(hex, res[1], 16);
|
||||||
|
uart_send(hex, strlen(hex));
|
||||||
|
digit_to_str(hex, res[2], 16);
|
||||||
|
uart_send(hex, strlen(hex));
|
||||||
|
digit_to_str(hex, res[3], 16);
|
||||||
|
uart_send(hex, strlen(hex));
|
||||||
|
uart_send("\r\n", 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
crc_t uart_commands_prog(void){
|
crc_t uart_commands_prog(error_code* status, int size){
|
||||||
uart_send(OK, 5);
|
char digit;
|
||||||
|
iap_prepare_sectors(status, addr_to_sector(APP_OFFSET), addr_to_sector(APP_OFFSET+size));
|
||||||
|
if(*status != ok){
|
||||||
|
uart_send_err();
|
||||||
|
itoa(*status, &digit, 10);
|
||||||
|
uart_send(&digit, 1);
|
||||||
|
}
|
||||||
|
iap_erase_sectors(status, addr_to_sector(APP_OFFSET), addr_to_sector(APP_OFFSET+size));
|
||||||
|
if(*status != ok){
|
||||||
|
uart_send_err();
|
||||||
|
itoa(*status, &digit, 10);
|
||||||
|
uart_send(&digit, 1);
|
||||||
|
}
|
||||||
|
iap_prepare_sectors(status, addr_to_sector(APP_OFFSET), addr_to_sector(APP_OFFSET+size));
|
||||||
|
if(*status != ok){
|
||||||
|
uart_send_err();
|
||||||
|
itoa(*status, &digit, 10);
|
||||||
|
uart_send(&digit, 1);
|
||||||
|
}
|
||||||
|
uart_send_ok();
|
||||||
return crc_init();
|
return crc_init();
|
||||||
}
|
}
|
||||||
|
|
||||||
error_code 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){
|
||||||
|
char digit;
|
||||||
uart_receive_data(data, size);
|
uart_receive_data(data, size);
|
||||||
crc_t checksum_tmp = crc_init();
|
crc_t checksum_tmp = crc_init();
|
||||||
checksum_tmp = crc_update(checksum_tmp, data, size);
|
checksum_tmp = crc_update(checksum_tmp, data, size);
|
||||||
checksum_tmp = crc_finalize(checksum_tmp);
|
checksum_tmp = crc_finalize(checksum_tmp);
|
||||||
if(checksum_loc == checksum_tmp){
|
if(checksum_loc == checksum_tmp){
|
||||||
int tmp = size;
|
iap_prepare_sectors(status, addr_to_sector(offset), addr_to_sector(offset+size)+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+1);
|
|
||||||
while(tmp >= 4096){
|
|
||||||
iap_copy_to_flash(status, offset/0x1000, &checksum_loc, 4096);
|
|
||||||
if(*status != ok){
|
|
||||||
uart_send(ERR, 6);
|
|
||||||
}
|
|
||||||
tmp = tmp - 4096;
|
|
||||||
}
|
|
||||||
while(tmp >= 1024){
|
|
||||||
iap_copy_to_flash(status, offset/0x1000, &checksum_loc, 1024);
|
|
||||||
if(*status != ok){
|
|
||||||
uart_send(ERR, 6);
|
|
||||||
}
|
|
||||||
tmp = tmp - 1024;
|
|
||||||
}
|
|
||||||
while(tmp >= 512){
|
|
||||||
iap_copy_to_flash(status, offset/0x1000, &checksum_loc, 512);
|
|
||||||
if(*status != ok){
|
|
||||||
uart_send(ERR, 6);
|
|
||||||
}
|
|
||||||
tmp = tmp - 512;
|
|
||||||
}
|
|
||||||
while(tmp >= 256){
|
|
||||||
iap_copy_to_flash(status, offset/0x1000, &checksum_loc, 256);
|
|
||||||
if(*status != ok){
|
|
||||||
uart_send(ERR, 6);
|
|
||||||
}
|
|
||||||
tmp = tmp - 256;
|
|
||||||
}
|
|
||||||
*checksum_global = crc_update(*checksum_global, data, size);
|
*checksum_global = crc_update(*checksum_global, data, size);
|
||||||
|
//verify if size == 4095 if smaller
|
||||||
|
if(size == 0x0400)
|
||||||
|
iap_copy_to_flash(status, (long unsigned int *)offset, data, size);
|
||||||
|
else{
|
||||||
|
iap_copy_to_flash(status, (long unsigned int *)offset, data, 0x400);
|
||||||
|
*checksum_global = crc_finalize(*checksum_global);
|
||||||
|
}
|
||||||
|
if(*status != ok){
|
||||||
|
uart_send_err();
|
||||||
|
itoa(*status, &digit, 10);
|
||||||
|
uart_send(&digit, 1);
|
||||||
|
return *status;
|
||||||
|
}
|
||||||
|
uart_send_ok();
|
||||||
return ok;
|
return ok;
|
||||||
}else{
|
}else{
|
||||||
uart_send(ERR, 6);
|
uart_send_err();
|
||||||
return local_checksum;
|
itoa(*status, &digit, 10);
|
||||||
|
uart_send(&digit, 1);
|
||||||
}
|
}
|
||||||
|
return local_checksum;
|
||||||
}
|
}
|
||||||
|
|
||||||
error_code uart_commands_check(crc_t crc_received, crc_t crc_calculated){
|
error_code uart_commands_check(crc_t crc_received, crc_t crc_calculated){
|
||||||
@ -88,7 +121,9 @@ error_code uart_commands_check(crc_t crc_received, crc_t crc_calculated){
|
|||||||
int uart_parse_command(char *user_input, cmd_t *cmd) {
|
int uart_parse_command(char *user_input, cmd_t *cmd) {
|
||||||
|
|
||||||
//Initialize a simple command (empty, simple, foreground)
|
//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;
|
cmd->argc = -1;
|
||||||
|
|
||||||
//Separate string in different token (i.e. command name + params + &)
|
//Separate string in different token (i.e. command name + params + &)
|
||||||
@ -96,10 +131,6 @@ int uart_parse_command(char *user_input, cmd_t *cmd) {
|
|||||||
//A new element will be added
|
//A new element will be added
|
||||||
cmd->argc += 1;
|
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)
|
//Get the adress of the next token (could be NULL to indicate end of argv)
|
||||||
cmd->argv[cmd->argc] = strtok(user_input, DELIMIERS);
|
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
|
user_input = NULL; //Useless to execute it each time but easier than having two different strtok calls
|
||||||
@ -110,8 +141,20 @@ int uart_parse_command(char *user_input, cmd_t *cmd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
uint32_t uart_string_to_int(const char *str) {
|
uint32_t uart_string_to_int(const char *str) {
|
||||||
// Convert input in port number
|
//char* tmp;
|
||||||
char* tmp;
|
//unsigned long long int hex = strtoull(str, &tmp, 16);
|
||||||
unsigned long long int hex = strtoull(str, &tmp, 16);
|
unsigned long int hex = strtoul(str, NULL, 16);
|
||||||
return hex;
|
return hex;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void uart_send_ok(){
|
||||||
|
uart_send(OK, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
void uart_send_err(){
|
||||||
|
uart_send(ERR, 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
void uart_send_cmd_illeg(){
|
||||||
|
uart_send(CMD_ILLEGAL, 13);
|
||||||
|
}
|
||||||
|
@ -7,7 +7,9 @@
|
|||||||
#include "crc.h"
|
#include "crc.h"
|
||||||
|
|
||||||
#define DELIMIERS ","
|
#define DELIMIERS ","
|
||||||
#define APP_OFFSET 4
|
#define LPC1769_FLASH_SIZE 0x80000
|
||||||
|
#define APP_OFFSET 0x4000
|
||||||
|
#define MAX_PROG_SIZE (LPC1769_FLASH_SIZE - APP_OFFSET)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -18,11 +20,12 @@ 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);
|
||||||
|
|
||||||
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);
|
error_code uart_commands_data(error_code* status, int size, crc_t* checksum_global, crc_t checksum_loc, int offset);
|
||||||
|
|
||||||
@ -30,4 +33,13 @@ error_code uart_commands_check(crc_t crc_received, crc_t crc_calculated);
|
|||||||
|
|
||||||
int uart_parse_command(char *user_input, cmd_t *cmd);
|
int uart_parse_command(char *user_input, cmd_t *cmd);
|
||||||
|
|
||||||
|
void uart_send_ok();
|
||||||
|
|
||||||
|
void uart_send_err();
|
||||||
|
|
||||||
|
void uart_send_cmd_illeg();
|
||||||
|
|
||||||
|
|
||||||
|
void digit_to_str(char* str, uint32_t number, int base);
|
||||||
|
|
||||||
uint32_t uart_string_to_int(const char* str);
|
uint32_t uart_string_to_int(const char* str);
|
||||||
|
Binary file not shown.
Reference in New Issue
Block a user