Allow for slower ADC operation. Add power saving code.
[fw/altos] / ao_usb.c
index dd0471d182d3adac0784b42616f053a232bc5320..1868ec3cea8355fd1de3e4e09f3e0cb987e86d6a 100644 (file)
--- a/ao_usb.c
+++ b/ao_usb.c
@@ -34,8 +34,8 @@ struct ao_task __xdata ao_usb_task;
 
 static __xdata uint16_t        ao_usb_in_bytes;
 static __xdata uint16_t        ao_usb_out_bytes;
-static __data uint8_t  ao_usb_iif;
-static __data uint8_t  ao_usb_oif;
+static __xdata uint8_t ao_usb_iif;
+static __xdata uint8_t ao_usb_oif;
 
 /* This interrupt is shared with port 2, 
  * so when we hook that up, fix this
@@ -67,20 +67,20 @@ struct ao_usb_setup {
        uint16_t        length;
 } __xdata ao_usb_setup;
 
-__data uint8_t ao_usb_ep0_state;
-uint8_t * __data ao_usb_ep0_in_data;
-__data uint8_t ao_usb_ep0_in_len;
+__xdata uint8_t ao_usb_ep0_state;
+uint8_t * __xdata ao_usb_ep0_in_data;
+__xdata uint8_t ao_usb_ep0_in_len;
 __xdata uint8_t        ao_usb_ep0_in_buf[2];
-__data uint8_t ao_usb_ep0_out_len;
+__xdata uint8_t ao_usb_ep0_out_len;
 __xdata uint8_t *__data ao_usb_ep0_out_data;
-__data uint8_t ao_usb_configuration;
+__xdata uint8_t ao_usb_configuration;
 
 /* Send an IN data packet */
 static void
 ao_usb_ep0_flush(void)
 {
-       uint8_t this_len;
-       uint8_t cs0;
+       __xdata uint8_t this_len;
+       __xdata uint8_t cs0;
        
        USBINDEX = 0;
        cs0 = USBCS0;
@@ -102,7 +102,7 @@ ao_usb_ep0_flush(void)
        USBCS0 = cs0;
 }
 
-#define LE_WORD(x)    ((x)&0xFF),((x)>>8)
+#define LE_WORD(x)    ((x)&0xFF),((uint8_t) (((uint16_t) (x))>>8))
 
 /* CDC definitions */
 #define CS_INTERFACE      0x24
@@ -120,7 +120,7 @@ struct ao_usb_line_coding {
        uint8_t         data_bits;
 } ;
 
-static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
+__xdata static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
 
 /* USB descriptors in one giant block of bytes */
 static const uint8_t ao_usb_descriptors [] = 
@@ -241,7 +241,7 @@ static const uint8_t ao_usb_descriptors [] =
        /* iSerial */
        0x0e,
        AO_USB_DESC_STRING,
-       't', 0, 'e', 0, 'l', 0, 'e', 0, '-', 0, '0', 0, 
+       '0', 0, '0', 0, '0', 0, '0', 0, '0', 0, '0', 0, 
 
        /* Terminating zero */
        0
@@ -252,9 +252,9 @@ static const uint8_t ao_usb_descriptors [] =
 static void
 ao_usb_get_descriptor(uint16_t value)
 {
-       const uint8_t   *descriptor;
-       uint8_t         type = value >> 8;
-       uint8_t         index = value;
+       const uint8_t           *__xdata descriptor;
+       __xdata uint8_t         type = value >> 8;
+       __xdata uint8_t         index = value;
 
        descriptor = ao_usb_descriptors;
        while (descriptor[0] != 0) {
@@ -275,7 +275,7 @@ ao_usb_get_descriptor(uint16_t value)
 static void
 ao_usb_ep0_fill(void)
 {
-       uint8_t len;
+       __xdata uint8_t len;
        
        USBINDEX = 0;
        len = USBCNT0;
@@ -369,6 +369,7 @@ ao_usb_ep0_setup(void)
                        }
                        break;
                case AO_USB_RECIP_INTERFACE:
+                       #pragma disable_warning 110
                        switch(ao_usb_setup.request) {
                        case AO_USB_REQ_GET_STATUS:
                                ao_usb_ep0_queue_byte(0);
@@ -417,7 +418,7 @@ ao_usb_ep0_setup(void)
 static void
 ao_usb_ep0(void)
 {
-       uint8_t cs0;
+       __xdata uint8_t cs0;
 
        ao_usb_ep0_state = AO_USB_EP0_IDLE;
        for (;;) {
@@ -493,7 +494,7 @@ ao_usb_putchar(uint8_t c) __critical
 uint8_t
 ao_usb_getchar(void) __critical
 {
-       uint8_t c;
+       __xdata uint8_t c;
        while (ao_usb_out_bytes == 0) {
                for (;;) {
                        USBINDEX = AO_USB_OUT_EP;
@@ -513,7 +514,7 @@ ao_usb_getchar(void) __critical
 }
 
 void
-ao_usb_init(void)
+ao_usb_enable(void)
 {
        /* Turn on the USB controller */
        SLEEP |= SLEEP_USB_EN;
@@ -536,6 +537,30 @@ ao_usb_init(void)
        USBCIF = 0;
        USBOIF = 0;
        USBIIF = 0;
+}
+
+void
+ao_usb_disable(void)
+{
+       /* Disable USB interrupts */
+       USBIIE = 0;
+       USBOIE = 0;
+       USBCIE = 0;
+       IEN2 &= ~IEN2_USBIE;
        
-       ao_add_task(&ao_usb_task, ao_usb_ep0);
+       /* Clear any pending interrupts */
+       USBCIF = 0;
+       USBOIF = 0;
+       USBIIF = 0;
+
+       /* Turn off the USB controller */
+       SLEEP &= ~SLEEP_USB_EN;
+}
+
+void
+ao_usb_init(void)
+{
+       ao_usb_enable();
+
+       ao_add_task(&ao_usb_task, ao_usb_ep0, "usb");
 }