!!!开始重新组织代码,让SmartOS头文件脱离对硬件设备固件库的依赖,将来使用的时候只需要引用设备固件库而不需要固件库的头文件。
Port作为样本编译通过,其它文件编译通不过,如果急需使用SmartOS,请回滚撤消当前版本。 cpp文件里面,需要把stm32.h放到开头,否则断言的编译可能会出错
This commit is contained in:
parent
8996ac2e60
commit
e23f2b926f
|
@ -110,7 +110,7 @@ void operator delete[](void* p)
|
||||||
|
|
||||||
#ifdef USE_FULL_ASSERT
|
#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);
|
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) { }
|
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);
|
debug_printf("%s Line %d, %s\r\n", msg, line, file);
|
||||||
|
|
||||||
|
|
76
Port.cpp
76
Port.cpp
|
@ -1,4 +1,5 @@
|
||||||
#include "Port.h"
|
#include "Platform\stm32.h"
|
||||||
|
#include "Port.h"
|
||||||
|
|
||||||
#if defined(STM32F1) || defined(STM32F4)
|
#if defined(STM32F1) || defined(STM32F4)
|
||||||
static const int PORT_IRQns[] = {
|
static const int PORT_IRQns[] = {
|
||||||
|
@ -23,13 +24,13 @@ _force_inline byte GroupToIndex(GPIO_TypeDef* group) { return (byte)(((int)group
|
||||||
// 端口基本功能
|
// 端口基本功能
|
||||||
#define REGION_Port 1
|
#define REGION_Port 1
|
||||||
#ifdef REGION_Port
|
#ifdef REGION_Port
|
||||||
/* Port::Port()
|
Port::Port()
|
||||||
{
|
{
|
||||||
_Pin = P0;
|
_Pin = P0;
|
||||||
Group = NULL;
|
Group = NULL;
|
||||||
Mask = 0;
|
Mask = 0;
|
||||||
Opened = false;
|
Opened = false;
|
||||||
} */
|
}
|
||||||
|
|
||||||
#ifndef TINY
|
#ifndef TINY
|
||||||
Port::~Port()
|
Port::~Port()
|
||||||
|
@ -145,8 +146,8 @@ bool Port::Open()
|
||||||
// 特别要慎重,有些结构体成员可能因为没有初始化而酿成大错
|
// 特别要慎重,有些结构体成员可能因为没有初始化而酿成大错
|
||||||
GPIO_StructInit(&gpio);
|
GPIO_StructInit(&gpio);
|
||||||
|
|
||||||
OnOpen(gpio);
|
OnOpen(&gpio);
|
||||||
GPIO_Init(Group, &gpio);
|
GPIO_Init((GPIO_TypeDef*)Group, &gpio);
|
||||||
|
|
||||||
Opened = true;
|
Opened = true;
|
||||||
|
|
||||||
|
@ -177,9 +178,10 @@ void Port::OnClose()
|
||||||
OpenClock(_Pin, false);
|
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
|
#ifdef STM32F1
|
||||||
// PA15/PB3/PB4 需要关闭JTAG
|
// PA15/PB3/PB4 需要关闭JTAG
|
||||||
|
@ -205,7 +207,7 @@ void Port::AFConfig(byte GPIO_AF) const
|
||||||
{
|
{
|
||||||
assert_param2(Opened, "必须打开端口以后才能配置AF");
|
assert_param2(Opened, "必须打开端口以后才能配置AF");
|
||||||
|
|
||||||
GPIO_PinAFConfig(Group, _PIN(_Pin), GPIO_AF);
|
GPIO_PinAFConfig((GPIO_TypeDef*)Group, _PIN(_Pin), GPIO_AF);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -213,7 +215,7 @@ bool Port::Read() const
|
||||||
{
|
{
|
||||||
if(_Pin == P0) return false;
|
if(_Pin == P0) return false;
|
||||||
|
|
||||||
return GPIO_ReadInputData(Group) & Mask;
|
return GPIO_ReadInputData((GPIO_TypeDef*)Group) & Mask;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -300,7 +302,7 @@ OutputPort& OutputPort::Init(Pin pin, bool invert)
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void OutputPort::OnOpen(GPIO_InitTypeDef& gpio)
|
void OutputPort::OnOpen(void* param)
|
||||||
{
|
{
|
||||||
TS("OutputPort::OnOpen");
|
TS("OutputPort::OnOpen");
|
||||||
|
|
||||||
|
@ -343,25 +345,26 @@ void OutputPort::OnOpen(GPIO_InitTypeDef& gpio)
|
||||||
debug_printf(" 初始电平=%d \r\n", rs);
|
debug_printf(" 初始电平=%d \r\n", rs);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
auto gpio = (GPIO_InitTypeDef*)param;
|
||||||
Port::OnOpen(gpio);
|
Port::OnOpen(gpio);
|
||||||
|
|
||||||
switch(Speed)
|
switch(Speed)
|
||||||
{
|
{
|
||||||
case 2: gpio.GPIO_Speed = GPIO_Speed_2MHz; break;
|
case 2: gpio->GPIO_Speed = GPIO_Speed_2MHz; break;
|
||||||
#ifndef STM32F4
|
#ifndef STM32F4
|
||||||
case 10: gpio.GPIO_Speed = GPIO_Speed_10MHz; break;
|
case 10: gpio->GPIO_Speed = GPIO_Speed_10MHz; break;
|
||||||
#else
|
#else
|
||||||
case 25: gpio.GPIO_Speed = GPIO_Speed_25MHz; break;
|
case 25: gpio->GPIO_Speed = GPIO_Speed_25MHz; break;
|
||||||
case 100: gpio.GPIO_Speed = GPIO_Speed_100MHz;break;
|
case 100: gpio->GPIO_Speed = GPIO_Speed_100MHz;break;
|
||||||
#endif
|
#endif
|
||||||
case 50: gpio.GPIO_Speed = GPIO_Speed_50MHz; break;
|
case 50: gpio->GPIO_Speed = GPIO_Speed_50MHz; break;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef STM32F1
|
#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
|
#else
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -370,7 +373,7 @@ bool OutputPort::Read() const
|
||||||
if(Empty()) return false;
|
if(Empty()) return false;
|
||||||
|
|
||||||
// 转为bool时会转为0/1
|
// 转为bool时会转为0/1
|
||||||
bool rs = GPIO_ReadOutputData(Group) & Mask;
|
bool rs = GPIO_ReadOutputData((GPIO_TypeDef*)Group) & Mask;
|
||||||
return rs ^ Invert;
|
return rs ^ Invert;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -386,9 +389,9 @@ void OutputPort::Write(bool value) const
|
||||||
if(Empty()) return;
|
if(Empty()) return;
|
||||||
|
|
||||||
if(value ^ Invert)
|
if(value ^ Invert)
|
||||||
GPIO_SetBits(Group, Mask);
|
GPIO_SetBits((GPIO_TypeDef*)Group, Mask);
|
||||||
else
|
else
|
||||||
GPIO_ResetBits(Group, Mask);
|
GPIO_ResetBits((GPIO_TypeDef*)Group, Mask);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OutputPort::Up(uint ms) const
|
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)
|
AlternatePort::AlternatePort(Pin pin, byte invert, bool openDrain, byte speed)
|
||||||
: OutputPort(pin, invert, openDrain, 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);
|
OutputPort::OnOpen(gpio);
|
||||||
|
|
||||||
#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;
|
||||||
#else
|
#else
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -729,7 +733,7 @@ InputPort::Trigger GetTrigger(InputPort::Trigger mode, bool invert)
|
||||||
return mode;
|
return mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
void InputPort::OnOpen(GPIO_InitTypeDef& gpio)
|
void InputPort::OnOpen(void* param)
|
||||||
{
|
{
|
||||||
TS("InputPort::OnOpen");
|
TS("InputPort::OnOpen");
|
||||||
|
|
||||||
|
@ -773,18 +777,19 @@ void InputPort::OnOpen(GPIO_InitTypeDef& gpio)
|
||||||
debug_printf(" 初始电平=%d \r\n", rs);
|
debug_printf(" 初始电平=%d \r\n", rs);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
auto gpio = (GPIO_InitTypeDef*)param;
|
||||||
Port::OnOpen(gpio);
|
Port::OnOpen(gpio);
|
||||||
|
|
||||||
#ifdef STM32F1
|
#ifdef STM32F1
|
||||||
if(Floating)
|
if(Floating)
|
||||||
gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
gpio->GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
||||||
else if(Pull == UP)
|
else if(Pull == UP)
|
||||||
gpio.GPIO_Mode = GPIO_Mode_IPU;
|
gpio->GPIO_Mode = GPIO_Mode_IPU;
|
||||||
else if(Pull == DOWN)
|
else if(Pull == DOWN)
|
||||||
gpio.GPIO_Mode = GPIO_Mode_IPD; // 这里很不确定,需要根据实际进行调整
|
gpio->GPIO_Mode = GPIO_Mode_IPD; // 这里很不确定,需要根据实际进行调整
|
||||||
#else
|
#else
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -867,18 +872,19 @@ bool InputPort::Register(IOReadHandler handler, void* param)
|
||||||
|
|
||||||
/******************************** AnalogInPort ********************************/
|
/******************************** AnalogInPort ********************************/
|
||||||
|
|
||||||
void AnalogInPort::OnOpen(GPIO_InitTypeDef& gpio)
|
void AnalogInPort::OnOpen(void* param)
|
||||||
{
|
{
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
debug_printf("\r\n");
|
debug_printf("\r\n");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
auto gpio = (GPIO_InitTypeDef*)param;
|
||||||
Port::OnOpen(gpio);
|
Port::OnOpen(gpio);
|
||||||
|
|
||||||
#ifdef STM32F1
|
#ifdef STM32F1
|
||||||
gpio.GPIO_Mode = GPIO_Mode_AIN; //
|
gpio->GPIO_Mode = GPIO_Mode_AIN; //
|
||||||
#else
|
#else
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
20
Port.h
20
Port.h
|
@ -17,10 +17,12 @@
|
||||||
class Port : public Object
|
class Port : public Object
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
GPIO_TypeDef* Group = NULL; // 引脚组
|
void* Group; // 引脚组
|
||||||
ushort Mask = 0; // 组内引脚位。每个引脚一个位
|
ushort Mask; // 组内引脚位。每个引脚一个位
|
||||||
Pin _Pin = P0; // 引脚
|
Pin _Pin; // 引脚
|
||||||
bool Opened = false; // 是否已经打开
|
bool Opened; // 是否已经打开
|
||||||
|
|
||||||
|
Port();
|
||||||
|
|
||||||
Port& Set(Pin pin); // 设置引脚
|
Port& Set(Pin pin); // 设置引脚
|
||||||
bool Empty() const;
|
bool Empty() const;
|
||||||
|
@ -50,7 +52,7 @@ protected:
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// 配置过程,由Open调用,最后GPIO_Init
|
// 配置过程,由Open调用,最后GPIO_Init
|
||||||
virtual void OnOpen(GPIO_InitTypeDef& gpio);
|
virtual void OnOpen(void* param);
|
||||||
virtual void OnClose();
|
virtual void OnClose();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -88,7 +90,7 @@ public:
|
||||||
operator bool() const { return Read(); }
|
operator bool() const { return Read(); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void OnOpen(GPIO_InitTypeDef& gpio);
|
virtual void OnOpen(void* param);
|
||||||
};
|
};
|
||||||
|
|
||||||
/******************************** AlternatePort ********************************/
|
/******************************** AlternatePort ********************************/
|
||||||
|
@ -102,7 +104,7 @@ 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(GPIO_InitTypeDef& gpio);
|
virtual void OnOpen(void* param);
|
||||||
};
|
};
|
||||||
|
|
||||||
/******************************** InputPort ********************************/
|
/******************************** InputPort ********************************/
|
||||||
|
@ -153,7 +155,7 @@ public:
|
||||||
operator bool() const { return Read(); }
|
operator bool() const { return Read(); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void OnOpen(GPIO_InitTypeDef& gpio);
|
virtual void OnOpen(void* param);
|
||||||
virtual void OnClose();
|
virtual void OnClose();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -178,7 +180,7 @@ public:
|
||||||
AnalogInPort(Pin pin) : Port() { Set(pin); Open(); }
|
AnalogInPort(Pin pin) : Port() { Set(pin); Open(); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void OnOpen(GPIO_InitTypeDef& gpio);
|
virtual void OnOpen(void* param);
|
||||||
};
|
};
|
||||||
|
|
||||||
/******************************** PortScope ********************************/
|
/******************************** PortScope ********************************/
|
||||||
|
|
15
Sys.h
15
Sys.h
|
@ -1,10 +1,10 @@
|
||||||
#ifndef _Sys_H_
|
#ifndef _Sys_H_
|
||||||
#define _Sys_H_
|
#define _Sys_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "Platform\stm32.h"
|
|
||||||
|
|
||||||
// 强迫内联
|
// 强迫内联
|
||||||
#define _force_inline __attribute__( ( always_inline ) ) __INLINE
|
#define _force_inline __attribute__( ( always_inline ) ) __INLINE
|
||||||
|
@ -25,23 +25,24 @@ extern "C"
|
||||||
#ifdef USE_FULL_ASSERT
|
#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);
|
bool assert_ptr_(const void* p);
|
||||||
|
|
||||||
void assert_failed(const char* msg, uint8_t* file, uint32_t line);
|
void assert_failed2(const char* msg, const char* file, unsigned int line);
|
||||||
#define assert_param2(expr, msg) ((expr) ? (void)0 : assert_failed(msg, (uint8_t *)__FILE__, __LINE__))
|
#define assert_param2(expr, msg) ((expr) ? (void)0 : assert_failed2(msg, (const char*)__FILE__, __LINE__))
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
#define assert_ptr(expr) ((void)0)
|
#define assert_ptr(expr) ((void)0)
|
||||||
#define assert_param2(expr,msg) ((void)0)
|
#define assert_param2(expr, msg) ((void)0)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "Type.h"
|
#include "Type.h"
|
||||||
|
|
||||||
/* 引脚定义 */
|
/* 引脚定义 */
|
||||||
//typedef ushort Pin;
|
|
||||||
#include "Platform\Pin.h"
|
#include "Platform\Pin.h"
|
||||||
|
|
||||||
/* 串口定义 */
|
/* 串口定义 */
|
||||||
|
@ -52,11 +53,9 @@ typedef enum
|
||||||
COM3 = 2,
|
COM3 = 2,
|
||||||
COM4 = 3,
|
COM4 = 3,
|
||||||
COM5 = 4,
|
COM5 = 4,
|
||||||
#ifdef STM32F4
|
|
||||||
COM6 = 5,
|
COM6 = 5,
|
||||||
COM7 = 6,
|
COM7 = 6,
|
||||||
COM8 = 7,
|
COM8 = 7,
|
||||||
#endif
|
|
||||||
COM_NONE = 0xFF
|
COM_NONE = 0xFF
|
||||||
} COM_Def;
|
} COM_Def;
|
||||||
|
|
||||||
|
|
1
Type.cpp
1
Type.cpp
|
@ -3,6 +3,7 @@
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include "Sys.h"
|
#include "Sys.h"
|
||||||
|
#include "Platform\stm32.h"
|
||||||
|
|
||||||
/******************************** Object ********************************/
|
/******************************** Object ********************************/
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue