From e608ac87afd39ed837ad3518bec652e3fd6eeffa Mon Sep 17 00:00:00 2001 From: Perry Hung Date: Sat, 24 Apr 2010 04:16:47 -0400 Subject: Initial SPI implementation. Squashed commit of the following: commit b41eb846ca60559cff242d0c550699eb8f309909 Author: Perry Hung Date: Sat Apr 24 04:09:16 2010 -0400 Turn the other peripherals back on. Not extensively tested for interactions between peripherals. commit bf4fc3bf6bc02342ae508b52fb4515d361d626f6 Author: Perry Hung Date: Sat Apr 24 03:16:35 2010 -0400 Upper level libmaple interface Added a C++ HardwareSPI class to access the SPI interface. See HardwareSPI.cpp and HardwareSPI.h for documentation. commit 17e0e5edde60e9bf2aa4d52173ad7d47d6d6da75 Author: Perry Hung Date: Thu Apr 22 02:36:01 2010 -0400 Initial SPI Polling implementation. Initial commit of a polling-based SPI driver. The driver is limited to synchronous, blocking sends and a 8-bit data frame format. Tested on SPI1 and SPI2. Other peripherals are temporarily disabled, and the rx function is untested until I find a good peripheral to test everything on. --- libmaple/libmaple_types.h | 2 + libmaple/rcc.h | 4 ++ libmaple/spi.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++ libmaple/spi.h | 115 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 253 insertions(+) create mode 100644 libmaple/spi.c create mode 100644 libmaple/spi.h (limited to 'libmaple') diff --git a/libmaple/libmaple_types.h b/libmaple/libmaple_types.h index ca98f6f..d49f95a 100644 --- a/libmaple/libmaple_types.h +++ b/libmaple/libmaple_types.h @@ -43,5 +43,7 @@ typedef long long int64; typedef void (*voidFuncPtr)(void); +#define __io volatile + #endif diff --git a/libmaple/rcc.h b/libmaple/rcc.h index 8f786ee..9973bca 100644 --- a/libmaple/rcc.h +++ b/libmaple/rcc.h @@ -100,6 +100,10 @@ #define RCC_APB1ENR_TIM4EN BIT(2) #define RCC_APB1ENR_USART2EN BIT(17) #define RCC_APB1ENR_USART3EN BIT(18) +#define RCC_APB1ENR_SPI2EN BIT(14) + +#define rcc_enable_clk_spi1() __set_bits(RCC_APB2ENR, RCC_APB2ENR_SPI1EN) +#define rcc_enable_clk_spi2() __set_bits(RCC_APB1ENR, RCC_APB1ENR_SPI2EN) #define rcc_enable_clk_timer1() __set_bits(RCC_APB2ENR, RCC_APB2ENR_TIM1EN) #define rcc_enable_clk_timer2() __set_bits(RCC_APB1ENR, RCC_APB1ENR_TIM2EN) diff --git a/libmaple/spi.c b/libmaple/spi.c new file mode 100644 index 0000000..5a77b03 --- /dev/null +++ b/libmaple/spi.c @@ -0,0 +1,132 @@ +/* ***************************************************************************** + * 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 libmaple serial peripheral interface (SPI) definitions + * + * Initial implementation for the SPI interface. + * + * This driver implements only the bare essentials of a polling driver at the + * moment. Master mode only, 8-bit data frames, and polling. + * + * The caller is responsible for controlling the chip select line. + * + * TODO: interrupt based driver, DMA. + * + */ + +#include "libmaple.h" +#include "gpio.h" +#include "rcc.h" +#include "spi.h" + +typedef struct spi_dev { + SPI *base; + GPIO_Port *port; + uint8 sck_pin; + uint8 miso_pin; + uint8 mosi_pin; +} spi_dev; + +static const spi_dev spi_dev1 = { + .base = (SPI*)SPI1_BASE, + .port = GPIOA_BASE, + .sck_pin = 5, + .miso_pin = 6, + .mosi_pin = 7 +}; + +static const spi_dev spi_dev2 = { + .base = (SPI*)SPI2_BASE, + .port = GPIOB_BASE, + .sck_pin = 13, + .miso_pin = 14, + .mosi_pin = 15 +}; + +static void spi_gpio_cfg(spi_dev *dev); + +/** + * @brief Initialize a spi peripheral + * @param spi_num which spi to turn on, SPI1 or SPI2? + * @param prescale prescale factor on the input clock. + * @param endian data frame format (LSBFIRST?) + * @param mode SPI mode number + */ +void spi_init(uint32 spi_num, + uint32 prescale, + uint32 endian, + uint32 mode) { + ASSERT(spi_num == 1 || spi_num == 2); + ASSERT(mode < 4); + + SPI *spi; + uint32 cr1 = 0; + + switch (spi_num) { + case 1: + /* limit to 18 mhz max speed */ + ASSERT(prescale != CR1_BR_PRESCALE_2); + spi = (SPI*)SPI1_BASE; + rcc_enable_clk_spi1(); + spi_gpio_cfg(&spi_dev1); + break; + case 2: + spi = (SPI*)SPI2_BASE; + rcc_enable_clk_spi2(); + spi_gpio_cfg(&spi_dev2); + break; + } + + cr1 = prescale | endian | mode | CR1_MSTR | CR1_SSI | CR1_SSM; + spi->CR1 = cr1; + + /* Peripheral enable */ + spi->CR1 |= CR1_SPE; +} + + +/** + * @brief SPI synchronous 8-bit write, blocking. + * @param spi_num which spi to send on + * @return data shifted back from the slave + */ +void spi_tx(uint32 spi_num, uint8 data) { + SPI *spi; + + ASSERT(spi_num == 1 || spi_num == 2); + + spi = (spi_num == 1) ? (SPI*)SPI1_BASE : (SPI*)SPI2_BASE; + + spi->DR = data; + + while (!(spi->SR & SR_TXE) || (spi->SR & SR_BSY)) + ; +} + +static void spi_gpio_cfg(spi_dev *dev) { + gpio_set_mode(dev->port, dev->sck_pin, GPIO_MODE_AF_OUTPUT_PP); + gpio_set_mode(dev->port, dev->miso_pin, GPIO_MODE_AF_OUTPUT_PP); + gpio_set_mode(dev->port, dev->mosi_pin, GPIO_MODE_AF_OUTPUT_PP); +} diff --git a/libmaple/spi.h b/libmaple/spi.h new file mode 100644 index 0000000..cde3b42 --- /dev/null +++ b/libmaple/spi.h @@ -0,0 +1,115 @@ +/* ***************************************************************************** + * 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 libmaple serial peripheral interface (SPI) prototypes and declarations + */ + +#ifndef _SPI_H_ +#define _SPI_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +/* peripheral addresses */ +#define SPI1_BASE 0x40013000 +#define SPI2_BASE 0x40003800 + +/* baud rate prescaler bits */ +#define CR1_BR 0x00000038 +#define CR1_BR_PRESCALE_2 0x00000000 +#define CR1_BR_PRESCALE_4 0x00000008 +#define CR1_BR_PRESCALE_8 0x00000010 +#define CR1_BR_PRESCALE_16 0x00000018 +#define CR1_BR_PRESCALE_32 0x00000020 +#define CR1_BR_PRESCALE_64 0x00000028 +#define CR1_BR_PRESCALE_128 0x00000030 +#define CR1_BR_PRESCALE_256 0x00000038 + +#define CR1_LSBFIRST BIT(7) // data frame format +#define CR1_MSTR BIT(2) // master selection +#define CR1_SSM BIT(9) // software slave management +#define CR1_SSI BIT(8) // internal slave select +#define CR1_SPE BIT(6) // peripheral enable + +/* Status register bits */ +#define SR_RXNE BIT(0) // rx buffer not empty +#define SR_TXE BIT(1) // transmit buffer empty +#define SR_BSY BIT(7) // busy flag + +typedef struct SPI { + __io uint16 CR1; + uint16 pad0; + __io uint8 CR2; + uint8 pad1[3]; + __io uint8 SR; + uint8 pad2[3]; + __io uint16 DR; + uint16 pad3; + __io uint16 CRCPR; + uint16 pad4; + __io uint16 RXCRCR; + uint16 pad5; + __io uint16 TXCRCR; + uint16 pad6; +} SPI; + +enum { + SPI_MSBFIRST = 0, + SPI_LSBFIRST = BIT(7), +}; + +enum { + SPI_PRESCALE_2 = (0x0 << 3), + SPI_PRESCALE_4 = (0x1 << 3), + SPI_PRESCALE_8 = (0x2 << 3), + SPI_PRESCALE_16 = (0x3 << 3), + SPI_PRESCALE_32 = (0x4 << 3), + SPI_PRESCALE_64 = (0x5 << 3), + SPI_PRESCALE_128 = (0x6 << 3), + SPI_PRESCALE_256 = (0x7 << 3) +}; + +void spi_init(uint32 spi_num, + uint32 prescale, + uint32 endian, + uint32 mode); +void spi_tx(uint32 spi_num, uint8 data); + +static inline uint8 spi_rx(uint32 spi_num) { + SPI *spi; + + ASSERT(spi_num == 1 || spi_num == 2); + spi = (spi_num == 1) ? (SPI*)SPI1_BASE : (SPI*)SPI2_BASE; + + return spi->DR; +} + +#ifdef __cplusplus +} +#endif + +#endif + -- cgit v1.2.3 From 7ea059f945003c90ab56b1e6f762df9ae29ddf3c Mon Sep 17 00:00:00 2001 From: Perry Hung Date: Sun, 25 Apr 2010 17:51:26 -0400 Subject: Added assert fails to exc.c --- libmaple/exc.c | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) (limited to 'libmaple') diff --git a/libmaple/exc.c b/libmaple/exc.c index ebf4b90..dd02476 100644 --- a/libmaple/exc.c +++ b/libmaple/exc.c @@ -1,3 +1,4 @@ + /* ***************************************************************************** * The MIT License * @@ -22,24 +23,41 @@ * THE SOFTWARE. * ****************************************************************************/ +/** + * @brief libmaple exception handlers. If MAPLE_DEBUG is set, then these + * exceptions will ASSERT fail and call into the default _fail() light + * blinking. + */ + +#include "util.h" + void NMIException(void) { + ASSERT(0); + while(1) + ; } + void HardFaultException(void) { - while (1) { - } + ASSERT(0); + while(1) + ; } void MemManageException(void) { - while (1) { - } + ASSERT(0); + while(1) + ; } void BusFaultException(void) { - while (1) { - } + ASSERT(0); + while(1) + ; } void UsageFaultException(void) { - while (1) { - } + ASSERT(0); + while(1) + ; } + -- cgit v1.2.3 From 62552a6f3f93223682f9df2df2614411f7e3d54c Mon Sep 17 00:00:00 2001 From: Perry Hung Date: Sun, 25 Apr 2010 18:08:56 -0400 Subject: Refactoring: The 'core' directory has now been renamed to 'wirish.' Wirish is our version of the Arduino Wiring language. --- Makefile | 62 ++--- README | 5 +- core/Print.cpp | 215 ---------------- core/Print.h | 61 ----- core/WProgram.h | 32 --- core/binary.h | 539 ----------------------------------------- core/bits.h | 56 ----- core/comm/HardwareSerial.cpp | 102 -------- core/comm/HardwareSerial.h | 53 ---- core/comm/HardwareUsb.cpp | 102 -------- core/comm/HardwareUsb.h | 54 ----- core/ext_interrupts.c | 109 --------- core/ext_interrupts.h | 60 ----- core/io.h | 145 ----------- core/main.cxx | 52 ---- core/pwm.c | 53 ---- core/pwm.h | 46 ---- core/time.c | 77 ------ core/time.h | 69 ------ core/wiring.c | 59 ----- core/wiring.h | 84 ------- core/wiring_analog.c | 44 ---- core/wiring_digital.c | 145 ----------- core/wiring_math.cpp | 54 ----- core/wiring_math.h | 56 ----- core/wiring_shift.c | 40 --- libmaple/nvic.c | 15 ++ libmaple/nvic.h | 6 +- wirish/Print.cpp | 211 ++++++++++++++++ wirish/Print.h | 61 +++++ wirish/WProgram.h | 32 +++ wirish/bits.h | 56 +++++ wirish/comm/HardwareSPI.cpp | 138 +++++++++++ wirish/comm/HardwareSPI.h | 57 +++++ wirish/comm/HardwareSerial.cpp | 102 ++++++++ wirish/comm/HardwareSerial.h | 53 ++++ wirish/comm/HardwareUsb.cpp | 102 ++++++++ wirish/comm/HardwareUsb.h | 54 +++++ wirish/ext_interrupts.c | 109 +++++++++ wirish/ext_interrupts.h | 60 +++++ wirish/io.h | 145 +++++++++++ wirish/main.cxx | 52 ++++ wirish/pwm.c | 51 ++++ wirish/pwm.h | 46 ++++ wirish/time.c | 77 ++++++ wirish/time.h | 69 ++++++ wirish/wirish.c | 47 ++++ wirish/wirish.h | 85 +++++++ wirish/wirish_analog.c | 42 ++++ wirish/wirish_digital.c | 143 +++++++++++ wirish/wirish_math.cpp | 54 +++++ wirish/wirish_math.h | 56 +++++ wirish/wirish_shift.c | 40 +++ 53 files changed, 1996 insertions(+), 2341 deletions(-) delete mode 100644 core/Print.cpp delete mode 100644 core/Print.h delete mode 100644 core/WProgram.h delete mode 100644 core/binary.h delete mode 100644 core/bits.h delete mode 100644 core/comm/HardwareSerial.cpp delete mode 100644 core/comm/HardwareSerial.h delete mode 100644 core/comm/HardwareUsb.cpp delete mode 100644 core/comm/HardwareUsb.h delete mode 100644 core/ext_interrupts.c delete mode 100644 core/ext_interrupts.h delete mode 100644 core/io.h delete mode 100644 core/main.cxx delete mode 100644 core/pwm.c delete mode 100644 core/pwm.h delete mode 100644 core/time.c delete mode 100644 core/time.h delete mode 100644 core/wiring.c delete mode 100644 core/wiring.h delete mode 100644 core/wiring_analog.c delete mode 100644 core/wiring_digital.c delete mode 100644 core/wiring_math.cpp delete mode 100644 core/wiring_math.h delete mode 100644 core/wiring_shift.c create mode 100644 wirish/Print.cpp create mode 100644 wirish/Print.h create mode 100644 wirish/WProgram.h create mode 100644 wirish/bits.h create mode 100644 wirish/comm/HardwareSPI.cpp create mode 100644 wirish/comm/HardwareSPI.h create mode 100644 wirish/comm/HardwareSerial.cpp create mode 100644 wirish/comm/HardwareSerial.h create mode 100644 wirish/comm/HardwareUsb.cpp create mode 100644 wirish/comm/HardwareUsb.h create mode 100644 wirish/ext_interrupts.c create mode 100644 wirish/ext_interrupts.h create mode 100644 wirish/io.h create mode 100644 wirish/main.cxx create mode 100644 wirish/pwm.c create mode 100644 wirish/pwm.h create mode 100644 wirish/time.c create mode 100644 wirish/time.h create mode 100644 wirish/wirish.c create mode 100644 wirish/wirish.h create mode 100644 wirish/wirish_analog.c create mode 100644 wirish/wirish_digital.c create mode 100644 wirish/wirish_math.cpp create mode 100644 wirish/wirish_math.h create mode 100644 wirish/wirish_shift.c (limited to 'libmaple') diff --git a/Makefile b/Makefile index e7df3a5..875e458 100644 --- a/Makefile +++ b/Makefile @@ -27,14 +27,14 @@ BUILD_PATH = build LIB_PATH = libmaple OUTDIRS = $(BUILD_PATH)/$(LIB_PATH)\ - $(BUILD_PATH)/core \ - $(BUILD_PATH)/core/comm + $(BUILD_PATH)/wirish \ + $(BUILD_PATH)/wirish/comm INCLUDES = -I$(LIB_PATH) \ -I./ \ - -Icore \ - -Icore/comm + -Iwirish \ + -Iwirish/comm # default is to upload to flash #DEFFLAGS = VECT_TAB_BASE @@ -57,33 +57,33 @@ ODFLAGS = -S # main source file MAIN=main.c -CSRC = libmaple/systick.c \ - libmaple/timers.c \ - libmaple/adc.c \ - libmaple/syscalls.c \ - libmaple/exc.c \ - libmaple/exti.c \ - libmaple/gpio.c \ - libmaple/nvic.c \ - libmaple/usart.c \ - libmaple/util.c \ - libmaple/usb.c \ - libmaple/rcc.c \ - libmaple/flash.c \ - libmaple/spi.c \ - core/wiring.c \ - core/wiring_shift.c \ - core/wiring_analog.c \ - core/time.c \ - core/pwm.c \ - core/ext_interrupts.c \ - core/wiring_digital.c - -CPPSRC = core/wiring_math.cpp \ - core/Print.cpp \ - core/comm/HardwareSerial.cpp \ - core/comm/HardwareUsb.cpp \ - core/comm/HardwareSPI.cpp \ +CSRC = libmaple/systick.c \ + libmaple/timers.c \ + libmaple/adc.c \ + libmaple/syscalls.c \ + libmaple/exc.c \ + libmaple/exti.c \ + libmaple/gpio.c \ + libmaple/nvic.c \ + libmaple/usart.c \ + libmaple/util.c \ + libmaple/usb.c \ + libmaple/rcc.c \ + libmaple/flash.c \ + libmaple/spi.c \ + wirish/wirish.c \ + wirish/wirish_shift.c \ + wirish/wirish_analog.c \ + wirish/time.c \ + wirish/pwm.c \ + wirish/ext_interrupts.c \ + wirish/wirish_digital.c + +CPPSRC = wirish/wirish_math.cpp \ + wirish/Print.cpp \ + wirish/comm/HardwareSerial.cpp \ + wirish/comm/HardwareUsb.cpp \ + wirish/comm/HardwareSPI.cpp \ main.cpp # i really have no idea what i'm doing diff --git a/README b/README index f3bb334..9638a6b 100644 --- a/README +++ b/README @@ -31,9 +31,10 @@ libmaple Repo Layout Lowest level definitions, routines, macros, and functions. This is the meat of the library. -/core/ +/wirish/ Extra wrappers and functionality around the lower level code which is - useful for programming in the IDE. + useful for programming in the IDE. Files in here implement the "Wirish" + language, an Arduino "Wiring"-like language. /examples/ What it sounds like. Copy these to /main.cpp to compile them. diff --git a/core/Print.cpp b/core/Print.cpp deleted file mode 100644 index da461ae..0000000 --- a/core/Print.cpp +++ /dev/null @@ -1,215 +0,0 @@ -/* - 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 -#include -#include -#include "wiring.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 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/core/Print.h b/core/Print.h deleted file mode 100644 index b2f8647..0000000 --- a/core/Print.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - 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 // 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/core/WProgram.h b/core/WProgram.h deleted file mode 100644 index 7653bb6..0000000 --- a/core/WProgram.h +++ /dev/null @@ -1,32 +0,0 @@ -/* ***************************************************************************** - * 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/core/binary.h b/core/binary.h deleted file mode 100644 index e4882fc..0000000 --- a/core/binary.h +++ /dev/null @@ -1,539 +0,0 @@ -/* ***************************************************************************** - * 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. - * ****************************************************************************/ - -#ifndef Binary_h -#define Binary_h - -#define B0 0 -#define B00 0 -#define B000 0 -#define B0000 0 -#define B00000 0 -#define B000000 0 -#define B0000000 0 -#define B00000000 0 -#define B1 1 -#define B01 1 -#define B001 1 -#define B0001 1 -#define B00001 1 -#define B000001 1 -#define B0000001 1 -#define B00000001 1 -#define B10 2 -#define B010 2 -#define B0010 2 -#define B00010 2 -#define B000010 2 -#define B0000010 2 -#define B00000010 2 -#define B11 3 -#define B011 3 -#define B0011 3 -#define B00011 3 -#define B000011 3 -#define B0000011 3 -#define B00000011 3 -#define B100 4 -#define B0100 4 -#define B00100 4 -#define B000100 4 -#define B0000100 4 -#define B00000100 4 -#define B101 5 -#define B0101 5 -#define B00101 5 -#define B000101 5 -#define B0000101 5 -#define B00000101 5 -#define B110 6 -#define B0110 6 -#define B00110 6 -#define B000110 6 -#define B0000110 6 -#define B00000110 6 -#define B111 7 -#define B0111 7 -#define B00111 7 -#define B000111 7 -#define B0000111 7 -#define B00000111 7 -#define B1000 8 -#define B01000 8 -#define B001000 8 -#define B0001000 8 -#define B00001000 8 -#define B1001 9 -#define B01001 9 -#define B001001 9 -#define B0001001 9 -#define B00001001 9 -#define B1010 10 -#define B01010 10 -#define B001010 10 -#define B0001010 10 -#define B00001010 10 -#define B1011 11 -#define B01011 11 -#define B001011 11 -#define B0001011 11 -#define B00001011 11 -#define B1100 12 -#define B01100 12 -#define B001100 12 -#define B0001100 12 -#define B00001100 12 -#define B1101 13 -#define B01101 13 -#define B001101 13 -#define B0001101 13 -#define B00001101 13 -#define B1110 14 -#define B01110 14 -#define B001110 14 -#define B0001110 14 -#define B00001110 14 -#define B1111 15 -#define B01111 15 -#define B001111 15 -#define B0001111 15 -#define B00001111 15 -#define B10000 16 -#define B010000 16 -#define B0010000 16 -#define B00010000 16 -#define B10001 17 -#define B010001 17 -#define B0010001 17 -#define B00010001 17 -#define B10010 18 -#define B010010 18 -#define B0010010 18 -#define B00010010 18 -#define B10011 19 -#define B010011 19 -#define B0010011 19 -#define B00010011 19 -#define B10100 20 -#define B010100 20 -#define B0010100 20 -#define B00010100 20 -#define B10101 21 -#define B010101 21 -#define B0010101 21 -#define B00010101 21 -#define B10110 22 -#define B010110 22 -#define B0010110 22 -#define B00010110 22 -#define B10111 23 -#define B010111 23 -#define B0010111 23 -#define B00010111 23 -#define B11000 24 -#define B011000 24 -#define B0011000 24 -#define B00011000 24 -#define B11001 25 -#define B011001 25 -#define B0011001 25 -#define B00011001 25 -#define B11010 26 -#define B011010 26 -#define B0011010 26 -#define B00011010 26 -#define B11011 27 -#define B011011 27 -#define B0011011 27 -#define B00011011 27 -#define B11100 28 -#define B011100 28 -#define B0011100 28 -#define B00011100 28 -#define B11101 29 -#define B011101 29 -#define B0011101 29 -#define B00011101 29 -#define B11110 30 -#define B011110 30 -#define B0011110 30 -#define B00011110 30 -#define B11111 31 -#define B011111 31 -#define B0011111 31 -#define B00011111 31 -#define B100000 32 -#define B0100000 32 -#define B00100000 32 -#define B100001 33 -#define B0100001 33 -#define B00100001 33 -#define B100010 34 -#define B0100010 34 -#define B00100010 34 -#define B100011 35 -#define B0100011 35 -#define B00100011 35 -#define B100100 36 -#define B0100100 36 -#define B00100100 36 -#define B100101 37 -#define B0100101 37 -#define B00100101 37 -#define B100110 38 -#define B0100110 38 -#define B00100110 38 -#define B100111 39 -#define B0100111 39 -#define B00100111 39 -#define B101000 40 -#define B0101000 40 -#define B00101000 40 -#define B101001 41 -#define B0101001 41 -#define B00101001 41 -#define B101010 42 -#define B0101010 42 -#define B00101010 42 -#define B101011 43 -#define B0101011 43 -#define B00101011 43 -#define B101100 44 -#define B0101100 44 -#define B00101100 44 -#define B101101 45 -#define B0101101 45 -#define B00101101 45 -#define B101110 46 -#define B0101110 46 -#define B00101110 46 -#define B101111 47 -#define B0101111 47 -#define B00101111 47 -#define B110000 48 -#define B0110000 48 -#define B00110000 48 -#define B110001 49 -#define B0110001 49 -#define B00110001 49 -#define B110010 50 -#define B0110010 50 -#define B00110010 50 -#define B110011 51 -#define B0110011 51 -#define B00110011 51 -#define B110100 52 -#define B0110100 52 -#define B00110100 52 -#define B110101 53 -#define B0110101 53 -#define B00110101 53 -#define B110110 54 -#define B0110110 54 -#define B00110110 54 -#define B110111 55 -#define B0110111 55 -#define B00110111 55 -#define B111000 56 -#define B0111000 56 -#define B00111000 56 -#define B111001 57 -#define B0111001 57 -#define B00111001 57 -#define B111010 58 -#define B0111010 58 -#define B00111010 58 -#define B111011 59 -#define B0111011 59 -#define B00111011 59 -#define B111100 60 -#define B0111100 60 -#define B00111100 60 -#define B111101 61 -#define B0111101 61 -#define B00111101 61 -#define B111110 62 -#define B0111110 62 -#define B00111110 62 -#define B111111 63 -#define B0111111 63 -#define B00111111 63 -#define B1000000 64 -#define B01000000 64 -#define B1000001 65 -#define B01000001 65 -#define B1000010 66 -#define B01000010 66 -#define B1000011 67 -#define B01000011 67 -#define B1000100 68 -#define B01000100 68 -#define B1000101 69 -#define B01000101 69 -#define B1000110 70 -#define B01000110 70 -#define B1000111 71 -#define B01000111 71 -#define B1001000 72 -#define B01001000 72 -#define B1001001 73 -#define B01001001 73 -#define B1001010 74 -#define B01001010 74 -#define B1001011 75 -#define B01001011 75 -#define B1001100 76 -#define B01001100 76 -#define B1001101 77 -#define B01001101 77 -#define B1001110 78 -#define B01001110 78 -#define B1001111 79 -#define B01001111 79 -#define B1010000 80 -#define B01010000 80 -#define B1010001 81 -#define B01010001 81 -#define B1010010 82 -#define B01010010 82 -#define B1010011 83 -#define B01010011 83 -#define B1010100 84 -#define B01010100 84 -#define B1010101 85 -#define B01010101 85 -#define B1010110 86 -#define B01010110 86 -#define B1010111 87 -#define B01010111 87 -#define B1011000 88 -#define B01011000 88 -#define B1011001 89 -#define B01011001 89 -#define B1011010 90 -#define B01011010 90 -#define B1011011 91 -#define B01011011 91 -#define B1011100 92 -#define B01011100 92 -#define B1011101 93 -#define B01011101 93 -#define B1011110 94 -#define B01011110 94 -#define B1011111 95 -#define B01011111 95 -#define B1100000 96 -#define B01100000 96 -#define B1100001 97 -#define B01100001 97 -#define B1100010 98 -#define B01100010 98 -#define B1100011 99 -#define B01100011 99 -#define B1100100 100 -#define B01100100 100 -#define B1100101 101 -#define B01100101 101 -#define B1100110 102 -#define B01100110 102 -#define B1100111 103 -#define B01100111 103 -#define B1101000 104 -#define B01101000 104 -#define B1101001 105 -#define B01101001 105 -#define B1101010 106 -#define B01101010 106 -#define B1101011 107 -#define B01101011 107 -#define B1101100 108 -#define B01101100 108 -#define B1101101 109 -#define B01101101 109 -#define B1101110 110 -#define B01101110 110 -#define B1101111 111 -#define B01101111 111 -#define B1110000 112 -#define B01110000 112 -#define B1110001 113 -#define B01110001 113 -#define B1110010 114 -#define B01110010 114 -#define B1110011 115 -#define B01110011 115 -#define B1110100 116 -#define B01110100 116 -#define B1110101 117 -#define B01110101 117 -#define B1110110 118 -#define B01110110 118 -#define B1110111 119 -#define B01110111 119 -#define B1111000 120 -#define B01111000 120 -#define B1111001 121 -#define B01111001 121 -#define B1111010 122 -#define B01111010 122 -#define B1111011 123 -#define B01111011 123 -#define B1111100 124 -#define B01111100 124 -#define B1111101 125 -#define B01111101 125 -#define B1111110 126 -#define B01111110 126 -#define B1111111 127 -#define B01111111 127 -#define B10000000 128 -#define B10000001 129 -#define B10000010 130 -#define B10000011 131 -#define B10000100 132 -#define B10000101 133 -#define B10000110 134 -#define B10000111 135 -#define B10001000 136 -#define B10001001 137 -#define B10001010 138 -#define B10001011 139 -#define B10001100 140 -#define B10001101 141 -#define B10001110 142 -#define B10001111 143 -#define B10010000 144 -#define B10010001 145 -#define B10010010 146 -#define B10010011 147 -#define B10010100 148 -#define B10010101 149 -#define B10010110 150 -#define B10010111 151 -#define B10011000 152 -#define B10011001 153 -#define B10011010 154 -#define B10011011 155 -#define B10011100 156 -#define B10011101 157 -#define B10011110 158 -#define B10011111 159 -#define B10100000 160 -#define B10100001 161 -#define B10100010 162 -#define B10100011 163 -#define B10100100 164 -#define B10100101 165 -#define B10100110 166 -#define B10100111 167 -#define B10101000 168 -#define B10101001 169 -#define B10101010 170 -#define B10101011 171 -#define B10101100 172 -#define B10101101 173 -#define B10101110 174 -#define B10101111 175 -#define B10110000 176 -#define B10110001 177 -#define B10110010 178 -#define B10110011 179 -#define B10110100 180 -#define B10110101 181 -#define B10110110 182 -#define B10110111 183 -#define B10111000 184 -#define B10111001 185 -#define B10111010 186 -#define B10111011 187 -#define B10111100 188 -#define B10111101 189 -#define B10111110 190 -#define B10111111 191 -#define B11000000 192 -#define B11000001 193 -#define B11000010 194 -#define B11000011 195 -#define B11000100 196 -#define B11000101 197 -#define B11000110 198 -#define B11000111 199 -#define B11001000 200 -#define B11001001 201 -#define B11001010 202 -#define B11001011 203 -#define B11001100 204 -#define B11001101 205 -#define B11001110 206 -#define B11001111 207 -#define B11010000 208 -#define B11010001 209 -#define B11010010 210 -#define B11010011 211 -#define B11010100 212 -#define B11010101 213 -#define B11010110 214 -#define B11010111 215 -#define B11011000 216 -#define B11011001 217 -#define B11011010 218 -#define B11011011 219 -#define B11011100 220 -#define B11011101 221 -#define B11011110 222 -#define B11011111 223 -#define B11100000 224 -#define B11100001 225 -#define B11100010 226 -#define B11100011 227 -#define B11100100 228 -#define B11100101 229 -#define B11100110 230 -#define B11100111 231 -#define B11101000 232 -#define B11101001 233 -#define B11101010 234 -#define B11101011 235 -#define B11101100 236 -#define B11101101 237 -#define B11101110 238 -#define B11101111 239 -#define B11110000 240 -#define B11110001 241 -#define B11110010 242 -#define B11110011 243 -#define B11110100 244 -#define B11110101 245 -#define B11110110 246 -#define B11110111 247 -#define B11111000 248 -#define B11111001 249 -#define B11111010 250 -#define B11111011 251 -#define B11111100 252 -#define B11111101 253 -#define B11111110 254 -#define B11111111 255 - -#endif diff --git a/core/bits.h b/core/bits.h deleted file mode 100644 index 7ebea80..0000000 --- a/core/bits.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - 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/core/comm/HardwareSerial.cpp b/core/comm/HardwareSerial.cpp deleted file mode 100644 index 42c5d61..0000000 --- a/core/comm/HardwareSerial.cpp +++ /dev/null @@ -1,102 +0,0 @@ -/* ***************************************************************************** - * 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 "wiring.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/core/comm/HardwareSerial.h b/core/comm/HardwareSerial.h deleted file mode 100644 index a488244..0000000 --- a/core/comm/HardwareSerial.h +++ /dev/null @@ -1,53 +0,0 @@ -/* ***************************************************************************** - * 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/core/comm/HardwareUsb.cpp b/core/comm/HardwareUsb.cpp deleted file mode 100644 index 2012c2e..0000000 --- a/core/comm/HardwareUsb.cpp +++ /dev/null @@ -1,102 +0,0 @@ -/* ***************************************************************************** - * The MIT License - * - * Copyright (c) 2010 Andrew Meyer. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * ****************************************************************************/ - -/** - * @brief Wiring like serial api to USB virtual COM - */ - -#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 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/core/comm/HardwareUsb.h b/core/comm/HardwareUsb.h deleted file mode 100644 index ee13f40..0000000 --- a/core/comm/HardwareUsb.h +++ /dev/null @@ -1,54 +0,0 @@ -/* ***************************************************************************** - * The MIT License - * - * Copyright (c) 2010 Andrew Meyer. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * ****************************************************************************/ - -/** - * @brief Wiring like serial api to USB virtual COM - */ - -#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/core/ext_interrupts.c b/core/ext_interrupts.c deleted file mode 100644 index bbe861d..0000000 --- a/core/ext_interrupts.c +++ /dev/null @@ -1,109 +0,0 @@ -/* ***************************************************************************** - * 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 "wiring.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/core/ext_interrupts.h b/core/ext_interrupts.h deleted file mode 100644 index fc69a15..0000000 --- a/core/ext_interrupts.h +++ /dev/null @@ -1,60 +0,0 @@ -/* ***************************************************************************** - * 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/core/io.h b/core/io.h deleted file mode 100644 index fff551c..0000000 --- a/core/io.h +++ /dev/null @@ -1,145 +0,0 @@ -/* ***************************************************************************** - * 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/core/main.cxx b/core/main.cxx deleted file mode 100644 index 440d464..0000000 --- a/core/main.cxx +++ /dev/null @@ -1,52 +0,0 @@ -/* ***************************************************************************** - * 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/core/pwm.c b/core/pwm.c deleted file mode 100644 index 8e58d01..0000000 --- a/core/pwm.c +++ /dev/null @@ -1,53 +0,0 @@ -/* ***************************************************************************** - * 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.c - * - * @brief - */ - -#include "wiring.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/core/pwm.h b/core/pwm.h deleted file mode 100644 index 4f69751..0000000 --- a/core/pwm.h +++ /dev/null @@ -1,46 +0,0 @@ -/* ***************************************************************************** - * 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/core/time.c b/core/time.c deleted file mode 100644 index 69e962c..0000000 --- a/core/time.c +++ /dev/null @@ -1,77 +0,0 @@ -/* ***************************************************************************** - * 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/core/time.h b/core/time.h deleted file mode 100644 index 9e5536b..0000000 --- a/core/time.h +++ /dev/null @@ -1,69 +0,0 @@ -/* ***************************************************************************** - * 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/core/wiring.c b/core/wiring.c deleted file mode 100644 index 803b571..0000000 --- a/core/wiring.c +++ /dev/null @@ -1,59 +0,0 @@ -/* ***************************************************************************** - * 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 wiring.c - * - * @brief - */ - -#include "wiring.h" -#include "rcc.h" -#include "systick.h" -#include "gpio.h" - -void nvic_init(void); - -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); -} - -void nvic_init(void) { -#ifdef VECT_TAB_ROM - nvic_set_vector_table(USER_ADDR_ROM, 0x0); -#elif defined VECT_TAB_RAM - nvic_set_vector_table(USER_ADDR_RAM, 0x0); -#else // VECT_TAB_BASE - /* Set the Vector Table base location at 0x08000000 */ - nvic_set_vector_table(((uint32)0x08000000), 0x0); -#endif -} diff --git a/core/wiring.h b/core/wiring.h deleted file mode 100644 index 8360ce6..0000000 --- a/core/wiring.h +++ /dev/null @@ -1,84 +0,0 @@ -/* ***************************************************************************** - * 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_H_ -#define _WIRING_H_ - -#include "libmaple.h" -#include "timers.h" -#include "io.h" -#include "binary.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 - -#define HIGH 0x1 -#define LOW 0x0 - -#define true 0x1 -#define false 0x0 - -#define SERIAL 0x0 -#define DISPLAY 0x1 - -#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); -unsigned long pulseIn(uint8 pin, uint8 state, unsigned long timeout); -void shiftOut(uint8 dataPin, uint8 clockPin, uint8 bitOrder, byte val); - - -#ifdef __cplusplus -} // extern "C" -#endif - - - - -#endif - diff --git a/core/wiring_analog.c b/core/wiring_analog.c deleted file mode 100644 index da41025..0000000 --- a/core/wiring_analog.c +++ /dev/null @@ -1,44 +0,0 @@ -/* ***************************************************************************** - * 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 wiring_analog.c - * - * @brief - */ - -#include "libmaple.h" -#include "wiring.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/core/wiring_digital.c b/core/wiring_digital.c deleted file mode 100644 index 52cc652..0000000 --- a/core/wiring_digital.c +++ /dev/null @@ -1,145 +0,0 @@ -/* ***************************************************************************** - * 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 wiring_digital.c - * - * @brief - */ - -#include "wiring.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/core/wiring_math.cpp b/core/wiring_math.cpp deleted file mode 100644 index 0d907c4..0000000 --- a/core/wiring_math.cpp +++ /dev/null @@ -1,54 +0,0 @@ -/* - 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 -#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/core/wiring_math.h b/core/wiring_math.h deleted file mode 100644 index b8fa710..0000000 --- a/core/wiring_math.h +++ /dev/null @@ -1,56 +0,0 @@ -/* ***************************************************************************** - * 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 - -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/core/wiring_shift.c b/core/wiring_shift.c deleted file mode 100644 index a2487e7..0000000 --- a/core/wiring_shift.c +++ /dev/null @@ -1,40 +0,0 @@ -/* - 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 "wiring.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); - } -} diff --git a/libmaple/nvic.c b/libmaple/nvic.c index 91e572e..6603d42 100644 --- a/libmaple/nvic.c +++ b/libmaple/nvic.c @@ -64,3 +64,18 @@ void nvic_enable_interrupt(uint32 n) { } + +/** + * @brief Initialice the NVIC at address addr + * @param addr Address to set the vector table at + */ +void nvic_init(void) { +#ifdef VECT_TAB_ROM + nvic_set_vector_table(USER_ADDR_ROM, 0x0); +#elif defined VECT_TAB_RAM + nvic_set_vector_table(USER_ADDR_RAM, 0x0); +#else // VECT_TAB_BASE + /* Set the Vector Table base location at 0x08000000 */ + nvic_set_vector_table(((uint32)0x08000000), 0x0); +#endif +} diff --git a/libmaple/nvic.h b/libmaple/nvic.h index e785018..a9f32d6 100644 --- a/libmaple/nvic.h +++ b/libmaple/nvic.h @@ -54,10 +54,15 @@ #define NVIC_NR_INTERRUPTS 60 +/* Where to put code */ +#define USER_ADDR_ROM 0x08005000 +#define USER_ADDR_RAM 0x20000C00 + #ifdef __cplusplus extern "C"{ #endif +void nvic_init(void); void nvic_disable_interrupts(void); void nvic_enable_interrupt(uint32); @@ -65,6 +70,5 @@ void nvic_enable_interrupt(uint32); } #endif - #endif 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 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 // 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 +#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 + +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); + } +} -- cgit v1.2.3