3 #include "ringbuffer.h"
12 #define BIT(x) (1<<(x))
14 static volatile uint8_t s88_read_count = 0;
15 static uint8_t s88_read_bit = 0;
16 static volatile uint8_t s88_read_phase = 0;
17 static uint8_t s88_data = 0;
18 static RINGBUFFER(s88_buffer, 8);
19 static 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))
37 reply[1] = s88_out_index;
38 for(uint8_t i=0; i<count; ++i)
39 reply[2+i] = ringbuffer_pop(s88_buffer);
40 interface_send(reply, 2+count);
41 s88_out_index += count;
45 uint8_t s88_command(const uint8_t *cmd_buf, uint8_t cmd_length)
47 if(cmd_buf[0]==S88_READ)
52 while(s88_read_count || s88_read_phase) ;
55 s88_read_phase = 0xF0;
56 s88_read_count = cmd_buf[1];
59 return INVALID_COMMAND;
64 static inline void s88_tick(void)
68 if(!s88_read_count || ringbuffer_space(s88_buffer)==0)
73 else if(s88_read_phase==1)
75 uint8_t bit = (PIND>>7)&1;
76 s88_data = (s88_data<<1)|bit;
80 ringbuffer_push(s88_buffer, s88_data);
86 else if(s88_read_phase==2)
88 else if(s88_read_phase>3)
90 if(s88_read_phase==0xF0)
92 else if(s88_read_phase==0xF4)
94 else if(s88_read_phase==0xF6)
96 else if(s88_read_phase==0xFA)
98 else if(s88_read_phase==0xFC)
100 else if(s88_read_phase==0xFF)
102 s88_data = (PIND>>7)&1;
110 s88_read_phase = (s88_read_phase+1)&3;
113 TIMER_SET_CALLBACK(0, s88_tick)