|
| 1 | +/* ATSHA204 Library Simple Example |
| 2 | + by: Jim Lindblom |
| 3 | + SparkFun Electronics |
| 4 | + date: November 8, 2012 |
| 5 | + |
| 6 | + This code shows how to wake up and verify that an SHA204 is |
| 7 | + connected and operational. And how to obtain an SHA204's unique serial |
| 8 | + number, and send it a MAC challenge. |
| 9 | + |
| 10 | + The ATSHA204's SDA pin can be connected to any of the Arduino's digital pins. |
| 11 | + When constructing your atsha204Class, pass the constructor the pin you want to use. |
| 12 | + In this example we'll attach SDA to pin 7. |
| 13 | + |
| 14 | + The ATSHA204 can be powered between 3.3V and 5V. |
| 15 | +*/ |
| 16 | +#include <sha204_library.h> |
| 17 | + |
| 18 | +const int sha204Pin = 7; |
| 19 | + |
| 20 | +atsha204Class sha204(sha204Pin); |
| 21 | + |
| 22 | +void setup() |
| 23 | +{ |
| 24 | + Serial.begin(9600); |
| 25 | + Serial.println("Sending a Wakup Command. Response should be:\r\n4 11 33 43:"); |
| 26 | + Serial.println("Response is:"); |
| 27 | + wakeupExample(); |
| 28 | + Serial.println(); |
| 29 | + Serial.println("Asking the SHA204's serial number. Response should be:"); |
| 30 | + Serial.println("1 23 x x x x x x x EE"); |
| 31 | + Serial.println("Response is:"); |
| 32 | + serialNumberExample(); |
| 33 | + Serial.println(); |
| 34 | + Serial.println("Sending a MAC Challenge. Response should be:"); |
| 35 | + Serial.println("23 6 67 0 4F 28 4D 6E 98 62 4 F4 60 A3 E8 75 8A 59 85 A6 79 96 C4 8A 88 46 43 4E B3 DB 58 A4 FB E5 73"); |
| 36 | + Serial.println("Response is:"); |
| 37 | + macChallengeExample(); |
| 38 | +} |
| 39 | + |
| 40 | +void loop() |
| 41 | +{ |
| 42 | +} |
| 43 | + |
| 44 | +byte wakeupExample() |
| 45 | +{ |
| 46 | + uint8_t response[SHA204_RSP_SIZE_MIN]; |
| 47 | + byte returnValue; |
| 48 | + |
| 49 | + returnValue = sha204.sha204c_wakeup(&response[0]); |
| 50 | + for (int i=0; i<SHA204_RSP_SIZE_MIN; i++) |
| 51 | + { |
| 52 | + Serial.print(response[i], HEX); |
| 53 | + Serial.print(" "); |
| 54 | + } |
| 55 | + Serial.println(); |
| 56 | + |
| 57 | + return returnValue; |
| 58 | +} |
| 59 | + |
| 60 | +byte serialNumberExample() |
| 61 | +{ |
| 62 | + uint8_t serialNumber[9]; |
| 63 | + byte returnValue; |
| 64 | + |
| 65 | + returnValue = sha204.getSerialNumber(serialNumber); |
| 66 | + for (int i=0; i<9; i++) |
| 67 | + { |
| 68 | + Serial.print(serialNumber[i], HEX); |
| 69 | + Serial.print(" "); |
| 70 | + } |
| 71 | + Serial.println(); |
| 72 | + |
| 73 | + return returnValue; |
| 74 | +} |
| 75 | + |
| 76 | +byte macChallengeExample() |
| 77 | +{ |
| 78 | + uint8_t command[MAC_COUNT_LONG]; |
| 79 | + uint8_t response[MAC_RSP_SIZE]; |
| 80 | + |
| 81 | + const uint8_t challenge[MAC_CHALLENGE_SIZE] = { |
| 82 | + 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, |
| 83 | + 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, |
| 84 | + 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, |
| 85 | + 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF |
| 86 | + }; |
| 87 | + |
| 88 | + uint8_t ret_code = sha204.sha204m_execute(SHA204_MAC, 0, 0, MAC_CHALLENGE_SIZE, |
| 89 | + (uint8_t *) challenge, 0, NULL, 0, NULL, sizeof(command), &command[0], |
| 90 | + sizeof(response), &response[0]); |
| 91 | + |
| 92 | + for (int i=0; i<SHA204_RSP_SIZE_MAX; i++) |
| 93 | + { |
| 94 | + Serial.print(response[i], HEX); |
| 95 | + Serial.print(' '); |
| 96 | + } |
| 97 | + Serial.println(); |
| 98 | + |
| 99 | + return ret_code; |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | + |
0 commit comments