struct ao_task __xdata ao_usb_task;
static __xdata uint16_t ao_usb_in_bytes;
+static __xdata uint16_t ao_usb_in_bytes_last;
static __xdata uint16_t ao_usb_out_bytes;
static __xdata uint8_t ao_usb_iif;
static __xdata uint8_t ao_usb_running;
* so when we hook that up, fix this
*/
void
-ao_usb_isr(void) interrupt 6
+ao_usb_isr(void) __interrupt 6
{
USBIF = 0;
ao_usb_iif |= USBIIF;
ao_wakeup(&ao_usb_in_bytes);
if (USBOIF & (1 << AO_USB_OUT_EP))
- ao_wakeup(&ao_usb_out_bytes);
+ ao_wakeup(&ao_stdin_ready);
if (USBCIF & USBCIF_RSTIF)
ao_usb_set_interrupts();
+#if HAS_BTM
+#if BT_LINK_ON_P2
+ ao_btm_isr();
+#endif
+#endif
}
struct ao_usb_setup {
__xdata uint8_t ao_usb_ep0_in_len;
__xdata uint8_t ao_usb_ep0_in_buf[2];
__xdata uint8_t ao_usb_ep0_out_len;
-__xdata uint8_t *__data ao_usb_ep0_out_data;
+__xdata uint8_t *__xdata ao_usb_ep0_out_data;
__xdata uint8_t ao_usb_configuration;
/* Send an IN data packet */
__xdata uint8_t this_len;
__xdata uint8_t cs0;
+ /* If the IN packet hasn't been picked up, just return */
USBINDEX = 0;
cs0 = USBCS0;
if (cs0 & USBCS0_INPKT_RDY)
- ao_panic(0);
+ return;
this_len = ao_usb_ep0_in_len;
if (this_len > AO_USB_CONTROL_SIZE)
}
}
-void
-ao_usb_flush(void) __critical
+/* Wait for a free IN buffer */
+static void
+ao_usb_in_wait(void)
{
- if (ao_usb_in_bytes) {
+ for (;;) {
USBINDEX = AO_USB_IN_EP;
- USBCSIL |= USBCSIL_INPKT_RDY;
- ao_usb_in_bytes = 0;
+ if ((USBCSIL & USBCSIL_INPKT_RDY) == 0)
+ break;
+ ao_sleep(&ao_usb_in_bytes);
}
}
+/* Send the current IN packet */
+static void
+ao_usb_in_send(void)
+{
+ USBINDEX = AO_USB_IN_EP;
+ USBCSIL |= USBCSIL_INPKT_RDY;
+ ao_usb_in_bytes_last = ao_usb_in_bytes;
+ ao_usb_in_bytes = 0;
+}
+
void
-ao_usb_putchar(char c) __critical
+ao_usb_flush(void) __critical
{
if (!ao_usb_running)
return;
- for (;;) {
- USBINDEX = AO_USB_IN_EP;
- if ((USBCSIL & USBCSIL_INPKT_RDY) == 0)
- break;
- ao_sleep(&ao_usb_in_bytes);
+
+ /* If there are pending bytes, or if the last packet was full,
+ * send another IN packet
+ */
+ if (ao_usb_in_bytes || (ao_usb_in_bytes_last == AO_USB_IN_SIZE)) {
+ ao_usb_in_wait();
+ ao_usb_in_send();
}
+}
+
+void
+ao_usb_putchar(char c) __critical __reentrant
+{
+ if (!ao_usb_running)
+ return;
+
+ ao_usb_in_wait();
+
+ /* Queue a byte, sending the packet when full */
USBFIFO[AO_USB_IN_EP << 1] = c;
- if (++ao_usb_in_bytes == AO_USB_IN_SIZE) {
- USBINDEX = AO_USB_IN_EP;
- USBCSIL |= USBCSIL_INPKT_RDY;
- ao_usb_in_bytes = 0;
- }
+ if (++ao_usb_in_bytes == AO_USB_IN_SIZE)
+ ao_usb_in_send();
}
char
-ao_usb_getchar(void) __critical
+ao_usb_pollchar(void) __critical
{
- __xdata char c;
- while (ao_usb_out_bytes == 0) {
- for (;;) {
+ char c;
+ if (ao_usb_out_bytes == 0) {
+ USBINDEX = AO_USB_OUT_EP;
+ if ((USBCSOL & USBCSOL_OUTPKT_RDY) == 0)
+ return AO_READ_AGAIN;
+ ao_usb_out_bytes = (USBCNTH << 8) | USBCNTL;
+ if (ao_usb_out_bytes == 0) {
USBINDEX = AO_USB_OUT_EP;
- if ((USBCSOL & USBCSOL_OUTPKT_RDY) != 0)
- break;
- ao_sleep(&ao_usb_out_bytes);
+ USBCSOL &= ~USBCSOL_OUTPKT_RDY;
+ return AO_READ_AGAIN;
}
- ao_usb_out_bytes = (USBCNTH << 8) | USBCNTL;
}
--ao_usb_out_bytes;
c = USBFIFO[AO_USB_OUT_EP << 1];
return c;
}
+char
+ao_usb_getchar(void) __critical
+{
+ char c;
+
+ while ((c = ao_usb_pollchar()) == AO_READ_AGAIN)
+ ao_sleep(&ao_stdin_ready);
+ return c;
+}
+
void
ao_usb_enable(void)
{
ao_usb_enable();
ao_add_task(&ao_usb_task, ao_usb_ep0, "usb");
+ ao_add_stdio(ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);
}