diff options
author | ajmeyer@mit.edu <ajmeyer@mit.edu@749a229e-a60e-11de-b98f-4500b42dc123> | 2010-01-07 03:31:35 +0000 |
---|---|---|
committer | ajmeyer@mit.edu <ajmeyer@mit.edu@749a229e-a60e-11de-b98f-4500b42dc123> | 2010-01-07 03:31:35 +0000 |
commit | 2addfe8c42c6bcdc0a15c751e2436447b73d03fe (patch) | |
tree | 07e569cd0fe7fd4a06cb5f71e12ba76dda3d5e2c /src | |
parent | 5f423270cde82f9dfffb52bdd617e5eb439921c5 (diff) | |
download | librambutan-2addfe8c42c6bcdc0a15c751e2436447b73d03fe.tar.gz librambutan-2addfe8c42c6bcdc0a15c751e2436447b73d03fe.zip |
Added a print class for USB, works identically to the Serial object. Bugs out if you try and pump more than 64 bytes through it in a single packet (which is really the OS's decision). This can be fixed
git-svn-id: https://leaflabs.googlecode.com/svn/trunk/library@93 749a229e-a60e-11de-b98f-4500b42dc123
Diffstat (limited to 'src')
-rw-r--r-- | src/example_main.cpp | 34 | ||||
-rw-r--r-- | src/example_main_bk.cpp | 74 | ||||
-rw-r--r-- | src/lib/bootVect.h | 52 | ||||
-rw-r--r-- | src/lib/usb.c | 24 | ||||
-rw-r--r-- | src/lib/usb.h | 28 | ||||
-rw-r--r-- | src/wiring/comm/HardwareUsb.cpp | 96 | ||||
-rw-r--r-- | src/wiring/comm/HardwareUsb.h | 48 |
7 files changed, 309 insertions, 47 deletions
diff --git a/src/example_main.cpp b/src/example_main.cpp index 8c90b7d..7202c66 100644 --- a/src/example_main.cpp +++ b/src/example_main.cpp @@ -1,19 +1,13 @@ #include "wiring.h"
#include "HardwareSerial.h"
+#include "HardwareUsb.h"
#include "math.h"
#include "usb.h"
int ledPin = 13;
uint8_t bytes_in;
-BootVectTable* mapleVect;
-
-void usb_tx_cb(void) {
-}
-
-void usb_rx_cb(void) {
- bytes_in = usb_serialGetRecvLen();
-}
+HardwareUsb Usb;
void setup()
{
@@ -27,30 +21,28 @@ void setup() Serial2.println("setup end");
- mapleVect = (BootVectTable*)(BOOTLOADER_VECT_TABLE);
- mapleVect->serial_tx_cb = usb_tx_cb;
- mapleVect->serial_rx_cb = usb_rx_cb;
+ Usb.begin();
+ Usb.flush();
}
-
int toggle = 0;
const char* testMsg = "hello world!\n";
-
+const char x = 'a';
static inline void loop() {
toggle ^= 1;
digitalWrite(ledPin, toggle);
- delay(1000);
- usb_serialWriteStr("blink...\n");
+ delay(50);
- if (bytes_in > 0) {
- int i;
- for (i=0;i<bytes_in;i++) {
- usb_serialWriteStr("b,");
+ uint8_t numBytes=Usb.available();
+
+ if (numBytes > 0) {
+ while (numBytes-->0) {
+ Usb.print(Usb.read());
}
- bytes_in = 0;
- usb_serialWriteStr("\n");
}
+
+ Usb.flush();
}
diff --git a/src/example_main_bk.cpp b/src/example_main_bk.cpp new file mode 100644 index 0000000..ce1b1c1 --- /dev/null +++ b/src/example_main_bk.cpp @@ -0,0 +1,74 @@ +#include "wiring.h"
+#include "HardwareSerial.h"
+#include "HardwareUsb.h"
+#include "math.h"
+#include "usb.h"
+
+int ledPin = 13;
+uint8_t bytes_in;
+
+BootVectTable* mapleVect;
+
+void usb_tx_cb(void) {
+}
+
+void usb_rx_cb(void) {
+ bytes_in = usb_serialGetRecvLen();
+}
+
+void setup()
+{
+ pinMode(ledPin, OUTPUT);
+ Serial2.begin(9600);
+ Serial2.println("setup start");
+
+ pinMode(6, PWM);
+ pwmWrite(6, 0x8000);
+ pinMode(7, OUTPUT);
+
+ Serial2.println("setup end");
+
+ mapleVect = (BootVectTable*)(BOOTLOADER_VECT_TABLE);
+ mapleVect->serial_tx_cb = usb_tx_cb;
+ mapleVect->serial_rx_cb = usb_rx_cb;
+}
+
+
+int toggle = 0;
+
+const char* testMsg = "hello world!\n";
+
+static inline void loop() {
+ toggle ^= 1;
+ digitalWrite(ledPin, toggle);
+ delay(1000);
+ usb_serialWriteStr("blink...\n");
+
+ if (bytes_in > 0) {
+ int i;
+ for (i=0;i<bytes_in;i++) {
+ usb_serialWriteStr("b,");
+ }
+ bytes_in = 0;
+ usb_serialWriteStr("\n");
+ }
+}
+
+
+int main(void) {
+ init();
+ setup();
+
+ while (1) {
+ loop();
+ }
+ return 0;
+}
+
+/* Required for C++ hackery */
+/* TODO: This really shouldn't go here... move it later
+ * */
+extern "C" void __cxa_pure_virtual(void) {
+ while(1)
+ ;
+}
diff --git a/src/lib/bootVect.h b/src/lib/bootVect.h new file mode 100644 index 0000000..5bcb42b --- /dev/null +++ b/src/lib/bootVect.h @@ -0,0 +1,52 @@ +/* ***************************************************************************** + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * ****************************************************************************/ + +/** + * @file HardwareUsb.h + * + * @brief Defines the maple boot vector structure + */ + +#ifndef _BOOTVECT_H_ +#define _BOOTVECT_H_ + +#define BOOTLOADER_VECT_TABLE ((uint32_t*)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_t* serial_count_in; + uint32_t* serial_count_out; + uint8_t* serial_buffer_out; + void* linecoding; + uint8_t major_rev; + uint8_t minor_rev; + void* usb_device_ptr; + void* usb_local_obj_ptr; +} BootVectTable; + +#ifdef __cplusplus +} +#endif + +#endif // _BOOTVECT_H_ diff --git a/src/lib/usb.c b/src/lib/usb.c index 17c8c8d..8c312f8 100644 --- a/src/lib/usb.c +++ b/src/lib/usb.c @@ -1,8 +1,6 @@ #include <inttypes.h> #include "usb.h" -BootVectTable* bootVect = ((BootVectTable*) BOOTLOADER_VECT_TABLE); - void usb_lpIRQHandler(void) { typedef void (*funcPtr)(void); @@ -60,8 +58,26 @@ void usb_serialWriteStr(const char* outStr) { } +void usb_serialWriteChar(unsigned char ch) { + BootVectTable *bootVector = ((BootVectTable*)BOOTLOADER_VECT_TABLE); + + delay(1); + + *(bootVector->serial_count_in) = 1; + usb_userToPMABufferCopy((u8*)(&ch),USB_SERIAL_ENDP_TXADDR,1); + _SetEPTxCount(USB_SERIAL_ENDP_TX,1); + _SetEPTxValid(USB_SERIAL_ENDP_TX); + +} + uint8_t usb_serialGetRecvLen() { - uint8_t count_out = _GetEPRxCount(USB_SERIAL_ENDP_RX); - _SetEPRxValid(USB_SERIAL_ENDP_RX); + uint8_t count_out =_GetEPRxCount(USB_SERIAL_ENDP_RX); return count_out; } + +void usb_copyRecvBuffer(unsigned char* dest, uint8_t len) { + ASSERT(len < USB_SERIAL_BUF_SIZE); + usb_PMAToUserBufferCopy((u8*)(dest),USB_SERIAL_ENDP_RXADDR,len); + _SetEPRxValid(USB_SERIAL_ENDP_RX); +} + diff --git a/src/lib/usb.h b/src/lib/usb.h index 3a7e92b..3b7a971 100644 --- a/src/lib/usb.h +++ b/src/lib/usb.h @@ -5,10 +5,7 @@ #include "util.h" #include "cortexm3_macro.h" #include "usb_regs.h" - -#ifdef __cplusplus -extern "C" { -#endif +#include "bootVect.h" #define USB_ISR_ADDR (0x08000090) #define USB_SERIAL_ENDP_TXADDR ((uint32_t) 0xC0) @@ -17,30 +14,17 @@ extern "C" { #define USB_SERIAL_ENDP_RX ((uint16_t) 0x3) #define USB_SERIAL_BUF_SIZE (0x40) -#define BOOTLOADER_VECT_TABLE ((uint32_t*)0x20000000) - -typedef void (*FuncPtr)(void); - -typedef struct { - FuncPtr serial_tx_cb; - FuncPtr serial_rx_cb; - FuncPtr serial_linecoding_cb; - uint32_t* serial_count_in; - uint32_t* serial_count_out; - uint8_t* serial_buffer_out; - void* linecoding; - uint8_t major_rev; - uint8_t minor_rev; - void* usb_device_ptr; -} BootVectTable; - -extern BootVectTable* bootVect; +#ifdef __cplusplus +extern "C" { +#endif void usb_lpIRQHandler(void); void usb_userToPMABufferCopy(u8 *pbUsrBuf,u16 wPMABufAddr,u16 wNBytes); void usb_PMAToUserBufferCopy(u8 *pbUsrBuf,u16 wPMABufAddr,u16 wNBytes); void usb_serialWriteStr(const char *outStr); +void usb_serialWriteChar(unsigned char ch); uint8_t usb_serialGetRecvLen(); +void usb_copyRecvBuffer(unsigned char* dest, uint8_t len); #ifdef __cplusplus } // extern "C" diff --git a/src/wiring/comm/HardwareUsb.cpp b/src/wiring/comm/HardwareUsb.cpp new file mode 100644 index 0000000..269a68f --- /dev/null +++ b/src/wiring/comm/HardwareUsb.cpp @@ -0,0 +1,96 @@ +/* ***************************************************************************** + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * ****************************************************************************/ + +/** + * @file HardwareUsb.cpp + * + * @brief Wiring like serial api to USB virtual COM + */ + +#include "wiring.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_t HardwareUsb::read(void) { + uint8_t outVal = rx_buffer[rx_buffer_offset_out++]; + +#if 1 + if (rx_buffer_offset_out == rx_buffer_offset_in) { + flush(); + } +#endif + + return outVal; +} + +uint8_t 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_t 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/src/wiring/comm/HardwareUsb.h b/src/wiring/comm/HardwareUsb.h new file mode 100644 index 0000000..b8733c1 --- /dev/null +++ b/src/wiring/comm/HardwareUsb.h @@ -0,0 +1,48 @@ +/* ***************************************************************************** + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * ****************************************************************************/ + +/** + * @file HardwareUsb.h + * + * @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_t rx_buffer_offset_out; + int8_t rx_buffer_offset_in; + public: + HardwareUsb(void); + void begin(); + uint8_t available(void); + uint8_t read(void); + void flush(void); + virtual void write(unsigned char); + using Print::write; +}; + +#endif //_HARDWAREUSB_H |