]> git.tdb.fi Git - model-railway-devices.git/blob - firmware/s88w-r.c
730ac10f3a5337ba2ec31e51ef79a3e8fbac27b6
[model-railway-devices.git] / firmware / s88w-r.c
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6
7 Firmware for wireless S88 receiver module
8
9 S88 pinout:
10 1 - DATA
11 2 - GND
12 3 - CLOCK
13 4 - LOAD
14 5 - RESET
15 6 - POWER
16
17 ATMega pinout:
18 D0 - serial RX
19 D1 - serial TX
20 D2 - S88 DATA
21 D3 - S88 CLOCK
22 D4 - S88 LOAD
23 D5 - S88 RESET
24 */
25
26 #include <avr/io.h>
27 #include <avr/interrupt.h>
28 #include "lcd.h"
29 #include "serial.h"
30 #include "delay.h"
31
32 #define DATA_OUT PORTD2
33 #define CLOCK    PIND3
34 #define LOAD     PIND4
35 #define RESET    PIND5
36
37 #define BIT(n)   (1<<(n))
38
39 void receive(uint8_t);
40 uint8_t hexdigit(uint8_t);
41 uint8_t decode_hex(uint8_t);
42
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 };
47 uint8_t log_row = 0;
48 uint8_t log_col = 0;
49
50 int main()
51 {
52         uint8_t clock_high = 0;
53         uint8_t bits = 0;
54         uint8_t n_bits = 8;
55         uint8_t offset = 0;
56         uint8_t i;
57
58         DDRD = 0x06;   // 00000110
59         PORTD = 0xC0;  // 11000000
60         DDRB = 0x20;   // 00100000
61         PORTB = 0x1F;  // 00011111
62
63         serial_init(9600);
64         lcd_init();
65
66         sei();
67
68         lcd_clear();
69         for(i=0; i<20; ++i)
70                 lcd_write('0');
71
72         while(1)
73         {
74                 uint8_t d_pins;
75
76                 d_pins = PIND;
77
78                 if(d_pins&BIT(CLOCK))
79                 {
80                         if(!clock_high)
81                         {
82                                 if(d_pins&BIT(LOAD))
83                                 {
84                                         offset = 0;
85                                         bits = latch[0];
86                                         n_bits = 8;
87                                 }
88                                 else
89                                 {
90                                         bits >>= 1;
91                                         if(!--n_bits)
92                                         {
93                                                 ++offset;
94                                                 bits = latch[offset];
95                                                 n_bits = 8;
96                                         }
97                                 }
98
99                                 if(bits&1)
100                                         PORTD |= BIT(DATA_OUT);
101                                 else
102                                         PORTD &= ~BIT(DATA_OUT);
103
104                                 clock_high = 1;
105                         }
106                 }
107                 else if(clock_high)
108                         clock_high = 0;
109
110                 if(d_pins&BIT(RESET))
111                 {
112                         uint8_t i;
113                         for(i=0; i<128; ++i)
114                                 latch[i] = input[i];
115                 }
116         }
117 }
118
119 void receive(uint8_t c)
120 {
121         if(rx_fill==0xFF)
122         {
123                 if(c==':')
124                         rx_fill = 0;
125         }
126         else if(c=='.')
127         {
128                 if(rx_fill>=4)
129                 {
130                         uint16_t offset;
131                         uint8_t nibbles;
132                         uint8_t i;
133
134                         offset = (decode_hex(rx_buf[0])<<8) | (decode_hex(rx_buf[1])<<4) | decode_hex(rx_buf[2]);
135                         nibbles = (offset&3);
136                         offset >>= 2;
137                         if(rx_fill>3+nibbles)
138                         {
139                                 for(i=0; i<=nibbles; ++i)
140                                 {
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];
146                                 }
147
148                                 lcd_gotoxy(19-offset-nibbles, 0);
149                                 for(i=0; i<=nibbles; ++i)
150                                         lcd_write(rx_buf[3+i]);
151                         }
152                 }
153                 rx_fill = 0xFF;
154         }
155         else
156         {
157                 if(rx_fill<sizeof(rx_buf))
158                         rx_buf[rx_fill++] = c;
159                 else
160                         rx_fill = 0xFF;
161         }
162
163         lcd_gotoxy(log_col, 1+log_row);
164         lcd_write(c);
165         ++log_col;
166         if(log_col>=20)
167         {
168                 log_col = 0;
169                 ++log_row;
170                 if(log_row>=3)
171                         log_row = 0;
172                 lcd_gotoxy(log_col, 1+log_row);
173         }
174         lcd_write(255);
175 }
176
177 SERIAL_SET_CALLBACK(receive)
178
179 uint8_t hexdigit(uint8_t n)
180 {
181         n &= 0xF;
182         if(n<10)
183                 return '0'+n;
184         else
185                 return 'A'+(n-0xA);
186 }
187
188 uint8_t decode_hex(uint8_t c)
189 {
190         if(c>='0' && c<='9')
191                 return c-'0';
192         else if(c>='A' && c<='F')
193                 return 0xA+(c-'A');
194         else
195                 return 0;
196 }