系统内核和外设驱动分别独立到不同目录

This commit is contained in:
Stone 2016-06-13 04:05:30 +00:00
parent 5de683990a
commit 532150941f
41 changed files with 252 additions and 228 deletions

View File

View File

@ -1,6 +1,6 @@
#include "Sys.h"
#include "Port.h"
#include "CAN.h"
#include "Can.h"
#include "Platform\stm32.h"
@ -10,25 +10,28 @@ static const Pin g_CAN_Pins_Map2[] = CAN_PINS_REMAP2;
static const Pin g_CAN_Pins_Map3[] = CAN_PINS_REMAP3;
#endif
CAN::CAN(CAN_TypeDef* port, Mode_TypeDef mode, int remap)
Can::Can(CAN index, Mode_TypeDef mode, int remap)
{
Port = port;
Mode = mode;
Remap = remap;
_index = index;
Mode = mode;
Remap = remap;
_Tx = nullptr;
_Rx = nullptr;
/*外设时钟设置*/
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
/*IO设置*/
#ifdef STM32F1
if(port == CAN1)
if(index == Can1)
{
if(remap == 1)
GPIO_PinRemapConfig(GPIO_Remap1_CAN1, ENABLE);
else if(remap == 2)
GPIO_PinRemapConfig(GPIO_Remap2_CAN1, ENABLE);
}
else if(port == CAN2)
else if(index == Can2)
GPIO_PinRemapConfig(GPIO_Remap_CAN2, ENABLE);
const Pin* p = g_CAN_Pins_Map;
@ -36,7 +39,7 @@ CAN::CAN(CAN_TypeDef* port, Mode_TypeDef mode, int remap)
p = g_CAN_Pins_Map2;
else if(remap == 3)
p = g_CAN_Pins_Map3;
AlternatePort tx(p[0]);
InputPort rx(p[1], false, InputPort::UP);
#endif
@ -48,28 +51,31 @@ CAN::CAN(CAN_TypeDef* port, Mode_TypeDef mode, int remap)
#endif
/************************CAN通信参数设置**********************************/
CAN_TypeDef* const g_CANs[] = CANS;
/*CAN寄存器初始化*/
CAN_DeInit(Port);
CAN_DeInit(g_CANs[_index]);
CAN_InitTypeDef _can;
CAN_StructInit(&_can);
/*CAN单元初始化*/
_can.CAN_TTCM = DISABLE; //MCR-TTCM 时间触发通信模式使能
_can.CAN_ABOM = Mode == Mode_Send ? ENABLE : DISABLE; //MCR-ABOM 自动离线管理
_can.CAN_ABOM = Mode == Mode_Send ? ENABLE : DISABLE; //MCR-ABOM 自动离线管理
_can.CAN_AWUM = Mode == Mode_Send ? ENABLE : DISABLE; //MCR-AWUM 自动唤醒模式
_can.CAN_NART = DISABLE; //MCR-NART 禁止报文自动重传 DISABLE-自动重传
_can.CAN_RFLM = DISABLE; //MCR-RFLM 接收FIFO 锁定模式 DISABLE-溢出时新报文会覆盖原有报文
_can.CAN_TXFP = DISABLE; //MCR-TXFP 发送FIFO优先级 DISABLE-优先级取决于报文标示符
_can.CAN_RFLM = DISABLE; //MCR-RFLM 接收FIFO 锁定模式 DISABLE-溢出时新报文会覆盖原有报文
_can.CAN_TXFP = DISABLE; //MCR-TXFP 发送FIFO优先级 DISABLE-优先级取决于报文标示符
_can.CAN_Mode = CAN_Mode_Normal; //正常发送模式
_can.CAN_SJW = CAN_SJW_1tq; //BTR-SJW 重新同步跳跃宽度 2个时间单元
_can.CAN_BS1 = CAN_BS1_3tq; //BTR-TS1 时间段1 占用了6个时间单元
_can.CAN_BS2 = CAN_BS2_2tq; //BTR-TS1 时间段2 占用了3个时间单元
_can.CAN_Prescaler = 6; ////BTR-BRP 波特率分频器 定义了时间单元的时间长度 36/(1+6+3)/4 = 0.8Mbps
CAN_Init(Port, &_can);
CAN_Init(g_CANs[_index], &_can);
if(Mode == Mode_Send)
_TxMsg = new CanTxMsg();
_Tx = new CanTxMsg();
else
{
_RxMsg = new CanRxMsg();
_Rx = new CanRxMsg();
CAN_FilterInitTypeDef filter;
@ -140,27 +146,37 @@ CAN::CAN(CAN_TypeDef* port, Mode_TypeDef mode, int remap)
}
}
void CAN::Send(byte* buf, uint len)
Can::~Can()
{
auto tx = (CanTxMsg*)_Tx;
delete tx;
auto rx = (CanRxMsg*)_Rx;
delete rx;
}
void Can::Send(byte* buf, uint len)
{
auto tx = (CanTxMsg*)_Tx;
switch(Mode)
{
case STD_Data:
case STD_Remote:
case STD:
_TxMsg->StdId = 0;
_TxMsg->IDE = CAN_ID_STD;
tx->StdId = 0;
tx->IDE = CAN_ID_STD;
break;
case EXT_Data:
case EXT_Remote:
case EXT:
_TxMsg->StdId = 0;
_TxMsg->IDE = CAN_ID_EXT;
tx->StdId = 0;
tx->IDE = CAN_ID_EXT;
break;
}
_TxMsg->RTR=CAN_RTR_DATA; //发送的是数据
_TxMsg->DLC=2; //数据长度为2字节
_TxMsg->Data[0] = 0x12;
_TxMsg->Data[1] = 0x34;
tx->RTR =CAN_RTR_DATA; //发送的是数据
tx->DLC =2; //数据长度为2字节
tx->Data[0] = 0x12;
tx->Data[1] = 0x34;
// 未完成
}

