diff options
author | Marti Bolivar <mbolivar@mit.edu> | 2010-10-11 23:39:50 -0400 |
---|---|---|
committer | Marti Bolivar <mbolivar@mit.edu> | 2010-10-11 23:39:50 -0400 |
commit | ba02b40212cb1aebd66990f76294c09e35217e93 (patch) | |
tree | 93a190b5a35602a1570d77f57ed2eb6a3cc4b8e2 /libmaple/usb | |
parent | fccf017f3a5aa7182080c1ed062ab46773a8f34a (diff) | |
download | librambutan-ba02b40212cb1aebd66990f76294c09e35217e93.tar.gz librambutan-ba02b40212cb1aebd66990f76294c09e35217e93.zip |
more sphinx docs
Diffstat (limited to 'libmaple/usb')
-rw-r--r-- | libmaple/usb/descriptors.c | 48 | ||||
-rw-r--r-- | libmaple/usb/usb.c | 204 | ||||
-rw-r--r-- | libmaple/usb/usb.h | 92 | ||||
-rw-r--r-- | libmaple/usb/usb_hardware.c | 28 |
4 files changed, 198 insertions, 174 deletions
diff --git a/libmaple/usb/descriptors.c b/libmaple/usb/descriptors.c index 5038709..360e6dd 100644 --- a/libmaple/usb/descriptors.c +++ b/libmaple/usb/descriptors.c @@ -1,12 +1,35 @@ -/* insert license */ - +/****************************************************************************** + * The MIT License + * + * Copyright (c) 2010 LeafLabs LLC. + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + *****************************************************************************/ #include "descriptors.h" #include "libmaple.h" #include "usb_config.h" const USB_Descriptor_Device usbVcomDescriptor_Device = { - bLength: sizeof(USB_Descriptor_Device), + bLength: sizeof(USB_Descriptor_Device), bDescriptorType: USB_DESCRIPTOR_TYPE_DEVICE, bcdUSB: 0x0200, bDeviceClass: USB_DEVICE_CLASS_CDC, @@ -29,7 +52,8 @@ const USB_Descriptor_Config usbVcomDescriptor_Config = { bNumInterfaces: 0x02, bConfigurationValue: 0x01, iConfiguration: 0x00, - bmAttributes: (USB_CONFIG_ATTR_BUSPOWERED | USB_CONFIG_ATTR_SELF_POWERED), + bmAttributes: (USB_CONFIG_ATTR_BUSPOWERED | + USB_CONFIG_ATTR_SELF_POWERED), bMaxPower: USB_CONFIG_MAX_POWER, CCI_Interface: @@ -77,7 +101,7 @@ const USB_Descriptor_Config usbVcomDescriptor_Config = { Data: {0x00, 0x01} }, - // ManagementEndpoint: + // ManagementEndpoint: // { EP1_bLength: 0x07,//sizeof(USB_Descriptor_Endpoint), EP1_bDescriptorType: USB_DESCRIPTOR_TYPE_ENDPOINT, @@ -126,12 +150,12 @@ const USB_Descriptor_Config usbVcomDescriptor_Config = { // } }; -/* +/* String Identifiers: we may choose to specify any or none of the following string identifiers: - + iManufacturer: LeafLabs iProduct: Maple R3 iSerialNumber: NONE @@ -143,7 +167,7 @@ const USB_Descriptor_Config usbVcomDescriptor_Config = { which is 0x0409 for US English */ -const uint8 usbVcomDescriptor_LangID[USB_DESCRIPTOR_STRING_LEN(1)] = +const uint8 usbVcomDescriptor_LangID[USB_DESCRIPTOR_STRING_LEN(1)] = { USB_DESCRIPTOR_STRING_LEN(1), USB_DESCRIPTOR_TYPE_STRING, @@ -151,18 +175,18 @@ const uint8 usbVcomDescriptor_LangID[USB_DESCRIPTOR_STRING_LEN(1)] = 0x04 }; -const uint8 usbVcomDescriptor_iManufacturer[USB_DESCRIPTOR_STRING_LEN(8)] = +const uint8 usbVcomDescriptor_iManufacturer[USB_DESCRIPTOR_STRING_LEN(8)] = { USB_DESCRIPTOR_STRING_LEN(8), USB_DESCRIPTOR_TYPE_STRING, - 'L', 0, 'e', 0, 'a', 0, 'f', 0, + 'L', 0, 'e', 0, 'a', 0, 'f', 0, 'L', 0, 'a', 0, 'b', 0, 's', 0 }; -const uint8 usbVcomDescriptor_iProduct[USB_DESCRIPTOR_STRING_LEN(8)] = +const uint8 usbVcomDescriptor_iProduct[USB_DESCRIPTOR_STRING_LEN(8)] = { USB_DESCRIPTOR_STRING_LEN(8), USB_DESCRIPTOR_TYPE_STRING, - 'M', 0, 'a', 0, 'p', 0, 'l', 0, + 'M', 0, 'a', 0, 'p', 0, 'l', 0, 'e', 0, ' ', 0, 'R', 0, '3', 0 }; diff --git a/libmaple/usb/usb.c b/libmaple/usb/usb.c index 026d7f0..923e54b 100644 --- a/libmaple/usb/usb.c +++ b/libmaple/usb/usb.c @@ -1,30 +1,30 @@ -/* ***************************************************************************** +/****************************************************************************** * The MIT License * * Copyright (c) 2010 LeafLabs LLC. * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * ****************************************************************************/ + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + *****************************************************************************/ /** - * @file usb.c - * * @brief usb-specific hardware setup, NVIC, clocks, and usb activities * in the pre-attached state. includes some of the lower level callbacks * needed by the usb library, like suspend,resume,init,etc @@ -47,87 +47,75 @@ volatile uint16 wIstr = 0; volatile bIntPackSOF = 0; DEVICE Device_Table = - { - NUM_ENDPTS, - 1 - }; + {NUM_ENDPTS, + 1}; DEVICE_PROP Device_Property = - { - usbInit, - usbReset, - usbStatusIn, - usbStatusOut, - usbDataSetup, - usbNoDataSetup, - usbGetInterfaceSetting, - usbGetDeviceDescriptor, - usbGetConfigDescriptor, - usbGetStringDescriptor, - 0, - bMaxPacketSize - }; + {usbInit, + usbReset, + usbStatusIn, + usbStatusOut, + usbDataSetup, + usbNoDataSetup, + usbGetInterfaceSetting, + usbGetDeviceDescriptor, + usbGetConfigDescriptor, + usbGetStringDescriptor, + 0, + bMaxPacketSize}; USER_STANDARD_REQUESTS User_Standard_Requests = - { - NOP_Process, - usbSetConfiguration, - NOP_Process, - NOP_Process, - NOP_Process, - NOP_Process, - NOP_Process, - NOP_Process, - usbSetDeviceAddress - }; + {NOP_Process, + usbSetConfiguration, + NOP_Process, + NOP_Process, + NOP_Process, + NOP_Process, + NOP_Process, + NOP_Process, + usbSetDeviceAddress}; void (*pEpInt_IN[7])(void) = -{ - vcomDataTxCb, - vcomManagementCb, - NOP_Process, - NOP_Process, - NOP_Process, - NOP_Process, - NOP_Process, -}; + {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, -}; - -struct -{ + {NOP_Process, + NOP_Process, + vcomDataRxCb, + NOP_Process, + NOP_Process, + NOP_Process, + NOP_Process}; + +struct { volatile RESUME_STATE eState; volatile uint8 bESOFcnt; } ResumeS; void setupUSB (void) { gpio_set_mode(USB_DISC_BANK, - USB_DISC_PIN, - GPIO_MODE_OUTPUT_PP); + USB_DISC_PIN, + GPIO_MODE_OUTPUT_PP); /* setup the apb1 clock for USB */ pRCC->APB1ENR |= 0x00800000; /* initialize the usb application */ - gpio_write_bit(USB_DISC_BANK, USB_DISC_PIN, 0); /* present ourselves to the host */ - - USB_Init(); /* low level init routine provided by st lib */ + gpio_write_bit(USB_DISC_BANK, USB_DISC_PIN, 0); // presents us to the host + USB_Init(); // low level init routine provided by the ST library } void disableUSB (void) { // These are just guesses about how to do this // TODO: real disable function usbDsbISR(); - gpio_write_bit(USB_DISC_BANK,USB_DISC_PIN,1); + gpio_write_bit(USB_DISC_BANK,USB_DISC_PIN,1); } void usbSuspend(void) { @@ -188,13 +176,12 @@ void usbResume(RESUME_STATE eResumeSetVal) { break; case RESUME_ON: ResumeS.bESOFcnt--; - if (ResumeS.bESOFcnt == 0) - { - wCNTR = _GetCNTR(); - wCNTR &= (~CNTR_RESUME); - _SetCNTR(wCNTR); - ResumeS.eState = RESUME_OFF; - } + if (ResumeS.bESOFcnt == 0) { + wCNTR = _GetCNTR(); + wCNTR &= (~CNTR_RESUME); + _SetCNTR(wCNTR); + ResumeS.eState = RESUME_OFF; + } break; case RESUME_OFF: case RESUME_ESOF: @@ -213,7 +200,7 @@ RESULT usbPowerOn(void) { wInterrupt_Mask = 0; _SetCNTR(wInterrupt_Mask); _SetISTR(0); - wInterrupt_Mask = CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM; /* the bare minimum */ + wInterrupt_Mask = CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM; // the bare minimum _SetCNTR(wInterrupt_Mask); return USB_SUCCESS; @@ -284,60 +271,51 @@ if (wIstr & ISTR_ERR & wInterrupt_Mask) #if (ISR_MSK & ISTR_WKUP) -if (wIstr & ISTR_WKUP & wInterrupt_Mask) - { +if (wIstr & ISTR_WKUP & wInterrupt_Mask) { _SetISTR((u16)CLR_WKUP); usbResume(RESUME_EXTERNAL); - } +} #endif /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ #if (ISR_MSK & ISTR_SUSP) -if (wIstr & ISTR_SUSP & wInterrupt_Mask) - { - +if (wIstr & ISTR_SUSP & wInterrupt_Mask) { /* check if SUSPEND is possible */ - if (F_SUSPEND_ENABLED) - { - usbSuspend(); - } - else - { - /* if not possible then resume after xx ms */ - usbResume(RESUME_LATER); - } + if (F_SUSPEND_ENABLED) { + usbSuspend(); + } else { + /* if not possible then resume after xx ms */ + usbResume(RESUME_LATER); + } /* clear of the ISTR bit must be done after setting of CNTR_FSUSP */ _SetISTR((u16)CLR_SUSP); - } +} #endif #if (ISR_MSK & ISTR_SOF) -if (wIstr & ISTR_SOF & wInterrupt_Mask) - { +if (wIstr & ISTR_SOF & wInterrupt_Mask) { _SetISTR((u16)CLR_SOF); bIntPackSOF++; - } + } #endif #if (ISR_MSK & ISTR_ESOF) -if (wIstr & ISTR_ESOF & wInterrupt_Mask) - { +if (wIstr & ISTR_ESOF & wInterrupt_Mask) { _SetISTR((u16)CLR_ESOF); /* resume handling timing is made with ESOFs */ usbResume(RESUME_ESOF); /* request without change of the machine state */ - } + } #endif /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ #if (ISR_MSK & ISTR_CTR) -if (wIstr & ISTR_CTR & wInterrupt_Mask) - { +if (wIstr & ISTR_CTR & wInterrupt_Mask) { /* servicing of the endpoint correct transfer interrupt */ /* clear of the CTR flag into the sub */ CTR_LP(); /* low priority ISR defined in the usb core lib */ - } + } #endif } @@ -349,7 +327,7 @@ void usbWaitReset(void) { /* This low-level send bytes function is NON-BLOCKING; blocking behavior, with * a timeout, is implemented in usercode (or in the Wirish C++ high level - * implementation). + * implementation). * * This function will quickly copy up to 64 bytes of data (out of an * arbitrarily large buffer) into the USB peripheral TX buffer and return the @@ -363,28 +341,28 @@ void usbWaitReset(void) { * context means that an actual program on the Host operating system is * connected to the virtual COM/ttyACM device and is recieving the bytes; the * Host operating system is almost always configured and keeping this endpoint - * alive, but the bytes never get read out of the endpoint buffer. + * alive, but the bytes never get read out of the endpoint buffer. * * The behavior of this function is subtle and frustrating; it has gone through * many simpler and cleaner implementation that frustratingly don't work cross * platform. * * */ -uint16 usbSendBytes(uint8* sendBuf, uint16 len) { - +uint16 usbSendBytes(uint8* sendBuf, uint16 len) { + uint16 loaded = 0; if (bDeviceState != CONFIGURED || (!usbGetDTR() && !usbGetRTS())) { // Indicates to caller to stop trying, were not configured/connected // The DTR and RTS lines are handled differently on major platforms, so // the above logic is unreliable - return 0; + return 0; } // Due to a variety of shit this is how we roll; all buffering etc is pushed // upstream - if (countTx) { - return 0; + if (countTx) { + return 0; } // We can only put VCOM_TX_EPSIZE bytes in the buffer @@ -466,7 +444,7 @@ uint8 usbIsConfigured() { uint8 usbIsConnected() { return (bDeviceState != UNCONNECTED); -} +} uint16 usbGetPending() { return countTx; diff --git a/libmaple/usb/usb.h b/libmaple/usb/usb.h index ec179b1..ffba9ff 100644 --- a/libmaple/usb/usb.h +++ b/libmaple/usb/usb.h @@ -1,8 +1,31 @@ -/* insert license */ - -#ifndef __USB_H -#define __USB_H +/****************************************************************************** + * The MIT License + * + * Copyright (c) 2010 LeafLabs LLC. + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + *****************************************************************************/ +#ifndef _USB_H_ +#define _USB_H_ #include "usb_lib.h" #include "libmaple.h" @@ -11,8 +34,7 @@ extern "C" { #endif -typedef enum - { +typedef enum { RESUME_EXTERNAL, RESUME_INTERNAL, RESUME_LATER, @@ -21,51 +43,49 @@ typedef enum RESUME_ON, RESUME_OFF, RESUME_ESOF - } RESUME_STATE; +} RESUME_STATE; -typedef enum - { +typedef enum { UNCONNECTED, ATTACHED, POWERED, SUSPENDED, ADDRESSED, CONFIGURED - } DEVICE_STATE; +} DEVICE_STATE; + +extern volatile uint32 bDeviceState; - extern volatile uint32 bDeviceState; +void setupUSB(void); +void disableUSB(void); +void usbSuspend(void); +void usbResumeInit(void); +void usbResume(RESUME_STATE); - void setupUSB(void); - void disableUSB(void); - void usbSuspend(void); - void usbResumeInit(void); - void usbResume(RESUME_STATE); - - RESULT usbPowerOn(void); - RESULT usbPowerOff(void); - - void usbDsbISR(void); - void usbEnbISR(void); +RESULT usbPowerOn(void); +RESULT usbPowerOff(void); - /* overloaded ISR routine, this is the main usb ISR */ - void usb_lpIRQHandler(void); - void usbWaitReset(void); +void usbDsbISR(void); +void usbEnbISR(void); - /* blocking functions for send/receive */ - uint16 usbSendBytes(uint8* sendBuf,uint16 len); - uint8 usbBytesAvailable(void); - uint8 usbReceiveBytes(uint8* recvBuf, uint8 len); - uint8 usbGetDTR(void); - uint8 usbGetRTS(void); - uint8 usbIsConnected(void); - uint8 usbIsConfigured(void); - uint16 usbGetPending(void); +/* overloaded ISR routine, this is the main usb ISR */ +void usb_lpIRQHandler(void); +void usbWaitReset(void); - void usbSendHello(void); +/* blocking functions for send/receive */ +uint16 usbSendBytes(uint8* sendBuf,uint16 len); +uint8 usbBytesAvailable(void); +uint8 usbReceiveBytes(uint8* recvBuf, uint8 len); +uint8 usbGetDTR(void); +uint8 usbGetRTS(void); +uint8 usbIsConnected(void); +uint8 usbIsConfigured(void); +uint16 usbGetPending(void); +void usbSendHello(void); #ifdef __cplusplus } // extern "C" #endif -#endif //_USB_H +#endif // _USB_H_ diff --git a/libmaple/usb/usb_hardware.c b/libmaple/usb/usb_hardware.c index d0cd693..505dcf1 100644 --- a/libmaple/usb/usb_hardware.c +++ b/libmaple/usb/usb_hardware.c @@ -3,24 +3,26 @@ * * Copyright (c) 2010 LeafLabs LLC. * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. -*****************************************************************************/ + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + *****************************************************************************/ /** * @file usb_hardware.c |