Добавлен файлы ow_port для реализации задержек и переключения пинов для конкретного МК
Реализована универсанльная функция дли переинициализации порта
This commit is contained in:
120
DS18B20/ow_port.c
Normal file
120
DS18B20/ow_port.c
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user