synth_searchrom.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. ChibiOS/RT - Copyright (C) 2014 Uladzimir Pylinsky aka barthess
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include <stdlib.h>
  14. /*
  15. ******************************************************************************
  16. * DEFINES
  17. ******************************************************************************
  18. */
  19. /* do not set it more than 64 because of some fill_pattern functions
  20. will be broken.*/
  21. #define SYNTH_DEVICES_MAX 64
  22. /*
  23. * synthetic device
  24. */
  25. typedef struct {
  26. bool active;
  27. uint64_t id;
  28. } OWSynthDevice;
  29. /*
  30. * synthetic bus
  31. */
  32. typedef struct {
  33. OWSynthDevice devices[SYNTH_DEVICES_MAX];
  34. size_t dev_present;
  35. bool complement_bit;
  36. ioline_t rom_bit;
  37. } OWSynthBus;
  38. /*
  39. ******************************************************************************
  40. * EXTERNS
  41. ******************************************************************************
  42. */
  43. /*
  44. ******************************************************************************
  45. * PROTOTYPES
  46. ******************************************************************************
  47. */
  48. /*
  49. ******************************************************************************
  50. * GLOBAL VARIABLES
  51. ******************************************************************************
  52. */
  53. static OWSynthBus synth_bus;
  54. /*
  55. * local buffer for discovered ROMs
  56. */
  57. static uint64_t detected_devices[SYNTH_DEVICES_MAX];
  58. /*
  59. ******************************************************************************
  60. ******************************************************************************
  61. * LOCAL FUNCTIONS
  62. ******************************************************************************
  63. ******************************************************************************
  64. */
  65. /*
  66. ******************************************************************************
  67. * EXPORTED FUNCTIONS
  68. ******************************************************************************
  69. */
  70. /*
  71. *
  72. */
  73. void _synth_ow_write_bit(onewireDriver *owp, ioline_t bit) {
  74. (void)owp;
  75. size_t i;
  76. for (i=0; i<SYNTH_DEVICES_MAX; i++) {
  77. if (((synth_bus.devices[i].id >> synth_bus.rom_bit) & 1U) != bit) {
  78. synth_bus.devices[i].active = false;
  79. }
  80. }
  81. synth_bus.rom_bit++;
  82. }
  83. /*
  84. *
  85. */
  86. ioline_t _synth_ow_read_bit(void) {
  87. ioline_t ret = 0xFF;
  88. size_t i;
  89. ioline_t bit;
  90. for (i=0; i<SYNTH_DEVICES_MAX; i++) {
  91. if (synth_bus.devices[i].active){
  92. bit = (synth_bus.devices[i].id >> synth_bus.rom_bit) & 1U;
  93. if (synth_bus.complement_bit){
  94. bit ^= 1U;
  95. }
  96. if (0xFF == ret)
  97. ret = bit;
  98. else
  99. ret &= bit;
  100. }
  101. }
  102. synth_bus.complement_bit = !synth_bus.complement_bit;
  103. return ret;
  104. }
  105. /*
  106. *
  107. */
  108. static void synth_reset_pulse(void){
  109. size_t i;
  110. for (i=0; i<synth_bus.dev_present; i++){
  111. synth_bus.devices[i].active = true;
  112. }
  113. }
  114. /*
  115. *
  116. */
  117. static size_t synth_search_rom(onewireDriver *owp, uint8_t *result, size_t max_rom_cnt) {
  118. size_t i;
  119. search_clean_start(&owp->search_rom);
  120. do {
  121. /* initialize buffer to store result */
  122. if (owp->search_rom.reg.devices_found >= max_rom_cnt)
  123. owp->search_rom.retbuf = result + 8*(max_rom_cnt-1);
  124. else
  125. owp->search_rom.retbuf = result + 8*owp->search_rom.reg.devices_found;
  126. memset(owp->search_rom.retbuf, 0, 8);
  127. /* clean iteration state */
  128. search_clean_iteration(&owp->search_rom);
  129. /**/
  130. synth_reset_pulse();
  131. synth_bus.rom_bit = 0;
  132. synth_bus.complement_bit = false;
  133. for (i=0; i<64*3 - 1; i++){
  134. ow_search_rom_cb(NULL, owp);
  135. }
  136. if (ONEWIRE_SEARCH_ROM_ERROR != owp->search_rom.reg.result) {
  137. /* store cached result for usage in next iteration */
  138. memcpy(owp->search_rom.prev_path, owp->search_rom.retbuf, 8);
  139. }
  140. }
  141. while (ONEWIRE_SEARCH_ROM_SUCCESS == owp->search_rom.reg.result);
  142. /**/
  143. if (ONEWIRE_SEARCH_ROM_ERROR == owp->search_rom.reg.result)
  144. return 0;
  145. else
  146. return owp->search_rom.reg.devices_found;
  147. }
  148. /*
  149. *
  150. */
  151. static void fill_pattern_real_devices(void) {
  152. size_t i;
  153. for (i=0; i<SYNTH_DEVICES_MAX; i++)
  154. synth_bus.devices[i].active = false;
  155. synth_bus.devices[0].active = true;
  156. synth_bus.devices[0].id = 0x1d00000567f5ec28;
  157. synth_bus.devices[1].active = true;
  158. synth_bus.devices[1].id = 0x37000005601abd28;
  159. synth_bus.devices[2].active = true;
  160. synth_bus.devices[2].id = 0x0f000005677d8328;
  161. }
  162. /*
  163. *
  164. */
  165. static void fill_pattern_00(size_t devices, size_t start) {
  166. size_t i;
  167. for (i=0; i<SYNTH_DEVICES_MAX; i++)
  168. synth_bus.devices[i].active = false;
  169. for (i=0; i<devices; i++){
  170. synth_bus.devices[i].active = true;
  171. synth_bus.devices[i].id = (start + i);
  172. }
  173. }
  174. /*
  175. *
  176. */
  177. static void fill_pattern_01(size_t devices) {
  178. size_t i;
  179. for (i=0; i<SYNTH_DEVICES_MAX; i++)
  180. synth_bus.devices[i].active = false;
  181. for (i=0; i<devices; i++){
  182. synth_bus.devices[i].active = true;
  183. synth_bus.devices[i].id = (devices - i);
  184. }
  185. }
  186. /*
  187. *
  188. */
  189. static void fill_pattern_02(size_t devices) {
  190. size_t i;
  191. for (i=0; i<SYNTH_DEVICES_MAX; i++)
  192. synth_bus.devices[i].active = false;
  193. for (i=0; i<devices; i++){
  194. synth_bus.devices[i].active = true;
  195. synth_bus.devices[i].id = ((uint64_t)1 << i);
  196. }
  197. }
  198. /*
  199. *
  200. */
  201. static void fill_pattern_03(size_t devices) {
  202. size_t i;
  203. for (i=0; i<SYNTH_DEVICES_MAX; i++)
  204. synth_bus.devices[i].active = false;
  205. for (i=0; i<devices; i++){
  206. synth_bus.devices[i].active = true;
  207. synth_bus.devices[i].id = ((uint64_t)0x8000000000000000 >> i);
  208. }
  209. }
  210. /*
  211. * Random pattern helper
  212. */
  213. static bool is_id_uniq(const OWSynthDevice *dev, size_t n, uint64_t id) {
  214. size_t i;
  215. for (i=0; i<n; i++) {
  216. if (dev[i].id == id)
  217. return false;
  218. }
  219. return true;
  220. }
  221. /*
  222. *
  223. */
  224. static void fill_pattern_rand(size_t devices) {
  225. size_t i;
  226. uint64_t new_id;
  227. for (i=0; i<SYNTH_DEVICES_MAX; i++){
  228. synth_bus.devices[i].active = false;
  229. synth_bus.devices[i].id = 0;
  230. }
  231. for (i=0; i<devices; i++) {
  232. do {
  233. new_id = rand();
  234. new_id = (new_id << 32) | rand();
  235. } while (true != is_id_uniq(synth_bus.devices, i, new_id));
  236. synth_bus.devices[i].id = new_id;
  237. synth_bus.devices[i].active = true;
  238. }
  239. }
  240. /*
  241. *
  242. */
  243. static bool check_result(size_t detected) {
  244. size_t i,j;
  245. bool match = false;
  246. for (i=0; i<detected; i++){
  247. match = false;
  248. for (j=0; j<detected; j++){
  249. if (synth_bus.devices[i].id == detected_devices[j]){
  250. match = true;
  251. break;
  252. }
  253. }
  254. if (false == match)
  255. return OSAL_FAILED;
  256. }
  257. return OSAL_SUCCESS;
  258. }
  259. /*
  260. *
  261. */
  262. void synthSearchRomTest(onewireDriver *owp) {
  263. size_t detected = 0;
  264. size_t i;
  265. synth_bus.dev_present = 3;
  266. fill_pattern_real_devices();
  267. detected = synth_search_rom(owp, (uint8_t *)detected_devices, SYNTH_DEVICES_MAX);
  268. osalDbgCheck(synth_bus.dev_present == detected);
  269. osalDbgCheck(OSAL_SUCCESS == check_result(detected));
  270. for (i=1; i<=SYNTH_DEVICES_MAX; i++){
  271. synth_bus.dev_present = i;
  272. fill_pattern_00(synth_bus.dev_present, 0);
  273. detected = synth_search_rom(owp, (uint8_t *)detected_devices, SYNTH_DEVICES_MAX);
  274. osalDbgCheck(synth_bus.dev_present == detected);
  275. osalDbgCheck(OSAL_SUCCESS == check_result(detected));
  276. fill_pattern_00(synth_bus.dev_present, 1);
  277. detected = synth_search_rom(owp, (uint8_t *)detected_devices, SYNTH_DEVICES_MAX);
  278. osalDbgCheck(synth_bus.dev_present == detected);
  279. osalDbgCheck(OSAL_SUCCESS == check_result(detected));
  280. fill_pattern_01(synth_bus.dev_present);
  281. detected = synth_search_rom(owp, (uint8_t *)detected_devices, SYNTH_DEVICES_MAX);
  282. osalDbgCheck(synth_bus.dev_present == detected);
  283. osalDbgCheck(OSAL_SUCCESS == check_result(detected));
  284. fill_pattern_02(synth_bus.dev_present);
  285. detected = synth_search_rom(owp, (uint8_t *)detected_devices, SYNTH_DEVICES_MAX);
  286. osalDbgCheck(synth_bus.dev_present == detected);
  287. osalDbgCheck(OSAL_SUCCESS == check_result(detected));
  288. fill_pattern_03(synth_bus.dev_present);
  289. detected = synth_search_rom(owp, (uint8_t *)detected_devices, SYNTH_DEVICES_MAX);
  290. osalDbgCheck(synth_bus.dev_present == detected);
  291. osalDbgCheck(OSAL_SUCCESS == check_result(detected));
  292. }
  293. i = 0;
  294. while (i < 1000) {
  295. synth_bus.dev_present = 1 + (rand() & 63);
  296. fill_pattern_rand(synth_bus.dev_present);
  297. detected = synth_search_rom(owp, (uint8_t *)detected_devices, SYNTH_DEVICES_MAX);
  298. osalDbgCheck(synth_bus.dev_present == detected);
  299. osalDbgCheck(OSAL_SUCCESS == check_result(detected));
  300. i++;
  301. }
  302. }