+ if (g.stream_enable_to) {
+ if (g.use_readinc) {
+ attachInterruptVector(IRQ_PORTB, portb_isr_do_to_inc);
+ attachInterruptVector(IRQ_PORTC, portc_isr_nop);
+ }
+ else {
+ attachInterruptVector(IRQ_PORTB, portb_isr_do_to);
+ attachInterruptVector(IRQ_PORTC, portc_isr_frameinc);
+ }
+ }
+ else if (g.stream_enable_from) {
+ g.use_pending = 1;
+ if (g.use_readinc) {
+ attachInterruptVector(IRQ_PORTB,
+ portb_isr_fixed_do_from);
+ attachInterruptVector(IRQ_PORTC, portc_isr_nop);
+ }
+ else {
+ attachInterruptVector(IRQ_PORTB, portb_isr_fixed);
+ attachInterruptVector(IRQ_PORTC,
+ portc_isr_frameinc_do_from);
+ }
+ }
+ __enable_irq();
+}
+
+// callers must disable IRQs
+static void clear_state(void)
+{
+ g.stream_enable_to = 0;
+ g.stream_enable_from = 0;
+ g.stream_started = 0;
+ g.stream_ended = 0;
+ g.use_readinc = 0;
+ g.use_pending = 0;
+ g.t_i = g.t_o = 0;
+ g.f_i = g.f_o = 0;
+ g.frame_cnt = 0;
+ attachInterruptVector(IRQ_PORTB, portb_isr_fixed);
+ attachInterruptVector(IRQ_PORTC, portc_isr_nop);
+}
+
+static int get_space_to(void)
+{
+ return STREAM_BUF_SIZE - ((g.t_i - g.t_o) & STREAM_BUF_MASK);