Merge pull request #281 from tbowmo/samd-rework
atmel SAMD compatability
This commit is contained in:
@@ -276,6 +276,8 @@
|
||||
#ifndef MY_RF24_CE_PIN
|
||||
#if defined(ARDUINO_ARCH_ESP8266)
|
||||
#define MY_RF24_CE_PIN 4
|
||||
#elif defined(ARDUINO_ARCH_SAMD)
|
||||
#define MY_RF24_CE_PIN 27
|
||||
#else
|
||||
#define MY_RF24_CE_PIN 9
|
||||
#endif
|
||||
@@ -284,6 +286,8 @@
|
||||
#ifndef MY_RF24_CS_PIN
|
||||
#if defined(ARDUINO_ARCH_ESP8266)
|
||||
#define MY_RF24_CS_PIN 15
|
||||
#elif defined(ARDUINO_ARCH_SAMD)
|
||||
#define MY_RF24_CS_PIN 3
|
||||
#else
|
||||
#define MY_RF24_CS_PIN 10
|
||||
#endif
|
||||
|
||||
@@ -52,6 +52,8 @@
|
||||
#define MY_GATEWAY_ESP8266
|
||||
#elif defined(ARDUINO_ARCH_AVR)
|
||||
#include "core/MyHwATMega328.cpp"
|
||||
#elif defined(ARDUINO_ARCH_SAMD)
|
||||
#include "core/MyHwSAMD.cpp"
|
||||
#endif
|
||||
|
||||
// LEDS
|
||||
|
||||
@@ -107,22 +107,22 @@ bool gatewayTransportInit() {
|
||||
while (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
SERIALDEVICE.print(".");
|
||||
yield();
|
||||
}
|
||||
Serial.print(F("IP: "));
|
||||
Serial.println(WiFi.localIP());
|
||||
SERIALDEVICE.print(F("IP: "));
|
||||
SERIALDEVICE.println(WiFi.localIP());
|
||||
|
||||
#else
|
||||
#ifdef MY_IP_ADDRESS
|
||||
Ethernet.begin(_ethernetGatewayMAC, _ethernetGatewayIP);
|
||||
Serial.print(F("IP: "));
|
||||
Serial.println(Ethernet.localIP());
|
||||
SERIALDEVICE.print(F("IP: "));
|
||||
SERIALDEVICE.println(Ethernet.localIP());
|
||||
#else
|
||||
// Get IP address from DHCP
|
||||
Ethernet.begin(_ethernetGatewayMAC);
|
||||
Serial.print(F("IP: "));
|
||||
Serial.println(Ethernet.localIP());
|
||||
SERIALDEVICE.print(F("IP: "));
|
||||
SERIALDEVICE.println(Ethernet.localIP());
|
||||
#endif /* IP_ADDRESS_DHCP */
|
||||
// give the Ethernet interface a second to initialize
|
||||
// TODO: use HW delay
|
||||
|
||||
@@ -161,21 +161,21 @@ bool gatewayTransportInit() {
|
||||
while (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
SERIALDEVICE.print(".");
|
||||
yield();
|
||||
}
|
||||
Serial.print(F("IP: "));
|
||||
Serial.println(WiFi.localIP());
|
||||
SERIALDEVICE.print(F("IP: "));
|
||||
SERIALDEVICE.println(WiFi.localIP());
|
||||
#else
|
||||
#ifdef MY_IP_ADDRESS
|
||||
Ethernet.begin(_clientMAC, _clientIp);
|
||||
Serial.print(F("IP: "));
|
||||
Serial.println(Ethernet.localIP());
|
||||
SERIALDEVICE.print(F("IP: "));
|
||||
SERIALDEVICE.println(Ethernet.localIP());
|
||||
#else
|
||||
// Get IP address from DHCP
|
||||
Ethernet.begin(_clientMAC);
|
||||
Serial.print(F("IP: "));
|
||||
Serial.println(Ethernet.localIP());
|
||||
SERIALDEVICE.print(F("IP: "));
|
||||
SERIALDEVICE.println(Ethernet.localIP());
|
||||
#endif /* IP_ADDRESS_DHCP */
|
||||
// give the Ethernet interface a second to initialize
|
||||
// TODO: use HW delay
|
||||
|
||||
@@ -31,7 +31,7 @@ MyMessage _serialMsg;
|
||||
|
||||
|
||||
bool gatewayTransportSend(MyMessage &message) {
|
||||
Serial.print(protocolFormat(message));
|
||||
SERIALDEVICE.print(protocolFormat(message));
|
||||
// Serial print is always successful
|
||||
return true;
|
||||
}
|
||||
@@ -45,9 +45,9 @@ bool gatewayTransportInit() {
|
||||
bool gatewayTransportAvailable() {
|
||||
bool available = false;
|
||||
|
||||
while (Serial.available()) {
|
||||
while (SERIALDEVICE.available()) {
|
||||
// get the new byte:
|
||||
char inChar = (char) Serial.read();
|
||||
char inChar = (char) SERIALDEVICE.read();
|
||||
// if the incoming character is a newline, set a flag
|
||||
// so the main loop can do something about it:
|
||||
if (_serialInputPos < MY_GATEWAY_MAX_RECEIVE_LENGTH - 1 && !available) {
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
#include <Arduino.h>
|
||||
#endif
|
||||
|
||||
#define SERIALDEVICE Serial
|
||||
#if defined __AVR_ATmega328P__
|
||||
#ifndef sleep_bod_disable
|
||||
#define sleep_bod_disable() \
|
||||
|
||||
@@ -30,7 +30,7 @@
|
||||
//#include <SPI.h>
|
||||
#endif
|
||||
|
||||
|
||||
#define SERIALDEVICE Serial
|
||||
#define min(a,b) ((a)<(b)?(a):(b))
|
||||
#define max(a,b) ((a)>(b)?(a):(b))
|
||||
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
/**
|
||||
* The MySensors Arduino library handles the wireless radio link and protocol
|
||||
* between your home built sensors/actuators and HA controller of choice.
|
||||
* The sensors forms a self healing radio network with optional repeaters. Each
|
||||
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
|
||||
* network topology allowing messages to be routed to nodes.
|
||||
*
|
||||
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
|
||||
* Copyright (C) 2013-2015 Sensnology AB
|
||||
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
|
||||
*
|
||||
* Documentation: http://www.mysensors.org
|
||||
* Support Forum: http://forum.mysensors.org
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* version 2 as published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
#include "MyHwSAMD.h"
|
||||
|
||||
|
||||
/*
|
||||
int8_t pinIntTrigger = 0;
|
||||
void wakeUp() //place to send the interrupts
|
||||
{
|
||||
pinIntTrigger = 1;
|
||||
}
|
||||
void wakeUp2() //place to send the second interrupts
|
||||
{
|
||||
pinIntTrigger = 2;
|
||||
}
|
||||
|
||||
// Watchdog Timer interrupt service routine. This routine is required
|
||||
// to allow automatic WDIF and WDIE bit clearance in hardware.
|
||||
ISR (WDT_vect)
|
||||
{
|
||||
// WDIE & WDIF is cleared in hardware upon entering this ISR
|
||||
wdt_disable();
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
void i2c_eeprom_write_byte(unsigned int eeaddress, byte data ) {
|
||||
int rdata = data;
|
||||
Wire.beginTransmission(I2C_EEP_ADDRESS);
|
||||
Wire.write((int)(eeaddress >> 8)); // MSB
|
||||
Wire.write((int)(eeaddress & 0xFF)); // LSB
|
||||
Wire.write(rdata);
|
||||
Wire.endTransmission();
|
||||
}
|
||||
|
||||
byte i2c_eeprom_read_byte(unsigned int eeaddress ) {
|
||||
byte rdata = 0xFF;
|
||||
Wire.beginTransmission(I2C_EEP_ADDRESS);
|
||||
Wire.write((int)(eeaddress >> 8)); // MSB
|
||||
Wire.write((int)(eeaddress & 0xFF)); // LSB
|
||||
Wire.endTransmission();
|
||||
Wire.requestFrom(I2C_EEP_ADDRESS,1);
|
||||
if (Wire.available()) rdata = Wire.read();
|
||||
return rdata;
|
||||
}
|
||||
|
||||
void hwReadConfigBlock(void* buf, void* adr, size_t length)
|
||||
{
|
||||
uint8_t* dst = static_cast<uint8_t*>(buf);
|
||||
int offs = reinterpret_cast<int>(adr);
|
||||
while (length-- > 0)
|
||||
{
|
||||
*dst++ = i2c_eeprom_read_byte(offs++);
|
||||
}
|
||||
}
|
||||
|
||||
void hwWriteConfigBlock(void* buf, void* adr, size_t length)
|
||||
{
|
||||
uint8_t* src = static_cast<uint8_t*>(buf);
|
||||
int offs = reinterpret_cast<int>(adr);
|
||||
while (length-- > 0)
|
||||
{
|
||||
i2c_eeprom_write_byte(offs++, *src++);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t hwReadConfig(int adr)
|
||||
{
|
||||
uint8_t value;
|
||||
hwReadConfigBlock(&value, reinterpret_cast<void*>(adr), 1);
|
||||
return value;
|
||||
}
|
||||
|
||||
void hwWriteConfig(int adr, uint8_t value)
|
||||
{
|
||||
uint8_t curr = hwReadConfig(adr);
|
||||
if (curr != value)
|
||||
{
|
||||
hwWriteConfigBlock(&value, reinterpret_cast<void*>(adr), 1);
|
||||
}
|
||||
}
|
||||
|
||||
void hwInit() {
|
||||
SERIALDEVICE.begin(MY_BAUD_RATE);
|
||||
while (!SERIALDEVICE) {}
|
||||
}
|
||||
|
||||
void hwWatchdogReset() {
|
||||
// TODO: Not supported!
|
||||
}
|
||||
|
||||
void hwReboot() {
|
||||
// TODO: Not supported!
|
||||
}
|
||||
|
||||
void hwSleep(unsigned long ms) {
|
||||
// TODO: Not supported!
|
||||
ms = ms + 1;
|
||||
}
|
||||
|
||||
bool hwSleep(uint8_t interrupt, uint8_t mode, unsigned long ms) {
|
||||
// TODO: Not supported!
|
||||
interrupt = interrupt;
|
||||
mode = mode;
|
||||
ms = ms;
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2, unsigned long ms) {
|
||||
// TODO: Not supported!
|
||||
interrupt1 = interrupt1;
|
||||
mode1 = mode1;
|
||||
interrupt2 = interrupt2;
|
||||
mode2 = mode2;
|
||||
ms = ms;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef MY_DEBUG
|
||||
void hwDebugPrint(const char *fmt, ... ) {
|
||||
char fmtBuffer[300];
|
||||
#ifdef MY_GATEWAY_FEATURE
|
||||
// prepend debug message to be handled correctly by controller (C_INTERNAL, I_LOG_MESSAGE)
|
||||
snprintf(fmtBuffer, 299, PSTR("0;0;%d;0;%d;"), C_INTERNAL, I_LOG_MESSAGE);
|
||||
SERIALDEVICE.print(fmtBuffer);
|
||||
#endif
|
||||
va_list args;
|
||||
va_start (args, fmt );
|
||||
va_end (args);
|
||||
#ifdef MY_GATEWAY_FEATURE
|
||||
// Truncate message if this is gateway node
|
||||
vsnprintf(fmtBuffer, 60, fmt, args);
|
||||
fmtBuffer[59] = '\n';
|
||||
fmtBuffer[60] = '\0';
|
||||
#else
|
||||
vsnprintf(fmtBuffer, 299, fmt, args);
|
||||
#endif
|
||||
va_end (args);
|
||||
SERIALDEVICE.print(fmtBuffer);
|
||||
// SERIALDEVICE.flush();
|
||||
|
||||
//Serial.write(freeRam());
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* The MySensors Arduino library handles the wireless radio link and protocol
|
||||
* between your home built sensors/actuators and HA controller of choice.
|
||||
* The sensors forms a self healing radio network with optional repeaters. Each
|
||||
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
|
||||
* network topology allowing messages to be routed to nodes.
|
||||
*
|
||||
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
|
||||
* Copyright (C) 2013-2015 Sensnology AB
|
||||
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
|
||||
*
|
||||
* Documentation: http://www.mysensors.org
|
||||
* Support Forum: http://forum.mysensors.org
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* version 2 as published by the Free Software Foundation.
|
||||
*/
|
||||
#ifndef MyHwSAMD_h
|
||||
#define MyHwSAMD_h
|
||||
|
||||
#include "MyHw.h"
|
||||
#include "MyConfig.h"
|
||||
|
||||
#include "MyMessage.h"
|
||||
#include <Wire.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
#include <Arduino.h>
|
||||
//#include <SPI.h>
|
||||
#endif
|
||||
|
||||
#include <avr/dtostrf.h>
|
||||
#define I2C_EEP_ADDRESS 55
|
||||
|
||||
#define min(a,b) ((a)<(b)?(a):(b))
|
||||
#define max(a,b) ((a)>(b)?(a):(b))
|
||||
|
||||
|
||||
// Define these as macros to save valuable space
|
||||
|
||||
uint8_t configBlock[1024];
|
||||
#define hwDigitalWrite(__pin, __value) (digitalWrite(__pin, __value))
|
||||
void hwInit();
|
||||
void hwWatchdogReset();
|
||||
void hwReboot();
|
||||
#define hwMillis() millis()
|
||||
|
||||
void hwReadConfigBlock(void* buf, void* adr, size_t length);
|
||||
void hwWriteConfigBlock(void* buf, void* adr, size_t length);
|
||||
void hwWriteConfig(int adr, uint8_t value);
|
||||
uint8_t hwReadConfig(int adr);
|
||||
|
||||
#define SERIALDEVICE SerialUSB
|
||||
|
||||
|
||||
/*
|
||||
#define hwReadConfigBlock(__buf, __adr, __length) ( __length = __length)
|
||||
#define hwWriteConfigBlock(__buf, __adr, __length) ( __length = __length)
|
||||
#define hwWriteConfig(__adr, __value) ( __value = __value)
|
||||
#define hwReadConfig(__adr) (0)
|
||||
*/
|
||||
#endif // #ifdef ARDUINO_ARCH_SAMD
|
||||
@@ -37,7 +37,7 @@ void inclusionModeSet(boolean newMode) {
|
||||
if (newMode != _inclusionMode) {
|
||||
_inclusionMode = newMode;
|
||||
// Send back mode change to controller
|
||||
gatewayTransportSend(buildGw(_msg, I_INCLUSION_MODE).set(_inclusionMode?1:0));
|
||||
gatewayTransportSend(buildGw(_msg, I_INCLUSION_MODE).set((uint8_t)(_inclusionMode?1:0)));
|
||||
if (_inclusionMode) {
|
||||
_inclusionStartTime = hwMillis();
|
||||
}
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
int main(void) {
|
||||
init();
|
||||
#if defined(USBCON)
|
||||
USBDevice.attach();
|
||||
USBDevice.init();
|
||||
USBDevice.attach();
|
||||
#endif
|
||||
_begin(); // Startup MySensors library
|
||||
|
||||
|
||||
@@ -24,7 +24,6 @@
|
||||
* HMAC-SHA256 authentication with a readout-protected key.
|
||||
*
|
||||
*/
|
||||
#if defined(ARDUINO_ARCH_AVR)
|
||||
|
||||
#include "MySigning.h"
|
||||
|
||||
@@ -64,7 +63,7 @@ static void DEBUG_SIGNING_PRINTBUF(const __FlashStringHelper* str, uint8_t* buf,
|
||||
#ifdef MY_GATEWAY_FEATURE
|
||||
// prepend debug message to be handled correctly by controller (C_INTERNAL, I_LOG_MESSAGE)
|
||||
snprintf_P(printBuffer, 299, PSTR("0;0;%d;0;%d;"), C_INTERNAL, I_LOG_MESSAGE);
|
||||
Serial.print(printBuffer);
|
||||
SERIALDEVICE.print(printBuffer);
|
||||
#endif
|
||||
for (int i=0; i<sz; i++)
|
||||
{
|
||||
@@ -76,12 +75,12 @@ static void DEBUG_SIGNING_PRINTBUF(const __FlashStringHelper* str, uint8_t* buf,
|
||||
// Truncate message if this is gateway node
|
||||
printBuffer[MY_GATEWAY_MAX_SEND_LENGTH-1-strlen_P((const char*)str)] = '\0';
|
||||
#endif
|
||||
Serial.print(str);
|
||||
SERIALDEVICE.print(str);
|
||||
if (sz > 0)
|
||||
{
|
||||
Serial.print(printBuffer);
|
||||
SERIALDEVICE.print(printBuffer);
|
||||
}
|
||||
Serial.println("");
|
||||
SERIALDEVICE.println("");
|
||||
}
|
||||
#else
|
||||
#define DEBUG_SIGNING_PRINTBUF(str, buf, sz)
|
||||
@@ -281,4 +280,3 @@ uint8_t* signerSha256(const uint8_t* data, size_t sz) {
|
||||
DEBUG_SIGNING_PRINTBUF(F("SHA256: "), &_singning_rx_buffer[SHA204_BUFFER_POS_DATA], 32);
|
||||
return &_singning_rx_buffer[SHA204_BUFFER_POS_DATA];
|
||||
}
|
||||
#endif // #if defined(ARDUINO_ARCH_AVR)
|
||||
|
||||
@@ -66,7 +66,7 @@ static void DEBUG_SIGNING_PRINTBUF(const __FlashStringHelper* str, uint8_t* buf,
|
||||
#ifdef MY_GATEWAY_FEATURE
|
||||
// prepend debug message to be handled correctly by controller (C_INTERNAL, I_LOG_MESSAGE)
|
||||
snprintf_P(printBuffer, 299, PSTR("0;0;%d;0;%d;"), C_INTERNAL, I_LOG_MESSAGE);
|
||||
Serial.print(printBuffer);
|
||||
SERIALDEVICE.print(printBuffer);
|
||||
#endif
|
||||
for (int i=0; i<sz; i++)
|
||||
{
|
||||
@@ -78,12 +78,12 @@ static void DEBUG_SIGNING_PRINTBUF(const __FlashStringHelper* str, uint8_t* buf,
|
||||
// Truncate message if this is gateway node
|
||||
printBuffer[MY_GATEWAY_MAX_SEND_LENGTH-1-strlen_P((const char*)str)] = '\0';
|
||||
#endif
|
||||
Serial.print(str);
|
||||
SERIALDEVICE.print(str);
|
||||
if (sz > 0)
|
||||
{
|
||||
Serial.print(printBuffer);
|
||||
SERIALDEVICE.print(printBuffer);
|
||||
}
|
||||
Serial.println("");
|
||||
SERIALDEVICE.println("");
|
||||
}
|
||||
#else
|
||||
#define DEBUG_SIGNING_PRINTBUF(str, buf, sz)
|
||||
|
||||
@@ -199,11 +199,11 @@ inline void transportProcess() {
|
||||
#if defined (MY_GATEWAY_FEATURE)
|
||||
#ifdef MY_SIGNING_REQUEST_SIGNATURES
|
||||
if (DO_SIGN(sender))
|
||||
_sendRoute(build(_msg, _nc.nodeId, sender, NODE_SENSOR_ID, C_INTERNAL, I_REQUEST_SIGNING, false).set(true));
|
||||
_sendRoute(build(_msg, _nc.nodeId, sender, NODE_SENSOR_ID, C_INTERNAL, I_REQUEST_SIGNING, false).set((uint8_t)true));
|
||||
else
|
||||
_sendRoute(build(_msg, _nc.nodeId, sender, NODE_SENSOR_ID, C_INTERNAL, I_REQUEST_SIGNING, false).set(false));
|
||||
_sendRoute(build(_msg, _nc.nodeId, sender, NODE_SENSOR_ID, C_INTERNAL, I_REQUEST_SIGNING, false).set((uint8_t)false));
|
||||
#else
|
||||
_sendRoute(build(_msg, _nc.nodeId, sender, NODE_SENSOR_ID, C_INTERNAL, I_REQUEST_SIGNING, false).set(false));
|
||||
_sendRoute(build(_msg, _nc.nodeId, sender, NODE_SENSOR_ID, C_INTERNAL, I_REQUEST_SIGNING, false).set((uint8_t)false));
|
||||
#endif
|
||||
#endif
|
||||
return; // Signing request is an internal MySensor protocol message, no need to inform caller about this
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
#if defined(ARDUINO_ARCH_AVR)
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "ATSHA204.h"
|
||||
|
||||
@@ -9,15 +7,7 @@
|
||||
// As well as the bit value for each of those registers
|
||||
ATSHA204Class::ATSHA204Class(uint8_t pin)
|
||||
{
|
||||
device_pin = digitalPinToBitMask(pin); // Find the bit value of the pin
|
||||
uint8_t port = digitalPinToPort(pin); // temoporarily used to get the next three registers
|
||||
|
||||
// Point to data direction register port of pin
|
||||
device_port_DDR = portModeRegister(port);
|
||||
// Point to output register of pin
|
||||
device_port_OUT = portOutputRegister(port);
|
||||
// Point to input register of pin
|
||||
device_port_IN = portInputRegister(port);
|
||||
device_pin = pin; // Find the bit value of the pin
|
||||
}
|
||||
|
||||
void ATSHA204Class::getSerialNumber(uint8_t * response)
|
||||
@@ -52,12 +42,12 @@ void ATSHA204Class::getSerialNumber(uint8_t * response)
|
||||
|
||||
void ATSHA204Class::swi_set_signal_pin(uint8_t is_high)
|
||||
{
|
||||
*device_port_DDR |= device_pin;
|
||||
SHA204_SET_OUTPUT();
|
||||
|
||||
if (is_high)
|
||||
*device_port_OUT |= device_pin;
|
||||
SHA204_POUT_HIGH();
|
||||
else
|
||||
*device_port_OUT &= ~device_pin;
|
||||
SHA204_POUT_LOW();
|
||||
}
|
||||
|
||||
uint8_t ATSHA204Class::swi_send_bytes(uint8_t count, uint8_t *buffer)
|
||||
@@ -68,8 +58,9 @@ uint8_t ATSHA204Class::swi_send_bytes(uint8_t count, uint8_t *buffer)
|
||||
noInterrupts(); //swi_disable_interrupts();
|
||||
|
||||
// Set signal pin as output.
|
||||
*device_port_OUT |= device_pin;
|
||||
*device_port_DDR |= device_pin;
|
||||
SHA204_POUT_HIGH();
|
||||
SHA204_SET_OUTPUT();
|
||||
|
||||
|
||||
// Wait turn around time.
|
||||
delayMicroseconds(RX_TX_DELAY); //RX_TX_DELAY;
|
||||
@@ -80,21 +71,21 @@ uint8_t ATSHA204Class::swi_send_bytes(uint8_t count, uint8_t *buffer)
|
||||
{
|
||||
if (bit_mask & buffer[i])
|
||||
{
|
||||
*device_port_OUT &= ~device_pin;
|
||||
SHA204_POUT_LOW() //*device_port_OUT &= ~device_pin;
|
||||
delayMicroseconds(BIT_DELAY); //BIT_DELAY_1;
|
||||
*device_port_OUT |= device_pin;
|
||||
SHA204_POUT_HIGH() //*device_port_OUT |= device_pin;
|
||||
delayMicroseconds(7*BIT_DELAY); //BIT_DELAY_7;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Send a zero bit.
|
||||
*device_port_OUT &= ~device_pin;
|
||||
SHA204_POUT_LOW() //*device_port_OUT &= ~device_pin;
|
||||
delayMicroseconds(BIT_DELAY); //BIT_DELAY_1;
|
||||
*device_port_OUT |= device_pin;
|
||||
SHA204_POUT_HIGH() //*device_port_OUT |= device_pin;
|
||||
delayMicroseconds(BIT_DELAY); //BIT_DELAY_1;
|
||||
*device_port_OUT &= ~device_pin;
|
||||
SHA204_POUT_LOW() //*device_port_OUT &= ~device_pin;
|
||||
delayMicroseconds(BIT_DELAY); //BIT_DELAY_1;
|
||||
*device_port_OUT |= device_pin;
|
||||
SHA204_POUT_HIGH() //*device_port_OUT |= device_pin;
|
||||
delayMicroseconds(5*BIT_DELAY); //BIT_DELAY_5;
|
||||
}
|
||||
}
|
||||
@@ -120,8 +111,8 @@ uint8_t ATSHA204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer)
|
||||
noInterrupts(); //swi_disable_interrupts();
|
||||
|
||||
// Configure signal pin as input.
|
||||
*device_port_DDR &= ~device_pin;
|
||||
|
||||
SHA204_SET_INPUT();
|
||||
|
||||
// Receive bits and store in buffer.
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
@@ -138,7 +129,7 @@ uint8_t ATSHA204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer)
|
||||
while (--timeout_count > 0)
|
||||
{
|
||||
// Wait for falling edge.
|
||||
if ((*device_port_IN & device_pin) == 0)
|
||||
if (SHA204_PIN_READ() == 0)
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -151,7 +142,7 @@ uint8_t ATSHA204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer)
|
||||
do
|
||||
{
|
||||
// Wait for rising edge.
|
||||
if ((*device_port_IN & device_pin) != 0)
|
||||
if (SHA204_PIN_READ() != 0)
|
||||
{
|
||||
// For an Atmel microcontroller this might be faster than "pulse_count++".
|
||||
pulse_count = 1;
|
||||
@@ -174,7 +165,7 @@ uint8_t ATSHA204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer)
|
||||
// Detect possible edge indicating zero bit.
|
||||
do
|
||||
{
|
||||
if ((*device_port_IN & device_pin) == 0)
|
||||
if (SHA204_PIN_READ() == 0)
|
||||
{
|
||||
// For an Atmel microcontroller this might be faster than "pulse_count++".
|
||||
pulse_count = 2;
|
||||
@@ -188,7 +179,7 @@ uint8_t ATSHA204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer)
|
||||
{
|
||||
do
|
||||
{
|
||||
if ((*device_port_IN & device_pin) != 0)
|
||||
if (SHA204_PIN_READ() != 0)
|
||||
break;
|
||||
} while (timeout_count-- > 0);
|
||||
}
|
||||
@@ -575,4 +566,3 @@ uint8_t ATSHA204Class::sha204c_check_crc(uint8_t *response)
|
||||
? SHA204_SUCCESS : SHA204_BAD_CRC;
|
||||
}
|
||||
|
||||
#endif // defined(ARDUINO_ARCH_AVR)
|
||||
@@ -220,11 +220,27 @@
|
||||
|
||||
#define SHA204_SERIAL_SZ 9 // The number of bytes the serial number consists of
|
||||
|
||||
/* Low level HW access macros */
|
||||
/* function calls is not working, as it will have too much overhead */
|
||||
#if !defined(ARDUINO_ARCH_AVR) // For everything else than AVR use pinMode / digitalWrite
|
||||
#define SHA204_SET_OUTPUT() pinMode(device_pin, OUTPUT)
|
||||
#define SHA204_SET_INPUT() pinMode(device_pin, INPUT)
|
||||
#define SHA204_POUT_HIGH() digitalWrite(device_pin, HIGH)
|
||||
#define SHA204_POUT_LOW() digitalWrite(device_pin, LOW)
|
||||
#define SHA204_PIN_READ() digitalRead(device_pin)
|
||||
#else
|
||||
#define SHA204_SET_INPUT() *device_port_DDR &= ~device_pin
|
||||
#define SHA204_SET_OUTPUT() *device_port_DDR |= device_pin
|
||||
#define SHA204_POUT_HIGH() *device_port_OUT |= device_pin
|
||||
#define SHA204_POUT_LOW() *device_port_OUT &= ~device_pin
|
||||
#define SHA204_PIN_READ() (*device_port_IN & device_pin)
|
||||
#endif
|
||||
|
||||
class ATSHA204Class
|
||||
{
|
||||
private:
|
||||
uint8_t device_pin;
|
||||
volatile uint8_t *device_port_DDR, *device_port_OUT, *device_port_IN;
|
||||
// volatile uint8_t *device_port_DDR, *device_port_OUT, *device_port_IN;
|
||||
void sha204c_calculate_crc(uint8_t length, uint8_t *data, uint8_t *crc);
|
||||
uint8_t sha204c_check_crc(uint8_t *response);
|
||||
void swi_set_signal_pin(uint8_t is_high);
|
||||
|
||||
@@ -51,7 +51,7 @@ void RF24::csn(bool mode)
|
||||
delayMicroseconds(11); // allow csn to settle
|
||||
}
|
||||
}
|
||||
#elif !defined (__arm__) || defined (CORE_TEENSY)
|
||||
#elif !defined (__arm__) || defined (CORE_TEENSY) || defined(ARDUINO_ARCH_SAMD)
|
||||
digitalWrite(csn_pin,mode);
|
||||
#endif
|
||||
|
||||
@@ -71,7 +71,7 @@ uint8_t RF24::read_register(uint8_t reg, uint8_t* buf, uint8_t len)
|
||||
{
|
||||
uint8_t status;
|
||||
|
||||
#if defined (__arm__) && !defined ( CORE_TEENSY )
|
||||
#if defined (__arm__) && !defined ( CORE_TEENSY ) && !defined (ARDUINO_ARCH_SAMD)
|
||||
status = _SPI.transfer(csn_pin, R_REGISTER | ( REGISTER_MASK & reg ), SPI_CONTINUE );
|
||||
while ( len-- > 1 ){
|
||||
*buf++ = _SPI.transfer(csn_pin,0xff, SPI_CONTINUE);
|
||||
@@ -96,7 +96,7 @@ uint8_t RF24::read_register(uint8_t reg, uint8_t* buf, uint8_t len)
|
||||
uint8_t RF24::read_register(uint8_t reg)
|
||||
{
|
||||
|
||||
#if defined (__arm__) && !defined ( CORE_TEENSY )
|
||||
#if defined (__arm__) && !defined ( CORE_TEENSY ) && !defined (ARDUINO_ARCH_SAMD)
|
||||
_SPI.transfer(csn_pin, R_REGISTER | ( REGISTER_MASK & reg ) , SPI_CONTINUE);
|
||||
uint8_t result = _SPI.transfer(csn_pin,0xff);
|
||||
#else
|
||||
@@ -116,7 +116,7 @@ uint8_t RF24::write_register(uint8_t reg, const uint8_t* buf, uint8_t len)
|
||||
{
|
||||
uint8_t status;
|
||||
|
||||
#if defined (__arm__) && !defined ( CORE_TEENSY )
|
||||
#if defined (__arm__) && !defined ( CORE_TEENSY ) && !defined (ARDUINO_ARCH_SAMD)
|
||||
status = _SPI.transfer(csn_pin, W_REGISTER | ( REGISTER_MASK & reg ), SPI_CONTINUE );
|
||||
while ( --len){
|
||||
_SPI.transfer(csn_pin,*buf++, SPI_CONTINUE);
|
||||
@@ -146,7 +146,7 @@ uint8_t RF24::write_register(uint8_t reg, uint8_t value)
|
||||
Serial.print(F("write_register(")); print_hex(reg, true); Serial.print(F(",")); print_hex(value, true); Serial.println(F(")"));
|
||||
#endif
|
||||
|
||||
#if defined (__arm__) && !defined ( CORE_TEENSY )
|
||||
#if defined (__arm__) && !defined ( CORE_TEENSY ) && !defined (ARDUINO_ARCH_SAMD)
|
||||
status = _SPI.transfer(csn_pin, W_REGISTER | ( REGISTER_MASK & reg ), SPI_CONTINUE);
|
||||
_SPI.transfer(csn_pin,value);
|
||||
#else
|
||||
@@ -171,7 +171,7 @@ uint8_t RF24::write_payload(const void* buf, uint8_t data_len, const uint8_t wri
|
||||
|
||||
//printf("[Writing %u bytes %u blanks]",data_len,blank_len);
|
||||
|
||||
#if defined (__arm__) && !defined ( CORE_TEENSY )
|
||||
#if defined (__arm__) && !defined ( CORE_TEENSY ) && !defined (ARDUINO_ARCH_SAMD)
|
||||
|
||||
status = _SPI.transfer(csn_pin, writeType , SPI_CONTINUE);
|
||||
|
||||
@@ -220,7 +220,7 @@ uint8_t RF24::read_payload(void* buf, uint8_t data_len)
|
||||
//printf("[Reading %u bytes %u blanks]",data_len,blank_len);
|
||||
|
||||
|
||||
#if defined (__arm__) && !defined ( CORE_TEENSY )
|
||||
#if defined (__arm__) && !defined ( CORE_TEENSY ) && !defined (ARDUINO_ARCH_SAMD)
|
||||
|
||||
status = _SPI.transfer(csn_pin, R_RX_PAYLOAD, SPI_CONTINUE );
|
||||
|
||||
@@ -276,7 +276,7 @@ uint8_t RF24::flush_tx(void)
|
||||
uint8_t RF24::spiTrans(uint8_t cmd){
|
||||
|
||||
uint8_t status;
|
||||
#if defined (__arm__) && !defined ( CORE_TEENSY )
|
||||
#if defined (__arm__) && !defined ( CORE_TEENSY ) && !defined (ARDUINO_ARCH_SAMD)
|
||||
status = _SPI.transfer(csn_pin, cmd );
|
||||
#else
|
||||
|
||||
@@ -481,7 +481,7 @@ void RF24::begin(void)
|
||||
// Initialize pins
|
||||
if (ce_pin != csn_pin) pinMode(ce_pin,OUTPUT);
|
||||
|
||||
#if defined(__arm__) && ! defined( CORE_TEENSY )
|
||||
#if defined(__arm__) && ! defined( CORE_TEENSY ) && !defined (ARDUINO_ARCH_SAMD)
|
||||
_SPI.begin(csn_pin); // Using the extended SPI features of the DUE
|
||||
_SPI.setClockDivider(csn_pin, 9); // Set the bus speed to 8.4mhz on Due
|
||||
_SPI.setBitOrder(csn_pin,MSBFIRST); // Set the bit order and mode specific to this device
|
||||
@@ -888,7 +888,7 @@ uint8_t RF24::getDynamicPayloadSize(void)
|
||||
{
|
||||
uint8_t result = 0;
|
||||
|
||||
#if defined (__arm__) && ! defined( CORE_TEENSY )
|
||||
#if defined (__arm__) && ! defined( CORE_TEENSY ) && !defined (ARDUINO_ARCH_SAMD)
|
||||
_SPI.transfer(csn_pin, R_RX_PL_WID, SPI_CONTINUE );
|
||||
result = _SPI.transfer(csn_pin,0xff);
|
||||
#else
|
||||
@@ -1079,7 +1079,7 @@ void RF24::closeReadingPipe( uint8_t pipe )
|
||||
void RF24::toggle_features(void)
|
||||
{
|
||||
|
||||
#if defined (__arm__) && ! defined( CORE_TEENSY )
|
||||
#if defined (__arm__) && ! defined( CORE_TEENSY ) && !defined (ARDUINO_ARCH_SAMD)
|
||||
_SPI.transfer(csn_pin, ACTIVATE, SPI_CONTINUE );
|
||||
_SPI.transfer(csn_pin, 0x73 );
|
||||
#else
|
||||
@@ -1145,7 +1145,7 @@ void RF24::writeAckPayload(uint8_t pipe, const void* buf, uint8_t len)
|
||||
|
||||
uint8_t data_len = min(len,32);
|
||||
|
||||
#if defined (__arm__) && ! defined( CORE_TEENSY )
|
||||
#if defined (__arm__) && ! defined( CORE_TEENSY ) && !defined (ARDUINO_ARCH_SAMD)
|
||||
_SPI.transfer(csn_pin, W_ACK_PAYLOAD | ( pipe & B111 ), SPI_CONTINUE);
|
||||
while ( data_len-- > 1 ){
|
||||
_SPI.transfer(csn_pin,*current++, SPI_CONTINUE);
|
||||
|
||||
@@ -28,12 +28,11 @@
|
||||
/**********************/
|
||||
|
||||
// Define _BV for non-Arduino platforms and for Arduino DUE
|
||||
#if defined (ARDUINO) && !defined (__arm__)
|
||||
#if defined (ARDUINO) && !defined (__arm__) && !defined(_SPI)
|
||||
#if defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) || defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
|
||||
#define RF24_TINY
|
||||
#define _SPI SPI
|
||||
#else
|
||||
#if defined SPI_UART
|
||||
#elif defined SPI_UART
|
||||
#include <SPI_UART.h>
|
||||
#define _SPI uspi
|
||||
#elif defined MY_SOFTSPI
|
||||
@@ -46,11 +45,12 @@
|
||||
|
||||
const uint8_t SPI_MODE = 0;
|
||||
#define _SPI spi
|
||||
|
||||
#elif defined(ARDUINO_ARCH_SAMD)
|
||||
#include <SPI.h>
|
||||
#define _SPI SPI1
|
||||
#else
|
||||
#include <SPI.h>
|
||||
#define _SPI SPI
|
||||
#endif
|
||||
#endif
|
||||
#else
|
||||
#include <stdint.h>
|
||||
@@ -70,10 +70,10 @@
|
||||
#else
|
||||
#define printf Serial.printf
|
||||
#endif
|
||||
|
||||
#if ! defined(_SPI)
|
||||
#define _SPI SPI
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
// Avoid spurious warnings
|
||||
// Arduino DUE is arm and uses traditional PROGMEM constructs
|
||||
@@ -101,7 +101,7 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if !defined ( CORE_TEENSY ) && ! defined(ESP8266)
|
||||
#if !defined ( CORE_TEENSY ) && ! defined(ESP8266) && ! defined(ARDUINO_ARCH_SAMD)
|
||||
typedef uint16_t prog_uint16_t;
|
||||
#define PSTR(x) (x)
|
||||
#define printf_P printf
|
||||
|
||||
@@ -5,10 +5,13 @@
|
||||
|
||||
// atsha204Class Constructor
|
||||
// Feed this function the Arduino-ized pin number you want to assign to the ATSHA204's SDA pin
|
||||
// This will find the DDRX, PORTX, and PINX registrs it'll need to point to to control that pin
|
||||
// For AVR ARCH, this will find the DDRX, PORTX, and PINX registrs it'll need to point to to control that pin
|
||||
// As well as the bit value for each of those registers
|
||||
// For others it will only store the pin number which is used in subsequent functions
|
||||
|
||||
atsha204Class::atsha204Class(uint8_t pin)
|
||||
{
|
||||
#if defined(ARDUINO_ARCH_AVR)
|
||||
device_pin = digitalPinToBitMask(pin); // Find the bit value of the pin
|
||||
uint8_t port = digitalPinToPort(pin); // temoporarily used to get the next three registers
|
||||
|
||||
@@ -18,6 +21,9 @@ atsha204Class::atsha204Class(uint8_t pin)
|
||||
device_port_OUT = portOutputRegister(port);
|
||||
// Point to input register of pin
|
||||
device_port_IN = portInputRegister(port);
|
||||
#else
|
||||
device_pin = pin;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Puts a the ATSHA204's unique, 4-byte serial number in the response array
|
||||
@@ -81,12 +87,14 @@ uint16_t atsha204Class::calculateAndUpdateCrc(uint8_t length, uint8_t *data, uin
|
||||
|
||||
void atsha204Class::swi_set_signal_pin(uint8_t is_high)
|
||||
{
|
||||
*device_port_DDR |= device_pin;
|
||||
SHA204_SET_OUTPUT();
|
||||
if (is_high) {
|
||||
SHA204_POUT_HIGH();
|
||||
}
|
||||
else {
|
||||
SHA204_POUT_LOW();
|
||||
}
|
||||
|
||||
if (is_high)
|
||||
*device_port_OUT |= device_pin;
|
||||
else
|
||||
*device_port_OUT &= ~device_pin;
|
||||
}
|
||||
|
||||
uint8_t atsha204Class::swi_send_bytes(uint8_t count, uint8_t *buffer)
|
||||
@@ -97,9 +105,8 @@ uint8_t atsha204Class::swi_send_bytes(uint8_t count, uint8_t *buffer)
|
||||
noInterrupts(); //swi_disable_interrupts();
|
||||
|
||||
// Set signal pin as output.
|
||||
*device_port_OUT |= device_pin;
|
||||
*device_port_DDR |= device_pin;
|
||||
|
||||
SHA204_POUT_HIGH();
|
||||
SHA204_SET_OUTPUT();
|
||||
// Wait turn around time.
|
||||
delayMicroseconds(RX_TX_DELAY); //RX_TX_DELAY;
|
||||
|
||||
@@ -109,21 +116,21 @@ uint8_t atsha204Class::swi_send_bytes(uint8_t count, uint8_t *buffer)
|
||||
{
|
||||
if (bit_mask & buffer[i])
|
||||
{
|
||||
*device_port_OUT &= ~device_pin;
|
||||
SHA204_POUT_LOW();
|
||||
delayMicroseconds(BIT_DELAY); //BIT_DELAY_1;
|
||||
*device_port_OUT |= device_pin;
|
||||
SHA204_POUT_HIGH();
|
||||
delayMicroseconds(7*BIT_DELAY); //BIT_DELAY_7;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Send a zero bit.
|
||||
*device_port_OUT &= ~device_pin;
|
||||
SHA204_POUT_LOW();
|
||||
delayMicroseconds(BIT_DELAY); //BIT_DELAY_1;
|
||||
*device_port_OUT |= device_pin;
|
||||
SHA204_POUT_HIGH();
|
||||
delayMicroseconds(BIT_DELAY); //BIT_DELAY_1;
|
||||
*device_port_OUT &= ~device_pin;
|
||||
SHA204_POUT_LOW();
|
||||
delayMicroseconds(BIT_DELAY); //BIT_DELAY_1;
|
||||
*device_port_OUT |= device_pin;
|
||||
SHA204_POUT_HIGH();
|
||||
delayMicroseconds(5*BIT_DELAY); //BIT_DELAY_5;
|
||||
}
|
||||
}
|
||||
@@ -148,8 +155,8 @@ uint8_t atsha204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer)
|
||||
// Disable interrupts while receiving.
|
||||
noInterrupts(); //swi_disable_interrupts();
|
||||
|
||||
// Configure signal pin as input.
|
||||
*device_port_DDR &= ~device_pin;
|
||||
// Configure signal pin as input
|
||||
SHA204_SET_INPUT();
|
||||
|
||||
// Receive bits and store in buffer.
|
||||
for (i = 0; i < count; i++)
|
||||
@@ -167,7 +174,7 @@ uint8_t atsha204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer)
|
||||
while (--timeout_count > 0)
|
||||
{
|
||||
// Wait for falling edge.
|
||||
if ((*device_port_IN & device_pin) == 0)
|
||||
if (SHA204_PIN_READ() == 0)
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -180,7 +187,7 @@ uint8_t atsha204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer)
|
||||
do
|
||||
{
|
||||
// Wait for rising edge.
|
||||
if ((*device_port_IN & device_pin) != 0)
|
||||
if (SHA204_PIN_READ() != 0)
|
||||
{
|
||||
// For an Atmel microcontroller this might be faster than "pulse_count++".
|
||||
pulse_count = 1;
|
||||
@@ -203,7 +210,7 @@ uint8_t atsha204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer)
|
||||
// Detect possible edge indicating zero bit.
|
||||
do
|
||||
{
|
||||
if ((*device_port_IN & device_pin) == 0)
|
||||
if (SHA204_PIN_READ() == 0)
|
||||
{
|
||||
// For an Atmel microcontroller this might be faster than "pulse_count++".
|
||||
pulse_count = 2;
|
||||
@@ -217,7 +224,7 @@ uint8_t atsha204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer)
|
||||
{
|
||||
do
|
||||
{
|
||||
if ((*device_port_IN & device_pin) != 0)
|
||||
if (SHA204_PIN_READ() != 0)
|
||||
break;
|
||||
} while (timeout_count-- > 0);
|
||||
}
|
||||
|
||||
@@ -279,12 +279,29 @@
|
||||
#define ADDRESS_OTPMODE 18 // Sets the One-time-programmable mode
|
||||
#define ADDRESS_SELECTOR 19 // Controls writability of Selector
|
||||
|
||||
/* Low level HW access macros */
|
||||
/* function calls is not working, as it will have too much overhead */
|
||||
#if !defined(ARDUINO_ARCH_AVR) // For everything else than AVR use pinMode / digitalWrite
|
||||
#define SHA204_SET_OUTPUT() pinMode(device_pin, OUTPUT)
|
||||
#define SHA204_SET_INPUT() pinMode(device_pin, INPUT)
|
||||
#define SHA204_POUT_HIGH() digitalWrite(device_pin, HIGH)
|
||||
#define SHA204_POUT_LOW() digitalWrite(device_pin, LOW)
|
||||
#define SHA204_PIN_READ() digitalRead(device_pin)
|
||||
#else
|
||||
#define SHA204_SET_INPUT() *device_port_DDR &= ~device_pin
|
||||
#define SHA204_SET_OUTPUT() *device_port_DDR |= device_pin
|
||||
#define SHA204_POUT_HIGH() *device_port_OUT |= device_pin
|
||||
#define SHA204_POUT_LOW() *device_port_OUT &= ~device_pin
|
||||
#define SHA204_PIN_READ() (*device_port_IN & device_pin)
|
||||
#endif
|
||||
|
||||
class atsha204Class
|
||||
{
|
||||
private:
|
||||
uint8_t device_pin;
|
||||
#ifdef ARDUINO_ARCH_AVR
|
||||
volatile uint8_t *device_port_DDR, *device_port_OUT, *device_port_IN;
|
||||
#endif
|
||||
void sha204c_calculate_crc(uint8_t length, uint8_t *data, uint8_t *crc);
|
||||
uint8_t sha204c_check_crc(uint8_t *response);
|
||||
void swi_set_signal_pin(uint8_t is_high);
|
||||
|
||||
Reference in New Issue
Block a user