one_wire_slave.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #include "one_wire_slave.h"
  2. #include "callback-connector.h"
  3. #include "main.h"
  4. #include "one_wire_device.h"
  5. #define OWET OneWireEmulateTiming
  6. void OneWireSlave::start(void) {
  7. // add exti interrupt
  8. hal_gpio_add_int_callback(one_wire_pin_record, exti_cb, this);
  9. // init gpio
  10. hal_gpio_init(one_wire_pin_record, GpioModeInterruptRiseFall, GpioPullNo, GpioSpeedLow);
  11. pin_set_float();
  12. // init instructions per us count
  13. __instructions_per_us = (SystemCoreClock / 1000000.0f);
  14. }
  15. void OneWireSlave::stop(void) {
  16. // deinit gpio
  17. hal_gpio_init(one_wire_pin_record, GpioModeInput, GpioPullNo, GpioSpeedLow);
  18. // remove exti interrupt
  19. hal_gpio_remove_int_callback(one_wire_pin_record);
  20. // deattach devices
  21. deattach();
  22. }
  23. OneWireSlave::OneWireSlave(const GpioPin* pin) {
  24. one_wire_pin_record = pin;
  25. exti_cb = cbc::obtain_connector(this, &OneWireSlave::exti_callback);
  26. }
  27. OneWireSlave::~OneWireSlave() {
  28. stop();
  29. }
  30. void OneWireSlave::attach(OneWireDevice* attached_device) {
  31. device = attached_device;
  32. device->attach(this);
  33. }
  34. void OneWireSlave::deattach(void) {
  35. if(device != nullptr) {
  36. device->deattach();
  37. }
  38. device = nullptr;
  39. }
  40. void OneWireSlave::set_result_callback(OneWireSlaveResultCallback result_cb, void* ctx) {
  41. this->result_cb = result_cb;
  42. this->result_cb_ctx = ctx;
  43. }
  44. void OneWireSlave::pin_set_float() {
  45. hal_gpio_write(one_wire_pin_record, true);
  46. }
  47. void OneWireSlave::pin_set_low() {
  48. hal_gpio_write(one_wire_pin_record, false);
  49. }
  50. void OneWireSlave::pin_init_interrupt_in_isr_ctx(void) {
  51. hal_gpio_init(one_wire_pin_record, GpioModeInterruptRiseFall, GpioPullNo, GpioSpeedLow);
  52. __HAL_GPIO_EXTI_CLEAR_IT(one_wire_pin_record->pin);
  53. }
  54. void OneWireSlave::pin_init_opendrain_in_isr_ctx(void) {
  55. hal_gpio_init(one_wire_pin_record, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
  56. __HAL_GPIO_EXTI_CLEAR_IT(one_wire_pin_record->pin);
  57. }
  58. OneWiteTimeType OneWireSlave::wait_while_gpio_is(OneWiteTimeType time, const bool pin_value) {
  59. uint32_t start = DWT->CYCCNT;
  60. uint32_t time_ticks = time * __instructions_per_us;
  61. uint32_t time_captured;
  62. do {
  63. time_captured = DWT->CYCCNT;
  64. if(hal_gpio_read(one_wire_pin_record) != pin_value) {
  65. OneWiteTimeType remaining_time = time_ticks - (time_captured - start);
  66. remaining_time /= __instructions_per_us;
  67. return remaining_time;
  68. }
  69. } while((time_captured - start) < time_ticks);
  70. return 0;
  71. }
  72. bool OneWireSlave::show_presence(void) {
  73. // wait while master delay presence check
  74. wait_while_gpio_is(OWET::PRESENCE_TIMEOUT, true);
  75. // show presence
  76. pin_set_low();
  77. delay_us(OWET::PRESENCE_MIN);
  78. pin_set_float();
  79. // somebody also can show presence
  80. const OneWiteTimeType wait_low_time = OWET::PRESENCE_MAX - OWET::PRESENCE_MIN;
  81. // so we will wait
  82. if(wait_while_gpio_is(wait_low_time, false) == 0) {
  83. error = OneWireSlaveError::PRESENCE_LOW_ON_LINE;
  84. return false;
  85. }
  86. return true;
  87. }
  88. bool OneWireSlave::receive_bit(void) {
  89. // wait while bus is low
  90. OneWiteTimeType time = OWET::SLOT_MAX;
  91. time = wait_while_gpio_is(time, false);
  92. if(time == 0) {
  93. error = OneWireSlaveError::RESET_IN_PROGRESS;
  94. return false;
  95. }
  96. // wait while bus is high
  97. time = OWET::MSG_HIGH_TIMEOUT;
  98. time = wait_while_gpio_is(time, true);
  99. if(time == 0) {
  100. error = OneWireSlaveError::AWAIT_TIMESLOT_TIMEOUT_HIGH;
  101. return false;
  102. }
  103. // wait a time of zero
  104. time = OWET::READ_MIN;
  105. time = wait_while_gpio_is(time, false);
  106. return (time > 0);
  107. }
  108. bool OneWireSlave::send_bit(bool value) {
  109. const bool write_zero = !value;
  110. // wait while bus is low
  111. OneWiteTimeType time = OWET::SLOT_MAX;
  112. time = wait_while_gpio_is(time, false);
  113. if(time == 0) {
  114. error = OneWireSlaveError::RESET_IN_PROGRESS;
  115. return false;
  116. }
  117. // wait while bus is high
  118. time = OWET::MSG_HIGH_TIMEOUT;
  119. time = wait_while_gpio_is(time, true);
  120. if(time == 0) {
  121. error = OneWireSlaveError::AWAIT_TIMESLOT_TIMEOUT_HIGH;
  122. return false;
  123. }
  124. // choose write time
  125. if(write_zero) {
  126. pin_set_low();
  127. time = OWET::WRITE_ZERO;
  128. } else {
  129. time = OWET::READ_MAX;
  130. }
  131. // hold line for ZERO or ONE time
  132. delay_us(time);
  133. pin_set_float();
  134. return true;
  135. }
  136. bool OneWireSlave::send(const uint8_t* address, const uint8_t data_length) {
  137. uint8_t bytes_sent = 0;
  138. pin_set_float();
  139. // bytes loop
  140. for(; bytes_sent < data_length; ++bytes_sent) {
  141. const uint8_t data_byte = address[bytes_sent];
  142. // bit loop
  143. for(uint8_t bit_mask = 0x01; bit_mask != 0; bit_mask <<= 1) {
  144. if(!send_bit(static_cast<bool>(bit_mask & data_byte))) {
  145. // if we cannot send first bit
  146. if((bit_mask == 0x01) && (error == OneWireSlaveError::AWAIT_TIMESLOT_TIMEOUT_HIGH))
  147. error = OneWireSlaveError::FIRST_BIT_OF_BYTE_TIMEOUT;
  148. return false;
  149. }
  150. }
  151. }
  152. return true;
  153. }
  154. bool OneWireSlave::receive(uint8_t* data, const uint8_t data_length) {
  155. uint8_t bytes_received = 0;
  156. pin_set_float();
  157. for(; bytes_received < data_length; ++bytes_received) {
  158. uint8_t value = 0;
  159. for(uint8_t bit_mask = 0x01; bit_mask != 0; bit_mask <<= 1) {
  160. if(receive_bit()) value |= bit_mask;
  161. }
  162. data[bytes_received] = value;
  163. }
  164. return (bytes_received != data_length);
  165. }
  166. void OneWireSlave::cmd_search_rom(void) {
  167. const uint8_t key_bytes = 8;
  168. uint8_t* key = device->id_storage;
  169. for(uint8_t i = 0; i < key_bytes; i++) {
  170. uint8_t key_byte = key[i];
  171. for(uint8_t j = 0; j < 8; j++) {
  172. bool bit = (key_byte >> j) & 0x01;
  173. if(!send_bit(bit)) return;
  174. if(!send_bit(!bit)) return;
  175. receive_bit();
  176. if(error != OneWireSlaveError::NO_ERROR) return;
  177. }
  178. }
  179. }
  180. bool OneWireSlave::receive_and_process_cmd(void) {
  181. uint8_t cmd;
  182. receive(&cmd, 1);
  183. if(error == OneWireSlaveError::RESET_IN_PROGRESS) return true;
  184. if(error != OneWireSlaveError::NO_ERROR) return false;
  185. switch(cmd) {
  186. case 0xF0:
  187. // SEARCH ROM
  188. cmd_search_rom();
  189. return true;
  190. case 0x0F:
  191. case 0x33:
  192. // READ ROM
  193. device->send_id();
  194. return true;
  195. default: // Unknown command
  196. error = OneWireSlaveError::INCORRECT_ONEWIRE_CMD;
  197. }
  198. if(error == OneWireSlaveError::RESET_IN_PROGRESS) return true;
  199. return (error == OneWireSlaveError::NO_ERROR);
  200. }
  201. bool OneWireSlave::bus_start(void) {
  202. bool result = true;
  203. if(device == nullptr) {
  204. result = false;
  205. } else {
  206. __disable_irq();
  207. pin_init_opendrain_in_isr_ctx();
  208. error = OneWireSlaveError::NO_ERROR;
  209. if(show_presence()) {
  210. // TODO think about multiple command cycles
  211. receive_and_process_cmd();
  212. result =
  213. (error == OneWireSlaveError::NO_ERROR ||
  214. error == OneWireSlaveError::INCORRECT_ONEWIRE_CMD);
  215. } else {
  216. result = false;
  217. }
  218. pin_init_interrupt_in_isr_ctx();
  219. __enable_irq();
  220. }
  221. return result;
  222. }
  223. void OneWireSlave::exti_callback(void* _ctx) {
  224. OneWireSlave* _this = static_cast<OneWireSlave*>(_ctx);
  225. volatile bool input_state = hal_gpio_read(_this->one_wire_pin_record);
  226. static uint32_t pulse_start = 0;
  227. if(input_state) {
  228. uint32_t pulse_length = (DWT->CYCCNT - pulse_start) / __instructions_per_us;
  229. if(pulse_length >= OWET::RESET_MIN) {
  230. if(pulse_length <= OWET::RESET_MAX) {
  231. // reset cycle ok
  232. bool result = _this->bus_start();
  233. if(result && _this->result_cb != nullptr) {
  234. _this->result_cb(result, _this->result_cb_ctx);
  235. }
  236. } else {
  237. error = OneWireSlaveError::VERY_LONG_RESET;
  238. }
  239. } else {
  240. error = OneWireSlaveError::VERY_SHORT_RESET;
  241. }
  242. } else {
  243. //FALL event
  244. pulse_start = DWT->CYCCNT;
  245. }
  246. }