From f5c79a0d8a7c071f6f865c0cb3049e9d1b2ab7a0 Mon Sep 17 00:00:00 2001 From: nnhy Date: Tue, 17 May 2016 14:38:54 +0000 Subject: [PATCH] =?UTF-8?q?=E4=B8=B4=E7=95=8C=E5=8C=BA=E5=92=8C=E5=80=92?= =?UTF-8?q?=E7=BD=AE=E7=94=B1Sys=E5=AE=9E=E7=8E=B0=EF=BC=8C=E4=BB=A5?= =?UTF-8?q?=E5=90=8E=E7=8B=AC=E7=AB=8B=E5=88=B0Chip?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/Queue.cpp | 20 ++++++++++++++------ Core/Stream.cpp | 16 ++++++++-------- Sys.cpp | 5 +++++ Sys.h | 4 ++++ 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/Core/Queue.cpp b/Core/Queue.cpp index 0ccd80a9..f08c3121 100644 --- a/Core/Queue.cpp +++ b/Core/Queue.cpp @@ -9,7 +9,7 @@ extern void EnterCritical(); extern void ExitCritical(); -// 智能IRQ,初始化时备份,销毁时还原 +/*// 智能IRQ,初始化时备份,销毁时还原 // SmartIRQ相当霸道,它直接关闭所有中断,再也没有别的任务可以跟当前任务争夺MCU class SmartIRQ { @@ -23,7 +23,7 @@ public: { ExitCritical(); } -}; +};*/ Queue::Queue(uint len) : _s(len) { @@ -54,16 +54,20 @@ void Queue::Push(byte dat) // 除法运算是一个超级大祸害,它浪费了大量时间,导致串口中断接收丢数据 if(_head >= _s.Capacity()) _head -= _s.Capacity(); - SmartIRQ irq; + //SmartIRQ irq; + EnterCritical(); _size++; + ExitCritical(); } byte Queue::Pop() { if(_size == 0) return 0; { - SmartIRQ irq; + //SmartIRQ irq; + EnterCritical(); _size--; + ExitCritical(); } /* @@ -121,8 +125,10 @@ uint Queue::Write(const Buffer& bs) } { - SmartIRQ irq; + //SmartIRQ irq; + EnterCritical(); _size += rs; + ExitCritical(); } return rs; @@ -176,8 +182,10 @@ uint Queue::Read(Buffer& bs) //bs.SetLength(rs, false); { - SmartIRQ irq; + //SmartIRQ irq; + EnterCritical(); _size -= rs; + ExitCritical(); } //if(_size == 0) _tail = _head; diff --git a/Core/Stream.cpp b/Core/Stream.cpp index aa67f2eb..455df225 100644 --- a/Core/Stream.cpp +++ b/Core/Stream.cpp @@ -10,8 +10,8 @@ #include "Stream.h" -extern ushort _Rev16(ushort); -extern uint _Rev32(uint); +extern ushort _REV16(ushort); +extern uint _REV(uint); // 使用缓冲区初始化数据流。注意,此时指针位于0,而内容长度为缓冲区长度 Stream::Stream(void* buf, uint len) @@ -331,7 +331,7 @@ ushort Stream::ReadUInt16() ushort v; Buffer bs(&v, sizeof(v)); if(!Read(bs)) return 0; - if(!Little) v = _Rev16(v); + if(!Little) v = _REV16(v); return v; } @@ -340,7 +340,7 @@ uint Stream::ReadUInt32() uint v; Buffer bs(&v, sizeof(v)); if(!Read(bs)) return 0; - if(!Little) v = _Rev32(v); + if(!Little) v = _REV(v); return v; } @@ -349,7 +349,7 @@ UInt64 Stream::ReadUInt64() UInt64 v; Buffer bs(&v, sizeof(v)); if(!Read(bs)) return 0; - if(!Little) v = _Rev32(v >> 32) | ((UInt64)_Rev32(v & 0xFFFFFFFF) << 32); + if(!Little) v = _REV(v >> 32) | ((UInt64)_REV(v & 0xFFFFFFFF) << 32); return v; } @@ -360,21 +360,21 @@ bool Stream::Write(byte value) bool Stream::Write(ushort value) { - if(!Little) value = _Rev16(value); + if(!Little) value = _REV16(value); return Write(Buffer(&value, sizeof(value))); } bool Stream::Write(uint value) { - if(!Little) value = _Rev32(value); + if(!Little) value = _REV(value); return Write(Buffer(&value, sizeof(value))); } bool Stream::Write(UInt64 value) { - if(!Little) value = _Rev32(value >> 32) | ((UInt64)_Rev32(value & 0xFFFFFFFF) << 32); + if(!Little) value = _REV(value >> 32) | ((UInt64)_REV(value & 0xFFFFFFFF) << 32); return Write(Buffer(&value, sizeof(value))); } diff --git a/Sys.cpp b/Sys.cpp index 49f6cc9a..78362664 100644 --- a/Sys.cpp +++ b/Sys.cpp @@ -571,6 +571,11 @@ void TSys::Trace(int times) const //#endif } +/******************************** 临界区 ********************************/ + +void EnterCritical() { __disable_irq(); } +void ExitCritical() { __enable_irq(); } + /******************************** REV ********************************/ uint _REV(uint value) { return __REV(value); } diff --git a/Sys.h b/Sys.h index 2e4c8037..52c057fc 100644 --- a/Sys.h +++ b/Sys.h @@ -3,6 +3,7 @@ #include #include +#include /*#include #include */ @@ -130,6 +131,9 @@ 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);