converted (fat) sha204 to use standard arduino cmds for pin handling
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user