@@ -15,7 +15,7 @@ static uint8_t swi_send_bytes(uint8_t count, uint8_t *buffer);
15
15
static uint8_t swi_send_byte (uint8_t value);
16
16
static uint8_t sha204p_receive_response (uint8_t size, uint8_t *response);
17
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_resync (uint8_t size, uint8_t *response);
18
+ static uint8_t sha204c_resync (uint8_t size, uint8_t *response);
19
19
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);
20
20
21
21
/* SWI bit bang functions */
@@ -40,23 +40,23 @@ static uint8_t swi_send_bytes(uint8_t count, uint8_t *buffer)
40
40
// Set signal pin as output.
41
41
SHA204_POUT_HIGH ();
42
42
SHA204_SET_OUTPUT ();
43
-
43
+
44
44
45
45
// Wait turn around time.
46
46
delayMicroseconds (RX_TX_DELAY); // RX_TX_DELAY;
47
47
48
- for (i = 0 ; i < count; i++)
48
+ for (i = 0 ; i < count; i++)
49
49
{
50
- for (bit_mask = 1 ; bit_mask > 0 ; bit_mask <<= 1 )
50
+ for (bit_mask = 1 ; bit_mask > 0 ; bit_mask <<= 1 )
51
51
{
52
- if (bit_mask & buffer[i])
52
+ if (bit_mask & buffer[i])
53
53
{
54
54
SHA204_POUT_LOW (); // *device_port_OUT &= ~device_pin;
55
55
delayMicroseconds (BIT_DELAY); // BIT_DELAY_1;
56
56
SHA204_POUT_HIGH (); // *device_port_OUT |= device_pin;
57
57
delayMicroseconds (7 *BIT_DELAY); // BIT_DELAY_7;
58
58
}
59
- else
59
+ else
60
60
{
61
61
// Send a zero bit.
62
62
SHA204_POUT_LOW (); // *device_port_OUT &= ~device_pin;
@@ -92,11 +92,11 @@ static uint8_t swi_receive_bytes(uint8_t count, uint8_t *buffer)
92
92
93
93
// Configure signal pin as input.
94
94
SHA204_SET_INPUT ();
95
-
95
+
96
96
// Receive bits and store in buffer.
97
97
for (i = 0 ; i < count; i++)
98
98
{
99
- for (bit_mask = 1 ; bit_mask > 0 ; bit_mask <<= 1 )
99
+ for (bit_mask = 1 ; bit_mask > 0 ; bit_mask <<= 1 )
100
100
{
101
101
pulse_count = 0 ;
102
102
@@ -106,31 +106,31 @@ static uint8_t swi_receive_bytes(uint8_t count, uint8_t *buffer)
106
106
timeout_count = START_PULSE_TIME_OUT;
107
107
108
108
// Detect start bit.
109
- while (--timeout_count > 0 )
109
+ while (--timeout_count > 0 )
110
110
{
111
111
// Wait for falling edge.
112
112
if (SHA204_PIN_READ () == 0 )
113
113
break ;
114
114
}
115
115
116
- if (timeout_count == 0 )
116
+ if (timeout_count == 0 )
117
117
{
118
118
status = SWI_FUNCTION_RETCODE_TIMEOUT;
119
119
break ;
120
120
}
121
121
122
- do
122
+ do
123
123
{
124
124
// Wait for rising edge.
125
- if (SHA204_PIN_READ () != 0 )
125
+ if (SHA204_PIN_READ () != 0 )
126
126
{
127
127
// For an Atmel microcontroller this might be faster than "pulse_count++".
128
128
pulse_count = 1 ;
129
129
break ;
130
130
}
131
131
} while (--timeout_count > 0 );
132
132
133
- if (pulse_count == 0 )
133
+ if (pulse_count == 0 )
134
134
{
135
135
status = SWI_FUNCTION_RETCODE_TIMEOUT;
136
136
break ;
@@ -143,9 +143,9 @@ static uint8_t swi_receive_bytes(uint8_t count, uint8_t *buffer)
143
143
timeout_count = ZERO_PULSE_TIME_OUT;
144
144
145
145
// Detect possible edge indicating zero bit.
146
- do
146
+ do
147
147
{
148
- if (SHA204_PIN_READ () == 0 )
148
+ if (SHA204_PIN_READ () == 0 )
149
149
{
150
150
// For an Atmel microcontroller this might be faster than "pulse_count++".
151
151
pulse_count = 2 ;
@@ -155,9 +155,9 @@ static uint8_t swi_receive_bytes(uint8_t count, uint8_t *buffer)
155
155
156
156
// Wait for rising edge of zero pulse before returning. Otherwise we might interpret
157
157
// its rising edge as the next start pulse.
158
- if (pulse_count == 2 )
158
+ if (pulse_count == 2 )
159
159
{
160
- do
160
+ do
161
161
{
162
162
if (SHA204_PIN_READ () != 0 )
163
163
break ;
@@ -174,7 +174,7 @@ static uint8_t swi_receive_bytes(uint8_t count, uint8_t *buffer)
174
174
}
175
175
interrupts (); // swi_enable_interrupts();
176
176
177
- if (status == SWI_FUNCTION_RETCODE_TIMEOUT)
177
+ if (status == SWI_FUNCTION_RETCODE_TIMEOUT)
178
178
{
179
179
if (i > 0 )
180
180
// Indicate that we timed out after having received at least one byte.
@@ -197,7 +197,7 @@ static uint8_t sha204p_receive_response(uint8_t size, uint8_t *response)
197
197
(void ) swi_send_byte (SHA204_SWI_FLAG_TX);
198
198
199
199
ret_code = swi_receive_bytes (size, response);
200
- if (ret_code == SWI_FUNCTION_RETCODE_SUCCESS || ret_code == SWI_FUNCTION_RETCODE_RX_FAIL)
200
+ if (ret_code == SWI_FUNCTION_RETCODE_SUCCESS || ret_code == SWI_FUNCTION_RETCODE_RX_FAIL)
201
201
{
202
202
count_byte = response[SHA204_BUFFER_POS_COUNT];
203
203
if ((count_byte < SHA204_RSP_SIZE_MIN) || (count_byte > size))
@@ -257,7 +257,7 @@ static uint8_t sha204c_send_and_receive(uint8_t *tx_buffer, uint8_t rx_size, uin
257
257
// Retry loop for sending a command and receiving a response.
258
258
n_retries_send = SHA204_RETRY_COUNT + 1 ;
259
259
260
- while ((n_retries_send-- > 0 ) && (ret_code != SHA204_SUCCESS))
260
+ while ((n_retries_send-- > 0 ) && (ret_code != SHA204_SUCCESS))
261
261
{
262
262
// Send command.
263
263
ret_code = swi_send_byte (SHA204_SWI_FLAG_CMD);
@@ -266,7 +266,7 @@ static uint8_t sha204c_send_and_receive(uint8_t *tx_buffer, uint8_t rx_size, uin
266
266
else
267
267
ret_code = swi_send_bytes (count, tx_buffer);
268
268
269
- if (ret_code != SHA204_SUCCESS)
269
+ if (ret_code != SHA204_SUCCESS)
270
270
{
271
271
if (sha204c_resync (rx_size, rx_buffer) == SHA204_RX_NO_RESPONSE)
272
272
return ret_code; // The device seems to be dead in the water.
@@ -279,22 +279,22 @@ static uint8_t sha204c_send_and_receive(uint8_t *tx_buffer, uint8_t rx_size, uin
279
279
280
280
// Retry loop for receiving a response.
281
281
n_retries_receive = SHA204_RETRY_COUNT + 1 ;
282
- while (n_retries_receive-- > 0 )
282
+ while (n_retries_receive-- > 0 )
283
283
{
284
284
// Reset response buffer.
285
285
for (i = 0 ; i < rx_size; i++)
286
286
rx_buffer[i] = 0 ;
287
287
288
288
// Poll for response.
289
289
timeout_countdown = execution_timeout_us;
290
- do
290
+ do
291
291
{
292
292
ret_code = sha204p_receive_response (rx_size, rx_buffer);
293
293
timeout_countdown -= SHA204_RESPONSE_TIMEOUT;
294
- }
294
+ }
295
295
while ((timeout_countdown > SHA204_RESPONSE_TIMEOUT) && (ret_code == SHA204_RX_NO_RESPONSE));
296
296
297
- if (ret_code == SHA204_RX_NO_RESPONSE)
297
+ if (ret_code == SHA204_RX_NO_RESPONSE)
298
298
{
299
299
// We did not receive a response. Re-synchronize and send command again.
300
300
if (sha204c_resync (rx_size, rx_buffer) == SHA204_RX_NO_RESPONSE)
@@ -324,7 +324,7 @@ static uint8_t sha204c_send_and_receive(uint8_t *tx_buffer, uint8_t rx_size, uin
324
324
// We received a response of valid size.
325
325
// Check the consistency of the response.
326
326
ret_code = sha204c_check_crc (rx_buffer);
327
- if (ret_code == SHA204_SUCCESS)
327
+ if (ret_code == SHA204_SUCCESS)
328
328
{
329
329
// Received valid response.
330
330
if (rx_buffer[SHA204_BUFFER_POS_COUNT] > SHA204_RSP_SIZE_MIN)
@@ -340,7 +340,7 @@ static uint8_t sha204c_send_and_receive(uint8_t *tx_buffer, uint8_t rx_size, uin
340
340
return SHA204_PARSE_ERROR;
341
341
if (status_byte == SHA204_STATUS_BYTE_EXEC)
342
342
return SHA204_CMD_FAIL;
343
- if (status_byte == SHA204_STATUS_BYTE_COMM)
343
+ if (status_byte == SHA204_STATUS_BYTE_COMM)
344
344
{
345
345
// In case of the device status byte indicating a communication
346
346
// error this function exits the retry loop for receiving a response
@@ -355,7 +355,7 @@ static uint8_t sha204c_send_and_receive(uint8_t *tx_buffer, uint8_t rx_size, uin
355
355
return ret_code;
356
356
}
357
357
358
- else
358
+ else
359
359
{
360
360
// Received response with incorrect CRC.
361
361
ret_code_resync = sha204c_resync (rx_size, rx_buffer);
@@ -410,7 +410,7 @@ static void sha204c_calculate_crc(uint8_t length, uint8_t *data, uint8_t *crc)
410
410
411
411
for (counter = 0 ; counter < length; counter++)
412
412
{
413
- for (shift_register = 0x01 ; shift_register > 0x00 ; shift_register <<= 1 )
413
+ for (shift_register = 0x01 ; shift_register > 0x00 ; shift_register <<= 1 )
414
414
{
415
415
data_bit = (data[counter] & shift_register) ? 1 : 0 ;
416
416
crc_bit = crc_register >> 15 ;
@@ -441,11 +441,11 @@ static uint8_t sha204c_check_crc(uint8_t *response)
441
441
/* Public functions */
442
442
443
443
void atsha204_init (uint8_t pin)
444
- {
444
+ {
445
445
#if defined(ARDUINO_ARCH_AVR)
446
446
device_pin = digitalPinToBitMask (pin); // Find the bit value of the pin
447
447
uint8_t port = digitalPinToPort (pin); // temoporarily used to get the next three registers
448
-
448
+
449
449
// Point to data direction register port of pin
450
450
device_port_DDR = portModeRegister (port);
451
451
// Point to output register of pin
@@ -483,7 +483,7 @@ uint8_t atsha204_wakeup(uint8_t *response)
483
483
ret_code = SHA204_INVALID_SIZE;
484
484
else if (response[SHA204_BUFFER_POS_STATUS] != SHA204_STATUS_BYTE_WAKEUP)
485
485
ret_code = SHA204_COMM_FAIL;
486
- else
486
+ else
487
487
{
488
488
if ((response[SHA204_RSP_SIZE_MIN - SHA204_CRC_SIZE] != 0x33 )
489
489
|| (response[SHA204_RSP_SIZE_MIN + 1 - SHA204_CRC_SIZE] != 0x43 ))
@@ -502,9 +502,9 @@ uint8_t atsha204_execute(uint8_t op_code, uint8_t param1, uint16_t param2,
502
502
uint8_t *p_buffer;
503
503
uint8_t len;
504
504
(void )tx_size;
505
-
505
+
506
506
// Supply delays and response size.
507
- switch (op_code)
507
+ switch (op_code)
508
508
{
509
509
case SHA204_GENDIG:
510
510
poll_delay = GENDIG_DELAY;
@@ -571,7 +571,7 @@ uint8_t atsha204_execute(uint8_t op_code, uint8_t param1, uint16_t param2,
571
571
&rx_buffer[0 ], poll_delay, poll_timeout);
572
572
}
573
573
574
- void atsha204_getSerialNumber (uint8_t * response)
574
+ uint8_t atsha204_getSerialNumber (uint8_t * response)
575
575
{
576
576
uint8_t readCommand[READ_COUNT];
577
577
uint8_t readResponse[READ_4_RSP_SIZE];
@@ -596,5 +596,5 @@ void atsha204_getSerialNumber(uint8_t * response)
596
596
}
597
597
}
598
598
599
- return ;
599
+ return returnCode ;
600
600
}
0 commit comments