📄 misc.c
字号:
/*
===============================================================================
| COPYRIGHT (C) 2004-2006 Haishen Ruan, All rights reserved.
| SUMMARY:
| Miscellaneous functions.
|
|
| LICENSE INFORMATION
| RS-RTOS is free software; you can redistribute it and/or modify it under
| terms of the GNU General Public License as published by the
| Free Software Foundation; either version 2, or (at your option) any
| later version.
|
| RS-RTOS is distributed in the hope that it will be useful,
| but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
| General Public License for more details.
|
| You should have received a copy of the GNU General Public License
| along with RS-RTOS; see file COPYING. If not, write to the Free Software
| Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
| As a special exception, including RS-RTOS header files in a file,
| instantiating RS-RTOS generics or templates, or linking other files
| with RS-RTOS objects to produce an executable application, does not
| by itself cause the resulting executable application to be covered
| by the GNU General Public License. This exception does not
| however invalidate any other reasons why the executable file might be
| covered by the GNU Public License.
|
|
| DESCRIPTION:
| See http://www.RS-RTOS.org for documentation, latest information, license
| and contact details.
| email:ruanhaishen@gmail.com
=============================================================================*/
/*===========================================================================*/
#define _WIN32_WINNT 0x0500
#include "arch/arch.h"
#include "inc/const.h"
#include "inc/queue.h"
#include "inc/kernel.h"
#include "inc/memory.h"
#include "inc/ipc.h"
#include "inc/kapi.h"
typedef struct _struct_regs
{
u32 edi;
u32 esi;
u32 ebp;
u32 task_fore;
u32 task_entry;
u32 task_return;
u32 task_arg;
u16 task_rarg;
} regs_t;
#define TARGET_RESOLUTION 5 // 1-millisecond target resolution
extern vect_t simuIRQ;
simuFL_t simuFL;
HANDLE MainHandle; //Handle to main thread
static UINT wTimerRes;
static UINT nTimerID;
void simup_tick_int(void);
void power_off(void);
static void __declspec(naked) __task_fore(void)
{
irq_enable();
_asm { ret }
}
sp_t __stack_init(entry_t entry, arg_t arg, sp_t stack_base)
{
regs_t* regs;
regs = (regs_t*)((u32)stack_base - sizeof(regs_t));
regs->edi = (u32)0x00000000;
regs->esi = (u32)0x00000000;
regs->ebp = (u32)0x00000000;
regs->task_fore = (u32)&__task_fore;
regs->task_entry = (u32)entry;
#if CFG_TASK_DELETE_EN > 0 || CFG_DEBUG > 0
regs->task_return = (u32)task_delete;
#endif
regs->task_arg = (u32)arg;
regs->task_rarg = (u16)0xffff;
return (sp_t)regs;
}
/*================================================*/
void simu_irq(u8 irq)
{
DWORD* sp;
CONTEXT context;
context.ContextFlags = CONTEXT_CONTROL;
SuspendThread(MainHandle);
if(simuFL.b.IE && !simuFL.b.IF) {
simuFL.b.IF = 1;
GetThreadContext(MainHandle, &context);
sp = (DWORD*)context.Esp;
*--sp = context.Eip;
context.Esp = (DWORD)sp;
simuIRQ = irq;
if (simuIRQ == 0) {
context.Eip = (DWORD)__timer_handle;
} else {
context.Eip = (DWORD)__interrupt_handle;
}
SetThreadContext(MainHandle, &context);
}
ResumeThread(MainHandle);
}
void CALLBACK simu_timer_proc(UINT wTimerID, UINT msg,
DWORD dwUser, DWORD dw1, DWORD dw2)
{
static int counter = 0;
static vect_t i = 0;
counter++;
if (counter % (500 / RS_TICK_FREQ) == 1)
{
simu_irq(i++);
if (i > 100) i = 0;
}
else if (counter % (1000 / RS_TICK_FREQ) == 0)
{
simu_irq(0);
}
}
void simu_timer_init(void)
{
TIMECAPS tc;
/* Obtaining and setting timer resolution */
if (timeGetDevCaps(&tc, sizeof(TIMECAPS)) == TIMERR_NOERROR)
{
wTimerRes = min(max(tc.wPeriodMin, TARGET_RESOLUTION), tc.wPeriodMax);
timeBeginPeriod(wTimerRes);
}
/* Starting a single timer event */
nTimerID = timeSetEvent(
2, // delay
wTimerRes, // resolution (global variable)
simu_timer_proc, // callback function
(DWORD)0, // user data
TIME_PERIODIC); // single timer event
}
void simu_init(void)
{
HANDLE cp, ct;
simuFL.B = 0;
cp = GetCurrentProcess();
ct = GetCurrentThread();
DuplicateHandle(cp, ct, cp, &MainHandle, 0, FALSE, DUPLICATE_SAME_ACCESS); //伪句柄转换,得到线程真句柄
simu_timer_init();
}
void simu_destroy(void)
{
timeKillEvent(nTimerID);
timeEndPeriod(wTimerRes);
}
/*===========================================================================*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -