1
1
#include " Arduino.h"
2
2
#include " ATSHA204.h"
3
3
4
- // atsha204Class Constructor
5
- // Feed this function the Arduino-ized pin number you want to assign to the ATSHA204's SDA pin
6
- // This will find the DDRX, PORTX, and PINX registrs it'll need to point to to control that pin
7
- // As well as the bit value for each of those registers
8
- ATSHA204Class::ATSHA204Class (uint8_t pin)
9
- {
10
- #if defined(ARDUINO_ARCH_AVR)
11
- device_pin = digitalPinToBitMask (pin); // Find the bit value of the pin
12
- uint8_t port = digitalPinToPort (pin); // temoporarily used to get the next three registers
13
-
14
- // Point to data direction register port of pin
15
- device_port_DDR = portModeRegister (port);
16
- // Point to output register of pin
17
- device_port_OUT = portOutputRegister (port);
18
- // Point to input register of pin
19
- device_port_IN = portInputRegister (port);
20
- #else
21
- device_pin = pin;
22
- #endif
23
- }
24
-
25
- void ATSHA204Class::getSerialNumber (uint8_t * response)
26
- {
27
- uint8_t readCommand[READ_COUNT];
28
- uint8_t readResponse[READ_4_RSP_SIZE];
4
+ /* Local data and function prototypes */
29
5
30
- /* read from bytes 0->3 of config zone */
31
- uint8_t returnCode = sha204m_read (readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN03);
32
- if (!returnCode)
33
- {
34
- for (int i=0 ; i<4 ; i++) // store bytes 0-3 into respones array
35
- response[i] = readResponse[SHA204_BUFFER_POS_DATA+i];
36
-
37
- /* read from bytes 8->11 of config zone */
38
- returnCode = sha204m_read (readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN47);
39
-
40
- for (int i=4 ; i<8 ; i++) // store bytes 4-7 of SN into response array
41
- response[i] = readResponse[SHA204_BUFFER_POS_DATA+(i-4 )];
42
-
43
- if (!returnCode)
44
- { /* Finally if last two reads were successful, read byte 8 of the SN */
45
- returnCode = sha204m_read (readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN8);
46
- response[8 ] = readResponse[SHA204_BUFFER_POS_DATA]; // Byte 8 of SN should always be 0xEE
47
- }
48
- }
49
-
50
- return ;
51
- }
6
+ static uint8_t device_pin;
7
+ #ifdef ARDUINO_ARCH_AVR
8
+ static volatile uint8_t *device_port_DDR, *device_port_OUT, *device_port_IN;
9
+ #endif
10
+ static void sha204c_calculate_crc (uint8_t length, uint8_t *data, uint8_t *crc);
11
+ static uint8_t sha204c_check_crc (uint8_t *response);
12
+ static void swi_set_signal_pin (uint8_t is_high);
13
+ static uint8_t swi_receive_bytes (uint8_t count, uint8_t *buffer);
14
+ static uint8_t swi_send_bytes (uint8_t count, uint8_t *buffer);
15
+ static uint8_t swi_send_byte (uint8_t value);
16
+ static uint8_t sha204p_receive_response (uint8_t size, uint8_t *response);
17
+ static uint8_t sha204m_read (uint8_t *tx_buffer, uint8_t *rx_buffer, uint8_t zone, uint16_t address);
18
+ static uint8_t sha204c_wakeup (uint8_t *response);
19
+ static uint8_t sha204c_resync (uint8_t size, uint8_t *response);
20
+ static 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);
52
21
53
22
/* SWI bit bang functions */
54
23
55
- void ATSHA204Class:: swi_set_signal_pin (uint8_t is_high)
24
+ static void swi_set_signal_pin (uint8_t is_high)
56
25
{
57
26
SHA204_SET_OUTPUT ();
58
27
@@ -62,7 +31,7 @@ void ATSHA204Class::swi_set_signal_pin(uint8_t is_high)
62
31
SHA204_POUT_LOW ();
63
32
}
64
33
65
- uint8_t ATSHA204Class:: swi_send_bytes (uint8_t count, uint8_t *buffer)
34
+ static uint8_t swi_send_bytes (uint8_t count, uint8_t *buffer)
66
35
{
67
36
uint8_t i, bit_mask;
68
37
@@ -106,12 +75,12 @@ uint8_t ATSHA204Class::swi_send_bytes(uint8_t count, uint8_t *buffer)
106
75
return SWI_FUNCTION_RETCODE_SUCCESS;
107
76
}
108
77
109
- uint8_t ATSHA204Class:: swi_send_byte (uint8_t value)
78
+ static uint8_t swi_send_byte (uint8_t value)
110
79
{
111
80
return swi_send_bytes (1 , &value);
112
81
}
113
82
114
- uint8_t ATSHA204Class:: swi_receive_bytes (uint8_t count, uint8_t *buffer)
83
+ static uint8_t swi_receive_bytes (uint8_t count, uint8_t *buffer)
115
84
{
116
85
uint8_t status = SWI_FUNCTION_RETCODE_SUCCESS;
117
86
uint8_t i;
@@ -217,12 +186,7 @@ uint8_t ATSHA204Class::swi_receive_bytes(uint8_t count, uint8_t *buffer)
217
186
218
187
/* Physical functions */
219
188
220
- void ATSHA204Class::sha204c_sleep ()
221
- {
222
- swi_send_byte (SHA204_SWI_FLAG_SLEEP);
223
- }
224
-
225
- uint8_t ATSHA204Class::sha204p_receive_response (uint8_t size, uint8_t *response)
189
+ static uint8_t sha204p_receive_response (uint8_t size, uint8_t *response)
226
190
{
227
191
uint8_t count_byte;
228
192
uint8_t i;
@@ -254,7 +218,7 @@ uint8_t ATSHA204Class::sha204p_receive_response(uint8_t size, uint8_t *response)
254
218
255
219
/* Communication functions */
256
220
257
- uint8_t ATSHA204Class:: sha204c_wakeup (uint8_t *response)
221
+ static uint8_t sha204c_wakeup (uint8_t *response)
258
222
{
259
223
swi_set_signal_pin (0 );
260
224
delayMicroseconds (10 *SHA204_WAKEUP_PULSE_WIDTH);
@@ -282,7 +246,7 @@ uint8_t ATSHA204Class::sha204c_wakeup(uint8_t *response)
282
246
return ret_code;
283
247
}
284
248
285
- uint8_t ATSHA204Class:: sha204c_resync (uint8_t size, uint8_t *response)
249
+ static uint8_t sha204c_resync (uint8_t size, uint8_t *response)
286
250
{
287
251
// Try to re-synchronize without sending a Wake token
288
252
// (step 1 of the re-synchronization process).
@@ -294,7 +258,7 @@ uint8_t ATSHA204Class::sha204c_resync(uint8_t size, uint8_t *response)
294
258
// We lost communication. Send a Wake pulse and try
295
259
// to receive a response (steps 2 and 3 of the
296
260
// re-synchronization process).
297
- sha204c_sleep ();
261
+ atsha204_sleep ();
298
262
ret_code = sha204c_wakeup (response);
299
263
300
264
// Translate a return value of success into one
@@ -303,7 +267,7 @@ uint8_t ATSHA204Class::sha204c_resync(uint8_t size, uint8_t *response)
303
267
return (ret_code == SHA204_SUCCESS ? SHA204_RESYNC_WITH_WAKEUP : ret_code);
304
268
}
305
269
306
- uint8_t ATSHA204Class:: sha204c_send_and_receive (uint8_t *tx_buffer, uint8_t rx_size, uint8_t *rx_buffer, uint8_t execution_delay, uint8_t execution_timeout)
270
+ static 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)
307
271
{
308
272
uint8_t ret_code = SHA204_FUNC_FAIL;
309
273
uint8_t ret_code_resync;
@@ -445,7 +409,8 @@ uint8_t ATSHA204Class::sha204c_send_and_receive(uint8_t *tx_buffer, uint8_t rx_s
445
409
446
410
447
411
/* Marshaling functions */
448
- uint8_t ATSHA204Class::sha204m_read (uint8_t *tx_buffer, uint8_t *rx_buffer, uint8_t zone, uint16_t address)
412
+
413
+ static uint8_t sha204m_read (uint8_t *tx_buffer, uint8_t *rx_buffer, uint8_t zone, uint16_t address)
449
414
{
450
415
uint8_t rx_size;
451
416
@@ -462,7 +427,71 @@ uint8_t ATSHA204Class::sha204m_read(uint8_t *tx_buffer, uint8_t *rx_buffer, uint
462
427
return sha204c_send_and_receive (&tx_buffer[0 ], rx_size, &rx_buffer[0 ], READ_DELAY, READ_EXEC_MAX - READ_DELAY);
463
428
}
464
429
465
- uint8_t ATSHA204Class::sha204m_execute (uint8_t op_code, uint8_t param1, uint16_t param2,
430
+ /* CRC Calculator and Checker */
431
+
432
+ static void sha204c_calculate_crc (uint8_t length, uint8_t *data, uint8_t *crc)
433
+ {
434
+ uint8_t counter;
435
+ uint16_t crc_register = 0 ;
436
+ uint16_t polynom = 0x8005 ;
437
+ uint8_t shift_register;
438
+ uint8_t data_bit, crc_bit;
439
+
440
+ for (counter = 0 ; counter < length; counter++)
441
+ {
442
+ for (shift_register = 0x01 ; shift_register > 0x00 ; shift_register <<= 1 )
443
+ {
444
+ data_bit = (data[counter] & shift_register) ? 1 : 0 ;
445
+ crc_bit = crc_register >> 15 ;
446
+
447
+ // Shift CRC to the left by 1.
448
+ crc_register <<= 1 ;
449
+
450
+ if ((data_bit ^ crc_bit) != 0 )
451
+ crc_register ^= polynom;
452
+ }
453
+ }
454
+ crc[0 ] = (uint8_t ) (crc_register & 0x00FF );
455
+ crc[1 ] = (uint8_t ) (crc_register >> 8 );
456
+ }
457
+
458
+ static uint8_t sha204c_check_crc (uint8_t *response)
459
+ {
460
+ uint8_t crc[SHA204_CRC_SIZE];
461
+ uint8_t count = response[SHA204_BUFFER_POS_COUNT];
462
+
463
+ count -= SHA204_CRC_SIZE;
464
+ sha204c_calculate_crc (count, response, crc);
465
+
466
+ return (crc[0 ] == response[count] && crc[1 ] == response[count + 1 ])
467
+ ? SHA204_SUCCESS : SHA204_BAD_CRC;
468
+ }
469
+
470
+ /* Public functions */
471
+
472
+ void atsha204_init (uint8_t pin)
473
+ {
474
+ #if defined(ARDUINO_ARCH_AVR)
475
+ device_pin = digitalPinToBitMask (pin); // Find the bit value of the pin
476
+ uint8_t port = digitalPinToPort (pin); // temoporarily used to get the next three registers
477
+
478
+ // Point to data direction register port of pin
479
+ device_port_DDR = portModeRegister (port);
480
+ // Point to output register of pin
481
+ device_port_OUT = portOutputRegister (port);
482
+ // Point to input register of pin
483
+ device_port_IN = portInputRegister (port);
484
+ #else
485
+ device_pin = pin;
486
+ #endif
487
+ }
488
+
489
+ void atsha204_sleep (void )
490
+ {
491
+ swi_send_byte (SHA204_SWI_FLAG_SLEEP);
492
+ }
493
+
494
+ uint8_t atsha204_execute (uint8_t op_code, uint8_t param1, uint16_t param2,
466
495
uint8_t datalen1, uint8_t *data1, uint8_t tx_size, uint8_t *tx_buffer, uint8_t rx_size, uint8_t *rx_buffer)
467
496
{
468
497
uint8_t poll_delay, poll_timeout, response_size;
@@ -538,43 +567,30 @@ uint8_t ATSHA204Class::sha204m_execute(uint8_t op_code, uint8_t param1, uint16_t
538
567
&rx_buffer[0 ], poll_delay, poll_timeout);
539
568
}
540
569
541
- /* CRC Calculator and Checker */
542
-
543
- void ATSHA204Class::sha204c_calculate_crc (uint8_t length, uint8_t *data, uint8_t *crc)
570
+ void atsha204_getSerialNumber (uint8_t * response)
544
571
{
545
- uint8_t counter;
546
- uint16_t crc_register = 0 ;
547
- uint16_t polynom = 0x8005 ;
548
- uint8_t shift_register;
549
- uint8_t data_bit, crc_bit;
572
+ uint8_t readCommand[READ_COUNT];
573
+ uint8_t readResponse[READ_4_RSP_SIZE];
550
574
551
- for (counter = 0 ; counter < length; counter++)
575
+ /* read from bytes 0->3 of config zone */
576
+ uint8_t returnCode = sha204m_read (readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN03);
577
+ if (!returnCode)
552
578
{
553
- for (shift_register = 0x01 ; shift_register > 0x00 ; shift_register <<= 1 )
554
- {
555
- data_bit = (data[counter] & shift_register) ? 1 : 0 ;
556
- crc_bit = crc_register >> 15 ;
579
+ for (int i=0 ; i<4 ; i++) // store bytes 0-3 into respones array
580
+ response[i] = readResponse[SHA204_BUFFER_POS_DATA+i];
557
581
558
- // Shift CRC to the left by 1.
559
- crc_register <<= 1 ;
582
+ /* read from bytes 8->11 of config zone */
583
+ returnCode = sha204m_read (readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN47) ;
560
584
561
- if ((data_bit ^ crc_bit) != 0 )
562
- crc_register ^= polynom;
585
+ for (int i=4 ; i<8 ; i++) // store bytes 4-7 of SN into response array
586
+ response[i] = readResponse[SHA204_BUFFER_POS_DATA+(i-4 )];
587
+
588
+ if (!returnCode)
589
+ { /* Finally if last two reads were successful, read byte 8 of the SN */
590
+ returnCode = sha204m_read (readCommand, readResponse, SHA204_ZONE_CONFIG, ADDRESS_SN8);
591
+ response[8 ] = readResponse[SHA204_BUFFER_POS_DATA]; // Byte 8 of SN should always be 0xEE
563
592
}
564
593
}
565
- crc[0 ] = (uint8_t ) (crc_register & 0x00FF );
566
- crc[1 ] = (uint8_t ) (crc_register >> 8 );
567
- }
568
-
569
- uint8_t ATSHA204Class::sha204c_check_crc (uint8_t *response)
570
- {
571
- uint8_t crc[SHA204_CRC_SIZE];
572
- uint8_t count = response[SHA204_BUFFER_POS_COUNT];
573
-
574
- count -= SHA204_CRC_SIZE;
575
- sha204c_calculate_crc (count, response, crc);
576
594
577
- return (crc[0 ] == response[count] && crc[1 ] == response[count + 1 ])
578
- ? SHA204_SUCCESS : SHA204_BAD_CRC;
595
+ return ;
579
596
}
580
-
0 commit comments