From 95050988dd5566f705268e4b87ebd4bf32560ac8 Mon Sep 17 00:00:00 2001 From: Razvalyaev Date: Thu, 6 Feb 2025 15:21:36 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20ow=5Fport=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D0=B8=20=D0=B7=D0=B0=D0=B4=D0=B5=D1=80=D0=B6=D0=B5=D0=BA?= =?UTF-8?q?=20=D0=B8=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BA=D0=BB=D1=8E=D1=87?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BF=D0=B8=D0=BD=D0=BE=D0=B2=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D0=BA=D0=BE=D0=BD=D0=BA=D1=80=D0=B5=D1=82?= =?UTF-8?q?=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=9C=D0=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Реализована универсанльная функция дли переинициализации порта --- DS18B20/dallas_tools.c | 140 ++++++++++++++++++++++++------ DS18B20/dallas_tools.h | 8 +- DS18B20/dwt.h | 2 +- DS18B20/onewire.c | 164 +++++++---------------------------- DS18B20/onewire.h | 2 +- DS18B20/ow_port.c | 120 +++++++++++++++++++++++++ DS18B20/ow_port.h | 24 +++++ test_project/Core/Src/main.c | 3 - 8 files changed, 294 insertions(+), 169 deletions(-) create mode 100644 DS18B20/ow_port.c create mode 100644 DS18B20/ow_port.h diff --git a/DS18B20/dallas_tools.c b/DS18B20/dallas_tools.c index 701d6bf..c0c00d1 100644 --- a/DS18B20/dallas_tools.c +++ b/DS18B20/dallas_tools.c @@ -49,56 +49,139 @@ void Dallas_ReadAll(void) void Dallas_FirstInit(void) { - uint8_t mask = DALLAS_USER_BYTE_ALL; + OW.DataPin = DS_Pin; + OW.DataPort = DS_GPIO_Port; + DS.Resolution = DS18B20_RESOLUTION_9BITS; + OneWire_Init(&OW); DS18B20_Search(&DS, &OW); - Dallas_SensorInitByInd(&OW, &AllSens.outdoor, 0); - Dallas_WriteUserBytes(&AllSens.outdoor, 1, NULL, mask); - Dallas_SensorInitByInd(&OW, &AllSens.indoor, 1); - Dallas_WriteUserBytes(&AllSens.indoor, 2, NULL, mask); - Dallas_SensorInitByInd(&OW, &AllSens.bathroom, 2); - Dallas_WriteUserBytes(&AllSens.bathroom, 3, NULL, mask); - Dallas_SensorInitByInd(&OW, &AllSens.kitchen, 3); - Dallas_WriteUserBytes(&AllSens.kitchen, 4, NULL, mask); - Dallas_SensorInitByInd(&OW, &AllSens.big_room, 4); - Dallas_WriteUserBytes(&AllSens.big_room, 5, NULL, mask); - Dallas_SensorInitByInd(&OW, &AllSens.small_room, 5); - Dallas_WriteUserBytes(&AllSens.small_room, 6, NULL, mask); - Dallas_SensorInitByInd(&OW, &AllSens.living_room, 6); - Dallas_WriteUserBytes(&AllSens.living_room, 7, NULL, mask); - Dallas_SensorInitByInd(&OW, &AllSens.basement, 7); - Dallas_WriteUserBytes(&AllSens.basement, 8, NULL, mask); + + Dallas_SensorInitByROM(&OW, &AllSens.outdoor, 0x28C60C61060000DC); + Dallas_SensorInitByROM(&OW, &AllSens.indoor, 0x283E4861060000F9); + Dallas_SensorInitByROM(&OW, &AllSens.bathroom, 0x28876D60060000CD); + + + uint8_t mask = DALLAS_USER_BYTE_ALL; +// Dallas_SensorInitByUserBytes(&OW, &AllSens.outdoor, 1, NULL); +// Dallas_SensorInitByUserBytes(&OW, &AllSens.indoor, 2, NULL); +// Dallas_SensorInitByUserBytes(&OW, &AllSens.bathroom, 3, NULL); + +// Dallas_SensorInitByInd(&OW, &AllSens.outdoor, 0); +// Dallas_SensorInitByInd(&OW, &AllSens.indoor, 1); +// Dallas_SensorInitByInd(&OW, &AllSens.bathroom, 2); +// Dallas_SensorInitByInd(&OW, &AllSens.kitchen, 3); +// Dallas_SensorInitByInd(&OW, &AllSens.big_room, 4); +// Dallas_SensorInitByInd(&OW, &AllSens.small_room, 5); +// Dallas_SensorInitByInd(&OW, &AllSens.living_room, 6); +// Dallas_SensorInitByInd(&OW, &AllSens.basement, 7); +// +// +// Dallas_WriteUserBytes(&AllSens.outdoor, 1, NULL, mask); +// Dallas_WriteUserBytes(&AllSens.indoor, 2, NULL, mask); +// Dallas_WriteUserBytes(&AllSens.bathroom, 3, NULL, mask); +// Dallas_WriteUserBytes(&AllSens.kitchen, 4, NULL, mask); +// Dallas_WriteUserBytes(&AllSens.big_room, 5, NULL, mask); +// Dallas_WriteUserBytes(&AllSens.small_room, 6, NULL, mask); +// Dallas_WriteUserBytes(&AllSens.living_room, 7, NULL, mask); +// Dallas_WriteUserBytes(&AllSens.basement, 8, NULL, mask); } + /** * @brief * @param onewire OneWire * @param sensor + * @param ROM ROM + * @retval HAL Status + */ +HAL_StatusTypeDef Dallas_SensorInitByROM(OneWire_t *onewire, DALLAS_HandleTypeDef *sensor, uint64_t intROM) +{ + HAL_StatusTypeDef result; + sensor->isInitialized = 0; + if(onewire == NULL) + return HAL_ERROR; + if(sensor == NULL) + return HAL_ERROR; + sensor->onewire = onewire; + + uint8_t ROM[8] = {0}; + ROM[0] = (intROM >> (7*8)) & 0xFF; + ROM[1] = (intROM >> (6*8)) & 0xFF; + ROM[2] = (intROM >> (5*8)) & 0xFF; + ROM[3] = (intROM >> (4*8)) & 0xFF; + ROM[4] = (intROM >> (3*8)) & 0xFF; + ROM[5] = (intROM >> (2*8)) & 0xFF; + ROM[6] = (intROM >> (1*8)) & 0xFF; + ROM[7] = (intROM) & 0xFF; + + + uint8_t comparebytes = DALLAS_ROM_SIZE; + for(int i = 0; i < DS18B20_DEVICE_AMOUNT; i++) + { + comparebytes = DALLAS_ROM_SIZE; + for(int rom_byte = 0; rom_byte < DALLAS_ROM_SIZE; rom_byte++) + { + if(DS.DevAddr[i][rom_byte] == ROM[rom_byte]) + comparebytes--; + } + if(comparebytes == 0) + { + sensor->sensROM = &DS.DevAddr[i]; + break; + } + } + /* */ + + if(comparebytes == 0) + { + result = Dallas_IsConnected(sensor); + if(result != HAL_OK) + return result; + + sensor->isInitialized = 1; + return HAL_OK; + } + else + { + return HAL_ERROR; + } + + +} + + + +/** + * @brief + * @param onewire OneWire + * @param sensor * @param UserBytes34 3 4, NULL * @param UserBytes12 1 2, NULL * @retval HAL Status * @details - UserByte4/UserByte2, - UserByte3/UserByte1. */ -HAL_StatusTypeDef Dallas_SensorInitByUserBytes(OneWire_t *onewire, DALLAS_HandleTypeDef *sensor, uint16_t UserBytes34, uint16_t UserBytes12) +HAL_StatusTypeDef Dallas_SensorInitByUserBytes(OneWire_t *onewire, DALLAS_HandleTypeDef *sensor, uint16_t UserBytes12, uint16_t UserBytes34) { HAL_StatusTypeDef result; + sensor->isInitialized = 0; + + if(onewire == NULL) + return HAL_ERROR; + if(sensor == NULL) + return HAL_ERROR; + sensor->onewire = onewire; + uint8_t UserByte1 = UserBytes12 & 0xFF; uint8_t UserByte2 = UserBytes12 >> 8; uint8_t UserByte3 = UserBytes34 & 0xFF; uint8_t UserByte4 = UserBytes34 >> 8; uint8_t UserByte12Cmp = 0; uint8_t UserByte34Cmp = 0; - - if(onewire == NULL) - return HAL_ERROR; - if(sensor == NULL) - return HAL_ERROR; - + for(int i = 0; i < DS18B20_DEVICE_AMOUNT; i++) { UserByte12Cmp = 0; UserByte34Cmp = 0; sensor->sensROM = &DS.DevAddr[i]; - sensor->onewire = onewire; /* */ result = Dallas_IsConnected(sensor); @@ -135,6 +218,7 @@ HAL_StatusTypeDef Dallas_SensorInitByUserBytes(OneWire_t *onewire, DALLAS_Handle /* - */ if(UserByte12Cmp && UserByte34Cmp) { + sensor->isInitialized = 1; return HAL_OK; } } @@ -154,20 +238,22 @@ HAL_StatusTypeDef Dallas_SensorInitByUserBytes(OneWire_t *onewire, DALLAS_Handle HAL_StatusTypeDef Dallas_SensorInitByInd(OneWire_t *onewire, DALLAS_HandleTypeDef *sensor, uint8_t sens_ind) { HAL_StatusTypeDef result; + sensor->isInitialized = 0; if(onewire == NULL) return HAL_ERROR; if(sensor == NULL) - return HAL_ERROR; + return HAL_ERROR; + sensor->onewire = onewire; sensor->sensROM = &DS.DevAddr[sens_ind]; - sensor->onewire = onewire; /* */ result = Dallas_IsConnected(sensor); if (result != HAL_OK) return result; + sensor->isInitialized = 1; return HAL_OK; } diff --git a/DS18B20/dallas_tools.h b/DS18B20/dallas_tools.h index 3bb7d4e..c729c79 100644 --- a/DS18B20/dallas_tools.h +++ b/DS18B20/dallas_tools.h @@ -27,7 +27,7 @@ #define DALLAS_USER_BYTE_ALL (DALLAS_USER_BYTE_12|DALLAS_USER_BYTE_34) /* Declarations and definitions ---------------------------------------------*/ -#define DALLAS_ROM_LEN_BYTES 8 +#define DALLAS_ROM_SIZE 8 #define DALLAS_SERIAL_NUMBER_LEN_BYTES 6 #define DALLAS_SERIAL_NUMBER_OFFSET_BYTES 1 @@ -88,13 +88,14 @@ typedef struct typedef struct { uint8_t isConnected; + uint8_t isInitialized; #ifdef ONEWIRE_UART_H UART_HandleTypeDef *uart; #else OneWire_t *onewire; #endif - uint8_t (*sensROM)[DALLAS_ROM_LEN_BYTES]; + uint8_t (*sensROM)[DALLAS_ROM_SIZE]; DALLAS_ScratchpadTypeDef scratchpad; float temperature; @@ -127,7 +128,8 @@ typedef enum void Dallas_Init(DALLAS_HandleTypeDef *sensor, UART_HandleTypeDef *huart); void Dallas_ReadAll(void); void Dallas_FirstInit(void); -HAL_StatusTypeDef Dallas_SensorInitByUserBytes(OneWire_t *onewire, DALLAS_HandleTypeDef *sensor, uint16_t UserBytes34, uint16_t UserBytes12); +HAL_StatusTypeDef Dallas_SensorInitByROM(OneWire_t *onewire, DALLAS_HandleTypeDef *sensor, uint64_t intROM); +HAL_StatusTypeDef Dallas_SensorInitByUserBytes(OneWire_t *onewire, DALLAS_HandleTypeDef *sensor, uint16_t UserBytes12, uint16_t UserBytes34); HAL_StatusTypeDef Dallas_SensorInitByInd(OneWire_t *onewire, DALLAS_HandleTypeDef *sensor, uint8_t sens_ind); HAL_StatusTypeDef Dallas_StartConvertTAll(OneWire_t *onewire, DALLAS_WaitCondition waitCondition, uint8_t dallas_delay_ms); HAL_StatusTypeDef Dallas_ConvertT(DALLAS_HandleTypeDef *sensor, DALLAS_WaitCondition waitCondition); diff --git a/DS18B20/dwt.h b/DS18B20/dwt.h index f49b9c7..4e71716 100644 --- a/DS18B20/dwt.h +++ b/DS18B20/dwt.h @@ -13,7 +13,7 @@ #endif /* Includes ------------------------------------------------------------------*/ -#include "main.h" +#include "stm32f1xx_hal.h" /* Custom Define -------------------------------------------------------------*/ #define DWT_LAR_UNLOCK (uint32_t)0xC5ACCE55 diff --git a/DS18B20/onewire.c b/DS18B20/onewire.c index bdeffb5..abcfd04 100644 --- a/DS18B20/onewire.c +++ b/DS18B20/onewire.c @@ -5,127 +5,6 @@ ****************************************************************************** */ #include "onewire.h" -//#include "onewire_uart.h" - -/** - * @brief The internal function is used as gpio pin mode - * @param OW OneWire HandleTypedef - * @param Mode Input or Output - */ -void OneWire_Pin_Mode(OneWire_t* OW, PinMode Mode) -{ -// GPIOA->CRH &= ~((GPIO_CRH_CNF9 | GPIO_CRH_MODE9)); -// GPIOA->CRH |= (2 << GPIO_CRH_CNF9_Pos); -#ifdef CMSIS_Driver - if(Mode == Input) - { - GPIOA->CRH &= ~((GPIO_CRH_CNF9 | GPIO_CRH_MODE9)); - GPIOA->CRH |= (1 << GPIO_CRH_CNF9_Pos); - }else{ - GPIOA->CRH &= ~((GPIO_CRH_CNF9 | GPIO_CRH_MODE9)); - GPIOA->CRH |= (3 << GPIO_CRH_MODE9_Pos); - } -// HAL_GPIO_Init(OW->DataPort, &GPIO_InitStruct); -// static uint32_t pin_numb = 0; -// static int get_pin_numb = 1; -// -// if(get_pin_numb) -// { -// get_pin_numb = 0; -// for(int i = 0; i < 16; i++) -// { -// if((OW->DataPin >> i) == 0x1) -// pin_numb = i; -// } -// } -// -// uint32_t config = 0; -// __IO uint32_t *configregister; /* Store the address of CRL or CRH register based on pin number */ -// uint32_t registeroffset; /* offset used during computation of CNF and MODE bits placement inside CRL or CRH register */ -// uint32_t iocurrent; -// -// if(Mode == Input) -// { -// config = 0; -// }else{ -// config = GPIO_SPEED_FREQ_HIGH; -// } -// /* Check if the current bit belongs to first half or last half of the pin count number -// in order to address CRH or CRL register*/ -// configregister = (OW->DataPin < GPIO_PIN_8) ? &OW->DataPort->CRL : &OW->DataPort->CRH; -// registeroffset = (OW->DataPin < GPIO_PIN_8) ? (pin_numb << 2u) : ((pin_numb - 8u) << 2u); -// /* Apply the new configuration of the pin to the register */ -// MODIFY_REG((*configregister), ((GPIO_CRL_MODE0 | GPIO_CRL_CNF0) << registeroffset), (config << registeroffset)); -#else -#ifdef LL_Driver - if(Mode == Input) - { - LL_GPIO_SetPinMode(OW->DataPort, OW->DataPin, LL_GPIO_MODE_INPUT); - }else{ - LL_GPIO_SetPinMode(OW->DataPort, OW->DataPin, LL_GPIO_MODE_OUTPUT); - } -#else - GPIO_InitTypeDef GPIO_InitStruct = {0}; - GPIO_InitStruct.Pin = OW->DataPin; - if(Mode == Input) - { - GPIO_InitStruct.Mode = GPIO_MODE_INPUT; - }else{ - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; - } - HAL_GPIO_Init(OW->DataPort, &GPIO_InitStruct); -#endif -#endif -} - -/** - * @brief The internal function is used as gpio pin level - * @param OW OneWire HandleTypedef - * @param Mode Level: Set/High = 1, Reset/Low = 0 - */ -void OneWire_Pin_Level(OneWire_t* OW, uint8_t Level) -{ -#ifdef CMSIS_Driver - if (Level != GPIO_PIN_RESET) - { - OW->DataPort->BSRR = OW->DataPin; - } - else - { - OW->DataPort->BSRR = (uint32_t)OW->DataPin << 16u; - } -#else -#ifdef LL_Driver - if(Level == 1) - { - LL_GPIO_SetOutputPin(OW->DataPort, OW->DataPin); - }else{ - LL_GPIO_ResetOutputPin(OW->DataPort, OW->DataPin); - } -#else - HAL_GPIO_WritePin(OW->DataPort, OW->DataPin, Level); -#endif -#endif -} - -/** - * @brief The internal function is used to read data pin - * @retval Pin level status - * @param OW OneWire HandleTypedef - */ -uint8_t OneWire_Pin_Read(OneWire_t* OW) -{ -#ifdef CMSIS_Driver - return ((OW->DataPort->IDR & OW->DataPin) != 0x00U) ? 1 : 0; -#else -#ifdef LL_Driver - return ((OW->DataPort->IDR & OW->DataPin) != 0x00U) ? 1 : 0; -#else - return HAL_GPIO_ReadPin(OW->DataPort, OW->DataPin); -#endif -#endif -} /** * @brief The internal function is used to write bit @@ -134,6 +13,8 @@ uint8_t OneWire_Pin_Read(OneWire_t* OW) */ void OneWire_WriteBit(OneWire_t* OW, uint8_t bit) { + if(OW == NULL) + return; #ifndef ONEWIRE_UART_H if(bit) { @@ -142,13 +23,13 @@ void OneWire_WriteBit(OneWire_t* OW, uint8_t bit) OneWire_Pin_Mode(OW, Output); /* Forming pulse */ - DwtDelay_us(ONEWIRE_WRITE_1_US); + OneWireDelay_uw(ONEWIRE_WRITE_1_US); /* Release line (pull up line) */ OneWire_Pin_Mode(OW, Input); /* Wait for 55 us and release the line */ - DwtDelay_us(ONEWIRE_COMMAND_SLOT_US - ONEWIRE_WRITE_1_US); + OneWireDelay_uw(ONEWIRE_COMMAND_SLOT_US - ONEWIRE_WRITE_1_US); OneWire_Pin_Mode(OW, Input); }else{ /* Set line low */ @@ -156,13 +37,13 @@ void OneWire_WriteBit(OneWire_t* OW, uint8_t bit) OneWire_Pin_Mode(OW, Output); /* Forming pulse */ - DwtDelay_us(ONEWIRE_WRITE_0_US); + OneWireDelay_uw(ONEWIRE_WRITE_0_US); /* Release line (pull up line) */ OneWire_Pin_Mode(OW, Input); /* Wait for 5 us and release the line */ - DwtDelay_us(ONEWIRE_COMMAND_SLOT_US - ONEWIRE_WRITE_0_US); + OneWireDelay_uw(ONEWIRE_COMMAND_SLOT_US - ONEWIRE_WRITE_0_US); OneWire_Pin_Mode(OW, Input); } #else @@ -177,22 +58,25 @@ void OneWire_WriteBit(OneWire_t* OW, uint8_t bit) */ uint8_t OneWire_ReadBit(OneWire_t* OW) { + if(OW == NULL) + return 0; + uint8_t bit = 0; #ifndef ONEWIRE_UART_H /* Line low */ OneWire_Pin_Level(OW, 0); OneWire_Pin_Mode(OW, Output); - DwtDelay_us(ONEWIRE_READ_CMD_US); + OneWireDelay_uw(ONEWIRE_READ_CMD_US); /* Release line */ OneWire_Pin_Mode(OW, Input); - DwtDelay_us(ONEWIRE_READ_DELAY_US); + OneWireDelay_uw(ONEWIRE_READ_DELAY_US); /* Read line value */ bit = OneWire_Pin_Read(OW); /* Wait 50us to complete 60us period */ - DwtDelay_us(ONEWIRE_COMMAND_SLOT_US - ONEWIRE_READ_CMD_US - ONEWIRE_READ_DELAY_US); + OneWireDelay_uw(ONEWIRE_COMMAND_SLOT_US - ONEWIRE_READ_CMD_US - ONEWIRE_READ_DELAY_US); #else bit = OneWireUART_ProcessBit(onewire_uart, 1); #endif @@ -207,6 +91,9 @@ uint8_t OneWire_ReadBit(OneWire_t* OW) */ void OneWire_WriteByte(OneWire_t* OW, uint8_t byte) { + if(OW == NULL) + return; + #ifndef ONEWIRE_UART_H uint8_t bit = 8; /* Write 8 bits */ @@ -227,6 +114,9 @@ void OneWire_WriteByte(OneWire_t* OW, uint8_t byte) */ uint8_t OneWire_ReadByte(OneWire_t* OW) { + if(OW == NULL) + return 0; + uint8_t byte = 0; #ifndef ONEWIRE_UART_H uint8_t bit = 8; @@ -248,21 +138,24 @@ uint8_t OneWire_ReadByte(OneWire_t* OW) */ uint8_t OneWire_Reset(OneWire_t* OW) { + if(OW == NULL) + return 1; + #ifndef ONEWIRE_UART_H /* Line low, and wait 480us */ OneWire_Pin_Level(OW, 0); OneWire_Pin_Mode(OW, Output); - DwtDelay_us(ONEWIRE_RESET_PULSE_US); + OneWireDelay_uw(ONEWIRE_RESET_PULSE_US); /* Release line and wait for 70us */ OneWire_Pin_Mode(OW, Input); - DwtDelay_us(ONEWIRE_PRESENCE_WAIT_US); + OneWireDelay_uw(ONEWIRE_PRESENCE_WAIT_US); /* Check bit value */ uint8_t rslt = OneWire_Pin_Read(OW); /* Delay for 410 us */ - DwtDelay_us(ONEWIRE_PRESENCE_DURATION_US); + OneWireDelay_uw(ONEWIRE_PRESENCE_DURATION_US); #else uint8_t rslt = 0; @@ -282,6 +175,9 @@ uint8_t OneWire_Reset(OneWire_t* OW) */ uint8_t OneWire_Search(OneWire_t* OW, uint8_t Cmd) { + if(OW == NULL) + return 0; + uint8_t id_bit_number = 1; uint8_t last_zero = 0; uint8_t rom_byte_number = 0; @@ -416,11 +312,11 @@ void OneWire_Init(OneWire_t* OW) { OneWire_Pin_Mode(OW, Output); OneWire_Pin_Level(OW, 1); - DwtDelay_us(1000); + OneWireDelay_uw(1000); OneWire_Pin_Level(OW, 0); - DwtDelay_us(1000); + OneWireDelay_uw(1000); OneWire_Pin_Level(OW, 1); - DwtDelay_us(2000); + OneWireDelay_uw(2000); /* Reset the search state */ OW->LastDiscrepancy = 0; diff --git a/DS18B20/onewire.h b/DS18B20/onewire.h index f1217d5..1452bc0 100644 --- a/DS18B20/onewire.h +++ b/DS18B20/onewire.h @@ -18,7 +18,7 @@ #endif /* Includes ------------------------------------------------------------------*/ -#include "dwt.h" +#include "ow_port.h" /* Driver Selection ----------------------------------------------------------*/ //#define LL_Driver diff --git a/DS18B20/ow_port.c b/DS18B20/ow_port.c new file mode 100644 index 0000000..f48a0c3 --- /dev/null +++ b/DS18B20/ow_port.c @@ -0,0 +1,120 @@ +/** + ****************************************************************************** + * @file ow_port.c + * @brief This file includes the driver for port for OneWire purposes + ****************************************************************************** + */ +#include "ow_port.h" +#include "onewire.h" + +/** + * @brief The internal function is used as gpio pin mode + * @param OW OneWire HandleTypedef + * @param Mode Input or Output + */ +void OneWire_Pin_Mode(OneWire_t* OW, PinMode Mode) +{ +#ifdef CMSIS_Driver + static uint32_t pin_cr_numb = 4; + static int get_pin_numb = 0; + + if(get_pin_numb) + { + get_pin_numb = 0; + for(int i = 0; i < 8; i++) + { + if((OW->DataPin >> i) == 0x1) + pin_cr_numb = i*4; + } + for(int i = 8; i < 16; i++) + { + if((OW->DataPin >> i) == 0x1) + pin_cr_numb = (i-8)*4; + } + } + if(Mode == Input) + { + OW->DataPort->CRH &= ~((GPIO_CRL_CNF0 | GPIO_CRL_MODE0) << pin_cr_numb); + OW->DataPort->CRH |= (1 << (pin_cr_numb+2)); + }else{ + OW->DataPort->CRH &= ~((GPIO_CRL_CNF0 | GPIO_CRL_MODE0) << pin_cr_numb); + OW->DataPort->CRH |= (3 << pin_cr_numb); + } +// if(Mode == Input) +// { +// GPIOA->CRH &= ~((GPIO_CRH_CNF9 | GPIO_CRH_MODE9)); +// GPIOA->CRH |= (1 << GPIO_CRH_CNF9_Pos); +// }else{ +// GPIOA->CRH &= ~((GPIO_CRH_CNF9 | GPIO_CRH_MODE9)); +// GPIOA->CRH |= (3 << GPIO_CRH_MODE9_Pos); +// } +#else +#ifdef LL_Driver + if(Mode == Input) + { + LL_GPIO_SetPinMode(OW->DataPort, OW->DataPin, LL_GPIO_MODE_INPUT); + }else{ + LL_GPIO_SetPinMode(OW->DataPort, OW->DataPin, LL_GPIO_MODE_OUTPUT); + } +#else + GPIO_InitTypeDef GPIO_InitStruct = {0}; + GPIO_InitStruct.Pin = OW->DataPin; + if(Mode == Input) + { + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + }else{ + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; + } + HAL_GPIO_Init(OW->DataPort, &GPIO_InitStruct); +#endif +#endif +} + +/** + * @brief The internal function is used as gpio pin level + * @param OW OneWire HandleTypedef + * @param Mode Level: Set/High = 1, Reset/Low = 0 + */ +void OneWire_Pin_Level(OneWire_t* OW, uint8_t Level) +{ +#ifdef CMSIS_Driver + if (Level != GPIO_PIN_RESET) + { + OW->DataPort->BSRR = OW->DataPin; + } + else + { + OW->DataPort->BSRR = (uint32_t)OW->DataPin << 16u; + } +#else +#ifdef LL_Driver + if(Level == 1) + { + LL_GPIO_SetOutputPin(OW->DataPort, OW->DataPin); + }else{ + LL_GPIO_ResetOutputPin(OW->DataPort, OW->DataPin); + } +#else + HAL_GPIO_WritePin(OW->DataPort, OW->DataPin, Level); +#endif +#endif +} + +/** + * @brief The internal function is used to read data pin + * @retval Pin level status + * @param OW OneWire HandleTypedef + */ +uint8_t OneWire_Pin_Read(OneWire_t* OW) +{ +#ifdef CMSIS_Driver + return ((OW->DataPort->IDR & OW->DataPin) != 0x00U) ? 1 : 0; +#else +#ifdef LL_Driver + return ((OW->DataPort->IDR & OW->DataPin) != 0x00U) ? 1 : 0; +#else + return HAL_GPIO_ReadPin(OW->DataPort, OW->DataPin); +#endif +#endif +} \ No newline at end of file diff --git a/DS18B20/ow_port.h b/DS18B20/ow_port.h new file mode 100644 index 0000000..93c3381 --- /dev/null +++ b/DS18B20/ow_port.h @@ -0,0 +1,24 @@ +/** + ****************************************************************************** + * @file onewire.h + * @brief This file contains all the constants parameters for the OneWire + ****************************************************************************** + * @attention + * Usage: + * Uncomment LL Driver for HAL driver + * + ****************************************************************************** + */ +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef ONEWIRE_PORT_H +#define ONEWIRE_PORT_H + + +/* Includes ------------------------------------------------------------------*/ +#include "stm32f1xx_hal.h" +#include "dwt.h" + +/* OneWire Timings -----------------------------------------------------------*/ +#define OneWireDelay_uw(_us_) DwtDelay_us(_us_) +/* Common Register -----------------------------------------------------------*/ +#endif /* ONEWIRE_PORT_H */ diff --git a/test_project/Core/Src/main.c b/test_project/Core/Src/main.c index af96cf0..c4a4b8c 100644 --- a/test_project/Core/Src/main.c +++ b/test_project/Core/Src/main.c @@ -95,9 +95,6 @@ int main(void) __HAL_RCC_GPIOB_CLK_ENABLE(); DwtInit(); /* Set parameter and initialize DS18B20 */ - OW.DataPin = DS_Pin; - OW.DataPort = DS_GPIO_Port; - DS.Resolution = DS18B20_Resolution_12bits; /* Initialize OneWire and reset all data */ Dallas_FirstInit(); /* USER CODE END 2 */