]> git.tdb.fi Git - model-railway-devices.git/blob - s88w/s88w-r.c
Use pin change interrupt to react faster to the clock
[model-railway-devices.git] / s88w / s88w-r.c
1 /*
2 Firmware for wireless S88 receiver module
3
4 S88 pinout:
5 1 - DATA
6 2 - GND
7 3 - CLOCK
8 4 - LOAD
9 5 - RESET
10 6 - POWER
11
12 ATMega pinout:
13 D0 - serial RX
14 D1 - serial TX
15 D2 - S88 DATA
16 D3 - S88 CLOCK
17 D4 - S88 LOAD
18 D5 - S88 RESET
19 */
20
21 #include <avr/io.h>
22 #include <avr/interrupt.h>
23 #include "lcd.h"
24 #include "serial.h"
25 #include "delay.h"
26
27 #define DATA_OUT PORTD2
28 #define CLOCK    PIND3
29 #define LOAD     PIND4
30 #define RESET    PIND5
31
32 #define LCD_DISABLE PINB4
33
34 #define BIT(n)   (1<<(n))
35
36 void check_input();
37 uint8_t hexdigit(uint8_t);
38 uint8_t decode_hex(uint8_t);
39
40 uint8_t clock_high = 0;
41 uint8_t rx_buf[7];
42 uint8_t rx_fill = 0xFF;
43 uint8_t input[128] = { 0 };
44 uint8_t latch[128] = { 0 };
45 uint8_t out_offset = 0;
46 uint8_t out_bits = 0;
47 uint8_t out_fill = 0;
48 volatile uint8_t reset_pos = 0xFF;
49 uint8_t lcd_enabled = 0;
50 uint8_t log_row = 0;
51 uint8_t log_col = 0;
52
53 int main()
54 {
55         DDRD = 0x06;   // 00000110
56         PORTD = 0xC0;  // 11000000
57         DDRB = 0x20;   // 00100000
58         PORTB = 0x1F;  // 00011111
59         PCMSK2 = BIT(PCINT19) | BIT(PCINT21);
60         PCICR = BIT(PCIE2);
61
62         serial_init(9600);
63         lcd_init();
64
65         sei();
66
67         while(1)
68         {
69                 uint8_t i;
70
71                 check_input();
72
73                 i = reset_pos;
74                 if(i!=0xFF)
75                 {
76                         latch[i] = input[i];
77                         if(++i>=sizeof(input))
78                                 i = 0xFF;
79                         reset_pos = i;
80                 }
81
82                 if(PINB&BIT(LCD_DISABLE))
83                 {
84                         if(lcd_enabled)
85                         {
86                                 lcd_enabled = 0;
87
88                                 lcd_clear();
89                         }
90                 }
91                 else if(!lcd_enabled)
92                 {
93                         lcd_enabled = 1;
94                         log_row = 0;
95                         log_col = 0;
96
97                         lcd_clear();
98                         for(i=0; i<20; ++i)
99                                 lcd_write(hexdigit(input[9-i/2]>>(4-i%2*4)));
100                         lcd_gotoxy(0, 1);
101                         lcd_write(255);
102                 }
103         }
104 }
105
106 void check_input()
107 {
108         uint8_t c;
109         if(!serial_read_available())
110                 return;
111
112         c = serial_read();
113         if(rx_fill==0xFF)
114         {
115                 if(c==':')
116                         rx_fill = 0;
117         }
118         else if(c=='.')
119         {
120                 if(rx_fill>=4)
121                 {
122                         uint16_t offset;
123                         uint8_t nibbles;
124                         uint8_t i;
125
126                         offset = (decode_hex(rx_buf[0])<<8) | (decode_hex(rx_buf[1])<<4) | decode_hex(rx_buf[2]);
127                         nibbles = (offset&3);
128                         offset >>= 2;
129                         if(rx_fill>3+nibbles)
130                         {
131                                 for(i=0; i<=nibbles; ++i)
132                                 {
133                                         uint16_t j = offset+nibbles-i;
134                                         uint8_t shift = 4*(j&1);
135                                         uint8_t bits = decode_hex(rx_buf[3+i]);
136                                         input[j/2] = (input[j/2]&~(0xF<<shift)) | (bits<<shift);
137                                         latch[j/2] = input[j/2];
138                                 }
139
140                                 if(lcd_enabled)
141                                 {
142                                         lcd_gotoxy(19-offset-nibbles, 0);
143                                         for(i=0; i<=nibbles; ++i)
144                                                 lcd_write(rx_buf[3+i]);
145                                 }
146                         }
147                 }
148                 rx_fill = 0xFF;
149         }
150         else
151         {
152                 if(rx_fill<sizeof(rx_buf))
153                         rx_buf[rx_fill++] = c;
154                 else
155                         rx_fill = 0xFF;
156         }
157
158         if(lcd_enabled)
159         {
160                 lcd_gotoxy(log_col, 1+log_row);
161                 lcd_write(c);
162                 ++log_col;
163                 if(log_col>=20)
164                 {
165                         log_col = 0;
166                         ++log_row;
167                         if(log_row>=3)
168                                 log_row = 0;
169                         lcd_gotoxy(log_col, 1+log_row);
170                 }
171                 lcd_write(255);
172         }
173 }
174
175 uint8_t hexdigit(uint8_t n)
176 {
177         n &= 0xF;
178         if(n<10)
179                 return '0'+n;
180         else
181                 return 'A'+(n-0xA);
182 }
183
184 uint8_t decode_hex(uint8_t c)
185 {
186         if(c>='0' && c<='9')
187                 return c-'0';
188         else if(c>='A' && c<='F')
189                 return 0xA+(c-'A');
190         else
191                 return 0;
192 }
193
194 ISR(PCINT2_vect)
195 {
196         uint8_t d = PIND;
197         if(d&BIT(CLOCK))
198         {
199                 if(!clock_high)
200                 {
201                         if(d&BIT(LOAD))
202                         {
203                                 out_offset = 0;
204                                 out_bits = latch[0];
205                                 out_fill = 8;
206                         }
207
208                         if(out_bits&1)
209                                 PORTD |= BIT(DATA_OUT);
210                         else
211                                 PORTD &= ~BIT(DATA_OUT);
212
213                         out_bits >>= 1;
214                         if(!--out_fill)
215                         {
216                                 ++out_offset;
217                                 out_bits = latch[out_offset];
218                                 out_fill = 8;
219                         }
220
221                         clock_high = 1;
222                 }
223         }
224         else
225                 clock_high = 0;
226
227         if(d&BIT(RESET))
228                 reset_pos = 0;
229 }