Changed HW signing (atsha204) to be SAMD compatible

This commit is contained in:
Thomas Mørch
2015-12-06 23:16:44 +01:00
parent c62aca4125
commit 5b3fac51cc
4 changed files with 21 additions and 34 deletions
@@ -24,7 +24,6 @@
* HMAC-SHA256 authentication with a readout-protected key. * HMAC-SHA256 authentication with a readout-protected key.
* *
*/ */
#if defined(ARDUINO_ARCH_AVR)
#include "MySigning.h" #include "MySigning.h"
@@ -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); DEBUG_SIGNING_PRINTBUF(F("SHA256: "), &_singning_rx_buffer[SHA204_BUFFER_POS_DATA], 32);
return &_singning_rx_buffer[SHA204_BUFFER_POS_DATA]; return &_singning_rx_buffer[SHA204_BUFFER_POS_DATA];
} }
#endif // #if defined(ARDUINO_ARCH_AVR)
+3 -3
View File
@@ -199,11 +199,11 @@ inline void transportProcess() {
#if defined (MY_GATEWAY_FEATURE) #if defined (MY_GATEWAY_FEATURE)
#ifdef MY_SIGNING_REQUEST_SIGNATURES #ifdef MY_SIGNING_REQUEST_SIGNATURES
if (DO_SIGN(sender)) 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 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 #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
#endif #endif
return; // Signing request is an internal MySensor protocol message, no need to inform caller about this 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 "Arduino.h"
#include "ATSHA204.h" #include "ATSHA204.h"
@@ -9,15 +7,7 @@
// As well as the bit value for each of those registers // As well as the bit value for each of those registers
ATSHA204Class::ATSHA204Class(uint8_t pin) ATSHA204Class::ATSHA204Class(uint8_t pin)
{ {
device_pin = digitalPinToBitMask(pin); // Find the bit value of the pin device_pin = 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);
} }
void ATSHA204Class::getSerialNumber(uint8_t * response) 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) void ATSHA204Class::swi_set_signal_pin(uint8_t is_high)
{ {
*device_port_DDR |= device_pin; pinMode(device_pin, OUTPUT);
if (is_high) if (is_high)
*device_port_OUT |= device_pin; digitalWrite(device_pin, HIGH);
else else
*device_port_OUT &= ~device_pin; digitalWrite(device_pin, LOW);
} }
uint8_t ATSHA204Class::swi_send_bytes(uint8_t count, uint8_t *buffer) uint8_t ATSHA204Class::swi_send_bytes(uint8_t count, uint8_t *buffer)
@@ -68,8 +58,8 @@ uint8_t ATSHA204Class::swi_send_bytes(uint8_t count, uint8_t *buffer)
noInterrupts(); //swi_disable_interrupts(); noInterrupts(); //swi_disable_interrupts();
// Set signal pin as output. // Set signal pin as output.
*device_port_OUT |= device_pin; pinMode(device_pin, OUTPUT);
*device_port_DDR |= device_pin; digitalWrite(device_pin, HIGH);
// Wait turn around time. // Wait turn around time.
delayMicroseconds(RX_TX_DELAY); //RX_TX_DELAY; delayMicroseconds(RX_TX_DELAY); //RX_TX_DELAY;
@@ -80,21 +70,21 @@ uint8_t ATSHA204Class::swi_send_bytes(uint8_t count, uint8_t *buffer)
{ {
if (bit_mask & buffer[i]) if (bit_mask & buffer[i])
{ {
*device_port_OUT &= ~device_pin; digitalWrite(device_pin, LOW); //*device_port_OUT &= ~device_pin;
delayMicroseconds(BIT_DELAY); //BIT_DELAY_1; delayMicroseconds(BIT_DELAY); //BIT_DELAY_1;
*device_port_OUT |= device_pin; digitalWrite(device_pin, HIGH); //*device_port_OUT |= device_pin;
delayMicroseconds(7*BIT_DELAY); //BIT_DELAY_7; delayMicroseconds(7*BIT_DELAY); //BIT_DELAY_7;
} }
else else
{ {
// Send a zero bit. // Send a zero bit.
*device_port_OUT &= ~device_pin; digitalWrite(device_pin, LOW); //*device_port_OUT &= ~device_pin;
delayMicroseconds(BIT_DELAY); //BIT_DELAY_1; delayMicroseconds(BIT_DELAY); //BIT_DELAY_1;
*device_port_OUT |= device_pin; digitalWrite(device_pin, HIGH); //*device_port_OUT |= device_pin;
delayMicroseconds(BIT_DELAY); //BIT_DELAY_1; delayMicroseconds(BIT_DELAY); //BIT_DELAY_1;
*device_port_OUT &= ~device_pin; digitalWrite(device_pin, LOW); //*device_port_OUT &= ~device_pin;
delayMicroseconds(BIT_DELAY); //BIT_DELAY_1; delayMicroseconds(BIT_DELAY); //BIT_DELAY_1;
*device_port_OUT |= device_pin; digitalWrite(device_pin, HIGH); //*device_port_OUT |= device_pin;
delayMicroseconds(5*BIT_DELAY); //BIT_DELAY_5; delayMicroseconds(5*BIT_DELAY); //BIT_DELAY_5;
} }
} }
@@ -120,7 +110,7 @@ uint8_t ATSHA204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer)
noInterrupts(); //swi_disable_interrupts(); noInterrupts(); //swi_disable_interrupts();
// Configure signal pin as input. // Configure signal pin as input.
*device_port_DDR &= ~device_pin; pinMode(device_pin, INPUT);
// Receive bits and store in buffer. // Receive bits and store in buffer.
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
@@ -138,7 +128,7 @@ uint8_t ATSHA204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer)
while (--timeout_count > 0) while (--timeout_count > 0)
{ {
// Wait for falling edge. // Wait for falling edge.
if ((*device_port_IN & device_pin) == 0) if (digitalRead(device_pin) == 0)
break; break;
} }
@@ -151,7 +141,7 @@ uint8_t ATSHA204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer)
do do
{ {
// Wait for rising edge. // Wait for rising edge.
if ((*device_port_IN & device_pin) != 0) if (digitalRead(device_pin) != 0)
{ {
// For an Atmel microcontroller this might be faster than "pulse_count++". // For an Atmel microcontroller this might be faster than "pulse_count++".
pulse_count = 1; pulse_count = 1;
@@ -174,7 +164,7 @@ uint8_t ATSHA204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer)
// Detect possible edge indicating zero bit. // Detect possible edge indicating zero bit.
do do
{ {
if ((*device_port_IN & device_pin) == 0) if (digitalRead(device_pin) == 0)
{ {
// For an Atmel microcontroller this might be faster than "pulse_count++". // For an Atmel microcontroller this might be faster than "pulse_count++".
pulse_count = 2; pulse_count = 2;
@@ -188,7 +178,7 @@ uint8_t ATSHA204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer)
{ {
do do
{ {
if ((*device_port_IN & device_pin) != 0) if (digitalRead(device_pin) != 0)
break; break;
} while (timeout_count-- > 0); } while (timeout_count-- > 0);
} }
@@ -575,4 +565,3 @@ uint8_t ATSHA204Class::sha204c_check_crc(uint8_t *response)
? SHA204_SUCCESS : SHA204_BAD_CRC; ? SHA204_SUCCESS : SHA204_BAD_CRC;
} }
#endif // defined(ARDUINO_ARCH_AVR)
@@ -224,7 +224,7 @@ class ATSHA204Class
{ {
private: private:
uint8_t device_pin; 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); void sha204c_calculate_crc(uint8_t length, uint8_t *data, uint8_t *crc);
uint8_t sha204c_check_crc(uint8_t *response); uint8_t sha204c_check_crc(uint8_t *response);
void swi_set_signal_pin(uint8_t is_high); void swi_set_signal_pin(uint8_t is_high);