From 88d4b095e4590ab9bbafcf76e134d168f66c41b1 Mon Sep 17 00:00:00 2001 From: Perry Hung Date: Fri, 4 Jun 2010 00:43:21 -0400 Subject: Preliminary wirish USBSerial implementation. -updated examples -removed HardwareUSB -cleaned up a handful of includes --- Makefile | 2 +- examples/spi_master.cpp | 1 - examples/test-session.cpp | 6 ---- libmaple/usb/usb.c | 41 ++++++++++++++----------- libmaple/usb/usb_callbacks.c | 8 ++--- libmaple/usb/usb_callbacks.h | 8 ++--- main.cpp.example | 10 +++--- wirish/Print.cpp | 8 +++-- wirish/Print.h | 2 +- wirish/WProgram.h | 3 -- wirish/comm/HardwareUsb.cpp | 60 ------------------------------------ wirish/comm/HardwareUsb.h | 48 ----------------------------- wirish/usb_serial.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++ wirish/usb_serial.h | 52 ++++++++++++++++++++++++++++++++ wirish/wirish.h | 6 ++++ 15 files changed, 173 insertions(+), 154 deletions(-) delete mode 100644 wirish/comm/HardwareUsb.cpp delete mode 100644 wirish/comm/HardwareUsb.h create mode 100644 wirish/usb_serial.cpp create mode 100644 wirish/usb_serial.h diff --git a/Makefile b/Makefile index 3917e35..c26027c 100644 --- a/Makefile +++ b/Makefile @@ -91,8 +91,8 @@ CSRC = libmaple/systick.c \ CPPSRC = wirish/wirish_math.cpp \ wirish/Print.cpp \ wirish/comm/HardwareSerial.cpp \ - wirish/comm/HardwareUsb.cpp \ wirish/comm/HardwareSPI.cpp \ + wirish/usb_serial.cpp \ wirish/cxxabi-compat.cpp \ main.cpp diff --git a/examples/spi_master.cpp b/examples/spi_master.cpp index 19679f7..2b62c8d 100644 --- a/examples/spi_master.cpp +++ b/examples/spi_master.cpp @@ -33,7 +33,6 @@ */ #include "wirish.h" -#include "HardwareSPI.h" #define CS 10 diff --git a/examples/test-session.cpp b/examples/test-session.cpp index 3d86dc6..78b1d31 100644 --- a/examples/test-session.cpp +++ b/examples/test-session.cpp @@ -23,16 +23,10 @@ * ****************************************************************************/ #include "wirish.h" -#include "HardwareSerial.h" -#include "HardwareUsb.h" -#include -#include "usb.h" #define LED_PIN 13 #define PWM_PIN 2 -HardwareUsb Usb; - uint8 input = 0; uint8 tiddle = 0; int toggle = 0; diff --git a/libmaple/usb/usb.c b/libmaple/usb/usb.c index f926e81..b7d2d93 100644 --- a/libmaple/usb/usb.c +++ b/libmaple/usb/usb.c @@ -26,7 +26,7 @@ * @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 + * in the pre-attached state. includes some of the lower level callbacks * needed by the usb library, like suspend,resume,init,etc */ @@ -46,13 +46,13 @@ volatile uint32 bDeviceState = UNCONNECTED; volatile uint16 wIstr = 0; volatile bIntPackSOF = 0; -DEVICE Device_Table = +DEVICE Device_Table = { NUM_ENDPTS, 1 }; -DEVICE_PROP Device_Property = +DEVICE_PROP Device_Property = { usbInit, usbReset, @@ -69,7 +69,7 @@ DEVICE_PROP Device_Property = bMaxPacketSize }; -USER_STANDARD_REQUESTS User_Standard_Requests = +USER_STANDARD_REQUESTS User_Standard_Requests = { NOP_Process, usbSetConfiguration, @@ -83,7 +83,7 @@ USER_STANDARD_REQUESTS User_Standard_Requests = }; void (*pEpInt_IN[7])(void) = -{ +{ vcomDataTxCb, vcomManagementCb, NOP_Process, @@ -218,7 +218,7 @@ RESULT usbPowerOff(void) { _SetISTR(0); _SetCNTR(CNTR_FRES + CNTR_PDWN); - /* note that all weve done here is powerdown the + /* 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 */ @@ -370,15 +370,18 @@ void usbWaitReset(void) { systemHardReset(); } -/* copies data out of sendBuf into the packet memory for - usb, but waits until any previous usb transmissions have +/* 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 + 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) { + /* Block for any pending writes */ + while (countTx) + ; /* while (countTx != 0) { */ /* if (reset_state == NDTR_NRTS) { */ /* return 0; */ @@ -390,7 +393,7 @@ int16 usbSendBytes(uint8* sendBuf, uint16 len) { } /* ideally we should wait here, but it gets stuck - for some reason. countTx wont decrement when + 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...*/ @@ -398,16 +401,18 @@ int16 usbSendBytes(uint8* sendBuf, uint16 len) { return 0; /* indicated to caller to keep trying, were just busy */ } - uint16 sent = len; + 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); + _SetEPTxCount(VCOM_TX_ENDP, VCOM_TX_EPSIZE); _SetEPTxValid(VCOM_TX_ENDP); - while(countTx != 0); + while (countTx) + ; + len -= VCOM_TX_EPSIZE; sendBuf += VCOM_TX_EPSIZE; } @@ -418,7 +423,7 @@ int16 usbSendBytes(uint8* sendBuf, uint16 len) { _SetEPTxCount(VCOM_TX_ENDP,len); _SetEPTxValid(VCOM_TX_ENDP); } - + return sent; } @@ -427,15 +432,15 @@ 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 +/* 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;iuart linking */ -} - -HardwareUsb Usb; diff --git a/wirish/comm/HardwareUsb.h b/wirish/comm/HardwareUsb.h deleted file mode 100644 index e2af2e2..0000000 --- a/wirish/comm/HardwareUsb.h +++ /dev/null @@ -1,48 +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. Dummy implmentation wrapping printChar() around usb.h for now. Todo: something better. - */ - -#ifndef _HARDWAREUSB_H_ -#define _HARDWAREUSB_H_ - -#include "Print.h" -#include "usb.h" - -class HardwareUsb : public Print { - public: - HardwareUsb(void); - void begin(); - uint8 available(void); - uint8 read(void); - void flush(void); - virtual void write(unsigned char); - using Print::write; -}; - -extern HardwareUsb Usb; - -#endif //_HARDWAREUSB_H/* ***************************************************************************** diff --git a/wirish/usb_serial.cpp b/wirish/usb_serial.cpp new file mode 100644 index 0000000..9476e99 --- /dev/null +++ b/wirish/usb_serial.cpp @@ -0,0 +1,72 @@ +/* ***************************************************************************** + * The MIT License + * + * Copyright (c) 2010 Perry Hung. + * + * 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 wirish usb class for easy goin communication, uses libmaple's + * virtual com port implementation + */ + +#include + +#include "wirish.h" +#include "usb.h" + +USBSerial :: USBSerial(void) { +} + +void USBSerial::write(uint8 ch) { + usbSendBytes(&ch, 1); +} + +void USBSerial::write(const char *str) { + uint32 len = strlen(str); + usbSendBytes((uint8*)str, len); +} + +void USBSerial::write(void *buf, uint32 size) { + if (!buf) { + return; + } + usbSendBytes((uint8*)buf, size); +} + +uint32 USBSerial::available(void) { + return usbBytesAvailable(); +} + +uint32 USBSerial::read(void *buf, uint32 len) { + if (!buf) { + return 0; + } + + return usbReceiveBytes((uint8*)buf, len); +} + +uint8 USBSerial::read(void) { + uint8 ch; + return usbReceiveBytes(&ch, 1); +} + +USBSerial SerialUSB; + diff --git a/wirish/usb_serial.h b/wirish/usb_serial.h new file mode 100644 index 0000000..e1c61ff --- /dev/null +++ b/wirish/usb_serial.h @@ -0,0 +1,52 @@ +/* ***************************************************************************** + * The MIT License + * + * Copyright (c) 2010 Perry Hung. + * + * 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 wirish usb class for easy goin communication, uses libmaple's + * virtual com port implementation + */ + +#ifndef _USB_SERIAL_H_ +#define _USB_SERIAL_H_ + +#include "Print.h" + +class USBSerial : public Print { + public: + USBSerial(void); + + uint32 available(void); + + uint32 read(void *buf, uint32 len); + uint8 read(void); + + void write(uint8); + void write(const char *str); + void write(void *, uint32); +}; + +extern USBSerial SerialUSB; + +#endif + diff --git a/wirish/wirish.h b/wirish/wirish.h index f54517a..2541e5e 100644 --- a/wirish/wirish.h +++ b/wirish/wirish.h @@ -40,6 +40,12 @@ #include "ext_interrupts.h" #include "wirish_math.h" +#ifdef __cplusplus +#include "HardwareSPI.h" +#include "HardwareSerial.h" +#include "usb_serial.h" +#endif + #ifdef __cplusplus extern "C"{ #endif -- cgit v1.2.3