Merge branch 'micropeak-1.1'
[fw/altos] / src / stm / ao_usb_stm.c
index 223fdeaa4620c9eea4d5802dd6966a2f0ae0e9e2..9379e5cd62efd19a8cdf994669fa171be0a9f11b 100644 (file)
@@ -223,16 +223,16 @@ _ao_usb_set_stat_tx(int ep, uint32_t stat_tx)
 static void
 ao_usb_set_stat_tx(int ep, uint32_t stat_tx)
 {
-       cli();
+       ao_arch_block_interrupts();
        _ao_usb_set_stat_tx(ep, stat_tx);
-       sei();
+       ao_arch_release_interrupts();
 }
 
 static void
 ao_usb_set_stat_rx(int ep, uint32_t stat_rx) {
        uint32_t        epr_write, epr_old;
 
-       cli();
+       ao_arch_block_interrupts();
        epr_write = epr_old = stm_usb.epr[ep];
        epr_write &= STM_USB_EPR_PRESERVE_MASK;
        epr_write |= STM_USB_EPR_INVARIANT;
@@ -240,7 +240,7 @@ ao_usb_set_stat_rx(int ep, uint32_t stat_rx) {
                              STM_USB_EPR_STAT_RX_MASK << STM_USB_EPR_STAT_RX,
                              stat_rx << STM_USB_EPR_STAT_RX);
        stm_usb.epr[ep] = epr_write;
-       sei();
+       ao_arch_release_interrupts();
 }
 
 /*
@@ -251,7 +251,7 @@ static void
 ao_usb_init_ep(uint8_t ep, uint32_t addr, uint32_t type, uint32_t stat_rx, uint32_t stat_tx)
 {
        uint32_t                epr;
-       cli();
+       ao_arch_block_interrupts();
        epr = stm_usb.epr[ep];
        epr = ((0 << STM_USB_EPR_CTR_RX) |
               (epr & (1 << STM_USB_EPR_DTOG_RX)) |
@@ -267,7 +267,7 @@ ao_usb_init_ep(uint8_t ep, uint32_t addr, uint32_t type, uint32_t stat_rx, uint3
                          (stat_tx << STM_USB_EPR_STAT_TX)) |
               (addr << STM_USB_EPR_EA));
        stm_usb.epr[ep] = epr;
-       sei();
+       ao_arch_release_interrupts();
        debug ("writing epr[%d] 0x%08x wrote 0x%08x\n",
               ep, epr, stm_usb.epr[ep]);
 }
@@ -792,32 +792,30 @@ static void
 ao_usb_in_send(void)
 {
        debug ("send %d\n", ao_usb_tx_count);
+       ao_usb_in_pending = 1;
        ao_usb_write(ao_usb_tx_buffer, ao_usb_in_tx_buffer, 0, ao_usb_tx_count);
        ao_usb_bdt[AO_USB_IN_EPR].single.count_tx = ao_usb_tx_count;
        ao_usb_set_stat_tx(AO_USB_IN_EPR, STM_USB_EPR_STAT_TX_VALID);
-       ao_usb_in_pending = 1;
        ao_usb_tx_count = 0;
 }
 
-/* Wait for a free IN buffer */
+/* Wait for a free IN buffer. Interrupts are blocked */
 static void
-ao_usb_in_wait(void)
+_ao_usb_in_wait(void)
 {
        for (;;) {
                /* Check if the current buffer is writable */
                if (ao_usb_tx_count < AO_USB_IN_SIZE)
                        break;
 
-               cli();
                /* Wait for an IN buffer to be ready */
                while (ao_usb_in_pending)
                        ao_sleep(&ao_usb_in_pending);
-               sei();
        }
 }
 
 void
-ao_usb_flush(void) __critical
+ao_usb_flush(void)
 {
        if (!ao_usb_running)
                return;
@@ -829,35 +827,37 @@ ao_usb_flush(void) __critical
         * packet was full, in which case we now
         * want to send an empty packet
         */
+       ao_arch_block_interrupts();
        if (!ao_usb_in_flushed) {
                ao_usb_in_flushed = 1;
-               cli();
                /* Wait for an IN buffer to be ready */
                while (ao_usb_in_pending)
                        ao_sleep(&ao_usb_in_pending);
-               sei();
                ao_usb_in_send();
        }
+       ao_arch_release_interrupts();
 }
 
 void
