1.LOOK_Mutex.H
#include "look_config.h"
#include <look.h>
// 任务类 task_Look_Mutex_Led1_t 的定义
class task_Look_Mutex_Led1_t : public task_t {
public:
__INLINE__ task_Look_Mutex_Led1_t(uintptr_t prio); // 构造函数
protected:
void routine(); // 任务例程
};
// 任务类 task_Look_Mutex_Led1_t 的构造函数
__INLINE__ task_Look_Mutex_Led1_t::task_Look_Mutex_Led1_t(uintptr_t prio)
:task_t(prio)
{
// TODO: 在此初始化 task_Look_Mutex_Led1_t 的类成员
}
// 任务类 task_Look_Mutex_Led2_t 的定义
class task_Look_Mutex_Led2_t : public task_t {
public:
__INLINE__ task_Look_Mutex_Led2_t(uintptr_t prio); // 构造函数
protected:
void routine(); // 任务例程
};
// 任务类 task_Look_Mutex_Led1_t 的构造函数
__INLINE__ task_Look_Mutex_Led2_t::task_Look_Mutex_Led2_t(uintptr_t prio)
:task_t(prio)
{
// TODO: 在此初始化 task_Look_Mutex_Led1_t 的类成员
}
// 任务类 task_Look_Mutex_Beep_t 的定义
class task_Look_Mutex_Beep_t : public task_t {
public:
__INLINE__ task_Look_Mutex_Beep_t(uintptr_t prio); // 构造函数
protected:
void routine(); // 任务例程
};
// 任务类 task_Look_Mutex_Led1_t 的构造函数
__INLINE__ task_Look_Mutex_Beep_t::task_Look_Mutex_Beep_t(uintptr_t prio)
:task_t(prio)
{
// TODO: 在此初始化 task_Look_Mutex_Led1_t 的类成员
}
extern instantiate::task<task_Look_Mutex_Led1_t, LOOK_STACK_SIZE> task_Look_Mutex_Led1;
extern instantiate::task<task_Look_Mutex_Led2_t, LOOK_STACK_SIZE> task_Look_Mutex_Led2;
extern instantiate::task<task_Look_Mutex_Beep_t, LOOK_STACK_SIZE> task_Look_Mutex_Beep;
2.LOOK_Mutex.CPP
#include "Look_Mutex.h"
#define LOOK_H 1
#define LOOK_SEMIHOST
#if LOOK_H == 0
#include "NUC1xx.h"
#include "NUC1xxM051Seriescfg.h"
#else
#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
mutex_t mutex;
mbox_t<int> mbox(0);
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)
{
uint32_t key;
#if LOOK_H == 0
key = GPIOBs.PIN.Regs;
#else
key = GPIOB.PIN;
#endif
if ((vector == EINT0_IRQn) && ((key & (1 << 14)) == 0))//Key2
{
#ifdef LOOK_SEMIHOST
cout << "中断Keyboard_t按下Key2键" << endl;
#endif
mbox.do_tryput(6);
}
else if ((vector == EINT1_IRQn) && ((key & (1 << 15)) == 0))//Key1
{
#ifdef LOOK_SEMIHOST
cout << "中断Keyboard_t按下Key1键" << endl;
#endif
mbox.do_tryput(2);
}
else
{
#ifdef LOOK_SEMIHOST
cout << "中断Keyboard_t键盘发生抖动!!!" << endl;
#endif
}
}
Keyboard_t Key; // 创建Key对象
// 任务类 task_Look_Mutex_Led1_t 的例程
void task_Look_Mutex_Led1_t::routine()
{
#ifdef LOOK_SEMIHOST
cout << "任务task_Look_Mutex_Led1_t开始:" << endl;
#endif
// TODO: 在此编写 task_Look_Mutex_Led1_t 例程的内容
while (true) {
// TODO: 在此编写 task_Look_Mutex_Led1_t 例程的内容
int Mutex = mutex.lock();
if (Mutex)
{
#ifdef LOOK_SEMIHOST
cout << "任务task_Look_Mutex_Led1_t保护开始" << endl;
#endif
delay(LOOK_TICKS_PER_SEC / 10);
uint32_t data = ~0b100;
#if LOOK_H == 0
GPIOAs.DMASK.Regs = ~0b111100;
#else
GPIOA.DMASK(-1)
.DMASK5(0)
.DMASK4(0)
.DMASK3(0)
.DMASK2(0);
#endif
for (uint32_t i = 0; i < 18; i ++){
data &= 0b111100;
#if LOOK_H == 0
GPIOAs.DOUT.Regs = data;
#else
GPIOA.DOUT = data;
#endif
data <<= 1;
data += data >> 4;
delay(LOOK_TICKS_PER_SEC / 10);
}
#if LOOK_H == 0
GPIOAs.DOUT.Regs = 0b111100;
#else
GPIOA.DOUT(0)
.DOUT5(1)
.DOUT4(1)
.DOUT3(1)
.DOUT2(1);
#endif
mutex.unlock();
#ifdef LOOK_SEMIHOST
cout << "任务task_Look_Mutex_Led1_t保护结束" << endl;
#endif
}
// delay(LOOK_TICKS_PER_SEC / 100);
}
}
// 任务类 task_Look_Mutex_Led2_t 的例程
void task_Look_Mutex_Led2_t::routine()
{
#ifdef LOOK_SEMIHOST
cout << "任务task_Look_Mutex_Led2_t开始:" << endl;
#endif
// TODO: 在此编写 task_Look_Mutex_Led2_t 例程的内容
while (true) {
// TODO: 在此编写 task_Look_Mutex_Led2_t 例程的内容
int Mutex = mutex.lock();
if (Mutex)
{
#ifdef LOOK_SEMIHOST
cout << "任务task_Look_Mutex_Led2_t保护开始" << endl;
#endif
delay(LOOK_TICKS_PER_SEC / 10);
uint32_t data = ~0b100000;
#if LOOK_H == 0
GPIOAs.DMASK.Regs = ~0b111100;
#else
GPIOA.DMASK(-1)
.DMASK5(0)
.DMASK4(0)
.DMASK3(0)
.DMASK2(0);
#endif
for (uint32_t i = 0; i < 18; i ++){
data &= 0b111100;
#if LOOK_H == 0
GPIOAs.DOUT.Regs = data;
#else
GPIOA.DOUT = data;
#endif
data >>= 1;
data += data << 4;
delay(LOOK_TICKS_PER_SEC / 10);
}
#if LOOK_H == 0
GPIOAs.DOUT.Regs = 0b111100;
#else
GPIOA.DOUT(0)
.DOUT5(1)
.DOUT4(1)
.DOUT3(1)
.DOUT2(1);
#endif
mutex.unlock();
#ifdef LOOK_SEMIHOST
cout << "任务task_Look_Mutex_Led2_t保护结束" << endl;
#endif
}
// delay(LOOK_TICKS_PER_SEC / 100);
}
}
// 任务类 task_Look_Mutex_Beep_t 的例程
void task_Look_Mutex_Beep_t::routine()
{
#ifdef LOOK_SEMIHOST
cout << "任务task_Look_Mutex_Beep_t开始:" << endl;
#endif
// TODO: 在此编写 task_Look_Mutex_Beep_t 例程的内容
while (true) {
// TODO: 在此编写 task_Look_Mutex_Beep_t 例程的内容
uint32_t msg = mbox.get();//取出响铃次数
if (msg)
{
/*
#ifdef LOOK_SEMIHOST
cout << "任务task_Look_Mutex_Beep_t mbox=" << msg << endl;
#endif
*/
#ifdef LOOK_SEMIHOST
if (msg == 2)
{
cout << "执行Key1键盘命令" << endl;
}
else if (msg == 2)
{
cout << "执行Key2键盘命令" << endl;
}
#endif
#if LOOK_H == 0
GPIOBs.DMASK.Bits.Pin10 = 0;
#else
GPIOB.DMASK().DMASK10(0);
#endif
for (uint32_t i = 0; i < msg; i ++){//蜂鸣器响X次
#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
delay(LOOK_TICKS_PER_SEC / 50);
}
}
}
#ifdef LOOK_SCHEDULING_PRIORITY
instantiate::task<task_Look_Mutex_Led1_t, LOOK_STACK_SIZE> task_Look_Mutex_Led1(0);
instantiate::task<task_Look_Mutex_Led2_t, LOOK_STACK_SIZE> task_Look_Mutex_Led2(0);
instantiate::task<task_Look_Mutex_Beep_t, LOOK_STACK_SIZE> task_Look_Mutex_Beep(0);
#else
instantiate::task<task_Look_Mutex_Led1_t, LOOK_STACK_SIZE> task_Look_Mutex_Led1;
instantiate::task<task_Look_Mutex_Led2_t, LOOK_STACK_SIZE> task_Look_Mutex_Led2;
instantiate::task<task_Look_Mutex_Beep_t, LOOK_STACK_SIZE> task_Look_Mutex_Beep;
#endif
3.贴图
4.LOOK_Mutex.RAR
Look_Mutex.rar
(1.6 MB)
|