View File

@ -1,10 +1,8 @@
#ifndef __CAN_H__
#define __CAN_H__
#include "Sys.h"
// CAN类
class CAN
class Can
{
public:
typedef enum
@ -19,26 +17,19 @@ public:
Mode_ALL = 0x07 // 接收所有类型
}Mode_TypeDef;
CAN_TypeDef* Port; // 端口
Mode_TypeDef Mode; // 工作模式
CAN(CAN_TypeDef* port = CAN1, Mode_TypeDef mode = Mode_Send, int remap = 1);
virtual ~CAN()
{
if(_TxMsg) delete _TxMsg;
_TxMsg = nullptr;
if(_RxMsg) delete _RxMsg;
_RxMsg = nullptr;
}
Can(CAN index = Can1, Mode_TypeDef mode = Mode_Send, int remap = 1);
~Can();
void Send(byte* buf, uint len);
private:
CAN_InitTypeDef _can;
int Remap;
CanTxMsg* _TxMsg;
CanRxMsg* _RxMsg;
private:
byte _index;
int Remap;
void* _Tx;
void* _Rx;
};
#endif

View File

View File

@ -1,9 +1,11 @@
#include "DMA.h"
#include "Sys.h"
#include "DMA.h"
#include "Platform\stm32.h"
bool DMA::Start()
{
#ifdef STM32F4
DMA_InitTypeDef DMA_InitStructure;
__IO uint32_t Timeout = TIMEOUT_MAX;
@ -16,16 +18,16 @@ bool DMA::Start()
/* Check if the DMA Stream is disabled before enabling it.
Note that this step is useful when the same Stream is used multiple times:
enabled, then disabled then re-enabled... In this case, the DMA Stream disable
will be effective only at the end of the ongoing data transfer and it will
not be possible to re-configure it before making sure that the Enable bit
has been cleared by hardware. If the Stream is used only once, this step might
will be effective only at the end of the ongoing data transfer and it will
not be possible to re-configure it before making sure that the Enable bit
has been cleared by hardware. If the Stream is used only once, this step might
be bypassed. */
while (DMA_GetCmdStatus(DMA_STREAM) != DISABLE)
{
}
/* Configure DMA Stream */
DMA_InitStructure.DMA_Channel = DMA_CHANNEL;
DMA_InitStructure.DMA_Channel = DMA_CHANNEL;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SRC_Const_Buffer;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)DST_Buffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToMemory;
@ -36,7 +38,7 @@ bool DMA::Start()
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
@ -49,7 +51,7 @@ bool DMA::Start()
DMA_Cmd(DMA_STREAM, ENABLE);
/* Check if the DMA Stream has been effectively enabled.
The DMA Stream Enable bit is cleared immediately by hardware if there is an
The DMA Stream Enable bit is cleared immediately by hardware if there is an
error in the configuration parameters and the transfer is no started (ie. when
wrong FIFO threshold is configured ...) */
Timeout = TIMEOUT_MAX;
@ -60,17 +62,21 @@ bool DMA::Start()
/* Check if a timeout condition occurred */
if (Timeout == 0)
{
/* Manage the error: to simplify the code enter an infinite loop */
while (1)
{
}
/* Manage the error: to simplify the code enter an infinite loop */
while (1)
{
}
}
Interrupt.Enable(DMA_STREAM_IRQ);
#endif
return true;
}
bool DMA::WaitForStop()
{
#ifdef STM32F4
uint retry = Retry;
//while (DMA_GetCurrentMemoryTarget(DMA_STREAM) != 0)
while (DMA_GetCmdStatus(DMA_STREAM) != DISABLE)
@ -81,5 +87,6 @@ bool DMA::WaitForStop()
return false;
}
}
#endif
return true;
}
}

