!!!开始重新组织代码,让SmartOS头文件脱离对硬件设备固件库的依赖,将来使用的时候只需要引用设备固件库而不需要固件库的头文件。

Port作为样本编译通过,其它文件编译通不过,如果急需使用SmartOS,请回滚撤消当前版本。
cpp文件里面,需要把stm32.h放到开头,否则断言的编译可能会出错
This commit is contained in:
Stone 2016-01-13 15:20:27 +00:00
parent 8996ac2e60
commit e23f2b926f
5 changed files with 62 additions and 54 deletions

View File

@ -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);

View File

@ -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
}

20
Port.h
View File

@ -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 ********************************/

15
Sys.h
View File

@ -1,10 +1,10 @@
#ifndef _Sys_H_
#define _Sys_H_
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;

View File

@ -3,6 +3,7 @@
#include <math.h>
#include <stdarg.h>
#include "Sys.h"
#include "Platform\stm32.h"
/******************************** Object ********************************/