altos/stmf0: Use device ID as usb serial number if requested
[fw/altos] / src / stmf0 / ao_usb_stm.c
index b8146c216f1a2b0538e964dd8bc24862895d984c..17e8709cf959095d1d089fd1a79fd909f86d2b80 100644 (file)
 #include "ao.h"
 #include "ao_usb.h"
 #include "ao_product.h"
+#include "ao_power.h"
 
 #define USB_DEBUG      0
+#define USB_STATUS     0
 #define USB_DEBUG_DATA 0
 #define USB_ECHO       0
 
 #error "must define AO_PA11_PA12_RMP"
 #endif
 
+#ifndef AO_POWER_MANAGEMENT
+#define AO_POWER_MANAGEMENT    0
+#endif
+
 #ifndef USE_USB_STDIO
 #define USE_USB_STDIO  1
 #endif
@@ -129,10 +135,9 @@ static uint8_t     ao_usb_out_avail;
 uint8_t                ao_usb_running;
 static uint8_t ao_usb_configuration;
 
-#define AO_USB_EP0_GOT_RESET   1
-#define AO_USB_EP0_GOT_SETUP   2
-#define AO_USB_EP0_GOT_RX_DATA 4
-#define AO_USB_EP0_GOT_TX_ACK  8
+#define AO_USB_EP0_GOT_SETUP   1
+#define AO_USB_EP0_GOT_RX_DATA 2
+#define AO_USB_EP0_GOT_TX_ACK  4
 
 static uint8_t ao_usb_ep0_receive;
 static uint8_t ao_usb_address;
@@ -396,6 +401,7 @@ ao_usb_set_configuration(void)
 {
        debug ("ao_usb_set_configuration\n");
 
+#if AO_USB_HAS_INT
        /* Set up the INT end point */
        ao_usb_bdt[AO_USB_INT_EPR].single.addr_tx = ao_usb_int_tx_offset;
        ao_usb_bdt[AO_USB_INT_EPR].single.count_tx = 0;
@@ -405,7 +411,9 @@ ao_usb_set_configuration(void)
                       STM_USB_EPR_EP_TYPE_INTERRUPT,
                       STM_USB_EPR_STAT_RX_DISABLED,
                       STM_USB_EPR_STAT_TX_NAK);
+#endif
 
+#if AO_USB_HAS_OUT
        /* Set up the OUT end point */
        ao_usb_bdt[AO_USB_OUT_EPR].single.addr_rx = ao_usb_out_rx_offset;
        ao_usb_bdt[AO_USB_OUT_EPR].single.count_rx = ((1 << STM_USB_BDT_COUNT_RX_BL_SIZE) |
@@ -416,7 +424,9 @@ ao_usb_set_configuration(void)
                       STM_USB_EPR_EP_TYPE_BULK,
                       STM_USB_EPR_STAT_RX_VALID,
                       STM_USB_EPR_STAT_TX_DISABLED);
+#endif
 
+#if AO_USB_HAS_IN
        /* Set up the IN end point */
        ao_usb_bdt[AO_USB_IN_EPR].single.addr_tx = ao_usb_in_tx_offset;
        ao_usb_bdt[AO_USB_IN_EPR].single.count_tx = 0;
@@ -426,6 +436,7 @@ ao_usb_set_configuration(void)
                       STM_USB_EPR_EP_TYPE_BULK,
                       STM_USB_EPR_STAT_RX_DISABLED,
                       STM_USB_EPR_STAT_TX_NAK);
+#endif
 
        ao_usb_running = 1;
 #if AO_USB_DIRECTIO
@@ -559,6 +570,36 @@ ao_usb_ep0_in_start(uint16_t max)
 
 static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
 
+#if AO_USB_DEVICE_ID_SERIAL
+static uint8_t ao_usb_serial[2 + 48];
+
+/* Convert a 32-bit value to 8 hexidecimal UCS2 characters */
+static void
+hex_to_ucs2(uint32_t in, uint8_t *out)
+{
+       int     i;
+
+       for (i = 28; i >= 0; i -= 4) {
+               uint8_t bits = (in >> i) & 0xf;
+               *out++ = (bits < 10) ? ('0' + bits) : ('a' + bits);
+               *out++ = 0;
+       }
+}
+
+/* Encode the device ID (96 bits) in hexidecimal to use as a device
+ * serial number
+ */
+static void
+ao_usb_serial_init(void)
+{
+       ao_usb_serial[0] = 50;  /* length */
+       ao_usb_serial[1] = AO_USB_DESC_STRING;
+       hex_to_ucs2(stm_device_id.u_id0, ao_usb_serial + 2 + 0);
+       hex_to_ucs2(stm_device_id.u_id1, ao_usb_serial + 2 + 16);
+       hex_to_ucs2(stm_device_id.u_id2, ao_usb_serial + 2 + 32);
+}
+#endif
+
 /* Walk through the list of descriptors and find a match
  */
 static void