View File

@ -1,25 +1,23 @@
#ifndef __DMA_H__
#define __DMA_H__
#include "Sys.h"
// DMA
class DMA
{
private:
byte _index; // 第几个定时器从0开始
bool _started;
public:
DMA(byte index);
~DMA();
int Retry; // 等待重试次数默认200
int Error; // 错误次数
bool Start(); // 开始
bool WaitForStop(); // 停止
//typedef void (*TimerHandler)(Timer* tim, void* param);
//void Register(TimerHandler handler, void* param = nullptr);

View File

View File

View File

167
Kernel/Sys.h Normal file
View File

@ -0,0 +1,167 @@
#ifndef _Sys_H_
#define _Sys_H_
#include <stdint.h>
#include <stdio.h>
#include <stddef.h>
#include "Core\Type.h"
#include "Core\Buffer.h"
#include "Core\Array.h"
#include "Core\ByteArray.h"
#include "Core\SString.h"
#include "Core\Stream.h"
#include "Core\DateTime.h"
#include "Core\Version.h"
#include "Core\List.h"
#include "Core\Dictionary.h"
#include "Core\Delegate.h"
/* 引脚定义 */
#include "Platform\Pin.h"
// 强迫内联
#define _force_inline __attribute__( ( always_inline ) ) __INLINE
extern "C"
{
#if defined(DEBUG) || defined(MSGDEBUG)
#define debug_printf printf
#else
#define debug_printf(format, ...)
#endif
}
#ifdef USE_FULL_ASSERT
// 验证确保对象不为空,并且在有效的内存范围内
//extern void assert_failed(uint8_t* file, uint32_t line);
#define assert_ptr(expr) (assert_ptr_(expr) ? (void)0 : assert_failed2("ptr==nullptr", (const char*)__FILE__, __LINE__))
bool assert_ptr_(const void* p);
void assert_failed2(cstring msg, cstring file, unsigned int line);
#define assert(expr, msg) ((expr) ? (void)0 : assert_failed2(msg, (const char*)__FILE__, __LINE__))
#else
#define assert_ptr(expr) ((void)0)
#define assert(expr, msg) ((void)0)
#endif
#if defined(BOOT) || defined(APP)
struct HandlerRemap
{
Func pUserHandler;
void* Reserved1;
void* Reserved2;
void* Reserved3;
};
extern struct HandlerRemap StrBoot;
#endif
// 判定指针是否在ROM区
#define IN_ROM_SECTION(p) ( (int)p < 0x20000000 )
// 系统类
class TSys
{
public:
COM MessagePort;// 消息口默认0表示USART1
uint Clock; // 系统时钟
uint CystalClock;// 晶振时钟
String Name; // 系统名称
String Company; // 系统厂商
ushort Code; // 产品代码
Version Ver; // 系统版本
byte ID[12]; // 芯片ID。
ushort DevID; // MCU编码。低字设备版本高字子版本
ushort RevID; // MCU编码。低字设备版本高字子版本
uint CPUID; // CPUID
ushort FlashSize; // 芯片Flash容量。
ushort RAMSize; // 芯片RAM容量
TSys(); // 构造函数
void InitClock(); // 初始化系统时钟
void Init(); // 初始化系统
void ShowInfo() const;
uint HeapBase() const; // 堆起始地址,前面是静态分配内存
uint StackTop() const; // 栈顶,后面是初始化不清零区域
UInt64 Ms() const; // 系统启动后的毫秒数
uint Seconds() const; // 系统绝对当前时间,秒
void Sleep(uint ms) const; // 毫秒级延迟
void Delay(uint us) const; // 微秒级延迟
typedef void (*FuncU32)(uint param);
FuncU32 OnSleep;
bool CheckMemory() const;
void Reset() const; // 重启系统
// 系统跟踪
void InitTrace(void* port) const;
void Trace(int times = 1) const;
public:
// 创建任务返回任务编号。dueTime首次调度时间msperiod调度间隔ms-1表示仅处理一次
uint AddTask(Action func, void* param, int dueTime = 0, int period = 0, cstring name = nullptr) const;
void RemoveTask(uint& taskid) const;
// 设置任务的开关状态同时运行指定任务最近一次调度的时间0表示马上调度
bool SetTask(uint taskid, bool enable, int msNextTime = -1) const;
// 改变任务周期
bool SetTaskPeriod(uint taskid, int period) const;
bool Started;
void Start(); // 开始系统大循环
};
extern TSys Sys; //创建一个全局的Sys对象 会在main函数之前执行构造函数
//#include "Time.h"
#include "Interrupt.h"
void EnterCritical();
void ExitCritical();
//extern uint32_t __REV(uint32_t value);
//extern uint32_t __REV16(uint16_t value);
uint _REV(uint value);
ushort _REV16(ushort value);
#endif //_Sys_H_
/*
v3.2.2016.0517 Core
v3.1.2015.1108
v3.0.2015.0806
v2.8.2014.0927 nRF24L01+
v2.7.2014.0919 线
v2.6.2014.0823
v2.5.2014.0819 new/delete实现Debug有效
v2.4.2014.0811 4k
TinyIPARP/ICMP/TCP/UDP7.5k
v2.3.2014.0806 使RTM优化编译
v2.2.2014.0801 使
v2.1.2014.0728 F0需配置RAM从0x200000C0开始
v2.0.2014.0725 使C++SmartOSIOUSARTSpiNRF24L01SPIFlashCANEnc28j60GD32超频
v1.3.2014.0624 Spi模块和NRF24L01+
v1.2.2014.0604 GD32芯片
v1.1.2014.0513 F0/F1的GPIO和串口功能
v1.0.2014.0506 SmartOS使C语言实现
*/

View File

@ -183,4 +183,10 @@ enum TIMER
Timer18 = 17,
};
enum CAN
{
Can1 = 0,
Can2 = 1,
};
#endif

