one_wire_slave_gpio.cpp 14 KB

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