重构端口类Port的移植逻辑

This commit is contained in:
大石头X2 2017-01-06 21:44:34 +08:00
parent 0d07f28e47
commit f1a1908615
3 changed files with 85 additions and 64 deletions

View File

@ -3,6 +3,9 @@
/******************************** Port ********************************/
// 保护引脚,别的功能要使用时将会报错。返回是否保护成功
static bool Port_Reserve(Pin pin, bool flag);
// 端口基本功能
#define REGION_Port 1
#ifdef REGION_Port
@ -76,10 +79,11 @@ bool Port::Open()
// 保护引脚
//Show();
GetType().Name().Show();
Reserve(_Pin, true);
Port_Reserve(_Pin, true);
#endif
OpenPin();
Opening();
OnOpen();
Opened = true;
@ -97,12 +101,20 @@ void Port::Close()
// 保护引脚
//Show();
GetType().Name().Show();
Reserve(_Pin, false);
Port_Reserve(_Pin, false);
debug_printf("\r\n");
#endif
Opened = false;
}
WEAK void Port::Opening() {}
WEAK void Port::OnOpen() {}
WEAK void Port::OnClose() {}
WEAK void Port::RemapConfig(uint param, bool sta) {}
WEAK void Port::AFConfig(GPIO_AF GPIO_AF) const {}
#endif
// 端口引脚保护
@ -112,7 +124,7 @@ void Port::Close()
static ushort Reserved[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF};
// 保护引脚,别的功能要使用时将会报错。返回是否保护成功
bool Port::Reserve(Pin pin, bool flag)
bool Port_Reserve(Pin pin, bool flag)
{
debug_printf("::");
int port = pin >> 4, bit = 1 << (pin & 0x0F);
@ -134,12 +146,12 @@ bool Port::Reserve(Pin pin, bool flag)
return true;
}
// 引脚是否被保护
/*// 引脚是否被保护
bool Port::IsBusy(Pin pin)
{
int port = pin >> 4, sh = pin & 0x0F;
return (Reserved[port] >> sh) & 1;
}
}*/
#endif
/******************************** OutputPort ********************************/
@ -172,7 +184,7 @@ OutputPort& OutputPort::Init(Pin pin, bool invert)
return *this;
}
void OutputPort::OnOpen(void* param)
void OutputPort::OnOpen()
{
TS("OutputPort::OnOpen");
@ -209,9 +221,16 @@ void OutputPort::OnOpen(void* param)
debug_printf(" 初始电平=%d \r\n", rs);
#endif
Port::OnOpen(param);
Port::OnOpen();
OpenPin(param);
OpenPin();
}
WEAK bool OutputPort::ReadInput() const
{
if(Empty()) return false;
return Port::Read() ^ Invert;
}
void OutputPort::Up(uint ms) const
@ -267,12 +286,7 @@ AlternatePort::AlternatePort(Pin pin, byte invert, bool openDrain, byte speed)
}
}
void AlternatePort::OnOpen(void* param)
{
OutputPort::OnOpen(param);
OpenPin(param);
}
WEAK void AlternatePort::OpenPin() { OutputPort::OpenPin(); }
#endif
@ -379,7 +393,7 @@ static void InputNoIRQTask(void* param)
port->OnPress(port->Read());
}
void InputPort::OnOpen(void* param)
void InputPort::OnOpen()
{
TS("InputPort::OnOpen");
@ -399,8 +413,8 @@ void InputPort::OnOpen(void* param)
bool fg = false;
#endif
Port::OnOpen(param);
OpenPin(param);
Port::OnOpen();
OpenPin();
// 根据倒置情况来获取初始状态,自动判断是否倒置
bool rs = Port::Read();
@ -459,13 +473,13 @@ bool InputPort::UsePress()
/******************************** AnalogInPort ********************************/
void AnalogInPort::OnOpen(void* param)
void AnalogInPort::OnOpen()
{
#if DEBUG
debug_printf("\r\n");
#endif
Port::OnOpen(param);
Port::OnOpen();
OpenPin(param);
OpenPin();
}

View File

@ -9,6 +9,19 @@
#define GPIO_MAX_SPEED 50
#endif
/******** 端口打开关闭流程 ********/
/*
Port::Open
#Port::Opening
OutputPort::OnOpen
#Port::OnOpen
#OutputPort::OpenPin
Port::Close
#Port::OnClose
*/
/******************************** Port ********************************/
// 端口基类
@ -32,7 +45,7 @@ public:
Pin _Pin; // 引脚
bool Opened; // 是否已经打开
byte Index; // 引脚自身次序编号,用于区分多引脚次序
void* State; // 用户状态数据。常用于批量端口操作时记录索引
void* State; // 用户状态数据
Port();
#ifndef TINY
@ -51,22 +64,15 @@ public:
virtual bool Read() const;
#if DEBUG
// 保护引脚,别的功能要使用时将会报错。返回是否保护成功
static bool Reserve(Pin pin, bool flag);
static bool IsBusy(Pin pin); // 引脚是否被保护
#endif
virtual String& ToStr(String& str) const;
protected:
// 配置过程由Open调用最后GPIO_Init
virtual void OnOpen(void* param);
// 配置过程
virtual void OnOpen();
virtual void OnClose();
private:
void OpenPin();
void Opening();
};
/******************************** OutputPort ********************************/
@ -103,10 +109,10 @@ public:
operator bool() const { return Read(); }
protected:
virtual void OnOpen(void* param);
virtual void OnOpen();
virtual void OpenPin();
private:
void OpenPin(void* param);
};
/******************************** AlternatePort ********************************/
@ -120,10 +126,10 @@ public:
AlternatePort(Pin pin, byte invert, bool openDrain = false, byte speed = GPIO_MAX_SPEED);
protected:
virtual void OnOpen(void* param);
//virtual void OnOpen();
virtual void OpenPin();
private:
void OpenPin(void* param);
};
/******************************** InputPort ********************************/
@ -138,7 +144,6 @@ public:
UP = 0x01, // 上拉电阻
DOWN = 0x02, // 下拉电阻
}PuPd;
//enum class Trigger // 强类型枚举
typedef enum
{
Rising = 0x01, // 上升沿
@ -174,7 +179,7 @@ public:
operator bool() const { return Read(); }
protected:
virtual void OnOpen(void* param);
virtual void OnOpen();
virtual void OnClose();
private:
@ -186,7 +191,7 @@ private:
static void InputTask(void* param);
private:
void OpenPin(void* param);
void OpenPin();
void ClosePin();
bool OnRegister();
};
@ -201,10 +206,10 @@ public:
AnalogInPort(Pin pin) : Port() { Set(pin); Open(); }
protected:
virtual void OnOpen(void* param);
virtual void OnOpen();
private:
void OpenPin(void* param);
void OpenPin();
};
/******************************** PortScope ********************************/

