converted (fat) sha204 to use standard arduino cmds for pin handling

This commit is contained in:
Thomas Mørch
2015-12-08 21:12:57 +01:00
parent 3edde2af17
commit 396a5fe5b0
2 changed files with 19 additions and 28 deletions
+19 -27
View File
@@ -9,15 +9,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);
} }
/* Puts a the ATSHA204's unique, 4-byte serial number in the response array /* 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) 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)
@@ -97,8 +89,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;
@@ -109,22 +101,22 @@ 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);
delayMicroseconds(BIT_DELAY); //BIT_DELAY_1; delayMicroseconds(BIT_DELAY); //BIT_DELAY_1;
*device_port_OUT |= device_pin; digitalWrite(device_pin, HIGH);
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);
delayMicroseconds(BIT_DELAY); //BIT_DELAY_1; delayMicroseconds(BIT_DELAY); //BIT_DELAY_1;
*device_port_OUT |= device_pin; digitalWrite(device_pin, HIGH);
delayMicroseconds(BIT_DELAY); //BIT_DELAY_1; delayMicroseconds(BIT_DELAY); //BIT_DELAY_1;
*device_port_OUT &= ~device_pin; digitalWrite(device_pin, LOW);
delayMicroseconds(BIT_DELAY); //BIT_DELAY_1; delayMicroseconds(BIT_DELAY); //BIT_DELAY_1;
*device_port_OUT |= device_pin; digitalWrite(device_pin, HIGH);
delayMicroseconds(5*BIT_DELAY); //BIT_DELAY_5; 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(); 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++)
{ {
@@ -167,7 +159,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;
} }
@@ -180,7 +172,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;
@@ -203,7 +195,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;
@@ -217,7 +209,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);
} }
-1
View File
@@ -284,7 +284,6 @@ class atsha204Class
{ {
private: private:
uint8_t device_pin; 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); 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);