减少使用TimeWheel

This commit is contained in:
Stone 2016-06-19 16:41:55 +00:00
parent 849ff61678
commit 61cc2b71ea
10 changed files with 95 additions and 118 deletions

View File

@ -78,9 +78,12 @@ bool IR::Send(const Buffer& bs)
// 开始 中断内处理数据
// _Pwm->Open();
_Tim->Open();
// 等待结束
TimeWheel tw(2);
while(!tw.Expired() && Stat != SendOver) Sys.Sleep(50);
_SendOK = false;
WaitHandle wh;
wh.WaitOne(2000, _SendOK);
// 结束 不论是超时还是发送结束都关闭pwm和定时器
//_Pwm->Close();
_Tim->Register(nullptr,nullptr);
@ -135,6 +138,8 @@ void IR::OnSend(void* sender, void* param)
//ir->_Pwm->Close();
//ir->_Tim->Close();
Stat = SendOver;
_SendOK = true;
return;
}
#endif
@ -217,9 +222,10 @@ int IR::Receive(Buffer& bs, int sTimeout)
// 等待起始条件DMA 开始搬运数据
Stat = WaitRev;
TimeWheel tw(sTimeout);
while(!tw.Expired()
&& DMA_GetCurrDataCounter(DMA1_Channel5) == DmaLen) Sys.Sleep(50);
tw.Sleep = 50;
while(!tw.Expired() && DMA_GetCurrDataCounter(DMA1_Channel5) == DmaLen);
if(tw.Expired())
{

View File

@ -22,10 +22,6 @@
class IR
{
private:
PWM* _Pwm = nullptr;
Timer* _Tim = nullptr;
AlternatePort * _Port = nullptr;
public:
IR(PWM * pwm);
@ -36,6 +32,12 @@ public:
int Receive(Buffer& bs, int sTimeout = 10);
private:
PWM* _Pwm = nullptr;
Timer* _Tim = nullptr;
AlternatePort * _Port = nullptr;
bool _SendOK;
//bool _RecvOK;
static void OnSend(void* sender, void* param);
};

View File

@ -50,22 +50,25 @@ void IC74HC165MOR::Trigger()
for(int i = 0; i < _Bs.Length(); i++)
{
byte temp = 0x00;
TimeWheel tw(0,2);
for(int j = 0; j < 8; j++)
int j = 0;
for(j = 0; j < 8; j++)
{
while(!tw.Expired() && _SCK != true ); // 等待 高电平
if(tw.Expired()) break;
int times = 2000;
while(!_SCK && --times); // 等待 高电平
if(!times) break;
if(_In) temp |= 0x01;
else temp &= 0xfe;
while(!tw.Expired() && _SCK != false ); // 等待低电平
times = 2000;
while(_SCK && --times); // 等待低电平
if(!times) break;
temp <<= 1;
}
_Bs[i] = temp;
if(tw.Expired())return; // 2ms 不结束 就强制退出
//if(tw.Expired())return; // 2ms 不结束 就强制退出
if(j < 8) break;
}
}

View File

@ -100,8 +100,8 @@ uint HX711::Read()
uint temp = 0x00000000;
// 等待 IC 数据
TimeWheel tw(0, 30);
while(!tw.Expired() && DOUT);
int times = 30000;
while(DOUT && --times);
// 读取数据
for(int i = 0;i < 24; i++)

View File

@ -857,7 +857,7 @@ bool NRF24L01::SendTo(const Buffer& bs, const Buffer& addr)
// https://devzone.nordicsemi.com/question/17074/nrf24l01-data-loss/
// It is important never to keep the nRF24L01+ in TX mode for more than 4ms at a time.
// If the Enhanced ShockBurst™ features are enabled, nRF24L01+ is never in TX mode longer than 4ms
TimeWheel tw(0, ms);
auto end = Sys.Ms() + ms;
do
{
Status = ReadReg(STATUS);
@ -894,7 +894,7 @@ bool NRF24L01::SendTo(const Buffer& bs, const Buffer& addr)
break;
}
}while(!tw.Expired());
}while(Sys.Ms() < end);
// 这里开始到CE拉高结束大概耗时186us。不拉高CE大概20us
//_CE = true;

View File

@ -3,8 +3,6 @@
#include "Sys.h"
class DateTime;
// 时间类
// 使用双计数时钟TIMx专用于毫秒级系统计时SysTick用于微秒级延迟秒级以及以上采用全局整数累加
// 这样子可以避免频繁使用微秒时带来长整型乘除法

View File

