3 #include "ringbuffer.h"
12 #define BIT(x) (1<<(x))
14 volatile uint8_t s88_read_count = 0;
15 uint8_t s88_read_bit = 0;
16 volatile uint8_t s88_read_phase = 0;
18 RINGBUFFER(s88_buffer, 8);
19 uint8_t s88_out_index = 0;
23 DDRD = (DDRD&0x0F)|0x70;
26 timer_start_hz(0, 80000, 1);
31 uint8_t count = ringbuffer_fill(s88_buffer);
32 if(count>=4 || (count>0 && !s88_read_count))
38 reply[1] = s88_out_index;
39 for(i=0; i<count; ++i)
40 reply[2+i] = ringbuffer_pop(s88_buffer);
41 interface_send(reply, 2+count);
42 s88_out_index += count;
46 uint8_t s88_command(const uint8_t *cmd_buf, uint8_t cmd_length)
48 if(cmd_buf[0]==S88_READ)
53 while(s88_read_count || s88_read_phase) ;
56 s88_read_phase = 0xF0;
57 s88_read_count = cmd_buf[1];
60 return INVALID_COMMAND;
65 static inline void s88_tick(void)
69 if(!s88_read_count || ringbuffer_space(s88_buffer)==0)
74 else if(s88_read_phase==1)
76 uint8_t bit = (PIND>>7)&1;
77 s88_data = (s88_data<<1)|bit;
81 ringbuffer_push(s88_buffer, s88_data);
87 else if(s88_read_phase==2)
89 else if(s88_read_phase>3)
91 if(s88_read_phase==0xF0)
93 else if(s88_read_phase==0xF4)
95 else if(s88_read_phase==0xF6)
97 else if(s88_read_phase==0xFA)
99 else if(s88_read_phase==0xFC)
100 PORTD &= ~BIT(RESET);
101 else if(s88_read_phase==0xFF)
103 s88_data = (PIND>>7)&1;
111 s88_read_phase = (s88_read_phase+1)&3;
114 TIMER_SET_CALLBACK(0, s88_tick)