临界区和倒置由Sys实现,以后独立到Chip

This commit is contained in:
nnhy 2016-05-17 14:38:54 +00:00
parent f099398fba
commit f5c79a0d8a
4 changed files with 31 additions and 14 deletions

View File

@ -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;

View File

@ -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)));
}

View File

@ -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); }

4
Sys.h
View File

@ -3,6 +3,7 @@
#include <stdint.h>
#include <stdio.h>
#include <stddef.h>
/*#include <stdlib.h>
#include <string.h>*/
@ -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);