View File

@ -122,6 +122,7 @@
}
/* 控制器区域网络(CAN)针脚 ------------------------------------------------------------------*/
#define CANS {CAN1, CAN2}
// TX RX
#define CAN_PINS {PA12, PA11} // AFIO_MAPR_CAN_REMAP_REMAP1
#define CAN_PINS_REMAP2 {PB9, PB8 } // AFIO_MAPR_CAN_REMAP_REMAP2

168
Sys.h
View File

@ -1,167 +1 @@
#ifndef _Sys_H_
#define _Sys_H_
#include <stdint.h>
#include <stdio.h>
#include <stddef.h>
#include "Core\Type.h"
#include "Core\Buffer.h"
#include "Core\Array.h"
#include "Core\ByteArray.h"
#include "Core\SString.h"
#include "Core\Stream.h"
#include "Core\DateTime.h"
#include "Core\Version.h"
#include "Core\List.h"
#include "Core\Dictionary.h"
#include "Core\Delegate.h"
/* 引脚定义 */
#include "Platform\Pin.h"
// 强迫内联
#define _force_inline __attribute__( ( always_inline ) ) __INLINE
extern "C"
{
#if defined(DEBUG) || defined(MSGDEBUG)
#define debug_printf printf
#else
#define debug_printf(format, ...)
#endif
}
#ifdef USE_FULL_ASSERT
// 验证确保对象不为空,并且在有效的内存范围内
//extern void assert_failed(uint8_t* file, uint32_t line);
#define assert_ptr(expr) (assert_ptr_(expr) ? (void)0 : assert_failed2("ptr==nullptr", (const char*)__FILE__, __LINE__))
bool assert_ptr_(const void* p);
void assert_failed2(cstring msg, cstring file, unsigned int line);
#define assert(expr, msg) ((expr) ? (void)0 : assert_failed2(msg, (const char*)__FILE__, __LINE__))
#else
#define assert_ptr(expr) ((void)0)
#define assert(expr, msg) ((void)0)
#endif
#if defined(BOOT) || defined(APP)
struct HandlerRemap
{
Func pUserHandler;
void* Reserved1;
void* Reserved2;
void* Reserved3;
};
extern struct HandlerRemap StrBoot;
#endif
// 判定指针是否在ROM区
#define IN_ROM_SECTION(p) ( (int)p < 0x20000000 )
// 系统类
class TSys
{
public:
COM MessagePort;// 消息口默认0表示USART1
uint Clock; // 系统时钟
uint CystalClock;// 晶振时钟
String Name; // 系统名称
String Company; // 系统厂商
ushort Code; // 产品代码
Version Ver; // 系统版本
byte ID[12]; // 芯片ID。
ushort DevID; // MCU编码。低字设备版本高字子版本
ushort RevID; // MCU编码。低字设备版本高字子版本
uint CPUID; // CPUID
ushort FlashSize; // 芯片Flash容量。
ushort RAMSize; // 芯片RAM容量
TSys(); // 构造函数
void InitClock(); // 初始化系统时钟
void Init(); // 初始化系统
void ShowInfo() const;
uint HeapBase() const; // 堆起始地址,前面是静态分配内存
uint StackTop() const; // 栈顶,后面是初始化不清零区域
UInt64 Ms() const; // 系统启动后的毫秒数
uint Seconds() const; // 系统绝对当前时间,秒
void Sleep(uint ms) const; // 毫秒级延迟
void Delay(uint us) const; // 微秒级延迟
typedef void (*FuncU32)(uint param);
FuncU32 OnSleep;
bool CheckMemory() const;
void Reset() const; // 重启系统
// 系统跟踪
void InitTrace(void* port) const;
void Trace(int times = 1) const;
public:
// 创建任务返回任务编号。dueTime首次调度时间msperiod调度间隔ms-1表示仅处理一次
uint AddTask(Action func, void* param, int dueTime = 0, int period = 0, cstring name = nullptr) const;
void RemoveTask(uint& taskid) const;
// 设置任务的开关状态同时运行指定任务最近一次调度的时间0表示马上调度
bool SetTask(uint taskid, bool enable, int msNextTime = -1) const;
// 改变任务周期
bool SetTaskPeriod(uint taskid, int period) const;
bool Started;
void Start(); // 开始系统大循环
};
extern TSys Sys; //创建一个全局的Sys对象 会在main函数之前执行构造函数
//#include "Time.h"
#include "Interrupt.h"
void EnterCritical();
void ExitCritical();
//extern uint32_t __REV(uint32_t value);
//extern uint32_t __REV16(uint16_t value);
uint _REV(uint value);
ushort _REV16(ushort value);
#endif //_Sys_H_
/*
v3.2.2016.0517 Core
v3.1.2015.1108
v3.0.2015.0806
v2.8.2014.0927 nRF24L01+
v2.7.2014.0919 线
v2.6.2014.0823
v2.5.2014.0819 new/delete实现Debug有效
v2.4.2014.0811 4k
TinyIPARP/ICMP/TCP/UDP7.5k
v2.3.2014.0806 使RTM优化编译
v2.2.2014.0801 使
v2.1.2014.0728 F0需配置RAM从0x200000C0开始
v2.0.2014.0725 使C++SmartOSIOUSARTSpiNRF24L01SPIFlashCANEnc28j60GD32超频
v1.3.2014.0624 Spi模块和NRF24L01+
v1.2.2014.0604 GD32芯片
v1.1.2014.0513 F0/F1的GPIO和串口功能
v1.0.2014.0506 SmartOS使C语言实现
*/
#include "Kernel\Sys.h"

View File

@ -22,7 +22,11 @@ namespace NewLife.Reflection
build.AddIncludes("..\\..\\Lib\\CMSIS");
build.AddIncludes("..\\..\\Lib\\Inc");
build.AddIncludes("..\\Core");
build.AddIncludes("..\\Kernel");
build.AddIncludes("..\\Device");
build.AddFiles("..\\Core");
build.AddFiles("..\\Kernel");
build.AddFiles("..\\Device");
build.AddFiles("..\\", "*.c;*.cpp", false, "CAN;DMA;Memory");
build.AddFiles("..\\Platform", "Boot_F1.cpp");
build.AddFiles("..\\Platform", "startup_stm32f10x.s");