1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
* \file
* \brief Hardware Interface Functions - SWI bit-banged
*
* \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries.
*
* \page License
*
* Subject to your compliance with these terms, you may use Microchip software
* and any derivatives exclusively with Microchip products. It is your
* responsibility to comply with third party license terms applicable to your
* use of third party software (including open source software) that may
* accompany Microchip software.
*
* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
* PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT,
* SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE
* OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF
* MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE
* FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL
* LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED
* THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR
* THIS SOFTWARE.
*/
#include <asf.h>
#include <stdint.h>
#include "swi_bitbang_samd21.h"
#include "atca_command.h"
SWIBuses swi_buses_default = {
{ EXT3_PIN_3, EXT3_PIN_9, EXT3_PIN_I2C_SDA, EXT3_PIN_13, EXT2_PIN_13, EXT2_PIN_5, EXT2_PIN_7, EXT2_PIN_9, EXT2_PIN_3, EXT2_PIN_15, EXT2_PIN_17, EXT1_PIN_3, EXT1_PIN_5, EXT1_PIN_7, EXT1_PIN_9, EXT1_PIN_13, EXT1_PIN_15, EXT1_PIN_17, EXT3_PIN_7, EXT3_PIN_10, EXT3_PIN_I2C_SCL, EXT3_PIN_14, EXT2_PIN_4, EXT2_PIN_6, EXT2_PIN_8, EXT2_PIN_10, EXT2_PIN_14, EXT2_PIN_16, EXT2_PIN_18, EXT1_PIN_4, EXT1_PIN_6, EXT1_PIN_8, EXT1_PIN_10, EXT1_PIN_14, EXT1_PIN_16, EXT1_PIN_18 }
};
//! declaration of the variable indicating which pin the selected device is connected to
static uint8_t device_pin;
void swi_set_pin(uint8_t id)
{
device_pin = id;
}
void swi_enable(void)
{
struct port_config pin_conf;
port_get_config_defaults(&pin_conf);
pin_conf.direction = PORT_PIN_DIR_OUTPUT;
port_pin_set_config(device_pin, &pin_conf);
}
void swi_disable(void)
{
struct port_config pin_conf;
port_get_config_defaults(&pin_conf);
port_pin_set_config(device_pin, &pin_conf);
}
void swi_set_signal_pin(uint8_t is_high)
{
if (is_high)
{
port_pin_set_output_level(device_pin, true);
}
else
{
port_pin_set_output_level(device_pin, false);
}
}
void swi_send_wake_token(void)
{
swi_set_signal_pin(0);
delay_us(60);
swi_set_signal_pin(1);
}
void swi_send_bytes(uint8_t count, uint8_t *buffer)
{
uint8_t i, bit_mask;
struct port_config pin_conf;
port_get_config_defaults(&pin_conf);
pin_conf.direction = PORT_PIN_DIR_OUTPUT;
port_pin_set_config(device_pin, &pin_conf);
cpu_irq_disable();
for (i = 0; i < count; i++)
{
for (bit_mask = 1; bit_mask > 0; bit_mask <<= 1)
{
if (bit_mask & buffer[i]) //!< Send Logic 1 (7F)
{
port_pin_set_output_level(device_pin, false);
BIT_DELAY_1L;
port_pin_set_output_level(device_pin, true);
BIT_DELAY_7;
}
else //!< Send Logic 0 (7D)
{
port_pin_set_output_level(device_pin, false);
BIT_DELAY_1L;
port_pin_set_output_level(device_pin, true);
BIT_DELAY_1H;
port_pin_set_output_level(device_pin, false);
BIT_DELAY_1L;
port_pin_set_output_level(device_pin, true);
BIT_DELAY_5;
}
}
}
cpu_irq_enable();
}
void swi_send_byte(uint8_t byte)
{
swi_send_bytes(1, &byte);
}
ATCA_STATUS swi_receive_bytes(uint8_t count, uint8_t *buffer)
{
ATCA_STATUS status = ATCA_SUCCESS;
uint8_t i;
uint8_t bit_mask;
uint8_t pulse_count;
uint16_t timeout_count;
struct port_config pin_conf;
port_get_config_defaults(&pin_conf);
port_pin_set_config(device_pin, &pin_conf);
cpu_irq_disable();
//! Receive bits and store in buffer.
for (i = 0; i < count; i++)
{
buffer[i] = 0;
for (bit_mask = 1; bit_mask > 0; bit_mask <<= 1)
{
pulse_count = 0;
timeout_count = START_PULSE_TIME_OUT;
//! Detect start bit.
while (--timeout_count > 0)
{
//! Wait for falling edge.
if (port_pin_get_input_level(device_pin) == 0)
{
break;
}
}
if (timeout_count == 0)
{
status = ATCA_RX_TIMEOUT;
break;
}
timeout_count = START_PULSE_TIME_OUT;
do
{
//! Wait for rising edge.
if (port_pin_get_input_level(device_pin) != 0)
{
pulse_count = 1;
break;
}
}
while (--timeout_count > 0);
if (pulse_count == 0)
{
status = ATCA_RX_TIMEOUT;
break;
}
//! let's just wait the maximum time for the falling edge of a zero bit
//! to arrive after we have detected the rising edge of the start bit.
timeout_count = ZERO_PULSE_TIME_OUT;
//! Detect possible edge indicating zero bit.
do
{
if (port_pin_get_input_level(device_pin) == 0)
{
pulse_count = 2;
break;
}
}
while (--timeout_count > 0);
//! Wait for rising edge of zero pulse before returning. Otherwise we might interpret
//! its rising edge as the next start pulse.
if (pulse_count == 2)
{
timeout_count = ZERO_PULSE_TIME_OUT;
do
{
if (port_pin_get_input_level(device_pin) != 0)
{
break;
}
}
while (timeout_count-- > 0);
}
//! Update byte at current buffer index.
else
//! received "one" bit
{
buffer[i] |= bit_mask;
}
}
if (status != ATCA_SUCCESS)
{
break;
}
if (i == 0)
{
if (buffer[0] < ATCA_RSP_SIZE_MIN)
{
status = ATCA_INVALID_SIZE;
break;
}
else if (buffer[0] > count)
{
status = ATCA_SMALL_BUFFER;
break;
}
else
{
count = buffer[0];
}
}
}
cpu_irq_enable();
RX_TX_DELAY; //forcing tTURNAROUND (To CryptoAuthentication)
if (status == ATCA_RX_TIMEOUT)
{
if (i > 0)
{
//! Indicate that we timed out after having received at least one byte.
status = ATCA_RX_FAIL;
}
}
return status;
}