diff options
author | Perry Hung <iperry@alum.mit.edu> | 2010-04-25 18:08:56 -0400 |
---|---|---|
committer | Perry Hung <iperry@alum.mit.edu> | 2010-04-25 18:08:56 -0400 |
commit | 62552a6f3f93223682f9df2df2614411f7e3d54c (patch) | |
tree | 05f71e01c98ac341c029b2a050b796f1be3de8b5 /wirish | |
parent | 7ea059f945003c90ab56b1e6f762df9ae29ddf3c (diff) | |
download | librambutan-62552a6f3f93223682f9df2df2614411f7e3d54c.tar.gz librambutan-62552a6f3f93223682f9df2df2614411f7e3d54c.zip |
Refactoring:
The 'core' directory has now been renamed to 'wirish.' Wirish is our
version of the Arduino Wiring language.
Diffstat (limited to 'wirish')
-rw-r--r-- | wirish/Print.cpp | 211 | ||||
-rw-r--r-- | wirish/Print.h | 61 | ||||
-rw-r--r-- | wirish/WProgram.h | 32 | ||||
-rw-r--r-- | wirish/bits.h | 56 | ||||
-rw-r--r-- | wirish/comm/HardwareSPI.cpp | 138 | ||||
-rw-r--r-- | wirish/comm/HardwareSPI.h | 57 | ||||
-rw-r--r-- | wirish/comm/HardwareSerial.cpp | 102 | ||||
-rw-r--r-- | wirish/comm/HardwareSerial.h | 53 | ||||
-rw-r--r-- | wirish/comm/HardwareUsb.cpp | 102 | ||||
-rw-r--r-- | wirish/comm/HardwareUsb.h | 54 | ||||
-rw-r--r-- | wirish/ext_interrupts.c | 109 | ||||
-rw-r--r-- | wirish/ext_interrupts.h | 60 | ||||
-rw-r--r-- | wirish/io.h | 145 | ||||
-rw-r--r-- | wirish/main.cxx | 52 | ||||
-rw-r--r-- | wirish/pwm.c | 51 | ||||
-rw-r--r-- | wirish/pwm.h | 46 | ||||
-rw-r--r-- | wirish/time.c | 77 | ||||
-rw-r--r-- | wirish/time.h | 69 | ||||
-rw-r--r-- | wirish/wirish.c | 47 | ||||
-rw-r--r-- | wirish/wirish.h | 85 | ||||
-rw-r--r-- | wirish/wirish_analog.c | 42 | ||||
-rw-r--r-- | wirish/wirish_digital.c | 143 | ||||
-rw-r--r-- | wirish/wirish_math.cpp | 54 | ||||
-rw-r--r-- | wirish/wirish_math.h | 56 | ||||
-rw-r--r-- | wirish/wirish_shift.c | 40 |
25 files changed, 1942 insertions, 0 deletions
diff --git a/wirish/Print.cpp b/wirish/Print.cpp new file mode 100644 index 0000000..5a1bc93 --- /dev/null +++ b/wirish/Print.cpp @@ -0,0 +1,211 @@ +/* + Print.cpp - Base class that provides print() and println() + Copyright (c) 2008 David A. Mellis. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Modified 23 November 2006 by David A. Mellis + */ + +#include "wirish.h" +#include "Print.h" + +// Public Methods ////////////////////////////////////////////////////////////// + +/* default implementation: may be overridden */ +void Print::write(const char *str) +{ + while (*str) + write(*str++); +} + +/* default implementation: may be overridden */ +void Print::write(const uint8 *buffer, uint32 size) +{ + while (size--) + write(*buffer++); +} + +void Print::print(uint8 b) +{ + this->write(b); +} + +void Print::print(char c) +{ + print((byte) c); +} + +void Print::print(const char str[]) +{ + write(str); +} + +void Print::print(int n) +{ + print((long) n); +} + +void Print::print(unsigned int n) +{ + print((unsigned long) n); +} + +void Print::print(long n) +{ + if (n < 0) { + print('-'); + n = -n; + } + printNumber(n, 10); +} + +void Print::print(unsigned long n) +{ + printNumber(n, 10); +} + +void Print::print(long n, int base) +{ + if (base == 0) + print((char) n); + else if (base == 10) + print(n); + else + printNumber(n, base); +} + +void Print::print(double n) +{ + printFloat(n, 2); +} + +void Print::println(void) +{ + print('\r'); + print('\n'); +} + +void Print::println(char c) +{ + print(c); + println(); +} + +void Print::println(const char c[]) +{ + print(c); + println(); +} + +void Print::println(uint8 b) +{ + print(b); + println(); +} + +void Print::println(int n) +{ + print(n); + println(); +} + +void Print::println(unsigned int n) +{ + print(n); + println(); +} + +void Print::println(long n) +{ + print(n); + println(); +} + +void Print::println(unsigned long n) +{ + print(n); + println(); +} + +void Print::println(long n, int base) +{ + print(n, base); + println(); +} + +void Print::println(double n) +{ + print(n); + println(); +} + +// Private Methods ///////////////////////////////////////////////////////////// + +void Print::printNumber(unsigned long n, uint8 base) +{ + unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars. + unsigned long i = 0; + + if (n == 0) { + print('0'); + return; + } + + while (n > 0) { + buf[i++] = n % base; + n /= base; + } + + for (; i > 0; i--) + print((char) (buf[i - 1] < 10 ? + '0' + buf[i - 1] : + 'A' + buf[i - 1] - 10)); +} + +void Print::printFloat(double number, uint8 digits) +{ + // Handle negative numbers + if (number < 0.0) + { + print('-'); + number = -number; + } + + // Round correctly so that print(1.999, 2) prints as "2.00" + double rounding = 0.5; + for (uint8 i=0; i<digits; ++i) + rounding /= 10.0; + + number += rounding; + + // Extract the integer part of the number and print it + unsigned long int_part = (unsigned long)number; + double remainder = number - (double)int_part; + print(int_part); + + // Print the decimal point, but only if there are digits beyond + if (digits > 0) + print("."); + + // Extract digits from the remainder one at a time + while (digits-- > 0) + { + remainder *= 10.0; + int toPrint = int(remainder); + print(toPrint); + remainder -= toPrint; + } +} diff --git a/wirish/Print.h b/wirish/Print.h new file mode 100644 index 0000000..b2f8647 --- /dev/null +++ b/wirish/Print.h @@ -0,0 +1,61 @@ +/* + Print.h - Base class that provides print() and println() + Copyright (c) 2008 David A. Mellis. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef Print_h +#define Print_h + +#include <stdio.h> // for size_t + +#define DEC 10 +#define HEX 16 +#define OCT 8 +#define BIN 2 +#define BYTE 0 + +class Print +{ + private: + void printNumber(unsigned long, uint8); + void printFloat(double, uint8); + public: + virtual void write(uint8) = 0; + virtual void write(const char *str); + virtual void write(const uint8 *buffer, uint32 size); + void print(char); + void print(const char[]); + void print(uint8); + void print(int); + void print(unsigned int); + void print(long); + void print(unsigned long); + void print(long, int); + void print(double); + void println(void); + void println(char); + void println(const char[]); + void println(uint8); + void println(int); + void println(unsigned int); + void println(long); + void println(unsigned long); + void println(long, int); + void println(double); +}; + +#endif diff --git a/wirish/WProgram.h b/wirish/WProgram.h new file mode 100644 index 0000000..7653bb6 --- /dev/null +++ b/wirish/WProgram.h @@ -0,0 +1,32 @@ +/* ***************************************************************************** + * 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 "wiring.h" +#include "HardwareSerial.h" +#include "HardwareUsb.h" +#include "math.h" +#include "usb.h" + +void setup(); +void loop(); diff --git a/wirish/bits.h b/wirish/bits.h new file mode 100644 index 0000000..7ebea80 --- /dev/null +++ b/wirish/bits.h @@ -0,0 +1,56 @@ +/*
+ Part of Arduino - http://www.arduino.cc/
+
+ Copyright (c) 2005-2006 David A. Mellis
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General
+ Public License along with this library; if not, write to the
+ Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ Boston, MA 02111-1307 USA
+*/
+
+// BIT DEFINITION
+
+#define BIT0 0x00000001
+#define BIT1 0x00000002
+#define BIT2 0x00000004
+#define BIT3 0x00000008
+#define BIT4 0x00000010
+#define BIT5 0x00000020
+#define BIT6 0x00000040
+#define BIT7 0x00000080
+#define BIT8 0x00000100
+#define BIT9 0x00000200
+#define BIT10 0x00000400
+#define BIT11 0x00000800
+#define BIT12 0x00001000
+#define BIT13 0x00002000
+#define BIT14 0x00004000
+#define BIT15 0x00008000
+#define BIT16 0x00010000
+#define BIT17 0x00020000
+#define BIT18 0x00040000
+#define BIT19 0x00080000
+#define BIT20 0x00100000
+#define BIT21 0x00200000
+#define BIT22 0x00400000
+#define BIT23 0x00800000
+#define BIT24 0x01000000
+#define BIT25 0x02000000
+#define BIT26 0x04000000
+#define BIT27 0x08000000
+#define BIT28 0x10000000
+#define BIT29 0x20000000
+#define BIT30 0x40000000
+#define BIT31 0x80000000
+
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 diff --git a/wirish/ext_interrupts.c b/wirish/ext_interrupts.c new file mode 100644 index 0000000..54b8be9 --- /dev/null +++ b/wirish/ext_interrupts.c @@ -0,0 +1,109 @@ +/* ***************************************************************************** + * 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 ext_interrupts.c + * + * @brief Wiring-like interface for external interrupts + */ + +#include "wirish.h" +#include "exti.h" +#include "ext_interrupts.h" + +typedef struct ExtiInfo { + uint8 channel; + uint8 port; +} ExtiInfo; + +static ExtiInfo PIN_TO_EXTI_CHANNEL[NR_MAPLE_PINS] = { + {EXTI3, EXTI_CONFIG_PORTA}, // D0/PA3 + {EXTI2, EXTI_CONFIG_PORTA}, // D1/PA2 + {EXTI0, EXTI_CONFIG_PORTA}, // D2/PA0 + {EXTI1, EXTI_CONFIG_PORTA}, // D3/PA1 + {EXTI5, EXTI_CONFIG_PORTB}, // D4/PB5 + {EXTI6, EXTI_CONFIG_PORTB}, // D5/PB6 + {EXTI8, EXTI_CONFIG_PORTA}, // D6/PA8 + {EXTI9, EXTI_CONFIG_PORTA}, // D7/PA9 + {EXTI10, EXTI_CONFIG_PORTA}, // D8/PA10 + {EXTI7, EXTI_CONFIG_PORTB}, // D9/PB7 + {EXTI4, EXTI_CONFIG_PORTA}, // D10/PA4 + {EXTI7, EXTI_CONFIG_PORTA}, // D11/PA7 + {EXTI6, EXTI_CONFIG_PORTA}, // D12/PA6 + {EXTI5, EXTI_CONFIG_PORTA}, // D13/PA5 +}; + + +/** + * @brief Attach an interrupt handler to be triggered on a given + * transition on the pin. Runs in interrupt context + * + * @param[in] pin Maple pin number + * @param[in] handler Function to run upon external interrupt trigger. + * @param[in] mode Type of transition to trigger on, eg falling, rising, etc. + * + * @sideeffect Registers a handler + */ +int attachInterrupt(uint8 pin, voidFuncPtr handler, ExtInterruptTriggerMode mode) { + uint8 outMode; + /* Parameter checking */ + if (pin >= NR_MAPLE_PINS) { + return EXT_INTERRUPT_INVALID_PIN; + } + + if (!handler) { + ASSERT(0); + return EXT_INTERRUPT_INVALID_FUNCTION; + } + + switch (mode) { + case RISING: + outMode = EXTI_RISING; + break; + case FALLING: + outMode = EXTI_FALLING; + break; + case CHANGE: + outMode = EXTI_RISING_FALLING; + break; + default: + ASSERT(0); + return EXT_INTERRUPT_INVALID_MODE;; + } + + exti_attach_interrupt(PIN_TO_EXTI_CHANNEL[pin].channel, + PIN_TO_EXTI_CHANNEL[pin].port, + handler, mode); + + return 0; +} + +int detachInterrupt(uint8 pin) { + if (!(pin < NR_MAPLE_PINS)) { + return EXT_INTERRUPT_INVALID_PIN; + } + + exti_detach_interrupt(PIN_TO_EXTI_CHANNEL[pin].channel); +} + diff --git a/wirish/ext_interrupts.h b/wirish/ext_interrupts.h new file mode 100644 index 0000000..fc69a15 --- /dev/null +++ b/wirish/ext_interrupts.h @@ -0,0 +1,60 @@ +/* ***************************************************************************** + * 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 ext_interrupts.h + * + * @brief External interrupt wiring prototypes and types + */ + +#ifndef _EXT_INTERRUPTS_H_ +#define _EXT_INTERRUPTS_H_ + +typedef enum ExtInterruptTriggerMode { + RISING, + FALLING, + CHANGE +} ExtInterruptTriggerMode; + + +enum ExtInterruptError { + EXT_INTERRUPT_INVALID_PIN = (-1), + EXT_INTERRUPT_INVALID_FUNCTION = (-2), + EXT_INTERRUPT_INVALID_MODE = (-3), +}; + +#ifdef __cplusplus +extern "C"{ +#endif + +int attachInterrupt(uint8 pin, voidFuncPtr, ExtInterruptTriggerMode mode); +int detachInterrupt(uint8 pin); + +#ifdef __cplusplus +} +#endif + + +#endif + diff --git a/wirish/io.h b/wirish/io.h new file mode 100644 index 0000000..fff551c --- /dev/null +++ b/wirish/io.h @@ -0,0 +1,145 @@ +/* ***************************************************************************** + * 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 io.h + * + * @brief + */ + +#ifndef _IO_H +#define _IO_H + +#include "gpio.h" +#include "adc.h" + +#ifdef __cplusplus +extern "C"{ +#endif + +/* stash these here for now */ +#define D0 0 +#define D1 1 +#define D2 2 +#define D3 3 +#define D4 4 +#define D5 5 +#define D6 6 +#define D7 7 +#define D8 8 +#define D9 9 +#define D10 10 +#define D11 11 +#define D12 12 +#define D13 13 +#define D14 14 +#define D15 15 +#define D16 16 +#define D16 16 +#define D17 17 +#define D18 18 +#define D19 19 +#define D20 20 +#define D21 21 +#define D22 22 +#define D23 23 +#define D24 24 +#define D25 25 +#define D26 26 +#define D27 27 +#define D28 28 +#define D29 29 +#define D30 30 +#define D31 31 +#define D32 32 +#define D33 33 +#define D34 34 +#define D35 35 +#define D36 36 +#define D37 37 +#define D38 38 +#define D39 39 + +typedef enum WiringPinMode { + OUTPUT, + OUTPUT_OPEN_DRAIN, + INPUT, + INPUT_ANALOG, + INPUT_PULLUP, + INPUT_PULLDOWN, + INPUT_FLOATING, + PWM +} WiringPinMode; + +typedef struct PinMapping { + GPIO_Port *port; + uint32 pin; + uint32 adc; + TimerCCR timer_channel; +} PinMapping; + +#define ADC_INVALID 0xFFFFFFFF +#define TIMER_INVALID (TimerCCR)0xFFFFFFFF + +/* Set pin to mode + * pinMode(pin, mode): + * pin -> {0-38, D0-D39, A0-16} + * mode -> { + * INPUT/INPUT_DIGITAL + * INPUT_PULLUP + * INPUT_PULLDOWN + * INPUT_ANALOG + * OUTPUT/OUTPUT_PP + * OUTPUT_OPEN_DRAIN + * } + */ +void pinMode(uint8, uint8); + +/* + * Writes VALUE to digital pin[0-38] + * digitalWrite(pin, value): + * pin -> {0-38, D0-D39, A0-16} + * value -> LOW, HIGH; +*/ +void digitalWrite(uint8, uint8); + +/* Read a digital value from pin, the pin mode must be set to + * {INPUT, INPUT_PULLUP, INPUT_PULLDOWN} + * digitalRead(pin) + * pin -> {0-38, D0-D39, A0-16} + */ +uint32 digitalRead(uint8); + +/* Read an analog value from pin, the pin mode must be set + * to INPUT_ANALOG + * analogRead(pin) + * pin -> {A0-A16} + * */ +uint32 analogRead(uint8); + +#ifdef __cplusplus +} // extern "C" +#endif +#endif + diff --git a/wirish/main.cxx b/wirish/main.cxx new file mode 100644 index 0000000..440d464 --- /dev/null +++ b/wirish/main.cxx @@ -0,0 +1,52 @@ +/* *****************************************************************************
+ * 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.
+ * ****************************************************************************/
+
+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)
+ ;
+}
+
+/* Implemented:
+ * void pinMode(pin, mode)
+ * void digitalWrite(pin, value)
+ * uint32_t digitalRead(pin)
+ * uint32_t analogRead(pin)
+ * void randomSeed(seed)
+ * long random(max)
+ * long random(min, max)
+ * */
diff --git a/wirish/pwm.c b/wirish/pwm.c new file mode 100644 index 0000000..40715b5 --- /dev/null +++ b/wirish/pwm.c @@ -0,0 +1,51 @@ +/* ***************************************************************************** + * 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 + */ + +#include "wirish.h" +#include "timers.h" +#include "io.h" +#include "pwm.h" + +extern const PinMapping PIN_MAP[NR_MAPLE_PINS]; + +void pwmWrite(uint8 pin, uint16 duty_cycle) { + TimerCCR ccr; + + if (pin >= NR_MAPLE_PINS) { + return; + } + + ccr = PIN_MAP[pin].timer_channel; + + if (ccr == TIMER_INVALID) + return; + + timer_pwm_write_ccr(ccr, duty_cycle); +} + + diff --git a/wirish/pwm.h b/wirish/pwm.h new file mode 100644 index 0000000..4f69751 --- /dev/null +++ b/wirish/pwm.h @@ -0,0 +1,46 @@ +/* ***************************************************************************** + * 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 pwm.h + * + * @brief + */ + +#ifndef _PWM_H +#define _PWM_H + +#ifdef __cplusplus +extern "C"{ +#endif + +void pwmWrite(uint8, uint16); + +#ifdef __cplusplus +} +#endif + + +#endif + diff --git a/wirish/time.c b/wirish/time.c new file mode 100644 index 0000000..69e962c --- /dev/null +++ b/wirish/time.c @@ -0,0 +1,77 @@ +/* ***************************************************************************** + * 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 time.c + * + * @brief + */ + +#include "libmaple.h" +#include "systick.h" +#include "time.h" + +#define CYCLES_PER_MICROSECOND 72 +#define FUDGE 42 + +extern volatile uint32 systick_timer_millis; + +uint32 millis() { + unsigned long m; + m = systick_timer_millis; + return m; +} + +void delay(unsigned long ms) +{ + unsigned long start = millis(); + + while (millis() - start <= ms) + ; +} + + + +#if 1 +void delayMicroseconds(uint32 us) { + uint32 target; + uint32 last, cur, count; + /* fudge factor hacky hack hack for function overhead */ + target = us * CYCLES_PER_MICROSECOND - FUDGE; + + /* Get current count */ + last = systick_get_count(); + cur = systick_get_count(); + count = last; + while ((count-cur) <= target) { + cur = systick_get_count(); + + /* check for overflow */ + if (cur > last) { + count += MAPLE_RELOAD_VAL; + } + last = cur; + } +} +#endif diff --git a/wirish/time.h b/wirish/time.h new file mode 100644 index 0000000..9e5536b --- /dev/null +++ b/wirish/time.h @@ -0,0 +1,69 @@ +/* ***************************************************************************** + * 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 time.h + * + * @brief + */ + +#ifndef _TIME_H +#define _TIME_H + +#ifdef __cplusplus +extern "C"{ +#endif +/* Returns time since boot in milliseconds */ +uint32 millis(void); + +/* Time in microseconds since boot */ +uint32 micros(void); + +/* Delay for ms milliseconds */ +void delay(unsigned long ms); + +/* Delay for us microseconds */ +void delayMicroseconds(uint32 us); + +#if 0 +static inline void delay_us(uint32 us) { + us *= 12; + asm volatile("mov r0, %[us] \n\t" + "subs r0, #2 \n\t" +"1: \n\t" + "subs r0, r0, #1 \n\t" + "bne 1b" + : + : [us] "r" (us) + : "r0", "cc"); + +} +#endif +#ifdef __cplusplus +} // extern "C" +#endif + + +#endif + diff --git a/wirish/wirish.c b/wirish/wirish.c new file mode 100644 index 0000000..5102124 --- /dev/null +++ b/wirish/wirish.c @@ -0,0 +1,47 @@ +/* ***************************************************************************** + * 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 Maple board bring up + */ + +#include "wirish.h" +#include "rcc.h" +#include "systick.h" +#include "gpio.h" +#include "nvic.h" + +void init(void) { + rcc_init(); + nvic_init(); + systick_init(); + gpio_init(); + adc_init(); + timer_init(1, 1); + timer_init(2, 1); + timer_init(3, 1); + timer_init(4, 1); +} + + diff --git a/wirish/wirish.h b/wirish/wirish.h new file mode 100644 index 0000000..5129c26 --- /dev/null +++ b/wirish/wirish.h @@ -0,0 +1,85 @@ +/* ***************************************************************************** + * 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 Main include file for the Wirish core. + * + * Includes various Arduino wiring macros and bit defines + */ + + +#ifndef _WIRISH_H_ +#define _WIRISH_H_ + +#include "libmaple.h" +#include "timers.h" +#include "io.h" +#include "bits.h" +#include "time.h" +#include "pwm.h" +#include "ext_interrupts.h" + +#ifdef __cplusplus +extern "C"{ +#endif + +#define MAPLE 1 +#define NR_MAPLE_PINS 39 // temporary + +/* Arduino wiring macros and bit defines */ +#define HIGH 0x1 +#define LOW 0x0 + +#define true 0x1 +#define false 0x0 + +#define LSBFIRST 0 +#define MSBFIRST 1 + +#define USER_ADDR_ROM 0x08005000 +#define USER_ADDR_RAM 0x20000C00 + +#define lowByte(w) ((w) & 0xff) +#define highByte(w) ((w) >> 8) +#define bitRead(value, bit) (((value) >> (bit)) & 0x01) +#define bitSet(value, bit) ((value) |= (1UL << (bit))) +#define bitClear(value, bit) ((value) &= ~(1UL << (bit))) +#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : \ + bitClear(value, bit)) +#define bit(b) (1UL << (b)) + +typedef uint8 boolean; +typedef uint8 byte; + +void init(void); +void shiftOut(uint8 dataPin, uint8 clockPin, uint8 bitOrder, byte val); + +#ifdef __cplusplus +} // extern "C" +#endif + + + + +#endif + diff --git a/wirish/wirish_analog.c b/wirish/wirish_analog.c new file mode 100644 index 0000000..f4c1204 --- /dev/null +++ b/wirish/wirish_analog.c @@ -0,0 +1,42 @@ +/* ***************************************************************************** + * 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 + */ + +#include "libmaple.h" +#include "wirish.h" +#include "io.h" + +extern const PinMapping PIN_MAP[NR_MAPLE_PINS]; + +/* Assumes that the ADC has been initialized and + * that the pin is set to ANALOG_INPUT */ +uint32 analogRead(uint8 pin) { + if (pin >= NR_ANALOG_PINS) + return 0; + + return adc_read(PIN_MAP[pin].adc); +} diff --git a/wirish/wirish_digital.c b/wirish/wirish_digital.c new file mode 100644 index 0000000..33217b6 --- /dev/null +++ b/wirish/wirish_digital.c @@ -0,0 +1,143 @@ +/* ***************************************************************************** + * 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 + */ + +#include "wirish.h" +#include "io.h" + +#define ADC0 0 +#define ADC1 1 +#define ADC2 2 +#define ADC3 3 +#define ADC4 4 +#define ADC5 5 +#define ADC6 6 +#define ADC7 7 +#define ADC8 8 +#define ADC9 9 +#define ADC10 10 +#define ADC11 11 +#define ADC12 12 +#define ADC13 13 +#define ADC14 14 +#define ADC15 15 +#define ADC16 16 + +const PinMapping PIN_MAP[NR_MAPLE_PINS] = { + {GPIOA_BASE, 3, ADC3, TIMER2_CH4_CCR}, // D0/PA3 + {GPIOA_BASE, 2, ADC2, TIMER2_CH3_CCR}, // D1/PA2 + {GPIOA_BASE, 0, ADC0, TIMER2_CH1_CCR}, // D2/PA0 + {GPIOA_BASE, 1, ADC1, TIMER2_CH2_CCR}, // D3/PA1 + {GPIOB_BASE, 5, ADC_INVALID, TIMER_INVALID}, // D4/PB5 + {GPIOB_BASE, 6, ADC_INVALID, TIMER4_CH1_CCR}, // D5/PB6 + {GPIOA_BASE, 8, ADC_INVALID, TIMER1_CH1_CCR}, // D6/PA8 + {GPIOA_BASE, 9, ADC_INVALID, TIMER1_CH2_CCR}, // D7/PA9 + {GPIOA_BASE, 10, ADC_INVALID, TIMER1_CH3_CCR}, // D8/PA10 + {GPIOB_BASE, 7, ADC_INVALID, TIMER4_CH2_CCR}, // D9/PB7 + {GPIOA_BASE, 4, ADC4, TIMER_INVALID}, // D10/PA4 + {GPIOA_BASE, 7, ADC7, TIMER3_CH2_CCR}, // D11/PA7 + {GPIOA_BASE, 6, ADC6, TIMER3_CH1_CCR}, // D12/PA6 + {GPIOA_BASE, 5, ADC5, TIMER_INVALID}, // D13/PA5 + {GPIOB_BASE, 8, ADC_INVALID, TIMER4_CH3_CCR}, // D14/PB8 + /* Little header */ + {GPIOC_BASE, 0, ADC10, TIMER_INVALID}, // D15/PC0 + {GPIOC_BASE, 1, ADC11, TIMER_INVALID}, // D16/PC1 + {GPIOC_BASE, 2, ADC12, TIMER_INVALID}, // D17/PC2 + {GPIOC_BASE, 3, ADC13, TIMER_INVALID}, // D18/PC3 + {GPIOC_BASE, 4, ADC14, TIMER_INVALID}, // D19/PC4 + {GPIOC_BASE, 5, ADC15, TIMER_INVALID}, // D20/PC5 + /* External header */ + {GPIOC_BASE, 13, ADC_INVALID, TIMER_INVALID}, // D21/PC13 + {GPIOC_BASE, 14, ADC_INVALID, TIMER_INVALID}, // D22/PC14 + {GPIOC_BASE, 15, ADC_INVALID, TIMER_INVALID}, // D23/PC15 + {GPIOB_BASE, 9, ADC_INVALID, TIMER4_CH4_CCR}, // D24/PB9 + {GPIOD_BASE, 2, ADC_INVALID, TIMER_INVALID}, // D25/PD2 + {GPIOC_BASE, 10, ADC_INVALID, TIMER_INVALID}, // D26/PC10 + {GPIOB_BASE, 0, ADC8, TIMER3_CH3_CCR}, // D27/PB0 + {GPIOB_BASE, 1, ADC9, TIMER3_CH4_CCR}, // D28/PB1 + {GPIOB_BASE, 10, ADC_INVALID, TIMER_INVALID}, // D29/PB10 + {GPIOB_BASE, 11, ADC_INVALID, TIMER_INVALID}, // D30/PB11 + {GPIOB_BASE, 12, ADC_INVALID, TIMER_INVALID}, // D31/PB12 + {GPIOB_BASE, 13, ADC_INVALID, TIMER_INVALID}, // D32/PB13 + {GPIOB_BASE, 14, ADC_INVALID, TIMER_INVALID}, // D33/PB14 + {GPIOB_BASE, 15, ADC_INVALID, TIMER_INVALID}, // D34/PB15 + {GPIOC_BASE, 6, ADC_INVALID, TIMER_INVALID}, // D35/PC6 + {GPIOC_BASE, 7, ADC_INVALID, TIMER_INVALID}, // D36/PC7 + {GPIOC_BASE, 8, ADC_INVALID, TIMER_INVALID}, // D37/PC8 + {GPIOC_BASE, 9, ADC_INVALID, TIMER_INVALID} // D38/PC9 +}; + +void pinMode(uint8 pin, WiringPinMode mode) { + uint8 outputMode; + + if (pin >= NR_MAPLE_PINS) + return; + + switch(mode) { + case OUTPUT: + outputMode = GPIO_MODE_OUTPUT_PP; + break; + case OUTPUT_OPEN_DRAIN: + outputMode = GPIO_MODE_OUTPUT_OD; + break; + case INPUT: + case INPUT_FLOATING: + outputMode = GPIO_MODE_INPUT_FLOATING; + break; + case INPUT_ANALOG: + outputMode = GPIO_MODE_INPUT_ANALOG; + break; + case INPUT_PULLUP: + outputMode = GPIO_MODE_INPUT_PU; + break; + case INPUT_PULLDOWN: + outputMode = GPIO_MODE_INPUT_PD; + break; + case PWM: + outputMode = GPIO_MODE_AF_OUTPUT_PP; + break; + default: + ASSERT(0); + return; + } + + gpio_set_mode(PIN_MAP[pin].port, PIN_MAP[pin].pin, outputMode); +} + + +uint32 digitalRead(uint8 pin) { + if (pin >= NR_MAPLE_PINS) + return 0; + return gpio_read_bit(PIN_MAP[pin].port, PIN_MAP[pin].pin); +} + +void digitalWrite(uint8 pin, uint8 val) { + if (pin >= NR_MAPLE_PINS) + return; + + gpio_write_bit(PIN_MAP[pin].port, PIN_MAP[pin].pin, val); +} diff --git a/wirish/wirish_math.cpp b/wirish/wirish_math.cpp new file mode 100644 index 0000000..0d907c4 --- /dev/null +++ b/wirish/wirish_math.cpp @@ -0,0 +1,54 @@ +/* + Modified by LeafLabs, LLC. + + Part of the Wiring project - http://wiring.org.co + Copyright (c) 2004-06 Hernando Barragan + Modified 13 August 2006, David A. Mellis for Arduino - http://www.arduino.cc/ + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General + Public License along with this library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place, Suite 330, + Boston, MA 02111-1307 USA + + $Id$ +*/ + +#include <stdlib.h> +#include "math.h" + +void randomSeed(unsigned int seed) { + if (seed != 0) { + srand(seed); + } +} + +long random(long howbig) { + if (howbig == 0) { + return 0; + } + return rand() % howbig; +} + +long random(long howsmall, long howbig) { + if (howsmall >= howbig) { + return howsmall; + } + long diff = howbig - howsmall; + return random(diff) + howsmall; +} + +long map(long x, long in_min, long in_max, long out_min, long out_max) { + return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; +} + + diff --git a/wirish/wirish_math.h b/wirish/wirish_math.h new file mode 100644 index 0000000..19f3892 --- /dev/null +++ b/wirish/wirish_math.h @@ -0,0 +1,56 @@ +/* ***************************************************************************** + * 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. + * ****************************************************************************/ + +#ifndef _WIRING_MATH_H_ +#define _WIRING_MATH_H_ + +#include <math.h> + +void randomSeed(unsigned int); +long random(long); +long random(long, long); +long map(long, long, long, long, long); + +#define PI 3.1415926535897932384626433832795 +#define HALF_PI 1.5707963267948966192313216916398 +#define TWO_PI 6.283185307179586476925286766559 +#define DEG_TO_RAD 0.017453292519943295769236907684886 +#define RAD_TO_DEG 57.295779513082320876798154814105 + +#define min(a,b) ((a)<(b)?(a):(b)) +#define max(a,b) ((a)>(b)?(a):(b)) +#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt))) +#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5)) +#define radians(deg) ((deg)*DEG_TO_RAD) +#define degrees(rad) ((rad)*RAD_TO_DEG) +#define sq(x) ((x)*(x)) + +/* undefine stdlib's abs if encountered */ +#ifdef abs +#undef abs +#endif +#define abs(x) (((x) > 0) ? (x) : -(unsigned)(x)) + +#endif + diff --git a/wirish/wirish_shift.c b/wirish/wirish_shift.c new file mode 100644 index 0000000..884b560 --- /dev/null +++ b/wirish/wirish_shift.c @@ -0,0 +1,40 @@ +/* + wiring_shift.c - shiftOut() function + Part of Arduino - http://www.arduino.cc/ + + Copyright (c) 2005-2006 David A. Mellis + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General + Public License along with this library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place, Suite 330, + Boston, MA 02111-1307 USA + + $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $ +*/ + +#include "wirish.h" + +void shiftOut(uint8 dataPin, uint8 clockPin, uint8 bitOrder, uint8 val) +{ + int i; + + for (i = 0; i < 8; i++) { + if (bitOrder == LSBFIRST) + digitalWrite(dataPin, !!(val & (1 << i))); + else + digitalWrite(dataPin, !!(val & (1 << (7 - i)))); + + digitalWrite(clockPin, HIGH); + digitalWrite(clockPin, LOW); + } +} |