MySensors Library & Examples  2.3.2-62-ge298769
ATSHA204.h
1 #ifndef ATSHA204_H
2 #define ATSHA204_H
3 #if !DOXYGEN
4 #include <Arduino.h>
5 
6 /* This is a scaled down variant of the ATSHA204 library, tweaked to meet the specific needs of the MySensors library. */
7 
8 /* Library return codes */
9 #define SHA204_SUCCESS ((uint8_t) 0x00)
10 #define SHA204_PARSE_ERROR ((uint8_t) 0xD2)
11 #define SHA204_CMD_FAIL ((uint8_t) 0xD3)
12 #define SHA204_STATUS_CRC ((uint8_t) 0xD4)
13 #define SHA204_STATUS_UNKNOWN ((uint8_t) 0xD5)
14 #define SHA204_FUNC_FAIL ((uint8_t) 0xE0)
15 #define SHA204_GEN_FAIL ((uint8_t) 0xE1)
16 #define SHA204_BAD_PARAM ((uint8_t) 0xE2)
17 #define SHA204_INVALID_ID ((uint8_t) 0xE3)
18 #define SHA204_INVALID_SIZE ((uint8_t) 0xE4)
19 #define SHA204_BAD_CRC ((uint8_t) 0xE5)
20 #define SHA204_RX_FAIL ((uint8_t) 0xE6)
21 #define SHA204_RX_NO_RESPONSE ((uint8_t) 0xE7)
22 #define SHA204_RESYNC_WITH_WAKEUP ((uint8_t) 0xE8)
23 
24 #define SHA204_COMM_FAIL ((uint8_t) 0xF0)
25 #define SHA204_TIMEOUT ((uint8_t) 0xF1)
26 
27 /* bitbang_config.h */
28 
29 #define PORT_ACCESS_TIME (630)
30 #define START_PULSE_WIDTH (4340)
31 #define BIT_DELAY (4)
32 #define RX_TX_DELAY (15)
33 #define START_PULSE_TIME_OUT (255)
34 #define ZERO_PULSE_TIME_OUT (26)
35 
36 /* swi_phys.h */
37 
38 #define SWI_FUNCTION_RETCODE_SUCCESS ((uint8_t) 0x00)
39 #define SWI_FUNCTION_RETCODE_TIMEOUT ((uint8_t) 0xF1)
40 #define SWI_FUNCTION_RETCODE_RX_FAIL ((uint8_t) 0xF9)
41 
42 /* sha204_physical.h */
43 
44 #define SHA204_RSP_SIZE_MIN ((uint8_t) 4)
45 #define SHA204_RSP_SIZE_MAX ((uint8_t) 35)
46 #define SHA204_BUFFER_POS_COUNT (0)
47 #define SHA204_BUFFER_POS_DATA (1)
48 #define SHA204_WAKEUP_PULSE_WIDTH (uint8_t) (6.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5)
49 #define SHA204_WAKEUP_DELAY (uint8_t) (3.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5)
50 
51 /* sha204_swi.c */
52 #define SHA204_SWI_FLAG_CMD ((uint8_t) 0x77)
53 #define SHA204_SWI_FLAG_TX ((uint8_t) 0x88)
54 #define SHA204_SWI_FLAG_IDLE ((uint8_t) 0xBB)
55 #define SHA204_SWI_FLAG_SLEEP ((uint8_t) 0xCC)
56 
57 /* sha204_comm_marshaling.h */
58 // command op-code definitions
59 #define SHA204_GENDIG ((uint8_t) 0x15)
60 #define SHA204_HMAC ((uint8_t) 0x11)
61 #define SHA204_NONCE ((uint8_t) 0x16)
62 #define SHA204_RANDOM ((uint8_t) 0x1B)
63 #define SHA204_READ ((uint8_t) 0x02)
64 #define SHA204_SHA ((uint8_t) 0x47)
65 #define SHA204_WRITE ((uint8_t) 0x12)
66 
67 // packet size definitions
68 #define SHA204_RSP_SIZE_VAL ((uint8_t) 7)
69 
70 // definitions for command packet indexes common to all commands
71 #define SHA204_COUNT_IDX ( 0)
72 #define SHA204_OPCODE_IDX ( 1)
73 #define SHA204_PARAM1_IDX ( 2)
74 #define SHA204_PARAM2_IDX ( 3)
75 #define SHA204_DATA_IDX ( 5)
76 
77 // zone definitions
78 #define SHA204_ZONE_CONFIG ((uint8_t) 0x00)
79 #define SHA204_ZONE_OTP ((uint8_t) 0x01)
80 #define SHA204_ZONE_DATA ((uint8_t) 0x02)
81 #define SHA204_ZONE_MASK ((uint8_t) 0x03)
82 #define SHA204_ZONE_COUNT_FLAG ((uint8_t) 0x80)
83 #define SHA204_ZONE_ACCESS_4 ((uint8_t) 4)
84 #define SHA204_ZONE_ACCESS_32 ((uint8_t) 32)
85 #define SHA204_ADDRESS_MASK_CONFIG ( 0x001F)
86 #define SHA204_ADDRESS_MASK_OTP ( 0x000F)
87 #define SHA204_ADDRESS_MASK ( 0x007F)
88 
89 // GenDig command definitions
90 #define GENDIG_ZONE_IDX SHA204_PARAM1_IDX
91 #define GENDIG_KEYID_IDX SHA204_PARAM2_IDX
92 #define GENDIG_DATA_IDX SHA204_DATA_IDX
93 #define GENDIG_COUNT SHA204_CMD_SIZE_MIN
94 #define GENDIG_COUNT_DATA (11)
95 #define GENDIG_OTHER_DATA_SIZE (4)
96 #define GENDIG_ZONE_CONFIG ((uint8_t) 0)
97 #define GENDIG_ZONE_OTP ((uint8_t) 1)
98 #define GENDIG_ZONE_DATA ((uint8_t) 2)
99 
100 // HMAC command definitions
101 #define HMAC_MODE_IDX SHA204_PARAM1_IDX
102 #define HMAC_KEYID_IDX SHA204_PARAM2_IDX
103 #define HMAC_COUNT SHA204_CMD_SIZE_MIN
104 #define HMAC_MODE_MASK ((uint8_t) 0x74)
105 #define HMAC_MODE_SOURCE_FLAG_MATCH ((uint8_t) 0x04)
106 
107 // Nonce command definitions
108 #define NONCE_MODE_IDX SHA204_PARAM1_IDX
109 #define NONCE_PARAM2_IDX SHA204_PARAM2_IDX
110 #define NONCE_INPUT_IDX SHA204_DATA_IDX
111 #define NONCE_COUNT_SHORT (27)
112 #define NONCE_COUNT_LONG (39)
113 #define NONCE_MODE_MASK ((uint8_t) 3)
114 #define NONCE_MODE_SEED_UPDATE ((uint8_t) 0x00)
115 #define NONCE_MODE_NO_SEED_UPDATE ((uint8_t) 0x01)
116 #define NONCE_MODE_INVALID ((uint8_t) 0x02)
117 #define NONCE_MODE_PASSTHROUGH ((uint8_t) 0x03)
118 #define NONCE_NUMIN_SIZE (20)
119 #define NONCE_NUMIN_SIZE_PASSTHROUGH (32)
120 
121 // Random command definitions
122 #define RANDOM_MODE_IDX SHA204_PARAM1_IDX
123 #define RANDOM_PARAM2_IDX SHA204_PARAM2_IDX
124 #define RANDOM_COUNT SHA204_CMD_SIZE_MIN
125 #define RANDOM_SEED_UPDATE ((uint8_t) 0x00)
126 #define RANDOM_NO_SEED_UPDATE ((uint8_t) 0x01)
127 
128 // Read command definitions
129 #define READ_ZONE_IDX SHA204_PARAM1_IDX
130 #define READ_ADDR_IDX SHA204_PARAM2_IDX
131 #define READ_COUNT SHA204_CMD_SIZE_MIN
132 #define READ_ZONE_MASK ((uint8_t) 0x83)
133 #define READ_ZONE_MODE_32_BYTES ((uint8_t) 0x80)
134 
135 // SHA command definitions
136 #define SHA_MODE_IDX SHA204_PARAM1_IDX
137 #define SHA_PARAM2_IDX SHA204_PARAM2_IDX
138 #define SHA_COUNT_SHORT SHA204_CMD_SIZE_MIN
139 #define SHA_COUNT_LONG (71)
140 #define SHA_MSG_SIZE (64)
141 #define SHA_INIT ((uint8_t) 0x00)
142 #define SHA_CALC ((uint8_t) 0x01)
143 
144 // Write command definitions
145 #define WRITE_ZONE_IDX SHA204_PARAM1_IDX
146 #define WRITE_ADDR_IDX SHA204_PARAM2_IDX
147 #define WRITE_VALUE_IDX SHA204_DATA_IDX
148 #define WRITE_MAC_VS_IDX ( 9)
149 #define WRITE_MAC_VL_IDX (37)
150 #define WRITE_COUNT_SHORT (11)
151 #define WRITE_COUNT_LONG (39)
152 #define WRITE_COUNT_SHORT_MAC (43)
153 #define WRITE_COUNT_LONG_MAC (71)
154 #define WRITE_MAC_SIZE (32)
155 #define WRITE_ZONE_MASK ((uint8_t) 0xC3)
156 #define WRITE_ZONE_WITH_MAC ((uint8_t) 0x40)
157 
158 // Response size definitions
159 #define GENDIG_RSP_SIZE SHA204_RSP_SIZE_MIN
160 #define HMAC_RSP_SIZE SHA204_RSP_SIZE_MAX
161 #define NONCE_RSP_SIZE_SHORT SHA204_RSP_SIZE_MIN
162 #define NONCE_RSP_SIZE_LONG SHA204_RSP_SIZE_MAX
163 #define RANDOM_RSP_SIZE SHA204_RSP_SIZE_MAX
164 #define READ_4_RSP_SIZE SHA204_RSP_SIZE_VAL
165 #define READ_32_RSP_SIZE SHA204_RSP_SIZE_MAX
166 #define SHA_RSP_SIZE_SHORT SHA204_RSP_SIZE_MIN
167 #define SHA_RSP_SIZE_LONG SHA204_RSP_SIZE_MAX
168 #define WRITE_RSP_SIZE SHA204_RSP_SIZE_MIN
169 
170 // command timing definitions for minimum execution times (ms)
171 #define GENDIG_DELAY ((uint8_t) (11.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
172 #define HMAC_DELAY ((uint8_t) (27.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
173 #define NONCE_DELAY ((uint8_t) (22.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
174 #define RANDOM_DELAY ((uint8_t) (11.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
175 #define READ_DELAY ((uint8_t) ( 0.4 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
176 #define SHA_DELAY ((uint8_t) (11.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
177 #define WRITE_DELAY ((uint8_t) ( 4.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
178 
179 // command timing definitions for maximum execution times (ms)
180 #define GENDIG_EXEC_MAX ((uint8_t) (43.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
181 #define HMAC_EXEC_MAX ((uint8_t) (69.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
182 #define NONCE_EXEC_MAX ((uint8_t) (60.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
183 #define RANDOM_EXEC_MAX ((uint8_t) (50.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
184 #define READ_EXEC_MAX ((uint8_t) ( 4.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
185 #define SHA_EXEC_MAX ((uint8_t) (22.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
186 #define WRITE_EXEC_MAX ((uint8_t) (42.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
187 
188 /* from sha204_config.h */
189 
190 #define CPU_CLOCK_DEVIATION_POSITIVE (1.01)
191 #define CPU_CLOCK_DEVIATION_NEGATIVE (0.99)
192 #define SHA204_RETRY_COUNT (1)
193 #define SWI_RECEIVE_TIME_OUT ((uint16_t) 163)
194 #define SWI_US_PER_BYTE ((uint16_t) 313)
195 #define SHA204_SYNC_TIMEOUT ((uint8_t) 85)
196 #define SHA204_RESPONSE_TIMEOUT ((uint16_t) SWI_RECEIVE_TIME_OUT + SWI_US_PER_BYTE)
197 
198 /* from sha204_comm.h */
199 
200 #define SHA204_COMMAND_EXEC_MAX ((uint8_t) (69.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
201 #define SHA204_CMD_SIZE_MIN ((uint8_t) 7)
202 #ifndef SHA204_CMD_SIZE_MAX
203 #define SHA204_CMD_SIZE_MAX ((uint8_t) SHA_COUNT_LONG)
204 #endif
205 #define SHA204_CRC_SIZE ((uint8_t) 2)
206 #define SHA204_BUFFER_POS_STATUS (1)
207 #define SHA204_BUFFER_POS_DATA (1)
208 #define SHA204_STATUS_BYTE_WAKEUP ((uint8_t) 0x11)
209 #define SHA204_STATUS_BYTE_PARSE ((uint8_t) 0x03)
210 #define SHA204_STATUS_BYTE_EXEC ((uint8_t) 0x0F)
211 #define SHA204_STATUS_BYTE_COMM ((uint8_t) 0xFF)
212 
213 /* EEPROM Addresses */
214 /* Configuration Zone */
215 #define ADDRESS_SN03 0 // SN[0:3] are bytes 0->3 of configuration zone
216 #define ADDRESS_RevNum 4 // bytes 4->7 of config zone are RevNum
217 #define ADDRESS_SN47 8 // SN[4:7] are bytes 8->11 of config zone
218 #define ADDRESS_SN8 12 // SN[8] is byte 12 of config zone, should be 0xEE
219 #define ADDRESS_I2CEN 14 // I2C Enable, bit 0 represents I2C enable status
220 #define ADDRESS_I2CADD 16 // Defines I2C address of SHA204
221 #define ADDRESS_OTPMODE 18 // Sets the One-time-programmable mode
222 #define ADDRESS_SELECTOR 19 // Controls writability of Selector
223 
224 #define SHA204_SERIAL_SZ 9 // The number of bytes the serial number consists of
225 
226 /* Low level HW access macros */
227 /* function calls is not working, as it will have too much overhead */
228 #if !defined(ARDUINO_ARCH_AVR) && !defined(ARDUINO_ARCH_MEGAAVR) // For everything else than AVR use pinMode / digitalWrite
229 #define SHA204_SET_OUTPUT() pinMode(device_pin, OUTPUT)
230 #define SHA204_SET_INPUT() pinMode(device_pin, INPUT)
231 #define SHA204_POUT_HIGH() digitalWrite(device_pin, HIGH)
232 #define SHA204_POUT_LOW() digitalWrite(device_pin, LOW)
233 #define SHA204_PIN_READ() digitalRead(device_pin)
234 #else
235 #define SHA204_SET_INPUT() *device_port_DDR &= ~device_pin
236 #define SHA204_SET_OUTPUT() *device_port_DDR |= device_pin
237 #define SHA204_POUT_HIGH() *device_port_OUT |= device_pin
238 #define SHA204_POUT_LOW() *device_port_OUT &= ~device_pin
239 #define SHA204_PIN_READ() (*device_port_IN & device_pin)
240 #endif
241 
242 void atsha204_init(uint8_t pin);
243 void atsha204_idle(void);
244 void atsha204_sleep(void);
245 uint8_t atsha204_wakeup(uint8_t *response);
246 uint8_t atsha204_execute(uint8_t op_code, uint8_t param1, uint16_t param2,
247  uint8_t datalen1, uint8_t *data1, uint8_t tx_size,
248  uint8_t *tx_buffer, uint8_t rx_size, uint8_t *rx_buffer);
249 uint8_t atsha204_getSerialNumber(uint8_t *response);
250 uint8_t atsha204_read(uint8_t *tx_buffer, uint8_t *rx_buffer, uint8_t zone, uint16_t address);
251 
252 #endif
253 #endif