Permalink
Browse files

Constructor now takes Arduino pin as parameter

Now you can connect the ATSHA204's SDA pin to any of the Arduino's
digital pins. The constructor now requires a parameter passed, which
should be whatever pin you want to connect the SDA pin to.
  • Loading branch information...
1 parent 4d00a79 commit 8e1ac05ef987b1869605ad067db2bdbaec1c109f Jim Lindblom committed Nov 9, 2012
@@ -7,12 +7,17 @@
connected and operational. And how to obtain an SHA204's unique serial
number, and send it a MAC challenge.
- As of the most recent version of th SHA204 library, the ATSHA204's SDA pin must
- be connected to D9. It can be powered between 3.3V and 5V.
+ The ATSHA204's SDA pin can be connected to any of the Arduino's digital pins.
+ When constructing your atsha204Class, pass the constructor the pin you want to use.
+ In this example we'll attach SDA to pin 7.
+
+ The ATSHA204 can be powered between 3.3V and 5V.
*/
#include <sha204_library.h>
-atsha204Class sha204;
+const int sha204Pin = 7;
+
+atsha204Class sha204(sha204Pin);
void setup()
{
@@ -2,9 +2,22 @@
#include "sha204_library.h"
#include "sha204_includes/sha204_lib_return_codes.h"
-atsha204Class::atsha204Class()
-{
- device_pin = 2;
+
+// 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
+// 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);
}
/* Puts a the ATSHA204's unique, 4-byte serial number in the response array
@@ -41,12 +54,12 @@ uint8_t atsha204Class::getSerialNumber(uint8_t * response)
void atsha204Class::swi_set_signal_pin(uint8_t is_high)
{
- PORT_DDR |= device_pin;
+ *device_port_DDR |= device_pin;
if (is_high)
- PORT_OUT |= device_pin;
+ *device_port_OUT |= device_pin;
else
- PORT_OUT &= ~device_pin;
+ *device_port_OUT &= ~device_pin;
}
uint8_t atsha204Class::swi_send_bytes(uint8_t count, uint8_t *buffer)
@@ -57,8 +70,8 @@ uint8_t atsha204Class::swi_send_bytes(uint8_t count, uint8_t *buffer)
noInterrupts(); //swi_disable_interrupts();
// Set signal pin as output.
- PORT_OUT |= device_pin;
- PORT_DDR |= device_pin;
+ *device_port_OUT |= device_pin;
+ *device_port_DDR |= device_pin;
// Wait turn around time.
delayMicroseconds(RX_TX_DELAY); //RX_TX_DELAY;
@@ -69,21 +82,21 @@ uint8_t atsha204Class::swi_send_bytes(uint8_t count, uint8_t *buffer)
{
if (bit_mask & buffer[i])
{
- PORT_OUT &= ~device_pin;
+ *device_port_OUT &= ~device_pin;
delayMicroseconds(BIT_DELAY); //BIT_DELAY_1;
- PORT_OUT |= device_pin;
+ *device_port_OUT |= device_pin;
delayMicroseconds(7*BIT_DELAY); //BIT_DELAY_7;
}
else
{
// Send a zero bit.
- PORT_OUT &= ~device_pin;
+ *device_port_OUT &= ~device_pin;
delayMicroseconds(BIT_DELAY); //BIT_DELAY_1;
- PORT_OUT |= device_pin;
+ *device_port_OUT |= device_pin;
delayMicroseconds(BIT_DELAY); //BIT_DELAY_1;
- PORT_OUT &= ~device_pin;
+ *device_port_OUT &= ~device_pin;
delayMicroseconds(BIT_DELAY); //BIT_DELAY_1;
- PORT_OUT |= device_pin;
+ *device_port_OUT |= device_pin;
delayMicroseconds(5*BIT_DELAY); //BIT_DELAY_5;
}
}
@@ -109,7 +122,7 @@ uint8_t atsha204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer)
noInterrupts(); //swi_disable_interrupts();
// Configure signal pin as input.
- PORT_DDR &= ~device_pin;
+ *device_port_DDR &= ~device_pin;
// Receive bits and store in buffer.
for (i = 0; i < count; i++)
@@ -127,7 +140,7 @@ uint8_t atsha204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer)
while (--timeout_count > 0)
{
// Wait for falling edge.
- if ((PORT_IN & device_pin) == 0)
+ if ((*device_port_IN & device_pin) == 0)
break;
}
@@ -140,7 +153,7 @@ uint8_t atsha204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer)
do
{
// Wait for rising edge.
- if ((PORT_IN & device_pin) != 0)
+ if ((*device_port_IN & device_pin) != 0)
{
// For an Atmel microcontroller this might be faster than "pulse_count++".
pulse_count = 1;
@@ -163,7 +176,7 @@ uint8_t atsha204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer)
// Detect possible edge indicating zero bit.
do
{
- if ((PORT_IN & device_pin) == 0)
+ if ((*device_port_IN & device_pin) == 0)
{
// For an Atmel microcontroller this might be faster than "pulse_count++".
pulse_count = 2;
@@ -177,7 +190,7 @@ uint8_t atsha204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer)
{
do
{
- if ((PORT_IN & device_pin) != 0)
+ if ((*device_port_IN & device_pin) != 0)
break;
} while (timeout_count-- > 0);
}
@@ -5,11 +5,6 @@
/* bitbang_config.h */
-#define PORT_DDR (DDRB) // B1 (D9)
-#define PORT_OUT (PORTB)
-#define PORT_IN (PINB)
-//uint8_t device_pin = 2;
-
#define PORT_ACCESS_TIME (630) //! time it takes to toggle the pin at CPU clock of 16 MHz (ns)
#define START_PULSE_WIDTH (4340) //! width of start pulse (ns)
#define BIT_DELAY (4) //! delay macro for width of one pulse (start pulse or zero pulse, in ns)
@@ -289,6 +284,7 @@ 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);
@@ -303,7 +299,7 @@ class atsha204Class
public:
- atsha204Class(); // Constructor
+ atsha204Class(uint8_t pin); // Constructor
uint8_t sha204c_wakeup(uint8_t *response);
uint8_t sha204c_send_and_receive(uint8_t *tx_buffer, uint8_t rx_size, uint8_t *rx_buffer, uint8_t execution_delay, uint8_t execution_timeout);
uint8_t sha204c_resync(uint8_t size, uint8_t *response);

0 comments on commit 8e1ac05

Please sign in to comment.