aboutsummaryrefslogtreecommitdiffstats
path: root/libmaple
diff options
context:
space:
mode:
authorMarti Bolivar <mbolivar@leaflabs.com>2011-08-10 17:13:18 -0400
committerMarti Bolivar <mbolivar@leaflabs.com>2011-10-18 13:30:18 -0400
commitd4e9573d41e6a6601ba3f680487dd548b23bf680 (patch)
tree4902cc1cf8c157cea3b584f2638526687c2c3e4a /libmaple
parent00c5efa7b637ee6ef1878eb870eff140f2af94a8 (diff)
downloadlibrambutan-d4e9573d41e6a6601ba3f680487dd548b23bf680.tar.gz
librambutan-d4e9573d41e6a6601ba3f680487dd548b23bf680.zip
usb: Clean up some globals with new struct usblib_dev.
Add struct usblib_dev (and USBLIB, a pointer to the singleton) to usb.h. USBLIB contains the global state which is used by functionality imported from usb_lib/. Consolidating global state into USBLIB will make it easier to remove later. Initial fields in struct usblib_dev are endpoint interrupt callbacks, a mask for what to handle in the low-priority USB interrupt, and device state. These replace pEpInt_IN, pEpInt_OUT; wInterrupt_Mask; and bDeviceState, respectively from usb_lib/, so remove their declarations from usb_lib_globals.h accordingly. Also remove unused SaveState declaration from usblib_globals.h. Move bDeviceState into 'state' field in usblib_dev. Device state type changes from DEVICE_STATE to usb_dev_state, volatile gets dropped, and enumerators get a 'USB_' prefix, but it's otherwise the same. usb_lib/ expects pInformation to point to Device_Info, pUser_Standard_Requests -> User_Standard_Requests, and pProperty -> Device_Property. Alter usb_init_usblib() to reflect these assumptions. Reorganize usb_lib_globals.h to make these assumptions more apparent to the reader. Modify usb_init_usblib() to take endpoint callbacks as arguments; update its caller in usb_cdcacm.c. usb_lib/ defines pInformation, pProperty, and pUser_Standard_Requests itself (in usb_init.c), but we have our own definitions (in usb.c). Remove the duplicates from usb.c. Also remove EPindex and Device_Info definitions from usb.c. Unused, and anyways already defined in usb_lib/usb_init.c. Signed-off-by: Marti Bolivar <mbolivar@leaflabs.com>
Diffstat (limited to 'libmaple')
-rw-r--r--libmaple/usb/usb.c54
-rw-r--r--libmaple/usb/usb.h35
-rw-r--r--libmaple/usb/usb_cdcacm.c63
-rw-r--r--libmaple/usb/usb_lib_globals.h21
4 files changed, 94 insertions, 79 deletions
diff --git a/libmaple/usb/usb.c b/libmaple/usb/usb.c
index 0206bb8..6a22cdf 100644
--- a/libmaple/usb/usb.c
+++ b/libmaple/usb/usb.c
@@ -49,21 +49,14 @@ static void dispatch_ctr_lp(void);
* usb_lib/ globals
*/
-volatile uint16 wIstr = 0;
-uint8 EPindex; /* current endpoint */
-DEVICE_INFO *pInformation;
-DEVICE_PROP *pProperty;
+volatile uint16 wIstr = 0; /* most recently read value of wIstr */
uint16 SaveTState; /* caches TX status for later use */
uint16 SaveRState; /* caches RX status for later use */
-uint16 wInterrupt_Mask;
-DEVICE_INFO Device_Info;
-USER_STANDARD_REQUESTS *pUser_Standard_Requests;
/*
* Other state
*/
-volatile uint32 bDeviceState = UNCONNECTED;
volatile uint32 bIntPackSOF = 0;
struct {
@@ -71,18 +64,31 @@ struct {
volatile uint8 bESOFcnt;
} ResumeS;
+static usblib_dev usblib = {
+ .irq_mask = USB_ISR_MSK,
+ .state = USB_UNCONNECTED,
+};
+usblib_dev *USBLIB = &usblib;
+
/*
* Routines
*/
-void usb_init_usblib(DEVICE_PROP *device, USER_STANDARD_REQUESTS *user) {
+void usb_init_usblib(void (**ep_int_in)(void), void (**ep_int_out)(void)) {
rcc_clk_enable(RCC_USB);
+ USBLIB->ep_int_in = ep_int_in;
+ USBLIB->ep_int_out = ep_int_out;
+
+ /* usb_lib/ declares both and then assumes that pFoo points to Foo
+ * (even though the names don't always match), which is stupid for
+ * all of the obvious reasons, but whatever. Here we are. */
pInformation = &Device_Info;
+ pProperty = &Device_Property;
+ pUser_Standard_Requests = &User_Standard_Requests;
+
pInformation->ControlState = 2; /* FIXME [0.0.12] use
CONTROL_STATE enumerator */
- pProperty = device;
- pUser_Standard_Requests = user;
pProperty->Init();
}
@@ -97,7 +103,7 @@ void usbSuspend(void) {
cntr |= USB_CNTR_LP_MODE;
USB_BASE->CNTR = cntr;
- bDeviceState = SUSPENDED;
+ USBLIB->state = USB_SUSPENDED;
}
void usbResumeInit(void) {
@@ -167,33 +173,33 @@ void __irq_usb_lp_can_rx0(void) {
/* Use USB_ISR_MSK to only include code for bits we care about. */
#if (USB_ISR_MSK & USB_ISTR_RESET)
- if (wIstr & USB_ISTR_RESET & wInterrupt_Mask) {
+ if (wIstr & USB_ISTR_RESET & USBLIB->irq_mask) {
USB_BASE->ISTR = ~USB_ISTR_RESET;
pProperty->Reset();
}
#endif
#if (USB_ISR_MSK & USB_ISTR_PMAOVR)
- if (wIstr & ISTR_PMAOVR & wInterrupt_Mask) {
+ if (wIstr & ISTR_PMAOVR & USBLIB->irq_mask) {
USB_BASE->ISTR = ~USB_ISTR_PMAOVR;
}
#endif
#if (USB_ISR_MSK & USB_ISTR_ERR)
- if (wIstr & USB_ISTR_ERR & wInterrupt_Mask) {
+ if (wIstr & USB_ISTR_ERR & USBLIB->irq_mask) {
USB_BASE->ISTR = ~USB_ISTR_ERR;
}
#endif
#if (USB_ISR_MSK & USB_ISTR_WKUP)
- if (wIstr & USB_ISTR_WKUP & wInterrupt_Mask) {
+ if (wIstr & USB_ISTR_WKUP & USBLIB->irq_mask) {
USB_BASE->ISTR = ~USB_ISTR_WKUP;
usbResume(RESUME_EXTERNAL);
}
#endif
#if (USB_ISR_MSK & USB_ISTR_SUSP)
- if (wIstr & USB_ISTR_SUSP & wInterrupt_Mask) {
+ if (wIstr & USB_ISTR_SUSP & USBLIB->irq_mask) {
/* check if SUSPEND is possible */
if (SUSPEND_ENABLED) {
usbSuspend();
@@ -207,14 +213,14 @@ void __irq_usb_lp_can_rx0(void) {
#endif
#if (USB_ISR_MSK & USB_ISTR_SOF)
- if (wIstr & USB_ISTR_SOF & wInterrupt_Mask) {
+ if (wIstr & USB_ISTR_SOF & USBLIB->irq_mask) {
USB_BASE->ISTR = ~USB_ISTR_SOF;
bIntPackSOF++;
}
#endif
#if (USB_ISR_MSK & USB_ISTR_ESOF)
- if (wIstr & USB_ISTR_ESOF & wInterrupt_Mask) {
+ if (wIstr & USB_ISTR_ESOF & USBLIB->irq_mask) {
USB_BASE->ISTR = ~USB_ISTR_ESOF;
/* resume handling timing is made with ESOFs */
usbResume(RESUME_ESOF); /* request without change of the machine state */
@@ -226,7 +232,7 @@ void __irq_usb_lp_can_rx0(void) {
*/
#if (USB_ISR_MSK & USB_ISTR_CTR)
- if (wIstr & USB_ISTR_CTR & wInterrupt_Mask) {
+ if (wIstr & USB_ISTR_CTR & USBLIB->irq_mask) {
dispatch_ctr_lp();
}
#endif
@@ -239,11 +245,11 @@ void usbWaitReset(void) {
}
uint8 usbIsConfigured() {
- return (bDeviceState == CONFIGURED);
+ return USBLIB->state == USB_CONFIGURED;
}
uint8 usbIsConnected() {
- return (bDeviceState != UNCONNECTED);
+ return USBLIB->state != USB_UNCONNECTED;
}
/*
@@ -349,11 +355,11 @@ static inline void dispatch_endpt(uint8 ep) {
* TODO try to find out if neither being set is possible. */
if (epr & USB_EP_CTR_RX) {
usb_clear_ctr_rx(ep);
- (pEpInt_OUT[ep - 1])();
+ (USBLIB->ep_int_out[ep - 1])();
}
if (epr & USB_EP_CTR_TX) {
usb_clear_ctr_tx(ep);
- (pEpInt_IN[ep - 1])();
+ (USBLIB->ep_int_in[ep - 1])();
}
}
diff --git a/libmaple/usb/usb.h b/libmaple/usb/usb.h
index 8ab40f0..b073156 100644
--- a/libmaple/usb/usb.h
+++ b/libmaple/usb/usb.h
@@ -39,6 +39,25 @@ extern "C" {
#define USB_ISR_MSK 0xBF00
#endif
+typedef enum usb_dev_state {
+ USB_UNCONNECTED,
+ USB_ATTACHED,
+ USB_POWERED,
+ USB_SUSPENDED,
+ USB_ADDRESSED,
+ USB_CONFIGURED
+} usb_dev_state;
+
+/* Encapsulates global state formerly handled by usb_lib/
+ * functionality */
+typedef struct usblib_dev {
+ uint32 irq_mask;
+ void (**ep_int_in)(void);
+ void (**ep_int_out)(void);
+ usb_dev_state state;
+} usblib_dev;
+
+extern usblib_dev *USBLIB;
/*
* Convenience routines, etc.
@@ -55,21 +74,7 @@ typedef enum {
RESUME_ESOF
} RESUME_STATE;
-typedef enum {
- UNCONNECTED,
- ATTACHED,
- POWERED,
- SUSPENDED,
- ADDRESSED,
- CONFIGURED
-} DEVICE_STATE;
-
-extern volatile uint32 bDeviceState;
-
-struct _DEVICE_PROP;
-struct _USER_STANDARD_REQUESTS;
-void usb_init_usblib(struct _DEVICE_PROP*,
- struct _USER_STANDARD_REQUESTS*);
+void usb_init_usblib(void (**ep_int_in)(void), void (**ep_int_out)(void));
void usbSuspend(void);
void usbResumeInit(void);
diff --git a/libmaple/usb/usb_cdcacm.c b/libmaple/usb/usb_cdcacm.c
index bf73c68..46d56bb 100644
--- a/libmaple/usb/usb_cdcacm.c
+++ b/libmaple/usb/usb_cdcacm.c
@@ -118,6 +118,11 @@ typedef enum {
DTR_LOW
} RESET_STATE;
+/* Some forward-declared callbacks */
+void vcomDataTxCb(void);
+void vcomDataRxCb(void);
+void vcomManagementCb(void);
+
const USB_Descriptor_Device usbVcomDescriptor_Device = {
.bLength = sizeof(USB_Descriptor_Device),
.bDescriptorType = USB_DESCRIPTOR_TYPE_DEVICE,
@@ -302,6 +307,24 @@ volatile uint32 newBytes = 0;
RESET_STATE reset_state = DTR_UNSET;
uint8 line_dtr_rts = 0;
+static void (*ep_int_in[7])(void) =
+ {vcomDataTxCb,
+ vcomManagementCb,
+ NOP_Process,
+ NOP_Process,
+ NOP_Process,
+ NOP_Process,
+ NOP_Process};
+
+static void (*ep_int_out[7])(void) =
+ {NOP_Process,
+ NOP_Process,
+ vcomDataRxCb,
+ NOP_Process,
+ NOP_Process,
+ NOP_Process,
+ NOP_Process};
+
/*
* VCOM callbacks
*/
@@ -388,11 +411,11 @@ void vcomSetLineState(void) {
RESULT usbPowerOn(void) {
USB_BASE->CNTR = USB_CNTR_FRES;
- wInterrupt_Mask = 0;
- USB_BASE->CNTR = wInterrupt_Mask;
+ USBLIB->irq_mask = 0;
+ USB_BASE->CNTR = USBLIB->irq_mask;
USB_BASE->ISTR = 0;
- wInterrupt_Mask = USB_CNTR_RESETM | USB_CNTR_SUSPM | USB_CNTR_WKUPM;
- USB_BASE->CNTR = wInterrupt_Mask;
+ USBLIB->irq_mask = USB_CNTR_RESETM | USB_CNTR_SUSPM | USB_CNTR_WKUPM;
+ USB_BASE->CNTR = USBLIB->irq_mask;
return USB_SUCCESS;
}
@@ -402,11 +425,11 @@ void usbInit(void) {
usbPowerOn();
USB_BASE->ISTR = 0;
- wInterrupt_Mask = USB_ISR_MSK;
- USB_BASE->CNTR = wInterrupt_Mask;
+ USBLIB->irq_mask = USB_ISR_MSK;
+ USB_BASE->CNTR = USBLIB->irq_mask;
nvic_irq_enable(NVIC_USB_LP_CAN_RX0);
- bDeviceState = UNCONNECTED;
+ USBLIB->state = USB_UNCONNECTED;
}
/* choose addresses to give endpoints the max 64 byte buffers */
@@ -450,7 +473,7 @@ void usbReset(void) {
usb_set_ep_tx_stat(VCOM_TX_ENDP, USB_EP_STAT_TX_NAK);
usb_set_ep_rx_stat(VCOM_TX_ENDP, USB_EP_STAT_RX_DISABLED);
- bDeviceState = ATTACHED;
+ USBLIB->state = USB_ATTACHED;
SetDeviceAddress(0);
/* reset the rx fifo */
@@ -585,12 +608,12 @@ u8* usbGetStringDescriptor(u16 length) {
/* internal callbacks to respond to standard requests */
void usbSetConfiguration(void) {
if (pInformation->Current_Configuration != 0) {
- bDeviceState = CONFIGURED;
+ USBLIB->state = USB_CONFIGURED;
}
}
void usbSetDeviceAddress(void) {
- bDeviceState = ADDRESSED;
+ USBLIB->state = USB_ADDRESSED;
}
/*
@@ -628,24 +651,6 @@ USER_STANDARD_REQUESTS User_Standard_Requests =
NOP_Process,
usbSetDeviceAddress};
-void (*pEpInt_IN[7])(void) =
- {vcomDataTxCb,
- vcomManagementCb,
- NOP_Process,
- NOP_Process,
- NOP_Process,
- NOP_Process,
- NOP_Process};
-
-void (*pEpInt_OUT[7])(void) =
- {NOP_Process,
- NOP_Process,
- vcomDataRxCb,
- NOP_Process,
- NOP_Process,
- NOP_Process,
- NOP_Process};
-
/*
* CDC ACM routines
*/
@@ -656,7 +661,7 @@ void usb_cdcacm_enable(gpio_dev *disc_dev, uint8 disc_bit) {
gpio_write_bit(disc_dev, disc_bit, 0); // presents us to the host
/* initialize USB peripheral */
- usb_init_usblib(&Device_Property, &User_Standard_Requests);
+ usb_init_usblib(ep_int_in, ep_int_out);
}
void usb_cdcacm_disable(gpio_dev *disc_dev, uint8 disc_bit) {
diff --git a/libmaple/usb/usb_lib_globals.h b/libmaple/usb/usb_lib_globals.h
index 854cb12..a494817 100644
--- a/libmaple/usb/usb_lib_globals.h
+++ b/libmaple/usb/usb_lib_globals.h
@@ -34,19 +34,18 @@
extern "C" {
#endif
-extern void (*pEpInt_IN[7])(void);
-extern void (*pEpInt_OUT[7])(void);
-extern DEVICE_PROP Device_Property;
-extern USER_STANDARD_REQUESTS User_Standard_Requests;
-extern DEVICE Device_Table;
-extern DEVICE_INFO Device_Info;
+extern USER_STANDARD_REQUESTS User_Standard_Requests;
+extern USER_STANDARD_REQUESTS *pUser_Standard_Requests;
+
+extern DEVICE_PROP Device_Property;
+extern DEVICE_PROP *pProperty;
+
+extern DEVICE_INFO Device_Info;
+extern DEVICE_INFO *pInformation;
+
+extern DEVICE Device_Table;
extern u16 SaveRState;
extern u16 SaveTState;
-extern DEVICE_INFO* pInformation;
-extern DEVICE_PROP* pProperty;
-extern USER_STANDARD_REQUESTS *pUser_Standard_Requests;
-extern u16 SaveState ;
-extern u16 wInterrupt_Mask;
#ifdef __cplusplus
}