Revision 199
| trunk/bootloader/bootloader.h (revision 199) | ||
|---|---|---|
| 10 | 10 |
#include "packet.h" |
| 11 | 11 |
|
| 12 | 12 |
/** @brief This is the address of the tool. Change this pre program instance */ |
| 13 |
#define ADDR 18 |
|
| 13 |
//#define ADDR 18 |
|
| 14 | 14 |
|
| 15 | 15 |
#endif |
| trunk/bootloader/packet.c (revision 199) | ||
|---|---|---|
| 1 |
#include <tooltron.h> |
|
| 2 | 1 |
#include <packet.h> |
| 3 | 2 |
|
| 4 | 3 |
|
| ... | ... | |
| 32 | 31 |
* |
| 33 | 32 |
* @pre mbuf is PROGD_PACKET_SIZE bytes large |
| 34 | 33 |
* @param mbuf Buffer to write the packet data payload to if there is one |
| 34 |
* @param addr The address of the sending node |
|
| 35 | 35 |
* @return the command received or TT_BAD on any error |
| 36 | 36 |
*/ |
| 37 |
char parse_packet(uint8_t *mbuf) {
|
|
| 37 |
char parse_packet(uint8_t *mbuf, uint8_t addr) {
|
|
| 38 | 38 |
uint8_t r; // Byte from the network |
| 39 | 39 |
uint8_t crc; // Running checksum of the packet |
| 40 | 40 |
uint8_t cmd; // The command received |
| ... | ... | |
| 80 | 80 |
case dest: |
| 81 | 81 |
if (r == DELIM) {
|
| 82 | 82 |
state = src; |
| 83 |
} else if (r == ADDR) {
|
|
| 83 |
} else if (r == addr) {
|
|
| 84 | 84 |
crc ^= r; |
| 85 | 85 |
state = comd; |
| 86 | 86 |
} else {
|
| ... | ... | |
| 134 | 134 |
/** |
| 135 | 135 |
* @brief Sends a packet of type cmd onto the network |
| 136 | 136 |
* @param cmd The command to send |
| 137 |
* @param addr The address of the sending node |
|
| 137 | 138 |
*/ |
| 138 |
void send_packet(uint8_t cmd) {
|
|
| 139 |
void send_packet(uint8_t cmd, uint8_t addr) {
|
|
| 139 | 140 |
rs485_send_byte(DELIM); |
| 140 |
rs485_send_byte(ADDR); |
|
| 141 |
rs485_send_byte(addr); |
|
| 141 | 142 |
rs485_send_byte(SERVER); |
| 142 | 143 |
rs485_send_byte(cmd); |
| 143 |
rs485_send_byte(ACK_CRC ^ cmd); |
|
| 144 |
rs485_send_byte(addr ^ SERVER ^ cmd); |
|
| 144 | 145 |
} |
| 145 | 146 |
|
| trunk/bootloader/bootloader.c (revision 199) | ||
|---|---|---|
| 10 | 10 |
}; |
| 11 | 11 |
*/ |
| 12 | 12 |
|
| 13 |
|
|
| 14 |
|
|
| 15 | 13 |
// Error thresholds |
| 16 | 14 |
#define MAX_RETRIES 5 // Number of times to retry before giving up |
| 17 | 15 |
|
| ... | ... | |
| 59 | 57 |
uint16_t prog_len; |
| 60 | 58 |
uint8_t i; |
| 61 | 59 |
uint8_t retries; |
| 60 |
uint8_t addr; |
|
| 62 | 61 |
|
| 63 | 62 |
retry_jpnt: |
| 64 | 63 |
iteration = 0; |
| 65 | 64 |
retries = 0; |
| 65 |
addr = eeprom_read_byte(EEPROM_ADDR); |
|
| 66 | 66 |
|
| 67 | 67 |
// Clear the watchdog timer |
| 68 | 68 |
MCUSR &= ~_BV(WDRF); |
| 69 | 69 |
wdt_disable(); |
| 70 | 70 |
WDTCSR = 0; |
| 71 | 71 |
|
| 72 |
|
|
| 73 | 72 |
rs485_init(BAUD9600); |
| 74 | 73 |
|
| 75 | 74 |
//set LED pin as output |
| ... | ... | |
| 77 | 76 |
PORTB = 0x07; |
| 78 | 77 |
|
| 79 | 78 |
//Start bootloading process |
| 80 |
send_packet(TT_BOOT); |
|
| 79 |
send_packet(TT_BOOT, addr); |
|
| 81 | 80 |
|
| 82 |
resp = parse_packet(mbuf); |
|
| 81 |
resp = parse_packet(mbuf, addr); |
|
| 83 | 82 |
|
| 84 | 83 |
// Enter programming mode |
| 85 | 84 |
if (resp == TT_PROGM) {
|
| ... | ... | |
| 98 | 97 |
main_start(); |
| 99 | 98 |
} |
| 100 | 99 |
|
| 101 |
send_packet(TT_ACK); |
|
| 100 |
send_packet(TT_ACK, addr); |
|
| 102 | 101 |
|
| 103 | 102 |
while(1) {
|
| 104 |
resp = parse_packet(mbuf); |
|
| 103 |
resp = parse_packet(mbuf, addr); |
|
| 105 | 104 |
|
| 106 | 105 |
if (resp == TT_PROGD) {
|
| 107 | 106 |
// We need to muck with the reset vector jump in the first page |
| ... | ... | |
| 129 | 128 |
caddr += PROGD_PACKET_SIZE; |
| 130 | 129 |
retries = 0; |
| 131 | 130 |
} else {
|
| 132 |
send_packet(TT_NACK); |
|
| 131 |
send_packet(TT_NACK, addr); |
|
| 133 | 132 |
retries++; |
| 134 | 133 |
|
| 135 | 134 |
// If we failed too many times, reset. This goes to the start |
| ... | ... | |
| 139 | 138 |
} |
| 140 | 139 |
} |
| 141 | 140 |
|
| 142 |
send_packet(TT_ACK); |
|
| 141 |
send_packet(TT_ACK, addr); |
|
| 143 | 142 |
|
| 144 | 143 |
// Once we write the last packet we must override the jump to |
| 145 | 144 |
// user code to point to the correct address |
| trunk/bootloader/packet.h (revision 199) | ||
|---|---|---|
| 1 | 1 |
#ifndef _PACKET_H_ |
| 2 | 2 |
#define _PACKET_H_ |
| 3 | 3 |
|
| 4 |
#include "packet.h" |
|
| 4 |
#include <tooltron.h> |
|
| 5 |
#include <avr/eeprom.h> |
|
| 5 | 6 |
#include "rs485_poll.h" |
| 6 | 7 |
#include "bootloader.h" |
| 7 | 8 |
|
| 8 | 9 |
/** @brief Max time to wait until we exit the packet handler */ |
| 9 | 10 |
#define MAX_TIMEOUT 60000 |
| 10 | 11 |
|
| 11 |
char parse_packet(uint8_t *mbuf); |
|
| 12 |
void send_packet(uint8_t cmd); |
|
| 12 |
char parse_packet(uint8_t *mbuf, uint8_t addr); |
|
| 13 |
void send_packet(uint8_t cmd, uint8_t addr); |
|
| 13 | 14 |
|
| 14 | 15 |
|
| 15 | 16 |
#endif |
Also available in: Unified diff