mirror of
https://github.com/digistump/DigistumpArduino.git
synced 2025-09-17 17:32:25 -07:00
Initial import of support files for all Digistump boards - Digispark, Pro, DigiX - including libraries, examples, tools, and other support files for the Arduino IDE
This commit is contained in:
178
hardware/digistump/avr/libraries/TinyPinChange/TinyPinChange.cpp
Normal file
178
hardware/digistump/avr/libraries/TinyPinChange/TinyPinChange.cpp
Normal file
@@ -0,0 +1,178 @@
|
||||
/****************************************************************************/
|
||||
/* PROJECT: All based on ATtinyX5, ATtinyX4, ATmega328P */
|
||||
/* MODULE: PinChange */
|
||||
/* VERSION: 1.0 */
|
||||
/* DATE: 30/01/2011 */
|
||||
/* TARGET: ATtinyX5, ATtinyX4, ATmega328P */
|
||||
/* COMPILER: WinAvr (avr-gcc) */
|
||||
/* IDE: AVR Studio 4 */
|
||||
/* PROGRAMER: AVR-JTAG-ICE MKII */
|
||||
/* AUTHOR: P.LOUSSOUARN (P.Loussouarn: http://p.loussouarn.free.fr) */
|
||||
/****************************************************************************/
|
||||
#include <TinyPinChange.h>
|
||||
#include <avr/interrupt.h>
|
||||
|
||||
/*************************************************************************
|
||||
MACROS
|
||||
*************************************************************************/
|
||||
|
||||
#define PIN_CHANGE_HANDLER_MAX_NB 3 /* ISR max number Pin Change ISR can handle */
|
||||
|
||||
|
||||
/*************************************************************************
|
||||
GLOBAL VARIABLES
|
||||
*************************************************************************/
|
||||
struct PinChangeStruct
|
||||
{
|
||||
void (*Isr[PIN_CHANGE_HANDLER_MAX_NB])(void);
|
||||
uint8_t LoadedIsrNb;
|
||||
uint8_t Event;
|
||||
uint8_t PinPrev;
|
||||
uint8_t PinCur;
|
||||
};
|
||||
|
||||
struct PinChangePortStruct
|
||||
{
|
||||
PinChangeStruct Port[PIN_CHG_PORT_NB];
|
||||
};
|
||||
|
||||
static volatile struct PinChangePortStruct PinChange;
|
||||
|
||||
/*************************************************************************
|
||||
INTERRUPT SUB-ROUTINE
|
||||
*************************************************************************/
|
||||
#define DECLARE_PIN_CHANGE_ISR(VirtualPortIdx) \
|
||||
ISR(PCINT##VirtualPortIdx##_vect) \
|
||||
{ \
|
||||
uint8_t Idx; \
|
||||
PinChange.Port[VirtualPortIdx].PinCur=(PC_PIN##VirtualPortIdx)&(PC_PCMSK##VirtualPortIdx); \
|
||||
PinChange.Port[VirtualPortIdx].Event=PinChange.Port[VirtualPortIdx].PinPrev^PinChange.Port[VirtualPortIdx].PinCur; \
|
||||
PinChange.Port[VirtualPortIdx].PinPrev=PinChange.Port[VirtualPortIdx].PinCur; \
|
||||
for(Idx=0;Idx<PinChange.Port[VirtualPortIdx].LoadedIsrNb;Idx++) \
|
||||
{ \
|
||||
PinChange.Port[VirtualPortIdx].Isr[Idx](); \
|
||||
} \
|
||||
}
|
||||
|
||||
DECLARE_PIN_CHANGE_ISR(0)
|
||||
|
||||
#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || !(defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__))
|
||||
DECLARE_PIN_CHANGE_ISR(1)
|
||||
#endif
|
||||
|
||||
#if !defined(__AVR_ATtiny24__) && !defined(__AVR_ATtiny44__) && !defined(__AVR_ATtiny84__) && !defined(__AVR_ATtiny25__) && !defined(__AVR_ATtiny45__) && !defined(__AVR_ATtiny85__)
|
||||
DECLARE_PIN_CHANGE_ISR(2)
|
||||
#endif
|
||||
|
||||
/*************************************************************************
|
||||
PUBLIC FUNCTIONS
|
||||
*************************************************************************/
|
||||
|
||||
/*********************************************************************
|
||||
PinChange Initialization Function
|
||||
Input:
|
||||
Void
|
||||
Output:
|
||||
Void
|
||||
*********************************************************************/
|
||||
void TinyPinChange_Init(void)
|
||||
{
|
||||
#if defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
|
||||
/* ATtinyX5 */
|
||||
PinChange.Port[0].PinCur=PC_PIN0&PC_PCMSK0; //PINB,
|
||||
PinChange.Port[0].PinPrev=PinChange.Port[0].PinCur;
|
||||
#else
|
||||
#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
|
||||
/* ATtinyX4 */
|
||||
PinChange.Port[0].PinCur=PC_PIN0&PC_PCMSK0;//PINA;
|
||||
PinChange.Port[0].PinPrev=PinChange.Port[0].PinCur;
|
||||
PinChange.Port[1].PinCur=PC_PIN1&PC_PCMSK1;//PINB;
|
||||
PinChange.Port[1].PinPrev=PinChange.Port[1].PinCur;
|
||||
#else
|
||||
/* UNO */
|
||||
PinChange.Port[0].PinCur=PC_PIN0&PC_PCMSK0;//PINB;
|
||||
PinChange.Port[0].PinPrev=PinChange.Port[0].PinCur;
|
||||
PinChange.Port[1].PinCur=PC_PIN1&PC_PCMSK1;//PINC;
|
||||
PinChange.Port[1].PinPrev=PinChange.Port[1].PinCur;
|
||||
PinChange.Port[2].PinCur=PC_PIN2&PC_PCMSK2;//PIND;
|
||||
PinChange.Port[2].PinPrev=PinChange.Port[2].PinCur;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
PinChange RegisterIsr Function
|
||||
Input:
|
||||
Pointer on a PinChange Function
|
||||
Output:
|
||||
The associated VirtualPortIdx (0 to 2)
|
||||
< 0 in case of failure
|
||||
*********************************************************************/
|
||||
int8_t TinyPinChange_RegisterIsr(uint8_t Pin, void(*Isr)(void))
|
||||
{
|
||||
int8_t IsrIdx,PortIdx,AlreadyLoaded=0;
|
||||
|
||||
PortIdx=DigitalPinToPortIdx(Pin);
|
||||
|
||||
for(IsrIdx=0;IsrIdx<PIN_CHANGE_HANDLER_MAX_NB;IsrIdx++)
|
||||
{
|
||||
if(PinChange.Port[PortIdx].Isr[IsrIdx]==Isr)
|
||||
{
|
||||
AlreadyLoaded=1;
|
||||
break; /* Already loaded */
|
||||
}
|
||||
}
|
||||
|
||||
if(!AlreadyLoaded)
|
||||
{
|
||||
if(PinChange.Port[PortIdx].LoadedIsrNb<PIN_CHANGE_HANDLER_MAX_NB)
|
||||
{
|
||||
/* Not aready loaded: load it */
|
||||
PinChange.Port[PortIdx].Isr[PinChange.Port[PortIdx].LoadedIsrNb]=Isr;
|
||||
PinChange.Port[PortIdx].LoadedIsrNb++;
|
||||
}
|
||||
else PortIdx=-1; /* Failure */
|
||||
}
|
||||
return(PortIdx);
|
||||
}
|
||||
|
||||
void TinyPinChange_EnablePin(uint8_t Pin)
|
||||
{
|
||||
if(digitalPinToPCICR(Pin))
|
||||
{
|
||||
*digitalPinToPCICR(Pin) |= _BV(digitalPinToPCICRbit(Pin));
|
||||
*digitalPinToPCMSK(Pin) |= _BV(digitalPinToPCMSKbit(Pin));
|
||||
}
|
||||
}
|
||||
|
||||
void TinyPinChange_DisablePin(uint8_t Pin)
|
||||
{
|
||||
if(digitalPinToPCICR(Pin))
|
||||
{
|
||||
*digitalPinToPCMSK(Pin) &= (_BV(digitalPinToPCMSKbit(Pin)) ^ 0xFF);
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
PinChange GetEvent Function
|
||||
Input:
|
||||
Idx: Index of the Port
|
||||
Output:
|
||||
The bits which have been changed in the port
|
||||
*********************************************************************/
|
||||
uint8_t TinyPinChange_GetPinEvent(uint8_t VirtualPortIdx)
|
||||
{
|
||||
return(PinChange.Port[VirtualPortIdx].Event);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
PinChange GetPinCurSt Function
|
||||
Input:
|
||||
Idx: Index of the Port
|
||||
Output:
|
||||
Current Pin Status of the port
|
||||
*********************************************************************/
|
||||
uint8_t TinyPinChange_GetPinCurSt(uint8_t VirtualPortIdx)
|
||||
{
|
||||
return(PinChange.Port[VirtualPortIdx].PinCur);
|
||||
}
|
@@ -0,0 +1,67 @@
|
||||
#ifndef TINY_PIN_CHANGE_H
|
||||
#define TINY_PIN_CHANGE_H 1
|
||||
|
||||
/*
|
||||
* <TinyPinChange>, a library for Pin Change Interrupt by RC Navy (2012)
|
||||
* Supported device ATmega238P (UNO), ATtiny84, ATtiny85
|
||||
*
|
||||
* http://p.loussouarn.free.fr
|
||||
*/
|
||||
|
||||
#if defined(ARDUINO) && ARDUINO >= 100
|
||||
#include "Arduino.h"
|
||||
#else
|
||||
#include "WProgram.h"
|
||||
#endif
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
#if defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
|
||||
/* ATtinyX5 */
|
||||
#define PIN_CHG_PORT_NB 1
|
||||
#define DigitalPinToPortIdx(p) 0
|
||||
#define PC_PIN0 PINB
|
||||
#define PC_PCMSK0 PCMSK
|
||||
#else
|
||||
#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
|
||||
/* ATtinyX4 */
|
||||
#define PIN_CHG_PORT_NB 2
|
||||
#define DigitalPinToPortIdx(p) (((p) <= 7) ? (0) : (((p) <= 10) ? (1) : (0)))
|
||||
#define PC_PIN0 PINA
|
||||
#define PC_PCMSK0 PCMSK0
|
||||
#define PC_PIN1 PINB
|
||||
#define PC_PCMSK1 PCMSK1
|
||||
#else
|
||||
/* UNO */
|
||||
#define PIN_CHG_PORT_NB 3
|
||||
#define DigitalPinToPortIdx(p) (((p) <= 7) ? (2) : (((p) <= 13) ? (0) : (((p) <= 21) ? (1) : (0))))
|
||||
#define PC_PIN0 PINB
|
||||
#define PC_PCMSK0 PCMSK0
|
||||
#define PC_PIN1 PINC
|
||||
#define PC_PCMSK1 PCMSK1
|
||||
#define PC_PIN2 PIND
|
||||
#define PC_PCMSK2 PCMSK2
|
||||
#endif
|
||||
#endif
|
||||
|
||||
void TinyPinChange_Init(void);
|
||||
int8_t TinyPinChange_RegisterIsr(uint8_t Pin, void(*Isr)(void));
|
||||
void TinyPinChange_EnablePin(uint8_t Pin);
|
||||
void TinyPinChange_DisablePin(uint8_t Pin);
|
||||
uint8_t TinyPinChange_GetPinEvent(uint8_t VirtualPortIdx);
|
||||
uint8_t TinyPinChange_GetPinCurSt(uint8_t VirtualPortIdx);
|
||||
#define TinyPinChange_PinToMsk(Pin) _BV(digitalPinToPCMSKbit(Pin))
|
||||
|
||||
/*******************************************************/
|
||||
/* Application Programming Interface (API) en Francais */
|
||||
/*******************************************************/
|
||||
|
||||
/* Methodes en Francais English native methods */
|
||||
#define TinyPinChange_EnregistreFonctionInterruption TinyPinChange_RegisterIsr
|
||||
#define TinyPinChange_ActiveBroche TinyPinChange_EnablePin
|
||||
#define TinyPinChange_DesactiveBroche TinyPinChange_DisablePin
|
||||
#define TinyPinChange_RetourneEvenemenPort TinyPinChange_GetPinEvent
|
||||
#define TinyPinChange_RetourneEtatCourantPort TinyPinChange_GetPinCurSt
|
||||
#define TinyPinChange_MasqueDeBroche TinyPinChange_PinToMsk
|
||||
|
||||
#endif
|
@@ -0,0 +1,159 @@
|
||||
/*
|
||||
_____ ____ __ _ ____ _ _ _ _
|
||||
| __ \ / __ \ | \ | | / __ \ | | | | | | | |
|
||||
| |__| | | / \_| | . \ | | / / \ \ | | | | \ \ / /
|
||||
| _ / | | _ | |\ \| | | |__| | | | | | \ ' /
|
||||
| | \ \ | \__/ | | | \ ' | | __ | \ \/ / | |
|
||||
|_| \_\ \____/ |_| \__| |_| |_| \__/ |_| 2013
|
||||
|
||||
http://p.loussouarn.free.fr
|
||||
|
||||
*******************************************************
|
||||
* <TinyPinChange> library Demo *
|
||||
* with debugging capabilities using *
|
||||
* <SoftSerial> object as single wire serial interface *
|
||||
*******************************************************
|
||||
|
||||
This sketch demonstrates how to use <TinyPinChange> library.
|
||||
It counts all the transitions on 2 different pins.
|
||||
/!\CAUTION/!\: as <TinyPinChange> library can be shared (and it is with SoftSerial in this sketch) , the user shall test if the changes are related to the declared pins.
|
||||
|
||||
Trick: By connecting Pin#1 to Pin#0 or to Pin#5 through a 1K resistor, you can generate transitions for testing purpose.
|
||||
Output results are sent to a software serial.
|
||||
|
||||
And the great thing is: using a <SoftSerial> object as a bi-directionnal software serial port (half-duplex) on a single pin to communicate with the outside world!
|
||||
|
||||
To display the sketch results on a PC (in a Terminal):
|
||||
1) Build the "Serial One Wire Debug Cable" and plug it to the regular RS232 port as depicted below,
|
||||
2) Open your favorite Terminal at 38400,n,8,1: HyperTerminal, Teraterm (Windows) or Minicom, GtkTerm (Linux) and CoolTerm (MAC) does the trick.
|
||||
3) You can also use the Serial Monitor of the arduino IDE: Tools->Serial Port and select your RS232 port (may be an USB virtual port), Rate=38400.
|
||||
4) To enable the display, type 1, to disable, type 0 in the Terminal/Monitor.
|
||||
|
||||
SERIAL ONE WIRE
|
||||
DEBUGGING CABLE
|
||||
_______________ ________________
|
||||
/ \___/\___/ \
|
||||
____
|
||||
.--------. | \
|
||||
| GND |--------------------------------+---o5 \
|
||||
| | 47K | | 9o |
|
||||
| | .--###--' | o4 |
|
||||
| DEBUG | 4.7K | | 8o |
|
||||
| TX_RX |-------------------###--+--|<|------o3 | ---> To regular RS232 SubD 9 pins Male of PC or Serial/USB adapter
|
||||
| PIN | ^ | 1N4148 | 7o |
|
||||
| | | '-----------o2 |
|
||||
'--------' | | 6o |
|
||||
ATtiny85 Single | o1 /
|
||||
(Digispark) I/O |____/
|
||||
SubD 9 pins
|
||||
Female
|
||||
*/
|
||||
#include <TinyPinChange.h>
|
||||
#include <SoftSerial.h>
|
||||
|
||||
#define LED_PIN 1
|
||||
|
||||
#define DEBUG_TX_RX_PIN 2
|
||||
|
||||
#define FIRST_INPUT 0
|
||||
#define SECOND_INPUT 5
|
||||
|
||||
volatile uint16_t FirstInputChangeCount=0; /* Volatile since the variable will be updated in interruption */
|
||||
volatile uint16_t SecondInputChangeCount=0; /* Volatile since the variable will be updated in interruption */
|
||||
|
||||
SoftSerial MySerial(DEBUG_TX_RX_PIN, DEBUG_TX_RX_PIN, true); /* Tx/Rx on a single Pin !!! (Pin#2) */
|
||||
|
||||
uint8_t VirtualPortNb;
|
||||
uint8_t VirtualPortNb_;
|
||||
|
||||
void setup()
|
||||
{
|
||||
TinyPinChange_Init();
|
||||
|
||||
MySerial.begin(38400); /* Trick: use a "high" data rate (less time wasted in ISR and for transmitting each character) */
|
||||
|
||||
VirtualPortNb=TinyPinChange_RegisterIsr(FIRST_INPUT, InterruptFunctionToCall);
|
||||
VirtualPortNb_=TinyPinChange_RegisterIsr(SECOND_INPUT, InterruptFunctionToCall);
|
||||
|
||||
/* Enable Pin Change for each pin */
|
||||
TinyPinChange_EnablePin(FIRST_INPUT);
|
||||
TinyPinChange_EnablePin(SECOND_INPUT);
|
||||
|
||||
MySerial.txMode();
|
||||
MySerial.println(F("\n*** Tiny PinChange Demo ***"));
|
||||
MySerial.print(F("Pin "));MySerial.print((int)FIRST_INPUT);
|
||||
MySerial.print(F(" is part of virtual port "));MySerial.println((int)VirtualPortNb);
|
||||
|
||||
MySerial.print(F("Pin "));MySerial.print((int)SECOND_INPUT);
|
||||
MySerial.print(F(" is part of virtual port "));MySerial.println((int)VirtualPortNb_);
|
||||
|
||||
MySerial.println(F("As you can see, virtual port is always port 0 for ATtiny85"));
|
||||
MySerial.println(F("Remember <TinyPinChange> is also designed for UNO, MEGA and ATtiny84 ;-)"));
|
||||
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
|
||||
MySerial.rxMode(); /* Switch to Rx Mode */
|
||||
}
|
||||
|
||||
/* Function called in interruption in case of change on pins */
|
||||
void InterruptFunctionToCall(void)
|
||||
{
|
||||
uint8_t PortChange;
|
||||
|
||||
PortChange = TinyPinChange_GetPinEvent(VirtualPortNb);
|
||||
if(PortChange & TinyPinChange_PinToMsk(FIRST_INPUT)) /* Check FIRST_INPUT has changed */
|
||||
{
|
||||
FirstInputChangeCount++; /* Rising AND Falling edges are counted */
|
||||
}
|
||||
if(PortChange & TinyPinChange_PinToMsk(SECOND_INPUT)) /* Check SECOND_INPUT has changed */
|
||||
{
|
||||
SecondInputChangeCount++; /* Rising AND Falling edges are counted */
|
||||
}
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
static boolean State=HIGH, DisplayEnabled=false;
|
||||
static uint32_t LedStartMs=millis(), DisplayStartMs=millis();
|
||||
uint16_t LocalFirstInputChangeCount;
|
||||
uint16_t LocalSecondInputChangeCount;
|
||||
|
||||
/* Blink the built-in LED */
|
||||
if(millis()-LedStartMs >= 500)
|
||||
{
|
||||
LedStartMs=millis();
|
||||
digitalWrite(LED_PIN, State);
|
||||
State=!State; /* State will be inverted at the next digitalWrite() */
|
||||
}
|
||||
|
||||
/* Get command from single wire SoftSerial */
|
||||
if(MySerial.available())
|
||||
{
|
||||
switch(MySerial.read())
|
||||
{
|
||||
case '0':
|
||||
DisplayEnabled=false;
|
||||
break;
|
||||
|
||||
case '1':
|
||||
DisplayEnabled=true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Diplay Transition numbers every second */
|
||||
if((millis()-DisplayStartMs >= 1000) && DisplayEnabled)
|
||||
{
|
||||
DisplayStartMs=millis();
|
||||
noInterrupts(); /* Mandatory since counters are 16 bits */
|
||||
LocalFirstInputChangeCount = FirstInputChangeCount;
|
||||
LocalSecondInputChangeCount = SecondInputChangeCount;
|
||||
interrupts();
|
||||
MySerial.txMode();
|
||||
MySerial.print(F("FirstInputChangeCount="));MySerial.println(LocalFirstInputChangeCount);
|
||||
MySerial.print(F("SecondInputChangeCount="));MySerial.println(LocalSecondInputChangeCount);
|
||||
MySerial.rxMode();
|
||||
}
|
||||
|
||||
}
|
||||
|
29
hardware/digistump/avr/libraries/TinyPinChange/keywords.txt
Normal file
29
hardware/digistump/avr/libraries/TinyPinChange/keywords.txt
Normal file
@@ -0,0 +1,29 @@
|
||||
#######################################
|
||||
# Syntax Coloring Map TinyPinChange
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
TinyPinChange KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
TinyPinChange_Init KEYWORD2
|
||||
TinyPinChange_RegisterIsr KEYWORD2
|
||||
TinyPinChange_EnregistreFonctionInterruption KEYWORD2
|
||||
TinyPinChange_EnablePin KEYWORD2
|
||||
TinyPinChange_ActiveBroche KEYWORD2
|
||||
TinyPinChange_DisablePin KEYWORD2
|
||||
TinyPinChange_DesactiveBroche KEYWORD2
|
||||
TinyPinChange_GetPinEvent KEYWORD2
|
||||
TinyPinChange_RetourneEvenemenPort KEYWORD2
|
||||
TinyPinChange_GetPinCurSt KEYWORD2
|
||||
TinyPinChange_RetourneEtatCourantPort KEYWORD2
|
||||
TinyPinChange_PinToMsk KEYWORD2
|
||||
TinyPinChange_MasqueDeBroche KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
Reference in New Issue
Block a user