Library 12- OneWire library for STM32F4
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.
OneWire key features
- Single Contact Sufficient for Control and Operation
- Unique ID Factory-Lasered in Each Device
- Power Derived from Signal Bus (“Parasitically Powered”)
- Multidrop Capable: Supports Multiple Devices on Single Line
- Exceptional ESD Performance
source: maximintegrated.com
17th May, 2014: There was some new functions added!
Library
Features
- Read/write byte
- Search for devices on 1-wire pin
- Read rom from device
- Version 2.0 – January 04, 2015
- Library rewritten
- It supports unlimited pins and gpios for OneWire
- This allows you to group sensors to separate ports to prevent communication failures if one sensor is broken
- Version 2.1 – March 10, 2015
- Supported new GPIO system
Dependencies
- CMSIS
- STM32F4xx
- STM32F4xx RCC
- STM32F4xx GPIO
- TM
- TM DELAY
- TO GPIO
- defines.h
- attributes.h
- TM DELAY
Initialize
Before you can work with 1-wire bus, you have to initialize it
1 2 3 |
TM_OneWire_t OneWire; //Initialize One Wire on pin PD0 TM_OneWire_Init(&OneWire, GPIOD, GPIO_Pin_0); |
Reset pulse
If you need to reset 1-wire bus, simple call reset function
1 2 |
//Reset 1-wire bus TM_OneWire_Reset(&OneWire); |
Write byte
1 2 |
//Write byte to 1-wire slave TM_OneWire_WriteByte(&OneWire, uint8_t byte); |
Read byte
1 2 |
//Read byte to 1-wire slave byte = TM_OneWire_ReadByte(&OneWire); |
Search devices
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
uint8_t devices, count, device[5][8]; devices = TM_OneWire_First(&OneWire); count = 0; while (devices) { count++; //Get byte per byte for (i = 0; i < 8; i++) { device[count - 1][i] = TM_OneWire_GetROM(&OneWire, i); } //NEW: or pass pointer to first location for ROM and it will fill bytes for you //TM_OneWire_GetFullROM(&OneWire, &device[count - 1][0]) devices = TM_OneWire_Next(&OneWire); } |
Select device
Because you have more devices on one line, you have to somehow select device which you want to communicate with. You can do this with
1 2 3 4 5 |
//Select 1-wire address //8 bytes ROM code is used as parameter TM_OneWire_Select(TM_OneWire_t* OneWireStruct, uint8_t addr[]); //NEW: You can select device with pointer to first byte of ROM like this TM_OneWire_SelectWithPointer(&OneWire, &ROM_addr[0]); |
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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
/** * OneWire working struct * * Parameters: * - 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 */ typedef struct { GPIO_TypeDef* GPIOx; uint16_t GPIO_Pin; uint8_t LastDiscrepancy; uint8_t LastFamilyDiscrepancy; uint8_t LastDeviceFlag; uint8_t ROM_NO[8]; } TM_OneWire_t; /** * Initialize OneWire bus * * Parameters: * - TM_OneWire_t* OneWireStruct: * Pointer to empty working onewire struct * - GPIO_TypeDef* GPIOx: * Pointer to GPIO used for onewire * - uint16_t GPIO_Pin: * GPIO Pin on specific GPIOx to be used for onewire * * No return */ extern void TM_OneWire_Init(TM_OneWire_t* OneWireStruct, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); /** * Reset OneWire bus * * Sends reset command for OneWire * * Parameters: * - TM_OneWire_t* OneWireStruct: * Pointer to TM_OneWire_t working struct * * No return */ extern uint8_t TM_OneWire_Reset(TM_OneWire_t* OneWireStruct); /** * Read byte from one wire bus * * Parameters: * - TM_OneWire_t* OneWireStruct: * Pointer to TM_OneWire_t working struct * * Byte is returned */ extern uint8_t TM_OneWire_ReadByte(TM_OneWire_t* OneWireStruct); /** * Write byte to bus * * Parameters: * - TM_OneWire_t* OneWireStruct: * Pointer to TM_OneWire_t working struct * - uint8_t byte: * Byte to be sent * * No return */ extern void TM_OneWire_WriteByte(TM_OneWire_t* OneWireStruct, uint8_t byte); /** * Write single bit to onewire bus * * Parameters: * - TM_OneWire_t* OneWireStruct: * Pointer to TM_OneWire_t working struct * - uint8_t bit: * bit to be sent, 1 or 0 * * No return */ extern void TM_OneWire_WriteBit(TM_OneWire_t* OneWireStruct, uint8_t bit); /** * Read single bit from one wire bus * * Parameters: * - TM_OneWire_t* OneWireStruct: * Pointer to TM_OneWire_t working struct * * Returns 0 if bit is low, or 1 if bit is high */ extern uint8_t TM_OneWire_ReadBit(TM_OneWire_t* OneWireStruct); /** * Search OneWire devices. * Not meant for public user use * * Parameters: * - TM_OneWire_t* OneWireStruct: * Pointer to TM_OneWire_t working struct * * Returns 1 if new device is recognized or 0 if not */ extern uint8_t TM_OneWire_Search(TM_OneWire_t* OneWireStruct, uint8_t command); /** * Reset search states * * Parameters: * - TM_OneWire_t* OneWireStruct: * Pointer to TM_OneWire_t working struct * * No return */ extern void TM_OneWire_ResetSearch(TM_OneWire_t* OneWireStruct); /** * Start search, reset states * * Parameters: * - TM_OneWire_t* OneWireStruct: * Pointer to TM_OneWire_t working struct * * Returns 1 if any device on bus */ extern uint8_t TM_OneWire_First(TM_OneWire_t* OneWireStruct); /** * Read next device * * Parameters: * - TM_OneWire_t* OneWireStruct: * Pointer to TM_OneWire_t working struct * * Returns 1 if more devices are on bus, otherwise 0 */ extern uint8_t TM_OneWire_Next(TM_OneWire_t* OneWireStruct); /** * Get rom from device from search * * Parameters * - TM_OneWire_t* OneWireStruct: * Pointer to TM_OneWire_t working struct * - uint8_t index: * because each device has 8bytes long rom, you have to call this 8 times, to get rom bytes from 0 to 7 * * Returns ROM byte for index (0 to 7) at current found device */ extern uint8_t TM_OneWire_GetROM(TM_OneWire_t* OneWireStruct, uint8_t index); /** * Get all 8 bytes ROM value from device from search * * Parameters * - TM_OneWire_t* OneWireStruct: * Pointer to TM_OneWire_t working struct * - uint8_t* firstIndex: * Pointer to first location for first byte, other bytes are automatically incremented * * No return */ extern void TM_OneWire_GetFullROM(TM_OneWire_t* OneWireStruct, uint8_t *firstIndex); /** * Select specific slave on bus * * Parameters: * - TM_OneWire_t* OneWireStruct: * Pointer to TM_OneWire_t working struct * - uint8_t* addr: * Pointer to 8 bytes ROM address * * No return */ extern void TM_OneWire_Select(TM_OneWire_t* OneWireStruct, uint8_t* addr); /** * Select specific slave on bus with pointer address * * Parameters: * - TM_OneWire_t* OneWireStruct: * Pointer to TM_OneWire_t working struct * - uint8_t* ROM: * pointer to first byte of ROM address * * No return */ extern void TM_OneWire_SelectWithPointer(TM_OneWire_t* OneWireStruct, uint8_t* ROM); /** * Calculate 8-bit CRC for 1-wire devices * * Parameters: * - uint8_t* addr: * Pointer to data to calculate CRC * - uint8_t len: * Number of bytes to check * * Calculated CRC is returned */ extern uint8_t TM_OneWire_CRC8(uint8_t* addr, uint8_t len); |
Example
I connected 2 DS18B20 sensors to PD0 (make sure, you have external pullup resistor) and this code returns me on terminal.
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 |
/** * Keil project for OneWire protocol library * * 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 * @packs STM32F4xx Keil packs version 2.2.0 or greater required * @stdperiph STM32F4xx Standard peripheral drivers version 1.4.0 or greater required */ /* Include core modules */ #include "stm32f4xx.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32f4_delay.h" #include "tm_stm32f4_onewire.h" #include "tm_stm32f4_usart.h" #include <stdio.h> int main(void) { char buf[30]; uint8_t devices, i, j, count, device[2][8]; /* OneWire working structure */ TM_OneWire_t OneWire1; /* Initialize system */ SystemInit(); /* Initialize delay */ TM_DELAY_Init(); /* Initialize OneWire1 instance on pin PD0 */ TM_OneWire_Init(&OneWire1, GPIOD, GPIO_Pin_0); /* Initialize USART, TX: PB6, RX: PB7 */ TM_USART_Init(USART1, TM_USART_PinsPack_2, 115200); /* Check for any device on 1-wire */ devices = TM_OneWire_First(&OneWire1); count = 0; while (devices) { /* Increase count variable */ count++; /* Get full 8-bytes rom address */ TM_OneWire_GetFullROM(&OneWire1, device[count - 1]); /* Check for new device */ devices = TM_OneWire_Next(&OneWire1); } /* If any devices on 1-wire */ if (count > 0) { sprintf(buf, "Devices found on 1-wire instance: %d\n", count); TM_USART_Puts(USART1, buf); /* Display 64bit rom code */ for (j = 0; j < count; j++) { for (i = 0; i < 8; i++) { sprintf(buf, "0x%02X ", device[j][i]); TM_USART_Puts(USART1, buf); } TM_USART_Puts(USART1, "\n"); } } else { /* Nothing on OneWire */ TM_USART_Puts(USART1, "No devices on OneWire.\n\n"); } while (1) { } } |
Example 2
- 4 devices are connected to STM32F4 via OneWire:
- 2 OneWire ports:
- PA3
- PD0
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 |
/** * Keil project for OneWire protocol library, 2 networks, PA3 and PD0 pins * * Before you start, select your target, on the right of the "Load" button * * Don't forget 4k7 pull up resistor! * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 * @packs STM32F4xx Keil packs version 2.2.0 or greater required * @stdperiph STM32F4xx Standard peripheral drivers version 1.4.0 or greater required */ /* Include core modules */ #include "stm32f4xx.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32f4_delay.h" #include "tm_stm32f4_onewire.h" #include "tm_stm32f4_usart.h" #include <stdio.h> int main(void) { char buf[30]; uint8_t devices, i, j, count, device[2][8]; /* OneWire working structure */ TM_OneWire_t OneWire1, OneWire2; /* Initialize system */ SystemInit(); /* Initialize delay */ TM_DELAY_Init(); /* Initialize OneWire1 instance on pin PA3 */ TM_OneWire_Init(&OneWire1, GPIOA, GPIO_Pin_3); /* Initialize OneWire2 instance on pin PD0 */ TM_OneWire_Init(&OneWire2, GPIOD, GPIO_Pin_0); /* Initialize USART, TX: PB6, RX: PB7 */ TM_USART_Init(USART1, TM_USART_PinsPack_2, 115200); /* Check PA3 OneWire1 instance */ /* Check for any device on 1-wire port OneWire1 = PA3 */ devices = TM_OneWire_First(&OneWire1); count = 0; while (devices) { /* Increase count variable */ count++; /* Get full 8-bytes rom address */ TM_OneWire_GetFullROM(&OneWire1, device[count - 1]); /* Check for new device */ devices = TM_OneWire_Next(&OneWire1); } /* If any devices on 1-wire on pin PA3 */ if (count > 0) { sprintf(buf, "Devices found on 1-wire instance 1 (PA3): %d\n", count); TM_USART_Puts(USART1, buf); /* Display 64bit rom code */ for (j = 0; j < count; j++) { for (i = 0; i < 8; i++) { sprintf(buf, "0x%02X ", device[j][i]); TM_USART_Puts(USART1, buf); } TM_USART_Puts(USART1, "\n"); } } else { /* Nothing on OneWire */ TM_USART_Puts(USART1, "No devices on OneWire instance 1.\n\n"); } /* New line */ TM_USART_Puts(USART1, "\n"); /* Check PD3 OneWire2 instance */ /* Check for any device on 1-wire port OneWire2 = PD3 */ devices = TM_OneWire_First(&OneWire2); count = 0; while (devices) { /* Increase count variable */ count++; /* Get full 8-bytes rom address */ TM_OneWire_GetFullROM(&OneWire2, device[count - 1]); /* Check for new device */ devices = TM_OneWire_Next(&OneWire2); } /* If any devices on 1-wire on pin PD0 */ if (count > 0) { sprintf(buf, "Devices found on 1-wire instance 2 (PD0): %d\n", count); TM_USART_Puts(USART1, buf); /* Display 64bit rom code */ for (j = 0; j < count; j++) { for (i = 0; i < 8; i++) { sprintf(buf, "0x%02X ", device[j][i]); TM_USART_Puts(USART1, buf); } TM_USART_Puts(USART1, "\n"); } } else { /* Nothing on OneWire */ TM_USART_Puts(USART1, "No devices on OneWire instance 2.\n\n"); } /* New line */ TM_USART_Puts(USART1, "\n"); while (1) { } } |
Projects available on Github, download library below.
OneWire library for onewire devices
Recent comments