@ -1,23 +1,21 @@
#include "Time.h"
#include "Arp.h"
#include "WaitHandle.h"
#define NET_DEBUG 0
class ArpSession
{
public:
IPAddress IP;
const IPAddress& IP;
MacAddress Mac;
bool Success;
WaitHandle Handle;
ArpSession(const IPAddress& ip)
{
IP = ip;
Success = false;
}
ArpSession(const IPAddress& ip) : IP(ip) { }
};
ArpSession* _ArpSession;
ArpSession* _ArpSession = nullptr;
ArpSocket::ArpSocket(TinyIP* tip) : TinySocket(tip, IP_NONE)
{
@ -80,7 +78,9 @@ bool ArpSocket::Process(IP_HEADER& ip, Stream& ms)
if(arp->Option == 0x0200 && _ArpSession && _ArpSession->IP.Value == arp->SrcIP)
{
_ArpSession->Mac = arp->SrcMac.Value();
_ArpSession->Success = true;
_ArpSession->Handle.Set();
_ArpSession = nullptr;
return true;
}
@ -164,30 +164,16 @@ bool ArpSocket::Request(const IPAddress& ip, MacAddress& mac, int timeout)
// 如果没有超时时间,表示异步请求,不用等待结果
if(timeout <= 0) return false;
// 等待反馈
//if(Tip->LoopWait(RequestCallback, &mac, timeout * 1000)) return true;
// 等待响应
ArpSession ss(ip);
_ArpSession = &ss;
// 等待响应
TimeWheel tw(timeout);
tw.Sleep = 1;
do{
if(ss.Success) break;
}while(!tw.Expired());
if(!ss.Handle.WaitOne(timeout)) return false;
if(ss.Success)
{
mac = ss.Mac;
return true;
}
_ArpSession = nullptr;
return false;
}
bool ArpSocket::Resolve(const IPAddress& ip, MacAddress& mac)
{
mac = MacAddress::Empty();

View File

@ -2,6 +2,8 @@
#include "Icmp.h"
#include "Arp.h"
#include "WaitHandle.h"
#define NET_DEBUG DEBUG
class PingSession
@ -10,14 +12,13 @@ public:
IPAddress Address;
ushort Identifier;
ushort Sequence;
bool Success;
WaitHandle Handle;
PingSession(IPAddress& ip, ushort id, ushort seq)
{
Address = ip;
Identifier = id;
Sequence = seq;
Success = false;
}
bool Check(IPAddress& remote, ICMP_HEADER* icmp)
@ -50,7 +51,9 @@ bool IcmpSocket::Process(IP_HEADER& ip, Stream& ms)
// 检查有没有会话等待
if(icmp->Type == 0 && _IcmpSession != nullptr && _IcmpSession->Check(remote, icmp))
{
_IcmpSession->Success = true;
_IcmpSession->Handle.Set();
_IcmpSession = nullptr;
return true;
}
@ -147,25 +150,15 @@ bool IcmpSocket::Ping(IPAddress& ip, uint payloadLength)
#endif
Tip->SendIP(IP_ICMP, ip, (byte*)icmp, sizeof(ICMP_HEADER) + payloadLength);
// 等待时间
//int ps[] = {ip.Value, id, seq};
//if(Tip->LoopWait(PingCallback, ps, 1000)) return true;
PingSession ps(ip, id, seq);
_IcmpSession = &ps;
// 等待响应
TimeWheel tw(0, 1000);
tw.Sleep = 1;
do{
if(ps.Success) break;
}while(!tw.Expired());
_IcmpSession = nullptr;
bool rs = ps.Handle.WaitOne(1000);
#if NET_DEBUG
uint cost = ct.Elapsed() / 1000;
if(ps.Success)
if(rs)
debug_printf(" 成功 %dms\r\n", cost);
else
debug_printf(" 失败 %dms\r\n", cost);

View File

@ -1,11 +1,11 @@
#include "Time.h"
#include "Tcp.h"
#include "WaitHandle.h"
#define NET_DEBUG 0
//#define NET_DEBUG DEBUG
bool* WaitAck;
bool Callback(TinyIP* tip, void* param, Stream& ms);
TcpSocket::TcpSocket(TinyIP* tip) : TinySocket(tip, IP_TCP)
@ -79,9 +79,10 @@ bool TcpSocket::Process(IP_HEADER& ip, Stream& ms)
//Local.Port = port;
//Local.Address = ip.DestIP;
if(WaitAck && (tcp->Flags & TCP_FLAGS_ACK))
if(_wait && (tcp->Flags & TCP_FLAGS_ACK))
{
*WaitAck = true;
((WaitHandle*)_wait)->Set();
_wait = nullptr;
}
OnProcess(*tcp, ms);
@ -408,26 +409,19 @@ bool TcpSocket::Send(const Buffer& bs)
//debug_printf("Seq=0x%04x Ack=0x%04x \r\n", Seq, Ack);
if(!SendPacket(*tcp, bs.Length(), TCP_FLAGS_PUSH | TCP_FLAGS_ACK)) return false;
bool wait = false;
WaitAck = &wait;
// 等待响应
TimeWheel tw(0, 3000);
tw.Sleep = 1;
do{
if(wait) break;
}while(!tw.Expired());
WaitAck = nullptr;
WaitHandle wh;
_wait = &wh;
bool rs = wh.WaitOne(3000);
#if NET_DEBUG
if(wait)
if(rs)
debug_printf("发送成功!\r\n");
else
debug_printf("发送失败!\r\n");
#endif
return wait;
return rs;
}
uint TcpSocket::Receive(Buffer& bs)
@ -474,19 +468,11 @@ bool TcpSocket::Connect(IPAddress& ip, ushort port)
return false;
}
bool wait = false;
WaitAck = &wait;
// 等待响应
TimeWheel tw(0, 3000);
tw.Sleep = 1;
do{
if(wait) break;
}while(!tw.Expired());
WaitHandle wh;
_wait = &wh;
WaitAck = nullptr;
if(wait)
if(wh.WaitOne(3000))
{
if(Status == Established)
{

View File

@ -70,6 +70,9 @@ protected:
virtual bool OnWrite(const Buffer& bs);
virtual uint OnRead(Buffer& bs);
private:
void* _wait = nullptr;
};
#endif