one_wire_slave.cpp 8.5 KB

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