clean up among libraries
This commit is contained in:
@@ -1,190 +0,0 @@
|
|||||||
|
|
||||||
#include "PN532_HSU.h"
|
|
||||||
#include "PN532_debug.h"
|
|
||||||
|
|
||||||
|
|
||||||
PN532_HSU::PN532_HSU(HardwareSerial &serial)
|
|
||||||
{
|
|
||||||
_serial = &serial;
|
|
||||||
command = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void PN532_HSU::begin()
|
|
||||||
{
|
|
||||||
_serial->begin((long)115200);
|
|
||||||
}
|
|
||||||
|
|
||||||
void PN532_HSU::wakeup()
|
|
||||||
{
|
|
||||||
_serial->write((uint8_t)0x55);
|
|
||||||
_serial->write((uint8_t)0x55);
|
|
||||||
_serial->write((uint8_t)0x00);
|
|
||||||
_serial->write((uint8_t)0x00);
|
|
||||||
_serial->write((uint8_t)0x00);
|
|
||||||
|
|
||||||
/** dump serial buffer */
|
|
||||||
if(_serial->available()){
|
|
||||||
DMSG("Dump serial buffer: ");
|
|
||||||
}
|
|
||||||
while(_serial->available()){
|
|
||||||
uint8_t ret = _serial->read();
|
|
||||||
DMSG_HEX(ret);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
int8_t PN532_HSU::writeCommand(const uint8_t *header, uint8_t hlen, const uint8_t *body, uint8_t blen)
|
|
||||||
{
|
|
||||||
|
|
||||||
/** dump serial buffer */
|
|
||||||
if(_serial->available()){
|
|
||||||
DMSG("Dump serial buffer: ");
|
|
||||||
}
|
|
||||||
while(_serial->available()){
|
|
||||||
uint8_t ret = _serial->read();
|
|
||||||
DMSG_HEX(ret);
|
|
||||||
}
|
|
||||||
|
|
||||||
command = header[0];
|
|
||||||
|
|
||||||
_serial->write((uint8_t)PN532_PREAMBLE);
|
|
||||||
_serial->write((uint8_t)PN532_STARTCODE1);
|
|
||||||
_serial->write((uint8_t)PN532_STARTCODE2);
|
|
||||||
|
|
||||||
uint8_t length = hlen + blen + 1; // length of data field: TFI + DATA
|
|
||||||
_serial->write(length);
|
|
||||||
_serial->write(~length + 1); // checksum of length
|
|
||||||
|
|
||||||
_serial->write((uint8_t)PN532_HOSTTOPN532);
|
|
||||||
uint8_t sum = PN532_HOSTTOPN532; // sum of TFI + DATA
|
|
||||||
|
|
||||||
_serial->write(header, hlen);
|
|
||||||
for (uint8_t i = 0; i < hlen; i++) {
|
|
||||||
sum += header[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
_serial->write(body, blen);
|
|
||||||
for (uint8_t i = 0; i < blen; i++) {
|
|
||||||
sum += body[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t checksum = ~sum + 1; // checksum of TFI + DATA
|
|
||||||
_serial->write(checksum);
|
|
||||||
_serial->write((uint8_t)PN532_POSTAMBLE);
|
|
||||||
|
|
||||||
return readAckFrame();
|
|
||||||
}
|
|
||||||
|
|
||||||
int16_t PN532_HSU::readResponse(uint8_t buf[], uint8_t len, uint16_t timeout)
|
|
||||||
{
|
|
||||||
uint8_t tmp[3];
|
|
||||||
|
|
||||||
DMSG("Read response\n");
|
|
||||||
|
|
||||||
/** Frame Preamble and Start Code */
|
|
||||||
if(receive(tmp, 3, timeout)<=0){
|
|
||||||
return PN532_TIMEOUT;
|
|
||||||
}
|
|
||||||
if(0 != tmp[0] || 0!= tmp[1] || 0xFF != tmp[2]){
|
|
||||||
DMSG("Preamble error");
|
|
||||||
return PN532_INVALID_FRAME;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** receive length and check */
|
|
||||||
uint8_t length[2];
|
|
||||||
if(receive(length, 2, timeout) <= 0){
|
|
||||||
return PN532_TIMEOUT;
|
|
||||||
}
|
|
||||||
if( 0 != (uint8_t)(length[0] + length[1]) ){
|
|
||||||
DMSG("Length error");
|
|
||||||
return PN532_INVALID_FRAME;
|
|
||||||
}
|
|
||||||
length[0] -= 2;
|
|
||||||
if( length[0] > len){
|
|
||||||
return PN532_NO_SPACE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** receive command byte */
|
|
||||||
uint8_t cmd = command + 1; // response command
|
|
||||||
if(receive(tmp, 2, timeout) <= 0){
|
|
||||||
return PN532_TIMEOUT;
|
|
||||||
}
|
|
||||||
if( PN532_PN532TOHOST != tmp[0] || cmd != tmp[1]){
|
|
||||||
DMSG("Command error");
|
|
||||||
return PN532_INVALID_FRAME;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(receive(buf, length[0], timeout) != length[0]){
|
|
||||||
return PN532_TIMEOUT;
|
|
||||||
}
|
|
||||||
uint8_t sum = PN532_PN532TOHOST + cmd;
|
|
||||||
for(uint8_t i=0; i<length[0]; i++){
|
|
||||||
sum += buf[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
/** checksum and postamble */
|
|
||||||
if(receive(tmp, 2, timeout) <= 0){
|
|
||||||
return PN532_TIMEOUT;
|
|
||||||
}
|
|
||||||
if( 0 != (uint8_t)(sum + tmp[0]) || 0 != tmp[1] ){
|
|
||||||
DMSG("Checksum error");
|
|
||||||
return PN532_INVALID_FRAME;
|
|
||||||
}
|
|
||||||
|
|
||||||
return length[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
int8_t PN532_HSU::readAckFrame()
|
|
||||||
{
|
|
||||||
const uint8_t PN532_ACK[] = {0, 0, 0xFF, 0, 0xFF, 0};
|
|
||||||
uint8_t ackBuf[sizeof(PN532_ACK)];
|
|
||||||
|
|
||||||
DMSG("Read Ack\n");
|
|
||||||
|
|
||||||
if( receive(ackBuf, sizeof(PN532_ACK)) <= 0 ){
|
|
||||||
DMSG("Read ACK Timeout\n");
|
|
||||||
return PN532_TIMEOUT;
|
|
||||||
}
|
|
||||||
|
|
||||||
if( memcmp(ackBuf, PN532_ACK, sizeof(PN532_ACK)) ){
|
|
||||||
DMSG("Invalid ACK\n");
|
|
||||||
return PN532_INVALID_ACK;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
@brief receive data .
|
|
||||||
@param buf --> return value buffer.
|
|
||||||
len --> length expect to receive.
|
|
||||||
timeout --> time of reveiving
|
|
||||||
@retval number of received bytes, 0 means no data received.
|
|
||||||
*/
|
|
||||||
int8_t PN532_HSU::receive(uint8_t *buf, int len, uint16_t timeout)
|
|
||||||
{
|
|
||||||
int read_bytes = 0;
|
|
||||||
int ret;
|
|
||||||
unsigned long start_millis;
|
|
||||||
|
|
||||||
while (read_bytes < len) {
|
|
||||||
start_millis = millis();
|
|
||||||
do {
|
|
||||||
ret = _serial->read();
|
|
||||||
if (ret >= 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} while( (millis()- start_millis ) < timeout);
|
|
||||||
|
|
||||||
if (ret < 0) {
|
|
||||||
if(read_bytes){
|
|
||||||
return read_bytes;
|
|
||||||
}else{
|
|
||||||
return PN532_TIMEOUT;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
buf[read_bytes] = (uint8_t)ret;
|
|
||||||
DMSG_HEX(ret);
|
|
||||||
read_bytes++;
|
|
||||||
}
|
|
||||||
return read_bytes;
|
|
||||||
}
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
|
|
||||||
#ifndef __PN532_HSU_H__
|
|
||||||
#define __PN532_HSU_H__
|
|
||||||
|
|
||||||
#include "PN532Interface.h"
|
|
||||||
|
|
||||||
#define PN532_HSU_DEBUG
|
|
||||||
|
|
||||||
#define PN532_HSU_READ_TIMEOUT (1000)
|
|
||||||
|
|
||||||
class PN532_HSU : public PN532Interface {
|
|
||||||
public:
|
|
||||||
PN532_HSU(HardwareSerial &serial);
|
|
||||||
|
|
||||||
void begin();
|
|
||||||
void wakeup();
|
|
||||||
virtual int8_t writeCommand(const uint8_t *header, uint8_t hlen, const uint8_t *body = 0, uint8_t blen = 0);
|
|
||||||
int16_t readResponse(uint8_t buf[], uint8_t len, uint16_t timeout);
|
|
||||||
|
|
||||||
private:
|
|
||||||
HardwareSerial* _serial;
|
|
||||||
uint8_t command;
|
|
||||||
|
|
||||||
int8_t readAckFrame();
|
|
||||||
|
|
||||||
int8_t receive(uint8_t *buf, int len, uint16_t timeout=PN532_HSU_READ_TIMEOUT);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,209 +0,0 @@
|
|||||||
|
|
||||||
#include "PN532_SPI.h"
|
|
||||||
#include "PN532_debug.h"
|
|
||||||
|
|
||||||
#define STATUS_READ 2
|
|
||||||
#define DATA_WRITE 1
|
|
||||||
#define DATA_READ 3
|
|
||||||
|
|
||||||
PN532_SPI::PN532_SPI(SPIClass &spi, uint8_t ss)
|
|
||||||
{
|
|
||||||
command = 0;
|
|
||||||
_spi = &spi;
|
|
||||||
_ss = ss;
|
|
||||||
}
|
|
||||||
|
|
||||||
void PN532_SPI::begin()
|
|
||||||
{
|
|
||||||
pinMode(_ss, OUTPUT);
|
|
||||||
|
|
||||||
_spi->begin();
|
|
||||||
_spi->setDataMode(SPI_MODE0); // PN532 only supports mode0
|
|
||||||
_spi->setBitOrder(LSBFIRST);
|
|
||||||
#ifndef __SAM3X8E__
|
|
||||||
_spi->setClockDivider(SPI_CLOCK_DIV8); // set clock 2MHz(max: 5MHz)
|
|
||||||
#else
|
|
||||||
/** DUE spi library does not support SPI_CLOCK_DIV8 macro */
|
|
||||||
_spi->setClockDivider(42); // set clock 2MHz(max: 5MHz)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void PN532_SPI::wakeup()
|
|
||||||
{
|
|
||||||
digitalWrite(_ss, LOW);
|
|
||||||
delay(2);
|
|
||||||
digitalWrite(_ss, HIGH);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int8_t PN532_SPI::writeCommand(const uint8_t *header, uint8_t hlen, const uint8_t *body, uint8_t blen)
|
|
||||||
{
|
|
||||||
command = header[0];
|
|
||||||
writeFrame(header, hlen, body, blen);
|
|
||||||
|
|
||||||
uint8_t timeout = PN532_ACK_WAIT_TIME;
|
|
||||||
while (!isReady()) {
|
|
||||||
delay(1);
|
|
||||||
timeout--;
|
|
||||||
if (0 == timeout) {
|
|
||||||
DMSG("Time out when waiting for ACK\n");
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (readAckFrame()) {
|
|
||||||
DMSG("Invalid ACK\n");
|
|
||||||
return PN532_INVALID_ACK;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int16_t PN532_SPI::readResponse(uint8_t buf[], uint8_t len, uint16_t timeout)
|
|
||||||
{
|
|
||||||
uint16_t time = 0;
|
|
||||||
while (!isReady()) {
|
|
||||||
delay(1);
|
|
||||||
time++;
|
|
||||||
if (timeout > 0 && time > timeout) {
|
|
||||||
return PN532_TIMEOUT;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
digitalWrite(_ss, LOW);
|
|
||||||
delay(1);
|
|
||||||
|
|
||||||
int16_t result;
|
|
||||||
do {
|
|
||||||
write(DATA_READ);
|
|
||||||
|
|
||||||
if (0x00 != read() || // PREAMBLE
|
|
||||||
0x00 != read() || // STARTCODE1
|
|
||||||
0xFF != read() // STARTCODE2
|
|
||||||
) {
|
|
||||||
|
|
||||||
result = PN532_INVALID_FRAME;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t length = read();
|
|
||||||
if (0 != (uint8_t)(length + read())) { // checksum of length
|
|
||||||
result = PN532_INVALID_FRAME;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t cmd = command + 1; // response command
|
|
||||||
if (PN532_PN532TOHOST != read() || (cmd) != read()) {
|
|
||||||
result = PN532_INVALID_FRAME;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
DMSG("read: ");
|
|
||||||
DMSG_HEX(cmd);
|
|
||||||
|
|
||||||
length -= 2;
|
|
||||||
if (length > len) {
|
|
||||||
for (uint8_t i = 0; i < length; i++) {
|
|
||||||
DMSG_HEX(read()); // dump message
|
|
||||||
}
|
|
||||||
DMSG("\nNot enough space\n");
|
|
||||||
read();
|
|
||||||
read();
|
|
||||||
result = PN532_NO_SPACE; // not enough space
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t sum = PN532_PN532TOHOST + cmd;
|
|
||||||
for (uint8_t i = 0; i < length; i++) {
|
|
||||||
buf[i] = read();
|
|
||||||
sum += buf[i];
|
|
||||||
|
|
||||||
DMSG_HEX(buf[i]);
|
|
||||||
}
|
|
||||||
DMSG('\n');
|
|
||||||
|
|
||||||
uint8_t checksum = read();
|
|
||||||
if (0 != (uint8_t)(sum + checksum)) {
|
|
||||||
DMSG("checksum is not ok\n");
|
|
||||||
result = PN532_INVALID_FRAME;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
read(); // POSTAMBLE
|
|
||||||
|
|
||||||
result = length;
|
|
||||||
} while (0);
|
|
||||||
|
|
||||||
digitalWrite(_ss, HIGH);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean PN532_SPI::isReady()
|
|
||||||
{
|
|
||||||
digitalWrite(_ss, LOW);
|
|
||||||
|
|
||||||
write(STATUS_READ);
|
|
||||||
uint8_t status = read() & 1;
|
|
||||||
digitalWrite(_ss, HIGH);
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
void PN532_SPI::writeFrame(const uint8_t *header, uint8_t hlen, const uint8_t *body, uint8_t blen)
|
|
||||||
{
|
|
||||||
digitalWrite(_ss, LOW);
|
|
||||||
delay(2); // wake up PN532
|
|
||||||
|
|
||||||
write(DATA_WRITE);
|
|
||||||
write(PN532_PREAMBLE);
|
|
||||||
write(PN532_STARTCODE1);
|
|
||||||
write(PN532_STARTCODE2);
|
|
||||||
|
|
||||||
uint8_t length = hlen + blen + 1; // length of data field: TFI + DATA
|
|
||||||
write(length);
|
|
||||||
write(~length + 1); // checksum of length
|
|
||||||
|
|
||||||
write(PN532_HOSTTOPN532);
|
|
||||||
uint8_t sum = PN532_HOSTTOPN532; // sum of TFI + DATA
|
|
||||||
|
|
||||||
DMSG("write: ");
|
|
||||||
|
|
||||||
for (uint8_t i = 0; i < hlen; i++) {
|
|
||||||
write(header[i]);
|
|
||||||
sum += header[i];
|
|
||||||
|
|
||||||
DMSG_HEX(header[i]);
|
|
||||||
}
|
|
||||||
for (uint8_t i = 0; i < blen; i++) {
|
|
||||||
write(body[i]);
|
|
||||||
sum += body[i];
|
|
||||||
|
|
||||||
DMSG_HEX(body[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t checksum = ~sum + 1; // checksum of TFI + DATA
|
|
||||||
write(checksum);
|
|
||||||
write(PN532_POSTAMBLE);
|
|
||||||
|
|
||||||
digitalWrite(_ss, HIGH);
|
|
||||||
|
|
||||||
DMSG('\n');
|
|
||||||
}
|
|
||||||
|
|
||||||
int8_t PN532_SPI::readAckFrame()
|
|
||||||
{
|
|
||||||
const uint8_t PN532_ACK[] = {0, 0, 0xFF, 0, 0xFF, 0};
|
|
||||||
|
|
||||||
uint8_t ackBuf[sizeof(PN532_ACK)];
|
|
||||||
|
|
||||||
digitalWrite(_ss, LOW);
|
|
||||||
delay(1);
|
|
||||||
write(DATA_READ);
|
|
||||||
|
|
||||||
for (uint8_t i = 0; i < sizeof(PN532_ACK); i++) {
|
|
||||||
ackBuf[i] = read();
|
|
||||||
}
|
|
||||||
|
|
||||||
digitalWrite(_ss, HIGH);
|
|
||||||
|
|
||||||
return memcmp(ackBuf, PN532_ACK, sizeof(PN532_ACK));
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
|
|
||||||
#ifndef __PN532_SPI_H__
|
|
||||||
#define __PN532_SPI_H__
|
|
||||||
|
|
||||||
#include <SPI.h>
|
|
||||||
#include "PN532Interface.h"
|
|
||||||
|
|
||||||
class PN532_SPI : public PN532Interface {
|
|
||||||
public:
|
|
||||||
PN532_SPI(SPIClass &spi, uint8_t ss);
|
|
||||||
|
|
||||||
void begin();
|
|
||||||
void wakeup();
|
|
||||||
int8_t writeCommand(const uint8_t *header, uint8_t hlen, const uint8_t *body = 0, uint8_t blen = 0);
|
|
||||||
|
|
||||||
int16_t readResponse(uint8_t buf[], uint8_t len, uint16_t timeout);
|
|
||||||
|
|
||||||
private:
|
|
||||||
SPIClass* _spi;
|
|
||||||
uint8_t _ss;
|
|
||||||
uint8_t command;
|
|
||||||
|
|
||||||
boolean isReady();
|
|
||||||
void writeFrame(const uint8_t *header, uint8_t hlen, const uint8_t *body = 0, uint8_t blen = 0);
|
|
||||||
int8_t readAckFrame();
|
|
||||||
|
|
||||||
inline void write(uint8_t data) {
|
|
||||||
_spi->transfer(data);
|
|
||||||
};
|
|
||||||
|
|
||||||
inline uint8_t read() {
|
|
||||||
return _spi->transfer(0);
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,73 +0,0 @@
|
|||||||
Name : Sleep_n0m1 Library
|
|
||||||
Author : Noah Shibley, Michael Grant, NoMi Design Ltd. http://n0m1.com
|
|
||||||
Date : July 10th 2011
|
|
||||||
Version : 0.1
|
|
||||||
Notes : Arduino Library to place the arduino into sleep mode for
|
|
||||||
a specific length of time, or a specific number of sleep cycles
|
|
||||||
Some of this code comes from "Cloudy" on the arduino forum
|
|
||||||
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1292898715
|
|
||||||
|
|
||||||
Refer to the examples for how to use this library.
|
|
||||||
|
|
||||||
Dependencies:
|
|
||||||
none
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
List of Functions:
|
|
||||||
|
|
||||||
Function: idleMode
|
|
||||||
Description: sets the Arduino into idle Mode sleep,
|
|
||||||
the least power saving, The idle mode stops the MCU
|
|
||||||
but leaves peripherals and timers running.
|
|
||||||
------------------------------------------------------
|
|
||||||
Function: adcMode
|
|
||||||
Description: sets the Arduino into adc Mode sleep,
|
|
||||||
This mode makes the MCU enter ADC Noise Reduction mode,
|
|
||||||
stopping the CPU but allowing the ADC, the external interrupts,
|
|
||||||
the 2-wire Serial Interface address watch, Timer/Counter2
|
|
||||||
and the Watchdog to continue operating
|
|
||||||
------------------------------------------------------
|
|
||||||
Function: pwrSaveMode
|
|
||||||
Description: sets the Arduino into power Save Mode sleep,
|
|
||||||
The timer crystal will continue to operate in this mode,
|
|
||||||
Timer2 still active.
|
|
||||||
------------------------------------------------------
|
|
||||||
Function: extStandbyMode
|
|
||||||
Description: sets the Arduino into extStandby Mode sleep,
|
|
||||||
This mode is identical to Power-save with the exception
|
|
||||||
that the Oscillator is kept running for fast wake up
|
|
||||||
------------------------------------------------------
|
|
||||||
Function: standbyMode
|
|
||||||
Description: sets the Arduino into standby Mode sleep,
|
|
||||||
This mode is identical to Power-down with the exception
|
|
||||||
that the Oscillator is kept running for fast wake up
|
|
||||||
------------------------------------------------------
|
|
||||||
Function: pwrDownMode
|
|
||||||
Description: sets the Arduino into power Down Mode sleep,
|
|
||||||
The most power saving, all systems are powered down
|
|
||||||
except the watch dog timer and external reset
|
|
||||||
------------------------------------------------------
|
|
||||||
Function: sleepDelay
|
|
||||||
Description: Works like the Arduino delay function, sets the
|
|
||||||
Arduino into sleep mode for a specified time.
|
|
||||||
Parameters: (unsigned long) time in ms of the sleep cycle
|
|
||||||
------------------------------------------------------
|
|
||||||
Function: sleepDelay
|
|
||||||
Description: Works like the Arduino delay function, sets the
|
|
||||||
Arduino into sleep mode for a specified time.
|
|
||||||
Parameters: (unsigned long) time in ms of the sleep cycle
|
|
||||||
(boolean) prevents the Arduino from entering sleep
|
|
||||||
------------------------------------------------------
|
|
||||||
Function: setCalibrationInterval
|
|
||||||
Description: the WDT needs to be calibrated against timer 0
|
|
||||||
periodically to keep the sleep time accurate. Default calibration
|
|
||||||
occurs every 100 wake/sleep cycles. recalibrate too often will
|
|
||||||
waste power and too rarely will make the sleep time inaccurate.
|
|
||||||
Parameters: (int) set the # of wake/sleep cycles between calibrations
|
|
||||||
------------------------------------------------------
|
|
||||||
Function: sleepInterrupt
|
|
||||||
Description: set the Arduino into sleep mode until an interrupt is
|
|
||||||
triggered. The interrupts are passed in as parameters
|
|
||||||
Parameters: (int) interrupt value, 0, 1, etc, see attachinterrupt()
|
|
||||||
(int) mode of trigger, LOW,RISING,FALLING,CHANGE
|
|
||||||
@@ -1,243 +0,0 @@
|
|||||||
/************************************************************************************
|
|
||||||
*
|
|
||||||
* Name : Sleep_n0m1.cpp
|
|
||||||
* Author : Noah Shibley / NoMi Design
|
|
||||||
* Date : July 10th 2011
|
|
||||||
* Version : 0.1
|
|
||||||
* Notes : Some of this code comes from "Cloudy" on the arduino forum
|
|
||||||
* http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1292898715
|
|
||||||
*
|
|
||||||
* Copyright (c) 2012 NoMi Design (http://n0m1.com) All right reserved.
|
|
||||||
*
|
|
||||||
* This file is part of Triggertrap. See Triggertrap.com for more information.
|
|
||||||
*
|
|
||||||
* Triggertrap is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* Triggertrap 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 General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with Triggertrap. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*
|
|
||||||
***********************************************************************************/
|
|
||||||
|
|
||||||
#include "Sleep_n0m1.h"
|
|
||||||
|
|
||||||
Sleep* Sleep::pSleep = 0;
|
|
||||||
|
|
||||||
Sleep::Sleep()
|
|
||||||
{
|
|
||||||
pSleep = this; //the ptr points to this object
|
|
||||||
timeSleep = 0; // total time due to sleep
|
|
||||||
calibv = 1.0; // ratio of real clock with WDT clock
|
|
||||||
byte isrcalled = 0; // WDT vector flag
|
|
||||||
sleepCycleCount = 0;
|
|
||||||
sleepCycleInterval = 100;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/********************************************************************
|
|
||||||
*
|
|
||||||
* setSleepMode
|
|
||||||
*
|
|
||||||
********************************************************************/
|
|
||||||
void Sleep::setSleepMode(int mode)
|
|
||||||
{
|
|
||||||
sleepMode_ = mode;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/********************************************************************
|
|
||||||
*
|
|
||||||
* calibrateTime
|
|
||||||
*
|
|
||||||
********************************************************************/
|
|
||||||
void Sleep::calibrateTime(unsigned long sleepTime, boolean &abortCycle) {
|
|
||||||
// timer0 continues to run in idle sleep mode
|
|
||||||
set_sleep_mode(SLEEP_MODE_IDLE);
|
|
||||||
long tt1=millis();
|
|
||||||
sleepWDT(sleepTime,abortCycle);
|
|
||||||
long tt2=millis();
|
|
||||||
|
|
||||||
calibv = (float) sleepTime/(tt2-tt1);
|
|
||||||
|
|
||||||
//Serial.println(calibv);
|
|
||||||
}
|
|
||||||
|
|
||||||
/********************************************************************
|
|
||||||
*
|
|
||||||
* WDTMillis
|
|
||||||
*
|
|
||||||
********************************************************************/
|
|
||||||
unsigned long Sleep::WDTMillis() {
|
|
||||||
return millis()+timeSleep;
|
|
||||||
}
|
|
||||||
|
|
||||||
/********************************************************************
|
|
||||||
*
|
|
||||||
* sleepNow
|
|
||||||
*
|
|
||||||
********************************************************************/
|
|
||||||
void Sleep::sleepInterrupt(int interrupt,int mode) {
|
|
||||||
|
|
||||||
if(mode == FALLING || mode == LOW)
|
|
||||||
{
|
|
||||||
int pin = interrupt + 2; //will fail on the mega
|
|
||||||
pinMode (pin, INPUT);
|
|
||||||
digitalWrite (pin, HIGH);
|
|
||||||
}
|
|
||||||
|
|
||||||
set_sleep_mode(sleepMode_);
|
|
||||||
sleep_enable();
|
|
||||||
attachInterrupt(interrupt,sleepHandler,mode);
|
|
||||||
sei(); //make sure interrupts are on!
|
|
||||||
sleep_mode();
|
|
||||||
//----------------------------- ZZZZZZ sleeping here----------------------
|
|
||||||
sleep_disable(); //disable sleep, awake now
|
|
||||||
detachInterrupt(interrupt);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/********************************************************************
|
|
||||||
*
|
|
||||||
* sleepDelay
|
|
||||||
*
|
|
||||||
********************************************************************/
|
|
||||||
void Sleep::sleepDelay(unsigned long sleepTime){
|
|
||||||
|
|
||||||
boolean abortCycle = false;
|
|
||||||
|
|
||||||
sleepDelay(sleepTime,abortCycle);
|
|
||||||
}
|
|
||||||
|
|
||||||
/********************************************************************
|
|
||||||
*
|
|
||||||
* sleepDelay
|
|
||||||
*
|
|
||||||
********************************************************************/
|
|
||||||
void Sleep::sleepDelay(unsigned long sleepTime, boolean &abortCycle) {
|
|
||||||
ADCSRA &= ~(1<<ADEN); // adc off
|
|
||||||
// PRR = 0xEF; // modules off
|
|
||||||
|
|
||||||
++sleepCycleCount;
|
|
||||||
sleepCycleCount = sleepCycleCount % sleepCycleInterval; //recalibrate every interval cycles
|
|
||||||
if(sleepCycleCount == 1)
|
|
||||||
{
|
|
||||||
calibrateTime(sleepTime,abortCycle);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
set_sleep_mode(sleepMode_);
|
|
||||||
int trem = sleepWDT(sleepTime*calibv,abortCycle);
|
|
||||||
timeSleep += (sleepTime-trem);
|
|
||||||
}
|
|
||||||
// PRR = 0x00; //modules on
|
|
||||||
ADCSRA |= (1<<ADEN); // adc on
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/********************************************************************
|
|
||||||
*
|
|
||||||
* sleepWDT
|
|
||||||
*
|
|
||||||
********************************************************************/
|
|
||||||
int Sleep::sleepWDT(unsigned long remainTime, boolean &abortCycle) {
|
|
||||||
|
|
||||||
#if defined(WDP3)
|
|
||||||
byte WDTps = 9; // WDT Prescaler value, 9 = 8192ms
|
|
||||||
#else
|
|
||||||
byte WDTps = 7; // WDT Prescaler value, 7 = 2048ms
|
|
||||||
#endif
|
|
||||||
|
|
||||||
isrcalled = 0;
|
|
||||||
sleep_enable();
|
|
||||||
while(remainTime > 0) {
|
|
||||||
//work out next prescale unit to use
|
|
||||||
while ((0x10<<WDTps) > remainTime && WDTps > 0) {
|
|
||||||
WDTps--;
|
|
||||||
}
|
|
||||||
// send prescaler mask to WDT_On
|
|
||||||
WDT_On((WDTps & 0x08 ? (1<<WDP3) : 0x00) | (WDTps & 0x07));
|
|
||||||
isrcalled=0;
|
|
||||||
while (isrcalled==0 && abortCycle == false) {
|
|
||||||
|
|
||||||
#if defined(__AVR_ATmega328P__)
|
|
||||||
// turn bod off
|
|
||||||
MCUCR |= (1<<BODS) | (1<<BODSE);
|
|
||||||
MCUCR &= ~(1<<BODSE); // must be done right before sleep
|
|
||||||
#endif
|
|
||||||
sleep_cpu(); // sleep here
|
|
||||||
}
|
|
||||||
// calculate remaining time
|
|
||||||
remainTime -= (0x10<<WDTps);
|
|
||||||
if ((long) remainTime < 0 ) {remainTime = 0;} //check for unsigned underflow, by converting to signed
|
|
||||||
|
|
||||||
}
|
|
||||||
sleep_disable();
|
|
||||||
return remainTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/********************************************************************
|
|
||||||
*
|
|
||||||
* WDT_On
|
|
||||||
*
|
|
||||||
********************************************************************/
|
|
||||||
void Sleep::WDT_On(byte psMask)
|
|
||||||
{
|
|
||||||
// prepare timed sequence first
|
|
||||||
byte ps = (psMask | (1<<WDIE)) & ~(1<<WDE);
|
|
||||||
cli();
|
|
||||||
wdt_reset();
|
|
||||||
/* Clear WDRF in MCUSR */
|
|
||||||
MCUSR &= ~(1<<WDRF);
|
|
||||||
// start timed sequence
|
|
||||||
WDTCSR |= (1<<WDCE) | (1<<WDE);
|
|
||||||
// set new watchdog timeout value
|
|
||||||
WDTCSR = ps;
|
|
||||||
sei();
|
|
||||||
}
|
|
||||||
|
|
||||||
/********************************************************************
|
|
||||||
*
|
|
||||||
* WDT_Off
|
|
||||||
*
|
|
||||||
********************************************************************/
|
|
||||||
void Sleep::WDT_Off() {
|
|
||||||
cli();
|
|
||||||
wdt_reset();
|
|
||||||
/* Clear WDRF in MCUSR */
|
|
||||||
MCUSR &= ~(1<<WDRF);
|
|
||||||
/* Write logical one to WDCE and WDE */
|
|
||||||
/* Keep old prescaler setting to prevent unintentional time-out */
|
|
||||||
WDTCSR |= (1<<WDCE) | (1<<WDE);
|
|
||||||
/* Turn off WDT */
|
|
||||||
WDTCSR = 0x00;
|
|
||||||
sei();
|
|
||||||
}
|
|
||||||
|
|
||||||
/********************************************************************
|
|
||||||
*
|
|
||||||
* sleepHandler ISR
|
|
||||||
*
|
|
||||||
********************************************************************/
|
|
||||||
void sleepHandler(void)
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/********************************************************************
|
|
||||||
*
|
|
||||||
* WDT ISR
|
|
||||||
*
|
|
||||||
********************************************************************/
|
|
||||||
ISR(WDT_vect) {
|
|
||||||
Sleep::pSleep->WDT_Off();
|
|
||||||
Sleep::pSleep->isrcalled=1;
|
|
||||||
}
|
|
||||||
@@ -1,109 +0,0 @@
|
|||||||
/************************************************************************************
|
|
||||||
*
|
|
||||||
* Name : Sleep_n0m1.h
|
|
||||||
* Author : Noah Shibley / NoMi Design
|
|
||||||
* Date : July 10th 2011
|
|
||||||
* Version : 0.1
|
|
||||||
* Notes : Some of this code comes from "Cloudy" on the arduino forum
|
|
||||||
* http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1292898715
|
|
||||||
*
|
|
||||||
* Copyright (c) 2012 NoMi Design (http://n0m1.com) All right reserved.
|
|
||||||
*
|
|
||||||
* This file is part of Triggertrap. See Triggertrap.com for more information.
|
|
||||||
*
|
|
||||||
* Triggertrap is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* Triggertrap 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 General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with Triggertrap. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*
|
|
||||||
***********************************************************************************/
|
|
||||||
|
|
||||||
#ifndef SLEEP_H
|
|
||||||
#define SLEEP_H
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#include <avr/sleep.h>
|
|
||||||
#include <avr/wdt.h>
|
|
||||||
#if defined(ARDUINO) && ARDUINO >= 100
|
|
||||||
#include "Arduino.h"
|
|
||||||
#else
|
|
||||||
#include "WProgram.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
extern "C" void WDT_vect(void) __attribute__ ((signal));
|
|
||||||
extern "C" void sleepHandler(void) __attribute__ ((signal));
|
|
||||||
|
|
||||||
class Sleep {
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
friend void WDT_vect(void);
|
|
||||||
friend void sleepHandler(void);
|
|
||||||
|
|
||||||
Sleep();
|
|
||||||
|
|
||||||
/* modes of sleep
|
|
||||||
SLEEP_MODE_IDLE
|
|
||||||
SLEEP_MODE_ADC
|
|
||||||
SLEEP_MODE_PWR_SAVE
|
|
||||||
SLEEP_MODE_EXT_STANDBY
|
|
||||||
SLEEP_MODE_STANDBY
|
|
||||||
SLEEP_MODE_PWR_DOWN
|
|
||||||
*/
|
|
||||||
|
|
||||||
void idleMode() { setSleepMode(SLEEP_MODE_IDLE);}
|
|
||||||
void adcMode() {setSleepMode(SLEEP_MODE_ADC);}
|
|
||||||
void pwrSaveMode() {setSleepMode(SLEEP_MODE_PWR_SAVE);}
|
|
||||||
void extStandbyMode(){setSleepMode(SLEEP_MODE_EXT_STANDBY);}
|
|
||||||
void standbyMode(){setSleepMode(SLEEP_MODE_STANDBY);}
|
|
||||||
void pwrDownMode(){setSleepMode(SLEEP_MODE_PWR_DOWN);}
|
|
||||||
|
|
||||||
//WatchDog Sleep Functions: Sleep for a specfic length of time
|
|
||||||
void sleepDelay(unsigned long sleepTime);
|
|
||||||
void sleepDelay(unsigned long sleepTime,boolean &abortCycle);
|
|
||||||
void setCalibrationInterval(int interval){ sleepCycleInterval = interval; }
|
|
||||||
|
|
||||||
//Interrupt Sleep Function: Sleep till something interrupts sleep
|
|
||||||
void sleepInterrupt(int interrupt,int mode);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
int sleepMode_;
|
|
||||||
unsigned long timeSleep;
|
|
||||||
float calibv;
|
|
||||||
volatile byte isrcalled;
|
|
||||||
static Sleep* pSleep; //static ptr to Sleep class for the ISR
|
|
||||||
int sleepCycleCount;
|
|
||||||
int sleepCycleInterval;
|
|
||||||
|
|
||||||
void setSleepMode(int mode);
|
|
||||||
void WDT_Off();
|
|
||||||
void WDT_On(byte psMask);
|
|
||||||
int sleepWDT(unsigned long remainTime,boolean &abortCycle);
|
|
||||||
void calibrateTime(unsigned long sleepTime,boolean &abortCycle); //calibrate the time keeping difference between WDT and Timer0
|
|
||||||
unsigned long WDTMillis(); // Estimated millis is real clock + calibrated sleep time
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
#include <Sleep_n0m1.h>
|
|
||||||
|
|
||||||
Sleep sleep;
|
|
||||||
|
|
||||||
void setup()
|
|
||||||
{
|
|
||||||
|
|
||||||
Serial.begin(9600);
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
|
|
||||||
delay(100); ////delays are just for serial print, without serial they can be removed
|
|
||||||
Serial.println("execute your code here");
|
|
||||||
|
|
||||||
Serial.print("Sleeping Till Interrupt");
|
|
||||||
|
|
||||||
delay(100); //delay to allow serial to fully print before sleep
|
|
||||||
|
|
||||||
sleep.pwrDownMode(); //set sleep mode
|
|
||||||
sleep.sleepInterrupt(0,FALLING); //sleep for: sleepTime
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
#include <Sleep_n0m1.h>
|
|
||||||
|
|
||||||
Sleep sleep;
|
|
||||||
boolean abortSleep; //cancel sleep cycle
|
|
||||||
int sleepCycleCount; //the number of times awake then asleep
|
|
||||||
unsigned long sleepTime; //how long you want the arduino to sleep
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void setup()
|
|
||||||
{
|
|
||||||
|
|
||||||
Serial.begin(9600);
|
|
||||||
abortSleep = false; //can be used to cancel the sleep cycle
|
|
||||||
sleepTime = 5000; //set sleep time in ms, max sleep time is 49.7 days
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
if(abortSleep)
|
|
||||||
{
|
|
||||||
Serial.println("sleep no more!");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
delay(100);
|
|
||||||
Serial.print("sleeping for ");
|
|
||||||
Serial.println(sleepTime);
|
|
||||||
delay(100); //delay to allow serial to fully print before sleep
|
|
||||||
}
|
|
||||||
|
|
||||||
sleepCycleCount++;
|
|
||||||
if(sleepCycleCount > 5)
|
|
||||||
{
|
|
||||||
abortSleep = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
sleep.pwrDownMode(); //set sleep mode
|
|
||||||
sleep.sleepDelay(sleepTime,abortSleep); //sleep for: sleepTime
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
#include <Sleep_n0m1.h>
|
|
||||||
|
|
||||||
int count = 0;
|
|
||||||
Sleep sleep;
|
|
||||||
unsigned long sleepTime; //how long you want the arduino to sleep
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void setup()
|
|
||||||
{
|
|
||||||
|
|
||||||
Serial.begin(9600);
|
|
||||||
sleepTime = 50000; //set sleep time in ms, max sleep time is 49.7 days
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
delay(100); //delay to allow serial output to be ready after wake up
|
|
||||||
Serial.print("Awake for ");
|
|
||||||
Serial.print(count);
|
|
||||||
Serial.println("sec");
|
|
||||||
|
|
||||||
count++;
|
|
||||||
delay(1000); // waits for a second
|
|
||||||
|
|
||||||
if(count >= 3)
|
|
||||||
{
|
|
||||||
count = 0;
|
|
||||||
Serial.print("sleeping ");
|
|
||||||
Serial.println(sleepTime);
|
|
||||||
delay(100); //delay to allow serial to fully print before sleep
|
|
||||||
|
|
||||||
sleep.pwrDownMode(); //set sleep mode
|
|
||||||
sleep.sleepDelay(sleepTime); //sleep for: sleepTime
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
#include <Sleep_n0m1.h>
|
|
||||||
|
|
||||||
Sleep sleep;
|
|
||||||
unsigned long sleepTime; //how long you want the arduino to sleep
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void setup()
|
|
||||||
{
|
|
||||||
|
|
||||||
Serial.begin(9600);
|
|
||||||
sleepTime = 50000; //set sleep time in ms, max sleep time is 49.7 days
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
|
|
||||||
delay(100); ////delays are just for serial print, without serial they can be removed
|
|
||||||
Serial.println("execute your code here");
|
|
||||||
|
|
||||||
Serial.print("sleeping for ");
|
|
||||||
Serial.println(sleepTime);
|
|
||||||
delay(100); //delay to allow serial to fully print before sleep
|
|
||||||
|
|
||||||
sleep.pwrDownMode(); //set sleep mode
|
|
||||||
sleep.sleepDelay(sleepTime); //sleep for: sleepTime
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user