aboutsummaryrefslogtreecommitdiffstats
path: root/wirish/comm
diff options
context:
space:
mode:
authorPerry Hung <iperry@alum.mit.edu>2010-04-25 18:14:43 -0400
committerPerry Hung <iperry@alum.mit.edu>2010-04-25 18:14:43 -0400
commit4ccbeb08e64b6bc7cee1f54a04001eff778b4282 (patch)
tree05f71e01c98ac341c029b2a050b796f1be3de8b5 /wirish/comm
parentb41eb846ca60559cff242d0c550699eb8f309909 (diff)
parent62552a6f3f93223682f9df2df2614411f7e3d54c (diff)
downloadlibrambutan-4ccbeb08e64b6bc7cee1f54a04001eff778b4282.tar.gz
librambutan-4ccbeb08e64b6bc7cee1f54a04001eff778b4282.zip
Merge branch 'master' into spi
Diffstat (limited to 'wirish/comm')
-rw-r--r--wirish/comm/HardwareSPI.cpp138
-rw-r--r--wirish/comm/HardwareSPI.h57
-rw-r--r--wirish/comm/HardwareSerial.cpp102
-rw-r--r--wirish/comm/HardwareSerial.h53
-rw-r--r--wirish/comm/HardwareUsb.cpp102
-rw-r--r--wirish/comm/HardwareUsb.h54
6 files changed, 506 insertions, 0 deletions
diff --git a/wirish/comm/HardwareSPI.cpp b/wirish/comm/HardwareSPI.cpp
new file mode 100644
index 0000000..49dad71
--- /dev/null
+++ b/wirish/comm/HardwareSPI.cpp
@@ -0,0 +1,138 @@
+/* *****************************************************************************
+ * 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 HardwareSPI "wiring-like" api for SPI
+ */
+
+/* NOTES:
+ *
+ * Speeds:
+ * -----------------------------------
+ * Interface num: SPI1 SPI2
+ * Bus APB2 APB1
+ * -----------------------------------
+ * Prescaler Frequencies
+ * -----------------------------------
+ * 2: N/A 18 000 000
+ * 4: 18 000 000 9 000 000
+ * 8: 9 000 000 4 500 000
+ * 16: 4 500 000 2 250 000
+ * 32: 2 250 000 1 125 000
+ * 64: 1 125 000 562 500
+ * 128: 562 500 281 250
+ * 256: 281 250 140 625
+ *
+ * TODO: Do the complementary PWM outputs mess up SPI2?
+ * */
+
+#include "wirish.h"
+#include "spi.h"
+#include "HardwareSPI.h"
+
+static const uint32 prescaleFactors[MAX_SPI_FREQS] = {
+ SPI_PRESCALE_2, // SPI_18MHZ
+ SPI_PRESCALE_4, // SPI_9MHZ
+ SPI_PRESCALE_8, // SPI_4_5MHZ
+ SPI_PRESCALE_16, // SPI_2_25MHZ
+ SPI_PRESCALE_32, // SPI_1_125MHZ
+ SPI_PRESCALE_64, // SPI_562_500KHZ
+ SPI_PRESCALE_128, // SPI_281_250KHZ
+ SPI_PRESCALE_256, // SPI_140_625KHZ
+};
+
+/**
+ * @brief Initialize a SPI peripheral
+ * @param freq frequency to run at, must one of the following values:
+ * - SPI_18MHZ
+ * - SPI_9MHZ
+ * - SPI_4_5MHZ
+ * - SPI_2_25MHZ
+ * - SPI_1_125MHZ
+ * - SPI_562_500KHZ
+ * - SPI_281_250KHZ
+ * - SPI_140_625KHZ
+ * @param endianness endianness of the data frame, must be either LSBFIRST
+ * or MSBFIRST
+ * @param mode SPI standard CPOL and CPHA levels
+ */
+void HardwareSPI::begin(SPIFrequency freq, uint32 endianness, uint32 mode) {
+ uint32 spi_num = this->spi_num;
+ uint32 high_speed = 0;
+ uint32 index;
+ uint32 prescale;
+
+ if ((freq >= MAX_SPI_FREQS) ||
+ !((endianness == LSBFIRST) ||
+ (endianness == MSBFIRST)) ||
+ (mode >= 4)) {
+ return;
+ }
+
+ if (spi_num == 1) {
+ /* SPI1 is too fast for 140625 */
+ if (freq == SPI_140_625KHZ) {
+ return;
+ }
+
+ /* Turn off PWM on shared pins */
+ timers_disable_channel(3, 2);
+ timers_disable_channel(3, 1);
+ }
+
+ endianness = (endianness == LSBFIRST) ? SPI_LSBFIRST : SPI_MSBFIRST;
+ prescale = (spi_num == 1) ? prescaleFactors[freq + 1] : prescaleFactors[freq];
+
+ spi_init(spi_num, prescale, endianness, 0);
+}
+
+/**
+ * @brief Initialize a SPI peripheral with a default speed of 1.125 MHZ, MSBFIRST,
+ * mode 0
+ * @param mode SPI standard CPOL and CPHA levels
+ */
+void HardwareSPI::begin(void) {
+ begin(SPI_1_125MHZ, MSBFIRST, 0);
+}
+
+/**
+ * @brief send a byte out the spi peripheral
+ * @param data byte to send
+ */
+void HardwareSPI::send(uint8 data) {
+ spi_tx(this->spi_num, data);
+}
+
+
+/**
+ * @brief read a byte from the spi peripheral
+ * @return byte in the buffer
+ */
+uint8 HardwareSPI::recv(void) {
+ return spi_rx(this->spi_num);
+}
+
+HardwareSPI::HardwareSPI(uint32 spi_num) {
+ this->spi_num = spi_num;
+}
diff --git a/wirish/comm/HardwareSPI.h b/wirish/comm/HardwareSPI.h
new file mode 100644
index 0000000..b974334
--- /dev/null
+++ b/wirish/comm/HardwareSPI.h
@@ -0,0 +1,57 @@
+/* *****************************************************************************
+ * 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 HardwareSPI definitions
+ */
+
+#ifndef _HARDWARESPI_H_
+#define _HARDWARESPI_H_
+
+typedef enum SPIFrequency {
+ SPI_18MHZ = 0,
+ SPI_9MHZ = 1,
+ SPI_4_5MHZ = 2,
+ SPI_2_25MHZ = 3,
+ SPI_1_125MHZ = 4,
+ SPI_562_500KHZ = 5,
+ SPI_281_250KHZ = 6,
+ SPI_140_625KHZ = 7,
+ MAX_SPI_FREQS = 8,
+} SPIFrequency;
+
+class HardwareSPI {
+ private:
+ uint32 spi_num;
+
+ public:
+ HardwareSPI(uint32 spi_num);
+ void begin(void);
+ void begin(SPIFrequency freq, uint32 endianness, uint32 mode);
+ void send(uint8 data);
+ uint8 recv(void);
+};
+
+#endif
+
diff --git a/wirish/comm/HardwareSerial.cpp b/wirish/comm/HardwareSerial.cpp
new file mode 100644
index 0000000..3e855ea
--- /dev/null
+++ b/wirish/comm/HardwareSerial.cpp
@@ -0,0 +1,102 @@
+/* *****************************************************************************
+ * 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.
+ * ****************************************************************************/
+
+/**
+ * @file HardwareSerial.cpp
+ *
+ * @brief Wiring-like serial api
+ */
+
+#include "wirish.h"
+#include "HardwareSerial.h"
+#include "usart.h"
+#include "gpio.h"
+#include "timers.h"
+
+#define USART1_TX_PORT GPIOA_BASE
+#define USART1_TX_PIN 9
+#define USART1_RX_PORT GPIOA_BASE
+#define USART1_RX_PIN 10
+
+#define USART2_TX_PORT GPIOA_BASE
+#define USART2_TX_PIN 2
+#define USART2_RX_PORT GPIOA_BASE
+#define USART2_RX_PIN 3
+
+#define USART3_TX_PORT GPIOB_BASE
+#define USART3_TX_PIN 10
+#define USART3_RX_PORT GPIOB_BASE
+#define USART3_RX_PIN 11
+
+HardwareSerial::HardwareSerial(uint8 usartNum) {
+ ASSERT(usartNum == 1 ||
+ usartNum == 2 ||
+ usartNum == 3);
+ this->usartNum = usartNum;
+}
+
+uint8 HardwareSerial::read(void) {
+ return usart_getc(usartNum);
+}
+
+uint32 HardwareSerial::available(void) {
+
+ return usart_data_available(usartNum);
+}
+
+void HardwareSerial::write(unsigned char ch) {
+ usart_putc(usartNum, ch);
+}
+
+void HardwareSerial::begin(uint32 baud) {
+ ASSERT(!(baud > USART_MAX_BAUD));
+
+ /* Set appropriate pin modes */
+ switch (usartNum) {
+ case 1:
+ gpio_set_mode(USART1_TX_PORT, USART1_TX_PIN, GPIO_MODE_AF_OUTPUT_PP);
+ gpio_set_mode(USART1_RX_PORT, USART1_RX_PIN, GPIO_MODE_INPUT_FLOATING);
+ /* Turn off any pwm */
+ timers_disable_channel(1, 2);
+ break;
+ case 2:
+ gpio_set_mode(USART2_TX_PORT, USART2_TX_PIN, GPIO_MODE_AF_OUTPUT_PP);
+ gpio_set_mode(USART2_RX_PORT, USART2_RX_PIN, GPIO_MODE_INPUT_FLOATING);
+ /* Turn off any pwm */
+ timers_disable_channel(2, 3);
+ break;
+ case 3:
+ gpio_set_mode(USART3_TX_PORT, USART3_TX_PIN, GPIO_MODE_AF_OUTPUT_PP);
+ gpio_set_mode(USART3_RX_PORT, USART3_RX_PIN, GPIO_MODE_INPUT_FLOATING);
+ break;
+ default:
+ ASSERT(0);
+ }
+
+ usart_init(usartNum, baud);
+}
+
+HardwareSerial Serial1(1);
+HardwareSerial Serial2(2);
+HardwareSerial Serial3(3);
diff --git a/wirish/comm/HardwareSerial.h b/wirish/comm/HardwareSerial.h
new file mode 100644
index 0000000..a488244
--- /dev/null
+++ b/wirish/comm/HardwareSerial.h
@@ -0,0 +1,53 @@
+/* *****************************************************************************
+ * 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.
+ * ****************************************************************************/
+
+/**
+ * @file HardwareSerial.h
+ *
+ * @brief
+ */
+
+#ifndef _HARDWARESERIAL_H_
+#define _HARDWARESERIAL_H_
+
+#include "Print.h"
+
+class HardwareSerial : public Print {
+ private:
+ uint8 usartNum;
+ public:
+ HardwareSerial(uint8);
+ void begin(uint32);
+ uint32 available(void);
+ uint8 read(void);
+ void flush(void);
+ virtual void write(unsigned char);
+ using Print::write;
+};
+
+extern HardwareSerial Serial1;
+extern HardwareSerial Serial2;
+extern HardwareSerial Serial3;
+#endif
+
diff --git a/wirish/comm/HardwareUsb.cpp b/wirish/comm/HardwareUsb.cpp
new file mode 100644
index 0000000..4398e96
--- /dev/null
+++ b/wirish/comm/HardwareUsb.cpp
@@ -0,0 +1,102 @@
+/* *****************************************************************************
+ * 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
new file mode 100644
index 0000000..ee13f40
--- /dev/null
+++ b/wirish/comm/HardwareUsb.h
@@ -0,0 +1,54 @@
+/* *****************************************************************************
+ * 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