原始版本
This commit is contained in:
15
RT_Thread/libcpu/sim/SConscript
Normal file
15
RT_Thread/libcpu/sim/SConscript
Normal file
@ -0,0 +1,15 @@
|
||||
# RT-Thread building script for bridge
|
||||
|
||||
import os
|
||||
from building import *
|
||||
|
||||
Import('rtconfig')
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
group = []
|
||||
list = os.listdir(cwd)
|
||||
|
||||
# cpu porting code files
|
||||
group = group + SConscript(os.path.join(rtconfig.CPU, 'SConscript'))
|
||||
|
||||
Return('group')
|
||||
13
RT_Thread/libcpu/sim/posix/SConscript
Normal file
13
RT_Thread/libcpu/sim/posix/SConscript
Normal file
@ -0,0 +1,13 @@
|
||||
# RT-Thread building script for component
|
||||
|
||||
from building import *
|
||||
|
||||
Import('rtconfig')
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
src = Glob('*.c') + Glob('*.cpp') + Glob('*_gcc.S')
|
||||
CPPPATH = [cwd]
|
||||
|
||||
group = DefineGroup('libcpu', src, depend = [''], CPPPATH = CPPPATH)
|
||||
|
||||
Return('group')
|
||||
563
RT_Thread/libcpu/sim/posix/cpu_port.c
Normal file
563
RT_Thread/libcpu/sim/posix/cpu_port.c
Normal file
@ -0,0 +1,563 @@
|
||||
/*
|
||||
* author : prife (goprife@gmail.com)
|
||||
* date : 2013/01/14 01:18:50
|
||||
* version: v 0.2.0
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include <semaphore.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
//#define TRACE printf
|
||||
#define TRACE(...)
|
||||
|
||||
typedef struct _thread
|
||||
{
|
||||
pthread_t pthread;
|
||||
void (*task)(void *);
|
||||
void *para;
|
||||
void (*exit)(void);
|
||||
sem_t sem;
|
||||
rt_thread_t rtthread;
|
||||
int status;
|
||||
void *data;
|
||||
} thread_t;
|
||||
|
||||
#define THREAD_T(thread) ((thread_t *)thread)
|
||||
|
||||
#define MSG_SUSPEND SIGUSR1 /* 10 */
|
||||
#define MSG_RESUME SIGUSR2
|
||||
#define MSG_TICK SIGALRM /* 14 */
|
||||
#define TIMER_TYPE ITIMER_REAL
|
||||
#define MAX_INTERRUPT_NUM ((unsigned int)sizeof(unsigned int) * 8)
|
||||
|
||||
#define INTERRUPT_ENABLE 0
|
||||
#define INTERRUPT_DISABLE 1
|
||||
|
||||
/* 线程挂起状态,共两种取值 */
|
||||
#define SUSPEND_LOCK 0
|
||||
#define SUSPEND_SIGWAIT 1
|
||||
#define THREAD_RUNNING 2
|
||||
|
||||
/* interrupt flag, if 1, disable, if 0, enable */
|
||||
static long interrupt_disable_flag;
|
||||
//static int systick_signal_flag;
|
||||
|
||||
/* flag in interrupt handling */
|
||||
rt_ubase_t rt_interrupt_from_thread, rt_interrupt_to_thread;
|
||||
rt_ubase_t rt_thread_switch_interrupt_flag;
|
||||
|
||||
/* interrupt event mutex */
|
||||
static pthread_mutex_t *ptr_int_mutex;
|
||||
static pthread_cond_t cond_int_hit; /* interrupt occured! */
|
||||
static volatile unsigned int cpu_pending_interrupts;
|
||||
static int (* cpu_isr_table[MAX_INTERRUPT_NUM])(void) = {0};
|
||||
|
||||
static pthread_t mainthread_pid;
|
||||
|
||||
/* function definition */
|
||||
static void start_sys_timer(void);
|
||||
static int tick_interrupt_isr(void);
|
||||
static void mthread_signal_tick(int sig);
|
||||
static int mainthread_scheduler(void);
|
||||
|
||||
int signal_install(int sig, void (*func)(int))
|
||||
{
|
||||
struct sigaction act;
|
||||
|
||||
/* set the signal handler */
|
||||
act.sa_handler = func ;
|
||||
sigemptyset(&act.sa_mask);
|
||||
act.sa_flags = 0;
|
||||
sigaction(sig, &act, 0);
|
||||
}
|
||||
|
||||
int signal_mask(void)
|
||||
{
|
||||
sigset_t sigmask, oldmask;
|
||||
|
||||
/* set signal mask */
|
||||
sigemptyset(&sigmask);
|
||||
sigaddset(&sigmask, SIGALRM);
|
||||
pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
|
||||
}
|
||||
static void thread_suspend_signal_handler(int sig)
|
||||
{
|
||||
sigset_t sigmask;
|
||||
pthread_t pid = pthread_self();
|
||||
thread_t *thread_from;
|
||||
thread_t *thread_to;
|
||||
rt_thread_t tid;
|
||||
|
||||
if (sig != MSG_SUSPEND)
|
||||
{
|
||||
printf("get an unexpected signal <%d>, exit\n", sig);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
thread_from = (thread_t *) rt_interrupt_from_thread;
|
||||
thread_to = (thread_t *) rt_interrupt_to_thread;
|
||||
|
||||
/* 注意!此时 rt_thread_self的值是to线程的值! */
|
||||
tid = rt_thread_self();
|
||||
/* FIXME RT_ASSERT(thread_from->pthread == pid); */
|
||||
RT_ASSERT((thread_t *)(tid->sp) == thread_to);
|
||||
|
||||
TRACE("signal: SIGSUSPEND suspend <%s>\n", thread_from->rtthread->name);
|
||||
|
||||
/* 使用sigwait或者sigsuspend来挂起from线程 */
|
||||
//sem_wait(&thread_from->sem);
|
||||
sigemptyset(&sigmask);
|
||||
sigaddset(&sigmask, MSG_RESUME);
|
||||
|
||||
/* Beginnig Linux Programming上说,当信号处理函数运行中,此信号就会被屏蔽,
|
||||
* 以防止重复执行信号处理函数
|
||||
*/
|
||||
thread_from->status = SUSPEND_SIGWAIT;
|
||||
if (sigwait(&sigmask, &sig) != 0)
|
||||
{
|
||||
printf("sigwait faild, %d\n", sig);
|
||||
}
|
||||
thread_to = (thread_t *) rt_interrupt_to_thread;
|
||||
RT_ASSERT(thread_to == thread_from);
|
||||
thread_to->status = THREAD_RUNNING;
|
||||
TRACE("signal: SIGSUSPEND resume <%s>\n", thread_from->rtthread->name);
|
||||
}
|
||||
|
||||
static void thread_resume_signal_handler(int sig)
|
||||
{
|
||||
sigset_t sigmask;
|
||||
pthread_t pid = pthread_self();
|
||||
thread_t *thread_from;
|
||||
thread_t *thread_to;
|
||||
rt_thread_t tid;
|
||||
|
||||
thread_from = (thread_t *) rt_interrupt_from_thread;
|
||||
thread_to = (thread_t *) rt_interrupt_to_thread;
|
||||
|
||||
/* 注意!此时 rt_thread_self的值是to线程的值! */
|
||||
tid = rt_thread_self();
|
||||
RT_ASSERT((thread_t *)(tid->sp) == thread_to);
|
||||
|
||||
TRACE("signal: SIGRESUME resume <%s>\n", thread_to->rtthread->name);
|
||||
}
|
||||
|
||||
static void *thread_run(void *parameter)
|
||||
{
|
||||
rt_thread_t tid;
|
||||
thread_t *thread;
|
||||
thread = THREAD_T(parameter);
|
||||
int res;
|
||||
|
||||
/* set signal mask, mask the timer! */
|
||||
signal_mask();
|
||||
|
||||
thread->status = SUSPEND_LOCK;
|
||||
TRACE("pid <%08x> stop on sem...\n", (unsigned int)(thread->pthread));
|
||||
sem_wait(&thread->sem);
|
||||
|
||||
tid = rt_thread_self();
|
||||
TRACE("pid <%08x> tid <%s> starts...\n", (unsigned int)(thread->pthread),
|
||||
tid->parent.name);
|
||||
thread->rtthread = tid;
|
||||
thread->task(thread->para);
|
||||
TRACE("pid <%08x> tid <%s> exit...\n", (unsigned int)(thread->pthread),
|
||||
tid->parent.name);
|
||||
thread->exit();
|
||||
|
||||
/*TODO:
|
||||
* 最后一行的pthread_exit永远没有机会执行,这是因为在threead->exit函数中
|
||||
* 会发生线程切换,并永久将此pthread线程挂起,所以更完美的解决方案是在这
|
||||
* 里发送信号给主线程,主线程中再次唤醒此线程令其自动退出。
|
||||
*/
|
||||
//sem_destroy(&thread->sem);
|
||||
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
static int thread_create(
|
||||
thread_t *thread, void *task, void *parameter, void *pexit)
|
||||
{
|
||||
int res;
|
||||
pthread_attr_t attr;
|
||||
|
||||
thread->task = task;
|
||||
thread->para = parameter;
|
||||
thread->exit = pexit;
|
||||
|
||||
if (sem_init(&thread->sem, 0, 0) != 0)
|
||||
{
|
||||
printf("init thread->sem failed, exit \n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
/* No need to join the threads. */
|
||||
pthread_attr_init(&attr);
|
||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
||||
|
||||
/* create a posix thread */
|
||||
res = pthread_create(&thread->pthread, &attr, &thread_run, (void *)thread);
|
||||
if (res)
|
||||
{
|
||||
printf("pthread create faild, <%d>\n", res);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* resume the thread */
|
||||
static int thread_resume(thread_t *thread)
|
||||
{
|
||||
sem_post(& thread->sem);
|
||||
}
|
||||
|
||||
|
||||
rt_uint8_t *rt_hw_stack_init(
|
||||
void *pEntry,
|
||||
void *pParam,
|
||||
rt_uint8_t *pStackAddr,
|
||||
void *pExit)
|
||||
{
|
||||
thread_t *thread;
|
||||
|
||||
thread = (thread_t *)(pStackAddr - sizeof(thread_t));
|
||||
|
||||
/* set the filed to zero */
|
||||
memset(thread, 0x00, sizeof(thread_t));
|
||||
|
||||
thread_create(thread, pEntry, pParam, pExit);
|
||||
//TRACE("thread %x created\n", (unsigned int)thread_table[t].pthread);
|
||||
|
||||
return (rt_uint8_t *) thread;
|
||||
}
|
||||
|
||||
rt_base_t rt_hw_interrupt_disable(void)
|
||||
{
|
||||
long back;
|
||||
|
||||
if (ptr_int_mutex == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
pthread_mutex_lock(ptr_int_mutex);
|
||||
back = interrupt_disable_flag;
|
||||
interrupt_disable_flag = INTERRUPT_DISABLE;
|
||||
|
||||
/*TODO: It may need to unmask the signal */
|
||||
return back;
|
||||
}
|
||||
|
||||
void rt_hw_interrupt_enable(rt_base_t level)
|
||||
{
|
||||
struct rt_thread * tid;
|
||||
pthread_t pid;
|
||||
thread_t *thread_from;
|
||||
thread_t *thread_to;
|
||||
|
||||
if (ptr_int_mutex == NULL)
|
||||
return;
|
||||
|
||||
interrupt_disable_flag = level;
|
||||
|
||||
pthread_mutex_unlock(ptr_int_mutex);
|
||||
/* 如果已经中断仍然关闭 */
|
||||
if (interrupt_disable_flag)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/* 表示当前中断打开, 检查是否有挂起的中断 */
|
||||
pthread_mutex_lock(ptr_int_mutex);
|
||||
if (!cpu_pending_interrupts)
|
||||
{
|
||||
pthread_mutex_unlock(ptr_int_mutex);
|
||||
return;
|
||||
}
|
||||
|
||||
thread_from = (thread_t *) rt_interrupt_from_thread;
|
||||
thread_to = (thread_t *) rt_interrupt_to_thread;
|
||||
tid = rt_thread_self();
|
||||
pid = pthread_self();
|
||||
|
||||
//pid != mainthread_pid &&
|
||||
if (thread_from->pthread == pid)
|
||||
{
|
||||
/* 注意这段代码是在RTT普通线程函数总函数中执行的,
|
||||
* from线程就是当前rtt线程 */
|
||||
/* 需要检查是否有挂起的中断需要处理 */
|
||||
TRACE("conswitch: P in pid<%x> ,suspend <%s>, resume <%s>!\n",
|
||||
(unsigned int)pid,
|
||||
thread_from->rtthread->name,
|
||||
thread_to->rtthread->name);
|
||||
|
||||
cpu_pending_interrupts --;
|
||||
thread_from->status = SUSPEND_LOCK;
|
||||
pthread_mutex_unlock(ptr_int_mutex);
|
||||
/* 唤醒被挂起的线程 */
|
||||
if (thread_to->status == SUSPEND_SIGWAIT)
|
||||
{
|
||||
pthread_kill(thread_to->pthread, MSG_RESUME);
|
||||
}
|
||||
else if (thread_to->status == SUSPEND_LOCK)
|
||||
{
|
||||
sem_post(& thread_to->sem);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("conswitch: should not be here! %d\n", __LINE__);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* 挂起当前的线程 */
|
||||
sem_wait(& thread_from->sem);
|
||||
pthread_mutex_lock(ptr_int_mutex);
|
||||
thread_from->status = THREAD_RUNNING;
|
||||
pthread_mutex_unlock(ptr_int_mutex);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* 注意这段代码可能在多种情况下运行:
|
||||
* 1. 在system tick中执行, 即主线程的SIGALRM信号处理函数中执行
|
||||
* 2. 其他线程中调用,比如用于获取按键输入的线程中调用
|
||||
*/
|
||||
TRACE("conswitch: S in pid<%x> ,suspend <%s>, resume <%s>!\n",
|
||||
(unsigned int)pid,
|
||||
thread_from->rtthread->name,
|
||||
thread_to->rtthread->name);
|
||||
|
||||
cpu_pending_interrupts --;
|
||||
|
||||
/* 需要把解锁函数放在前面,以防止死锁?? */
|
||||
pthread_mutex_unlock(ptr_int_mutex);
|
||||
|
||||
/* 挂起from线程 */
|
||||
pthread_kill(thread_from->pthread, MSG_SUSPEND);
|
||||
/* 注意:这里需要确保线程被挂起了, 否则312行就很可能就会报错退出
|
||||
* 因为这里挂起线程是通过信号实现的,所以一定要确保线程挂起才行 */
|
||||
while (thread_from->status != SUSPEND_SIGWAIT)
|
||||
{
|
||||
sched_yield();
|
||||
}
|
||||
|
||||
/* 唤醒to线程 */
|
||||
if (thread_to->status == SUSPEND_SIGWAIT)
|
||||
{
|
||||
pthread_kill(thread_to->pthread, MSG_RESUME);
|
||||
}
|
||||
else if (thread_to->status == SUSPEND_LOCK)
|
||||
{
|
||||
sem_post(& thread_to->sem);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("conswitch: should not be here! %d\n", __LINE__);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
}
|
||||
/*TODO: It may need to unmask the signal */
|
||||
}
|
||||
|
||||
void rt_hw_context_switch(rt_ubase_t from,
|
||||
rt_ubase_t to)
|
||||
{
|
||||
struct rt_thread * tid;
|
||||
pthread_t pid;
|
||||
thread_t *thread_from;
|
||||
thread_t *thread_to;
|
||||
|
||||
RT_ASSERT(from != to);
|
||||
|
||||
#if 0
|
||||
//TODO: 可能还需要考虑嵌套切换的情况
|
||||
if (rt_thread_switch_interrupt_flag != 1)
|
||||
{
|
||||
rt_thread_switch_interrupt_flag = 1;
|
||||
|
||||
// set rt_interrupt_from_thread
|
||||
rt_interrupt_from_thread = *((rt_ubase_t *)from);
|
||||
}
|
||||
#endif
|
||||
pthread_mutex_lock(ptr_int_mutex);
|
||||
rt_interrupt_from_thread = *((rt_ubase_t *)from);
|
||||
rt_interrupt_to_thread = *((rt_ubase_t *)to);
|
||||
|
||||
/* 这个函数只是并不会真正执行中断处理函数,而只是简单的
|
||||
* 设置一下中断挂起标志位
|
||||
*/
|
||||
cpu_pending_interrupts ++;
|
||||
pthread_mutex_unlock(ptr_int_mutex);
|
||||
}
|
||||
|
||||
void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to, rt_thread_t from_thread, rt_thread_t to_thread)
|
||||
{
|
||||
rt_hw_context_switch(from, to);
|
||||
}
|
||||
|
||||
void rt_hw_context_switch_to(rt_ubase_t to)
|
||||
{
|
||||
//set to thread
|
||||
rt_interrupt_to_thread = *((rt_ubase_t *)(to));
|
||||
|
||||
//clear from thread
|
||||
rt_interrupt_from_thread = 0;
|
||||
|
||||
//set interrupt to 1
|
||||
rt_thread_switch_interrupt_flag = 0; //TODO: 还需要考虑这个嵌套切换的情况
|
||||
|
||||
/* enable interrupt
|
||||
* note: NOW, there are only one interrupt in simposix: system tick */
|
||||
rt_hw_interrupt_enable(0);
|
||||
|
||||
//start the main thread scheduler
|
||||
mainthread_scheduler();
|
||||
|
||||
//never reach here!
|
||||
return;
|
||||
}
|
||||
|
||||
static int mainthread_scheduler(void)
|
||||
{
|
||||
int i, res, sig;
|
||||
thread_t *thread_from;
|
||||
thread_t *thread_to;
|
||||
pthread_mutex_t mutex;
|
||||
pthread_mutexattr_t mutexattr;
|
||||
sigset_t sigmask, oldmask;
|
||||
|
||||
/* save the main thread id */
|
||||
mainthread_pid = pthread_self();
|
||||
TRACE("pid <%08x> mainthread\n", (unsigned int)(mainthread_pid));
|
||||
|
||||
/* 屏蔽suspend信号和resume信号 */
|
||||
sigemptyset(&sigmask);
|
||||
sigaddset(&sigmask, MSG_SUSPEND);
|
||||
sigaddset(&sigmask, MSG_RESUME);
|
||||
pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
|
||||
|
||||
sigemptyset(&sigmask);
|
||||
sigaddset(&sigmask, SIGALRM);
|
||||
|
||||
/* install signal handler of system tick */
|
||||
signal_install(SIGALRM, mthread_signal_tick);
|
||||
/* install signal handler used to suspend/resume threads */
|
||||
signal_install(MSG_SUSPEND, thread_suspend_signal_handler);
|
||||
signal_install(MSG_RESUME, thread_resume_signal_handler);
|
||||
|
||||
/* create a mutex and condition val, used to indicate interrupts occrue */
|
||||
ptr_int_mutex = &mutex;
|
||||
pthread_mutexattr_init(&mutexattr);
|
||||
pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE_NP);
|
||||
pthread_mutex_init(ptr_int_mutex, &mutexattr);
|
||||
|
||||
/* start timer */
|
||||
start_sys_timer();
|
||||
|
||||
thread_to = (thread_t *) rt_interrupt_to_thread;
|
||||
thread_resume(thread_to);
|
||||
for (;;)
|
||||
{
|
||||
#if 1
|
||||
if (sigwait(&sigmask, &sig) != 0)
|
||||
{
|
||||
printf("mthread: sigwait get unexpected sig %d\n", sig);
|
||||
}
|
||||
#else
|
||||
pause();
|
||||
#endif
|
||||
TRACE("mthread:got sig %d\n", sig);
|
||||
/* signal mask sigalrm 屏蔽SIGALRM信号 */
|
||||
pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
|
||||
|
||||
// if (systick_signal_flag != 0)
|
||||
if (pthread_mutex_trylock(ptr_int_mutex) == 0)
|
||||
{
|
||||
tick_interrupt_isr();
|
||||
// systick_signal_flag = 0;
|
||||
pthread_mutex_unlock(ptr_int_mutex);
|
||||
}
|
||||
else
|
||||
{
|
||||
TRACE("try lock failed.\n");
|
||||
}
|
||||
|
||||
/* 开启SIGALRM信号 */
|
||||
pthread_sigmask(SIG_UNBLOCK, &sigmask, &oldmask);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Setup the systick timer to generate the tick interrupts at the required
|
||||
* frequency.
|
||||
*/
|
||||
static void start_sys_timer(void)
|
||||
{
|
||||
struct itimerval itimer, oitimer;
|
||||
int us;
|
||||
|
||||
us = 1000000 / RT_TICK_PER_SECOND - 1;
|
||||
|
||||
TRACE("start system tick!\n");
|
||||
/* Initialise the structure with the current timer information. */
|
||||
if (0 != getitimer(TIMER_TYPE, &itimer))
|
||||
{
|
||||
TRACE("get timer failed.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* Set the interval between timer events. */
|
||||
itimer.it_interval.tv_sec = 0;
|
||||
itimer.it_interval.tv_usec = us;
|
||||
/* Set the current count-down. */
|
||||
itimer.it_value.tv_sec = 0;
|
||||
itimer.it_value.tv_usec = us;
|
||||
|
||||
/* Set-up the timer interrupt. */
|
||||
if (0 != setitimer(TIMER_TYPE, &itimer, &oitimer))
|
||||
{
|
||||
TRACE("set timer failed.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
static void mthread_signal_tick(int sig)
|
||||
{
|
||||
int res;
|
||||
pthread_t pid = pthread_self();
|
||||
|
||||
if (sig == SIGALRM)
|
||||
{
|
||||
TRACE("pid <%x> signal: SIGALRM enter!\n", (unsigned int)pid);
|
||||
//systick_signal_flag = 1;
|
||||
TRACE("pid <%x> signal: SIGALRM leave!\n", (unsigned int)pid);
|
||||
}
|
||||
else
|
||||
{
|
||||
TRACE("got an unexpected signal <%d>\n", sig);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
/* isr return value: 1, should not be masked, if 0, can be masked */
|
||||
static int tick_interrupt_isr(void)
|
||||
{
|
||||
TRACE("isr: systick enter!\n");
|
||||
/* enter interrupt */
|
||||
rt_interrupt_enter();
|
||||
|
||||
rt_tick_increase();
|
||||
|
||||
/* leave interrupt */
|
||||
rt_interrupt_leave();
|
||||
|
||||
TRACE("isr: systick leave!\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
13
RT_Thread/libcpu/sim/posix/startup.c
Normal file
13
RT_Thread/libcpu/sim/posix/startup.c
Normal file
@ -0,0 +1,13 @@
|
||||
#include <rtthread.h>
|
||||
|
||||
#if defined(__GNUC__)
|
||||
|
||||
int rtthread_startup(void);
|
||||
|
||||
static int start(void)
|
||||
{
|
||||
rtthread_startup();
|
||||
return 0;
|
||||
}
|
||||
__attribute__((section(".init_array"))) typeof(start) *__init = start;
|
||||
#endif
|
||||
13
RT_Thread/libcpu/sim/win32/SConscript
Normal file
13
RT_Thread/libcpu/sim/win32/SConscript
Normal file
@ -0,0 +1,13 @@
|
||||
# RT-Thread building script for component
|
||||
|
||||
from building import *
|
||||
|
||||
Import('rtconfig')
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
src = Glob('*.c') + Glob('*.cpp')
|
||||
CPPPATH = [cwd]
|
||||
|
||||
group = DefineGroup('libcpu', src, depend = [''], CPPPATH = CPPPATH)
|
||||
|
||||
Return('group')
|
||||
700
RT_Thread/libcpu/sim/win32/cpu_port.c
Normal file
700
RT_Thread/libcpu/sim/win32/cpu_port.c
Normal file
@ -0,0 +1,700 @@
|
||||
/*
|
||||
************************************************************************************************************************
|
||||
* File : cpu_port.c
|
||||
* By : xyou
|
||||
* Version : V1.00.00
|
||||
*
|
||||
* By : prife
|
||||
* Version : V1.00.01
|
||||
************************************************************************************************************************
|
||||
*/
|
||||
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
* INCLUDE FILES
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include <windows.h>
|
||||
#include <mmsystem.h>
|
||||
#include <stdio.h>
|
||||
#include "cpu_port.h"
|
||||
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
* WinThread STRUCTURE
|
||||
* Windows runs each task in a thread.
|
||||
* The context switch is managed by the threads.So the task stack does not have to be managed directly,
|
||||
* although the stack stack is still used to hold an WinThreadState structure this is the only thing it
|
||||
* will be ever hold.
|
||||
* YieldEvent used to make sure the thread does not execute before asynchronous SuspendThread() operation
|
||||
* actually being performed.
|
||||
* the structure indirectly maps the task handle to a thread handle
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
void *Param; //Thread param
|
||||
void (*Entry)(void *); //Thread entry
|
||||
void (*Exit)(void); //Thread exit
|
||||
HANDLE YieldEvent;
|
||||
HANDLE ThreadHandle;
|
||||
DWORD ThreadID;
|
||||
}win_thread_t;
|
||||
|
||||
const DWORD MS_VC_EXCEPTION=0x406D1388;
|
||||
|
||||
#pragma pack(push,8)
|
||||
typedef struct tagTHREADNAME_INFO
|
||||
{
|
||||
DWORD dwType; // Must be 0x1000.
|
||||
LPCSTR szName; // Pointer to name (in user addr space).
|
||||
DWORD dwThreadID; // Thread ID (-1=caller thread).
|
||||
DWORD dwFlags; // Reserved for future use, must be zero.
|
||||
} THREADNAME_INFO;
|
||||
#pragma pack(pop)
|
||||
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
* LOCAL DEFINES
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
#define MAX_INTERRUPT_NUM ((rt_uint32_t)sizeof(rt_uint32_t) * 8)
|
||||
|
||||
/*
|
||||
* Simulated interrupt waiting to be processed.this is a bit mask where each bit represent one interrupt
|
||||
* so a maximum of 32 interrupts can be simulated
|
||||
*/
|
||||
static volatile rt_uint32_t CpuPendingInterrupts = 0;
|
||||
|
||||
/*
|
||||
* An event used to inform the simulated interrupt processing thread (a high priority thread
|
||||
* that simulated interrupt processing) that an interrupt is pending
|
||||
*/
|
||||
static HANDLE hInterruptEventHandle = NULL;
|
||||
|
||||
/*
|
||||
* Mutex used to protect all the simulated interrupt variables that are accessed by multiple threads
|
||||
*/
|
||||
static HANDLE hInterruptEventMutex = NULL;
|
||||
|
||||
/*
|
||||
* Handler for all the simulate software interrupts.
|
||||
* The first two positions are used the Yield and Tick interrupt so are handled slightly differently
|
||||
* all the other interrupts can be user defined
|
||||
*/
|
||||
static rt_uint32_t (*CpuIsrHandler[MAX_INTERRUPT_NUM])(void) = {0};
|
||||
|
||||
/*
|
||||
* Handler for OSTick Thread
|
||||
*/
|
||||
static HANDLE OSTick_Thread;
|
||||
static DWORD OSTick_ThreadID;
|
||||
static HANDLE OSTick_SignalPtr;
|
||||
static TIMECAPS OSTick_TimerCap;
|
||||
static MMRESULT OSTick_TimerID;
|
||||
|
||||
/*
|
||||
* flag in interrupt handling
|
||||
*/
|
||||
volatile rt_ubase_t rt_interrupt_from_thread = 0;
|
||||
volatile rt_ubase_t rt_interrupt_to_thread = 0;
|
||||
volatile rt_uint32_t rt_thread_switch_interrupt_flag = 0;
|
||||
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
* PRIVATE FUNCTION PROTOTYPES
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
//static void WinThreadScheduler(void);
|
||||
void WinThreadScheduler(void);
|
||||
rt_uint32_t YieldInterruptHandle(void);
|
||||
rt_uint32_t SysTickInterruptHandle(void);
|
||||
static DWORD WINAPI ThreadforSysTickTimer(LPVOID lpParam);
|
||||
static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam);
|
||||
|
||||
static void SetThreadName(DWORD dwThreadID, char* threadName)
|
||||
{
|
||||
#if defined(_MSC_VER)
|
||||
THREADNAME_INFO info;
|
||||
info.dwType = 0x1000;
|
||||
info.szName = threadName;
|
||||
info.dwThreadID = dwThreadID;
|
||||
info.dwFlags = 0;
|
||||
|
||||
__try
|
||||
{
|
||||
RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info );
|
||||
}
|
||||
__except(EXCEPTION_EXECUTE_HANDLER)
|
||||
{
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
* rt_hw_stack_init()
|
||||
* Description : Initialize stack of thread
|
||||
* Argument(s) : void *pvEntry,void *pvParam,rt_uint8_t *pStackAddr,void *pvExit
|
||||
* Return(s) : rt_uint8_t*
|
||||
* Caller(s) : rt_thread_init or rt_thread_create
|
||||
* Note(s) : none
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
|
||||
static DWORD WINAPI thread_run( LPVOID lpThreadParameter )
|
||||
{
|
||||
rt_thread_t tid = rt_thread_self();
|
||||
win_thread_t *pWinThread = (win_thread_t *)lpThreadParameter;
|
||||
|
||||
SetThreadName(GetCurrentThreadId(), tid->parent.name);
|
||||
|
||||
pWinThread->Entry(pWinThread->Param);
|
||||
|
||||
pWinThread->Exit();
|
||||
return 0;
|
||||
}
|
||||
|
||||
rt_uint8_t* rt_hw_stack_init(void *pEntry,void *pParam,rt_uint8_t *pStackAddr,void *pExit)
|
||||
{
|
||||
win_thread_t *pWinThread = NULL;
|
||||
|
||||
/*
|
||||
* In this simulated case a stack is not initialized
|
||||
* The thread handles the context switching itself. The WinThreadState object is placed onto the stack
|
||||
* that was created for the task
|
||||
* so the stack buffer is still used,just not in the conventional way.
|
||||
*/
|
||||
pWinThread = (win_thread_t *)(pStackAddr - sizeof(win_thread_t));
|
||||
|
||||
pWinThread->Entry = pEntry;
|
||||
pWinThread->Param = pParam;
|
||||
pWinThread->Exit = pExit;
|
||||
|
||||
pWinThread->ThreadHandle = NULL;
|
||||
pWinThread->ThreadID = 0;
|
||||
|
||||
pWinThread->YieldEvent = CreateEvent(NULL,
|
||||
FALSE,
|
||||
FALSE,
|
||||
NULL);
|
||||
|
||||
/* Create the winthread */
|
||||
pWinThread->ThreadHandle = CreateThread(NULL,
|
||||
0,
|
||||
(LPTHREAD_START_ROUTINE) thread_run,
|
||||
pWinThread,
|
||||
CREATE_SUSPENDED,
|
||||
&(pWinThread->ThreadID));
|
||||
SetThreadAffinityMask(pWinThread->ThreadHandle,
|
||||
0x01);
|
||||
SetThreadPriorityBoost(pWinThread->ThreadHandle,
|
||||
TRUE);
|
||||
SetThreadPriority(pWinThread->ThreadHandle,
|
||||
THREAD_PRIORITY_IDLE);
|
||||
|
||||
return (rt_uint8_t*)pWinThread;
|
||||
} /*** rt_hw_stack_init ***/
|
||||
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
* rt_hw_interrupt_disable()
|
||||
* Description : disable cpu interrupts
|
||||
* Argument(s) : void
|
||||
* Return(s) : rt_base_t
|
||||
* Caller(s) : Applicatios or os_kernel
|
||||
* Note(s) : none
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
rt_base_t rt_hw_interrupt_disable(void)
|
||||
{
|
||||
if(hInterruptEventMutex != NULL)
|
||||
{
|
||||
WaitForSingleObject(hInterruptEventMutex,INFINITE);
|
||||
}
|
||||
|
||||
return 0;
|
||||
} /*** rt_hw_interrupt_disable ***/
|
||||
|
||||
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
* rt_hw_interrupt_enable()
|
||||
* Description : enable cpu interrupts
|
||||
* Argument(s) : rt_base_t level
|
||||
* Return(s) : void
|
||||
* Caller(s) : Applications or os_kernel
|
||||
* Note(s) : none
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
void rt_hw_interrupt_enable(rt_base_t level)
|
||||
{
|
||||
level = level;
|
||||
|
||||
if (hInterruptEventMutex != NULL)
|
||||
{
|
||||
ReleaseMutex(hInterruptEventMutex);
|
||||
}
|
||||
|
||||
} /*** rt_hw_interrupt_enable ***/
|
||||
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
* rt_hw_context_switch_interrupt()
|
||||
* Description : switch thread's contex
|
||||
* Argument(s) : void
|
||||
* Return(s) : void
|
||||
* Caller(s) : os kernel
|
||||
* Note(s) : none
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to, rt_thread_t from_thread, rt_thread_t to_thread)
|
||||
{
|
||||
if(rt_thread_switch_interrupt_flag != 1)
|
||||
{
|
||||
rt_thread_switch_interrupt_flag = 1;
|
||||
|
||||
// set rt_interrupt_from_thread
|
||||
rt_interrupt_from_thread = *((rt_ubase_t *)(from));
|
||||
}
|
||||
|
||||
rt_interrupt_to_thread = *((rt_ubase_t *)(to));
|
||||
|
||||
//trigger YIELD exception(cause context switch)
|
||||
TriggerSimulateInterrupt(CPU_INTERRUPT_YIELD);
|
||||
} /*** rt_hw_context_switch_interrupt ***/
|
||||
|
||||
|
||||
|
||||
void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to)
|
||||
{
|
||||
if(rt_thread_switch_interrupt_flag != 1)
|
||||
{
|
||||
rt_thread_switch_interrupt_flag = 1;
|
||||
|
||||
// set rt_interrupt_from_thread
|
||||
rt_interrupt_from_thread = *((rt_ubase_t *)(from));
|
||||
|
||||
}
|
||||
|
||||
// set rt_interrupt_to_thread
|
||||
rt_interrupt_to_thread = *((rt_ubase_t *)(to));
|
||||
|
||||
//trigger YIELD exception(cause contex switch)
|
||||
TriggerSimulateInterrupt(CPU_INTERRUPT_YIELD);
|
||||
|
||||
// make sure the event is not already signaled
|
||||
win_thread_t *WinThread = (win_thread_t *)rt_interrupt_from_thread;
|
||||
ResetEvent(WinThread->YieldEvent);
|
||||
|
||||
/*
|
||||
* enable interrupt in advance so that scheduler can be executed.please note that interrupt
|
||||
* maybe disable twice before.
|
||||
*/
|
||||
rt_hw_interrupt_enable(0);
|
||||
rt_hw_interrupt_enable(0);
|
||||
|
||||
// wait to suspend.
|
||||
WaitForSingleObject(WinThread->YieldEvent, INFINITE);
|
||||
} /*** rt_hw_context_switch ***/
|
||||
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
* rt_hw_context_switch_to()
|
||||
* Description : switch to new thread
|
||||
* Argument(s) : rt_uint32_t to //the stack address of the thread which will switch to
|
||||
* Return(s) : void
|
||||
* Caller(s) : rt_thread schecale
|
||||
* Note(s) : this function is used to perform the first thread switch
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
void rt_hw_context_switch_to(rt_ubase_t to)
|
||||
{
|
||||
//set to thread
|
||||
rt_interrupt_to_thread = *((rt_ubase_t *)(to));
|
||||
|
||||
//clear from thread
|
||||
rt_interrupt_from_thread = 0;
|
||||
|
||||
//set interrupt to 1
|
||||
rt_thread_switch_interrupt_flag = 1;
|
||||
|
||||
//start WinThreadScheduler
|
||||
WinThreadScheduler();
|
||||
|
||||
//never reach here!
|
||||
return;
|
||||
|
||||
} /*** rt_hw_context_switch_to ***/
|
||||
|
||||
|
||||
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
* TriggerSimulateInterrupt()
|
||||
* Description : Trigger a simulated interrupts handle
|
||||
* Argument(s) : t_uint32_t IntIndex
|
||||
* Return(s) : void
|
||||
* Caller(s) : Applications
|
||||
* Note(s) : none
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
void TriggerSimulateInterrupt(rt_uint32_t IntIndex)
|
||||
{
|
||||
if((IntIndex < MAX_INTERRUPT_NUM) && (hInterruptEventMutex != NULL))
|
||||
{
|
||||
/* Yield interrupts are processed even when critical nesting is non-zero */
|
||||
WaitForSingleObject(hInterruptEventMutex,
|
||||
INFINITE);
|
||||
|
||||
CpuPendingInterrupts |= (1 << IntIndex);
|
||||
|
||||
SetEvent(hInterruptEventHandle);
|
||||
|
||||
ReleaseMutex(hInterruptEventMutex);
|
||||
}
|
||||
} /*** TriggerSimulateInterrupt ***/
|
||||
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
* RegisterSimulateInterrupt()
|
||||
* Description : Register a interrupt handle to simulate paltform
|
||||
* Argument(s) : rt_uint32_t IntIndex,rt_uint32_t (*IntHandler)(void)
|
||||
* Return(s) : void
|
||||
* Caller(s) : Applications
|
||||
* Note(s) : none
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
void RegisterSimulateInterrupt(rt_uint32_t IntIndex,rt_uint32_t (*IntHandler)(void))
|
||||
{
|
||||
if(IntIndex < MAX_INTERRUPT_NUM)
|
||||
{
|
||||
if (hInterruptEventMutex != NULL)
|
||||
{
|
||||
WaitForSingleObject(hInterruptEventMutex,
|
||||
INFINITE);
|
||||
|
||||
CpuIsrHandler[IntIndex] = IntHandler;
|
||||
|
||||
ReleaseMutex(hInterruptEventMutex);
|
||||
}
|
||||
else
|
||||
{
|
||||
CpuIsrHandler[IntIndex] = IntHandler;
|
||||
}
|
||||
}
|
||||
|
||||
} /*** RegisterSimulateInterrupt ***/
|
||||
|
||||
|
||||
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
* PRIVATE FUNCTION
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
* WinThreadScheduler()
|
||||
* Description : Handle all simulate interrupts
|
||||
* Argument(s) : void
|
||||
* Return(s) : static void
|
||||
* Caller(s) : os scachle
|
||||
* Note(s) : none
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
#define WIN_WM_MIN_RES (1)
|
||||
void WinThreadScheduler(void)
|
||||
{
|
||||
HANDLE hInterruptObjectList[2];
|
||||
HANDLE hThreadHandle;
|
||||
rt_uint32_t SwitchRequiredMask;
|
||||
rt_uint32_t i;
|
||||
|
||||
win_thread_t *WinThreadFrom;
|
||||
win_thread_t *WinThreadTo;
|
||||
|
||||
/*
|
||||
* Install the interrupt handlers used bye scheduler itself
|
||||
*/
|
||||
RegisterSimulateInterrupt(CPU_INTERRUPT_YIELD,
|
||||
YieldInterruptHandle);
|
||||
RegisterSimulateInterrupt(CPU_INTERRUPT_TICK,
|
||||
SysTickInterruptHandle);
|
||||
|
||||
/*
|
||||
* Create the events and mutex that are used to synchronise all the WinThreads
|
||||
*/
|
||||
hInterruptEventMutex = CreateMutex(NULL,
|
||||
FALSE,
|
||||
NULL);
|
||||
hInterruptEventHandle = CreateEvent(NULL,
|
||||
FALSE,
|
||||
FALSE,
|
||||
NULL);
|
||||
|
||||
if((hInterruptEventMutex == NULL) || (hInterruptEventHandle == NULL))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* Set the priority of this WinThread such that it is above the priority of the WinThreads
|
||||
* that run rt-threads.
|
||||
* This is higher priority is required to ensure simulate interrupts take priority over rt-threads
|
||||
*/
|
||||
hThreadHandle = GetCurrentThread();
|
||||
if(hThreadHandle == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (SetThreadPriority(hThreadHandle,
|
||||
THREAD_PRIORITY_HIGHEST) == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
SetThreadPriorityBoost(hThreadHandle,
|
||||
TRUE);
|
||||
SetThreadAffinityMask(hThreadHandle,
|
||||
0x01);
|
||||
|
||||
/*
|
||||
* Start the thread that simulates the timer peripheral to generate tick interrupts.
|
||||
*/
|
||||
OSTick_Thread = CreateThread(NULL,
|
||||
0,
|
||||
ThreadforSysTickTimer,
|
||||
0,
|
||||
CREATE_SUSPENDED,
|
||||
&OSTick_ThreadID);
|
||||
if(OSTick_Thread == NULL)
|
||||
{
|
||||
//Display Error Message
|
||||
|
||||
|
||||
return;
|
||||
}
|
||||
SetThreadPriority(OSTick_Thread,
|
||||
THREAD_PRIORITY_NORMAL);
|
||||
SetThreadPriorityBoost(OSTick_Thread,
|
||||
TRUE);
|
||||
SetThreadAffinityMask(OSTick_Thread,
|
||||
0x01);
|
||||
|
||||
/*
|
||||
* Set timer Caps
|
||||
*/
|
||||
if (timeGetDevCaps(&OSTick_TimerCap,
|
||||
sizeof(OSTick_TimerCap)) != TIMERR_NOERROR)
|
||||
{
|
||||
|
||||
CloseHandle(OSTick_Thread);
|
||||
|
||||
return;
|
||||
}
|
||||
if (OSTick_TimerCap.wPeriodMin < WIN_WM_MIN_RES)
|
||||
{
|
||||
OSTick_TimerCap.wPeriodMin = WIN_WM_MIN_RES;
|
||||
}
|
||||
|
||||
if(timeBeginPeriod(OSTick_TimerCap.wPeriodMin) != TIMERR_NOERROR)
|
||||
{
|
||||
CloseHandle(OSTick_Thread);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
OSTick_SignalPtr = CreateEvent(NULL,TRUE,FALSE,NULL);
|
||||
if(OSTick_SignalPtr == NULL)
|
||||
{
|
||||
// disp error message
|
||||
|
||||
timeEndPeriod(OSTick_TimerCap.wPeriodMin);
|
||||
CloseHandle(OSTick_Thread);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
OSTick_TimerID = timeSetEvent((UINT ) (1000 / RT_TICK_PER_SECOND) ,
|
||||
(UINT ) OSTick_TimerCap.wPeriodMin,
|
||||
(LPTIMECALLBACK ) OSTick_SignalPtr,
|
||||
(DWORD_PTR ) NULL,
|
||||
(UINT ) (TIME_PERIODIC | TIME_CALLBACK_EVENT_SET));
|
||||
|
||||
if(OSTick_TimerID == 0)
|
||||
{
|
||||
//disp
|
||||
|
||||
CloseHandle(OSTick_SignalPtr);
|
||||
timeEndPeriod(OSTick_TimerCap.wPeriodMin);
|
||||
CloseHandle(OSTick_Thread);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* Start OS Tick Thread an release Interrupt Mutex
|
||||
*/
|
||||
ResumeThread(OSTick_Thread);
|
||||
ReleaseMutex( hInterruptEventMutex );
|
||||
|
||||
//trigger YEILD INTERRUPT
|
||||
TriggerSimulateInterrupt(CPU_INTERRUPT_YIELD);
|
||||
|
||||
/*
|
||||
* block on the mutex that ensure exclusive access to the simulated interrupt objects
|
||||
* and the events that signals that a simulated interrupt should be processed.
|
||||
*/
|
||||
|
||||
hInterruptObjectList[0] = hInterruptEventHandle;
|
||||
hInterruptObjectList[1] = hInterruptEventMutex;
|
||||
|
||||
|
||||
while (1)
|
||||
{
|
||||
WaitForMultipleObjects(sizeof(hInterruptObjectList) / sizeof(HANDLE),
|
||||
hInterruptObjectList,
|
||||
TRUE,
|
||||
INFINITE);
|
||||
|
||||
/*
|
||||
* Used to indicate whether the simulate interrupt processing has necessitated a contex
|
||||
* switch to another thread
|
||||
*/
|
||||
SwitchRequiredMask = 0;
|
||||
|
||||
/*
|
||||
* For each interrupt we are interested in processing ,each of which is represented
|
||||
* by a bit in the 32bit CpuPendingInterrupts variable.
|
||||
*/
|
||||
for (i = 0; i < MAX_INTERRUPT_NUM; ++i)
|
||||
{
|
||||
/* is the simulated interrupt pending ? */
|
||||
if (CpuPendingInterrupts & (1UL << i))
|
||||
{
|
||||
/* Is a handler installed ?*/
|
||||
if (CpuIsrHandler[i] != NULL)
|
||||
{
|
||||
/* Run the actual handler */
|
||||
if (CpuIsrHandler[i]() != 0)
|
||||
{
|
||||
SwitchRequiredMask |= (1UL << i);
|
||||
}
|
||||
}
|
||||
|
||||
/* Clear the interrupt pending bit */
|
||||
CpuPendingInterrupts &= ~(1UL << i);
|
||||
}
|
||||
}
|
||||
|
||||
if(SwitchRequiredMask != 0)
|
||||
{
|
||||
WinThreadFrom = (win_thread_t *)rt_interrupt_from_thread;
|
||||
WinThreadTo = (win_thread_t *)rt_interrupt_to_thread;
|
||||
|
||||
if ((WinThreadFrom != NULL) && (WinThreadFrom->ThreadHandle != NULL))
|
||||
{
|
||||
SuspendThread(WinThreadFrom->ThreadHandle);
|
||||
SetEvent(WinThreadFrom->YieldEvent);
|
||||
}
|
||||
|
||||
ResumeThread(WinThreadTo->ThreadHandle);
|
||||
|
||||
}
|
||||
|
||||
ReleaseMutex(hInterruptEventMutex);
|
||||
}
|
||||
} /*** WinThreadScheduler ***/
|
||||
|
||||
|
||||
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
* ThreadforSysTickTimer()
|
||||
* Description : win thread to simulate a systick timer
|
||||
* Argument(s) : LPVOID lpParam
|
||||
* Return(s) : static DWORD WINAPI
|
||||
* Caller(s) : none
|
||||
* Note(s) : This is not a real time way of generating tick events as the next wake time should be relative
|
||||
* to the previous wake time,not the time Sleep() is called.
|
||||
* It is done this way to prevent overruns in this very non real time simulated/emulated environment
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
static DWORD WINAPI ThreadforSysTickTimer(LPVOID lpParam)
|
||||
{
|
||||
|
||||
(void)lpParam; //prevent compiler warnings
|
||||
|
||||
for(;;)
|
||||
{
|
||||
/*
|
||||
* Wait until the timer expires and we can access the simulated interrupt variables.
|
||||
*/
|
||||
WaitForSingleObject(OSTick_SignalPtr,INFINITE);
|
||||
|
||||
ResetEvent(OSTick_SignalPtr);
|
||||
|
||||
/*
|
||||
* Trigger a systick interrupt
|
||||
*/
|
||||
TriggerSimulateInterrupt(CPU_INTERRUPT_TICK);
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
} /*** prvThreadforSysTickTimer ***/
|
||||
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
* SysTickInterruptHandle()
|
||||
* Description : Interrupt handle for systick
|
||||
* Argument(s) : void
|
||||
* Return(s) : rt_uint32_t
|
||||
* Caller(s) : none
|
||||
* Note(s) : none
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
rt_uint32_t SysTickInterruptHandle(void)
|
||||
{
|
||||
|
||||
/* enter interrupt */
|
||||
rt_interrupt_enter();
|
||||
|
||||
rt_tick_increase();
|
||||
|
||||
/* leave interrupt */
|
||||
rt_interrupt_leave();
|
||||
|
||||
return 0;
|
||||
} /*** SysTickInterruptHandle ***/
|
||||
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
* YieldInterruptHandle()
|
||||
* Description : Interrupt handle for Yield
|
||||
* Argument(s) : void
|
||||
* Return(s) : rt_uint32_t
|
||||
* Caller(s) : none
|
||||
* Note(s) : none
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
rt_uint32_t YieldInterruptHandle(void)
|
||||
{
|
||||
|
||||
/*
|
||||
* if rt_thread_switch_interrupt_flag = 1 yield already handled
|
||||
*/
|
||||
if(rt_thread_switch_interrupt_flag != 0)
|
||||
{
|
||||
rt_thread_switch_interrupt_flag = 0;
|
||||
|
||||
/* return thread switch request = 1 */
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
} /*** YieldInterruptHandle ***/
|
||||
33
RT_Thread/libcpu/sim/win32/cpu_port.h
Normal file
33
RT_Thread/libcpu/sim/win32/cpu_port.h
Normal file
@ -0,0 +1,33 @@
|
||||
/*
|
||||
************************************************************************************************************************
|
||||
* File : cpu_port.h
|
||||
* By : xyou
|
||||
* Version : V1.00.00
|
||||
************************************************************************************************************************
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#ifndef _CPU_PORT_H_
|
||||
#define _CPU_PORT_H_
|
||||
|
||||
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
* CPU INTERRUPT PRIORITY
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
#define CPU_INTERRUPT_YIELD 0x01 // should be set to the lowest priority.
|
||||
#define CPU_INTERRUPT_TICK 0x00
|
||||
|
||||
|
||||
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
* FUNCTION PROTOTYPES
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
void TriggerSimulateInterrupt(rt_uint32_t IntIndex);
|
||||
|
||||
void WinThreadScheduler(void);
|
||||
#endif /* _CPU_PORT_H_ */
|
||||
325
RT_Thread/libcpu/sim/win32/startup_msvc.c
Normal file
325
RT_Thread/libcpu/sim/win32/startup_msvc.c
Normal file
@ -0,0 +1,325 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2020-12-04 tyx first implementation
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
|
||||
#ifdef RT_USING_USER_MAIN
|
||||
#ifndef RT_MAIN_THREAD_STACK_SIZE
|
||||
#define RT_MAIN_THREAD_STACK_SIZE 2048
|
||||
#endif
|
||||
#ifndef RT_MAIN_THREAD_PRIORITY
|
||||
#define RT_MAIN_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX / 3)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_COMPONENTS_INIT
|
||||
/*
|
||||
* Components Initialization will initialize some driver and components as following
|
||||
* order:
|
||||
* rti_start --> 0
|
||||
* BOARD_EXPORT --> 1
|
||||
* rti_board_end --> 1.end
|
||||
*
|
||||
* DEVICE_EXPORT --> 2
|
||||
* COMPONENT_EXPORT --> 3
|
||||
* FS_EXPORT --> 4
|
||||
* ENV_EXPORT --> 5
|
||||
* APP_EXPORT --> 6
|
||||
*
|
||||
* rti_end --> 6.end
|
||||
*
|
||||
* These automatically initialization, the driver or component initial function must
|
||||
* be defined with:
|
||||
* INIT_BOARD_EXPORT(fn);
|
||||
* INIT_DEVICE_EXPORT(fn);
|
||||
* ...
|
||||
* INIT_APP_EXPORT(fn);
|
||||
* etc.
|
||||
*/
|
||||
|
||||
#pragma section("rti_fn$a", read)
|
||||
const char __rti_fn_begin_name[] = "__rti_fn_start";
|
||||
__declspec(allocate("rti_fn$a")) const struct rt_init_desc __rti_fn_begin =
|
||||
{
|
||||
__rti_fn_begin_name,
|
||||
NULL
|
||||
};
|
||||
|
||||
#pragma section("rti_fn$z", read)
|
||||
const char __rti_fn_end_name[] = "__rti_fn_end";
|
||||
__declspec(allocate("rti_fn$z")) const struct rt_init_desc __rti_fn_end =
|
||||
{
|
||||
__rti_fn_end_name,
|
||||
NULL
|
||||
};
|
||||
|
||||
static int rti_start(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
INIT_EXPORT(rti_start, "0");
|
||||
|
||||
static int rti_board_end(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
INIT_EXPORT(rti_board_end, "1.end");
|
||||
|
||||
static int rti_end(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
INIT_EXPORT(rti_end, "6.end");
|
||||
|
||||
struct rt_init_tag
|
||||
{
|
||||
const char *level;
|
||||
init_fn_t fn;
|
||||
#ifdef RT_DEBUGING_AUTO_INIT
|
||||
const char *fn_name;
|
||||
#endif
|
||||
};
|
||||
|
||||
static rt_size_t rt_init_num = 0;
|
||||
static struct rt_init_tag rt_init_table[2048] = { 0 };
|
||||
static rt_bool_t rt_init_flag = RT_FALSE;
|
||||
|
||||
static int rt_init_objects_sort(void)
|
||||
{
|
||||
rt_size_t index_i, index_j;
|
||||
struct rt_init_tag init_temp = { 0 };
|
||||
unsigned int *ptr_begin = (unsigned int *)&__rti_fn_begin;
|
||||
unsigned int *ptr_end = (unsigned int *)&__rti_fn_end;
|
||||
struct rt_init_tag *table = rt_init_table;
|
||||
ptr_begin += (sizeof(struct rt_init_desc) / sizeof(unsigned int));
|
||||
|
||||
if (rt_init_flag)
|
||||
return rt_init_num;
|
||||
|
||||
while (*ptr_begin == 0)
|
||||
ptr_begin++;
|
||||
|
||||
do (ptr_end--);
|
||||
while (*ptr_end == 0);
|
||||
|
||||
while (ptr_begin < ptr_end)
|
||||
{
|
||||
if (*ptr_begin != 0)
|
||||
{
|
||||
table->level = ((struct rt_init_desc *)ptr_begin)->level;
|
||||
table->fn = ((struct rt_init_desc *)ptr_begin)->fn;
|
||||
#ifdef RT_DEBUGING_AUTO_INIT
|
||||
table->fn_name = ((struct rt_init_desc *)ptr_begin)->fn_name;
|
||||
#endif
|
||||
ptr_begin += sizeof(struct rt_init_desc) / sizeof(unsigned int);
|
||||
table++;
|
||||
rt_init_num += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
ptr_begin++;
|
||||
}
|
||||
}
|
||||
|
||||
if (rt_init_num == 0) /* no need sort */
|
||||
return rt_init_num;
|
||||
|
||||
/* bubble sort algorithms */
|
||||
for (index_i = 0; index_i < (rt_init_num - 1); index_i++)
|
||||
{
|
||||
for (index_j = 0; index_j < ((rt_init_num - 1) - index_i); index_j++)
|
||||
{
|
||||
if (rt_strcmp(rt_init_table[index_j].level, rt_init_table[index_j + 1].level) > 0)
|
||||
{
|
||||
init_temp = rt_init_table[index_j];
|
||||
rt_init_table[index_j] = rt_init_table[index_j + 1];
|
||||
rt_init_table[index_j + 1] = init_temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rt_init_flag = RT_TRUE;
|
||||
return rt_init_num;
|
||||
}
|
||||
|
||||
/**
|
||||
* RT-Thread Components Initialization for board
|
||||
*/
|
||||
void rt_components_board_init(void)
|
||||
{
|
||||
const char* lv_start = ".rti_fn.0";
|
||||
const char* lv_end = ".rti_fn.1.end";
|
||||
rt_size_t index_i;
|
||||
int result;
|
||||
|
||||
rt_init_objects_sort();
|
||||
|
||||
for (index_i = 0; index_i < rt_init_num; index_i++)
|
||||
{
|
||||
if (rt_init_table[index_i].fn)
|
||||
{
|
||||
if (rt_strcmp(rt_init_table[index_i].level, lv_end) >= 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
#ifdef RT_DEBUGING_AUTO_INIT
|
||||
rt_kprintf("initialize %s", rt_init_table[index_i].fn_name);
|
||||
result = rt_init_table[index_i].fn();
|
||||
rt_kprintf(":%d done\n", result);
|
||||
#else
|
||||
result = rt_init_table[index_i].fn();
|
||||
#endif /* RT_DEBUGING_AUTO_INIT */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* RT-Thread Components Initialization
|
||||
*/
|
||||
void rt_components_init(void)
|
||||
{
|
||||
const char* lv_start = ".rti_fn.1.end";
|
||||
const char* lv_end = ".rti_fn.6.end";
|
||||
int result;
|
||||
rt_size_t index_i;
|
||||
|
||||
rt_init_objects_sort();
|
||||
|
||||
for (index_i = 0; index_i < rt_init_num; index_i++)
|
||||
{
|
||||
if (rt_init_table[index_i].fn)
|
||||
{
|
||||
if (rt_strcmp(rt_init_table[index_i].level, lv_start) <= 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (rt_strcmp(rt_init_table[index_i].level, lv_end) >= 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
#ifdef RT_DEBUGING_AUTO_INIT
|
||||
rt_kprintf("initialize %s", rt_init_table[index_i].fn_name);
|
||||
result = rt_init_table[index_i].fn();
|
||||
rt_kprintf(":%d done\n", result);
|
||||
#else
|
||||
result = rt_init_table[index_i].fn();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif /* RT_USING_COMPONENTS_INIT */
|
||||
|
||||
#ifdef RT_USING_USER_MAIN
|
||||
|
||||
void rt_application_init(void);
|
||||
void rt_hw_board_init(void);
|
||||
int rtthread_startup(void);
|
||||
|
||||
/* system entry */
|
||||
extern int rtthread_startup(void);
|
||||
int wmain(int argc, char* argv[])
|
||||
{
|
||||
/* disable interrupt first */
|
||||
rt_hw_interrupt_disable();
|
||||
/* startup RT-Thread RTOS */
|
||||
rtthread_startup();
|
||||
}
|
||||
#pragma comment(linker, "/subsystem:console /entry:wmainCRTStartup")
|
||||
|
||||
#ifndef RT_USING_HEAP
|
||||
/* if there is not enable heap, we should use static thread and stack. */
|
||||
rt_align(8)
|
||||
static rt_uint8_t main_stack[RT_MAIN_THREAD_STACK_SIZE];
|
||||
struct rt_thread main_thread;
|
||||
#endif
|
||||
|
||||
/* the system main thread */
|
||||
void main_thread_entry(void *parameter)
|
||||
{
|
||||
extern int main(void);
|
||||
|
||||
#ifdef RT_USING_COMPONENTS_INIT
|
||||
/* RT-Thread components initialization */
|
||||
rt_components_init();
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_SMP
|
||||
rt_hw_secondary_cpu_up();
|
||||
#endif
|
||||
|
||||
/* invoke system main function */
|
||||
main();
|
||||
}
|
||||
|
||||
void rt_application_init(void)
|
||||
{
|
||||
rt_thread_t tid;
|
||||
|
||||
#ifdef RT_USING_HEAP
|
||||
tid = rt_thread_create("main", main_thread_entry, RT_NULL,
|
||||
RT_MAIN_THREAD_STACK_SIZE, RT_MAIN_THREAD_PRIORITY, 20);
|
||||
RT_ASSERT(tid != RT_NULL);
|
||||
#else
|
||||
rt_err_t result;
|
||||
|
||||
tid = &main_thread;
|
||||
result = rt_thread_init(tid, "main", main_thread_entry, RT_NULL,
|
||||
main_stack, sizeof(main_stack), RT_MAIN_THREAD_PRIORITY, 20);
|
||||
RT_ASSERT(result == RT_EOK);
|
||||
|
||||
/* if not define RT_USING_HEAP, using to eliminate the warning */
|
||||
(void)result;
|
||||
#endif
|
||||
|
||||
rt_thread_startup(tid);
|
||||
}
|
||||
|
||||
int rtthread_startup(void)
|
||||
{
|
||||
rt_hw_interrupt_disable();
|
||||
|
||||
/* board level initialization
|
||||
* NOTE: please initialize heap inside board initialization.
|
||||
*/
|
||||
rt_hw_board_init();
|
||||
|
||||
/* show RT-Thread version */
|
||||
rt_show_version();
|
||||
|
||||
/* timer system initialization */
|
||||
rt_system_timer_init();
|
||||
|
||||
/* scheduler system initialization */
|
||||
rt_system_scheduler_init();
|
||||
|
||||
#ifdef RT_USING_SIGNALS
|
||||
/* signal system initialization */
|
||||
rt_system_signal_init();
|
||||
#endif
|
||||
|
||||
/* create init_thread */
|
||||
rt_application_init();
|
||||
|
||||
/* timer thread initialization */
|
||||
rt_system_timer_thread_init();
|
||||
|
||||
/* idle thread initialization */
|
||||
rt_thread_idle_init();
|
||||
|
||||
#ifdef RT_USING_SMP
|
||||
rt_hw_spin_lock(&_cpus_lock);
|
||||
#endif /*RT_USING_SMP*/
|
||||
|
||||
/* start scheduler */
|
||||
rt_system_scheduler_start();
|
||||
|
||||
/* never reach here */
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user