减少使用TimeWheel
This commit is contained in:
parent
849ff61678
commit
61cc2b71ea
68
App/IR.cpp
68
App/IR.cpp
|
@ -29,7 +29,7 @@ typedef enum
|
|||
WaitRev,
|
||||
Rev,
|
||||
RevOver,
|
||||
|
||||
|
||||
Sending,
|
||||
SendOver,
|
||||
}IRStat;
|
||||
|
@ -45,7 +45,7 @@ bool IR::Send(const Buffer& bs)
|
|||
#ifdef STM32F0
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
||||
TIM_DeInit(TIM2);
|
||||
|
||||
|
||||
if(_Tim == nullptr)
|
||||
{
|
||||
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
||||
|
@ -54,7 +54,7 @@ bool IR::Send(const Buffer& bs)
|
|||
// _Tim->SetFrequency(50); // 5Hz 20ms
|
||||
// 使用一个字节 需要自行配置分频系数
|
||||
_Tim->Prescaler = 400; // 分频 需要结果是 Period < 256 时能计数 20ms
|
||||
|
||||
|
||||
SendP = (ushort *)bs.GetBuffer();
|
||||
SendBufLen = bs.Length()/2;
|
||||
SendIndex = 0;
|
||||
|
@ -63,24 +63,27 @@ bool IR::Send(const Buffer& bs)
|
|||
// 取消接收时候的配置
|
||||
_Tim->Opened = true;
|
||||
_Tim->Close();
|
||||
|
||||
|
||||
// 注册中断
|
||||
_Tim->Register(OnSend,this);
|
||||
// 重新配置
|
||||
_Tim->SetCounter(0x00000000);
|
||||
_Tim->Config();
|
||||
|
||||
|
||||
Stat = Sending;
|
||||
debug_printf("Start %04X\r\n",SendP[0]);
|
||||
|
||||
|
||||
// 暂时避过 定时器一启动立马中断的错误
|
||||
ErrorIRQ = true;
|
||||
// 开始 中断内处理数据
|
||||
// _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);
|
||||
|
@ -88,7 +91,7 @@ bool IR::Send(const Buffer& bs)
|
|||
// 清空使用的数据
|
||||
SendIndex = 0;
|
||||
SendP = nullptr;
|
||||
|
||||
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, DISABLE);
|
||||
#endif
|
||||
return true;
|
||||
|
@ -111,7 +114,7 @@ void IR::OnSend(void* sender, void* param)
|
|||
// PWM归位
|
||||
TIM_SetCounter(ti, 0x00000000);
|
||||
// PWM变化
|
||||
//if(SendIndex % 2)
|
||||
//if(SendIndex % 2)
|
||||
if(SendIndex & 1) //不行 暂不知问题所在
|
||||
{
|
||||
TIM_Cmd(ti, ENABLE);
|
||||
|
@ -135,6 +138,8 @@ void IR::OnSend(void* sender, void* param)
|
|||
//ir->_Pwm->Close();
|
||||
//ir->_Tim->Close();
|
||||
Stat = SendOver;
|
||||
_SendOK = true;
|
||||
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
@ -146,7 +151,7 @@ int IR::Receive(Buffer& bs, int sTimeout)
|
|||
#ifdef STM32F0
|
||||
uint bufLen = bs.Length();
|
||||
uint DmaLen = bufLen/2 -1;
|
||||
|
||||
|
||||
// 捕获引脚初始化
|
||||
if(_Port == nullptr)
|
||||
{
|
||||
|
@ -156,39 +161,39 @@ int IR::Receive(Buffer& bs, int sTimeout)
|
|||
_Port->AFConfig(Port::AF_2);
|
||||
}
|
||||
if(_Tim == nullptr) _Tim = Timer::Create(0x01); // 直接占用TIMER2
|
||||
|
||||
|
||||
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, DISABLE);
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
||||
// TIM_DeInit(TIM2);
|
||||
|
||||
|
||||
// _Tim->SetFrequency(50); //Prescaler Period 需要定制
|
||||
_Tim->Prescaler = 400;
|
||||
// 波形间隔悬殊,用一个字节误差太大
|
||||
_Tim->Period = 65536;
|
||||
_Tim->Config();
|
||||
|
||||
|
||||
/* 使能自动装载 */
|
||||
TIM_ARRPreloadConfig(TIM2, ENABLE);
|
||||
// 捕获设置
|
||||
// 2通道映射到1通道 配套使用DMA1 Channel5
|
||||
TIM_ICInitTypeDef TIM_ICInitStructure;
|
||||
TIM_ICInitTypeDef TIM_ICInitStructure;
|
||||
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
|
||||
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;
|
||||
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_IndirectTI;
|
||||
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
|
||||
TIM_ICInitStructure.TIM_ICFilter = 0x0;
|
||||
TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);
|
||||
|
||||
|
||||
/* Select the TIM2 Input Trigger: TI2FP2 */
|
||||
TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);
|
||||
/* 捕获的同时清空定时器计数,不能使用 这个复位是以电平为准,不是沿
|
||||
/* 捕获的同时清空定时器计数,不能使用 这个复位是以电平为准,不是沿
|
||||
使用会使采集到的数据一半为零 后期数据处理时候做差就好 */
|
||||
// TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);
|
||||
// TIM_SelectMasterSlaveMode(TIM2,TIM_MasterSlaveMode_Enable);
|
||||
// DMA 读取CNT寄存器,一字节设置 无法解决数据冗余
|
||||
// TIM_DMAConfig(TIM2, TIM_DMABase_CNT, TIM_DMABurstLength_1Transfer);
|
||||
//TIM_Cmd(TIM2, ENABLE);
|
||||
|
||||
|
||||
// DMA 时钟
|
||||
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);
|
||||
// 使用通道5
|
||||
|
@ -208,34 +213,35 @@ int IR::Receive(Buffer& bs, int sTimeout)
|
|||
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
|
||||
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
|
||||
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
|
||||
|
||||
|
||||
// 开启DMA TIME
|
||||
DMA_Cmd(DMA1_Channel5, ENABLE);
|
||||
TIM_Cmd(TIM2, ENABLE);
|
||||
// 连接DMA 触发机制
|
||||
TIM_DMACmd(TIM2, TIM_DMA_CC1, ENABLE);
|
||||
|
||||
|
||||
// 等待起始条件,DMA 开始搬运数据
|
||||
Stat = WaitRev;
|
||||
TimeWheel tw(sTimeout);
|
||||
while(!tw.Expired()
|
||||
&& DMA_GetCurrDataCounter(DMA1_Channel5) == DmaLen) Sys.Sleep(50);
|
||||
|
||||
if(tw.Expired())
|
||||
{
|
||||
TimeWheel tw(sTimeout);
|
||||
tw.Sleep = 50;
|
||||
while(!tw.Expired() && DMA_GetCurrDataCounter(DMA1_Channel5) == DmaLen);
|
||||
|
||||
if(tw.Expired())
|
||||
{
|
||||
// 超时没有收到学习
|
||||
Stat = RevOver;
|
||||
// 关闭Time DMA
|
||||
DMA_Cmd(DMA1_Channel5, DISABLE);
|
||||
TIM_Cmd(TIM2, DISABLE);
|
||||
|
||||
|
||||
int length = DMA_GetCurrDataCounter(DMA1_Channel5);
|
||||
debug_printf("\r\n DMA::Counter %d \r\n", length);
|
||||
|
||||
|
||||
bs.SetLength(0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
// 进入接收状态
|
||||
Stat = Rev;
|
||||
debug_printf("\r\n开始接收数据\r\n");
|
||||
|
@ -243,9 +249,9 @@ int IR::Receive(Buffer& bs, int sTimeout)
|
|||
// 不能使用 定时器 中断作为超时依据 只能等。。
|
||||
tw.Reset(0,400);
|
||||
while(!tw.Expired()) Sys.Sleep(50);
|
||||
|
||||
|
||||
// 接收结束
|
||||
// 关闭Time DMA
|
||||
// 关闭Time DMA
|
||||
DMA_Cmd(DMA1_Channel5, DISABLE);
|
||||
TIM_Cmd(TIM2, DISABLE);
|
||||
// 本次接收结束,关闭 RCC 防止这些配置贻害无穷
|
||||
|
@ -253,7 +259,7 @@ int IR::Receive(Buffer& bs, int sTimeout)
|
|||
// 拿到数据长度
|
||||
uint len = DmaLen - DMA_GetCurrDataCounter(DMA1_Channel5);
|
||||
debug_printf("DMA REV %d byte\r\n",len);
|
||||
|
||||
|
||||
if(len > 30)
|
||||
{
|
||||
if(len >= DmaLen)
|
||||
|
|
10
App/IR.h
10
App/IR.h
|
@ -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);
|
||||
};
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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++)
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -3,8 +3,6 @@
|
|||
|
||||
#include "Sys.h"
|
||||
|
||||
class DateTime;
|
||||
|
||||
// 时间类
|
||||
// 使用双计数时钟,TIMx专用于毫秒级系统计时,SysTick用于微秒级延迟,秒级以及以上采用全局整数累加
|
||||
// 这样子可以避免频繁使用微秒时带来长整型乘除法
|
||||
|
|
|
@ -1,23 +1,21 @@
|
|||
#include "Time.h"
|
||||
#include "Arp.h"
|
||||
|
||||
#include "WaitHandle.h"
|
||||
|
||||
#define NET_DEBUG 0
|
||||
|
||||
class ArpSession
|
||||
{
|
||||
public:
|
||||
IPAddress IP;
|
||||
MacAddress Mac;
|
||||
bool Success;
|
||||
const IPAddress& IP;
|
||||
MacAddress Mac;
|
||||
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,28 +164,14 @@ 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;
|
||||
mac = ss.Mac;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ArpSocket::Resolve(const IPAddress& ip, MacAddress& mac)
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -70,6 +70,9 @@ protected:
|
|||
|
||||
virtual bool OnWrite(const Buffer& bs);
|
||||
virtual uint OnRead(Buffer& bs);
|
||||
|
||||
private:
|
||||
void* _wait = nullptr;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue