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

View File

@ -9,6 +9,19 @@
#define GPIO_MAX_SPEED 50 #define GPIO_MAX_SPEED 50
#endif #endif
/******** 端口打开关闭流程 ********/
/*
Port::Open
#Port::Opening
OutputPort::OnOpen
#Port::OnOpen
#OutputPort::OpenPin
Port::Close
#Port::OnClose
*/
/******************************** Port ********************************/ /******************************** Port ********************************/
// 端口基类 // 端口基类
@ -31,8 +44,8 @@ public:
Pin _Pin; // 引脚 Pin _Pin; // 引脚
bool Opened; // 是否已经打开 bool Opened; // 是否已经打开
byte Index; // 引脚自身次序编号,用于区分多引脚次序 byte Index; // 引脚自身次序编号,用于区分多引脚次序
void* State; // 用户状态数据。常用于批量端口操作时记录索引 void* State; // 用户状态数据
Port(); Port();
#ifndef TINY #ifndef TINY
@ -47,26 +60,19 @@ public:
void Clear(); void Clear();
void AFConfig(GPIO_AF GPIO_AF) const; void AFConfig(GPIO_AF GPIO_AF) const;
static void RemapConfig(uint param,bool sta); static void RemapConfig(uint param, bool sta);
virtual bool Read() const; 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; virtual String& ToStr(String& str) const;
protected: protected:
// 配置过程
// 配置过程由Open调用最后GPIO_Init virtual void OnOpen();
virtual void OnOpen(void* param);
virtual void OnClose(); virtual void OnClose();
private: private:
void OpenPin(); void Opening();
}; };
/******************************** OutputPort ********************************/ /******************************** OutputPort ********************************/
@ -103,10 +109,10 @@ public:
operator bool() const { return Read(); } operator bool() const { return Read(); }
protected: protected:
virtual void OnOpen(void* param); virtual void OnOpen();
virtual void OpenPin();
private: private:
void OpenPin(void* param);
}; };
/******************************** AlternatePort ********************************/ /******************************** AlternatePort ********************************/
@ -120,10 +126,10 @@ public:
AlternatePort(Pin pin, byte invert, bool openDrain = false, byte speed = GPIO_MAX_SPEED); AlternatePort(Pin pin, byte invert, bool openDrain = false, byte speed = GPIO_MAX_SPEED);
protected: protected:
virtual void OnOpen(void* param); //virtual void OnOpen();
virtual void OpenPin();
private: private:
void OpenPin(void* param);
}; };
/******************************** InputPort ********************************/ /******************************** InputPort ********************************/
@ -138,7 +144,6 @@ public:
UP = 0x01, // 上拉电阻 UP = 0x01, // 上拉电阻
DOWN = 0x02, // 下拉电阻 DOWN = 0x02, // 下拉电阻
}PuPd; }PuPd;
//enum class Trigger // 强类型枚举
typedef enum typedef enum
{ {
Rising = 0x01, // 上升沿 Rising = 0x01, // 上升沿
@ -174,7 +179,7 @@ public:
operator bool() const { return Read(); } operator bool() const { return Read(); }
protected: protected:
virtual void OnOpen(void* param); virtual void OnOpen();
virtual void OnClose(); virtual void OnClose();
private: private:
@ -186,7 +191,7 @@ private:
static void InputTask(void* param); static void InputTask(void* param);
private: private:
void OpenPin(void* param); void OpenPin();
void ClosePin(); void ClosePin();
bool OnRegister(); bool OnRegister();
}; };
@ -201,10 +206,10 @@ public:
AnalogInPort(Pin pin) : Port() { Set(pin); Open(); } AnalogInPort(Pin pin) : Port() { Set(pin); Open(); }
protected: protected:
virtual void OnOpen(void* param); virtual void OnOpen();
private: private:
void OpenPin(void* param); void OpenPin();
}; };
/******************************** PortScope ********************************/ /******************************** PortScope ********************************/

View File

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