From d03beb22577b4b4ee01bcd02a5b3bb2e1b2b888d Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Sat, 11 Jun 2011 11:06:18 -0300 Subject: [PATCH] atusb/fw/: changed USB stack to use interrupts instead of polling Note: this change surprisingly _increases_ the DFU wait in the boot loader. Not yet sure why. - boot.c (main): move the interrupt vectors to the boot loader section - atusb.c (main): move the interrupt vectors to the application section - boot.c (main): enable global interrupts while looping (disable them before jumping to the application) - board_app.c (__timer_read, timer_read): removed wrapped since we're now always called with interrupts disabled - usb/atu2.c (ep_init): enable endpoint interrupts - usb/atu2.c (usb_init): enable device interrupts - usb/atu2.c (usb_poll, USB_GEN_vect, USB_COM_vect): moved poll loop code into separate handlers for device and endpoint interrupts - boot.c (main), atusb.c (main): removed call to usb_poll --- atusb/fw/atusb.c | 7 +++++-- atusb/fw/board_app.c | 13 +------------ atusb/fw/boot.c | 10 +++++++++- atusb/fw/usb/atu2.c | 19 +++++++++++++++---- atusb/fw/usb/usb.h | 1 - 5 files changed, 30 insertions(+), 20 deletions(-) diff --git a/atusb/fw/atusb.c b/atusb/fw/atusb.c index b51e058..0244713 100644 --- a/atusb/fw/atusb.c +++ b/atusb/fw/atusb.c @@ -37,8 +37,11 @@ int main(void) ep0_init(); timer_init(); + /* move interrupt vectors to 0 */ + MCUCR = 1 << IVCE; + MCUCR = 0; + sei(); - while (1) - usb_poll(); + while (1); } diff --git a/atusb/fw/board_app.c b/atusb/fw/board_app.c index 6ce0558..ba1dd67 100644 --- a/atusb/fw/board_app.c +++ b/atusb/fw/board_app.c @@ -51,7 +51,7 @@ ISR(TIMER1_OVF_vect) } -static uint64_t __timer_read(void) +uint64_t timer_read(void) { uint32_t high; uint8_t low, mid; @@ -75,17 +75,6 @@ static uint64_t __timer_read(void) } -uint64_t timer_read(void) -{ - uint64_t res; - - cli(); - res = __timer_read(); - sei(); - return res; -} - - void timer_init(void) { /* configure timer 1 as a free-running CLK counter */ diff --git a/atusb/fw/boot.c b/atusb/fw/boot.c index 19c5294..6b81524 100644 --- a/atusb/fw/boot.c +++ b/atusb/fw/boot.c @@ -14,6 +14,7 @@ #include #include +#include #include #define F_CPU 8000000UL @@ -50,10 +51,15 @@ int main(void) usb_init(); dfu_init(); + /* move interrupt vectors to the boot loader */ + MCUCR = 1 << IVCE; + MCUCR = 1 << IVSEL; + + sei(); + led(1); while (loop != MS_TO_LOOPS(2000)) { - usb_poll(); if (dfu.state == dfuIDLE && pgm_read_byte(zero) != 0xff) loop++; else @@ -62,6 +68,8 @@ int main(void) led(0); + cli(); + usb_reset(); run_payload(); diff --git a/atusb/fw/usb/atu2.c b/atusb/fw/usb/atu2.c index b755ebc..abb6de8 100644 --- a/atusb/fw/usb/atu2.c +++ b/atusb/fw/usb/atu2.c @@ -24,6 +24,7 @@ #include #include +#include #include "usb.h" #include "../board.h" @@ -177,14 +178,17 @@ static void ep_init(void) while (!(UESTA0X & (1 << CFGOK))); + UEIENX = + (1 << RXSTPE) | (1 << RXOUTE) | (1 << STALLEDE) | (1 << TXINE); + eps[0].state = EP_IDLE; eps[0].size = 64; } -void usb_poll(void) +ISR(USB_GEN_vect) { - uint8_t flags, i; + uint8_t flags; flags = UDINT; if (flags & (1 << EORSTI)) { @@ -193,11 +197,17 @@ void usb_poll(void) ep_init(); UDINT = ~(1 << EORSTI); } +} + + +ISR(USB_COM_vect) +{ + uint8_t flags, i; + flags = UEINT; for (i = 0; i != NUM_EPS; i++) - if (1 || flags & (1 << i)) + if (flags & (1 << i)) handle_ep(i); - /* @@@ USB bus reset */ } @@ -223,6 +233,7 @@ void usb_init(void) USBCON &= ~(1 << FRZCLK); /* thaw the clock */ UDCON &= ~(1 << DETACH); /* attach the pull-up */ + UDIEN = 1 << EORSTE; /* enable device interrupts */ // UDCON |= 1 << RSTCPU; /* reset CPU on bus reset */ ep_init(); diff --git a/atusb/fw/usb/usb.h b/atusb/fw/usb/usb.h index 8b7eebc..f6e7655 100644 --- a/atusb/fw/usb/usb.h +++ b/atusb/fw/usb/usb.h @@ -152,6 +152,5 @@ int handle_setup(const struct setup_request *setup); int set_addr(uint8_t addr); void usb_reset(void); void usb_init(void); -void usb_poll(void); #endif /* !USB_H */