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