one_wire_slave.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. api_interrupt_add(exti_cb, InterruptTypeExternalInterrupt, this);
  9. // init gpio
  10. gpio_init(one_wire_pin_record, GpioModeInterruptRiseFall);
  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. gpio_init_ex(one_wire_pin_record, GpioModeInput, GpioPullNo, GpioSpeedLow);
  18. // remove exti interrupt
  19. api_interrupt_remove(exti_cb, InterruptTypeExternalInterrupt);
  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. device = nullptr;
  36. device->deattach();
  37. }
  38. void OneWireSlave::set_result_callback(OneWireSlaveResultCallback result_cb, void* ctx) {
  39. this->result_cb = result_cb;
  40. this->result_cb_ctx = ctx;
  41. }
  42. void OneWireSlave::pin_set_float() {
  43. gpio_write(one_wire_pin_record, true);
  44. }
  45. void OneWireSlave::pin_set_low() {
  46. gpio_write(one_wire_pin_record, false);
  47. }
  48. void OneWireSlave::pin_init_interrupt_in_isr_ctx(void) {
  49. hal_gpio_init(one_wire_pin_record, GpioModeInterruptRiseFall, GpioPullNo, GpioSpeedLow);
  50. __HAL_GPIO_EXTI_CLEAR_IT(one_wire_pin_record->pin);
  51. }
  52. void OneWireSlave::pin_init_opendrain_in_isr_ctx(void) {
  53. hal_gpio_init(one_wire_pin_record, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
  54. __HAL_GPIO_EXTI_CLEAR_IT(one_wire_pin_record->pin);
  55. }
  56. OneWiteTimeType OneWireSlave::wait_while_gpio_is(OneWiteTimeType time, const bool pin_value) {
  57. uint32_t start = DWT->CYCCNT;
  58. uint32_t time_ticks = time * __instructions_per_us;
  59. uint32_t time_captured;
  60. do {
  61. time_captured = DWT->CYCCNT;
  62. if(gpio_read(one_wire_pin_record) != pin_value) {
  63. OneWiteTimeType remaining_time = time_ticks - (time_captured - start);
  64. remaining_time /= __instructions_per_us;
  65. return remaining_time;
  66. }
  67. } while((time_captured - start) < time_ticks);
  68. return 0;
  69. }
  70. bool OneWireSlave::show_presence(void) {
  71. // wait while master delay presence check
  72. wait_while_gpio_is(OWET::PRESENCE_TIMEOUT, true);
  73. // show presence
  74. pin_set_low();
  75. delay_us(OWET::PRESENCE_MIN);
  76. pin_set_float();
  77. // somebody also can show presence
  78. const OneWiteTimeType wait_low_time = OWET::PRESENCE_MAX - OWET::PRESENCE_MIN;
  79. // so we will wait
  80. if(wait_while_gpio_is(wait_low_time, false) == 0) {
  81. error = OneWireSlaveError::PRESENCE_LOW_ON_LINE;
  82. return false;
  83. }
  84. return true;
  85. }
  86. bool OneWireSlave::receive_bit(void) {
  87. // wait while bus is low
  88. OneWiteTimeType time = OWET::SLOT_MAX;
  89. time = wait_while_gpio_is(time, false);
  90. if(time == 0) {
  91. error = OneWireSlaveError::RESET_IN_PROGRESS;
  92. return false;
  93. }
  94. // wait while bus is high
  95. time = OWET::MSG_HIGH_TIMEOUT;
  96. time = wait_while_gpio_is(time, true);
  97. if(time == 0) {
  98. error = OneWireSlaveError::AWAIT_TIMESLOT_TIMEOUT_HIGH;
  99. return false;
  100. }
  101. // wait a time of zero
  102. time = OWET::READ_MIN;
  103. time = wait_while_gpio_is(time, false);
  104. return (time > 0);
  105. }
  106. bool OneWireSlave::send_bit(bool value) {
  107. const bool write_zero = !value;
  108. // wait while bus is low
  109. OneWiteTimeType time = OWET::SLOT_MAX;
  110. time = wait_while_gpio_is(time, false);
  111. if(time == 0) {
  112. error = OneWireSlaveError::RESET_IN_PROGRESS;
  113. return false;
  114. }
  115. // wait while bus is high
  116. time = OWET::MSG_HIGH_TIMEOUT;
  117. time = wait_while_gpio_is(time, true);
  118. if(time == 0) {
  119. error = OneWireSlaveError::AWAIT_TIMESLOT_TIMEOUT_HIGH;
  120. return false;
  121. }
  122. // choose write time
  123. if(write_zero) {
  124. pin_set_low();
  125. time = OWET::WRITE_ZERO;
  126. } else {
  127. time = OWET::READ_MAX;
  128. }
  129. // hold line for ZERO or ONE time
  130. delay_us(time);
  131. pin_set_float();
  132. return true;
  133. }
  134. bool OneWireSlave::send(const uint8_t* address, const uint8_t data_length) {
  135. uint8_t bytes_sent = 0;
  136. pin_set_float();
  137. // bytes loop
  138. for(; bytes_sent < data_length; ++bytes_sent) {
  139. const uint8_t data_byte = address[bytes_sent];
  140. // bit loop
  141. for(uint8_t bit_mask = 0x01; bit_mask != 0; bit_mask <<= 1) {
  142. if(!send_bit(static_cast<bool>(bit_mask & data_byte))) {
  143. // if we cannot send first bit
  144. if((bit_mask == 0x01) && (error == OneWireSlaveError::AWAIT_TIMESLOT_TIMEOUT_HIGH))
  145. error = OneWireSlaveError::FIRST_BIT_OF_BYTE_TIMEOUT;
  146. return false;
  147. }
  148. }
  149. }
  150. return true;
  151. }
  152. bool OneWireSlave::receive(uint8_t* data, const uint8_t data_length) {
  153. uint8_t bytes_received = 0;
  154. pin_set_float();
  155. for(; bytes_received < data_length; ++bytes_received) {
  156. uint8_t value = 0;
  157. for(uint8_t bit_mask = 0x01; bit_mask != 0; bit_mask <<= 1) {
  158. if(receive_bit()) value |= bit_mask;
  159. }
  160. data[bytes_received] = value;
  161. }
  162. return (bytes_received != data_length);
  163. }
  164. void OneWireSlave::cmd_search_rom(void) {
  165. const uint8_t key_bytes = 8;
  166. uint8_t* key = device->id_storage;
  167. for(uint8_t i = 0; i < key_bytes; i++) {
  168. uint8_t key_byte = key[i];
  169. for(uint8_t j = 0; j < 8; j++) {
  170. bool bit = (key_byte >> j) & 0x01;
  171. if(!send_bit(bit)) return;
  172. if(!send_bit(!bit)) return;
  173. const bool bit_recv = receive_bit();
  174. if(error != OneWireSlaveError::NO_ERROR) return;
  175. }
  176. }
  177. }
  178. bool OneWireSlave::receive_and_process_cmd(void) {
  179. uint8_t cmd;
  180. receive(&cmd, 1);
  181. if(error == OneWireSlaveError::RESET_IN_PROGRESS) return true;
  182. if(error != OneWireSlaveError::NO_ERROR) return false;
  183. switch(cmd) {
  184. case 0xF0:
  185. // SEARCH ROM
  186. cmd_search_rom();
  187. return true;
  188. case 0x33:
  189. // READ ROM
  190. device->send_id();
  191. return false;
  192. default: // Unknown command
  193. error = OneWireSlaveError::INCORRECT_ONEWIRE_CMD;
  194. }
  195. if(error == OneWireSlaveError::RESET_IN_PROGRESS) return true;
  196. return (error == OneWireSlaveError::NO_ERROR);
  197. }
  198. bool OneWireSlave::bus_start(void) {
  199. bool result = true;
  200. if(device == nullptr) {
  201. result = false;
  202. } else {
  203. pin_init_opendrain_in_isr_ctx();
  204. error = OneWireSlaveError::NO_ERROR;
  205. if(show_presence()) {
  206. __disable_irq();
  207. // TODO think about multiple command cycles
  208. bool return_to_reset = receive_and_process_cmd();
  209. result =
  210. (error == OneWireSlaveError::NO_ERROR ||
  211. error == OneWireSlaveError::INCORRECT_ONEWIRE_CMD);
  212. __enable_irq();
  213. } else {
  214. result = false;
  215. }
  216. pin_init_interrupt_in_isr_ctx();
  217. }
  218. return result;
  219. }
  220. void OneWireSlave::exti_callback(void* _pin, void* _ctx) {
  221. // interrupt manager get us pin constant, so...
  222. uint32_t pin = (uint32_t)_pin;
  223. OneWireSlave* _this = static_cast<OneWireSlave*>(_ctx);
  224. if(pin == _this->one_wire_pin_record->pin) {
  225. volatile bool input_state = 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(_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. }
  247. }