From e23f2b926f19c7e9bbf2dcd7c1ab3da28a236701 Mon Sep 17 00:00:00 2001 From: Stone Date: Wed, 13 Jan 2016 15:20:27 +0000 Subject: [PATCH] =?UTF-8?q?=EF=BC=81=EF=BC=81=EF=BC=81=E5=BC=80=E5=A7=8B?= =?UTF-8?q?=E9=87=8D=E6=96=B0=E7=BB=84=E7=BB=87=E4=BB=A3=E7=A0=81=EF=BC=8C?= =?UTF-8?q?=E8=AE=A9SmartOS=E5=A4=B4=E6=96=87=E4=BB=B6=E8=84=B1=E7=A6=BB?= =?UTF-8?q?=E5=AF=B9=E7=A1=AC=E4=BB=B6=E8=AE=BE=E5=A4=87=E5=9B=BA=E4=BB=B6?= =?UTF-8?q?=E5=BA=93=E7=9A=84=E4=BE=9D=E8=B5=96=EF=BC=8C=E5=B0=86=E6=9D=A5?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=9A=84=E6=97=B6=E5=80=99=E5=8F=AA=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E5=BC=95=E7=94=A8=E8=AE=BE=E5=A4=87=E5=9B=BA=E4=BB=B6?= =?UTF-8?q?=E5=BA=93=E8=80=8C=E4=B8=8D=E9=9C=80=E8=A6=81=E5=9B=BA=E4=BB=B6?= =?UTF-8?q?=E5=BA=93=E7=9A=84=E5=A4=B4=E6=96=87=E4=BB=B6=E3=80=82=20Port?= =?UTF-8?q?=E4=BD=9C=E4=B8=BA=E6=A0=B7=E6=9C=AC=E7=BC=96=E8=AF=91=E9=80=9A?= =?UTF-8?q?=E8=BF=87=EF=BC=8C=E5=85=B6=E5=AE=83=E6=96=87=E4=BB=B6=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E9=80=9A=E4=B8=8D=E8=BF=87=EF=BC=8C=E5=A6=82=E6=9E=9C?= =?UTF-8?q?=E6=80=A5=E9=9C=80=E4=BD=BF=E7=94=A8SmartOS=EF=BC=8C=E8=AF=B7?= =?UTF-8?q?=E5=9B=9E=E6=BB=9A=E6=92=A4=E6=B6=88=E5=BD=93=E5=89=8D=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E3=80=82=20cpp=E6=96=87=E4=BB=B6=E9=87=8C=E9=9D=A2?= =?UTF-8?q?=EF=BC=8C=E9=9C=80=E8=A6=81=E6=8A=8Astm32.h=E6=94=BE=E5=88=B0?= =?UTF-8?q?=E5=BC=80=E5=A4=B4=EF=BC=8C=E5=90=A6=E5=88=99=E6=96=AD=E8=A8=80?= =?UTF-8?q?=E7=9A=84=E7=BC=96=E8=AF=91=E5=8F=AF=E8=83=BD=E4=BC=9A=E5=87=BA?= =?UTF-8?q?=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Debug.cpp | 4 +-- Port.cpp | 76 ++++++++++++++++++++++++++++++------------------------- Port.h | 20 ++++++++------- Sys.h | 15 +++++------ Type.cpp | 1 + 5 files changed, 62 insertions(+), 54 deletions(-) diff --git a/Debug.cpp b/Debug.cpp index 8ea45110..d45d6fc5 100644 --- a/Debug.cpp +++ b/Debug.cpp @@ -110,7 +110,7 @@ void operator delete[](void* p) #ifdef USE_FULL_ASSERT -void assert_failed(uint8_t* file, uint32_t line) +void assert_failed(uint8_t* file, unsigned int line) { debug_printf("Assert Failed! Line %d, %s\r\n", line, file); @@ -119,7 +119,7 @@ void assert_failed(uint8_t* file, uint32_t line) while (1) { } } -void assert_failed(const char* msg, uint8_t* file, uint32_t line) +void assert_failed2(const char* msg, const char* file, unsigned int line) { debug_printf("%s Line %d, %s\r\n", msg, line, file); diff --git a/Port.cpp b/Port.cpp index a0d91e64..8e7da035 100644 --- a/Port.cpp +++ b/Port.cpp @@ -1,4 +1,5 @@ -#include "Port.h" +#include "Platform\stm32.h" +#include "Port.h" #if defined(STM32F1) || defined(STM32F4) static const int PORT_IRQns[] = { @@ -23,13 +24,13 @@ _force_inline byte GroupToIndex(GPIO_TypeDef* group) { return (byte)(((int)group // 端口基本功能 #define REGION_Port 1 #ifdef REGION_Port -/* Port::Port() +Port::Port() { _Pin = P0; Group = NULL; Mask = 0; Opened = false; -} */ +} #ifndef TINY Port::~Port() @@ -145,8 +146,8 @@ bool Port::Open() // 特别要慎重,有些结构体成员可能因为没有初始化而酿成大错 GPIO_StructInit(&gpio); - OnOpen(gpio); - GPIO_Init(Group, &gpio); + OnOpen(&gpio); + GPIO_Init((GPIO_TypeDef*)Group, &gpio); Opened = true; @@ -177,9 +178,10 @@ void Port::OnClose() OpenClock(_Pin, false); } -void Port::OnOpen(GPIO_InitTypeDef& gpio) +void Port::OnOpen(void* param) { - gpio.GPIO_Pin = Mask; + auto gpio = (GPIO_InitTypeDef*)param; + gpio->GPIO_Pin = Mask; #ifdef STM32F1 // PA15/PB3/PB4 需要关闭JTAG @@ -205,7 +207,7 @@ void Port::AFConfig(byte GPIO_AF) const { assert_param2(Opened, "必须打开端口以后才能配置AF"); - GPIO_PinAFConfig(Group, _PIN(_Pin), GPIO_AF); + GPIO_PinAFConfig((GPIO_TypeDef*)Group, _PIN(_Pin), GPIO_AF); } #endif @@ -213,7 +215,7 @@ bool Port::Read() const { if(_Pin == P0) return false; - return GPIO_ReadInputData(Group) & Mask; + return GPIO_ReadInputData((GPIO_TypeDef*)Group) & Mask; } #endif @@ -300,7 +302,7 @@ OutputPort& OutputPort::Init(Pin pin, bool invert) return *this; } -void OutputPort::OnOpen(GPIO_InitTypeDef& gpio) +void OutputPort::OnOpen(void* param) { TS("OutputPort::OnOpen"); @@ -343,25 +345,26 @@ void OutputPort::OnOpen(GPIO_InitTypeDef& gpio) debug_printf(" 初始电平=%d \r\n", rs); #endif + auto gpio = (GPIO_InitTypeDef*)param; Port::OnOpen(gpio); switch(Speed) { - case 2: gpio.GPIO_Speed = GPIO_Speed_2MHz; break; + case 2: gpio->GPIO_Speed = GPIO_Speed_2MHz; break; #ifndef STM32F4 - case 10: gpio.GPIO_Speed = GPIO_Speed_10MHz; break; + case 10: gpio->GPIO_Speed = GPIO_Speed_10MHz; break; #else - case 25: gpio.GPIO_Speed = GPIO_Speed_25MHz; break; - case 100: gpio.GPIO_Speed = GPIO_Speed_100MHz;break; + case 25: gpio->GPIO_Speed = GPIO_Speed_25MHz; break; + case 100: gpio->GPIO_Speed = GPIO_Speed_100MHz;break; #endif - case 50: gpio.GPIO_Speed = GPIO_Speed_50MHz; break; + case 50: gpio->GPIO_Speed = GPIO_Speed_50MHz; break; } #ifdef STM32F1 - gpio.GPIO_Mode = OpenDrain ? GPIO_Mode_Out_OD : GPIO_Mode_Out_PP; + gpio->GPIO_Mode = OpenDrain ? GPIO_Mode_Out_OD : GPIO_Mode_Out_PP; #else - gpio.GPIO_Mode = GPIO_Mode_OUT; - gpio.GPIO_OType = OpenDrain ? GPIO_OType_OD : GPIO_OType_PP; + gpio->GPIO_Mode = GPIO_Mode_OUT; + gpio->GPIO_OType = OpenDrain ? GPIO_OType_OD : GPIO_OType_PP; #endif } @@ -370,7 +373,7 @@ bool OutputPort::Read() const if(Empty()) return false; // 转为bool时会转为0/1 - bool rs = GPIO_ReadOutputData(Group) & Mask; + bool rs = GPIO_ReadOutputData((GPIO_TypeDef*)Group) & Mask; return rs ^ Invert; } @@ -386,9 +389,9 @@ void OutputPort::Write(bool value) const if(Empty()) return; if(value ^ Invert) - GPIO_SetBits(Group, Mask); + GPIO_SetBits((GPIO_TypeDef*)Group, Mask); else - GPIO_ResetBits(Group, Mask); + GPIO_ResetBits((GPIO_TypeDef*)Group, Mask); } void OutputPort::Up(uint ms) const @@ -441,15 +444,16 @@ AlternatePort::AlternatePort(Pin pin) : OutputPort(pin, false, false) { } AlternatePort::AlternatePort(Pin pin, byte invert, bool openDrain, byte speed) : OutputPort(pin, invert, openDrain, speed) { } -void AlternatePort::OnOpen(GPIO_InitTypeDef& gpio) +void AlternatePort::OnOpen(void* param) { + auto gpio = (GPIO_InitTypeDef*)param; OutputPort::OnOpen(gpio); #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; #else - gpio.GPIO_Mode = GPIO_Mode_AF; - gpio.GPIO_OType = OpenDrain ? GPIO_OType_OD : GPIO_OType_PP; + gpio->GPIO_Mode = GPIO_Mode_AF; + gpio->GPIO_OType = OpenDrain ? GPIO_OType_OD : GPIO_OType_PP; #endif } @@ -729,7 +733,7 @@ InputPort::Trigger GetTrigger(InputPort::Trigger mode, bool invert) return mode; } -void InputPort::OnOpen(GPIO_InitTypeDef& gpio) +void InputPort::OnOpen(void* param) { TS("InputPort::OnOpen"); @@ -773,18 +777,19 @@ void InputPort::OnOpen(GPIO_InitTypeDef& gpio) debug_printf(" 初始电平=%d \r\n", rs); #endif + auto gpio = (GPIO_InitTypeDef*)param; Port::OnOpen(gpio); #ifdef STM32F1 if(Floating) - gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING; + gpio->GPIO_Mode = GPIO_Mode_IN_FLOATING; else if(Pull == UP) - gpio.GPIO_Mode = GPIO_Mode_IPU; + gpio->GPIO_Mode = GPIO_Mode_IPU; else if(Pull == DOWN) - gpio.GPIO_Mode = GPIO_Mode_IPD; // 这里很不确定,需要根据实际进行调整 + gpio->GPIO_Mode = GPIO_Mode_IPD; // 这里很不确定,需要根据实际进行调整 #else - gpio.GPIO_Mode = GPIO_Mode_IN; - //gpio.GPIO_OType = !Floating ? GPIO_OType_OD : GPIO_OType_PP; + gpio->GPIO_Mode = GPIO_Mode_IN; + //gpio->GPIO_OType = !Floating ? GPIO_OType_OD : GPIO_OType_PP; #endif } @@ -867,18 +872,19 @@ bool InputPort::Register(IOReadHandler handler, void* param) /******************************** AnalogInPort ********************************/ -void AnalogInPort::OnOpen(GPIO_InitTypeDef& gpio) +void AnalogInPort::OnOpen(void* param) { #if DEBUG debug_printf("\r\n"); #endif + auto gpio = (GPIO_InitTypeDef*)param; Port::OnOpen(gpio); #ifdef STM32F1 - gpio.GPIO_Mode = GPIO_Mode_AIN; // + gpio->GPIO_Mode = GPIO_Mode_AIN; // #else - gpio.GPIO_Mode = GPIO_Mode_AN; - //gpio.GPIO_OType = !Floating ? GPIO_OType_OD : GPIO_OType_PP; + gpio->GPIO_Mode = GPIO_Mode_AN; + //gpio->GPIO_OType = !Floating ? GPIO_OType_OD : GPIO_OType_PP; #endif } diff --git a/Port.h b/Port.h index 7db7a256..eca452fb 100644 --- a/Port.h +++ b/Port.h @@ -17,10 +17,12 @@ class Port : public Object { public: - GPIO_TypeDef* Group = NULL; // 引脚组 - ushort Mask = 0; // 组内引脚位。每个引脚一个位 - Pin _Pin = P0; // 引脚 - bool Opened = false; // 是否已经打开 + void* Group; // 引脚组 + ushort Mask; // 组内引脚位。每个引脚一个位 + Pin _Pin; // 引脚 + bool Opened; // 是否已经打开 + + Port(); Port& Set(Pin pin); // 设置引脚 bool Empty() const; @@ -50,7 +52,7 @@ protected: #endif // 配置过程,由Open调用,最后GPIO_Init - virtual void OnOpen(GPIO_InitTypeDef& gpio); + virtual void OnOpen(void* param); virtual void OnClose(); }; @@ -88,7 +90,7 @@ public: operator bool() const { return Read(); } protected: - virtual void OnOpen(GPIO_InitTypeDef& gpio); + virtual void OnOpen(void* param); }; /******************************** AlternatePort ********************************/ @@ -102,7 +104,7 @@ public: AlternatePort(Pin pin, byte invert, bool openDrain = false, byte speed = GPIO_MAX_SPEED); protected: - virtual void OnOpen(GPIO_InitTypeDef& gpio); + virtual void OnOpen(void* param); }; /******************************** InputPort ********************************/ @@ -153,7 +155,7 @@ public: operator bool() const { return Read(); } protected: - virtual void OnOpen(GPIO_InitTypeDef& gpio); + virtual void OnOpen(void* param); virtual void OnClose(); private: @@ -178,7 +180,7 @@ public: AnalogInPort(Pin pin) : Port() { Set(pin); Open(); } protected: - virtual void OnOpen(GPIO_InitTypeDef& gpio); + virtual void OnOpen(void* param); }; /******************************** PortScope ********************************/ diff --git a/Sys.h b/Sys.h index 90c207c3..e8c3ef5f 100644 --- a/Sys.h +++ b/Sys.h @@ -1,10 +1,10 @@ #ifndef _Sys_H_ #define _Sys_H_ +#include #include #include #include -#include "Platform\stm32.h" // 强迫内联 #define _force_inline __attribute__( ( always_inline ) ) __INLINE @@ -25,23 +25,24 @@ extern "C" #ifdef USE_FULL_ASSERT // 验证确保对象不为空,并且在有效的内存范围内 -#define assert_ptr(expr) (assert_ptr_(expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) +extern void assert_failed(uint8_t* file, uint32_t line); + +#define assert_ptr(expr) (assert_ptr_(expr) ? (void)0 : assert_failed((uint8_t*)__FILE__, __LINE__)) bool assert_ptr_(const void* p); -void assert_failed(const char* msg, uint8_t* file, uint32_t line); -#define assert_param2(expr, msg) ((expr) ? (void)0 : assert_failed(msg, (uint8_t *)__FILE__, __LINE__)) +void assert_failed2(const char* msg, const char* file, unsigned int line); +#define assert_param2(expr, msg) ((expr) ? (void)0 : assert_failed2(msg, (const char*)__FILE__, __LINE__)) #else #define assert_ptr(expr) ((void)0) -#define assert_param2(expr,msg) ((void)0) +#define assert_param2(expr, msg) ((void)0) #endif #include "Type.h" /* 引脚定义 */ -//typedef ushort Pin; #include "Platform\Pin.h" /* 串口定义 */ @@ -52,11 +53,9 @@ typedef enum COM3 = 2, COM4 = 3, COM5 = 4, -#ifdef STM32F4 COM6 = 5, COM7 = 6, COM8 = 7, -#endif COM_NONE = 0xFF } COM_Def; diff --git a/Type.cpp b/Type.cpp index de927aea..d60098bb 100644 --- a/Type.cpp +++ b/Type.cpp @@ -3,6 +3,7 @@ #include #include #include "Sys.h" +#include "Platform\stm32.h" /******************************** Object ********************************/