diff options
author | Perry Hung <iperry@alum.mit.edu> | 2010-06-04 00:43:21 -0400 |
---|---|---|
committer | Perry Hung <iperry@alum.mit.edu> | 2010-06-04 00:43:21 -0400 |
commit | 88d4b095e4590ab9bbafcf76e134d168f66c41b1 (patch) | |
tree | 5947dd76c0136194e0e08ea45cbe4e004d7b81ff | |
parent | 1a908d88b8c288fca89b17ceea182044d82e766e (diff) | |
download | librambutan-88d4b095e4590ab9bbafcf76e134d168f66c41b1.tar.gz librambutan-88d4b095e4590ab9bbafcf76e134d168f66c41b1.zip |
Preliminary wirish USBSerial implementation.
-updated examples
-removed HardwareUSB
-cleaned up a handful of includes
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | examples/spi_master.cpp | 1 | ||||
-rw-r--r-- | examples/test-session.cpp | 6 | ||||
-rw-r--r-- | libmaple/usb/usb.c | 41 | ||||
-rw-r--r-- | libmaple/usb/usb_callbacks.c | 8 | ||||
-rw-r--r-- | libmaple/usb/usb_callbacks.h | 8 | ||||
-rw-r--r-- | main.cpp.example | 10 | ||||
-rw-r--r-- | wirish/Print.cpp | 8 | ||||
-rw-r--r-- | wirish/Print.h | 2 | ||||
-rw-r--r-- | wirish/WProgram.h | 3 | ||||
-rw-r--r-- | wirish/usb_serial.cpp (renamed from wirish/comm/HardwareUsb.cpp) | 50 | ||||
-rw-r--r-- | wirish/usb_serial.h (renamed from wirish/comm/HardwareUsb.h) | 38 | ||||
-rw-r--r-- | wirish/wirish.h | 6 |
13 files changed, 101 insertions, 82 deletions
@@ -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 <math.h>
-#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;i<len;i++) { recvBuf[i] = (uint8)(vcomBufferRx[recvBufOut]); diff --git a/libmaple/usb/usb_callbacks.c b/libmaple/usb/usb_callbacks.c index 08d62c5..5c4a386 100644 --- a/libmaple/usb/usb_callbacks.c +++ b/libmaple/usb/usb_callbacks.c @@ -32,10 +32,10 @@ USB_Line_Coding line_coding = { }; uint8 vcomBufferRx[VCOM_RX_EPSIZE]; -uint8 countTx = 0; -uint8 recvBufIn = 0; -uint8 recvBufOut = 0; -uint8 maxNewBytes = VCOM_RX_EPSIZE; +volatile uint8 countTx = 0; +volatile uint8 recvBufIn = 0; +volatile uint8 recvBufOut = 0; +volatile uint8 maxNewBytes = VCOM_RX_EPSIZE; RESET_STATE reset_state = START; diff --git a/libmaple/usb/usb_callbacks.h b/libmaple/usb/usb_callbacks.h index f8a2ef3..ed57fa1 100644 --- a/libmaple/usb/usb_callbacks.h +++ b/libmaple/usb/usb_callbacks.h @@ -34,11 +34,11 @@ typedef enum { } RESET_STATE; extern RESET_STATE reset_state; /* tracks DTR/RTS */ -extern uint8 countTx; +extern volatile 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; +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; void vcomDataTxCb(void); void vcomDataRxCb(void); diff --git a/main.cpp.example b/main.cpp.example index 67380ae..18966da 100644 --- a/main.cpp.example +++ b/main.cpp.example @@ -23,13 +23,11 @@ * ****************************************************************************/
/**
- * @brief Sample main.cpp file. Blinks an LED, sends a message out USART2
- * and turns on PWM on pin 2
+ * @brief Sample main.cpp file. Blinks an LED, sends a message out USART2 and the,
+ * USB virtual serial port, and turns on PWM on pin 2
*/
#include "wirish.h"
-#include "HardwareSerial.h"
-#include "usb.h"
#define LED_PIN 13
#define PWM_PIN 2
@@ -43,10 +41,12 @@ void setup() Serial2.begin(9600);
Serial2.println("Hello world!");
+ /* Send a message out the usb virtual serial port */
+ SerialUSB.println("Hello!");
+
/* Turn on PWM on pin PWM_PIN */
pinMode(PWM_PIN, PWM);
pwmWrite(PWM_PIN, 0x8000);
-
}
int toggle = 0;
diff --git a/wirish/Print.cpp b/wirish/Print.cpp index 5a1bc93..9baa757 100644 --- a/wirish/Print.cpp +++ b/wirish/Print.cpp @@ -32,10 +32,12 @@ void Print::write(const char *str) } /* default implementation: may be overridden */ -void Print::write(const uint8 *buffer, uint32 size) +void Print::write(void *buffer, uint32 size) { - while (size--) - write(*buffer++); + uint8 *ch = (uint8*)buffer; + while (size--) { + write(*ch++); + } } void Print::print(uint8 b) diff --git a/wirish/Print.h b/wirish/Print.h index b2f8647..72208b4 100644 --- a/wirish/Print.h +++ b/wirish/Print.h @@ -36,7 +36,7 @@ class Print public: virtual void write(uint8) = 0; virtual void write(const char *str); - virtual void write(const uint8 *buffer, uint32 size); + virtual void write(void *, uint32); void print(char); void print(const char[]); void print(uint8); diff --git a/wirish/WProgram.h b/wirish/WProgram.h index 889219e..9143991 100644 --- a/wirish/WProgram.h +++ b/wirish/WProgram.h @@ -23,9 +23,6 @@ * ****************************************************************************/ #include "wirish.h" -#include "HardwareSerial.h" -#include "HardwareUsb.h" -#include "math.h" void setup(); void loop(); diff --git a/wirish/comm/HardwareUsb.cpp b/wirish/usb_serial.cpp index 064bcc8..9476e99 100644 --- a/wirish/comm/HardwareUsb.cpp +++ b/wirish/usb_serial.cpp @@ -1,7 +1,7 @@ /* ***************************************************************************** * The MIT License * - * Copyright (c) 2010 Andrew Meyer. + * 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 @@ -23,38 +23,50 @@ * ****************************************************************************/ /** - * @brief Wiring like serial api to USB virtual COM + * @brief wirish usb class for easy goin communication, uses libmaple's + * virtual com port implementation */ +#include <string.h> + #include "wirish.h" -#include "HardwareUsb.h" #include "usb.h" -HardwareUsb::HardwareUsb(void) { +USBSerial :: USBSerial(void) { } -uint8 HardwareUsb::read(void) { - uint8 outVal; - usbReceiveBytes(&outVal,1); - return outVal; +void USBSerial::write(uint8 ch) { + usbSendBytes(&ch, 1); } -uint8 HardwareUsb::available(void) { - return usbBytesAvailable(); +void USBSerial::write(const char *str) { + uint32 len = strlen(str); + usbSendBytes((uint8*)str, len); } -void HardwareUsb::flush(void) { - uint8 totalBytes = usbBytesAvailable(); - uint8 recvBuf[totalBytes]; - usbReceiveBytes(recvBuf,totalBytes); +void USBSerial::write(void *buf, uint32 size) { + if (!buf) { + return; + } + usbSendBytes((uint8*)buf, size); } -void HardwareUsb::write(unsigned char ch) { - while (usbSendBytes(&ch, 1) == 0); +uint32 USBSerial::available(void) { + return usbBytesAvailable(); } -void HardwareUsb::begin(void) { - /* placeholder for usb<->uart linking */ +uint32 USBSerial::read(void *buf, uint32 len) { + if (!buf) { + return 0; + } + + return usbReceiveBytes((uint8*)buf, len); } -HardwareUsb Usb; +uint8 USBSerial::read(void) { + uint8 ch; + return usbReceiveBytes(&ch, 1); +} + +USBSerial SerialUSB; + diff --git a/wirish/comm/HardwareUsb.h b/wirish/usb_serial.h index e2af2e2..e1c61ff 100644 --- a/wirish/comm/HardwareUsb.h +++ b/wirish/usb_serial.h @@ -1,7 +1,7 @@ /* ***************************************************************************** * The MIT License * - * Copyright (c) 2010 Andrew Meyer. + * 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 @@ -23,26 +23,30 @@ * ****************************************************************************/ /** - * @brief Wiring like serial api to USB virtual COM. Dummy implmentation wrapping printChar() around usb.h for now. Todo: something better. + * @brief wirish usb class for easy goin communication, uses libmaple's + * virtual com port implementation */ -#ifndef _HARDWAREUSB_H_ -#define _HARDWAREUSB_H_ +#ifndef _USB_SERIAL_H_ +#define _USB_SERIAL_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; + +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 HardwareUsb Usb; +extern USBSerial SerialUSB; + +#endif -#endif //_HARDWAREUSB_H/* ***************************************************************************** diff --git a/wirish/wirish.h b/wirish/wirish.h index f54517a..2541e5e 100644 --- a/wirish/wirish.h +++ b/wirish/wirish.h @@ -41,6 +41,12 @@ #include "wirish_math.h" #ifdef __cplusplus +#include "HardwareSPI.h" +#include "HardwareSerial.h" +#include "usb_serial.h" +#endif + +#ifdef __cplusplus extern "C"{ #endif |