@@ -576,6 +617,13 @@ ao_usb_get_descriptor(uint16_t value)
                                len = descriptor[2];
                        else
                                len = descriptor[0];
+#if AO_USB_DEVICE_ID_SERIAL
+                       /* Slightly hacky - the serial number is string 3 */
+                       if (type == AO_USB_DESC_STRING && (value & 0xff) == 3) {
+                               descriptor = ao_usb_serial;
+                               len = sizeof (ao_usb_serial);
+                       }
+#endif
                        ao_usb_ep0_in_set(descriptor, len);
                        break;
                }
@@ -686,11 +734,6 @@ static void
 ao_usb_ep0_handle(uint8_t receive)
 {
        ao_usb_ep0_receive = 0;
-       if (receive & AO_USB_EP0_GOT_RESET) {
-               debug ("\treset\n");
-               ao_usb_set_ep0();
-               return;
-       }
        if (receive & AO_USB_EP0_GOT_SETUP) {
                debug ("\tsetup\n");
                ao_usb_ep0_setup();
@@ -721,6 +764,25 @@ ao_usb_ep0_handle(uint8_t receive)
        }
 }
 
+#if AO_POWER_MANAGEMENT
+void
+ao_usb_suspend(void)
+{
+       stm_usb.cntr |= (1 << STM_USB_CNTR_FSUSP);
+       ao_power_suspend();
+       stm_usb.cntr |= (1 << STM_USB_CNTR_LP_MODE);
+       ao_clock_suspend();
+}
+
+void
+ao_usb_wakeup(void)
+{
+       ao_clock_resume();
+       stm_usb.cntr &= ~(1 << STM_USB_CNTR_FSUSP);
+       ao_power_resume();
+}
+#endif
+
 void
 stm_usb_isr(void)
 {
@@ -784,10 +846,19 @@ stm_usb_isr(void)
 
        if (istr & (1 << STM_USB_ISTR_RESET)) {
                ++reset_count;
-               ao_usb_ep0_receive |= AO_USB_EP0_GOT_RESET;
-               ao_usb_ep0_handle(ao_usb_ep0_receive);
+               debug ("\treset\n");
+               ao_usb_set_ep0();
        }
-
+#if AO_POWER_MANAGEMENT
+       if (istr & (1 << STM_USB_ISTR_SUSP)) {
+               debug ("\tsuspend\n");
+               ao_usb_suspend();
+       }
+       if (istr & (1 << STM_USB_ISTR_WKUP)) {
+               debug ("\twakeup\n");
+               ao_usb_wakeup();
+       }
+#endif
 }
 
 /* Queue the current IN buffer for transmission */
@@ -1050,8 +1121,8 @@ ao_usb_enable(void)
        stm_usb.cntr = ((1 << STM_USB_CNTR_CTRM) |
                        (0 << STM_USB_CNTR_PMAOVRM) |
                        (0 << STM_USB_CNTR_ERRM) |
-                       (0 << STM_USB_CNTR_WKUPM) |
-                       (0 << STM_USB_CNTR_SUSPM) |
+                       (AO_POWER_MANAGEMENT << STM_USB_CNTR_WKUPM) |
+                       (AO_POWER_MANAGEMENT << STM_USB_CNTR_SUSPM) |
                        (1 << STM_USB_CNTR_RESETM) |
                        (0 << STM_USB_CNTR_SOFM) |
                        (0 << STM_USB_CNTR_ESOFM) |
@@ -1086,7 +1157,7 @@ ao_usb_echo(void)
 }
 #endif
 
-#if USB_DEBUG
+#if USB_STATUS
 static void
 ao_usb_irq(void)
 {
@@ -1111,6 +1182,10 @@ ao_usb_init(void)
 
        ao_usb_enable();
 
+#if AO_USB_DEVICE_ID_SERIAL
+       ao_usb_serial_init();
+#endif
+
        debug ("ao_usb_init\n");
        ao_usb_ep0_state = AO_USB_EP0_IDLE;
 
@@ -1119,7 +1194,7 @@ ao_usb_init(void)
 #if USB_ECHO
        ao_add_task(&ao_usb_echo_task, ao_usb_echo, "usb echo");
 #endif
-#if USB_DEBUG
+#if USB_STATUS
        ao_cmd_register(&ao_usb_cmds[0]);
 #endif
 #if !USB_ECHO