View File

@ -59,7 +59,7 @@ static void OpenClock(Pin pin, bool flag)
}
// 确定配置,确认用对象内部的参数进行初始化
void Port::OpenPin()
void Port::Opening()
{
// 先打开时钟才能配置
OpenClock(_Pin, true);
@ -68,14 +68,12 @@ void Port::OpenPin()
// 特别要慎重,有些结构体成员可能因为没有初始化而酿成大错
GPIO_StructInit(&gpio);
OnOpen(&gpio);
GPIO_Init(IndexToGroup(_Pin >> 4), &gpio);
State = &gpio;
}
void Port::OnOpen(void* param)
void Port::OnOpen()
{
auto gpio = (GPIO_InitTypeDef*)param;
auto gpio = (GPIO_InitTypeDef*)State;
gpio->GPIO_Pin = 1 << (_Pin & 0x0F);
#ifdef STM32F1
@ -101,6 +99,9 @@ void Port::OnClose()
{
// 不能随便关闭时钟,否则可能会影响别的引脚
OpenClock(_Pin, false);
delete (gpio_t*)State;
State = nullptr;
}
void Port::RemapConfig(uint param, bool sta)
@ -136,7 +137,7 @@ bool Port::Read() const
#define REGION_Output 1
#ifdef REGION_Output
void OutputPort::OpenPin(void* param)
void OutputPort::OpenPin()
{
#ifndef STM32F4
assert(Speed == 2 || Speed == 10 || Speed == 50, "Speed");
@ -144,7 +145,7 @@ void OutputPort::OpenPin(void* param)
assert(Speed == 2 || Speed == 25 || Speed == 50 || Speed == 100, "Speed");
#endif
auto gpio = (GPIO_InitTypeDef*)param;
auto gpio = (GPIO_InitTypeDef*)State;
switch(Speed)
{
@ -164,6 +165,8 @@ void OutputPort::OpenPin(void* param)
gpio->GPIO_Mode = GPIO_Mode_OUT;
gpio->GPIO_OType = OpenDrain ? GPIO_OType_OD : GPIO_OType_PP;
#endif
GPIO_Init(IndexToGroup(_Pin >> 4), gpio);
}
bool OutputPort::Read() const
@ -177,13 +180,6 @@ bool OutputPort::Read() const
return rs ^ Invert;
}
bool OutputPort::ReadInput() const
{
if(Empty()) return false;
return Port::Read() ^ Invert;
}
void OutputPort::Write(bool value) const
{
if(Empty()) return;
@ -209,9 +205,9 @@ void OutputPort::Write(Pin pin, bool value)
/******************************** AlternatePort ********************************/
void AlternatePort::OpenPin(void* param)
void AlternatePort::OpenPin()
{
auto gpio = (GPIO_InitTypeDef*)param;
auto gpio = (GPIO_InitTypeDef*)State;
#ifdef STM32F1
gpio->GPIO_Mode = OpenDrain ? GPIO_Mode_AF_OD : GPIO_Mode_AF_PP;
@ -219,6 +215,8 @@ void AlternatePort::OpenPin(void* param)
gpio->GPIO_Mode = GPIO_Mode_AF;
gpio->GPIO_OType = OpenDrain ? GPIO_OType_OD : GPIO_OType_PP;
#endif
GPIO_Init(IndexToGroup(_Pin >> 4), gpio);
}
#endif
@ -306,9 +304,9 @@ void SetEXIT(int pinIndex, bool enable, InputPort::Trigger mode)
EXTI_Init(&ext);
}
void InputPort::OpenPin(void* param)
void InputPort::OpenPin()
{
auto gpio = (GPIO_InitTypeDef*)param;
auto gpio = (GPIO_InitTypeDef*)State;
#ifdef STM32F1
if(Floating)
@ -321,6 +319,8 @@ void InputPort::OpenPin(void* param)
gpio->GPIO_Mode = GPIO_Mode_IN;
//gpio->GPIO_OType = !Floating ? GPIO_OType_OD : GPIO_OType_PP;
#endif
GPIO_Init(IndexToGroup(_Pin >> 4), gpio);
}
// 是否独享中断号
@ -448,9 +448,9 @@ bool InputPort::OnRegister()
/******************************** AnalogInPort ********************************/
void AnalogInPort::OpenPin(void* param)
void AnalogInPort::OpenPin()
{
auto gpio = (GPIO_InitTypeDef*)param;
auto gpio = (GPIO_InitTypeDef*)State;
#ifdef STM32F1
gpio->GPIO_Mode = GPIO_Mode_AIN; //
@ -458,4 +458,6 @@ void AnalogInPort::OpenPin(void* param)
gpio->GPIO_Mode = GPIO_Mode_AN;
//gpio->GPIO_OType = !Floating ? GPIO_OType_OD : GPIO_OType_PP;
#endif
GPIO_Init(IndexToGroup(_Pin >> 4), gpio);
}