- working SREC

git-svn-id: http://moon:8086/svn/projects/HendiControl@51 fda53097-d464-4ada-af97-ba876c37ca34
This commit is contained in:
2019-03-03 12:34:46 +00:00
parent 9776a64598
commit 2c6927ff43
7 changed files with 457 additions and 15 deletions
+137 -2
View File
@@ -5,14 +5,149 @@
* Author : Jens
*/
#include "machine.h"
#include <ctype.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/boot.h>
#include <util/delay.h>
#include "uart.h"
#include "srec.h"
int main(void)
void (*startApplication)( void ) = 0x0000; /* Funktionspointer auf 0x0000 */
void (*startBootloader)( void ) = 0x7800; /* Funktionspointer auf 0x0000 */
inline void bootloader_enter()
{
/* Replace with your application code */
/* Interrupt Vektoren verbiegen */
char sregtemp = SREG;
char temp = MCUCR;
MCUCR = temp | (1<<IVCE);
MCUCR = temp | (1<<IVSEL);
SREG = sregtemp;
}
inline void bootloader_exit()
{
/* Interrupt Vektoren wieder gerade biegen */
char temp = MCUCR;
MCUCR = temp | (1<<IVCE);
MCUCR = temp & ~(1<<IVSEL);
/* Return to address 0x0000 */
startApplication();
}
typedef enum _eState
{
StateIdle = 0,
StateHex,
} State;
const char *stateStr[2] = {"Idle", "Hex"};
const char *srecStr[] = {"srec_err", "srec_sob", "srec_data", "srec_rec", "srec_eob"};
int main(void)
{
uint8_t page_buffer[SPM_PAGESIZE];
uint8_t hex_buffer[64];
State state = StateIdle;
State state_next;
srec_t srec;
cli();
bootloader_enter();
uart_init(115200);
uart_puts("Hallo, Welt!\n");
size_t image_size = 0;
uint16_t page_addr = 0;
int page_isEmpty = 1;
size_t hex_buffer_size = 0;
while (1)
{
char c;
if ((c = uart_getc()))
{
char C = toupper(c);
state_next = state;
switch(state)
{
case StateIdle:
if (C == 'S')
{
hex_buffer_size = 0;
state_next = StateHex;
hex_buffer[hex_buffer_size++] = C;
}
if (C == 'R')
{
startBootloader();
}
if (C == 'Q')
{
startApplication();
}
break;
case StateHex:
if(((C != 0x0D) && (C != 0x0A)))
{
hex_buffer[hex_buffer_size++] = C;
}
else
{
int srec_state = srec_decode(&srec, hex_buffer, hex_buffer_size);
uart_puts(" : "); uart_puts(srecStr[srec_state]); uart_puts("\n");
hex_buffer_size = 0;
switch(srec_state)
{
case srec_sob:
image_size = 0;
page_isEmpty = 1;
break;
case srec_data:
{
if ((srec.addr + srec.size) > image_size)
image_size = srec.addr + srec.size;
if (page_isEmpty)
{
page_isEmpty = 0;
page_addr = srec.addr;
}
uart_puts("SREC.ADDR: "); print_word(srec.addr); uart_puts("\n");
}
break;
case srec_rec:
break;
case srec_eob:
break;
case srec_err:
startBootloader();
break;
}
}
break;
default:
break;
}
if (state_next != state)
{
uart_puts("State: "); uart_puts(stateStr[state]); uart_puts(" => "); uart_puts(stateStr[state_next]); uart_puts("\n");
}
state = state_next;
uart_putc(c);
}
}
uart_puts("Exit bootloader!\n");
bootloader_exit();
}