aboutsummaryrefslogtreecommitdiffstats
path: root/libmaple/usb
diff options
context:
space:
mode:
Diffstat (limited to 'libmaple/usb')
-rw-r--r--libmaple/usb/README84
-rw-r--r--libmaple/usb/descriptors.c48
-rw-r--r--libmaple/usb/descriptors.h25
-rw-r--r--libmaple/usb/usb.c272
-rw-r--r--libmaple/usb/usb.h93
-rw-r--r--libmaple/usb/usb_callbacks.c425
-rw-r--r--libmaple/usb/usb_callbacks.h11
-rw-r--r--libmaple/usb/usb_config.h1
-rw-r--r--libmaple/usb/usb_hardware.c183
9 files changed, 588 insertions, 554 deletions
diff --git a/libmaple/usb/README b/libmaple/usb/README
index f3970b6..540b1ea 100644
--- a/libmaple/usb/README
+++ b/libmaple/usb/README
@@ -5,16 +5,19 @@ The USB submodule of libmaple is responsible for:
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.
+ via SerialUSB.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
+
+ [This section is out of date. Does SerialUSB.begin() do the same
+ thing as the old Usb.init()?]
+
+ SerialUSB.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
@@ -22,23 +25,27 @@ Current Status:
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.
+ code. Ideally, the new core library should mimic 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) It allows serial data transfers between user sketches an a
+ host computer.
+
+ 2) It 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 upload operation (uploads
+ application binary into RAM/FLASH). Thus, 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
@@ -51,19 +58,19 @@ Current Status:
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
+ other components (mass storage 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
+ Be mindful that enabling the USB peripheral isnt "free." The
device must respond to periodic bus activity (every few
- milliseconds) by servicing an ISR. Therefore the USB application
+ 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
+ be asserted (on Maple this is GPIO C12). 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
+ 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
@@ -72,21 +79,22 @@ Current Status:
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.
+ The current USB lib is ported 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 doesn't
+ have too many dependencies on libmaple. It even ensures that
+ clocks are configured correctly for its operation. However, over
+ time, some libmaple dependencies have crept in.
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
+ should have their own analogs 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
+ - implement a SerialUSB.link(SerialX) that forces a passthrough
the host computer virtual com to SerialX, and utilizes the
line_config commands correctly (sets baud etc)
diff --git a/libmaple/usb/descriptors.c b/libmaple/usb/descriptors.c
index 5038709..360e6dd 100644
--- a/libmaple/usb/descriptors.c
+++ b/libmaple/usb/descriptors.c
@@ -1,12 +1,35 @@
-/* insert license */
-
+/******************************************************************************
+ * The MIT License
+ *
+ * Copyright (c) 2010 LeafLabs LLC.
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *****************************************************************************/
#include "descriptors.h"
#include "libmaple.h"
#include "usb_config.h"
const USB_Descriptor_Device usbVcomDescriptor_Device = {
- bLength: sizeof(USB_Descriptor_Device),
+ bLength: sizeof(USB_Descriptor_Device),
bDescriptorType: USB_DESCRIPTOR_TYPE_DEVICE,
bcdUSB: 0x0200,
bDeviceClass: USB_DEVICE_CLASS_CDC,
@@ -29,7 +52,8 @@ const USB_Descriptor_Config usbVcomDescriptor_Config = {
bNumInterfaces: 0x02,
bConfigurationValue: 0x01,
iConfiguration: 0x00,
- bmAttributes: (USB_CONFIG_ATTR_BUSPOWERED | USB_CONFIG_ATTR_SELF_POWERED),
+ bmAttributes: (USB_CONFIG_ATTR_BUSPOWERED |
+ USB_CONFIG_ATTR_SELF_POWERED),
bMaxPower: USB_CONFIG_MAX_POWER,
CCI_Interface:
@@ -77,7 +101,7 @@ const USB_Descriptor_Config usbVcomDescriptor_Config = {
Data: {0x00, 0x01}
},
- // ManagementEndpoint:
+ // ManagementEndpoint:
// {
EP1_bLength: 0x07,//sizeof(USB_Descriptor_Endpoint),
EP1_bDescriptorType: USB_DESCRIPTOR_TYPE_ENDPOINT,
@@ -126,12 +150,12 @@ const USB_Descriptor_Config usbVcomDescriptor_Config = {
// }
};
-/*
+/*
String Identifiers:
we may choose to specify any or none of the following string
identifiers:
-
+
iManufacturer: LeafLabs
iProduct: Maple R3
iSerialNumber: NONE
@@ -143,7 +167,7 @@ const USB_Descriptor_Config usbVcomDescriptor_Config = {
which is 0x0409 for US English
*/
-const uint8 usbVcomDescriptor_LangID[USB_DESCRIPTOR_STRING_LEN(1)] =
+const uint8 usbVcomDescriptor_LangID[USB_DESCRIPTOR_STRING_LEN(1)] =
{
USB_DESCRIPTOR_STRING_LEN(1),
USB_DESCRIPTOR_TYPE_STRING,
@@ -151,18 +175,18 @@ const uint8 usbVcomDescriptor_LangID[USB_DESCRIPTOR_STRING_LEN(1)] =
0x04
};
-const uint8 usbVcomDescriptor_iManufacturer[USB_DESCRIPTOR_STRING_LEN(8)] =
+const uint8 usbVcomDescriptor_iManufacturer[USB_DESCRIPTOR_STRING_LEN(8)] =
{
USB_DESCRIPTOR_STRING_LEN(8),
USB_DESCRIPTOR_TYPE_STRING,
- 'L', 0, 'e', 0, 'a', 0, 'f', 0,
+ 'L', 0, 'e', 0, 'a', 0, 'f', 0,
'L', 0, 'a', 0, 'b', 0, 's', 0
};
-const uint8 usbVcomDescriptor_iProduct[USB_DESCRIPTOR_STRING_LEN(8)] =
+const uint8 usbVcomDescriptor_iProduct[USB_DESCRIPTOR_STRING_LEN(8)] =
{
USB_DESCRIPTOR_STRING_LEN(8),
USB_DESCRIPTOR_TYPE_STRING,
- 'M', 0, 'a', 0, 'p', 0, 'l', 0,
+ 'M', 0, 'a', 0, 'p', 0, 'l', 0,
'e', 0, ' ', 0, 'R', 0, '3', 0
};
diff --git a/libmaple/usb/descriptors.h b/libmaple/usb/descriptors.h
index 6652942..6f7d08b 100644
--- a/libmaple/usb/descriptors.h
+++ b/libmaple/usb/descriptors.h
@@ -1,4 +1,27 @@
-/* insert license */
+/* *****************************************************************************
+ * The MIT License
+ *
+ * Copyright (c) 2010 LeafLabs LLC.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ * ****************************************************************************/
+
#ifndef __DESCRIPTORS_H
#define __DESCRIPTORS_H
diff --git a/libmaple/usb/usb.c b/libmaple/usb/usb.c
index 026d7f0..d875785 100644
--- a/libmaple/usb/usb.c
+++ b/libmaple/usb/usb.c
@@ -1,30 +1,30 @@
-/* *****************************************************************************
+/******************************************************************************
* The MIT License
*
* Copyright (c) 2010 LeafLabs LLC.
*
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- * ****************************************************************************/
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *****************************************************************************/
/**
- * @file usb.c
- *
* @brief usb-specific hardware setup, NVIC, clocks, and usb activities
* in the pre-attached state. includes some of the lower level callbacks
* needed by the usb library, like suspend,resume,init,etc
@@ -47,87 +47,75 @@ volatile uint16 wIstr = 0;
volatile bIntPackSOF = 0;
DEVICE Device_Table =
- {
- NUM_ENDPTS,
- 1
- };
+ {NUM_ENDPTS,
+ 1};
DEVICE_PROP Device_Property =
- {
- usbInit,
- usbReset,
- usbStatusIn,
- usbStatusOut,
- usbDataSetup,
- usbNoDataSetup,
- usbGetInterfaceSetting,
- usbGetDeviceDescriptor,
- usbGetConfigDescriptor,
- usbGetStringDescriptor,
- 0,
- bMaxPacketSize
- };
+ {usbInit,
+ usbReset,
+ usbStatusIn,
+ usbStatusOut,
+ usbDataSetup,
+ usbNoDataSetup,
+ usbGetInterfaceSetting,
+ usbGetDeviceDescriptor,
+ usbGetConfigDescriptor,
+ usbGetStringDescriptor,
+ 0,
+ bMaxPacketSize};
USER_STANDARD_REQUESTS User_Standard_Requests =
- {
- NOP_Process,
- usbSetConfiguration,
- NOP_Process,
- NOP_Process,
- NOP_Process,
- NOP_Process,
- NOP_Process,
- NOP_Process,
- usbSetDeviceAddress
- };
+ {NOP_Process,
+ usbSetConfiguration,
+ NOP_Process,
+ NOP_Process,
+ NOP_Process,
+ NOP_Process,
+ NOP_Process,
+ NOP_Process,
+ usbSetDeviceAddress};
void (*pEpInt_IN[7])(void) =
-{
- vcomDataTxCb,
- vcomManagementCb,
- NOP_Process,
- NOP_Process,
- NOP_Process,
- NOP_Process,
- NOP_Process,
-};
+ {vcomDataTxCb,
+ vcomManagementCb,
+ NOP_Process,
+ NOP_Process,
+ NOP_Process,
+ NOP_Process,
+ NOP_Process};
void (*pEpInt_OUT[7])(void) =
-{
- NOP_Process,
- NOP_Process,
- vcomDataRxCb,
- NOP_Process,
- NOP_Process,
- NOP_Process,
- NOP_Process,
-};
-
-struct
-{
+ {NOP_Process,
+ NOP_Process,
+ vcomDataRxCb,
+ NOP_Process,
+ NOP_Process,
+ NOP_Process,
+ NOP_Process};
+
+struct {
volatile RESUME_STATE eState;
volatile uint8 bESOFcnt;
} ResumeS;
void setupUSB (void) {
gpio_set_mode(USB_DISC_BANK,
- USB_DISC_PIN,
- GPIO_MODE_OUTPUT_PP);
+ USB_DISC_PIN,
+ GPIO_MODE_OUTPUT_PP);
/* setup the apb1 clock for USB */
pRCC->APB1ENR |= 0x00800000;
/* initialize the usb application */
- gpio_write_bit(USB_DISC_BANK, USB_DISC_PIN, 0); /* present ourselves to the host */
-
- USB_Init(); /* low level init routine provided by st lib */
+ gpio_write_bit(USB_DISC_BANK, USB_DISC_PIN, 0); // presents us to the host
+ USB_Init(); // low level init routine provided by the ST library
}
void disableUSB (void) {
// These are just guesses about how to do this
// TODO: real disable function
usbDsbISR();
- gpio_write_bit(USB_DISC_BANK,USB_DISC_PIN,1);
+ gpio_write_bit(USB_DISC_BANK,USB_DISC_PIN,1);
}
void usbSuspend(void) {
@@ -188,13 +176,12 @@ void usbResume(RESUME_STATE eResumeSetVal) {
break;
case RESUME_ON:
ResumeS.bESOFcnt--;
- if (ResumeS.bESOFcnt == 0)
- {
- wCNTR = _GetCNTR();
- wCNTR &= (~CNTR_RESUME);
- _SetCNTR(wCNTR);
- ResumeS.eState = RESUME_OFF;
- }
+ if (ResumeS.bESOFcnt == 0) {
+ wCNTR = _GetCNTR();
+ wCNTR &= (~CNTR_RESUME);
+ _SetCNTR(wCNTR);
+ ResumeS.eState = RESUME_OFF;
+ }
break;
case RESUME_OFF:
case RESUME_ESOF:
@@ -213,7 +200,7 @@ RESULT usbPowerOn(void) {
wInterrupt_Mask = 0;
_SetCNTR(wInterrupt_Mask);
_SetISTR(0);
- wInterrupt_Mask = CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM; /* the bare minimum */
+ wInterrupt_Mask = CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM; // the bare minimum
_SetCNTR(wInterrupt_Mask);
return USB_SUCCESS;
@@ -284,60 +271,51 @@ if (wIstr & ISTR_ERR & wInterrupt_Mask)
#if (ISR_MSK & ISTR_WKUP)
-if (wIstr & ISTR_WKUP & wInterrupt_Mask)
- {
+if (wIstr & ISTR_WKUP & wInterrupt_Mask) {
_SetISTR((u16)CLR_WKUP);
usbResume(RESUME_EXTERNAL);
- }
+}
#endif
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
#if (ISR_MSK & ISTR_SUSP)
-if (wIstr & ISTR_SUSP & wInterrupt_Mask)
- {
-
+if (wIstr & ISTR_SUSP & wInterrupt_Mask) {
/* check if SUSPEND is possible */
- if (F_SUSPEND_ENABLED)
- {
- usbSuspend();
- }
- else
- {
- /* if not possible then resume after xx ms */
- usbResume(RESUME_LATER);
- }
+ if (F_SUSPEND_ENABLED) {
+ usbSuspend();
+ } else {
+ /* if not possible then resume after xx ms */
+ usbResume(RESUME_LATER);
+ }
/* clear of the ISTR bit must be done after setting of CNTR_FSUSP */
_SetISTR((u16)CLR_SUSP);
- }
+}
#endif
#if (ISR_MSK & ISTR_SOF)
-if (wIstr & ISTR_SOF & wInterrupt_Mask)
- {
+if (wIstr & ISTR_SOF & wInterrupt_Mask) {
_SetISTR((u16)CLR_SOF);
bIntPackSOF++;
- }
+ }
#endif
#if (ISR_MSK & ISTR_ESOF)
-if (wIstr & ISTR_ESOF & wInterrupt_Mask)
- {
+if (wIstr & ISTR_ESOF & wInterrupt_Mask) {
_SetISTR((u16)CLR_ESOF);
/* resume handling timing is made with ESOFs */
usbResume(RESUME_ESOF); /* request without change of the machine state */
- }
+ }
#endif
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
#if (ISR_MSK & ISTR_CTR)
-if (wIstr & ISTR_CTR & wInterrupt_Mask)
- {
+if (wIstr & ISTR_CTR & wInterrupt_Mask) {
/* servicing of the endpoint correct transfer interrupt */
/* clear of the CTR flag into the sub */
CTR_LP(); /* low priority ISR defined in the usb core lib */
- }
+ }
#endif
}
@@ -349,87 +327,73 @@ void usbWaitReset(void) {
/* This low-level send bytes function is NON-BLOCKING; blocking behavior, with
* a timeout, is implemented in usercode (or in the Wirish C++ high level
- * implementation).
+ * implementation).
*
* This function will quickly copy up to 64 bytes of data (out of an
* arbitrarily large buffer) into the USB peripheral TX buffer and return the
* number placed in that buffer. It is up to usercode to divide larger packets
- * into 64-byte chunks to guarantee delivery. Use usbGetCountTx() to determine
- * whether the bytes were ACTUALLY recieved by the host or just transfered to
- * the buffer.
+ * into 64-byte chunks to guarantee delivery.
*
- * The function will return -1 if it doesn't think that the USB host is
- * "connected", but it can't detect this state robustly. "Connected" in this
- * context means that an actual program on the Host operating system is
- * connected to the virtual COM/ttyACM device and is recieving the bytes; the
- * Host operating system is almost always configured and keeping this endpoint
- * alive, but the bytes never get read out of the endpoint buffer.
*
- * The behavior of this function is subtle and frustrating; it has gone through
- * many simpler and cleaner implementation that frustratingly don't work cross
- * platform.
- *
- * */
-uint16 usbSendBytes(uint8* sendBuf, uint16 len) {
-
- uint16 loaded = 0;
-
- if (bDeviceState != CONFIGURED || (!usbGetDTR() && !usbGetRTS())) {
- // Indicates to caller to stop trying, were not configured/connected
- // The DTR and RTS lines are handled differently on major platforms, so
- // the above logic is unreliable
- return 0;
- }
+ */
+void usbBlockingSendByte(char ch) {
+ while (countTx);
+ UserToPMABufferCopy(&ch,VCOM_TX_ADDR,1);
+ _SetEPTxCount(VCOM_TX_ENDP,1);
+ _SetEPTxValid(VCOM_TX_ENDP);
+ countTx = 1;
+ while (countTx);
+}
+uint32 usbSendBytes(uint8* sendBuf, uint32 len) {
+ /* any checks on connection (via dtr/rts) done upstream in wirish or
+ by user */
- // Due to a variety of shit this is how we roll; all buffering etc is pushed
- // upstream
- if (countTx) {
- return 0;
+ /* last xmit hasnt finished, abort */
+ if (countTx) {
+ return 0;
}
// We can only put VCOM_TX_EPSIZE bytes in the buffer
- if(len > VCOM_TX_EPSIZE) {
- loaded = VCOM_TX_EPSIZE;
- } else {
- loaded = len;
+ if(len > VCOM_TX_EPSIZE/2) {
+ len = VCOM_TX_EPSIZE/2;
}
// Try to load some bytes if we can
- if (loaded) {
- UserToPMABufferCopy(sendBuf,VCOM_TX_ADDR + countTx, loaded);
- _SetEPTxCount(VCOM_TX_ENDP, countTx+loaded);
+ if (len) {
+ UserToPMABufferCopy(sendBuf,VCOM_TX_ADDR, len);
+ _SetEPTxCount(VCOM_TX_ENDP, len);
+ countTx += len;
_SetEPTxValid(VCOM_TX_ENDP);
- countTx += loaded;
}
- return loaded;
+ return len;
}
/* returns the number of available bytes are in the recv FIFO */
-uint8 usbBytesAvailable(void) {
- return VCOM_RX_EPSIZE - maxNewBytes;
+uint32 usbBytesAvailable(void) {
+ return newBytes;
}
/* 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;
+uint32 usbReceiveBytes(uint8* recvBuf, uint32 len) {
+ if (len > newBytes) {
+ len = newBytes;
}
int i;
for (i=0;i<len;i++) {
- recvBuf[i] = (uint8)(vcomBufferRx[recvBufOut]);
- recvBufOut = (recvBufOut + 1) % VCOM_RX_EPSIZE;
+ recvBuf[i] = (uint8)(vcomBufferRx[i]);
}
- maxNewBytes += len;
+ newBytes -= len;
/* re-enable the rx endpoint which we had set to receive 0 bytes */
- if (maxNewBytes - len == 0) {
- SetEPRxCount(VCOM_RX_ENDP,maxNewBytes);
+ if (newBytes == 0) {
+ SetEPRxCount(VCOM_RX_ENDP,VCOM_RX_EPSIZE);
+ SetEPRxStatus(VCOM_RX_ENDP,EP_RX_VALID);
}
return len;
@@ -466,7 +430,7 @@ uint8 usbIsConfigured() {
uint8 usbIsConnected() {
return (bDeviceState != UNCONNECTED);
-}
+}
uint16 usbGetPending() {
return countTx;
diff --git a/libmaple/usb/usb.h b/libmaple/usb/usb.h
index ec179b1..0ed02e5 100644
--- a/libmaple/usb/usb.h
+++ b/libmaple/usb/usb.h
@@ -1,8 +1,31 @@
-/* insert license */
-
-#ifndef __USB_H
-#define __USB_H
+/******************************************************************************
+ * The MIT License
+ *
+ * Copyright (c) 2010 LeafLabs LLC.
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *****************************************************************************/
+#ifndef _USB_H_
+#define _USB_H_
#include "usb_lib.h"
#include "libmaple.h"
@@ -11,8 +34,7 @@
extern "C" {
#endif
-typedef enum
- {
+typedef enum {
RESUME_EXTERNAL,
RESUME_INTERNAL,
RESUME_LATER,
@@ -21,51 +43,50 @@ typedef enum
RESUME_ON,
RESUME_OFF,
RESUME_ESOF
- } RESUME_STATE;
+} RESUME_STATE;
-typedef enum
- {
+typedef enum {
UNCONNECTED,
ATTACHED,
POWERED,
SUSPENDED,
ADDRESSED,
CONFIGURED
- } DEVICE_STATE;
+} DEVICE_STATE;
+
+extern volatile uint32 bDeviceState;
- extern volatile uint32 bDeviceState;
+void setupUSB(void);
+void disableUSB(void);
+void usbSuspend(void);
+void usbResumeInit(void);
+void usbResume(RESUME_STATE);
- void setupUSB(void);
- void disableUSB(void);
- void usbSuspend(void);
- void usbResumeInit(void);
- void usbResume(RESUME_STATE);
-
- RESULT usbPowerOn(void);
- RESULT usbPowerOff(void);
-
- void usbDsbISR(void);
- void usbEnbISR(void);
+RESULT usbPowerOn(void);
+RESULT usbPowerOff(void);
- /* overloaded ISR routine, this is the main usb ISR */
- void usb_lpIRQHandler(void);
- void usbWaitReset(void);
+void usbDsbISR(void);
+void usbEnbISR(void);
- /* blocking functions for send/receive */
- uint16 usbSendBytes(uint8* sendBuf,uint16 len);
- uint8 usbBytesAvailable(void);
- uint8 usbReceiveBytes(uint8* recvBuf, uint8 len);
- uint8 usbGetDTR(void);
- uint8 usbGetRTS(void);
- uint8 usbIsConnected(void);
- uint8 usbIsConfigured(void);
- uint16 usbGetPending(void);
+/* overloaded ISR routine, this is the main usb ISR */
+void usb_lpIRQHandler(void);
+void usbWaitReset(void);
- void usbSendHello(void);
+/* blocking functions for send/receive */
+void usbBlockingSendByte(char ch);
+uint32 usbSendBytes(uint8* sendBuf,uint32 len);
+uint32 usbBytesAvailable(void);
+uint32 usbReceiveBytes(uint8* recvBuf, uint32 len);
+uint8 usbGetDTR(void);
+uint8 usbGetRTS(void);
+uint8 usbIsConnected(void);
+uint8 usbIsConfigured(void);
+uint16 usbGetPending(void);
+void usbSendHello(void);
#ifdef __cplusplus
} // extern "C"
#endif
-#endif //_USB_H
+#endif // _USB_H_
diff --git a/libmaple/usb/usb_callbacks.c b/libmaple/usb/usb_callbacks.c
index 4cdaf73..ccb0fdd 100644
--- a/libmaple/usb/usb_callbacks.c
+++ b/libmaple/usb/usb_callbacks.c
@@ -8,19 +8,19 @@
#include "usb_hardware.h"
ONE_DESCRIPTOR Device_Descriptor = {
- (uint8*)&usbVcomDescriptor_Device,
- sizeof(USB_Descriptor_Device)
+ (uint8*)&usbVcomDescriptor_Device,
+ sizeof(USB_Descriptor_Device)
};
ONE_DESCRIPTOR Config_Descriptor = {
- (uint8*)&usbVcomDescriptor_Config,
- 0x43//sizeof(USB_Descriptor_Config)
+ (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*)&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;
@@ -32,22 +32,22 @@ USB_Line_Coding line_coding = {
datatype: 0x08
};
-uint8 vcomBufferRx[VCOM_RX_EPSIZE];
-volatile uint8 countTx = 0;
-volatile uint8 recvBufIn = 0;
-volatile uint8 recvBufOut = 0;
-volatile uint8 maxNewBytes = VCOM_RX_EPSIZE;
-
+uint8 vcomBufferRx[VCOM_RX_BUFLEN];
+volatile uint32 countTx = 0;
+volatile uint32 recvBufIn = 0;
+volatile uint32 recvBufOut = 0;
+volatile uint32 maxNewBytes = VCOM_RX_BUFLEN;
+volatile uint32 newBytes = 0;
RESET_STATE reset_state = DTR_UNSET;
uint8 line_dtr_rts = 0;
void vcomDataTxCb(void) {
- /* do whatever after data has been sent to host */
-
- /* allows usbSendBytes to stop blocking */
+ /* do whatever after data has been sent to host */
+ /* allows usbSendBytes to stop blocking */
- countTx = 0;
+ /* assumes tx transactions are atomic 64 bytes (nearly certain they are) */
+ countTx = 0;
}
/* we could get arbitrarily complicated here for speed purposes
@@ -60,11 +60,11 @@ void vcomDataRxCb(void) {
/* 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); */
+ newBytes = GetEPRxCount(VCOM_RX_ENDP);
+ SetEPRxStatus(VCOM_RX_ENDP,EP_RX_NAK);
/* todo, not checking very carefully for edge cases. USUALLY,
- if we emit the reset pulse and send 4 bytes, then newBytes
+ if we emit the reset pulse and send 4 bytes, then newBytes
should be 4. But its POSSIBLE that this would be violated
in some cases */
@@ -82,270 +82,251 @@ void vcomDataRxCb(void) {
int i;
USB_Bool cmpMatch = TRUE;
for (i=0; i<4; i++) {
- if (chkBuf[i] != cmpBuf[i]) {
- cmpMatch = FALSE;
- }
+ if (chkBuf[i] != cmpBuf[i]) {
+ cmpMatch = FALSE;
+ }
}
if (cmpMatch) {
- 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 */
+ 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 */
}
}
}
-
-
- 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);
+ PMAToUserBufferCopy(&vcomBufferRx[0],VCOM_RX_ADDR,newBytes);
}
void vcomManagementCb(void) {
-/* unused. This enpoint would callback if we had sent a linestate
- changed notification */
+ /* 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;
+ 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();
+ pInformation->Current_Configuration = 0;
+ usbPowerOn();
- _SetISTR(0);
- wInterrupt_Mask = ISR_MSK;
- _SetCNTR(wInterrupt_Mask);
+ _SetISTR(0);
+ wInterrupt_Mask = ISR_MSK;
+ _SetCNTR(wInterrupt_Mask);
- usbEnbISR();
- bDeviceState = UNCONNECTED;
+ 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;
+ 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 */
+ /* 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;
+ 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;
- }
+ if (CopyRoutine == NULL) {
+ return USB_UNSUPPORT;
+ }
- pInformation->Ctrl_Info.CopyData = CopyRoutine;
- pInformation->Ctrl_Info.Usb_wOffset = 0;
- (*CopyRoutine)(0);
- return USB_SUCCESS;
+ 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);
- line_dtr_rts = new_signal & 0x03;
-
- switch (reset_state) {
- /* no default, covered enum */
- case DTR_UNSET:
- if ((new_signal & CONTROL_LINE_DTR) == 0 ) {
- reset_state = DTR_LOW;
- } else {
- reset_state = DTR_HIGH;
- }
- break;
-
- case DTR_HIGH:
- if ((new_signal & CONTROL_LINE_DTR) == 0 ) {
- reset_state = DTR_NEGEDGE;
- } else {
- reset_state = DTR_HIGH;
- }
- break;
-
- case DTR_NEGEDGE:
- if ((new_signal & CONTROL_LINE_DTR) == 0 ) {
- reset_state = DTR_LOW;
- } else {
- reset_state = DTR_HIGH;
- }
- break;
-
- case DTR_LOW:
- if ((new_signal & CONTROL_LINE_DTR) == 0 ) {
- reset_state = DTR_LOW;
- } else {
- reset_state = DTR_HIGH;
- }
- break;
- }
-
- return USB_SUCCESS;
+ 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);
+ line_dtr_rts = new_signal & 0x03;
+
+ switch (reset_state) {
+ /* no default, covered enum */
+ case DTR_UNSET:
+ if ((new_signal & CONTROL_LINE_DTR) == 0 ) {
+ reset_state = DTR_LOW;
+ } else {
+ reset_state = DTR_HIGH;
+ }
+ break;
+
+ case DTR_HIGH:
+ if ((new_signal & CONTROL_LINE_DTR) == 0 ) {
+ reset_state = DTR_NEGEDGE;
+ } else {
+ reset_state = DTR_HIGH;
+ }
+ break;
+
+ case DTR_NEGEDGE:
+ if ((new_signal & CONTROL_LINE_DTR) == 0 ) {
+ reset_state = DTR_LOW;
+ } else {
+ reset_state = DTR_HIGH;
+ }
+ break;
+
+ case DTR_LOW:
+ if ((new_signal & CONTROL_LINE_DTR) == 0 ) {
+ reset_state = DTR_LOW;
+ } else {
+ reset_state = DTR_HIGH;
+ }
+ break;
+ }
+
+ return USB_SUCCESS;
+ }
}
- }
- return USB_UNSUPPORT;
+ return USB_UNSUPPORT;
}
RESULT usbGetInterfaceSetting(uint8 interface, uint8 alt_setting) {
- if (alt_setting > 0) {
- return USB_UNSUPPORT;
- } else if (interface > 1) {
- return USB_UNSUPPORT;
- }
+ if (alt_setting > 0) {
+ return USB_UNSUPPORT;
+ } else if (interface > 1) {
+ return USB_UNSUPPORT;
+ }
- return USB_SUCCESS;
+ return USB_SUCCESS;
}
u8* usbGetDeviceDescriptor(u16 length) {
- return Standard_GetDescriptorData(length, &Device_Descriptor);
+ return Standard_GetDescriptorData(length, &Device_Descriptor);
}
u8* usbGetConfigDescriptor(u16 length) {
- return Standard_GetDescriptorData(length, &Config_Descriptor);
+ return Standard_GetDescriptorData(length, &Config_Descriptor);
}
u8* usbGetStringDescriptor(u16 length) {
- uint8 wValue0 = pInformation->USBwValue0;
+ uint8 wValue0 = pInformation->USBwValue0;
- if (wValue0 > 2) {
- return NULL;
- }
- return Standard_GetDescriptorData(length, &String_Descriptor[wValue0]);
+ 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;
- }
+ if (pInformation->Current_Configuration != 0) {
+ bDeviceState = CONFIGURED;
+ }
}
void usbSetDeviceAddress(void) {
- bDeviceState = ADDRESSED;
+ bDeviceState = ADDRESSED;
}
diff --git a/libmaple/usb/usb_callbacks.h b/libmaple/usb/usb_callbacks.h
index a94de11..20d2c13 100644
--- a/libmaple/usb/usb_callbacks.h
+++ b/libmaple/usb/usb_callbacks.h
@@ -34,11 +34,12 @@ typedef enum {
extern RESET_STATE reset_state; /* tracks DTR/RTS */
extern uint8 line_dtr_rts;
-extern volatile uint8 countTx;
-extern uint8 vcomBufferRx[VCOM_RX_EPSIZE]; /* no reason this has to be VCOM_RX_EPSIZE, could be bigger */
-extern volatile uint8 recvBufIn; /* the FIFO in index to the recvbuffer */
-extern volatile uint8 recvBufOut; /* the FIFO out index to the recvbuffer */
-extern volatile uint8 maxNewBytes;
+extern volatile uint32 countTx;
+extern uint8 vcomBufferRx[VCOM_RX_BUFLEN]; /* no reason this has to be VCOM_RX_EPSIZE, could be bigger */
+extern volatile uint32 recvBufIn; /* the FIFO in index to the recvbuffer */
+extern volatile uint32 recvBufOut; /* the FIFO out index to the recvbuffer */
+extern volatile uint32 maxNewBytes;
+extern volatile uint32 newBytes;
void vcomDataTxCb(void);
void vcomDataRxCb(void);
diff --git a/libmaple/usb/usb_config.h b/libmaple/usb/usb_config.h
index ba05d42..e5f3979 100644
--- a/libmaple/usb/usb_config.h
+++ b/libmaple/usb/usb_config.h
@@ -26,6 +26,7 @@
#define VCOM_RX_EPNUM 0x03
#define VCOM_RX_ADDR 0x110
#define VCOM_RX_EPSIZE 0x40
+#define VCOM_RX_BUFLEN (VCOM_RX_EPSIZE*3)
#define bMaxPacketSize 0x40 /* 64B, maximum for USB FS Devices */
diff --git a/libmaple/usb/usb_hardware.c b/libmaple/usb/usb_hardware.c
index 2f37df6..505dcf1 100644
--- a/libmaple/usb/usb_hardware.c
+++ b/libmaple/usb/usb_hardware.c
@@ -1,29 +1,31 @@
-/* *****************************************************************************
+/******************************************************************************
* The MIT License
*
* Copyright (c) 2010 LeafLabs LLC.
*
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- * ****************************************************************************/
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *****************************************************************************/
/**
- * @file hardware.c
+ * @file usb_hardware.c
*
* @brief init routines to setup clocks and interrupts for usb.
*
@@ -32,97 +34,106 @@
#include "usb_hardware.h"
void setPin(u32 bank, u8 pin) {
- u32 pinMask = 0x1 << (pin);
- SET_REG(GPIO_BSRR(bank),pinMask);
+ 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);
+ 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_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 */
+ 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 */
+ /* enable HSE */
+ SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x00010001);
+ /* for it to come on */
+ while ((GET_REG(RCC_CR) & 0x00020000) == 0);
+
+ /* Configure PLL */
+ /* pll=72Mhz,APB1=36Mhz,AHB=72Mhz */
+ SET_REG(RCC_CFGR,GET_REG(RCC_CFGR) | 0x001D0400);
+ /* enable the pll */
+ SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x01000000);
+ /* wait for it to come on */
+ while ((GET_REG(RCC_CR) & 0x03000000) == 0);
+
+ /* Set SYSCLK as PLL */
+ SET_REG(RCC_CFGR,GET_REG(RCC_CFGR) | 0x00000002);
+ /* wait for it to come on */
+ while ((GET_REG(RCC_CFGR) & 0x00000008) == 0);
}
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);
+ 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 */
+ NVIC_TypeDef* rNVIC = (NVIC_TypeDef *) NVIC_BASE;
+ rNVIC->ICER[0] = 0xFFFFFFFF;
+ rNVIC->ICER[1] = 0xFFFFFFFF;
+ rNVIC->ICPR[0] = 0xFFFFFFFF;
+ rNVIC->ICPR[1] = 0xFFFFFFFF;
+
+ /* Disable the systick timer, which operates separately from NVIC */
+ SET_REG(STK_CTRL,0x04);
}
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");
- }
+ 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");
+ }
}