one_wire.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /**
  2. * @file module_one_wire.c
  3. * @author Juraj Dudak
  4. * @date 16 January, 2015
  5. * @version 1.0
  6. *
  7. * @brief Implementacia funkcii pre 1-Wire zbernicu.
  8. *
  9. */
  10. //----------------------------------------------------------------------
  11. // Include & define
  12. //----------------------------------------------------------------------
  13. #include "one_wire.h"
  14. #define TRUE 0x01 // konstanty v kniznici 1-wire
  15. #define FALSE 0x00
  16. /** @addtogroup Firmware
  17. * @{
  18. */
  19. /** @addtogroup Implementácia_MultiSlave
  20. * @{
  21. */
  22. /** @defgroup OneWire_modul
  23. * @brief Implementácia komunikácie na zbernici 1-wire
  24. * @{
  25. */
  26. //----------------------------------------------------------------------
  27. // Lokalne premenne
  28. //----------------------------------------------------------------------
  29. /**
  30. * SW driver pre zbernicu 1-wire. Obsahuje dátový buffer, časové konštanty,
  31. * príznaky komunikácie a stavové premenné.
  32. */
  33. oneWireDriver_typeDef oneWireDrv;
  34. /** Definícia pinov pre ovládanie zbernice 1-wire*/
  35. oneWirePin_typeDef oneWirePins;
  36. /** globalna hodnota CRC */
  37. uint8_t crc8_ow;
  38. /** Tabuľka pre výpočet súčtu CRC8 */
  39. static const uint8_t dscrc_table[] = {
  40. 0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65,
  41. 157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220,
  42. 35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98,
  43. 190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255,
  44. 70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7,
  45. 219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154,
  46. 101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36,
  47. 248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185,
  48. 140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205,
  49. 17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80,
  50. 175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 238,
  51. 50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115,
  52. 202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139,
  53. 87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22,
  54. 233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168,
  55. 116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53
  56. };
  57. //--------------------------------------------------------------------------
  58. // Implementacia low-level funkcii pre STM32
  59. //--------------------------------------------------------------------------
  60. /**
  61. * Nastavi pin pre 1W ako vstupný.
  62. * Aktuálne je podporovaná rýchla zmena len pre STM32F0x
  63. * Opis registrov: http://www.hertaville.com/stm32f0-gpio-tutorial-part-1.html
  64. */
  65. __attribute__((optimize("O1"))) static void setPinIn(){
  66. #ifdef STM32F0
  67. #define PIN_IN_USE_IN
  68. uint32_t temp = oneWirePins.port->MODER;
  69. CLEAR_BIT(temp, GPIO_MODER_MODER0 << (oneWirePins.pin_ow_moder_bit));
  70. SET_BIT(temp, (0) << (oneWirePins.pin_ow_moder_bit));
  71. oneWirePins.port->MODER = temp;
  72. #endif // STM32F0
  73. #ifdef STM32L0
  74. #define PIN_IN_USE_IN
  75. uint32_t temp = oneWirePins.port->MODER;
  76. CLEAR_BIT(temp, GPIO_MODER_MODE0 << (oneWirePins.pin_ow_moder_bit));
  77. SET_BIT(temp, (0) << (oneWirePins.pin_ow_moder_bit));
  78. oneWirePins.port->MODER = temp;
  79. #endif // STM32L0
  80. #ifndef PIN_IN_USE_IN
  81. #error aktualne MCU nie je podporovane
  82. #endif // PIN_IN_USE_IN
  83. }
  84. /**
  85. * Nastavi pin pre 1W ako výstupný.
  86. * Aktuálne je podporovaná rýchla zmena len pre STM32F0x
  87. * Opis registrov: http://www.hertaville.com/stm32f0-gpio-tutorial-part-1.html
  88. */
  89. __attribute__((optimize("O1"))) static void setPinOut() {
  90. #ifdef STM32F0
  91. #define PIN_IN_USE_IN
  92. uint32_t temp = oneWirePins.port->MODER;
  93. CLEAR_BIT(temp, GPIO_MODER_MODER0 << (oneWirePins.pin_ow_moder_bit));
  94. SET_BIT(temp, (1) << (oneWirePins.pin_ow_moder_bit));
  95. oneWirePins.port->MODER = temp;
  96. #endif // STM32F0
  97. #ifdef STM32L0
  98. #define PIN_IN_USE_IN
  99. uint32_t temp = oneWirePins.port->MODER;
  100. CLEAR_BIT(temp, GPIO_MODER_MODE0 << (oneWirePins.pin_ow_moder_bit));
  101. SET_BIT(temp, (1) << (oneWirePins.pin_ow_moder_bit));
  102. oneWirePins.port->MODER = temp;
  103. #endif // STM32L0
  104. #ifndef PIN_IN_USE_IN
  105. #error aktualne MCU nie je podporovane
  106. #endif // STM32F0
  107. }
  108. //----------------------------------------------------------------------
  109. // Implementacia high-level funkcii podla Maxim, AN187
  110. //----------------------------------------------------------------------
  111. /**
  112. * @brief Inicializacia drivera zbernice 1-wire
  113. * - Nastavia sa časové konštanty na preddefinované hodnoty.
  114. * - Nastavia sa piny pre zbernicu One-wire
  115. *
  116. * @param timer časovač použirý an generovanie:
  117. * - blokujúcej pauzy v ráde us
  118. * @param GPIOx port pre piny
  119. * @param GPIO_Pin pin pre zápis na zbernicu pre zápis/čítanie
  120. * @return none
  121. */
  122. void oneWire_init(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin ) {
  123. oneWireDrv.one_wire_timing_settings = OW_SETTING_NORMAL;
  124. oneWirePins.port = GPIOx;
  125. oneWirePins.pin_ow = GPIO_Pin;
  126. uint16_t tmp = oneWirePins.pin_ow;
  127. oneWirePins.pin_ow_moder_bit = 0;
  128. while(tmp){
  129. oneWirePins.pin_ow_moder_bit++;
  130. tmp>>=1;
  131. }
  132. oneWirePins.pin_ow_moder_bit--;
  133. oneWirePins.pin_ow_moder_bit*=2;
  134. setPinOut();
  135. OW_SET_HI;
  136. *(oneWireDrv.ROM_NO) = (uint32_t)0;
  137. *(oneWireDrv.ROM_NO+4) = (uint32_t)0;
  138. }
  139. /**
  140. * Vráti pointer na ROM_NO.
  141. * @retval pointer na ROM_NO
  142. */
  143. uint8_t *oneWire_getROM() {
  144. return oneWireDrv.ROM_NO;
  145. }
  146. /**
  147. * @brief Find the 'first' devices on the 1-Wire bus
  148. * @param none
  149. * @retval TRUE - device found, ROM number in ROM_NO buffer
  150. * FALSE - no device present
  151. */
  152. uint8_t owFirst() {
  153. // reset the search state
  154. oneWireDrv.LastDiscrepancy = 0;
  155. oneWireDrv.LastDeviceFlag = FALSE;
  156. oneWireDrv.LastFamilyDiscrepancy = 0;
  157. return owSearch();
  158. }
  159. /**
  160. * @brief Find the 'next' devices on the 1-Wire bus
  161. * @param none
  162. * @retval : TRUE - device found, ROM number in ROM_NO buffer
  163. * FALSE : device not found, end of search
  164. */
  165. uint8_t owNext() {
  166. return owSearch();
  167. }
  168. void owSelect_ROM(const uint8_t rom[8]) {
  169. uint8_t i;
  170. owWriteByte(OW_CMD_MATCH_ROM, OW_STRONG_OFF); // Choose ROM
  171. for (i = 0; i < 8; i++) {
  172. owWriteByte(rom[i], OW_STRONG_OFF);
  173. }
  174. }
  175. void owSelect() {
  176. OW_DIS_INTERRUPTS;
  177. uint8_t i;
  178. if (owReset()) {
  179. owWriteByte(OW_CMD_MATCH_ROM, OW_STRONG_OFF);
  180. for(i=0; i<8; i++) {
  181. owWriteByte(oneWireDrv.ROM_NO[i], OW_STRONG_OFF);
  182. }
  183. }
  184. OW_ENA_INTERRUPTS;
  185. }
  186. /**
  187. * Skopíruje obsah ROM.
  188. * @param mem_page pole bytov[8], kde bude uložený obsah ROM_NO
  189. */
  190. void owGetROM(uint8_t *mem_page) {
  191. memcpy(mem_page, oneWireDrv.ROM_NO, 8);
  192. }
  193. /**
  194. * @brief Perform the 1-Wire Search Algorithm on the 1-Wire bus
  195. * using the existing search state.
  196. * @param none
  197. * @retval : TRUE - device found, ROM number in ROM_NO buffer
  198. * FALSE : device not found, end of search
  199. */
  200. uint8_t owSearch() {
  201. OW_DIS_INTERRUPTS;
  202. uint32_t id_bit_number;
  203. uint32_t last_zero, rom_byte_number, search_result;
  204. uint32_t id_bit, cmp_id_bit;
  205. uint8_t rom_byte_mask, search_direction;
  206. // initialize for search
  207. id_bit_number = 1;
  208. last_zero = 0;
  209. rom_byte_number = 0;
  210. rom_byte_mask = 1;
  211. search_result = 0;
  212. crc8_ow = 0;
  213. // if the last call was not the last one
  214. if (oneWireDrv.LastDeviceFlag == FALSE) {
  215. // 1-Wire reset
  216. if (!owReset()) {
  217. // reset the search
  218. oneWireDrv.LastDiscrepancy = 0;
  219. oneWireDrv.LastDeviceFlag = FALSE;
  220. oneWireDrv.LastFamilyDiscrepancy = 0;
  221. OW_ENA_INTERRUPTS;
  222. return FALSE;
  223. }
  224. // issue the search command
  225. owWriteByte(OW_CMD_SEARCH_ROM, OW_STRONG_OFF);
  226. // loop to do the search
  227. do {
  228. // read a bit and its complement
  229. id_bit = owReadBit();
  230. cmp_id_bit = owReadBit();
  231. // check for no devices on 1-wire
  232. if ((id_bit == 1) && (cmp_id_bit == 1)) {
  233. break;
  234. } else {
  235. // all devices coupled have 0 or 1
  236. if (id_bit != cmp_id_bit) {
  237. search_direction = id_bit; // bit write value for search
  238. } else {
  239. // if this discrepancy if before the Last Discrepancy
  240. // on a previous next then pick the same as last time
  241. if (id_bit_number < oneWireDrv.LastDiscrepancy)
  242. search_direction = ((oneWireDrv.ROM_NO[rom_byte_number] & rom_byte_mask) > 0);
  243. else
  244. // if equal to last pick 1, if not then pick 0
  245. search_direction = (id_bit_number == oneWireDrv.LastDiscrepancy);
  246. // if 0 was picked then record its position in LastZero
  247. if (search_direction == 0) {
  248. last_zero = id_bit_number;
  249. // check for Last discrepancy in family
  250. if (last_zero < 9)
  251. oneWireDrv.LastFamilyDiscrepancy = last_zero;
  252. }
  253. }
  254. // set or clear the bit in the ROM byte rom_byte_number
  255. // with mask rom_byte_mask
  256. if (search_direction == 1)
  257. oneWireDrv.ROM_NO[rom_byte_number] |= rom_byte_mask;
  258. else
  259. oneWireDrv.ROM_NO[rom_byte_number] &= ~rom_byte_mask;
  260. // serial number search direction write bit
  261. owWriteBit(&search_direction);
  262. // increment the byte counter id_bit_number
  263. // and shift the mask rom_byte_mask
  264. id_bit_number++;
  265. rom_byte_mask <<= 1;
  266. // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
  267. if (rom_byte_mask == 0) {
  268. owCrc8(oneWireDrv.ROM_NO[rom_byte_number]); // accumulate the CRC
  269. rom_byte_number++;
  270. rom_byte_mask = 1;
  271. }
  272. }
  273. } while (rom_byte_number < 8); // loop until through all ROM bytes 0-7
  274. // if the search was successful then
  275. if (!((id_bit_number < 65) || (crc8_ow != 0))) {
  276. // search successful so set LastDiscrepancy,LastDeviceFlag,search_result
  277. oneWireDrv.LastDiscrepancy = last_zero;
  278. // check for last device
  279. if (oneWireDrv.LastDiscrepancy == 0)
  280. oneWireDrv.LastDeviceFlag = TRUE;
  281. search_result = TRUE;
  282. }
  283. }
  284. // if no device found then reset counters so next 'search' will be like a first
  285. if (!search_result || !oneWireDrv.ROM_NO[0]) {
  286. oneWireDrv.LastDiscrepancy = 0;
  287. oneWireDrv.LastDeviceFlag = FALSE;
  288. oneWireDrv.LastFamilyDiscrepancy = 0;
  289. search_result = FALSE;
  290. }
  291. if(search_result){
  292. // owSelect();
  293. }
  294. OW_ENA_INTERRUPTS;
  295. return search_result;
  296. }
  297. /**
  298. * @brief Verify the device with the ROM number in ROM_NO buffer is present.
  299. * @param none
  300. * @return : TRUE : device verified present
  301. * FALSE : device not present
  302. */
  303. uint8_t owVerify() {
  304. OW_DIS_INTERRUPTS;
  305. unsigned char rom_backup[8];
  306. int i, rslt, ld_backup, ldf_backup, lfd_backup;
  307. // keep a backup copy of the current state
  308. for (i = 0; i < 8; i++)
  309. rom_backup[i] = oneWireDrv.ROM_NO[i];
  310. ld_backup = oneWireDrv.LastDiscrepancy;
  311. ldf_backup = oneWireDrv.LastDeviceFlag;
  312. lfd_backup = oneWireDrv.LastFamilyDiscrepancy;
  313. // set search to find the same device
  314. oneWireDrv.LastDiscrepancy = 64;
  315. oneWireDrv.LastDeviceFlag = FALSE;
  316. if (owSearch()) {
  317. // check if same device found
  318. rslt = TRUE;
  319. for (i = 0; i < 8; i++) {
  320. if (rom_backup[i] != oneWireDrv.ROM_NO[i]) {
  321. rslt = FALSE;
  322. break;
  323. }
  324. }
  325. } else
  326. rslt = FALSE;
  327. // restore the search state
  328. for (i = 0; i < 8; i++){
  329. oneWireDrv.ROM_NO[i] = rom_backup[i];
  330. }
  331. oneWireDrv.LastDiscrepancy = ld_backup;
  332. oneWireDrv.LastDeviceFlag = ldf_backup;
  333. oneWireDrv.LastFamilyDiscrepancy = lfd_backup;
  334. if(rslt == TRUE) {
  335. owSelect();
  336. }
  337. OW_ENA_INTERRUPTS;
  338. // return the result of the verify
  339. return rslt;
  340. }
  341. /**
  342. * @brief Setup the search to find the device type 'family_code' on the next call
  343. * to owNext() if it is present.
  344. * @param none
  345. * @retval : none
  346. */
  347. void owTargetSetup(uint8_t family_code) {
  348. // set the search state to find SearchFamily type devices
  349. oneWireDrv.ROM_NO[0] = family_code;
  350. oneWireDrv.ROM_NO[1] = 0;
  351. oneWireDrv.ROM_NO[2] = 0;
  352. oneWireDrv.ROM_NO[3] = 0;
  353. oneWireDrv.ROM_NO[4] = 0;
  354. oneWireDrv.ROM_NO[5] = 0;
  355. oneWireDrv.ROM_NO[6] = 0;
  356. oneWireDrv.ROM_NO[7] = 0;
  357. oneWireDrv.LastDiscrepancy = 64;
  358. oneWireDrv.LastFamilyDiscrepancy = 0;
  359. oneWireDrv.LastDeviceFlag = FALSE;
  360. }
  361. /**
  362. * @brief Setup the search to skip the current device type on the next call
  363. * to owNeOW_LOWxt().
  364. * @param none
  365. * @retval : none
  366. */
  367. void owFamilySkipSetup(void) {
  368. // set the Last discrepancy to last family discrepancy
  369. oneWireDrv.LastDiscrepancy = oneWireDrv.LastFamilyDiscrepancy;
  370. oneWireDrv.LastFamilyDiscrepancy = 0;
  371. // check for end of list
  372. if (oneWireDrv.LastDiscrepancy == 0)
  373. oneWireDrv.LastDeviceFlag = TRUE;
  374. }
  375. /**
  376. * @brief Setup new ROM number
  377. * @param array with new ROM number
  378. * @retval : none
  379. */
  380. void owSetRom(uint8_t *array) {
  381. *oneWireDrv.ROM_NO = *array;
  382. *(oneWireDrv.ROM_NO +4 )= *(array+4);
  383. }
  384. /**
  385. * @brief Calculate the CRC8 of the byte value provided with the
  386. * current global 'crc8' value.
  387. * @param byte value
  388. * @retval : current global crc8 value
  389. */
  390. uint8_t owCrc8(uint8_t value) {
  391. crc8_ow = dscrc_table[crc8_ow ^ value];
  392. return crc8_ow;
  393. }
  394. /**
  395. * @brief Reset 1-wire zbernice s detekciou pritomnosti zariadenia
  396. * @param none
  397. * @retval : TRUE - device present
  398. * FALSE - no device present
  399. */
  400. uint8_t __attribute__((optimize("O3"))) owReset(void) {
  401. uint8_t state;
  402. OW_GPIO_PULSE(OW_RESET_MEASTER, 0, MCU_FREQUENCY, 1, oneWirePins.port, oneWirePins.pin_ow);
  403. OW_SET_HI;
  404. setPinIn();
  405. OW_DELAY_US(OW_RESET_PRESENCE, MCU_FREQUENCY, 1); //TODO set offset
  406. state = (oneWirePins.port->IDR & oneWirePins.pin_ow ) != 0;
  407. //setPinOut();
  408. OW_DELAY_US(OW_RESET_DELAY, MCU_FREQUENCY, 40); //TODO set offset
  409. setPinOut(); //Toto som presunul až za oneskorenie, pretože to spôsobovalo nechcené pulzy
  410. return !state;
  411. }
  412. /**
  413. * @brief Vyslanie 8-bitov na zbernicu 1-wire
  414. * @param byte_value 1W príkaz
  415. * @param strong príznak, či sa má po poslednom bite dať zbernica do stavu STRONG.
  416. * povolené hodnoty: @arg OW_STRONG_OFF - zbernica bude v stave RESISTIVE,
  417. * @arg OW_STRONG_ON zbernica bude v stave STRONG
  418. * @retval : none
  419. */
  420. void __attribute__((optimize("O1"))) owWriteByte(uint8_t byte_value, uint8_t strong) {
  421. owWriteBit(&byte_value);
  422. owWriteBit(&byte_value);
  423. owWriteBit(&byte_value);
  424. owWriteBit(&byte_value);
  425. owWriteBit(&byte_value);
  426. owWriteBit(&byte_value);
  427. owWriteBit(&byte_value);
  428. //owWriteBit(&byte_value);
  429. #if USE_STRONG == 1
  430. if(strong == OW_STRONG_ON){
  431. owWriteBitStrong(byte_value>>7);
  432. }else{
  433. if(byte_value & 0x01){
  434. // zapis bit=1
  435. OW_GPIO_PULSE(OW_WRITE_ONE, 0, MCU_FREQUENCY, 1, oneWirePins.port, oneWirePins.pin_ow);
  436. OW_GPIO_PULSE(OW_WRITE_ONE_DELAY + OW_WRITE_DELAY, 1, MCU_FREQUENCY, 29, oneWirePins.port, oneWirePins.pin_ow);
  437. //Offset je nastavený na 19, pretože zohľadňuje volanie aj obsluhu funkcie
  438. }
  439. else{
  440. // zapis bit=0
  441. OW_GPIO_PULSE(OW_WRITE_ZERO, 0, MCU_FREQUENCY, 1, oneWirePins.port, oneWirePins.pin_ow);
  442. OW_GPIO_PULSE(OW_WRITE_DELAY, 1, MCU_FREQUENCY, 30, oneWirePins.port, oneWirePins.pin_ow);
  443. //Offset je nastavený na 21, pretože zohľadňuje volanie aj obsluhu funkcie
  444. }
  445. }
  446. #else
  447. if(byte_value & 0x01){
  448. // zapis bit=1
  449. OW_GPIO_PULSE(OW_WRITE_ONE, 0, MCU_FREQUENCY, 1, oneWirePins.port, oneWirePins.pin_ow);
  450. OW_GPIO_PULSE(OW_WRITE_ONE_DELAY + OW_WRITE_DELAY, 1, MCU_FREQUENCY, 29, oneWirePins.port, oneWirePins.pin_ow);
  451. //Offset je nastavený na 19, pretože zohľadňuje volanie aj obsluhu funkcie
  452. }
  453. else{
  454. // zapis bit=0
  455. OW_GPIO_PULSE(OW_WRITE_ZERO, 0, MCU_FREQUENCY, 1, oneWirePins.port, oneWirePins.pin_ow);
  456. OW_GPIO_PULSE(OW_WRITE_DELAY, 1, MCU_FREQUENCY, 30, oneWirePins.port, oneWirePins.pin_ow);
  457. //Offset je nastavený na 21, pretože zohľadňuje volanie aj obsluhu funkcie
  458. }
  459. #endif
  460. }
  461. /**
  462. * Na zbernicu 1-wire zapíše 1 bit. Po zápise nastaví zbernicu
  463. * do stavu STRONG.
  464. * @param bit_value - bit, ktorý sa bude zapisovať.
  465. */
  466. void owWriteBitStrong(uint8_t bit_value) {
  467. if(bit_value & 0x01){
  468. // zapis bit=1
  469. OW_GPIO_PULSE(OW_WRITE_ONE, 1, MCU_FREQUENCY, 15, oneWirePins.port, oneWirePins.pin_ow);
  470. //TODO offset je stanovený od oka v tomto prípade
  471. }
  472. else{
  473. // zapis bit=0
  474. OW_GPIO_PULSE(OW_WRITE_ZERO, 0, MCU_FREQUENCY, 17, oneWirePins.port, oneWirePins.pin_ow);
  475. //TODO offset je stanovený od oka v tomto prípade
  476. }
  477. }
  478. /**
  479. * @brief Vyslanie 1 bitu na zbernicu 1-wire
  480. * @param data, akceptovany je LSB
  481. * @retval : none
  482. */
  483. void __attribute__((optimize("Ofast"))) owWriteBit(uint8_t* bit_value) {
  484. if(*bit_value & 0x01){
  485. // zapis bit=1
  486. OW_GPIO_PULSE(OW_WRITE_ONE, 0, MCU_FREQUENCY, 1, oneWirePins.port, oneWirePins.pin_ow);
  487. OW_GPIO_PULSE(OW_WRITE_ONE_DELAY + OW_WRITE_DELAY, 1, MCU_FREQUENCY, 19, oneWirePins.port, oneWirePins.pin_ow);
  488. //Offset je nastavený na 19, pretože zohľadňuje volanie aj obsluhu funkcie
  489. }
  490. else{
  491. // zapis bit=0
  492. OW_GPIO_PULSE(OW_WRITE_ZERO, 0, MCU_FREQUENCY, 1, oneWirePins.port, oneWirePins.pin_ow);
  493. OW_GPIO_PULSE(OW_WRITE_DELAY, 1, MCU_FREQUENCY, 21, oneWirePins.port, oneWirePins.pin_ow);
  494. //Offset je nastavený na 21, pretože zohľadňuje volanie aj obsluhu funkcie
  495. }
  496. *bit_value >>= 1;
  497. }
  498. /**
  499. * @brief Nacitanie 1 bitu zo zbernicu 1-wire
  500. * @param none
  501. * @retval : nacitana hodnota 0/1
  502. */
  503. uint8_t owReadBit(void) {
  504. uint8_t state=0x00;
  505. OW_GPIO_PULSE(OW_READ_PULSE, 0, MCU_FREQUENCY, 7, oneWirePins.port, oneWirePins.pin_ow); //TODO set offset
  506. //OW_SET_HI;
  507. setPinIn();
  508. OW_SET_HI; //Toto som presunul až za oneskorenie, pretože to spôsobovalo nechcené pulzy
  509. OW_DELAY_US(OW_READ_PRESENCE, MCU_FREQUENCY, 1); //TODO set offset
  510. state = (oneWirePins.port->IDR & oneWirePins.pin_ow ) != 0;
  511. //setPinOut();
  512. //OW_SET_HI;
  513. OW_DELAY_US(OW_READ_DELAY, MCU_FREQUENCY, 1); //TODO set offset
  514. setPinOut(); //Toto som presunul až za oneskorenie, pretože to spôsobovalo nechcené pulzy
  515. OW_DELAY_US(OW_BOOSTER, MCU_FREQUENCY, 36); //TODO set offset
  516. return state;
  517. }
  518. /**
  519. * @brief Nacitanie 1 byte zo zbernice 1-wire
  520. * @param none
  521. * @retval nacitana hodnota 8-bit hodnota
  522. */
  523. uint8_t owReadByte(void) {
  524. uint8_t value;
  525. value = (owReadBit()); // LSB first
  526. value |= (owReadBit()<<1);
  527. value |= (owReadBit()<<2);
  528. value |= (owReadBit()<<3);
  529. value |= (owReadBit()<<4);
  530. value |= (owReadBit()<<5);
  531. value |= (owReadBit()<<6);
  532. value |= (owReadBit()<<7);
  533. return value;
  534. }
  535. /**
  536. * @}
  537. */
  538. /**
  539. * @}
  540. */
  541. /**
  542. * @}
  543. */