mirror of
https://github.com/arduino/Arduino.git
synced 2025-03-15 12:29:26 +01:00
Merge branch 'master' of https://tigoe@github.com/arduino/Arduino
This commit is contained in:
commit
2d9a2d1b2c
127
hardware/arduino/cores/arduino/HardwareSerial.cpp
Executable file → Normal file
127
hardware/arduino/cores/arduino/HardwareSerial.cpp
Executable file → Normal file
@ -17,6 +17,7 @@
|
|||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
|
||||||
Modified 23 November 2006 by David A. Mellis
|
Modified 23 November 2006 by David A. Mellis
|
||||||
|
Modified 28 September 2010 by Mark Sproul
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -26,25 +27,39 @@
|
|||||||
#include "wiring.h"
|
#include "wiring.h"
|
||||||
#include "wiring_private.h"
|
#include "wiring_private.h"
|
||||||
|
|
||||||
|
// this next line disables the entire HardwareSerial.cpp,
|
||||||
|
// this is so I can support Attiny series and any other chip without a uart
|
||||||
|
#if defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H)
|
||||||
|
|
||||||
#include "HardwareSerial.h"
|
#include "HardwareSerial.h"
|
||||||
|
|
||||||
// Define constants and variables for buffering incoming serial data. We're
|
// Define constants and variables for buffering incoming serial data. We're
|
||||||
// using a ring buffer (I think), in which rx_buffer_head is the index of the
|
// using a ring buffer (I think), in which rx_buffer_head is the index of the
|
||||||
// location to which to write the next incoming character and rx_buffer_tail
|
// location to which to write the next incoming character and rx_buffer_tail
|
||||||
// is the index of the location from which to read.
|
// is the index of the location from which to read.
|
||||||
|
#if (RAMEND < 1000)
|
||||||
|
#define RX_BUFFER_SIZE 32
|
||||||
|
#else
|
||||||
#define RX_BUFFER_SIZE 128
|
#define RX_BUFFER_SIZE 128
|
||||||
|
#endif
|
||||||
|
|
||||||
struct ring_buffer {
|
struct ring_buffer
|
||||||
|
{
|
||||||
unsigned char buffer[RX_BUFFER_SIZE];
|
unsigned char buffer[RX_BUFFER_SIZE];
|
||||||
int head;
|
int head;
|
||||||
int tail;
|
int tail;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#if defined(UBRRH) || defined(UBRR0H)
|
||||||
ring_buffer rx_buffer = { { 0 }, 0, 0 };
|
ring_buffer rx_buffer = { { 0 }, 0, 0 };
|
||||||
|
#endif
|
||||||
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
#if defined(UBRR1H)
|
||||||
ring_buffer rx_buffer1 = { { 0 }, 0, 0 };
|
ring_buffer rx_buffer1 = { { 0 }, 0, 0 };
|
||||||
|
#endif
|
||||||
|
#if defined(UBRR2H)
|
||||||
ring_buffer rx_buffer2 = { { 0 }, 0, 0 };
|
ring_buffer rx_buffer2 = { { 0 }, 0, 0 };
|
||||||
|
#endif
|
||||||
|
#if defined(UBRR3H)
|
||||||
ring_buffer rx_buffer3 = { { 0 }, 0, 0 };
|
ring_buffer rx_buffer3 = { { 0 }, 0, 0 };
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -62,49 +77,96 @@ inline void store_char(unsigned char c, ring_buffer *rx_buffer)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
#if defined(USART_RX_vect)
|
||||||
|
SIGNAL(USART_RX_vect)
|
||||||
|
{
|
||||||
|
#if defined(UDR0)
|
||||||
|
unsigned char c = UDR0;
|
||||||
|
#elif defined(UDR)
|
||||||
|
unsigned char c = UDR; // atmega8535
|
||||||
|
#else
|
||||||
|
#error UDR not defined
|
||||||
|
#endif
|
||||||
|
store_char(c, &rx_buffer);
|
||||||
|
}
|
||||||
|
#elif defined(SIG_USART0_RECV) && defined(UDR0)
|
||||||
SIGNAL(SIG_USART0_RECV)
|
SIGNAL(SIG_USART0_RECV)
|
||||||
{
|
{
|
||||||
unsigned char c = UDR0;
|
unsigned char c = UDR0;
|
||||||
store_char(c, &rx_buffer);
|
store_char(c, &rx_buffer);
|
||||||
}
|
}
|
||||||
|
#elif defined(SIG_UART0_RECV) && defined(UDR0)
|
||||||
|
SIGNAL(SIG_UART0_RECV)
|
||||||
|
{
|
||||||
|
unsigned char c = UDR0;
|
||||||
|
store_char(c, &rx_buffer);
|
||||||
|
}
|
||||||
|
//#elif defined(SIG_USART_RECV)
|
||||||
|
#elif defined(USART0_RX_vect)
|
||||||
|
// fixed by Mark Sproul this is on the 644/644p
|
||||||
|
//SIGNAL(SIG_USART_RECV)
|
||||||
|
SIGNAL(USART0_RX_vect)
|
||||||
|
{
|
||||||
|
#if defined(UDR0)
|
||||||
|
unsigned char c = UDR0;
|
||||||
|
#elif defined(UDR)
|
||||||
|
unsigned char c = UDR; // atmega8, atmega32
|
||||||
|
#else
|
||||||
|
#error UDR not defined
|
||||||
|
#endif
|
||||||
|
store_char(c, &rx_buffer);
|
||||||
|
}
|
||||||
|
#elif defined(SIG_UART_RECV)
|
||||||
|
// this is for atmega8
|
||||||
|
SIGNAL(SIG_UART_RECV)
|
||||||
|
{
|
||||||
|
#if defined(UDR0)
|
||||||
|
unsigned char c = UDR0; // atmega645
|
||||||
|
#elif defined(UDR)
|
||||||
|
unsigned char c = UDR; // atmega8
|
||||||
|
#endif
|
||||||
|
store_char(c, &rx_buffer);
|
||||||
|
}
|
||||||
|
#elif defined(USBCON)
|
||||||
|
#warning No interrupt handler for usart 0
|
||||||
|
#warning Serial(0) is on USB interface
|
||||||
|
#else
|
||||||
|
#error No interrupt handler for usart 0
|
||||||
|
#endif
|
||||||
|
|
||||||
SIGNAL(SIG_USART1_RECV)
|
//#if defined(SIG_USART1_RECV)
|
||||||
|
#if defined(USART1_RX_vect)
|
||||||
|
//SIGNAL(SIG_USART1_RECV)
|
||||||
|
SIGNAL(USART1_RX_vect)
|
||||||
{
|
{
|
||||||
unsigned char c = UDR1;
|
unsigned char c = UDR1;
|
||||||
store_char(c, &rx_buffer1);
|
store_char(c, &rx_buffer1);
|
||||||
}
|
}
|
||||||
|
#elif defined(SIG_USART1_RECV)
|
||||||
|
#error SIG_USART1_RECV
|
||||||
|
#endif
|
||||||
|
|
||||||
SIGNAL(SIG_USART2_RECV)
|
#if defined(USART2_RX_vect) && defined(UDR2)
|
||||||
|
SIGNAL(USART2_RX_vect)
|
||||||
{
|
{
|
||||||
unsigned char c = UDR2;
|
unsigned char c = UDR2;
|
||||||
store_char(c, &rx_buffer2);
|
store_char(c, &rx_buffer2);
|
||||||
}
|
}
|
||||||
|
#elif defined(SIG_USART2_RECV)
|
||||||
|
#error SIG_USART2_RECV
|
||||||
|
#endif
|
||||||
|
|
||||||
SIGNAL(SIG_USART3_RECV)
|
#if defined(USART3_RX_vect) && defined(UDR3)
|
||||||
|
SIGNAL(USART3_RX_vect)
|
||||||
{
|
{
|
||||||
unsigned char c = UDR3;
|
unsigned char c = UDR3;
|
||||||
store_char(c, &rx_buffer3);
|
store_char(c, &rx_buffer3);
|
||||||
}
|
}
|
||||||
|
#elif defined(SIG_USART3_RECV)
|
||||||
#else
|
#error SIG_USART3_RECV
|
||||||
|
|
||||||
#if defined(__AVR_ATmega8__)
|
|
||||||
SIGNAL(SIG_UART_RECV)
|
|
||||||
#else
|
|
||||||
SIGNAL(USART_RX_vect)
|
|
||||||
#endif
|
#endif
|
||||||
{
|
|
||||||
#if defined(__AVR_ATmega8__)
|
|
||||||
unsigned char c = UDR;
|
|
||||||
#else
|
|
||||||
unsigned char c = UDR0;
|
|
||||||
#endif
|
|
||||||
store_char(c, &rx_buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Constructors ////////////////////////////////////////////////////////////////
|
// Constructors ////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
@ -223,14 +285,25 @@ void HardwareSerial::write(uint8_t c)
|
|||||||
|
|
||||||
// Preinstantiate Objects //////////////////////////////////////////////////////
|
// Preinstantiate Objects //////////////////////////////////////////////////////
|
||||||
|
|
||||||
#if defined(__AVR_ATmega8__)
|
#if defined(UBRRH) && defined(UBRRL)
|
||||||
HardwareSerial Serial(&rx_buffer, &UBRRH, &UBRRL, &UCSRA, &UCSRB, &UDR, RXEN, TXEN, RXCIE, UDRE, U2X);
|
HardwareSerial Serial(&rx_buffer, &UBRRH, &UBRRL, &UCSRA, &UCSRB, &UDR, RXEN, TXEN, RXCIE, UDRE, U2X);
|
||||||
#else
|
#elif defined(UBRR0H) && defined(UBRR0L)
|
||||||
HardwareSerial Serial(&rx_buffer, &UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UDR0, RXEN0, TXEN0, RXCIE0, UDRE0, U2X0);
|
HardwareSerial Serial(&rx_buffer, &UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UDR0, RXEN0, TXEN0, RXCIE0, UDRE0, U2X0);
|
||||||
|
#elif defined(USBCON)
|
||||||
|
#warning no serial port defined (port 0)
|
||||||
|
#else
|
||||||
|
#error no serial port defined (port 0)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
#if defined(UBRR1H)
|
||||||
HardwareSerial Serial1(&rx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UDR1, RXEN1, TXEN1, RXCIE1, UDRE1, U2X1);
|
HardwareSerial Serial1(&rx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UDR1, RXEN1, TXEN1, RXCIE1, UDRE1, U2X1);
|
||||||
|
#endif
|
||||||
|
#if defined(UBRR2H)
|
||||||
HardwareSerial Serial2(&rx_buffer2, &UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UDR2, RXEN2, TXEN2, RXCIE2, UDRE2, U2X2);
|
HardwareSerial Serial2(&rx_buffer2, &UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UDR2, RXEN2, TXEN2, RXCIE2, UDRE2, U2X2);
|
||||||
|
#endif
|
||||||
|
#if defined(UBRR3H)
|
||||||
HardwareSerial Serial3(&rx_buffer3, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UDR3, RXEN3, TXEN3, RXCIE3, UDRE3, U2X3);
|
HardwareSerial Serial3(&rx_buffer3, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UDR3, RXEN3, TXEN3, RXCIE3, UDRE3, U2X3);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#endif // whole file
|
||||||
|
|
||||||
|
13
hardware/arduino/cores/arduino/HardwareSerial.h
Executable file → Normal file
13
hardware/arduino/cores/arduino/HardwareSerial.h
Executable file → Normal file
@ -15,6 +15,8 @@
|
|||||||
You should have received a copy of the GNU Lesser General Public
|
You should have received a copy of the GNU Lesser General Public
|
||||||
License along with this library; if not, write to the Free Software
|
License along with this library; if not, write to the Free Software
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
|
||||||
|
Modified 28 September 2010 by Mark Sproul
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef HardwareSerial_h
|
#ifndef HardwareSerial_h
|
||||||
@ -56,11 +58,18 @@ class HardwareSerial : public Stream
|
|||||||
using Print::write; // pull in write(str) and write(buf, size) from Print
|
using Print::write; // pull in write(str) and write(buf, size) from Print
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#if defined(UBRRH) || defined(UBRR0H)
|
||||||
extern HardwareSerial Serial;
|
extern HardwareSerial Serial;
|
||||||
|
#elif defined(USBCON)
|
||||||
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
#include "usb_api.h"
|
||||||
|
#endif
|
||||||
|
#if defined(UBRR1H)
|
||||||
extern HardwareSerial Serial1;
|
extern HardwareSerial Serial1;
|
||||||
|
#endif
|
||||||
|
#if defined(UBRR2H)
|
||||||
extern HardwareSerial Serial2;
|
extern HardwareSerial Serial2;
|
||||||
|
#endif
|
||||||
|
#if defined(UBRR3H)
|
||||||
extern HardwareSerial Serial3;
|
extern HardwareSerial Serial3;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
Boston, MA 02111-1307 USA
|
Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
Modified 24 November 2006 by David A. Mellis
|
Modified 24 November 2006 by David A. Mellis
|
||||||
|
Modified 1 August 2010 by Mark Sproul
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
@ -35,11 +36,6 @@
|
|||||||
volatile static voidFuncPtr intFunc[EXTERNAL_NUM_INTERRUPTS];
|
volatile static voidFuncPtr intFunc[EXTERNAL_NUM_INTERRUPTS];
|
||||||
// volatile static voidFuncPtr twiIntFunc;
|
// volatile static voidFuncPtr twiIntFunc;
|
||||||
|
|
||||||
#if defined(__AVR_ATmega8__)
|
|
||||||
#define EICRA MCUCR
|
|
||||||
#define EIMSK GICR
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) {
|
void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) {
|
||||||
if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
|
if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
|
||||||
intFunc[interruptNum] = userFunc;
|
intFunc[interruptNum] = userFunc;
|
||||||
@ -52,7 +48,7 @@ void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) {
|
|||||||
// Enable the interrupt.
|
// Enable the interrupt.
|
||||||
|
|
||||||
switch (interruptNum) {
|
switch (interruptNum) {
|
||||||
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
#if defined(EICRA) && defined(EICRB) && defined(EIMSK)
|
||||||
case 2:
|
case 2:
|
||||||
EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
|
EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
|
||||||
EIMSK |= (1 << INT0);
|
EIMSK |= (1 << INT0);
|
||||||
@ -87,12 +83,33 @@ void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) {
|
|||||||
break;
|
break;
|
||||||
#else
|
#else
|
||||||
case 0:
|
case 0:
|
||||||
|
#if defined(EICRA) && defined(ISC00) && defined(EIMSK)
|
||||||
EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
|
EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
|
||||||
EIMSK |= (1 << INT0);
|
EIMSK |= (1 << INT0);
|
||||||
|
#elif defined(MCUCR) && defined(ISC00) && defined(GICR)
|
||||||
|
MCUCR = (MCUCR & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
|
||||||
|
GICR |= (1 << INT0);
|
||||||
|
#elif defined(MCUCR) && defined(ISC00) && defined(GIMSK)
|
||||||
|
MCUCR = (MCUCR & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
|
||||||
|
GIMSK |= (1 << INT0);
|
||||||
|
#else
|
||||||
|
#error attachInterrupt not finished for this CPU (case 0)
|
||||||
|
#endif
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 1:
|
case 1:
|
||||||
|
#if defined(EICRA) && defined(ISC10) && defined(ISC11) && defined(EIMSK)
|
||||||
EICRA = (EICRA & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
|
EICRA = (EICRA & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
|
||||||
EIMSK |= (1 << INT1);
|
EIMSK |= (1 << INT1);
|
||||||
|
#elif defined(MCUCR) && defined(ISC10) && defined(ISC11) && defined(GICR)
|
||||||
|
MCUCR = (MCUCR & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
|
||||||
|
GICR |= (1 << INT1);
|
||||||
|
#elif defined(MCUCR) && defined(ISC10) && defined(GIMSK) && defined(GIMSK)
|
||||||
|
MCUCR = (MCUCR & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
|
||||||
|
GIMSK |= (1 << INT1);
|
||||||
|
#else
|
||||||
|
#warning attachInterrupt may need some more work for this cpu (case 1)
|
||||||
|
#endif
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@ -105,7 +122,7 @@ void detachInterrupt(uint8_t interruptNum) {
|
|||||||
// to the number of the EIMSK bit to clear, as this isn't true on the
|
// to the number of the EIMSK bit to clear, as this isn't true on the
|
||||||
// ATmega8. There, INT0 is 6 and INT1 is 7.)
|
// ATmega8. There, INT0 is 6 and INT1 is 7.)
|
||||||
switch (interruptNum) {
|
switch (interruptNum) {
|
||||||
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
#if defined(EICRA) && defined(EICRB) && defined(EIMSK)
|
||||||
case 2:
|
case 2:
|
||||||
EIMSK &= ~(1 << INT0);
|
EIMSK &= ~(1 << INT0);
|
||||||
break;
|
break;
|
||||||
@ -132,10 +149,27 @@ void detachInterrupt(uint8_t interruptNum) {
|
|||||||
break;
|
break;
|
||||||
#else
|
#else
|
||||||
case 0:
|
case 0:
|
||||||
|
#if defined(EIMSK) && defined(INT0)
|
||||||
EIMSK &= ~(1 << INT0);
|
EIMSK &= ~(1 << INT0);
|
||||||
|
#elif defined(GICR) && defined(ISC00)
|
||||||
|
GICR &= ~(1 << INT0); // atmega32
|
||||||
|
#elif defined(GIMSK) && defined(INT0)
|
||||||
|
GIMSK &= ~(1 << INT0);
|
||||||
|
#else
|
||||||
|
#error detachInterrupt not finished for this cpu
|
||||||
|
#endif
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 1:
|
case 1:
|
||||||
|
#if defined(EIMSK) && defined(INT1)
|
||||||
EIMSK &= ~(1 << INT1);
|
EIMSK &= ~(1 << INT1);
|
||||||
|
#elif defined(GICR) && defined(INT1)
|
||||||
|
GICR &= ~(1 << INT1); // atmega32
|
||||||
|
#elif defined(GIMSK) && defined(INT1)
|
||||||
|
GIMSK &= ~(1 << INT1);
|
||||||
|
#else
|
||||||
|
#warning detachInterrupt may need some more work for this cpu (case 1)
|
||||||
|
#endif
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@ -150,7 +184,7 @@ void attachInterruptTwi(void (*userFunc)(void) ) {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
#if defined(EICRA) && defined(EICRB)
|
||||||
|
|
||||||
SIGNAL(INT0_vect) {
|
SIGNAL(INT0_vect) {
|
||||||
if(intFunc[EXTERNAL_INT_2])
|
if(intFunc[EXTERNAL_INT_2])
|
||||||
|
@ -80,7 +80,14 @@ unsigned long micros() {
|
|||||||
|
|
||||||
cli();
|
cli();
|
||||||
m = timer0_overflow_count;
|
m = timer0_overflow_count;
|
||||||
|
#if defined(TCNT0)
|
||||||
t = TCNT0;
|
t = TCNT0;
|
||||||
|
#elif defined(TCNT0L)
|
||||||
|
t = TCNT0L;
|
||||||
|
#else
|
||||||
|
#error TIMER 0 not defined
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#ifdef TIFR0
|
#ifdef TIFR0
|
||||||
if ((TIFR0 & _BV(TOV0)) && (t < 255))
|
if ((TIFR0 & _BV(TOV0)) && (t < 255))
|
||||||
@ -166,23 +173,38 @@ void init()
|
|||||||
// on the ATmega168, timer 0 is also used for fast hardware pwm
|
// on the ATmega168, timer 0 is also used for fast hardware pwm
|
||||||
// (using phase-correct PWM would mean that timer 0 overflowed half as often
|
// (using phase-correct PWM would mean that timer 0 overflowed half as often
|
||||||
// resulting in different millis() behavior on the ATmega8 and ATmega168)
|
// resulting in different millis() behavior on the ATmega8 and ATmega168)
|
||||||
#if !defined(__AVR_ATmega8__)
|
#if defined(TCCR0A) && defined(WGM01)
|
||||||
sbi(TCCR0A, WGM01);
|
sbi(TCCR0A, WGM01);
|
||||||
sbi(TCCR0A, WGM00);
|
sbi(TCCR0A, WGM00);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// set timer 0 prescale factor to 64
|
// set timer 0 prescale factor to 64
|
||||||
#if defined(__AVR_ATmega8__)
|
#if defined(__AVR_ATmega128__)
|
||||||
|
// CPU specific: different values for the ATmega128
|
||||||
|
sbi(TCCR0, CS02);
|
||||||
|
#elif defined(TCCR0) && defined(CS01) && defined(CS00)
|
||||||
|
// this combination is for the standard atmega8
|
||||||
sbi(TCCR0, CS01);
|
sbi(TCCR0, CS01);
|
||||||
sbi(TCCR0, CS00);
|
sbi(TCCR0, CS00);
|
||||||
#else
|
#elif defined(TCCR0B) && defined(CS01) && defined(CS00)
|
||||||
|
// this combination is for the standard 168/328/1280/2560
|
||||||
sbi(TCCR0B, CS01);
|
sbi(TCCR0B, CS01);
|
||||||
sbi(TCCR0B, CS00);
|
sbi(TCCR0B, CS00);
|
||||||
#endif
|
#elif defined(TCCR0A) && defined(CS01) && defined(CS00)
|
||||||
// enable timer 0 overflow interrupt
|
// this combination is for the __AVR_ATmega645__ series
|
||||||
#if defined(__AVR_ATmega8__)
|
sbi(TCCR0A, CS01);
|
||||||
sbi(TIMSK, TOIE0);
|
sbi(TCCR0A, CS00);
|
||||||
#else
|
#else
|
||||||
|
#error Timer 0 prescale factor 64 not set correctly
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// enable timer 0 overflow interrupt
|
||||||
|
#if defined(TIMSK) && defined(TOIE0)
|
||||||
|
sbi(TIMSK, TOIE0);
|
||||||
|
#elif defined(TIMSK0) && defined(TOIE0)
|
||||||
sbi(TIMSK0, TOIE0);
|
sbi(TIMSK0, TOIE0);
|
||||||
|
#else
|
||||||
|
#error Timer 0 overflow interrupt not set correctly
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// timers 1 and 2 are used for phase-correct hardware pwm
|
// timers 1 and 2 are used for phase-correct hardware pwm
|
||||||
@ -193,35 +215,57 @@ void init()
|
|||||||
TCCR1B = 0;
|
TCCR1B = 0;
|
||||||
|
|
||||||
// set timer 1 prescale factor to 64
|
// set timer 1 prescale factor to 64
|
||||||
|
#if defined(TCCR1B) && defined(CS11) && defined(CS10)
|
||||||
sbi(TCCR1B, CS11);
|
sbi(TCCR1B, CS11);
|
||||||
sbi(TCCR1B, CS10);
|
sbi(TCCR1B, CS10);
|
||||||
|
#elif defined(TCCR1) && defined(CS11) && defined(CS10)
|
||||||
|
sbi(TCCR1, CS11);
|
||||||
|
sbi(TCCR1, CS10);
|
||||||
|
#endif
|
||||||
// put timer 1 in 8-bit phase correct pwm mode
|
// put timer 1 in 8-bit phase correct pwm mode
|
||||||
|
#if defined(TCCR1A) && defined(WGM10)
|
||||||
sbi(TCCR1A, WGM10);
|
sbi(TCCR1A, WGM10);
|
||||||
|
#elif defined(TCCR1)
|
||||||
|
#warning this needs to be finished
|
||||||
|
#endif
|
||||||
|
|
||||||
// set timer 2 prescale factor to 64
|
// set timer 2 prescale factor to 64
|
||||||
#if defined(__AVR_ATmega8__)
|
#if defined(TCCR2) && defined(CS22)
|
||||||
sbi(TCCR2, CS22);
|
sbi(TCCR2, CS22);
|
||||||
#else
|
#elif defined(TCCR2B) && defined(CS22)
|
||||||
sbi(TCCR2B, CS22);
|
sbi(TCCR2B, CS22);
|
||||||
#endif
|
|
||||||
// configure timer 2 for phase correct pwm (8-bit)
|
|
||||||
#if defined(__AVR_ATmega8__)
|
|
||||||
sbi(TCCR2, WGM20);
|
|
||||||
#else
|
#else
|
||||||
|
#warning Timer 2 not finished (may not be present on this CPU)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// configure timer 2 for phase correct pwm (8-bit)
|
||||||
|
#if defined(TCCR2) && defined(WGM20)
|
||||||
|
sbi(TCCR2, WGM20);
|
||||||
|
#elif defined(TCCR2A) && defined(WGM20)
|
||||||
sbi(TCCR2A, WGM20);
|
sbi(TCCR2A, WGM20);
|
||||||
|
#else
|
||||||
|
#warning Timer 2 not finished (may not be present on this CPU)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
#if defined(TCCR3B) && defined(CS31) && defined(WGM30)
|
||||||
// set timer 3, 4, 5 prescale factor to 64
|
sbi(TCCR3B, CS31); // set timer 3 prescale factor to 64
|
||||||
sbi(TCCR3B, CS31); sbi(TCCR3B, CS30);
|
sbi(TCCR3B, CS30);
|
||||||
sbi(TCCR4B, CS41); sbi(TCCR4B, CS40);
|
sbi(TCCR3A, WGM30); // put timer 3 in 8-bit phase correct pwm mode
|
||||||
sbi(TCCR5B, CS51); sbi(TCCR5B, CS50);
|
|
||||||
// put timer 3, 4, 5 in 8-bit phase correct pwm mode
|
|
||||||
sbi(TCCR3A, WGM30);
|
|
||||||
sbi(TCCR4A, WGM40);
|
|
||||||
sbi(TCCR5A, WGM50);
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR4B) && defined(CS41) && defined(WGM40)
|
||||||
|
sbi(TCCR4B, CS41); // set timer 4 prescale factor to 64
|
||||||
|
sbi(TCCR4B, CS40);
|
||||||
|
sbi(TCCR4A, WGM40); // put timer 4 in 8-bit phase correct pwm mode
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR5B) && defined(CS51) && defined(WGM50)
|
||||||
|
sbi(TCCR5B, CS51); // set timer 5 prescale factor to 64
|
||||||
|
sbi(TCCR5B, CS50);
|
||||||
|
sbi(TCCR5A, WGM50); // put timer 5 in 8-bit phase correct pwm mode
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(ADCSRA)
|
||||||
// set a2d prescale factor to 128
|
// set a2d prescale factor to 128
|
||||||
// 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
|
// 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
|
||||||
// XXX: this will not work properly for other clock speeds, and
|
// XXX: this will not work properly for other clock speeds, and
|
||||||
@ -232,13 +276,14 @@ void init()
|
|||||||
|
|
||||||
// enable a2d conversions
|
// enable a2d conversions
|
||||||
sbi(ADCSRA, ADEN);
|
sbi(ADCSRA, ADEN);
|
||||||
|
#endif
|
||||||
|
|
||||||
// the bootloader connects pins 0 and 1 to the USART; disconnect them
|
// the bootloader connects pins 0 and 1 to the USART; disconnect them
|
||||||
// here so they can be used as normal digital i/o; they will be
|
// here so they can be used as normal digital i/o; they will be
|
||||||
// reconnected in Serial.begin()
|
// reconnected in Serial.begin()
|
||||||
#if defined(__AVR_ATmega8__)
|
#if defined(UCSRB)
|
||||||
UCSRB = 0;
|
UCSRB = 0;
|
||||||
#else
|
#elif defined(UCSR0B)
|
||||||
UCSR0B = 0;
|
UCSR0B = 0;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
@ -85,8 +85,8 @@ extern "C"{
|
|||||||
#define noInterrupts() cli()
|
#define noInterrupts() cli()
|
||||||
|
|
||||||
#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
|
#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
|
||||||
#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
|
#define clockCyclesToMicroseconds(a) ( ((a) * 1000L) / (F_CPU / 1000L) )
|
||||||
#define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )
|
#define microsecondsToClockCycles(a) ( ((a) * (F_CPU / 1000L)) / 1000L )
|
||||||
|
|
||||||
#define lowByte(w) ((uint8_t) ((w) & 0xff))
|
#define lowByte(w) ((uint8_t) ((w) & 0xff))
|
||||||
#define highByte(w) ((uint8_t) ((w) >> 8))
|
#define highByte(w) ((uint8_t) ((w) >> 8))
|
||||||
|
262
hardware/arduino/cores/arduino/wiring_analog.c
Executable file → Normal file
262
hardware/arduino/cores/arduino/wiring_analog.c
Executable file → Normal file
@ -19,6 +19,8 @@
|
|||||||
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
||||||
Boston, MA 02111-1307 USA
|
Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
|
Modified 28 September 2010 by Mark Sproul
|
||||||
|
|
||||||
$Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
|
$Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -41,22 +43,27 @@ int analogRead(uint8_t pin)
|
|||||||
|
|
||||||
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
||||||
if (pin >= 54) pin -= 54; // allow for channel or pin numbers
|
if (pin >= 54) pin -= 54; // allow for channel or pin numbers
|
||||||
|
#else
|
||||||
|
if (pin >= 14) pin -= 14; // allow for channel or pin numbers
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(ADCSRB) && defined(MUX5)
|
||||||
// the MUX5 bit of ADCSRB selects whether we're reading from channels
|
// the MUX5 bit of ADCSRB selects whether we're reading from channels
|
||||||
// 0 to 7 (MUX5 low) or 8 to 15 (MUX5 high).
|
// 0 to 7 (MUX5 low) or 8 to 15 (MUX5 high).
|
||||||
ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5);
|
ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5);
|
||||||
#else
|
|
||||||
if (pin >= 14) pin -= 14; // allow for channel or pin numbers
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// set the analog reference (high two bits of ADMUX) and select the
|
// set the analog reference (high two bits of ADMUX) and select the
|
||||||
// channel (low 4 bits). this also sets ADLAR (left-adjust result)
|
// channel (low 4 bits). this also sets ADLAR (left-adjust result)
|
||||||
// to 0 (the default).
|
// to 0 (the default).
|
||||||
|
#if defined(ADMUX)
|
||||||
ADMUX = (analog_reference << 6) | (pin & 0x07);
|
ADMUX = (analog_reference << 6) | (pin & 0x07);
|
||||||
|
#endif
|
||||||
|
|
||||||
// without a delay, we seem to read from the wrong channel
|
// without a delay, we seem to read from the wrong channel
|
||||||
//delay(1);
|
//delay(1);
|
||||||
|
|
||||||
|
#if defined(ADCSRA) && defined(ADCL)
|
||||||
// start the conversion
|
// start the conversion
|
||||||
sbi(ADCSRA, ADSC);
|
sbi(ADCSRA, ADSC);
|
||||||
|
|
||||||
@ -69,6 +76,11 @@ int analogRead(uint8_t pin)
|
|||||||
// as ADCL and ADCH would be locked when it completed.
|
// as ADCL and ADCH would be locked when it completed.
|
||||||
low = ADCL;
|
low = ADCL;
|
||||||
high = ADCH;
|
high = ADCH;
|
||||||
|
#else
|
||||||
|
// we dont have an ADC, return 0
|
||||||
|
low = 0;
|
||||||
|
high = 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
// combine the two bytes
|
// combine the two bytes
|
||||||
return (high << 8) | low;
|
return (high << 8) | low;
|
||||||
@ -86,98 +98,162 @@ void analogWrite(uint8_t pin, int val)
|
|||||||
// for consistenty with Wiring, which doesn't require a pinMode
|
// for consistenty with Wiring, which doesn't require a pinMode
|
||||||
// call for the analog output pins.
|
// call for the analog output pins.
|
||||||
pinMode(pin, OUTPUT);
|
pinMode(pin, OUTPUT);
|
||||||
|
if (val == 0)
|
||||||
if (digitalPinToTimer(pin) == TIMER1A) {
|
{
|
||||||
// connect pwm to pin on timer 1, channel A
|
|
||||||
sbi(TCCR1A, COM1A1);
|
|
||||||
// set pwm duty
|
|
||||||
OCR1A = val;
|
|
||||||
} else if (digitalPinToTimer(pin) == TIMER1B) {
|
|
||||||
// connect pwm to pin on timer 1, channel B
|
|
||||||
sbi(TCCR1A, COM1B1);
|
|
||||||
// set pwm duty
|
|
||||||
OCR1B = val;
|
|
||||||
#if defined(__AVR_ATmega8__)
|
|
||||||
} else if (digitalPinToTimer(pin) == TIMER2) {
|
|
||||||
// connect pwm to pin on timer 2, channel B
|
|
||||||
sbi(TCCR2, COM21);
|
|
||||||
// set pwm duty
|
|
||||||
OCR2 = val;
|
|
||||||
#else
|
|
||||||
} else if (digitalPinToTimer(pin) == TIMER0A) {
|
|
||||||
if (val == 0) {
|
|
||||||
digitalWrite(pin, LOW);
|
digitalWrite(pin, LOW);
|
||||||
} else {
|
|
||||||
// connect pwm to pin on timer 0, channel A
|
|
||||||
sbi(TCCR0A, COM0A1);
|
|
||||||
// set pwm duty
|
|
||||||
OCR0A = val;
|
|
||||||
}
|
}
|
||||||
} else if (digitalPinToTimer(pin) == TIMER0B) {
|
else if (val == 255)
|
||||||
if (val == 0) {
|
{
|
||||||
digitalWrite(pin, LOW);
|
|
||||||
} else {
|
|
||||||
// connect pwm to pin on timer 0, channel B
|
|
||||||
sbi(TCCR0A, COM0B1);
|
|
||||||
// set pwm duty
|
|
||||||
OCR0B = val;
|
|
||||||
}
|
|
||||||
} else if (digitalPinToTimer(pin) == TIMER2A) {
|
|
||||||
// connect pwm to pin on timer 2, channel A
|
|
||||||
sbi(TCCR2A, COM2A1);
|
|
||||||
// set pwm duty
|
|
||||||
OCR2A = val;
|
|
||||||
} else if (digitalPinToTimer(pin) == TIMER2B) {
|
|
||||||
// connect pwm to pin on timer 2, channel B
|
|
||||||
sbi(TCCR2A, COM2B1);
|
|
||||||
// set pwm duty
|
|
||||||
OCR2B = val;
|
|
||||||
#endif
|
|
||||||
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
|
||||||
// XXX: need to handle other timers here
|
|
||||||
} else if (digitalPinToTimer(pin) == TIMER3A) {
|
|
||||||
// connect pwm to pin on timer 3, channel A
|
|
||||||
sbi(TCCR3A, COM3A1);
|
|
||||||
// set pwm duty
|
|
||||||
OCR3A = val;
|
|
||||||
} else if (digitalPinToTimer(pin) == TIMER3B) {
|
|
||||||
// connect pwm to pin on timer 3, channel B
|
|
||||||
sbi(TCCR3A, COM3B1);
|
|
||||||
// set pwm duty
|
|
||||||
OCR3B = val;
|
|
||||||
} else if (digitalPinToTimer(pin) == TIMER3C) {
|
|
||||||
// connect pwm to pin on timer 3, channel C
|
|
||||||
sbi(TCCR3A, COM3C1);
|
|
||||||
// set pwm duty
|
|
||||||
OCR3C = val;
|
|
||||||
} else if (digitalPinToTimer(pin) == TIMER4A) {
|
|
||||||
// connect pwm to pin on timer 4, channel A
|
|
||||||
sbi(TCCR4A, COM4A1);
|
|
||||||
// set pwm duty
|
|
||||||
OCR4A = val;
|
|
||||||
} else if (digitalPinToTimer(pin) == TIMER4B) {
|
|
||||||
// connect pwm to pin on timer 4, channel B
|
|
||||||
sbi(TCCR4A, COM4B1);
|
|
||||||
// set pwm duty
|
|
||||||
OCR4B = val;
|
|
||||||
} else if (digitalPinToTimer(pin) == TIMER4C) {
|
|
||||||
// connect pwm to pin on timer 4, channel C
|
|
||||||
sbi(TCCR4A, COM4C1);
|
|
||||||
// set pwm duty
|
|
||||||
OCR4C = val;
|
|
||||||
} else if (digitalPinToTimer(pin) == TIMER5A) {
|
|
||||||
// connect pwm to pin on timer 5, channel A
|
|
||||||
sbi(TCCR5A, COM5A1);
|
|
||||||
// set pwm duty
|
|
||||||
OCR5A = val;
|
|
||||||
} else if (digitalPinToTimer(pin) == TIMER5B) {
|
|
||||||
// connect pwm to pin on timer 5, channel B
|
|
||||||
sbi(TCCR5A, COM5B1);
|
|
||||||
// set pwm duty
|
|
||||||
OCR5B = val;
|
|
||||||
#endif
|
|
||||||
} else if (val < 128)
|
|
||||||
digitalWrite(pin, LOW);
|
|
||||||
else
|
|
||||||
digitalWrite(pin, HIGH);
|
digitalWrite(pin, HIGH);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
switch(digitalPinToTimer(pin))
|
||||||
|
{
|
||||||
|
// XXX fix needed for atmega8
|
||||||
|
#if defined(TCCR0) && defined(COM00) && !defined(__AVR_ATmega8__)
|
||||||
|
case TIMER0A:
|
||||||
|
// connect pwm to pin on timer 0
|
||||||
|
sbi(TCCR0, COM00);
|
||||||
|
OCR0 = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR0A) && defined(COM0A1)
|
||||||
|
case TIMER0A:
|
||||||
|
// connect pwm to pin on timer 0, channel A
|
||||||
|
sbi(TCCR0A, COM0A1);
|
||||||
|
OCR0A = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR0A) && defined(COM0B1)
|
||||||
|
case TIMER0B:
|
||||||
|
// connect pwm to pin on timer 0, channel B
|
||||||
|
sbi(TCCR0A, COM0B1);
|
||||||
|
OCR0B = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR1A) && defined(COM1A1)
|
||||||
|
case TIMER1A:
|
||||||
|
// connect pwm to pin on timer 1, channel A
|
||||||
|
sbi(TCCR1A, COM1A1);
|
||||||
|
OCR1A = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR1A) && defined(COM1B1)
|
||||||
|
case TIMER1B:
|
||||||
|
// connect pwm to pin on timer 1, channel B
|
||||||
|
sbi(TCCR1A, COM1B1);
|
||||||
|
OCR1B = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR2) && defined(COM21)
|
||||||
|
case TIMER2:
|
||||||
|
// connect pwm to pin on timer 2
|
||||||
|
sbi(TCCR2, COM21);
|
||||||
|
OCR2 = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR2A) && defined(COM2A1)
|
||||||
|
case TIMER2A:
|
||||||
|
// connect pwm to pin on timer 2, channel A
|
||||||
|
sbi(TCCR2A, COM2A1);
|
||||||
|
OCR2A = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR2A) && defined(COM2B1)
|
||||||
|
case TIMER2B:
|
||||||
|
// connect pwm to pin on timer 2, channel B
|
||||||
|
sbi(TCCR2A, COM2B1);
|
||||||
|
OCR2B = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR3A) && defined(COM3A1)
|
||||||
|
case TIMER3A:
|
||||||
|
// connect pwm to pin on timer 3, channel A
|
||||||
|
sbi(TCCR3A, COM3A1);
|
||||||
|
OCR3A = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR3A) && defined(COM3B1)
|
||||||
|
case TIMER3B:
|
||||||
|
// connect pwm to pin on timer 3, channel B
|
||||||
|
sbi(TCCR3A, COM3B1);
|
||||||
|
OCR3B = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR3A) && defined(COM3C1)
|
||||||
|
case TIMER3C:
|
||||||
|
// connect pwm to pin on timer 3, channel C
|
||||||
|
sbi(TCCR3A, COM3C1);
|
||||||
|
OCR3C = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR4A) && defined(COM4A1)
|
||||||
|
case TIMER4A:
|
||||||
|
// connect pwm to pin on timer 4, channel A
|
||||||
|
sbi(TCCR4A, COM4A1);
|
||||||
|
OCR4A = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR4A) && defined(COM4B1)
|
||||||
|
case TIMER4B:
|
||||||
|
// connect pwm to pin on timer 4, channel B
|
||||||
|
sbi(TCCR4A, COM4B1);
|
||||||
|
OCR4B = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR4A) && defined(COM4C1)
|
||||||
|
case TIMER4C:
|
||||||
|
// connect pwm to pin on timer 4, channel C
|
||||||
|
sbi(TCCR4A, COM4C1);
|
||||||
|
OCR4C = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR5A) && defined(COM5A1)
|
||||||
|
case TIMER5A:
|
||||||
|
// connect pwm to pin on timer 5, channel A
|
||||||
|
sbi(TCCR5A, COM5A1);
|
||||||
|
OCR5A = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR5A) && defined(COM5B1)
|
||||||
|
case TIMER5B:
|
||||||
|
// connect pwm to pin on timer 5, channel B
|
||||||
|
sbi(TCCR5A, COM5B1);
|
||||||
|
OCR5B = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR5A) && defined(COM5C1)
|
||||||
|
case TIMER5C:
|
||||||
|
// connect pwm to pin on timer 5, channel C
|
||||||
|
sbi(TCCR5A, COM5C1);
|
||||||
|
OCR5C = val; // set pwm duty
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
case NOT_ON_TIMER:
|
||||||
|
default:
|
||||||
|
if (val < 128) {
|
||||||
|
digitalWrite(pin, LOW);
|
||||||
|
} else {
|
||||||
|
digitalWrite(pin, HIGH);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -19,6 +19,8 @@
|
|||||||
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
||||||
Boston, MA 02111-1307 USA
|
Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
|
Modified 28 September 2010 by Mark Sproul
|
||||||
|
|
||||||
$Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
|
$Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -56,32 +58,67 @@ void pinMode(uint8_t pin, uint8_t mode)
|
|||||||
// But shouldn't this be moved into pinMode? Seems silly to check and do on
|
// But shouldn't this be moved into pinMode? Seems silly to check and do on
|
||||||
// each digitalread or write.
|
// each digitalread or write.
|
||||||
//
|
//
|
||||||
static inline void turnOffPWM(uint8_t timer) __attribute__ ((always_inline));
|
// Mark Sproul:
|
||||||
static inline void turnOffPWM(uint8_t timer)
|
// - Removed inline. Save 170 bytes on atmega1280
|
||||||
|
// - changed to a switch statment; added 32 bytes but much easier to read and maintain.
|
||||||
|
// - Added more #ifdefs, now compiles for atmega645
|
||||||
|
//
|
||||||
|
//static inline void turnOffPWM(uint8_t timer) __attribute__ ((always_inline));
|
||||||
|
//static inline void turnOffPWM(uint8_t timer)
|
||||||
|
static void turnOffPWM(uint8_t timer)
|
||||||
{
|
{
|
||||||
if (timer == TIMER1A) cbi(TCCR1A, COM1A1);
|
switch (timer)
|
||||||
if (timer == TIMER1B) cbi(TCCR1A, COM1B1);
|
{
|
||||||
|
#if defined(TCCR1A) && defined(COM1A1)
|
||||||
#if defined(__AVR_ATmega8__)
|
case TIMER1A: cbi(TCCR1A, COM1A1); break;
|
||||||
if (timer == TIMER2) cbi(TCCR2, COM21);
|
#endif
|
||||||
#else
|
#if defined(TCCR1A) && defined(COM1B1)
|
||||||
if (timer == TIMER0A) cbi(TCCR0A, COM0A1);
|
case TIMER1B: cbi(TCCR1A, COM1B1); break;
|
||||||
if (timer == TIMER0B) cbi(TCCR0A, COM0B1);
|
|
||||||
if (timer == TIMER2A) cbi(TCCR2A, COM2A1);
|
|
||||||
if (timer == TIMER2B) cbi(TCCR2A, COM2B1);
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
#if defined(TCCR2) && defined(COM21)
|
||||||
if (timer == TIMER3A) cbi(TCCR3A, COM3A1);
|
case TIMER2: cbi(TCCR2, COM21); break;
|
||||||
if (timer == TIMER3B) cbi(TCCR3A, COM3B1);
|
|
||||||
if (timer == TIMER3C) cbi(TCCR3A, COM3C1);
|
|
||||||
if (timer == TIMER4A) cbi(TCCR4A, COM4A1);
|
|
||||||
if (timer == TIMER4B) cbi(TCCR4A, COM4B1);
|
|
||||||
if (timer == TIMER4C) cbi(TCCR4A, COM4C1);
|
|
||||||
if (timer == TIMER5A) cbi(TCCR5A, COM5A1);
|
|
||||||
if (timer == TIMER5B) cbi(TCCR5A, COM5B1);
|
|
||||||
if (timer == TIMER5C) cbi(TCCR5A, COM5C1);
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR0A) && defined(COM0A1)
|
||||||
|
case TIMER0A: cbi(TCCR0A, COM0A1); break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TIMER0B) && defined(COM0B1)
|
||||||
|
case TIMER0B: cbi(TCCR0A, COM0B1); break;
|
||||||
|
#endif
|
||||||
|
#if defined(TCCR2A) && defined(COM2A1)
|
||||||
|
case TIMER2A: cbi(TCCR2A, COM2A1); break;
|
||||||
|
#endif
|
||||||
|
#if defined(TCCR2A) && defined(COM2B1)
|
||||||
|
case TIMER2B: cbi(TCCR2A, COM2B1); break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR3A) && defined(COM3A1)
|
||||||
|
case TIMER3A: cbi(TCCR3A, COM3A1); break;
|
||||||
|
#endif
|
||||||
|
#if defined(TCCR3A) && defined(COM3B1)
|
||||||
|
case TIMER3B: cbi(TCCR3A, COM3B1); break;
|
||||||
|
#endif
|
||||||
|
#if defined(TCCR3A) && defined(COM3C1)
|
||||||
|
case TIMER3C: cbi(TCCR3A, COM3C1); break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(TCCR4A) && defined(COM4A1)
|
||||||
|
case TIMER4A: cbi(TCCR4A, COM4A1); break;
|
||||||
|
#endif
|
||||||
|
#if defined(TCCR4A) && defined(COM4B1)
|
||||||
|
case TIMER4B: cbi(TCCR4A, COM4B1); break;
|
||||||
|
#endif
|
||||||
|
#if defined(TCCR4A) && defined(COM4C1)
|
||||||
|
case TIMER4C: cbi(TCCR4A, COM4C1); break;
|
||||||
|
#endif
|
||||||
|
#if defined(TCCR5A)
|
||||||
|
case TIMER5A: cbi(TCCR5A, COM5A1); break;
|
||||||
|
case TIMER5B: cbi(TCCR5A, COM5B1); break;
|
||||||
|
case TIMER5C: cbi(TCCR5A, COM5C1); break;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void digitalWrite(uint8_t pin, uint8_t val)
|
void digitalWrite(uint8_t pin, uint8_t val)
|
||||||
|
@ -0,0 +1,436 @@
|
|||||||
|
/*
|
||||||
|
This introduces modifications on the normal Firmata made for Arduino so that the LED
|
||||||
|
blinks until receiving the first command over serial.
|
||||||
|
|
||||||
|
Copyright (C) 2010 David Cuartielles. All rights reserved.
|
||||||
|
|
||||||
|
based at 99.9% on Firmata by HC Steiner according to the following license terms:
|
||||||
|
|
||||||
|
Copyright (C) 2006-2008 Hans-Christoph Steiner. All rights 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.
|
||||||
|
|
||||||
|
See file LICENSE.txt for further informations on licensing terms.
|
||||||
|
|
||||||
|
formatted using the GNU C formatting and indenting
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* TODO: use Program Control to load stored profiles from EEPROM
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <Servo.h>
|
||||||
|
#include <Firmata.h>
|
||||||
|
|
||||||
|
/*==============================================================================
|
||||||
|
* GLOBAL VARIABLES
|
||||||
|
*============================================================================*/
|
||||||
|
|
||||||
|
/* has the command arrived? */
|
||||||
|
boolean firstCommand = false;
|
||||||
|
int dataOnSerial = 0;
|
||||||
|
boolean statusLed = false;
|
||||||
|
|
||||||
|
/* analog inputs */
|
||||||
|
int analogInputsToReport = 0; // bitwise array to store pin reporting
|
||||||
|
|
||||||
|
/* digital input ports */
|
||||||
|
byte reportPINs[TOTAL_PORTS]; // 1 = report this port, 0 = silence
|
||||||
|
byte previousPINs[TOTAL_PORTS]; // previous 8 bits sent
|
||||||
|
|
||||||
|
/* pins configuration */
|
||||||
|
byte pinConfig[TOTAL_PINS]; // configuration of every pin
|
||||||
|
byte portConfigInputs[TOTAL_PORTS]; // each bit: 1 = pin in INPUT, 0 = anything else
|
||||||
|
int pinState[TOTAL_PINS]; // any value that has been written
|
||||||
|
|
||||||
|
/* timer variables */
|
||||||
|
unsigned long currentMillis; // store the current value from millis()
|
||||||
|
unsigned long previousMillis; // for comparison with currentMillis
|
||||||
|
int samplingInterval = 19; // how often to run the main loop (in ms)
|
||||||
|
unsigned long toggleMillis;
|
||||||
|
|
||||||
|
Servo servos[MAX_SERVOS];
|
||||||
|
|
||||||
|
/*==============================================================================
|
||||||
|
* FUNCTIONS
|
||||||
|
*============================================================================*/
|
||||||
|
|
||||||
|
void toggleLed()
|
||||||
|
{
|
||||||
|
if (millis() - toggleMillis > 500) {
|
||||||
|
statusLed = !statusLed;
|
||||||
|
digitalWrite(13, statusLed);
|
||||||
|
toggleMillis = millis();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void outputPort(byte portNumber, byte portValue, byte forceSend)
|
||||||
|
{
|
||||||
|
// pins not configured as INPUT are cleared to zeros
|
||||||
|
portValue = portValue & portConfigInputs[portNumber];
|
||||||
|
// only send if the value is different than previously sent
|
||||||
|
if(forceSend || previousPINs[portNumber] != portValue) {
|
||||||
|
Firmata.sendDigitalPort(portNumber, portValue);
|
||||||
|
previousPINs[portNumber] = portValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* -----------------------------------------------------------------------------
|
||||||
|
* check all the active digital inputs for change of state, then add any events
|
||||||
|
* to the Serial output queue using Serial.print() */
|
||||||
|
void checkDigitalInputs(void)
|
||||||
|
{
|
||||||
|
/* Using non-looping code allows constants to be given to readPort().
|
||||||
|
* The compiler will apply substantial optimizations if the inputs
|
||||||
|
* to readPort() are compile-time constants. */
|
||||||
|
if (TOTAL_PORTS > 0 && reportPINs[0]) outputPort(0, readPort(0, portConfigInputs[0]), false);
|
||||||
|
if (TOTAL_PORTS > 1 && reportPINs[1]) outputPort(1, readPort(1, portConfigInputs[1]), false);
|
||||||
|
if (TOTAL_PORTS > 2 && reportPINs[2]) outputPort(2, readPort(2, portConfigInputs[2]), false);
|
||||||
|
if (TOTAL_PORTS > 3 && reportPINs[3]) outputPort(3, readPort(3, portConfigInputs[3]), false);
|
||||||
|
if (TOTAL_PORTS > 4 && reportPINs[4]) outputPort(4, readPort(4, portConfigInputs[4]), false);
|
||||||
|
if (TOTAL_PORTS > 5 && reportPINs[5]) outputPort(5, readPort(5, portConfigInputs[5]), false);
|
||||||
|
if (TOTAL_PORTS > 6 && reportPINs[6]) outputPort(6, readPort(6, portConfigInputs[6]), false);
|
||||||
|
if (TOTAL_PORTS > 7 && reportPINs[7]) outputPort(7, readPort(7, portConfigInputs[7]), false);
|
||||||
|
if (TOTAL_PORTS > 8 && reportPINs[8]) outputPort(8, readPort(8, portConfigInputs[8]), false);
|
||||||
|
if (TOTAL_PORTS > 9 && reportPINs[9]) outputPort(9, readPort(9, portConfigInputs[9]), false);
|
||||||
|
if (TOTAL_PORTS > 10 && reportPINs[10]) outputPort(10, readPort(10, portConfigInputs[10]), false);
|
||||||
|
if (TOTAL_PORTS > 11 && reportPINs[11]) outputPort(11, readPort(11, portConfigInputs[11]), false);
|
||||||
|
if (TOTAL_PORTS > 12 && reportPINs[12]) outputPort(12, readPort(12, portConfigInputs[12]), false);
|
||||||
|
if (TOTAL_PORTS > 13 && reportPINs[13]) outputPort(13, readPort(13, portConfigInputs[13]), false);
|
||||||
|
if (TOTAL_PORTS > 14 && reportPINs[14]) outputPort(14, readPort(14, portConfigInputs[14]), false);
|
||||||
|
if (TOTAL_PORTS > 15 && reportPINs[15]) outputPort(15, readPort(15, portConfigInputs[15]), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
/* sets the pin mode to the correct state and sets the relevant bits in the
|
||||||
|
* two bit-arrays that track Digital I/O and PWM status
|
||||||
|
*/
|
||||||
|
void setPinModeCallback(byte pin, int mode)
|
||||||
|
{
|
||||||
|
if (IS_PIN_SERVO(pin) && mode != SERVO && servos[PIN_TO_SERVO(pin)].attached()) {
|
||||||
|
servos[PIN_TO_SERVO(pin)].detach();
|
||||||
|
}
|
||||||
|
if (IS_PIN_ANALOG(pin)) {
|
||||||
|
reportAnalogCallback(PIN_TO_ANALOG(pin), mode == ANALOG ? 1 : 0); // turn on/off reporting
|
||||||
|
}
|
||||||
|
if (IS_PIN_DIGITAL(pin)) {
|
||||||
|
if (mode == INPUT) {
|
||||||
|
portConfigInputs[pin/8] |= (1 << (pin & 7));
|
||||||
|
} else {
|
||||||
|
portConfigInputs[pin/8] &= ~(1 << (pin & 7));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pinState[pin] = 0;
|
||||||
|
switch(mode) {
|
||||||
|
case ANALOG:
|
||||||
|
if (IS_PIN_ANALOG(pin)) {
|
||||||
|
if (IS_PIN_DIGITAL(pin)) {
|
||||||
|
pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver
|
||||||
|
digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups
|
||||||
|
}
|
||||||
|
pinConfig[pin] = ANALOG;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case INPUT:
|
||||||
|
if (IS_PIN_DIGITAL(pin)) {
|
||||||
|
pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver
|
||||||
|
digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups
|
||||||
|
pinConfig[pin] = INPUT;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case OUTPUT:
|
||||||
|
if (IS_PIN_DIGITAL(pin)) {
|
||||||
|
digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable PWM
|
||||||
|
pinMode(PIN_TO_DIGITAL(pin), OUTPUT);
|
||||||
|
pinConfig[pin] = OUTPUT;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case PWM:
|
||||||
|
if (IS_PIN_PWM(pin)) {
|
||||||
|
pinMode(PIN_TO_PWM(pin), OUTPUT);
|
||||||
|
analogWrite(PIN_TO_PWM(pin), 0);
|
||||||
|
pinConfig[pin] = PWM;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case SERVO:
|
||||||
|
if (IS_PIN_SERVO(pin)) {
|
||||||
|
pinConfig[pin] = SERVO;
|
||||||
|
if (!servos[PIN_TO_SERVO(pin)].attached()) {
|
||||||
|
servos[PIN_TO_SERVO(pin)].attach(PIN_TO_DIGITAL(pin));
|
||||||
|
} else {
|
||||||
|
Firmata.sendString("Servo only on pins from 2 to 13");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case I2C:
|
||||||
|
pinConfig[pin] = mode;
|
||||||
|
Firmata.sendString("I2C mode not yet supported");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
Firmata.sendString("Unknown pin mode"); // TODO: put error msgs in EEPROM
|
||||||
|
}
|
||||||
|
// TODO: save status to EEPROM here, if changed
|
||||||
|
}
|
||||||
|
|
||||||
|
void analogWriteCallback(byte pin, int value)
|
||||||
|
{
|
||||||
|
if (pin < TOTAL_PINS) {
|
||||||
|
switch(pinConfig[pin]) {
|
||||||
|
case SERVO:
|
||||||
|
if (IS_PIN_SERVO(pin))
|
||||||
|
servos[PIN_TO_SERVO(pin)].write(value);
|
||||||
|
pinState[pin] = value;
|
||||||
|
break;
|
||||||
|
case PWM:
|
||||||
|
if (IS_PIN_PWM(pin))
|
||||||
|
analogWrite(PIN_TO_PWM(pin), value);
|
||||||
|
pinState[pin] = value;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void digitalWriteCallback(byte port, int value)
|
||||||
|
{
|
||||||
|
byte pin, lastPin, mask=1, pinWriteMask=0;
|
||||||
|
|
||||||
|
if (port < TOTAL_PORTS) {
|
||||||
|
// create a mask of the pins on this port that are writable.
|
||||||
|
lastPin = port*8+8;
|
||||||
|
if (lastPin > TOTAL_PINS) lastPin = TOTAL_PINS;
|
||||||
|
for (pin=port*8; pin < lastPin; pin++) {
|
||||||
|
// do not disturb non-digital pins (eg, Rx & Tx)
|
||||||
|
if (IS_PIN_DIGITAL(pin)) {
|
||||||
|
// only write to OUTPUT and INPUT (enables pullup)
|
||||||
|
// do not touch pins in PWM, ANALOG, SERVO or other modes
|
||||||
|
if (pinConfig[pin] == OUTPUT || pinConfig[pin] == INPUT) {
|
||||||
|
pinWriteMask |= mask;
|
||||||
|
pinState[pin] = ((byte)value & mask) ? 1 : 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mask = mask << 1;
|
||||||
|
}
|
||||||
|
writePort(port, (byte)value, pinWriteMask);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
/* sets bits in a bit array (int) to toggle the reporting of the analogIns
|
||||||
|
*/
|
||||||
|
//void FirmataClass::setAnalogPinReporting(byte pin, byte state) {
|
||||||
|
//}
|
||||||
|
void reportAnalogCallback(byte analogPin, int value)
|
||||||
|
{
|
||||||
|
if (analogPin < TOTAL_ANALOG_PINS) {
|
||||||
|
if(value == 0) {
|
||||||
|
analogInputsToReport = analogInputsToReport &~ (1 << analogPin);
|
||||||
|
} else {
|
||||||
|
analogInputsToReport = analogInputsToReport | (1 << analogPin);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TODO: save status to EEPROM here, if changed
|
||||||
|
}
|
||||||
|
|
||||||
|
void reportDigitalCallback(byte port, int value)
|
||||||
|
{
|
||||||
|
if (port < TOTAL_PORTS) {
|
||||||
|
reportPINs[port] = (byte)value;
|
||||||
|
}
|
||||||
|
// do not disable analog reporting on these 8 pins, to allow some
|
||||||
|
// pins used for digital, others analog. Instead, allow both types
|
||||||
|
// of reporting to be enabled, but check if the pin is configured
|
||||||
|
// as analog when sampling the analog inputs. Likewise, while
|
||||||
|
// scanning digital pins, portConfigInputs will mask off values from any
|
||||||
|
// pins configured as analog
|
||||||
|
}
|
||||||
|
|
||||||
|
/*==============================================================================
|
||||||
|
* SYSEX-BASED commands
|
||||||
|
*============================================================================*/
|
||||||
|
|
||||||
|
void sysexCallback(byte command, byte argc, byte *argv)
|
||||||
|
{
|
||||||
|
switch(command) {
|
||||||
|
case SERVO_CONFIG:
|
||||||
|
if(argc > 4) {
|
||||||
|
// these vars are here for clarity, they'll optimized away by the compiler
|
||||||
|
byte pin = argv[0];
|
||||||
|
int minPulse = argv[1] + (argv[2] << 7);
|
||||||
|
int maxPulse = argv[3] + (argv[4] << 7);
|
||||||
|
|
||||||
|
if (IS_PIN_SERVO(pin)) {
|
||||||
|
// servos are pins from 2 to 13, so offset for array
|
||||||
|
if (servos[PIN_TO_SERVO(pin)].attached())
|
||||||
|
servos[PIN_TO_SERVO(pin)].detach();
|
||||||
|
servos[PIN_TO_SERVO(pin)].attach(PIN_TO_DIGITAL(pin), minPulse, maxPulse);
|
||||||
|
setPinModeCallback(pin, SERVO);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case SAMPLING_INTERVAL:
|
||||||
|
if (argc > 1)
|
||||||
|
samplingInterval = argv[0] + (argv[1] << 7);
|
||||||
|
else
|
||||||
|
Firmata.sendString("Not enough data");
|
||||||
|
break;
|
||||||
|
case EXTENDED_ANALOG:
|
||||||
|
if (argc > 1) {
|
||||||
|
int val = argv[1];
|
||||||
|
if (argc > 2) val |= (argv[2] << 7);
|
||||||
|
if (argc > 3) val |= (argv[3] << 14);
|
||||||
|
analogWriteCallback(argv[0], val);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case CAPABILITY_QUERY:
|
||||||
|
Serial.write(START_SYSEX);
|
||||||
|
Serial.write(CAPABILITY_RESPONSE);
|
||||||
|
for (byte pin=0; pin < TOTAL_PINS; pin++) {
|
||||||
|
if (IS_PIN_DIGITAL(pin)) {
|
||||||
|
Serial.write((byte)INPUT);
|
||||||
|
Serial.write(1);
|
||||||
|
Serial.write((byte)OUTPUT);
|
||||||
|
Serial.write(1);
|
||||||
|
}
|
||||||
|
if (IS_PIN_ANALOG(pin)) {
|
||||||
|
Serial.write(ANALOG);
|
||||||
|
Serial.write(10);
|
||||||
|
}
|
||||||
|
if (IS_PIN_PWM(pin)) {
|
||||||
|
Serial.write(PWM);
|
||||||
|
Serial.write(8);
|
||||||
|
}
|
||||||
|
if (IS_PIN_SERVO(pin)) {
|
||||||
|
Serial.write(SERVO);
|
||||||
|
Serial.write(14);
|
||||||
|
}
|
||||||
|
Serial.write(127);
|
||||||
|
}
|
||||||
|
Serial.write(END_SYSEX);
|
||||||
|
break;
|
||||||
|
case PIN_STATE_QUERY:
|
||||||
|
if (argc > 0) {
|
||||||
|
byte pin=argv[0];
|
||||||
|
Serial.write(START_SYSEX);
|
||||||
|
Serial.write(PIN_STATE_RESPONSE);
|
||||||
|
Serial.write(pin);
|
||||||
|
if (pin < TOTAL_PINS) {
|
||||||
|
Serial.write((byte)pinConfig[pin]);
|
||||||
|
Serial.write((byte)pinState[pin] & 0x7F);
|
||||||
|
if (pinState[pin] & 0xFF80) Serial.write((byte)(pinState[pin] >> 7) & 0x7F);
|
||||||
|
if (pinState[pin] & 0xC000) Serial.write((byte)(pinState[pin] >> 14) & 0x7F);
|
||||||
|
}
|
||||||
|
Serial.write(END_SYSEX);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ANALOG_MAPPING_QUERY:
|
||||||
|
Serial.write(START_SYSEX);
|
||||||
|
Serial.write(ANALOG_MAPPING_RESPONSE);
|
||||||
|
for (byte pin=0; pin < TOTAL_PINS; pin++) {
|
||||||
|
Serial.write(IS_PIN_ANALOG(pin) ? PIN_TO_ANALOG(pin) : 127);
|
||||||
|
}
|
||||||
|
Serial.write(END_SYSEX);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*==============================================================================
|
||||||
|
* SETUP()
|
||||||
|
*============================================================================*/
|
||||||
|
void setup()
|
||||||
|
{
|
||||||
|
byte i;
|
||||||
|
|
||||||
|
Firmata.setFirmwareVersion(2, 2);
|
||||||
|
|
||||||
|
Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
|
||||||
|
Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback);
|
||||||
|
Firmata.attach(REPORT_ANALOG, reportAnalogCallback);
|
||||||
|
Firmata.attach(REPORT_DIGITAL, reportDigitalCallback);
|
||||||
|
Firmata.attach(SET_PIN_MODE, setPinModeCallback);
|
||||||
|
Firmata.attach(START_SYSEX, sysexCallback);
|
||||||
|
|
||||||
|
// TODO: load state from EEPROM here
|
||||||
|
|
||||||
|
/* these are initialized to zero by the compiler startup code
|
||||||
|
for (i=0; i < TOTAL_PORTS; i++) {
|
||||||
|
reportPINs[i] = false;
|
||||||
|
portConfigInputs[i] = 0;
|
||||||
|
previousPINs[i] = 0;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
for (i=0; i < TOTAL_PINS; i++) {
|
||||||
|
if (IS_PIN_ANALOG(i)) {
|
||||||
|
// turns off pullup, configures everything
|
||||||
|
setPinModeCallback(i, ANALOG);
|
||||||
|
} else {
|
||||||
|
// sets the output to 0, configures portConfigInputs
|
||||||
|
setPinModeCallback(i, OUTPUT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// by defult, do not report any analog inputs
|
||||||
|
analogInputsToReport = 0;
|
||||||
|
|
||||||
|
Firmata.begin(57600);
|
||||||
|
|
||||||
|
/* send digital inputs to set the initial state on the host computer,
|
||||||
|
* since once in the loop(), this firmware will only send on change */
|
||||||
|
for (i=0; i < TOTAL_PORTS; i++) {
|
||||||
|
outputPort(i, readPort(i, portConfigInputs[i]), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* init the toggleLed counter */
|
||||||
|
toggleMillis = millis();
|
||||||
|
pinMode(13, OUTPUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*==============================================================================
|
||||||
|
* LOOP()
|
||||||
|
*============================================================================*/
|
||||||
|
void loop()
|
||||||
|
{
|
||||||
|
byte pin, analogPin;
|
||||||
|
|
||||||
|
/* DIGITALREAD - as fast as possible, check for changes and output them to the
|
||||||
|
* FTDI buffer using Serial.print() */
|
||||||
|
checkDigitalInputs();
|
||||||
|
|
||||||
|
//XXX: hack Firmata to blink until serial command arrives
|
||||||
|
dataOnSerial = Firmata.available();
|
||||||
|
if (dataOnSerial > 0 && !firstCommand) {
|
||||||
|
firstCommand = true;
|
||||||
|
}
|
||||||
|
//XXX: do the blink if the first command hasn't arrived yet
|
||||||
|
// configures pin 13 as output and then back as input
|
||||||
|
if (!firstCommand) {
|
||||||
|
toggleLed();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* SERIALREAD - processing incoming messagse as soon as possible, while still
|
||||||
|
* checking digital inputs. */
|
||||||
|
while(dataOnSerial) {
|
||||||
|
Firmata.processInput();
|
||||||
|
dataOnSerial = Firmata.available();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* SEND FTDI WRITE BUFFER - make sure that the FTDI buffer doesn't go over
|
||||||
|
* 60 bytes. use a timer to sending an event character every 4 ms to
|
||||||
|
* trigger the buffer to dump. */
|
||||||
|
|
||||||
|
currentMillis = millis();
|
||||||
|
if (currentMillis - previousMillis > samplingInterval) {
|
||||||
|
previousMillis += samplingInterval;
|
||||||
|
/* ANALOGREAD - do all analogReads() at the configured sampling interval */
|
||||||
|
for(pin=0; pin<TOTAL_PINS; pin++) {
|
||||||
|
if (IS_PIN_ANALOG(pin) && pinConfig[pin] == ANALOG) {
|
||||||
|
analogPin = PIN_TO_ANALOG(pin);
|
||||||
|
if (analogInputsToReport & (1 << analogPin)) {
|
||||||
|
Firmata.sendAnalog(analogPin, analogRead(analogPin));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user