Добавлен файлы ow_port для реализации задержек и переключения пинов для конкретного МК

Реализована универсанльная функция дли переинициализации порта
This commit is contained in:
2025-02-06 15:21:36 +03:00
parent 586907b339
commit 95050988dd
8 changed files with 294 additions and 169 deletions

View File

@@ -49,56 +49,139 @@ void Dallas_ReadAll(void)
void Dallas_FirstInit(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); OneWire_Init(&OW);
DS18B20_Search(&DS, &OW); DS18B20_Search(&DS, &OW);
Dallas_SensorInitByInd(&OW, &AllSens.outdoor, 0);
Dallas_WriteUserBytes(&AllSens.outdoor, 1, NULL, mask); Dallas_SensorInitByROM(&OW, &AllSens.outdoor, 0x28C60C61060000DC);
Dallas_SensorInitByInd(&OW, &AllSens.indoor, 1); Dallas_SensorInitByROM(&OW, &AllSens.indoor, 0x283E4861060000F9);
Dallas_WriteUserBytes(&AllSens.indoor, 2, NULL, mask); Dallas_SensorInitByROM(&OW, &AllSens.bathroom, 0x28876D60060000CD);
Dallas_SensorInitByInd(&OW, &AllSens.bathroom, 2);
Dallas_WriteUserBytes(&AllSens.bathroom, 3, NULL, mask);
Dallas_SensorInitByInd(&OW, &AllSens.kitchen, 3); uint8_t mask = DALLAS_USER_BYTE_ALL;
Dallas_WriteUserBytes(&AllSens.kitchen, 4, NULL, mask); // Dallas_SensorInitByUserBytes(&OW, &AllSens.outdoor, 1, NULL);
Dallas_SensorInitByInd(&OW, &AllSens.big_room, 4); // Dallas_SensorInitByUserBytes(&OW, &AllSens.indoor, 2, NULL);
Dallas_WriteUserBytes(&AllSens.big_room, 5, NULL, mask); // Dallas_SensorInitByUserBytes(&OW, &AllSens.bathroom, 3, NULL);
Dallas_SensorInitByInd(&OW, &AllSens.small_room, 5);
Dallas_WriteUserBytes(&AllSens.small_room, 6, NULL, mask); // Dallas_SensorInitByInd(&OW, &AllSens.outdoor, 0);
Dallas_SensorInitByInd(&OW, &AllSens.living_room, 6); // Dallas_SensorInitByInd(&OW, &AllSens.indoor, 1);
Dallas_WriteUserBytes(&AllSens.living_room, 7, NULL, mask); // Dallas_SensorInitByInd(&OW, &AllSens.bathroom, 2);
Dallas_SensorInitByInd(&OW, &AllSens.basement, 7); // Dallas_SensorInitByInd(&OW, &AllSens.kitchen, 3);
Dallas_WriteUserBytes(&AllSens.basement, 8, NULL, mask); // 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 Èíèöèàëèçèðóåò ñòðóêòóðó äàò÷èêà ïî èíäåêó * @brief Èíèöèàëèçèðóåò ñòðóêòóðó äàò÷èêà ïî èíäåêó
* @param onewire Óêàçàòåëü íà ñòðóêòóðó OneWire * @param onewire Óêàçàòåëü íà ñòðóêòóðó OneWire
* @param sensor Óêàçàòåëü íà ñòðóêòóðó äàò÷èêà * @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 UserBytes34 Ïîëüçîâàòåëüñêèå áàéòû 3 è 4, NULL äëÿ èãíîðà
* @param UserBytes12 Ïîëüçîâàòåëüñêèå áàéòû 1 è 2, NULL äëÿ èãíîðà * @param UserBytes12 Ïîëüçîâàòåëüñêèå áàéòû 1 è 2, NULL äëÿ èãíîðà
* @retval HAL Status * @retval HAL Status
* @details ñòàðøèé áàéò - UserByte4/UserByte2, ìëàäøèé - UserByte3/UserByte1. * @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; 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 UserByte1 = UserBytes12 & 0xFF;
uint8_t UserByte2 = UserBytes12 >> 8; uint8_t UserByte2 = UserBytes12 >> 8;
uint8_t UserByte3 = UserBytes34 & 0xFF; uint8_t UserByte3 = UserBytes34 & 0xFF;
uint8_t UserByte4 = UserBytes34 >> 8; uint8_t UserByte4 = UserBytes34 >> 8;
uint8_t UserByte12Cmp = 0; uint8_t UserByte12Cmp = 0;
uint8_t UserByte34Cmp = 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++) for(int i = 0; i < DS18B20_DEVICE_AMOUNT; i++)
{ {
UserByte12Cmp = 0; UserByte34Cmp = 0; UserByte12Cmp = 0; UserByte34Cmp = 0;
sensor->sensROM = &DS.DevAddr[i]; sensor->sensROM = &DS.DevAddr[i];
sensor->onewire = onewire;
/* Ïðîâåðêà ïðèñóòñòâóåò ëè âûáðàííûé äàò÷èê íà ëèíèè */ /* Ïðîâåðêà ïðèñóòñòâóåò ëè âûáðàííûé äàò÷èê íà ëèíèè */
result = Dallas_IsConnected(sensor); result = Dallas_IsConnected(sensor);
@@ -135,6 +218,7 @@ HAL_StatusTypeDef Dallas_SensorInitByUserBytes(OneWire_t *onewire, DALLAS_Handle
/* Åñëè íàøëè íóæíûé äàò÷èê - çàâåðøàåì ïîèñê */ /* Åñëè íàøëè íóæíûé äàò÷èê - çàâåðøàåì ïîèñê */
if(UserByte12Cmp && UserByte34Cmp) if(UserByte12Cmp && UserByte34Cmp)
{ {
sensor->isInitialized = 1;
return HAL_OK; 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 Dallas_SensorInitByInd(OneWire_t *onewire, DALLAS_HandleTypeDef *sensor, uint8_t sens_ind)
{ {
HAL_StatusTypeDef result; HAL_StatusTypeDef result;
sensor->isInitialized = 0;
if(onewire == NULL) if(onewire == NULL)
return HAL_ERROR; return HAL_ERROR;
if(sensor == NULL) if(sensor == NULL)
return HAL_ERROR; return HAL_ERROR;
sensor->onewire = onewire;
sensor->sensROM = &DS.DevAddr[sens_ind]; sensor->sensROM = &DS.DevAddr[sens_ind];
sensor->onewire = onewire;
/* Ïðîâåðêà ïðèñóòñòâóåò ëè âûáðàííûé äàò÷èê íà ëèíèè */ /* Ïðîâåðêà ïðèñóòñòâóåò ëè âûáðàííûé äàò÷èê íà ëèíèè */
result = Dallas_IsConnected(sensor); result = Dallas_IsConnected(sensor);
if (result != HAL_OK) if (result != HAL_OK)
return result; return result;
sensor->isInitialized = 1;
return HAL_OK; return HAL_OK;
} }

View File

@@ -27,7 +27,7 @@
#define DALLAS_USER_BYTE_ALL (DALLAS_USER_BYTE_12|DALLAS_USER_BYTE_34) #define DALLAS_USER_BYTE_ALL (DALLAS_USER_BYTE_12|DALLAS_USER_BYTE_34)
/* Declarations and definitions ---------------------------------------------*/ /* 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_LEN_BYTES 6
#define DALLAS_SERIAL_NUMBER_OFFSET_BYTES 1 #define DALLAS_SERIAL_NUMBER_OFFSET_BYTES 1
@@ -88,13 +88,14 @@ typedef struct
typedef struct typedef struct
{ {
uint8_t isConnected; uint8_t isConnected;
uint8_t isInitialized;
#ifdef ONEWIRE_UART_H #ifdef ONEWIRE_UART_H
UART_HandleTypeDef *uart; UART_HandleTypeDef *uart;
#else #else
OneWire_t *onewire; OneWire_t *onewire;
#endif #endif
uint8_t (*sensROM)[DALLAS_ROM_LEN_BYTES]; uint8_t (*sensROM)[DALLAS_ROM_SIZE];
DALLAS_ScratchpadTypeDef scratchpad; DALLAS_ScratchpadTypeDef scratchpad;
float temperature; float temperature;
@@ -127,7 +128,8 @@ typedef enum
void Dallas_Init(DALLAS_HandleTypeDef *sensor, UART_HandleTypeDef *huart); void Dallas_Init(DALLAS_HandleTypeDef *sensor, UART_HandleTypeDef *huart);
void Dallas_ReadAll(void); void Dallas_ReadAll(void);
void Dallas_FirstInit(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_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_StartConvertTAll(OneWire_t *onewire, DALLAS_WaitCondition waitCondition, uint8_t dallas_delay_ms);
HAL_StatusTypeDef Dallas_ConvertT(DALLAS_HandleTypeDef *sensor, DALLAS_WaitCondition waitCondition); HAL_StatusTypeDef Dallas_ConvertT(DALLAS_HandleTypeDef *sensor, DALLAS_WaitCondition waitCondition);

View File

@@ -13,7 +13,7 @@
#endif #endif
/* Includes ------------------------------------------------------------------*/ /* Includes ------------------------------------------------------------------*/
#include "main.h" #include "stm32f1xx_hal.h"
/* Custom Define -------------------------------------------------------------*/ /* Custom Define -------------------------------------------------------------*/
#define DWT_LAR_UNLOCK (uint32_t)0xC5ACCE55 #define DWT_LAR_UNLOCK (uint32_t)0xC5ACCE55

View File

@@ -5,127 +5,6 @@
****************************************************************************** ******************************************************************************
*/ */
#include "onewire.h" #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 * @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) void OneWire_WriteBit(OneWire_t* OW, uint8_t bit)
{ {
if(OW == NULL)
return;
#ifndef ONEWIRE_UART_H #ifndef ONEWIRE_UART_H
if(bit) if(bit)
{ {
@@ -142,13 +23,13 @@ void OneWire_WriteBit(OneWire_t* OW, uint8_t bit)
OneWire_Pin_Mode(OW, Output); OneWire_Pin_Mode(OW, Output);
/* Forming pulse */ /* Forming pulse */
DwtDelay_us(ONEWIRE_WRITE_1_US); OneWireDelay_uw(ONEWIRE_WRITE_1_US);
/* Release line (pull up line) */ /* Release line (pull up line) */
OneWire_Pin_Mode(OW, Input); OneWire_Pin_Mode(OW, Input);
/* Wait for 55 us and release the line */ /* 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); OneWire_Pin_Mode(OW, Input);
}else{ }else{
/* Set line low */ /* Set line low */
@@ -156,13 +37,13 @@ void OneWire_WriteBit(OneWire_t* OW, uint8_t bit)
OneWire_Pin_Mode(OW, Output); OneWire_Pin_Mode(OW, Output);
/* Forming pulse */ /* Forming pulse */
DwtDelay_us(ONEWIRE_WRITE_0_US); OneWireDelay_uw(ONEWIRE_WRITE_0_US);
/* Release line (pull up line) */ /* Release line (pull up line) */
OneWire_Pin_Mode(OW, Input); OneWire_Pin_Mode(OW, Input);
/* Wait for 5 us and release the line */ /* 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); OneWire_Pin_Mode(OW, Input);
} }
#else #else
@@ -177,22 +58,25 @@ void OneWire_WriteBit(OneWire_t* OW, uint8_t bit)
*/ */
uint8_t OneWire_ReadBit(OneWire_t* OW) uint8_t OneWire_ReadBit(OneWire_t* OW)
{ {
if(OW == NULL)
return 0;
uint8_t bit = 0; uint8_t bit = 0;
#ifndef ONEWIRE_UART_H #ifndef ONEWIRE_UART_H
/* Line low */ /* Line low */
OneWire_Pin_Level(OW, 0); OneWire_Pin_Level(OW, 0);
OneWire_Pin_Mode(OW, Output); OneWire_Pin_Mode(OW, Output);
DwtDelay_us(ONEWIRE_READ_CMD_US); OneWireDelay_uw(ONEWIRE_READ_CMD_US);
/* Release line */ /* Release line */
OneWire_Pin_Mode(OW, Input); OneWire_Pin_Mode(OW, Input);
DwtDelay_us(ONEWIRE_READ_DELAY_US); OneWireDelay_uw(ONEWIRE_READ_DELAY_US);
/* Read line value */ /* Read line value */
bit = OneWire_Pin_Read(OW); bit = OneWire_Pin_Read(OW);
/* Wait 50us to complete 60us period */ /* 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 #else
bit = OneWireUART_ProcessBit(onewire_uart, 1); bit = OneWireUART_ProcessBit(onewire_uart, 1);
#endif #endif
@@ -207,6 +91,9 @@ uint8_t OneWire_ReadBit(OneWire_t* OW)
*/ */
void OneWire_WriteByte(OneWire_t* OW, uint8_t byte) void OneWire_WriteByte(OneWire_t* OW, uint8_t byte)
{ {
if(OW == NULL)
return;
#ifndef ONEWIRE_UART_H #ifndef ONEWIRE_UART_H
uint8_t bit = 8; uint8_t bit = 8;
/* Write 8 bits */ /* Write 8 bits */
@@ -227,6 +114,9 @@ void OneWire_WriteByte(OneWire_t* OW, uint8_t byte)
*/ */
uint8_t OneWire_ReadByte(OneWire_t* OW) uint8_t OneWire_ReadByte(OneWire_t* OW)
{ {
if(OW == NULL)
return 0;
uint8_t byte = 0; uint8_t byte = 0;
#ifndef ONEWIRE_UART_H #ifndef ONEWIRE_UART_H
uint8_t bit = 8; uint8_t bit = 8;
@@ -248,21 +138,24 @@ uint8_t OneWire_ReadByte(OneWire_t* OW)
*/ */
uint8_t OneWire_Reset(OneWire_t* OW) uint8_t OneWire_Reset(OneWire_t* OW)
{ {
if(OW == NULL)
return 1;
#ifndef ONEWIRE_UART_H #ifndef ONEWIRE_UART_H
/* Line low, and wait 480us */ /* Line low, and wait 480us */
OneWire_Pin_Level(OW, 0); OneWire_Pin_Level(OW, 0);
OneWire_Pin_Mode(OW, Output); OneWire_Pin_Mode(OW, Output);
DwtDelay_us(ONEWIRE_RESET_PULSE_US); OneWireDelay_uw(ONEWIRE_RESET_PULSE_US);
/* Release line and wait for 70us */ /* Release line and wait for 70us */
OneWire_Pin_Mode(OW, Input); OneWire_Pin_Mode(OW, Input);
DwtDelay_us(ONEWIRE_PRESENCE_WAIT_US); OneWireDelay_uw(ONEWIRE_PRESENCE_WAIT_US);
/* Check bit value */ /* Check bit value */
uint8_t rslt = OneWire_Pin_Read(OW); uint8_t rslt = OneWire_Pin_Read(OW);
/* Delay for 410 us */ /* Delay for 410 us */
DwtDelay_us(ONEWIRE_PRESENCE_DURATION_US); OneWireDelay_uw(ONEWIRE_PRESENCE_DURATION_US);
#else #else
uint8_t rslt = 0; 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) uint8_t OneWire_Search(OneWire_t* OW, uint8_t Cmd)
{ {
if(OW == NULL)
return 0;
uint8_t id_bit_number = 1; uint8_t id_bit_number = 1;
uint8_t last_zero = 0; uint8_t last_zero = 0;
uint8_t rom_byte_number = 0; uint8_t rom_byte_number = 0;
@@ -416,11 +312,11 @@ void OneWire_Init(OneWire_t* OW)
{ {
OneWire_Pin_Mode(OW, Output); OneWire_Pin_Mode(OW, Output);
OneWire_Pin_Level(OW, 1); OneWire_Pin_Level(OW, 1);
DwtDelay_us(1000); OneWireDelay_uw(1000);
OneWire_Pin_Level(OW, 0); OneWire_Pin_Level(OW, 0);
DwtDelay_us(1000); OneWireDelay_uw(1000);
OneWire_Pin_Level(OW, 1); OneWire_Pin_Level(OW, 1);
DwtDelay_us(2000); OneWireDelay_uw(2000);
/* Reset the search state */ /* Reset the search state */
OW->LastDiscrepancy = 0; OW->LastDiscrepancy = 0;

View File

@@ -18,7 +18,7 @@
#endif #endif
/* Includes ------------------------------------------------------------------*/ /* Includes ------------------------------------------------------------------*/
#include "dwt.h" #include "ow_port.h"
/* Driver Selection ----------------------------------------------------------*/ /* Driver Selection ----------------------------------------------------------*/
//#define LL_Driver //#define LL_Driver

120
DS18B20/ow_port.c Normal file
View File

@@ -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
}

24
DS18B20/ow_port.h Normal file
View File

@@ -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 */

View File

@@ -95,9 +95,6 @@ int main(void)
__HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE();
DwtInit(); DwtInit();
/* Set parameter and initialize DS18B20 */ /* 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 */ /* Initialize OneWire and reset all data */
Dallas_FirstInit(); Dallas_FirstInit();
/* USER CODE END 2 */ /* USER CODE END 2 */