Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ATSHA204.h 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #ifndef ATSHA204_H
  2. #define ATSHA204_H
  3. #if !DOXYGEN
  4. #include <Arduino.h>
  5. /* This is a scaled down variant of the ATSHA204 library, tweaked to meet the specific needs of the MySensors library. */
  6. /* Library return codes */
  7. #define SHA204_SUCCESS ((uint8_t) 0x00) //!< Function succeeded.
  8. #define SHA204_PARSE_ERROR ((uint8_t) 0xD2) //!< response status byte indicates parsing error
  9. #define SHA204_CMD_FAIL ((uint8_t) 0xD3) //!< response status byte indicates command execution error
  10. #define SHA204_STATUS_CRC ((uint8_t) 0xD4) //!< response status byte indicates CRC error
  11. #define SHA204_STATUS_UNKNOWN ((uint8_t) 0xD5) //!< response status byte is unknown
  12. #define SHA204_FUNC_FAIL ((uint8_t) 0xE0) //!< Function could not execute due to incorrect condition / state.
  13. #define SHA204_GEN_FAIL ((uint8_t) 0xE1) //!< unspecified error
  14. #define SHA204_BAD_PARAM ((uint8_t) 0xE2) //!< bad argument (out of range, null pointer, etc.)
  15. #define SHA204_INVALID_ID ((uint8_t) 0xE3) //!< invalid device id, id not set
  16. #define SHA204_INVALID_SIZE ((uint8_t) 0xE4) //!< Count value is out of range or greater than buffer size.
  17. #define SHA204_BAD_CRC ((uint8_t) 0xE5) //!< incorrect CRC received
  18. #define SHA204_RX_FAIL ((uint8_t) 0xE6) //!< Timed out while waiting for response. Number of bytes received is > 0.
  19. #define SHA204_RX_NO_RESPONSE ((uint8_t) 0xE7) //!< Not an error while the Command layer is polling for a command response.
  20. #define SHA204_RESYNC_WITH_WAKEUP ((uint8_t) 0xE8) //!< re-synchronization succeeded, but only after generating a Wake-up
  21. #define SHA204_COMM_FAIL ((uint8_t) 0xF0) //!< Communication with device failed. Same as in hardware dependent modules.
  22. #define SHA204_TIMEOUT ((uint8_t) 0xF1) //!< Timed out while waiting for response. Number of bytes received is 0.
  23. /* bitbang_config.h */
  24. #define PORT_ACCESS_TIME (630) //! time it takes to toggle the pin at CPU clock of 16 MHz (ns)
  25. #define START_PULSE_WIDTH (4340) //! width of start pulse (ns)
  26. #define BIT_DELAY (4) //! delay macro for width of one pulse (start pulse or zero pulse, in ns)
  27. #define RX_TX_DELAY (15) //! turn around time when switching from receive to transmit
  28. #define START_PULSE_TIME_OUT (255) //! This value is decremented while waiting for the falling edge of a start pulse.
  29. #define ZERO_PULSE_TIME_OUT (26) //! This value is decremented while waiting for the falling edge of a zero pulse.
  30. /* swi_phys.h */
  31. #define SWI_FUNCTION_RETCODE_SUCCESS ((uint8_t) 0x00) //!< Communication with device succeeded.
  32. #define SWI_FUNCTION_RETCODE_TIMEOUT ((uint8_t) 0xF1) //!< Communication timed out.
  33. #define SWI_FUNCTION_RETCODE_RX_FAIL ((uint8_t) 0xF9) //!< Communication failed after at least one byte was received.
  34. /* sha204_physical.h */
  35. #define SHA204_RSP_SIZE_MIN ((uint8_t) 4) //!< minimum number of bytes in response
  36. #define SHA204_RSP_SIZE_MAX ((uint8_t) 35) //!< maximum size of response packet
  37. #define SHA204_BUFFER_POS_COUNT (0) //!< buffer index of count byte in command or response
  38. #define SHA204_BUFFER_POS_DATA (1) //!< buffer index of data in response
  39. #define SHA204_WAKEUP_PULSE_WIDTH (uint8_t) (6.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5) //! width of Wakeup pulse in 10 us units
  40. #define SHA204_WAKEUP_DELAY (uint8_t) (3.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5) //! delay between Wakeup pulse and communication in ms
  41. /* sha204_swi.c */
  42. #define SHA204_SWI_FLAG_CMD ((uint8_t) 0x77) //!< flag preceding a command
  43. #define SHA204_SWI_FLAG_TX ((uint8_t) 0x88) //!< flag requesting a response
  44. #define SHA204_SWI_FLAG_IDLE ((uint8_t) 0xBB) //!< flag requesting to go into Idle mode
  45. #define SHA204_SWI_FLAG_SLEEP ((uint8_t) 0xCC) //!< flag requesting to go into Sleep mode
  46. /* sha204_comm_marshaling.h */
  47. // command op-code definitions
  48. #define SHA204_GENDIG ((uint8_t) 0x15) //!< GenDig command op-code
  49. #define SHA204_HMAC ((uint8_t) 0x11) //!< HMAC command op-code
  50. #define SHA204_NONCE ((uint8_t) 0x16) //!< Nonce command op-code
  51. #define SHA204_RANDOM ((uint8_t) 0x1B) //!< Random command op-code
  52. #define SHA204_READ ((uint8_t) 0x02) //!< Read command op-code
  53. #define SHA204_SHA ((uint8_t) 0x47) //!< SHA command op-code
  54. #define SHA204_WRITE ((uint8_t) 0x12) //!< Write command op-code
  55. // packet size definitions
  56. #define SHA204_RSP_SIZE_VAL ((uint8_t) 7) //!< size of response packet containing four bytes of data
  57. // definitions for command packet indexes common to all commands
  58. #define SHA204_COUNT_IDX ( 0) //!< command packet index for count
  59. #define SHA204_OPCODE_IDX ( 1) //!< command packet index for op-code
  60. #define SHA204_PARAM1_IDX ( 2) //!< command packet index for first parameter
  61. #define SHA204_PARAM2_IDX ( 3) //!< command packet index for second parameter
  62. #define SHA204_DATA_IDX ( 5) //!< command packet index for second parameter
  63. // zone definitions
  64. #define SHA204_ZONE_CONFIG ((uint8_t) 0x00) //!< Configuration zone
  65. #define SHA204_ZONE_OTP ((uint8_t) 0x01) //!< OTP (One Time Programming) zone
  66. #define SHA204_ZONE_DATA ((uint8_t) 0x02) //!< Data zone
  67. #define SHA204_ZONE_MASK ((uint8_t) 0x03) //!< Zone mask
  68. #define SHA204_ZONE_COUNT_FLAG ((uint8_t) 0x80) //!< Zone bit 7 set: Access 32 bytes, otherwise 4 bytes.
  69. #define SHA204_ZONE_ACCESS_4 ((uint8_t) 4) //!< Read or write 4 bytes.
  70. #define SHA204_ZONE_ACCESS_32 ((uint8_t) 32) //!< Read or write 32 bytes.
  71. #define SHA204_ADDRESS_MASK_CONFIG ( 0x001F) //!< Address bits 5 to 7 are 0 for Configuration zone.
  72. #define SHA204_ADDRESS_MASK_OTP ( 0x000F) //!< Address bits 4 to 7 are 0 for OTP zone.
  73. #define SHA204_ADDRESS_MASK ( 0x007F) //!< Address bit 7 to 15 are always 0.
  74. // GenDig command definitions
  75. #define GENDIG_ZONE_IDX SHA204_PARAM1_IDX //!< GenDig command index for zone
  76. #define GENDIG_KEYID_IDX SHA204_PARAM2_IDX //!< GenDig command index for key id
  77. #define GENDIG_DATA_IDX SHA204_DATA_IDX //!< GenDig command index for optional data
  78. #define GENDIG_COUNT SHA204_CMD_SIZE_MIN //!< GenDig command packet size without "other data"
  79. #define GENDIG_COUNT_DATA (11) //!< GenDig command packet size with "other data"
  80. #define GENDIG_OTHER_DATA_SIZE (4) //!< GenDig size of "other data"
  81. #define GENDIG_ZONE_CONFIG ((uint8_t) 0) //!< GenDig zone id config
  82. #define GENDIG_ZONE_OTP ((uint8_t) 1) //!< GenDig zone id OTP
  83. #define GENDIG_ZONE_DATA ((uint8_t) 2) //!< GenDig zone id data
  84. // HMAC command definitions
  85. #define HMAC_MODE_IDX SHA204_PARAM1_IDX //!< HMAC command index for mode
  86. #define HMAC_KEYID_IDX SHA204_PARAM2_IDX //!< HMAC command index for key id
  87. #define HMAC_COUNT SHA204_CMD_SIZE_MIN //!< HMAC command packet size
  88. #define HMAC_MODE_MASK ((uint8_t) 0x74) //!< HMAC mode bits 0, 1, 3, and 7 are 0.
  89. #define HMAC_MODE_SOURCE_FLAG_MATCH ((uint8_t) 0x04) //!< HMAC mode bit 2: match TempKey.SourceFlag
  90. // Nonce command definitions
  91. #define NONCE_MODE_IDX SHA204_PARAM1_IDX //!< Nonce command index for mode
  92. #define NONCE_PARAM2_IDX SHA204_PARAM2_IDX //!< Nonce command index for 2. parameter
  93. #define NONCE_INPUT_IDX SHA204_DATA_IDX //!< Nonce command index for input data
  94. #define NONCE_COUNT_SHORT (27) //!< Nonce command packet size for 20 bytes of data
  95. #define NONCE_COUNT_LONG (39) //!< Nonce command packet size for 32 bytes of data
  96. #define NONCE_MODE_MASK ((uint8_t) 3) //!< Nonce mode bits 2 to 7 are 0.
  97. #define NONCE_MODE_SEED_UPDATE ((uint8_t) 0x00) //!< Nonce mode: update seed
  98. #define NONCE_MODE_NO_SEED_UPDATE ((uint8_t) 0x01) //!< Nonce mode: do not update seed
  99. #define NONCE_MODE_INVALID ((uint8_t) 0x02) //!< Nonce mode 2 is invalid.
  100. #define NONCE_MODE_PASSTHROUGH ((uint8_t) 0x03) //!< Nonce mode: pass-through
  101. #define NONCE_NUMIN_SIZE (20) //!< Nonce data length
  102. #define NONCE_NUMIN_SIZE_PASSTHROUGH (32) //!< Nonce data length in pass-through mode (mode = 3)
  103. // Random command definitions
  104. #define RANDOM_MODE_IDX SHA204_PARAM1_IDX //!< Random command index for mode
  105. #define RANDOM_PARAM2_IDX SHA204_PARAM2_IDX //!< Random command index for 2. parameter
  106. #define RANDOM_COUNT SHA204_CMD_SIZE_MIN //!< Random command packet size
  107. #define RANDOM_SEED_UPDATE ((uint8_t) 0x00) //!< Random mode for automatic seed update
  108. #define RANDOM_NO_SEED_UPDATE ((uint8_t) 0x01) //!< Random mode for no seed update
  109. // Read command definitions
  110. #define READ_ZONE_IDX SHA204_PARAM1_IDX //!< Read command index for zone
  111. #define READ_ADDR_IDX SHA204_PARAM2_IDX //!< Read command index for address
  112. #define READ_COUNT SHA204_CMD_SIZE_MIN //!< Read command packet size
  113. #define READ_ZONE_MASK ((uint8_t) 0x83) //!< Read zone bits 2 to 6 are 0.
  114. #define READ_ZONE_MODE_32_BYTES ((uint8_t) 0x80) //!< Read mode: 32 bytes
  115. // SHA command definitions
  116. #define SHA_MODE_IDX SHA204_PARAM1_IDX //!< SHA command index for mode
  117. #define SHA_PARAM2_IDX SHA204_PARAM2_IDX //!< SHA command index for 2. parameter
  118. #define SHA_COUNT_SHORT SHA204_CMD_SIZE_MIN //!< SHA command packet size for init
  119. #define SHA_COUNT_LONG (71) //!< SHA command packet size for calculation
  120. #define SHA_MSG_SIZE (64) //!< SHA message data size
  121. #define SHA_INIT ((uint8_t) 0x00) //!< SHA mode for init
  122. #define SHA_CALC ((uint8_t) 0x01) //!< SHA mode for calculation
  123. // Write command definitions
  124. #define WRITE_ZONE_IDX SHA204_PARAM1_IDX //!< Write command index for zone
  125. #define WRITE_ADDR_IDX SHA204_PARAM2_IDX //!< Write command index for address
  126. #define WRITE_VALUE_IDX SHA204_DATA_IDX //!< Write command index for data
  127. #define WRITE_MAC_VS_IDX ( 9) //!< Write command index for MAC following short data
  128. #define WRITE_MAC_VL_IDX (37) //!< Write command index for MAC following long data
  129. #define WRITE_COUNT_SHORT (11) //!< Write command packet size with short data and no MAC
  130. #define WRITE_COUNT_LONG (39) //!< Write command packet size with long data and no MAC
  131. #define WRITE_COUNT_SHORT_MAC (43) //!< Write command packet size with short data and MAC
  132. #define WRITE_COUNT_LONG_MAC (71) //!< Write command packet size with long data and MAC
  133. #define WRITE_MAC_SIZE (32) //!< Write MAC size
  134. #define WRITE_ZONE_MASK ((uint8_t) 0xC3) //!< Write zone bits 2 to 5 are 0.
  135. #define WRITE_ZONE_WITH_MAC ((uint8_t) 0x40) //!< Write zone bit 6: write encrypted with MAC
  136. // Response size definitions
  137. #define GENDIG_RSP_SIZE SHA204_RSP_SIZE_MIN //!< response size of GenDig command
  138. #define HMAC_RSP_SIZE SHA204_RSP_SIZE_MAX //!< response size of HMAC command
  139. #define NONCE_RSP_SIZE_SHORT SHA204_RSP_SIZE_MIN //!< response size of Nonce command with mode[0:1] = 3
  140. #define NONCE_RSP_SIZE_LONG SHA204_RSP_SIZE_MAX //!< response size of Nonce command
  141. #define RANDOM_RSP_SIZE SHA204_RSP_SIZE_MAX //!< response size of Random command
  142. #define READ_4_RSP_SIZE SHA204_RSP_SIZE_VAL //!< response size of Read command when reading 4 bytes
  143. #define READ_32_RSP_SIZE SHA204_RSP_SIZE_MAX //!< response size of Read command when reading 32 bytes
  144. #define SHA_RSP_SIZE_SHORT SHA204_RSP_SIZE_MIN //!< response size of SHA command with mode[0:1] = 0
  145. #define SHA_RSP_SIZE_LONG SHA204_RSP_SIZE_MAX //!< response size of SHA command
  146. #define WRITE_RSP_SIZE SHA204_RSP_SIZE_MIN //!< response size of Write command
  147. // command timing definitions for minimum execution times (ms)
  148. #define GENDIG_DELAY ((uint8_t) (11.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
  149. #define HMAC_DELAY ((uint8_t) (27.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
  150. #define NONCE_DELAY ((uint8_t) (22.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
  151. #define RANDOM_DELAY ((uint8_t) (11.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
  152. #define READ_DELAY ((uint8_t) ( 0.4 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
  153. #define SHA_DELAY ((uint8_t) (11.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
  154. #define WRITE_DELAY ((uint8_t) ( 4.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
  155. // command timing definitions for maximum execution times (ms)
  156. #define GENDIG_EXEC_MAX ((uint8_t) (43.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
  157. #define HMAC_EXEC_MAX ((uint8_t) (69.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
  158. #define NONCE_EXEC_MAX ((uint8_t) (60.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
  159. #define RANDOM_EXEC_MAX ((uint8_t) (50.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
  160. #define READ_EXEC_MAX ((uint8_t) ( 4.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
  161. #define SHA_EXEC_MAX ((uint8_t) (22.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
  162. #define WRITE_EXEC_MAX ((uint8_t) (42.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
  163. /* from sha204_config.h */
  164. #define CPU_CLOCK_DEVIATION_POSITIVE (1.01)
  165. #define CPU_CLOCK_DEVIATION_NEGATIVE (0.99)
  166. #define SHA204_RETRY_COUNT (1)
  167. #define SWI_RECEIVE_TIME_OUT ((uint16_t) 163) //! #START_PULSE_TIME_OUT in us instead of loop counts
  168. #define SWI_US_PER_BYTE ((uint16_t) 313) //! It takes 312.5 us to send a byte (9 single-wire bits / 230400 Baud * 8 flag bits).
  169. #define SHA204_SYNC_TIMEOUT ((uint8_t) 85)//! delay before sending a transmit flag in the synchronization routine
  170. #define SHA204_RESPONSE_TIMEOUT ((uint16_t) SWI_RECEIVE_TIME_OUT + SWI_US_PER_BYTE) //! SWI response timeout is the sum of receive timeout and the time it takes to send the TX flag.
  171. /* from sha204_comm.h */
  172. #define SHA204_COMMAND_EXEC_MAX ((uint8_t) (69.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5)) //! maximum command delay
  173. #define SHA204_CMD_SIZE_MIN ((uint8_t) 7) //! minimum number of bytes in command (from count byte to second CRC byte)
  174. #ifndef SHA204_CMD_SIZE_MAX
  175. #define SHA204_CMD_SIZE_MAX ((uint8_t) SHA_COUNT_LONG) //! maximum size of command packet (SHA)
  176. #endif
  177. #define SHA204_CRC_SIZE ((uint8_t) 2) //! number of CRC bytes
  178. #define SHA204_BUFFER_POS_STATUS (1) //! buffer index of status byte in status response
  179. #define SHA204_BUFFER_POS_DATA (1) //! buffer index of first data byte in data response
  180. #define SHA204_STATUS_BYTE_WAKEUP ((uint8_t) 0x11) //! command parse error
  181. #define SHA204_STATUS_BYTE_PARSE ((uint8_t) 0x03) //! command parse error
  182. #define SHA204_STATUS_BYTE_EXEC ((uint8_t) 0x0F) //! command execution error
  183. #define SHA204_STATUS_BYTE_COMM ((uint8_t) 0xFF) //! communication error
  184. /* EEPROM Addresses */
  185. /* Configuration Zone */
  186. #define ADDRESS_SN03 0 // SN[0:3] are bytes 0->3 of configuration zone
  187. #define ADDRESS_RevNum 4 // bytes 4->7 of config zone are RevNum
  188. #define ADDRESS_SN47 8 // SN[4:7] are bytes 8->11 of config zone
  189. #define ADDRESS_SN8 12 // SN[8] is byte 12 of config zone, should be 0xEE
  190. #define ADDRESS_I2CEN 14 // I2C Enable, bit 0 represents I2C enable status
  191. #define ADDRESS_I2CADD 16 // Defines I2C address of SHA204
  192. #define ADDRESS_OTPMODE 18 // Sets the One-time-programmable mode
  193. #define ADDRESS_SELECTOR 19 // Controls writability of Selector
  194. #define SHA204_SERIAL_SZ 9 // The number of bytes the serial number consists of
  195. /* Low level HW access macros */
  196. /* function calls is not working, as it will have too much overhead */
  197. #if !defined(ARDUINO_ARCH_AVR) // For everything else than AVR use pinMode / digitalWrite
  198. #define SHA204_SET_OUTPUT() pinMode(device_pin, OUTPUT)
  199. #define SHA204_SET_INPUT() pinMode(device_pin, INPUT)
  200. #define SHA204_POUT_HIGH() digitalWrite(device_pin, HIGH)
  201. #define SHA204_POUT_LOW() digitalWrite(device_pin, LOW)
  202. #define SHA204_PIN_READ() digitalRead(device_pin)
  203. #else
  204. #define SHA204_SET_INPUT() *device_port_DDR &= ~device_pin
  205. #define SHA204_SET_OUTPUT() *device_port_DDR |= device_pin
  206. #define SHA204_POUT_HIGH() *device_port_OUT |= device_pin
  207. #define SHA204_POUT_LOW() *device_port_OUT &= ~device_pin
  208. #define SHA204_PIN_READ() (*device_port_IN & device_pin)
  209. #endif
  210. void atsha204_init(uint8_t pin);
  211. void atsha204_idle(void);
  212. void atsha204_sleep(void);
  213. uint8_t atsha204_wakeup(uint8_t *response);
  214. uint8_t atsha204_execute(uint8_t op_code, uint8_t param1, uint16_t param2,
  215. uint8_t datalen1, uint8_t *data1, uint8_t tx_size,
  216. uint8_t *tx_buffer, uint8_t rx_size, uint8_t *rx_buffer);
  217. uint8_t atsha204_getSerialNumber(uint8_t *response);
  218. uint8_t atsha204_read(uint8_t *tx_buffer, uint8_t *rx_buffer, uint8_t zone, uint16_t address);
  219. #endif
  220. #endif