| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- #ifndef ONE_WIRE_LIB_H
- #define ONE_WIRE_LIB_H
- #if defined(STM32F100xB) || defined(STM32F100xE) || defined(STM32F101x6) || defined(STM32F101xB) \
- || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F102x6) || defined(STM32F102xB) \
- || defined(STM32F103x6) || defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG)
- #include "stm32f1xx.h"
- #define STM32F1
- #endif
- #if defined(STM32F030x6) || defined(STM32F030x8) || defined(STM32F031x6) || defined(STM32F038xx) \
- || defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F051x8) || defined(STM32F058xx) \
- || defined(STM32F070x6) || defined(STM32F070xB) || defined(STM32F071xB) || defined(STM32F072xB) \
- || defined(STM32F078xx) || defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC)
- #include "stm32f0xx.h"
- #define STM32F0
- #endif // defined
- #if defined(STM32L011xx) || defined(STM32L021xx) || defined(STM32L031xx) || defined(STM32L041xx) \
- || defined(STM32L051xx) || defined(STM32L052xx) || defined(STM32L053xx) || defined(STM32L061xx) \
- || defined(STM32L062xx) || defined(STM32L063xx) || defined(STM32L071xx) || defined(STM32L072xx) \
- || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
- #include "stm32l0xx.h"
- #define STM32L0
- #endif // defined
- #if defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx) || defined(STM32F302xC) \
- || defined(STM32F303xC) || defined(STM32F358xx) || defined(STM32F303x8) || defined(STM32F334x8) \
- || defined(STM32F328xx) || defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) \
- || defined(STM32F373xC) || defined(STM32F378xx)
- #include "stm32f3xx.h"
- #define STM32F3
- #endif // defined
- #if defined(STM32L432xx)
- #include "stm32l4xx.h"
- #include "stm32l4xx_hal.h"
- #endif
- #include <string.h>
- #define TRUE 0x01 // konstanty v kniznici 1-wire
- #define FALSE 0x00
- #define MCU_FREQUENCY 32000 //4194
- #define USE_STRONG 0
- #define OW_WRITE_ZERO 60
- #define OW_WRITE_ONE 6
- #define OW_WRITE_ONE_DELAY 54
- #define OW_WRITE_DELAY 10
- #define OW_READ_PULSE 6
- #define OW_READ_PRESENCE 9
- #define OW_READ_DELAY 45
- #define OW_RESET_MEASTER 500
- #define OW_RESET_PRESENCE 80
- #define OW_RESET_DELAY 400
- #define OW_CONVERT_DELAY 750
- #define OW_BOOSTER 25 // od 5 do 1000 usec
- #define OW_CMD_READ_ROM 0x33
- #define OW_CMD_MATCH_ROM 0x55
- #define OW_CMD_SEARCH_ROM 0xF0
- #define OW_CMD_ALARM_SEARCH 0xEC
- #define OW_CMD_SKIP_ROM 0xCC
- #define OW_CMD_CONVERT_VALUE 0x44
- #define OW_CMD_COPY_SCRATCHPAD 0x48
- #define OW_CMD_READ_POWER_SUPPLY 0xB4
- #define OW_CMD_RECALL_E2 0xB8
- #define OW_CMD_READ_SCRATCHPAD 0xBE
- #define OW_CMD_WRITE_SCRATCHPAD 0x4E
- #define OW_SETTING_NORMAL 1
- #define OW_SETTING_LONG 2
- #define OW_SETTING_USER 3
- #define OW_SET_HI (oneWirePins.port->BSRR = (uint32_t)oneWirePins.pin_ow)
- #define OW_SET_LO (oneWirePins.port->BRR = (uint32_t)oneWirePins.pin_ow)
- #define OW_UP_SET (oneWirePins.port->BSRR = oneWirePins.pin_ow_up)
- #define OW_UP_CLEAR (oneWirePins.port->BRR = oneWirePins.pin_ow_up)
- #define OW_DOWN_SET (oneWirePins.port->BSRR= oneWirePins.pin_ow_down)
- #define OW_DOWN_CLEAR (oneWirePins.port->BRR = oneWirePins.pin_ow_down)
- //#define OW_NO_INTERRUPTS_ONLY_SYSTICK
- #ifdef OW_NO_INTERRUPTS_ONLY_SYSTICK
- //Only systick will be disabled when calling OW_DIS_INTERRUPTS
- #define OW_DIS_INTERRUPTS DISABLE_sysTick_IRQ
- #define OW_ENA_INTERRUPTS ENABLE_sysTick_IRQ
- #else
- //All interrupts will be disabled when calling OW_DIS_INTERRUPTS
- #define OW_DIS_INTERRUPTS uint32_t primSpec55511 = __get_PRIMASK(); \
- do{ \
- __disable_irq(); \
- }while(0)
- #define OW_ENA_INTERRUPTS do{ \
- if (!primSpec55511) {__enable_irq();} \
- }while(0)
- #endif // OW_NO_INTERRUPTS_ONLY_SYSTICK
- /**
- * @brief Delay ticks helper.
- * @param ticks Count of ticks to wait divided by 3.
- * @see delay_ticks_c
- */
- #define delay_ticks_0(ticks) { uint32_t tcks = (ticks); \
- __ASM volatile( ".syntax unified;" \
- "0: SUBS %[count], 1;" \
- "BNE 0b;" :[count]"+r"(tcks) );}
- //This works only with constant numbers - ifs are evaluated at compile time
- /**
- * @brief Delays specified amount of ticks.
- * @param ticks Count of ticks to wait. This has to be constant value, because
- * this macro contains ifs, which has to be evaluated at compilation time.
- * @note Always disable interrupts when using this macro to achieve accurate result.
- */
- #define delay_ticks_c(ticks) do{ \
- if((ticks) > 2){delay_ticks_0((ticks)/3);} \
- if((ticks) % 3 == 1) __ASM volatile( ".syntax unified; nop;"); \
- else if((ticks) % 3 == 2) __ASM volatile( ".syntax unified; nop; nop;"); \
- }while(0);
- /**
- * @brief Calculates number of ticks (MCU cycles) from frequency and time.
- * @param us Time in microseconds.
- * @param freq MCU frequency in kHz.
- */
- #define OW_US_FREQ_TO_TICKS(us, freq) (((us)*(freq))/1000UL)
- #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));}
- #define delay_us(us) OW_DELAY_US((us), MCU_FREQUENCY, 0)
- /**
- * @brief Creates pulse with specified duration on selected GPIO pin. At first, new pin value is set and
- * after that delay is applied.
- * @param us Duration of pulse in microseconds.
- * @param hi 1 for high pulse, 0 for low pulse.
- * @param freq MCU frequency in kHz.
- * @param tick_offs Count of ticks, that are wait outside of this macro. It's value has to be at least 1,
- * because GPIO pin setting takes 1 tick. It can be used to fine tune pulse width, which can be affected
- * by instructions called after this macro.
- * @param GPIO_port GPIO port to be used.
- * @param GPIO_pin Pin on specified GPIO port.
- * @note Always disable interrupts when using this macro to achieve accurate result.
- */
- #define OW_GPIO_PULSE(us, hi, freq, tick_offs, GPIO_port, GPIO_pin) do{ \
- if(hi){(GPIO_port)->BSRR = (GPIO_pin);} \
- else {(GPIO_port)->BRR = (GPIO_pin);} \
- OW_DELAY_US((us),(freq),(tick_offs)); \
- }while(0);
- /**
- * Dátová štruktúra pre komunikáciu so zbernicou 1-wire. Obsahuje:
- * - časové konštanty
- * - príznaky komunikácie
- * - komunikačný buffer
- * - inštanciu časovača knižnice STM32 HAL
- */
- typedef struct {
- /** Globalne nastavenie casovania zvernice 1w */
- volatile uint8_t one_wire_timing_settings;
- /** lokalny buffer pre ID zariadenia */
- uint8_t ROM_NO[8];
- /** priznak konfliktu na zbernici pri vyhladavani ID */
- __IO uint32_t LastDiscrepancy;
- __IO uint32_t LastFamilyDiscrepancy;
- __IO uint32_t LastDeviceFlag;
- /** Priznak timeru ze aktualne je v stave cakania na udalost pri ktorej sa zmenia jeho parametre. */
- __IO uint8_t flag_timer_inUse;
- /** Stav zbernice: OW_STATE_READY, OW_STATE_WAITING. */
- __IO uint8_t ow_state;
- /** Priznak pripravenosti dat. Hodnoty môžu nadobúdať zo štruktúry oneWireDataReady_typeDef. */
- __IO uint8_t ow_data_ready;
- } oneWireDriver_typeDef;
- /**
- * Definícia pinov pre ovládanie zbernice 1-wire. Aktuálne nepoužité.
- */
- typedef struct oneWirePins_struct {
- /** Pin pre signál, ktorý zdvihne zbernicu na hodnotu 1 */
- volatile uint16_t pin_ow_up;
- /** Pin pre signál, ktorý zloží zbernicu na hodnotu 0 */
- volatile uint16_t pin_ow_down;
- /** Pin pre signál, ktorý zdvihne zbernicu na tvrdú hodnotu 1 */
- volatile uint16_t pin_ow_strong;
- /** Pin pre vstupný signál */
- volatile uint16_t pin_ow_in;
- /** Port, na ktorom sú piny */
- GPIO_TypeDef* port;
- } oneWirePins_typeDef;
- typedef struct oneWirePin_struct {
- __IO uint16_t pin_ow;
- __IO uint16_t pin_ow_moder_bit;
- GPIO_TypeDef* port;
- } oneWirePin_typeDef;
- /**
- * Príznak pripravenosti dát zo senzorov zbernice 1-wire.
- */
- typedef enum OW_DATA_enum {
- /** Dáta nie sú pripravené, práve sa merajú */
- OW_DATA_BUSY=0,
- /** Dáta sú pripravené */
- OW_DATA_READY=1,
- } oneWireDataReady_typeDef;
- /**
- * Príznak stavu zbernice v stave STRONG.
- */
- typedef enum OW_STRONG_STATE_enum {
- /** Zbernica nie je v stave STRONG */
- OW_STRONG_OFF = 0,
- /** Zbernica je v stave STRONG */
- OW_STRONG_ON = 1,
- } oneWireStrongState_typeDef;
- /**
- * Príznak pre stav časovača.
- */
- typedef enum {
- /** Časovač je spustený, prebieha meranie */
- TIMER_STATE_RUN = 0,
- /** Časovač je vypnutý */
- TIMER_STATE_IDLE = 1,
- } oneWireTimerState_typeDef;
- void oneWire_init(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin ); // konfiguracia portu pre 1-wire
- uint8_t owReset(void); // reset zbernice a kontrola pritomnosti zariadeni
- uint8_t owFirst(); // nacitanie ID prveho zariadenia
- uint8_t owNext(); // iteracia zoznamu zariadeni
- uint8_t owVerify(void); // kontrola existencia ID zariadenia
- void owTargetSetup(uint8_t fcode); // nastavenie podmnoziny vyhladavanych zariadeni
- void owFamilySkipSetup(void); // zrusenie selektivneho vyhladavania
- void owSetRom(uint8_t *array); // nastavenie ID konkretneho zariadenia
- uint8_t owSearch();
- uint8_t owCrc8(uint8_t);
- uint8_t owReadBit(void);
- void owWriteByte(uint8_t, uint8_t);
- uint8_t owReadByte(void);
- void owWriteBit(uint8_t*);
- void owWriteBitStrong(uint8_t);
- void owSetDelayMs(uint16_t time);
- void ow_stop_ms_timer(void);
- uint8_t *oneWire_getROM();
- void owGetROM(uint8_t *mem_page);
- void owSelect_ROM(const uint8_t rom[8]);
- void owSelect();
- void ow_ClearStrong(void);
- void ow_SetStrong(void);
- #endif //ONE_WIRE_LIB_H
|