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 /* Check remaining read count before ringbuffer fill. If it is zero, no
32 more bytes are arriving in the buffer and the contents can be sent out. */
33 uint8_t rc = s88_read_count;
34 uint8_t count = ringbuffer_fill(s88_buffer);
35 if(count>=4 || (count>0 && !rc))
40 reply[1] = s88_out_index;
41 for(uint8_t i=0; i<count; ++i)
42 reply[2+i] = ringbuffer_pop(s88_buffer);
43 interface_send(reply, 2+count);
44 s88_out_index += count;
48 uint8_t s88_command(const uint8_t *cmd_buf, uint8_t cmd_length)
50 if(cmd_buf[0]==S88_READ)
55 while(s88_read_count || s88_read_phase) ;
58 s88_read_phase = 0xF0;
59 s88_read_count = cmd_buf[1];
62 return INVALID_COMMAND;
67 static inline void s88_tick(void)
71 if(!s88_read_count || ringbuffer_space(s88_buffer)==0)
76 else if(s88_read_phase==1)
78 uint8_t bit = (PIND>>7)&1;
79 s88_data = (s88_data<<1)|bit;
83 ringbuffer_push(s88_buffer, s88_data);
89 else if(s88_read_phase==2)
91 else if(s88_read_phase>3)
93 if(s88_read_phase==0xF0)
95 else if(s88_read_phase==0xF4)
97 else if(s88_read_phase==0xF6)
99 else if(s88_read_phase==0xFA)
101 else if(s88_read_phase==0xFC)
102 PORTD &= ~BIT(RESET);
103 else if(s88_read_phase==0xFF)
105 s88_data = (PIND>>7)&1;
113 s88_read_phase = (s88_read_phase+1)&3;
116 TIMER_SET_CALLBACK(0, s88_tick)