aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile41
-rwxr-xr-xcopy-to-ide4
-rw-r--r--libmaple/bootVect.h61
-rw-r--r--libmaple/usb.c109
-rw-r--r--libmaple/usb.h50
-rw-r--r--libmaple/usb/README68
-rw-r--r--libmaple/usb/descriptors.c168
-rw-r--r--libmaple/usb/descriptors.h202
-rw-r--r--libmaple/usb/usb.c470
-rw-r--r--libmaple/usb/usb.h64
-rw-r--r--libmaple/usb/usb_callbacks.c293
-rw-r--r--libmaple/usb/usb_callbacks.h74
-rw-r--r--libmaple/usb/usb_config.h53
-rw-r--r--libmaple/usb/usb_hardware.c129
-rw-r--r--libmaple/usb/usb_hardware.h157
-rw-r--r--libmaple/usb/usb_lib/usb_core.c1016
-rw-r--r--libmaple/usb/usb_lib/usb_core.h252
-rw-r--r--libmaple/usb/usb_lib/usb_def.h88
-rw-r--r--libmaple/usb/usb_lib/usb_init.c64
-rw-r--r--libmaple/usb/usb_lib/usb_init.h57
-rw-r--r--libmaple/usb/usb_lib/usb_int.c192
-rw-r--r--libmaple/usb/usb_lib/usb_int.h41
-rw-r--r--libmaple/usb/usb_lib/usb_lib.h37
-rw-r--r--libmaple/usb/usb_lib/usb_mem.c73
-rw-r--r--libmaple/usb/usb_lib/usb_mem.h40
-rw-r--r--libmaple/usb/usb_lib/usb_regs.c748
-rw-r--r--libmaple/usb/usb_lib/usb_regs.h (renamed from libmaple/usb_regs.h)151
-rw-r--r--libmaple/usb/usb_lib/usb_type.h77
-rw-r--r--main.cpp.example5
-rw-r--r--notes/45-maple.rules2
-rwxr-xr-xreset.py29
-rw-r--r--wirish/WProgram.h3
-rw-r--r--wirish/comm/HardwareUsb.cpp102
-rw-r--r--wirish/comm/HardwareUsb.h54
-rw-r--r--wirish/wirish.c2
35 files changed, 4543 insertions, 433 deletions
diff --git a/Makefile b/Makefile
index 2a14670..2de5847 100644
--- a/Makefile
+++ b/Makefile
@@ -17,6 +17,9 @@ OD := arm-none-eabi-objdump
ARCH = $(shell uname -m)
OS = $(shell uname)
+VENDOR_ID = 1EAF
+PRODUCT_ID = 0003
+
ifeq ($(OS),Linux)
DFU = dfu-util
else
@@ -28,13 +31,17 @@ LIB_PATH = libmaple
OUTDIRS = $(BUILD_PATH)/$(LIB_PATH)\
$(BUILD_PATH)/wirish \
- $(BUILD_PATH)/wirish/comm
+ $(BUILD_PATH)/wirish/comm \
+ $(BUILD_PATH)/$(LIB_PATH)/usb \
+ $(BUILD_PATH)/$(LIB_PATH)/usb/usb_lib
INCLUDES = -I$(LIB_PATH) \
-I./ \
-Iwirish \
- -Iwirish/comm
+ -Iwirish/comm \
+ -I$(LIB_PATH)/usb \
+ -I$(LIB_PATH)/usb/usb_lib
# default is to upload to flash
#DEFFLAGS = VECT_TAB_BASE
@@ -54,9 +61,6 @@ LFLAGS = -Tstm32conf/$(LINKER) -L stm32conf/lanchon-stm32 \
CPFLAGS = -v -Obinary
ODFLAGS = -S
-# main source file
-MAIN=main.c
-
CSRC = libmaple/systick.c \
libmaple/timers.c \
libmaple/adc.c \
@@ -67,7 +71,6 @@ CSRC = libmaple/systick.c \
libmaple/nvic.c \
libmaple/usart.c \
libmaple/util.c \
- libmaple/usb.c \
libmaple/rcc.c \
libmaple/flash.c \
libmaple/spi.c \
@@ -77,15 +80,23 @@ CSRC = libmaple/systick.c \
wirish/time.c \
wirish/pwm.c \
wirish/ext_interrupts.c \
- wirish/wirish_digital.c
+ wirish/wirish_digital.c \
+ libmaple/usb/usb.c \
+ libmaple/usb/usb_callbacks.c \
+ libmaple/usb/usb_hardware.c \
+ libmaple/usb/descriptors.c \
+ libmaple/usb/usb_lib/usb_core.c \
+ libmaple/usb/usb_lib/usb_init.c \
+ libmaple/usb/usb_lib/usb_int.c \
+ libmaple/usb/usb_lib/usb_mem.c \
+ libmaple/usb/usb_lib/usb_regs.c \
CPPSRC = wirish/wirish_math.cpp \
wirish/Print.cpp \
wirish/comm/HardwareSerial.cpp \
- wirish/comm/HardwareUsb.cpp \
wirish/comm/HardwareSPI.cpp \
wirish/cxxabi-compat.cpp \
- main.cpp
+ main.cpp
# i really have no idea what i'm doing
meep += $(CSRC)
@@ -129,6 +140,10 @@ info:
all: info
+tags:
+ etags `find . -name "*.c" -o -name "*.cpp" -o -name "*.h"`
+ @echo "Made TAGS file for EMACS code browsing"
+
$(BUILD_PATH):
mkdir -p build
@@ -183,10 +198,14 @@ jtag: $(BUILD_PATH)/main.bin
@echo "JTAG build"
program_ram: ram
- $(DFU) -a0 -d 0110:1001 -D build/main.bin -R
+ ./reset.py
+ sleep 1
+ $(DFU) -a0 -d $(VENDOR_ID):$(PRODUCT_ID) -D build/main.bin -R
program_flash: flash
- $(DFU) -a1 -d 0110:1001 -D build/main.bin -R
+ ./reset.py
+ sleep 1
+ $(DFU) -a1 -d $(VENDOR_ID):$(PRODUCT_ID) -D build/main.bin -R
program_jtag: jtag
openocd -f stm32conf/flash.cfg
diff --git a/copy-to-ide b/copy-to-ide
index bd531e2..2af1620 100755
--- a/copy-to-ide
+++ b/copy-to-ide
@@ -11,6 +11,10 @@ ARCH=`uname -a`
FILES="LICENSE
./libmaple/*.h
./libmaple/*.c
+ ./libmaple/usb/*.h
+ ./libmaple/usb/*.c
+ ./libmaple/usb/usb_lib/*.h
+ ./libmaple/usb/usb_lib/*.c
./wirish/*.h
./wirish/*.c
./wirish/main.cxx
diff --git a/libmaple/bootVect.h b/libmaple/bootVect.h
deleted file mode 100644
index 26d876e..0000000
--- a/libmaple/bootVect.h
+++ /dev/null
@@ -1,61 +0,0 @@
-/* *****************************************************************************
- * The MIT License
- *
- * Copyright (c) 2010 Andrew Meyer.
- *
- * 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.
- * ****************************************************************************/
-
-
-/**
- * @file HardwareUsb.h
- *
- * @brief Defines the maple boot vector structure
- */
-
-#ifndef _BOOTVECT_H_
-#define _BOOTVECT_H_
-
-#define BOOTLOADER_VECT_TABLE ((uint32*)0x20000000)
-
-#ifdef __cplusplus
-extern "C"{
-#endif
-
-typedef void (*FuncPtr)(void);
-
-typedef struct {
- FuncPtr serial_tx_cb;
- FuncPtr serial_rx_cb;
- FuncPtr serial_linecoding_cb;
- uint32* serial_count_in;
- uint32* serial_count_out;
- uint8* serial_buffer_out;
- void* linecoding;
- uint8 major_rev;
- uint8 minor_rev;
- void* usb_device_ptr;
- void* usb_local_obj_ptr;
-} BootVectTable;
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif // _BOOTVECT_H_
diff --git a/libmaple/usb.c b/libmaple/usb.c
deleted file mode 100644
index eff0317..0000000
--- a/libmaple/usb.c
+++ /dev/null
@@ -1,109 +0,0 @@
-/* *****************************************************************************
- * The MIT License
- *
- * Copyright (c) 2010 Andrew Meyer.
- *
- * 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 "libmaple.h"
-#include "usb.h"
-#include "usb_regs.h"
-#include "bootVect.h"
-
-void usb_lpIRQHandler(void)
-{
- typedef void (*funcPtr)(void);
-
- const uint32 usbIsrAddr = *(uint32*)(USB_ISR_ADDR);
- void (*ptrToUsbISR)(void) = (funcPtr) usbIsrAddr;
- ptrToUsbISR();
-}
-
-void usb_userToPMABufferCopy(uint8 *pbUsrBuf, uint16 wPMABufAddr, uint16 wNBytes)
-{
- uint32 n = (wNBytes + 1) >> 1; /* n = (wNBytes + 1) / 2 */
- uint32 i, temp1, temp2;
- uint16 *pdwVal;
- pdwVal = (uint16 *)(wPMABufAddr * 2 + PMAAddr);
- for (i = n; i != 0; i--)
- {
- temp1 = (uint16) * pbUsrBuf;
- pbUsrBuf++;
- temp2 = temp1 | (uint16) * pbUsrBuf << 8;
- *pdwVal++ = temp2;
- pdwVal++;
- pbUsrBuf++;
- }
-}
-
-void usb_PMAToUserBufferCopy(uint8 *pbUsrBuf, uint16 wPMABufAddr, uint16 wNBytes)
-{
- uint32 n = (wNBytes + 1) >> 1;/* /2*/
- uint32 i;
- uint32 *pdwVal;
- pdwVal = (uint32 *)(wPMABufAddr * 2 + PMAAddr);
- for (i = n; i != 0; i--)
- {
- *(uint16*)pbUsrBuf++ = *pdwVal++;
- pbUsrBuf++;
- }
-}
-
-void usb_serialWriteStr(const char* outStr) {
- uint8 offset=0;
- BootVectTable *bootVector = ((BootVectTable*)BOOTLOADER_VECT_TABLE);
-
- while ((outStr[offset] != '\0')
- && (offset < USB_SERIAL_BUF_SIZE)) {
- offset++;
- }
-
- delay(offset*1);
-
- bootVector->serial_count_in = (uint32*) &offset;
- usb_userToPMABufferCopy((uint8*)outStr,USB_SERIAL_ENDP_TXADDR,offset);
- _SetEPTxCount(USB_SERIAL_ENDP_TX,offset);
- _SetEPTxValid(USB_SERIAL_ENDP_TX);
-
-}
-
-void usb_serialWriteChar(unsigned char ch) {
- BootVectTable *bootVector = ((BootVectTable*)BOOTLOADER_VECT_TABLE);
-
- delay(1);
-
- *(bootVector->serial_count_in) = 1;
- usb_userToPMABufferCopy((uint8*)(&ch),USB_SERIAL_ENDP_TXADDR,1);
- _SetEPTxCount(USB_SERIAL_ENDP_TX,1);
- _SetEPTxValid(USB_SERIAL_ENDP_TX);
-
-}
-
-uint8 usb_serialGetRecvLen() {
- uint8 count_out =_GetEPRxCount(USB_SERIAL_ENDP_RX);
- return count_out;
-}
-
-void usb_copyRecvBuffer(unsigned char* dest, uint8 len) {
- ASSERT(len < USB_SERIAL_BUF_SIZE);
- usb_PMAToUserBufferCopy((uint8*)(dest),USB_SERIAL_ENDP_RXADDR,len);
- _SetEPRxValid(USB_SERIAL_ENDP_RX);
-}
-
diff --git a/libmaple/usb.h b/libmaple/usb.h
deleted file mode 100644
index 7d233ef..0000000
--- a/libmaple/usb.h
+++ /dev/null
@@ -1,50 +0,0 @@
-/* *****************************************************************************
- * The MIT License
- *
- * Copyright (c) 2010 Andrew Meyer.
- *
- * 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_
-
-#define USB_ISR_ADDR (0x08000090)
-#define USB_SERIAL_ENDP_TXADDR ((uint32) 0xC0)
-#define USB_SERIAL_ENDP_RXADDR ((uint32) 0x110)
-#define USB_SERIAL_ENDP_TX ((uint16) 0x1)
-#define USB_SERIAL_ENDP_RX ((uint16) 0x3)
-#define USB_SERIAL_BUF_SIZE (0x40)
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-void usb_lpIRQHandler(void);
-void usb_userToPMABufferCopy(uint8 *pbUsrBuf,uint16 wPMABufAddr,uint16 wNBytes);
-void usb_PMAToUserBufferCopy(uint8 *pbUsrBuf,uint16 wPMABufAddr,uint16 wNBytes);
-void usb_serialWriteStr(const char *outStr);
-void usb_serialWriteChar(unsigned char ch);
-uint8 usb_serialGetRecvLen();
-void usb_copyRecvBuffer(unsigned char* dest, uint8 len);
-
-#ifdef __cplusplus
-} // extern "C"
-#endif
-#endif //_USB_H
diff --git a/libmaple/usb/README b/libmaple/usb/README
new file mode 100644
index 0000000..e542dde
--- /dev/null
+++ b/libmaple/usb/README
@@ -0,0 +1,68 @@
+The USB submodule of libmaple is responsible for:
+ initilizing the usb peripheral, scaling the peripheral clocks appropriately,
+ enabling the interrupt channels to usb, defining the usb isr, resetting the usb
+ disc pin (used to tell the host were alive). Additionally, the usb submodule defines
+ the virtual com port usb applications that is available to all user sketches via Usb.print()
+ and others.
+
+To use it:
+ Call Usb.init() to enable the IRQ channel, configure the clocks,
+ pull down usb_disc, and setup the vcom buffers
+ Usb.print/ln, available(), read(), write() implement the same
+ interface as Serial1/2/3
+
+
+Current Status:
+ Currently, the USB submodule relies on the low level core library provided by ST to access the
+ usb peripheral registers and implement the usb transfer protocol for control endpoint transfers.
+ The high level virtual com port application is unfortunately hard to untangle from this low level
+ dependence, and when a new USB core library is written (to nix ST dependence) changes will likely
+ have to be made to virtual com application code. Ideally, the new core library should mimick the
+ form of MyUSB (LUFA), since this library (USB for AVR) is growing in popularity and in example
+ applications. Additionally, the usb lib here relies on low level hardware functions that were
+ just ripped out of the bootloader code (for simplicity) but clearly this should be replaced with
+ direct accesses to functions provided elsewhere in libmaple.
+
+ The virtual com port serves two important purposes. 1) is allows serial data transfers between
+ user sketches an a host computer. 2) is allows the host machine to issue a system reset by
+ asserting the DTR signal. After reset, Maple will run the DFU bootloader for a few seconds,
+ during which the user can begin a DFU download operation ('downloads' application binary into
+ RAM/FLASH). This without this virtual com port, it would be necessary to find an alternative means
+ to reset the chip in order to enable the bootloader.
+
+ If you would like to develop your own USB application for whatever reason (uses faster isochronous
+ enpoints for streaming audio, or implements the USB HID or Mass Storage specs for examples) then
+ ensure that you leave some hook for resetting Maple remotely in order to spin up the DFU bootloader.
+ Please make sure to give yourself a unique vendor/product ID pair in your application, as some
+ operating systems will assign a host-side driver based on these tags.
+
+ It would be possible to build a compound usb device, that implements endpoints for both the virtual
+ COM port as well as some other components (mass sotrage etc.) however this turns out to be a burden
+ from the host driver side, as windows and *nix handle compound usb devices quite differently.
+
+ Be mindful that running the usb application isnt "free." The device must respond to periodic bus
+ activity (every few milliseconds) by servicing an ISR. Therefore the usb application should be disabled
+ inside of timing critical applications. In order to disconnect the device from the host, the USB_DISC
+ pin can be asserted (on Maple v1,2,3 this is GPIOC,12). Alternatively, the NVIC can be directly configured
+ to disable the USB LP/HP IRQ's
+
+ This library should exposed through usb.h, do not include any other files direcly in your application.
+
+ The files inside of usb_lib were provided by ST and are subject to their own license, all other files were
+ written by the LeafLabs team and fall under the MIT license.
+
+Integration with libmaple:
+ The current usb lib is ported directly from the maple bootloader code, adapted to be a virtual com rather than
+ a DFU device. That means several functions are redefined locally that could have been pulled from elsewhere
+ in libmaple. Thus, ths usb module depends absolutely zero on libmaple, it even ensures that clocks are configured
+ correctly for its operation.
+
+Todo:
+ - write custom low level usb stack to strip out any remaining dependence on ST code
+ - remove dependence on hardware.c, since any functions here really should have their
+ own analogues elsewhere inside libmaple
+ - add a high level usb application library that would allow users to make their own
+ HID/Mass Storage/Audio/Video devices.
+ - implement a Usb.link(SerialX) that forces a passthrough
+ the host computer virtual com to SerialX, and utilizes the
+ line_config commands correctly (sets baud etc) \ No newline at end of file
diff --git a/libmaple/usb/descriptors.c b/libmaple/usb/descriptors.c
new file mode 100644
index 0000000..5038709
--- /dev/null
+++ b/libmaple/usb/descriptors.c
@@ -0,0 +1,168 @@
+/* insert license */
+
+
+#include "descriptors.h"
+#include "libmaple.h"
+#include "usb_config.h"
+
+const USB_Descriptor_Device usbVcomDescriptor_Device = {
+ bLength: sizeof(USB_Descriptor_Device),
+ bDescriptorType: USB_DESCRIPTOR_TYPE_DEVICE,
+ bcdUSB: 0x0200,
+ bDeviceClass: USB_DEVICE_CLASS_CDC,
+ bDeviceSubClass: USB_DEVICE_SUBCLASS_CDC,
+ bDeviceProtocol: 0x00,
+ bMaxPacketSize0: 0x40,
+ idVendor: VCOM_ID_VENDOR,
+ idProduct: VCOM_ID_PRODUCT,
+ bcdDevice: 0x0200,
+ iManufacturer: 0x01,
+ iProduct: 0x02,
+ iSerialNumber: 0x00,
+ bNumConfigurations: 0x01
+};
+
+const USB_Descriptor_Config usbVcomDescriptor_Config = {
+ bLength: 0x09,//sizeof(USB_Descriptor_Config_Header),
+ bDescriptorType: USB_DESCRIPTOR_TYPE_CONFIGURATION,
+ wTotalLength: 0x43,//sizeof(USB_Descriptor_Config),
+ bNumInterfaces: 0x02,
+ bConfigurationValue: 0x01,
+ iConfiguration: 0x00,
+ bmAttributes: (USB_CONFIG_ATTR_BUSPOWERED | USB_CONFIG_ATTR_SELF_POWERED),
+ bMaxPower: USB_CONFIG_MAX_POWER,
+
+ CCI_Interface:
+ {
+ bLength: 0x09,//sizeof(USB_Descriptor_Interface),
+ bDescriptorType: USB_DESCRIPTOR_TYPE_INTERFACE,
+ bInterfaceNumber: 0x00,
+ bAlternateSetting: 0x00,
+ bNumEndpoints: 0x01,
+ bInterfaceClass: 0x02,
+ bInterfaceSubClass: 0x02,
+ bInterfaceProtocol: 0x01,
+ iInterface: 0x00
+ },
+
+ CDC_Functional_IntHeader:
+ {
+ bLength: 0x05,//sizeof(CDC_FUNCTIONAL_DESCRIPTOR(2)),
+ bDescriptorType: 0x24,
+ SubType: 0x00,
+ Data: {0x01, 0x10}
+ },
+
+ CDC_Functional_CallManagement:
+ {
+ bLength: 0x05,//sizeof(CDC_FUNCTIONAL_DESCRIPTOR(2)),
+ bDescriptorType: 0x24,
+ SubType: 0x01,
+ Data: {0x03, 0x01}
+ },
+
+ CDC_Functional_ACM:
+ {
+ bLength: 0x04,//sizeof(CDC_FUNCTIONAL_DESCRIPTOR(1)),
+ bDescriptorType: 0x24,
+ SubType: 0x02,
+ Data: {0x06}
+ },
+
+ CDC_Functional_Union:
+ {
+ bLength: 0x05,//sizeof(CDC_FUNCTIONAL_DESCRIPTOR(2)),
+ bDescriptorType: 0x24,
+ SubType: 0x06,
+ Data: {0x00, 0x01}
+ },
+
+ // ManagementEndpoint:
+ // {
+ EP1_bLength: 0x07,//sizeof(USB_Descriptor_Endpoint),
+ EP1_bDescriptorType: USB_DESCRIPTOR_TYPE_ENDPOINT,
+ EP1_bEndpointAddress: (USB_DESCRIPTOR_ENDPOINT_IN | VCOM_NOTIFICATION_EPNUM),
+ EP1_bmAttributes: EP_TYPE_INTERRUPT,
+ EP1_wMaxPacketSize0: VCOM_NOTIFICATION_EPSIZE,
+ EP1_wMaxPacketSize1: 0x00,
+ EP1_bInterval: 0xFF,
+ // },
+
+ // DCI_Interface:
+ // {
+ DCI_bLength: 0x09,//sizeof(USB_Descriptor_Interface),
+ DCI_bDescriptorType: USB_DESCRIPTOR_TYPE_INTERFACE,
+ DCI_bInterfaceNumber: 0x01,
+ DCI_bAlternateSetting: 0x00,
+ DCI_bNumEndpoints: 0x02,
+ DCI_bInterfaceClass: 0x0A,
+ DCI_bInterfaceSubClass: 0x00,
+ DCI_bInterfaceProtocol: 0x00,
+ DCI_iInterface: 0x00,
+ // },
+
+ //DataOutEndpoint:
+ // {
+ // },
+ EP2_bLength: 0x07,//sizeof(USB_Descriptor_Endpoint),
+ EP2_bDescriptorType: USB_DESCRIPTOR_TYPE_ENDPOINT,
+ EP2_bEndpointAddress: (USB_DESCRIPTOR_ENDPOINT_OUT | VCOM_RX_EPNUM),
+ EP2_bmAttributes: EP_TYPE_BULK,
+ EP2_wMaxPacketSize0: VCOM_RX_EPSIZE,
+ EP2_wMaxPacketSize1: 0x00,
+ EP2_bInterval: 0x00,
+
+
+ // DataInEndpoint:
+ // {
+ EP3_bLength: 0x07,//sizeof(USB_Descriptor_Endpoint),
+ EP3_bDescriptorType: USB_DESCRIPTOR_TYPE_ENDPOINT,
+ EP3_bEndpointAddress: (USB_DESCRIPTOR_ENDPOINT_IN | VCOM_TX_EPNUM),
+ EP3_bmAttributes: EP_TYPE_BULK,
+ EP3_wMaxPacketSize0: VCOM_TX_EPSIZE,
+ EP3_wMaxPacketSize1: 0x00,
+ EP3_bInterval: 0x00
+
+ // }
+};
+
+/*
+ String Identifiers:
+
+ we may choose to specify any or none of the following string
+ identifiers:
+
+ iManufacturer: LeafLabs
+ iProduct: Maple R3
+ iSerialNumber: NONE
+ iConfiguration: NONE
+ iInterface(CCI): NONE
+ iInterface(DCI): NONE
+
+ additionally we must provide the unicode language identifier,
+ which is 0x0409 for US English
+*/
+
+const uint8 usbVcomDescriptor_LangID[USB_DESCRIPTOR_STRING_LEN(1)] =
+{
+ USB_DESCRIPTOR_STRING_LEN(1),
+ USB_DESCRIPTOR_TYPE_STRING,
+ 0x09,
+ 0x04
+};
+
+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, 'a', 0, 'b', 0, 's', 0
+};
+
+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,
+ 'e', 0, ' ', 0, 'R', 0, '3', 0
+};
diff --git a/libmaple/usb/descriptors.h b/libmaple/usb/descriptors.h
new file mode 100644
index 0000000..1efe8c1
--- /dev/null
+++ b/libmaple/usb/descriptors.h
@@ -0,0 +1,202 @@
+/* insert license */
+#ifndef __DESCRIPTORS_H
+#define __DESCRIPTORS_H
+
+
+#include "libmaple.h"
+#include "usb_lib.h"
+
+#define USB_DESCRIPTOR_TYPE_DEVICE 0x01
+#define USB_DESCRIPTOR_TYPE_CONFIGURATION 0x02
+#define USB_DESCRIPTOR_TYPE_STRING 0x03
+#define USB_DESCRIPTOR_TYPE_INTERFACE 0x04
+#define USB_DESCRIPTOR_TYPE_ENDPOINT 0x05
+
+#define USB_DEVICE_CLASS_CDC 0x02
+#define USB_DEVICE_SUBCLASS_CDC 0x00
+
+#define USB_CONFIG_ATTR_BUSPOWERED 0b10000000
+#define USB_CONFIG_ATTR_SELF_POWERED 0b11000000
+
+#define EP_TYPE_INTERRUPT 0x03
+#define EP_TYPE_BULK 0x02
+
+#define USB_DESCRIPTOR_ENDPOINT_IN 0x80
+#define USB_DESCRIPTOR_ENDPOINT_OUT 0x00
+
+#define USB_DESCRIPTOR_STRING_LEN(x) (2 + (x << 1))
+
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+#define USB_DESCRIPTOR_STRING(len) \
+ struct { \
+ uint8 bLength; \
+ uint8 bDescriptorType; \
+ uint16 bString[len]; \
+ }
+
+#define CDC_FUCNTIONAL_DESCRIPTOR(DataSize) \
+ struct \
+ { \
+ uint8 bLength; \
+ uint8 bDescriptorType; \
+ uint8 SubType; \
+ uint8 Data[DataSize]; \
+ }
+
+typedef struct {
+ uint8 bLength;
+ uint8 bDescriptorType;
+ uint16 bcdUSB;
+ uint8 bDeviceClass;
+ uint8 bDeviceSubClass;
+ uint8 bDeviceProtocol;
+ uint8 bMaxPacketSize0;
+ uint16 idVendor;
+ uint16 idProduct;
+ uint16 bcdDevice;
+ uint8 iManufacturer;
+ uint8 iProduct;
+ uint8 iSerialNumber;
+ uint8 bNumConfigurations;
+} USB_Descriptor_Device;
+
+typedef struct {
+ uint8 bLength;
+ uint8 bDescriptorType;
+ uint16 wTotalLength;
+ uint8 bNumInterfaces;
+ uint8 bConfigurationValue;
+ uint8 iConfiguration;
+ uint8 bmAttributes;
+ uint8 bMaxPower;
+} USB_Descriptor_Config_Header;
+
+typedef struct {
+ uint8 bLength;
+ uint8 bDescriptorType;
+ uint8 bInterfaceNumber;
+ uint8 bAlternateSetting;
+ uint8 bNumEndpoints;
+ uint8 bInterfaceClass;
+ uint8 bInterfaceSubClass;
+ uint8 bInterfaceProtocol;
+ uint8 iInterface;
+} USB_Descriptor_Interface;
+
+typedef struct {
+ uint8 bLength;
+ uint8 bDescriptorType;
+ uint8 bEndpointAddress;
+ uint8 bmAttributes;
+ uint16 wMaxPacketSize;
+ uint8 bInterval;
+} USB_Descriptor_Endpoint;
+
+typedef struct {
+ /* config header */
+ uint8 bLength;
+ uint8 bDescriptorType;
+ uint16 wTotalLength;
+ uint8 bNumInterfaces;
+ uint8 bConfigurationValue;
+ uint8 iConfiguration;
+ uint8 bmAttributes;
+ uint8 bMaxPower;
+
+ USB_Descriptor_Interface CCI_Interface;
+ struct {
+ uint8 bLength;
+ uint8 bDescriptorType;
+ uint8 SubType;
+ uint8 Data[2];
+ } CDC_Functional_IntHeader;
+ struct {
+ uint8 bLength;
+ uint8 bDescriptorType;
+ uint8 SubType;
+ uint8 Data[2];
+ } CDC_Functional_CallManagement;
+ struct {
+ uint8 bLength;
+ uint8 bDescriptorType;
+ uint8 SubType;
+ uint8 Data[1];
+ } CDC_Functional_ACM;
+ struct {
+ uint8 bLength;
+ uint8 bDescriptorType;
+ uint8 SubType;
+ uint8 Data[2];
+ } CDC_Functional_Union;
+
+ /*
+ USB_Descriptor_Endpoint ManagementEndpoint;
+ */
+ uint8 EP1_bLength;
+ uint8 EP1_bDescriptorType;
+ uint8 EP1_bEndpointAddress;
+ uint8 EP1_bmAttributes;
+ uint8 EP1_wMaxPacketSize0;
+ uint8 EP1_wMaxPacketSize1;
+ uint8 EP1_bInterval;
+
+ /*
+ USB_Descriptor_Interface DCI_Interface;
+ */
+
+ uint8 DCI_bLength;
+ uint8 DCI_bDescriptorType;
+ uint8 DCI_bInterfaceNumber;
+ uint8 DCI_bAlternateSetting;
+ uint8 DCI_bNumEndpoints;
+ uint8 DCI_bInterfaceClass;
+ uint8 DCI_bInterfaceSubClass;
+ uint8 DCI_bInterfaceProtocol;
+ uint8 DCI_iInterface;
+
+ /*
+ USB_Descriptor_Endpoint DataOutEndpoint;
+ USB_Descriptor_Endpoint DataInEndpoint;
+ */
+
+ uint8 EP2_bLength;
+ uint8 EP2_bDescriptorType;
+ uint8 EP2_bEndpointAddress;
+ uint8 EP2_bmAttributes;
+ uint8 EP2_wMaxPacketSize0;
+ uint8 EP2_wMaxPacketSize1;
+ uint8 EP2_bInterval;
+
+ uint8 EP3_bLength;
+ uint8 EP3_bDescriptorType;
+ uint8 EP3_bEndpointAddress;
+ uint8 EP3_bmAttributes;
+ uint8 EP3_wMaxPacketSize0;
+ uint8 EP3_wMaxPacketSize1;
+ uint8 EP3_bInterval;
+
+
+}USB_Descriptor_Config;
+
+ typedef struct {
+ uint8 bLength;
+ uint8 bDescriptorType;
+ uint16 bString[];
+ } USB_Descriptor_String;
+
+extern const USB_Descriptor_Device usbVcomDescriptor_Device;
+extern const USB_Descriptor_Config usbVcomDescriptor_Config;
+
+extern const uint8 usbVcomDescriptor_LangID[USB_DESCRIPTOR_STRING_LEN(1)];
+extern const uint8 usbVcomDescriptor_iManufacturer[USB_DESCRIPTOR_STRING_LEN(8)];
+extern const uint8 usbVcomDescriptor_iProduct[USB_DESCRIPTOR_STRING_LEN(8)];
+
+#if defined(__cplusplus)
+ }
+#endif
+
+#endif // __DESCRIPTORS_H
diff --git a/libmaple/usb/usb.c b/libmaple/usb/usb.c
new file mode 100644
index 0000000..f926e81
--- /dev/null
+++ b/libmaple/usb/usb.c
@@ -0,0 +1,470 @@
+/* *****************************************************************************
+ * 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.
+ * ****************************************************************************/
+
+/**
+ * @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
+ */
+
+#include "usb.h"
+#include "libmaple.h"
+#include "usb_lib.h"
+#include "gpio.h"
+#include "usb_hardware.h"
+
+#include "usb_config.h"
+#include "usb_callbacks.h"
+#include "usb_lib.h"
+
+/* persistent usb structs */
+
+volatile uint32 bDeviceState = UNCONNECTED;
+volatile uint16 wIstr = 0;
+volatile bIntPackSOF = 0;
+
+DEVICE Device_Table =
+ {
+ NUM_ENDPTS,
+ 1
+ };
+
+DEVICE_PROP Device_Property =
+ {
+ usbInit,
+ usbReset,
+ usbStatusIn,
+ usbStatusOut,
+ usbDataSetup,
+ usbNoDataSetup,
+ usbGetInterfaceSetting,
+ usbGetDeviceDescriptor,
+ usbGetConfigDescriptor,
+ usbGetStringDescriptor,
+ NOP_Process,
+ 0,
+ bMaxPacketSize
+ };
+
+USER_STANDARD_REQUESTS User_Standard_Requests =
+ {
+ 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,
+};
+
+void (*pEpInt_OUT[7])(void) =
+{
+ 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);
+
+ /* 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 */
+
+}
+
+void usbSuspend(void) {
+ u16 wCNTR;
+ wCNTR = _GetCNTR();
+ wCNTR |= CNTR_FSUSP | CNTR_LPMODE;
+ _SetCNTR(wCNTR);
+
+ /* run any power reduction handlers */
+ bDeviceState = SUSPENDED;
+}
+
+void usbResumeInit(void) {
+ u16 wCNTR;
+
+ /* restart any clocks that had been stopped */
+
+ wCNTR = _GetCNTR();
+ wCNTR &= (~CNTR_LPMODE);
+ _SetCNTR(wCNTR);
+
+ /* undo power reduction handlers here */
+ _SetCNTR(ISR_MSK);
+
+}
+
+void usbResume(RESUME_STATE eResumeSetVal) {
+ u16 wCNTR;
+
+ if (eResumeSetVal != RESUME_ESOF)
+ ResumeS.eState = eResumeSetVal;
+
+ switch (ResumeS.eState)
+ {
+ case RESUME_EXTERNAL:
+ usbResumeInit();
+ ResumeS.eState = RESUME_OFF;
+ break;
+ case RESUME_INTERNAL:
+ usbResumeInit();
+ ResumeS.eState = RESUME_START;
+ break;
+ case RESUME_LATER:
+ ResumeS.bESOFcnt = 2;
+ ResumeS.eState = RESUME_WAIT;
+ break;
+ case RESUME_WAIT:
+ ResumeS.bESOFcnt--;
+ if (ResumeS.bESOFcnt == 0)
+ ResumeS.eState = RESUME_START;
+ break;
+ case RESUME_START:
+ wCNTR = _GetCNTR();
+ wCNTR |= CNTR_RESUME;
+ _SetCNTR(wCNTR);
+ ResumeS.eState = RESUME_ON;
+ ResumeS.bESOFcnt = 10;
+ break;
+ case RESUME_ON:
+ ResumeS.bESOFcnt--;
+ if (ResumeS.bESOFcnt == 0)
+ {
+ wCNTR = _GetCNTR();
+ wCNTR &= (~CNTR_RESUME);
+ _SetCNTR(wCNTR);
+ ResumeS.eState = RESUME_OFF;
+ }
+ break;
+ case RESUME_OFF:
+ case RESUME_ESOF:
+ default:
+ ResumeS.eState = RESUME_OFF;
+ break;
+ }
+}
+
+RESULT usbPowerOn(void) {
+ u16 wRegVal;
+
+ wRegVal = CNTR_FRES;
+ _SetCNTR(wRegVal);
+
+ wInterrupt_Mask = 0;
+ _SetCNTR(wInterrupt_Mask);
+ _SetISTR(0);
+ wInterrupt_Mask = CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM; /* the bare minimum */
+ _SetCNTR(wInterrupt_Mask);
+
+ return USB_SUCCESS;
+}
+
+RESULT usbPowerOff(void) {
+ _SetCNTR(CNTR_FRES);
+ _SetISTR(0);
+ _SetCNTR(CNTR_FRES + CNTR_PDWN);
+
+ /* note that all weve done here is powerdown the
+ usb peripheral. we have no disabled the clocks,
+ pulled the usb_disc pin back up, or reset the
+ application state machines */
+
+ return USB_SUCCESS;
+}
+
+void usbEnbISR(void) {
+ NVIC_InitTypeDef NVIC_InitStructure;
+
+
+ NVIC_InitStructure.NVIC_IRQChannel = USB_LP_IRQ;
+ NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
+ NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
+ NVIC_InitStructure.NVIC_IRQChannelCmd = TRUE;
+ nvicInit(&NVIC_InitStructure);
+}
+
+void usbDsbISR(void) {
+ NVIC_InitTypeDef NVIC_InitStructure;
+ NVIC_InitStructure.NVIC_IRQChannel = USB_LP_IRQ;
+ NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
+ NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
+ NVIC_InitStructure.NVIC_IRQChannelCmd = FALSE;
+ nvicInit(&NVIC_InitStructure);
+}
+
+/* overloaded ISR routine, this is the main usb ISR */
+void usb_lpIRQHandler(void) {
+wIstr = _GetISTR();
+
+/* go nuts with the preproc switches since this is an ISTR and must be FAST */
+#if (ISR_MSK & ISTR_RESET)
+if (wIstr & ISTR_RESET & wInterrupt_Mask)
+ {
+ _SetISTR((u16)CLR_RESET);
+ Device_Property.Reset();
+ }
+#endif
+
+
+#if (ISR_MSK & ISTR_DOVR)
+if (wIstr & ISTR_DOVR & wInterrupt_Mask)
+ {
+ _SetISTR((u16)CLR_DOVR);
+ }
+#endif
+
+
+#if (ISR_MSK & ISTR_ERR)
+if (wIstr & ISTR_ERR & wInterrupt_Mask)
+ {
+ _SetISTR((u16)CLR_ERR);
+ }
+#endif
+
+
+#if (ISR_MSK & ISTR_WKUP)
+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)
+ {
+
+ /* check if SUSPEND is possible */
+ 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)
+ {
+ _SetISTR((u16)CLR_SOF);
+ bIntPackSOF++;
+ }
+#endif
+
+
+#if (ISR_MSK & ISTR_ESOF)
+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)
+ {
+ /* 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
+
+/* if we are about to reset from the DTR signal, then dont return
+ to user, instead return from the ISR into a wait slide */
+ if (reset_state == RESET_NOW) {
+ reset_state = START;
+ unsigned int target = (unsigned int)usbWaitReset | 0x1;
+
+ asm volatile("mov r0, %[stack_top] \n\t" // Reset the stack
+ "mov sp, r0 \n\t"
+ "mov r0, #1 \n\t"
+ "mov r1, %[target_addr] \n\t"
+ "mov r2, %[cpsr] \n\t"
+ "push {r2} \n\t" // Fake xPSR
+ "push {r1} \n\t" // Target address for PC
+ "push {r0} \n\t" // Fake LR
+ "push {r0} \n\t" // Fake R12
+ "push {r0} \n\t" // Fake R3
+ "push {r0} \n\t" // Fake R2
+ "push {r0} \n\t" // Fake R1
+ "push {r0} \n\t" // Fake R0
+ "mov lr, %[exc_return] \n\t"
+ "bx lr"
+ :
+ : [stack_top] "r" (STACK_TOP),
+ [target_addr] "r" (target),
+ [exc_return] "r" (EXC_RETURN),
+ [cpsr] "r" (DEFAULT_CPSR)
+ : "r0", "r1", "r2");
+ // Should never get here.
+ }
+}
+
+void usbWaitReset(void) {
+ static count=1000000;
+ while (count-- >0);
+ systemHardReset();
+}
+
+/* copies data out of sendBuf into the packet memory for
+ usb, but waits until any previous usb transmissions have
+ completed before doing this. It returns without waiting
+ for its data to be sent. most efficient when 64 bytes are copied
+ at a time. users responsible for not overflowing sendbuf
+ with len! if > 64 bytes are being sent, then the function
+ will block at every 64 byte packet
+*/
+int16 usbSendBytes(uint8* sendBuf, uint16 len) {
+/* while (countTx != 0) { */
+/* if (reset_state == NDTR_NRTS) { */
+/* return 0; */
+/* } */
+/* }/\* wait for pipe to be clear *\/ */
+
+ if (reset_state == NDTR_NRTS) {
+ return -1; /* indicates to caller to stop trying, were not connected */
+ }
+
+ /* ideally we should wait here, but it gets stuck
+ for some reason. countTx wont decrement when
+ theres no host side port reading the data, this is
+ known, but even if we add the check for NDTR_NRTS it
+ still gets stuck...*/
+ if (countTx != 0) {
+ return 0; /* indicated to caller to keep trying, were just busy */
+ }
+
+ uint16 sent = len;
+
+ while (len > VCOM_TX_EPSIZE) {
+ countTx = VCOM_TX_EPSIZE;
+
+ UserToPMABufferCopy(sendBuf,VCOM_TX_ADDR,VCOM_TX_EPSIZE);
+ _SetEPTxCount(VCOM_TX_ENDP,VCOM_TX_EPSIZE);
+ _SetEPTxValid(VCOM_TX_ENDP);
+
+ while(countTx != 0);
+ len -= VCOM_TX_EPSIZE;
+ sendBuf += VCOM_TX_EPSIZE;
+ }
+
+ if (len != 0) {
+ countTx = len;
+ UserToPMABufferCopy(sendBuf,VCOM_TX_ADDR,len);
+ _SetEPTxCount(VCOM_TX_ENDP,len);
+ _SetEPTxValid(VCOM_TX_ENDP);
+ }
+
+ return sent;
+}
+
+/* returns the number of available bytes are in the recv FIFO */
+uint8 usbBytesAvailable(void) {
+ return VCOM_RX_EPSIZE - maxNewBytes;
+}
+
+/* copies len bytes from the local recieve FIFO (not
+ usb packet buffer) into recvBuf and deq's the fifo.
+ will only copy the minimum of len or the available
+ bytes. returns the number of bytes copied */
+uint8 usbReceiveBytes(uint8* recvBuf, uint8 len) {
+ if (len > VCOM_RX_EPSIZE - maxNewBytes) {
+ len = VCOM_RX_EPSIZE - maxNewBytes;
+ }
+
+ int i;
+ for (i=0;i<len;i++) {
+ recvBuf[i] = (uint8)(vcomBufferRx[recvBufOut]);
+ recvBufOut = (recvBufOut + 1) % VCOM_RX_EPSIZE;
+ }
+
+ maxNewBytes += len;
+
+ /* re-enable the rx endpoint which we had set to receive 0 bytes */
+ if (maxNewBytes - len == 0) {
+ SetEPRxCount(VCOM_RX_ENDP,maxNewBytes);
+ }
+
+ return len;
+}
+
+void usbSendHello(void) {
+ char* myStr = "hello!";
+
+ uint8 bufin = 48 + recvBufIn;;
+ uint8 bufout = 48 + recvBufOut;
+ uint8 avail = 48 + usbBytesAvailable();
+
+ char *line = "\r\n";
+ while(usbSendBytes(&bufin,1) == 0);
+ while(usbSendBytes(&bufout,1) == 0);
+ while(usbSendBytes(&avail,1) == 0);
+ while(usbSendBytes((uint8*)line,2) == 0);
+
+ uint8 recv[64];
+ usbReceiveBytes(&recv[0],1);
+}
diff --git a/libmaple/usb/usb.h b/libmaple/usb/usb.h
new file mode 100644
index 0000000..e76772e
--- /dev/null
+++ b/libmaple/usb/usb.h
@@ -0,0 +1,64 @@
+/* insert license */
+
+#ifndef __USB_H_
+#define __USB_H_
+
+
+#include "usb_lib.h"
+#include "libmaple.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef enum
+ {
+ RESUME_EXTERNAL,
+ RESUME_INTERNAL,
+ RESUME_LATER,
+ RESUME_WAIT,
+ RESUME_START,
+ RESUME_ON,
+ RESUME_OFF,
+ RESUME_ESOF
+ } RESUME_STATE;
+
+typedef enum
+ {
+ UNCONNECTED,
+ ATTACHED,
+ POWERED,
+ SUSPENDED,
+ ADDRESSED,
+ CONFIGURED
+ } DEVICE_STATE;
+
+ extern volatile uint32 bDeviceState;
+
+ void setupUSB(void);
+ void usbSuspend(void);
+ void usbResumeInit(void);
+ void usbResume(RESUME_STATE);
+
+ RESULT usbPowerOn(void);
+ RESULT usbPowerOff(void);
+
+ void usbDsbISR(void);
+ void usbEnbISR(void);
+
+ /* overloaded ISR routine, this is the main usb ISR */
+ void usb_lpIRQHandler(void);
+ void usbWaitReset(void);
+
+ /* blocking functions for send/receive */
+ int16 usbSendBytes(uint8* sendBuf,uint16 len);
+ uint8 usbBytesAvailable(void);
+ uint8 usbReceiveBytes(uint8* recvBuf, uint8 len);
+
+ void usbSendHello(void);
+
+#ifdef __cplusplus
+} // extern "C"
+#endif
+
+#endif //_USB_H
diff --git a/libmaple/usb/usb_callbacks.c b/libmaple/usb/usb_callbacks.c
new file mode 100644
index 0000000..08d62c5
--- /dev/null
+++ b/libmaple/usb/usb_callbacks.c
@@ -0,0 +1,293 @@
+/* insert license */
+
+#include "usb_callbacks.h"
+#include "usb_lib.h"
+#include "descriptors.h"
+#include "usb_config.h"
+#include "usb.h"
+
+ONE_DESCRIPTOR Device_Descriptor = {
+ (uint8*)&usbVcomDescriptor_Device,
+ sizeof(USB_Descriptor_Device)
+};
+
+ONE_DESCRIPTOR Config_Descriptor = {
+ (uint8*)&usbVcomDescriptor_Config,
+ 0x43//sizeof(USB_Descriptor_Config)
+};
+
+ONE_DESCRIPTOR String_Descriptor[3] = {
+ {(uint8*)&usbVcomDescriptor_LangID, USB_DESCRIPTOR_STRING_LEN(1)},
+ {(uint8*)&usbVcomDescriptor_iManufacturer,USB_DESCRIPTOR_STRING_LEN(8)},
+ {(uint8*)&usbVcomDescriptor_iProduct, USB_DESCRIPTOR_STRING_LEN(8)}
+};
+
+uint8 last_request = 0;
+
+USB_Line_Coding line_coding = {
+ bitrate: 115200,
+ format: 0x00, /* stop bits-1 */
+ paritytype: 0x00,
+ datatype: 0x08
+};
+
+uint8 vcomBufferRx[VCOM_RX_EPSIZE];
+uint8 countTx = 0;
+uint8 recvBufIn = 0;
+uint8 recvBufOut = 0;
+uint8 maxNewBytes = VCOM_RX_EPSIZE;
+
+RESET_STATE reset_state = START;
+
+void vcomDataTxCb(void) {
+ /* do whatever after data has been sent to host */
+
+ /* allows usbSendBytes to stop blocking */
+
+
+ countTx = 0;
+}
+
+/* we could get arbitrarily complicated here for speed purposes
+ however, the simple scheme here is to implement a receive fifo
+ and always set the maximum to new bytes to the space remaining
+ in the fifo. this number will be reincremented after calls
+ to usbReceiveBytes */
+void vcomDataRxCb(void) {
+ /* do whatever after data has been received from host */
+
+ /* setEPRxCount on the previous cycle should garuntee
+ we havnt received more bytes than we can fit */
+ uint8 newBytes = GetEPRxCount(VCOM_RX_ENDP);
+ /* assert (newBytes <= maxNewBytes); */
+
+ if (recvBufIn + newBytes < VCOM_RX_EPSIZE) {
+ PMAToUserBufferCopy(&vcomBufferRx[recvBufIn],VCOM_RX_ADDR,newBytes);
+ recvBufIn += newBytes;
+ } else {
+ /* we have to copy the data in two chunks because we roll over
+ the edge of the circular buffer */
+ uint8 tailBytes = VCOM_RX_EPSIZE - recvBufIn;
+ uint8 remaining = newBytes - tailBytes;
+
+ PMAToUserBufferCopy(&vcomBufferRx[recvBufIn],VCOM_RX_ADDR,tailBytes);
+ PMAToUserBufferCopy(&vcomBufferRx[0], VCOM_RX_ADDR,remaining);
+
+ recvBufIn = (recvBufIn + newBytes ) % VCOM_RX_EPSIZE;
+ }
+
+ maxNewBytes -= newBytes;
+ SetEPRxCount(VCOM_RX_ENDP,maxNewBytes);
+ SetEPRxValid(VCOM_RX_ENDP);
+}
+
+void vcomManagementCb(void) {
+/* unused. This enpoint would callback if we had sent a linestate
+ changed notification */
+}
+
+u8* vcomGetSetLineCoding(uint16 length) {
+ if (length == 0) {
+ pInformation->Ctrl_Info.Usb_wLength = sizeof(USB_Line_Coding);
+ }
+ return (uint8*)&line_coding;
+}
+
+vcomSetLineState(void) {
+}
+
+void usbInit(void) {
+ pInformation->Current_Configuration = 0;
+ usbPowerOn();
+
+ _SetISTR(0);
+ wInterrupt_Mask = ISR_MSK;
+ _SetCNTR(wInterrupt_Mask);
+
+ usbEnbISR();
+ bDeviceState = UNCONNECTED;
+}
+
+void usbReset(void) {
+ pInformation->Current_Configuration = 0;
+
+ /* current feature is current bmAttributes */
+ pInformation->Current_Feature = (USB_CONFIG_ATTR_BUSPOWERED | USB_CONFIG_ATTR_SELF_POWERED);
+
+ _SetBTABLE(USB_BTABLE_ADDRESS);
+
+ /* setup control endpoint 0 */
+ _SetEPType(ENDP0, EP_CONTROL);
+ _SetEPTxStatus(ENDP0, EP_TX_STALL);
+ _SetEPRxAddr(ENDP0,VCOM_CTRL_RX_ADDR);
+ _SetEPTxAddr(ENDP0,VCOM_CTRL_TX_ADDR);
+ Clear_Status_Out(ENDP0);
+
+ SetEPRxCount(ENDP0, pProperty->MaxPacketSize);
+ SetEPRxValid(ENDP0);
+
+ /* setup management endpoint 1 */
+ SetEPType (VCOM_NOTIFICATION_ENDP, EP_INTERRUPT);
+ SetEPTxAddr (VCOM_NOTIFICATION_ENDP, VCOM_NOTIFICATION_ADDR);
+ SetEPTxStatus (VCOM_NOTIFICATION_ENDP, EP_TX_NAK);
+ SetEPRxStatus (VCOM_NOTIFICATION_ENDP, EP_RX_DIS);
+
+ /* setup data endpoint OUT (rx) */
+/* SetEPType (VCOM_RX_ENDP, EP_BULK); */
+/* SetEPRxAddr (VCOM_RX_ENDP, VCOM_RX_ADDR); */
+/* SetEPRxCount (VCOM_RX_ENDP, VCOM_RX_EPSIZE); */
+/* // SetEPTxStatus (VCOM_RX_ENDP, EP_TX_DIS); */
+/* SetEPRxStatus (VCOM_RX_ENDP, EP_RX_VALID); */
+
+ SetEPType (3, EP_BULK);
+ SetEPRxAddr (3, 0x110);
+ SetEPRxCount (3,64);
+ // SetEPTxStatus (VCOM_RX_ENDP, EP_TX_DIS);
+ SetEPRxStatus (3, EP_RX_VALID);
+
+ /* setup data endpoint IN (tx) */
+ SetEPType (VCOM_TX_ENDP, EP_BULK);
+ SetEPTxAddr (VCOM_TX_ENDP, VCOM_TX_ADDR);
+ SetEPTxStatus (VCOM_TX_ENDP, EP_TX_NAK);
+ SetEPRxStatus (VCOM_TX_ENDP, EP_RX_DIS);
+
+ bDeviceState = ATTACHED;
+ SetDeviceAddress(0);
+
+ /* reset the rx fifo */
+ recvBufIn = 0;
+ recvBufOut = 0;
+ maxNewBytes = VCOM_RX_EPSIZE;
+ countTx = 0;
+}
+
+
+void usbStatusIn(void) {
+ /* adjust the usart line coding
+ if we wish to couple the CDC line coding
+ with the real usart port */
+}
+
+void usbStatusOut(void) {
+}
+
+RESULT usbDataSetup(uint8 request) {
+ uint8 *(*CopyRoutine)(uint16);
+ CopyRoutine = NULL;
+
+ if (Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT)) {
+ switch (request) {
+ case (GET_LINE_CODING):
+ CopyRoutine = vcomGetSetLineCoding;
+ last_request = GET_LINE_CODING;
+ break;
+ case (SET_LINE_CODING):
+ CopyRoutine = vcomGetSetLineCoding;
+ last_request = SET_LINE_CODING;
+ break;
+ default: break;
+ }
+ }
+
+ if (CopyRoutine == NULL) {
+ return USB_UNSUPPORT;
+ }
+
+ pInformation->Ctrl_Info.CopyData = CopyRoutine;
+ pInformation->Ctrl_Info.Usb_wOffset = 0;
+ (*CopyRoutine)(0);
+ return USB_SUCCESS;
+}
+
+RESULT usbNoDataSetup(u8 request) {
+ uint8 new_signal;
+
+ /* we support set com feature but dont handle it */
+ if (Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT)) {
+
+ switch (request) {
+ case (SET_COMM_FEATURE):
+ return USB_SUCCESS;
+ case (SET_CONTROL_LINE_STATE):
+ /* to reset the board, pull both dtr and rts low
+ then pulse dtr by itself */
+ new_signal = pInformation->USBwValues.bw.bb0 & (CONTROL_LINE_DTR | CONTROL_LINE_RTS);
+ switch (reset_state) {
+ /* no default, covered enum */
+ case START:
+ if (new_signal == 0) {
+ reset_state = NDTR_NRTS;
+ }
+ break;
+
+ case NDTR_NRTS:
+ if (new_signal == CONTROL_LINE_DTR) {
+ reset_state = DTR_NRTS;
+ } else if (new_signal == 0) {
+ reset_state = NDTR_NRTS;
+ } else {
+ reset_state = START;
+ }
+ break;
+
+ case DTR_NRTS:
+ if (new_signal == 0) {
+ /* dont reset here, otherwise
+ well likely crash the host! */
+ reset_state = RESET_NOW;
+ } else {
+ reset_state = START;
+ }
+ break;
+ case RESET_NEXT:
+ reset_state = RESET_NOW;
+ break;
+ case RESET_NOW:
+ /* do nothing, wait for reset */
+ break;
+ }
+ return USB_SUCCESS;
+ }
+ }
+ return USB_UNSUPPORT;
+}
+
+RESULT usbGetInterfaceSetting(uint8 interface, uint8 alt_setting) {
+ if (alt_setting > 0) {
+ return USB_UNSUPPORT;
+ } else if (interface > 1) {
+ return USB_UNSUPPORT;
+ }
+
+ return USB_SUCCESS;
+}
+
+
+u8* usbGetDeviceDescriptor(u16 length) {
+ return Standard_GetDescriptorData(length, &Device_Descriptor);
+}
+
+u8* usbGetConfigDescriptor(u16 length) {
+ return Standard_GetDescriptorData(length, &Config_Descriptor);
+}
+
+u8* usbGetStringDescriptor(u16 length) {
+ uint8 wValue0 = pInformation->USBwValue0;
+
+ if (wValue0 > 2) {
+ return NULL;
+ }
+ return Standard_GetDescriptorData(length, &String_Descriptor[wValue0]);
+}
+
+/* internal callbacks to respond to standard requests */
+void usbSetConfiguration(void) {
+ if (pInformation->Current_Configuration != 0) {
+ bDeviceState = CONFIGURED;
+ }
+}
+
+void usbSetDeviceAddress(void) {
+ bDeviceState = ADDRESSED;
+}
+
diff --git a/libmaple/usb/usb_callbacks.h b/libmaple/usb/usb_callbacks.h
new file mode 100644
index 0000000..f8a2ef3
--- /dev/null
+++ b/libmaple/usb/usb_callbacks.h
@@ -0,0 +1,74 @@
+/* insert license */
+
+#ifndef __USB_CALLBACKS
+#define __USB_CALLBACKS
+
+#include "libmaple.h"
+#include "usb_lib.h"
+#include "usb_config.h"
+
+#define SET_LINE_CODING 0x20
+#define GET_LINE_CODING 0x21
+#define SET_COMM_FEATURE 0x02
+#define SET_CONTROL_LINE_STATE 0x22
+#define CONTROL_LINE_DTR (0x01)
+#define CONTROL_LINE_RTS (0x02)
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+typedef struct {
+ uint32 bitrate;
+ uint8 format;
+ uint8 paritytype;
+ uint8 datatype;
+} USB_Line_Coding;
+
+typedef enum {
+ START,
+ NDTR_NRTS,
+ DTR_NRTS,
+ RESET_NEXT,
+ RESET_NOW
+} RESET_STATE;
+
+extern RESET_STATE reset_state; /* tracks DTR/RTS */
+extern uint8 countTx;
+extern uint8 vcomBufferRx[VCOM_RX_EPSIZE]; /* no reason this has to be VCOM_RX_EPSIZE, could be bigger */
+extern uint8 recvBufIn; /* the FIFO in index to the recvbuffer */
+extern uint8 recvBufOut; /* the FIFO out index to the recvbuffer */
+extern uint8 maxNewBytes;
+
+void vcomDataTxCb(void);
+void vcomDataRxCb(void);
+void vcomManagementCb(void);
+
+uint8* vcomGetSetLineCoding(uint16 length);
+void vcomSetLineSate(void);
+
+void usbInit(void);
+/* internal functions (as per the usb_core pProperty structure) */
+void usbInit(void);
+void usbReset(void);
+void usbStatusIn(void);
+void usbStatusOut(void);
+
+RESULT usbDataSetup(uint8 request);
+RESULT usbNoDataSetup(uint8 request);
+RESULT usbGetInterfaceSetting(uint8,uint8);
+
+uint8* usbGetDeviceDescriptor(uint16 length);
+uint8* usbGetConfigDescriptor(uint16 length);
+uint8* usbGetStringDescriptor(uint16 length);
+
+/* internal callbacks to respond to standard requests */
+void usbSetConfiguration(void);
+void usbSetDeviceAddress(void);
+
+#if defined(__cplusplus)
+}
+#endif
+
+
+#endif
diff --git a/libmaple/usb/usb_config.h b/libmaple/usb/usb_config.h
new file mode 100644
index 0000000..e1eabb3
--- /dev/null
+++ b/libmaple/usb/usb_config.h
@@ -0,0 +1,53 @@
+/* insert license */
+
+#ifndef __USB_CONFIG_H
+#define __USB_CONFIG_H
+
+#include "usb_lib.h"
+
+#define VCOM_ID_VENDOR 0x1EAF
+#define VCOM_ID_PRODUCT 0x0004
+
+#define USB_CONFIG_MAX_POWER (100 >> 1)
+
+/* choose addresses to give endpoints the max 64 byte buffers */
+#define USB_BTABLE_ADDRESS 0x00
+#define VCOM_CTRL_EPNUM 0x00
+#define VCOM_CTRL_RX_ADDR 0x40
+#define VCOM_CTRL_TX_ADDR 0x80
+#define VCOM_CTRL_EPSIZE 0x40
+
+#define VCOM_TX_ENDP ENDP1
+#define VCOM_TX_EPNUM 0x01
+#define VCOM_TX_ADDR 0xC0
+#define VCOM_TX_EPSIZE 0x40
+
+#define VCOM_NOTIFICATION_ENDP ENDP2
+#define VCOM_NOTIFICATION_EPNUM 0x02
+#define VCOM_NOTIFICATION_ADDR 0x100
+#define VCOM_NOTIFICATION_EPSIZE 0x40
+
+#define VCOM_RX_ENDP ENDP3
+#define VCOM_RX_EPNUM 0x03
+#define VCOM_RX_ADDR 0x110
+#define VCOM_RX_EPSIZE 0x40
+
+#define bMaxPacketSize 0x40 /* 64B, maximum for USB FS Devices */
+
+#define NUM_ENDPTS 0x04
+
+/* handle all usb interrupts */
+#define ISR_MSK ( CNTR_CTRM | \
+ CNTR_WKUPM | \
+ CNTR_SUSPM | \
+ CNTR_ERRM | \
+ CNTR_SOFM | \
+ CNTR_ESOFM | \
+ CNTR_RESETM )
+
+#define USB_DISC_BANK GPIOC_BASE
+#define USB_DISC_PIN 12
+
+#define F_SUSPEND_ENABLED 1
+
+#endif
diff --git a/libmaple/usb/usb_hardware.c b/libmaple/usb/usb_hardware.c
new file mode 100644
index 0000000..2f37df6
--- /dev/null
+++ b/libmaple/usb/usb_hardware.c
@@ -0,0 +1,129 @@
+/* *****************************************************************************
+ * 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.
+ * ****************************************************************************/
+
+/**
+ * @file hardware.c
+ *
+ * @brief init routines to setup clocks and interrupts for usb.
+ *
+ */
+
+#include "usb_hardware.h"
+
+void setPin(u32 bank, u8 pin) {
+ u32 pinMask = 0x1 << (pin);
+ SET_REG(GPIO_BSRR(bank),pinMask);
+}
+
+void resetPin(u32 bank, u8 pin) {
+ u32 pinMask = 0x1 << (16+pin);
+ SET_REG(GPIO_BSRR(bank),pinMask);
+}
+
+void systemReset(void) {
+ SET_REG(RCC_CR, GET_REG(RCC_CR) | 0x00000001);
+ SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) & 0xF8FF0000);
+ SET_REG(RCC_CR, GET_REG(RCC_CR) & 0xFEF6FFFF);
+ SET_REG(RCC_CR, GET_REG(RCC_CR) & 0xFFFBFFFF);
+ SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) & 0xFF80FFFF);
+
+ SET_REG(RCC_CIR, 0x00000000); /* disable all RCC interrupts */
+}
+
+void setupCLK (void) {
+ /* enable HSE */
+ SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x00010001);
+ while ((GET_REG(RCC_CR) & 0x00020000) == 0); /* for it to come on */
+
+ /* Configure PLL */
+ SET_REG(RCC_CFGR,GET_REG(RCC_CFGR) | 0x001D0400); /* pll=72Mhz,APB1=36Mhz,AHB=72Mhz */
+ SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x01000000); /* enable the pll */
+ while ((GET_REG(RCC_CR) & 0x03000000) == 0); /* wait for it to come on */
+
+ /* Set SYSCLK as PLL */
+ SET_REG(RCC_CFGR,GET_REG(RCC_CFGR) | 0x00000002);
+ while ((GET_REG(RCC_CFGR) & 0x00000008) == 0); /* wait for it to come on */
+}
+
+
+void nvicInit(NVIC_InitTypeDef* NVIC_InitStruct) {
+ u32 tmppriority = 0x00;
+ u32 tmpreg = 0x00;
+ u32 tmpmask = 0x00;
+ u32 tmppre = 0;
+ u32 tmpsub = 0x0F;
+
+ SCB_TypeDef* rSCB = (SCB_TypeDef *) SCB_BASE;
+ NVIC_TypeDef* rNVIC = (NVIC_TypeDef *) NVIC_BASE;
+
+
+ /* Compute the Corresponding IRQ Priority --------------------------------*/
+ tmppriority = (0x700 - (rSCB->AIRCR & (u32)0x700))>> 0x08;
+ tmppre = (0x4 - tmppriority);
+ tmpsub = tmpsub >> tmppriority;
+
+ tmppriority = (u32)NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre;
+ tmppriority |= NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub;
+
+ tmppriority = tmppriority << 0x04;
+ tmppriority = ((u32)tmppriority) << ((NVIC_InitStruct->NVIC_IRQChannel & (u8)0x03) * 0x08);
+
+ tmpreg = rNVIC->IPR[(NVIC_InitStruct->NVIC_IRQChannel >> 0x02)];
+ tmpmask = (u32)0xFF << ((NVIC_InitStruct->NVIC_IRQChannel & (u8)0x03) * 0x08);
+ tmpreg &= ~tmpmask;
+ tmppriority &= tmpmask;
+ tmpreg |= tmppriority;
+
+ rNVIC->IPR[(NVIC_InitStruct->NVIC_IRQChannel >> 0x02)] = tmpreg;
+
+ /* Enable the Selected IRQ Channels --------------------------------------*/
+ rNVIC->ISER[(NVIC_InitStruct->NVIC_IRQChannel >> 0x05)] =
+ (u32)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (u8)0x1F);
+}
+
+void nvicDisableInterrupts() {
+ NVIC_TypeDef* rNVIC = (NVIC_TypeDef *) NVIC_BASE;
+ rNVIC->ICER[0] = 0xFFFFFFFF;
+ rNVIC->ICER[1] = 0xFFFFFFFF;
+ rNVIC->ICPR[0] = 0xFFFFFFFF;
+ rNVIC->ICPR[1] = 0xFFFFFFFF;
+
+ SET_REG(STK_CTRL,0x04); /* disable the systick, which operates separately from nvic */
+}
+
+void systemHardReset(void) {
+ SCB_TypeDef* rSCB = (SCB_TypeDef *) SCB_BASE;
+ typedef void (*funcPtr)(void);
+
+ /* Reset */
+ rSCB->AIRCR = (u32)AIRCR_RESET_REQ;
+
+ /* should never get here */
+ while (1) {
+ asm volatile("nop");
+ }
+}
+
+
+
diff --git a/libmaple/usb/usb_hardware.h b/libmaple/usb/usb_hardware.h
new file mode 100644
index 0000000..208fa3a
--- /dev/null
+++ b/libmaple/usb/usb_hardware.h
@@ -0,0 +1,157 @@
+/* *****************************************************************************
+ * 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 __HARDWARE_H
+#define __HARDWARE_H
+
+#include "usb_type.h"
+
+/* macro'd register and peripheral definitions */
+#define EXC_RETURN 0xFFFFFFF9
+#define DEFAULT_CPSR 0x61000000
+#define STACK_TOP 0x20005000
+
+#define RCC ((u32)0x40021000)
+#define FLASH ((u32)0x40022000)
+#define GPIOA ((u32)0x40010800)
+#define GPIOC ((u32)0x40011000)
+
+#define USB_PACKET_BUFFER ((u32)0x40006000)
+
+#define RCC_CR RCC
+#define RCC_CFGR RCC + 0x04
+#define RCC_CIR RCC + 0x08
+#define RCC_AHBENR RCC + 0x14
+#define RCC_APB2ENR RCC + 0x18
+#define RCC_APB1ENR RCC + 0x16
+
+#define GPIO_CRL(port) port
+#define GPIO_CRH(port) port+0x04
+#define GPIO_ODR(port) port+0x0c
+#define GPIO_BSRR(port) port+0x10
+
+#define SCS_BASE ((u32)0xE000E000)
+#define NVIC_BASE (SCS_BASE + 0x0100)
+#define SCB_BASE (SCS_BASE + 0x0D00)
+
+#define SCS 0xE000E000
+#define NVIC (SCS+0x100)
+#define SCB (SCS+0xD00)
+#define STK (SCS+0x10)
+
+#define SCB_VTOR (SCB+0x08)
+#define STK_CTRL (STK+0x00)
+
+#define USB_HP_IRQ ((u8)0x13)
+#define USB_LP_IRQ ((u8)0x14)
+
+/* AIRCR */
+#define AIRCR_RESET 0x05FA0000
+#define AIRCR_RESET_REQ (AIRCR_RESET | (u32)0x04);
+
+/* temporary copyage of example from kiel */
+#define __VAL(__TIMCLK, __PERIOD) ((__TIMCLK/1000000UL)*__PERIOD)
+#define __PSC(__TIMCLK, __PERIOD) (((__VAL(__TIMCLK, __PERIOD)+49999UL)/50000UL) - 1)
+#define __ARR(__TIMCLK, __PERIOD) ((__VAL(__TIMCLK, __PERIOD)/(__PSC(__TIMCLK, __PERIOD)+1)) - 1)
+
+/* todo, wrap in do whiles for the natural ; */
+#define SET_REG(addr,val) *(vu32*)(addr)=val
+#define GET_REG(addr) *(vu32*)(addr)
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+/* todo: there must be some major misunderstanding in how we access regs. The direct access approach (GET_REG)
+ causes the usb init to fail upon trying to activate RCC_APB1 |= 0x00800000. However, using the struct approach
+ from ST, it works fine...temporarily switching to that approach */
+typedef struct
+{
+ vu32 CR;
+ vu32 CFGR;
+ vu32 CIR;
+ vu32 APB2RSTR;
+ vu32 APB1RSTR;
+ vu32 AHBENR;
+ vu32 APB2ENR;
+ vu32 APB1ENR;
+ vu32 BDCR;
+ vu32 CSR;
+} RCC_RegStruct;
+#define pRCC ((RCC_RegStruct *) RCC)
+
+typedef struct {
+ vu32 ISER[2];
+ u32 RESERVED0[30];
+ vu32 ICER[2];
+ u32 RSERVED1[30];
+ vu32 ISPR[2];
+ u32 RESERVED2[30];
+ vu32 ICPR[2];
+ u32 RESERVED3[30];
+ vu32 IABR[2];
+ u32 RESERVED4[62];
+ vu32 IPR[15];
+} NVIC_TypeDef;
+
+typedef struct {
+ u8 NVIC_IRQChannel;
+ u8 NVIC_IRQChannelPreemptionPriority;
+ u8 NVIC_IRQChannelSubPriority;
+ USB_Bool NVIC_IRQChannelCmd; /* TRUE for enable */
+} NVIC_InitTypeDef;
+
+typedef struct {
+ vuc32 CPUID;
+ vu32 ICSR;
+ vu32 VTOR;
+ vu32 AIRCR;
+ vu32 SCR;
+ vu32 CCR;
+ vu32 SHPR[3];
+ vu32 SHCSR;
+ vu32 CFSR;
+ vu32 HFSR;
+ vu32 DFSR;
+ vu32 MMFAR;
+ vu32 BFAR;
+ vu32 AFSR;
+} SCB_TypeDef;
+
+
+void setPin (u32 bank, u8 pin);
+void resetPin (u32 bank, u8 pin);
+
+void systemHardReset(void);
+void systemReset (void);
+void setupCLK (void);
+
+void nvicInit (NVIC_InitTypeDef*);
+void nvicDisableInterrupts(void);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
diff --git a/libmaple/usb/usb_lib/usb_core.c b/libmaple/usb/usb_lib/usb_core.c
new file mode 100644
index 0000000..223a2e1
--- /dev/null
+++ b/libmaple/usb/usb_lib/usb_core.c
@@ -0,0 +1,1016 @@
+/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
+* File Name : usb_core.c
+* Author : MCD Application Team
+* Version : V2.2.1
+* Date : 09/22/2008
+* Description : Standard protocol processing (USB v2.0)
+********************************************************************************
+* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
+* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
+* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
+* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
+* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
+* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
+*******************************************************************************/
+
+/* Includes ------------------------------------------------------------------*/
+#include "usb_lib.h"
+/* Private typedef -----------------------------------------------------------*/
+/* Private define ------------------------------------------------------------*/
+#define ValBit(VAR,Place) (VAR & (1 << Place))
+#define SetBit(VAR,Place) (VAR |= (1 << Place))
+#define ClrBit(VAR,Place) (VAR &= ((1 << Place) ^ 255))
+
+#define Send0LengthData() { _SetEPTxCount(ENDP0, 0); \
+ vSetEPTxStatus(EP_TX_VALID); \
+ }
+
+#define vSetEPRxStatus(st) (SaveRState = st)
+#define vSetEPTxStatus(st) (SaveTState = st)
+
+#define USB_StatusIn() Send0LengthData()
+#define USB_StatusOut() vSetEPRxStatus(EP_RX_VALID)
+
+#define StatusInfo0 StatusInfo.bw.bb1 /* Reverse bb0 & bb1 */
+#define StatusInfo1 StatusInfo.bw.bb0
+
+/* Private macro -------------------------------------------------------------*/
+/* Private variables ---------------------------------------------------------*/
+u16_u8 StatusInfo;
+USB_Bool Data_Mul_MaxPacketSize = FALSE;
+/* Private function prototypes -----------------------------------------------*/
+static void DataStageOut(void);
+static void DataStageIn(void);
+static void NoData_Setup0(void);
+static void Data_Setup0(void);
+/* Private functions ---------------------------------------------------------*/
+
+/*******************************************************************************
+* Function Name : Standard_GetConfiguration.
+* Description : Return the current configuration variable address.
+* Input : Length - How many bytes are needed.
+* Output : None.
+* Return : Return 1 , if the request is invalid when "Length" is 0.
+* Return "Buffer" if the "Length" is not 0.
+*******************************************************************************/
+u8 *Standard_GetConfiguration(u16 Length)
+{
+ if (Length == 0)
+ {
+ pInformation->Ctrl_Info.Usb_wLength =
+ sizeof(pInformation->Current_Configuration);
+ return 0;
+ }
+ pUser_Standard_Requests->User_GetConfiguration();
+ return (u8 *)&pInformation->Current_Configuration;
+}
+
+/*******************************************************************************
+* Function Name : Standard_SetConfiguration.
+* Description : This routine is called to set the configuration value
+* Then each class should configure device themself.
+* Input : None.
+* Output : None.
+* Return : Return USB_SUCCESS, if the request is performed.
+* Return USB_UNSUPPORT, if the request is invalid.
+*******************************************************************************/
+RESULT Standard_SetConfiguration(void)
+{
+
+ if ((pInformation->USBwValue0 <=
+ Device_Table.Total_Configuration) && (pInformation->USBwValue1 == 0)
+ && (pInformation->USBwIndex == 0)) /*call Back usb spec 2.0*/
+ {
+ pInformation->Current_Configuration = pInformation->USBwValue0;
+ pUser_Standard_Requests->User_SetConfiguration();
+ return USB_SUCCESS;
+ }
+ else
+ {
+ return USB_UNSUPPORT;
+ }
+}
+
+/*******************************************************************************
+* Function Name : Standard_GetInterface.
+* Description : Return the Alternate Setting of the current interface.
+* Input : Length - How many bytes are needed.
+* Output : None.
+* Return : Return 0, if the request is invalid when "Length" is 0.
+* Return "Buffer" if the "Length" is not 0.
+*******************************************************************************/
+u8 *Standard_GetInterface(u16 Length)
+{
+ if (Length == 0)
+ {
+ pInformation->Ctrl_Info.Usb_wLength =
+ sizeof(pInformation->Current_AlternateSetting);
+ return 0;
+ }
+ pUser_Standard_Requests->User_GetInterface();
+ return (u8 *)&pInformation->Current_AlternateSetting;
+}
+
+/*******************************************************************************
+* Function Name : Standard_SetInterface.
+* Description : This routine is called to set the interface.
+* Then each class should configure the interface them self.
+* Input : None.
+* Output : None.
+* Return : - Return USB_SUCCESS, if the request is performed.
+* - Return USB_UNSUPPORT, if the request is invalid.
+*******************************************************************************/
+RESULT Standard_SetInterface(void)
+{
+ RESULT Re;
+ /*Test if the specified Interface and Alternate Setting are supported by
+ the application Firmware*/
+ Re = (*pProperty->Class_Get_Interface_Setting)(pInformation->USBwIndex0, pInformation->USBwValue0);
+
+ if (pInformation->Current_Configuration != 0)
+ {
+ if ((Re != USB_SUCCESS) || (pInformation->USBwIndex1 != 0)
+ || (pInformation->USBwValue1 != 0))
+ {
+ return USB_UNSUPPORT;
+ }
+ else if (Re == USB_SUCCESS)
+ {
+ pUser_Standard_Requests->User_SetInterface();
+ pInformation->Current_Interface = pInformation->USBwIndex0;
+ pInformation->Current_AlternateSetting = pInformation->USBwValue0;
+ return USB_SUCCESS;
+ }
+
+ }
+
+ return USB_UNSUPPORT;
+}
+
+/*******************************************************************************
+* Function Name : Standard_GetStatus.
+* Description : Copy the device request data to "StatusInfo buffer".
+* Input : - Length - How many bytes are needed.
+* Output : None.
+* Return : Return 0, if the request is at end of data block,
+* or is invalid when "Length" is 0.
+*******************************************************************************/
+u8 *Standard_GetStatus(u16 Length)
+{
+ if (Length == 0)
+ {
+ pInformation->Ctrl_Info.Usb_wLength = 2;
+ return 0;
+ }
+
+ StatusInfo.w = 0;
+ /* Reset Status Information */
+
+ if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT))
+ {
+ /*Get Device Status */
+ u8 Feature = pInformation->Current_Feature;
+
+ /* Remote Wakeup enabled */
+ if (ValBit(Feature, 5))
+ {
+ SetBit(StatusInfo0, 1);
+ }
+
+ /* Bus-powered */
+ if (ValBit(Feature, 6))
+ {
+ ClrBit(StatusInfo0, 0);
+ }
+ else /* Self-powered */
+ {
+ SetBit(StatusInfo0, 0);
+ }
+ }
+ /*Interface Status*/
+ else if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT))
+ {
+ return (u8 *)&StatusInfo;
+ }
+ /*Get EndPoint Status*/
+ else if (Type_Recipient == (STANDARD_REQUEST | ENDPOINT_RECIPIENT))
+ {
+ u8 Related_Endpoint;
+ u8 wIndex0 = pInformation->USBwIndex0;
+
+ Related_Endpoint = (wIndex0 & 0x0f);
+ if (ValBit(wIndex0, 7))
+ {
+ /* IN endpoint */
+ if (_GetTxStallStatus(Related_Endpoint))
+ {
+ SetBit(StatusInfo0, 0); /* IN Endpoint stalled */
+ }
+ }
+ else
+ {
+ /* OUT endpoint */
+ if (_GetRxStallStatus(Related_Endpoint))
+ {
+ SetBit(StatusInfo0, 0); /* OUT Endpoint stalled */
+ }
+ }
+
+ }
+ else
+ {
+ return NULL;
+ }
+ pUser_Standard_Requests->User_GetStatus();
+ return (u8 *)&StatusInfo;
+}
+
+/*******************************************************************************
+* Function Name : Standard_ClearFeature.
+* Description : Clear or disable a specific feature.
+* Input : None.
+* Output : None.
+* Return : - Return USB_SUCCESS, if the request is performed.
+* - Return USB_UNSUPPORT, if the request is invalid.
+*******************************************************************************/
+RESULT Standard_ClearFeature(void)
+{
+ u32 Type_Rec = Type_Recipient;
+ u32 Status;
+
+
+ if (Type_Rec == (STANDARD_REQUEST | DEVICE_RECIPIENT))
+ {/*Device Clear Feature*/
+ ClrBit(pInformation->Current_Feature, 5);
+ return USB_SUCCESS;
+ }
+ else if (Type_Rec == (STANDARD_REQUEST | ENDPOINT_RECIPIENT))
+ {/*EndPoint Clear Feature*/
+ DEVICE* pDev;
+ u32 Related_Endpoint;
+ u32 wIndex0;
+ u32 rEP;
+
+ if ((pInformation->USBwValue != ENDPOINT_STALL)
+ || (pInformation->USBwIndex1 != 0))
+ {
+ return USB_UNSUPPORT;
+ }
+
+ pDev = &Device_Table;
+ wIndex0 = pInformation->USBwIndex0;
+ rEP = wIndex0 & ~0x80;
+ Related_Endpoint = ENDP0 + rEP;
+
+ if (ValBit(pInformation->USBwIndex0, 7))
+ {
+ /*Get Status of endpoint & stall the request if the related_ENdpoint
+ is Disabled*/
+ Status = _GetEPTxStatus(Related_Endpoint);
+ }
+ else
+ {
+ Status = _GetEPRxStatus(Related_Endpoint);
+ }
+
+ if ((rEP >= pDev->Total_Endpoint) || (Status == 0)
+ || (pInformation->Current_Configuration == 0))
+ {
+ return USB_UNSUPPORT;
+ }
+
+
+ if (wIndex0 & 0x80)
+ {
+ /* IN endpoint */
+ if (_GetTxStallStatus(Related_Endpoint ))
+ {
+ ClearDTOG_TX(Related_Endpoint);
+ SetEPTxStatus(Related_Endpoint, EP_TX_VALID);
+ }
+ }
+ else
+ {
+ /* OUT endpoint */
+ if (_GetRxStallStatus(Related_Endpoint))
+ {
+ if (Related_Endpoint == ENDP0)
+ {
+ /* After clear the STALL, enable the default endpoint receiver */
+ SetEPRxCount(Related_Endpoint, Device_Property.MaxPacketSize);
+ _SetEPRxStatus(Related_Endpoint, EP_RX_VALID);
+ }
+ else
+ {
+ ClearDTOG_RX(Related_Endpoint);
+ _SetEPRxStatus(Related_Endpoint, EP_RX_VALID);
+ }
+ }
+ }
+ pUser_Standard_Requests->User_ClearFeature();
+ return USB_SUCCESS;
+ }
+
+ return USB_UNSUPPORT;
+}
+
+/*******************************************************************************
+* Function Name : Standard_SetEndPointFeature
+* Description : Set or enable a specific feature of EndPoint
+* Input : None.
+* Output : None.
+* Return : - Return USB_SUCCESS, if the request is performed.
+* - Return USB_UNSUPPORT, if the request is invalid.
+*******************************************************************************/
+RESULT Standard_SetEndPointFeature(void)
+{
+ u32 wIndex0;
+ u32 Related_Endpoint;
+ u32 rEP;
+ u32 Status;
+
+ wIndex0 = pInformation->USBwIndex0;
+ rEP = wIndex0 & ~0x80;
+ Related_Endpoint = ENDP0 + rEP;
+
+ if (ValBit(pInformation->USBwIndex0, 7))
+ {
+ /* get Status of endpoint & stall the request if the related_ENdpoint
+ is Disabled*/
+ Status = _GetEPTxStatus(Related_Endpoint);
+ }
+ else
+ {
+ Status = _GetEPRxStatus(Related_Endpoint);
+ }
+
+ if (Related_Endpoint >= Device_Table.Total_Endpoint
+ || pInformation->USBwValue != 0 || Status == 0
+ || pInformation->Current_Configuration == 0)
+ {
+ return USB_UNSUPPORT;
+ }
+ else
+ {
+ if (wIndex0 & 0x80)
+ {
+ /* IN endpoint */
+ _SetEPTxStatus(Related_Endpoint, EP_TX_STALL);
+ }
+
+ else
+ {
+ /* OUT endpoint */
+ _SetEPRxStatus(Related_Endpoint, EP_RX_STALL);
+ }
+ }
+ pUser_Standard_Requests->User_SetEndPointFeature();
+ return USB_SUCCESS;
+}
+
+/*******************************************************************************
+* Function Name : Standard_SetDeviceFeature.
+* Description : Set or enable a specific feature of Device.
+* Input : None.
+* Output : None.
+* Return : - Return USB_SUCCESS, if the request is performed.
+* - Return USB_UNSUPPORT, if the request is invalid.
+*******************************************************************************/
+RESULT Standard_SetDeviceFeature(void)
+{
+ SetBit(pInformation->Current_Feature, 5);
+ pUser_Standard_Requests->User_SetDeviceFeature();
+ return USB_SUCCESS;
+}
+
+/*******************************************************************************
+* Function Name : Standard_GetDescriptorData.
+* Description : Standard_GetDescriptorData is used for descriptors transfer.
+* : This routine is used for the descriptors resident in Flash
+* or RAM
+* pDesc can be in either Flash or RAM
+* The purpose of this routine is to have a versatile way to
+* response descriptors request. It allows user to generate
+* certain descriptors with software or read descriptors from
+* external storage part by part.
+* Input : - Length - Length of the data in this transfer.
+* - pDesc - A pointer points to descriptor struct.
+* The structure gives the initial address of the descriptor and
+* its original size.
+* Output : None.
+* Return : Address of a part of the descriptor pointed by the Usb_
+* wOffset The buffer pointed by this address contains at least
+* Length bytes.
+*******************************************************************************/
+u8 *Standard_GetDescriptorData(u16 Length, ONE_DESCRIPTOR *pDesc)
+{
+ u32 wOffset;
+
+ wOffset = pInformation->Ctrl_Info.Usb_wOffset;
+ if (Length == 0)
+ {
+ pInformation->Ctrl_Info.Usb_wLength = pDesc->Descriptor_Size - wOffset;
+ return 0;
+ }
+
+ return pDesc->Descriptor + wOffset;
+}
+
+/*******************************************************************************
+* Function Name : DataStageOut.
+* Description : Data stage of a Control Write Transfer.
+* Input : None.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void DataStageOut(void)
+{
+ ENDPOINT_INFO *pEPinfo = &pInformation->Ctrl_Info;
+ u32 save_rLength;
+
+ save_rLength = pEPinfo->Usb_rLength;
+
+ if (pEPinfo->CopyData && save_rLength)
+ {
+ u8 *Buffer;
+ u32 Length;
+
+ Length = pEPinfo->PacketSize;
+ if (Length > save_rLength)
+ {
+ Length = save_rLength;
+ }
+
+ Buffer = (*pEPinfo->CopyData)(Length);
+ pEPinfo->Usb_rLength -= Length;
+ pEPinfo->Usb_rOffset += Length;
+
+ PMAToUserBufferCopy(Buffer, GetEPRxAddr(ENDP0), Length);
+ }
+
+ if (pEPinfo->Usb_rLength != 0)
+ {
+ vSetEPRxStatus(EP_RX_VALID);/* re-enable for next data reception */
+ SetEPTxCount(ENDP0, 0);
+ vSetEPTxStatus(EP_TX_VALID);/* Expect the host to abort the data OUT stage */
+ }
+ /* Set the next State*/
+ if (pEPinfo->Usb_rLength >= pEPinfo->PacketSize)
+ {
+ pInformation->ControlState = OUT_DATA;
+ }
+ else
+ {
+ if (pEPinfo->Usb_rLength > 0)
+ {
+ pInformation->ControlState = LAST_OUT_DATA;
+ }
+ else if (pEPinfo->Usb_rLength == 0)
+ {
+ pInformation->ControlState = WAIT_STATUS_IN;
+ USB_StatusIn();
+ }
+ }
+}
+
+/*******************************************************************************
+* Function Name : DataStageIn.
+* Description : Data stage of a Control Read Transfer.
+* Input : None.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void DataStageIn(void)
+{
+ ENDPOINT_INFO *pEPinfo = &pInformation->Ctrl_Info;
+ u32 save_wLength = pEPinfo->Usb_wLength;
+ u32 ControlState = pInformation->ControlState;
+
+ u8 *DataBuffer;
+ u32 Length;
+
+ if ((save_wLength == 0) && (ControlState == LAST_IN_DATA))
+ {
+ if(Data_Mul_MaxPacketSize == TRUE)
+ {
+ /* No more data to send and empty packet */
+ Send0LengthData();
+ ControlState = LAST_IN_DATA;
+ Data_Mul_MaxPacketSize = FALSE;
+ }
+ else
+ {
+ /* No more data to send so STALL the TX Status*/
+ ControlState = WAIT_STATUS_OUT;
+ vSetEPTxStatus(EP_TX_STALL);
+ }
+
+ goto Expect_Status_Out;
+ }
+
+ Length = pEPinfo->PacketSize;
+ ControlState = (save_wLength <= Length) ? LAST_IN_DATA : IN_DATA;
+
+ if (Length > save_wLength)
+ {
+ Length = save_wLength;
+ }
+
+ DataBuffer = (*pEPinfo->CopyData)(Length);
+
+ UserToPMABufferCopy(DataBuffer, GetEPTxAddr(ENDP0), Length);
+
+ SetEPTxCount(ENDP0, Length);
+
+ pEPinfo->Usb_wLength -= Length;
+ pEPinfo->Usb_wOffset += Length;
+ vSetEPTxStatus(EP_TX_VALID);
+
+ USB_StatusOut();/* Expect the host to abort the data IN stage */
+
+Expect_Status_Out:
+ pInformation->ControlState = ControlState;
+}
+
+/*******************************************************************************
+* Function Name : NoData_Setup0.
+* Description : Proceed the processing of setup request without data stage.
+* Input : None.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void NoData_Setup0(void)
+{
+ RESULT Result = USB_UNSUPPORT;
+ u32 RequestNo = pInformation->USBbRequest;
+ u32 ControlState;
+
+ if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT))
+ {
+ /* Device Request*/
+ /* SET_CONFIGURATION*/
+ if (RequestNo == SET_CONFIGURATION)
+ {
+ Result = Standard_SetConfiguration();
+ }
+
+ /*SET ADDRESS*/
+ else if (RequestNo == SET_ADDRESS)
+ {
+ if ((pInformation->USBwValue0 > 127) || (pInformation->USBwValue1 != 0)
+ || (pInformation->USBwIndex != 0)
+ || (pInformation->Current_Configuration != 0))
+ /* Device Address should be 127 or less*/
+ {
+ ControlState = STALLED;
+ goto exit_NoData_Setup0;
+ }
+ else
+ {
+ Result = USB_SUCCESS;
+ }
+ }
+ /*SET FEATURE for Device*/
+ else if (RequestNo == SET_FEATURE)
+ {
+ if ((pInformation->USBwValue0 == DEVICE_REMOTE_WAKEUP)
+ && (pInformation->USBwIndex == 0)
+ && (ValBit(pInformation->Current_Feature, 5)))
+ {
+ Result = Standard_SetDeviceFeature();
+ }
+ else
+ {
+ Result = USB_UNSUPPORT;
+ }
+ }
+ /*Clear FEATURE for Device */
+ else if (RequestNo == CLEAR_FEATURE)
+ {
+ if (pInformation->USBwValue0 == DEVICE_REMOTE_WAKEUP
+ && pInformation->USBwIndex == 0
+ && ValBit(pInformation->Current_Feature, 5))
+ {
+ Result = Standard_ClearFeature();
+ }
+ else
+ {
+ Result = USB_UNSUPPORT;
+ }
+ }
+
+ }
+
+ /* Interface Request*/
+ else if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT))
+ {
+ /*SET INTERFACE*/
+ if (RequestNo == SET_INTERFACE)
+ {
+ Result = Standard_SetInterface();
+ }
+ }
+
+ /* EndPoint Request*/
+ else if (Type_Recipient == (STANDARD_REQUEST | ENDPOINT_RECIPIENT))
+ {
+ /*CLEAR FEATURE for EndPoint*/
+ if (RequestNo == CLEAR_FEATURE)
+ {
+ Result = Standard_ClearFeature();
+ }
+ /* SET FEATURE for EndPoint*/
+ else if (RequestNo == SET_FEATURE)
+ {
+ Result = Standard_SetEndPointFeature();
+ }
+ }
+ else
+ {
+ Result = USB_UNSUPPORT;
+ }
+
+
+ if (Result != USB_SUCCESS)
+ {
+ Result = (*pProperty->Class_NoData_Setup)(RequestNo);
+ if (Result == USB_NOT_READY)
+ {
+ ControlState = PAUSE;
+ goto exit_NoData_Setup0;
+ }
+ }
+
+ if (Result != USB_SUCCESS)
+ {
+ ControlState = STALLED;
+ goto exit_NoData_Setup0;
+ }
+
+ ControlState = WAIT_STATUS_IN;/* After no data stage SETUP */
+
+ USB_StatusIn();
+
+exit_NoData_Setup0:
+ pInformation->ControlState = ControlState;
+ return;
+}
+
+/*******************************************************************************
+* Function Name : Data_Setup0.
+* Description : Proceed the processing of setup request with data stage.
+* Input : None.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void Data_Setup0(void)
+{
+ u8 *(*CopyRoutine)(u16);
+ RESULT Result;
+ u32 Request_No = pInformation->USBbRequest;
+
+ u32 Related_Endpoint, Reserved;
+ u32 wOffset, Status;
+
+
+
+ CopyRoutine = NULL;
+ wOffset = 0;
+
+ if (Request_No == GET_DESCRIPTOR)
+ {
+ if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT))
+ {
+ u8 wValue1 = pInformation->USBwValue1;
+ if (wValue1 == DEVICE_DESCRIPTOR)
+ {
+ CopyRoutine = pProperty->GetDeviceDescriptor;
+ }
+ else if (wValue1 == CONFIG_DESCRIPTOR)
+ {
+ CopyRoutine = pProperty->GetConfigDescriptor;
+ }
+ else if (wValue1 == STRING_DESCRIPTOR)
+ {
+ CopyRoutine = pProperty->GetStringDescriptor;
+ } else if (wValue1 == 0x21) /* added to support functional descriptors */
+ {
+ CopyRoutine = pProperty->GetFunctionalDescriptor;
+ } /* End of GET_DESCRIPTOR */
+ }
+ }
+
+ /*GET STATUS*/
+ else if ((Request_No == GET_STATUS) && (pInformation->USBwValue == 0)
+ && (pInformation->USBwLength == 0x0002)
+ && (pInformation->USBwIndex1 == 0))
+ {
+ /* GET STATUS for Device*/
+ if ((Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT))
+ && (pInformation->USBwIndex == 0))
+ {
+ CopyRoutine = Standard_GetStatus;
+ }
+
+ /* GET STATUS for Interface*/
+ else if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT))
+ {
+ if (((*pProperty->Class_Get_Interface_Setting)(pInformation->USBwIndex0, 0) == USB_SUCCESS)
+ && (pInformation->Current_Configuration != 0))
+ {
+ CopyRoutine = Standard_GetStatus;
+ }
+ }
+
+ /* GET STATUS for EndPoint*/
+ else if (Type_Recipient == (STANDARD_REQUEST | ENDPOINT_RECIPIENT))
+ {
+ Related_Endpoint = (pInformation->USBwIndex0 & 0x0f);
+ Reserved = pInformation->USBwIndex0 & 0x70;
+
+ if (ValBit(pInformation->USBwIndex0, 7))
+ {
+ /*Get Status of endpoint & stall the request if the related_ENdpoint
+ is Disabled*/
+ Status = _GetEPTxStatus(Related_Endpoint);
+ }
+ else
+ {
+ Status = _GetEPRxStatus(Related_Endpoint);
+ }
+
+ if ((Related_Endpoint < Device_Table.Total_Endpoint) && (Reserved == 0)
+ && (Status != 0))
+ {
+ CopyRoutine = Standard_GetStatus;
+ }
+ }
+
+ }
+
+ /*GET CONFIGURATION*/
+ else if (Request_No == GET_CONFIGURATION)
+ {
+ if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT))
+ {
+ CopyRoutine = Standard_GetConfiguration;
+ }
+ }
+ /*GET INTERFACE*/
+ else if (Request_No == GET_INTERFACE)
+ {
+ if ((Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT))
+ && (pInformation->Current_Configuration != 0) && (pInformation->USBwValue == 0)
+ && (pInformation->USBwIndex1 == 0) && (pInformation->USBwLength == 0x0001)
+ && ((*pProperty->Class_Get_Interface_Setting)(pInformation->USBwIndex0, 0) == USB_SUCCESS))
+ {
+ CopyRoutine = Standard_GetInterface;
+ }
+
+ }
+
+ if (CopyRoutine)
+ {
+ pInformation->Ctrl_Info.Usb_wOffset = wOffset;
+ pInformation->Ctrl_Info.CopyData = CopyRoutine;
+ /* sb in the original the cast to word was directly */
+ /* now the cast is made step by step */
+ (*CopyRoutine)(0);
+ Result = USB_SUCCESS;
+ }
+ else
+ {
+ Result = (*pProperty->Class_Data_Setup)(pInformation->USBbRequest);
+ if (Result == USB_NOT_READY)
+ {
+ pInformation->ControlState = PAUSE;
+ return;
+ }
+ }
+
+ if (pInformation->Ctrl_Info.Usb_wLength == 0xFFFF)
+ {
+ /* Data is not ready, wait it */
+ pInformation->ControlState = PAUSE;
+ return;
+ }
+ if ((Result == USB_UNSUPPORT) || (pInformation->Ctrl_Info.Usb_wLength == 0))
+ {
+ /* Unsupported request */
+ pInformation->ControlState = STALLED;
+ return;
+ }
+
+
+ if (ValBit(pInformation->USBbmRequestType, 7))
+ {
+ /* Device ==> Host */
+ vu32 wLength = pInformation->USBwLength;
+
+ /* Restrict the data length to be the one host asks */
+ if (pInformation->Ctrl_Info.Usb_wLength > wLength)
+ {
+ pInformation->Ctrl_Info.Usb_wLength = wLength;
+ }
+
+ else if (pInformation->Ctrl_Info.Usb_wLength < pInformation->USBwLength)
+ {
+ if (pInformation->Ctrl_Info.Usb_wLength < pProperty->MaxPacketSize)
+ {
+ Data_Mul_MaxPacketSize = FALSE;
+ }
+ else if ((pInformation->Ctrl_Info.Usb_wLength % pProperty->MaxPacketSize) == 0)
+ {
+ Data_Mul_MaxPacketSize = TRUE;
+ }
+ }
+
+ pInformation->Ctrl_Info.PacketSize = pProperty->MaxPacketSize;
+ DataStageIn();
+ }
+ else
+ {
+ pInformation->ControlState = OUT_DATA;
+ vSetEPRxStatus(EP_RX_VALID); /* enable for next data reception */
+ }
+
+ return;
+}
+
+/*******************************************************************************
+* Function Name : Setup0_Process
+* Description : Get the device request data and dispatch to individual process.
+* Input : None.
+* Output : None.
+* Return : Post0_Process.
+*******************************************************************************/
+u8 Setup0_Process(void)
+{
+
+ union
+ {
+ u8* b;
+ u16* w;
+ } pBuf;
+
+ pBuf.b = PMAAddr + (u8 *)(_GetEPRxAddr(ENDP0) * 2); /* *2 for 32 bits addr */
+
+ if (pInformation->ControlState != PAUSE)
+ {
+ pInformation->USBbmRequestType = *pBuf.b++; /* bmRequestType */
+ pInformation->USBbRequest = *pBuf.b++; /* bRequest */
+ pBuf.w++; /* word not accessed because of 32 bits addressing */
+ pInformation->USBwValue = ByteSwap(*pBuf.w++); /* wValue */
+ pBuf.w++; /* word not accessed because of 32 bits addressing */
+ pInformation->USBwIndex = ByteSwap(*pBuf.w++); /* wIndex */
+ pBuf.w++; /* word not accessed because of 32 bits addressing */
+ pInformation->USBwLength = *pBuf.w; /* wLength */
+ }
+
+ pInformation->ControlState = SETTING_UP;
+ if (pInformation->USBwLength == 0)
+ {
+ /* Setup with no data stage */
+ NoData_Setup0();
+ }
+ else
+ {
+ /* Setup with data stage */
+ Data_Setup0();
+ }
+ return Post0_Process();
+}
+
+/*******************************************************************************
+* Function Name : In0_Process
+* Description : Process the IN token on all default endpoint.
+* Input : None.
+* Output : None.
+* Return : Post0_Process.
+*******************************************************************************/
+u8 In0_Process(void)
+{
+ u32 ControlState = pInformation->ControlState;
+
+ if ((ControlState == IN_DATA) || (ControlState == LAST_IN_DATA))
+ {
+ DataStageIn();
+ /* ControlState may be changed outside the function */
+ ControlState = pInformation->ControlState;
+ }
+
+ else if (ControlState == WAIT_STATUS_IN)
+ {
+ if ((pInformation->USBbRequest == SET_ADDRESS) &&
+ (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)))
+ {
+ SetDeviceAddress(pInformation->USBwValue0);
+ pUser_Standard_Requests->User_SetDeviceAddress();
+ }
+ (*pProperty->Process_Status_IN)();
+ ControlState = STALLED;
+ }
+
+ else
+ {
+ ControlState = STALLED;
+ }
+
+ pInformation->ControlState = ControlState;
+
+ return Post0_Process();
+}
+
+/*******************************************************************************
+* Function Name : Out0_Process
+* Description : Process the OUT token on all default endpoint.
+* Input : None.
+* Output : None.
+* Return : Post0_Process.
+*******************************************************************************/
+u8 Out0_Process(void)
+{
+ u32 ControlState = pInformation->ControlState;
+
+ if ((ControlState == OUT_DATA) || (ControlState == LAST_OUT_DATA))
+ {
+ DataStageOut();
+ ControlState = pInformation->ControlState; /* may be changed outside the function */
+ }
+
+ else if (ControlState == WAIT_STATUS_OUT)
+ {
+ (*pProperty->Process_Status_OUT)();
+ ControlState = STALLED;
+ }
+
+ else if ((ControlState == IN_DATA) || (ControlState == LAST_IN_DATA))
+ {
+ /* host aborts the transfer before finish */
+ ControlState = STALLED;
+ }
+
+ /* Unexpect state, STALL the endpoint */
+ else
+ {
+ ControlState = STALLED;
+ }
+
+ pInformation->ControlState = ControlState;
+
+ return Post0_Process();
+}
+
+/*******************************************************************************
+* Function Name : Post0_Process
+* Description : Stall the Endpoint 0 in case of error.
+* Input : None.
+* Output : None.
+* Return : - 0 if the control State is in PAUSE
+* - 1 if not.
+*******************************************************************************/
+u8 Post0_Process(void)
+{
+ SetEPRxCount(ENDP0, Device_Property.MaxPacketSize);
+
+ if (pInformation->ControlState == STALLED)
+ {
+ vSetEPRxStatus(EP_RX_STALL);
+ vSetEPTxStatus(EP_TX_STALL);
+ }
+
+ return (pInformation->ControlState == PAUSE);
+}
+
+/*******************************************************************************
+* Function Name : SetDeviceAddress.
+* Description : Set the device and all the used Endpoints addresses.
+* Input : - Val: device adress.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void SetDeviceAddress(u8 Val)
+{
+ u32 i;
+ u32 nEP = Device_Table.Total_Endpoint;
+
+ /* set address in every used endpoint */
+ for (i = 0; i < nEP; i++)
+ {
+ _SetEPAddress((u8)i, (u8)i);
+ } /* for */
+ _SetDADDR(Val | DADDR_EF); /* set device address and enable function */
+}
+
+/*******************************************************************************
+* Function Name : NOP_Process
+* Description : No operation function.
+* Input : None.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void NOP_Process(void)
+{
+}
+
+/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
diff --git a/libmaple/usb/usb_lib/usb_core.h b/libmaple/usb/usb_lib/usb_core.h
new file mode 100644
index 0000000..1931fbf
--- /dev/null
+++ b/libmaple/usb/usb_lib/usb_core.h
@@ -0,0 +1,252 @@
+/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
+* File Name : usb_core.h
+* Author : MCD Application Team
+* Version : V2.2.1
+* Date : 09/22/2008
+* Description : Standard protocol processing functions prototypes
+********************************************************************************
+* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
+* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
+* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
+* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
+* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
+* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
+*******************************************************************************/
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __USB_CORE_H
+#define __USB_CORE_H
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+/* Includes ------------------------------------------------------------------*/
+/* Exported types ------------------------------------------------------------*/
+typedef enum _CONTROL_STATE
+{
+ WAIT_SETUP, /* 0 */
+ SETTING_UP, /* 1 */
+ IN_DATA, /* 2 */
+ OUT_DATA, /* 3 */
+ LAST_IN_DATA, /* 4 */
+ LAST_OUT_DATA, /* 5 */
+ WAIT_STATUS_IN, /* 7 */
+ WAIT_STATUS_OUT, /* 8 */
+ STALLED, /* 9 */
+ PAUSE /* 10 */
+} CONTROL_STATE; /* The state machine states of a control pipe */
+
+typedef struct OneDescriptor
+{
+ u8 *Descriptor;
+ u16 Descriptor_Size;
+}
+ONE_DESCRIPTOR, *PONE_DESCRIPTOR;
+/* All the request process routines return a value of this type
+ If the return value is not SUCCESS or NOT_READY,
+ the software will STALL the correspond endpoint */
+typedef enum _RESULT
+{
+ USB_SUCCESS = 0, /* Process sucessfully */
+ USB_ERROR,
+ USB_UNSUPPORT,
+ USB_NOT_READY /* The process has not been finished, endpoint will be
+ NAK to further rquest */
+} RESULT;
+
+
+/*-*-*-*-*-*-*-*-*-*-* Definitions for endpoint level -*-*-*-*-*-*-*-*-*-*-*-*/
+typedef struct _ENDPOINT_INFO
+{
+ /* When send data out of the device,
+ CopyData() is used to get data buffer 'Length' bytes data
+ if Length is 0,
+ CopyData() returns the total length of the data
+ if the request is not supported, returns 0
+ (NEW Feature )
+ if CopyData() returns -1, the calling routine should not proceed
+ further and will resume the SETUP process by the class device
+ if Length is not 0,
+ CopyData() returns a pointer to indicate the data location
+ Usb_wLength is the data remain to be sent,
+ Usb_wOffset is the Offset of original data
+ When receive data from the host,
+ CopyData() is used to get user data buffer which is capable
+ of Length bytes data to copy data from the endpoint buffer.
+ if Length is 0,
+ CopyData() returns the available data length,
+ if Length is not 0,
+ CopyData() returns user buffer address
+ Usb_rLength is the data remain to be received,
+ Usb_rPointer is the Offset of data buffer
+ */
+ u16 Usb_wLength;
+ u16 Usb_wOffset;
+ u16 PacketSize;
+ u8 *(*CopyData)(u16 Length);
+}ENDPOINT_INFO;
+
+/*-*-*-*-*-*-*-*-*-*-*-* Definitions for device level -*-*-*-*-*-*-*-*-*-*-*-*/
+
+typedef struct _DEVICE
+{
+ u8 Total_Endpoint; /* Number of endpoints that are used */
+ u8 Total_Configuration;/* Number of configuration available */
+}
+DEVICE;
+
+typedef union
+{
+ u16 w;
+ struct BW
+ {
+ u8 bb1;
+ u8 bb0;
+ }
+ bw;
+} u16_u8;
+
+typedef struct _DEVICE_INFO
+{
+ u8 USBbmRequestType; /* bmRequestType */
+ u8 USBbRequest; /* bRequest */
+ u16_u8 USBwValues; /* wValue */
+ u16_u8 USBwIndexs; /* wIndex */
+ u16_u8 USBwLengths; /* wLength */
+
+ u8 ControlState; /* of type CONTROL_STATE */
+ u8 Current_Feature;
+ u8 Current_Configuration; /* Selected configuration */
+ u8 Current_Interface; /* Selected interface of current configuration */
+ u8 Current_AlternateSetting;/* Selected Alternate Setting of current
+ interface*/
+
+ ENDPOINT_INFO Ctrl_Info;
+}DEVICE_INFO;
+
+typedef struct _DEVICE_PROP
+{
+ void (*Init)(void); /* Initialize the device */
+ void (*Reset)(void); /* Reset routine of this device */
+
+ /* Device dependent process after the status stage */
+ void (*Process_Status_IN)(void);
+ void (*Process_Status_OUT)(void);
+
+ /* Procedure of process on setup stage of a class specified request with data stage */
+ /* All class specified requests with data stage are processed in Class_Data_Setup
+ Class_Data_Setup()
+ responses to check all special requests and fills ENDPOINT_INFO
+ according to the request
+ If IN tokens are expected, then wLength & wOffset will be filled
+ with the total transferring bytes and the starting position
+ If OUT tokens are expected, then rLength & rOffset will be filled
+ with the total expected bytes and the starting position in the buffer
+
+ If the request is valid, Class_Data_Setup returns SUCCESS, else UNSUPPORT
+
+ CAUTION:
+ Since GET_CONFIGURATION & GET_INTERFACE are highly related to
+ the individual classes, they will be checked and processed here.
+ */
+ RESULT (*Class_Data_Setup)(u8 RequestNo);
+
+ /* Procedure of process on setup stage of a class specified request without data stage */
+ /* All class specified requests without data stage are processed in Class_NoData_Setup
+ Class_NoData_Setup
+ responses to check all special requests and perform the request
+
+ CAUTION:
+ Since SET_CONFIGURATION & SET_INTERFACE are highly related to
+ the individual classes, they will be checked and processed here.
+ */
+ RESULT (*Class_NoData_Setup)(u8 RequestNo);
+
+ /*Class_Get_Interface_Setting
+ This function is used by the file usb_core.c to test if the selected Interface
+ and Alternate Setting (u8 Interface, u8 AlternateSetting) are supported by
+ the application.
+ This function is writing by user. It should return "SUCCESS" if the Interface
+ and Alternate Setting are supported by the application or "UNSUPPORT" if they
+ are not supported. */
+
+ RESULT (*Class_Get_Interface_Setting)(u8 Interface, u8 AlternateSetting);
+
+ u8* (*GetDeviceDescriptor)(u16 Length);
+ u8* (*GetConfigDescriptor)(u16 Length);
+ u8* (*GetStringDescriptor)(u16 Length);
+ u8* (*GetFunctionalDescriptor)(u16 Length);
+
+ u8* RxEP_buffer;
+ u8 MaxPacketSize;
+
+}DEVICE_PROP;
+
+typedef struct _USER_STANDARD_REQUESTS
+{
+ void (*User_GetConfiguration)(void); /* Get Configuration */
+ void (*User_SetConfiguration)(void); /* Set Configuration */
+ void (*User_GetInterface)(void); /* Get Interface */
+ void (*User_SetInterface)(void); /* Set Interface */
+ void (*User_GetStatus)(void); /* Get Status */
+ void (*User_ClearFeature)(void); /* Clear Feature */
+ void (*User_SetEndPointFeature)(void); /* Set Endpoint Feature */
+ void (*User_SetDeviceFeature)(void); /* Set Device Feature */
+ void (*User_SetDeviceAddress)(void); /* Set Device Address */
+}
+USER_STANDARD_REQUESTS;
+
+/* Exported constants --------------------------------------------------------*/
+#define Type_Recipient (pInformation->USBbmRequestType & (REQUEST_TYPE | RECIPIENT))
+
+#define Usb_rLength Usb_wLength
+#define Usb_rOffset Usb_wOffset
+
+#define USBwValue USBwValues.w
+#define USBwValue0 USBwValues.bw.bb0
+#define USBwValue1 USBwValues.bw.bb1
+#define USBwIndex USBwIndexs.w
+#define USBwIndex0 USBwIndexs.bw.bb0
+#define USBwIndex1 USBwIndexs.bw.bb1
+#define USBwLength USBwLengths.w
+#define USBwLength0 USBwLengths.bw.bb0
+#define USBwLength1 USBwLengths.bw.bb1
+
+/* Exported macro ------------------------------------------------------------*/
+/* Exported functions ------------------------------------------------------- */
+u8 Setup0_Process(void);
+u8 Post0_Process(void);
+u8 Out0_Process(void);
+u8 In0_Process(void);
+
+RESULT Standard_SetEndPointFeature(void);
+RESULT Standard_SetDeviceFeature(void);
+
+u8 *Standard_GetConfiguration(u16 Length);
+RESULT Standard_SetConfiguration(void);
+u8 *Standard_GetInterface(u16 Length);
+RESULT Standard_SetInterface(void);
+u8 *Standard_GetDescriptorData(u16 Length, PONE_DESCRIPTOR pDesc);
+
+u8 *Standard_GetStatus(u16 Length);
+RESULT Standard_ClearFeature(void);
+void SetDeviceAddress(u8);
+void NOP_Process(void);
+
+extern DEVICE_PROP Device_Property;
+extern USER_STANDARD_REQUESTS User_Standard_Requests;
+extern DEVICE Device_Table;
+extern DEVICE_INFO Device_Info;
+
+/* cells saving status during interrupt servicing */
+extern u16 SaveRState;
+extern u16 SaveTState;
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* __USB_CORE_H */
+
+/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
diff --git a/libmaple/usb/usb_lib/usb_def.h b/libmaple/usb/usb_lib/usb_def.h
new file mode 100644
index 0000000..aa6bcba
--- /dev/null
+++ b/libmaple/usb/usb_lib/usb_def.h
@@ -0,0 +1,88 @@
+/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
+* File Name : usb_def.h
+* Author : MCD Application Team
+* Version : V2.2.1
+* Date : 09/22/2008
+* Description : Definitions related to USB Core
+********************************************************************************
+* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
+* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
+* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
+* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
+* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
+* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
+*******************************************************************************/
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __USB_DEF_H
+#define __USB_DEF_H
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+/* Includes ------------------------------------------------------------------*/
+/* Exported types ------------------------------------------------------------*/
+typedef enum _RECIPIENT_TYPE
+{
+ DEVICE_RECIPIENT, /* Recipient device */
+ INTERFACE_RECIPIENT, /* Recipient interface */
+ ENDPOINT_RECIPIENT, /* Recipient endpoint */
+ OTHER_RECIPIENT
+} RECIPIENT_TYPE;
+
+
+typedef enum _STANDARD_REQUESTS
+{
+ GET_STATUS = 0,
+ CLEAR_FEATURE,
+ RESERVED1,
+ SET_FEATURE,
+ RESERVED2,
+ SET_ADDRESS,
+ GET_DESCRIPTOR,
+ SET_DESCRIPTOR,
+ GET_CONFIGURATION,
+ SET_CONFIGURATION,
+ GET_INTERFACE,
+ SET_INTERFACE,
+ TOTAL_sREQUEST, /* Total number of Standard request */
+ SYNCH_FRAME = 12
+} STANDARD_REQUESTS;
+
+/* Definition of "USBwValue" */
+typedef enum _DESCRIPTOR_TYPE
+{
+ DEVICE_DESCRIPTOR = 1,
+ CONFIG_DESCRIPTOR,
+ STRING_DESCRIPTOR,
+ INTERFACE_DESCRIPTOR,
+ ENDPOINT_DESCRIPTOR
+} DESCRIPTOR_TYPE;
+
+/* Feature selector of a SET_FEATURE or CLEAR_FEATURE */
+typedef enum _FEATURE_SELECTOR
+{
+ ENDPOINT_STALL,
+ DEVICE_REMOTE_WAKEUP
+} FEATURE_SELECTOR;
+
+/* Exported constants --------------------------------------------------------*/
+/* Definition of "USBbmRequestType" */
+#define REQUEST_TYPE 0x60 /* Mask to get request type */
+#define STANDARD_REQUEST 0x00 /* Standard request */
+#define CLASS_REQUEST 0x20 /* Class request */
+#define VENDOR_REQUEST 0x40 /* Vendor request */
+
+#define RECIPIENT 0x1F /* Mask to get recipient */
+
+/* Exported macro ------------------------------------------------------------*/
+/* Exported functions ------------------------------------------------------- */
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* __USB_DEF_H */
+
+/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
diff --git a/libmaple/usb/usb_lib/usb_init.c b/libmaple/usb/usb_lib/usb_init.c
new file mode 100644
index 0000000..5862145
--- /dev/null
+++ b/libmaple/usb/usb_lib/usb_init.c
@@ -0,0 +1,64 @@
+/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
+* File Name : usb_init.c
+* Author : MCD Application Team
+* Version : V2.2.1
+* Date : 09/22/2008
+* Description : Initialization routines & global variables
+********************************************************************************
+* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
+* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
+* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
+* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
+* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
+* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
+*******************************************************************************/
+
+/* Includes ------------------------------------------------------------------*/
+#include "usb_lib.h"
+
+/* Private typedef -----------------------------------------------------------*/
+/* Private define ------------------------------------------------------------*/
+/* Private macro -------------------------------------------------------------*/
+/* Private variables ---------------------------------------------------------*/
+/* The number of current endpoint, it will be used to specify an endpoint */
+ u8 EPindex;
+/* The number of current device, it is an index to the Device_Table */
+/* u8 Device_no; */
+/* Points to the DEVICE_INFO structure of current device */
+/* The purpose of this register is to speed up the execution */
+DEVICE_INFO *pInformation;
+/* Points to the DEVICE_PROP structure of current device */
+/* The purpose of this register is to speed up the execution */
+DEVICE_PROP *pProperty;
+/* Temporary save the state of Rx & Tx status. */
+/* Whenever the Rx or Tx state is changed, its value is saved */
+/* in this variable first and will be set to the EPRB or EPRA */
+/* at the end of interrupt process */
+u16 SaveState ;
+u16 wInterrupt_Mask;
+DEVICE_INFO Device_Info;
+USER_STANDARD_REQUESTS *pUser_Standard_Requests;
+
+/* Extern variables ----------------------------------------------------------*/
+/* Private function prototypes -----------------------------------------------*/
+/* Private functions ---------------------------------------------------------*/
+
+/*******************************************************************************
+* Function Name : USB_Init
+* Description : USB system initialization
+* Input : None.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void USB_Init(void)
+{
+ pInformation = &Device_Info;
+ pInformation->ControlState = 2;
+ pProperty = &Device_Property;
+ pUser_Standard_Requests = &User_Standard_Requests;
+ /* Initialize devices one by one */
+
+ pProperty->Init();
+}
+
+/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
diff --git a/libmaple/usb/usb_lib/usb_init.h b/libmaple/usb/usb_lib/usb_init.h
new file mode 100644
index 0000000..1091ce5
--- /dev/null
+++ b/libmaple/usb/usb_lib/usb_init.h
@@ -0,0 +1,57 @@
+/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
+* File Name : usb_init.h
+* Author : MCD Application Team
+* Version : V2.2.1
+* Date : 09/22/2008
+* Description : Initialization routines & global variables
+********************************************************************************
+* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
+* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
+* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
+* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
+* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
+* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
+*******************************************************************************/
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __USB_INIT_H
+#define __USB_INIT_H
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+/* Includes ------------------------------------------------------------------*/
+/* Exported types ------------------------------------------------------------*/
+/* Exported constants --------------------------------------------------------*/
+/* Exported macro ------------------------------------------------------------*/
+/* Exported functions ------------------------------------------------------- */
+void USB_Init(void);
+
+/* External variables --------------------------------------------------------*/
+/* The number of current endpoint, it will be used to specify an endpoint */
+extern u8 EPindex;
+/* The number of current device, it is an index to the Device_Table */
+/*extern u8 Device_no; */
+/* Points to the DEVICE_INFO structure of current device */
+/* The purpose of this register is to speed up the execution */
+extern DEVICE_INFO* pInformation;
+/* Points to the DEVICE_PROP structure of current device */
+/* The purpose of this register is to speed up the execution */
+extern DEVICE_PROP* pProperty;
+/* Temporary save the state of Rx & Tx status. */
+/* Whenever the Rx or Tx state is changed, its value is saved */
+/* in this variable first and will be set to the EPRB or EPRA */
+/* at the end of interrupt process */
+extern USER_STANDARD_REQUESTS *pUser_Standard_Requests;
+
+extern u16 SaveState ;
+extern u16 wInterrupt_Mask;
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* __USB_INIT_H */
+
+/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
diff --git a/libmaple/usb/usb_lib/usb_int.c b/libmaple/usb/usb_lib/usb_int.c
new file mode 100644
index 0000000..f1d82be
--- /dev/null
+++ b/libmaple/usb/usb_lib/usb_int.c
@@ -0,0 +1,192 @@
+/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
+* File Name : usb_int.c
+* Author : MCD Application Team
+* Version : V2.2.1
+* Date : 09/22/2008
+* Description : Endpoint CTR (Low and High) interrupt's service routines
+********************************************************************************
+* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
+* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
+* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
+* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
+* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
+* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
+*******************************************************************************/
+
+/* Includes ------------------------------------------------------------------*/
+#include "usb_lib.h"
+
+/* Private typedef -----------------------------------------------------------*/
+/* Private define ------------------------------------------------------------*/
+/* Private macro -------------------------------------------------------------*/
+/* Private variables ---------------------------------------------------------*/
+u16 SaveRState;
+u16 SaveTState;
+
+/* Extern variables ----------------------------------------------------------*/
+extern void (*pEpInt_IN[7])(void); /* Handles IN interrupts */
+extern void (*pEpInt_OUT[7])(void); /* Handles OUT interrupts */
+
+/* Private function prototypes -----------------------------------------------*/
+/* Private functions ---------------------------------------------------------*/
+
+/*******************************************************************************
+* Function Name : CTR_LP.
+* Description : Low priority Endpoint Correct Transfer interrupt's service
+* routine.
+* Input : None.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void CTR_LP(void)
+{
+ u32 wEPVal = 0;
+ /* stay in loop while pending ints */
+ while (((wIstr = _GetISTR()) & ISTR_CTR) != 0)
+ {
+ _SetISTR((u16)CLR_CTR); /* clear CTR flag */
+ /* extract highest priority endpoint number */
+ EPindex = (u8)(wIstr & ISTR_EP_ID);
+ if (EPindex == 0)
+ {
+ /* Decode and service control endpoint interrupt */
+ /* calling related service routine */
+ /* (Setup0_Process, In0_Process, Out0_Process) */
+
+ /* save RX & TX status */
+ /* and set both to NAK */
+ SaveRState = _GetEPRxStatus(ENDP0);
+ SaveTState = _GetEPTxStatus(ENDP0);
+ _SetEPRxStatus(ENDP0, EP_RX_NAK);
+ _SetEPTxStatus(ENDP0, EP_TX_NAK);
+
+
+ /* DIR bit = origin of the interrupt */
+
+ if ((wIstr & ISTR_DIR) == 0)
+ {
+ /* DIR = 0 */
+
+ /* DIR = 0 => IN int */
+ /* DIR = 0 implies that (EP_CTR_TX = 1) always */
+
+
+ _ClearEP_CTR_TX(ENDP0);
+ In0_Process();
+
+ /* before terminate set Tx & Rx status */
+ _SetEPRxStatus(ENDP0, SaveRState);
+ _SetEPTxStatus(ENDP0, SaveTState);
+ return;
+ }
+ else
+ {
+ /* DIR = 1 */
+
+ /* DIR = 1 & CTR_RX => SETUP or OUT int */
+ /* DIR = 1 & (CTR_TX | CTR_RX) => 2 int pending */
+
+ wEPVal = _GetENDPOINT(ENDP0);
+ if ((wEPVal & EP_CTR_TX) != 0)
+ {
+ _ClearEP_CTR_TX(ENDP0);
+ In0_Process();
+ /* before terminate set Tx & Rx status */
+ _SetEPRxStatus(ENDP0, SaveRState);
+ _SetEPTxStatus(ENDP0, SaveTState);
+ return;
+ }
+ else if ((wEPVal &EP_SETUP) != 0)
+ {
+ _ClearEP_CTR_RX(ENDP0); /* SETUP bit kept frozen while CTR_RX = 1 */
+ Setup0_Process();
+ /* before terminate set Tx & Rx status */
+ _SetEPRxStatus(ENDP0, SaveRState);
+ _SetEPTxStatus(ENDP0, SaveTState);
+ return;
+ }
+
+ else if ((wEPVal & EP_CTR_RX) != 0)
+ {
+ _ClearEP_CTR_RX(ENDP0);
+ Out0_Process();
+ /* before terminate set Tx & Rx status */
+ _SetEPRxStatus(ENDP0, SaveRState);
+ _SetEPTxStatus(ENDP0, SaveTState);
+ return;
+ }
+ }
+ }/* if(EPindex == 0) */
+ else
+ {
+ /* Decode and service non control endpoints interrupt */
+
+ /* process related endpoint register */
+ wEPVal = _GetENDPOINT(EPindex);
+ if ((wEPVal & EP_CTR_RX) != 0)
+ {
+ /* clear int flag */
+ _ClearEP_CTR_RX(EPindex);
+
+ /* call OUT service function */
+ (*pEpInt_OUT[EPindex-1])();
+
+ } /* if((wEPVal & EP_CTR_RX) */
+
+ if ((wEPVal & EP_CTR_TX) != 0)
+ {
+ /* clear int flag */
+ _ClearEP_CTR_TX(EPindex);
+
+ /* call IN service function */
+ (*pEpInt_IN[EPindex-1])();
+ } /* if((wEPVal & EP_CTR_TX) != 0) */
+
+ }/* if(EPindex == 0) else */
+
+ }/* while(...) */
+}
+
+/*******************************************************************************
+* Function Name : CTR_HP.
+* Description : High Priority Endpoint Correct Transfer interrupt's service
+* routine.
+* Input : None.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void CTR_HP(void)
+{
+ u32 wEPVal = 0;
+
+ while (((wIstr = _GetISTR()) & ISTR_CTR) != 0)
+ {
+ _SetISTR((u16)CLR_CTR); /* clear CTR flag */
+ /* extract highest priority endpoint number */
+ EPindex = (u8)(wIstr & ISTR_EP_ID);
+ /* process related endpoint register */
+ wEPVal = _GetENDPOINT(EPindex);
+ if ((wEPVal & EP_CTR_RX) != 0)
+ {
+ /* clear int flag */
+ _ClearEP_CTR_RX(EPindex);
+
+ /* call OUT service function */
+ (*pEpInt_OUT[EPindex-1])();
+
+ } /* if((wEPVal & EP_CTR_RX) */
+ else if ((wEPVal & EP_CTR_TX) != 0)
+ {
+ /* clear int flag */
+ _ClearEP_CTR_TX(EPindex);
+
+ /* call IN service function */
+ (*pEpInt_IN[EPindex-1])();
+
+
+ } /* if((wEPVal & EP_CTR_TX) != 0) */
+
+ }/* while(...) */
+}
+
+/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
diff --git a/libmaple/usb/usb_lib/usb_int.h b/libmaple/usb/usb_lib/usb_int.h
new file mode 100644
index 0000000..10a33fb
--- /dev/null
+++ b/libmaple/usb/usb_lib/usb_int.h
@@ -0,0 +1,41 @@
+/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
+* File Name : usb_int.h
+* Author : MCD Application Team
+* Version : V2.2.1
+* Date : 09/22/2008
+* Description : Endpoint CTR (Low and High) interrupt's service routines
+* prototypes
+********************************************************************************
+* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
+* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
+* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
+* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
+* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
+* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
+*******************************************************************************/
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __USB_INT_H
+#define __USB_INT_H
+
+/* Includes ------------------------------------------------------------------*/
+/* Exported types ------------------------------------------------------------*/
+/* Exported constants --------------------------------------------------------*/
+/* Exported macro ------------------------------------------------------------*/
+/* Exported functions ------------------------------------------------------- */
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+void CTR_LP(void);
+void CTR_HP(void);
+
+/* External variables --------------------------------------------------------*/
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* __USB_INT_H */
+
+/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
diff --git a/libmaple/usb/usb_lib/usb_lib.h b/libmaple/usb/usb_lib/usb_lib.h
new file mode 100644
index 0000000..5d14b5a
--- /dev/null
+++ b/libmaple/usb/usb_lib/usb_lib.h
@@ -0,0 +1,37 @@
+/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
+* File Name : usb_lib.h
+* Author : MCD Application Team
+* Version : V2.2.1
+* Date : 09/22/2008
+* Description : USB library include files
+********************************************************************************
+* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
+* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
+* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
+* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
+* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
+* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
+*******************************************************************************/
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __USB_LIB_H
+#define __USB_LIB_H
+
+/* Includes ------------------------------------------------------------------*/
+#include "usb_type.h"
+#include "usb_regs.h"
+#include "usb_def.h"
+#include "usb_core.h"
+#include "usb_init.h"
+#include "usb_mem.h"
+#include "usb_int.h"
+
+/* Exported types ------------------------------------------------------------*/
+/* Exported constants --------------------------------------------------------*/
+/* Exported macro ------------------------------------------------------------*/
+/* Exported functions ------------------------------------------------------- */
+/* External variables --------------------------------------------------------*/
+
+#endif /* __USB_LIB_H */
+
+/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
diff --git a/libmaple/usb/usb_lib/usb_mem.c b/libmaple/usb/usb_lib/usb_mem.c
new file mode 100644
index 0000000..ee698c5
--- /dev/null
+++ b/libmaple/usb/usb_lib/usb_mem.c
@@ -0,0 +1,73 @@
+/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
+* File Name : usb_mem.c
+* Author : MCD Application Team
+* Version : V2.2.1
+* Date : 09/22/2008
+* Description : Utility functions for memory transfers to/from PMA
+********************************************************************************
+* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
+* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
+* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
+* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
+* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
+* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
+*******************************************************************************/
+
+/* Includes ------------------------------------------------------------------*/
+#include "usb_lib.h"
+
+/* Private typedef -----------------------------------------------------------*/
+/* Private define ------------------------------------------------------------*/
+/* Private macro -------------------------------------------------------------*/
+/* Private variables ---------------------------------------------------------*/
+/* Extern variables ----------------------------------------------------------*/
+/* Private function prototypes -----------------------------------------------*/
+/* Private functions ---------------------------------------------------------*/
+/*******************************************************************************
+* Function Name : UserToPMABufferCopy
+* Description : Copy a buffer from user memory area to packet memory area (PMA)
+* Input : - pbUsrBuf: pointer to user memory area.
+* - wPMABufAddr: address into PMA.
+* - wNBytes: no. of bytes to be copied.
+* Output : None.
+* Return : None .
+*******************************************************************************/
+void UserToPMABufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes)
+{
+ u32 n = (wNBytes + 1) >> 1; /* n = (wNBytes + 1) / 2 */
+ u32 i, temp1, temp2;
+ u16 *pdwVal;
+ pdwVal = (u16 *)(wPMABufAddr * 2 + PMAAddr);
+ for (i = n; i != 0; i--)
+ {
+ temp1 = (u16) * pbUsrBuf;
+ pbUsrBuf++;
+ temp2 = temp1 | (u16) * pbUsrBuf << 8;
+ *pdwVal++ = temp2;
+ pdwVal++;
+ pbUsrBuf++;
+ }
+}
+/*******************************************************************************
+* Function Name : PMAToUserBufferCopy
+* Description : Copy a buffer from user memory area to packet memory area (PMA)
+* Input : - pbUsrBuf = pointer to user memory area.
+* - wPMABufAddr = address into PMA.
+* - wNBytes = no. of bytes to be copied.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void PMAToUserBufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes)
+{
+ u32 n = (wNBytes + 1) >> 1;/* /2*/
+ u32 i;
+ u32 *pdwVal;
+ pdwVal = (u32 *)(wPMABufAddr * 2 + PMAAddr);
+ for (i = n; i != 0; i--)
+ {
+ *(u16*)pbUsrBuf++ = *pdwVal++;
+ pbUsrBuf++;
+ }
+}
+
+/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
diff --git a/libmaple/usb/usb_lib/usb_mem.h b/libmaple/usb/usb_lib/usb_mem.h
new file mode 100644
index 0000000..a3d7927
--- /dev/null
+++ b/libmaple/usb/usb_lib/usb_mem.h
@@ -0,0 +1,40 @@
+/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
+* File Name : usb_mem.h
+* Author : MCD Application Team
+* Version : V2.2.1
+* Date : 09/22/2008
+* Description : Utility prototypes functions for memory/PMA transfers
+********************************************************************************
+* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
+* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
+* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
+* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
+* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
+* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
+*******************************************************************************/
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __USB_MEM_H
+#define __USB_MEM_H
+
+/* Includes ------------------------------------------------------------------*/
+/* Exported types ------------------------------------------------------------*/
+/* Exported constants --------------------------------------------------------*/
+/* Exported macro ------------------------------------------------------------*/
+/* Exported functions ------------------------------------------------------- */
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+void UserToPMABufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes);
+void PMAToUserBufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes);
+
+#if defined(__cplusplus)
+}
+#endif
+
+/* External variables --------------------------------------------------------*/
+
+#endif /*__USB_MEM_H*/
+
+/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
diff --git a/libmaple/usb/usb_lib/usb_regs.c b/libmaple/usb/usb_lib/usb_regs.c
new file mode 100644
index 0000000..c28ef5a
--- /dev/null
+++ b/libmaple/usb/usb_lib/usb_regs.c
@@ -0,0 +1,748 @@
+/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
+* File Name : usb_regs.c
+* Author : MCD Application Team
+* Version : V2.2.1
+* Date : 09/22/2008
+* Description : Interface functions to USB cell registers
+********************************************************************************
+* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
+* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
+* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
+* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
+* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
+* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
+*******************************************************************************/
+
+/* Includes ------------------------------------------------------------------*/
+#include "usb_lib.h"
+
+/* Private typedef -----------------------------------------------------------*/
+/* Private define ------------------------------------------------------------*/
+/* Private macro -------------------------------------------------------------*/
+/* Private variables ---------------------------------------------------------*/
+/* Extern variables ----------------------------------------------------------*/
+/* Private function prototypes -----------------------------------------------*/
+/* Private functions ---------------------------------------------------------*/
+
+/*******************************************************************************
+* Function Name : SetCNTR.
+* Description : Set the CNTR register value.
+* Input : wRegValue: new register value.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void SetCNTR(u16 wRegValue)
+{
+ _SetCNTR(wRegValue);
+}
+
+/*******************************************************************************
+* Function Name : GetCNTR.
+* Description : returns the CNTR register value.
+* Input : None.
+* Output : None.
+* Return : CNTR register Value.
+*******************************************************************************/
+u16 GetCNTR(void)
+{
+ return(_GetCNTR());
+}
+
+/*******************************************************************************
+* Function Name : SetISTR.
+* Description : Set the ISTR register value.
+* Input : wRegValue: new register value.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void SetISTR(u16 wRegValue)
+{
+ _SetISTR(wRegValue);
+}
+
+/*******************************************************************************
+* Function Name : GetISTR
+* Description : Returns the ISTR register value.
+* Input : None.
+* Output : None.
+* Return : ISTR register Value
+*******************************************************************************/
+u16 GetISTR(void)
+{
+ return(_GetISTR());
+}
+
+/*******************************************************************************
+* Function Name : GetFNR
+* Description : Returns the FNR register value.
+* Input : None.
+* Output : None.
+* Return : FNR register Value
+*******************************************************************************/
+u16 GetFNR(void)
+{
+ return(_GetFNR());
+}
+
+/*******************************************************************************
+* Function Name : SetDADDR
+* Description : Set the DADDR register value.
+* Input : wRegValue: new register value.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void SetDADDR(u16 wRegValue)
+{
+ _SetDADDR(wRegValue);
+}
+
+/*******************************************************************************
+* Function Name : GetDADDR
+* Description : Returns the DADDR register value.
+* Input : None.
+* Output : None.
+* Return : DADDR register Value
+*******************************************************************************/
+u16 GetDADDR(void)
+{
+ return(_GetDADDR());
+}
+
+/*******************************************************************************
+* Function Name : SetBTABLE
+* Description : Set the BTABLE.
+* Input : wRegValue: New register value.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void SetBTABLE(u16 wRegValue)
+{
+ _SetBTABLE(wRegValue);
+}
+
+/*******************************************************************************
+* Function Name : GetBTABLE.
+* Description : Returns the BTABLE register value.
+* Input : None.
+* Output : None.
+* Return : BTABLE address.
+*******************************************************************************/
+u16 GetBTABLE(void)
+{
+ return(_GetBTABLE());
+}
+
+/*******************************************************************************
+* Function Name : SetENDPOINT
+* Description : Setthe Endpoint register value.
+* Input : bEpNum: Endpoint Number.
+* wRegValue.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void SetENDPOINT(u8 bEpNum, u16 wRegValue)
+{
+ _SetENDPOINT(bEpNum, wRegValue);
+}
+
+/*******************************************************************************
+* Function Name : GetENDPOINT
+* Description : Return the Endpoint register value.
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : Endpoint register value.
+*******************************************************************************/
+u16 GetENDPOINT(u8 bEpNum)
+{
+ return(_GetENDPOINT(bEpNum));
+}
+
+/*******************************************************************************
+* Function Name : SetEPType
+* Description : sets the type in the endpoint register.
+* Input : bEpNum: Endpoint Number.
+* wType: type definition.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void SetEPType(u8 bEpNum, u16 wType)
+{
+ _SetEPType(bEpNum, wType);
+}
+
+/*******************************************************************************
+* Function Name : GetEPType
+* Description : Returns the endpoint type.
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : Endpoint Type
+*******************************************************************************/
+u16 GetEPType(u8 bEpNum)
+{
+ return(_GetEPType(bEpNum));
+}
+
+/*******************************************************************************
+* Function Name : SetEPTxStatus
+* Description : Set the status of Tx endpoint.
+* Input : bEpNum: Endpoint Number.
+* wState: new state.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void SetEPTxStatus(u8 bEpNum, u16 wState)
+{
+ _SetEPTxStatus(bEpNum, wState);
+}
+
+/*******************************************************************************
+* Function Name : SetEPRxStatus
+* Description : Set the status of Rx endpoint.
+* Input : bEpNum: Endpoint Number.
+* wState: new state.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void SetEPRxStatus(u8 bEpNum, u16 wState)
+{
+ _SetEPRxStatus(bEpNum, wState);
+}
+
+/*******************************************************************************
+* Function Name : SetDouBleBuffEPStall
+* Description : sets the status for Double Buffer Endpoint to STALL
+* Input : bEpNum: Endpoint Number.
+* bDir: Endpoint direction.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void SetDouBleBuffEPStall(u8 bEpNum, u8 bDir)
+{
+ u16 Endpoint_DTOG_Status;
+ Endpoint_DTOG_Status = GetENDPOINT(bEpNum);
+ if (bDir == EP_DBUF_OUT)
+ { /* OUT double buffered endpoint */
+ _SetENDPOINT(bEpNum, Endpoint_DTOG_Status & ~EPRX_DTOG1);
+ }
+ else if (bDir == EP_DBUF_IN)
+ { /* IN double buffered endpoint */
+ _SetENDPOINT(bEpNum, Endpoint_DTOG_Status & ~EPTX_DTOG1);
+ }
+}
+
+/*******************************************************************************
+* Function Name : GetEPTxStatus
+* Description : Returns the endpoint Tx status.
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : Endpoint TX Status
+*******************************************************************************/
+u16 GetEPTxStatus(u8 bEpNum)
+{
+ return(_GetEPTxStatus(bEpNum));
+}
+
+/*******************************************************************************
+* Function Name : GetEPRxStatus
+* Description : Returns the endpoint Rx status.
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : Endpoint RX Status
+*******************************************************************************/
+u16 GetEPRxStatus(u8 bEpNum)
+{
+ return(_GetEPRxStatus(bEpNum));
+}
+
+/*******************************************************************************
+* Function Name : SetEPTxValid
+* Description : Valid the endpoint Tx Status.
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void SetEPTxValid(u8 bEpNum)
+{
+ _SetEPTxStatus(bEpNum, EP_TX_VALID);
+}
+
+/*******************************************************************************
+* Function Name : SetEPRxValid
+* Description : Valid the endpoint Rx Status.
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void SetEPRxValid(u8 bEpNum)
+{
+ _SetEPRxStatus(bEpNum, EP_RX_VALID);
+}
+
+/*******************************************************************************
+* Function Name : SetEP_KIND
+* Description : Clear the EP_KIND bit.
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void SetEP_KIND(u8 bEpNum)
+{
+ _SetEP_KIND(bEpNum);
+}
+
+/*******************************************************************************
+* Function Name : ClearEP_KIND
+* Description : set the EP_KIND bit.
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void ClearEP_KIND(u8 bEpNum)
+{
+ _ClearEP_KIND(bEpNum);
+}
+/*******************************************************************************
+* Function Name : Clear_Status_Out
+* Description : Clear the Status Out of the related Endpoint
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void Clear_Status_Out(u8 bEpNum)
+{
+ _ClearEP_KIND(bEpNum);
+}
+/*******************************************************************************
+* Function Name : Set_Status_Out
+* Description : Set the Status Out of the related Endpoint
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void Set_Status_Out(u8 bEpNum)
+{
+ _SetEP_KIND(bEpNum);
+}
+/*******************************************************************************
+* Function Name : SetEPDoubleBuff
+* Description : Enable the double buffer feature for the endpoint.
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void SetEPDoubleBuff(u8 bEpNum)
+{
+ _SetEP_KIND(bEpNum);
+}
+/*******************************************************************************
+* Function Name : ClearEPDoubleBuff
+* Description : Disable the double buffer feature for the endpoint.
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void ClearEPDoubleBuff(u8 bEpNum)
+{
+ _ClearEP_KIND(bEpNum);
+}
+/*******************************************************************************
+* Function Name : GetTxStallStatus
+* Description : Returns the Stall status of the Tx endpoint.
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : Tx Stall status.
+*******************************************************************************/
+u16 GetTxStallStatus(u8 bEpNum)
+{
+ return(_GetTxStallStatus(bEpNum));
+}
+/*******************************************************************************
+* Function Name : GetRxStallStatus
+* Description : Returns the Stall status of the Rx endpoint.
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : Rx Stall status.
+*******************************************************************************/
+u16 GetRxStallStatus(u8 bEpNum)
+{
+ return(_GetRxStallStatus(bEpNum));
+}
+/*******************************************************************************
+* Function Name : ClearEP_CTR_RX
+* Description : Clear the CTR_RX bit.
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void ClearEP_CTR_RX(u8 bEpNum)
+{
+ _ClearEP_CTR_RX(bEpNum);
+}
+/*******************************************************************************
+* Function Name : ClearEP_CTR_TX
+* Description : Clear the CTR_TX bit.
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void ClearEP_CTR_TX(u8 bEpNum)
+{
+ _ClearEP_CTR_TX(bEpNum);
+}
+/*******************************************************************************
+* Function Name : ToggleDTOG_RX
+* Description : Toggle the DTOG_RX bit.
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void ToggleDTOG_RX(u8 bEpNum)
+{
+ _ToggleDTOG_RX(bEpNum);
+}
+/*******************************************************************************
+* Function Name : ToggleDTOG_TX
+* Description : Toggle the DTOG_TX bit.
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void ToggleDTOG_TX(u8 bEpNum)
+{
+ _ToggleDTOG_TX(bEpNum);
+}
+/*******************************************************************************
+* Function Name : ClearDTOG_RX.
+* Description : Clear the DTOG_RX bit.
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void ClearDTOG_RX(u8 bEpNum)
+{
+ _ClearDTOG_RX(bEpNum);
+}
+/*******************************************************************************
+* Function Name : ClearDTOG_TX.
+* Description : Clear the DTOG_TX bit.
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void ClearDTOG_TX(u8 bEpNum)
+{
+ _ClearDTOG_TX(bEpNum);
+}
+/*******************************************************************************
+* Function Name : SetEPAddress
+* Description : Set the endpoint address.
+* Input : bEpNum: Endpoint Number.
+* bAddr: New endpoint address.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void SetEPAddress(u8 bEpNum, u8 bAddr)
+{
+ _SetEPAddress(bEpNum, bAddr);
+}
+/*******************************************************************************
+* Function Name : GetEPAddress
+* Description : Get the endpoint address.
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : Endpoint address.
+*******************************************************************************/
+u8 GetEPAddress(u8 bEpNum)
+{
+ return(_GetEPAddress(bEpNum));
+}
+/*******************************************************************************
+* Function Name : SetEPTxAddr
+* Description : Set the endpoint Tx buffer address.
+* Input : bEpNum: Endpoint Number.
+* wAddr: new address.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void SetEPTxAddr(u8 bEpNum, u16 wAddr)
+{
+ _SetEPTxAddr(bEpNum, wAddr);
+}
+/*******************************************************************************
+* Function Name : SetEPRxAddr
+* Description : Set the endpoint Rx buffer address.
+* Input : bEpNum: Endpoint Number.
+* wAddr: new address.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void SetEPRxAddr(u8 bEpNum, u16 wAddr)
+{
+ _SetEPRxAddr(bEpNum, wAddr);
+}
+/*******************************************************************************
+* Function Name : GetEPTxAddr
+* Description : Returns the endpoint Tx buffer address.
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : Rx buffer address.
+*******************************************************************************/
+u16 GetEPTxAddr(u8 bEpNum)
+{
+ return(_GetEPTxAddr(bEpNum));
+}
+/*******************************************************************************
+* Function Name : GetEPRxAddr.
+* Description : Returns the endpoint Rx buffer address.
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : Rx buffer address.
+*******************************************************************************/
+u16 GetEPRxAddr(u8 bEpNum)
+{
+ return(_GetEPRxAddr(bEpNum));
+}
+/*******************************************************************************
+* Function Name : SetEPTxCount.
+* Description : Set the Tx count.
+* Input : bEpNum: Endpoint Number.
+* wCount: new count value.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void SetEPTxCount(u8 bEpNum, u16 wCount)
+{
+ _SetEPTxCount(bEpNum, wCount);
+}
+/*******************************************************************************
+* Function Name : SetEPCountRxReg.
+* Description : Set the Count Rx Register value.
+* Input : *pdwReg: point to the register.
+* wCount: the new register value.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void SetEPCountRxReg(u32 *pdwReg, u16 wCount)
+{
+ _SetEPCountRxReg(dwReg, wCount);
+}
+/*******************************************************************************
+* Function Name : SetEPRxCount
+* Description : Set the Rx count.
+* Input : bEpNum: Endpoint Number.
+* wCount: the new count value.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void SetEPRxCount(u8 bEpNum, u16 wCount)
+{
+ _SetEPRxCount(bEpNum, wCount);
+}
+/*******************************************************************************
+* Function Name : GetEPTxCount
+* Description : Get the Tx count.
+* Input : bEpNum: Endpoint Number.
+* Output : None
+* Return : Tx count value.
+*******************************************************************************/
+u16 GetEPTxCount(u8 bEpNum)
+{
+ return(_GetEPTxCount(bEpNum));
+}
+/*******************************************************************************
+* Function Name : GetEPRxCount
+* Description : Get the Rx count.
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : Rx count value.
+*******************************************************************************/
+u16 GetEPRxCount(u8 bEpNum)
+{
+ return(_GetEPRxCount(bEpNum));
+}
+/*******************************************************************************
+* Function Name : SetEPDblBuffAddr
+* Description : Set the addresses of the buffer 0 and 1.
+* Input : bEpNum: Endpoint Number.
+* wBuf0Addr: new address of buffer 0.
+* wBuf1Addr: new address of buffer 1.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void SetEPDblBuffAddr(u8 bEpNum, u16 wBuf0Addr, u16 wBuf1Addr)
+{
+ _SetEPDblBuffAddr(bEpNum, wBuf0Addr, wBuf1Addr);
+}
+/*******************************************************************************
+* Function Name : SetEPDblBuf0Addr
+* Description : Set the Buffer 1 address.
+* Input : bEpNum: Endpoint Number
+* wBuf0Addr: new address.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void SetEPDblBuf0Addr(u8 bEpNum, u16 wBuf0Addr)
+{
+ _SetEPDblBuf0Addr(bEpNum, wBuf0Addr);
+}
+/*******************************************************************************
+* Function Name : SetEPDblBuf1Addr
+* Description : Set the Buffer 1 address.
+* Input : bEpNum: Endpoint Number
+* wBuf1Addr: new address.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void SetEPDblBuf1Addr(u8 bEpNum, u16 wBuf1Addr)
+{
+ _SetEPDblBuf1Addr(bEpNum, wBuf1Addr);
+}
+/*******************************************************************************
+* Function Name : GetEPDblBuf0Addr
+* Description : Returns the address of the Buffer 0.
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : None.
+*******************************************************************************/
+u16 GetEPDblBuf0Addr(u8 bEpNum)
+{
+ return(_GetEPDblBuf0Addr(bEpNum));
+}
+/*******************************************************************************
+* Function Name : GetEPDblBuf1Addr
+* Description : Returns the address of the Buffer 1.
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : Address of the Buffer 1.
+*******************************************************************************/
+u16 GetEPDblBuf1Addr(u8 bEpNum)
+{
+ return(_GetEPDblBuf1Addr(bEpNum));
+}
+/*******************************************************************************
+* Function Name : SetEPDblBuffCount
+* Description : Set the number of bytes for a double Buffer
+* endpoint.
+* Input : bEpNum,bDir, wCount
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void SetEPDblBuffCount(u8 bEpNum, u8 bDir, u16 wCount)
+{
+ _SetEPDblBuffCount(bEpNum, bDir, wCount);
+}
+/*******************************************************************************
+* Function Name : SetEPDblBuf0Count
+* Description : Set the number of bytes in the buffer 0 of a double Buffer
+* endpoint.
+* Input : bEpNum, bDir, wCount
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void SetEPDblBuf0Count(u8 bEpNum, u8 bDir, u16 wCount)
+{
+ _SetEPDblBuf0Count(bEpNum, bDir, wCount);
+}
+/*******************************************************************************
+* Function Name : SetEPDblBuf1Count
+* Description : Set the number of bytes in the buffer 0 of a double Buffer
+* endpoint.
+* Input : bEpNum, bDir, wCount
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void SetEPDblBuf1Count(u8 bEpNum, u8 bDir, u16 wCount)
+{
+ _SetEPDblBuf1Count(bEpNum, bDir, wCount);
+}
+/*******************************************************************************
+* Function Name : GetEPDblBuf0Count
+* Description : Returns the number of byte received in the buffer 0 of a double
+* Buffer endpoint.
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : Endpoint Buffer 0 count
+*******************************************************************************/
+u16 GetEPDblBuf0Count(u8 bEpNum)
+{
+ return(_GetEPDblBuf0Count(bEpNum));
+}
+/*******************************************************************************
+* Function Name : GetEPDblBuf1Count
+* Description : Returns the number of data received in the buffer 1 of a double
+* Buffer endpoint.
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : Endpoint Buffer 1 count.
+*******************************************************************************/
+u16 GetEPDblBuf1Count(u8 bEpNum)
+{
+ return(_GetEPDblBuf1Count(bEpNum));
+}
+/*******************************************************************************
+* Function Name : GetEPDblBufDir
+* Description : gets direction of the double buffered endpoint
+* Input : bEpNum: Endpoint Number.
+* Output : None.
+* Return : EP_DBUF_OUT, EP_DBUF_IN,
+* EP_DBUF_ERR if the endpoint counter not yet programmed.
+*******************************************************************************/
+EP_DBUF_DIR GetEPDblBufDir(u8 bEpNum)
+{
+ if ((u16)(*_pEPRxCount(bEpNum) & 0xFC00) != 0)
+ return(EP_DBUF_OUT);
+ else if (((u16)(*_pEPTxCount(bEpNum)) & 0x03FF) != 0)
+ return(EP_DBUF_IN);
+ else
+ return(EP_DBUF_ERR);
+}
+/*******************************************************************************
+* Function Name : FreeUserBuffer
+* Description : free buffer used from the application realizing it to the line
+ toggles bit SW_BUF in the double buffered endpoint register
+* Input : bEpNum, bDir
+* Output : None.
+* Return : None.
+*******************************************************************************/
+void FreeUserBuffer(u8 bEpNum, u8 bDir)
+{
+ if (bDir == EP_DBUF_OUT)
+ { /* OUT double buffered endpoint */
+ _ToggleDTOG_TX(bEpNum);
+ }
+ else if (bDir == EP_DBUF_IN)
+ { /* IN double buffered endpoint */
+ _ToggleDTOG_RX(bEpNum);
+ }
+}
+
+/*******************************************************************************
+* Function Name : ToWord
+* Description : merge two byte in a word.
+* Input : bh: byte high, bl: bytes low.
+* Output : None.
+* Return : resulted word.
+*******************************************************************************/
+u16 ToWord(u8 bh, u8 bl)
+{
+ u16 wRet;
+ wRet = (u16)bl | ((u16)bh << 8);
+ return(wRet);
+}
+/*******************************************************************************
+* Function Name : ByteSwap
+* Description : Swap two byte in a word.
+* Input : wSwW: word to Swap.
+* Output : None.
+* Return : resulted word.
+*******************************************************************************/
+u16 ByteSwap(u16 wSwW)
+{
+ u8 bTemp;
+ u16 wRet;
+ bTemp = (u8)(wSwW & 0xff);
+ wRet = (wSwW >> 8) | ((u16)bTemp << 8);
+ return(wRet);
+}
+
+/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
diff --git a/libmaple/usb_regs.h b/libmaple/usb/usb_lib/usb_regs.h
index bf5e10c..9a4df75 100644
--- a/libmaple/usb_regs.h
+++ b/libmaple/usb/usb_lib/usb_regs.h
@@ -19,6 +19,9 @@
/* Includes ------------------------------------------------------------------*/
/* Exported types ------------------------------------------------------------*/
+#if defined(__cplusplus)
+extern "C" {
+#endif
typedef enum _EP_DBUF_DIR
{
@@ -60,14 +63,14 @@ enum EP_BUF_NUM
#define EP0REG ((volatile unsigned *)(RegBase)) /* endpoint 0 register address */
/* endpoints enumeration */
-#define ENDP0 ((uint8)0)
-#define ENDP1 ((uint8)1)
-#define ENDP2 ((uint8)2)
-#define ENDP3 ((uint8)3)
-#define ENDP4 ((uint8)4)
-#define ENDP5 ((uint8)5)
-#define ENDP6 ((uint8)6)
-#define ENDP7 ((uint8)7)
+#define ENDP0 ((u8)0)
+#define ENDP1 ((u8)1)
+#define ENDP2 ((u8)2)
+#define ENDP3 ((u8)3)
+#define ENDP4 ((u8)4)
+#define ENDP5 ((u8)5)
+#define ENDP6 ((u8)6)
+#define ENDP7 ((u8)7)
/******************************************************************************/
/* ISTR interrupt events */
/******************************************************************************/
@@ -174,38 +177,38 @@ enum EP_BUF_NUM
#define EPRX_DTOGMASK (EPRX_STAT|EPREG_MASK)
/* Exported macro ------------------------------------------------------------*/
/* SetCNTR */
-#define _SetCNTR(wRegValue) (*CNTR = (uint16)wRegValue)
+#define _SetCNTR(wRegValue) (*CNTR = (u16)wRegValue)
/* SetISTR */
-#define _SetISTR(wRegValue) (*ISTR = (uint16)wRegValue)
+#define _SetISTR(wRegValue) (*ISTR = (u16)wRegValue)
/* SetDADDR */
-#define _SetDADDR(wRegValue) (*DADDR = (uint16)wRegValue)
+#define _SetDADDR(wRegValue) (*DADDR = (u16)wRegValue)
/* SetBTABLE */
-#define _SetBTABLE(wRegValue)(*BTABLE = (uint16)(wRegValue & 0xFFF8))
+#define _SetBTABLE(wRegValue)(*BTABLE = (u16)(wRegValue & 0xFFF8))
/* GetCNTR */
-#define _GetCNTR() ((uint16) *CNTR)
+#define _GetCNTR() ((u16) *CNTR)
/* GetISTR */
-#define _GetISTR() ((uint16) *ISTR)
+#define _GetISTR() ((u16) *ISTR)
/* GetFNR */
-#define _GetFNR() ((uint16) *FNR)
+#define _GetFNR() ((u16) *FNR)
/* GetDADDR */
-#define _GetDADDR() ((uint16) *DADDR)
+#define _GetDADDR() ((u16) *DADDR)
/* GetBTABLE */
-#define _GetBTABLE() ((uint16) *BTABLE)
+#define _GetBTABLE() ((u16) *BTABLE)
/* SetENDPOINT */
#define _SetENDPOINT(bEpNum,wRegValue) (*(EP0REG + bEpNum)= \
- (uint16)wRegValue)
+ (u16)wRegValue)
/* GetENDPOINT */
-#define _GetENDPOINT(bEpNum) ((uint16)(*(EP0REG + bEpNum)))
+#define _GetENDPOINT(bEpNum) ((u16)(*(EP0REG + bEpNum)))
/*******************************************************************************
* Macro Name : SetEPType
@@ -236,7 +239,7 @@ enum EP_BUF_NUM
* Return : None.
*******************************************************************************/
#define _SetEPTxStatus(bEpNum,wState) {\
- register uint16 _wRegVal; \
+ register u16 _wRegVal; \
_wRegVal = _GetENDPOINT(bEpNum) & EPTX_DTOGMASK;\
/* toggle first bit ? */ \
if((EPTX_DTOG1 & wState)!= 0) \
@@ -256,7 +259,7 @@ enum EP_BUF_NUM
* Return : None.
*******************************************************************************/
#define _SetEPRxStatus(bEpNum,wState) {\
- register uint16 _wRegVal; \
+ register u16 _wRegVal; \
\
_wRegVal = _GetENDPOINT(bEpNum) & EPRX_DTOGMASK;\
/* toggle first bit ? */ \
@@ -275,9 +278,9 @@ enum EP_BUF_NUM
* Output : None.
* Return : status .
*******************************************************************************/
-#define _GetEPTxStatus(bEpNum) ((uint16)_GetENDPOINT(bEpNum) & EPTX_STAT)
+#define _GetEPTxStatus(bEpNum) ((u16)_GetENDPOINT(bEpNum) & EPTX_STAT)
-#define _GetEPRxStatus(bEpNum) ((uint16)_GetENDPOINT(bEpNum) & EPRX_STAT)
+#define _GetEPRxStatus(bEpNum) ((u16)_GetENDPOINT(bEpNum) & EPRX_STAT)
/*******************************************************************************
* Macro Name : SetEPTxValid / SetEPRxValid
@@ -387,12 +390,12 @@ enum EP_BUF_NUM
* Output : None.
* Return : None.
*******************************************************************************/
-#define _GetEPAddress(bEpNum) ((uint8)(_GetENDPOINT(bEpNum) & EPADDR_FIELD))
+#define _GetEPAddress(bEpNum) ((u8)(_GetENDPOINT(bEpNum) & EPADDR_FIELD))
-#define _pEPTxAddr(bEpNum) ((uint32 *)((_GetBTABLE()+bEpNum*8 )*2 + PMAAddr))
-#define _pEPTxCount(bEpNum) ((uint32 *)((_GetBTABLE()+bEpNum*8+2)*2 + PMAAddr))
-#define _pEPRxAddr(bEpNum) ((uint32 *)((_GetBTABLE()+bEpNum*8+4)*2 + PMAAddr))
-#define _pEPRxCount(bEpNum) ((uint32 *)((_GetBTABLE()+bEpNum*8+6)*2 + PMAAddr))
+#define _pEPTxAddr(bEpNum) ((u32 *)((_GetBTABLE()+bEpNum*8 )*2 + PMAAddr))
+#define _pEPTxCount(bEpNum) ((u32 *)((_GetBTABLE()+bEpNum*8+2)*2 + PMAAddr))
+#define _pEPRxAddr(bEpNum) ((u32 *)((_GetBTABLE()+bEpNum*8+4)*2 + PMAAddr))
+#define _pEPRxCount(bEpNum) ((u32 *)((_GetBTABLE()+bEpNum*8+6)*2 + PMAAddr))
/*******************************************************************************
* Macro Name : SetEPTxAddr / SetEPRxAddr.
@@ -412,8 +415,8 @@ enum EP_BUF_NUM
* Output : None.
* Return : address of the buffer.
*******************************************************************************/
-#define _GetEPTxAddr(bEpNum) ((uint16)*_pEPTxAddr(bEpNum))
-#define _GetEPRxAddr(bEpNum) ((uint16)*_pEPRxAddr(bEpNum))
+#define _GetEPTxAddr(bEpNum) ((u16)*_pEPTxAddr(bEpNum))
+#define _GetEPRxAddr(bEpNum) ((u16)*_pEPRxAddr(bEpNum))
/*******************************************************************************
* Macro Name : SetEPCountRxReg.
@@ -427,18 +430,18 @@ enum EP_BUF_NUM
wNBlocks = wCount >> 5;\
if((wCount & 0x1f) == 0)\
wNBlocks--;\
- *pdwReg = (uint32)((wNBlocks << 10) | 0x8000);\
+ *pdwReg = (u32)((wNBlocks << 10) | 0x8000);\
}/* _BlocksOf32 */
#define _BlocksOf2(dwReg,wCount,wNBlocks) {\
wNBlocks = wCount >> 1;\
if((wCount & 0x1) != 0)\
wNBlocks++;\
- *pdwReg = (uint32)(wNBlocks << 10);\
+ *pdwReg = (u32)(wNBlocks << 10);\
}/* _BlocksOf2 */
#define _SetEPCountRxReg(dwReg,wCount) {\
- uint16 wNBlocks;\
+ u16 wNBlocks;\
if(wCount > 62){_BlocksOf32(dwReg,wCount,wNBlocks);}\
else {_BlocksOf2(dwReg,wCount,wNBlocks);}\
}/* _SetEPCountRxReg */
@@ -446,7 +449,7 @@ enum EP_BUF_NUM
#define _SetEPRxDblBuf0Count(bEpNum,wCount) {\
- uint32 *pdwReg = _pEPTxCount(bEpNum); \
+ u32 *pdwReg = _pEPTxCount(bEpNum); \
_SetEPCountRxReg(pdwReg, wCount);\
}
/*******************************************************************************
@@ -459,7 +462,7 @@ enum EP_BUF_NUM
*******************************************************************************/
#define _SetEPTxCount(bEpNum,wCount) (*_pEPTxCount(bEpNum) = wCount)
#define _SetEPRxCount(bEpNum,wCount) {\
- uint32 *pdwReg = _pEPRxCount(bEpNum); \
+ u32 *pdwReg = _pEPRxCount(bEpNum); \
_SetEPCountRxReg(pdwReg, wCount);\
}
/*******************************************************************************
@@ -469,8 +472,8 @@ enum EP_BUF_NUM
* Output : None.
* Return : Counter value.
*******************************************************************************/
-#define _GetEPTxCount(bEpNum)((uint16)(*_pEPTxCount(bEpNum)) & 0x3ff)
-#define _GetEPRxCount(bEpNum)((uint16)(*_pEPRxCount(bEpNum)) & 0x3ff)
+#define _GetEPTxCount(bEpNum)((u16)(*_pEPTxCount(bEpNum)) & 0x3ff)
+#define _GetEPRxCount(bEpNum)((u16)(*_pEPRxCount(bEpNum)) & 0x3ff)
/*******************************************************************************
* Macro Name : SetEPDblBuf0Addr / SetEPDblBuf1Addr.
@@ -523,7 +526,7 @@ enum EP_BUF_NUM
{_SetEPRxDblBuf0Count(bEpNum,wCount);} \
else if(bDir == EP_DBUF_IN)\
/* IN endpoint */ \
- *_pEPTxCount(bEpNum) = (uint32)wCount; \
+ *_pEPTxCount(bEpNum) = (u32)wCount; \
} /* SetEPDblBuf0Count*/
#define _SetEPDblBuf1Count(bEpNum, bDir, wCount) { \
@@ -532,7 +535,7 @@ enum EP_BUF_NUM
{_SetEPRxCount(bEpNum,wCount);}\
else if(bDir == EP_DBUF_IN)\
/* IN endpoint */\
- *_pEPRxCount(bEpNum) = (uint32)wCount; \
+ *_pEPRxCount(bEpNum) = (u32)wCount; \
} /* SetEPDblBuf1Count */
#define _SetEPDblBuffCount(bEpNum, bDir, wCount) {\
@@ -550,5 +553,75 @@ enum EP_BUF_NUM
#define _GetEPDblBuf0Count(bEpNum) (_GetEPTxCount(bEpNum))
#define _GetEPDblBuf1Count(bEpNum) (_GetEPRxCount(bEpNum))
+
+/* External variables --------------------------------------------------------*/
+extern volatile u16 wIstr; /* ISTR register last read value */
+
+/* Exported functions ------------------------------------------------------- */
+void SetCNTR(u16 /*wRegValue*/);
+void SetISTR(u16 /*wRegValue*/);
+void SetDADDR(u16 /*wRegValue*/);
+void SetBTABLE(u16 /*wRegValue*/);
+u16 GetCNTR(void);
+u16 GetISTR(void);
+u16 GetFNR(void);
+u16 GetDADDR(void);
+u16 GetBTABLE(void);
+void SetENDPOINT(u8 /*bEpNum*/, u16 /*wRegValue*/);
+u16 GetENDPOINT(u8 /*bEpNum*/);
+void SetEPType(u8 /*bEpNum*/, u16 /*wType*/);
+u16 GetEPType(u8 /*bEpNum*/);
+void SetEPTxStatus(u8 /*bEpNum*/, u16 /*wState*/);
+void SetEPRxStatus(u8 /*bEpNum*/, u16 /*wState*/);
+void SetDouBleBuffEPStall(u8 /*bEpNum*/, u8 bDir);
+u16 GetEPTxStatus(u8 /*bEpNum*/);
+u16 GetEPRxStatus(u8 /*bEpNum*/);
+void SetEPTxValid(u8 /*bEpNum*/);
+void SetEPRxValid(u8 /*bEpNum*/);
+u16 GetTxStallStatus(u8 /*bEpNum*/);
+u16 GetRxStallStatus(u8 /*bEpNum*/);
+void SetEP_KIND(u8 /*bEpNum*/);
+void ClearEP_KIND(u8 /*bEpNum*/);
+void Set_Status_Out(u8 /*bEpNum*/);
+void Clear_Status_Out(u8 /*bEpNum*/);
+void SetEPDoubleBuff(u8 /*bEpNum*/);
+void ClearEPDoubleBuff(u8 /*bEpNum*/);
+void ClearEP_CTR_RX(u8 /*bEpNum*/);
+void ClearEP_CTR_TX(u8 /*bEpNum*/);
+void ToggleDTOG_RX(u8 /*bEpNum*/);
+void ToggleDTOG_TX(u8 /*bEpNum*/);
+void ClearDTOG_RX(u8 /*bEpNum*/);
+void ClearDTOG_TX(u8 /*bEpNum*/);
+void SetEPAddress(u8 /*bEpNum*/, u8 /*bAddr*/);
+u8 GetEPAddress(u8 /*bEpNum*/);
+void SetEPTxAddr(u8 /*bEpNum*/, u16 /*wAddr*/);
+void SetEPRxAddr(u8 /*bEpNum*/, u16 /*wAddr*/);
+u16 GetEPTxAddr(u8 /*bEpNum*/);
+u16 GetEPRxAddr(u8 /*bEpNum*/);
+void SetEPCountRxReg(u32 * /*pdwReg*/, u16 /*wCount*/);
+void SetEPTxCount(u8 /*bEpNum*/, u16 /*wCount*/);
+void SetEPRxCount(u8 /*bEpNum*/, u16 /*wCount*/);
+u16 GetEPTxCount(u8 /*bEpNum*/);
+u16 GetEPRxCount(u8 /*bEpNum*/);
+void SetEPDblBuf0Addr(u8 /*bEpNum*/, u16 /*wBuf0Addr*/);
+void SetEPDblBuf1Addr(u8 /*bEpNum*/, u16 /*wBuf1Addr*/);
+void SetEPDblBuffAddr(u8 /*bEpNum*/, u16 /*wBuf0Addr*/, u16 /*wBuf1Addr*/);
+u16 GetEPDblBuf0Addr(u8 /*bEpNum*/);
+u16 GetEPDblBuf1Addr(u8 /*bEpNum*/);
+void SetEPDblBuffCount(u8 /*bEpNum*/, u8 /*bDir*/, u16 /*wCount*/);
+void SetEPDblBuf0Count(u8 /*bEpNum*/, u8 /*bDir*/, u16 /*wCount*/);
+void SetEPDblBuf1Count(u8 /*bEpNum*/, u8 /*bDir*/, u16 /*wCount*/);
+u16 GetEPDblBuf0Count(u8 /*bEpNum*/);
+u16 GetEPDblBuf1Count(u8 /*bEpNum*/);
+EP_DBUF_DIR GetEPDblBufDir(u8 /*bEpNum*/);
+void FreeUserBuffer(u8 bEpNum/*bEpNum*/, u8 bDir);
+u16 ToWord(u8, u8);
+u16 ByteSwap(u16);
+
+#if defined(__cplusplus)
+}
#endif
+
+#endif /* __USB_REGS_H */
+
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
diff --git a/libmaple/usb/usb_lib/usb_type.h b/libmaple/usb/usb_lib/usb_type.h
new file mode 100644
index 0000000..1106786
--- /dev/null
+++ b/libmaple/usb/usb_lib/usb_type.h
@@ -0,0 +1,77 @@
+/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
+* File Name : usb_type.h
+* Author : MCD Application Team
+* Version : V2.2.1
+* Date : 09/22/2008
+* Description : Type definitions used by the USB Library
+********************************************************************************
+* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
+* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
+* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
+* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
+* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
+* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
+*******************************************************************************/
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __USB_TYPE_H
+#define __USB_TYPE_H
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+/* Exported types ------------------------------------------------------------*/
+/* Exported constants --------------------------------------------------------*/
+#ifndef NULL
+#define NULL ((void *)0)
+#endif
+
+typedef signed long s32;
+typedef signed short s16;
+typedef signed char s8;
+
+typedef volatile signed long vs32;
+typedef volatile signed short vs16;
+typedef volatile signed char vs8;
+
+typedef unsigned long u32;
+typedef unsigned short u16;
+typedef unsigned char u8;
+
+typedef unsigned long const uc32; /* Read Only */
+typedef unsigned short const uc16; /* Read Only */
+typedef unsigned char const uc8; /* Read Only */
+
+typedef volatile unsigned long vu32;
+typedef volatile unsigned short vu16;
+typedef volatile unsigned char vu8;
+
+typedef volatile unsigned long const vuc32; /* Read Only */
+typedef volatile unsigned short const vuc16; /* Read Only */
+typedef volatile unsigned char const vuc8; /* Read Only */
+
+
+typedef enum
+{
+ FALSE = 0, TRUE = !FALSE
+}
+USB_Bool;
+
+typedef enum { RESET = 0, SET = !RESET } FlagStatus, ITStatus;
+
+typedef enum { DISABLE = 0, ENABLE = !DISABLE} FunctionalState;
+
+typedef enum { ERROR = 0, SUCCESS = !ERROR} ErrorStatus;
+
+/* Exported macro ------------------------------------------------------------*/
+/* Exported functions ------------------------------------------------------- */
+/* External variables --------------------------------------------------------*/
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* __USB_TYPE_H */
+
+/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
diff --git a/main.cpp.example b/main.cpp.example
index ae63996..67380ae 100644
--- a/main.cpp.example
+++ b/main.cpp.example
@@ -29,14 +29,11 @@
#include "wirish.h"
#include "HardwareSerial.h"
-#include "HardwareUsb.h"
#include "usb.h"
#define LED_PIN 13
#define PWM_PIN 2
-HardwareUsb Usb;
-
void setup()
{
/* Set up the LED to blink */
@@ -50,8 +47,6 @@ void setup()
pinMode(PWM_PIN, PWM);
pwmWrite(PWM_PIN, 0x8000);
- /* Send a message out the USB virtual com port */
- Usb.println("Hello world!");
}
int toggle = 0;
diff --git a/notes/45-maple.rules b/notes/45-maple.rules
index e1d3638..c2e6c56 100644
--- a/notes/45-maple.rules
+++ b/notes/45-maple.rules
@@ -2,5 +2,7 @@ BUS!="usb", ACTION!="add", SUBSYSTEM!=="usb_device", GOTO="maple_rules_end"
SYSFS{idProduct}=="1001", SYSFS{idVendor}=="0110", MODE="664", GROUP="plugdev"
SYSFS{idProduct}=="1002", SYSFS{idVendor}=="0110", MODE="664", GROUP="plugdev"
+SYSFS{idProduct}=="0003", SYSFS{idVendor}=="1EAF", MODE="664", GROUP="plugdev"
+SYSFS{idProduct}=="0004", SYSFS{idVendor}=="1EAF", MODE="664", GROUP="plugdev"
LABEL="maple_rules_end"
diff --git a/reset.py b/reset.py
new file mode 100755
index 0000000..ae5d74b
--- /dev/null
+++ b/reset.py
@@ -0,0 +1,29 @@
+#!/usr/bin/python
+
+import serial
+import os
+
+try:
+ highest = 0
+ # this totally won't work on Mac OSX! /dev/ttySOMETHINGELSE?
+ for f in filter(lambda x: x.startswith('ttyACM'), os.listdir('/dev/')):
+ if (int(f[6:]) > highest):
+ highest = int(f[6:])
+
+ print "Trying /dev/ttyACM%d..." % highest
+ ser = serial.Serial('/dev/ttyACM%d' % highest, baudrate=115200)
+ ser.open()
+
+ # pull dtr and rts low
+ ser.setRTS(0)
+ ser.setDTR(0)
+
+ # toggle DTR
+ ser.setDTR(1)
+
+ # close
+ ser.close()
+
+except:
+ print "Failed to open the serial port for reset, perhaps this is your first upload or the board was already in bootloader mode"
+
diff --git a/wirish/WProgram.h b/wirish/WProgram.h
index 5445518..36a9e6d 100644
--- a/wirish/WProgram.h
+++ b/wirish/WProgram.h
@@ -24,8 +24,7 @@
#include "wirish.h"
#include "HardwareSerial.h"
-#include "HardwareUsb.h"
-#include "usb.h"
+#include "math.h"
void setup();
void loop();
diff --git a/wirish/comm/HardwareUsb.cpp b/wirish/comm/HardwareUsb.cpp
deleted file mode 100644
index 4398e96..0000000
--- a/wirish/comm/HardwareUsb.cpp
+++ /dev/null
@@ -1,102 +0,0 @@
-/* *****************************************************************************
- * The MIT License
- *
- * Copyright (c) 2010 Andrew Meyer.
- *
- * 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.
- * ****************************************************************************/
-
-/**
- * @brief Wiring like serial api to USB virtual COM
- */
-
-#include "wirish.h"
-#include "HardwareUsb.h"
-#include "bootVect.h"
-#include "usb.h"
-
-HardwareUsb::HardwareUsb(void) {
- mapleVectTable = (BootVectTable*)(BOOTLOADER_VECT_TABLE);
- mapleVectTable->serial_tx_cb = usb_tx_cb;
- mapleVectTable->serial_rx_cb = usb_rx_cb;
- mapleVectTable->usb_local_obj_ptr = this;
- rx_buffer_offset_in = 0;
- rx_buffer_offset_out = 0;
-}
-
-uint8 HardwareUsb::read(void) {
- uint8 outVal = rx_buffer[rx_buffer_offset_out++];
-
-#if 1
- if (rx_buffer_offset_out == rx_buffer_offset_in) {
- flush();
- }
-#endif
-
- return outVal;
-}
-
-uint8 HardwareUsb::available(void) {
- ASSERT(rx_buffer_offset_out >= 0);
- // return rx_buffer_offset+1;
- // return usb_serialGetRecvLen();
- return rx_buffer_offset_in - rx_buffer_offset_out;
-}
-
-void HardwareUsb::flush(void) {
- rx_buffer_offset_in = 0;
- rx_buffer_offset_out = 0;
-}
-
-void HardwareUsb::write(unsigned char ch) {
- usb_serialWriteChar(ch);
-}
-
-void HardwareUsb::begin(void) {
- /* placeholder for usb<->uart linking */
-}
-
-void HardwareUsb::usb_rx_cb(void) {
- BootVectTable *vectTable = (BootVectTable*)(BOOTLOADER_VECT_TABLE);
- HardwareUsb *thisPtr = (HardwareUsb*) vectTable->usb_local_obj_ptr;
-
- uint8 numBytes = usb_serialGetRecvLen();
-
-#if 0
- /* ONE-SHOT-TO-READ Version (buffer cleared on next recv interrupt */
- usb_copyRecvBuffer(thisPtr->rx_buffer,numBytes);
- thisPtr->rx_buffer_offset_in = numBytes;
- thisPtr->rx_buffer_offset_out = 0;
-#else
- /* FIFO DISCARD OVERFLOW VERSION */
- if ((thisPtr->rx_buffer_offset_in + numBytes) > (USB_SERIAL_BUF_SIZE)) {
- numBytes = USB_SERIAL_BUF_SIZE - thisPtr->rx_buffer_offset_in;
- }
-
- if (numBytes > 0) {
- ASSERT(thisPtr->rx_buffer_offset_in <= USB_SERIAL_BUF_SIZE);
- usb_copyRecvBuffer(&(thisPtr->rx_buffer[thisPtr->rx_buffer_offset_in]),numBytes);
- thisPtr->rx_buffer_offset_in += numBytes;
- }
-#endif
-}
-
-void HardwareUsb::usb_tx_cb(void) {
- /* placeholder for when serial dumps exceed buflen */
-}
diff --git a/wirish/comm/HardwareUsb.h b/wirish/comm/HardwareUsb.h
deleted file mode 100644
index ee13f40..0000000
--- a/wirish/comm/HardwareUsb.h
+++ /dev/null
@@ -1,54 +0,0 @@
-/* *****************************************************************************
- * The MIT License
- *
- * Copyright (c) 2010 Andrew Meyer.
- *
- * 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.
- * ****************************************************************************/
-
-/**
- * @brief Wiring like serial api to USB virtual COM
- */
-
-#ifndef _HARDWAREUSB_H_
-#define _HARDWAREUSB_H_
-
-#include "Print.h"
-#include "bootVect.h"
-#include "usb.h"
-
-class HardwareUsb : public Print {
- private:
- BootVectTable* mapleVectTable;
- static void usb_rx_cb(void);
- static void usb_tx_cb(void);
- unsigned char rx_buffer[USB_SERIAL_BUF_SIZE];
- int8 rx_buffer_offset_out;
- int8 rx_buffer_offset_in;
- public:
- HardwareUsb(void);
- void begin();
- uint8 available(void);
- uint8 read(void);
- void flush(void);
- virtual void write(unsigned char);
- using Print::write;
-};
-
-#endif //_HARDWAREUSB_H
diff --git a/wirish/wirish.c b/wirish/wirish.c
index 3cfc12e..e21f792 100644
--- a/wirish/wirish.c
+++ b/wirish/wirish.c
@@ -31,6 +31,7 @@
#include "systick.h"
#include "gpio.h"
#include "nvic.h"
+#include "usb.h"
void init(void) {
rcc_init();
@@ -42,4 +43,5 @@ void init(void) {
timer_init(2, 1);
timer_init(3, 1);
timer_init(4, 1);
+ setupUSB();
}