Port驱动不再记录Group和Mask,改为具体移植库自己计算
This commit is contained in:
parent
0dc04fb164
commit
e759dea052
|
@ -266,9 +266,9 @@ static void UnionPress(InputPort& port, bool down)
|
|||
byte data[1];
|
||||
data[0] = down ? 1 : 0;
|
||||
|
||||
client->Store.Write(port.Index + 1, Buffer(data, 1));
|
||||
client->Store.Write(port.State + 1, Buffer(data, 1));
|
||||
// 主动上报状态
|
||||
client->ReportAsync(port.Index + 1, 1);
|
||||
client->ReportAsync(port.State + 1, 1);
|
||||
|
||||
}
|
||||
void IOK027X::Union(Pin pin1, Pin pin2)
|
||||
|
@ -279,7 +279,7 @@ void IOK027X::Union(Pin pin1, Pin pin2)
|
|||
auto port = new InputPort(p[i]);
|
||||
port->Invert = true;
|
||||
port->ShakeTime = 40;
|
||||
port->Index = i;
|
||||
port->State = i;
|
||||
port->Press.Bind(UnionPress);
|
||||
port->UsePress();
|
||||
port->Open();
|
||||
|
|
|
@ -9,8 +9,6 @@
|
|||
Port::Port()
|
||||
{
|
||||
_Pin = P0;
|
||||
Group = nullptr;
|
||||
Mask = 0;
|
||||
Opened = false;
|
||||
State = 0;
|
||||
}
|
||||
|
@ -49,16 +47,6 @@ Port& Port::Set(Pin pin)
|
|||
#endif
|
||||
|
||||
_Pin = pin;
|
||||
if(_Pin != P0)
|
||||
{
|
||||
Group = IndexToGroup(pin >> 4);
|
||||
Mask = 1 << (pin & 0x0F);
|
||||
}
|
||||
else
|
||||
{
|
||||
Group = nullptr;
|
||||
Mask = 0;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
@ -67,16 +55,12 @@ bool Port::Empty() const
|
|||
{
|
||||
if(_Pin != P0) return false;
|
||||
|
||||
if(Group == nullptr || Mask == 0) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void Port::Clear()
|
||||
{
|
||||
Group = nullptr;
|
||||
_Pin = P0;
|
||||
Mask = 0;
|
||||
}
|
||||
|
||||
// 确定配置,确认用对象内部的参数进行初始化
|
||||
|
|
|
@ -29,12 +29,9 @@ public:
|
|||
AF_7 = 7
|
||||
};
|
||||
|
||||
void* Group; // 引脚组
|
||||
ushort Mask; // 组内引脚位。每个引脚一个位
|
||||
Pin _Pin; // 引脚
|
||||
bool Opened; // 是否已经打开
|
||||
int State; // 用户状态数据。常用于批量端口操作时记录索引
|
||||
byte Index; //引脚自身次序编号,用于区分多引脚次序
|
||||
|
||||
Port();
|
||||
|
||||
|
@ -69,7 +66,6 @@ protected:
|
|||
virtual void OnClose();
|
||||
|
||||
private:
|
||||
static void* IndexToGroup(byte index);
|
||||
void OpenPin();
|
||||
};
|
||||
|
||||
|
|
|
@ -28,12 +28,12 @@ static const byte PORT_IRQns[] = {
|
|||
#define REGION_Port 1
|
||||
#ifdef REGION_Port
|
||||
|
||||
void* Port::IndexToGroup(byte index) { return ((GPIO_TypeDef *) (GPIOA_BASE + (index << 10))); }
|
||||
static GPIO_TypeDef* IndexToGroup(byte index) { return ((GPIO_TypeDef *) (GPIOA_BASE + (index << 10))); }
|
||||
|
||||
// 分组时钟
|
||||
static byte _GroupClock[10];
|
||||
|
||||
void Port::OpenClock(Pin pin, bool flag)
|
||||
static void OpenClock(Pin pin, bool flag)
|
||||
{
|
||||
int gi = pin >> 4;
|
||||
|
||||
|
@ -69,13 +69,14 @@ void Port::OpenPin()
|
|||
GPIO_StructInit(&gpio);
|
||||
|
||||
OnOpen(&gpio);
|
||||
GPIO_Init((GPIO_TypeDef*)Group, &gpio);
|
||||
|
||||
GPIO_Init(IndexToGroup(_Pin >> 4), &gpio);
|
||||
}
|
||||
|
||||
void Port::OnOpen(void* param)
|
||||
{
|
||||
auto gpio = (GPIO_InitTypeDef*)param;
|
||||
gpio->GPIO_Pin = Mask;
|
||||
gpio->GPIO_Pin = 1 << (_Pin & 0x0F);
|
||||
|
||||
#ifdef STM32F1
|
||||
// PA15/PB3/PB4 需要关闭JTAG
|
||||
|
@ -115,7 +116,7 @@ void Port::AFConfig(GPIO_AF GPIO_AF) const
|
|||
#if defined(STM32F0) || defined(GD32F150) || defined(STM32F4)
|
||||
assert(Opened, "打开后才能配置AF");
|
||||
|
||||
GPIO_PinAFConfig((GPIO_TypeDef*)Group, _PIN(_Pin), GPIO_AF);
|
||||
GPIO_PinAFConfig(IndexToGroup(_Pin >> 4), _PIN(_Pin), GPIO_AF);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -123,7 +124,9 @@ bool Port::Read() const
|
|||
{
|
||||
if(_Pin == P0) return false;
|
||||
|
||||
return GPIO_ReadInputData((GPIO_TypeDef*)Group) & Mask;
|
||||
auto gp = IndexToGroup(_Pin >> 4);
|
||||
ushort ms = 1 << (_Pin & 0x0F);
|
||||
return GPIO_ReadInputData(gp) & ms;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -167,8 +170,10 @@ bool OutputPort::Read() const
|
|||
{
|
||||
if(Empty()) return false;
|
||||
|
||||
auto gp = IndexToGroup(_Pin >> 4);
|
||||
ushort ms = 1 << (_Pin & 0x0F);
|
||||
// 转为bool时会转为0/1
|
||||
bool rs = GPIO_ReadOutputData((GPIO_TypeDef*)Group) & Mask;
|
||||
bool rs = GPIO_ReadOutputData(gp) & ms;
|
||||
return rs ^ Invert;
|
||||
}
|
||||
|
||||
|
@ -183,10 +188,12 @@ void OutputPort::Write(bool value) const
|
|||
{
|
||||
if(Empty()) return;
|
||||
|
||||
auto gi = IndexToGroup(_Pin >> 4);
|
||||
ushort ms = 1 << (_Pin & 0x0F);
|
||||
if(value ^ Invert)
|
||||
GPIO_SetBits((GPIO_TypeDef*)Group, Mask);
|
||||
GPIO_SetBits(gi, ms);
|
||||
else
|
||||
GPIO_ResetBits((GPIO_TypeDef*)Group, Mask);
|
||||
GPIO_ResetBits(gi, ms);
|
||||
}
|
||||
|
||||
// 设置端口状态
|
||||
|
@ -372,7 +379,7 @@ InputPort::Trigger GetTrigger(InputPort::Trigger mode, bool invert)
|
|||
|
||||
void InputPort::ClosePin()
|
||||
{
|
||||
int idx = Bits2Index(Mask);
|
||||
int idx = Bits2Index(1 << (_Pin & 0x0F));
|
||||
|
||||
auto st = &States[idx];
|
||||
if(st->Port == this)
|
||||
|
@ -399,7 +406,7 @@ bool InputPort::OnRegister()
|
|||
}
|
||||
|
||||
byte gi = _Pin >> 4;
|
||||
int idx = Bits2Index(Mask);
|
||||
int idx = Bits2Index(1 << (_Pin & 0x0F));
|
||||
auto st = &States[idx];
|
||||
|
||||
auto port = st->Port;
|
||||
|
@ -416,7 +423,7 @@ bool InputPort::OnRegister()
|
|||
st->Port = this;
|
||||
|
||||
//byte gi = _Pin >> 4;
|
||||
//int idx = Bits2Index(Mask);
|
||||
//int idx = Bits2Index(1 << (_Pin & 0x0F));
|
||||
|
||||
// 打开时钟,选择端口作为端口EXTI时钟线
|
||||
#if defined(STM32F0) || defined(GD32F150) || defined(STM32F4)
|
||||
|
|
Loading…
Reference in New Issue