3 This file is part of the MSP Märklin suite
4 Copyright © 2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
7 Firmware for wireless S88 receiver module
27 #include <avr/interrupt.h>
32 #define DATA_OUT PORTD2
37 #define BIT(n) (1<<(n))
39 void receive(uint8_t);
40 uint8_t hexdigit(uint8_t);
41 uint8_t decode_hex(uint8_t);
43 volatile uint8_t rx_buf[7];
44 volatile uint8_t rx_fill = 0xFF;
45 volatile uint8_t input[128] = { 0 };
46 volatile uint8_t latch[128] = { 0 };
52 uint8_t clock_high = 0;
58 DDRD = 0x06; // 00000110
59 PORTD = 0xC0; // 11000000
60 DDRB = 0x20; // 00100000
61 PORTB = 0x1F; // 00011111
100 PORTD |= BIT(DATA_OUT);
102 PORTD &= ~BIT(DATA_OUT);
110 if(d_pins&BIT(RESET))
119 void receive(uint8_t c)
134 offset = (decode_hex(rx_buf[0])<<8) | (decode_hex(rx_buf[1])<<4) | decode_hex(rx_buf[2]);
135 nibbles = (offset&3);
137 if(rx_fill>3+nibbles)
139 for(i=0; i<=nibbles; ++i)
141 uint16_t j = offset+nibbles-i;
142 uint8_t shift = 4*(j&1);
143 uint8_t bits = decode_hex(rx_buf[3+i]);
144 input[j/2] = (input[j/2]&~(0xF<<shift)) | (bits<<shift);
145 latch[j/2] = input[j/2];
148 lcd_gotoxy(19-offset-nibbles, 0);
149 for(i=0; i<=nibbles; ++i)
150 lcd_write(rx_buf[3+i]);
157 if(rx_fill<sizeof(rx_buf))
158 rx_buf[rx_fill++] = c;
163 lcd_gotoxy(log_col, 1+log_row);
172 lcd_gotoxy(log_col, 1+log_row);
177 SERIAL_SET_CALLBACK(receive)
179 uint8_t hexdigit(uint8_t n)
188 uint8_t decode_hex(uint8_t c)
192 else if(c>='A' && c<='F')