diff --git a/ChangeLog b/ChangeLog index ecb47ad..7548819 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +2012-05-22 Niibe Yutaka + + * src/usb_lld.c: Support FREE_STANDING environment as well as + under ChibiOS/RT. + (usb_lld_init): Call usb_lld_sys_init. Don't call user defined + method. Call usb_lld_set_configuration. + (usb_lld_shutdown): Call usb_lld_sys_shutdown. + (Vector90): Move to usb_lld_sys.c. + (usb_interrupt_handler): Export to global. + + * src/usb_lld_sys.c: New. + + * src/usb_prop.c (Device_Method): Remove gnuk_device_init. + (gnuk_device_init): Remove. + 2012-05-19 Niibe Yutaka * src/usb_lld.c (handle_datastage_in): Bug fix, erable RX when diff --git a/src/Makefile.in b/src/Makefile.in index 4237c36..0c6e20f 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -81,7 +81,7 @@ CSRC = $(PORTSRC) \ $(CHIBIOS)/os/various/evtimer.c \ $(CHIBIOS)/os/various/syscalls.c \ $(CRYPTSRC) \ - main.c usb_lld.c \ + main.c usb_lld.c usb_lld_sys.c \ usb_desc.c usb_prop.c \ usb-icc.c openpgp.c ac.c openpgp-do.c flash.c \ random.c neug.c diff --git a/src/usb_lld.c b/src/usb_lld.c index 948f3a7..578f764 100644 --- a/src/usb_lld.c +++ b/src/usb_lld.c @@ -1,5 +1,10 @@ +#ifdef FREE_STANDING +#include "types.h" +#else #include "ch.h" #include "hal.h" +#endif + #include "usb_lld.h" #define USB_MAX_PACKET_SIZE 64 /* For FS device */ @@ -66,9 +71,15 @@ struct DEVICE_INFO uint8_t state; }; -static struct CONTROL_INFO *ctrl_p; -static struct DEVICE_INFO *dev_p; -static struct DATA_INFO *data_p; +static struct CONTROL_INFO control_info; +static struct DEVICE_INFO device_info; +static struct DATA_INFO data_info; +extern const struct usb_device_method Device_Method; + +static struct CONTROL_INFO *const ctrl_p = &control_info; +static struct DEVICE_INFO *const dev_p = &device_info; +static struct DATA_INFO *const data_p = &data_info; +static const struct usb_device_method *const method_p = &Device_Method; #define REG_BASE (0x40005C00UL) /* USB_IP Peripheral Registers base address */ #define PMA_ADDR (0x40006000UL) /* USB_IP Packet Memory Area base address */ @@ -347,9 +358,36 @@ static void st103_ep_clear_dtog_tx (uint8_t ep_num) } } -static const struct usb_device_method* method_p; +void usb_lld_init (void) +{ + usb_lld_sys_init (); -static void + dev_p->state = IN_DATA; + + usb_lld_set_configuration (0); + + /* Reset USB */ + st103_set_cntr (CNTR_FRES); + st103_set_cntr (0); + + /* Clear Interrupt Status Register, and enable interrupt for USB */ + st103_set_istr (0); + st103_set_cntr (CNTR_CTRM | CNTR_RESETM); +} + +void usb_lld_prepare_shutdown (void) +{ + st103_set_istr (0); + st103_set_cntr (0); +} + +void usb_lld_shutdown (void) +{ + st103_set_cntr (CNTR_PDWN); + usb_lld_sys_shutdown (); +} + +void usb_interrupt_handler (void) { uint16_t istr_value = st103_get_istr (); @@ -370,16 +408,6 @@ usb_interrupt_handler (void) st103_set_istr (CLR_ERR); } -CH_IRQ_HANDLER (Vector90) { - CH_IRQ_PROLOGUE(); - chSysLockFromIsr(); - - usb_interrupt_handler (); - - chSysUnlockFromIsr(); - CH_IRQ_EPILOGUE(); -} - static void handle_datastage_out (void) { if (data_p->addr && data_p->len) @@ -903,7 +931,7 @@ static void nop_proc (void) { } -#define WEAK __attribute__ ((weak)) +#define WEAK __attribute__ ((weak, alias ("nop_proc"))) void WEAK EP1_IN_Callback (void); void WEAK EP2_IN_Callback (void); void WEAK EP3_IN_Callback (void); @@ -920,22 +948,6 @@ void WEAK EP5_OUT_Callback (void); void WEAK EP6_OUT_Callback (void); void WEAK EP7_OUT_Callback (void); -#pragma weak EP1_IN_Callback = nop_proc -#pragma weak EP2_IN_Callback = nop_proc -#pragma weak EP3_IN_Callback = nop_proc -#pragma weak EP4_IN_Callback = nop_proc -#pragma weak EP5_IN_Callback = nop_proc -#pragma weak EP6_IN_Callback = nop_proc -#pragma weak EP7_IN_Callback = nop_proc - -#pragma weak EP1_OUT_Callback = nop_proc -#pragma weak EP2_OUT_Callback = nop_proc -#pragma weak EP3_OUT_Callback = nop_proc -#pragma weak EP4_OUT_Callback = nop_proc -#pragma weak EP5_OUT_Callback = nop_proc -#pragma weak EP6_OUT_Callback = nop_proc -#pragma weak EP7_OUT_Callback = nop_proc - void (*const ep_intr_handler_IN[7]) (void) = { EP1_IN_Callback, EP2_IN_Callback, @@ -1018,58 +1030,12 @@ usb_handle_transfer (void) } } -static struct CONTROL_INFO Control_Info; -static struct DEVICE_INFO Device_Info; -static struct DATA_INFO Data_Info; - void usb_lld_reset (void) { st103_set_btable (); st103_set_daddr (0); } -void usb_lld_init (void) -{ - RCC->APB1ENR |= RCC_APB1ENR_USBEN; - NVICEnableVector (USB_LP_CAN1_RX0_IRQn, - CORTEX_PRIORITY_MASK (STM32_USB_IRQ_PRIORITY)); - /* - * Note that we also have other IRQ(s): - * USB_HP_CAN1_TX_IRQn (for double-buffered or isochronous) - * USBWakeUp_IRQn (suspend/resume) - */ - RCC->APB1RSTR = RCC_APB1RSTR_USBRST; - RCC->APB1RSTR = 0; - - dev_p = &Device_Info; - ctrl_p = &Control_Info; - data_p = &Data_Info; - dev_p->state = IN_DATA; - method_p = &Device_Method; - - method_p->init(); - - /* Reset USB */ - st103_set_cntr (CNTR_FRES); - st103_set_cntr (0); - - /* Clear Interrupt Status Register, and enable interrupt for USB */ - st103_set_istr (0); - st103_set_cntr (CNTR_CTRM | CNTR_RESETM); -} - -void usb_lld_prepare_shutdown (void) -{ - st103_set_istr (0); - st103_set_cntr (0); -} - -void usb_lld_shutdown (void) -{ - st103_set_cntr (CNTR_PDWN); - RCC->APB1ENR &= ~RCC_APB1ENR_USBEN; -} - void usb_lld_txcpy (const void *src, int ep_num, int offset, size_t len) { diff --git a/src/usb_lld.h b/src/usb_lld.h index 2652dad..cffbfbe 100644 --- a/src/usb_lld.h +++ b/src/usb_lld.h @@ -63,7 +63,6 @@ enum struct usb_device_method { - void (*init) (void); void (*reset) (void); void (*ctrl_write_finish) (uint8_t req, uint8_t req_no, uint16_t value, uint16_t index, uint16_t len); @@ -155,3 +154,8 @@ extern inline void usb_lld_set_data_to_recv (void *p, size_t len) extern void usb_lld_prepare_shutdown (void); extern void usb_lld_shutdown (void); + +extern void usb_interrupt_handler (void); + +extern void usb_lld_sys_init (void); +extern void usb_lld_sys_shutdown (void); diff --git a/src/usb_lld_sys.c b/src/usb_lld_sys.c new file mode 100644 index 0000000..be78d7e --- /dev/null +++ b/src/usb_lld_sys.c @@ -0,0 +1,34 @@ +#include "ch.h" +#include "hal.h" +#include "usb_lld.h" + +CH_IRQ_HANDLER (Vector90) { + CH_IRQ_PROLOGUE(); + chSysLockFromIsr(); + + usb_interrupt_handler (); + + chSysUnlockFromIsr(); + CH_IRQ_EPILOGUE(); +} + +void usb_lld_sys_init (void) +{ + RCC->APB1ENR |= RCC_APB1ENR_USBEN; + NVICEnableVector (USB_LP_CAN1_RX0_IRQn, + CORTEX_PRIORITY_MASK (STM32_USB_IRQ_PRIORITY)); + /* + * Note that we also have other IRQ(s): + * USB_HP_CAN1_TX_IRQn (for double-buffered or isochronous) + * USBWakeUp_IRQn (suspend/resume) + */ + RCC->APB1RSTR = RCC_APB1RSTR_USBRST; + RCC->APB1RSTR = 0; + + USB_Cable_Config (1); +} + +void usb_lld_sys_shutdown (void) +{ + RCC->APB1ENR &= ~RCC_APB1ENR_USBEN; +} diff --git a/src/usb_prop.c b/src/usb_prop.c index a7ca500..df4ac0c 100644 --- a/src/usb_prop.c +++ b/src/usb_prop.c @@ -91,14 +91,6 @@ vcom_port_data_setup (uint8_t req, uint8_t req_no) uint32_t bDeviceState = UNCONNECTED; /* USB device status */ -static void -gnuk_device_init (void) -{ - usb_lld_set_configuration (0); - USB_Cable_Config (1); - bDeviceState = UNCONNECTED; -} - static void gnuk_setup_endpoints_for_interface (uint16_t interface, int stop) { @@ -423,7 +415,6 @@ static int gnuk_interface (uint8_t cmd, uint16_t interface, uint16_t alt) */ const struct usb_device_method Device_Method = { - gnuk_device_init, gnuk_device_reset, gnuk_ctrl_write_finish, gnuk_setup,