Skip to content
Snippets Groups Projects
Commit 01a421dd authored by pplew's avatar pplew
Browse files

Bootloader improvements

parent 1a6cf60f
No related branches found
No related tags found
No related merge requests found
No preview for this file type
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "timer_lib.h" #include "timer_lib.h"
#include "usart_lib.h" #include "usart_lib.h"
#include <avr/eeprom.h> #include <avr/eeprom.h>
#include <util/delay.h>
#include <asf.h> #include <asf.h>
#include <ccp.h> #include <ccp.h>
...@@ -34,7 +35,7 @@ void start_firmware() ...@@ -34,7 +35,7 @@ void start_firmware()
void start_bootloader() void start_bootloader()
{ {
uint8_t chr = 0, temp, error_counter = 0; uint8_t chr = 0, error_counter = 0;
bool err = false, escape = false; bool err = false, escape = false;
while(true) while(true)
{ {
...@@ -60,6 +61,7 @@ void start_bootloader() ...@@ -60,6 +61,7 @@ void start_bootloader()
usart_putchar(USART_SERIAL, BOOTLDR_RES_OK); usart_putchar(USART_SERIAL, BOOTLDR_RES_OK);
usart_putchar(USART_SERIAL, handshake); usart_putchar(USART_SERIAL, handshake);
timer_reset_counter(); timer_reset_counter();
eeprom_write_upgrade_started();
state = DATA; state = DATA;
break; break;
...@@ -193,6 +195,7 @@ void eeprom_write_upgrade( void ) ...@@ -193,6 +195,7 @@ void eeprom_write_upgrade( void )
void eeprom_write_ready( void ) void eeprom_write_ready( void )
{ {
eeprom_write_byte(EEPROM_ADDR_FWUPGRADE, EEPROM_FWREADY_VALUE); eeprom_write_byte(EEPROM_ADDR_FWUPGRADE, EEPROM_FWREADY_VALUE);
_delay_ms(40);
} }
bool eeprom_read_upgrade( void ) bool eeprom_read_upgrade( void )
...@@ -209,3 +212,13 @@ bool read_compare_checksum(void) ...@@ -209,3 +212,13 @@ bool read_compare_checksum(void)
usart_serial_getchar(USART_SERIAL, &chr); usart_serial_getchar(USART_SERIAL, &chr);
return (chr == checksum); return (chr == checksum);
} }
void eeprom_write_upgrade_started( void )
{
eeprom_write_byte(EEPROM_ADDR_FWUPGRADE, EEPROM_FWUPGRADE_STARTED);
}
bool eeprom_is_pending( void )
{
return eeprom_read_byte(EEPROM_ADDR_FWUPGRADE) == EEPROM_FWUPGRADE_STARTED;
}
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include <stdint.h> #include <stdint.h>
typedef enum {INIT, HANDSHAKE, DATA, FINISH} BootloaderState; typedef enum {INIT, HANDSHAKE, DATA, FINISH} BootloaderState;
extern BootloaderState state;
#define BOOTLDR_ESCAPE 0x01 #define BOOTLDR_ESCAPE 0x01
#define BOOTLDR_HANDSHAKE 0x02 #define BOOTLDR_HANDSHAKE 0x02
...@@ -29,13 +30,16 @@ typedef enum {INIT, HANDSHAKE, DATA, FINISH} BootloaderState; ...@@ -29,13 +30,16 @@ typedef enum {INIT, HANDSHAKE, DATA, FINISH} BootloaderState;
#define EEPROM_ADDR_FWUPGRADE (uint8_t *) 63 #define EEPROM_ADDR_FWUPGRADE (uint8_t *) 63
#define EEPROM_FWUPGRADE_VALUE (uint8_t) 0x56 #define EEPROM_FWUPGRADE_VALUE (uint8_t) 0x56
#define EEPROM_FWREADY_VALUE (uint8_t) 0x55 #define EEPROM_FWREADY_VALUE (uint8_t) 0x55
#define EEPROM_FWUPGRADE_STARTED (uint8_t) 0x44
void start_bootloader(void); void start_bootloader(void);
void start_firmware(void); void start_firmware(void);
void eeprom_write_upgrade(void); void eeprom_write_upgrade(void);
void eeprom_write_upgrade_started(void);
void eeprom_write_ready(void); void eeprom_write_ready(void);
bool eeprom_read_upgrade(void); bool eeprom_read_upgrade(void);
bool eeprom_is_pending(void);
void checksum_add_byte(uint8_t byte); void checksum_add_byte(uint8_t byte);
bool read_compare_checksum(void); bool read_compare_checksum(void);
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include "timer_lib.h" #include "timer_lib.h"
#include "usart_lib.h" #include "usart_lib.h"
#include "bootloader.h"
#define PERIOD_10MS 1250 #define PERIOD_10MS 1250
#define PERIOD_100MS 12500 #define PERIOD_100MS 12500
...@@ -54,11 +55,15 @@ static void tc_overflow_callback(void) //100 ms ...@@ -54,11 +55,15 @@ static void tc_overflow_callback(void) //100 ms
ioport_toggle_pin(MMC_LED); ioport_toggle_pin(MMC_LED);
sec_counter = 0; sec_counter = 0;
} }
//RESET after 20 s of inactivity //RESET after 10 s of inactivity
if (idle_counter == 200) if (idle_counter == INACTIVITY_BEFORE_RESET_S*10)
{ {
//printString("Reset");
//reset processor //reset processor
if (state == INIT || state == HANDSHAKE)
{
if (!eeprom_is_pending())
eeprom_write_ready();
}
ccp_write_io((void *)&RST.CTRL, RST_SWRST_bm); ccp_write_io((void *)&RST.CTRL, RST_SWRST_bm);
} }
} }
......
...@@ -28,6 +28,8 @@ ...@@ -28,6 +28,8 @@
#ifndef TIMER_LIB_H_ #ifndef TIMER_LIB_H_
#define TIMER_LIB_H_ #define TIMER_LIB_H_
#define INACTIVITY_BEFORE_RESET_S 10
extern volatile bool timer_100ms_flag; extern volatile bool timer_100ms_flag;
void timer_init(void); void timer_init(void);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment