diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2016-06-05 18:06:49 +0200 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2016-06-05 18:09:51 +0200 |
commit | 0af68991e9b7829e2046810d9d748c5a35dc437a (patch) | |
tree | 6cf0e9567256a3c4e882240f816d293a837f9986 /src/glutt-o-logique/ds18b20 | |
parent | c6bf425075ca38ec52198e584dcdc67eaf0f1d98 (diff) | |
download | glutte-o-matic-0af68991e9b7829e2046810d9d748c5a35dc437a.tar.gz glutte-o-matic-0af68991e9b7829e2046810d9d748c5a35dc437a.tar.bz2 glutte-o-matic-0af68991e9b7829e2046810d9d748c5a35dc437a.zip |
Restructure stm32 project
Diffstat (limited to 'src/glutt-o-logique/ds18b20')
-rw-r--r-- | src/glutt-o-logique/ds18b20/README | 1 | ||||
-rw-r--r-- | src/glutt-o-logique/ds18b20/tm_stm32f4_ds18b20.c | 394 | ||||
-rw-r--r-- | src/glutt-o-logique/ds18b20/tm_stm32f4_ds18b20.h | 317 | ||||
-rw-r--r-- | src/glutt-o-logique/ds18b20/tm_stm32f4_onewire.c | 414 | ||||
-rw-r--r-- | src/glutt-o-logique/ds18b20/tm_stm32f4_onewire.h | 300 |
5 files changed, 1426 insertions, 0 deletions
diff --git a/src/glutt-o-logique/ds18b20/README b/src/glutt-o-logique/ds18b20/README new file mode 100644 index 0000000..0cd66d5 --- /dev/null +++ b/src/glutt-o-logique/ds18b20/README @@ -0,0 +1 @@ +This folder contains a library to access Dallas 1-wire temperature sensors. diff --git a/src/glutt-o-logique/ds18b20/tm_stm32f4_ds18b20.c b/src/glutt-o-logique/ds18b20/tm_stm32f4_ds18b20.c new file mode 100644 index 0000000..d16e48c --- /dev/null +++ b/src/glutt-o-logique/ds18b20/tm_stm32f4_ds18b20.c @@ -0,0 +1,394 @@ +/**
+ * |----------------------------------------------------------------------
+ * | Copyright (C) Tilen Majerle, 2014
+ * |
+ * | This program is free software: you can redistribute it and/or modify
+ * | it under the terms of the GNU General Public License as published by
+ * | the Free Software Foundation, either version 3 of the License, or
+ * | any later version.
+ * |
+ * | This program is distributed in the hope that it will be useful,
+ * | but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * | GNU General Public License for more details.
+ * |
+ * | You should have received a copy of the GNU General Public License
+ * | along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * |----------------------------------------------------------------------
+ */
+#include "tm_stm32f4_ds18b20.h"
+
+uint8_t TM_DS18B20_Start(TM_OneWire_t* OneWire, uint8_t *ROM) {
+ /* Check if device is DS18B20 */
+ if (!TM_DS18B20_Is(ROM)) {
+ return 0;
+ }
+
+ /* Reset line */
+ TM_OneWire_Reset(OneWire);
+ /* Select ROM number */
+ TM_OneWire_SelectWithPointer(OneWire, ROM);
+ /* Start temperature conversion */
+ TM_OneWire_WriteByte(OneWire, DS18B20_CMD_CONVERTTEMP);
+
+ return 1;
+}
+
+void TM_DS18B20_StartAll(TM_OneWire_t* OneWire) {
+ /* Reset pulse */
+ TM_OneWire_Reset(OneWire);
+ /* Skip rom */
+ TM_OneWire_WriteByte(OneWire, ONEWIRE_CMD_SKIPROM);
+ /* Start conversion on all connected devices */
+ TM_OneWire_WriteByte(OneWire, DS18B20_CMD_CONVERTTEMP);
+}
+
+uint8_t TM_DS18B20_Read(TM_OneWire_t* OneWire, uint8_t *ROM, float *destination) {
+ uint16_t temperature;
+ uint8_t resolution;
+ int8_t digit, minus = 0;
+ float decimal;
+ uint8_t i = 0;
+ uint8_t data[9];
+ uint8_t crc;
+
+ /* Check if device is DS18B20 */
+ if (!TM_DS18B20_Is(ROM)) {
+ return 0;
+ }
+
+ /* Check if line is released, if it is, then conversion is complete */
+ if (!TM_OneWire_ReadBit(OneWire)) {
+ /* Conversion is not finished yet */
+ return 0;
+ }
+
+ /* Reset line */
+ TM_OneWire_Reset(OneWire);
+ /* Select ROM number */
+ TM_OneWire_SelectWithPointer(OneWire, ROM);
+ /* Read scratchpad command by onewire protocol */
+ TM_OneWire_WriteByte(OneWire, ONEWIRE_CMD_RSCRATCHPAD);
+
+ /* Get data */
+ for (i = 0; i < 9; i++) {
+ /* Read byte by byte */
+ data[i] = TM_OneWire_ReadByte(OneWire);
+ }
+
+ /* Calculate CRC */
+ crc = TM_OneWire_CRC8(data, 8);
+
+ /* Check if CRC is ok */
+ if (crc != data[8]) {
+ /* CRC invalid */
+ return 0;
+ }
+
+ /* First two bytes of scratchpad are temperature values */
+ temperature = data[0] | (data[1] << 8);
+
+ /* Reset line */
+ TM_OneWire_Reset(OneWire);
+
+ /* Check if temperature is negative */
+ if (temperature & 0x8000) {
+ /* Two's complement, temperature is negative */
+ temperature = ~temperature + 1;
+ minus = 1;
+ }
+
+
+ /* Get sensor resolution */
+ resolution = ((data[4] & 0x60) >> 5) + 9;
+
+
+ /* Store temperature integer digits and decimal digits */
+ digit = temperature >> 4;
+ digit |= ((temperature >> 8) & 0x7) << 4;
+
+ /* Store decimal digits */
+ switch (resolution) {
+ case 9: {
+ decimal = (temperature >> 3) & 0x01;
+ decimal *= (float)DS18B20_DECIMAL_STEPS_9BIT;
+ } break;
+ case 10: {
+ decimal = (temperature >> 2) & 0x03;
+ decimal *= (float)DS18B20_DECIMAL_STEPS_10BIT;
+ } break;
+ case 11: {
+ decimal = (temperature >> 1) & 0x07;
+ decimal *= (float)DS18B20_DECIMAL_STEPS_11BIT;
+ } break;
+ case 12: {
+ decimal = temperature & 0x0F;
+ decimal *= (float)DS18B20_DECIMAL_STEPS_12BIT;
+ } break;
+ default: {
+ decimal = 0xFF;
+ digit = 0;
+ }
+ }
+
+ /* Check for negative part */
+ decimal = digit + decimal;
+ if (minus) {
+ decimal = 0 - decimal;
+ }
+
+ /* Set to pointer */
+ *destination = decimal;
+
+ /* Return 1, temperature valid */
+ return 1;
+}
+
+uint8_t TM_DS18B20_GetResolution(TM_OneWire_t* OneWire, uint8_t *ROM) {
+ uint8_t conf;
+
+ if (!TM_DS18B20_Is(ROM)) {
+ return 0;
+ }
+
+ /* Reset line */
+ TM_OneWire_Reset(OneWire);
+ /* Select ROM number */
+ TM_OneWire_SelectWithPointer(OneWire, ROM);
+ /* Read scratchpad command by onewire protocol */
+ TM_OneWire_WriteByte(OneWire, ONEWIRE_CMD_RSCRATCHPAD);
+
+ /* Ignore first 4 bytes */
+ TM_OneWire_ReadByte(OneWire);
+ TM_OneWire_ReadByte(OneWire);
+ TM_OneWire_ReadByte(OneWire);
+ TM_OneWire_ReadByte(OneWire);
+
+ /* 5th byte of scratchpad is configuration register */
+ conf = TM_OneWire_ReadByte(OneWire);
+
+ /* Return 9 - 12 value according to number of bits */
+ return ((conf & 0x60) >> 5) + 9;
+}
+
+uint8_t TM_DS18B20_SetResolution(TM_OneWire_t* OneWire, uint8_t *ROM, TM_DS18B20_Resolution_t resolution) {
+ uint8_t th, tl, conf;
+ if (!TM_DS18B20_Is(ROM)) {
+ return 0;
+ }
+
+ /* Reset line */
+ TM_OneWire_Reset(OneWire);
+ /* Select ROM number */
+ TM_OneWire_SelectWithPointer(OneWire, ROM);
+ /* Read scratchpad command by onewire protocol */
+ TM_OneWire_WriteByte(OneWire, ONEWIRE_CMD_RSCRATCHPAD);
+
+ /* Ignore first 2 bytes */
+ TM_OneWire_ReadByte(OneWire);
+ TM_OneWire_ReadByte(OneWire);
+
+ th = TM_OneWire_ReadByte(OneWire);
+ tl = TM_OneWire_ReadByte(OneWire);
+ conf = TM_OneWire_ReadByte(OneWire);
+
+ if (resolution == TM_DS18B20_Resolution_9bits) {
+ conf &= ~(1 << DS18B20_RESOLUTION_R1);
+ conf &= ~(1 << DS18B20_RESOLUTION_R0);
+ } else if (resolution == TM_DS18B20_Resolution_10bits) {
+ conf &= ~(1 << DS18B20_RESOLUTION_R1);
+ conf |= 1 << DS18B20_RESOLUTION_R0;
+ } else if (resolution == TM_DS18B20_Resolution_11bits) {
+ conf |= 1 << DS18B20_RESOLUTION_R1;
+ conf &= ~(1 << DS18B20_RESOLUTION_R0);
+ } else if (resolution == TM_DS18B20_Resolution_12bits) {
+ conf |= 1 << DS18B20_RESOLUTION_R1;
+ conf |= 1 << DS18B20_RESOLUTION_R0;
+ }
+
+ /* Reset line */
+ TM_OneWire_Reset(OneWire);
+ /* Select ROM number */
+ TM_OneWire_SelectWithPointer(OneWire, ROM);
+ /* Write scratchpad command by onewire protocol, only th, tl and conf register can be written */
+ TM_OneWire_WriteByte(OneWire, ONEWIRE_CMD_WSCRATCHPAD);
+
+ /* Write bytes */
+ TM_OneWire_WriteByte(OneWire, th);
+ TM_OneWire_WriteByte(OneWire, tl);
+ TM_OneWire_WriteByte(OneWire, conf);
+
+ /* Reset line */
+ TM_OneWire_Reset(OneWire);
+ /* Select ROM number */
+ TM_OneWire_SelectWithPointer(OneWire, ROM);
+ /* Copy scratchpad to EEPROM of DS18B20 */
+ TM_OneWire_WriteByte(OneWire, ONEWIRE_CMD_CPYSCRATCHPAD);
+
+ return 1;
+}
+
+uint8_t TM_DS18B20_Is(uint8_t *ROM) {
+ /* Checks if first byte is equal to DS18B20's family code */
+ if (*ROM == DS18B20_FAMILY_CODE) {
+ return 1;
+ }
+ return 0;
+}
+
+uint8_t TM_DS18B20_SetAlarmLowTemperature(TM_OneWire_t* OneWire, uint8_t *ROM, int8_t temp) {
+ uint8_t tl, th, conf;
+ if (!TM_DS18B20_Is(ROM)) {
+ return 0;
+ }
+ if (temp > 125) {
+ temp = 125;
+ }
+ if (temp < -55) {
+ temp = -55;
+ }
+ /* Reset line */
+ TM_OneWire_Reset(OneWire);
+ /* Select ROM number */
+ TM_OneWire_SelectWithPointer(OneWire, ROM);
+ /* Read scratchpad command by onewire protocol */
+ TM_OneWire_WriteByte(OneWire, ONEWIRE_CMD_RSCRATCHPAD);
+
+ /* Ignore first 2 bytes */
+ TM_OneWire_ReadByte(OneWire);
+ TM_OneWire_ReadByte(OneWire);
+
+ th = TM_OneWire_ReadByte(OneWire);
+ tl = TM_OneWire_ReadByte(OneWire);
+ conf = TM_OneWire_ReadByte(OneWire);
+
+ tl = (uint8_t)temp;
+
+ /* Reset line */
+ TM_OneWire_Reset(OneWire);
+ /* Select ROM number */
+ TM_OneWire_SelectWithPointer(OneWire, ROM);
+ /* Write scratchpad command by onewire protocol, only th, tl and conf register can be written */
+ TM_OneWire_WriteByte(OneWire, ONEWIRE_CMD_WSCRATCHPAD);
+
+ /* Write bytes */
+ TM_OneWire_WriteByte(OneWire, th);
+ TM_OneWire_WriteByte(OneWire, tl);
+ TM_OneWire_WriteByte(OneWire, conf);
+
+ /* Reset line */
+ TM_OneWire_Reset(OneWire);
+ /* Select ROM number */
+ TM_OneWire_SelectWithPointer(OneWire, ROM);
+ /* Copy scratchpad to EEPROM of DS18B20 */
+ TM_OneWire_WriteByte(OneWire, ONEWIRE_CMD_CPYSCRATCHPAD);
+
+ return 1;
+}
+
+uint8_t TM_DS18B20_SetAlarmHighTemperature(TM_OneWire_t* OneWire, uint8_t *ROM, int8_t temp) {
+ uint8_t tl, th, conf;
+ if (!TM_DS18B20_Is(ROM)) {
+ return 0;
+ }
+ if (temp > 125) {
+ temp = 125;
+ }
+ if (temp < -55) {
+ temp = -55;
+ }
+ /* Reset line */
+ TM_OneWire_Reset(OneWire);
+ /* Select ROM number */
+ TM_OneWire_SelectWithPointer(OneWire, ROM);
+ /* Read scratchpad command by onewire protocol */
+ TM_OneWire_WriteByte(OneWire, ONEWIRE_CMD_RSCRATCHPAD);
+
+ /* Ignore first 2 bytes */
+ TM_OneWire_ReadByte(OneWire);
+ TM_OneWire_ReadByte(OneWire);
+
+ th = TM_OneWire_ReadByte(OneWire);
+ tl = TM_OneWire_ReadByte(OneWire);
+ conf = TM_OneWire_ReadByte(OneWire);
+
+ th = (uint8_t)temp;
+
+ /* Reset line */
+ TM_OneWire_Reset(OneWire);
+ /* Select ROM number */
+ TM_OneWire_SelectWithPointer(OneWire, ROM);
+ /* Write scratchpad command by onewire protocol, only th, tl and conf register can be written */
+ TM_OneWire_WriteByte(OneWire, ONEWIRE_CMD_WSCRATCHPAD);
+
+ /* Write bytes */
+ TM_OneWire_WriteByte(OneWire, th);
+ TM_OneWire_WriteByte(OneWire, tl);
+ TM_OneWire_WriteByte(OneWire, conf);
+
+ /* Reset line */
+ TM_OneWire_Reset(OneWire);
+ /* Select ROM number */
+ TM_OneWire_SelectWithPointer(OneWire, ROM);
+ /* Copy scratchpad to EEPROM of DS18B20 */
+ TM_OneWire_WriteByte(OneWire, ONEWIRE_CMD_CPYSCRATCHPAD);
+
+ return 1;
+}
+
+uint8_t TM_DS18B20_DisableAlarmTemperature(TM_OneWire_t* OneWire, uint8_t *ROM) {
+ uint8_t tl, th, conf;
+ if (!TM_DS18B20_Is(ROM)) {
+ return 0;
+ }
+ /* Reset line */
+ TM_OneWire_Reset(OneWire);
+ /* Select ROM number */
+ TM_OneWire_SelectWithPointer(OneWire, ROM);
+ /* Read scratchpad command by onewire protocol */
+ TM_OneWire_WriteByte(OneWire, ONEWIRE_CMD_RSCRATCHPAD);
+
+ /* Ignore first 2 bytes */
+ TM_OneWire_ReadByte(OneWire);
+ TM_OneWire_ReadByte(OneWire);
+
+ th = TM_OneWire_ReadByte(OneWire);
+ tl = TM_OneWire_ReadByte(OneWire);
+ conf = TM_OneWire_ReadByte(OneWire);
+
+ th = 125;
+ tl = (uint8_t)-55;
+
+ /* Reset line */
+ TM_OneWire_Reset(OneWire);
+ /* Select ROM number */
+ TM_OneWire_SelectWithPointer(OneWire, ROM);
+ /* Write scratchpad command by onewire protocol, only th, tl and conf register can be written */
+ TM_OneWire_WriteByte(OneWire, ONEWIRE_CMD_WSCRATCHPAD);
+
+ /* Write bytes */
+ TM_OneWire_WriteByte(OneWire, th);
+ TM_OneWire_WriteByte(OneWire, tl);
+ TM_OneWire_WriteByte(OneWire, conf);
+
+ /* Reset line */
+ TM_OneWire_Reset(OneWire);
+ /* Select ROM number */
+ TM_OneWire_SelectWithPointer(OneWire, ROM);
+ /* Copy scratchpad to EEPROM of DS18B20 */
+ TM_OneWire_WriteByte(OneWire, ONEWIRE_CMD_CPYSCRATCHPAD);
+
+ return 1;
+}
+
+uint8_t TM_DS18B20_AlarmSearch(TM_OneWire_t* OneWire) {
+ /* Start alarm search */
+ return TM_OneWire_Search(OneWire, DS18B20_CMD_ALARMSEARCH);
+}
+
+uint8_t TM_DS18B20_AllDone(TM_OneWire_t* OneWire) {
+ /* If read bit is low, then device is not finished yet with calculation temperature */
+ return TM_OneWire_ReadBit(OneWire);
+}
+
+
diff --git a/src/glutt-o-logique/ds18b20/tm_stm32f4_ds18b20.h b/src/glutt-o-logique/ds18b20/tm_stm32f4_ds18b20.h new file mode 100644 index 0000000..337883e --- /dev/null +++ b/src/glutt-o-logique/ds18b20/tm_stm32f4_ds18b20.h @@ -0,0 +1,317 @@ +/**
+ * @author Tilen Majerle
+ * @email tilen@majerle.eu
+ * @website http://stm32f4-discovery.com
+ * @link http://stm32f4-discovery.com/2014/05/13-reading-temperature-with-dallas-ds18b20-on-stm32f429-discovery-board/
+ * @version v2.0
+ * @ide Keil uVision
+ * @license GNU GPL v3
+ * @brief Library for interfacing DS18B20 temperature sensor from Dallas semiconductors.
+ *
+@verbatim
+ ----------------------------------------------------------------------
+ Copyright (C) Tilen Majerle, 2015
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+ ----------------------------------------------------------------------
+@endverbatim
+ */
+/**
+ * Library for interfacing DS18B20 temperature sensor from Dallas semiconductors.
+ *
+ * @author Tilen Majerle
+ * @email tilen@majerle.eu
+ * @website http://stm32f4-discovery.com
+ * @link http://stm32f4-discovery.com/2014/05/13-reading-temperature-with-dallas-ds18b20-on-stm32f429-discovery-board/
+ * @version v2.0
+ * @ide Keil uVision
+ * @license GNU GPL v3
+ *
+ * |----------------------------------------------------------------------
+ * | Copyright (C) Tilen Majerle, 2014
+ * |
+ * | This program is free software: you can redistribute it and/or modify
+ * | it under the terms of the GNU General Public License as published by
+ * | the Free Software Foundation, either version 3 of the License, or
+ * | any later version.
+ * |
+ * | This program is distributed in the hope that it will be useful,
+ * | but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * | GNU General Public License for more details.
+ * |
+ * | You should have received a copy of the GNU General Public License
+ * | along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * |----------------------------------------------------------------------
+ *
+ * Version 2.0
+ * - January 04, 2015
+ * - New system, supporting OneWire library 2.0
+ *
+ * Version 1.1
+ * - December 06, 2014
+ * - Now CRC is calculated and checked if data are valid
+ * - New version of OneWire library is required, download already available on stm32f4-discovery.com
+ *
+ * With this you can read temperature, set and get temperature resolution from 9 to 12 bits
+ * and check if device is DS18B20
+ *
+ * Pin for STM32F4xx is the same as set with TM ONEWIRE library.
+ */
+#ifndef TM_DS18B20_H
+#define TM_DS18B20_H 200
+
+/**
+ * @addtogroup TM_STM32F4xx_Libraries
+ * @{
+ */
+
+/**
+ * @defgroup TM_DS12820
+ * @brief Library for interfacing DS18B20 temperature sensor from Dallas semiconductors - http://stm32f4-discovery.com/2014/05/13-reading-temperature-with-dallas-ds18b20-on-stm32f429-discovery-board/
+ * @{
+ *
+ * With this you can read temperature, set and get temperature resolution from 9 to 12 bits and check if device is DS18B20.
+ *
+ * Pin for STM32F4xx is the same as set with TM ONEWIRE library.
+ *
+ * \par Changelog
+ *
+@verbatim
+ Version 2.0
+ - January 04, 2015
+ - New system, supporting OneWire library 2.0
+
+ Version 1.1
+ - December 06, 2014
+ - Now CRC is calculated and checked if data are valid
+ - New version of OneWire library is required, download already available on stm32f4-discovery.com
+
+ Version 1.0
+ - First release
+@endverbatim
+ *
+ * \par Dependencies
+ *
+@verbatim
+ - STM32F4xx
+ - TM ONEWIRE
+ - TM GPIO
+@endverbatim
+ */
+
+#include "stm32f4xx.h"
+#include "tm_stm32f4_onewire.h"
+
+/* OneWire version check */
+#if TM_ONEWIRE_H < 200
+#error "Please update TM ONEWIRE LIB, minimum required version is 2.0.0. Download available on stm32f4-discovery.com website"
+#endif
+
+/**
+ * @defgroup TM_DS18B20_Macros
+ * @brief Library defines
+ * @{
+ */
+
+/* Every onewire chip has different ROM code, but all the same chips has same family code */
+/* in case of DS18B20 this is 0x28 and this is first byte of ROM address */
+#define DS18B20_FAMILY_CODE 0x28
+#define DS18B20_CMD_ALARMSEARCH 0xEC
+
+/* DS18B20 read temperature command */
+#define DS18B20_CMD_CONVERTTEMP 0x44 /* Convert temperature */
+#define DS18B20_DECIMAL_STEPS_12BIT 0.0625
+#define DS18B20_DECIMAL_STEPS_11BIT 0.125
+#define DS18B20_DECIMAL_STEPS_10BIT 0.25
+#define DS18B20_DECIMAL_STEPS_9BIT 0.5
+
+/* Bits locations for resolution */
+#define DS18B20_RESOLUTION_R1 6
+#define DS18B20_RESOLUTION_R0 5
+
+/* CRC enabled */
+#ifdef DS18B20_USE_CRC
+#define DS18B20_DATA_LEN 9
+#else
+#define DS18B20_DATA_LEN 2
+#endif
+
+/**
+ * @}
+ */
+
+/**
+ * @defgroup TM_DS18B20_Typedefs
+ * @brief Library Typedefs
+ * @{
+ */
+
+/**
+ * @brief DS18B0 Resolutions available
+ */
+typedef enum {
+ TM_DS18B20_Resolution_9bits = 9, /*!< DS18B20 9 bits resolution */
+ TM_DS18B20_Resolution_10bits = 10, /*!< DS18B20 10 bits resolution */
+ TM_DS18B20_Resolution_11bits = 11, /*!< DS18B20 11 bits resolution */
+ TM_DS18B20_Resolution_12bits = 12 /*!< DS18B20 12 bits resolution */
+} TM_DS18B20_Resolution_t;
+
+/**
+ * @}
+ */
+
+/**
+ * @defgroup TM_DS18B20_Functions
+ * @brief Library Functions
+ * @{
+ */
+
+/**
+ * @brief Starts temperature conversion for specific DS18B20 on specific onewire channel
+ * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working structure (OneWire channel)
+ * @param *ROM: Pointer to first byte of ROM address for desired DS12B80 device.
+ * Entire ROM address is 8-bytes long
+ * @retval 1 if device is DS18B20 or 0 if not
+ */
+uint8_t TM_DS18B20_Start(TM_OneWire_t* OneWireStruct, uint8_t* ROM);
+
+/**
+ * @brief Starts temperature conversion for all DS18B20 devices on specific onewire channel
+ * @note This mode will skip ROM addressing
+ * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working structure (OneWire channel)
+ * @retval None
+ */
+void TM_DS18B20_StartAll(TM_OneWire_t* OneWireStruct);
+
+/**
+ * @brief Reads temperature from DS18B20
+ * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working structure (OneWire channel)
+ * @param *ROM: Pointer to first byte of ROM address for desired DS12B80 device.
+ * Entire ROM address is 8-bytes long
+ * @param *destination: Pointer to float variable to store temperature
+ * @retval Temperature status:
+ * - 0: Device is not DS18B20 or conversion is not done yet or CRC failed
+ * - > 0: Temperature is read OK
+ */
+uint8_t TM_DS18B20_Read(TM_OneWire_t* OneWireStruct, uint8_t* ROM, float* destination);
+
+/**
+ * @brief Gets resolution for temperature conversion from DS18B20 device
+ * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working structure (OneWire channel)
+ * @param *ROM: Pointer to first byte of ROM address for desired DS12B80 device.
+ * Entire ROM address is 8-bytes long
+ * @retval Resolution:
+ * - 0: Device is not DS18B20
+ * - 9 - 12: Resolution of DS18B20
+ */
+uint8_t TM_DS18B20_GetResolution(TM_OneWire_t* OneWireStruct, uint8_t* ROM);
+
+/**
+ * @brief Sets resolution for specific DS18B20 device
+ * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working structure (OneWire channel)
+ * @param *ROM: Pointer to first byte of ROM address for desired DS12B80 device.
+ * Entire ROM address is 8-bytes long
+ * @param resolution: Resolution for DS18B20 device. This parameter can be a value of @ref TM_DS18B20_Resolution_t enumeration.
+ * @retval Success status:
+ * - 0: Device is not DS18B20
+ * - > 0: Resolution set OK
+ */
+uint8_t TM_DS18B20_SetResolution(TM_OneWire_t* OneWireStruct, uint8_t* ROM, TM_DS18B20_Resolution_t resolution);
+
+/**
+ * @brief Checks if device with specific ROM number is DS18B20
+ * @param *ROM: Pointer to first byte of ROM address for desired DS12B80 device.
+ * Entire ROM address is 8-bytes long
+ * @retval Device status
+ * - 0: Device is not DS18B20
+ * - > 0: Device is DS18B20
+ */
+uint8_t TM_DS18B20_Is(uint8_t* ROM);
+
+/**
+ * @brief Sets high alarm temperature to specific DS18B20 sensor
+ * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working structure (OneWire channel)
+ * @param *ROM: Pointer to first byte of ROM address for desired DS12B80 device.
+ * Entire ROM address is 8-bytes long
+ * @param temp: integer value for temperature between -55 to 125 degrees
+ * @retval Success status:
+ * - 0: Device is not DS18B20
+ * - > 0: High alarm set OK
+ */
+uint8_t TM_DS18B20_SetAlarmHighTemperature(TM_OneWire_t* OneWireStruct, uint8_t* ROM, int8_t temp);
+
+/**
+ * @brief Sets low alarm temperature to specific DS18B20 sensor
+ * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working structure (OneWire channel)
+ * @param *ROM: Pointer to first byte of ROM address for desired DS12B80 device.
+ * Entire ROM address is 8-bytes long
+ * @param temp: integer value for temperature between -55 to 125 degrees
+ * @retval Success status:
+ * - 0: Device is not DS18B20
+ * - > 0: Low alarm set OK
+ */
+uint8_t TM_DS18B20_SetAlarmLowTemperature(TM_OneWire_t* OneWireStruct, uint8_t* ROM, int8_t temp);
+
+/**
+ * @brief Disables alarm temperature for specific DS18B20 sensor
+ * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working structure (OneWire channel)
+ * @param *ROM: Pointer to first byte of ROM address for desired DS12B80 device.
+ * Entire ROM address is 8-bytes long
+ * @retval Success status:
+ * - 0: Device is not DS18B20
+ * - > 0: Alarm disabled OK
+ */
+uint8_t TM_DS18B20_DisableAlarmTemperature(TM_OneWire_t* OneWireStruct, uint8_t* ROM);
+
+/**
+ * @brief Searches for devices with alarm flag set
+ * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working structure (OneWire channel)
+ * @retval Alarm search status
+ * - 0: No device found with alarm flag set
+ * - > 0: Device is found with alarm flag
+ * @note To get all devices on one onewire channel with alarm flag set, you can do this:
+@verbatim
+while (TM_DS18B20_AlarmSearch(&OneWireStruct)) {
+ //Read device ID here
+ //Print to user device by device
+}
+@endverbatim
+ * @retval 1 if any device has flag, otherwise 0
+ */
+uint8_t TM_DS18B20_AlarmSearch(TM_OneWire_t* OneWireStruct);
+
+/**
+ * @brief Checks if all DS18B20 sensors are done with temperature conversion
+ * @param *OneWireStruct: Pointer to @ref TM_OneWire_t working structure (OneWire channel)
+ * @retval Conversion status
+ * - 0: Not all devices are done
+ * - > 0: All devices are done with conversion
+ */
+uint8_t TM_DS18B20_AllDone(TM_OneWire_t* OneWireStruct);
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+#endif
+
diff --git a/src/glutt-o-logique/ds18b20/tm_stm32f4_onewire.c b/src/glutt-o-logique/ds18b20/tm_stm32f4_onewire.c new file mode 100644 index 0000000..44ec8db --- /dev/null +++ b/src/glutt-o-logique/ds18b20/tm_stm32f4_onewire.c @@ -0,0 +1,414 @@ +/**
+ * |----------------------------------------------------------------------
+ * | Copyright (C) Tilen Majerle, 2014
+ * |
+ * | This program is free software: you can redistribute it and/or modify
+ * | it under the terms of the GNU General Public License as published by
+ * | the Free Software Foundation, either version 3 of the License, or
+ * | any later version.
+ * |
+ * | This program is distributed in the hope that it will be useful,
+ * | but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * | GNU General Public License for more details.
+ * |
+ * | You should have received a copy of the GNU General Public License
+ * | along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * |----------------------------------------------------------------------
+ */
+#include "tm_stm32f4_onewire.h"
+
+void delay_us(uint32_t);
+
+static void
+TM_GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
+{
+ GPIO_SetBits(GPIOx, GPIO_Pin);
+}
+
+static void
+TM_GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
+{
+ GPIO_ResetBits(GPIOx, GPIO_Pin);
+}
+
+
+static void
+TM_GPIO_SetPinAsInput(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
+{
+ GPIO_SetBits(GPIOx, GPIO_Pin);
+}
+
+static void
+TM_GPIO_SetPinAsOutput(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
+{
+#if 0
+ GPIO_InitTypeDef GPIO_InitStructure;
+ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
+ GPIO_InitStructure.GPIO_Pin = GPIO_Pin;
+ GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
+ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
+ GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
+ GPIO_Init(GPIOx, &GPIO_InitStructure);
+#endif
+ GPIO_ResetBits(GPIOx, GPIO_Pin);
+}
+
+static int
+TM_GPIO_GetInputPinValue(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
+{
+ return GPIO_ReadInputDataBit(GPIOx, GPIO_Pin) ? 1 : 0;
+}
+
+static void
+ONEWIRE_DELAY(uint32_t micros)
+{
+ delay_us(micros);
+}
+
+void TM_OneWire_Init(TM_OneWire_t* OneWireStruct, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) {
+ ONEWIRE_INPUT(OneWireStruct);
+ /* Save settings */
+ OneWireStruct->GPIOx = GPIOx;
+ OneWireStruct->GPIO_Pin = GPIO_Pin;
+}
+
+uint8_t TM_OneWire_Reset(TM_OneWire_t* OneWireStruct) {
+ uint8_t i;
+
+ /* Line low, and wait 480us */
+ ONEWIRE_LOW(OneWireStruct);
+ ONEWIRE_OUTPUT(OneWireStruct);
+ ONEWIRE_DELAY(480);
+
+ /* Release line and wait for 70us */
+ ONEWIRE_INPUT(OneWireStruct);
+ ONEWIRE_DELAY(70);
+
+ /* Check bit value */
+ i = TM_GPIO_GetInputPinValue(OneWireStruct->GPIOx, OneWireStruct->GPIO_Pin);
+
+ /* Delay for 410 us */
+ ONEWIRE_DELAY(410);
+
+ /* Return value of presence pulse, 0 = OK, 1 = ERROR */
+ return i;
+}
+
+void TM_OneWire_WriteBit(TM_OneWire_t* OneWireStruct, uint8_t bit) {
+ if (bit) {
+ /* Set line low */
+ ONEWIRE_LOW(OneWireStruct);
+ ONEWIRE_OUTPUT(OneWireStruct);
+ ONEWIRE_DELAY(10);
+
+ /* Bit high */
+ ONEWIRE_INPUT(OneWireStruct);
+
+ /* Wait for 55 us and release the line */
+ ONEWIRE_DELAY(55);
+ ONEWIRE_INPUT(OneWireStruct);
+ } else {
+ /* Set line low */
+ ONEWIRE_LOW(OneWireStruct);
+ ONEWIRE_OUTPUT(OneWireStruct);
+ ONEWIRE_DELAY(65);
+
+ /* Bit high */
+ ONEWIRE_INPUT(OneWireStruct);
+
+ /* Wait for 5 us and release the line */
+ ONEWIRE_DELAY(5);
+ ONEWIRE_INPUT(OneWireStruct);
+ }
+
+}
+
+uint8_t TM_OneWire_ReadBit(TM_OneWire_t* OneWireStruct) {
+ uint8_t bit = 0;
+
+ /* Line low */
+ ONEWIRE_LOW(OneWireStruct);
+ ONEWIRE_OUTPUT(OneWireStruct);
+ ONEWIRE_DELAY(3);
+
+ /* Release line */
+ ONEWIRE_INPUT(OneWireStruct);
+ ONEWIRE_DELAY(10);
+
+ /* Read line value */
+ if (TM_GPIO_GetInputPinValue(OneWireStruct->GPIOx, OneWireStruct->GPIO_Pin)) {
+ /* Bit is HIGH */
+ bit = 1;
+ }
+
+ /* Wait 50us to complete 60us period */
+ ONEWIRE_DELAY(50);
+
+ /* Return bit value */
+ return bit;
+}
+
+void TM_OneWire_WriteByte(TM_OneWire_t* OneWireStruct, uint8_t byte) {
+ uint8_t i = 8;
+ /* Write 8 bits */
+ while (i--) {
+ /* LSB bit is first */
+ TM_OneWire_WriteBit(OneWireStruct, byte & 0x01);
+ byte >>= 1;
+ }
+}
+
+uint8_t TM_OneWire_ReadByte(TM_OneWire_t* OneWireStruct) {
+ uint8_t i = 8, byte = 0;
+ while (i--) {
+ byte >>= 1;
+ byte |= (TM_OneWire_ReadBit(OneWireStruct) << 7);
+ }
+
+ return byte;
+}
+
+uint8_t TM_OneWire_First(TM_OneWire_t* OneWireStruct) {
+ /* Reset search values */
+ TM_OneWire_ResetSearch(OneWireStruct);
+
+ /* Start with searching */
+ return TM_OneWire_Search(OneWireStruct, ONEWIRE_CMD_SEARCHROM);
+}
+
+uint8_t TM_OneWire_Next(TM_OneWire_t* OneWireStruct) {
+ /* Leave the search state alone */
+ return TM_OneWire_Search(OneWireStruct, ONEWIRE_CMD_SEARCHROM);
+}
+
+void TM_OneWire_ResetSearch(TM_OneWire_t* OneWireStruct) {
+ /* Reset the search state */
+ OneWireStruct->LastDiscrepancy = 0;
+ OneWireStruct->LastDeviceFlag = 0;
+ OneWireStruct->LastFamilyDiscrepancy = 0;
+}
+
+uint8_t TM_OneWire_Search(TM_OneWire_t* OneWireStruct, uint8_t command) {
+ uint8_t id_bit_number;
+ uint8_t last_zero, rom_byte_number, search_result;
+ uint8_t id_bit, cmp_id_bit;
+ uint8_t rom_byte_mask, search_direction;
+
+ /* Initialize for search */
+ id_bit_number = 1;
+ last_zero = 0;
+ rom_byte_number = 0;
+ rom_byte_mask = 1;
+ search_result = 0;
+
+ // if the last call was not the last one
+ if (!OneWireStruct->LastDeviceFlag) {
+ // 1-Wire reset
+ if (TM_OneWire_Reset(OneWireStruct)) {
+ /* Reset the search */
+ OneWireStruct->LastDiscrepancy = 0;
+ OneWireStruct->LastDeviceFlag = 0;
+ OneWireStruct->LastFamilyDiscrepancy = 0;
+ return 0;
+ }
+
+ // issue the search command
+ TM_OneWire_WriteByte(OneWireStruct, command);
+
+ // loop to do the search
+ do {
+ // read a bit and its complement
+ id_bit = TM_OneWire_ReadBit(OneWireStruct);
+ cmp_id_bit = TM_OneWire_ReadBit(OneWireStruct);
+
+ // check for no devices on 1-wire
+ if ((id_bit == 1) && (cmp_id_bit == 1)) {
+ break;
+ } else {
+ // all devices coupled have 0 or 1
+ if (id_bit != cmp_id_bit) {
+ search_direction = id_bit; // bit write value for search
+ } else {
+ // if this discrepancy if before the Last Discrepancy
+ // on a previous next then pick the same as last time
+ if (id_bit_number < OneWireStruct->LastDiscrepancy) {
+ search_direction = ((OneWireStruct->ROM_NO[rom_byte_number] & rom_byte_mask) > 0);
+ } else {
+ // if equal to last pick 1, if not then pick 0
+ search_direction = (id_bit_number == OneWireStruct->LastDiscrepancy);
+ }
+
+ // if 0 was picked then record its position in LastZero
+ if (search_direction == 0) {
+ last_zero = id_bit_number;
+
+ // check for Last discrepancy in family
+ if (last_zero < 9) {
+ OneWireStruct->LastFamilyDiscrepancy = last_zero;
+ }
+ }
+ }
+
+ // set or clear the bit in the ROM byte rom_byte_number
+ // with mask rom_byte_mask
+ if (search_direction == 1) {
+ OneWireStruct->ROM_NO[rom_byte_number] |= rom_byte_mask;
+ } else {
+ OneWireStruct->ROM_NO[rom_byte_number] &= ~rom_byte_mask;
+ }
+
+ // serial number search direction write bit
+ TM_OneWire_WriteBit(OneWireStruct, search_direction);
+
+ // increment the byte counter id_bit_number
+ // and shift the mask rom_byte_mask
+ id_bit_number++;
+ rom_byte_mask <<= 1;
+
+ // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
+ if (rom_byte_mask == 0) {
+ //docrc8(ROM_NO[rom_byte_number]); // accumulate the CRC
+ rom_byte_number++;
+ rom_byte_mask = 1;
+ }
+ }
+ } while (rom_byte_number < 8); // loop until through all ROM bytes 0-7
+
+ // if the search was successful then
+ if (!(id_bit_number < 65)) {
+ // search successful so set LastDiscrepancy,LastDeviceFlag,search_result
+ OneWireStruct->LastDiscrepancy = last_zero;
+
+ // check for last device
+ if (OneWireStruct->LastDiscrepancy == 0) {
+ OneWireStruct->LastDeviceFlag = 1;
+ }
+
+ search_result = 1;
+ }
+ }
+
+ // if no device found then reset counters so next 'search' will be like a first
+ if (!search_result || !OneWireStruct->ROM_NO[0]) {
+ OneWireStruct->LastDiscrepancy = 0;
+ OneWireStruct->LastDeviceFlag = 0;
+ OneWireStruct->LastFamilyDiscrepancy = 0;
+ search_result = 0;
+ }
+
+ return search_result;
+}
+
+int TM_OneWire_Verify(TM_OneWire_t* OneWireStruct) {
+ unsigned char rom_backup[8];
+ int i,rslt,ld_backup,ldf_backup,lfd_backup;
+
+ // keep a backup copy of the current state
+ for (i = 0; i < 8; i++)
+ rom_backup[i] = OneWireStruct->ROM_NO[i];
+ ld_backup = OneWireStruct->LastDiscrepancy;
+ ldf_backup = OneWireStruct->LastDeviceFlag;
+ lfd_backup = OneWireStruct->LastFamilyDiscrepancy;
+
+ // set search to find the same device
+ OneWireStruct->LastDiscrepancy = 64;
+ OneWireStruct->LastDeviceFlag = 0;
+
+ if (TM_OneWire_Search(OneWireStruct, ONEWIRE_CMD_SEARCHROM)) {
+ // check if same device found
+ rslt = 1;
+ for (i = 0; i < 8; i++) {
+ if (rom_backup[i] != OneWireStruct->ROM_NO[i]) {
+ rslt = 1;
+ break;
+ }
+ }
+ } else {
+ rslt = 0;
+ }
+
+ // restore the search state
+ for (i = 0; i < 8; i++) {
+ OneWireStruct->ROM_NO[i] = rom_backup[i];
+ }
+ OneWireStruct->LastDiscrepancy = ld_backup;
+ OneWireStruct->LastDeviceFlag = ldf_backup;
+ OneWireStruct->LastFamilyDiscrepancy = lfd_backup;
+
+ // return the result of the verify
+ return rslt;
+}
+
+void TM_OneWire_TargetSetup(TM_OneWire_t* OneWireStruct, uint8_t family_code) {
+ uint8_t i;
+
+ // set the search state to find SearchFamily type devices
+ OneWireStruct->ROM_NO[0] = family_code;
+ for (i = 1; i < 8; i++) {
+ OneWireStruct->ROM_NO[i] = 0;
+ }
+
+ OneWireStruct->LastDiscrepancy = 64;
+ OneWireStruct->LastFamilyDiscrepancy = 0;
+ OneWireStruct->LastDeviceFlag = 0;
+}
+
+void TM_OneWire_FamilySkipSetup(TM_OneWire_t* OneWireStruct) {
+ // set the Last discrepancy to last family discrepancy
+ OneWireStruct->LastDiscrepancy = OneWireStruct->LastFamilyDiscrepancy;
+ OneWireStruct->LastFamilyDiscrepancy = 0;
+
+ // check for end of list
+ if (OneWireStruct->LastDiscrepancy == 0) {
+ OneWireStruct->LastDeviceFlag = 1;
+ }
+}
+
+uint8_t TM_OneWire_GetROM(TM_OneWire_t* OneWireStruct, uint8_t index) {
+ return OneWireStruct->ROM_NO[index];
+}
+
+void TM_OneWire_Select(TM_OneWire_t* OneWireStruct, uint8_t* addr) {
+ uint8_t i;
+ TM_OneWire_WriteByte(OneWireStruct, ONEWIRE_CMD_MATCHROM);
+
+ for (i = 0; i < 8; i++) {
+ TM_OneWire_WriteByte(OneWireStruct, *(addr + i));
+ }
+}
+
+void TM_OneWire_SelectWithPointer(TM_OneWire_t* OneWireStruct, uint8_t *ROM) {
+ uint8_t i;
+ TM_OneWire_WriteByte(OneWireStruct, ONEWIRE_CMD_MATCHROM);
+
+ for (i = 0; i < 8; i++) {
+ TM_OneWire_WriteByte(OneWireStruct, *(ROM + i));
+ }
+}
+
+void TM_OneWire_GetFullROM(TM_OneWire_t* OneWireStruct, uint8_t *firstIndex) {
+ uint8_t i;
+ for (i = 0; i < 8; i++) {
+ *(firstIndex + i) = OneWireStruct->ROM_NO[i];
+ }
+}
+
+uint8_t TM_OneWire_CRC8(uint8_t *addr, uint8_t len) {
+ uint8_t crc = 0, inbyte, i, mix;
+
+ while (len--) {
+ inbyte = *addr++;
+ for (i = 8; i; i--) {
+ mix = (crc ^ inbyte) & 0x01;
+ crc >>= 1;
+ if (mix) {
+ crc ^= 0x8C;
+ }
+ inbyte >>= 1;
+ }
+ }
+
+ /* Return calculated CRC */
+ return crc;
+}
diff --git a/src/glutt-o-logique/ds18b20/tm_stm32f4_onewire.h b/src/glutt-o-logique/ds18b20/tm_stm32f4_onewire.h new file mode 100644 index 0000000..9b14109 --- /dev/null +++ b/src/glutt-o-logique/ds18b20/tm_stm32f4_onewire.h @@ -0,0 +1,300 @@ +/**
+ * @author Tilen Majerle
+ * @email tilen@majerle.eu
+ * @website http://stm32f4-discovery.com
+ * @link http://stm32f4-discovery.com/2014/05/library-12-onewire-library-for-stm43f4xx/
+ * @version v2.1
+ * @ide Keil uVision
+ * @license GNU GPL v3
+ * @brief Onewire library for STM32F4 devices
+ *
+@verbatim
+ ----------------------------------------------------------------------
+ Copyright (C) Tilen Majerle, 2015
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+ ----------------------------------------------------------------------
+@endverbatim
+ */
+#ifndef TM_ONEWIRE_H
+#define TM_ONEWIRE_H 210
+
+/* C++ detection */
+#ifdef __cplusplus
+extern C {
+#endif
+
+/**
+ * @addtogroup TM_STM32F4xx_Libraries
+ * @{
+ */
+
+/**
+ * @defgroup TM_ONEWIRE
+ * @brief Onewire library for STM32F4 devices - http://stm32f4-discovery.com/2014/05/library-12-onewire-library-for-stm43f4xx/
+ * @{
+ *
+ * As of version 2.0 you can now use more than just one one-wire "port" on STM32F4. This allows you to group devices to separate ports.
+ *
+ * Because if you have a loot devices on one port, if one device fail, everything is failed. You can prevent this by use more than just one port.
+ *
+ * To set your port and pin for OneWire protocol, you can do this when calling @ref TM_OneWire_Init function.
+ *
+ * \par Changelog
+ *
+@verbatim
+ Version 2.1
+ - March 10, 2015
+ - Added support for new GPIO library
+
+ Version 2.0
+ - January 04, 2015
+ - New OneWire system
+ - With support for multiple OneWire ports to separate group of devices
+
+ Version 1.1
+ - December 06, 2014
+ - Added 8-bit CRC calculation for 1-Wire devices, algorithm from Dallas
+
+ Version 1.0
+ - First release
+@endverbatim
+ *
+ * \par Dependencies
+ *
+@verbatim
+ - STM32F4xx
+ - STM32F4xx RCC
+ - STM32F4xx GPIO
+ - TM GPIO
+@endverbatim
+ */
+#include "stm32f4xx.h"
+#include "stm32f4xx_rcc.h"
+#include "stm32f4xx_gpio.h"
+
+/**
+ * @defgroup TM_ONEWIRE_Macros
+ * @brief Library defines
+ * @{
+ */
+
+/* Pin settings */
+
+#define ONEWIRE_LOW(structure) TM_GPIO_ResetBits((structure)->GPIOx, (structure)->GPIO_Pin)
+#define ONEWIRE_HIGH(structure) TM_GPIO_SetBits((structure)->GPIOx, (structure)->GPIO_Pin)
+#define ONEWIRE_INPUT(structure) TM_GPIO_SetPinAsInput(structure->GPIOx, (structure)->GPIO_Pin)
+#define ONEWIRE_OUTPUT(structure) TM_GPIO_SetPinAsOutput(structure->GPIOx, (structure)->GPIO_Pin)
+
+/* OneWire commands */
+#define ONEWIRE_CMD_RSCRATCHPAD 0xBE
+#define ONEWIRE_CMD_WSCRATCHPAD 0x4E
+#define ONEWIRE_CMD_CPYSCRATCHPAD 0x48
+#define ONEWIRE_CMD_RECEEPROM 0xB8
+#define ONEWIRE_CMD_RPWRSUPPLY 0xB4
+#define ONEWIRE_CMD_SEARCHROM 0xF0
+#define ONEWIRE_CMD_READROM 0x33
+#define ONEWIRE_CMD_MATCHROM 0x55
+#define ONEWIRE_CMD_SKIPROM 0xCC
+
+/**
+ * @}
+ */
+
+/**
+ * @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);
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+/* C++ detection */
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+
|