Revision 184 trunk/bootloader/bootloader.c
| bootloader.c (revision 184) | ||
|---|---|---|
| 2 | 2 |
#include <avr/boot.h> |
| 3 | 3 |
#include <avr/wdt.h> |
| 4 | 4 |
#include <tooltron.h> |
| 5 |
#include "uart.h" |
|
| 5 |
#include "rs485_sw.h" |
|
| 6 | 6 |
|
| 7 | 7 |
#define ADDR 18 |
| 8 | 8 |
|
| ... | ... | |
| 23 | 23 |
int16_t sword; |
| 24 | 24 |
} rjump_t; |
| 25 | 25 |
|
| 26 |
void init_uart(uint16_t baud) {
|
|
| 27 |
// Set baud rate |
|
| 28 |
UBRRH = (uint8_t)(baud>>8); |
|
| 29 |
UBRRL = (uint8_t)baud; |
|
| 30 |
|
|
| 31 |
// Enable RX/TX |
|
| 32 |
UCSRB = _BV(RXEN) | _BV(TXEN); |
|
| 33 |
|
|
| 34 |
// Enable the TXEN pin as output |
|
| 35 |
DDRD |= TX_EN; |
|
| 36 |
uart_toggle_transmit(UART_TX_OFF); |
|
| 37 |
} |
|
| 38 | 26 |
|
| 39 |
int8_t uart_get_byte(uint8_t *output_byte) {
|
|
| 40 |
if (UCSRA & _BV(RXC)) {
|
|
| 41 |
*output_byte = UDR; |
|
| 42 |
return 0; |
|
| 43 |
} else {
|
|
| 44 |
return -1; |
|
| 45 |
} |
|
| 46 |
} |
|
| 47 |
|
|
| 48 |
void uart_send_byte(uint8_t data) {
|
|
| 49 |
//Waits until current transmit is done |
|
| 50 |
while (!(UCSRA & _BV(UDRE))); |
|
| 51 |
|
|
| 52 |
// Enable writes and send |
|
| 53 |
uart_toggle_transmit(UART_TX_ON); |
|
| 54 |
UDR = data; |
|
| 55 |
|
|
| 56 |
// Waits until the transmit is done |
|
| 57 |
while(!(UCSRA & _BV(TXC))); |
|
| 58 |
uart_toggle_transmit(UART_TX_OFF); |
|
| 59 |
UCSRA |= _BV(TXC); |
|
| 60 |
|
|
| 61 |
return; |
|
| 62 |
} |
|
| 63 |
|
|
| 64 |
void uart_toggle_transmit(uint8_t state) {
|
|
| 65 |
if (state == UART_TX_ON) {
|
|
| 66 |
PORTD |= TX_EN; |
|
| 67 |
} else {
|
|
| 68 |
PORTD &= ~TX_EN; |
|
| 69 |
} |
|
| 70 |
} |
|
| 71 |
|
|
| 72 | 27 |
char parse_packet(uint8_t *mbuf) {
|
| 73 | 28 |
uint8_t r; // Byte from the network |
| 74 | 29 |
uint8_t crc; // Running checksum of the packet |
| ... | ... | |
| 88 | 43 |
|
| 89 | 44 |
while (1) {
|
| 90 | 45 |
// Wait for the next byte |
| 91 |
while ((uart_get_byte(&r)) < 0) {
|
|
| 46 |
while ((rs485_get_byte(&r)) < 0) {
|
|
| 92 | 47 |
if (count >= MAX_TIMEOUT) {
|
| 93 | 48 |
return TT_BAD; |
| 94 | 49 |
} else {
|
| ... | ... | |
| 167 | 122 |
} |
| 168 | 123 |
|
| 169 | 124 |
void send_packet(uint8_t cmd) {
|
| 170 |
uart_send_byte(DELIM); |
|
| 171 |
uart_send_byte(ADDR); |
|
| 172 |
uart_send_byte(SERVER); |
|
| 173 |
uart_send_byte(cmd); |
|
| 174 |
uart_send_byte(ACK_CRC ^ cmd); |
|
| 125 |
rs485_send_byte(DELIM); |
|
| 126 |
rs485_send_byte(ADDR); |
|
| 127 |
rs485_send_byte(SERVER); |
|
| 128 |
rs485_send_byte(cmd); |
|
| 129 |
rs485_send_byte(ACK_CRC ^ cmd); |
|
| 175 | 130 |
} |
| 176 | 131 |
|
| 177 | 132 |
// SPM_PAGESIZE is set to 32 bytes |
| ... | ... | |
| 210 | 165 |
WDTCSR = 0; |
| 211 | 166 |
|
| 212 | 167 |
|
| 213 |
init_uart(51); //MAGIC NUMBER?? |
|
| 168 |
rs485_init(51); //MAGIC NUMBER?? |
|
| 214 | 169 |
|
| 215 | 170 |
//set LED pin as output |
| 216 | 171 |
LED_DDR |= 0x07; |
Also available in: Unified diff