-ao_usb_putchar(char c) __critical __reentrant
+ao_usb_putchar(char c)
 {
        if (!ao_usb_running)
                return;
 
-       ao_usb_in_wait();
+       ao_arch_block_interrupts();
+       _ao_usb_in_wait();
 
+       ao_usb_in_flushed = 0;
        ao_usb_tx_buffer[ao_usb_tx_count++] = (uint8_t) c;
 
        /* Send the packet when full */
        if (ao_usb_tx_count == AO_USB_IN_SIZE)
                ao_usb_in_send();
-       ao_usb_in_flushed = 0;
+       ao_arch_release_interrupts();
 }
 
 static void
-ao_usb_out_recv(void)
+_ao_usb_out_recv(void)
 {
        ao_usb_out_avail = 0;
 
@@ -873,10 +873,10 @@ ao_usb_out_recv(void)
        ao_usb_set_stat_rx(AO_USB_OUT_EPR, STM_USB_EPR_STAT_RX_VALID);
 }
 
-static char
+static int
 _ao_usb_pollchar(void)
 {
-       char c;
+       uint8_t c;
 
        if (!ao_usb_running)
                return AO_READ_AGAIN;
@@ -888,7 +888,7 @@ _ao_usb_pollchar(void)
                /* Check to see if a packet has arrived */
                if (!ao_usb_out_avail)
                        return AO_READ_AGAIN;
-               ao_usb_out_recv();
+               _ao_usb_out_recv();
        }
 
        /* Pull a character out of the fifo */
@@ -896,31 +896,32 @@ _ao_usb_pollchar(void)
        return c;
 }
 
-char
+int
 ao_usb_pollchar(void)
 {
-       char    c;
-       cli();
+       int     c;
+       ao_arch_block_interrupts();
        c = _ao_usb_pollchar();
-       sei();
+       ao_arch_release_interrupts();
        return c;
 }
 
 char
-ao_usb_getchar(void) __critical
+ao_usb_getchar(void)
 {
-       char    c;
+       int     c;
 
-       cli();
+       ao_arch_block_interrupts();
        while ((c = _ao_usb_pollchar()) == AO_READ_AGAIN)
                ao_sleep(&ao_stdin_ready);
-       sei();
+       ao_arch_release_interrupts();
        return c;
 }
 
 void
 ao_usb_disable(void)
 {
+       ao_arch_block_interrupts();
        stm_usb.cntr = (1 << STM_USB_CNTR_FRES);
        stm_usb.istr = 0;
 
@@ -932,12 +933,13 @@ ao_usb_disable(void)
 
        /* Disable the interface */
        stm_rcc.apb1enr &+ ~(1 << STM_RCC_APB1ENR_USBEN);
+       ao_arch_release_interrupts();
 }
 
 void
 ao_usb_enable(void)
 {
-       uint16_t        tick;
+       int     t;
 
        /* Enable SYSCFG */
        stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_SYSCFGEN);
@@ -954,6 +956,8 @@ ao_usb_enable(void)
         * pulled low and doesn't work at all
         */
 
+       ao_arch_block_interrupts();
+
        /* Route interrupts */
        stm_nvic_set_priority(STM_ISR_USB_LP_POS, 3);
        stm_nvic_set_enable(STM_ISR_USB_LP_POS);
@@ -985,6 +989,10 @@ ao_usb_enable(void)
                        (0 << STM_USB_CNTR_PDWN) |
                        (0 << STM_USB_CNTR_FRES));
 
+       ao_arch_release_interrupts();
+
+       for (t = 0; t < 1000; t++)
+               ao_arch_nop();
        /* Enable USB pull-up */
        stm_syscfg.pmc |= (1 << STM_SYSCFG_PMC_USB_PU);
 }
@@ -1005,6 +1013,7 @@ ao_usb_echo(void)
 }
 #endif
 
+#if USB_DEBUG
 static void
 ao_usb_irq(void)
 {
@@ -1016,6 +1025,7 @@ __code struct ao_cmds ao_usb_cmds[] = {
        { ao_usb_irq, "I\0Show USB interrupt counts" },
        { 0, NULL }
 };
+#endif
 
 void
 ao_usb_init(void)
@@ -1027,7 +1037,9 @@ ao_usb_init(void)
 #if USB_ECHO
        ao_add_task(&ao_usb_echo_task, ao_usb_echo, "usb echo");
 #endif
+#if USB_DEBUG
        ao_cmd_register(&ao_usb_cmds[0]);
+#endif
 #if !USB_ECHO
        ao_add_stdio(ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);
 #endif