one_wire.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #ifndef ONE_WIRE_LIB_H
  2. #define ONE_WIRE_LIB_H
  3. #if defined(STM32F100xB) || defined(STM32F100xE) || defined(STM32F101x6) || defined(STM32F101xB) \
  4. || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F102x6) || defined(STM32F102xB) \
  5. || defined(STM32F103x6) || defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG)
  6. #include "stm32f1xx.h"
  7. #define STM32F1
  8. #endif
  9. #if defined(STM32F030x6) || defined(STM32F030x8) || defined(STM32F031x6) || defined(STM32F038xx) \
  10. || defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F051x8) || defined(STM32F058xx) \
  11. || defined(STM32F070x6) || defined(STM32F070xB) || defined(STM32F071xB) || defined(STM32F072xB) \
  12. || defined(STM32F078xx) || defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC)
  13. #include "stm32f0xx.h"
  14. #define STM32F0
  15. #endif // defined
  16. #if defined(STM32L011xx) || defined(STM32L021xx) || defined(STM32L031xx) || defined(STM32L041xx) \
  17. || defined(STM32L051xx) || defined(STM32L052xx) || defined(STM32L053xx) || defined(STM32L061xx) \
  18. || defined(STM32L062xx) || defined(STM32L063xx) || defined(STM32L071xx) || defined(STM32L072xx) \
  19. || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
  20. #include "stm32l0xx.h"
  21. #define STM32L0
  22. #endif // defined
  23. #if defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx) || defined(STM32F302xC) \
  24. || defined(STM32F303xC) || defined(STM32F358xx) || defined(STM32F303x8) || defined(STM32F334x8) \
  25. || defined(STM32F328xx) || defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) \
  26. || defined(STM32F373xC) || defined(STM32F378xx)
  27. #include "stm32f3xx.h"
  28. #define STM32F3
  29. #endif // defined
  30. #if defined(STM32L432xx)
  31. #include "stm32l4xx.h"
  32. #include "stm32l4xx_hal.h"
  33. #endif
  34. #include <string.h>
  35. #define TRUE 0x01 // konstanty v kniznici 1-wire
  36. #define FALSE 0x00
  37. #define MCU_FREQUENCY 32000 //4194
  38. #define USE_STRONG 0
  39. #define OW_WRITE_ZERO 60
  40. #define OW_WRITE_ONE 6
  41. #define OW_WRITE_ONE_DELAY 54
  42. #define OW_WRITE_DELAY 10
  43. #define OW_READ_PULSE 6
  44. #define OW_READ_PRESENCE 9
  45. #define OW_READ_DELAY 45
  46. #define OW_RESET_MEASTER 500
  47. #define OW_RESET_PRESENCE 80
  48. #define OW_RESET_DELAY 400
  49. #define OW_CONVERT_DELAY 750
  50. #define OW_BOOSTER 25 // od 5 do 1000 usec
  51. #define OW_CMD_READ_ROM 0x33
  52. #define OW_CMD_MATCH_ROM 0x55
  53. #define OW_CMD_SEARCH_ROM 0xF0
  54. #define OW_CMD_ALARM_SEARCH 0xEC
  55. #define OW_CMD_SKIP_ROM 0xCC
  56. #define OW_CMD_CONVERT_VALUE 0x44
  57. #define OW_CMD_COPY_SCRATCHPAD 0x48
  58. #define OW_CMD_READ_POWER_SUPPLY 0xB4
  59. #define OW_CMD_RECALL_E2 0xB8
  60. #define OW_CMD_READ_SCRATCHPAD 0xBE
  61. #define OW_CMD_WRITE_SCRATCHPAD 0x4E
  62. #define OW_SETTING_NORMAL 1
  63. #define OW_SETTING_LONG 2
  64. #define OW_SETTING_USER 3
  65. #define OW_SET_HI (oneWirePins.port->BSRR = (uint32_t)oneWirePins.pin_ow)
  66. #define OW_SET_LO (oneWirePins.port->BRR = (uint32_t)oneWirePins.pin_ow)
  67. #define OW_UP_SET (oneWirePins.port->BSRR = oneWirePins.pin_ow_up)
  68. #define OW_UP_CLEAR (oneWirePins.port->BRR = oneWirePins.pin_ow_up)
  69. #define OW_DOWN_SET (oneWirePins.port->BSRR= oneWirePins.pin_ow_down)
  70. #define OW_DOWN_CLEAR (oneWirePins.port->BRR = oneWirePins.pin_ow_down)
  71. //#define OW_NO_INTERRUPTS_ONLY_SYSTICK
  72. #ifdef OW_NO_INTERRUPTS_ONLY_SYSTICK
  73. //Only systick will be disabled when calling OW_DIS_INTERRUPTS
  74. #define OW_DIS_INTERRUPTS DISABLE_sysTick_IRQ
  75. #define OW_ENA_INTERRUPTS ENABLE_sysTick_IRQ
  76. #else
  77. //All interrupts will be disabled when calling OW_DIS_INTERRUPTS
  78. #define OW_DIS_INTERRUPTS uint32_t primSpec55511 = __get_PRIMASK(); \
  79. do{ \
  80. __disable_irq(); \
  81. }while(0)
  82. #define OW_ENA_INTERRUPTS do{ \
  83. if (!primSpec55511) {__enable_irq();} \
  84. }while(0)
  85. #endif // OW_NO_INTERRUPTS_ONLY_SYSTICK
  86. /**
  87. * @brief Delay ticks helper.
  88. * @param ticks Count of ticks to wait divided by 3.
  89. * @see delay_ticks_c
  90. */
  91. #define delay_ticks_0(ticks) { uint32_t tcks = (ticks); \
  92. __ASM volatile( ".syntax unified;" \
  93. "0: SUBS %[count], 1;" \
  94. "BNE 0b;" :[count]"+r"(tcks) );}
  95. //This works only with constant numbers - ifs are evaluated at compile time
  96. /**
  97. * @brief Delays specified amount of ticks.
  98. * @param ticks Count of ticks to wait. This has to be constant value, because
  99. * this macro contains ifs, which has to be evaluated at compilation time.
  100. * @note Always disable interrupts when using this macro to achieve accurate result.
  101. */
  102. #define delay_ticks_c(ticks) do{ \
  103. if((ticks) > 2){delay_ticks_0((ticks)/3);} \
  104. if((ticks) % 3 == 1) __ASM volatile( ".syntax unified; nop;"); \
  105. else if((ticks) % 3 == 2) __ASM volatile( ".syntax unified; nop; nop;"); \
  106. }while(0);
  107. /**
  108. * @brief Calculates number of ticks (MCU cycles) from frequency and time.
  109. * @param us Time in microseconds.
  110. * @param freq MCU frequency in kHz.
  111. */
  112. #define OW_US_FREQ_TO_TICKS(us, freq) (((us)*(freq))/1000UL)
  113. #define OW_DELAY_US(us, freq, tick_offs) if(OW_US_FREQ_TO_TICKS((us), (freq)) >= (tick_offs)) {delay_ticks_c(OW_US_FREQ_TO_TICKS((us), (freq)) - (tick_offs));}
  114. #define delay_us(us) OW_DELAY_US((us), MCU_FREQUENCY, 0)
  115. /**
  116. * @brief Creates pulse with specified duration on selected GPIO pin. At first, new pin value is set and
  117. * after that delay is applied.
  118. * @param us Duration of pulse in microseconds.
  119. * @param hi 1 for high pulse, 0 for low pulse.
  120. * @param freq MCU frequency in kHz.
  121. * @param tick_offs Count of ticks, that are wait outside of this macro. It's value has to be at least 1,
  122. * because GPIO pin setting takes 1 tick. It can be used to fine tune pulse width, which can be affected
  123. * by instructions called after this macro.
  124. * @param GPIO_port GPIO port to be used.
  125. * @param GPIO_pin Pin on specified GPIO port.
  126. * @note Always disable interrupts when using this macro to achieve accurate result.
  127. */
  128. #define OW_GPIO_PULSE(us, hi, freq, tick_offs, GPIO_port, GPIO_pin) do{ \
  129. if(hi){(GPIO_port)->BSRR = (GPIO_pin);} \
  130. else {(GPIO_port)->BRR = (GPIO_pin);} \
  131. OW_DELAY_US((us),(freq),(tick_offs)); \
  132. }while(0);
  133. /**
  134. * Dátová štruktúra pre komunikáciu so zbernicou 1-wire. Obsahuje:
  135. * - časové konštanty
  136. * - príznaky komunikácie
  137. * - komunikačný buffer
  138. * - inštanciu časovača knižnice STM32 HAL
  139. */
  140. typedef struct {
  141. /** Globalne nastavenie casovania zvernice 1w */
  142. volatile uint8_t one_wire_timing_settings;
  143. /** lokalny buffer pre ID zariadenia */
  144. uint8_t ROM_NO[8];
  145. /** priznak konfliktu na zbernici pri vyhladavani ID */
  146. __IO uint32_t LastDiscrepancy;
  147. __IO uint32_t LastFamilyDiscrepancy;
  148. __IO uint32_t LastDeviceFlag;
  149. /** Priznak timeru ze aktualne je v stave cakania na udalost pri ktorej sa zmenia jeho parametre. */
  150. __IO uint8_t flag_timer_inUse;
  151. /** Stav zbernice: OW_STATE_READY, OW_STATE_WAITING. */
  152. __IO uint8_t ow_state;
  153. /** Priznak pripravenosti dat. Hodnoty môžu nadobúdať zo štruktúry oneWireDataReady_typeDef. */
  154. __IO uint8_t ow_data_ready;
  155. } oneWireDriver_typeDef;
  156. /**
  157. * Definícia pinov pre ovládanie zbernice 1-wire. Aktuálne nepoužité.
  158. */
  159. typedef struct oneWirePins_struct {
  160. /** Pin pre signál, ktorý zdvihne zbernicu na hodnotu 1 */
  161. volatile uint16_t pin_ow_up;
  162. /** Pin pre signál, ktorý zloží zbernicu na hodnotu 0 */
  163. volatile uint16_t pin_ow_down;
  164. /** Pin pre signál, ktorý zdvihne zbernicu na tvrdú hodnotu 1 */
  165. volatile uint16_t pin_ow_strong;
  166. /** Pin pre vstupný signál */
  167. volatile uint16_t pin_ow_in;
  168. /** Port, na ktorom sú piny */
  169. GPIO_TypeDef* port;
  170. } oneWirePins_typeDef;
  171. typedef struct oneWirePin_struct {
  172. __IO uint16_t pin_ow;
  173. __IO uint16_t pin_ow_moder_bit;
  174. GPIO_TypeDef* port;
  175. } oneWirePin_typeDef;
  176. /**
  177. * Príznak pripravenosti dát zo senzorov zbernice 1-wire.
  178. */
  179. typedef enum OW_DATA_enum {
  180. /** Dáta nie sú pripravené, práve sa merajú */
  181. OW_DATA_BUSY=0,
  182. /** Dáta sú pripravené */
  183. OW_DATA_READY=1,
  184. } oneWireDataReady_typeDef;
  185. /**
  186. * Príznak stavu zbernice v stave STRONG.
  187. */
  188. typedef enum OW_STRONG_STATE_enum {
  189. /** Zbernica nie je v stave STRONG */
  190. OW_STRONG_OFF = 0,
  191. /** Zbernica je v stave STRONG */
  192. OW_STRONG_ON = 1,
  193. } oneWireStrongState_typeDef;
  194. /**
  195. * Príznak pre stav časovača.
  196. */
  197. typedef enum {
  198. /** Časovač je spustený, prebieha meranie */
  199. TIMER_STATE_RUN = 0,
  200. /** Časovač je vypnutý */
  201. TIMER_STATE_IDLE = 1,
  202. } oneWireTimerState_typeDef;
  203. void oneWire_init(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin ); // konfiguracia portu pre 1-wire
  204. uint8_t owReset(void); // reset zbernice a kontrola pritomnosti zariadeni
  205. uint8_t owFirst(); // nacitanie ID prveho zariadenia
  206. uint8_t owNext(); // iteracia zoznamu zariadeni
  207. uint8_t owVerify(void); // kontrola existencia ID zariadenia
  208. void owTargetSetup(uint8_t fcode); // nastavenie podmnoziny vyhladavanych zariadeni
  209. void owFamilySkipSetup(void); // zrusenie selektivneho vyhladavania
  210. void owSetRom(uint8_t *array); // nastavenie ID konkretneho zariadenia
  211. uint8_t owSearch();
  212. uint8_t owCrc8(uint8_t);
  213. uint8_t owReadBit(void);
  214. void owWriteByte(uint8_t, uint8_t);
  215. uint8_t owReadByte(void);
  216. void owWriteBit(uint8_t*);
  217. void owWriteBitStrong(uint8_t);
  218. void owSetDelayMs(uint16_t time);
  219. void ow_stop_ms_timer(void);
  220. uint8_t *oneWire_getROM();
  221. void owGetROM(uint8_t *mem_page);
  222. void owSelect_ROM(const uint8_t rom[8]);
  223. void owSelect();
  224. void ow_ClearStrong(void);
  225. void ow_SetStrong(void);
  226. #endif //ONE_WIRE_LIB_H