From 396a5fe5b03227f450b746d1cd147ecbe191d3b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=B8rch?= Date: Tue, 8 Dec 2015 21:12:57 +0100 Subject: [PATCH] converted (fat) sha204 to use standard arduino cmds for pin handling --- libraries/sha204/sha204_library.cpp | 46 ++++++++++++----------------- libraries/sha204/sha204_library.h | 1 - 2 files changed, 19 insertions(+), 28 deletions(-) diff --git a/libraries/sha204/sha204_library.cpp b/libraries/sha204/sha204_library.cpp index 8728096..d06c6d4 100644 --- a/libraries/sha204/sha204_library.cpp +++ b/libraries/sha204/sha204_library.cpp @@ -9,15 +9,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 } /* Puts a the ATSHA204's unique, 4-byte serial number in the response array @@ -81,12 +73,12 @@ 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; + pinMode(device_pin, OUTPUT); if (is_high) - *device_port_OUT |= device_pin; + digitalWrite(device_pin, HIGH); else - *device_port_OUT &= ~device_pin; + digitalWrite(device_pin, LOW); } uint8_t atsha204Class::swi_send_bytes(uint8_t count, uint8_t *buffer) @@ -97,8 +89,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; + pinMode(device_pin, OUTPUT); + digitalWrite(device_pin, HIGH); // Wait turn around time. delayMicroseconds(RX_TX_DELAY); //RX_TX_DELAY; @@ -109,22 +101,22 @@ uint8_t atsha204Class::swi_send_bytes(uint8_t count, uint8_t *buffer) { if (bit_mask & buffer[i]) { - *device_port_OUT &= ~device_pin; + digitalWrite(device_pin, LOW); delayMicroseconds(BIT_DELAY); //BIT_DELAY_1; - *device_port_OUT |= device_pin; + digitalWrite(device_pin, HIGH); delayMicroseconds(7*BIT_DELAY); //BIT_DELAY_7; } else { // Send a zero bit. - *device_port_OUT &= ~device_pin; + digitalWrite(device_pin, LOW); delayMicroseconds(BIT_DELAY); //BIT_DELAY_1; - *device_port_OUT |= device_pin; + digitalWrite(device_pin, HIGH); delayMicroseconds(BIT_DELAY); //BIT_DELAY_1; - *device_port_OUT &= ~device_pin; + digitalWrite(device_pin, LOW); delayMicroseconds(BIT_DELAY); //BIT_DELAY_1; - *device_port_OUT |= device_pin; - delayMicroseconds(5*BIT_DELAY); //BIT_DELAY_5; + digitalWrite(device_pin, HIGH); + delayMicroseconds(5*BIT_DELAY); //BIT_DELAY_5; } } } @@ -149,8 +141,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; - + pinMode(device_pin,INPUT); + // Receive bits and store in buffer. for (i = 0; i < count; i++) { @@ -167,7 +159,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 (digitalRead(device_pin) == 0) break; } @@ -180,7 +172,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 (digitalRead(device_pin) != 0) { // For an Atmel microcontroller this might be faster than "pulse_count++". pulse_count = 1; @@ -203,7 +195,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 (digitalRead(device_pin) == 0) { // For an Atmel microcontroller this might be faster than "pulse_count++". pulse_count = 2; @@ -217,7 +209,7 @@ uint8_t atsha204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer) { do { - if ((*device_port_IN & device_pin) != 0) + if (digitalRead(device_pin) != 0) break; } while (timeout_count-- > 0); } diff --git a/libraries/sha204/sha204_library.h b/libraries/sha204/sha204_library.h index 3e2c9f0..762c85b 100644 --- a/libraries/sha204/sha204_library.h +++ b/libraries/sha204/sha204_library.h @@ -284,7 +284,6 @@ class atsha204Class { private: uint8_t device_pin; - 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);