本帖最后由 wang0225 于 2011-8-18 15:12 编辑
#include "COND.h"
#define LOOK_H 1
#define LOOK_SEMIHOST 1
#if LOOK_H == 0
#include "NUC1xx.h"
#include "NUC1xxM051Seriescfg.h"
#else
#define __HAVE_GPIO
#include <nuc120re3an.h>
using namespace nuvoton;
#endif
#ifdef LOOK_SEMIHOST
#include <ostream>
#include <semihosting>
using namespace redapricot;
ostream_t<semihosting::console_t, 128> cout;
#endif
int sum = 0;
flag_t flag(0);
mutex_t mutex;
cond_t cond;
class Keyboard_t : public interrupt_t {
public:
__INLINE__ Keyboard_t();
protected:
bool isr(int vector);
void dsr(int vector, uintptr_t count);
};
// Keyboard_t 构造函数
__INLINE__ Keyboard_t::Keyboard_t()
{
#ifdef LOOK_SEMIHOST
cout << "Keyboard_t构造函数开始" << endl;
#endif
attach(EINT0_IRQn);
attach(EINT1_IRQn);
#if LOOK_H == 0
GPIOBs.IEN.Regs = (1 << Pin15) | (1 << Pin14); // 开启中断
#else
GPIOB.IEN(0)
.IF_EN15(1)
.IF_EN14(1); // 开启中断
#endif
vector_t::enable(EINT0_IRQn);
vector_t::enable(EINT1_IRQn);
#ifdef LOOK_SEMIHOST
cout << "Keyboard_t构造函数结束" << endl;
#endif
}
// Keyboard_t 中断服务例程
bool Keyboard_t::isr(int vector)
{
#if LOOK_H == 0
GPIOBs.ISRC.Regs = GPIOBs.ISRC.Regs; // 清中断 flag
#else
//注意:下句被优化了
// GPIOB.ISRC = GPIOB.ISRC; // 清中断 flag
//注意:下句不能被优化,特别注意()是建立缓存
GPIOB.ISRC = GPIOB.ISRC(); // 清中断 flag
#endif
#ifdef LOOK_SEMIHOST
cout << "中断Keyboard_t清除中断标志" << endl;
#endif
return true;
}
// Keyboard_t 中断滞后服务例程
void Keyboard_t::dsr(int vector, uintptr_t count)
{
if (vector == EINT0_IRQn)//Key2
{
flag.do_set_bits(0b110);
#ifdef LOOK_SEMIHOST
cout << "中断Keyboard_t按下Key2键" << endl;
#endif
}
else if (vector == EINT1_IRQn)//Key1
{
flag.do_set_bits(0b101);
#ifdef LOOK_SEMIHOST
cout << "中断Keyboard_t按下Key1键" << endl;
#endif
}
}
Keyboard_t Key; // 创建Key对象
// 任务类 task_COND1_t 的例程
void task_COND1_t::routine()
{
// TODO: 在此编写 task_COND1_t 例程的内容
#ifdef LOOK_SEMIHOST
cout << "任务task_COND1_t开始:" << endl;
#endif
while (true) {
// TODO: 在此编写 task_COND1_t 例程的内容
int 标志寄存器 = flag.wait(0b001, flag_t::ANY_CONSUME);
if (标志寄存器)
{
#ifdef LOOK_SEMIHOST
cout << "任务task_COND1_t 标志寄存器=" << 标志寄存器 << endl;
#endif
mutex.lock();
#ifdef LOOK_SEMIHOST
cout << "任务task_COND1_t mutex.lock()" << endl;
#endif
sum++;
if (sum < 2) {mutex.unlock();
#ifdef LOOK_SEMIHOST
cout << "任务task_COND1_t mutex.unlock()" << endl;
#endif
}
else{
mutex.unlock();
cond.signal();
#ifdef LOOK_SEMIHOST
cout << "任务task_COND1_t cond.signal()" << endl;
cout << "任务task_COND1_t mutex.unlock()" << endl;
#endif
}
}
}
}
// 任务类 task_COND2_t 的例程
void task_COND2_t::routine()
{
// TODO: 在此编写 task_COND2_t 例程的内容
while (true) {
// TODO: 在此编写 task_COND2_t 例程的内容
#ifdef LOOK_SEMIHOST
cout << "任务task_COND2_t开始:" << endl;
#endif
int 标志寄存器 = flag.wait(0b010, flag_t::ANY_CONSUME);
if (标志寄存器)
{
#ifdef LOOK_SEMIHOST
cout << "任务task_COND2_t 标志寄存器=" << 标志寄存器 << endl;
#endif
mutex.lock();
#ifdef LOOK_SEMIHOST
cout << "任务task_COND2_t mutex.lock()" << endl;
#endif
sum++;
if (sum < 2) {mutex.unlock();
#ifdef LOOK_SEMIHOST
cout << "任务task_COND2_t mutex.unlock()" << endl;
#endif
}
else{
mutex.unlock();
cond.signal();
#ifdef LOOK_SEMIHOST
cout << "任务task_COND2_t cond.signal()" << endl;
cout << "任务task_COND2_t mutex.unlock()" << endl;
#endif
}
}
}
}
// 任务类 task_COND3_t 的例程
void task_COND3_t::routine()
{
// TODO: 在此编写 task_COND3_t 例程的内容
static int count = 1;
#ifdef LOOK_SEMIHOST
cout << "任务task_COND3_t开始:" << endl;
#endif
while (true) {
// TODO: 在此编写 task_COND3_t 例程的内容
mutex.lock();
#ifdef LOOK_SEMIHOST
cout << "任务task_COND3_t mutex.lock()" << endl;
#endif
while (sum < 2){
cond.wait(mutex);
#ifdef LOOK_SEMIHOST
cout << "任务task_COND3_t 收到cond.signal()" << endl;
#endif
mutex.unlock();
sum = 0;
break;
}
#if LOOK_H == 0
GPIOAs.DMASK.Regs = ~0b111100;
GPIOAs.DOUT.Regs = (count << 2) ^ 0b111100;
#else
GPIOA.DMASK(-1)
.DMASK5(0)
.DMASK4(0)
.DMASK3(0)
.DMASK2(0);
GPIOA.DOUT = (count << 2) ^ 0b111100;
#endif
if (++count >= 16) count = 1;
#ifdef LOOK_SEMIHOST
cout << "任务task_COND3_t 相应LED亮灭" << endl;
#endif
}
}
// 任务类 task_BEEP_t 的例程
void task_BEEP_t::routine()
{
// TODO: 在此编写 task_BEEP_t 例程的内容
#ifdef LOOK_SEMIHOST
cout << "任务task_BEEP_t开始:" << endl;
#endif
while (true) {
// TODO: 在此编写 task_BEEP_t 例程的内容
int 标志寄存器 = flag.wait(0b100, flag_t::ANY_CONSUME);
if (标志寄存器)
{
#ifdef LOOK_SEMIHOST
cout << "任务task_BEEP_t 标志寄存器=" << 标志寄存器 << endl;
#endif
for (uint32_t i = 0; i < 6; i ++){//蜂鸣器响三次
#if LOOK_H == 0
GPIOBs.DOUT.Bits.Pin10 ^= 1;//蜂鸣器响PB10=1,不响PB10=0
#else
GPIOB.DOUT().DOUT10 ^= 1;
#endif
delay(LOOK_TICKS_PER_SEC / 20);//断续间隔
}
#if LOOK_H == 0
GPIOBs.DOUT.Bits.Pin10 = 0;
#else
GPIOB.DOUT().DOUT10(0);
#endif
#ifdef LOOK_SEMIHOST
cout << "任务task_BEEP_t关闭蜂鸣器" << endl;
#endif
}
}
}
#ifdef LOOK_SCHEDULING_PRIORITY
instantiate::task<task_COND1_t, LOOK_STACK_SIZE> task_COND1(0);
instantiate::task<task_COND2_t, LOOK_STACK_SIZE> task_COND2(0);
instantiate::task<task_COND3_t, LOOK_STACK_SIZE> task_COND3(0);
instantiate::task<task_BEEP_t, LOOK_STACK_SIZE> task_BEEP(0);
#else
instantiate::task<task_COND1_t, LOOK_STACK_SIZE> task_COND1;
instantiate::task<task_COND2_t, LOOK_STACK_SIZE> task_COND2;
instantiate::task<task_COND3_t, LOOK_STACK_SIZE> task_COND3;
instantiate::task<task_BEEP_t, LOOK_STACK_SIZE> task_BEEP;
#endif
工程暂不传啦,大家自己先照着改试下
|