- 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
@@ -20,13 +20,13 @@
<OverrideVtor>false</OverrideVtor> <OverrideVtor>false</OverrideVtor>
<CacheFlash>true</CacheFlash> <CacheFlash>true</CacheFlash>
<ProgFlashFromRam>true</ProgFlashFromRam> <ProgFlashFromRam>true</ProgFlashFromRam>
<RamSnippetAddress /> <RamSnippetAddress>0x20000000</RamSnippetAddress>
<UncachedRange /> <UncachedRange />
<preserveEEPROM>true</preserveEEPROM> <preserveEEPROM>true</preserveEEPROM>
<OverrideVtorValue /> <OverrideVtorValue>exception_table</OverrideVtorValue>
<BootSegment>2</BootSegment> <BootSegment>2</BootSegment>
<ResetRule>0</ResetRule> <ResetRule>0</ResetRule>
<eraseonlaunchrule>0</eraseonlaunchrule> <eraseonlaunchrule>1</eraseonlaunchrule>
<EraseKey /> <EraseKey />
<AsfFrameworkConfig> <AsfFrameworkConfig>
<framework-data xmlns=""> <framework-data xmlns="">
@@ -40,6 +40,20 @@
</dependencies> </dependencies>
</framework-data> </framework-data>
</AsfFrameworkConfig> </AsfFrameworkConfig>
<avrtool>com.atmel.avrdbg.tool.atmelice</avrtool>
<avrtoolserialnumber>J41800089261</avrtoolserialnumber>
<avrdeviceexpectedsignature>0x1E950F</avrdeviceexpectedsignature>
<com_atmel_avrdbg_tool_atmelice>
<ToolOptions>
<InterfaceProperties>
</InterfaceProperties>
<InterfaceName>debugWIRE</InterfaceName>
</ToolOptions>
<ToolType>com.atmel.avrdbg.tool.atmelice</ToolType>
<ToolNumber>J41800089261</ToolNumber>
<ToolName>Atmel-ICE</ToolName>
</com_atmel_avrdbg_tool_atmelice>
<avrtoolinterface>debugWIRE</avrtoolinterface>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<ToolchainSettings> <ToolchainSettings>
@@ -55,6 +69,7 @@
<avrgcc.compiler.symbols.DefSymbols> <avrgcc.compiler.symbols.DefSymbols>
<ListValues> <ListValues>
<Value>NDEBUG</Value> <Value>NDEBUG</Value>
<Value>ARDUINO_NANO=1</Value>
</ListValues> </ListValues>
</avrgcc.compiler.symbols.DefSymbols> </avrgcc.compiler.symbols.DefSymbols>
<avrgcc.compiler.directories.IncludePaths> <avrgcc.compiler.directories.IncludePaths>
@@ -71,6 +86,11 @@
<Value>libm</Value> <Value>libm</Value>
</ListValues> </ListValues>
</avrgcc.linker.libraries.Libraries> </avrgcc.linker.libraries.Libraries>
<avrgcc.linker.memorysettings.Flash>
<ListValues>
<Value>.text=0x3C00</Value>
</ListValues>
</avrgcc.linker.memorysettings.Flash>
<avrgcc.assembler.general.IncludePaths> <avrgcc.assembler.general.IncludePaths>
<ListValues> <ListValues>
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.2.209\include</Value> <Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.2.209\include</Value>
@@ -93,6 +113,7 @@
<avrgcc.compiler.symbols.DefSymbols> <avrgcc.compiler.symbols.DefSymbols>
<ListValues> <ListValues>
<Value>DEBUG</Value> <Value>DEBUG</Value>
<Value>ARDUINO_NANO=1</Value>
</ListValues> </ListValues>
</avrgcc.compiler.symbols.DefSymbols> </avrgcc.compiler.symbols.DefSymbols>
<avrgcc.compiler.directories.IncludePaths> <avrgcc.compiler.directories.IncludePaths>
@@ -100,7 +121,7 @@
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.2.209\include</Value> <Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.2.209\include</Value>
</ListValues> </ListValues>
</avrgcc.compiler.directories.IncludePaths> </avrgcc.compiler.directories.IncludePaths>
<avrgcc.compiler.optimization.level>Optimize (-O1)</avrgcc.compiler.optimization.level> <avrgcc.compiler.optimization.level>Optimize debugging experience (-Og)</avrgcc.compiler.optimization.level>
<avrgcc.compiler.optimization.PackStructureMembers>True</avrgcc.compiler.optimization.PackStructureMembers> <avrgcc.compiler.optimization.PackStructureMembers>True</avrgcc.compiler.optimization.PackStructureMembers>
<avrgcc.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcc.compiler.optimization.AllocateBytesNeededForEnum> <avrgcc.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcc.compiler.optimization.AllocateBytesNeededForEnum>
<avrgcc.compiler.optimization.DebugLevel>Default (-g2)</avrgcc.compiler.optimization.DebugLevel> <avrgcc.compiler.optimization.DebugLevel>Default (-g2)</avrgcc.compiler.optimization.DebugLevel>
@@ -110,6 +131,11 @@
<Value>libm</Value> <Value>libm</Value>
</ListValues> </ListValues>
</avrgcc.linker.libraries.Libraries> </avrgcc.linker.libraries.Libraries>
<avrgcc.linker.memorysettings.Flash>
<ListValues>
<Value>.text=0x3C00</Value>
</ListValues>
</avrgcc.linker.memorysettings.Flash>
<avrgcc.assembler.general.IncludePaths> <avrgcc.assembler.general.IncludePaths>
<ListValues> <ListValues>
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.2.209\include</Value> <Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.2.209\include</Value>
@@ -120,9 +146,24 @@
</ToolchainSettings> </ToolchainSettings>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Include="machine.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="main.c"> <Compile Include="main.c">
<SubType>compile</SubType> <SubType>compile</SubType>
</Compile> </Compile>
<Compile Include="srec.c">
<SubType>compile</SubType>
</Compile>
<Compile Include="srec.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="uart.c">
<SubType>compile</SubType>
</Compile>
<Compile Include="uart.h">
<SubType>compile</SubType>
</Compile>
</ItemGroup> </ItemGroup>
<Import Project="$(AVRSTUDIO_EXE_PATH)\\Vs\\Compiler.targets" /> <Import Project="$(AVRSTUDIO_EXE_PATH)\\Vs\\Compiler.targets" />
</Project> </Project>
@@ -0,0 +1,21 @@
/*
* machine.h
*
* Created: 03.03.2019 09:21:56
* Author: Jens
*/
#ifndef MACHINE_H_
#define MACHINE_H_
#if ARDUINO_NANO
#define F_CPU (16000000UL) // MHz
#else
#define F_CPU (18432000UL) // MHz
#endif
#endif /* MACHINE_H_ */
+136 -1
View File
@@ -5,14 +5,149 @@
* Author : Jens * Author : Jens
*/ */
#include "machine.h"
#include <ctype.h>
#include <avr/io.h> #include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/boot.h>
#include <util/delay.h>
#include "uart.h"
#include "srec.h"
void (*startApplication)( void ) = 0x0000; /* Funktionspointer auf 0x0000 */
void (*startBootloader)( void ) = 0x7800; /* Funktionspointer auf 0x0000 */
inline void bootloader_enter()
{
/* 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) int main(void)
{ {
/* Replace with your application code */ 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) 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();
} }
@@ -0,0 +1,78 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
#include "srec.h"
uint8_t h2i(uint8_t *c)
{
int i;
uint8_t t, b = 0;
for (i=0; i < 2; i++)
{
t = c[i];
if (t >= 'A')
t = t - 'A' + 10;
else
t -= '0';
b = (b << 4) | t;
}
return b;
}
int srec_decode(srec_t *pRec, uint8_t *pBuf, int buflen)
{
uint8_t chksum, byte;
int alen, i;
if (!buflen)
return srec_err;
pRec->type = srec_rec;
alen = 0;
if ((pBuf[1] > '0') && (pBuf[1] < '4'))
{
alen = pBuf[1] - '0' + 1;
pRec->type = srec_data;
}
else if ((pBuf[1] >= '7') && (pBuf[1] <= '9'))
{
alen = 11 - (pBuf[1] - '0');
pRec->type = srec_eob;
}
else if (pBuf[1] == '0')
{
alen = 2;
pRec->type = srec_sob;
}
byte = h2i(&pBuf[2]);
pRec->size = byte;
chksum = byte;
pRec->addr = 0;
for (i=0; i < alen; i++)
{
byte = h2i(&pBuf[4+2*i]);
pRec->addr = pRec->addr << 8 | byte;
chksum += byte;
}
pRec->size -= (alen+1);
pRec->data = (uint8_t*) &pBuf[4 + 2*alen];
for (i=0; i < (int)pRec->size; i++)
{
byte = h2i(&pRec->data[2*i]);
pRec->data[i] = byte;
chksum += byte;
}
chksum = ~chksum;
byte = h2i(&pRec->data[2*i]);
if (chksum != byte)
return srec_err;
return pRec->type;
}
@@ -0,0 +1,45 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: srec.h
* Author: jens
*
* Created on 22. Januar 2017, 14:10
*/
#ifndef SREC_H
#define SREC_H
#include <stdint.h>
typedef struct _ssrec_t
{
uint32_t addr;
uint32_t size;
uint32_t type;
uint8_t *data;
} srec_t;
enum srec_type
{
srec_err, srec_sob, srec_data, srec_rec, srec_eob
};
#ifdef __cplusplus
extern "C" {
#endif
int srec_decode(srec_t *pRec, uint8_t *pBuf, int buflen);
int srec_getline(uint8_t *pLine);
#ifdef __cplusplus
}
#endif
#endif /* SREC_H */
@@ -0,0 +1,69 @@
/*
* uart.c
*
* Created: 20.02.2019 20:50:13
* Author: jens
*/
#include <stdlib.h>
#include <avr/io.h>
#include "uart.h"
void uart_init(uint32_t baudrate)
{
uint16_t prescale8 = UART_PRESCALE(baudrate, 8UL);
uint16_t prescale16 = UART_PRESCALE(baudrate, 16UL);
uint32_t baud8 = ((prescale8+1) * 8);
uint32_t baud16 = ((prescale16+1) * 16);
uint32_t err8 = abs(baudrate - baud8);
uint32_t err16 = abs(baudrate - baud16);
uint16_t prescale = prescale16;
if (err8 < err16)
{
prescale = prescale8;
// Double UART speed
UCSR0A |= 1 << U2X0;
}
// Set baud rate
UBRR0L = (uint8_t)prescale;
UBRR0H = (uint8_t)(prescale >> 8);
// Enable receiver and transmitter
UCSR0B = (1<<TXEN0)|(1<<RXEN0);
}
void uart_putc(char c)
{
while((UCSR0A & (1<<UDRE0)) == 0)
{
}
UDR0 = c;
}
char uart_getc()
{
while ((UCSR0A & (1 << RXC0)) == 0)
{
}
return UDR0;
}
void uart_puts(char const *str)
{
while(*str)
{
char c = *(str++);
uart_putc(c);
if (c == 0x0A)
{
uart_putc(0x0D);
}
}
}
@@ -0,0 +1,53 @@
/*
* uart.h
*
* Created: 20.02.2019 20:50:01
* Author: jens
*/
#ifndef UART_H_
#define UART_H_
#include "machine.h"
#define UART_PRESCALE(baudrate, smpPerBit) ((5+ 10*F_CPU / (baudrate*smpPerBit) - 1)/10)
void uart_init(uint32_t baudrate);
void uart_putc(char c);
char uart_getc(void);
void uart_puts(char const *str);
inline void print_byte(char byte)
{
int i;
unsigned char c, nibble;
for (i=0; i < 2; i++)
{
nibble = (char)((byte >> 4) & 0xF);
byte <<= 4;
if (nibble < 10)
c = nibble + '0';
else
c = nibble + 'A' - 10;
uart_putc(c);
}
}
inline void print_word(uint16_t word)
{
int i;
unsigned char c;
for (i=0; i < 2; i++)
{
c = (char) (word >> 8);
print_byte(c);
word <<= 8;
}
}
#endif /* UART_H_ */