HAL library 05- OneWire for STM32Fxxx
Next port from STM32F4xx STD drivers to STM32Fxxx HAL drivers was done for OneWire library. This library is protocol based only. It features basic functions for read/write byte, reset port and search devices on OneWire port.
1-Wire is a device communications bus system designed by Dallas Semiconductor Corp. that provides low-speed data, signaling, and power over a single signal.
It’s used in some temperature sensors, like DS18B20 temp sensor and many others products from Dallas.
Library
Features
- Read/write byte
- Search for devices on 1-wire pin
- Read rom from device
- Unlimited OneWire ports on STM32Fxxx
-
- This allows you to group sensors to separate ports to prevent communication failures if one sensor is broken
-
Dependencies
- HAL
- TM
- STM32Fxxx HAL
- defines.h
- GPIO
- DELAY
Functions and enumerations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
/** * @defgroup TM_ONEWIRE_Typedefs * @brief Library Typedefs * @{ */ /** * @brief OneWire working struct * @note Except ROM_NO member, everything is fully private and should not be touched by user */ typedef struct { GPIO_TypeDef* GPIOx; /*!< GPIOx port to be used for I/O functions */ uint16_t GPIO_Pin; /*!< GPIO Pin to be used for I/O functions */ uint8_t LastDiscrepancy; /*!< Search private */ uint8_t LastFamilyDiscrepancy; /*!< Search private */ uint8_t LastDeviceFlag; /*!< Search private */ uint8_t ROM_NO[8]; /*!< 8-bytes address of last search device */ } TM_OneWire_t; /** * @} */ /** * @defgroup TM_ONEWIRE_Functions * @brief Library Functions * @{ */ /** * @brief Initializes OneWire bus * @param *OneWireStruct: Pointer to @ref TM_OneWire_t empty working onewire structure * @param *Pointer to GPIO port used for onewire channel * @param GPIO_Pin: GPIO Pin on specific GPIOx to be used for onewire channel * @retval None */ void TM_OneWire_Init(TM_OneWire_t* OneWireStruct, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); /** * @brief Resets OneWire bus * * @note Sends reset command for OneWire * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working onewire structure * @retval None */ uint8_t TM_OneWire_Reset(TM_OneWire_t* OneWireStruct); /** * @brief Reads byte from one wire bus * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working onewire structure * @retval Byte from read operation */ uint8_t TM_OneWire_ReadByte(TM_OneWire_t* OneWireStruct); /** * @brief Writes byte to bus * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working onewire structure * @param byte: 8-bit value to write over OneWire protocol * @retval None */ void TM_OneWire_WriteByte(TM_OneWire_t* OneWireStruct, uint8_t byte); /** * @brief Writes single bit to onewire bus * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working onewire structure * @param bit: Bit value to send, 1 or 0 * @retval None */ void TM_OneWire_WriteBit(TM_OneWire_t* OneWireStruct, uint8_t bit); /** * @brief Reads single bit from one wire bus * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working onewire structure * @retval Bit value: * - 0: Bit is low (zero) * - > 0: Bit is high (one) */ uint8_t TM_OneWire_ReadBit(TM_OneWire_t* OneWireStruct); /** * @brief Searches for OneWire devices on specific Onewire port * @note Not meant for public use. Use @ref TM_OneWire_First and @ref TM_OneWire_Next for this. * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working onewire structure where to search * @param Device status: * - 0: No devices detected * - > 0: Device detected */ uint8_t TM_OneWire_Search(TM_OneWire_t* OneWireStruct, uint8_t command); /** * @brief Resets search states * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working onewire where to reset search values * @retval None */ void TM_OneWire_ResetSearch(TM_OneWire_t* OneWireStruct); /** * @brief Starts search, reset states first * @note When you want to search for ALL devices on one onewire port, you should first use this function. @verbatim /...Initialization before status = TM_OneWire_First(&OneWireStruct); while (status) { //Save ROM number from device TM_OneWire_GetFullROM(ROM_Array_Pointer); //Check for new device status = TM_OneWire_Next(&OneWireStruct); } @endverbatim * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working onewire where to reset search values * @param Device status: * - 0: No devices detected * - > 0: Device detected */ uint8_t TM_OneWire_First(TM_OneWire_t* OneWireStruct); /** * @brief Reads next device * @note Use @ref TM_OneWire_First to start searching * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working onewire * @param Device status: * - 0: No devices detected any more * - > 0: New device detected */ uint8_t TM_OneWire_Next(TM_OneWire_t* OneWireStruct); /** * @brief Gets ROM number from device from search * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working onewire * @param index: Because each device has 8-bytes long ROm address, you have to call this 8 times, to get ROM bytes from 0 to 7 * @reetval ROM byte for index (0 to 7) at current found device */ uint8_t TM_OneWire_GetROM(TM_OneWire_t* OneWireStruct, uint8_t index); /** * @brief Gets all 8 bytes ROM value from device from search * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working onewire * @param *firstIndex: Pointer to first location for first byte, other bytes are automatically incremented * @retval None */ void TM_OneWire_GetFullROM(TM_OneWire_t* OneWireStruct, uint8_t *firstIndex); /** * @brief Selects specific slave on bus * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working onewire * @param *addr: Pointer to first location of 8-bytes long ROM address * @retval None */ void TM_OneWire_Select(TM_OneWire_t* OneWireStruct, uint8_t* addr); /** * @brief Selects specific slave on bus with pointer address * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working onewire * @param *ROM: Pointer to first byte of ROM address * @retval None */ void TM_OneWire_SelectWithPointer(TM_OneWire_t* OneWireStruct, uint8_t* ROM); /** * @brief Calculates 8-bit CRC for 1-wire devices * @param *addr: Pointer to 8-bit array of data to calculate CRC * @param len: Number of bytes to check * * @retval Calculated CRC from input data */ uint8_t TM_OneWire_CRC8(uint8_t* addr, uint8_t len); /** * @} */ |
Example 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
/** * Keil project example for ONEWIRE protocol * * Before you start, select your target, on the right of the "Load" button * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 * @conf PLL parameters are set in "Options for Target" -> "C/C++" -> "Defines" * @packs STM32F4xx/STM32F7xx Keil packs are requred with HAL driver support * @stdperiph STM32F4xx/STM32F7xx HAL drivers required */ /* Include core modules */ #include "stm32fxxx_hal.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32_disco.h" #include "tm_stm32_delay.h" #include "tm_stm32_onewire.h" /* Onewire structure */ TM_OneWire_t OW; int main(void) { /* Init system clock for maximum system speed */ TM_RCC_InitSystem(); /* Init HAL layer */ HAL_Init(); /* Init leds */ TM_DISCO_LedInit(); /* Init button */ TM_DISCO_ButtonInit(); /* Init ONEWIRE port on PB4 pin */ TM_OneWire_Init(&OW, GPIOB, GPIO_PIN_4); /* Check if any device is connected */ if (TM_OneWire_First(&OW)) { /* Set LED GREEN */ TM_DISCO_LedOn(LED_GREEN); /* Search for next devices */ do { /* Read ROM from device */ //TM_OneWire_GetFullROM(&OW, &array_8_bytes[0]); } while (TM_OneWire_Next(&OW)); } else { /* Set LED RED */ TM_DISCO_LedOn(LED_RED); } while (1) { } } |
Example 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
/** * Keil project example for ONEWIRE protocol on multiple GPIO pins * * Before you start, select your target, on the right of the "Load" button * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 * @conf PLL parameters are set in "Options for Target" -> "C/C++" -> "Defines" * @packs STM32F4xx/STM32F7xx Keil packs are requred with HAL driver support * @stdperiph STM32F4xx/STM32F7xx HAL drivers required */ /* Include core modules */ #include "stm32fxxx_hal.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32_disco.h" #include "tm_stm32_delay.h" #include "tm_stm32_onewire.h" /* Onewire structure */ TM_OneWire_t OW1; TM_OneWire_t OW2; int main(void) { /* Init system clock for maximum system speed */ TM_RCC_InitSystem(); /* Init HAL layer */ HAL_Init(); /* Init leds */ TM_DISCO_LedInit(); /* Init ONEWIRE port on PB4 and PC6 pins */ TM_OneWire_Init(&OW1, GPIOB, GPIO_PIN_4); TM_OneWire_Init(&OW2, GPIOC, GPIO_PIN_6); /* Check if any device is connected on OW port 1 */ if (TM_OneWire_First(&OW1)) { /* Set LED green */ TM_DISCO_LedOn(LED_GREEN); /* Search for next devices */ do { /* Read rom from device */ //TM_OneWire_GetFullROM(&OW1, &array_8_bytes[0]); } while (TM_OneWire_Next(&OW1)); } /* Check if any device is connected on OW port 2 */ if (TM_OneWire_First(&OW2)) { /* Set LED red */ TM_DISCO_LedOn(LED_RED); /* Search for next devices */ do { /* Read rom from device */ //TM_OneWire_GetFullROM(&OW2, &array_8_bytes[0]); } while (TM_OneWire_Next(&OW2)); } while (1) { } } |
Project is available on Github, download all libraries below.
STM32 libraries based on STM32Fxxx HAL drivers.
Recent comments