+ if (avr_regbit_get(avr, p->rxen))
+ avr_raise_interrupt(avr, &p->rxc);
+ return 0;
+}
+
+static uint8_t avr_uart_rxc_read(struct avr_t * avr, avr_io_addr_t addr, void * param)
+{
+ avr_uart_t * p = (avr_uart_t *)param;
+ uint8_t v = avr_core_watch_read(avr, addr);
+
+ //static uint8_t old = 0xff; if (v != old) printf("UCSRA read %02x\n", v); old = v;
+ //
+ // if RX is enabled, and there is nothing to read, and
+ // the AVR core is reading this register, it's probably
+ // to poll the RXC TXC flag and spinloop
+ // so here we introduce a usleep to make it a bit lighter
+ // on CPU and let data arrive
+ //
+ uint8_t ri = !avr_regbit_get(avr, p->rxen) || !avr_regbit_get(avr, p->rxc.raised);
+ uint8_t ti = !avr_regbit_get(avr, p->txen) || !avr_regbit_get(avr, p->txc.raised);
+
+ if (p->flags & AVR_UART_FLAG_POOL_SLEEP) {
+
+ if (ri && ti)
+ usleep(1);
+ }
+ // if reception is idle and the fifo is empty, tell whomever there is room
+ if (avr_regbit_get(avr, p->rxen) && uart_fifo_isempty(&p->input)) {
+ avr_raise_irq(p->io.irq + UART_IRQ_OUT_XOFF, 0);
+ avr_raise_irq(p->io.irq + UART_IRQ_OUT_XON, 1);
+ }
+
+ return v;
+}
+
+static uint8_t avr_uart_read(struct avr_t * avr, avr_io_addr_t addr, void * param)
+{
+ avr_uart_t * p = (avr_uart_t *)param;
+
+ // clear the rxc bit in case the code is using polling
+ avr_regbit_clear(avr, p->rxc.raised);