one_wire_slave_gpio.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. #include "one_wire_slave_gpio.h"
  2. #include "one_wire_device.h"
  3. #include "one_wire_device_ds_1990.h"
  4. // TODO fix GPL compability
  5. // currently we use rework of OneWireHub
  6. static uint32_t __instructions_per_us = 0;
  7. OneWireGpioSlave::OneWireGpioSlave(const GpioPin* one_wire_gpio) {
  8. gpio = one_wire_gpio;
  9. error = OneWireGpioSlaveError::NO_ERROR;
  10. devices_count = 0;
  11. device_selected = nullptr;
  12. for(uint8_t i = 0; i < ONE_WIRE_MAX_DEVICES; ++i) {
  13. devices[i] = nullptr;
  14. }
  15. __instructions_per_us = (SystemCoreClock / 1000000.0f);
  16. }
  17. OneWireGpioSlave::~OneWireGpioSlave() {
  18. stop();
  19. }
  20. void OneWireGpioSlave::start(void) {
  21. gpio_init(gpio, GpioModeOutputOpenDrain);
  22. }
  23. void OneWireGpioSlave::stop(void) {
  24. gpio_init(gpio, GpioModeAnalog);
  25. }
  26. bool OneWireGpioSlave::emulate() {
  27. bool anything_emulated = false;
  28. error = OneWireGpioSlaveError::NO_ERROR;
  29. while(1) {
  30. if(devices_count == 0) return false;
  31. if(!check_reset()) {
  32. return anything_emulated;
  33. } else {
  34. }
  35. // OK, we receive reset
  36. osKernelLock();
  37. if(!show_presence()) {
  38. return anything_emulated;
  39. } else {
  40. anything_emulated = true;
  41. }
  42. // and we succefully show our presence on bus
  43. __disable_irq();
  44. // TODO think about return condition
  45. if(!receive_and_process_cmd()) {
  46. __enable_irq();
  47. osKernelUnlock();
  48. } else {
  49. __enable_irq();
  50. osKernelUnlock();
  51. }
  52. }
  53. }
  54. OneWiteTimeType OneWireGpioSlave::wait_while_gpio_is(OneWiteTimeType time, const bool pin_value) {
  55. uint32_t start = DWT->CYCCNT;
  56. uint32_t time_ticks = time * __instructions_per_us;
  57. uint32_t time_captured;
  58. do {
  59. time_captured = DWT->CYCCNT;
  60. if(gpio_read(gpio) != pin_value) {
  61. OneWiteTimeType remaining_time = time_ticks - (time_captured - start);
  62. remaining_time /= __instructions_per_us;
  63. return remaining_time;
  64. }
  65. } while((time_captured - start) < time_ticks);
  66. return 0;
  67. }
  68. void OneWireGpioSlave::pin_set_float() {
  69. gpio_write(gpio, true);
  70. }
  71. void OneWireGpioSlave::pin_set_low() {
  72. gpio_write(gpio, false);
  73. }
  74. const char* OneWireGpioSlave::decode_error() {
  75. const char* error_text[16] = {
  76. "NO_ERROR",
  77. "READ_TIMESLOT_TIMEOUT",
  78. "WRITE_TIMESLOT_TIMEOUT",
  79. "WAIT_RESET_TIMEOUT",
  80. "VERY_LONG_RESET",
  81. "VERY_SHORT_RESET",
  82. "PRESENCE_LOW_ON_LINE",
  83. "READ_TIMESLOT_TIMEOUT_LOW",
  84. "AWAIT_TIMESLOT_TIMEOUT_HIGH",
  85. "PRESENCE_HIGH_ON_LINE",
  86. "INCORRECT_ONEWIRE_CMD",
  87. "INCORRECT_SLAVE_USAGE",
  88. "TRIED_INCORRECT_WRITE",
  89. "FIRST_TIMESLOT_TIMEOUT",
  90. "FIRST_BIT_OF_BYTE_TIMEOUT",
  91. "RESET_IN_PROGRESS"};
  92. return error_text[static_cast<uint8_t>(error)];
  93. }
  94. uint8_t OneWireGpioSlave::attach(OneWireDevice& device) {
  95. if(devices_count >= ONE_WIRE_MAX_DEVICES) return 255; // hub is full
  96. uint8_t position = 255;
  97. for(uint8_t i = 0; i < ONE_WIRE_MAX_DEVICES; ++i) {
  98. if(devices[i] == &device) {
  99. return i;
  100. }
  101. if((position > ONE_WIRE_MAX_DEVICES) && (devices[i] == nullptr)) {
  102. position = i;
  103. }
  104. }
  105. if(position == 255) return 255;
  106. devices[position] = &device;
  107. devices_count++;
  108. build_id_tree();
  109. return position;
  110. }
  111. bool OneWireGpioSlave::detach(const OneWireDevice& device) {
  112. uint8_t position = 255;
  113. for(uint8_t i = 0; i < ONE_WIRE_MAX_DEVICES; ++i) {
  114. if(devices[i] == &device) {
  115. position = i;
  116. break;
  117. }
  118. }
  119. if(position != 255) return detach(position);
  120. return false;
  121. }
  122. bool OneWireGpioSlave::detach(uint8_t device_number) {
  123. if(devices[device_number] == nullptr) return false;
  124. if(devices_count == 0) return false;
  125. if(device_number >= ONE_WIRE_MAX_DEVICES) return false;
  126. devices[device_number] = nullptr;
  127. devices_count--;
  128. build_id_tree();
  129. return true;
  130. }
  131. uint8_t OneWireGpioSlave::get_next_device_index(const uint8_t index_start) const {
  132. for(uint8_t i = index_start; i < ONE_WIRE_MAX_DEVICES; ++i) {
  133. if(devices[i] != nullptr) return i;
  134. }
  135. return 0;
  136. }
  137. uint8_t OneWireGpioSlave::build_id_tree(void) {
  138. uint32_t device_mask = 0;
  139. uint32_t bit_mask = 0x01;
  140. // build mask
  141. for(uint8_t i = 0; i < ONE_WIRE_MAX_DEVICES; ++i) {
  142. if(devices[i] != nullptr) device_mask |= bit_mask;
  143. bit_mask <<= 1;
  144. }
  145. for(uint8_t i = 0; i < ONE_WIRE_MAX_DEVICES; ++i) {
  146. id_tree[i].id_position = 255;
  147. }
  148. // begin with root-element
  149. build_id_tree(0, device_mask); // goto branch
  150. return 0;
  151. }
  152. uint8_t OneWireGpioSlave::build_id_tree(uint8_t id_bit_position, uint32_t device_mask) {
  153. if(device_mask == 0) return (255);
  154. while(id_bit_position < 64) {
  155. uint32_t mask_pos{0};
  156. uint32_t mask_neg{0};
  157. const uint8_t pos_byte{static_cast<uint8_t>(id_bit_position >> 3)};
  158. const uint8_t mask_bit{static_cast<uint8_t>(1 << (id_bit_position & 7))};
  159. uint32_t mask_id{1};
  160. // searchid_tree through all active slaves
  161. for(uint8_t id = 0; id < ONE_WIRE_MAX_DEVICES; ++id) {
  162. if((device_mask & mask_id) != 0) {
  163. // if slave is in mask differentiate the bitValue
  164. if((devices[id]->id_storage[pos_byte] & mask_bit) != 0)
  165. mask_pos |= mask_id;
  166. else
  167. mask_neg |= mask_id;
  168. }
  169. mask_id <<= 1;
  170. }
  171. if((mask_neg != 0) && (mask_pos != 0)) {
  172. // there was found a junction
  173. const uint8_t active_element = get_first_id_tree_el_position();
  174. id_tree[active_element].id_position = id_bit_position;
  175. id_tree[active_element].device_selected = get_first_bit_set_position(device_mask);
  176. id_bit_position++;
  177. id_tree[active_element].got_one = build_id_tree(id_bit_position, mask_pos);
  178. id_tree[active_element].got_zero = build_id_tree(id_bit_position, mask_neg);
  179. return active_element;
  180. }
  181. id_bit_position++;
  182. }
  183. // gone through the address, store this result
  184. uint8_t active_element = get_first_id_tree_el_position();
  185. id_tree[active_element].id_position = 128;
  186. id_tree[active_element].device_selected = get_first_bit_set_position(device_mask);
  187. id_tree[active_element].got_one = 255;
  188. id_tree[active_element].got_zero = 255;
  189. return active_element;
  190. }
  191. uint8_t OneWireGpioSlave::get_first_bit_set_position(uint32_t mask) const {
  192. uint32_t _mask = mask;
  193. for(uint8_t i = 0; i < ONE_WIRE_MAX_DEVICES; ++i) {
  194. if((_mask & 1) != 0) return i;
  195. _mask >>= 1;
  196. }
  197. return 0;
  198. }
  199. uint8_t OneWireGpioSlave::get_first_id_tree_el_position(void) const {
  200. for(uint8_t i = 0; i < ONE_WIRE_MAX_DEVICES; ++i) {
  201. if(id_tree[i].id_position == 255) return i;
  202. }
  203. return 0;
  204. }
  205. void OneWireGpioSlave::cmd_search_rom(void) {
  206. uint8_t id_bit_position = 0;
  207. uint8_t trigger_position = 0;
  208. uint8_t active_slave = id_tree[trigger_position].device_selected;
  209. uint8_t trigger_bit = id_tree[trigger_position].id_position;
  210. while(id_bit_position < 64) {
  211. // if junction is reached, act different
  212. if(id_bit_position == trigger_bit) {
  213. if(!send_bit(false)) return;
  214. if(!send_bit(false)) return;
  215. const bool bit_recv = receive_bit();
  216. if(error != OneWireGpioSlaveError::NO_ERROR) return;
  217. // switch to next junction
  218. trigger_position = bit_recv ? id_tree[trigger_position].got_one :
  219. id_tree[trigger_position].got_zero;
  220. active_slave = id_tree[trigger_position].device_selected;
  221. trigger_bit = (trigger_position == 255) ? uint8_t(255) :
  222. id_tree[trigger_position].id_position;
  223. } else {
  224. const uint8_t pos_byte = (id_bit_position >> 3);
  225. const uint8_t mask_bit = (static_cast<uint8_t>(1) << (id_bit_position & (7)));
  226. bool bit_send;
  227. if((devices[active_slave]->id_storage[pos_byte] & mask_bit) != 0) {
  228. bit_send = true;
  229. if(!send_bit(true)) return;
  230. if(!send_bit(false)) return;
  231. } else {
  232. bit_send = false;
  233. if(!send_bit(false)) return;
  234. if(!send_bit(true)) return;
  235. }
  236. const bool bit_recv = receive_bit();
  237. if(error != OneWireGpioSlaveError::NO_ERROR) return;
  238. if(bit_send != bit_recv) return;
  239. }
  240. id_bit_position++;
  241. }
  242. device_selected = devices[active_slave];
  243. }
  244. bool OneWireGpioSlave::check_reset(void) {
  245. pin_set_float();
  246. if(error == OneWireGpioSlaveError::RESET_IN_PROGRESS) {
  247. error = OneWireGpioSlaveError::NO_ERROR;
  248. if(wait_while_gpio_is(
  249. OWET::RESET_MIN[overdrive_mode] - OWET::SLOT_MAX[overdrive_mode] -
  250. OWET::READ_MAX[overdrive_mode],
  251. false) == 0) {
  252. // we want to show_presence on high, so wait for it
  253. const OneWiteTimeType time_remaining = wait_while_gpio_is(OWET::RESET_MAX[0], false);
  254. if(overdrive_mode &&
  255. ((OWET::RESET_MAX[0] - OWET::RESET_MIN[overdrive_mode]) > time_remaining)) {
  256. overdrive_mode = false;
  257. };
  258. return true;
  259. }
  260. }
  261. // if line is low, then just leave
  262. if(gpio_read(gpio) == 0) {
  263. return false;
  264. }
  265. // wait while gpio is high
  266. if(wait_while_gpio_is(OWET::RESET_TIMEOUT, true) == 0) {
  267. return false;
  268. }
  269. // store low time
  270. OneWiteTimeType time_remaining = wait_while_gpio_is(OWET::RESET_MAX[0], false);
  271. // low time more than RESET_MAX time
  272. if(time_remaining == 0) {
  273. error = OneWireGpioSlaveError::VERY_LONG_RESET;
  274. return false;
  275. }
  276. // get real reset time
  277. time_remaining = OWET::RESET_MAX[0] - time_remaining;
  278. // if time, while bus was low, fit in standart reset timings
  279. if(overdrive_mode && ((OWET::RESET_MAX[0] - OWET::RESET_MIN[0]) <= time_remaining)) {
  280. // normal reset detected
  281. overdrive_mode = false;
  282. };
  283. bool result = (time_remaining <= OWET::RESET_MAX[0]) &&
  284. time_remaining >= OWET::RESET_MIN[overdrive_mode];
  285. return result;
  286. }
  287. bool OneWireGpioSlave::show_presence(void) {
  288. // wait while master delay presence check
  289. wait_while_gpio_is(OWET::PRESENCE_TIMEOUT, true);
  290. // show presence
  291. pin_set_low();
  292. delay_us(OWET::PRESENCE_MIN[overdrive_mode]);
  293. pin_set_float();
  294. // somebody also can show presence
  295. const OneWiteTimeType wait_low_time =
  296. OWET::PRESENCE_MAX[overdrive_mode] - OWET::PRESENCE_MIN[overdrive_mode];
  297. // so we will wait
  298. if(wait_while_gpio_is(wait_low_time, false) == 0) {
  299. error = OneWireGpioSlaveError::PRESENCE_LOW_ON_LINE;
  300. return false;
  301. }
  302. return true;
  303. }
  304. bool OneWireGpioSlave::receive_and_process_cmd(void) {
  305. receive(&cmd);
  306. if(error == OneWireGpioSlaveError::RESET_IN_PROGRESS) return true;
  307. if(error != OneWireGpioSlaveError::NO_ERROR) return false;
  308. switch(cmd) {
  309. case 0xF0:
  310. // SEARCH ROM
  311. device_selected = nullptr;
  312. cmd_search_rom();
  313. // trigger reinit
  314. return true;
  315. case 0x33:
  316. // READ ROM
  317. // work only when one slave on the bus
  318. if((device_selected == nullptr) && (devices_count == 1)) {
  319. device_selected = devices[get_next_device_index()];
  320. }
  321. if(device_selected != nullptr) {
  322. device_selected->send_id(this);
  323. }
  324. return false;
  325. default: // Unknown command
  326. error = OneWireGpioSlaveError::INCORRECT_ONEWIRE_CMD;
  327. //error_cmd = cmd;
  328. }
  329. if(error == OneWireGpioSlaveError::RESET_IN_PROGRESS) return true;
  330. return (error == OneWireGpioSlaveError::NO_ERROR);
  331. }
  332. bool OneWireGpioSlave::receive_bit(void) {
  333. // wait while bus is low
  334. OneWiteTimeType time = OWET::SLOT_MAX[overdrive_mode];
  335. time = wait_while_gpio_is(time, false);
  336. if(time == 0) {
  337. error = OneWireGpioSlaveError::RESET_IN_PROGRESS;
  338. return false;
  339. }
  340. // wait while bus is high
  341. time = OWET::MSG_HIGH_TIMEOUT;
  342. time = wait_while_gpio_is(time, true);
  343. if(time == 0) {
  344. error = OneWireGpioSlaveError::AWAIT_TIMESLOT_TIMEOUT_HIGH;
  345. error_place = 1;
  346. return false;
  347. }
  348. // wait a time of zero
  349. time = OWET::READ_MIN[overdrive_mode];
  350. time = wait_while_gpio_is(time, false);
  351. return (time > 0);
  352. }
  353. bool OneWireGpioSlave::send_bit(bool value) {
  354. const bool write_zero = !value;
  355. // wait while bus is low
  356. OneWiteTimeType time = OWET::SLOT_MAX[overdrive_mode];
  357. time = wait_while_gpio_is(time, false);
  358. if(time == 0) {
  359. error = OneWireGpioSlaveError::RESET_IN_PROGRESS;
  360. return false;
  361. }
  362. // wait while bus is high
  363. time = OWET::MSG_HIGH_TIMEOUT;
  364. time = wait_while_gpio_is(time, true);
  365. if(time == 0) {
  366. error = OneWireGpioSlaveError::AWAIT_TIMESLOT_TIMEOUT_HIGH;
  367. error_place = 2;
  368. return false;
  369. }
  370. // choose write time
  371. if(write_zero) {
  372. pin_set_low();
  373. time = OWET::WRITE_ZERO[overdrive_mode];
  374. } else {
  375. time = OWET::READ_MAX[overdrive_mode];
  376. }
  377. // hold line for ZERO or ONE time
  378. delay_us(time);
  379. pin_set_float();
  380. return true;
  381. }
  382. bool OneWireGpioSlave::send(const uint8_t* address, const uint8_t data_length) {
  383. uint8_t bytes_sent = 0;
  384. pin_set_float();
  385. // bytes loop
  386. for(; bytes_sent < data_length; ++bytes_sent) {
  387. const uint8_t data_byte = address[bytes_sent];
  388. // bit loop
  389. for(uint8_t bit_mask = 0x01; bit_mask != 0; bit_mask <<= 1) {
  390. if(!send_bit(static_cast<bool>(bit_mask & data_byte))) {
  391. // if we cannot send first bit
  392. if((bit_mask == 0x01) &&
  393. (error == OneWireGpioSlaveError::AWAIT_TIMESLOT_TIMEOUT_HIGH))
  394. error = OneWireGpioSlaveError::FIRST_BIT_OF_BYTE_TIMEOUT;
  395. return false;
  396. }
  397. }
  398. }
  399. return true;
  400. }
  401. bool OneWireGpioSlave::receive(uint8_t* data, const uint8_t data_length) {
  402. uint8_t bytes_received = 0;
  403. pin_set_float();
  404. for(; bytes_received < data_length; ++bytes_received) {
  405. uint8_t value = 0;
  406. for(uint8_t bit_mask = 0x01; bit_mask != 0; bit_mask <<= 1) {
  407. if(receive_bit()) value |= bit_mask;
  408. }
  409. data[bytes_received] = value;
  410. }
  411. return (bytes_received != data_length);
  412. }