增加获取系统启动时间的Sys.Ms(),等同于Time.Current(),但是无需再引用Time.h

This commit is contained in:
nnhy 2015-11-03 08:52:31 +00:00
parent c7c67f7d46
commit 59baca7be6
20 changed files with 71 additions and 81 deletions

View File

@ -1,5 +1,4 @@
#include "Time.h"
#include "Enc28j60.h"
#include "Enc28j60.h"
#define ENC_DEBUG DEBUG
#define NET_DEBUG DEBUG
@ -338,7 +337,7 @@ Enc28j60::Enc28j60()
_spi = NULL;
LastTime = Time.Current();
LastTime = Sys.Ms();
ResetPeriod = 6000;
_ResetTask = 0;
@ -689,7 +688,7 @@ bool Enc28j60::OnOpen()
#endif
debug_printf("Enc28j60::Inited! Revision=%d\r\n", rev);
LastTime = Time.Current();
LastTime = Sys.Ms();
if(!_ResetTask)
{
@ -995,7 +994,7 @@ uint Enc28j60::OnRead(ByteArray& bs)
WriteOp(ENC28J60_BIT_FIELD_SET, ECON2, ECON2_PKTDEC);
// 最后收到数据包的时间
LastTime = Time.Current();
LastTime = Sys.Ms();
return len;
}
@ -1087,7 +1086,7 @@ void Enc28j60::CheckError()
return;
}*/
uint ts = Time.Current() - LastTime;
uint ts = Sys.Ms() - LastTime;
if(ResetPeriod < ts)
{
Error++;

View File

@ -1,5 +1,4 @@
#include "Time.h"
#include "Stream.h"
#include "Stream.h"
#include "ITransport.h"
#include "Dhcp.h"
@ -105,9 +104,9 @@ void Dhcp::Request()
void Dhcp::Start()
{
ulong now = Time.Current();
ulong now = Sys.Ms();
_expiredTime = now + ExpiredTime;
if(!dhcpid) dhcpid = (now << 16) | Time.CurrentTicks();
if(!dhcpid) dhcpid = now;
debug_printf("Dhcp::Start ExpiredTime=%ds DhcpID=0x%08x\r\n", ExpiredTime, dhcpid);
@ -145,7 +144,7 @@ void Dhcp::SendDiscover(void* param)
if(!_dhcp->Running) return;
// 检查总等待时间
if(_dhcp->_expiredTime < Time.Current())
if(_dhcp->_expiredTime < Sys.Ms())
{
_dhcp->Stop();
return;

View File

@ -232,7 +232,7 @@ void HardRTC::SaveTicks()
#ifdef STM32F1
uint sec = Time.Seconds;
uint ms = Time.Current() - sec;
uint ms = Sys.Ms() - sec * 1000;
while(ms > 1000) ms -= 1000;
#if TIME_DEBUG
debug_printf("SaveTicks %ds %dms\r\n", sec, ms);

View File

@ -240,7 +240,7 @@ void TSys::Init(void)
// 堆起始地址,前面是静态分配内存
uint TSys::HeapBase()
{
return (uint)&__heap_base;
return (uint)&__heap_base;
}
// 栈顶,后面是初始化不清零区域
@ -474,6 +474,9 @@ void TimeSleep(uint us)
if(us) Time.Delay(us);
}
// 系统启动后的毫秒数
ulong Ms() { return Time.Current(); }
void TSys::Sleep(uint ms)
{
// 优先使用线程级睡眠

2
Sys.h
View File

@ -95,6 +95,8 @@ public:
uint HeapBase(); // 堆起始地址,前面是静态分配内存
uint StackTop(); // 栈顶,后面是初始化不清零区域
ulong Ms(); // 系统启动后的毫秒数
void Sleep(uint ms); // 毫秒级延迟
void Delay(uint us); // 微秒级延迟
typedef void (*FuncU32)(uint param);

View File

@ -81,7 +81,7 @@ void Task::Set(bool enable, int msNextTime)
Enable = enable;
// 可以安排最近一次执行的时间比如0表示马上调度执行
if(msNextTime >= 0) NextTime = Time.Current() + msNextTime;
if(msNextTime >= 0) NextTime = Sys.Ms() + msNextTime;
// 如果系统调度器处于Sleep让它立马退出
if(enable) Scheduler()->Sleeping = false;
@ -174,7 +174,7 @@ uint TaskScheduler::Add(Action func, void* param, int dueTime, int period, strin
task->Event = true;
}
else
task->NextTime = Time.Current() + dueTime;
task->NextTime = Sys.Ms() + dueTime;
Count++;
@ -236,7 +236,7 @@ void TaskScheduler::Stop()
// 执行一次循环。指定最大可用时间
void TaskScheduler::Execute(uint msMax)
{
ulong now = Time.Current();
ulong now = Sys.Ms();
ulong end = now + msMax;
ulong min = UInt64_Max; // 最小时间,这个时间就会有任务到来
@ -249,13 +249,13 @@ void TaskScheduler::Execute(uint msMax)
if((task->NextTime <= now || task->NextTime < 0)
// 并且任务的平均耗时要足够调度才安排执行避免上层是Sleep时超出预期时间
&& Time.Current() + task->CostMs <= end)
&& Sys.Ms() + task->CostMs <= end)
{
task->Execute(now);
// 为了确保至少被有效调度一次,需要在被调度任务内判断
// 如果已经超出最大可用时间,则退出
if(!msMax || Time.Current() > end) return;
if(!msMax || Sys.Ms() > end) return;
}
// 注意Execute内部可能已经释放了任务
if(task->ID && task->Enable)
@ -276,7 +276,7 @@ void TaskScheduler::Execute(uint msMax)
if(ct > MaxCost) MaxCost = ct;
// 如果有最小时间,睡一会吧
now = Time.Current(); // 当前时间
now = Sys.Ms(); // 当前时间
if(min != UInt64_Max && min > now)
{
min -= now;
@ -299,7 +299,7 @@ void TaskScheduler::ShowStatus(void* param)
debug_printf("Task::ShowStatus 平均 %dus 最大 %dus 当前 ", host->Cost, host->MaxCost);
Time.Now().Show();
debug_printf(" 启动 ");
DateTime dt(Time.Current() / 1000);
DateTime dt(Sys.Ms() / 1000);
dt.Show(true);
// 计算任务执行的平均毫秒数,用于中途调度其它任务,避免一个任务执行时间过长而堵塞其它任务

View File

@ -61,9 +61,9 @@ void FlashLed(void* param)
TinyMessage msg(0x10);
byte leds[] = {0, 1, 2};
leds[0] = Time.CurrentTicks() % 4 - 1;
leds[1] = Time.CurrentTicks() % 4 - 1;
leds[2] = Time.CurrentTicks() % 4 - 1;
leds[0] = Sys.Ms() % 4 - 1;
leds[1] = Sys.Ms() % 4 - 1;
leds[2] = Sys.Ms() % 4 - 1;
msg.SetData(leds, ArrayLength(leds));

View File

@ -1,6 +1,4 @@
#include "Sys.h"
#include "Time.h"
#include "Drivers\NRF24L01.h"
#include "Drivers\NRF24L01.h"
#include "conf.h"
@ -12,7 +10,7 @@ void OnSend(void* param)
{
// 最后4个字节修改为秒数
// 大概4.86%的误差
uint s = __REV(Time.Current() >> 10);
uint s = __REV(Sys.Ms() >> 10);
byte* p = tx_buf + ArrayLength(tx_buf) - 8;
Sys.ToHex(p, (byte*)&s, 4);

View File

@ -1,14 +1,13 @@
#include "Sys.h"
#include "Port.h"
#include "Thread.h"
#include "Time.h"
Thread* th;
void ThreadTask(void* param)
{
OutputPort* leds = (OutputPort*)param;
uint m = Time.Current() % 2;
uint m = Sys.Ms() % 2;
uint n = 100;
while(--n)
//while(true)

View File

@ -1,5 +1,4 @@
#include "Time.h"
#include "Thread.h"
#include "Thread.h"
#include "Task.h"
//#define TH_DEBUG DEBUG
@ -217,7 +216,7 @@ void Thread::Sleep(uint ms)
{
SmartIRQ irq; // 关闭全局中断
DelayExpire = Time.Current() + ms;
DelayExpire = Sys.Ms() + ms;
assert_param(State == Running || State == Ready);
@ -239,7 +238,7 @@ void Thread::Sleep(uint ms)
// 检查Sleep是否过期
bool Thread::CheckExpire()
{
if(State == Suspended && DelayExpire > 0 && DelayExpire <= Time.Current())
if(State == Suspended && DelayExpire > 0 && DelayExpire <= Sys.Ms())
{
//Resume();
State = Ready;

View File

@ -193,7 +193,7 @@ bool ArpSocket::Request(const IPAddress& ip, MacAddress& mac, int timeout)
_ArpSession = &ss;
// 等待响应
TimeWheel tw(0, timeout * 1000);
TimeWheel tw(timeout);
tw.Sleep = 1;
do{
if(ss.Success) break;
@ -226,7 +226,7 @@ bool ArpSocket::Resolve(const IPAddress& ip, MacAddress& mac)
ARP_ITEM* item = NULL; // 匹配项
if(_Arps)
{
uint sNow = Time.Current() >> 10; // 当前时间,秒
uint sNow = Sys.Ms() >> 10; // 当前时间,秒
// 在表中查找
for(int i=0; i<Count; i++)
{
@ -329,7 +329,7 @@ void ArpSocket::Add(const IPAddress& ip, const MacAddress& mac)
#endif
}
uint sNow = Time.Current() >> 10; // 当前时间,秒
uint sNow = Sys.Ms() >> 10; // 当前时间,秒
// 保存
item->IP = ip.Value;
item->Mac = mac;

View File

@ -129,7 +129,7 @@ bool IcmpSocket::Ping(IPAddress& ip, uint payloadLength)
ms.Write((byte)('a' + k));
}
ushort now = Time.Current();
ushort now = Sys.Ms();
ushort id = __REV16(Sys.ID[0]);
ushort seq = __REV16(now);
icmp->Identifier = id;

View File

@ -1,5 +1,4 @@
#include "Time.h"
#include "TinyIP.h"
#include "TinyIP.h"
#include "Arp.h"
#include "Task.h"
@ -41,7 +40,7 @@ void TinyIP::Init()
void TinyIP::Init(ITransport* port)
{
_port = port;
_StartTime = Time.Current();
_StartTime = Sys.Ms();
const byte defip_[] = {192, 168, 1, 1};
IPAddress defip(defip_);
@ -237,7 +236,7 @@ bool TinyIP::Open()
task->MaxDeepth = 2; // 以太网允许重入,因为有时候在接收里面等待下一次接收
#if NET_DEBUG
uint us = Time.Current() - _StartTime;
uint us = Sys.Ms() - _StartTime;
debug_printf("TinyIP Ready! Cost:%dms\r\n\r\n", us / 1000);
#endif

View File

@ -1,7 +1,4 @@
#include "Time.h"
#include "Task.h"
#include "TinyClient.h"
#include "TinyClient.h"
#include "JoinMessage.h"
@ -40,7 +37,7 @@ void TinyClient::Open()
Control->Received = OnClientReceived;
Control->Param = this;
TranID = (int)Time.Current();
TranID = (int)Sys.Ms();
_TaskID = Sys.AddTask(TinyClientTask, this, 0, 5000, "客户端服务");
@ -115,7 +112,7 @@ bool TinyClient::OnReceive(TinyMessage& msg)
if(Type != 0x01C8)
if(msg.Code != 0x01&& Server != msg.Src) return true;//不是无线中继,不是组网消息。不是被组网网关消息,不受其它消息设备控制.
if(msg.Src == Server) LastActive = Time.Current();
if(msg.Src == Server) LastActive = Sys.Ms();
switch(msg.Code)
{
@ -367,7 +364,7 @@ bool TinyClient::OnJoin(const TinyMessage& msg)
Sys.SetTaskPeriod(_TaskID, time * 1000);
// 组网成功更新一次最后活跃时间
LastActive = Time.Current();
LastActive = Sys.Ms();
// 保存配置
Cfg->Save();
@ -386,7 +383,7 @@ void TinyClient::Ping()
{
ushort off = Cfg->OfflineTime;
if(off < 10) off = 10;
if(LastActive > 0 && LastActive + off * 1000 < Time.Current())
if(LastActive > 0 && LastActive + off * 1000 < Sys.Ms())
{
if(Server == 0) return;
@ -411,7 +408,7 @@ void TinyClient::Ping()
Send(msg);
if(LastActive == 0) LastActive = Time.Current();
if(LastActive == 0) LastActive = Sys.Ms();
}
bool TinyClient::OnPing(const TinyMessage& msg)
@ -422,7 +419,7 @@ bool TinyClient::OnPing(const TinyMessage& msg)
// 忽略响应消息
if(msg.Reply)
{
if(msg.Src == Server) LastActive = Time.Current();
if(msg.Src == Server) LastActive = Sys.Ms();
return true;
}

View File

@ -1,6 +1,4 @@
#include "Time.h"
#include "Security\Crc.h"
#include "Security\Crc.h"
#include "TinyMessage.h"
@ -191,7 +189,7 @@ TinyController::TinyController() : Controller()
while(Address < 2 || Address > 254)
{
Sys.Delay(30);
Address = Time.CurrentTicks();
Address = Sys.Ms();
}
// 接收模式。0只收自己1接收自己和广播2接收所有。默认0
@ -368,7 +366,7 @@ void TinyController::AckRequest(const TinyMessage& msg)
MessageNode& node = _Queue[i];
if(node.Using && node.Sequence == msg.Sequence)
{
int cost = Time.Current() - node.LastSend;
int cost = Sys.Ms() - node.LastSend;
if(cost < 0) cost = -cost;
Total.Cost += cost;
@ -474,7 +472,7 @@ void TinyController::Loop()
// 检查时间。至少发送一次
if(node.Next > 0)
{
ulong now2 = Time.Current();
ulong now2 = Sys.Ms();
// 下一次发送时间还没到,跳过
if(node.Next > now2) continue;
@ -508,7 +506,7 @@ void TinyController::Loop()
memset(&Total, 0, sizeof(Total));
}
ulong now = Time.Current();
ulong now = Sys.Ms();
node.LastSend = now;
// 随机延迟。随机数1~5。每次延迟递增
@ -553,9 +551,9 @@ bool TinyController::Post(TinyMessage& msg, int expire)
if(!node) return false;
node->SetMessage(msg);
node->StartTime = Time.Current();
node->StartTime = Sys.Ms();
node->Next = 0;
node->Expired = Time.Current() + expire;
node->Expired = Sys.Ms() + expire;
Total.Msg++;
@ -656,7 +654,7 @@ void RingQueue::Push(ushort item)
if(Index == ArrayLength(Arr)) Index = 0;
// 更新过期时间10秒
Expired = Time.Current() + 10000;
Expired = Sys.Ms() + 10000;
}
int RingQueue::Find(ushort item)
@ -675,7 +673,7 @@ bool RingQueue::Check(ushort item)
if(!Expired) return false;
// 首先检查是否过期。如果已过期,说明很长时间都没有收到消息
if(Expired < Time.Current())
if(Expired < Sys.Ms())
{
//debug_printf("环形队列过期,清空 \r\n");
// 清空队列,重新开始

View File

@ -1,5 +1,4 @@
#include "Time.h"
#include "Flash.h"
#include "Flash.h"
#include "TinyServer.h"
#include "JoinMessage.h"
@ -98,7 +97,7 @@ bool TinyServer::OnReceive(TinyMessage& msg)
}
// 更新设备信息
if(dv) dv->LastTime = Time.Current();
if(dv) dv->LastTime = Sys.Ms();
// 设置当前设备
Current = dv;
@ -172,7 +171,7 @@ bool TinyServer::OnJoin(const TinyMessage& msg)
byte id = msg.Src;
if(!id) return false;
ulong now = Time.Current();
ulong now = Sys.Ms();
JoinMessage dm;
dm.ReadMessage(msg);
@ -251,7 +250,7 @@ bool TinyServer::OnJoin(const TinyMessage& msg)
bool TinyServer::ResetPassword(byte id)
{
ulong now = Time.Current();
ulong now = Sys.Ms();
JoinMessage dm;

View File

@ -1,5 +1,4 @@
#include "Time.h"
#include "Gateway.h"
#include "Gateway.h"
#include "Config.h"
#include "Security\MD5.h"
@ -68,7 +67,7 @@ void Gateway::Start()
dv->Kind = Sys.Code;
dv->HardID.SetLength(16);
dv->HardID = Sys.ID;
dv->LastTime = Time.Current();
dv->LastTime = Sys.Ms();
dv->Name = Sys.Name;
Server->Devices.Push(dv);
@ -124,7 +123,7 @@ bool Gateway::OnLocal(const TinyMessage& msg)
if(dv)
{
// 短时间内注册或者登录
ulong now = Time.Current() - 500;
ulong now = Sys.Ms() - 500;
if(dv->RegTime > now) DeviceRegister(dv->Address);
if(dv->LoginTime > now) DeviceOnline(dv->Address);
}

View File

@ -1,5 +1,4 @@
#include "Time.h"
#include "LoginMessage.h"
#include "LoginMessage.h"
#include "Security\MD5.h"
@ -29,7 +28,7 @@ void LoginMessage::Write(Stream& ms)
// 密码取MD5后传输
ms.WriteArray(MD5::Hash(Key));
ulong now = Time.Current();
ulong now = Sys.Ms();
Salt.Set((byte*)&now, 8);
ms.WriteArray(Salt);

View File

@ -71,7 +71,7 @@ bool TokenClient::Reply(TokenMessage& msg)
bool TokenClient::OnReceive(TokenMessage& msg)
{
LastActive = Time.Current();
LastActive = Sys.Ms();
switch(msg.Code)
{
@ -218,7 +218,7 @@ bool TokenClient::OnHello(TokenMessage& msg)
ext2.Reply = msg.Reply;
//ext2.LocalTime = ext.LocalTime;
// 使用当前时间
ext2.LocalTime = Time.Current() * 1000;
ext2.LocalTime = Sys.Ms() * 1000;
ext2.WriteMessage(rs);
Reply(rs);
@ -321,7 +321,7 @@ bool TokenClient::OnLogin(TokenMessage& msg)
// Ping指令用于保持与对方的活动状态
void TokenClient::Ping()
{
if(LastActive > 0 && LastActive + 30000 < Time.Current())
if(LastActive > 0 && LastActive + 30000 < Sys.Ms())
{
// 30秒无法联系服务端可能已经掉线重启Hello任务
debug_printf("30秒无法联系服务端可能已经掉线重新开始握手\r\n");
@ -333,7 +333,7 @@ void TokenClient::Ping()
TokenMessage msg(3);
ulong time = Time.Current();
ulong time = Sys.Ms();
Stream ms = msg.ToStream();
ms.WriteArray(ByteArray(&time, 8));
msg.Length = ms.Position();
@ -348,7 +348,7 @@ bool TokenClient::OnPing(TokenMessage& msg)
Stream ms = msg.ToStream();
ulong now = Time.Current();
ulong now = Sys.Ms();
ulong start = ms.ReadArray().ToUInt64();
int cost = (int)(now - start);
if(cost < 0) cost = -cost;

View File

@ -415,7 +415,7 @@ bool TokenController::StartSendStat(byte code)
if(_Queue[i].Code == 0)
{
_Queue[i].Code = code;
_Queue[i].Time = Time.Current();
_Queue[i].Time = Sys.Ms();
return true;
}
}
@ -434,7 +434,7 @@ bool TokenController::EndSendStat(byte code, bool success)
bool rs = false;
if(success)
{
int cost = (int)(Time.Current() - _Queue[i].Time);
int cost = (int)(Sys.Ms() - _Queue[i].Time);
// 莫名其妙,有时候竟然是负数
if(cost < 0) cost = -cost;
if(cost < 1000)