diff options
93 files changed, 11692 insertions, 0 deletions
diff --git a/csource/Fade.cpp b/csource/Fade.cpp new file mode 100644 index 0000000..ffc7636 --- /dev/null +++ b/csource/Fade.cpp @@ -0,0 +1,36 @@ +#include "Energia.h" +void setup(); +void loop(); +#line 1 +/* + Fade + + This example shows how to fade an LED on pin 9 + using the analogWrite() function. + + This example code is in the public domain. + + */ +int brightness = 0; // how bright the LED is +int fadeAmount = 5; // how many points to fade the LED by + +void setup() { + // declare pin 9 to be an output: + pinMode(GREEN_LED, OUTPUT); +} + +void loop() { + // set the brightness of pin 9: + analogWrite(GREEN_LED, brightness); + + // change the brightness for next time through the loop: + brightness = brightness + fadeAmount; + + // reverse the direction of the fading at the ends of the fade: + if (brightness == 0 || brightness == 255) { + fadeAmount = -fadeAmount ; + } + // wait for 30 milliseconds to see the dimming effect + delay(30); +} + diff --git a/msp430/boards.txt b/msp430/boards.txt new file mode 100644 index 0000000..10a6f4a --- /dev/null +++ b/msp430/boards.txt @@ -0,0 +1,37 @@ +############################################################## +lpmsp430g2231.name=LaunchPad w/ msp430g2231 (1MHz) +lpmsp430g2231.upload.protocol=rf2500 +lpmsp430g2231.upload.maximum_size=2048 +lpmsp430g2231.build.mcu=msp430g2231 +lpmsp430g2231.build.f_cpu=1000000L +lpmsp430g2231.build.core=msp430 +lpmsp430g2231.build.variant=launchpad + +############################################################## +#lpmsp430g2231f.name=LaunchPad w/ msp430g2231 (16MHz) +#lpmsp430g2231f.upload.protocol=rf2500 +#lpmsp430g2231f.upload.maximum_size=2048 +#lpmsp430g2231f.build.mcu=msp430g2231 +#lpmsp430g2231f.build.f_cpu=16000000L +#lpmsp430g2231f.build.core=msp430 +#lpmsp430g2231f.build.variant=launchpad + +############################################################## +lpmsp430g2452.name=LaunchPad w/ msp430g2452 (16MHz) +lpmsp430g2452.upload.protocol=rf2500 +lpmsp430g2452.upload.maximum_size=8192 +lpmsp430g2452.build.mcu=msp430g2452 +lpmsp430g2452.build.f_cpu=16000000L +lpmsp430g2452.build.core=msp430 +lpmsp430g2452.build.variant=launchpad + +############################################################## +lpmsp430g2553.name=LaunchPad w/ msp430g2553 (16MHz) +lpmsp430g2553.upload.protocol=rf2500 +lpmsp430g2553.upload.maximum_size=16384 +lpmsp430g2553.build.mcu=msp430g2553 +lpmsp430g2553.build.f_cpu=16000000L +lpmsp430g2553.build.core=msp430 +lpmsp430g2553.build.variant=launchpad + +############################################################## diff --git a/msp430/cores/msp430/Arduino.h b/msp430/cores/msp430/Arduino.h new file mode 100644 index 0000000..5f3919f --- /dev/null +++ b/msp430/cores/msp430/Arduino.h @@ -0,0 +1,4 @@ +// Stub for Arduino header file - For added compatibility with existing arduino libraries +// Include Energia instead +#include "Energia.h" + diff --git a/msp430/cores/msp430/Energia.h b/msp430/cores/msp430/Energia.h new file mode 100644 index 0000000..3aec86f --- /dev/null +++ b/msp430/cores/msp430/Energia.h @@ -0,0 +1,185 @@ +#ifndef Energia_h +#define Energia_h + +#include <msp430.h> +#include <stdint.h> +#include <string.h> +#include <math.h> + +#include "binary.h" + +#ifdef __cplusplus +extern "C"{ +#endif + +#define NOT_A_PORT 0 +#define NOT_A_PIN 0 +#define NOT_ON_TIMER 0 + +#define HIGH 0x1 +#define LOW 0x0 + +#define LSBFIRST 0 +#define MSBFIRST 1 + +#define FALLING 1 +#define RISING 0 + +#define INPUT 0x0 +#define OUTPUT 0x1 +#define INPUT_PULLUP 0x2 +#define INPUT_PULLDOWN 0x3 + +#define true 0x1 +#define false 0x0 + +#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 + +#if defined(__MSP430_HAS_ADC10__) +#define DEFAULT SREF_0 +#define INTERNAL1V5 SREF_1 + REFON +#define INTERNAL2V5 SREF_1 + REFON + REF2_5V +#define EXTERNAL SREF_2 +#endif + +#define P1 1 +#define P2 2 +#define P3 3 +#define P4 4 +#define P5 5 +#define P6 6 +#define P7 7 + +#define T0A0 0 +#define T0A1 1 +#define T0A2 2 +#define T1A0 3 +#define T1A1 4 +#define T1A2 5 +#define T1A3 6 +#define T1A4 7 +#define T1A5 8 +#define T2A0 9 +#define T2A1 10 +#define T2A2 11 + +typedef uint8_t boolean; +typedef uint8_t byte; + +#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)) + +#define interrupts() __bis_SR_register(GIE) +#define noInterrupts() __bic_SR_register(GIE) + +#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L ) +#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() ) +#define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() ) + +#define lowByte(w) ((uint8_t) ((w) & 0xff)) +#define highByte(w) ((uint8_t) ((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)) + + +typedef unsigned int word; + +#define bit(b) (1UL << (b)) + +void init(void); +void setup(void); +void loop(void); + +void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val); +uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder); +unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout); +void pinMode(uint8_t, uint8_t); +void digitalWrite(uint8_t, uint8_t); +int digitalRead(uint8_t); +uint16_t analogRead(uint8_t); +void analogWrite(uint8_t, int); +void analogReference(uint16_t); +void analogFrequency(uint32_t); +void analogResolution(uint16_t); + + + +void delay(uint32_t milliseconds); + +void attachInterrupt(uint8_t, void (*)(void), int mode); +void detachInterrupt(uint8_t); + +extern const uint8_t digital_pin_to_timer[]; +extern const uint8_t digital_pin_to_port[]; +extern const uint8_t digital_pin_to_bit_mask[]; +extern const uint16_t port_to_sel[]; +extern const uint16_t port_to_sel2[]; +extern const uint16_t port_to_input[]; +extern const uint16_t port_to_output[]; + +#define digitalPinToPort(P) ( digital_pin_to_port[P] ) +#define digitalPinToBitMask(P) ( digital_pin_to_bit_mask[P] ) +#define digitalPinToTimer(P) ( digital_pin_to_timer[P] ) +#define portDirRegister(P) ( (volatile uint16_t *)( port_to_dir[P]) ) +#define portSelRegister(P) ( (volatile uint16_t *)( port_to_sel[P]) ) +#define portSel2Register(P) ( (volatile uint16_t *)( port_to_sel2[P]) ) +#define portRenRegister(P) ( (volatile uint16_t *)( port_to_ren[P]) ) +#define portOutputRegister(P) ( (volatile uint16_t *)( port_to_output[P]) ) +#define portInputRegister(P) ( (volatile uint16_t *)( port_to_input[P]) ) +#define digitalPinToTimer(P) ( digital_pin_to_timer[P] ) + +// Implemented in wiring.c +void delayMicroseconds(unsigned int us); +unsigned long micros(); +unsigned long millis(); +void disableWatchDog(); +void enableWatchDog(); + +#ifdef __cplusplus +} // extern "C" +#endif + +#ifdef __cplusplus +#include "WCharacter.h" +#include "WString.h" +#ifdef __MSP430_HAS_USCI__ +#include "HardwareSerial.h" +#else +#include "TimerSerial.h" +#endif + +uint16_t makeWord(uint16_t w); +uint16_t makeWord(byte h, byte l); + +#define word(...) makeWord(__VA_ARGS__) + +unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L); + +void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0); +void noTone(uint8_t _pin); + +// WMath prototypes +long random(long); +long random(long, long); +void randomSeed(unsigned int); +long map(long, long, long, long, long); + +#endif + +#include "pins_energia.h" + +#endif + + diff --git a/msp430/cores/msp430/HardwareSerial.cpp b/msp430/cores/msp430/HardwareSerial.cpp new file mode 100644 index 0000000..04bf715 --- /dev/null +++ b/msp430/cores/msp430/HardwareSerial.cpp @@ -0,0 +1,213 @@ +/* + ************************************************************************ + * HardwareSerial.cpp + * + * Arduino core files for MSP430 + * Copyright (c) 2012 Robert Wessels. All right reserved. + * + * + *********************************************************************** + Derived from: + HardwareSerial.cpp - Hardware serial library for Wiring + Copyright (c) 2006 Nicholas Zambetti. 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 + Modified 28 September 2010 by Mark Sproul +*/ + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <inttypes.h> +#include "Energia.h" +#include "wiring_private.h" +#include "usci_isr_handler.h" + +#if defined(__MSP430_HAS_USCI__) + +#include "HardwareSerial.h" + +HardwareSerial *SerialPtr; + +/** + * Receive Data (RXD) at P1.1 + */ +#define RXD BIT1 + +/** + * Receive Data (TXD) at P1.2 + */ +#define TXD BIT2 + + +#define SERIAL_BUFFER_SIZE 16 + +struct ring_buffer +{ + unsigned char buffer[SERIAL_BUFFER_SIZE]; + volatile unsigned int head; + volatile unsigned int tail; +}; + +ring_buffer rx_buffer = { { 0 }, 0, 0 }; +ring_buffer tx_buffer = { { 0 }, 0, 0 }; + +inline void store_char(unsigned char c, ring_buffer *buffer) +{ + unsigned int i = (unsigned int)(buffer->head + 1) % SERIAL_BUFFER_SIZE; + + // if we should be storing the received character into the location + // just before the tail (meaning that the head would advance to the + // current location of the tail), we're about to overflow the buffer + // and so we don't write the character or advance the head. + if (i != buffer->tail) { + buffer->buffer[buffer->head] = c; + buffer->head = i; + } +} + +// Constructors //////////////////////////////////////////////////////////////// + +HardwareSerial::HardwareSerial(ring_buffer *rx_buffer, ring_buffer *tx_buffer) +{ + _rx_buffer = rx_buffer; + _tx_buffer = tx_buffer; +} + +// Public Methods ////////////////////////////////////////////////////////////// +#define SMCLK F_CPU //SMCLK = F_CPU for now + +void HardwareSerial::begin(unsigned long baud) +{ + unsigned int divider; + unsigned char mod, oversampling; + + /* Calling this dummy function prevents the linker + * from stripping the USCI interupt vectors.*/ + usci_isr_install(); + if (SMCLK/baud>=48) { // requires SMCLK for oversampling + oversampling = 1; + } + else { + oversampling= 0; + } + + divider=(SMCLK<<4)/baud; + + if(!oversampling) { + mod = ((divider&0xF)+1)&0xE; // UCBRSx (bit 1-3) + divider >>=4; + } else { + mod = ((divider&0xf8)+0x8)&0xf0; // UCBRFx (bit 4-7) + divider>>=8; + } + + SerialPtr = this; + P1SEL |= RXD + TXD; + P1SEL2 |= RXD + TXD; + + UCA0CTL1 = UCSWRST; + UCA0CTL1 = UCSSEL_2; //SMCLK + UCA0CTL0 = 0; + UCA0ABCTL = 0; + UCA0BR0 = divider; + UCA0BR1 = divider>>8; + UCA0MCTL = (oversampling ? UCOS16:0) | mod; + UCA0CTL1 &= ~UCSWRST; + UC0IE |= UCA0RXIE; +} + +void HardwareSerial::end() +{ + // wait for transmission of outgoing data + while (_tx_buffer->head != _tx_buffer->tail); + + _rx_buffer->head = _rx_buffer->tail; +} + +int HardwareSerial::available(void) +{ + return (unsigned int)(SERIAL_BUFFER_SIZE + _rx_buffer->head - _rx_buffer->tail) % SERIAL_BUFFER_SIZE; +} + +int HardwareSerial::peek(void) +{ + if (_rx_buffer->head == _rx_buffer->tail) { + return -1; + } else { + return _rx_buffer->buffer[_rx_buffer->tail]; + } +} + +int HardwareSerial::read(void) +{ + // if the head isn't ahead of the tail, we don't have any characters + if (_rx_buffer->head == _rx_buffer->tail) { + return -1; + } else { + unsigned char c = _rx_buffer->buffer[_rx_buffer->tail]; + _rx_buffer->tail = (unsigned int)(_rx_buffer->tail + 1) % SERIAL_BUFFER_SIZE; + return c; + } +} + +void HardwareSerial::flush() +{ + while (_tx_buffer->head != _tx_buffer->tail); +} + +size_t HardwareSerial::write(uint8_t c) +{ + unsigned int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE; + + // If the output buffer is full, there's nothing for it other than to + // wait for the interrupt handler to empty it a bit + // ???: return 0 here instead? + while (i == _tx_buffer->tail); + + _tx_buffer->buffer[_tx_buffer->head] = c; + _tx_buffer->head = i; + + UC0IE |= UCA0TXIE; + + return 1; +} + +void uart_rx_isr(void) +{ + unsigned char c = UCA0RXBUF; + store_char(c, &rx_buffer); +} + +void uart_tx_isr(void) +{ + if (tx_buffer.head == tx_buffer.tail) { + // Buffer empty, so disable interrupts + UC0IE &= ~UCA0TXIE; + return; + } + + unsigned char c = tx_buffer.buffer[tx_buffer.tail]; + tx_buffer.tail = (tx_buffer.tail + 1) % SERIAL_BUFFER_SIZE; + UCA0TXBUF = c; +} + +// Preinstantiate Objects ////////////////////////////////////////////////////// + +HardwareSerial Serial(&rx_buffer, &tx_buffer); + +#endif diff --git a/msp430/cores/msp430/HardwareSerial.h b/msp430/cores/msp430/HardwareSerial.h new file mode 100644 index 0000000..a04d8ef --- /dev/null +++ b/msp430/cores/msp430/HardwareSerial.h @@ -0,0 +1,65 @@ +/* + ************************************************************************ + * TimerSerial.h + * + * Arduino core files for MSP430 + * Copyright (c) 2012 Robert Wessels. All right reserved. + * + * + *********************************************************************** + Derived from: + HardwareSerial.cpp - Hardware serial library for Wiring + Copyright (c) 2006 Nicholas Zambetti. 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 HardwareSerial_h +#define HardwareSerial_h + +#ifdef __MSP430_HAS_USCI__ +#include <inttypes.h> + +#include "Stream.h" + +struct ring_buffer; + +class HardwareSerial : public Stream +{ + private: + uint8_t lock; + ring_buffer *_rx_buffer; + ring_buffer *_tx_buffer; + static void USCI0RX_ISR (void); + static void USCI0TX_ISR (void); + public: + HardwareSerial(ring_buffer *rx_buffer, ring_buffer *tx_buffer); + void begin(unsigned long); + void end(); + virtual int available(void); + virtual int peek(void); + virtual int read(void); + virtual void flush(void); + virtual size_t write(uint8_t); + using Print::write; // pull in write(str) and write(buf, size) from Print +}; + +extern HardwareSerial Serial; + +extern void serialEventRun(void) __attribute__((weak)); + +#endif // __MSP430_HAS_USCI__ + +#endif diff --git a/msp430/cores/msp430/Print.cpp b/msp430/cores/msp430/Print.cpp new file mode 100644 index 0000000..18c48f1 --- /dev/null +++ b/msp430/cores/msp430/Print.cpp @@ -0,0 +1,252 @@ +/* + 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 + Modified for msp403 2012 by Robert Wessels + */ + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <math.h> +#include "Energia.h" + +#include "Print.h" + +// Public Methods ////////////////////////////////////////////////////////////// + +/* default implementation: may be overridden */ +size_t Print::write(const uint8_t *buffer, size_t size) +{ + size_t n = 0; + while (size--) { + n += write(*buffer++); + } + return n; +} + +size_t Print::print(const String &s) +{ + size_t n = 0; + for (uint16_t i = 0; i < s.length(); i++) { + n += write(s[i]); + } + return n; +} + +size_t Print::print(const char str[]) +{ + return write(str); +} + +size_t Print::print(char c) +{ + return write(c); +} + +size_t Print::print(unsigned char b, int base) +{ + return print((unsigned long) b, base); +} + +size_t Print::print(int n, int base) +{ + return print((long) n, base); +} + +size_t Print::print(unsigned int n, int base) +{ + return print((unsigned long) n, base); +} + +size_t Print::print(long n, int base) +{ + if (base == 0) { + return write(n); + } else if (base == 10) { + if (n < 0) { + int t = print('-'); + n = -n; + return printNumber(n, 10) + t; + } + return printNumber(n, 10); + } else { + return printNumber(n, base); + } +} + +size_t Print::print(unsigned long n, int base) +{ + if (base == 0) return write(n); + else return printNumber(n, base); +} + +size_t Print::print(double n, int digits) +{ + return printFloat(n, digits); +} + +//size_t Print::println(const __FlashStringHelper *ifsh) +//{ +// size_t n = print(ifsh); +// n += println(); +// return n; +//} + +size_t Print::print(const Printable& x) +{ + return x.printTo(*this); +} + +size_t Print::println(void) +{ + size_t n = print('\r'); + n += print('\n'); + return n; +} + +size_t Print::println(const String &s) +{ + size_t n = print(s); + n += println(); + return n; +} + +size_t Print::println(const char c[]) +{ + size_t n = print(c); + n += println(); + return n; +} + +size_t Print::println(char c) +{ + size_t n = print(c); + n += println(); + return n; +} + +size_t Print::println(unsigned char b, int base) +{ + size_t n = print(b, base); + n += println(); + return n; +} + +size_t Print::println(int num, int base) +{ + size_t n = print(num, base); + n += println(); + return n; +} + +size_t Print::println(unsigned int num, int base) +{ + size_t n = print(num, base); + n += println(); + return n; +} + +size_t Print::println(long num, int base) +{ + size_t n = print(num, base); + n += println(); + return n; +} + +size_t Print::println(unsigned long num, int base) +{ + size_t n = print(num, base); + n += println(); + return n; +} + +size_t Print::println(double num, int digits) +{ + size_t n = print(num, digits); + n += println(); + return n; +} + +size_t Print::println(const Printable& x) +{ + size_t n = print(x); + n += println(); + return n; +} + +// Private Methods ///////////////////////////////////////////////////////////// + +size_t Print::printNumber(unsigned long n, uint8_t base) { + char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte. + char *str = &buf[sizeof(buf) - 1]; + + *str = '\0'; + + // prevent crash if called with base == 1 + if (base < 2) base = 10; + + do { + unsigned long m = n; + n /= base; + char c = m - base * n; + *--str = c < 10 ? c + '0' : c + 'A' - 10; + } while(n); + + return write(str); +} + +size_t Print::printFloat(double number, uint8_t digits) +{ + size_t n = 0; + + // Handle negative numbers + if (number < 0.0) + { + n += print('-'); + number = -number; + } + + // Round correctly so that print(1.999, 2) prints as "2.00" + double rounding = 0.5; + for (uint8_t i=0; i<digits; ++i) + rounding /= 10.0; + + number += rounding; + + // Extract the integer part of the number and print it + unsigned long int_part = (unsigned long)number; + double remainder = number - (double)int_part; + n += print(int_part); + + // Print the decimal point, but only if there are digits beyond + if (digits > 0) { + n += print("."); + } + + // Extract digits from the remainder one at a time + while (digits-- > 0) + { + remainder *= 10.0; + int toPrint = int(remainder); + n += print(toPrint); + remainder -= toPrint; + } + + return n; +} diff --git a/msp430/cores/msp430/Print.h b/msp430/cores/msp430/Print.h new file mode 100644 index 0000000..65ca598 --- /dev/null +++ b/msp430/cores/msp430/Print.h @@ -0,0 +1,85 @@ +/* + 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 <inttypes.h> +#include <stdio.h> // for size_t + +#include "WString.h" +#include "Printable.h" + +#define DEC 10 +#define HEX 16 +#define OCT 8 +#define BIN 2 + +class Print +{ + private: + int write_error; + size_t printNumber(unsigned long, uint8_t); + size_t printFloat(double, uint8_t); + + // Prevent heap allocation + void * operator new (size_t); + void * operator new[] (size_t); + void operator delete (void *); + void operator delete[] (void*); + + protected: + void setWriteError(int err = 1) { write_error = err; } + public: + Print() : write_error(0) {} + + int getWriteError() { return write_error; } + void clearWriteError() { setWriteError(0); } + + virtual size_t write(uint8_t) = 0; + size_t write(const char *str) { return write((const uint8_t *)str, strlen(str)); } + virtual size_t write(const uint8_t *buffer, size_t size); + + //size_t print(const __FlashStringHelper *); + size_t print(const String &); + size_t print(const char[]); + size_t print(char); + size_t print(unsigned char, int = DEC); + size_t print(int, int = DEC); + size_t print(unsigned int, int = DEC); + size_t print(long, int = DEC); + size_t print(unsigned long, int = DEC); + size_t print(double, int = 2); + size_t print(const Printable&); + + //size_t println(const __FlashStringHelper *); + size_t println(const String &s); + size_t println(const char[]); + size_t println(char); + size_t println(unsigned char, int = DEC); + size_t println(int, int = DEC); + size_t println(unsigned int, int = DEC); + size_t println(long, int = DEC); + size_t println(unsigned long, int = DEC); + size_t println(double, int = 2); + size_t println(const Printable&); + size_t println(void); +}; + +#endif diff --git a/msp430/cores/msp430/Printable.h b/msp430/cores/msp430/Printable.h new file mode 100644 index 0000000..d03c9af --- /dev/null +++ b/msp430/cores/msp430/Printable.h @@ -0,0 +1,40 @@ +/* + Printable.h - Interface class that allows printing of complex types + Copyright (c) 2011 Adrian McEwen. 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 Printable_h +#define Printable_h + +#include <new.h> + +class Print; + +/** The Printable class provides a way for new classes to allow themselves to be printed. + By deriving from Printable and implementing the printTo method, it will then be possible + for users to print out instances of this class by passing them into the usual + Print::print and Print::println methods. +*/ + +class Printable +{ + public: + virtual size_t printTo(Print& p) const = 0; +}; + +#endif + diff --git a/msp430/cores/msp430/Stream.cpp b/msp430/cores/msp430/Stream.cpp new file mode 100644 index 0000000..c7d1863 --- /dev/null +++ b/msp430/cores/msp430/Stream.cpp @@ -0,0 +1,246 @@ +/* + Stream.cpp - adds parsing methods to Stream class + 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 + + Created July 2011 + parsing functions based on TextFinder library by Michael Margolis + */ + +#include "Energia.h" +#include "Stream.h" + +#define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait +#define NO_SKIP_CHAR 1 // a magic char not found in a valid ASCII numeric field + +// private method to read stream with timeout +int Stream::timedRead() +{ + int c; + _startMillis = millis(); + do { + c = read(); + if (c >= 0) return c; + } while(millis() - _startMillis < _timeout); + return -1; // -1 indicates timeout +} + +// private method to peek stream with timeout +int Stream::timedPeek() +{ + int c; + _startMillis = millis(); + do { + c = peek(); + if (c >= 0) return c; + } while(millis() - _startMillis < _timeout); + return -1; // -1 indicates timeout +} + +// returns peek of the next digit in the stream or -1 if timeout +// discards non-numeric characters +int Stream::peekNextDigit() +{ + int c; + while (1) { + c = timedPeek(); + if (c < 0) return c; // timeout + if (c == '-') return c; + if (c >= '0' && c <= '9') return c; + read(); // discard non-numeric + } +} + +// Public Methods +////////////////////////////////////////////////////////////// + +void Stream::setTimeout(unsigned long timeout) // sets the maximum number of milliseconds to wait +{ + _timeout = timeout; +} + + // find returns true if the target string is found +bool Stream::find(char *target) +{ + return findUntil(target, NULL); +} + +// reads data from the stream until the target string of given length is found +// returns true if target string is found, false if timed out +bool Stream::find(char *target, size_t length) +{ + return findUntil(target, length, NULL, 0); +} + +// as find but search ends if the terminator string is found +bool Stream::findUntil(char *target, char *terminator) +{ + return findUntil(target, strlen(target), terminator, strlen(terminator)); +} + +// reads data from the stream until the target string of the given length is found +// search terminated if the terminator string is found +// returns true if target string is found, false if terminated or timed out +bool Stream::findUntil(char *target, size_t targetLen, char *terminator, size_t termLen) +{ + size_t index = 0; // maximum target string length is 64k bytes! + size_t termIndex = 0; + int c; + + if( *target == 0) + return true; // return true if target is a null string + while( (c = timedRead()) > 0){ + + if(c != target[index]) + index = 0; // reset index if any char does not match + + if( c == target[index]){ + //////Serial.print("found "); Serial.write(c); Serial.print("index now"); Serial.println(index+1); + if(++index >= targetLen){ // return true if all chars in the target match + return true; + } + } + + if(termLen > 0 && c == terminator[termIndex]){ + if(++termIndex >= termLen) + return false; // return false if terminate string found before target string + } + else + termIndex = 0; + } + return false; +} + + +// returns the first valid (long) integer value from the current position. +// initial characters that are not digits (or the minus sign) are skipped +// function is terminated by the first character that is not a digit. +long Stream::parseInt() +{ + return parseInt(NO_SKIP_CHAR); // terminate on first non-digit character (or timeout) +} + +// as above but a given skipChar is ignored +// this allows format characters (typically commas) in values to be ignored +long Stream::parseInt(char skipChar) +{ + boolean isNegative = false; + long value = 0; + int c; + + c = peekNextDigit(); + // ignore non numeric leading characters + if(c < 0) + return 0; // zero returned if timeout + + do{ + if(c == skipChar) + ; // ignore this charactor + else if(c == '-') + isNegative = true; + else if(c >= '0' && c <= '9') // is c a digit? + value = value * 10 + c - '0'; + read(); // consume the character we got with peek + c = timedPeek(); + } + while( (c >= '0' && c <= '9') || c == skipChar ); + + if(isNegative) + value = -value; + return value; +} + + +// as parseInt but returns a floating point value +float Stream::parseFloat() +{ + return parseFloat(NO_SKIP_CHAR); +} + +// as above but the given skipChar is ignored +// this allows format characters (typically commas) in values to be ignored +float Stream::parseFloat(char skipChar){ + boolean isNegative = false; + boolean isFraction = false; + long value = 0; + char c; + float fraction = 1.0; + + c = peekNextDigit(); + // ignore non numeric leading characters + if(c < 0) + return 0; // zero returned if timeout + + do{ + if(c == skipChar) + ; // ignore + else if(c == '-') + isNegative = true; + else if (c == '.') + isFraction = true; + else if(c >= '0' && c <= '9') { // is c a digit? + value = value * 10 + c - '0'; + if(isFraction) + fraction *= 0.1; + } + read(); // consume the character we got with peek + c = timedPeek(); + } + while( (c >= '0' && c <= '9') || c == '.' || c == skipChar ); + + if(isNegative) + value = -value; + if(isFraction) + return value * fraction; + else + return value; +} + +// read characters from stream into buffer +// terminates if length characters have been read, or timeout (see setTimeout) +// returns the number of characters placed in the buffer +// the buffer is NOT null terminated. +// +size_t Stream::readBytes(char *buffer, size_t length) +{ + size_t count = 0; + while (count < length) { + int c = timedRead(); + if (c < 0) break; + *buffer++ = (char)c; + count++; + } + return count; +} + + +// as readBytes with terminator character +// terminates if length characters have been read, timeout, or if the terminator character detected +// returns the number of characters placed in the buffer (0 means no valid data found) + +size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length) +{ + if (length < 1) return 0; + size_t index = 0; + while (index < length) { + int c = timedRead(); + if (c < 0 || c == terminator) break; + *buffer++ = (char)c; + index++; + } + return index; // return number of characters, not including null terminator +} + diff --git a/msp430/cores/msp430/Stream.h b/msp430/cores/msp430/Stream.h new file mode 100644 index 0000000..13f11be --- /dev/null +++ b/msp430/cores/msp430/Stream.h @@ -0,0 +1,94 @@ +/* + Stream.h - base class for character-based streams. + Copyright (c) 2010 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 + + parsing functions based on TextFinder library by Michael Margolis +*/ + +#ifndef Stream_h +#define Stream_h + +#include <inttypes.h> +#include "Print.h" + +// compatability macros for testing +/* +#define getInt() parseInt() +#define getInt(skipChar) parseInt(skipchar) +#define getFloat() parseFloat() +#define getFloat(skipChar) parseFloat(skipChar) +#define getString( pre_string, post_string, buffer, length) +readBytesBetween( pre_string, terminator, buffer, length) +*/ + +class Stream : public Print +{ + private: + unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read + unsigned long _startMillis; // used for timeout measurement + int timedRead(); // private method to read stream with timeout + int timedPeek(); // private method to peek stream with timeout + int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout + + public: + virtual int available() = 0; + virtual int read() = 0; + virtual int peek() = 0; + virtual void flush() = 0; + + Stream() {_timeout=1000;} + +// parsing methods + + void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second + + bool find(char *target); // reads data from the stream until the target string is found + // returns true if target string is found, false if timed out (see setTimeout) + + bool find(char *target, size_t length); // reads data from the stream until the target string of given length is found + // returns true if target string is found, false if timed out + + bool findUntil(char *target, char *terminator); // as find but search ends if the terminator string is found + + bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found + + + long parseInt(); // returns the first valid (long) integer value from the current position. + // initial characters that are not digits (or the minus sign) are skipped + // integer is terminated by the first character that is not a digit. + + float parseFloat(); // float version of parseInt + + size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer + // terminates if length characters have been read or timeout (see setTimeout) + // returns the number of characters placed in the buffer (0 means no valid data found) + + size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character + // terminates if length characters have been read, timeout, or if the terminator character detected + // returns the number of characters placed in the buffer (0 means no valid data found) + + // Arduino String functions to be added here + + protected: + long parseInt(char skipChar); // as above but the given skipChar is ignored + // as above but the given skipChar is ignored + // this allows format characters (typically commas) in values to be ignored + + float parseFloat(char skipChar); // as above but the given skipChar is ignored +}; + +#endif diff --git a/msp430/cores/msp430/TimerSerial.cpp b/msp430/cores/msp430/TimerSerial.cpp new file mode 100644 index 0000000..a6b6e85 --- /dev/null +++ b/msp430/cores/msp430/TimerSerial.cpp @@ -0,0 +1,256 @@ +/* + ************************************************************************ + * TimerSerial.cpp + * + * Arduino core files for MSP430 + * Copyright (c) 2012 Robert Wessels. All right reserved. + * + * + *********************************************************************** + Derived from: + HardwareSerial.cpp - Hardware serial library for Wiring + Copyright (c) 2006 Nicholas Zambetti. All right reserved. + and + msp430softserial by Rick Kimball + https://github.com/RickKimball + + 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 +*/ + +#include "Energia.h" +#include "TimerSerial.h" + +#define SERIAL_BUFFER_SIZE 16 + +#ifndef TIMERA0_VECTOR + #define TIMERA0_VECTOR TIMER0_A0_VECTOR +#endif /* TIMER0_A0_VECTOR */ + +#ifndef TIMERA1_VECTOR + #define TIMERA1_VECTOR TIMER0_A1_VECTOR +#endif /* TIMERA1_VECTOR */ + +struct ring_buffer_ts +{ + volatile unsigned int head; + volatile unsigned int tail; + unsigned char buffer[SERIAL_BUFFER_SIZE]; +}; + +/** + * uint8x2_t - optimized structure storage for ISR. Fits our static variables in one register + * This tweak allows the ISR to use one less register saving a push and pop + * We also save a couple of instructions being able to write to both values with + * one mov.w instruction. + */ +typedef union uint8x2_t { + //---------- word access + uint16_t mask_data; // access both as a word: mask is low byte, data is high byte + //--- or --- individual byte access + struct { + uint8_t mask:8; // bit mask to set data bits. Also used as a loop end flag + uint8_t data:8; // working value for bits received + } b; +} uint8x2_t; + +// --- --- +static volatile unsigned int USARTTXBUF; +static uint16_t TICKS_PER_BIT; +static uint16_t TICKS_PER_BIT_DIV2; +static ring_buffer_ts rx_buffer; + +#if NEEDS_BUFF_PTR + static ring_buffer_ts tx_buffer; // required for the g2231, without it we get garbage +#endif + +#ifndef __MSP430_HAS_USCI__ + TimerSerial Serial; +#endif + +TimerSerial::TimerSerial() +{ +#if NEEDS_BUFF_PTR + _rx_buffer = &rx_buffer; + _tx_buffer = &tx_buffer; +#endif +} + +void TimerSerial::begin(register unsigned long baud) +{ + P1OUT |= TX_PIN | RX_PIN; // Initialize all GPIO + P1SEL |= TX_PIN | RX_PIN; // Enabled Timer ISR function for TXD/RXD pins + P1DIR |= TX_PIN; // Enable TX_PIN for output + + TACCTL0 = OUT; // Set TXD Idle state as Mark = '1', +3.3 volts normal + TACCTL1 = SCS | CM1 | CAP | CCIE; // Sync TACLK and MCLK, Detect Neg Edge, Enable Capture mode and RX Interrupt + TACTL = TASSEL_2 | MC_2 | TACLR; // Clock TIMERA from SMCLK, run in continuous mode counting from to 0-0xFFFF + +#if F_CPU == 1000000 + baud = (baud<=4800) ? baud : 4800; // force 4800 for slow F_CPU +#endif + + TICKS_PER_BIT = F_CPU / baud; + TICKS_PER_BIT_DIV2 = TICKS_PER_BIT >> 1; +} + +void TimerSerial::end() +{ + while (TACCTL0 & CCIE) { + ; // wait for previous xmit to finish + } + + P1SEL &= ~TX_PIN; // P1 functions revert back to default + P1DIR &= ~TX_PIN; // Input +} + +int TimerSerial::read() +{ + register uint16_t temp_tail=rx_buffer.tail; + + if (rx_buffer.head != temp_tail) { + uint8_t c = rx_buffer.buffer[temp_tail++]; + rx_buffer.tail = temp_tail % SERIAL_BUFFER_SIZE; + return c; + } + else { + return -1; + } +} + +int TimerSerial::available() +{ + unsigned cnt = (rx_buffer.head - rx_buffer.tail) % SERIAL_BUFFER_SIZE; + + return cnt; +} + +void TimerSerial::flush() +{ + while (TACCTL0 & CCIE) { + ; // wait for previous xmit to finish + } +} + +int TimerSerial::peek() +{ + register uint16_t temp_tail=rx_buffer.tail; + + if (rx_buffer.head == temp_tail) { + return rx_buffer.buffer[temp_tail]; + } + else { + return -1; + } +} + +size_t TimerSerial::write(uint8_t c) +{ + // TIMERA0 disables the interrupt flag when it has sent + // the final stop bit. While a transmit is in progress the + // interrupt is enabled + while (TACCTL0 & CCIE) { + ; // wait for previous xmit to finish + } + + // make the next output at least TICKS_PER_BIT in the future + // so we don't stomp on the the stop bit from our previous xmt + + TACCR0 = TAR; // resync with current TimerA clock + TACCR0 += TICKS_PER_BIT; // setup the next timer tick + TACCTL0 = OUTMOD0 + CCIE; // set TX_PIN HIGH and reenable interrupts + + // now that we have set the next interrupt in motion + // we quickly need to set the TX data. Hopefully the + // next 2 lines happens before the next timer tick. + + // Note: This code makes great use of multiple peripherals + // + // In the code above, we start with a busy wait on the CCIE + // interrupt flag. As soon as it is available, we setup the next + // send time and then enable the interrupt. Until that time happens, + // we have a few free cycles available to stuff the start and stop bits + // into the data buffer before the timer ISR kicks in and handles + // the event. Note: if you are using a really slow clock or a really + // fast baud rate you could run into problems if the interrupt is + // triggered before you have finished with the USARTTXBUF + + register unsigned value = c | 0x100; // add stop bit '1' + value <<= 1; // Add the start bit '0' + USARTTXBUF=value; // queue up the byte for xmit + return 1; +} + +#ifndef __GNUC__ +#pragma vector = TIMERA0_VECTOR +__interrupt +#else +__attribute__((interrupt(TIMERA0_VECTOR))) +#endif +static void TimerSerial__TxIsr(void) +{ + TACCR0 += TICKS_PER_BIT; // setup next time to send a bit, OUT will be set then + + TACCTL0 |= OUTMOD2; // reset OUT (set to 0) OUTMOD2|OUTMOD0 (0b101) + if ( USARTTXBUF & 0x01 ) { // look at LSB if 1 then set OUT high + TACCTL0 &= ~OUTMOD2; // set OUT (set to 1) OUTMOD0 (0b001) + } + + if (!(USARTTXBUF >>= 1)) { // All bits transmitted ? + TACCTL0 &= ~CCIE; // disable interrupt, indicates we are done + } +} + +#define store_rxchar(c) { \ + register unsigned int next_head;\ + next_head = rx_buffer.head;\ + rx_buffer.buffer[next_head++]=c; \ + next_head %= SERIAL_BUFFER_SIZE; \ + if ( next_head != rx_buffer.tail ) { \ + rx_buffer.head = next_head; \ + } \ +} + +#ifndef __GNUC__ +#pragma vector = TIMERA1_VECTOR +__interrupt +#else +__attribute__((interrupt(TIMERA1_VECTOR))) +#endif +static void TimerSerial__RxIsr(void) +{ + static uint8x2_t rx_bits; // persistent storage for data and mask. fits in one 16 bit register + volatile uint16_t resetTAIVIFG; // just reading TAIV will reset the interrupt flag + resetTAIVIFG=TAIV;(void)resetTAIVIFG; + + register uint16_t regCCTL1=TA0CCTL1; // using a temp register provides a slight performance improvement + + TA0CCR1 += TICKS_PER_BIT; // Setup next time to sample + + if (regCCTL1 & CAP) { // Are we in capture mode? If so, this is a start bit + TA0CCR1 += TICKS_PER_BIT_DIV2; // adjust sample time, so next sample is in the middle of the bit width + rx_bits.mask_data = 0x0001; // initialize both values, set data to 0x00 and mask to 0x01 + TA0CCTL1 = regCCTL1 & ~CAP; // Switch from capture mode to compare mode + } + else { + if (regCCTL1 & SCCI) { // sampled bit value from receive latch + rx_bits.b.data|=rx_bits.b.mask; // if latch is high, then set the bit using the sliding mask + } + + if (!(rx_bits.b.mask <<= 1)) { // Are all bits received? Use the mask to end loop + store_rxchar(rx_bits.b.data); // Store the bits into the rx_buffer + TA0CCTL1 = regCCTL1 | CAP; // Switch back to capture mode and wait for next start bit (HI->LOW) + } + } +} diff --git a/msp430/cores/msp430/TimerSerial.h b/msp430/cores/msp430/TimerSerial.h new file mode 100644 index 0000000..eee6259 --- /dev/null +++ b/msp430/cores/msp430/TimerSerial.h @@ -0,0 +1,78 @@ +/* + TimerSerial.h - Timer based serial library for MSP430 + Copyright (c) 2012 Robert Wessels. All right reserved. + Modeled after Nicholas Zambetti's HardwareSerial. + and + msp430softserial by Rick Kimball + https://github.com/RickKimball/msp430softserial/ + + 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 TimerSerial_h +#define TimerSerial_h + +#include <inttypes.h> +#include <Stream.h> + +#define TX_PIN BIT1 // TXD on P1.1 +#define RX_PIN BIT2 // RXD on P1.2 + +// running at < 3MHz requires a lower baud rate +#ifndef TIMERSERIAL_BAUD +#if F_CPU > 1000000 + #define TIMERSERIAL_BAUD 9600 +#else + #define TIMERSERIAL_BAUD 4800 +#endif +#endif + +#if defined(__MSP430G2231__) + #define NEEDS_BUFF_PTR 1 // sadly, the g2231 seems to have a problem if we don't use the original structure + struct ring_buffer_ts; // forward declaration +#else + #define NEEDS_BUFF_PTR 0 // everything else is happy to run fully optimized +#endif + +class TimerSerial : public Stream +{ +public: + TimerSerial(void); + + void begin(unsigned long baud = TIMERSERIAL_BAUD); + void end(void); + + virtual size_t write(uint8_t byte); + virtual int read(void); + virtual int available(void); + virtual void flush(void); + virtual int peek(void); + + using Print::write; + +private: + +#if NEEDS_BUFF_PTR + ring_buffer_ts *_rx_buffer; // gcc seems to get confused on the g2231 without this + ring_buffer_ts *_tx_buffer; +#endif + +}; + +#ifndef __MSP430_HAS_USCI__ +extern TimerSerial Serial; +#endif + +#endif diff --git a/msp430/cores/msp430/Tone.cpp b/msp430/cores/msp430/Tone.cpp new file mode 100644 index 0000000..e4fc506 --- /dev/null +++ b/msp430/cores/msp430/Tone.cpp @@ -0,0 +1,223 @@ +/* Tone.cpp + + A Tone Generator Library - Modified for Energia + Implements up to 3 (software) PWM outputs using TIMERA0 compare registers and IRQ. + Can use any digital output pin for pulse generation + + (c) 2012 - Peter Brier. + + Based on code Originally written by Brett Hagman + + 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 + +Version Modified By Date Comments +------- ----------- -------- -------- +0001 B Hagman 09/08/02 Initial coding +0002 B Hagman 09/08/18 Multiple pins +0003 B Hagman 09/08/18 Moved initialization from constructor to begin() +0004 B Hagman 09/09/26 Fixed problems with ATmega8 +0005 B Hagman 09/11/23 Scanned prescalars for best fit on 8 bit timers + 09/11/25 Changed pin toggle method to XOR + 09/11/25 Fixed timer0 from being excluded +0006 D Mellis 09/12/29 Replaced objects with functions +0007 M Sproul 10/08/29 Changed #ifdefs from cpu to register +0008 P Brier 12/05/28 Modified for TI MSP430 processor +0009 P Brier 12/05/29 Fixed problem with re-init of expired tone +*************************************************/ + +#include "wiring_private.h" +#include "pins_energia.h" +#include "Energia.h" + +// local funcions +static void initTimers(); +static void setTimer(uint8_t n, unsigned int frequency, unsigned long duration); +static void stopTimer(uint8_t n); + +// timer clock frequency set to clock/8, at F_CPU = 1MHZ this gives an output freq range of ~[1Hz ..65Khz] and at 16Mhz this is ~[16Hz .. 1MHz] +#define F_TIMER (F_CPU/8L) + +#ifdef __MSP430_HAS_TA3__ +#define AVAILABLE_TONE_PINS 3 +#define SETARRAY(a) a,a,a +#else +#define AVAILABLE_TONE_PINS 2 +#define SETARRAY(a) a,a +#endif + + +// tone_duration: +// > 0 - duration specified +// = 0 - stopped +// < 0 - infinitely (until stop() method called, or new play() called) + +static uint8_t tone_state = 0; // 0==not initialized, 1==timer running +static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { SETARRAY(255) }; +static uint8_t tone_bit[AVAILABLE_TONE_PINS] = { SETARRAY(255) }; +volatile static uint16_t *tone_out[AVAILABLE_TONE_PINS] = { SETARRAY(0) }; +static uint16_t tone_interval[AVAILABLE_TONE_PINS] = { SETARRAY(-1) }; +static int16_t tone_periods[AVAILABLE_TONE_PINS] = { SETARRAY(0) }; + + +/** +*** tone() -- Output a tone (50% Dutycycle PWM signal) on a pin +*** pin: This pin is selected as output +*** frequency: [Hertz] +** duration: [milliseconds], if duration <=0, then we output tone continously, otherwise tone is stopped after this time (output = 0) +**/ +void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) +{ + uint8_t port = digitalPinToPort(_pin); + if (port == NOT_A_PORT) return; + + // find if we are using it at the moment, if so: update it + for (int i = 0; i < AVAILABLE_TONE_PINS; i++) + { + if (tone_pins[i] == _pin) + { + setTimer(i, frequency, duration); + return; // we are done, timer reprogrammed + } + } + + // new tone pin, find empty timer and set it + for (int i = 0; i < AVAILABLE_TONE_PINS; i++) + { + if (tone_pins[i] == 255) + { + tone_pins[i] = _pin; + tone_bit[i] = digitalPinToBitMask(_pin); + tone_out[i] = portOutputRegister(port); + if ( tone_state == 0 ) + initTimers(); + pinMode(_pin, OUTPUT); + setTimer(i, frequency, duration); + return; // we are done, timer set + } + } + // if we exit here, no unused timer was found, nothing is done +} + + +/** +*** noTone() - Stop outputting the tone on a pin +**/ +void noTone(uint8_t _pin) +{ + if ( _pin == 255 ) return; // Should not happen! + for (int i = 0; i < AVAILABLE_TONE_PINS; i++) + { + if (tone_pins[i] == _pin) + { + tone_pins[i] = 255; + stopTimer(i); + } + } +} + + +// Initialize the timers - Set mode and Enable IRQ +static void inline initTimers() +{ + // disable IRQs + TA0CCTL0 = 0; + TA0CCTL1 = 0; +#ifdef __MSP430_HAS_TA3__ + TA0CCTL2 = 0; +#endif + TA0CTL = TACLR + TASSEL_2 + ID_3 + MC_2; // clear counter, source=SMCLK/8, mode=continous count up + tone_state = 1; // init is done +} + + +// Set the timer interval and duration +// frequency in [Hz] and duration in [msec] +// we initialize the timer match value only if the tone was not running already, to prevent glitches when re-programming a running tone +static void setTimer(uint8_t n, unsigned int frequency, unsigned long duration) +{ + if ( frequency <= 0 ) + { + tone_interval[n] = 0; + tone_periods[n] = 0; + return; + } + tone_interval[n] = F_TIMER / (2L*frequency); + if ( duration > 0 ) + tone_periods[n] = (duration * (F_TIMER/2)) / (1000L * tone_interval[n]); + else + tone_periods[n] = -1; + switch( n ) // enable IRQ and set next match time in various timer compare registers (if we where not enabled already) + { + case 0: + if ( ! (TA0CCTL0 & CCIE) ) TA0CCR0 = TAR + tone_interval[0]; + TA0CCTL0 = CCIE; + break; + case 1: + if ( !(TA0CCTL1 & CCIE) ) TA0CCR1 = TAR + tone_interval[1]; + TA0CCTL1 = CCIE; + break; +#ifdef __MSP430_HAS_TA3__ + case 2: + if ( !(TA0CCTL2 & CCIE) ) TA0CCR2 = TAR + tone_interval[2]; + TA0CCTL2 = CCIE; + break; +#endif + } +} + +/* stopTimer() - Disable timer IRQ */ +static void inline stopTimer(uint8_t n) +{ + switch( n ) + { + case 0: TA0CCTL0 = 0; break; + case 1: TA0CCTL1 = 0; break; +#ifdef __MSP430_HAS_TA3__ + case 2: TA0CCTL2 = 0; break; +#endif + } + *tone_out[n] &= ~tone_bit[n]; +} + + +// Peform the isr magic, toggle output, decrease duation if > 0, and stop if duration == 0, continous if duration < 0 +// set new interval - defined as macro to limit ISR overhead (at the expense of some code size) +#define isrTimer(n,ccr) do { \ + *tone_out[n] ^= tone_bit[n]; \ + if ( tone_periods[n] == 0 ) stopTimer(n);\ + else if ( tone_periods[n] > 0) tone_periods[n]--; \ + ccr += tone_interval[n]; \ +} while(0) + + +// TIMERA vector (CCR0) +__attribute__((interrupt(TIMER0_A0_VECTOR))) +void TIMER0_A0_ISR(void) +{ + isrTimer(0, TA0CCR0); +} + +// TAIV vector (CCR1/CCR2) +__attribute__((interrupt(TIMER0_A1_VECTOR))) +void TIMER0_A1_ISR(void) +{ + switch ( TAIV ) + { + case 0x2: isrTimer(1, TA0CCR1); break; // CCR1 +#ifdef __MSP430_HAS_TA3__ + case 0x4: isrTimer(2, TA0CCR2); break; // CCR2 +#endif + } +} diff --git a/msp430/cores/msp430/WCharacter.h b/msp430/cores/msp430/WCharacter.h new file mode 100644 index 0000000..074539c --- /dev/null +++ b/msp430/cores/msp430/WCharacter.h @@ -0,0 +1,170 @@ +/* + WCharacter.h - Character utility functions for Wiring & Arduino + Copyright (c) 2010 Hernando Barragan. 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 Character_h +#define Character_h + +#include <ctype.h> + +// WCharacter.h prototypes +inline boolean isAlphaNumeric(int c) __attribute__((always_inline)); +inline boolean isAlpha(int c) __attribute__((always_inline)); +inline boolean isAscii(int c) __attribute__((always_inline)); +inline boolean isWhitespace(int c) __attribute__((always_inline)); +inline boolean isControl(int c) __attribute__((always_inline)); +inline boolean isDigit(int c) __attribute__((always_inline)); +inline boolean isGraph(int c) __attribute__((always_inline)); +inline boolean isLowerCase(int c) __attribute__((always_inline)); +inline boolean isPrintable(int c) __attribute__((always_inline)); +inline boolean isPunct(int c) __attribute__((always_inline)); +inline boolean isSpace(int c) __attribute__((always_inline)); +inline boolean isUpperCase(int c) __attribute__((always_inline)); +inline boolean isHexadecimalDigit(int c) __attribute__((always_inline)); +inline int toAscii(int c) __attribute__((always_inline)); +inline int toLowerCase(int c) __attribute__((always_inline)); +inline int toUpperCase(int c)__attribute__((always_inline)); + + +// Checks for an alphanumeric character. +// It is equivalent to (isalpha(c) || isdigit(c)). +inline boolean isAlphaNumeric(int c) +{ + return ( isalnum(c) == 0 ? false : true); +} + + +// Checks for an alphabetic character. +// It is equivalent to (isupper(c) || islower(c)). +inline boolean isAlpha(int c) +{ + return ( isalpha(c) == 0 ? false : true); +} + + +// Checks whether c is a 7-bit unsigned char value +// that fits into the ASCII character set. +inline boolean isAscii(int c) +{ + return ( isascii (c) == 0 ? false : true); +} + + +// Checks for a blank character, that is, a space or a tab. +inline boolean isWhitespace(int c) +{ + return ( isblank (c) == 0 ? false : true); +} + + +// Checks for a control character. +inline boolean isControl(int c) +{ + return ( iscntrl (c) == 0 ? false : true); +} + + +// Checks for a digit (0 through 9). +inline boolean isDigit(int c) +{ + return ( isdigit (c) == 0 ? false : true); +} + + +//TODO: mspgcc does not seem to have isgraph?!? + +//// Checks for any printable character except space. +//inline boolean isGraph(int c) +//{ +// return ( isgraph (c) == 0 ? false : true); +//} + + +// Checks for a lower-case character. +inline boolean isLowerCase(int c) +{ + return (islower (c) == 0 ? false : true); +} + + +// Checks for any printable character including space. +inline boolean isPrintable(int c) +{ + return ( isprint (c) == 0 ? false : true); +} + + +// Checks for any printable character which is not a space +// or an alphanumeric character. +inline boolean isPunct(int c) +{ + return ( ispunct (c) == 0 ? false : true); +} + + +// Checks for white-space characters. For the avr-libc library, +// these are: space, formfeed ('\f'), newline ('\n'), carriage +// return ('\r'), horizontal tab ('\t'), and vertical tab ('\v'). +inline boolean isSpace(int c) +{ + return ( isspace (c) == 0 ? false : true); +} + + +// Checks for an uppercase letter. +inline boolean isUpperCase(int c) +{ + return ( isupper (c) == 0 ? false : true); +} + + +// Checks for a hexadecimal digits, i.e. one of 0 1 2 3 4 5 6 7 +// 8 9 a b c d e f A B C D E F. +inline boolean isHexadecimalDigit(int c) +{ + return ( isxdigit (c) == 0 ? false : true); +} + + +// Converts c to a 7-bit unsigned char value that fits into the +// ASCII character set, by clearing the high-order bits. +inline int toAscii(int c) +{ + return toascii (c); +} + + +// Warning: +// Many people will be unhappy if you use this function. +// This function will convert accented letters into random +// characters. + +// Converts the letter c to lower case, if possible. +inline int toLowerCase(int c) +{ + return tolower (c); +} + + +// Converts the letter c to upper case, if possible. +inline int toUpperCase(int c) +{ + return toupper (c); +} + +#endif diff --git a/msp430/cores/msp430/WInterrupts.c b/msp430/cores/msp430/WInterrupts.c new file mode 100644 index 0000000..bce2afe --- /dev/null +++ b/msp430/cores/msp430/WInterrupts.c @@ -0,0 +1,138 @@ +/* + ************************************************************************ + * WInterrupts.c + * + * Arduino core files for MSP430 + * Copyright (c) 2012 Robert Wessels. All right reserved. + * + * + *********************************************************************** + Derived from: + + WInterrupts.c Part of the Wiring project - http://wiring.uniandes.edu.co + + Copyright (c) 2004-05 Hernando Barragan + + 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 + + Modified 24 November 2006 by David A. Mellis + Modified 1 August 2010 by Mark Sproul +*/ + +#include <inttypes.h> +#include <stdio.h> + +#include "wiring_private.h" + +#ifndef BV +#define BV(x) (1 << (x)) +#endif + +#define bit_pos(A) ((A) == 1u << 0 ? 0 \ +: (A) == 1u << 1 ? 1 \ +: (A) == 1u << 2 ? 2 \ +: (A) == 1u << 3 ? 3 \ +: (A) == 1u << 4 ? 4 \ +: (A) == 1u << 5 ? 5 \ +: (A) == 1u << 6 ? 6 \ +: (A) == 1u << 7 ? 7 \ +: 0) + +#define NUM_INTS_PER_PORT 8 +static volatile voidFuncPtr intFuncP1[NUM_INTS_PER_PORT]; +#if defined(__MSP430_HAS_PORT2_R__) +static volatile voidFuncPtr intFuncP2[NUM_INTS_PER_PORT]; +#endif + +void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) { + uint8_t bit = digitalPinToBitMask(interruptNum); + uint8_t port = digitalPinToPort(interruptNum); + + + if ((port == NOT_A_PIN) || !((mode == FALLING) || (mode == RISING))) return; + + __dint(); + + switch(port) { + case P1: + P1IE |= bit; + P1IFG &= ~bit; + P1IES = mode ? P1IES | bit : P1IES & ~bit; + intFuncP1[bit_pos(bit)] = userFunc; + break; + #if defined(__MSP430_HAS_PORT2_R__) + case P2: + P2IE |= bit; + P2IFG &= bit; + P2IES = mode ? P2IES | bit : P2IES & ~bit; + intFuncP2[bit_pos(bit)] = userFunc; + break; + #endif + default: + break; + } + + __eint(); +} + +void detachInterrupt(uint8_t interruptNum) { + uint8_t bit = digitalPinToBitMask(interruptNum); + uint8_t port = digitalPinToPort(interruptNum); + + if (port == NOT_A_PIN) return; + + switch(port) { + case P1: + P1IE &= ~bit; + intFuncP1[bit_pos(bit)] = 0; + break; + #if defined(__MSP430_HAS_PORT2_R__) + case P2: + P2IE &= ~bit; + intFuncP2[bit_pos(bit)] = 0; + break; + #endif + default: + break; + } +} + + +__attribute__((interrupt(PORT1_VECTOR))) +void Port_1(void) +{ + uint8_t i; + for(i = 0; i < 8; i++) { + if((P1IFG & BV(i)) && intFuncP1[i]) { + intFuncP1[i](); + P1IFG &= ~BV(i); + } + } +} + +#if defined(__MSP430_HAS_PORT2_R__) +__attribute__((interrupt(PORT2_VECTOR))) +void Port_2(void) +{ + uint8_t i; + for(i = 0; i < 8; i++) { + if((P2IFG & BV(i)) && intFuncP2[i]) { + intFuncP2[i](); + P2IFG &= ~BV(i); + } + } +} +#endif diff --git a/msp430/cores/msp430/WMath.cpp b/msp430/cores/msp430/WMath.cpp new file mode 100644 index 0000000..d754215 --- /dev/null +++ b/msp430/cores/msp430/WMath.cpp @@ -0,0 +1,65 @@ +/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + 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$ +*/ + +extern "C" { + #include "stdlib.h" + /* Using interal random and srandom in file random.c + * until msp430-libc adds supports for random and srandom */ + extern long random(void); + extern void srandom(unsigned long __seed); +} + +void randomSeed(unsigned int seed) +{ + if (seed != 0) { + srandom(seed); + } +} + +long random(long howbig) +{ + if (howbig == 0) { + return 0; + } + return random() % 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; +} + +unsigned int makeWord(unsigned int w) { return w; } +unsigned int makeWord(unsigned char h, unsigned char l) { return (h << 8) | l; } diff --git a/msp430/cores/msp430/WString.cpp b/msp430/cores/msp430/WString.cpp new file mode 100644 index 0000000..6dc1a73 --- /dev/null +++ b/msp430/cores/msp430/WString.cpp @@ -0,0 +1,648 @@ +/* + WString.cpp - String library for Wiring & Arduino + ...mostly rewritten by Paul Stoffregen... + Copyright (c) 2009-10 Hernando Barragan. All rights reserved. + Copyright 2011, Paul Stoffregen, paul@pjrc.com + + 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 +*/ + +#include "WString.h" + + +/*********************************************/ +/* Constructors */ +/*********************************************/ + +String::String(const char *cstr) +{ + init(); + if (cstr) copy(cstr, strlen(cstr)); +} + +String::String(const String &value) +{ + init(); + *this = value; +} + +#ifdef __GXX_EXPERIMENTAL_CXX0X__ +String::String(String &&rval) +{ + init(); + move(rval); +} +String::String(StringSumHelper &&rval) +{ + init(); + move(rval); +} +#endif + +String::String(char c) +{ + init(); + char buf[2]; + buf[0] = c; + buf[1] = 0; + *this = buf; +} + +String::String(unsigned char value, unsigned char base) +{ + init(); + char buf[9]; + utoa(value, buf, base); + *this = buf; +} + +String::String(int value, unsigned char base) +{ + init(); + char buf[18]; + itoa(value, buf, base); + *this = buf; +} + +String::String(unsigned int value, unsigned char base) +{ + init(); + char buf[17]; + utoa(value, buf, base); + *this = buf; +} + +String::String(long value, unsigned char base) +{ + init(); + char buf[34]; + ltoa(value, buf, base); + *this = buf; +} + +String::String(unsigned long value, unsigned char base) +{ + init(); + char buf[33]; + ultoa(value, buf, base); + *this = buf; +} + +String::~String() +{ + free(buffer); +} + +/*********************************************/ +/* Memory Management */ +/*********************************************/ + +inline void String::init(void) +{ + buffer = NULL; + capacity = 0; + len = 0; + flags = 0; +} + +void String::invalidate(void) +{ + if (buffer) free(buffer); + buffer = NULL; + capacity = len = 0; +} + +unsigned char String::reserve(unsigned int size) +{ + if (buffer && capacity >= size) return 1; + if (changeBuffer(size)) { + if (len == 0) buffer[0] = 0; + return 1; + } + return 0; +} + +unsigned char String::changeBuffer(unsigned int maxStrLen) +{ + //char *newbuffer = (char *)realloc(buffer, maxStrLen + 1); + char *newbuffer = (char *)malloc(maxStrLen + 1); + + if (newbuffer) { + free(buffer); + buffer = newbuffer; + capacity = maxStrLen; + return 1; + } + return 0; +} + +/*********************************************/ +/* Copy and Move */ +/*********************************************/ + +String & String::copy(const char *cstr, unsigned int length) +{ + if (!reserve(length)) { + invalidate(); + return *this; + } + len = length; + strcpy(buffer, cstr); + return *this; +} + +#ifdef __GXX_EXPERIMENTAL_CXX0X__ +void String::move(String &rhs) +{ + if (buffer) { + if (capacity >= rhs.len) { + strcpy(buffer, rhs.buffer); + len = rhs.len; + rhs.len = 0; + return; + } else { + free(buffer); + } + } + buffer = rhs.buffer; + capacity = rhs.capacity; + len = rhs.len; + rhs.buffer = NULL; + rhs.capacity = 0; + rhs.len = 0; +} +#endif + +String & String::operator = (const String &rhs) +{ + if (this == &rhs) return *this; + + if (rhs.buffer) copy(rhs.buffer, rhs.len); + else invalidate(); + + return *this; +} + +#ifdef __GXX_EXPERIMENTAL_CXX0X__ +String & String::operator = (String &&rval) +{ + if (this != &rval) move(rval); + return *this; +} + +String & String::operator = (StringSumHelper &&rval) +{ + if (this != &rval) move(rval); + return *this; +} +#endif + +String & String::operator = (const char *cstr) +{ + if (cstr) copy(cstr, strlen(cstr)); + else invalidate(); + + return *this; +} + +/*********************************************/ +/* concat */ +/*********************************************/ + +unsigned char String::concat(const String &s) +{ + return concat(s.buffer, s.len); +} + +unsigned char String::concat(const char *cstr, unsigned int length) +{ + unsigned int newlen = len + length; + if (!cstr) return 0; + if (length == 0) return 1; + if (!reserve(newlen)) return 0; + strcpy(buffer + len, cstr); + len = newlen; + return 1; +} + +unsigned char String::concat(const char *cstr) +{ + if (!cstr) return 0; + return concat(cstr, strlen(cstr)); +} + +unsigned char String::concat(char c) +{ + char buf[2]; + buf[0] = c; + buf[1] = 0; + return concat(buf, 1); +} + +unsigned char String::concat(unsigned char num) +{ + char buf[4]; + itoa(num, buf, 10); + return concat(buf, strlen(buf)); +} + +unsigned char String::concat(int num) +{ + char buf[7]; + itoa(num, buf, 10); + return concat(buf, strlen(buf)); +} + +unsigned char String::concat(unsigned int num) +{ + char buf[6]; + utoa(num, buf, 10); + return concat(buf, strlen(buf)); +} + +unsigned char String::concat(long num) +{ + char buf[12]; + ltoa(num, buf, 10); + return concat(buf, strlen(buf)); +} + +unsigned char String::concat(unsigned long num) +{ + char buf[11]; + ultoa(num, buf, 10); + return concat(buf, strlen(buf)); +} + +/*********************************************/ +/* Concatenate */ +/*********************************************/ + +StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs) +{ + StringSumHelper &a = const_cast<StringSumHelper&>(lhs); + if (!a.concat(rhs.buffer, rhs.len)) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr) +{ + StringSumHelper &a = const_cast<StringSumHelper&>(lhs); + if (!cstr || !a.concat(cstr, strlen(cstr))) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, char c) +{ + StringSumHelper &a = const_cast<StringSumHelper&>(lhs); + if (!a.concat(c)) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num) +{ + StringSumHelper &a = const_cast<StringSumHelper&>(lhs); + if (!a.concat(num)) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, int num) +{ + StringSumHelper &a = const_cast<StringSumHelper&>(lhs); + if (!a.concat(num)) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num) +{ + StringSumHelper &a = const_cast<StringSumHelper&>(lhs); + if (!a.concat(num)) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, long num) +{ + StringSumHelper &a = const_cast<StringSumHelper&>(lhs); + if (!a.concat(num)) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num) +{ + StringSumHelper &a = const_cast<StringSumHelper&>(lhs); + if (!a.concat(num)) a.invalidate(); + return a; +} + +/*********************************************/ +/* Comparison */ +/*********************************************/ + +int String::compareTo(const String &s) const +{ + if (!buffer || !s.buffer) { + if (s.buffer && s.len > 0) return 0 - *(unsigned char *)s.buffer; + if (buffer && len > 0) return *(unsigned char *)buffer; + return 0; + } + return strcmp(buffer, s.buffer); +} + +unsigned char String::equals(const String &s2) const +{ + return (len == s2.len && compareTo(s2) == 0); +} + +unsigned char String::equals(const char *cstr) const +{ + if (len == 0) return (cstr == NULL || *cstr == 0); + if (cstr == NULL) return buffer[0] == 0; + return strcmp(buffer, cstr) == 0; +} + +unsigned char String::operator<(const String &rhs) const +{ + return compareTo(rhs) < 0; +} + +unsigned char String::operator>(const String &rhs) const +{ + return compareTo(rhs) > 0; +} + +unsigned char String::operator<=(const String &rhs) const +{ + return compareTo(rhs) <= 0; +} + +unsigned char String::operator>=(const String &rhs) const +{ + return compareTo(rhs) >= 0; +} + +unsigned char String::equalsIgnoreCase( const String &s2 ) const +{ + if (this == &s2) return 1; + if (len != s2.len) return 0; + if (len == 0) return 1; + const char *p1 = buffer; + const char *p2 = s2.buffer; + while (*p1) { + if (tolower(*p1++) != tolower(*p2++)) return 0; + } + return 1; +} + +unsigned char String::startsWith( const String &s2 ) const +{ + if (len < s2.len) return 0; + return startsWith(s2, 0); +} + +unsigned char String::startsWith( const String &s2, unsigned int offset ) const +{ + if (offset > len - s2.len || !buffer || !s2.buffer) return 0; + return strncmp( &buffer[offset], s2.buffer, s2.len ) == 0; +} + +unsigned char String::endsWith( const String &s2 ) const +{ + if ( len < s2.len || !buffer || !s2.buffer) return 0; + return strcmp(&buffer[len - s2.len], s2.buffer) == 0; +} + +/*********************************************/ +/* Character Access */ +/*********************************************/ + +char String::charAt(unsigned int loc) const +{ + return operator[](loc); +} + +void String::setCharAt(unsigned int loc, char c) +{ + if (loc < len) buffer[loc] = c; +} + +char & String::operator[](unsigned int index) +{ + static char dummy_writable_char; + if (index >= len || !buffer) { + dummy_writable_char = 0; + return dummy_writable_char; + } + return buffer[index]; +} + +char String::operator[]( unsigned int index ) const +{ + if (index >= len || !buffer) return 0; + return buffer[index]; +} + +void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index) const +{ + if (!bufsize || !buf) return; + if (index >= len) { + buf[0] = 0; + return; + } + unsigned int n = bufsize - 1; + if (n > len - index) n = len - index; + strncpy((char *)buf, buffer + index, n); + buf[n] = 0; +} + +/*********************************************/ +/* Search */ +/*********************************************/ + +int String::indexOf(char c) const +{ + return indexOf(c, 0); +} + +int String::indexOf( char ch, unsigned int fromIndex ) const +{ + if (fromIndex >= len) return -1; + const char* temp = strchr(buffer + fromIndex, ch); + if (temp == NULL) return -1; + return temp - buffer; +} + +int String::indexOf(const String &s2) const +{ + return indexOf(s2, 0); +} + +int String::indexOf(const String &s2, unsigned int fromIndex) const +{ + if (fromIndex >= len) return -1; + const char *found = strstr(buffer + fromIndex, s2.buffer); + if (found == NULL) return -1; + return found - buffer; +} + +int String::lastIndexOf( char theChar ) const +{ + return lastIndexOf(theChar, len - 1); +} + +int String::lastIndexOf(char ch, unsigned int fromIndex) const +{ + if (fromIndex >= len) return -1; + char tempchar = buffer[fromIndex + 1]; + buffer[fromIndex + 1] = '\0'; + char* temp = strrchr( buffer, ch ); + buffer[fromIndex + 1] = tempchar; + if (temp == NULL) return -1; + return temp - buffer; +} + +int String::lastIndexOf(const String &s2) const +{ + return lastIndexOf(s2, len - s2.len); +} + +int String::lastIndexOf(const String &s2, unsigned int fromIndex) const +{ + if (s2.len == 0 || len == 0 || s2.len > len) return -1; + if (fromIndex >= len) fromIndex = len - 1; + int found = -1; + for (char *p = buffer; p <= buffer + fromIndex; p++) { + p = strstr(p, s2.buffer); + if (!p) break; + if ((unsigned int)(p - buffer) <= fromIndex) found = p - buffer; + } + return found; +} + +String String::substring( unsigned int left ) const +{ + return substring(left, len); +} + +String String::substring(unsigned int left, unsigned int right) const +{ + if (left > right) { + unsigned int temp = right; + right = left; + left = temp; + } + String out; + if (left > len) return out; + if (right > len) right = len; + char temp = buffer[right]; // save the replaced character + buffer[right] = '\0'; + out = buffer + left; // pointer arithmetic + buffer[right] = temp; //restore character + return out; +} + +/*********************************************/ +/* Modification */ +/*********************************************/ + +void String::replace(char find, char replace) +{ + if (!buffer) return; + for (char *p = buffer; *p; p++) { + if (*p == find) *p = replace; + } +} + +void String::replace(const String& find, const String& replace) +{ + if (len == 0 || find.len == 0) return; + int diff = replace.len - find.len; + char *readFrom = buffer; + char *foundAt; + if (diff == 0) { + while ((foundAt = strstr(readFrom, find.buffer)) != NULL) { + memcpy(foundAt, replace.buffer, replace.len); + readFrom = foundAt + replace.len; + } + } else if (diff < 0) { + char *writeTo = buffer; + while ((foundAt = strstr(readFrom, find.buffer)) != NULL) { + unsigned int n = foundAt - readFrom; + memcpy(writeTo, readFrom, n); + writeTo += n; + memcpy(writeTo, replace.buffer, replace.len); + writeTo += replace.len; + readFrom = foundAt + find.len; + len += diff; + } + strcpy(writeTo, readFrom); + } else { + unsigned int size = len; // compute size needed for result + while ((foundAt = strstr(readFrom, find.buffer)) != NULL) { + readFrom = foundAt + find.len; + size += diff; + } + if (size == len) return; + if (size > capacity && !changeBuffer(size)) return; // XXX: tell user! + int index = len - 1; + while (index >= 0 && (index = lastIndexOf(find, index)) >= 0) { + readFrom = buffer + index + find.len; + memmove(readFrom + diff, readFrom, len - (readFrom - buffer)); + len += diff; + buffer[len] = 0; + memcpy(buffer + index, replace.buffer, replace.len); + index--; + } + } +} + +void String::toLowerCase(void) +{ + if (!buffer) return; + for (char *p = buffer; *p; p++) { + *p = tolower(*p); + } +} + +void String::toUpperCase(void) +{ + if (!buffer) return; + for (char *p = buffer; *p; p++) { + *p = toupper(*p); + } +} + +void String::trim(void) +{ + if (!buffer || len == 0) return; + char *begin = buffer; + while (isspace(*begin)) begin++; + char *end = buffer + len - 1; + while (isspace(*end) && end >= begin) end--; + len = end + 1 - begin; + if (begin > buffer) memcpy(buffer, begin, len); + buffer[len] = 0; +} + +/*********************************************/ +/* Parsing / Conversion */ +/*********************************************/ + +long String::toInt(void) const +{ + if (buffer) return atol(buffer); + return 0; +} + + diff --git a/msp430/cores/msp430/WString.h b/msp430/cores/msp430/WString.h new file mode 100644 index 0000000..73d1213 --- /dev/null +++ b/msp430/cores/msp430/WString.h @@ -0,0 +1,204 @@ +/* + WString.h - String library for Wiring & Arduino + ...mostly rewritten by Paul Stoffregen... + Copyright (c) 2009-10 Hernando Barragan. All right reserved. + Copyright 2011, Paul Stoffregen, paul@pjrc.com + + 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 String_class_h +#define String_class_h +#ifdef __cplusplus + +#include <stdlib.h> +#include <string.h> +#include <ctype.h> + +// When compiling programs with this class, the following gcc parameters +// dramatically increase performance and memory (RAM) efficiency, typically +// with little or no increase in code size. +// -felide-constructors +// -std=c++0x + +//class __FlashStringHelper; +//#define F(string_literal) (reinterpret_cast<__FlashStringHelper *>(PSTR(string_literal))) + +// An inherited class for holding the result of a concatenation. These +// result objects are assumed to be writable by subsequent concatenations. +class StringSumHelper; + +// The string class +class String +{ + // use a function pointer to allow for "if (s)" without the + // complications of an operator bool(). for more information, see: + // http://www.artima.com/cppsource/safebool.html + typedef void (String::*StringIfHelperType)() const; + void StringIfHelper() const {} + +public: + // constructors + // creates a copy of the initial value. + // if the initial value is null or invalid, or if memory allocation + // fails, the string will be marked as invalid (i.e. "if (s)" will + // be false). + String(const char *cstr = ""); + String(const String &str); + #ifdef __GXX_EXPERIMENTAL_CXX0X__ + String(String &&rval); + String(StringSumHelper &&rval); + #endif + explicit String(char c); + explicit String(unsigned char, unsigned char base=10); + explicit String(int, unsigned char base=10); + explicit String(unsigned int, unsigned char base=10); + explicit String(long, unsigned char base=10); + explicit String(unsigned long, unsigned char base=10); + ~String(void); + + // memory management + // return true on success, false on failure (in which case, the string + // is left unchanged). reserve(0), if successful, will validate an + // invalid string (i.e., "if (s)" will be true afterwards) + unsigned char reserve(unsigned int size); + inline unsigned int length(void) const {return len;} + + // creates a copy of the assigned value. if the value is null or + // invalid, or if the memory allocation fails, the string will be + // marked as invalid ("if (s)" will be false). + String & operator = (const String &rhs); + String & operator = (const char *cstr); + #ifdef __GXX_EXPERIMENTAL_CXX0X__ + String & operator = (String &&rval); + String & operator = (StringSumHelper &&rval); + #endif + + // concatenate (works w/ built-in types) + + // returns true on success, false on failure (in which case, the string + // is left unchanged). if the argument is null or invalid, the + // concatenation is considered unsucessful. + unsigned char concat(const String &str); + unsigned char concat(const char *cstr); + unsigned char concat(char c); + unsigned char concat(unsigned char c); + unsigned char concat(int num); + unsigned char concat(unsigned int num); + unsigned char concat(long num); + unsigned char concat(unsigned long num); + + // if there's not enough memory for the concatenated value, the string + // will be left unchanged (but this isn't signalled in any way) + String & operator += (const String &rhs) {concat(rhs); return (*this);} + String & operator += (const char *cstr) {concat(cstr); return (*this);} + String & operator += (char c) {concat(c); return (*this);} + String & operator += (unsigned char num) {concat(num); return (*this);} + String & operator += (int num) {concat(num); return (*this);} + String & operator += (unsigned int num) {concat(num); return (*this);} + String & operator += (long num) {concat(num); return (*this);} + String & operator += (unsigned long num) {concat(num); return (*this);} + + friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs); + friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr); + friend StringSumHelper & operator + (const StringSumHelper &lhs, char c); + friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, int num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, long num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num); + + // comparison (only works w/ Strings and "strings") + operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; } + int compareTo(const String &s) const; + unsigned char equals(const String &s) const; + unsigned char equals(const char *cstr) const; + unsigned char operator == (const String &rhs) const {return equals(rhs);} + unsigned char operator == (const char *cstr) const {return equals(cstr);} + unsigned char operator != (const String &rhs) const {return !equals(rhs);} + unsigned char operator != (const char *cstr) const {return !equals(cstr);} + unsigned char operator < (const String &rhs) const; + unsigned char operator > (const String &rhs) const; + unsigned char operator <= (const String &rhs) const; + unsigned char operator >= (const String &rhs) const; + unsigned char equalsIgnoreCase(const String &s) const; + unsigned char startsWith( const String &prefix) const; + unsigned char startsWith(const String &prefix, unsigned int offset) const; + unsigned char endsWith(const String &suffix) const; + + // character acccess + char charAt(unsigned int index) const; + void setCharAt(unsigned int index, char c); + char operator [] (unsigned int index) const; + char& operator [] (unsigned int index); + void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const; + void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const + {getBytes((unsigned char *)buf, bufsize, index);} + + // search + int indexOf( char ch ) const; + int indexOf( char ch, unsigned int fromIndex ) const; + int indexOf( const String &str ) const; + int indexOf( const String &str, unsigned int fromIndex ) const; + int lastIndexOf( char ch ) const; + int lastIndexOf( char ch, unsigned int fromIndex ) const; + int lastIndexOf( const String &str ) const; + int lastIndexOf( const String &str, unsigned int fromIndex ) const; + String substring( unsigned int beginIndex ) const; + String substring( unsigned int beginIndex, unsigned int endIndex ) const; + + // modification + void replace(char find, char replace); + void replace(const String& find, const String& replace); + void toLowerCase(void); + void toUpperCase(void); + void trim(void); + + // parsing/conversion + long toInt(void) const; + +protected: + char *buffer; // the actual char array + unsigned int capacity; // the array length minus one (for the '\0') + unsigned int len; // the String length (not counting the '\0') + unsigned char flags; // unused, for future features +protected: + void init(void); + void invalidate(void); + unsigned char changeBuffer(unsigned int maxStrLen); + unsigned char concat(const char *cstr, unsigned int length); + + // copy and move + String & copy(const char *cstr, unsigned int length); + #ifdef __GXX_EXPERIMENTAL_CXX0X__ + void move(String &rhs); + #endif +}; + +class StringSumHelper : public String +{ +public: + StringSumHelper(const String &s) : String(s) {} + StringSumHelper(const char *p) : String(p) {} + StringSumHelper(char c) : String(c) {} + StringSumHelper(unsigned char num) : String(num) {} + StringSumHelper(int num) : String(num) {} + StringSumHelper(unsigned int num) : String(num) {} + StringSumHelper(long num) : String(num) {} + StringSumHelper(unsigned long num) : String(num) {} +}; + +#endif // __cplusplus +#endif // String_class_h diff --git a/msp430/cores/msp430/Wire.cpp b/msp430/cores/msp430/Wire.cpp new file mode 100644 index 0000000..d7d74a6 --- /dev/null +++ b/msp430/cores/msp430/Wire.cpp @@ -0,0 +1,307 @@ +/* + ************************************************************************ + * Wire.cpp + * + * Arduino core files for MSP430 + * Copyright (c) 2012 Robert Wessels. All right reserved. + * + * + *********************************************************************** + Derived from: + TwoWire.cpp - TWI/I2C library for Wiring & Arduino + Copyright (c) 2006 Nicholas Zambetti. 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 2012 by Todd Krein (todd@krein.org) to implement repeated starts +*/ + +extern "C" { + #include <stdlib.h> + #include <string.h> + #include <inttypes.h> + #include "twi.h" +} + +#include "Wire.h" + +// Initialize Class Variables ////////////////////////////////////////////////// + +uint8_t TwoWire::rxBuffer[BUFFER_LENGTH]; +uint8_t TwoWire::rxBufferIndex = 0; +uint8_t TwoWire::rxBufferLength = 0; + +uint8_t TwoWire::txAddress = 0; +uint8_t TwoWire::txBuffer[BUFFER_LENGTH]; +uint8_t TwoWire::txBufferIndex = 0; +uint8_t TwoWire::txBufferLength = 0; + +uint8_t TwoWire::transmitting = 0; +void (*TwoWire::user_onRequest)(void); +void (*TwoWire::user_onReceive)(int); + +// Constructors //////////////////////////////////////////////////////////////// + +TwoWire::TwoWire() +{ +} + +// Public Methods ////////////////////////////////////////////////////////////// + +void TwoWire::begin(void) +{ + rxBufferIndex = 0; + rxBufferLength = 0; + + txBufferIndex = 0; + txBufferLength = 0; + + twi_init(); +} + +void TwoWire::begin(uint8_t address) +{ + twi_setAddress(address); + twi_attachSlaveTxEvent(onRequestService); + twi_attachSlaveRxEvent(onReceiveService); + begin(); +} + +void TwoWire::begin(int address) +{ + begin((uint8_t)address); +} + +uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop) +{ + // clamp to buffer length + if(quantity > BUFFER_LENGTH){ + quantity = BUFFER_LENGTH; + } + // perform blocking read into buffer + uint8_t read = twi_readFrom(address, rxBuffer, quantity, sendStop); + // set rx buffer iterator vars + rxBufferIndex = 0; + rxBufferLength = read; + + return read; +} + +uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity) +{ + return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true); +} + +uint8_t TwoWire::requestFrom(int address, int quantity) +{ + return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true); +} + +uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop) +{ + return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop); +} + +void TwoWire::beginTransmission(uint8_t address) +{ + // indicate that we are transmitting + transmitting = 1; + // set address of targeted slave + txAddress = address; + // reset tx buffer iterator vars + txBufferIndex = 0; + txBufferLength = 0; +} + +void TwoWire::beginTransmission(int address) +{ + beginTransmission((uint8_t)address); +} + +// +// Originally, 'endTransmission' was an f(void) function. +// It has been modified to take one parameter indicating +// whether or not a STOP should be performed on the bus. +// Calling endTransmission(false) allows a sketch to +// perform a repeated start. +// +// WARNING: Nothing in the library keeps track of whether +// the bus tenure has been properly ended with a STOP. It +// is very possible to leave the bus in a hung state if +// no call to endTransmission(true) is made. Some I2C +// devices will behave oddly if they do not see a STOP. +// +uint8_t TwoWire::endTransmission(uint8_t sendStop) +{ + // transmit buffer (blocking) + int8_t ret = twi_writeTo(txAddress, txBuffer, txBufferLength, 1, sendStop); + // reset tx buffer iterator vars + txBufferIndex = 0; + txBufferLength = 0; + // indicate that we are done transmitting + transmitting = 0; + return ret; +} + +// This provides backwards compatibility with the original +// definition, and expected behaviour, of endTransmission +// +uint8_t TwoWire::endTransmission(void) +{ + return endTransmission(true); +} + +// must be called in: +// slave tx event callback +// or after beginTransmission(address) +size_t TwoWire::write(uint8_t data) +{ + if(transmitting){ + // in master transmitter mode + // don't bother if buffer is full + if(txBufferLength >= BUFFER_LENGTH){ + setWriteError(); + return 0; + } + // put byte in tx buffer + txBuffer[txBufferIndex] = data; + ++txBufferIndex; + // update amount in buffer + txBufferLength = txBufferIndex; + }else{ + // in slave send mode + // reply to master + twi_transmit(&data, 1); + } + return 1; +} + +// must be called in: +// slave tx event callback +// or after beginTransmission(address) +size_t TwoWire::write(const uint8_t *data, size_t quantity) +{ + if(transmitting){ + // in master transmitter mode + for(size_t i = 0; i < quantity; ++i){ + write(data[i]); + } + }else{ + // in slave send mode + // reply to master + twi_transmit(data, quantity); + } + return quantity; +} + +// must be called in: +// slave rx event callback +// or after requestFrom(address, numBytes) +int TwoWire::available(void) +{ + return rxBufferLength - rxBufferIndex; +} + +// must be called in: +// slave rx event callback +// or after requestFrom(address, numBytes) +int TwoWire::read(void) +{ + int value = -1; + + // get each successive byte on each call + if(rxBufferIndex < rxBufferLength){ + value = rxBuffer[rxBufferIndex]; + ++rxBufferIndex; + } + + return value; +} + +// must be called in: +// slave rx event callback +// or after requestFrom(address, numBytes) +int TwoWire::peek(void) +{ + int value = -1; + + if(rxBufferIndex < rxBufferLength){ + value = rxBuffer[rxBufferIndex]; + } + + return value; +} + +void TwoWire::flush(void) +{ + // XXX: to be implemented. +} + +// behind the scenes function that is called when data is received +void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes) +{ + // don't bother if user hasn't registered a callback + if(!user_onReceive){ + return; + } + // don't bother if rx buffer is in use by a master requestFrom() op + // i know this drops data, but it allows for slight stupidity + // meaning, they may not have read all the master requestFrom() data yet + if(rxBufferIndex < rxBufferLength){ + return; + } + // copy twi rx buffer into local read buffer + // this enables new reads to happen in parallel + for(uint8_t i = 0; i < numBytes; ++i){ + rxBuffer[i] = inBytes[i]; + } + // set rx iterator vars + rxBufferIndex = 0; + rxBufferLength = numBytes; + // alert user program + user_onReceive(numBytes); +} + +// behind the scenes function that is called when data is requested +void TwoWire::onRequestService(void) +{ + // don't bother if user hasn't registered a callback + if(!user_onRequest){ + return; + } + // reset tx buffer iterator vars + // !!! this will kill any pending pre-master sendTo() activity + txBufferIndex = 0; + txBufferLength = 0; + // alert user program + user_onRequest(); +} + +// sets function called on slave write +void TwoWire::onReceive( void (*function)(int) ) +{ + user_onReceive = function; +} + +// sets function called on slave read +void TwoWire::onRequest( void (*function)(void) ) +{ + user_onRequest = function; +} + +// Preinstantiate Objects ////////////////////////////////////////////////////// + +TwoWire Wire = TwoWire(); + diff --git a/msp430/cores/msp430/Wire.h b/msp430/cores/msp430/Wire.h new file mode 100644 index 0000000..57d0a2e --- /dev/null +++ b/msp430/cores/msp430/Wire.h @@ -0,0 +1,94 @@ +/* + ************************************************************************ + * Wire.h + * + * Arduino core files for MSP430 + * Copyright (c) 2012 Robert Wessels. All right reserved. + * + * + *********************************************************************** + Derived from: + TwoWire.h - TWI/I2C library for Arduino & Wiring + Copyright (c) 2006 Nicholas Zambetti. 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 2012 by Todd Krein (todd@krein.org) to implement repeated starts +*/ + +#ifndef TwoWire_h +#define TwoWire_h + +#include <inttypes.h> +#include "Stream.h" + +#define BUFFER_LENGTH 16 + +class TwoWire : public Stream +{ + private: + static uint8_t rxBuffer[]; + static uint8_t rxBufferIndex; + static uint8_t rxBufferLength; + + static uint8_t txAddress; + static uint8_t txBuffer[]; + static uint8_t txBufferIndex; + static uint8_t txBufferLength; + + static uint8_t transmitting; + static void (*user_onRequest)(void); + static void (*user_onReceive)(int); + static void onRequestService(void); + static void onReceiveService(uint8_t*, int); + public: + TwoWire(); + void begin(); + void begin(uint8_t); + void begin(int); + void beginTransmission(uint8_t); + void beginTransmission(int); + uint8_t endTransmission(void); + uint8_t endTransmission(uint8_t); + uint8_t requestFrom(uint8_t, uint8_t); + uint8_t requestFrom(uint8_t, uint8_t, uint8_t); + uint8_t requestFrom(int, int); + uint8_t requestFrom(int, int, int); + virtual size_t write(uint8_t); + virtual size_t write(const uint8_t *, size_t); + virtual int available(void); + virtual int read(void); + virtual int peek(void); + virtual void flush(void); +#define USCI_ERROR "\n*********\nI2C Slave is not implemented for this MSP430. \nConsider using using a MSP430 with USCI peripheral e.g. MSP430G2553.\n*********\n" +#if defined(__MSP430_HAS_USCI__) + void onReceive( void (*)(int) ); + void onRequest( void (*)(void) ); +#else + void onReceive( void (*)(int) ) __attribute__ ((error(USCI_ERROR))); + void onRequest( void (*)(void) ) __attribute__ ((error(USCI_ERROR))); +#endif + + inline size_t write(unsigned long n) { return write((uint8_t)n); } + inline size_t write(long n) { return write((uint8_t)n); } + inline size_t write(unsigned int n) { return write((uint8_t)n); } + inline size_t write(int n) { return write((uint8_t)n); } + using Print::write; +}; + +extern TwoWire Wire; + +#endif + diff --git a/msp430/cores/msp430/binary.h b/msp430/cores/msp430/binary.h new file mode 100644 index 0000000..af14980 --- /dev/null +++ b/msp430/cores/msp430/binary.h @@ -0,0 +1,515 @@ +#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/msp430/cores/msp430/main.cpp b/msp430/cores/msp430/main.cpp new file mode 100644 index 0000000..1062bf9 --- /dev/null +++ b/msp430/cores/msp430/main.cpp @@ -0,0 +1,15 @@ +#include <Energia.h> + +int main(void) +{ + init(); + + setup(); + + for (;;) { + loop(); + } + + return 0; +} + diff --git a/msp430/cores/msp430/new.cpp b/msp430/cores/msp430/new.cpp new file mode 100644 index 0000000..0f6d422 --- /dev/null +++ b/msp430/cores/msp430/new.cpp @@ -0,0 +1,18 @@ +#include <new.h> + +void * operator new(size_t size) +{ + return malloc(size); +} + +void operator delete(void * ptr) +{ + free(ptr); +} + +int __cxa_guard_acquire(__guard *g) {return !*(char *)(g);}; +void __cxa_guard_release (__guard *g) {*(char *)g = 1;}; +void __cxa_guard_abort (__guard *) {}; + +void __cxa_pure_virtual(void) {}; + diff --git a/msp430/cores/msp430/new.h b/msp430/cores/msp430/new.h new file mode 100644 index 0000000..cd940ce --- /dev/null +++ b/msp430/cores/msp430/new.h @@ -0,0 +1,22 @@ +/* Header to define new/delete operators as they aren't provided by avr-gcc by default + Taken from http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=59453 + */ + +#ifndef NEW_H +#define NEW_H + +#include <stdlib.h> + +void * operator new(size_t size); +void operator delete(void * ptr); + +__extension__ typedef int __guard __attribute__((mode (__DI__))); + +extern "C" int __cxa_guard_acquire(__guard *); +extern "C" void __cxa_guard_release (__guard *); +extern "C" void __cxa_guard_abort (__guard *); + +extern "C" void __cxa_pure_virtual(void); + +#endif + diff --git a/msp430/cores/msp430/random.c b/msp430/cores/msp430/random.c new file mode 100644 index 0000000..6b46d5e --- /dev/null +++ b/msp430/cores/msp430/random.c @@ -0,0 +1,90 @@ +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * Posix rand_r function added May 1999 by Wes Peters <wes@softweyr.com>. + * + * $Id$ + */ + +/* + * From: +static char sccsid[] = "@(#)rand.c 8.1 (Berkeley) 6/14/93"; +*/ +#ifndef RANDOM_MAX +#define RANDOM_MAX 0x7FFFFFFF +#endif + +#include <stdlib.h> + +static long +do_random(unsigned long *ctx) +{ + /* + * Compute x = (7^5 * x) mod (2^31 - 1) + * wihout overflowing 31 bits: + * (2^31 - 1) = 127773 * (7^5) + 2836 + * From "Random number generators: good ones are hard to find", + * Park and Miller, Communications of the ACM, vol. 31, no. 10, + * October 1988, p. 1195. + */ + long hi, lo, x; + + x = *ctx; + /* Can't be initialized with 0, so use another value. */ + if (x == 0) + x = 123459876L; + hi = x / 127773L; + lo = x % 127773L; + x = 16807L * lo - 2836L * hi; + if (x < 0) + x += 0x7fffffffL; + return ((*ctx = x) % ((unsigned long)RANDOM_MAX + 1)); +} + + +long +random_r(unsigned long *ctx) +{ + return do_random(ctx); +} + + +static unsigned long next = 1; + +long +random(void) +{ + return do_random(&next); +} + +void +srandom(unsigned long seed) +{ + next = seed; +} + diff --git a/msp430/cores/msp430/twi.c b/msp430/cores/msp430/twi.c new file mode 100644 index 0000000..01cd1a4 --- /dev/null +++ b/msp430/cores/msp430/twi.c @@ -0,0 +1,847 @@ +/* + ************************************************************************ + * twi.c + * + * Arduino core files for MSP430 + * Copyright (c) 2012 Robert Wessels. All right reserved. + * + * + *********************************************************************** + Derived from: + twi.c - TWI/I2C library for Wiring & Arduino + Copyright (c) 2006 Nicholas Zambetti. 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 +*/ + +#include <math.h> +#include <stdlib.h> +#include "Energia.h" // for digitalWrite + +#ifndef cbi +#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) +#endif + +#ifndef sbi +#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) +#endif + +#include "wiring_private.h" +#include "pins_energia.h" +#include "twi.h" +#include "usci_isr_handler.h" + +static volatile uint8_t twi_state; +static volatile uint8_t twi_sendStop; // should the transaction end with a stop +static volatile uint8_t twi_inRepStart; // in the middle of a repeated start + +static void (*twi_onSlaveTransmit)(void); +static void (*twi_onSlaveReceive)(uint8_t*, int); + +static uint8_t twi_masterBuffer[TWI_BUFFER_LENGTH]; +static volatile uint8_t twi_masterBufferIndex; +static uint8_t twi_masterBufferLength; + +static uint8_t twi_txBuffer[TWI_BUFFER_LENGTH]; +static volatile uint8_t twi_txBufferIndex; +static volatile uint8_t twi_txBufferLength; + +static uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH]; +static volatile uint8_t twi_rxBufferIndex; + +static volatile uint8_t twi_error; + +#ifdef __MSP430_HAS_USI__ +static uint8_t twi_slarw; +static uint8_t twi_my_addr; + +#endif + +#ifdef __MSP430_HAS_USCI__ +#endif + +#ifdef __MSP430_HAS_EUSCI_B0__ +#endif + + +/* + * Function twi_init + * Desc readys twi pins and sets twi bitrate + * Input none + * Output none + */ +void twi_init(void) +{ + // initialize state + twi_state = TWI_IDLE; + twi_sendStop = true; // default value + twi_inRepStart = false; + +#ifdef __MSP430_HAS_USI__ + + /* 100 KHz for all */ +#if (F_CPU >= 16000000L) || (F_CPU >= 12000000L) + USICKCTL = USIDIV_7; +#elif defined(CALBC1_8MHZ_) && (F_CPU >= 8000000L) + USICKCTL = USIDIV_6; +#elif defined(CALBC1_1MHZ_) && (F_CPU >= 1000000L) + USICKCTL = USIDIV_3; +#endif + /* Enable USI I2C mode. */ + USICTL1 = USII2C; + /* SDA/SCL port enable and hold in reset */ + USICTL0 = (USIPE6 | USIPE7 | USISWRST); + /* SMCLK and SCL inactive state is high */ + USICKCTL |= (USISSEL_2 | USICKPL); + /* Disable automatic clear control */ + USICNT |= USIIFGCC; + /* Enable USI */ + USICTL0 &= ~USISWRST; + /* Counter interrupt enable */ + USICTL1 |= USIIE; +#endif + +#ifdef __MSP430_HAS_USCI__ + /* Calling this dummy function prevents the linker + * from stripping the USCI interupt vectors.*/ + usci_isr_install(); + + P1SEL |= BIT6 + BIT7; // Assign I2C pins to USCI_B0 + P1SEL2|= BIT6 + BIT7; // Assign I2C pins to USCI_B0 + + //Disable the USCI module and clears the other bits of control register + UCB0CTL1 = UCSWRST; + + /* + * Configure as I2C Slave. + * UCMODE_3 = I2C mode + * UCSYNC = Synchronous mode + * UCCLK = SMCLK + */ + UCB0CTL0 = UCMODE_3 | UCSYNC; + /* + * Compute the clock divider that achieves less than or + * equal to 100kHz. The numerator is biased to favor a larger + * clock divider so that the resulting clock is always less than or equal + * to the desired clock, never greater. + */ + UCB0BR0 = (unsigned char)((F_CPU / TWI_FREQ) & 0xFF); + UCB0BR1 = (unsigned char)((F_CPU / TWI_FREQ) >> 8); + + UCB0CTL1 &= ~(UCSWRST); + + /* Set I2C state change interrupt mask */ + UCB0I2CIE |= (UCALIE|UCNACKIE|UCSTTIE|UCSTPIE); + /* Enable state change and TX/RX interrupts */ + UC0IE |= UCB0RXIE | UCB0TXIE; +#endif +#ifdef __MSP430_HAS_EUSCI_B0__ + + P1SEL1 |= BIT6 + BIT7; // Pin init + + //Disable the USCI module and clears the other bits of control register + UCB0CTLW0 = UCSWRST; + + //Configure Automatic STOP condition generation + UCB0CTLW1 &= ~UCASTP_3; + //UCB0CTLW1 |= autoSTOPGeneration; + + //Byte Count Threshold + //UCB0TBCNT = byteCounterThreshold; + /* + * Configure as I2C master mode. + * UCMST = Master mode + * UCMODE_3 = I2C mode + * UCSYNC = Synchronous mode + * UCCLK = SMCLK + */ + UCB0CTLW0 = UCMST | UCMODE_3 | UCSSEL__SMCLK | UCSYNC | UCSWRST; + + /* + * Compute the clock divider that achieves the fastest speed less than or + * equal to the desired speed. The numerator is biased to favor a larger + * clock divider so that the resulting clock is always less than or equal + * to the desired clock, never greater. + */ + UCB0BRW = (unsigned short)(F_CPU / 400000); + UCB0CTLW0 &= ~(UCSWRST); + UCB0IE |= (UCRXIE0|UCALIE|UCNACKIE|UCSTTIE|UCSTPIE); // Enable I2C interrupts +#endif +} + +/* + * Function twi_setAddress + * Desc sets slave address and enables interrupt + * Input none + * Output none + */ +void twi_setAddress(uint8_t address) +{ +#ifdef __MSP430_HAS_USI__ + twi_my_addr = address << 1; +#endif +#ifdef __MSP430_HAS_USCI__ + /* UCGCEN = respond to general Call */ + UCB0I2COA = (address | UCGCEN); +#endif +#ifdef __MSP430_HAS_EUSCI_B0__ + /* UCGCEN = respond to general Call */ + UCB0I2COA0 = (address | UCOAEN | UCGCEN); +#endif +} + +/* + * Function twi_readFrom + * Desc attempts to become twi bus master and read a + * series of bytes from a device on the bus + * Input address: 7bit i2c device address + * data: pointer to byte array + * length: number of bytes to read into array + * Output number of bytes read + */ +uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sendStop) +{ + uint8_t i; + +#ifdef __MSP430_HAS_USI__ + /* Disable START condition interrupt */ + USICTL1 &= ~USISTTIE; + /* I2C master mode */ + USICTL0 |= USIMST; +#endif +#ifdef __MSP430_HAS_USCI__ + UCB0CTL1 = UCSWRST; // Enable SW reset + UCB0CTL1 |= (UCSSEL_2); // I2C Master, synchronous mode + UCB0CTL0 |= (UCMST | UCMODE_3 | UCSYNC); // I2C Master, synchronous mode + UCB0CTL1 &= ~(UCTR); // Configure in receive mode + UCB0I2CSA = address; // Set Slave Address + UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation + UCB0I2CIE |= (UCALIE|UCNACKIE|UCSTPIE); // Enable I2C interrupts + UC0IE |= (UCB0RXIE | UCB0TXIE); // Enable I2C interrupts +#endif +#ifdef __MSP430_HAS_EUSCI_B0__ + UCB0CTLW0 = UCSWRST; // Enable SW reset + UCB0CTLW0 |= (UCMST | UCMODE_3 | UCSYNC | UCSSEL__SMCLK); // I2C Master, synchronous mode + UCB0CTLW0 &= ~(UCTR); // Configure in receive mode + UCB0I2CSA = address; // Set Slave Address + UCB0CTLW0 &= ~UCSWRST; // Clear SW reset, resume operation + UCB0IE |= (UCRXIE0|UCALIE|UCNACKIFG|UCSTTIFG|UCSTPIFG); // Enable I2C interrupts +#endif + // ensure data will fit into buffer + if(TWI_BUFFER_LENGTH < length){ + return 0; + } + + // initialize buffer iteration vars + twi_masterBufferIndex = 0; + twi_masterBufferLength = length-1; // This is not intuitive, read on... + // On receive, the previously configured ACK/NACK setting is transmitted in + // response to the received byte before the interrupt is signalled. + // Therefor we must actually set NACK when the _next_ to last byte is + // received, causing that NACK to be sent in response to receiving the last + // expected byte of data. + +#ifdef __MSP430_HAS_USI__ + /* build sla+w, slave device address + w bit */ + twi_slarw = 1; + twi_slarw |= address << 1; + + // send start condition + twi_state = TWI_SND_START; + // this will trigger an interrupt kicking off the state machine in the isr + USICTL1 |= USIIFG; +#endif +#ifdef __MSP430_HAS_USCI__ + twi_state = TWI_MRX; // Master receive mode + UCB0CTL1 |= UCTXSTT; // I2C start condition +#endif +#ifdef __MSP430_HAS_EUSCI_B0__ + twi_state = TWI_MRX; // Master receive mode + while (UCB0CTLW0 & UCTXSTP); // Ensure stop condition got sent + UCB0CTLW0 |= UCTXSTT; // I2C start condition +#endif + + /* Wait in low power mode for read operation to complete */ + while(twi_state != TWI_IDLE){ + __bis_SR_register(LPM0_bits); + } + + if (twi_masterBufferIndex < length) + length = twi_masterBufferIndex; + + for(i = 0; i < length; ++i){ + data[i] = twi_masterBuffer[i]; + } + +#ifdef __MSP430_HAS_USCI__ + /* Ensure stop condition got sent before we exit. */ + while (UCB0CTL1 & UCTXSTP); +#endif + return length; +} + +/* + * Function twi_writeTo + * Desc attempts to become twi bus master and write a + * series of bytes to a device on the bus + * Input address: 7bit i2c device address + * data: pointer to byte array + * length: number of bytes in array + * wait: boolean indicating to wait for write or not + * Output 0 .. success + * 1 .. length to long for buffer + * 2 .. address send, NACK received + * 3 .. data send, NACK received + * 4 .. other twi error (lost bus arbitration, bus error, ..) + */ +uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait, uint8_t sendStop) +{ + uint8_t i; + twi_error = TWI_ERRROR_NO_ERROR; + twi_sendStop = sendStop; + +#ifdef __MSP430_HAS_USI__ + /* Disable START condition interrupt */ + USICTL1 &= ~USISTTIE; + /* I2C master mode */ + USICTL0 |= USIMST; +#endif +#ifdef __MSP430_HAS_USCI__ + UCB0CTL1 = UCSWRST; // Enable SW reset + UCB0CTL1 |= UCSSEL_2; // SMCLK + UCB0CTL0 |= (UCMST | UCMODE_3 | UCSYNC); // I2C Master, synchronous mode + UCB0CTL1 |= UCTR; // Configure in transmit mode + UCB0I2CSA = address; // Set Slave Address + UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation + UCB0I2CIE |= (UCALIE|UCNACKIE|UCSTPIE); // Enable I2C interrupts + UC0IE |= UCB0TXIE; // Enable I2C interrupts +#endif +#ifdef __MSP430_HAS_EUSCI_B0__ + UCB0CTLW0 = UCSWRST; // Enable SW reset + UCB0CTLW0 |= (UCMST | UCMODE_3 | UCSSEL__SMCLK | UCSYNC); // I2C Master, synchronous mode + UCB0CTLW0 |= UCTR; // Configure in transmit mode + UCB0I2CSA = address; // Set Slave Address + UCB0CTLW0 &= ~UCSWRST; // Clear SW reset, resume operation + UCB0IE |= (UCTXIE0|UCALIE|UCNACKIE|UCSTPIE); // Enable I2C interrupts +#endif + if(length == 0) { + return 0; + } + + /* Ensure data will fit into buffer */ + if(length > TWI_BUFFER_LENGTH){ + return TWI_ERROR_BUF_TO_LONG; + } + + + /* initialize buffer iteration vars */ + twi_masterBufferIndex = 0; + twi_masterBufferLength = length; + + for(i = 0; i < length; ++i){ + twi_masterBuffer[i] = data[i]; + } + +#ifdef __MSP430_HAS_USI__ + /* build sla+w, slave device address + w bit */ + twi_slarw = 0; + twi_slarw |= address << 1; + + twi_state = TWI_SND_START; + /* This will trigger an interrupt kicking off the state machine in the isr */ + USICTL1 |= USIIFG; +#endif +#ifdef __MSP430_HAS_USCI__ + twi_state = TWI_MTX; // Master Transmit mode + UCB0CTL1 |= UCTXSTT; // I2C start condition +#endif +#ifdef __MSP430_HAS_EUSCI_B0__ + twi_state = TWI_MTX; // Master Transmit mode + while (UCB0CTLW0 & UCTXSTP); // Ensure stop condition got sent + UCB0CTLW0 |= UCTXSTT; // I2C start condition +#endif + + /* Wait for the transaction to complete */ + while(twi_state != TWI_IDLE) { + __bis_SR_register(LPM0_bits); + } + +#ifdef __MSP430_HAS_USCI__ + /* Ensure stop condition got sent before we exit. */ + while (UCB0CTL1 & UCTXSTP); +#endif + + return twi_error; +} + +/* + * Function twi_transmit + * Desc fills slave tx buffer with data + * must be called in slave tx event callback + * Input data: pointer to byte array + * length: number of bytes in array + * Output 1 length too long for buffer + * 2 not slave transmitter + * 0 ok + */ +uint8_t twi_transmit(const uint8_t* data, uint8_t length) +{ + uint8_t i; + + twi_state = TWI_STX; // Slave transmit mode + + // ensure data will fit into buffer + if(TWI_BUFFER_LENGTH < length){ + return 1; + } + // set length and copy data into tx buffer + twi_txBufferLength = length; + for(i = 0; i < length; ++i){ + twi_txBuffer[i] = data[i]; + } + + return 0; +} + +/* + * Function twi_attachSlaveRxEvent + * Desc sets function called before a slave read operation + * Input function: callback function to use + * Output none + */ +void twi_attachSlaveRxEvent( void (*function)(uint8_t*, int) ) +{ + twi_onSlaveReceive = function; +} + +/* + * Function twi_attachSlaveTxEvent + * Desc sets function called before a slave write operation + * Input function: callback function to use + * Output none + */ +void twi_attachSlaveTxEvent( void (*function)(void) ) +{ + twi_onSlaveTransmit = function; +} + +void send_start() +{ +#ifdef __MSP430_HAS_USI__ + USISRL = 0x00; + USICTL0 |= USIGE+USIOE; + USICTL0 &= ~USIGE; + USISRL = twi_slarw; + USICNT = (USICNT & 0xE0) + 0x08; +#endif +} + +#ifdef __MSP430_HAS_USI__ +__attribute__((interrupt(USI_VECTOR))) +void USI_ISR(void) +{ + if (!(USICTL0 & USIMST) && (USICTL1 & USISTTIFG)) { + twi_state = TWI_SL_START; + } + + switch(twi_state){ + /* Master transmit / receive */ + case TWI_SND_START: + send_start(); + twi_state = TWI_PREP_SLA_ADDR_ACK; + break; + case TWI_PREP_SLA_ADDR_ACK: // reveive (N)ACK + USICTL0 &= ~USIOE; // SDA = input + USICNT |= 0x01; // Bit counter=1 + twi_state = TWI_MT_PROC_ADDR_ACK; + break; + case TWI_MT_PROC_ADDR_ACK: + if (USISRL & 0x01) { + twi_error = TWI_ERROR_ADDR_NACK; + USICTL0 |= USIOE; + USISRL = 0x00; + USICNT |= 0x01; + twi_state = TWI_EXIT; + break; + } + + if(twi_slarw & 1) + goto mtre; + else + goto mtpd; + + break; + /* Prepare to receive data (N)ACK */ + case TWI_MT_PREP_DATA_ACK: + /* SDA = input */ + USICTL0 &= ~USIOE; + /* Bit counter = 1 */ + USICNT |= 0x01; + twi_state = TWI_MT_PROC_DATA_ACK; + break; + case TWI_MT_PROC_DATA_ACK: +mtpd: + if (USISRL & 0x01) { + twi_error = TWI_ERROR_DATA_NACK; + USICTL0 |= USIOE; + USISRL = 0x00; + USICNT |= 0x01; + twi_state = TWI_EXIT; + break; + } + + if(twi_masterBufferIndex == twi_masterBufferLength) { + USICTL0 |= USIOE; + USISRL = 0x00; + USICNT |= 0x01; + twi_state = TWI_EXIT; + break; + } + + USICTL0 |= USIOE; + USISRL = twi_masterBuffer[twi_masterBufferIndex++]; + USICNT |= 0x08; + twi_state = TWI_MT_PREP_DATA_ACK; + break; + // Master receiver +mtre: + case TWI_MR_PREP_DATA_RECV: + /* SDA input */ + USICTL0 &= ~USIOE; + /* bit counter = 8 */ + USICNT |= 0x08; + twi_state = TWI_MR_PROC_DATA_RECV; + break; + case TWI_MR_PROC_DATA_RECV: + /* SDA output */ + USICTL0 |= USIOE; + + twi_masterBuffer[twi_masterBufferIndex++] = USISRL; + if(twi_masterBufferIndex > twi_masterBufferLength ) { + USISRL = 0xFF; // that was the last byte send NACK + twi_state = TWI_MR_PREP_STOP; + } else { + USISRL = 0x00; // keep on receiving and send ACK + twi_state = TWI_MR_PREP_DATA_RECV; + } + + USICNT |= 0x01; + break; + case TWI_MR_PREP_STOP: + USICTL0 |= USIOE; + USISRL = 0x00; + USICNT |= 0x01; + twi_state = TWI_EXIT; + break; + /* All */ + case TWI_EXIT: + /* Load FF into output shift register */ + USISRL = 0x0FF; + /* Output latch transparant. MSB of USISR to the SDO pin. */ + USICTL0 |= USIGE; + /* Latch disabled and make SDA input */ + USICTL0 &= ~(USIGE+USIOE); + twi_state = TWI_IDLE; + break; + case TWI_IDLE: + /* Nothing to do. Fall thru and clear interrupt flag. */ + default: + break; + } + + /* Clear counter interrupt */ + USICTL1 &= ~USIIFG; + +} +#endif /* __MSP430_HAS_USI__ */ + +#ifdef __MSP430_HAS_USCI__ +void i2c_txrx_isr(void) // RX/TX Service +{ + /* USCI I2C mode. USCI_B0 receive interrupt flag. + * UCB0RXIFG is set when UCB0RXBUF has received a complete character. */ + if (UC0IFG & UCB0RXIFG){ + /* Master receive mode. */ + if (twi_state == TWI_MRX) { + twi_masterBuffer[twi_masterBufferIndex++] = UCB0RXBUF; + if(twi_masterBufferIndex == twi_masterBufferLength ) + /* Only one byte left. Generate STOP condition. + * In master mode a STOP is preceded by a NACK */ + UCB0CTL1 |= UCTXSTP; + if(twi_masterBufferIndex > twi_masterBufferLength ) { + /* All bytes received. We are idle*/ + __bic_SR_register(LPM0_bits); + twi_state = TWI_IDLE; + } + /* Slave receive mode. (twi_state = TWI_SRX) */ + } else { + // if there is still room in the rx buffer + if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){ + // put byte in buffer and ack + twi_rxBuffer[twi_rxBufferIndex++] = UCB0RXBUF; + }else{ + // otherwise nack + UCB0CTL1 |= UCTXNACK; // Generate NACK condition + } + } + } + /* USCI I2C mode. USCI_B0 transmit interrupt flag. + * UCB0TXIFG is set when UCB0TXBUF is empty.*/ + if (UC0IFG & UCB0TXIFG){ + /* Master transmit mode */ + if (twi_state == TWI_MTX) { + // if there is data to send, send it, otherwise stop + if(twi_masterBufferIndex < twi_masterBufferLength){ + // Copy data to output register and ack. + UCB0TXBUF = twi_masterBuffer[twi_masterBufferIndex++]; + }else{ + if (twi_sendStop) { + /* All done. Generate STOP condition and IDLE */ + UCB0CTL1 |= UCTXSTP; + twi_state = TWI_IDLE; + __bic_SR_register(LPM0_bits); + } else { + twi_inRepStart = true; // we're gonna send the START + // don't enable the interrupt. We'll generate the start, but we + // avoid handling the interrupt until we're in the next transaction, + // at the point where we would normally issue the start. + UCB0CTL1 |= UCTXSTT; + twi_state = TWI_IDLE; + __bic_SR_register(LPM0_bits); + } + } + /* Slave transmit mode (twi_state = TWI_STX) */ + } else { + // copy data to output register + UCB0TXBUF = twi_txBuffer[twi_txBufferIndex++]; + // if there is more to send, ack, otherwise nack + if(twi_txBufferIndex < twi_txBufferLength){ + }else{ + UCB0CTL1 |= UCTXNACK; // Generate NACK condition + } + } + } +} + +void i2c_state_isr(void) // I2C Service +{ + /* Arbitration lost interrupt flag */ + if (UCB0STAT & UCALIFG) { + UCB0STAT &= ~UCALIFG; + /* TODO: Handle bus arbitration lost */ + } + /* Not-acknowledge received interrupt flag. + * UCNACKIFG is automatically cleared when a START condition is received.*/ + if (UCB0STAT & UCNACKIFG) { + UCB0STAT &= ~UCNACKIFG; + UCB0CTL1 |= UCTXSTP; + twi_state = TWI_IDLE; + /* TODO: This can just as well be an address NACK. + * Figure out a way to distinguish between ANACK and DNACK */ + twi_error = TWI_ERROR_DATA_NACK; + __bic_SR_register(LPM0_bits); + } + /* Start condition interrupt flag. + * UCSTTIFG is automatically cleared if a STOP condition is received. */ + if (UCB0STAT & UCSTTIFG) { + UCB0STAT &= ~UCSTTIFG; + /* UCTR is automagically set by the USCI module upon a START condition. */ + if (UCB0CTL1 & UCTR) { + /* Slave TX mode. */ + twi_state = TWI_STX; + /* Ready the tx buffer index for iteration. */ + twi_txBufferIndex = 0; + /* Set tx buffer length to be zero, to verify if user changes it. */ + twi_txBufferLength = 0; + /* Request for txBuffer to be filled and length to be set. */ + /* note: user must call twi_transmit(bytes, length) to do this */ + twi_onSlaveTransmit(); + /* If they didn't change buffer & length, initialize it + * TODO: Is this right? Shouldn't we reply with a NACK if there is no data to send? */ + if (0 == twi_txBufferLength) { + twi_txBufferLength = 1; + twi_txBuffer[0] = 0x00; + } + /* Enable I2C TX interrupts. + * UCB0TXIFG is automagically set by the USCI module upon start condition + * Enabling UCBTXIE will trigger a USCIAB0TX_VECTOR as soon as USCIAB0RX_VECTOR exits */ + UC0IE |= (UCB0TXIE); // Enable I2C TX interrupts + } else { + /* Slave receive mode. */ + twi_state = TWI_SRX; + /* Indicate that rx buffer can be overwritten and ACK */ + twi_rxBufferIndex = 0; + } + } + /* Stop condition interrupt flag. + * UCSTPIFG is automatically cleared when a START condition is received. */ + if (UCB0STAT & UCSTPIFG) { + UCB0STAT &= ~UCSTPIFG; + if (twi_state == TWI_SRX) { + /* Callback to user defined callback */ + twi_onSlaveReceive(twi_rxBuffer, twi_rxBufferIndex); + } + twi_state = TWI_IDLE; + } +} +#endif + +#ifdef __MSP430_HAS_EUSCI_B0__ +__attribute__((interrupt(USCI_B0_VECTOR))) +void USCI_B0_ISR(void) +{ +// switch(twi_state){ + switch(UCB0IV){ + case USCI_NONE: // No Interrupt pending + break; + case USCI_I2C_UCALIFG: // USCI I2C Mode: UCALIFG + // enter slave transmitter mode + twi_state = TWI_STX; + // ready the tx buffer index for iteration + twi_txBufferIndex = 0; + // set tx buffer length to be zero, to verify if user changes it + twi_txBufferLength = 0; + // request for txBuffer to be filled and length to be set + // note: user must call twi_transmit(bytes, length) to do this + twi_onSlaveTransmit(); + // if they didn't change buffer & length, initialize it + if(0 == twi_txBufferLength){ + twi_txBufferLength = 1; + twi_txBuffer[0] = 0x00; + } + // transmit first byte from buffer, fall + // copy data to output register + UCB0TXBUF = twi_txBuffer[twi_txBufferIndex++]; + // if there is more to send, ack, otherwise nack + if(twi_txBufferIndex < twi_txBufferLength){ + //twi_reply(1); + }else{ + UCB0CTLW0 |= UCTXNACK; // Generate NACK condition + } + // leave slave receiver state + twi_state = TWI_IDLE; + + break; + case USCI_I2C_UCNACKIFG: // USCI I2C Mode: UCNACKIFG + // leave slave receiver state + twi_state = TWI_IDLE; + twi_error = TWI_ERROR_DATA_NACK; + __bic_SR_register_on_exit(CPUOFF); // Exit LPM0 + break; + case USCI_I2C_UCSTTIFG: // USCI I2C Mode: UCSTTIFG + if (twi_state == TWI_IDLE){ + if (UCB0CTLW0 & UCTR){ + twi_state = TWI_STX; // Slave Transmit mode + // ready the tx buffer index for iteration + twi_txBufferIndex = 0; + // set tx buffer length to be zero, to verify if user changes it + twi_txBufferLength = 0; + // request for txBuffer to be filled and length to be set + // note: user must call twi_transmit(bytes, length) to do this + twi_onSlaveTransmit(); + // if they didn't change buffer & length, initialize it + if(0 == twi_txBufferLength){ + twi_txBufferLength = 1; + twi_txBuffer[0] = 0x00; + } + }else{ + twi_state = TWI_SRX; // Slave receive mode + // indicate that rx buffer can be overwritten and ack + twi_rxBufferIndex = 0; + + } + } + if (UCB0CTLW0 & UCTR) + UCB0IE |= (UCTXIE0); // Enable I2C TX interrupts + break; + case USCI_I2C_UCSTPIFG: // USCI I2C Mode: UCSTPIFG + if (twi_state == TWI_SRX){ + // callback to user defined callback + twi_onSlaveReceive(twi_rxBuffer, twi_rxBufferIndex); + } + twi_state = TWI_IDLE; // IDLE mode + __bic_SR_register_on_exit(CPUOFF); // Exit LPM0 + break; + case USCI_I2C_UCRXIFG3: // USCI I2C Mode: UCRXIFG3 + break; + case USCI_I2C_UCTXIFG3: // USCI I2C Mode: UCTXIFG3 + break; + case USCI_I2C_UCRXIFG2: // USCI I2C Mode: UCRXIFG2 + break; + case USCI_I2C_UCTXIFG2: // USCI I2C Mode: UCTXIFG2 + break; + case USCI_I2C_UCRXIFG1: // USCI I2C Mode: UCRXIFG1 + break; + case USCI_I2C_UCTXIFG1: // USCI I2C Mode: UCTXIFG1 + break; + case USCI_I2C_UCRXIFG0: // USCI I2C Mode: UCRXIFG0 + if (twi_state == TWI_MRX) { // Master receive mode + twi_masterBuffer[twi_masterBufferIndex++] = UCB0RXBUF; // Get RX data + if(twi_masterBufferIndex == twi_masterBufferLength ) + UCB0CTLW0 |= UCTXSTP; // Generate I2C stop condition + if(twi_masterBufferIndex > twi_masterBufferLength ) { + twi_state = TWI_IDLE; //Idle + } else { + } + } else { + // if there is still room in the rx buffer + if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){ + // put byte in buffer and ack + twi_rxBuffer[twi_rxBufferIndex++] = UCB0RXBUF; + }else{ + // otherwise nack + UCB0CTLW0 |= UCTXNACK; // Generate NACK condition + } + } + break; + case USCI_I2C_UCTXIFG0: // USCI I2C Mode: UCTXIFG0 + if (twi_state == TWI_MTX) { // Master receive mode + // if there is data to send, send it, otherwise stop + if(twi_masterBufferIndex < twi_masterBufferLength){ + // copy data to output register and ack + UCB0TXBUF = twi_masterBuffer[twi_masterBufferIndex++]; // Transmit data at address PTxData + }else{ + if (twi_sendStop) + UCB0CTLW0 |= UCTXSTP; // Generate I2C stop condition + else { + twi_inRepStart = true; // we're gonna send the START + // don't enable the interrupt. We'll generate the start, but we + // avoid handling the interrupt until we're in the next transaction, + // at the point where we would normally issue the start. + UCB0CTLW0 |= UCTXSTT; + twi_state = TWI_IDLE; + } + UCB0IFG &= ~UCTXIFG; // Clear USCI_B0 TX int flag + } + } else { + // copy data to output register + UCB0TXBUF = twi_txBuffer[twi_txBufferIndex++]; + // if there is more to send, ack, otherwise nack + if(twi_txBufferIndex < twi_txBufferLength){ + }else{ + UCB0CTLW0 |= UCTXNACK; // Generate NACK condition + } + } + break; + case USCI_I2C_UCBCNTIFG: // USCI I2C Mode: UCBCNTIFG + break; + case USCI_I2C_UCCLTOIFG: // USCI I2C Mode: UCCLTOIFG/ + break; + case USCI_I2C_UCBIT9IFG: // USCI I2C Mode: UCBIT9IFG + break; + } +} +#endif + diff --git a/msp430/cores/msp430/twi.h b/msp430/cores/msp430/twi.h new file mode 100644 index 0000000..ea383b6 --- /dev/null +++ b/msp430/cores/msp430/twi.h @@ -0,0 +1,104 @@ +/* + ************************************************************************ + * twi.h + * + * Arduino core files for MSP430 + * Copyright (c) 2012 Robert Wessels. All right reserved. + * + * + *********************************************************************** + Derived from: + twi.h - TWI/I2C library for Wiring & Arduino + Copyright (c) 2006 Nicholas Zambetti. 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 twi_h +#define twi_h + +#include <msp430.h> + +#if !defined (__MSP430_HAS_USI__) && !defined (__MSP430_HAS_USCI__) && !defined (__MSP430_HAS_EUSCI_B0__) +#ifndef __MSP430_HAS_USI__ +#error "********** USI not available" +#endif + +#if !defined (__MSP430_HAS_USCI__) && !defined (__MSP430_HAS_EUSCI_B0__) +#error "********** USCI not available" +#endif +#endif + + +#include <inttypes.h> + +#ifndef TWI_FREQ +#define TWI_FREQ 100000L +#endif + +#ifndef TWI_BUFFER_LENGTH +#define TWI_BUFFER_LENGTH 16 +#endif + + +#define TWI_READY 0 +#define TWI_MRX 1 +#define TWI_MTX 2 +#define TWI_SRX 3 +#define TWI_STX 4 + + +#define TWI_SND_START 0 +#define TWI_PREP_SLA_ADDR_ACK 1 +#define TWI_MT_PROC_ADDR_ACK 2 +#define TWI_MT_PREP_DATA_ACK 3 +#define TWI_MT_PROC_DATA_ACK 4 +#define TWI_MR_PREP_DATA_RECV 5 +#define TWI_MR_PROC_DATA_RECV 6 +#define TWI_MR_PREP_STOP 7 + +#define TWI_SL_START 8 +#define TWI_SL_PROC_ADDR 9 +#define TWI_SL_SEND_BYTE 10 +#define TWI_SL_PREP_DATA_ACK 11 +#define TWI_SL_PROC_DATA_ACK 12 +#define TWI_SL_RECV_BYTE 13 +#define TWI_SL_PROC_BYTE 14 +#define TWI_SL_RESET 15 +#define TWI_EXIT 16 +#define TWI_IDLE 17 + + +#define TWI_ERRROR_NO_ERROR 0 +#define TWI_ERROR_BUF_TO_LONG 1 +#define TWI_ERROR_ADDR_NACK 2 +#define TWI_ERROR_DATA_NACK 3 +#define TWI_ERROR_OTHER 4 + + + +void twi_init(void); +void twi_setAddress(uint8_t); +uint8_t twi_readFrom(uint8_t, uint8_t*, uint8_t, uint8_t); +uint8_t twi_writeTo(uint8_t, uint8_t*, uint8_t, uint8_t, uint8_t); +uint8_t twi_transmit(const uint8_t*, uint8_t); +void twi_attachSlaveRxEvent( void (*)(uint8_t*, int) ); +void twi_attachSlaveTxEvent( void (*)(void) ); +void twi_reply(uint8_t); +void twi_stop(void); +void twi_releaseBus(void); + +#endif + diff --git a/msp430/cores/msp430/usci_isr_handler.c b/msp430/cores/msp430/usci_isr_handler.c new file mode 100644 index 0000000..1ea8b8c --- /dev/null +++ b/msp430/cores/msp430/usci_isr_handler.c @@ -0,0 +1,44 @@ +#include "Energia.h" +#if defined(__MSP430_HAS_USCI__) +#include "usci_isr_handler.h" + +/* This dummy function ensures that, when called from any module that + * is interested in having the USCIAB0TX_VECTOR and USCIAB0TX_VECTOR + * installed, the linker won't strip the vectors.*/ +void usci_isr_install(){} + +/* USCI_Ax and USCI_Bx share the same TX interrupt vector. + * UART: + * USCIAB0TX_VECTOR services the UCA0TXIFG set in UC0IFG. + * USCIAB0RX_VECTOR services the UCA0RXIFG set in UC0IFG. + * I2C: + * USCIAB0TX_VECTOR services both UCB0TXIFG and UCB0RXIFG + * set in UC0IFG. + * USCIAB0RX_VECTOR services the state change interrupts + * UCSTTIFG, UCSTPIFG, UCIFG, UCALIFG set in UCB0STAT.*/ + +__attribute__((interrupt(USCIAB0TX_VECTOR))) +void USCIAB0TX_ISR(void) +{ + /* USCI_A0 UART interrupt? */ + if (UC0IFG & UCA0TXIFG) + uart_tx_isr(); + + /* USCI_B0 I2C TX RX interrupt. */ + if ((UC0IFG & (UCB0TXIFG | UCB0RXIFG)) != 0) + i2c_txrx_isr(); + +} + +__attribute__((interrupt(USCIAB0RX_VECTOR))) +void USCIAB0RX_ISR(void) +{ + /* USCI_A0 UART interrupt? */ + if (UC0IFG & UCA0RXIFG) + uart_rx_isr(); + + /* USCI_B0 I2C state change interrupt. */ + if ((UCB0STAT & (UCALIFG | UCNACKIFG | UCSTTIFG | UCSTPIFG)) != 0) + i2c_state_isr(); +} +#endif diff --git a/msp430/cores/msp430/usci_isr_handler.h b/msp430/cores/msp430/usci_isr_handler.h new file mode 100644 index 0000000..b207fc3 --- /dev/null +++ b/msp430/cores/msp430/usci_isr_handler.h @@ -0,0 +1,18 @@ +#ifndef usci_isr_handler_h +#define usci_isr_handler_h + +#ifdef __MSP430_HAS_USCI__ + +#ifdef __cplusplus +extern "C" { +#endif +void uart_tx_isr(void); +void uart_rx_isr(void); +void i2c_txrx_isr(void); +void i2c_state_isr(void); +void usci_isr_install(void); +#ifdef __cplusplus +} +#endif +#endif /* __MSP430_HAS_USCI__ */ +#endif /* usci_isr_handler_h */ diff --git a/msp430/cores/msp430/wiring.c b/msp430/cores/msp430/wiring.c new file mode 100644 index 0000000..b5fc5ab --- /dev/null +++ b/msp430/cores/msp430/wiring.c @@ -0,0 +1,198 @@ +/* + ************************************************************************ + * wiring.c + * + * Arduino core files for MSP430 + * Copyright (c) 2012 Robert Wessels. All right reserved. + * + * + *********************************************************************** + Derived from: + wiring.c - Partial implementation of the Wiring API for the ATmega8. + 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 +*/ +#include "Energia.h" + +void initClocks(void); +void enableWatchDogIntervalMode(void); + +void init() +{ + disableWatchDog(); + initClocks(); + enableWatchDogIntervalMode(); + __eint(); +} + +void disableWatchDog() +{ + /* Diable watchdog timer */ + WDTCTL = WDTPW | WDTHOLD; +} + +void enableWatchDog() +{ + enableWatchDogIntervalMode(); +} + +/* WDT_TICKS_PER_MILISECOND = (F_CPU / WDT_DIVIDER) / 1000 + * WDT_TICKS_PER_MILISECONDS = 1.953125 = 2 */ +#define SMCLK_FREQUENCY F_CPU +#if (F_CPU >= 2000000L) +#define WDT_TICKS_PER_MILISECOND 2 +#define WDT_DIVIDER 8192 +#define WDT_DIV_BITS WDTIS0 +#else +#define WDT_TICKS_PER_MILISECOND 2 +#define WDT_DIVIDER 512 +#define WDT_DIV_BITS WDTIS1 +#endif + +void enableWatchDogIntervalMode(void) +{ + /* WDT Password + WDT interval mode + Watchdog clock source /512 + source from SMCLK + * Note that we WDT is running in interval mode. WDT will not trigger a reset on expire in this mode. */ + WDTCTL = WDTPW + WDTTMSEL + WDTCNTCL + WDT_DIV_BITS; + + /* WDT interrupt enable */ + IE1 |= WDTIE; +} + +void initClocks(void) +{ +#if defined(CALBC1_16MHZ_) && F_CPU >= 16000000L + BCSCTL1 = CALBC1_16MHZ; + DCOCTL = CALDCO_16MHZ; +#elif defined(CALBC1_12MHZ_) && (F_CPU >= 12000000L) + BCSCTL1 = CALBC1_12MHZ; + DCOCTL = CALDCO_12MHZ; +#elif defined(CALBC1_8MHZ_) && (F_CPU >= 8000000L) + BCSCTL1 = CALBC1_8MHZ; + DCOCTL = CALDCO_8MHZ; +#elif defined(CALBC1_1MHZ_) && (F_CPU >= 1000000L) + BCSCTL1 = CALBC1_1MHZ; + DCOCTL = CALDCO_1MHZ; +#else + #warning No Suitable Frequency found! +#endif + /* SMCLK = DCO / DIVS = nMHz */ + BCSCTL2 &= ~(DIVS_0); + /* ACLK = VLO = ~ 12 KHz */ + BCSCTL3 |= LFXT1S_2; +} + + + +volatile uint32_t wdtCounter = 0; + +unsigned long micros() +{ + return (1000 * wdtCounter) / WDT_TICKS_PER_MILISECOND; +} + +unsigned long millis() +{ + return wdtCounter / WDT_TICKS_PER_MILISECOND; +} + +/* Delay for the given number of microseconds. Assumes a 1, 8 or 16 MHz clock. */ +void delayMicroseconds(unsigned int us) +{ +#if F_CPU >= 20000000L + /* For a one-microsecond delay, simply wait 2 cycle and return. The overhead + * of the function call yields a delay of exactly one microsecond. */ + __asm__ __volatile__ ( + "nop" "\n\t" + "nop"); + if (--us == 0) + return; + + /* The following loop takes a 1/5 of a microsecond (4 cycles) + * per iteration, so execute it five times for each microsecond of + * delay requested. */ + us = (us<<2) + us; // x5 us + + /* Account for the time taken in the preceeding commands. */ + us -= 2; + +#elif F_CPU >= 16000000L + /* For the 16 MHz clock on most boards */ + + /* For a one-microsecond delay, simply return. the overhead + * of the function call yields a delay of approximately 1 1/8 us. */ + if (--us == 0) + return; + + /* The following loop takes a quarter of a microsecond (4 cycles) + * per iteration, so execute it four times for each microsecond of + * delay requested. */ + us <<= 2; + + /* Account for the time taken in the preceeding commands. */ + us -= 2; +#else + /* For the 1 MHz */ + + /* For a one- or two-microsecond delay, simply return. the overhead of + * the function calls takes more than two microseconds. can't just + * subtract two, since us is unsigned; we'd overflow. */ + if (--us == 0) + return; + if (--us == 0) + return; + + /* The following loop takes 4 microsecond (4 cycles) + * per iteration, so execute it ones for each 4 microsecond of + * delay requested. */ + us >>= 2; + + /* Partially compensate for the time taken by the preceeding commands. + * we can't subtract any more than this or we'd overflow w/ small delays. */ + us--; +#endif + + /* Busy wait */ + __asm__ __volatile__ ( + /* even steven */ + "L1: nop \n\t" + /* 1 instruction */ + "dec.w %[us] \n\t" + /* 2 instructions */ + "jnz L1 \n\t" + : [us] "=r" (us) : "[us]" (us) + ); +} + +/* (ab)use the WDT */ +void delay(uint32_t milliseconds) +{ + uint32_t wakeTime = wdtCounter + (milliseconds * WDT_TICKS_PER_MILISECOND); + while(wdtCounter < wakeTime) + /* Wait for WDT interrupt in LMP0 */ + __bis_status_register(LPM0_bits+GIE); +} + +__attribute__((interrupt(WDT_VECTOR))) +void watchdog_isr (void) +{ + wdtCounter++; + /* Exit from LMP3 on reti (this includes LMP0) */ + __bic_status_register_on_exit(LPM3_bits); +} diff --git a/msp430/cores/msp430/wiring_analog.c b/msp430/cores/msp430/wiring_analog.c new file mode 100644 index 0000000..a076d8b --- /dev/null +++ b/msp430/cores/msp430/wiring_analog.c @@ -0,0 +1,204 @@ +/* + ************************************************************************ + * wiring_analog.c + * + * Arduino core files for MSP430 + * Copyright (c) 2012 Robert Wessels. All right reserved. + * + * + *********************************************************************** + Derived from: + wiring_analog.c - analog input and output + 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 +*/ + +#include "wiring_private.h" +#include "pins_energia.h" + +uint16_t analog_reference = DEFAULT, analog_period = F_CPU/490, analog_div = 0, analog_res=255; // devide clock with 0, 2, 4, 8 + +void analogReference(uint16_t mode) +{ + // can't actually set the register here because the default setting + // will connect AVCC and the AREF pin, which would cause a short if + // there's something connected to AREF. + analog_reference = mode; +} + +//TODO: Can be a lot more efficiant. +// - lower clock rated / input devider to conserve Energia. +// - pin configuration logic. + +// Note set frequency before sending analog value +// Lowest fequency is defined by clock frequency F_CPU, and max counter value 2^16-1 +// fmin = F_CPU / 2^16 +void analogFrequency(uint32_t freq) +{ + if ( freq <= F_CPU/(4*65334L) ) { analog_div = ID_3; freq *=8; } + else if ( freq <= F_CPU/(2*65334L) ) { analog_div = ID_2; freq *=4; } + else if ( freq <= F_CPU/(4*65334L) ) { analog_div = ID_1; freq *=2; } + analog_period = F_CPU/freq; +} + +// Set the resulution (nr of counts for 100%), default = 255, large values may not work at all frequencies +void analogResolution(uint16_t res) +{ + analog_res = res; +} + +//Arduino specifies ~490 Hz for analog out PWM so we follow suit. +#define PWM_PERIOD analog_period // F_CPU/490 +#define PWM_DUTY(x) ( (unsigned long)x*PWM_PERIOD / (unsigned long)analog_res ) +void analogWrite(uint8_t pin, int val) +{ + pinMode(pin, OUTPUT); // pin as output + + if (val == 0) + { + digitalWrite(pin, LOW); // set pin to LOW when duty cycle is 0 + // digitalWrite will take care of invalid pins + } + else if (val == 255) + { + digitalWrite(pin, HIGH); // set pin HIGH when duty cycle is 255 + // digitalWrite will take care of invalid pins + } + else + { + + uint8_t bit = digitalPinToBitMask(pin); // get pin bit + uint8_t port = digitalPinToPort(pin); // get pin port + volatile uint16_t *sel; + volatile uint16_t *sel2; + + if (port == NOT_A_PORT) return; // pin on timer? + + sel = portSelRegister(port); // get the port function select register address + *sel |= bit; // set bit in pin function select register + + //TODO: Firgure out a better way to determine if SEL2 needs to be set + if(bit == BV(4) && port == P1) { + sel2 = portSel2Register(port); // get the port function select register address + *sel2 |= bit; + } + switch(digitalPinToTimer(pin)) { // which timer and CCR? + //case: T0A0 // CCR0 used as period register + case T0A1: // Timer0 / CCR1 + TA0CCR0 = PWM_PERIOD; // PWM Period + TA0CCTL1 = OUTMOD_7; // reset/set + TA0CCR1 = PWM_DUTY(val); // PWM duty cycle + TA0CTL = TASSEL_2 + MC_1 + analog_div; // SMCLK, up mode + break; +#if defined(__MSP430_HAS_TA3__) + case T0A2: // Timer0 / CCR1 + TA0CCR0 = PWM_PERIOD; // PWM Period + TA0CCTL2 = OUTMOD_7; // reset/set + TA0CCR2 = PWM_DUTY(val); // PWM duty cycle + TA0CTL = TASSEL_2 + MC_1+ analog_div; // SMCLK, up mode + break; +#endif +#if defined(__MSP430_HAS_T1A3__) + //case: T1A0 // CCR0 used as period register + case T1A1: // Timer0 / CCR1 + TA1CCR0 = PWM_PERIOD; // PWM Period + TA1CCTL1 = OUTMOD_7; // reset/set + TA1CCR1 = PWM_DUTY(val); // PWM duty cycle + TA1CTL = TASSEL_2 + MC_1+ analog_div; // SMCLK, up mode + break; + case T1A2: // Timer0 / CCR1 + TA1CCR0 = PWM_PERIOD; // PWM Period + TA1CCTL2 = OUTMOD_7; // reset/set + TA1CCR2 = PWM_DUTY(val); // PWM duty cycle + TA1CTL = TASSEL_2 + MC_1+ analog_div; // SMCLK, up mode + break; +#endif +#if defined(__MSP430_HAS_T2A3__) + //case: T2A0 // CCR0 used as period register + case T2A1: // Timer0 / CCR1 + TA2CCR0 = PWM_PERIOD; // PWM Period + TA2CCTL1 = OUTMOD_7; // reset/set + TA2CCR1 = PWM_DUTY(val); // PWM duty cycle + TA2CTL = TASSEL_2 + MC_1+ analog_div; // SMCLK, up mode + break; + case T2A2: // Timer0 / CCR1 + TA2CCR0 = PWM_PERIOD; // PWM Period + TA2CCTL2 = OUTMOD_7; // reset/set + TA2CCR2 = PWM_DUTY(val); // PWM duty cycle + TA2CTL = TASSEL_2 + MC_1+ analog_div; // SMCLK, up mode + break; +#endif + case NOT_ON_TIMER: // not on a timer output pin + default: // or TxA0 pin + if (val < 128) { + digitalWrite(pin, LOW); // + } else { + digitalWrite(pin, HIGH); + } + } + } +} + +uint16_t analogRead(uint8_t pin) +{ +// make sure we have an ADC +#if defined(__MSP430_HAS_ADC10__) + // 0000 A0 + // 0001 A1 + // 0010 A2 + // 0011 A3 + // 0100 A4 + // 0101 A5 + // 0110 A6 + // 0111 A7 + // 1010 Internal temperature sensor + + //TODO: Only int. temp. sensor requires Tsample > 30us. + // The below ADC configuration applies this rule to all channels right now. + // ADC10CLK = 5MHz / 5 = 1Mhz + // Tsample = S&H / ADC10CLK = 64 / 1 MHz = 64 us + // Tconvert = 13 / ADC10CLK = 13 / 1 MHz = 13 us + // Total time per sample = Tconvert + Tsample = 64 + 13 = 67 us = ~15k samples / sec + if (pin > 7 && pin != 10) { // Is this a valid ADC pin? + return 0; + } + + ADC10CTL0 &= ~ENC; // disable ADC + ADC10CTL0 = analog_reference | // set analog reference + ADC10ON | ADC10SHT_3 | ADC10IE; // turn ADC ON; sample + hold @ 64 × ADC10CLKs; Enable interrupts + ADC10CTL1 = ADC10SSEL_0 | ADC10DIV_5 | // ADC10OSC as ADC10CLK (~5MHz) / 5 + (pin << 12); // select channel + __delay_cycles(128); // Delay to allow Ref to settle + ADC10CTL0 |= ENC | ADC10SC; // enable ADC and start conversion + while (ADC10CTL1 & ADC10BUSY) { // sleep and wait for completion + __bis_SR_register(CPUOFF + GIE); // LPM0 with interrupts enabled + } + + return ADC10MEM; // return sampled value after returning to active mode in ADC10_ISR +#else + // no ADC + return 0; +#endif +} + +__attribute__((interrupt(ADC10_VECTOR))) +void ADC10_ISR(void) +{ + __bic_SR_register_on_exit(CPUOFF); // return to active mode +} diff --git a/msp430/cores/msp430/wiring_digital.c b/msp430/cores/msp430/wiring_digital.c new file mode 100644 index 0000000..9fa207e --- /dev/null +++ b/msp430/cores/msp430/wiring_digital.c @@ -0,0 +1,100 @@ +/* + ************************************************************************ + * HardwareSerial.cpp + * + * Arduino core files for MSP430 + * Copyright (c) 2012 Robert Wessels. All right reserved. + * + * + *********************************************************************** + Derived from: + wiring_digital.c - digital input and output functions + 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 +*/ + +#define ARDUINO_MAIN +#include "wiring_private.h" +#include "pins_energia.h" + +void pinMode(uint8_t pin, uint8_t mode) +{ + uint8_t bit = digitalPinToBitMask(pin); + uint8_t port = digitalPinToPort(pin); + + volatile uint16_t *dir; + volatile uint16_t *ren; + volatile uint16_t *out; + + if (port == NOT_A_PORT) return; + + dir = portDirRegister(port); + ren = portRenRegister(port); + out = portOutputRegister(port); + + if (mode == INPUT) { + *dir &= ~bit; + } else if (mode == INPUT_PULLUP) { + *dir &= ~bit; + *out |= bit; + *ren |= bit; + } else if (mode == INPUT_PULLDOWN) { + *dir &= ~bit; + *out &= ~bit; + *ren |= bit; + } else { + *dir |= bit; + } +} + +int digitalRead(uint8_t pin) +{ + uint8_t bit = digitalPinToBitMask(pin); + uint8_t port = digitalPinToPort(pin); + + if (port == NOT_A_PORT) return LOW; + + if (*portInputRegister(port) & bit) return HIGH; + return LOW; +} + +void digitalWrite(uint8_t pin, uint8_t val) +{ + uint8_t bit = digitalPinToBitMask(pin); + uint8_t port = digitalPinToPort(pin); + volatile uint16_t *out; + volatile uint16_t *sel; + + if (port == NOT_A_PORT) return; + + /* + * Clear bit in PxSEL register to select GPIO function. Other functions like analogWrite(...) + * will set this bit so need to clear it. + */ + sel = portSelRegister(port); /* get the port function select register address */ + *sel &= ~bit; /* clear bit in pin function select register */ + + out = portOutputRegister(port); + + if (val == LOW) { + *out &= ~bit; + } else { + *out |= bit; + } +} diff --git a/msp430/cores/msp430/wiring_private.h b/msp430/cores/msp430/wiring_private.h new file mode 100644 index 0000000..9ed8c6f --- /dev/null +++ b/msp430/cores/msp430/wiring_private.h @@ -0,0 +1,60 @@ +/* + ************************************************************************ + * wiring_private.h + * + * Arduino core files for MSP430 + * Copyright (c) 2012 Robert Wessels. All right reserved. + * + * + *********************************************************************** + Derived from: + wiring_private.h - Internal header file. + 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.h 239 2007-01-12 17:58:39Z mellis $ +*/ + +#ifndef WiringPrivate_h +#define WiringPrivate_h + +#include "Energia.h" + +#ifdef __cplusplus +extern "C"{ +#endif + +// TODO: This is a hack and needs cleaning up. Not all pins are available on the board +//.Change this to a more intelligent number of interrupt selection + +#if defined(__MSP430_HAS_PORT1_R__) +#define EXTERNAL_NUM_INTERRUPTS 8 +#elif defined(__MSP430_HAS_PORT2_R__) +#define EXTERNAL_NUM_INTERRUPTS 16 +#else +#define EXTERNAL_NUM_INTERRUPTS 8 +#endif + +typedef void (*voidFuncPtr)(void); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif diff --git a/msp430/cores/msp430/wiring_pulse.c b/msp430/cores/msp430/wiring_pulse.c new file mode 100644 index 0000000..6375717 --- /dev/null +++ b/msp430/cores/msp430/wiring_pulse.c @@ -0,0 +1,76 @@ +/* + ************************************************************************ + * wiring_pulse.c + * + * Energia core files for MSP430 + * Copyright (c) 2012 Robert Wessels. All right reserved. + * + * + *********************************************************************** + Derived from: + wiring_pulse.c - pulseIn() 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 +*/ + +#include "wiring_private.h" +#include "pins_energia.h" + +/* Measures the length (in microseconds) of a pulse on the pin; state is HIGH + * or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds + * to 3 minutes in length, but must be called at least a few dozen microseconds + * before the start of the pulse. */ +unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout) +{ + // cache the port and bit of the pin in order to speed up the + // pulse width measuring loop and achieve finer resolution. calling + // digitalRead() instead yields much coarser resolution. + uint8_t bit = digitalPinToBitMask(pin); + uint8_t port = digitalPinToPort(pin); + uint8_t stateMask = (state ? bit : 0); + unsigned long width = 0; // keep initialization out of time critical area + + // convert the timeout from microseconds to a number of times through + // the initial loop; it takes 11 clock cycles per iteration. + unsigned long numloops = 0; + unsigned long maxloops = microsecondsToClockCycles(timeout) / 11; + + // wait for any previous pulse to end + while ((*portInputRegister(port) & bit) == stateMask) + if (numloops++ == maxloops) + return 0; + + // wait for the pulse to start + while ((*portInputRegister(port) & bit) != stateMask) + if (numloops++ == maxloops) + return 0; + + // wait for the pulse to stop + while ((*portInputRegister(port) & bit) == stateMask) { + if (numloops++ == maxloops) + return 0; + width++; + } + + // convert the reading to microseconds. The loop has been determined + // to be 13 clock cycles long and have about 11 clocks between the edge + // and the start of the loop. There will be some error introduced by + // the interrupt handlers. + return clockCyclesToMicroseconds(width * 13 + 11); +} diff --git a/msp430/cores/msp430/wiring_shift.c b/msp430/cores/msp430/wiring_shift.c new file mode 100644 index 0000000..30d2b56 --- /dev/null +++ b/msp430/cores/msp430/wiring_shift.c @@ -0,0 +1,63 @@ +/* + ************************************************************************ + * wiring_shift.c + * + * Energia core files for MSP430 + * Copyright (c) 2012 Robert Wessels. All right reserved. + * + * + *********************************************************************** + Derived from: + 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 + +*/ + +#include "wiring_private.h" + +uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) { + uint8_t value = 0; + uint8_t i; + + for (i = 0; i < 8; ++i) { + digitalWrite(clockPin, HIGH); + if (bitOrder == LSBFIRST) + value |= digitalRead(dataPin) << i; + else + value |= digitalRead(dataPin) << (7 - i); + digitalWrite(clockPin, LOW); + } + return value; +} + +void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val) +{ + uint8_t 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/msp430/libraries/IRremote/IRremote.cpp b/msp430/libraries/IRremote/IRremote.cpp new file mode 100644 index 0000000..18d0209 --- /dev/null +++ b/msp430/libraries/IRremote/IRremote.cpp @@ -0,0 +1,758 @@ +/* + * IRremote + * Version 0.11 August, 2009 + * Copyright 2009 Ken Shirriff + * For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.html + * + * Modified by Paul Stoffregen <paul@pjrc.com> to support other boards and timers + * + * Interrupt code based on NECIRrcv by Joe Knapp + * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556 + * Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/ + */ + +#include "IRremote.h" +#include "IRremoteInt.h" + +volatile irparams_t irparams; + +// These versions of MATCH, MATCH_MARK, and MATCH_SPACE are only for debugging. +// To use them, set DEBUG in IRremoteInt.h +// Normally macros are used for efficiency + +int MATCH(int measured, int desired) { + #ifdef DEBUG + Serial.print("Testing: "); + Serial.print(TICKS_LOW(desired), DEC); + Serial.print(" <= "); + Serial.print(measured, DEC); + Serial.print(" <= "); + Serial.println(TICKS_HIGH(desired), DEC); + #endif + return measured >= TICKS_LOW(desired) && measured <= TICKS_HIGH(desired); +} + +int MATCH_MARK(int measured_ticks, int desired_us) { + #ifdef DEBUG + Serial.print("Testing mark "); + Serial.print(measured_ticks * USECPERTICK, DEC); + Serial.print(" vs "); + Serial.print(desired_us, DEC); + Serial.print(": "); + Serial.print(TICKS_LOW(desired_us - MARK_EXCESS), DEC); // SH 071112 + -> - + Serial.print(" <= "); + Serial.print(measured_ticks, DEC); + Serial.print(" <= "); + Serial.println(TICKS_HIGH(desired_us + MARK_EXCESS), DEC); + #endif + return measured_ticks >= TICKS_LOW(desired_us - MARK_EXCESS) && measured_ticks <= TICKS_HIGH(desired_us + MARK_EXCESS); // SH 071112 + -> - in TICKS_LOW +} + +int MATCH_SPACE(int measured_ticks, int desired_us) { + #ifdef DEBUG + Serial.print("Testing space "); + Serial.print(measured_ticks * USECPERTICK, DEC); + Serial.print(" vs "); + Serial.print(desired_us, DEC); + Serial.print(": "); + Serial.print(TICKS_LOW(desired_us - MARK_EXCESS), DEC); + Serial.print(" <= "); + Serial.print(measured_ticks, DEC); + Serial.print(" <= "); + Serial.println(TICKS_HIGH(desired_us + MARK_EXCESS), DEC); // SH 071112 - -> + + #endif + return measured_ticks >= TICKS_LOW(desired_us - MARK_EXCESS) && measured_ticks <= TICKS_HIGH(desired_us + MARK_EXCESS); // SH 071112 - -> + in TICKS_HIGH +} + +// Soojin Han: this function is for serial debug interface +void debug_print(const char *data) +{ + #ifdef DEBUG + delay(100); + Serial.println(data); + #endif +} + +void IRsend::sendNEC(unsigned long data, int nbits) +{ + enableIROut(38); + mark(NEC_HDR_MARK); + space(NEC_HDR_SPACE); + for (int i = 0; i < nbits; i++) { + if (data & TOPBIT) { + mark(NEC_BIT_MARK); + space(NEC_ONE_SPACE); + } + else { + mark(NEC_BIT_MARK); + space(NEC_ZERO_SPACE); + } + data <<= 1; + } + mark(NEC_BIT_MARK); + space(0); +} + +void IRsend::sendSony(unsigned long data, int nbits) { + enableIROut(40); + mark(SONY_HDR_MARK); + space(SONY_HDR_SPACE); + data = data << (32 - nbits); + for (int i = 0; i < nbits; i++) { + if (data & TOPBIT) { + mark(SONY_ONE_MARK); + space(SONY_HDR_SPACE); + } + else { + mark(SONY_ZERO_MARK); + space(SONY_HDR_SPACE); + } + data <<= 1; + } +} + +void IRsend::sendRaw(unsigned int buf[], int len, int hz) +{ + enableIROut(hz); + for (int i = 0; i < len; i++) { + if (i & 1) { + space(buf[i]); + } + else { + mark(buf[i]); + } + } + space(0); // Just to be sure +} + +// Note: first bit must be a one (start bit) +void IRsend::sendRC5(unsigned long data, int nbits) +{ + enableIROut(36); + data = data << (32 - nbits); + mark(RC5_T1); // First start bit + space(RC5_T1); // Second start bit + mark(RC5_T1); // Second start bit + for (int i = 0; i < nbits; i++) { + if (data & TOPBIT) { + space(RC5_T1); // 1 is space, then mark + mark(RC5_T1); + } + else { + mark(RC5_T1); + space(RC5_T1); + } + data <<= 1; + } + space(0); // Turn off at end +} + +// Caller needs to take care of flipping the toggle bit +void IRsend::sendRC6(unsigned long data, int nbits) +{ + enableIROut(36); + data = data << (32 - nbits); + mark(RC6_HDR_MARK); + space(RC6_HDR_SPACE); + mark(RC6_T1); // start bit + space(RC6_T1); + int t; + for (int i = 0; i < nbits; i++) { + if (i == 3) { + // double-wide trailer bit + t = 2 * RC6_T1; + } + else { + t = RC6_T1; + } + if (data & TOPBIT) { + mark(t); + space(t); + } + else { + space(t); + mark(t); + } + + data <<= 1; + } + space(0); // Turn off at end +} + + +void IRsend::mark(int time) { + // Sends an IR mark for the specified number of microseconds. + // The mark output is modulated at the PWM frequency. + TIMER_ENABLE_PWM; // Set SMCLK, Up mode + delayMicroseconds(time); +} + +/* Leave pin off for time (given in microseconds) */ +void IRsend::space(int time) { + // Sends an IR space for the specified number of microseconds. + // A space is no output, so the PWM output is disabled. + TIMER_DISABLE_PWM; // Set SMCLK, Stop mode + delayMicroseconds(time); +} + +// TODO: update the comments below + +void IRsend::enableIROut(int khz) { + // Enables IR output. The khz value controls the modulation frequency in kilohertz. + // The IR output will be on pin 3 (OC2B). + // This routine is designed for 36-40KHz; if you use it for other values, it's up to you + // to make sure it gives reasonable results. (Watch out for overflow / underflow / rounding.) + // TIMER2 is used in phase-correct PWM mode, with OCR2A controlling the frequency and OCR2B + // controlling the duty cycle. + // There is no prescaling, so the output frequency is 16MHz / (2 * OCR2A) + // To turn the output on and off, we leave the PWM running, but connect and disconnect the output pin. + // A few hours staring at the ATmega documentation and this will all make sense. + // See my Secrets of Arduino PWM at http://arcfn.com/2009/07/secrets-of-arduino-pwm.html for details. + + + // Disable the Timer_A Interrupt (which is used for receiving IR) + TIMER_DISABLE_INTR; //Timer_A Overflow Interrupt + + TIMER_PIN_SELECT(); // P2.3 output and P2.3 option select (when TIMER_PWM_PIN is P2_3) + + pinMode(TIMER_PWM_PIN, OUTPUT); + digitalWrite(TIMER_PWM_PIN, LOW); // When not sending PWM, we want it low + + // TODO: update the comments below + + // COM2B = 00: disconnect OC2B; to send signal set to 10: OC2B non-inverted + // WGM2 = 101: phase-correct PWM with OCRA as top + // CS2 = 000: no prescaling + // The top value for the timer. The modulation frequency will be SYSCLOCK / 2 / OCR2A. + TIMER_CONFIG_KHZ(khz); +} + +IRrecv::IRrecv(int recvpin) +{ + irparams.recvpin = recvpin; + irparams.blinkflag = 0; +} + +// initialization +void IRrecv::enableIRIn() { + _DINT(); // disable interrupts + // setup pulse clock timer interrupt + + //Prescale /8 (16M/8 = 0.5 microseconds per tick) + // Therefore, the timer interval can range from 0.5 to 128 microseconds + // depending on the reset value (255 to 0) + TIMER_CONFIG_NORMAL(); + + //Timer_A Overflow Interrupt Enable + TIMER_ENABLE_INTR; + + TIMER_RESET; + + _EINT(); // enable interrupts + + // initialize state machine variables + irparams.rcvstate = STATE_IDLE; + irparams.rawlen = 0; + + // set pin modes + pinMode(irparams.recvpin, INPUT); +} + +// enable/disable blinking of pin 13 on IR processing +void IRrecv::blink13(int blinkflag) +{ + irparams.blinkflag = blinkflag; + if (blinkflag) + pinMode(BLINKLED, OUTPUT); +} + +// TIMERA interrupt code to collect raw data. +// Widths of alternating SPACE, MARK are recorded in rawbuf. +// Recorded in ticks of 50 microseconds. +// rawlen counts the number of entries recorded so far. +// First entry is the SPACE between transmissions. +// As soon as a SPACE gets long, ready is set, state switches to IDLE, timing of SPACE continues. +// As soon as first MARK arrives, gap width is recorded, ready is cleared, and new logging starts +__attribute__((interrupt(TIMER_INTR_NAME))) +void IRrecv::RecvIsr(void) +{ + TIMER_RESET; + //P2DIR |= BIT0 | BIT1 | BIT2; + // P2OUT ^= BIT2; + + uint8_t irdata = (uint8_t)digitalRead(irparams.recvpin); + + irparams.timer++; // One more 50us tick + if (irparams.rawlen >= RAWBUF) { + // Buffer overflow + irparams.rcvstate = STATE_STOP; + } + switch(irparams.rcvstate) { + case STATE_IDLE: // In the middle of a gap + //P2OUT |= BIT0; + //P2OUT &= ~BIT1; + //P2OUT &= ~BIT2; + + if (irdata == MARK) { + if (irparams.timer < GAP_TICKS) { + // Not big enough to be a gap. + irparams.timer = 0; + } + else { + // gap just ended, record duration and start recording transmission + irparams.rawlen = 0; + irparams.rawbuf[irparams.rawlen++] = irparams.timer; + irparams.timer = 0; + irparams.rcvstate = STATE_MARK; + } + } + break; + case STATE_MARK: // timing MARK + if (irdata == SPACE) { // MARK ended, record time + irparams.rawbuf[irparams.rawlen++] = irparams.timer; + irparams.timer = 0; + irparams.rcvstate = STATE_SPACE; + } + break; + case STATE_SPACE: // timing SPACE + if (irdata == MARK) { // SPACE just ended, record it + irparams.rawbuf[irparams.rawlen++] = irparams.timer; + irparams.timer = 0; + irparams.rcvstate = STATE_MARK; + } + else { // SPACE + if (irparams.timer > GAP_TICKS) { + // big SPACE, indicates gap between codes + // Mark current code as ready for processing + // Switch to STOP + // Don't reset timer; keep counting space width + irparams.rcvstate = STATE_STOP; + } + } + break; + case STATE_STOP: // waiting, measuring gap + if (irdata == MARK) { // reset gap timer + irparams.timer = 0; + } + break; + } + + if (irparams.blinkflag) { + if (irdata == MARK) { + BLINKLED_ON(); // turn pin 13 LED on + } + else { + BLINKLED_OFF(); // turn pin 13 LED off + } + } +} + +void IRrecv::resume() { + irparams.rcvstate = STATE_IDLE; + irparams.rawlen = 0; +} + + + +// Decodes the received IR message +// Returns 0 if no data ready, 1 if data ready. +// Results of decoding are stored in results +int IRrecv::decode(decode_results *results) { + results->rawbuf = irparams.rawbuf; + results->rawlen = irparams.rawlen; + + if (irparams.rcvstate != STATE_STOP) { + return ERR; + } + /* + #ifdef DEBUG + if (results->rawlen != 0){ + Serial.print("results->rawlen: "); + Serial.println(irparams.rawlen); + } + #endif + */ +#ifdef DEBUG + Serial.println("Attempting NEC decode"); +#endif + if (decodeNEC(results)) { + return DECODED; + } +#ifdef DEBUG + Serial.println("Attempting Sony decode"); +#endif + if (decodeSony(results)) { + return DECODED; + } +#ifdef DEBUG + Serial.println("Attempting RC5 decode"); +#endif + if (decodeRC5(results)) { + return DECODED; + } +#ifdef DEBUG + Serial.println("Attempting RC6 decode"); +#endif + if (decodeRC6(results)) { + return DECODED; + } + // decodeHash returns a hash on any input. + // Thus, it needs to be last in the list. + // If you add any decodes, add them before this. + if (decodeHash(results)) { + return DECODED; + } + // Throw away and start over + resume(); + debug_print("ERR in IRrecv::decode"); + return ERR; +} + +long IRrecv::decodeNEC(decode_results *results) { + long data = 0; + int offset = 1; // Skip first space + // Initial mark + if (!MATCH_MARK(results->rawbuf[offset], NEC_HDR_MARK)) { + return ERR; + } + offset++; + // Check for repeat + if (irparams.rawlen == 4 && + MATCH_SPACE(results->rawbuf[offset], NEC_RPT_SPACE) && + MATCH_MARK(results->rawbuf[offset+1], NEC_BIT_MARK)) { + results->bits = 0; + results->value = REPEAT; + results->decode_type = NEC; + return DECODED; + } + if (irparams.rawlen < 2 * NEC_BITS + 4) { + return ERR; + } + // Initial space + if (!MATCH_SPACE(results->rawbuf[offset], NEC_HDR_SPACE)) { + return ERR; + } + offset++; + for (int i = 0; i < NEC_BITS; i++) { + if (!MATCH_MARK(results->rawbuf[offset], NEC_BIT_MARK)) { + return ERR; + } + offset++; + if (MATCH_SPACE(results->rawbuf[offset], NEC_ONE_SPACE)) { + data = (data << 1) | 1; + } + else if (MATCH_SPACE(results->rawbuf[offset], NEC_ZERO_SPACE)) { + data <<= 1; + } + else { + return ERR; + } + offset++; + } + // Success + results->bits = NEC_BITS; + results->value = data; + results->decode_type = NEC; + return DECODED; +} + +long IRrecv::decodeSony(decode_results *results) { + long data = 0; + if (irparams.rawlen < 2 * SONY_BITS + 2) { + return ERR; + } + int offset = 1; // Skip first space + // Initial mark + if (!MATCH_MARK(results->rawbuf[offset], SONY_HDR_MARK)) { + return ERR; + } + offset++; + + while (offset + 1 < irparams.rawlen) { + if (!MATCH_SPACE(results->rawbuf[offset], SONY_HDR_SPACE)) { + break; + } + offset++; + if (MATCH_MARK(results->rawbuf[offset], SONY_ONE_MARK)) { + data = (data << 1) | 1; + } + else if (MATCH_MARK(results->rawbuf[offset], SONY_ZERO_MARK)) { + data <<= 1; + } + else { + return ERR; + } + offset++; + } + + // Success + results->bits = (offset - 1) / 2; + if (results->bits < 12) { + results->bits = 0; + return ERR; + } + results->value = data; + results->decode_type = SONY; + return DECODED; +} + +// Gets one undecoded level at a time from the raw buffer. +// The RC5/6 decoding is easier if the data is broken into time intervals. +// E.g. if the buffer has MARK for 2 time intervals and SPACE for 1, +// successive calls to getRClevel will return MARK, MARK, SPACE. +// offset and used are updated to keep track of the current position. +// t1 is the time interval for a single bit in microseconds. +// Returns -1 for error (measured time interval is not a multiple of t1). +int IRrecv::getRClevel(decode_results *results, int *offset, int *used, int t1) { + if (*offset >= results->rawlen) { + // After end of recorded buffer, assume SPACE. + return SPACE; + } + int width = results->rawbuf[*offset]; + int val = ((*offset) % 2) ? MARK : SPACE; + int correction = (val == MARK) ? MARK_EXCESS : - MARK_EXCESS; + + int avail; + if (MATCH(width, t1 + correction)) { + avail = 1; + } + else if (MATCH(width, 2*t1 + correction)) { + avail = 2; + } + else if (MATCH(width, 3*t1 + correction)) { + avail = 3; + } + else { + return -1; + } + + (*used)++; + if (*used >= avail) { + *used = 0; + (*offset)++; + } +#ifdef DEBUG + if (val == MARK) { + Serial.println("MARK"); + } + else { + Serial.println("SPACE"); + } +#endif + return val; +} + +long IRrecv::decodeRC5(decode_results *results) { + if (irparams.rawlen < MIN_RC5_SAMPLES + 2) { + return ERR; + } + int offset = 1; // Skip gap space + long data = 0; + int used = 0; + // Get start bits + if (getRClevel(results, &offset, &used, RC5_T1) != MARK) return ERR; + if (getRClevel(results, &offset, &used, RC5_T1) != SPACE) return ERR; + if (getRClevel(results, &offset, &used, RC5_T1) != MARK) return ERR; + int nbits; + for (nbits = 0; offset < irparams.rawlen; nbits++) { + int levelA = getRClevel(results, &offset, &used, RC5_T1); + int levelB = getRClevel(results, &offset, &used, RC5_T1); + if (levelA == SPACE && levelB == MARK) { + // 1 bit + data = (data << 1) | 1; + } + else if (levelA == MARK && levelB == SPACE) { + // zero bit + data <<= 1; + } + else { + return ERR; + } + } + + // Success + results->bits = nbits; + results->value = data; + results->decode_type = RC5; + return DECODED; +} + +long IRrecv::decodeRC6(decode_results *results) { + if (results->rawlen < MIN_RC6_SAMPLES) { + return ERR; + } + int offset = 1; // Skip first space + // Initial mark + if (!MATCH_MARK(results->rawbuf[offset], RC6_HDR_MARK)) { + return ERR; + } + offset++; + if (!MATCH_SPACE(results->rawbuf[offset], RC6_HDR_SPACE)) { + return ERR; + } + offset++; + long data = 0; + int used = 0; + // Get start bit (1) + if (getRClevel(results, &offset, &used, RC6_T1) != MARK) return ERR; + if (getRClevel(results, &offset, &used, RC6_T1) != SPACE) return ERR; + int nbits; + for (nbits = 0; offset < results->rawlen; nbits++) { + int levelA, levelB; // Next two levels + levelA = getRClevel(results, &offset, &used, RC6_T1); + if (nbits == 3) { + // T bit is double wide; make sure second half matches + if (levelA != getRClevel(results, &offset, &used, RC6_T1)) return ERR; + } + levelB = getRClevel(results, &offset, &used, RC6_T1); + if (nbits == 3) { + // T bit is double wide; make sure second half matches + if (levelB != getRClevel(results, &offset, &used, RC6_T1)) return ERR; + } + if (levelA == MARK && levelB == SPACE) { // reversed compared to RC5 + // 1 bit + data = (data << 1) | 1; + } + else if (levelA == SPACE && levelB == MARK) { + // zero bit + data <<= 1; + } + else { + return ERR; // Error + } + } + // Success + results->bits = nbits; + results->value = data; + results->decode_type = RC6; + return DECODED; +} + +/* ----------------------------------------------------------------------- + * hashdecode - decode an arbitrary IR code. + * Instead of decoding using a standard encoding scheme + * (e.g. Sony, NEC, RC5), the code is hashed to a 32-bit value. + * + * The algorithm: look at the sequence of MARK signals, and see if each one + * is shorter (0), the same length (1), or longer (2) than the previous. + * Do the same with the SPACE signals. Hszh the resulting sequence of 0's, + * 1's, and 2's to a 32-bit value. This will give a unique value for each + * different code (probably), for most code systems. + * + * http://arcfn.com/2010/01/using-arbitrary-remotes-with-arduino.html + */ + +// Compare two tick values, returning 0 if newval is shorter, +// 1 if newval is equal, and 2 if newval is longer +// Use a tolerance of 20% +int IRrecv::compare(unsigned int oldval, unsigned int newval) { + if (newval < oldval * .8) { + return 0; + } + else if (oldval < newval * .8) { + return 2; + } + else { + return 1; + } +} + +// Use FNV hash algorithm: http://isthe.com/chongo/tech/comp/fnv/#FNV-param +#define FNV_PRIME_32 16777619 +#define FNV_BASIS_32 2166136261U + +/* Converts the raw code values into a 32-bit hash code. + * Hopefully this code is unique for each button. + * This isn't a "real" decoding, just an arbitrary value. + */ +long IRrecv::decodeHash(decode_results *results) { + // Require at least 6 samples to prevent triggering on noise + if (results->rawlen < 6) { + return ERR; + } + long hash = FNV_BASIS_32; + for (int i = 1; i+2 < results->rawlen; i++) { + int value = compare(results->rawbuf[i], results->rawbuf[i+2]); + // Add value into the hash + hash = (hash * FNV_PRIME_32) ^ value; + } + results->value = hash; + results->bits = 32; + results->decode_type = UNKNOWN; + return DECODED; +} + +/* Sharp and DISH support by Todd Treece ( http://unionbridge.org/design/ircommand ) + +The Dish send function needs to be repeated 4 times, and the Sharp function +has the necessary repeat built in because of the need to invert the signal. + +Sharp protocol documentation: +http://www.sbprojects.com/knowledge/ir/sharp.htm + +Here are the LIRC files that I found that seem to match the remote codes +from the oscilloscope: + +Sharp LCD TV: +http://lirc.sourceforge.net/remotes/sharp/GA538WJSA + +DISH NETWORK (echostar 301): +http://lirc.sourceforge.net/remotes/echostar/301_501_3100_5100_58xx_59xx + +For the DISH codes, only send the last for characters of the hex. +i.e. use 0x1C10 instead of 0x0000000000001C10 which is listed in the +linked LIRC file. +*/ + +void IRsend::sendSharp(unsigned long data, int nbits) { + unsigned long invertdata = data ^ SHARP_TOGGLE_MASK; + enableIROut(38); + for (int i = 0; i < nbits; i++) { + if (data & 0x4000) { + mark(SHARP_BIT_MARK); + space(SHARP_ONE_SPACE); + } + else { + mark(SHARP_BIT_MARK); + space(SHARP_ZERO_SPACE); + } + data <<= 1; + } + + mark(SHARP_BIT_MARK); + space(SHARP_ZERO_SPACE); + delay(46); + for (int i = 0; i < nbits; i++) { + if (invertdata & 0x4000) { + mark(SHARP_BIT_MARK); + space(SHARP_ONE_SPACE); + } + else { + mark(SHARP_BIT_MARK); + space(SHARP_ZERO_SPACE); + } + invertdata <<= 1; + } + mark(SHARP_BIT_MARK); + space(SHARP_ZERO_SPACE); + delay(46); +} + +void IRsend::sendDISH(unsigned long data, int nbits) +{ + enableIROut(56); + mark(DISH_HDR_MARK); + space(DISH_HDR_SPACE); + for (int i = 0; i < nbits; i++) { + if (data & DISH_TOP_BIT) { + mark(DISH_BIT_MARK); + space(DISH_ONE_SPACE); + } + else { + mark(DISH_BIT_MARK); + space(DISH_ZERO_SPACE); + } + data <<= 1; + } +} + diff --git a/msp430/libraries/IRremote/IRremote.h b/msp430/libraries/IRremote/IRremote.h new file mode 100644 index 0000000..0a9f04f --- /dev/null +++ b/msp430/libraries/IRremote/IRremote.h @@ -0,0 +1,101 @@ +/* + * IRremote + * Version 0.1 July, 2009 + * Copyright 2009 Ken Shirriff + * For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.htm http://arcfn.com + * + * Interrupt code based on NECIRrcv by Joe Knapp + * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556 + * Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/ + */ + +#ifndef IRremote_h +#define IRremote_h + +// The following are compile-time library options. +// If you change them, recompile the library. +// If DEBUG is defined, a lot of debugging output will be printed during decoding. +// TEST must be defined for the IRtest unittests to work. It will make some +// methods virtual, which will be slightly slower, which is why it is optional. +// #define DEBUG +// #define TEST + +// Results returned from the decoder +class decode_results { +public: + int decode_type; // NEC, SONY, RC5, UNKNOWN + unsigned long value; // Decoded value + int bits; // Number of bits in decoded value + volatile unsigned int *rawbuf; // Raw intervals in .5 us ticks + int rawlen; // Number of records in rawbuf. +}; + +// Values for decode_type +#define NEC 1 +#define SONY 2 +#define RC5 3 +#define RC6 4 +#define DISH 5 +#define SHARP 6 +#define UNKNOWN -1 + +// Decoded value for NEC when a repeat code is received +#define REPEAT 0xffffffff + +// main class for receiving IR +class IRrecv +{ +public: + IRrecv(int recvpin); + void blink13(int blinkflag); + int decode(decode_results *results); + void enableIRIn(); + void resume(); +private: + // These are called by decode + int getRClevel(decode_results *results, int *offset, int *used, int t1); + long decodeNEC(decode_results *results); + long decodeSony(decode_results *results); + long decodeRC5(decode_results *results); + long decodeRC6(decode_results *results); + long decodeHash(decode_results *results); + int compare(unsigned int oldval, unsigned int newval); + static void RecvIsr(); +} +; + +// Only used for testing; can remove virtual for shorter code +#ifdef TEST +#define VIRTUAL virtual +#else +#define VIRTUAL +#endif + +class IRsend +{ +public: + IRsend() {} + void sendNEC(unsigned long data, int nbits); + void sendSony(unsigned long data, int nbits); + void sendRaw(unsigned int buf[], int len, int hz); + void sendRC5(unsigned long data, int nbits); + void sendRC6(unsigned long data, int nbits); + void sendDISH(unsigned long data, int nbits); + void sendSharp(unsigned long data, int nbits); + // private: + void enableIROut(int khz); + VIRTUAL void mark(int usec); + VIRTUAL void space(int usec); +} +; + +// Some useful constants + +#define USECPERTICK 50 +#define RAWBUF 76 // Length of raw duration buffer + +// Marks tend to be 100us too long, and spaces 100us too short +// when received due to sensor lag. +#define MARK_EXCESS 100 + +#endif diff --git a/msp430/libraries/IRremote/IRremoteInt.h b/msp430/libraries/IRremote/IRremoteInt.h new file mode 100644 index 0000000..15a6d85 --- /dev/null +++ b/msp430/libraries/IRremote/IRremoteInt.h @@ -0,0 +1,185 @@ +/* + * IRremote + * Version 0.1 July, 2009 + * Copyright 2009 Ken Shirriff + * For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.html + * + * Modified by Paul Stoffregen <paul@pjrc.com> to support other boards and timers + * + * Interrupt code based on NECIRrcv by Joe Knapp + * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556 + * Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/ + */ + +#ifndef IRremoteint_h +#define IRremoteint_h + +#include <Energia.h> + +// NOTE: launchpad with a M430G2553 + +#define IR_USE_TIMERA + +// Clock freq in kilohertz +// Defined because using F_CPU in TIMER_CONFIG_KHZ does not work. +// TIMER_CONFIG_KHZ does a calculation and using too large of a +// number causes overflow. +#define FOSC 16000 + +#ifdef F_CPU +#define SYSCLOCK F_CPU // main MSP430 clock +#else +#define SYSCLOCK 16000000 +#endif + +#define ERR 0 +#define DECODED 1 + +//#define DEBUG + +// TODO: edit these to match MSP430 + +// defines for setting and clearing register bits +#ifndef cbi +#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) +#endif +#ifndef sbi +#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) +#endif + +// pulse parameters in usec +#define NEC_HDR_MARK 9000 +#define NEC_HDR_SPACE 4500 +#define NEC_BIT_MARK 560 +#define NEC_ONE_SPACE 1690 +#define NEC_ZERO_SPACE 560 +#define NEC_RPT_SPACE 2250 + +#define SONY_HDR_MARK 2400 +#define SONY_HDR_SPACE 600 +#define SONY_ONE_MARK 1200 +#define SONY_ZERO_MARK 600 +#define SONY_RPT_LENGTH 45000 + +#define RC5_T1 889 +#define RC5_RPT_LENGTH 46000 + +#define RC6_HDR_MARK 2666 +#define RC6_HDR_SPACE 889 +#define RC6_T1 444 +#define RC6_RPT_LENGTH 46000 + +#define SHARP_BIT_MARK 245 +#define SHARP_ONE_SPACE 1805 +#define SHARP_ZERO_SPACE 795 +#define SHARP_GAP 600000 +#define SHARP_TOGGLE_MASK 0x3FF +#define SHARP_RPT_SPACE 3000 + +#define DISH_HDR_MARK 400 +#define DISH_HDR_SPACE 6100 +#define DISH_BIT_MARK 400 +#define DISH_ONE_SPACE 1700 +#define DISH_ZERO_SPACE 2800 +#define DISH_RPT_SPACE 6200 +#define DISH_TOP_BIT 0x8000 + +#define SHARP_BITS 15 +#define DISH_BITS 16 + +#define TOLERANCE 25 // percent tolerance in measurements +#define LTOL (1.0 - TOLERANCE/100.) +#define UTOL (1.0 + TOLERANCE/100.) + +#define _GAP 5000 // Minimum map between transmissions +#define GAP_TICKS (_GAP/USECPERTICK) + +#define TICKS_LOW(us) (int) (((us)*LTOL/USECPERTICK)) +#define TICKS_HIGH(us) (int) (((us)*UTOL/USECPERTICK + 1)) + +#ifndef DEBUG +// SH 071112 this macros have a tolerance problem. +//#define MATCH(measured_ticks, desired_us) ((measured_ticks) >= TICKS_LOW(desired_us) && (measured_ticks) <= TICKS_HIGH(desired_us)) +//#define MATCH_MARK(measured_ticks, desired_us) MATCH(measured_ticks, (desired_us) + MARK_EXCESS) +//#define MATCH_SPACE(measured_ticks, desired_us) MATCH((measured_ticks), (desired_us) - MARK_EXCESS) +// Debugging versions are in IRremote.cpp +#endif + +// receiver states +#define STATE_IDLE 2 +#define STATE_MARK 3 +#define STATE_SPACE 4 +#define STATE_STOP 5 + +// information for the interrupt handler +typedef struct { + uint8_t recvpin; // pin for IR data from detector + uint8_t rcvstate; // state machine + uint8_t blinkflag; // TRUE to enable blinking of GREEN_LED on IR processing + unsigned int timer; // state timer, counts 50uS ticks. + unsigned int rawbuf[RAWBUF]; // raw data + uint8_t rawlen; // counter of entries in rawbuf +} +irparams_t; + +// Defined in IRremote.cpp +extern volatile irparams_t irparams; + +// IR detector output is active low +#define MARK 0 +#define SPACE 1 + +#define TOPBIT 0x80000000 + +#define NEC_BITS 32 +#define SONY_BITS 12 +#define MIN_RC5_SAMPLES 11 +#define MIN_RC6_SAMPLES 1 + +#define TIMER_PWM_PIN P2_3 +// NOTE: TIMER_PIN_SELECT depends on TIMER_PWM_PIN +// P2DIR |= BIT3; // P2.3 output +// P2SEL |= BIT3; // P2.3 option select +#define TIMER_PIN_SELECT() ({ \ + P2DIR |= BIT3; \ + P2SEL |= BIT3; \ +}) + +// defines for Timer_A (16 bits) +// NOTE: Using A1 instead of A0 because A0 is used by Serial +#if defined(IR_USE_TIMERA) +#define TIMER_RESET +#define TIMER_ENABLE_PWM (TA1CTL = TASSEL_2 + MC_1) // SMCLK, Up mode +#define TIMER_DISABLE_PWM (TA1CTL = TASSEL_2 + MC_0) // SMCLK, Stop mode +#define TIMER_ENABLE_INTR ({ /*TA1CTL |= TAIE;*/ TA1CCTL0 |= CCIE;}) // SH 071112 +#define TIMER_DISABLE_INTR ({TA1CTL &= ~TAIE; TA1CCTL0 &= ~CCIE;}) +#define TIMER_INTR_NAME TIMER1_A0_VECTOR +#define TIMER_CONFIG_KHZ(val) ({ \ + TA1CCTL0 |= OUTMOD_4; \ + TA1CCR0 = FOSC/val/2; \ +}) + +// TODO: update TIMER_CONFIG_NORMAL +// need to alter the math? + +// TA1CCR0 = SYSCLOCK * USECPERTICK / 1000000; + +#define TIMER_CONFIG_NORMAL() ({ \ + TA1CTL = TASSEL_2 + MC_1; \ + TA1CCTL0 |= OUTMOD_4; \ + TA1CCR0 = SYSCLOCK * USECPERTICK / 1000000; \ + TA1R = 0; \ +}) +#endif + +#else // unknown timer +#error "Internal code configuration error, no known IR_USE_TIMER# defined\n" +#endif + +// defines for blinking the (Green) LED +// MSP430 launchpad also has RED_LED +#define GREEN_LED P1_0 +#define BLINKLED GREEN_LED +#define BLINKLED_ON() (digitalWrite(BLINKLED, HIGH)) +#define BLINKLED_OFF() (digitalWrite(BLINKLED, LOW)) + diff --git a/msp430/libraries/IRremote/LICENSE.txt b/msp430/libraries/IRremote/LICENSE.txt new file mode 100644 index 0000000..77cec6d --- /dev/null +++ b/msp430/libraries/IRremote/LICENSE.txt @@ -0,0 +1,458 @@ + + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + diff --git a/msp430/libraries/IRremote/examples/IRrecord/IRrecord.pde b/msp430/libraries/IRremote/examples/IRrecord/IRrecord.pde new file mode 100644 index 0000000..913d431 --- /dev/null +++ b/msp430/libraries/IRremote/examples/IRrecord/IRrecord.pde @@ -0,0 +1,170 @@ +/* + * IRrecord: record and play back IR signals as a minimal + * An IR detector/demodulator must be connected to the input RECV_PIN. + * An IR LED must be connected to the output PWM pin 3. + * A button must be connected to the input BUTTON_PIN; this is the + * send button. + * A visible LED can be connected to STATUS_PIN to provide status. + * + * The logic is: + * If the button is pressed, send the IR code. + * If an IR code is received, record it. + * + * Version 0.11 September, 2009 + * Copyright 2009 Ken Shirriff + * http://arcfn.com + */ + +#include <IRremote.h> + +int RECV_PIN = 11; +int BUTTON_PIN = 5; +int STATUS_PIN = 2; + +IRrecv irrecv(RECV_PIN); +IRsend irsend; + +decode_results results; + +void setup() +{ + Serial.begin(9600); + irrecv.enableIRIn(); // Start the receiver + pinMode(BUTTON_PIN, INPUT); + pinMode(STATUS_PIN, OUTPUT); +} + +// Storage for the recorded code +int codeType = -1; // The type of code +unsigned long codeValue; // The code value if not raw +unsigned int rawCodes[RAWBUF]; // The durations if raw +int codeLen; // The length of the code +int toggle = 0; // The RC5/6 toggle state + +// Stores the code for later playback +// Most of this code is just logging +void storeCode(void* v){ + decode_results *results = (decode_results *)v; +//void storeCode(decode_results *results) { + + codeType = results->decode_type; + int count = results->rawlen; + if (codeType == UNKNOWN) { + Serial.println("Received unknown code, saving as raw"); + codeLen = results->rawlen - 1; + // To store raw codes: + // Drop first value (gap) + // Convert from ticks to microseconds + // Tweak marks shorter, and spaces longer to cancel out IR receiver distortion + for (int i = 1; i <= codeLen; i++) { + if (i % 2) { + // Mark + rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK - MARK_EXCESS; + Serial.print(" m"); + } + else { + // Space + rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK + MARK_EXCESS; + Serial.print(" s"); + } + Serial.print(rawCodes[i - 1], DEC); + } + Serial.println(""); + } + else { + if (codeType == NEC) { + Serial.print("Received NEC: "); + if (results->value == REPEAT) { + // Don't record a NEC repeat value as that's useless. + Serial.println("repeat; ignoring."); + return; + } + } + else if (codeType == SONY) { + Serial.print("Received SONY: "); + } + else if (codeType == RC5) { + Serial.print("Received RC5: "); + } + else if (codeType == RC6) { + Serial.print("Received RC6: "); + } + else { + Serial.print("Unexpected codeType "); + Serial.print(codeType, DEC); + Serial.println(""); + } + Serial.println(results->value, HEX); + codeValue = results->value; + codeLen = results->bits; + } +} + +void sendCode(int repeat) { + if (codeType == NEC) { + if (repeat) { + irsend.sendNEC(REPEAT, codeLen); + Serial.println("Sent NEC repeat"); + } + else { + irsend.sendNEC(codeValue, codeLen); + Serial.print("Sent NEC "); + Serial.println(codeValue, HEX); + } + } + else if (codeType == SONY) { + irsend.sendSony(codeValue, codeLen); + Serial.print("Sent Sony "); + Serial.println(codeValue, HEX); + } + else if (codeType == RC5 || codeType == RC6) { + if (!repeat) { + // Flip the toggle bit for a new button press + toggle = 1 - toggle; + } + // Put the toggle bit into the code to send + codeValue = codeValue & ~(1 << (codeLen - 1)); + codeValue = codeValue | (toggle << (codeLen - 1)); + if (codeType == RC5) { + Serial.print("Sent RC5 "); + Serial.println(codeValue, HEX); + irsend.sendRC5(codeValue, codeLen); + } + else { + irsend.sendRC6(codeValue, codeLen); + Serial.print("Sent RC6 "); + Serial.println(codeValue, HEX); + } + } + else if (codeType == UNKNOWN /* i.e. raw */) { + // Assume 38 KHz + irsend.sendRaw(rawCodes, codeLen, 38); + Serial.println("Sent raw"); + } +} + +int lastButtonState; + +void loop() { + // If button pressed, send the code. + int buttonState = digitalRead(BUTTON_PIN); + if (lastButtonState == LOW && buttonState == HIGH) { + Serial.println("Released"); + irrecv.enableIRIn(); // Re-enable receiver + } + + if (buttonState==LOW) { + Serial.println("Pressed, sending"); + digitalWrite(STATUS_PIN, HIGH); + sendCode(lastButtonState == buttonState); + digitalWrite(STATUS_PIN, LOW); + delay(50); // Wait a bit between retransmissions + } + else if (irrecv.decode(&results)) { + digitalWrite(STATUS_PIN, HIGH); + storeCode(&results); + irrecv.resume(); // resume receiver + digitalWrite(STATUS_PIN, LOW); + } + lastButtonState = buttonState; +}
\ No newline at end of file diff --git a/msp430/libraries/IRremote/examples/IRrecvDemo/IRrecvDemo.pde b/msp430/libraries/IRremote/examples/IRrecvDemo/IRrecvDemo.pde new file mode 100644 index 0000000..14982d8 --- /dev/null +++ b/msp430/libraries/IRremote/examples/IRrecvDemo/IRrecvDemo.pde @@ -0,0 +1,28 @@ +/* + * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv + * An IR detector/demodulator must be connected to the input RECV_PIN. + * Version 0.1 July, 2009 + * Copyright 2009 Ken Shirriff + * http://arcfn.com + */ + +#include <IRremote.h> + +int RECV_PIN = 11; + +IRrecv irrecv(RECV_PIN); + +decode_results results; + +void setup() +{ + Serial.begin(9600); + irrecv.enableIRIn(); // Start the receiver +} + +void loop() { + if (irrecv.decode(&results)) { + Serial.println(results.value, HEX); + irrecv.resume(); // Receive the next value + } +}
diff --git a/msp430/libraries/IRremote/examples/IRrecvDump/IRrecvDump.pde b/msp430/libraries/IRremote/examples/IRrecvDump/IRrecvDump.pde new file mode 100644 index 0000000..61f200f --- /dev/null +++ b/msp430/libraries/IRremote/examples/IRrecvDump/IRrecvDump.pde @@ -0,0 +1,74 @@ +/* + * IRremote: IRrecvDump - dump details of IR codes with IRrecv + * An IR detector/demodulator must be connected to the input RECV_PIN. + * Version 0.1 July, 2009 + * Copyright 2009 Ken Shirriff + * http://arcfn.com + */ + +#include <IRremote.h> + +int RECV_PIN = 11; + +IRrecv irrecv(RECV_PIN); + +decode_results results; + +void setup() +{ + Serial.begin(9600); + irrecv.enableIRIn(); // Start the receiver +} + +// Dumps out the decode_results structure. +// Call this after IRrecv::decode() +// void * to work around compiler issue +void dump(void *v) { + decode_results *results = (decode_results *)v; +//void dump(decode_results *results) { + int count = results->rawlen; + if (results->decode_type == UNKNOWN) { + Serial.println("Could not decode message"); + } + else { + if (results->decode_type == NEC) { + Serial.print("Decoded NEC: "); + } + else if (results->decode_type == SONY) { + Serial.print("Decoded SONY: "); + } + else if (results->decode_type == RC5) { + Serial.print("Decoded RC5: "); + } + else if (results->decode_type == RC6) { + Serial.print("Decoded RC6: "); + } + Serial.print(results->value, HEX); + Serial.print(" ("); + Serial.print(results->bits, DEC); + Serial.println(" bits)"); + } + Serial.print("Raw ("); + Serial.print(count, DEC); + Serial.print("): "); + + for (int i = 0; i < count; i++) { + if ((i % 2) == 1) { + Serial.print(results->rawbuf[i]*USECPERTICK, DEC); + } + else { + Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC); + } + Serial.print(" "); + } + Serial.println(""); +} + + +void loop() { + if (irrecv.decode(&results)) { + Serial.println(results.value, HEX); + dump(&results); + irrecv.resume(); // Receive the next value + } +}
diff --git a/msp430/libraries/IRremote/examples/IRrelay/IRrelay.pde b/msp430/libraries/IRremote/examples/IRrelay/IRrelay.pde new file mode 100644 index 0000000..170a184 --- /dev/null +++ b/msp430/libraries/IRremote/examples/IRrelay/IRrelay.pde @@ -0,0 +1,85 @@ +/* + * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv + * An IR detector/demodulator must be connected to the input RECV_PIN. + * Version 0.1 July, 2009 + * Copyright 2009 Ken Shirriff + * http://arcfn.com + */ + +#include <IRremote.h> + +int RECV_PIN = 11; +int RELAY_PIN = 4; + +IRrecv irrecv(RECV_PIN); +decode_results results; + +// Dumps out the decode_results structure. +// Call this after IRrecv::decode() +// void * to work around compiler issue +void dump(void *v) { + decode_results *results = (decode_results *)v; +//void dump(decode_results *results) { + int count = results->rawlen; + if (results->decode_type == UNKNOWN) { + Serial.println("Could not decode message"); + } + else { + if (results->decode_type == NEC) { + Serial.print("Decoded NEC: "); + } + else if (results->decode_type == SONY) { + Serial.print("Decoded SONY: "); + } + else if (results->decode_type == RC5) { + Serial.print("Decoded RC5: "); + } + else if (results->decode_type == RC6) { + Serial.print("Decoded RC6: "); + } + Serial.print(results->value, HEX); + Serial.print(" ("); + Serial.print(results->bits, DEC); + Serial.println(" bits)"); + } + Serial.print("Raw ("); + Serial.print(count, DEC); + Serial.print("): "); + + for (int i = 0; i < count; i++) { + if ((i % 2) == 1) { + Serial.print(results->rawbuf[i]*USECPERTICK, DEC); + } + else { + Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC); + } + Serial.print(" "); + } + Serial.println(""); +} + +void setup() +{ + pinMode(RELAY_PIN, OUTPUT); + pinMode(13, OUTPUT); + Serial.begin(9600); + irrecv.enableIRIn(); // Start the receiver +} + +int on = 0; +unsigned long last = millis(); + +void loop() { + if (irrecv.decode(&results)) { + // If it's been at least 1/4 second since the last + // IR received, toggle the relay + if (millis() - last > 250) { + on = !on; + digitalWrite(RELAY_PIN, on ? HIGH : LOW); + digitalWrite(13, on ? HIGH : LOW); + dump(&results); + } + last = millis(); + irrecv.resume(); // Receive the next value + } +}
diff --git a/msp430/libraries/IRremote/examples/IRsendDemo/IRsendDemo.pde b/msp430/libraries/IRremote/examples/IRsendDemo/IRsendDemo.pde new file mode 100644 index 0000000..d864c82 --- /dev/null +++ b/msp430/libraries/IRremote/examples/IRsendDemo/IRsendDemo.pde @@ -0,0 +1,26 @@ +/* + * IRremote: IRsendDemo - demonstrates sending IR codes with IRsend + * An IR LED must be connected to Arduino PWM pin 3. + * Version 0.1 July, 2009 + * Copyright 2009 Ken Shirriff + * http://arcfn.com + */ + +#include <IRremote.h> + +IRsend irsend; + +void setup() +{ + Serial.begin(9600); +} + +void loop() { + if (Serial.read() != -1) { + for (int i = 0; i < 3; i++) { + irsend.sendSony(0xa90, 12); // Sony TV power code + delay(100); + } + } +} +
diff --git a/msp430/libraries/IRremote/examples/IRtest/IRtest.pde b/msp430/libraries/IRremote/examples/IRtest/IRtest.pde new file mode 100644 index 0000000..62743af --- /dev/null +++ b/msp430/libraries/IRremote/examples/IRtest/IRtest.pde @@ -0,0 +1,190 @@ +/*
+ * IRremote: IRtest unittest
+ * Version 0.1 July, 2009
+ * Copyright 2009 Ken Shirriff
+ * http://arcfn.com
+ *
+ * Note: to run these tests, edit IRremote/IRremote.h to add "#define TEST"
+ * You must then recompile the library by removing IRremote.o and restarting
+ * the arduino IDE.
+ */
+
+#include <IRremote.h>
+#include <IRremoteInt.h>
+
+// Dumps out the decode_results structure.
+// Call this after IRrecv::decode()
+// void * to work around compiler issue
+void dump(void *v) {
+ decode_results *results = (decode_results *)v;
+//void dump(decode_results *results) {
+ int count = results->rawlen;
+ if (results->decode_type == UNKNOWN) {
+ Serial.println("Could not decode message");
+ }
+ else {
+ if (results->decode_type == NEC) {
+ Serial.print("Decoded NEC: ");
+ }
+ else if (results->decode_type == SONY) {
+ Serial.print("Decoded SONY: ");
+ }
+ else if (results->decode_type == RC5) {
+ Serial.print("Decoded RC5: ");
+ }
+ else if (results->decode_type == RC6) {
+ Serial.print("Decoded RC6: ");
+ }
+ Serial.print(results->value, HEX);
+ Serial.print(" (");
+ Serial.print(results->bits, DEC);
+ Serial.println(" bits)");
+ }
+ Serial.print("Raw (");
+ Serial.print(count, DEC);
+ Serial.print("): ");
+
+ for (int i = 0; i < count; i++) {
+ if ((i % 2) == 1) {
+ Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
+ }
+ else {
+ Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
+ }
+ Serial.print(" ");
+ }
+ Serial.println("");
+}
+
+IRrecv irrecv(0);
+decode_results results;
+
+class IRsendDummy :
+public IRsend
+{
+public:
+ // For testing, just log the marks/spaces
+#define SENDLOG_LEN 128
+ int sendlog[SENDLOG_LEN];
+ int sendlogcnt;
+ IRsendDummy() :
+ IRsend() {
+ }
+ void reset() {
+ sendlogcnt = 0;
+ }
+ void mark(int time) {
+ sendlog[sendlogcnt] = time;
+ if (sendlogcnt < SENDLOG_LEN) sendlogcnt++;
+ }
+ void space(int time) {
+ sendlog[sendlogcnt] = -time;
+ if (sendlogcnt < SENDLOG_LEN) sendlogcnt++;
+ }
+ // Copies the dummy buf into the interrupt buf
+ void useDummyBuf() {
+ int last = SPACE;
+ irparams.rcvstate = STATE_STOP;
+ irparams.rawlen = 1; // Skip the gap
+ for (int i = 0 ; i < sendlogcnt; i++) {
+ if (sendlog[i] < 0) {
+ if (last == MARK) {
+ // New space
+ irparams.rawbuf[irparams.rawlen++] = (-sendlog[i] - MARK_EXCESS) / USECPERTICK;
+ last = SPACE;
+ }
+ else {
+ // More space
+ irparams.rawbuf[irparams.rawlen - 1] += -sendlog[i] / USECPERTICK;
+ }
+ }
+ else if (sendlog[i] > 0) {
+ if (last == SPACE) {
+ // New mark
+ irparams.rawbuf[irparams.rawlen++] = (sendlog[i] + MARK_EXCESS) / USECPERTICK;
+ last = MARK;
+ }
+ else {
+ // More mark
+ irparams.rawbuf[irparams.rawlen - 1] += sendlog[i] / USECPERTICK;
+ }
+ }
+ }
+ if (irparams.rawlen % 2) {
+ irparams.rawlen--; // Remove trailing space
+ }
+ }
+};
+
+IRsendDummy irsenddummy;
+
+void verify(unsigned long val, int bits, int type) {
+ irsenddummy.useDummyBuf();
+ irrecv.decode(&results);
+ Serial.print("Testing ");
+ Serial.print(val, HEX);
+ if (results.value == val && results.bits == bits && results.decode_type == type) {
+ Serial.println(": OK");
+ }
+ else {
+ Serial.println(": Error");
+ dump(&results);
+ }
+}
+
+void testNEC(unsigned long val, int bits) {
+ irsenddummy.reset();
+ irsenddummy.sendNEC(val, bits);
+ verify(val, bits, NEC);
+}
+void testSony(unsigned long val, int bits) {
+ irsenddummy.reset();
+ irsenddummy.sendSony(val, bits);
+ verify(val, bits, SONY);
+}
+void testRC5(unsigned long val, int bits) {
+ irsenddummy.reset();
+ irsenddummy.sendRC5(val, bits);
+ verify(val, bits, RC5);
+}
+void testRC6(unsigned long val, int bits) {
+ irsenddummy.reset();
+ irsenddummy.sendRC6(val, bits);
+ verify(val, bits, RC6);
+}
+
+void test() {
+ Serial.println("NEC tests");
+ testNEC(0x00000000, 32);
+ testNEC(0xffffffff, 32);
+ testNEC(0xaaaaaaaa, 32);
+ testNEC(0x55555555, 32);
+ testNEC(0x12345678, 32);
+ Serial.println("Sony tests");
+ testSony(0xfff, 12);
+ testSony(0x000, 12);
+ testSony(0xaaa, 12);
+ testSony(0x555, 12);
+ testSony(0x123, 12);
+ Serial.println("RC5 tests");
+ testRC5(0xfff, 12);
+ testRC5(0x000, 12);
+ testRC5(0xaaa, 12);
+ testRC5(0x555, 12);
+ testRC5(0x123, 12);
+ Serial.println("RC6 tests");
+ testRC6(0xfffff, 20);
+ testRC6(0x00000, 20);
+ testRC6(0xaaaaa, 20);
+ testRC6(0x55555, 20);
+ testRC6(0x12345, 20);
+}
+
+void setup()
+{
+ Serial.begin(9600);
+ test();
+}
+
+void loop() {
+}
diff --git a/msp430/libraries/IRremote/keywords.txt b/msp430/libraries/IRremote/keywords.txt new file mode 100644 index 0000000..7bf0f25 --- /dev/null +++ b/msp430/libraries/IRremote/keywords.txt @@ -0,0 +1,37 @@ +####################################### +# Syntax Coloring Map For IRremote +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +decode_results KEYWORD1 +IRrecv KEYWORD1 +IRsend KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +blink13 KEYWORD2 +decode KEYWORD2 +enableIRIn KEYWORD2 +resume KEYWORD2 +enableIROut KEYWORD2 +sendNEC KEYWORD2 +sendSony KEYWORD2 +sendRaw KEYWORD2 +sendRC5 KEYWORD2 +sendRC6 KEYWORD2 +# +####################################### +# Constants (LITERAL1) +####################################### + +NEC LITERAL1 +SONY LITERAL1 +RC5 LITERAL1 +RC6 LITERAL1 +UNKNOWN LITERAL1 +REPEAT LITERAL1 diff --git a/msp430/libraries/LiquidCrystal/LiquidCrystal.cpp b/msp430/libraries/LiquidCrystal/LiquidCrystal.cpp new file mode 100644 index 0000000..611113a --- /dev/null +++ b/msp430/libraries/LiquidCrystal/LiquidCrystal.cpp @@ -0,0 +1,309 @@ +#include "LiquidCrystal.h" + +#include <stdio.h> +#include <string.h> +#include <inttypes.h> +#include "Arduino.h" + +// When the display powers up, it is configured as follows: +// +// 1. Display clear +// 2. Function set: +// DL = 1; 8-bit interface data +// N = 0; 1-line display +// F = 0; 5x8 dot character font +// 3. Display on/off control: +// D = 0; Display off +// C = 0; Cursor off +// B = 0; Blinking off +// 4. Entry mode set: +// I/D = 1; Increment by 1 +// S = 0; No shift +// +// Note, however, that resetting the Arduino doesn't reset the LCD, so we +// can't assume that its in that state when a sketch starts (and the +// LiquidCrystal constructor is called). + +LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable, + uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, + uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7) +{ + init(0, rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7); +} + +LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t enable, + uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, + uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7) +{ + init(0, rs, 255, enable, d0, d1, d2, d3, d4, d5, d6, d7); +} + +LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable, + uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3) +{ + init(1, rs, rw, enable, d0, d1, d2, d3, 0, 0, 0, 0); +} + +LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t enable, + uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3) +{ + init(1, rs, 255, enable, d0, d1, d2, d3, 0, 0, 0, 0); +} + +void LiquidCrystal::init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable, + uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, + uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7) +{ + _rs_pin = rs; + _rw_pin = rw; + _enable_pin = enable; + + _data_pins[0] = d0; + _data_pins[1] = d1; + _data_pins[2] = d2; + _data_pins[3] = d3; + _data_pins[4] = d4; + _data_pins[5] = d5; + _data_pins[6] = d6; + _data_pins[7] = d7; + + pinMode(_rs_pin, OUTPUT); + // we can save 1 pin by not using RW. Indicate by passing 255 instead of pin# + if (_rw_pin != 255) { + pinMode(_rw_pin, OUTPUT); + } + pinMode(_enable_pin, OUTPUT); + + if (fourbitmode) + _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS; + else + _displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS; + +} + +void LiquidCrystal::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) { + if (lines > 1) { + _displayfunction |= LCD_2LINE; + } + _numlines = lines; + _currline = 0; + + // for some 1 line displays you can select a 10 pixel high font + if ((dotsize != 0) && (lines == 1)) { + _displayfunction |= LCD_5x10DOTS; + } + + // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION! + // according to datasheet, we need at least 40ms after power rises above 2.7V + // before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50 + delayMicroseconds(50000); + // Now we pull both RS and R/W low to begin commands + digitalWrite(_rs_pin, LOW); + digitalWrite(_enable_pin, LOW); + if (_rw_pin != 255) { + digitalWrite(_rw_pin, LOW); + } + + //put the LCD into 4 bit or 8 bit mode + if (! (_displayfunction & LCD_8BITMODE)) { + // this is according to the hitachi HD44780 datasheet + // figure 24, pg 46 + + // we start in 8bit mode, try to set 4 bit mode + write4bits(0x03); + delayMicroseconds(4500); // wait min 4.1ms + + // second try + write4bits(0x03); + delayMicroseconds(4500); // wait min 4.1ms + + // third go! + write4bits(0x03); + delayMicroseconds(150); + + // finally, set to 4-bit interface + write4bits(0x02); + } else { + // this is according to the hitachi HD44780 datasheet + // page 45 figure 23 + + // Send function set command sequence + command(LCD_FUNCTIONSET | _displayfunction); + delayMicroseconds(4500); // wait more than 4.1ms + + // second try + command(LCD_FUNCTIONSET | _displayfunction); + delayMicroseconds(150); + + // third go + command(LCD_FUNCTIONSET | _displayfunction); + } + + // finally, set # lines, font size, etc. + command(LCD_FUNCTIONSET | _displayfunction); + + // turn the display on with no cursor or blinking default + _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF; + display(); + + // clear it off + clear(); + + // Initialize to default text direction (for romance languages) + _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT; + // set the entry mode + command(LCD_ENTRYMODESET | _displaymode); + +} + +/********** high level commands, for the user! */ +void LiquidCrystal::clear() +{ + command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero + delayMicroseconds(2000); // this command takes a long time! +} + +void LiquidCrystal::home() +{ + command(LCD_RETURNHOME); // set cursor position to zero + delayMicroseconds(2000); // this command takes a long time! +} + +void LiquidCrystal::setCursor(uint8_t col, uint8_t row) +{ + int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 }; + if ( row >= _numlines ) { + row = _numlines-1; // we count rows starting w/0 + } + + command(LCD_SETDDRAMADDR | (col + row_offsets[row])); +} + +// Turn the display on/off (quickly) +void LiquidCrystal::noDisplay() { + _displaycontrol &= ~LCD_DISPLAYON; + command(LCD_DISPLAYCONTROL | _displaycontrol); +} +void LiquidCrystal::display() { + _displaycontrol |= LCD_DISPLAYON; + command(LCD_DISPLAYCONTROL | _displaycontrol); +} + +// Turns the underline cursor on/off +void LiquidCrystal::noCursor() { + _displaycontrol &= ~LCD_CURSORON; + command(LCD_DISPLAYCONTROL | _displaycontrol); +} +void LiquidCrystal::cursor() { + _displaycontrol |= LCD_CURSORON; + command(LCD_DISPLAYCONTROL | _displaycontrol); +} + +// Turn on and off the blinking cursor +void LiquidCrystal::noBlink() { + _displaycontrol &= ~LCD_BLINKON; + command(LCD_DISPLAYCONTROL | _displaycontrol); +} +void LiquidCrystal::blink() { + _displaycontrol |= LCD_BLINKON; + command(LCD_DISPLAYCONTROL | _displaycontrol); +} + +// These commands scroll the display without changing the RAM +void LiquidCrystal::scrollDisplayLeft(void) { + command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT); +} +void LiquidCrystal::scrollDisplayRight(void) { + command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT); +} + +// This is for text that flows Left to Right +void LiquidCrystal::leftToRight(void) { + _displaymode |= LCD_ENTRYLEFT; + command(LCD_ENTRYMODESET | _displaymode); +} + +// This is for text that flows Right to Left +void LiquidCrystal::rightToLeft(void) { + _displaymode &= ~LCD_ENTRYLEFT; + command(LCD_ENTRYMODESET | _displaymode); +} + +// This will 'right justify' text from the cursor +void LiquidCrystal::autoscroll(void) { + _displaymode |= LCD_ENTRYSHIFTINCREMENT; + command(LCD_ENTRYMODESET | _displaymode); +} + +// This will 'left justify' text from the cursor +void LiquidCrystal::noAutoscroll(void) { + _displaymode &= ~LCD_ENTRYSHIFTINCREMENT; + command(LCD_ENTRYMODESET | _displaymode); +} + +// Allows us to fill the first 8 CGRAM locations +// with custom characters +void LiquidCrystal::createChar(uint8_t location, uint8_t charmap[]) { + location &= 0x7; // we only have 8 locations 0-7 + command(LCD_SETCGRAMADDR | (location << 3)); + for (int i=0; i<8; i++) { + write(charmap[i]); + } +} + +/*********** mid level commands, for sending data/cmds */ + +inline void LiquidCrystal::command(uint8_t value) { + send(value, LOW); +} + +inline size_t LiquidCrystal::write(uint8_t value) { + send(value, HIGH); + return 1; // assume sucess +} + +/************ low level data pushing commands **********/ + +// write either command or data, with automatic 4/8-bit selection +void LiquidCrystal::send(uint8_t value, uint8_t mode) { + digitalWrite(_rs_pin, mode); + + // if there is a RW pin indicated, set it low to Write + if (_rw_pin != 255) { + digitalWrite(_rw_pin, LOW); + } + + if (_displayfunction & LCD_8BITMODE) { + write8bits(value); + } else { + write4bits(value>>4); + write4bits(value); + } +} + +void LiquidCrystal::pulseEnable(void) { + digitalWrite(_enable_pin, LOW); + delayMicroseconds(1); + digitalWrite(_enable_pin, HIGH); + delayMicroseconds(1); // enable pulse must be >450ns + digitalWrite(_enable_pin, LOW); + delayMicroseconds(100); // commands need > 37us to settle +} + +void LiquidCrystal::write4bits(uint8_t value) { + for (int i = 0; i < 4; i++) { + pinMode(_data_pins[i], OUTPUT); + digitalWrite(_data_pins[i], (value >> i) & 0x01); + } + + pulseEnable(); +} + +void LiquidCrystal::write8bits(uint8_t value) { + for (int i = 0; i < 8; i++) { + pinMode(_data_pins[i], OUTPUT); + digitalWrite(_data_pins[i], (value >> i) & 0x01); + } + + pulseEnable(); +} diff --git a/msp430/libraries/LiquidCrystal/LiquidCrystal.h b/msp430/libraries/LiquidCrystal/LiquidCrystal.h new file mode 100644 index 0000000..24ec5af --- /dev/null +++ b/msp430/libraries/LiquidCrystal/LiquidCrystal.h @@ -0,0 +1,106 @@ +#ifndef LiquidCrystal_h +#define LiquidCrystal_h + +#include <inttypes.h> +#include "Print.h" + +// commands +#define LCD_CLEARDISPLAY 0x01 +#define LCD_RETURNHOME 0x02 +#define LCD_ENTRYMODESET 0x04 +#define LCD_DISPLAYCONTROL 0x08 +#define LCD_CURSORSHIFT 0x10 +#define LCD_FUNCTIONSET 0x20 +#define LCD_SETCGRAMADDR 0x40 +#define LCD_SETDDRAMADDR 0x80 + +// flags for display entry mode +#define LCD_ENTRYRIGHT 0x00 +#define LCD_ENTRYLEFT 0x02 +#define LCD_ENTRYSHIFTINCREMENT 0x01 +#define LCD_ENTRYSHIFTDECREMENT 0x00 + +// flags for display on/off control +#define LCD_DISPLAYON 0x04 +#define LCD_DISPLAYOFF 0x00 +#define LCD_CURSORON 0x02 +#define LCD_CURSOROFF 0x00 +#define LCD_BLINKON 0x01 +#define LCD_BLINKOFF 0x00 + +// flags for display/cursor shift +#define LCD_DISPLAYMOVE 0x08 +#define LCD_CURSORMOVE 0x00 +#define LCD_MOVERIGHT 0x04 +#define LCD_MOVELEFT 0x00 + +// flags for function set +#define LCD_8BITMODE 0x10 +#define LCD_4BITMODE 0x00 +#define LCD_2LINE 0x08 +#define LCD_1LINE 0x00 +#define LCD_5x10DOTS 0x04 +#define LCD_5x8DOTS 0x00 + +class LiquidCrystal : public Print { +public: + LiquidCrystal(uint8_t rs, uint8_t enable, + uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, + uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7); + LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable, + uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, + uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7); + LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable, + uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3); + LiquidCrystal(uint8_t rs, uint8_t enable, + uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3); + + void init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable, + uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, + uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7); + + void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS); + + void clear(); + void home(); + + void noDisplay(); + void display(); + void noBlink(); + void blink(); + void noCursor(); + void cursor(); + void scrollDisplayLeft(); + void scrollDisplayRight(); + void leftToRight(); + void rightToLeft(); + void autoscroll(); + void noAutoscroll(); + + void createChar(uint8_t, uint8_t[]); + void setCursor(uint8_t, uint8_t); + virtual size_t write(uint8_t); + void command(uint8_t); + + using Print::write; +private: + void send(uint8_t, uint8_t); + void write4bits(uint8_t); + void write8bits(uint8_t); + void pulseEnable(); + + uint8_t _rs_pin; // LOW: command. HIGH: character. + uint8_t _rw_pin; // LOW: write to LCD. HIGH: read from LCD. + uint8_t _enable_pin; // activated by a HIGH pulse. + uint8_t _data_pins[8]; + + uint8_t _displayfunction; + uint8_t _displaycontrol; + uint8_t _displaymode; + + uint8_t _initialized; + + uint8_t _numlines,_currline; +}; + +#endif diff --git a/msp430/libraries/LiquidCrystal/examples/Autoscroll/Autoscroll.ino b/msp430/libraries/LiquidCrystal/examples/Autoscroll/Autoscroll.ino new file mode 100644 index 0000000..756d5b5 --- /dev/null +++ b/msp430/libraries/LiquidCrystal/examples/Autoscroll/Autoscroll.ino @@ -0,0 +1,83 @@ +/* + LiquidCrystal Library - Autoscroll + + Demonstrates the use a 16x2 LCD display. The LiquidCrystal + library works with all LCD displays that are compatible with the + Hitachi HD44780 driver. There are many of them out there, and you + can usually tell them by the 16-pin interface. + + This sketch demonstrates the use of the autoscroll() + and noAutoscroll() functions to make new text scroll or not. + + The circuit: + ================================= + LCD pin Connect to + --------------------------------- + 01 - GND GND, pot + 02 - VCC +5V, pot + 03 - Contrast Pot wiper + 04 - RS Pin8 (P2.0) + 05 - R/W GND + 06 - EN Pin9 (P2.1) + 07 - DB0 GND + 08 - DB1 GND + 09 - DB2 GND + 10 - DB3 GND + 11 - DB4 Pin10 (P2.2) + 12 - DB5 Pin11 (P2.3) + 13 - DB6 Pin12 (P2.4) + 14 - DB7 Pin13 (P2.5) + 15 - BL+ +5V + 16 - BL- GND + ================================= + + Library originally added 18 Apr 2008 + by David A. Mellis + library modified 5 Jul 2009 + by Limor Fried (http://www.ladyada.net) + example added 9 Jul 2009 + by Tom Igoe + modified 22 Nov 2010 + by Tom Igoe + + This example code is in the public domain. + + http://www.arduino.cc/en/Tutorial/LiquidCrystal + */ + +// include the library code: +#include <LiquidCrystal.h> + +// initialize the library with the numbers of the interface pins +LiquidCrystal lcd(P2_0, P2_1, P2_2, P2_3, P2_4, P2_5); + +void setup() { + // set up the LCD's number of columns and rows: + lcd.begin(16,2); +} + +void loop() { + // set the cursor to (0,0): + lcd.setCursor(0, 0); + // print from 0 to 9: + for (int thisChar = 0; thisChar < 10; thisChar++) { + lcd.print(thisChar); + delay(500); + } + + // set the cursor to (16,1): + lcd.setCursor(16,1); + // set the display to automatically scroll: + lcd.autoscroll(); + // print from 0 to 9: + for (int thisChar = 0; thisChar < 10; thisChar++) { + lcd.print(thisChar); + delay(500); + } + // turn off automatic scrolling + lcd.noAutoscroll(); + + // clear screen for the next loop: + lcd.clear(); +} + diff --git a/msp430/libraries/LiquidCrystal/examples/Blink/Blink.ino b/msp430/libraries/LiquidCrystal/examples/Blink/Blink.ino new file mode 100644 index 0000000..b0d2505 --- /dev/null +++ b/msp430/libraries/LiquidCrystal/examples/Blink/Blink.ino @@ -0,0 +1,71 @@ +/* + LiquidCrystal Library - Blink + + Demonstrates the use a 16x2 LCD display. The LiquidCrystal + library works with all LCD displays that are compatible with the + Hitachi HD44780 driver. There are many of them out there, and you + can usually tell them by the 16-pin interface. + + This sketch prints "Hello World!" to the LCD and makes the + cursor block blink. + + The circuit: + ================================= + LCD pin Connect to + --------------------------------- + 01 - GND GND, pot + 02 - VCC +5V, pot + 03 - Contrast Pot wiper + 04 - RS Pin8 (P2.0) + 05 - R/W GND + 06 - EN Pin9 (P2.1) + 07 - DB0 GND + 08 - DB1 GND + 09 - DB2 GND + 10 - DB3 GND + 11 - DB4 Pin10 (P2.2) + 12 - DB5 Pin11 (P2.3) + 13 - DB6 Pin12 (P2.4) + 14 - DB7 Pin13 (P2.5) + 15 - BL+ +5V + 16 - BL- GND + ================================= + + Library originally added 18 Apr 2008 + by David A. Mellis + library modified 5 Jul 2009 + by Limor Fried (http://www.ladyada.net) + example added 9 Jul 2009 + by Tom Igoe + modified 22 Nov 2010 + by Tom Igoe + + This example code is in the public domain. + + http://www.arduino.cc/en/Tutorial/LiquidCrystal + + */ + +// include the library code: +#include <LiquidCrystal.h> + +// initialize the library with the numbers of the interface pins +LiquidCrystal lcd(P2_0, P2_1, P2_2, P2_3, P2_4, P2_5); + +void setup() { + // set up the LCD's number of columns and rows: + lcd.begin(16, 2); + // Print a message to the LCD. + lcd.print("hello, world!"); +} + +void loop() { + // Turn off the blinking cursor: + lcd.noBlink(); + delay(3000); + // Turn on the blinking cursor: + lcd.blink(); + delay(3000); +} + + diff --git a/msp430/libraries/LiquidCrystal/examples/Cursor/Cursor.ino b/msp430/libraries/LiquidCrystal/examples/Cursor/Cursor.ino new file mode 100644 index 0000000..8513ef7 --- /dev/null +++ b/msp430/libraries/LiquidCrystal/examples/Cursor/Cursor.ino @@ -0,0 +1,70 @@ +/* + LiquidCrystal Library - Cursor + + Demonstrates the use a 16x2 LCD display. The LiquidCrystal + library works with all LCD displays that are compatible with the + Hitachi HD44780 driver. There are many of them out there, and you + can usually tell them by the 16-pin interface. + + This sketch prints "Hello World!" to the LCD and + uses the cursor() and noCursor() methods to turn + on and off the cursor. + + The circuit: + ================================= + LCD pin Connect to + --------------------------------- + 01 - GND GND, pot + 02 - VCC +5V, pot + 03 - Contrast Pot wiper + 04 - RS Pin8 (P2.0) + 05 - R/W GND + 06 - EN Pin9 (P2.1) + 07 - DB0 GND + 08 - DB1 GND + 09 - DB2 GND + 10 - DB3 GND + 11 - DB4 Pin10 (P2.2) + 12 - DB5 Pin11 (P2.3) + 13 - DB6 Pin12 (P2.4) + 14 - DB7 Pin13 (P2.5) + 15 - BL+ +5V + 16 - BL- GND + ================================= + + Library originally added 18 Apr 2008 + by David A. Mellis + library modified 5 Jul 2009 + by Limor Fried (http://www.ladyada.net) + example added 9 Jul 2009 + by Tom Igoe + modified 22 Nov 2010 + by Tom Igoe + + This example code is in the public domain. + + http://www.arduino.cc/en/Tutorial/LiquidCrystal + */ + +// include the library code: +#include <LiquidCrystal.h> + +// initialize the library with the numbers of the interface pins +LiquidCrystal lcd(P2_0, P2_1, P2_2, P2_3, P2_4, P2_5); + +void setup() { + // set up the LCD's number of columns and rows: + lcd.begin(16, 2); + // Print a message to the LCD. + lcd.print("hello, world!"); +} + +void loop() { + // Turn off the cursor: + lcd.noCursor(); + delay(500); + // Turn on the cursor: + lcd.cursor(); + delay(500); +} + diff --git a/msp430/libraries/LiquidCrystal/examples/CustomCharacter/CustomCharacter.ino b/msp430/libraries/LiquidCrystal/examples/CustomCharacter/CustomCharacter.ino new file mode 100644 index 0000000..23fbe56 --- /dev/null +++ b/msp430/libraries/LiquidCrystal/examples/CustomCharacter/CustomCharacter.ino @@ -0,0 +1,147 @@ +/* + LiquidCrystal Library - Custom Characters + + Demonstrates how to add custom characters on an LCD display. + The LiquidCrystal library works with all LCD displays that are + compatible with the Hitachi HD44780 driver. There are many of + them out there, and you can usually tell them by the 16-pin interface. + + This sketch prints "I <heart> Arduino!" and a little dancing man + to the LCD. + + The circuit: + ================================= + LCD pin Connect to + --------------------------------- + 01 - GND GND, pot + 02 - VCC +5V, pot + 03 - Contrast Pot wiper + 04 - RS Pin8 (P2.0) + 05 - R/W GND + 06 - EN Pin9 (P2.1) + 07 - DB0 GND + 08 - DB1 GND + 09 - DB2 GND + 10 - DB3 GND + 11 - DB4 Pin10 (P2.2) + 12 - DB5 Pin11 (P2.3) + 13 - DB6 Pin12 (P2.4) + 14 - DB7 Pin13 (P2.5) + 15 - BL+ +5V + 16 - BL- GND + ================================= + + created21 Mar 2011 + by Tom Igoe + Based on Adafruit's example at + https://github.com/adafruit/SPI_VFD/blob/master/examples/createChar/createChar.pde + + This example code is in the public domain. + http://www.arduino.cc/en/Tutorial/LiquidCrystal + + Also useful: + http://icontexto.com/charactercreator/ + + */ + +// include the library code: +#include <LiquidCrystal.h> + +// initialize the library with the numbers of the interface pins +LiquidCrystal lcd(P2_0, P2_1, P2_2, P2_3, P2_4, P2_5); + +// make some custom characters: +byte heart[8] = { + 0b00000, + 0b01010, + 0b11111, + 0b11111, + 0b11111, + 0b01110, + 0b00100, + 0b00000 +}; + +byte smiley[8] = { + 0b00000, + 0b00000, + 0b01010, + 0b00000, + 0b00000, + 0b10001, + 0b01110, + 0b00000 +}; + +byte frownie[8] = { + 0b00000, + 0b00000, + 0b01010, + 0b00000, + 0b00000, + 0b00000, + 0b01110, + 0b10001 +}; + +byte armsDown[8] = { + 0b00100, + 0b01010, + 0b00100, + 0b00100, + 0b01110, + 0b10101, + 0b00100, + 0b01010 +}; + +byte armsUp[8] = { + 0b00100, + 0b01010, + 0b00100, + 0b10101, + 0b01110, + 0b00100, + 0b00100, + 0b01010 +}; +void setup() { + // create a new character + lcd.createChar(0, heart); + // create a new character + lcd.createChar(1, smiley); + // create a new character + lcd.createChar(2, frownie); + // create a new character + lcd.createChar(3, armsDown); + // create a new character + lcd.createChar(4, armsUp); + + // set up the lcd's number of columns and rows: + lcd.begin(16, 2); + // Print a message to the lcd. + lcd.print("I "); + lcd.write(0); + lcd.print(" Arduino! "); + lcd.write(1); + +} + +void loop() { + // read the potentiometer on A0: + int sensorReading = analogRead(A0); + // map the result to 200 - 1000: + int delayTime = map(sensorReading, 0, 1023, 200, 1000); + // set the cursor to the bottom row, 5th position: + lcd.setCursor(4, 1); + // draw the little man, arms down: + lcd.write(3); + delay(delayTime); + lcd.setCursor(4, 1); + // draw him arms up: + lcd.write(4); + delay(delayTime); +} + + + diff --git a/msp430/libraries/LiquidCrystal/examples/Display/Display.ino b/msp430/libraries/LiquidCrystal/examples/Display/Display.ino new file mode 100644 index 0000000..31289b4 --- /dev/null +++ b/msp430/libraries/LiquidCrystal/examples/Display/Display.ino @@ -0,0 +1,70 @@ +/* + LiquidCrystal Library - display() and noDisplay() + + Demonstrates the use a 16x2 LCD display. The LiquidCrystal + library works with all LCD displays that are compatible with the + Hitachi HD44780 driver. There are many of them out there, and you + can usually tell them by the 16-pin interface. + + This sketch prints "Hello World!" to the LCD and uses the + display() and noDisplay() functions to turn on and off + the display. + + The circuit: + ================================= + LCD pin Connect to + --------------------------------- + 01 - GND GND, pot + 02 - VCC +5V, pot + 03 - Contrast Pot wiper + 04 - RS Pin8 (P2.0) + 05 - R/W GND + 06 - EN Pin9 (P2.1) + 07 - DB0 GND + 08 - DB1 GND + 09 - DB2 GND + 10 - DB3 GND + 11 - DB4 Pin10 (P2.2) + 12 - DB5 Pin11 (P2.3) + 13 - DB6 Pin12 (P2.4) + 14 - DB7 Pin13 (P2.5) + 15 - BL+ +5V + 16 - BL- GND + ================================= + + Library originally added 18 Apr 2008 + by David A. Mellis + library modified 5 Jul 2009 + by Limor Fried (http://www.ladyada.net) + example added 9 Jul 2009 + by Tom Igoe + modified 22 Nov 2010 + by Tom Igoe + + This example code is in the public domain. + + http://www.arduino.cc/en/Tutorial/LiquidCrystal + */ + +// include the library code: +#include <LiquidCrystal.h> + +// initialize the library with the numbers of the interface pins +LiquidCrystal lcd(12, 11, 5, 4, 3, 2); + +void setup() { + // set up the LCD's number of columns and rows: + lcd.begin(16, 2); + // Print a message to the LCD. + lcd.print("hello, world!"); +} + +void loop() { + // Turn off the display: + lcd.noDisplay(); + delay(500); + // Turn on the display: + lcd.display(); + delay(500); +} + diff --git a/msp430/libraries/LiquidCrystal/examples/HelloWorld/HelloWorld.ino b/msp430/libraries/LiquidCrystal/examples/HelloWorld/HelloWorld.ino new file mode 100644 index 0000000..f906cd4 --- /dev/null +++ b/msp430/libraries/LiquidCrystal/examples/HelloWorld/HelloWorld.ino @@ -0,0 +1,68 @@ +/* + LiquidCrystal Library - Hello World + + Demonstrates the use a 16x2 LCD display. The LiquidCrystal + library works with all LCD displays that are compatible with the + Hitachi HD44780 driver. There are many of them out there, and you + can usually tell them by the 16-pin interface. + + This sketch prints "Hello World!" to the LCD + and shows the time. + + The circuit: + ================================= + LCD pin Connect to + --------------------------------- + 01 - GND GND, pot + 02 - VCC +5V, pot + 03 - Contrast Pot wiper + 04 - RS Pin8 (P2.0) + 05 - R/W GND + 06 - EN Pin9 (P2.1) + 07 - DB0 GND + 08 - DB1 GND + 09 - DB2 GND + 10 - DB3 GND + 11 - DB4 Pin10 (P2.2) + 12 - DB5 Pin11 (P2.3) + 13 - DB6 Pin12 (P2.4) + 14 - DB7 Pin13 (P2.5) + 15 - BL+ +5V + 16 - BL- GND + ================================= + + Library originally added 18 Apr 2008 + by David A. Mellis + library modified 5 Jul 2009 + by Limor Fried (http://www.ladyada.net) + example added 9 Jul 2009 + by Tom Igoe + modified 22 Nov 2010 + by Tom Igoe + + This example code is in the public domain. + + http://www.arduino.cc/en/Tutorial/LiquidCrystal + */ + +// include the library code: +#include <LiquidCrystal.h> + +// initialize the library with the numbers of the interface pins +LiquidCrystal lcd(P2_0, P2_1, P2_2, P2_3, P2_4, P2_5); + +void setup() { + // set up the LCD's number of columns and rows: + lcd.begin(16, 2); + // Print a message to the LCD. + lcd.print("hello, world!"); +} + +void loop() { + // set the cursor to column 0, line 1 + // (note: line 1 is the second row, since counting begins with 0): + lcd.setCursor(0, 1); + // print the number of seconds since reset: + lcd.print(millis()/1000); +} + diff --git a/msp430/libraries/LiquidCrystal/examples/Scroll/Scroll.ino b/msp430/libraries/LiquidCrystal/examples/Scroll/Scroll.ino new file mode 100644 index 0000000..0da976d --- /dev/null +++ b/msp430/libraries/LiquidCrystal/examples/Scroll/Scroll.ino @@ -0,0 +1,95 @@ +/* + LiquidCrystal Library - scrollDisplayLeft() and scrollDisplayRight() + + Demonstrates the use a 16x2 LCD display. The LiquidCrystal + library works with all LCD displays that are compatible with the + Hitachi HD44780 driver. There are many of them out there, and you + can usually tell them by the 16-pin interface. + + This sketch prints "Hello World!" to the LCD and uses the + scrollDisplayLeft() and scrollDisplayRight() methods to scroll + the text. + + The circuit: + ================================= + LCD pin Connect to + --------------------------------- + 01 - GND GND, pot + 02 - VCC +5V, pot + 03 - Contrast Pot wiper + 04 - RS Pin8 (P2.0) + 05 - R/W GND + 06 - EN Pin9 (P2.1) + 07 - DB0 GND + 08 - DB1 GND + 09 - DB2 GND + 10 - DB3 GND + 11 - DB4 Pin10 (P2.2) + 12 - DB5 Pin11 (P2.3) + 13 - DB6 Pin12 (P2.4) + 14 - DB7 Pin13 (P2.5) + 15 - BL+ +5V + 16 - BL- GND + ================================= + + Library originally added 18 Apr 2008 + by David A. Mellis + library modified 5 Jul 2009 + by Limor Fried (http://www.ladyada.net) + example added 9 Jul 2009 + by Tom Igoe + modified 22 Nov 2010 + by Tom Igoe + + This example code is in the public domain. + + http://www.arduino.cc/en/Tutorial/LiquidCrystal + */ + +// include the library code: +#include <LiquidCrystal.h> + +// initialize the library with the numbers of the interface pins +LiquidCrystal lcd(P2_0, P2_1, P2_2, P2_3, P2_4, P2_5); + +void setup() { + // set up the LCD's number of columns and rows: + lcd.begin(16, 2); + // Print a message to the LCD. + lcd.print("hello, world!"); + delay(1000); +} + +void loop() { + // scroll 13 positions (string length) to the left + // to move it offscreen left: + for (int positionCounter = 0; positionCounter < 13; positionCounter++) { + // scroll one position left: + lcd.scrollDisplayLeft(); + // wait a bit: + delay(150); + } + + // scroll 29 positions (string length + display length) to the right + // to move it offscreen right: + for (int positionCounter = 0; positionCounter < 29; positionCounter++) { + // scroll one position right: + lcd.scrollDisplayRight(); + // wait a bit: + delay(150); + } + + // scroll 16 positions (display length + string length) to the left + // to move it back to center: + for (int positionCounter = 0; positionCounter < 16; positionCounter++) { + // scroll one position left: + lcd.scrollDisplayLeft(); + // wait a bit: + delay(150); + } + + // delay at the end of the full loop: + delay(1000); + +} + diff --git a/msp430/libraries/LiquidCrystal/examples/SerialDisplay/SerialDisplay.ino b/msp430/libraries/LiquidCrystal/examples/SerialDisplay/SerialDisplay.ino new file mode 100644 index 0000000..f76d23d --- /dev/null +++ b/msp430/libraries/LiquidCrystal/examples/SerialDisplay/SerialDisplay.ino @@ -0,0 +1,87 @@ +/* + LiquidCrystal Library - Serial Input + + Demonstrates the use a 16x2 LCD display. The LiquidCrystal + library works with all LCD displays that are compatible with the + Hitachi HD44780 driver. There are many of them out there, and you + can usually tell them by the 16-pin interface. + + This sketch displays text sent over the serial port + (e.g. from the Serial Monitor) on an attached LCD. + + The circuit: + ================================= + LCD pin Connect to + --------------------------------- + 01 - GND GND, pot + 02 - VCC +5V, pot + 03 - Contrast Pot wiper + 04 - RS Pin8 (P2.0) + 05 - R/W GND + 06 - EN Pin9 (P2.1) + 07 - DB0 GND + 08 - DB1 GND + 09 - DB2 GND + 10 - DB3 GND + 11 - DB4 Pin10 (P2.2) + 12 - DB5 Pin11 (P2.3) + 13 - DB6 Pin12 (P2.4) + 14 - DB7 Pin13 (P2.5) + 15 - BL+ +5V + 16 - BL- GND + ================================= + + The circuit: + * LCD RS pin to digital pin 12 + * LCD Enable pin to digital pin 11 + * LCD D4 pin to digital pin 5 + * LCD D5 pin to digital pin 4 + * LCD D6 pin to digital pin 3 + * LCD D7 pin to digital pin 2 + * LCD R/W pin to ground + * 10K resistor: + * ends to +5V and ground + * wiper to LCD VO pin (pin 3) + + Library originally added 18 Apr 2008 + by David A. Mellis + library modified 5 Jul 2009 + by Limor Fried (http://www.ladyada.net) + example added 9 Jul 2009 + by Tom Igoe + modified 22 Nov 2010 + by Tom Igoe + + This example code is in the public domain. + + http://www.arduino.cc/en/Tutorial/LiquidCrystal + */ + +// include the library code: +#include <LiquidCrystal.h> + +// initialize the library with the numbers of the interface pins +LiquidCrystal lcd(P2_0, P2_1, P2_2, P2_3, P2_4, P2_5); + +void setup(){ + // set up the LCD's number of columns and rows: + lcd.begin(16, 2); + // initialize the serial communications: + Serial.begin(9600); +} + +void loop() +{ + // when characters arrive over the serial port... + if (Serial.available()) { + // wait a bit for the entire message to arrive + delay(100); + // clear the screen + lcd.clear(); + // read all the available characters + while (Serial.available() > 0) { + // display each character to the LCD + lcd.write(Serial.read()); + } + } +} diff --git a/msp430/libraries/LiquidCrystal/examples/TextDirection/TextDirection.ino b/msp430/libraries/LiquidCrystal/examples/TextDirection/TextDirection.ino new file mode 100644 index 0000000..9114197 --- /dev/null +++ b/msp430/libraries/LiquidCrystal/examples/TextDirection/TextDirection.ino @@ -0,0 +1,97 @@ + /* + LiquidCrystal Library - TextDirection + + Demonstrates the use a 16x2 LCD display. The LiquidCrystal + library works with all LCD displays that are compatible with the + Hitachi HD44780 driver. There are many of them out there, and you + can usually tell them by the 16-pin interface. + + This sketch demonstrates how to use leftToRight() and rightToLeft() + to move the cursor. + + The circuit: + ================================= + LCD pin Connect to + --------------------------------- + 01 - GND GND, pot + 02 - VCC +5V, pot + 03 - Contrast Pot wiper + 04 - RS Pin8 (P2.0) + 05 - R/W GND + 06 - EN Pin9 (P2.1) + 07 - DB0 GND + 08 - DB1 GND + 09 - DB2 GND + 10 - DB3 GND + 11 - DB4 Pin10 (P2.2) + 12 - DB5 Pin11 (P2.3) + 13 - DB6 Pin12 (P2.4) + 14 - DB7 Pin13 (P2.5) + 15 - BL+ +5V + 16 - BL- GND + ================================= + + Library originally added 18 Apr 2008 + by David A. Mellis + library modified 5 Jul 2009 + by Limor Fried (http://www.ladyada.net) + example added 9 Jul 2009 + by Tom Igoe + modified 22 Nov 2010 + by Tom Igoe + + This example code is in the public domain. + + http://www.arduino.cc/en/Tutorial/LiquidCrystal + + */ + +// include the library code: +#include <LiquidCrystal.h> + +// initialize the library with the numbers of the interface pins +LiquidCrystal lcd(P2_0, P2_1, P2_2, P2_3, P2_4, P2_5); + +int thisChar = 'a'; + +void setup() { + // set up the LCD's number of columns and rows: + lcd.begin(16, 2); + // turn on the cursor: + lcd.cursor(); + Serial.begin(9600); +} + +void loop() { + // reverse directions at 'm': + if (thisChar == 'm') { + // go right for the next letter + lcd.rightToLeft(); + } + // reverse again at 's': + if (thisChar == 's') { + // go left for the next letter + lcd.leftToRight(); + } + // reset at 'z': + if (thisChar > 'z') { + // go to (0,0): + lcd.home(); + // start again at 0 + thisChar = 'a'; + } + // print the character + lcd.write(thisChar); + // wait a second: + delay(1000); + // increment the letter: + thisChar++; +} + + + + + + + + diff --git a/msp430/libraries/LiquidCrystal/examples/setCursor/setCursor.ino b/msp430/libraries/LiquidCrystal/examples/setCursor/setCursor.ino new file mode 100644 index 0000000..131e810 --- /dev/null +++ b/msp430/libraries/LiquidCrystal/examples/setCursor/setCursor.ino @@ -0,0 +1,81 @@ +/* + LiquidCrystal Library - setCursor + + Demonstrates the use a 16x2 LCD display. The LiquidCrystal + library works with all LCD displays that are compatible with the + Hitachi HD44780 driver. There are many of them out there, and you + can usually tell them by the 16-pin interface. + + This sketch prints to all the positions of the LCD using the + setCursor(0 method: + + The circuit: + ================================= + LCD pin Connect to + --------------------------------- + 01 - GND GND, pot + 02 - VCC +5V, pot + 03 - Contrast Pot wiper + 04 - RS Pin8 (P2.0) + 05 - R/W GND + 06 - EN Pin9 (P2.1) + 07 - DB0 GND + 08 - DB1 GND + 09 - DB2 GND + 10 - DB3 GND + 11 - DB4 Pin10 (P2.2) + 12 - DB5 Pin11 (P2.3) + 13 - DB6 Pin12 (P2.4) + 14 - DB7 Pin13 (P2.5) + 15 - BL+ +5V + 16 - BL- GND + ================================= + + Library originally added 18 Apr 2008 + by David A. Mellis + library modified 5 Jul 2009 + by Limor Fried (http://www.ladyada.net) + example added 9 Jul 2009 + by Tom Igoe + modified 22 Nov 2010 + by Tom Igoe + + This example code is in the public domain. + + http://www.arduino.cc/en/Tutorial/LiquidCrystal + */ + +// include the library code: +#include <LiquidCrystal.h> + +// these constants won't change. But you can change the size of +// your LCD using them: +const int numRows = 2; +const int numCols = 16; + +// initialize the library with the numbers of the interface pins +LiquidCrystal lcd(P2_0, P2_1, P2_2, P2_3, P2_4, P2_5); + +void setup() { + // set up the LCD's number of columns and rows: + lcd.begin(numCols,numRows); +} + +void loop() { + // loop from ASCII 'a' to ASCII 'z': + for (int thisLetter = 'a'; thisLetter <= 'z'; thisLetter++) { + // loop over the columns: + for (int thisCol = 0; thisCol < numRows; thisCol++) { + // loop over the rows: + for (int thisRow = 0; thisRow < numCols; thisRow++) { + // set the cursor position: + lcd.setCursor(thisRow,thisCol); + // print the letter: + lcd.write(thisLetter); + delay(200); + } + } + } +} + + diff --git a/msp430/libraries/LiquidCrystal/keywords.txt b/msp430/libraries/LiquidCrystal/keywords.txt new file mode 100644 index 0000000..132845c --- /dev/null +++ b/msp430/libraries/LiquidCrystal/keywords.txt @@ -0,0 +1,37 @@ +####################################### +# Syntax Coloring Map For LiquidCrystal +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +LiquidCrystal KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +begin KEYWORD2 +clear KEYWORD2 +home KEYWORD2 +print KEYWORD2 +setCursor KEYWORD2 +cursor KEYWORD2 +noCursor KEYWORD2 +blink KEYWORD2 +noBlink KEYWORD2 +display KEYWORD2 +noDisplay KEYWORD2 +autoscroll KEYWORD2 +noAutoscroll KEYWORD2 +leftToRight KEYWORD2 +rightToLeft KEYWORD2 +scrollDisplayLeft KEYWORD2 +scrollDisplayRight KEYWORD2 +createChar KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### + diff --git a/msp430/libraries/MspFlash/MspFlash.cpp b/msp430/libraries/MspFlash/MspFlash.cpp new file mode 100644 index 0000000..3495e62 --- /dev/null +++ b/msp430/libraries/MspFlash/MspFlash.cpp @@ -0,0 +1,62 @@ +/* + MspFlash.h - Read/Write flash memory library for MSP430 Energia + Copyright (c) 2012 Peter Brier. 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 +*/ +#include "MspFlash.h" +#include <msp430.h> +#include <Energia.h> + +// The Flash clock must be between 200 and 400 kHz to operate correctly +// TODO: calculate correct devider (0..64) depending on clock frequenct (F_CPU) +// Now we set F_CPU/64 is 250khz at F_CPU = 16MHz + +#define FLASHCLOCK FSSEL1+((F_CPU/400000L) & 63); // SCLK + +// erase flash, make sure pointer is in the segment you wish to erase, otherwise you may erase you program or some data +void MspFlashClass::erase(unsigned char *flash) +{ + disableWatchDog(); // Disable WDT + FCTL2 = FWKEY+FLASHCLOCK; // SMCLK/2 + FCTL3 = FWKEY; // Clear LOCK + FCTL1 = FWKEY+ERASE; //Enable segment erase + *flash = 0; // Dummy write, erase Segment + FCTL3 = FWKEY+LOCK; // Done, set LOCK + enableWatchDog(); // Enable WDT +} + +// load from memory, at segment boundary +void MspFlashClass::read(unsigned char *flash, unsigned char *dest, int len) +{ + while(len--) + *(dest++) = *(flash++); +} + +// save in to flash (at segment boundary) +void MspFlashClass::write(unsigned char *flash, unsigned char *src, int len) +{ + disableWatchDog(); // Disable WDT + FCTL2 = FWKEY+FLASHCLOCK; // SMCLK/2 + FCTL3 = FWKEY; // Clear LOCK + FCTL1 = FWKEY+WRT; // Enable write + while(len--) // Copy data + *(flash++) = *(src++); + FCTL1 = FWKEY; //Done. Clear WRT + FCTL3 = FWKEY+LOCK; // Set LOCK + enableWatchDog(); // Enable WDT +} + +MspFlashClass Flash; diff --git a/msp430/libraries/MspFlash/MspFlash.h b/msp430/libraries/MspFlash/MspFlash.h new file mode 100644 index 0000000..afa131c --- /dev/null +++ b/msp430/libraries/MspFlash/MspFlash.h @@ -0,0 +1,83 @@ +/* + MspFlash.h - Read/Write flash memory library for MSP430 Energia + Copyright (c) 2012 Peter Brier. 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 + + Provide access to the MSP430 flash memory controller. + All flash memory can be read, erased and written (except SEGMENT_A, the LOCK bits are not in the code, for a good reason). + Flash can only be erased per 512 byte segments (except the 4 special information segments, they are 64 bytes in size) + + The same flash locations can be written multiple times with new values, but flash bits can only be reset (from 1 to 0) and cannot + change to a 1 (you need to flash erase the whole segment) + + functions: + ~~~~~~~~~~ + erase(): Erase a flash segment, all bytes in the segment will read 0xFF after an erase + + read(): Read flash locations (actually just a proxy for memcpy) + + write(): Write flash locations (actually just a proxy for memcpy), the same location can be written multiple times, + but once a bit is reset, you cannot set it with a subsequent write, you need to flash the complete segment to do so. + + NOTE: you are flashing the program memory, you can modify EVERYTHING (program and data) this is usually not what you want. + Be carefull when flashing data. You may use SEG_B to SEG_D for normal use, they should not be filled by the compiler with code + or static data. If you wish to use main memory without wasting any space, you need to inform the linker NOT to use the required + segments. This is done in the linker script (this is not for the faint of heart). + + An alternative approach is to allocate a static variable with a size of AT LEAST 2 SEGMENTS in your program. + This makes sure there is at least ONE COMPLETE SEGMENT in this static variable, so there is no colleteral damage when you flash this + area. You need to find the pointer to the start of the next segment. There is a macro define to do this: SEGPTR(x) + A example that makes 2 segments available for flashing by allocating 3 segments of constant data: + + static const unsigned char flash[3*512] = {0}; + + Flash segments: |...SEGMENT-0...|...SEGMENT-1...|...SEGMENT-2...|...SEGMENT-3...|...SEGMENT-4...|...SEGMENT-5...| + Program output: |Program code.......|flash[3*512***********************************]|Other data.................| + Available segments: |-------------------------------|+++++++++++++++|+++++++++++++++|-------------------------------| + + +*/ + +#ifndef MSP_FLASH_h +#define MSP_FLASH_h + +#define SEGMENT_SIZE 512 // main segment size (smallest flasheable area) +#define INFO_SIZE 64 // information flah sizes (SEG_A to SEG_D) + +// segment addresses of 64 byte segments +#define SEGMENT_D ((unsigned char*)0x1000) +#define SEGMENT_C ((unsigned char*)0x1000+64) +#define SEGMENT_B ((unsigned char*)0x1000+128) +// #define SEGMENT_A ((unsigned char*)0x1000+192) // NOTE: Contains chip calibarion data, do not change or erase (the protection bit should prevent this happening) + +// Segment start addresses of 512 byte segments 0..63 (depending on device flash size) +#define SEGMENT(x) ( (unsigned char*) 0xFFFF - (x)*SEG_SIZE ) + +// return segment pointer inside variable. Note: return start of NEXT segment +#define SEGPTR(x) ( (unsigned char *) (((unsigned short)(&x)+512) & 0xFE00) ) + +class MspFlashClass +{ + public: + void erase(unsigned char *flash); + void read(unsigned char *flash, unsigned char *dest, int len); + void write(unsigned char *flash, unsigned char *src, int len); +}; + +extern MspFlashClass Flash; + +#endif // MSP_FLASH_h +
diff --git a/msp430/libraries/MspFlash/examples/flash_readwrite/flash_readwrite.ino b/msp430/libraries/MspFlash/examples/flash_readwrite/flash_readwrite.ino new file mode 100644 index 0000000..70f3674 --- /dev/null +++ b/msp430/libraries/MspFlash/examples/flash_readwrite/flash_readwrite.ino @@ -0,0 +1,151 @@ +/* + flas_readwrite.h - Read/Write flash memory library example for MSP430 Energia + Copyright (c) 2012 Peter Brier. 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 + + Provide access to the MSP430 flash memory controller. + All flash memory can be read, erased and written (except SEGMENT_A, the LOCK bits are not in the code, for a good reason). + Flash can only be erased per 512 byte segments (except the 4 special information segments, they are 64 bytes in size) + + The same flash locations can be written multiple times with new values, but flash bits can only be reset (from 1 to 0) and cannot + change to a 1 (you need to flash erase the whole segment) + + functions: + ~~~~~~~~~~ + erase(): Erase a flash segment, all bytes in the segment will read 0xFF after an erase + + read(): Read flash locations (actually just a proxy for memcpy) + + write(): Write flash locations (actually just a proxy for memcpy), the same location can be written multiple times, + but once a bit is reset, you cannot set it with a subsequent write, you need to flash the complete segment to do so. + + constants: + ~~~~~~~~~~ + SEGMENT_A // pointer to 64 byte flash segments + SEGMENT_B + SEGMENT_C + SEGMENT_D + + Macros: + ~~~~~~~ + SEGPTR(x) // Return pointer to first complete segment inside variable + SEGMENT(n) // Return pointer to start of segment n (n=0..63) + + + NOTE: you are flashing the program memory, you can modify EVERYTHING (program, data) this is usually not what you want. + Be carefull when flashing data. You may use SEG_B to SEG_D for normal use, they should not be filled by the compiler + If you wish to use main memory, you need to inform the linker NOT to use the segments you wish to use in the linker script + (this is not for the faint of heart). + + An alternative approach is to allocate a static variable with a size of AT LEAST 2 SEGMENTS in your program. + This makes sure there is at least ONE COMPLETE SEGMENT in this static variable, so there is no colleteral damage when you flash this + area. You need to find the pointer to the start of the next segment. There is a macro define to do this: SEGPTR(x) + A example that makes 2 segments available for flashing by allocating 3 segments of constant data: + + + Using the example: + ~~~~~~~~~~~~~~~~~~ + + On the launchpad; put the two UART jumpers in HARDWARE SERIAL position (horizontal position) and use the terminal window to connect + to the board (9600baud). + + 'e' Erase the flash segment + 'w' Write "Hello World" to the flash + 'r' Read the contents of the flash, and print as byte values and characters. stop at the first NULL byte + + - When you program the launchpad and read the flash, a single "0" character should be read (mem contains zero values) + Writing the flash before you have erased it is not possible (you cannot program OFF bits to ON bits) + - When you erase the flash, 0xFF values will be read back + - When you write the flash, "Hello World" will be read back + + +*/ + +#include "MspFlash.h" + + +// Two options to use for flash: One of the info flash segments, or a part of the program memory +// either define a bit of constant program memory, and pass a pointer to the start of a segment to the flash functions, + +//*** Option 1: use program memory, uncomment these lines and you have 512 bytes of flash available (1024 bytes allocated) **** +//const unsigned char data[2*SEGMENT_SIZE] = {0}; +//#define flash SEGPTR(data) +// + +//*** Option 2: use one of the 64 byte info segments, uncomment this line. Use SEGMENT_B, SEGMENT_C or SEGMENT_D (each 64 bytes, 192 bytes in total) +#define flash SEGMENT_D +// + + +void setup() +{ + // put your setup code here, to run once: + Serial.begin(9600); +} + +void loop() { + // put your main code here, to run repeatedly: + if ( Serial.available() ) + { + switch ( Serial.read() ) + { + case 'e': doErase(); break; + case 'r': doRead(); break; + case 'w': doWrite(); break; + case 10: + case 13: break; + default: doHelp(); + } + } +} + +void doRead() +{ + unsigned char p = 0; + int i=0; + Serial.println("Read:"); + do + { + Flash.read(flash+i, &p, 1); + Serial.write(p); + Serial.print(":"); + Serial.println(p); + } while ( p && (i++ < 16) ); + Serial.println("."); +} + + +void doErase() +{ + Serial.println("Erase"); + Flash.erase(flash); + Serial.println("Done."); +} + +void doWrite() +{ + Serial.println("Write"); + Flash.write(flash, (unsigned char*) "Hello World!" ,13); + Serial.println("Done."); +} + +void doHelp() +{ + int div = (F_CPU/400000L) & 63; + Serial.println("flash test: e, r, w"); + Serial.println(F_CPU); + Serial.println(div); +} diff --git a/msp430/libraries/MspFlash/keywords.txt b/msp430/libraries/MspFlash/keywords.txt new file mode 100644 index 0000000..72b8fc2 --- /dev/null +++ b/msp430/libraries/MspFlash/keywords.txt @@ -0,0 +1,29 @@ +#######################################
+# Syntax Coloring Map
+#######################################
+
+#######################################
+# Datatypes (KEYWORD1)
+#######################################
+
+Flash KEYWORD1
+
+#######################################
+# Methods and Functions (KEYWORD2)
+#######################################
+erase KEYWORD2
+read KEYWORD2
+write KEYWORD2
+
+
+#######################################
+# Constants (LITERAL1)
+#######################################
+SEGMENT KEYWORD2
+SEGMENT_A LITERAL1
+SEGMENT_B LITERAL1
+SEGMENT_C LITERAL1
+SEGMENT_D LITERAL1
+SEG_SIZE LITERAL1
+INFO_SIZE LITERAL1
+SEGPTR KEYWORD2
\ No newline at end of file diff --git a/msp430/libraries/SPI/SPI.cpp b/msp430/libraries/SPI/SPI.cpp new file mode 100644 index 0000000..bcb7323 --- /dev/null +++ b/msp430/libraries/SPI/SPI.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st> + * SPI Master library for arduino. + * + * This file is free software; you can redistribute it and/or modify + * it under the terms of either the GNU General Public License version 2 + * or the GNU Lesser General Public License version 2.1, both as + * published by the Free Software Foundation. + * + * 2012-04-29 rick@kimballsoftware.com - added msp430 support. + * + */ + +#include <Energia.h> + +#include "SPI.h" + +SPIClass SPI; + +void SPIClass::begin() +{ + spi_initialize(); +} + +void SPIClass::end() +{ + spi_disable(); +} + +void SPIClass::setBitOrder(uint8_t bitOrder) +{ + spi_set_bitorder(bitOrder); +} + +void SPIClass::setDataMode(uint8_t mode) +{ + spi_set_datamode(mode); +} + +void SPIClass::setClockDivider(uint8_t rate) +{ + spi_set_divisor(rate); +} diff --git a/msp430/libraries/SPI/SPI.h b/msp430/libraries/SPI/SPI.h new file mode 100644 index 0000000..28b47eb --- /dev/null +++ b/msp430/libraries/SPI/SPI.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st> + * SPI Master library for arduino. + * + * This file is free software; you can redistribute it and/or modify + * it under the terms of either the GNU General Public License version 2 + * or the GNU Lesser General Public License version 2.1, both as + * published by the Free Software Foundation. + * + * 2012-04-29 rick@kimballsoftware.com - added msp430 support. + * + */ + +#ifndef _SPI_H_INCLUDED +#define _SPI_H_INCLUDED + +#include <Energia.h> +#include <inttypes.h> + +#if defined(__MSP430_HAS_USCI__) || defined(__MSP430_HAS_USI__) +#include "utility/spi_430.h" +#endif + +#define SPI_MODE0 0 +#define SPI_MODE1 1 +#define SPI_MODE2 2 +#define SPI_MODE3 4 + +class SPIClass { +public: + inline static uint8_t transfer(uint8_t _data); + + // SPI Configuration methods + + static void begin(); // Default + static void end(); + + static void setBitOrder(uint8_t); + static void setDataMode(uint8_t); + static void setClockDivider(uint8_t); + + inline static void attachInterrupt(); + inline static void detachInterrupt(); +}; + +extern SPIClass SPI; + +uint8_t SPIClass::transfer(uint8_t _data) { + return spi_send(_data); +} + +void SPIClass::attachInterrupt() { + /* undocumented in Arduino 1.0 */ +} + +void SPIClass::detachInterrupt() { + /* undocumented in Arduino 1.0 */ +} + +#endif diff --git a/msp430/libraries/SPI/examples/BarometricPressureSensor/BarometricPressureSensor.ino b/msp430/libraries/SPI/examples/BarometricPressureSensor/BarometricPressureSensor.ino new file mode 100644 index 0000000..66568ea --- /dev/null +++ b/msp430/libraries/SPI/examples/BarometricPressureSensor/BarometricPressureSensor.ino @@ -0,0 +1,146 @@ +/* + SCP1000 Barometric Pressure Sensor Display + + Shows the output of a Barometric Pressure Sensor on a + Uses the SPI library. For details on the sensor, see: + http://www.sparkfun.com/commerce/product_info.php?products_id=8161 + http://www.vti.fi/en/support/obsolete_products/pressure_sensors/ + + This sketch adapted from Nathan Seidle's SCP1000 example for PIC: + http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip + + Circuit: + SCP1000 sensor attached to pins 6, 7, 10 - 13: + DRDY: pin 6 + CSB: pin 8 + MOSI: pin 14/15 + MISO: pin 15/14 * need to level convert this + SCK: pin 7 + + created 31 July 2010 + modified 14 August 2010 + by Tom Igoe + + modified 2 May 2012 + Rick Kimball - changed pin # for msp430 + + */ + +// the sensor communicates using SPI, so include the library: +#include <SPI.h> + +//Sensor's memory register addresses: +const int PRESSURE = 0x1F; // 3 most significant bits of pressure +const int PRESSURE_LSB = 0x20; // 16 least significant bits of pressure +const int TEMPERATURE = 0x21; // 16 bit temperature reading +const byte READ = 0b11111100; // SCP1000's read command +const byte WRITE = 0b00000010; // SCP1000's write command + +// pins used for the connection with the sensor +// the other you need are controlled by the SPI library): +const int dataReadyPin = 9; // P2.1 +const int chipSelectPin = 8; // P2.0 + +void setup() { + Serial.begin(9600); + + // start the SPI library: + SPI.begin(); + + // initialize the data ready and chip select pins: + pinMode(dataReadyPin, INPUT); + pinMode(chipSelectPin, OUTPUT); + + //Configure SCP1000 for low noise configuration: + writeRegister(0x02, 0x2D); + writeRegister(0x01, 0x03); + writeRegister(0x03, 0x02); + // give the sensor time to set up: + delay(100); +} + +void loop() { + //Select High Resolution Mode + writeRegister(0x03, 0x0A); + + // don't do anything until the data ready pin is high: + if (digitalRead(dataReadyPin) == HIGH) { + //Read the temperature data + int tempData = readRegister(0x21, 2); + + // convert the temperature to celsius and display it: + float realTemp = (float)tempData / 20.0; + Serial.print("Temp[C]="); + Serial.print(realTemp); + + + //Read the pressure data highest 3 bits: + byte pressure_data_high = readRegister(0x1F, 1); + pressure_data_high &= 0b00000111; //you only needs bits 2 to 0 + + //Read the pressure data lower 16 bits: + unsigned int pressure_data_low = readRegister(0x20, 2); + //combine the two parts into one 19-bit number: + long pressure = ((pressure_data_high << 16) | pressure_data_low)/4; + + // display the temperature: + Serial.println("\tPressure [Pa]=" + String(pressure)); + } +} + +//Read from or write to register from the SCP1000: +unsigned int readRegister(byte thisRegister, int bytesToRead ) { + byte inByte = 0; // incoming byte from the SPI + unsigned int result = 0; // result to return + Serial.print(thisRegister, BIN); + Serial.print("\t"); + // SCP1000 expects the register name in the upper 6 bits + // of the byte. So shift the bits left by two bits: + thisRegister = thisRegister << 2; + // now combine the address and the command into one byte + byte dataToSend = thisRegister & READ; + Serial.println(thisRegister, BIN); + // take the chip select low to select the device: + digitalWrite(chipSelectPin, LOW); + // send the device the register you want to read: + SPI.transfer(dataToSend); + // send a value of 0 to read the first byte returned: + result = SPI.transfer(0x00); + // decrement the number of bytes left to read: + bytesToRead--; + // if you still have another byte to read: + if (bytesToRead > 0) { + // shift the first byte left, then get the second byte: + result = result << 8; + inByte = SPI.transfer(0x00); + // combine the byte you just got with the previous one: + result = result | inByte; + // decrement the number of bytes left to read: + bytesToRead--; + } + // take the chip select high to de-select: + digitalWrite(chipSelectPin, HIGH); + // return the result: + return(result); +} + + +//Sends a write command to SCP1000 + +void writeRegister(byte thisRegister, byte thisValue) { + + // SCP1000 expects the register address in the upper 6 bits + // of the byte. So shift the bits left by two bits: + thisRegister = thisRegister << 2; + // now combine the register address and the command into one byte: + byte dataToSend = thisRegister | WRITE; + + // take the chip select low to select the device: + digitalWrite(chipSelectPin, LOW); + + SPI.transfer(dataToSend); //Send register location + SPI.transfer(thisValue); //Send value to record into register + + // take the chip select high to de-select: + digitalWrite(chipSelectPin, HIGH); +} diff --git a/msp430/libraries/SPI/examples/DigitalPotControl/DigitalPotControl.ino b/msp430/libraries/SPI/examples/DigitalPotControl/DigitalPotControl.ino new file mode 100644 index 0000000..d808a68 --- /dev/null +++ b/msp430/libraries/SPI/examples/DigitalPotControl/DigitalPotControl.ino @@ -0,0 +1,73 @@ +/* + Digital Pot Control + + This example controls an Analog Devices AD5206 digital potentiometer. + The AD5206 has 6 potentiometer channels. Each channel's pins are labeled + A - connect this to voltage + W - this is the pot's wiper, which changes when you set it + B - connect this to ground. + + The AD5206 is SPI-compatible,and to command it, you send two bytes, + one with the channel number (0 - 5) and one with the resistance value for the + channel (0 - 255). + + The circuit: + * All A pins of AD5206 connected to +5V + * All B pins of AD5206 connected to ground + * An LED and a 220-ohm resisor in series connected from each W pin to ground + * CS - to digital pin 8 (SS pin) P2.0 + * SDI - to digital pin 14/15 (MOSI pin USI/USCI) P1.6/P1.7 + * CLK - to digital pin 7 (SCK pin) P1.5 + + created 10 Aug 2010 + by Tom Igoe + + modified 2 May 2012 - changed SS pin + by Rick Kimball + + Thanks to Heather Dewey-Hagborg for the original tutorial, 2005 + +*/ + + +// include the SPI library: +#include <SPI.h> + +// set pin 8 as the slave select for the digital pot: +const int slaveSelectPin = SS; + +void setup() { + // set the slaveSelectPin as an output: + pinMode (slaveSelectPin, OUTPUT); + // initialize SPI: + SPI.begin(); +} + +void loop() { + // go through the six channels of the digital pot: + for (int channel = 0; channel < 6; channel++) { + // change the resistance on this channel from min to max: + for (int level = 0; level < 255; level++) { + digitalPotWrite(channel, level); + delay(10); + } + // wait a second at the top: + delay(100); + // change the resistance on this channel from max to min: + for (int level = 0; level < 255; level++) { + digitalPotWrite(channel, 255 - level); + delay(10); + } + } + +} + +int digitalPotWrite(int address, int value) { + // take the SS pin low to select the chip: + digitalWrite(slaveSelectPin,LOW); + // send in the address and value via SPI: + SPI.transfer(address); + SPI.transfer(value); + // take the SS pin high to de-select the chip: + digitalWrite(slaveSelectPin,HIGH); +}
\ No newline at end of file diff --git a/msp430/libraries/SPI/keywords.txt b/msp430/libraries/SPI/keywords.txt new file mode 100644 index 0000000..fa76165 --- /dev/null +++ b/msp430/libraries/SPI/keywords.txt @@ -0,0 +1,36 @@ +####################################### +# Syntax Coloring Map SPI +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +SPI KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### +begin KEYWORD2 +end KEYWORD2 +transfer KEYWORD2 +setBitOrder KEYWORD2 +setDataMode KEYWORD2 +setClockDivider KEYWORD2 + + +####################################### +# Constants (LITERAL1) +####################################### +SPI_CLOCK_DIV4 LITERAL1 +SPI_CLOCK_DIV16 LITERAL1 +SPI_CLOCK_DIV64 LITERAL1 +SPI_CLOCK_DIV128 LITERAL1 +SPI_CLOCK_DIV2 LITERAL1 +SPI_CLOCK_DIV8 LITERAL1 +SPI_CLOCK_DIV32 LITERAL1 +SPI_CLOCK_DIV64 LITERAL1 +SPI_MODE0 LITERAL1 +SPI_MODE1 LITERAL1 +SPI_MODE2 LITERAL1 +SPI_MODE3 LITERAL1
\ No newline at end of file diff --git a/msp430/libraries/SPI/utility/spi_430.h b/msp430/libraries/SPI/utility/spi_430.h new file mode 100644 index 0000000..262759a --- /dev/null +++ b/msp430/libraries/SPI/utility/spi_430.h @@ -0,0 +1,50 @@ +/*
+ * spi_430.h - common function declarations for different SPI implementations
+ *
+ * Copyright (c) 2012 by Rick Kimball <rick@kimballsoftware.com>
+ * spi abstraction api for msp430
+ *
+ * This file is free software; you can redistribute it and/or modify
+ * it under the terms of either the GNU General Public License version 2
+ * or the GNU Lesser General Public License version 2.1, both as
+ * published by the Free Software Foundation.
+ *
+ */
+
+#ifndef _SPI_430_H_
+#define _SPI_430_H_
+
+#if defined(__MSP430_HAS_USCI__)
+
+#define SPI_CLOCK_DIV1 1
+#define SPI_CLOCK_DIV2 2
+#define SPI_CLOCK_DIV4 4
+#define SPI_CLOCK_DIV8 8
+#define SPI_CLOCK_DIV16 16
+#define SPI_CLOCK_DIV32 32
+#define SPI_CLOCK_DIV64 64
+#define SPI_CLOCK_DIV128 128
+
+#elif defined(__MSP430_HAS_USI__)
+
+#define SPI_CLOCK_DIV1 0
+#define SPI_CLOCK_DIV2 USIDIV_1
+#define SPI_CLOCK_DIV4 USIDIV_2
+#define SPI_CLOCK_DIV8 USIDIV_3
+#define SPI_CLOCK_DIV16 USIDIV_4
+#define SPI_CLOCK_DIV32 USIDIV_5
+#define SPI_CLOCK_DIV64 USIDIV_6
+#define SPI_CLOCK_DIV128 USIDIV_7
+
+#else
+ #error "SPI not supported by hardware on this chip"
+#endif
+
+void spi_initialize(void);
+void spi_disable(void);
+uint8_t spi_send(const uint8_t);
+void spi_set_bitorder(const uint8_t);
+void spi_set_datamode(const uint8_t);
+void spi_set_divisor(const uint16_t clkdivider);
+
+#endif /*_SPI_430_H_*/
diff --git a/msp430/libraries/SPI/utility/usci_spi.cpp b/msp430/libraries/SPI/utility/usci_spi.cpp new file mode 100644 index 0000000..25d7157 --- /dev/null +++ b/msp430/libraries/SPI/utility/usci_spi.cpp @@ -0,0 +1,133 @@ +/**
+ * File: usci_spi.c - msp430 USCI SPI implementation
+ *
+ * Copyright (c) 2012 by Rick Kimball <rick@kimballsoftware.com>
+ * spi abstraction api for msp430
+ *
+ * This file is free software; you can redistribute it and/or modify
+ * it under the terms of either the GNU General Public License version 2
+ * or the GNU Lesser General Public License version 2.1, both as
+ * published by the Free Software Foundation.
+ *
+ */
+
+#include <msp430.h>
+#include <stdint.h>
+#include "spi_430.h"
+
+#ifdef __MSP430_HAS_USCI__
+
+/**
+ * USCI flags for various the SPI MODEs
+ *
+ * Note: The msp430 UCCKPL tracks the CPOL value. However,
+ * the UCCKPH flag is inverted when compared to the CPHA
+ * value described in Motorola documentation.
+ */
+
+#define SPI_MODE_0 (UCCKPH) /* CPOL=0 CPHA=0 */
+#define SPI_MODE_1 (0) /* CPOL=0 CPHA=1 */
+#define SPI_MODE_2 (UCCKPL | UCCKPH) /* CPOL=1 CPHA=0 */
+#define SPI_MODE_3 (UCCKPL) /* CPOL=1 CPHA=1 */
+
+#define SPI_MODE_MASK (UCCKPL | UCCKPH)
+
+/**
+ * spi_initialize() - Configure USCI UCB0 for SPI mode
+ *
+ * P2.0 - CS (active low)
+ * P1.5 - SCLK
+ * P1.6 - MISO aka SOMI
+ * P1.7 - MOSI aka SIMO
+ *
+ */
+void spi_initialize(void)
+{
+ UCB0CTL1 = UCSWRST | UCSSEL_2; // Put USCI in reset mode, source USCI clock from SMCLK
+ UCB0CTL0 = SPI_MODE_0 | UCMSB | UCSYNC | UCMST; // Use SPI MODE 0 - CPOL=0 CPHA=0
+
+ P1OUT |= (BIT5 | BIT7); // SPI output pins low
+ P1SEL |= BIT5 | BIT6 | BIT7; // configure P1.5, P1.6, P1.7 for USCI
+ P1SEL2 |= BIT5 | BIT6 | BIT7; // more required SPI configuration
+
+ UCB0BR0 = SPI_CLOCK_DIV4 & 0xFF; // set initial speed to 4MHz
+ UCB0BR1 = (SPI_CLOCK_DIV4 >> 8 ) & 0xFF;
+
+ UCB0CTL1 &= ~UCSWRST; // release USCI for operation
+}
+
+/**
+ * spi_disable() - put USCI into reset mode
+ */
+void spi_disable(void)
+{
+ UCB0CTL1 |= UCSWRST; // Put USCI in reset mode
+}
+
+/**
+ * spi_send() - send a byte and recv response
+ */
+uint8_t spi_send(const uint8_t _data)
+{
+ while (!(UC0IFG & UCB0TXIFG))
+ ; // wait for previous tx to complete
+
+ UCB0TXBUF = _data; // setting TXBUF clears the TXIFG flag
+
+ while (!(UC0IFG & UCB0RXIFG))
+ ; // wait for an rx character?
+
+ return UCB0RXBUF; // reading clears RXIFG flag
+}
+
+/***SPI_MODE_0
+ * spi_set_divisor() - set new clock divider for USCI
+ *
+ * USCI speed is based on the SMCLK divided by BR0 and BR1
+ *
+ */
+void spi_set_divisor(const uint16_t clkdiv)
+{
+ UCB0CTL1 |= UCSWRST; // go into reset state
+ UCB0BR0 = clkdiv & 0xFF;
+ UCB0BR1 = (clkdiv >> 8 ) & 0xFF;
+ UCB0CTL1 &= ~UCSWRST; // release for operation
+}
+
+/**
+ * spi_set_bitorder(LSBFIRST=0 | MSBFIRST=1)
+ */
+void spi_set_bitorder(const uint8_t order)
+{
+ UCB0CTL1 |= UCSWRST; // go into reset state
+ UCB0CTL0 = (UCB0CTL0 & ~UCMSB) | ((order == 1 /*MSBFIRST*/) ? UCMSB : 0); /* MSBFIRST = 1 */
+ UCB0CTL1 &= ~UCSWRST; // release for operation
+}
+
+/**
+ * spi_set_datamode() - mode 0 - 3
+ */
+void spi_set_datamode(const uint8_t mode)
+{
+ UCB0CTL1 |= UCSWRST; // go into reset state
+ switch(mode) {
+ case 0: /* SPI_MODE0 */
+ UCB0CTL0 = (UCB0CTL0 & ~SPI_MODE_MASK) | SPI_MODE_0;
+ break;
+ case 1: /* SPI_MODE1 */
+ UCB0CTL0 = (UCB0CTL0 & ~SPI_MODE_MASK) | SPI_MODE_1;
+ break;
+ case 2: /* SPI_MODE2 */
+ UCB0CTL0 = (UCB0CTL0 & ~SPI_MODE_MASK) | SPI_MODE_2;
+ break;
+ case 4: /* SPI_MODE3 */
+ UCB0CTL0 = (UCB0CTL0 & ~SPI_MODE_MASK) | SPI_MODE_3;
+ break;
+ default:
+ break;
+ }
+ UCB0CTL1 &= ~UCSWRST; // release for operation
+}
+#else
+ //#error "Error! This device doesn't have a USCI peripheral"
+#endif
diff --git a/msp430/libraries/SPI/utility/usi_spi.cpp b/msp430/libraries/SPI/utility/usi_spi.cpp new file mode 100644 index 0000000..a03fd6a --- /dev/null +++ b/msp430/libraries/SPI/utility/usi_spi.cpp @@ -0,0 +1,164 @@ +/**
+ * File: usi_spi.c - msp430 USI SPI implementation
+ *
+ * Copyright (c) 2012 by Rick Kimball <rick@kimballsoftware.com>
+ * spi abstraction api for msp430
+ *
+ * This file is free software; you can redistribute it and/or modify
+ * it under the terms of either the GNU General Public License version 2
+ * or the GNU Lesser General Public License version 2.1, both as
+ * published by the Free Software Foundation.
+ *
+ * 07-14-2012 - rick@kimballsoftware.com
+ * Fixed MODE2/MODE3 phase problems. Added logic to deal with USI5 errata.
+ */
+
+#include <msp430.h>
+#include <stdint.h>
+#include "spi_430.h"
+
+#ifdef __MSP430_HAS_USI__
+/**
+ * USI flags for various the SPI MODEs
+ *
+ * Note: The msp430 USICKPL tracks the CPOL value. However,
+ * the USICKPH flag is inverted when compared to the CPHA
+ * value described in Motorola documentation.
+ */
+#define SPI_DIV_MASK (USIDIV0 | USIDIV1 | USIDIV2)
+#define SPI_LSBMSB_MASK (USILSB)
+
+/* deal with USI5 errata see: document slaz061b */
+enum USI5 {
+ USI5_NO_ADJUST=0,
+ USI5_ADJUST=1,
+ USI5_SENT=2,
+};
+static enum USI5 bResetAdjust;
+
+/**
+ * spi_initialize() - Configure USI for SPI mode
+ *
+ * P2.0 - CS (active low)
+ * P1.5 - SCLK
+ * P1.6 - MOSI aka SIMO
+ * P1.7 - MISO aka SOMI
+ */
+
+void spi_initialize(void)
+{
+ USICTL0 |= USISWRST; // put USI in reset mode, source USI clock from SMCLK
+ USICTL0 |= USIPE5 | USIPE6 | USIPE7 | USIMST | USIOE;
+ USICKCTL |= USIDIV_2 | USISSEL_2; // default speed 4MHz 16MHz/4
+ USICTL1 = USICKPH; // SPI_MODE_0
+
+ P1OUT |= BIT5 | BIT6; // SPI OUTPUT PINS LOW
+ P1DIR = (P1DIR & ~BIT7) | BIT5 | BIT6; // configure P1.5, P1.6, P1.7 for USI
+
+ USICTL0 &= ~USISWRST; // release USI for operation
+
+ bResetAdjust = USI5_ADJUST;
+}
+
+void spi_disable(void) {
+ USICTL0 |= USISWRST; // put USI in reset mode
+}
+
+/**
+ * spi_send() - send a byte and recv response
+ */
+uint8_t spi_send(const uint8_t _data)
+{
+ USISRL = _data;
+
+ // SPI master generates one additional clock after module reset if USICKPH is set.
+ if ( bResetAdjust == USI5_ADJUST ) {
+ USICNT=7; // adjust first time send
+ bResetAdjust = USI5_SENT;
+ }
+ else {
+ USICNT = 8;
+ }
+
+ while (!(USICTL1 & USIIFG)) {
+ ; // wait for an USICNT to decrement to 0
+ }
+
+ return USISRL; // reading clears RXIFG flag
+}
+
+/**
+ * spi_set_divisor() - set new clock divider for USI
+ *
+ * There are a fixed set of valid values for clock divisors
+ * see the slau144 for details. DIV by 2/4/8 .. 128
+ */
+
+void spi_set_divisor(const uint16_t clkdiv)
+{
+ USICTL0 |= USISWRST; // put USI in reset mode
+ USICKCTL = (USICKCTL & ~SPI_DIV_MASK) | clkdiv;
+ USICTL0 &= ~USISWRST; // release for operation
+}
+
+/**
+ * spi_set_bitorder (enum LSBFIRST=0|MSBFIRST=1)
+ *
+ * Note: this should use the LSBFIRST/MSBFIRST defines however
+ * it doesn't to allow this code to compile without Energia.
+ *
+ */
+void spi_set_bitorder(const uint8_t order)
+{
+ USICTL0 |= USISWRST; // put USI in reset mode
+ USICTL0 = (USICTL0 & ~SPI_LSBMSB_MASK) | ((order == 1 /*MSBFIRST*/) ? 0 : USILSB); /* MSBFIRST = 1 */
+ USICTL0 &= ~USISWRST; // release for operation
+}
+
+/**
+ * spi_set_datamode() - Motorola SPI Mode 0 ... 3
+ *
+ * mode is really an enum SPI_MODE0 ... SPI_MODE3 as defined in
+ * the Energia header file.
+ */
+void spi_set_datamode(const uint8_t mode)
+{
+ USICTL0 |= USISWRST; // put USI in reset mode while we make changes
+ switch(mode) {
+ case 0: /* SPI_MODE0 */
+ USICKCTL &= ~USICKPL; /* CPOL=0 */
+ USICTL1 |= USICKPH; /* CPHA=0 */
+ break;
+
+ case 1: /* SPI_MODE1 */
+ USICKCTL &= ~USICKPL; /* CPOL=0 */
+ USICTL1 &= ~USICKPH; /* CPHA=1 */
+ break;
+
+ case 2: /* SPI_MODE2 */
+ USICKCTL |= USICKPL; /* CPOL=1 */
+ USICTL1 |= USICKPH; /* CPHA=0 */
+ break;
+
+ case 4: /* SPI_MODE3 */
+ USICKCTL |= USICKPL; /* CPOL=1 */
+ USICTL1 &= ~USICKPH; /* CPHA=1 */
+ break;
+
+ default:
+ break;
+ }
+ USICTL0 &= ~USISWRST; // release for operation
+
+ if ( USICTL1 & USICKPH ) {
+ if ( bResetAdjust != USI5_SENT ) {
+ bResetAdjust = USI5_ADJUST;
+ }
+ }
+ else {
+ bResetAdjust = USI5_NO_ADJUST;
+ }
+}
+#else
+ //#warning "Error! This device doesn't have a USI peripheral"
+#endif
diff --git a/msp430/libraries/Servo/Servo.cpp b/msp430/libraries/Servo/Servo.cpp new file mode 100644 index 0000000..8853dd9 --- /dev/null +++ b/msp430/libraries/Servo/Servo.cpp @@ -0,0 +1,207 @@ +/*
+ Servo.cpp - Interrupt driven Servo library for MSP430
+ Copyright (c) 2012 Petr Baudis. All right reserved.
+ Modified by Peter Brier 26-6-2012: Fixed timing/IRQ problem
+
+ Derived from:
+ Servo.cpp - Interrupt driven Servo library for Arduino using 16 bit timers- Version 2
+ Copyright (c) 2009 Michael Margolis. 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
+ */
+
+#include "Energia.h"
+#include "Servo.h"
+
+#define F_TIMER (F_CPU/8L)
+#define usToTicks(_us) (( clockCyclesPerMicrosecond()* _us) / 8) // converts microseconds to timer ticks (assumes prescale of 8)
+#define ticksToUs(_ticks) (( (unsigned)_ticks * 8)/ clockCyclesPerMicrosecond() ) // converts from ticks back to microseconds
+
+#define TRIM_DURATION 2 // compensation ticks to trim adjust for digitalWrite delays // 12 August 2009
+
+
+#define SERVO_MIN() (MIN_PULSE_WIDTH - this->min * 4) // minimum value in uS for this servo
+#define SERVO_MAX() (MAX_PULSE_WIDTH - this->max * 4) // maximum value in uS for this servo
+
+
+/************ static functions and data structures common to all instances ***********************/
+
+
+static servo_t servos[MAX_SERVOS]; // static array of servo structures
+static unsigned int ServoCount = 0; // the total number of attached servos
+
+static volatile int counter = 0; // Servo counter; -1 before first servo starts being serviced
+static volatile unsigned int totalWait = 0; // Total amount waited so far in the current period; after all servos, wait for the rest of REFRESH_INTERVAL
+
+#ifndef TIMERA0_VECTOR
+#define TIMERA0_VECTOR TIMER0_A0_VECTOR
+#endif /* TIMER0_A0_VECTOR */
+
+// Timer A0 interrupt service routine
+static void
+Timer_A(void)
+{
+ static unsigned long wait;
+ if (counter >= 0) {
+ /* Turn pulse off. */
+ digitalWrite(servos[counter].Pin.nbr, LOW);
+ }
+
+ /* Service next servo, while skipping any inactive servo records. */
+ do {
+ counter++;
+ } while (!servos[counter].Pin.isActive && counter < ServoCount);
+
+ // Counter range is 0-ServoCount, the last count is used to complete the REFRESH_INTERVAL
+ if (counter < ServoCount) {
+ /* Turn pulse on for the next servo. */
+ digitalWrite(servos[counter].Pin.nbr, HIGH);
+ /* And hold! */
+ totalWait += servos[counter].ticks;
+ CCR0 = servos[counter].ticks;
+ } else {
+ /* Wait for the remaining of REFRESH_INTERVAL. */
+ wait = usToTicks(REFRESH_INTERVAL) - totalWait;
+ totalWait = 0;
+ CCR0 = (wait < 1000 ? 1000 : wait);
+ counter = -1;
+ }
+}
+
+// Timer A0 interrupt service routine
+__attribute__((interrupt(TIMERA0_VECTOR)))
+static void
+Timer_A_int(void)
+{
+ Timer_A();
+}
+
+static boolean isTimerActive(void)
+{
+ // returns true if any servo is active
+ for(int i = 0; i < MAX_SERVOS; i++)
+ if (servos[i].Pin.isActive == true)
+ return true;
+ return false;
+}
+
+static void enableTimer(void)
+{
+ counter = -1;
+ totalWait = 0;
+
+ Timer_A(); // enable first servo
+
+ CCTL0 = CCIE; // CCR0 interrupt enabled
+ TACTL = TASSEL_2 + MC_1 + ID_3; // prescale SMCLK/8, upmode
+}
+
+static void disableTimer(void)
+{
+ // disable interrupt
+ CCTL0 = 0;
+ CCR0 = 0;
+}
+
+
+/****************** end of static functions ******************************/
+
+Servo::Servo()
+{
+ if( ServoCount < MAX_SERVOS) {
+ this->servoIndex = ServoCount++; // assign a servo index to this instance
+ servos[this->servoIndex].ticks = usToTicks(DEFAULT_PULSE_WIDTH); // store default values
+ }
+ else
+ this->servoIndex = INVALID_SERVO ; // too many servos
+}
+
+uint8_t Servo::attach(int pin)
+{
+ return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
+}
+
+uint8_t Servo::attach(int pin, int min, int max)
+{
+ if(this->servoIndex < MAX_SERVOS ) {
+ pinMode( pin, OUTPUT) ; // set servo pin to output
+ servos[this->servoIndex].Pin.nbr = pin;
+
+ // todo min/max check: abs(min - MIN_PULSE_WIDTH) /4 < 128
+ this->min = (MIN_PULSE_WIDTH - min)/4; //resolution of min/max is 4 uS
+ this->max = (MAX_PULSE_WIDTH - max)/4;
+
+ boolean timer_active = isTimerActive();
+ servos[this->servoIndex].Pin.isActive = true;
+ if (!timer_active)
+ enableTimer();
+ }
+ return this->servoIndex ;
+}
+
+void Servo::detach()
+{
+ servos[this->servoIndex].Pin.isActive = false;
+ if (!isTimerActive())
+ disableTimer();
+}
+
+void Servo::write(int value)
+{
+ if(value < MIN_PULSE_WIDTH)
+ { // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
+ if(value < 0) value = 0;
+ if(value > 180) value = 180;
+ value = map(value, 0, 180, SERVO_MIN(), SERVO_MAX());
+ }
+ this->writeMicroseconds(value);
+}
+
+void Servo::writeMicroseconds(int value)
+{
+ // calculate and store the values for the given channel
+ byte channel = this->servoIndex;
+ if( (channel < MAX_SERVOS) ) // ensure channel is valid
+ {
+ if( value < SERVO_MIN() ) // ensure pulse width is valid
+ value = SERVO_MIN();
+ else if( value > SERVO_MAX() )
+ value = SERVO_MAX();
+ value = value - TRIM_DURATION;
+ value = usToTicks(value); // convert to ticks after compensating for interrupt overhead - 12 Aug 2009
+ servos[channel].ticks = value; // this is atomic on a 16bit uC, no need to disable Interrupts
+ }
+}
+
+int Servo::read() // return the value as degrees
+{
+ return map( this->readMicroseconds()+1, SERVO_MIN(), SERVO_MAX(), 0, 180);
+}
+
+int Servo::readMicroseconds()
+{
+ unsigned int pulsewidth;
+ if( this->servoIndex != INVALID_SERVO )
+ pulsewidth = ticksToUs(servos[this->servoIndex].ticks) + TRIM_DURATION;
+ else
+ pulsewidth = 0;
+
+ return pulsewidth;
+}
+
+bool Servo::attached()
+{
+ return servos[this->servoIndex].Pin.isActive ;
+}
diff --git a/msp430/libraries/Servo/Servo.h b/msp430/libraries/Servo/Servo.h new file mode 100644 index 0000000..27a6a31 --- /dev/null +++ b/msp430/libraries/Servo/Servo.h @@ -0,0 +1,83 @@ +/* + Servo.h - Interrupt driven Servo library for MSP430 + Copyright (c) 2012 Petr Baudis. All right reserved. + Modified by Peter Brier 26-6-2012: Fixed timing/IRQ problem + + Derived from: + Servo.h - Interrupt driven Servo library for Arduino using 16 bit timers- Version 2 + Copyright (c) 2009 Michael Margolis. 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 Servo_H +#define Servo_H + +#include <inttypes.h> + +/* + This version attempts to be compatible with the Arduino Servo library, + but supports only a single timer. So do not add too many servos. :-) + */ + +/* + Note that the Servo uses TIMER0_A, which cannot be used for other + purposes, e.g. for TimerSerial. + + XXX: Also, some pins reused by TIMER0_A may be affected by this + library, but I do not understand the datasheet enough regarding this. + Most likely, you should avoid PWM on pin 2? (And other stuff too?) + */ + +#define Servo_VERSION 2 // software version of this library + +#define MIN_PULSE_WIDTH 544 // the shortest pulse sent to a servo [uS] +#define MAX_PULSE_WIDTH 2400 // the longest pulse sent to a servo [uS] +#define DEFAULT_PULSE_WIDTH 1500 // default pulse width when servo is attached +#define REFRESH_INTERVAL 20000 // servos refresh period in microseconds + +#define MAX_SERVOS 8 + +#define INVALID_SERVO 255 // flag indicating an invalid servo index + +typedef struct { + uint8_t nbr :6 ; // a pin number from 0 to 63 + uint8_t isActive :1 ; // true if this channel is enabled, pin not pulsed if false +} ServoPin_t; + +typedef struct { + ServoPin_t Pin; + unsigned int ticks; +} servo_t; + +class Servo +{ +public: + Servo(); + uint8_t attach(int pin); // attach the given pin to the next free channel, sets pinMode, returns channel number or 0 if failure + uint8_t attach(int pin, int min, int max); // as above but also sets min and max values for writes. + void detach(); + void write(int value); // if value is < 200 its treated as an angle, otherwise as pulse width in microseconds + void writeMicroseconds(int value); // Write pulse width in microseconds + int read(); // returns current pulse width as an angle between 0 and 180 degrees + int readMicroseconds(); // returns current pulse width in microseconds for this servo (was read_us() in first release) + bool attached(); // return true if this servo is attached, otherwise false +private: + uint8_t servoIndex; // index into the channel data for this servo + int8_t min; // minimum is this value times 4 added to MIN_PULSE_WIDTH + int8_t max; // maximum is this value times 4 added to MAX_PULSE_WIDTH +}; + +#endif diff --git a/msp430/libraries/Servo/examples/Knob/Knob.ino b/msp430/libraries/Servo/examples/Knob/Knob.ino new file mode 100644 index 0000000..886e107 --- /dev/null +++ b/msp430/libraries/Servo/examples/Knob/Knob.ino @@ -0,0 +1,22 @@ +// Controlling a servo position using a potentiometer (variable resistor) +// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott> + +#include <Servo.h> + +Servo myservo; // create servo object to control a servo + +int potpin = 0; // analog pin used to connect the potentiometer +int val; // variable to read the value from the analog pin + +void setup() +{ + myservo.attach(9); // attaches the servo on pin 9 to the servo object +} + +void loop() +{ + val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) + val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180) + myservo.write(val); // sets the servo position according to the scaled value + delay(15); // waits for the servo to get there +} diff --git a/msp430/libraries/Servo/examples/Sweep/Sweep.ino b/msp430/libraries/Servo/examples/Sweep/Sweep.ino new file mode 100644 index 0000000..fb326e7 --- /dev/null +++ b/msp430/libraries/Servo/examples/Sweep/Sweep.ino @@ -0,0 +1,31 @@ +// Sweep +// by BARRAGAN <http://barraganstudio.com> +// This example code is in the public domain. + + +#include <Servo.h> + +Servo myservo; // create servo object to control a servo + // a maximum of eight servo objects can be created + +int pos = 0; // variable to store the servo position + +void setup() +{ + myservo.attach(9); // attaches the servo on pin 9 to the servo object +} + + +void loop() +{ + for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees + { // in steps of 1 degree + myservo.write(pos); // tell servo to go to position in variable 'pos' + delay(15); // waits 15ms for the servo to reach the position + } + for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees + { + myservo.write(pos); // tell servo to go to position in variable 'pos' + delay(15); // waits 15ms for the servo to reach the position + } +} diff --git a/msp430/libraries/Servo/keywords.txt b/msp430/libraries/Servo/keywords.txt new file mode 100644 index 0000000..ca5ba79 --- /dev/null +++ b/msp430/libraries/Servo/keywords.txt @@ -0,0 +1,24 @@ +####################################### +# Syntax Coloring Map Servo +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +Servo KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### +attach KEYWORD2 +detach KEYWORD2 +write KEYWORD2 +read KEYWORD2 +attached KEYWORD2 +writeMicroseconds KEYWORD2 +readMicroseconds KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### diff --git a/msp430/libraries/Stepper/Stepper.cpp b/msp430/libraries/Stepper/Stepper.cpp new file mode 100644 index 0000000..5d6b5e5 --- /dev/null +++ b/msp430/libraries/Stepper/Stepper.cpp @@ -0,0 +1,220 @@ +/* + Stepper.cpp - - Stepper library for Wiring/Arduino - Version 0.4 + + Original library (0.1) by Tom Igoe. + Two-wire modifications (0.2) by Sebastian Gassner + Combination version (0.3) by Tom Igoe and David Mellis + Bug fix for four-wire (0.4) by Tom Igoe, bug fix from Noah Shibley + + Drives a unipolar or bipolar stepper motor using 2 wires or 4 wires + + When wiring multiple stepper motors to a microcontroller, + you quickly run out of output pins, with each motor requiring 4 connections. + + By making use of the fact that at any time two of the four motor + coils are the inverse of the other two, the number of + control connections can be reduced from 4 to 2. + + A slightly modified circuit around a Darlington transistor array or an L293 H-bridge + connects to only 2 microcontroler pins, inverts the signals received, + and delivers the 4 (2 plus 2 inverted ones) output signals required + for driving a stepper motor. + + The sequence of control signals for 4 control wires is as follows: + + Step C0 C1 C2 C3 + 1 1 0 1 0 + 2 0 1 1 0 + 3 0 1 0 1 + 4 1 0 0 1 + + The sequence of controls signals for 2 control wires is as follows + (columns C1 and C2 from above): + + Step C0 C1 + 1 0 1 + 2 1 1 + 3 1 0 + 4 0 0 + + The circuits can be found at + +http://www.arduino.cc/en/Tutorial/Stepper + + + */ + + +#include "Arduino.h" +#include "Stepper.h" + +/* + * two-wire constructor. + * Sets which wires should control the motor. + */ +Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2) +{ + this->step_number = 0; // which step the motor is on + this->speed = 0; // the motor speed, in revolutions per minute + this->direction = 0; // motor direction + this->last_step_time = 0; // time stamp in ms of the last step taken + this->number_of_steps = number_of_steps; // total number of steps for this motor + + // Arduino pins for the motor control connection: + this->motor_pin_1 = motor_pin_1; + this->motor_pin_2 = motor_pin_2; + + // setup the pins on the microcontroller: + pinMode(this->motor_pin_1, OUTPUT); + pinMode(this->motor_pin_2, OUTPUT); + + // When there are only 2 pins, set the other two to 0: + this->motor_pin_3 = 0; + this->motor_pin_4 = 0; + + // pin_count is used by the stepMotor() method: + this->pin_count = 2; +} + + +/* + * constructor for four-pin version + * Sets which wires should control the motor. + */ + +Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, int motor_pin_3, int motor_pin_4) +{ + this->step_number = 0; // which step the motor is on + this->speed = 0; // the motor speed, in revolutions per minute + this->direction = 0; // motor direction + this->last_step_time = 0; // time stamp in ms of the last step taken + this->number_of_steps = number_of_steps; // total number of steps for this motor + + // Arduino pins for the motor control connection: + this->motor_pin_1 = motor_pin_1; + this->motor_pin_2 = motor_pin_2; + this->motor_pin_3 = motor_pin_3; + this->motor_pin_4 = motor_pin_4; + + // setup the pins on the microcontroller: + pinMode(this->motor_pin_1, OUTPUT); + pinMode(this->motor_pin_2, OUTPUT); + pinMode(this->motor_pin_3, OUTPUT); + pinMode(this->motor_pin_4, OUTPUT); + + // pin_count is used by the stepMotor() method: + this->pin_count = 4; +} + +/* + Sets the speed in revs per minute + +*/ +void Stepper::setSpeed(long whatSpeed) +{ + this->step_delay = 60L * 1000L / this->number_of_steps / whatSpeed; +} + +/* + Moves the motor steps_to_move steps. If the number is negative, + the motor moves in the reverse direction. + */ +void Stepper::step(int steps_to_move) +{ + int steps_left = abs(steps_to_move); // how many steps to take + + // determine direction based on whether steps_to_mode is + or -: + if (steps_to_move > 0) {this->direction = 1;} + if (steps_to_move < 0) {this->direction = 0;} + + + // decrement the number of steps, moving one step each time: + while(steps_left > 0) { + // move only if the appropriate delay has passed: + if (millis() - this->last_step_time >= this->step_delay) { + // get the timeStamp of when you stepped: + this->last_step_time = millis(); + // increment or decrement the step number, + // depending on direction: + if (this->direction == 1) { + this->step_number++; + if (this->step_number == this->number_of_steps) { + this->step_number = 0; + } + } + else { + if (this->step_number == 0) { + this->step_number = this->number_of_steps; + } + this->step_number--; + } + // decrement the steps left: + steps_left--; + // step the motor to step number 0, 1, 2, or 3: + stepMotor(this->step_number % 4); + } + } +} + +/* + * Moves the motor forward or backwards. + */ +void Stepper::stepMotor(int thisStep) +{ + if (this->pin_count == 2) { + switch (thisStep) { + case 0: /* 01 */ + digitalWrite(motor_pin_1, LOW); + digitalWrite(motor_pin_2, HIGH); + break; + case 1: /* 11 */ + digitalWrite(motor_pin_1, HIGH); + digitalWrite(motor_pin_2, HIGH); + break; + case 2: /* 10 */ + digitalWrite(motor_pin_1, HIGH); + digitalWrite(motor_pin_2, LOW); + break; + case 3: /* 00 */ + digitalWrite(motor_pin_1, LOW); + digitalWrite(motor_pin_2, LOW); + break; + } + } + if (this->pin_count == 4) { + switch (thisStep) { + case 0: // 1010 + digitalWrite(motor_pin_1, HIGH); + digitalWrite(motor_pin_2, LOW); + digitalWrite(motor_pin_3, HIGH); + digitalWrite(motor_pin_4, LOW); + break; + case 1: // 0110 + digitalWrite(motor_pin_1, LOW); + digitalWrite(motor_pin_2, HIGH); + digitalWrite(motor_pin_3, HIGH); + digitalWrite(motor_pin_4, LOW); + break; + case 2: //0101 + digitalWrite(motor_pin_1, LOW); + digitalWrite(motor_pin_2, HIGH); + digitalWrite(motor_pin_3, LOW); + digitalWrite(motor_pin_4, HIGH); + break; + case 3: //1001 + digitalWrite(motor_pin_1, HIGH); + digitalWrite(motor_pin_2, LOW); + digitalWrite(motor_pin_3, LOW); + digitalWrite(motor_pin_4, HIGH); + break; + } + } +} + +/* + version() returns the version of the library: +*/ +int Stepper::version(void) +{ + return 4; +} diff --git a/msp430/libraries/Stepper/Stepper.h b/msp430/libraries/Stepper/Stepper.h new file mode 100644 index 0000000..4094aee --- /dev/null +++ b/msp430/libraries/Stepper/Stepper.h @@ -0,0 +1,83 @@ +/* + Stepper.h - - Stepper library for Wiring/Arduino - Version 0.4 + + Original library (0.1) by Tom Igoe. + Two-wire modifications (0.2) by Sebastian Gassner + Combination version (0.3) by Tom Igoe and David Mellis + Bug fix for four-wire (0.4) by Tom Igoe, bug fix from Noah Shibley + + Drives a unipolar or bipolar stepper motor using 2 wires or 4 wires + + When wiring multiple stepper motors to a microcontroller, + you quickly run out of output pins, with each motor requiring 4 connections. + + By making use of the fact that at any time two of the four motor + coils are the inverse of the other two, the number of + control connections can be reduced from 4 to 2. + + A slightly modified circuit around a Darlington transistor array or an L293 H-bridge + connects to only 2 microcontroler pins, inverts the signals received, + and delivers the 4 (2 plus 2 inverted ones) output signals required + for driving a stepper motor. + + The sequence of control signals for 4 control wires is as follows: + + Step C0 C1 C2 C3 + 1 1 0 1 0 + 2 0 1 1 0 + 3 0 1 0 1 + 4 1 0 0 1 + + The sequence of controls signals for 2 control wires is as follows + (columns C1 and C2 from above): + + Step C0 C1 + 1 0 1 + 2 1 1 + 3 1 0 + 4 0 0 + + The circuits can be found at + http://www.arduino.cc/en/Tutorial/Stepper +*/ + +// ensure this library description is only included once +#ifndef Stepper_h +#define Stepper_h + +// library interface description +class Stepper { + public: + // constructors: + Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2); + Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, int motor_pin_3, int motor_pin_4); + + // speed setter method: + void setSpeed(long whatSpeed); + + // mover method: + void step(int number_of_steps); + + int version(void); + + private: + void stepMotor(int this_step); + + int direction; // Direction of rotation + int speed; // Speed in RPMs + unsigned long step_delay; // delay between steps, in ms, based on speed + int number_of_steps; // total number of steps this motor can take + int pin_count; // whether you're driving the motor with 2 or 4 pins + int step_number; // which step the motor is on + + // motor pin numbers: + int motor_pin_1; + int motor_pin_2; + int motor_pin_3; + int motor_pin_4; + + long last_step_time; // time stamp in ms of when the last step was taken +}; + +#endif + diff --git a/msp430/libraries/Stepper/examples/MotorKnob/MotorKnob.ino b/msp430/libraries/Stepper/examples/MotorKnob/MotorKnob.ino new file mode 100644 index 0000000..d428186 --- /dev/null +++ b/msp430/libraries/Stepper/examples/MotorKnob/MotorKnob.ino @@ -0,0 +1,41 @@ +/* + * MotorKnob + * + * A stepper motor follows the turns of a potentiometer + * (or other sensor) on analog input 0. + * + * http://www.arduino.cc/en/Reference/Stepper + * This example code is in the public domain. + */ + +#include <Stepper.h> + +// change this to the number of steps on your motor +#define STEPS 100 + +// create an instance of the stepper class, specifying +// the number of steps of the motor and the pins it's +// attached to +Stepper stepper(STEPS, 8, 9, 10, 11); + +// the previous reading from the analog input +int previous = 0; + +void setup() +{ + // set the speed of the motor to 30 RPMs + stepper.setSpeed(30); +} + +void loop() +{ + // get the sensor value + int val = analogRead(0); + + // move a number of steps equal to the change in the + // sensor reading + stepper.step(val - previous); + + // remember the previous value of the sensor + previous = val; +}
\ No newline at end of file diff --git a/msp430/libraries/Stepper/examples/stepper_oneRevolution/stepper_oneRevolution.ino b/msp430/libraries/Stepper/examples/stepper_oneRevolution/stepper_oneRevolution.ino new file mode 100644 index 0000000..2dbb57d --- /dev/null +++ b/msp430/libraries/Stepper/examples/stepper_oneRevolution/stepper_oneRevolution.ino @@ -0,0 +1,44 @@ + +/* + Stepper Motor Control - one revolution + + This program drives a unipolar or bipolar stepper motor. + The motor is attached to digital pins 8 - 11 of the Arduino. + + The motor should revolve one revolution in one direction, then + one revolution in the other direction. + + + Created 11 Mar. 2007 + Modified 30 Nov. 2009 + by Tom Igoe + + */ + +#include <Stepper.h> + +const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution + // for your motor + +// initialize the stepper library on pins 8 through 11: +Stepper myStepper(stepsPerRevolution, 8,9,10,11); + +void setup() { + // set the speed at 60 rpm: + myStepper.setSpeed(60); + // initialize the serial port: + Serial.begin(9600); +} + +void loop() { + // step one revolution in one direction: + Serial.println("clockwise"); + myStepper.step(stepsPerRevolution); + delay(500); + + // step one revolution in the other direction: + Serial.println("counterclockwise"); + myStepper.step(-stepsPerRevolution); + delay(500); +} + diff --git a/msp430/libraries/Stepper/examples/stepper_oneStepAtATime/stepper_oneStepAtATime.ino b/msp430/libraries/Stepper/examples/stepper_oneStepAtATime/stepper_oneStepAtATime.ino new file mode 100644 index 0000000..36d3299 --- /dev/null +++ b/msp430/libraries/Stepper/examples/stepper_oneStepAtATime/stepper_oneStepAtATime.ino @@ -0,0 +1,44 @@ + +/* + Stepper Motor Control - one step at a time + + This program drives a unipolar or bipolar stepper motor. + The motor is attached to digital pins 8 - 11 of the Arduino. + + The motor will step one step at a time, very slowly. You can use this to + test that you've got the four wires of your stepper wired to the correct + pins. If wired correctly, all steps should be in the same direction. + + Use this also to count the number of steps per revolution of your motor, + if you don't know it. Then plug that number into the oneRevolution + example to see if you got it right. + + Created 30 Nov. 2009 + by Tom Igoe + + */ + +#include <Stepper.h> + +const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution + // for your motor + +// initialize the stepper library on pins 8 through 11: +Stepper myStepper(stepsPerRevolution, 8,9,10,11); + +int stepCount = 0; // number of steps the motor has taken + +void setup() { + // initialize the serial port: + Serial.begin(9600); +} + +void loop() { + // step one step: + myStepper.step(1); + Serial.print("steps:" ); + Serial.println(stepCount); + stepCount++; + delay(500); +} + diff --git a/msp430/libraries/Stepper/examples/stepper_speedControl/stepper_speedControl.ino b/msp430/libraries/Stepper/examples/stepper_speedControl/stepper_speedControl.ino new file mode 100644 index 0000000..dbd0f7f --- /dev/null +++ b/msp430/libraries/Stepper/examples/stepper_speedControl/stepper_speedControl.ino @@ -0,0 +1,49 @@ + +/* + Stepper Motor Control - speed control + + This program drives a unipolar or bipolar stepper motor. + The motor is attached to digital pins 8 - 11 of the Arduino. + A potentiometer is connected to analog input 0. + + The motor will rotate in a clockwise direction. The higher the potentiometer value, + the faster the motor speed. Because setSpeed() sets the delay between steps, + you may notice the motor is less responsive to changes in the sensor value at + low speeds. + + Created 30 Nov. 2009 + Modified 28 Oct 2010 + by Tom Igoe + + */ + +#include <Stepper.h> + +const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution +// for your motor + + +// initialize the stepper library on pins 8 through 11: +Stepper myStepper(stepsPerRevolution, 8,9,10,11); + +int stepCount = 0; // number of steps the motor has taken + +void setup() { + // initialize the serial port: + Serial.begin(9600); +} + +void loop() { + // read the sensor value: + int sensorReading = analogRead(A0); + // map it to a range from 0 to 100: + int motorSpeed = map(sensorReading, 0, 1023, 0, 100); + // set the motor speed: + if (motorSpeed > 0) { + myStepper.setSpeed(motorSpeed); + // step 1/100 of a revolution: + myStepper.step(stepsPerRevolution/100); + } +} + + diff --git a/msp430/libraries/Stepper/keywords.txt b/msp430/libraries/Stepper/keywords.txt new file mode 100644 index 0000000..19a0fad --- /dev/null +++ b/msp430/libraries/Stepper/keywords.txt @@ -0,0 +1,28 @@ +####################################### +# Syntax Coloring Map For Test +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +Stepper KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +step KEYWORD2 +setSpeed KEYWORD2 +version KEYWORD2 + +###################################### +# Instances (KEYWORD2) +####################################### +direction KEYWORD2 +speed KEYWORD2 + + +####################################### +# Constants (LITERAL1) +####################################### diff --git a/msp430/libraries/Wire/examples/SFRRanger_reader/SFRRanger_reader.ino b/msp430/libraries/Wire/examples/SFRRanger_reader/SFRRanger_reader.ino new file mode 100644 index 0000000..9c41c18 --- /dev/null +++ b/msp430/libraries/Wire/examples/SFRRanger_reader/SFRRanger_reader.ino @@ -0,0 +1,87 @@ +// I2C SRF10 or SRF08 Devantech Ultrasonic Ranger Finder +// by Nicholas Zambetti <http://www.zambetti.com> +// and James Tichenor <http://www.jamestichenor.net> + +// Demonstrates use of the Wire library reading data from the +// Devantech Utrasonic Rangers SFR08 and SFR10 + +// Created 29 April 2006 + +// This example code is in the public domain. + + +#include <Wire.h> + +void setup() +{ + Wire.begin(); // join i2c bus (address optional for master) + Serial.begin(9600); // start serial communication at 9600bps +} + +int reading = 0; + +void loop() +{ + // step 1: instruct sensor to read echoes + Wire.beginTransmission(112); // transmit to device #112 (0x70) + // the address specified in the datasheet is 224 (0xE0) + // but i2c adressing uses the high 7 bits so it's 112 + Wire.write(byte(0x00)); // sets register pointer to the command register (0x00) + Wire.write(byte(0x50)); // command sensor to measure in "inches" (0x50) + // use 0x51 for centimeters + // use 0x52 for ping microseconds + Wire.endTransmission(); // stop transmitting + + // step 2: wait for readings to happen + delay(70); // datasheet suggests at least 65 milliseconds + + // step 3: instruct sensor to return a particular echo reading + Wire.beginTransmission(112); // transmit to device #112 + Wire.write(byte(0x02)); // sets register pointer to echo #1 register (0x02) + Wire.endTransmission(); // stop transmitting + + // step 4: request reading from sensor + Wire.requestFrom(112, 2); // request 2 bytes from slave device #112 + + // step 5: receive reading from sensor + if(2 <= Wire.available()) // if two bytes were received + { + reading = Wire.read(); // receive high byte (overwrites previous reading) + reading = reading << 8; // shift high byte to be high 8 bits + reading |= Wire.read(); // receive low byte as lower 8 bits + Serial.println(reading); // print the reading + } + + delay(250); // wait a bit since people have to read the output :) +} + + +/* + +// The following code changes the address of a Devantech Ultrasonic Range Finder (SRF10 or SRF08) +// usage: changeAddress(0x70, 0xE6); + +void changeAddress(byte oldAddress, byte newAddress) +{ + Wire.beginTransmission(oldAddress); + Wire.write(byte(0x00)); + Wire.write(byte(0xA0)); + Wire.endTransmission(); + + Wire.beginTransmission(oldAddress); + Wire.write(byte(0x00)); + Wire.write(byte(0xAA)); + Wire.endTransmission(); + + Wire.beginTransmission(oldAddress); + Wire.write(byte(0x00)); + Wire.write(byte(0xA5)); + Wire.endTransmission(); + + Wire.beginTransmission(oldAddress); + Wire.write(byte(0x00)); + Wire.write(newAddress); + Wire.endTransmission(); +} + +*/ diff --git a/msp430/libraries/Wire/examples/digital_potentiometer/digital_potentiometer.ino b/msp430/libraries/Wire/examples/digital_potentiometer/digital_potentiometer.ino new file mode 100644 index 0000000..38da1c5 --- /dev/null +++ b/msp430/libraries/Wire/examples/digital_potentiometer/digital_potentiometer.ino @@ -0,0 +1,39 @@ +// I2C Digital Potentiometer +// by Nicholas Zambetti <http://www.zambetti.com> +// and Shawn Bonkowski <http://people.interaction-ivrea.it/s.bonkowski/> + +// Demonstrates use of the Wire library +// Controls AD5171 digital potentiometer via I2C/TWI + +// Created 31 March 2006 + +// This example code is in the public domain. + +// This example code is in the public domain. + + +#include <Wire.h> + +void setup() +{ + Wire.begin(); // join i2c bus (address optional for master) +} + +byte val = 0; + +void loop() +{ + Wire.beginTransmission(44); // transmit to device #44 (0x2c) + // device address is specified in datasheet + Wire.write(byte(0x00)); // sends instruction byte + Wire.write(val); // sends potentiometer value byte + Wire.endTransmission(); // stop transmitting + + val++; // increment value + if(val == 64) // if reached 64th position (max) + { + val = 0; // start over from lowest value + } + delay(500); +} + diff --git a/msp430/libraries/Wire/examples/master_reader/master_reader.ino b/msp430/libraries/Wire/examples/master_reader/master_reader.ino new file mode 100644 index 0000000..4124d7d --- /dev/null +++ b/msp430/libraries/Wire/examples/master_reader/master_reader.ino @@ -0,0 +1,32 @@ +// Wire Master Reader +// by Nicholas Zambetti <http://www.zambetti.com> + +// Demonstrates use of the Wire library +// Reads data from an I2C/TWI slave device +// Refer to the "Wire Slave Sender" example for use with this + +// Created 29 March 2006 + +// This example code is in the public domain. + + +#include <Wire.h> + +void setup() +{ + Wire.begin(); // join i2c bus (address optional for master) + Serial.begin(9600); // start serial for output +} + +void loop() +{ + Wire.requestFrom(2, 6); // request 6 bytes from slave device #2 + + while(Wire.available()) // slave may send less than requested + { + char c = Wire.read(); // receive a byte as character + Serial.print(c); // print the character + } + + delay(500); +} diff --git a/msp430/libraries/Wire/examples/master_writer/master_writer.ino b/msp430/libraries/Wire/examples/master_writer/master_writer.ino new file mode 100644 index 0000000..ccaa036 --- /dev/null +++ b/msp430/libraries/Wire/examples/master_writer/master_writer.ino @@ -0,0 +1,31 @@ +// Wire Master Writer +// by Nicholas Zambetti <http://www.zambetti.com> + +// Demonstrates use of the Wire library +// Writes data to an I2C/TWI slave device +// Refer to the "Wire Slave Receiver" example for use with this + +// Created 29 March 2006 + +// This example code is in the public domain. + + +#include <Wire.h> + +void setup() +{ + Wire.begin(); // join i2c bus (address optional for master) +} + +byte x = 0; + +void loop() +{ + Wire.beginTransmission(4); // transmit to device #4 + Wire.write("x is "); // sends five bytes + Wire.write(x); // sends one byte + Wire.endTransmission(); // stop transmitting + + x++; + delay(500); +} diff --git a/msp430/libraries/Wire/examples/slave_receiver/slave_receiver.ino b/msp430/libraries/Wire/examples/slave_receiver/slave_receiver.ino new file mode 100644 index 0000000..60dd4bd --- /dev/null +++ b/msp430/libraries/Wire/examples/slave_receiver/slave_receiver.ino @@ -0,0 +1,38 @@ +// Wire Slave Receiver +// by Nicholas Zambetti <http://www.zambetti.com> + +// Demonstrates use of the Wire library +// Receives data as an I2C/TWI slave device +// Refer to the "Wire Master Writer" example for use with this + +// Created 29 March 2006 + +// This example code is in the public domain. + + +#include <Wire.h> + +void setup() +{ + Wire.begin(4); // join i2c bus with address #4 + Wire.onReceive(receiveEvent); // register event + Serial.begin(9600); // start serial for output +} + +void loop() +{ + delay(100); +} + +// function that executes whenever data is received from master +// this function is registered as an event, see setup() +void receiveEvent(int howMany) +{ + while(1 < Wire.available()) // loop through all but the last + { + char c = Wire.read(); // receive byte as a character + Serial.print(c); // print the character + } + int x = Wire.read(); // receive byte as an integer + Serial.println(x); // print the integer +} diff --git a/msp430/libraries/Wire/examples/slave_sender/slave_sender.ino b/msp430/libraries/Wire/examples/slave_sender/slave_sender.ino new file mode 100644 index 0000000..d3b238a --- /dev/null +++ b/msp430/libraries/Wire/examples/slave_sender/slave_sender.ino @@ -0,0 +1,32 @@ +// Wire Slave Sender +// by Nicholas Zambetti <http://www.zambetti.com> + +// Demonstrates use of the Wire library +// Sends data as an I2C/TWI slave device +// Refer to the "Wire Master Reader" example for use with this + +// Created 29 March 2006 + +// This example code is in the public domain. + + +#include <Wire.h> + +void setup() +{ + Wire.begin(2); // join i2c bus with address #2 + Wire.onRequest(requestEvent); // register event +} + +void loop() +{ + delay(100); +} + +// function that executes whenever data is requested by master +// this function is registered as an event, see setup() +void requestEvent() +{ + Wire.write("hello "); // respond with message of 6 bytes + // as expected by master +} diff --git a/msp430/libraries/Wire/keywords.txt b/msp430/libraries/Wire/keywords.txt new file mode 100644 index 0000000..12f129b --- /dev/null +++ b/msp430/libraries/Wire/keywords.txt @@ -0,0 +1,31 @@ +####################################### +# Syntax Coloring Map For Wire +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +begin KEYWORD2 +beginTransmission KEYWORD2 +endTransmission KEYWORD2 +requestFrom KEYWORD2 +send KEYWORD2 +receive KEYWORD2 +onReceive KEYWORD2 +onRequest KEYWORD2 + +####################################### +# Instances (KEYWORD2) +####################################### + +Wire KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### + diff --git a/msp430/programmers.txt b/msp430/programmers.txt new file mode 100644 index 0000000..9071910 --- /dev/null +++ b/msp430/programmers.txt @@ -0,0 +1,4 @@ +# See: http://code.google.com/p/arduino/wiki/Platforms + +rf2500.name=rf2500 +rf2500.protocol=rf2500 diff --git a/msp430/variants/launchpad/pins_energia.h b/msp430/variants/launchpad/pins_energia.h new file mode 100644 index 0000000..8e1a775 --- /dev/null +++ b/msp430/variants/launchpad/pins_energia.h @@ -0,0 +1,245 @@ +/* + ************************************************************************ + * pins_energia.h + * + * Energia core files for MSP430 + * Copyright (c) 2012 Robert Wessels. All right reserved. + * + * Contribution: Rei VILO + * + *********************************************************************** + Derived from: + pins_arduino.h - Pin definition functions for Arduino + Part of Arduino - http://www.arduino.cc/ + + Copyright (c) 2007 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 +*/ + +#ifndef Pins_Arduino_h +#define Pins_Arduino_h +#ifndef BV +#define BV(x) (1 << (x)) +#endif + +#if defined(__MSP430_HAS_USCI__) +static const uint8_t SS = 8; /* P2.0 */ +static const uint8_t SCK = 7; /* P1.5 */ +static const uint8_t MOSI = 15; /* P1.7 */ +static const uint8_t MISO = 14; /* P1.6 */ +#endif + +#if defined(__MSP430_HAS_USI__) +static const uint8_t SS = 8; /* P2.0 */ +static const uint8_t SCK = 7; /* P1.5 */ +static const uint8_t MOSI = 14; /* P1.6 */ +static const uint8_t MISO = 15; /* P1.7 */ +#endif + +static const uint8_t A0 = 0; +static const uint8_t A1 = 1; +static const uint8_t A2 = 2; +static const uint8_t A3 = 3; +static const uint8_t A4 = 4; +static const uint8_t A5 = 5; +static const uint8_t A6 = 6; +static const uint8_t A7 = 7; +static const uint8_t A10 = 10; // special. This is the internal temp sensor + +// +-\/-+ +// VCC 1| |20 GND +// (A0) P1.0 2| |19 XIN +// (A1) P1.1 3| |18 XOUT +// (A2) P1.2 4| |17 TEST +// (A3) P1.3 5| |16 RST# +// (A4) P1.4 6| |15 P1.7 (A7) (SCL) (MISO) depends on chip +// (A5) P1.5 7| |14 P1.6 (A6) (SDA) (MOSI) +// P2.0 8| |13 P2.5 +// P2.1 9| |12 P2.4 +// P2.2 10| |11 P2.3 +// +----+ +// + +// Pin names based on the silkscreen +// +static const uint8_t P1_0 = 2; +static const uint8_t P1_1 = 3; +static const uint8_t P1_2 = 4; +static const uint8_t P1_3 = 5; +static const uint8_t P1_4 = 6; +static const uint8_t P1_5 = 7; +static const uint8_t P2_0 = 8; +static const uint8_t P2_1 = 9; +static const uint8_t P2_2 = 10; +static const uint8_t P2_3 = 11; +static const uint8_t P2_4 = 12; +static const uint8_t P2_5 = 13; +static const uint8_t P1_6 = 14; +static const uint8_t P1_7 = 15; + +static const uint8_t RED_LED = 2; +static const uint8_t GREEN_LED = 14; +static const uint8_t PUSH2 = 5; +static const uint8_t TEMPSENSOR = 10; // depends on chip + + +#ifdef ARDUINO_MAIN + +const uint16_t port_to_dir[] = { + NOT_A_PORT, + (uint16_t) &P1DIR, + (uint16_t) &P2DIR, +#ifdef __MSP430_HAS_PORT3_R__ + (uint16_t) &P3DIR, +#endif +}; + +const uint16_t port_to_sel[] = { + NOT_A_PORT, + (uint16_t) &P1SEL, + (uint16_t) &P2SEL, +#ifdef __MSP430_HAS_PORT3_R__ + (uint16_t) &P3SEL, +#endif +}; + +const uint16_t port_to_ren[] = { + NOT_A_PORT, + (uint16_t) &P1REN, + (uint16_t) &P2REN, +#ifdef __MSP430_HAS_PORT3_R__ + (uint16_t) &P3REN, +#endif +}; + +const uint16_t port_to_sel2[] = { + NOT_A_PORT, +#ifdef P1SEL2_ + (uint16_t) &P1SEL2, +#else + NOT_A_PORT, +#endif +#ifdef P2SEL2_ + (uint16_t) &P2SEL2, +#else + NOT_A_PORT, +#endif +#ifdef P3SEL2_ + (uint16_t) &P3SEL2, +#else + NOT_A_PORT, +#endif +}; + +const uint16_t port_to_input[] = { + NOT_A_PORT, + (uint16_t) &P1IN, + (uint16_t) &P2IN, +#ifdef __MSP430_HAS_PORT3_R__ + (uint16_t) &P3IN, +#endif +}; +const uint16_t port_to_output[] = { + NOT_A_PORT, + (uint16_t) &P1OUT, + (uint16_t) &P2OUT, +#ifdef __MSP430_HAS_PORT3_R__ + (uint16_t) &P3OUT, +#endif +}; + +/* + * Defines for devices with 2x TA3 timers (e.g. MSP430g2553). On the 20pin devices, upto 3 analog outputs are available + * T0A1, T1A1 and T1A2 + */ +const uint8_t digital_pin_to_timer[] = { + NOT_ON_TIMER, /* dummy */ + NOT_ON_TIMER, /* 1 - VCC */ + NOT_ON_TIMER, /* 2 - P1.0 */ + T0A0, /* 3 - P1.1, note: A0 output cannot be used with analogWrite */ + T0A1, /* 4 - P1.2 */ + NOT_ON_TIMER, /* 5 - P1.3 */ + NOT_ON_TIMER, /* 6 - P1.4 note: special case. Leaving as no timer due to difficulty determining if available */ + T0A0, /* 7 - P1.5 note: A0 output cannot be used with analogWrite */ +#if defined(__MSP430_HAS_T1A3__) + T1A0, /* 8 - P2.0 note: A0 output cannot be used with analogWrite */ + T1A1, /* 9 - P2.1 */ + T1A1, /* 10 - P2.3 */ + T1A0, /* 11 - P2.4 note: A0 output cannot be used with analogWrite */ + T1A2, /* 12 - P2.5 */ + T1A2, /* 13 - P2.6 */ +#else + NOT_ON_TIMER, /* 8 - P2.0 */ + NOT_ON_TIMER, /* 9 - P2.1 */ + NOT_ON_TIMER, /* 10 - P2.3 */ + NOT_ON_TIMER, /* 11 - P2.4 */ + NOT_ON_TIMER, /* 12 - P2.5 */ + NOT_ON_TIMER, /* 13 - P2.6 */ +#endif + T0A1, /* 14 - P1.6 */ + NOT_ON_TIMER, /* 15 - P1.7 */ + NOT_ON_TIMER, /* 16 - /RESET */ + NOT_ON_TIMER, /* 17 - TEST */ + NOT_ON_TIMER, /* 18 - XOUT - P2.7 */ + T0A1, /* 18 - XIN - P2.6: */ + NOT_ON_TIMER, /* 20 - GND */ +}; + +const uint8_t digital_pin_to_port[] = { + NOT_A_PIN, /* dummy */ + NOT_A_PIN, /* 1 */ + P1, /* 2 */ + P1, /* 3 */ + P1, /* 4 */ + P1, /* 5 */ + P1, /* 6 */ + P1, /* 7 */ + P2, /* 8 */ + P2, /* 9 */ + P2, /* 10 */ + P2, /* 11 */ + P2, /* 12 */ + P2, /* 13 */ + P1, /* 14 */ + P1, /* 15 */ +}; + +const uint8_t digital_pin_to_bit_mask[] = { + NOT_A_PIN, /* 0, pin count starts at 1 */ + NOT_A_PIN, /* 1, VCC */ + BV(0), /* 2, port P1.0 */ + BV(1), /* 3, port P1.1 */ + BV(2), /* 4, port P1.2 */ + BV(3), /* 5, port P1.3*/ + BV(4), /* 6, port P1.4 */ + BV(5), /* 7, port P1.5 */ + BV(0), /* 8, port P2.0 */ + BV(1), /* 9, port P2.1 */ + BV(2), /* 10, port P2.2 */ + BV(3), /* 11, port P2.3 */ + BV(4), /* 12, port P2.4 */ + BV(5), /* 13, port P2.5 */ + BV(6), /* 14, port P1.6 */ + BV(7), /* 15, port P1.7 */ + NOT_A_PIN, /* 16, RST */ + NOT_A_PIN, /* 17, TEST */ + NOT_A_PIN, /* 18, XOUT */ + NOT_A_PIN, /* 19, XIN */ + NOT_A_PIN, /* 20, GND */ +}; +#endif +#endif |
