From 7b58dd92fada8a99cf4f4beecb68a987f67c925f Mon Sep 17 00:00:00 2001 From: prife Date: Sun, 13 Jan 2013 22:58:45 +0800 Subject: [PATCH 01/19] add libcpu/sim/posix/cpu_port.c --- libcpu/sim/posix/cpu_port.c | 447 ++++++++++++++++++++++++++++++++++++ 1 file changed, 447 insertions(+) create mode 100755 libcpu/sim/posix/cpu_port.c diff --git a/libcpu/sim/posix/cpu_port.c b/libcpu/sim/posix/cpu_port.c new file mode 100755 index 0000000000..919abc5209 --- /dev/null +++ b/libcpu/sim/posix/cpu_port.c @@ -0,0 +1,447 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +//#define TRACE printf +#define TRACE(...) +#define _DEBUG + +typedef struct _thread +{ + pthread_t pthread; + void (*task)(void *); + void *para; + void (*exit)(void); + sem_t sem; + rt_thread_t rtthread; + 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 INT_ENABLE 0 + * #define INT_DISABLE 1 + */ +/* flag in interrupt handling */ +rt_uint32_t rt_interrupt_from_thread, rt_interrupt_to_thread; +rt_uint32_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_switch_handler(int sig) +{ + 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; + + /* FIXME 注意!此时 rt_thread_self的值是to线程的值! */ + tid = rt_thread_self(); + RT_ASSERT(thread_from->pthread == pid); + RT_ASSERT((thread_t *)(tid->sp) == thread_to); + + TRACE("signal: SIGSUSPEND suspend <%s>\n", thread_from->rtthread->name); + sem_wait(&thread_from->sem); + TRACE("signal: SIGSUSPEND resume <%s>\n", thread_from->rtthread->name); +} + +static void *thread_run(void *parameter) +{ + rt_thread_t tid; + thread_t *thread; + thread = THREAD_T(parameter); + int res; + + /* FIXME set signal mask, mask the timer! */ + signal_mask(); + + 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->name); + thread->rtthread = tid; + thread->task(thread->para); + TRACE("pid <%08x> tid <%s> exit...\n", (unsigned int)(thread->pthread), + tid->name); + //FIXME + thread->exit(); + //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; +} + + +/* interrupt contex switch hit, value 1 or 0 */ +static long int_cs_hit; + +rt_base_t rt_hw_interrupt_disable(void) +{ + if (ptr_int_mutex != NULL) + { +// pthread_mutex_lock(ptr_int_mutex); //FIXME + } + + /*TODO: It may need to mask the signal */ + return 0; +} + +void rt_hw_interrupt_enable(rt_base_t level) +{ + level = level; + + if (ptr_int_mutex != NULL) + { +// pthread_mutex_unlock(ptr_int_mutex); //FIXME + } + /*TODO: It may need to unmask the signal */ +} + +void rt_hw_context_switch_interrupt(rt_uint32_t from, + rt_uint32_t to) +{ + rt_hw_context_switch(from, to); +} + +void rt_hw_context_switch(rt_uint32_t from, + rt_uint32_t to) +{ + struct rt_thread * tid; + pthread_t pid; + thread_t *thread_from; + thread_t *thread_to; + + RT_ASSERT(from != to); + +#if 0 + if (rt_thread_switch_interrupt_flag != 1) + { + rt_thread_switch_interrupt_flag = 1; + + // set rt_interrupt_from_thread + rt_interrupt_from_thread = *((rt_uint32_t *)from); + } +#endif + rt_interrupt_from_thread = *((rt_uint32_t *)from); + rt_interrupt_to_thread = *((rt_uint32_t *)to); + + thread_from = (thread_t *) rt_interrupt_from_thread; + thread_to = (thread_t *) rt_interrupt_to_thread; + + /* FIXME note: now, rt_current_thread is the thread_to! scheduler.c:272 */ + tid = rt_thread_self(); + pid = pthread_self(); + + /* 注意,只有两种可可能,一种是线程函数中调用此函数,一种是在中断中调用 + * 而在中断调用是在主线程的信号处理函数中实现(目前只有tick中断)。 + * 即, 只有如下两种可能: + * 1)普通RTT线程间接调用,如rt_thread_delay函数 + * 2)或者主线程信号处理函数,如rt_tick_increase中调用 + */ + if (pid != mainthread_pid) + { + TRACE("conswitch: P in pid<%x> ,suspend <%s>, resume <%s>!\n", + (unsigned int)pid, + thread_from->rtthread->name, + thread_to->rtthread->name); + /* from线程就是当前rtt线程 */ + + /* 确定一下,这两个值一定是相等的! */ + RT_ASSERT(thread_from->pthread == pid); + + /* 唤醒to线程 */ + sem_post(& thread_to->sem); + + /* 挂起from线程, 既然from线程就是当前线程,所以应该直接 + * 挂起在这里 + */ + sem_wait(& thread_from->sem); + } + else + { + /* FIXME: 注意这段代码是在system tick 函数中执行的, + * 即此时位于主线程的SIGALRM信号处理函数中 */ + TRACE("conswitch: S in pid<%x> ,suspend <%s>, resume <%s>!\n", + (unsigned int)pid, + thread_from->rtthread->name, + thread_to->rtthread->name); + + /* 挂起from线程 */ + pthread_kill(thread_from->pthread, MSG_SUSPEND); + + /* 唤醒to线程 */ + sem_post(& thread_to->sem); + } +} + +void rt_hw_context_switch_to(rt_uint32_t to) +{ + //set to thread + rt_interrupt_to_thread = *((rt_uint32_t *)(to)); + + //clear from thread + rt_interrupt_from_thread = 0; + + //set interrupt to 1 + rt_thread_switch_interrupt_flag = 0; //<------ + + //start the main thread scheduler + mainthread_scheduler(); + + //never reach here! + return; +} + +static int mainthread_scheduler(void) +{ + int i, res; + thread_t *thread_from; + thread_t *thread_to; + pthread_mutex_t mutex; + pthread_mutexattr_t mutexattr; + unsigned int contex_switch_mask; + + /* save the main thread id */ + mainthread_pid = pthread_self(); + TRACE("pid <%08x> mainthread\n", (unsigned int)(mainthread_pid)); + + /* register interrupts which is simulated for yield and systick */ + //register_interrupt(CPU_INTERRUPT_YIELD, yield_interrupt_isr); + //register_interrupt(CPU_INTERRUPT_TICK, tick_interrupt_isr); + + /* install signal handler of system tick */ + signal_install(SIGALRM, mthread_signal_tick); + /* install signal handler used to suspend itself */ + signal_install(MSG_SUSPEND, thread_switch_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); + pthread_mutex_init(ptr_int_mutex, &mutexattr); + pthread_cond_init(&cond_int_hit, NULL); + + /* start timer */ + start_sys_timer(); + + /* FIXME: note that, cond var could not released earlier than pthread_con_wait */ + /* trigger_interrupt(CPU_INTERRUPT_YIELD); */ + thread_to = (thread_t *) rt_interrupt_to_thread; + thread_resume(thread_to); + + for (;;) + { +#if 0 + pthread_mutex_lock(ptr_int_mutex); + /*Lock mutex and wait for signal. Note that the pthread_cond_wait + *routine will automatically and atomically unlock mutex while it waits. + */ + TRACE("mthread: wait cond val!\n"); + pthread_cond_wait(&cond_int_hit, ptr_int_mutex); + TRACE("mthread: got cond val!\n"); + + pthread_mutex_unlock(ptr_int_mutex); +#endif + //printf("main thread...\n"); + sleep(1); + } + + 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; + + RT_ASSERT(RT_TICK_PER_SECOND <= 1000000 || RT_TICK_PER_SECOND >= 1); + + 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) +{ + pthread_t pid = pthread_self(); + + if (sig == SIGALRM) + { + TRACE("pid <%x> signal: SIGALRM enter!\n", (unsigned int)pid); + tick_interrupt_isr(); + 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; +} + +#if 0 + +static void trigger_interrupt(int index) +{ + if ((index < MAX_INTERRUPT_NUM) && ptr_int_mutex != NULL) + { + pthread_mutex_lock(ptr_int_mutex); + cpu_pending_interrupts |= (1 << index); + + /* signal the condition val */ + pthread_cond_signal(&cond_int_hit); + + pthread_mutex_unlock(ptr_int_mutex); + } +} +#endif + From 212b71205b61f7b8f9b478113f8e0ec10990243e Mon Sep 17 00:00:00 2001 From: prife Date: Sun, 13 Jan 2013 23:10:49 +0800 Subject: [PATCH 02/19] add bsp: simluator for linux --- bsp/simlinux/SConscript | 12 + bsp/simlinux/SConstruct | 91 ++++++ bsp/simlinux/applications/SConscript | 9 + bsp/simlinux/applications/application.c | 146 +++++++++ bsp/simlinux/applications/platform.c | 29 ++ bsp/simlinux/applications/startup.c | 92 ++++++ bsp/simlinux/drivers/SConscript | 22 ++ bsp/simlinux/drivers/board.c | 91 ++++++ bsp/simlinux/drivers/board.h | 33 ++ bsp/simlinux/drivers/nanddrv_file.c | 399 +++++++++++++++++++++++ bsp/simlinux/drivers/sd_sim.c | 193 +++++++++++ bsp/simlinux/drivers/sdl_fb.c | 304 +++++++++++++++++ bsp/simlinux/drivers/serial.c | 176 ++++++++++ bsp/simlinux/drivers/serial.h | 22 ++ bsp/simlinux/drivers/sst25vfxx_mtd.h | 24 ++ bsp/simlinux/drivers/sst25vfxx_mtd_sim.c | 227 +++++++++++++ bsp/simlinux/drivers/usart_sim.c | 138 ++++++++ bsp/simlinux/rtconfig.h | 228 +++++++++++++ bsp/simlinux/rtconfig.py | 80 +++++ 19 files changed, 2316 insertions(+) create mode 100755 bsp/simlinux/SConscript create mode 100755 bsp/simlinux/SConstruct create mode 100755 bsp/simlinux/applications/SConscript create mode 100755 bsp/simlinux/applications/application.c create mode 100755 bsp/simlinux/applications/platform.c create mode 100755 bsp/simlinux/applications/startup.c create mode 100755 bsp/simlinux/drivers/SConscript create mode 100755 bsp/simlinux/drivers/board.c create mode 100755 bsp/simlinux/drivers/board.h create mode 100755 bsp/simlinux/drivers/nanddrv_file.c create mode 100755 bsp/simlinux/drivers/sd_sim.c create mode 100755 bsp/simlinux/drivers/sdl_fb.c create mode 100755 bsp/simlinux/drivers/serial.c create mode 100755 bsp/simlinux/drivers/serial.h create mode 100755 bsp/simlinux/drivers/sst25vfxx_mtd.h create mode 100755 bsp/simlinux/drivers/sst25vfxx_mtd_sim.c create mode 100755 bsp/simlinux/drivers/usart_sim.c create mode 100755 bsp/simlinux/rtconfig.h create mode 100755 bsp/simlinux/rtconfig.py diff --git a/bsp/simlinux/SConscript b/bsp/simlinux/SConscript new file mode 100755 index 0000000000..0992612410 --- /dev/null +++ b/bsp/simlinux/SConscript @@ -0,0 +1,12 @@ +from building import * + +cwd = GetCurrentDir() +objs = [] +list = os.listdir(cwd) + +for d in list: + path = os.path.join(cwd, d) + if os.path.isfile(os.path.join(path, 'SConscript')): + objs = objs + SConscript(os.path.join(d, 'SConscript')) + +Return('objs') diff --git a/bsp/simlinux/SConstruct b/bsp/simlinux/SConstruct new file mode 100755 index 0000000000..4dd5342cc6 --- /dev/null +++ b/bsp/simlinux/SConstruct @@ -0,0 +1,91 @@ +import os +import sys +import rtconfig + +if os.getenv('RTT_ROOT'): + RTT_ROOT = os.getenv('RTT_ROOT') +else: + RTT_ROOT = os.path.normpath(os.getcwd() + '/../..') + +if os.getenv('RTT_RTGUI'): + RTT_RTGUI = os.getenv('RTT_RTGUI') +else: + # set the rtgui root directory by hand + # empty string means use the RTGUI in svn + # RTT_RTGUI = os.path.normpath(r'F:\Project\git\rt-gui\components\rtgui') + RTT_RTGUI ='' + +sys.path = sys.path + [os.path.join(RTT_ROOT, 'tools')] +from building import * + + +env = Environment() + +Export('RTT_ROOT') +Export('rtconfig') + +if rtconfig.PLATFORM == 'cl': + TARGET = 'rtthread-win32.' + rtconfig.TARGET_EXT + + libs = Split(''' + winmm + gdi32 + winspool + comdlg32 + advapi32 + shell32 + ole32 + oleaut32 + uuid + odbc32 + odbccp32 + ''') + definitions = Split(''' + WIN32 + _DEBUG + _CONSOLE + MSVC + _TIME_T_DEFINED + ''') + env.Append(CCFLAGS=rtconfig.CFLAGS) + env.Append(LINKFLAGS=rtconfig.LFLAGS) + env['LIBS']=libs + env['CPPDEFINES']=definitions +else: + TARGET = 'rtthread' + env.Append(CCFLAGS=rtconfig.CFLAGS) + env.Append(LINKFLAGS=rtconfig.LFLAGS) + + +# prepare building environment + +objs = PrepareBuilding(env, RTT_ROOT, has_libcpu=False, remove_components=['rtgui']) +if GetDepend('RT_USING_RTGUI'): + sdl_lib = ['SDL', 'SDLmain'] + sdl_lib_path = [os.path.abspath('SDL/lib/x86')] + sdl_include_path = [os.path.abspath('SDL/include')] + env.Append(LIBS=sdl_lib) + env.Append(LIBPATH=sdl_lib_path) + env.Append(CPPPATH=sdl_include_path) + + if RTT_RTGUI: + objs += SConscript(os.path.join(RTT_RTGUI, 'SConscript'), + variant_dir='build/components/rtgui', + duplicate=0) + objs = objs + SConscript(RTT_RTGUI+'/../../demo/examples/SConscript', + variant_dir='build/examples/gui', duplicate=0) + else: + objs += SConscript(os.path.join(RTT_ROOT + '/components/rtgui', 'SConscript'), + variant_dir='build/components/rtgui', + duplicate=0) + objs = objs + SConscript(RTT_ROOT + '/examples/gui/SConscript', + variant_dir='build/examples/gui', duplicate=0) + +if GetDepend('RT_USING_TC'): + objs = objs + SConscript(RTT_ROOT + '/examples/kernel/SConscript', variant_dir = 'build/tc/kernel', duplicate=0) + +# build program +program = env.Program(TARGET, objs) + +# end building +EndBuilding(TARGET, program) diff --git a/bsp/simlinux/applications/SConscript b/bsp/simlinux/applications/SConscript new file mode 100755 index 0000000000..4ccb177207 --- /dev/null +++ b/bsp/simlinux/applications/SConscript @@ -0,0 +1,9 @@ +from building import * + +cwd = GetCurrentDir() +src = Glob('*.c') +CPPPATH = [cwd, str(Dir('#'))] + +group = DefineGroup('Applications', src, depend = [''], CPPPATH = CPPPATH) + +Return('group') diff --git a/bsp/simlinux/applications/application.c b/bsp/simlinux/applications/application.c new file mode 100755 index 0000000000..c8ced1accb --- /dev/null +++ b/bsp/simlinux/applications/application.c @@ -0,0 +1,146 @@ +/* + * File : application.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-01-05 Bernard the first version + */ + +#include +#include +#include + +#include + +void rt_init_thread_entry(void *parameter) +{ +#ifdef RT_USING_LWIP + pcap_netif_hw_init(); +#endif + + /* initialization RT-Thread Components */ + rt_components_init(); + + rt_platform_init(); + + /* File system Initialization */ +#ifdef RT_USING_DFS + { +#ifdef RT_USING_DFS_WINSHAREDIR + { + extern rt_err_t rt_win_sharedir_init(const char *name); + extern int dfs_win32_init(void); + + rt_win_sharedir_init("wdd"); + dfs_win32_init(); + + if (dfs_mount("wdd", "/", "wdir", 0, 0) == 0) + rt_kprintf("win32 share directory initialized!\n"); + else + rt_kprintf("win32 share directory initialized failed!\n"); + } +#endif + +#ifdef RT_USING_DFS_ELMFAT + /* mount sd card fatfs as root directory */ + if (dfs_mount("sd0", "/disk/sd", "elm", 0, 0) == 0) + rt_kprintf("fatfs initialized!\n"); + else + rt_kprintf("fatfs initialization failed!\n"); +#endif + +#ifdef RT_USING_DFS_UFFS + /* mount uffs as the nand flash file system */ + if (dfs_mount("nand0", "/disk/nand", "uffs", 0, 0) == 0) + rt_kprintf("uffs initialized!\n"); + else + rt_kprintf("uffs initialization failed!\n"); +#endif + +#ifdef RT_USING_DFS_JFFS2 + /* mount jffs2 as the nor flash file system */ + if (dfs_mount("nor", "/disk/nor", "jffs2", 0, 0) == 0) + rt_kprintf("jffs2 initialized!\n"); + else + rt_kprintf("jffs2 initialization failed!\n"); +#endif + + } +#endif + +#if 0 + { + extern void application_init(void); + rt_thread_delay(RT_TICK_PER_SECOND); + application_init(); + } +#endif + +#if defined(RT_USING_RTGUI) + rt_thread_delay(3000); + snake_main(); +#endif +} + +static void rt_test_thread_entry(void *parameter) +{ + int i; + for (i = 0; i < 10; i++) + { + rt_kprintf("hello, world\n"); + rt_thread_delay(RT_TICK_PER_SECOND); + } +} + +static void rt_high_thread_entry(void *parameter) +{ + int i; + for (i = 0; i < 3; i++) + { + rt_kprintf("high thread <%d> \n", i); + rt_thread_delay(2*RT_TICK_PER_SECOND); + } +} + +int rt_application_init() +{ + rt_thread_t tid; + +#if 0 + tid = rt_thread_create("init", + rt_init_thread_entry, RT_NULL, + 2048, RT_THREAD_PRIORITY_MAX / 3, 20); + + if (tid != RT_NULL) + rt_thread_startup(tid); + tid = rt_thread_create("test", + rt_test_thread_entry, RT_NULL, + 2048, RT_THREAD_PRIORITY_MAX * 3 / 4, 20); + if (tid != RT_NULL) + rt_thread_startup(tid); + +#endif + + tid = rt_thread_create("test1", + rt_high_thread_entry, RT_NULL, + 2048, RT_THREAD_PRIORITY_MAX / 2, 20); + if (tid != RT_NULL) + rt_thread_startup(tid); + + tid = rt_thread_create("test2", + rt_test_thread_entry, RT_NULL, + 2048, RT_THREAD_PRIORITY_MAX / 2, 20); + if (tid != RT_NULL) + rt_thread_startup(tid); + + return 0; +} + + +/*@}*/ diff --git a/bsp/simlinux/applications/platform.c b/bsp/simlinux/applications/platform.c new file mode 100755 index 0000000000..3c7ffdb541 --- /dev/null +++ b/bsp/simlinux/applications/platform.c @@ -0,0 +1,29 @@ +#include +#include "board.h" + +void rt_platform_init(void) +{ +#ifdef RT_USING_DFS + /* initialize sd card */ + rt_hw_sdcard_init(); + +#if defined(RT_USING_MTD_NAND) + rt_hw_mtd_nand_init(); +#endif + +#if defined(RT_USING_MTD_NOR) + sst25vfxx_mtd_init("nor", 0, RT_UINT32_MAX); +#endif + +#endif /* RT_USING_DFS */ + +#ifdef RT_USING_RTGUI + /* start sdl thread to simulate an LCD */ + rt_hw_sdl_start(); +#endif /* RT_USING_RTGUI */ + +#ifdef _WIN32 + rt_thread_idle_sethook(rt_hw_win32_low_cpu); +#endif +} + diff --git a/bsp/simlinux/applications/startup.c b/bsp/simlinux/applications/startup.c new file mode 100755 index 0000000000..2b21c69016 --- /dev/null +++ b/bsp/simlinux/applications/startup.c @@ -0,0 +1,92 @@ +/* + * File : startup.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006, RT-Thread Develop Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://openlab.rt-thread.com/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2012-09-03 prife first implementation + */ + +#include +#include + +#include "board.h" + +/** + * @addtogroup win32 + */ + +/*@{*/ + +extern int rt_application_init(void); +#ifdef RT_USING_FINSH +extern void finsh_system_init(void); +extern void finsh_set_device(const char *device); +#endif + +extern rt_uint8_t *heap; +/** + * This function will startup RT-Thread RTOS. + */ +void rtthread_startup(void) +{ + /* init board */ + rt_hw_board_init(); + + /* show version */ + rt_show_version(); + + /* init tick */ + rt_system_tick_init(); + + /* init kernel object */ + rt_system_object_init(); + + /* init timer system */ + rt_system_timer_init(); + +#ifdef RT_USING_HEAP + /* init memory system */ + rt_system_heap_init((void *)heap, (void *)&heap[RT_HEAP_SIZE - 1]); +#endif + + /* init scheduler system */ + rt_system_scheduler_init(); + + /* init all device */ +#ifdef RT_USING_DEVICE + rt_device_init_all(); +#endif + /* init application */ + rt_application_init(); + + /* init timer thread */ + rt_system_timer_thread_init(); + + /* init idle thread */ + rt_thread_idle_init(); + + /* start scheduler */ + rt_system_scheduler_start(); + + /* never reach here */ + return ; +} + +int main(void) +{ + /* disable interrupt first */ + rt_hw_interrupt_disable(); + + /* startup RT-Thread RTOS */ + rtthread_startup(); + + return 0; +} + +/*@}*/ diff --git a/bsp/simlinux/drivers/SConscript b/bsp/simlinux/drivers/SConscript new file mode 100755 index 0000000000..34b1a0bda7 --- /dev/null +++ b/bsp/simlinux/drivers/SConscript @@ -0,0 +1,22 @@ +from building import * + +cwd = GetCurrentDir() +src = Glob('*.c') + +# remove no need file. +if GetDepend('RT_USING_RTGUI') == False: + SrcRemove(src, 'sdl_fb.c') +if GetDepend('RT_USING_DFS') == False or GetDepend('RT_USING_DFS_ELMFAT') == False: + SrcRemove(src, 'sd_sim.c') +if GetDepend('RT_USING_DFS') == False or GetDepend('RT_USING_MTD_NAND') == False: + SrcRemove(src, 'nanddrv_file.c') +if GetDepend('RT_USING_DFS') == False or GetDepend('RT_USING_MTD_NOR') == False: + SrcRemove(src, 'sst25vfxx_mtd_sim.c') +if GetDepend('RT_USING_SERIAL') == False: + SrcRemove(src, 'usart_sim.c') + +CPPPATH = [cwd] + +group = DefineGroup('Drivers', src, depend = [''], CPPPATH = CPPPATH) + +Return('group') diff --git a/bsp/simlinux/drivers/board.c b/bsp/simlinux/drivers/board.c new file mode 100755 index 0000000000..5506acb55d --- /dev/null +++ b/bsp/simlinux/drivers/board.c @@ -0,0 +1,91 @@ +/* + * File : board.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2009 RT-Thread Develop Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-01-05 Bernard first implementation + */ + +#include +#include +#include "board.h" +#include + +/** + * @addtogroup simulator on win32 + */ +rt_uint8_t *heap; + +rt_uint8_t *rt_hw_sram_init(void) +{ + rt_uint8_t *heap; + heap = malloc(RT_HEAP_SIZE); + if (heap == RT_NULL) + { + rt_kprintf("there is no memory in pc."); +#ifdef _WIN32 + _exit(1); +#else + exit(1); +#endif + } + return heap; +} + +#ifdef _WIN32 +#include +#endif + +void rt_hw_win32_low_cpu(void) +{ +#ifdef _WIN32 + /* in windows */ + Sleep(1000); +#else + /* in linux */ + sleep(1); +#endif +} + +#if defined(RT_USING_FINSH) + +#ifndef _CRT_TERMINATE_DEFINED +#define _CRT_TERMINATE_DEFINED +_CRTIMP __declspec(noreturn) void __cdecl exit(__in int _Code); +_CRTIMP __declspec(noreturn) void __cdecl _exit(__in int _Code); +_CRTIMP void __cdecl abort(void); +#endif + +#include +void rt_hw_exit(void) +{ + rt_kprintf("RT-Thread, bye\n"); + exit(0); +} +FINSH_FUNCTION_EXPORT_ALIAS(rt_hw_exit, exit, exit rt - thread); +#endif /* RT_USING_FINSH */ + +/** + * This function will initial win32 + */ +void rt_hw_board_init() +{ + /* init system memory */ + heap = rt_hw_sram_init(); + +#if defined(RT_USING_USART) + rt_hw_usart_init(); +#endif + +#if defined(RT_USING_CONSOLE) + rt_hw_serial_init(); + rt_console_set_device(RT_CONSOLE_DEVICE_NAME); +#endif +} +/*@}*/ diff --git a/bsp/simlinux/drivers/board.h b/bsp/simlinux/drivers/board.h new file mode 100755 index 0000000000..7edb856505 --- /dev/null +++ b/bsp/simlinux/drivers/board.h @@ -0,0 +1,33 @@ +/* + * File : board.h + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2009, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-09-22 Bernard add board.h to this bsp + */ + +#ifndef __BOARD_H__ +#define __BOARD_H__ +void rt_hw_board_init(void); +rt_uint8_t *rt_hw_sram_init(void); + +/* SD Card init function */ +void rt_hw_sdcard_init(void); + +int rt_hw_mtd_nand_init(void); +int sst25vfxx_mtd_init(const char *, unsigned int , unsigned int); +void pcap_netif_hw_init(void); +void rt_platform_init(void); +void rt_hw_usart_init(void); +void rt_hw_serial_init(void); +void rt_hw_sdl_start(void); +void rt_hw_win32_low_cpu(void); + +void rt_hw_exit(void); +#endif diff --git a/bsp/simlinux/drivers/nanddrv_file.c b/bsp/simlinux/drivers/nanddrv_file.c new file mode 100755 index 0000000000..770751ac79 --- /dev/null +++ b/bsp/simlinux/drivers/nanddrv_file.c @@ -0,0 +1,399 @@ +#include +#include +#include +#include + +#define NAND_SIM "nand.bin" +#if 1 +#define OOB_SIZE 64 +#define PAGE_DATA_SIZE 2048 +#define PAGE_SIZE (2048 + 64) +#define PAGE_PER_BLOCK 64 +#define BLOCK_SIZE (PAGE_SIZE * PAGE_PER_BLOCK) +#define BLOCK_NUM 512 +// #define BLOCK_NUM 2048 +#else +#define OOB_SIZE 16 +#define PAGE_SIZE (512 + OOB_SIZE) +#define PAGE_PER_BLOCK 32 +#define BLOCK_SIZE (PAGE_SIZE * PAGE_PER_BLOCK) +#define BLOCK_NUM 512 +#endif + +#define ECC_SIZE ((PAGE_DATA_SIZE) * 3 / 256) + +static unsigned char block_data[BLOCK_SIZE]; +static struct rt_mtd_nand_device _nanddrv_file_device; +static FILE *file = NULL; + +static rt_uint8_t CountBitsInByte(rt_uint8_t byte) +{ + rt_uint8_t count = 0; + + while (byte > 0) + { + if (byte & 1) + { + count++; + } + byte >>= 1; + } + + return count; +} + +static void Compute256(const rt_uint8_t *data, rt_uint8_t *code) +{ + rt_uint32_t i; + rt_uint8_t columnSum = 0; + rt_uint8_t evenLineCode = 0; + rt_uint8_t oddLineCode = 0; + rt_uint8_t evenColumnCode = 0; + rt_uint8_t oddColumnCode = 0; + + // Xor all bytes together to get the column sum; + // At the same time, calculate the even and odd line codes + for (i = 0; i < 256; i++) + { + columnSum ^= data[i]; + + // If the xor sum of the byte is 0, then this byte has no incidence on + // the computed code; so check if the sum is 1. + if ((CountBitsInByte(data[i]) & 1) == 1) + { + // Parity groups are formed by forcing a particular index bit to 0 + // (even) or 1 (odd). + // Example on one byte: + // + // bits (dec) 7 6 5 4 3 2 1 0 + // (bin) 111 110 101 100 011 010 001 000 + // '---'---'---'----------. + // | + // groups P4' ooooooooooooooo eeeeeeeeeeeeeee P4 | + // P2' ooooooo eeeeeee ooooooo eeeeeee P2 | + // P1' ooo eee ooo eee ooo eee ooo eee P1 | + // | + // We can see that: | + // - P4 -> bit 2 of index is 0 --------------------' + // - P4' -> bit 2 of index is 1. + // - P2 -> bit 1 of index if 0. + // - etc... + // We deduce that a bit position has an impact on all even Px if + // the log2(x)nth bit of its index is 0 + // ex: log2(4) = 2, bit2 of the index must be 0 (-> 0 1 2 3) + // and on all odd Px' if the log2(x)nth bit of its index is 1 + // ex: log2(2) = 1, bit1 of the index must be 1 (-> 0 1 4 5) + // + // As such, we calculate all the possible Px and Px' values at the + // same time in two variables, evenLineCode and oddLineCode, such as + // evenLineCode bits: P128 P64 P32 P16 P8 P4 P2 P1 + // oddLineCode bits: P128' P64' P32' P16' P8' P4' P2' P1' + // + evenLineCode ^= (255 - i); + oddLineCode ^= i; + } + } + + // At this point, we have the line parities, and the column sum. First, We + // must caculate the parity group values on the column sum. + for (i = 0; i < 8; i++) + { + if (columnSum & 1) + { + evenColumnCode ^= (7 - i); + oddColumnCode ^= i; + } + columnSum >>= 1; + } + + // Now, we must interleave the parity values, to obtain the following layout: + // Code[0] = Line1 + // Code[1] = Line2 + // Code[2] = Column + // Line = Px' Px P(x-1)- P(x-1) ... + // Column = P4' P4 P2' P2 P1' P1 PadBit PadBit + code[0] = 0; + code[1] = 0; + code[2] = 0; + + for (i = 0; i < 4; i++) + { + code[0] <<= 2; + code[1] <<= 2; + code[2] <<= 2; + + // Line 1 + if ((oddLineCode & 0x80) != 0) + { + code[0] |= 2; + } + + if ((evenLineCode & 0x80) != 0) + { + code[0] |= 1; + } + + // Line 2 + if ((oddLineCode & 0x08) != 0) + { + code[1] |= 2; + } + + if ((evenLineCode & 0x08) != 0) + { + code[1] |= 1; + } + + // Column + if ((oddColumnCode & 0x04) != 0) + { + code[2] |= 2; + } + + if ((evenColumnCode & 0x04) != 0) + { + code[2] |= 1; + } + + oddLineCode <<= 1; + evenLineCode <<= 1; + oddColumnCode <<= 1; + evenColumnCode <<= 1; + } + + // Invert codes (linux compatibility) + code[0] = (~(rt_uint32_t)code[0]); + code[1] = (~(rt_uint32_t)code[1]); + code[2] = (~(rt_uint32_t)code[2]); +} + +void ecc_hamming_compute256x(const rt_uint8_t *pucData, rt_uint32_t dwSize, rt_uint8_t *puCode) +{ + while (dwSize > 0) + { + Compute256(pucData, puCode) ; + + pucData += 256; + puCode += 3; + dwSize -= 256; + } +} + +/* read chip id */ +static rt_uint32_t nanddrv_file_read_id(struct rt_mtd_nand_device *device) +{ + return 0x00; +} + +/* read/write/move page */ +static rt_err_t nanddrv_file_read_page(struct rt_mtd_nand_device *device, + rt_off_t page, + rt_uint8_t *data, rt_uint32_t data_len, + rt_uint8_t *spare, rt_uint32_t spare_len) +{ + rt_uint32_t offset; + rt_uint8_t oob_ecc [ECC_SIZE]; + rt_uint8_t ecc [ECC_SIZE]; + + page = page + device->block_start * device->pages_per_block; + + if (page / device->pages_per_block > device->block_end) + { + return -RT_EIO; + } + + /* write page */ + offset = page * PAGE_SIZE; + if (data != NULL && data_len != 0) + { + fseek(file, offset, SEEK_SET); + fread(data, data_len, 1, file); + if (data_len == PAGE_DATA_SIZE) + { + /* read ecc size */ + fread(oob_ecc, ECC_SIZE, 1, file); + + /* verify ECC */ + ecc_hamming_compute256x(data, PAGE_DATA_SIZE, &ecc[0]); + if (memcmp(&oob_ecc[0], &ecc[0], ECC_SIZE) != 0) + return -RT_MTD_EECC; + } + } + + if (spare != NULL && spare_len) + { + offset = page * PAGE_SIZE + PAGE_DATA_SIZE; + fseek(file, offset, SEEK_SET); + fread(spare, spare_len, 1, file); + } + + return RT_EOK; +} + +static rt_err_t nanddrv_file_write_page(struct rt_mtd_nand_device *device, + rt_off_t page, + const rt_uint8_t *data, rt_uint32_t data_len, + const rt_uint8_t *oob, rt_uint32_t spare_len) +{ + rt_uint32_t offset; + rt_uint8_t ecc[ECC_SIZE]; + + page = page + device->block_start * device->pages_per_block; + if (page / device->pages_per_block > device->block_end) + { + return -RT_EIO; + } + + /* write page */ + offset = page * PAGE_SIZE; + if (data != RT_NULL && data_len != 0) + { + fseek(file, offset, SEEK_SET); + fwrite(data, data_len, 1, file); + + if (data_len == PAGE_DATA_SIZE) + { + /*write the ecc information */ + ecc_hamming_compute256x(data, PAGE_DATA_SIZE, ecc); + + fwrite(ecc, ECC_SIZE, 1, file); + } + } + + if (oob != RT_NULL && spare_len != 0) + { + offset = page * PAGE_SIZE + PAGE_DATA_SIZE + ECC_SIZE; + fseek(file, offset, SEEK_SET); + fwrite(&oob[ECC_SIZE], spare_len-ECC_SIZE, 1, file); + } + + return RT_EOK; +} + +static rt_err_t nanddrv_file_move_page(struct rt_mtd_nand_device *device, rt_off_t from, rt_off_t to) +{ + rt_uint32_t offset; + rt_uint8_t page_buffer[PAGE_DATA_SIZE]; + rt_uint8_t oob_buffer[OOB_SIZE]; + + from = from + device->block_start * device->pages_per_block; + to = to + device->block_start * device->pages_per_block; + + if (from / device->pages_per_block > device->block_end || + to / device->pages_per_block > device->block_end) + { + return -RT_EIO; + } + + if (device->plane_num > 1) + { + rt_uint32_t mask; + rt_uint16_t from_block, to_block; + + from_block = (rt_uint16_t)(from / PAGE_PER_BLOCK); + to_block = (rt_uint16_t)(to / PAGE_PER_BLOCK); + mask = device->plane_num - 1; + + if ((from_block & mask) != (to_block & mask)) + { + rt_kprintf("invalid page copy on the block. from [%d] --> to[%d]\n", from_block, to_block); + return -RT_EIO; + } + } + + /* read page */ + offset = from * PAGE_SIZE; + fseek(file, offset, SEEK_SET); + fread(page_buffer, sizeof(page_buffer), 1, file); + fread(oob_buffer, sizeof(oob_buffer), 1, file); + + /* write page */ + offset = to * PAGE_SIZE; + fseek(file, offset, SEEK_SET); + fwrite(page_buffer, sizeof(page_buffer), 1, file); + fwrite(oob_buffer, sizeof(oob_buffer), 1, file); + + return RT_EOK; +} + +/* erase block */ +static rt_err_t nanddrv_file_erase_block(struct rt_mtd_nand_device *device, rt_uint32_t block) +{ + if (block > BLOCK_NUM) return -RT_EIO; + + /* add the start blocks */ + block = block + device->block_start; + + fseek(file, block * BLOCK_SIZE, SEEK_SET); + fwrite(block_data, sizeof(block_data), 1, file); + + return RT_EOK; +} + +const static struct rt_mtd_nand_driver_ops _ops = +{ + nanddrv_file_read_id, + nanddrv_file_read_page, + nanddrv_file_write_page, + nanddrv_file_move_page, + nanddrv_file_erase_block, + RT_NULL, + RT_NULL, +}; + +void nand_eraseall(void); + +void rt_hw_mtd_nand_init(void) +{ + rt_uint16_t ecc_size; + rt_uint32_t size; + + memset(block_data, 0xff, sizeof(block_data)); + /* open file */ + file = fopen(NAND_SIM, "rb+"); + if (file == NULL) + { + file = fopen(NAND_SIM, "wb+"); + } + fseek(file, 0, SEEK_END); + size = ftell(file); + + fseek(file, 0, SEEK_SET); + if (size < BLOCK_NUM * BLOCK_SIZE) + { + rt_uint32_t index; + fseek(file, 0, SEEK_SET); + for (index = 0; index < BLOCK_NUM; index ++) + { + fwrite(block_data, sizeof(block_data), 1, file); + } + } + fseek(file, 0, SEEK_SET); + + ecc_size = (PAGE_DATA_SIZE) * 3 / 256; + _nanddrv_file_device.plane_num = 2; + _nanddrv_file_device.oob_size = OOB_SIZE; + _nanddrv_file_device.oob_free = OOB_SIZE - ecc_size; + _nanddrv_file_device.page_size = PAGE_DATA_SIZE; + _nanddrv_file_device.pages_per_block = PAGE_PER_BLOCK; + _nanddrv_file_device.block_start = 0; + _nanddrv_file_device.block_end = BLOCK_NUM / 2; + _nanddrv_file_device.block_total = _nanddrv_file_device.block_end - _nanddrv_file_device.block_start; + _nanddrv_file_device.ops = &_ops; + + rt_mtd_nand_register_device("nand0", &_nanddrv_file_device); +} + +#if defined(RT_USING_FINSH) +#include +void nand_eraseall() +{ + int index; + for (index = 0; index < _nanddrv_file_device.block_total; index ++) + { + nanddrv_file_erase_block(&_nanddrv_file_device, index); + } +} +FINSH_FUNCTION_EXPORT(nand_eraseall, erase all of block in the nand flash); + +#endif //RT_USING_FINSH diff --git a/bsp/simlinux/drivers/sd_sim.c b/bsp/simlinux/drivers/sd_sim.c new file mode 100755 index 0000000000..7c9844bae3 --- /dev/null +++ b/bsp/simlinux/drivers/sd_sim.c @@ -0,0 +1,193 @@ +#include +#include +#include +#include +#include + +// #define SD_TRACE rt_kprintf +#define SD_TRACE(...) + +//#define SDCARD_SIM "F:\\Project\\tools\\SDCARD" +#define SDCARD_SIM "sd.bin" +#define SDCARD_SIZE (16*1024*1024) //16M + +struct sdcard_device +{ + struct rt_device parent; + FILE *file; +}; +static struct sdcard_device _sdcard; + +#define SDCARD_DEVICE(device) (( struct sdcard_device*)(device)) + +static rt_mutex_t lock; + +/* RT-Thread device interface */ + +static rt_err_t rt_sdcard_init(rt_device_t dev) +{ + return RT_EOK; +} + +static rt_err_t rt_sdcard_open(rt_device_t dev, rt_uint16_t oflag) +{ + return RT_EOK; +} + +static rt_err_t rt_sdcard_close(rt_device_t dev) +{ + return RT_EOK; +} + +/* position: block page address, not bytes address + * buffer: + * size : how many blocks + */ +static rt_size_t rt_sdcard_read(rt_device_t device, rt_off_t position, void *buffer, rt_size_t size) +{ + struct sdcard_device *sd; + int result = 0; + + SD_TRACE("sd read: pos %d, size %d\n", position, size); + + rt_mutex_take(lock, RT_WAITING_FOREVER); + sd = SDCARD_DEVICE(device); + fseek(sd->file, position * SECTOR_SIZE, SEEK_SET); + + result = fread(buffer, size * SECTOR_SIZE, 1, sd->file); + if (result < 0) + goto _err; + + rt_mutex_release(lock); + return size; + +_err: + SD_TRACE("sd read errors!\n"); + rt_mutex_release(lock); + return 0; +} + +/* position: block page address, not bytes address + * buffer: + * size : how many blocks + */ +static rt_size_t rt_sdcard_write(rt_device_t device, rt_off_t position, const void *buffer, rt_size_t size) +{ + struct sdcard_device *sd; + int result = 0; + + SD_TRACE("sst write: pos %d, size %d\n", position, size); + + rt_mutex_take(lock, RT_WAITING_FOREVER); + sd = SDCARD_DEVICE(device); + fseek(sd->file, position * SECTOR_SIZE, SEEK_SET); + + result = fwrite(buffer, size * SECTOR_SIZE, 1, sd->file); + if (result < 0) + goto _err; + + rt_mutex_release(lock); + return size; + +_err: + SD_TRACE("sd write errors!\n"); + rt_mutex_release(lock); + return 0; +} + +static rt_err_t rt_sdcard_control(rt_device_t dev, rt_uint8_t cmd, void *args) +{ + struct sdcard_device *sd; + unsigned int size; + + RT_ASSERT(dev != RT_NULL); + + sd = SDCARD_DEVICE(dev); + + if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME) + { + struct rt_device_blk_geometry *geometry; + + geometry = (struct rt_device_blk_geometry *)args; + if (geometry == RT_NULL) return -RT_ERROR; + + geometry->bytes_per_sector = SECTOR_SIZE; + geometry->block_size = SECTOR_SIZE; + + fseek(sd->file, 0, SEEK_END); + size = ftell(sd->file); + + geometry->sector_count = size / SECTOR_SIZE; + } + return RT_EOK; +} + + +rt_err_t rt_hw_sdcard_init(const char *spi_device_name) +{ + int size; + struct sdcard_device *sd; + struct rt_device *device; + + sd = &_sdcard; + device = &(sd->parent); + + lock = rt_mutex_create("lock", RT_IPC_FLAG_FIFO); + + /* open sd card file, if not exist, then create it */ + sd->file = fopen(SDCARD_SIM, "rb+"); + if (sd->file == NULL) + { + /* create a file to simulate sd card */ + sd->file = fopen(SDCARD_SIM, "wb+"); + + fseek(sd->file, 0, SEEK_END); + size = ftell(sd->file); + + fseek(sd->file, 0, SEEK_SET); + if (size < SDCARD_SIZE) + { + int i; + unsigned char *ptr; + + ptr = (unsigned char *) malloc(1024 * 1024); + if (ptr == NULL) + { + SD_TRACE("malloc error, no memory!\n"); + return RT_ERROR; + } + memset(ptr, 0x0, 1024 * 1024); + + fseek(sd->file, 0, SEEK_SET); + + for (i = 0; i < (SDCARD_SIZE / (1024 * 1024)); i++) + fwrite(ptr, 1024 * 1024, 1, sd->file); + + free(ptr); + } + } + fseek(sd->file, 0, SEEK_SET); + + device->type = RT_Device_Class_Block; + device->init = rt_sdcard_init; + device->open = rt_sdcard_open; + device->close = rt_sdcard_close; + device->read = rt_sdcard_read; + device->write = rt_sdcard_write; + device->control = rt_sdcard_control; + device->user_data = NULL; + + rt_device_register(device, "sd0", + RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE); + + return RT_EOK; +} + +#ifdef RT_USING_FINSH +#include +void eraseall(void) +{ + printf("had not implemented yet!\n"); +} +FINSH_FUNCTION_EXPORT(eraseall, erase all block in SPI flash); +#endif diff --git a/bsp/simlinux/drivers/sdl_fb.c b/bsp/simlinux/drivers/sdl_fb.c new file mode 100755 index 0000000000..790797f2af --- /dev/null +++ b/bsp/simlinux/drivers/sdl_fb.c @@ -0,0 +1,304 @@ +#include + +#include +#include +#include + +#define SDL_SCREEN_WIDTH 800 +#define SDL_SCREEN_HEIGHT 480 + +struct sdlfb_device +{ + struct rt_device parent; + + SDL_Surface *screen; + rt_uint16_t width; + rt_uint16_t height; +}; +struct sdlfb_device _device; + +/* common device interface */ +static rt_err_t sdlfb_init(rt_device_t dev) +{ + return RT_EOK; +} +static rt_err_t sdlfb_open(rt_device_t dev, rt_uint16_t oflag) +{ + return RT_EOK; +} +static rt_err_t sdlfb_close(rt_device_t dev) +{ + SDL_Quit(); + return RT_EOK; +} + +static rt_mutex_t sdllock; +static rt_err_t sdlfb_control(rt_device_t dev, rt_uint8_t cmd, void *args) +{ + struct sdlfb_device *device; + + rt_mutex_take(sdllock, RT_WAITING_FOREVER); + device = (struct sdlfb_device *)dev; + RT_ASSERT(device != RT_NULL); + RT_ASSERT(device->screen != RT_NULL); + + switch (cmd) + { + case RTGRAPHIC_CTRL_GET_INFO: + { + struct rt_device_graphic_info *info; + + info = (struct rt_device_graphic_info *) args; + info->bits_per_pixel = 16; + info->pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565P; + info->framebuffer = device->screen->pixels; + info->width = device->screen->w; + info->height = device->screen->h; + } + break; + case RTGRAPHIC_CTRL_RECT_UPDATE: + { + struct rt_device_rect_info *rect; + rect = (struct rt_device_rect_info *)args; + + /* SDL_UpdateRect(_device.screen, rect->x, rect->y, rect->x + rect->w, rect->y + rect->h); */ + SDL_UpdateRect(_device.screen, 0, 0, device->width, device->height); + } + break; + case RTGRAPHIC_CTRL_SET_MODE: + { +#if 0 + struct rt_device_rect_info *rect; + + rect = (struct rt_device_rect_info *)args; + if ((_device.width == rect->width) && (_device.height == rect->height)) return -RT_ERROR; + + _device.width = rect->width; + _device.height = rect->height; + + if (_device.screen != RT_NULL) + { + SDL_FreeSurface(_device.screen); + + /* re-create screen surface */ + _device.screen = SDL_SetVideoMode(_device.width, _device.height, 16, SDL_SWSURFACE | SDL_DOUBLEBUF); + if (_device.screen == NULL) + { + fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError()); + exit(1); + } + + SDL_WM_SetCaption("RT-Thread/GUI Simulator", NULL); + } +#endif + } + break; + } + rt_mutex_release(sdllock); + return RT_EOK; +} + +static void sdlfb_hw_init(void) +{ + /* set video driver for VC++ debug */ + //_putenv("SDL_VIDEODRIVER=windib"); + + //if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO) < 0) + if (SDL_Init(SDL_INIT_EVERYTHING) < 0) + { + fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); + exit(1); + } + + _device.parent.init = sdlfb_init; + _device.parent.open = sdlfb_open; + _device.parent.close = sdlfb_close; + _device.parent.read = RT_NULL; + _device.parent.write = RT_NULL; + _device.parent.control = sdlfb_control; + + _device.width = SDL_SCREEN_WIDTH; + _device.height = SDL_SCREEN_HEIGHT; + _device.screen = SDL_SetVideoMode(_device.width, _device.height, 16, SDL_SWSURFACE | SDL_DOUBLEBUF); + if (_device.screen == NULL) + { + fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError()); + exit(1); + } + + SDL_WM_SetCaption("RT-Thread/GUI Simulator", NULL); + rt_device_register(RT_DEVICE(&_device), "sdl", RT_DEVICE_FLAG_RDWR); + + sdllock = rt_mutex_create("fb", RT_IPC_FLAG_FIFO); +} + +#include +#include +#include +#include +#include +#include +#include +#include + +static DWORD WINAPI sdl_loop(LPVOID lpParam) +{ + int quit = 0; + SDL_Event event; + int button_state = 0; + + rt_device_t device; + sdlfb_hw_init(); + + device = rt_device_find("sdl"); + rtgui_graphic_set_device(device); + + /* handle SDL event */ + while (!quit) + { + SDL_WaitEvent(&event); + + switch (event.type) + { + case SDL_MOUSEMOTION: +#if 0 + { + struct rtgui_event_mouse emouse; + emouse.parent.type = RTGUI_EVENT_MOUSE_MOTION; + emouse.parent.sender = RT_NULL; + emouse.wid = RT_NULL; + + emouse.x = ((SDL_MouseMotionEvent *)&event)->x; + emouse.y = ((SDL_MouseMotionEvent *)&event)->y; + + /* init mouse button */ + emouse.button = button_state; + + /* send event to server */ + rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse)); + } +#endif + break; + + case SDL_MOUSEBUTTONDOWN: + case SDL_MOUSEBUTTONUP: + { + struct rtgui_event_mouse emouse; + SDL_MouseButtonEvent *mb; + + emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON; + emouse.parent.sender = RT_NULL; + emouse.wid = RT_NULL; + + mb = (SDL_MouseButtonEvent *)&event; + + emouse.x = mb->x; + emouse.y = mb->y; + + /* init mouse button */ + emouse.button = 0; + + /* set emouse button */ + if (mb->button & (1 << (SDL_BUTTON_LEFT - 1))) + { + emouse.button |= RTGUI_MOUSE_BUTTON_LEFT; + } + else if (mb->button & (1 << (SDL_BUTTON_RIGHT - 1))) + { + emouse.button |= RTGUI_MOUSE_BUTTON_RIGHT; + } + else if (mb->button & (1 << (SDL_BUTTON_MIDDLE - 1))) + { + emouse.button |= RTGUI_MOUSE_BUTTON_MIDDLE; + } + + if (mb->type == SDL_MOUSEBUTTONDOWN) + { + emouse.button |= RTGUI_MOUSE_BUTTON_DOWN; + button_state = emouse.button; + } + else + { + emouse.button |= RTGUI_MOUSE_BUTTON_UP; + button_state = 0; + } + + + /* send event to server */ + rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse)); + } + break; + + case SDL_KEYUP: + { + struct rtgui_event_kbd ekbd; + ekbd.parent.type = RTGUI_EVENT_KBD; + ekbd.parent.sender = RT_NULL; + ekbd.type = RTGUI_KEYUP; + ekbd.wid = RT_NULL; + ekbd.mod = event.key.keysym.mod; + ekbd.key = event.key.keysym.sym; + + /* FIXME: unicode */ + ekbd.unicode = 0; + + /* send event to server */ + rtgui_server_post_event(&ekbd.parent, sizeof(struct rtgui_event_kbd)); + } + break; + + case SDL_KEYDOWN: + { + struct rtgui_event_kbd ekbd; + ekbd.parent.type = RTGUI_EVENT_KBD; + ekbd.parent.sender = RT_NULL; + ekbd.type = RTGUI_KEYDOWN; + ekbd.wid = RT_NULL; + ekbd.mod = event.key.keysym.mod; + ekbd.key = event.key.keysym.sym; + + /* FIXME: unicode */ + ekbd.unicode = 0; + + /* send event to server */ + rtgui_server_post_event(&ekbd.parent, sizeof(struct rtgui_event_kbd)); + } + break; + + case SDL_QUIT: + SDL_Quit(); + quit = 1; + break; + + default: + break; + } + + if (quit) + break; + } + //exit(0); + return 0; +} + +/* start sdl thread */ +void rt_hw_sdl_start(void) +{ + HANDLE thread; + DWORD thread_id; + + /* create thread that loop sdl event */ + thread = CreateThread(NULL, + 0, + (LPTHREAD_START_ROUTINE)sdl_loop, + 0, + CREATE_SUSPENDED, + &thread_id); + if (thread == NULL) + { + //Display Error Message + + return; + } + ResumeThread(thread); +} diff --git a/bsp/simlinux/drivers/serial.c b/bsp/simlinux/drivers/serial.c new file mode 100755 index 0000000000..2bcdb15866 --- /dev/null +++ b/bsp/simlinux/drivers/serial.c @@ -0,0 +1,176 @@ +/* +****************************************************************************** +* By : parai +* email:parai@foxmail.com +* virtual serial driver +****************************************************************************** +*/ + +#include +#include + +#define _DEBUG_SERIAL 0 +#include "serial.h" +#include +struct rt_device serial_device; +//extern struct serial_int_rx serial_rx; +struct serial_int_rx serial_rx; + +#if 0 +static FILE *fp = RT_NULL; +#endif + +/*@{*/ + +/* RT-Thread Device Interface */ +/** + * This function initializes serial + */ +static rt_err_t rt_serial_init(rt_device_t dev) +{ + if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED)) + { + if (dev->flag & RT_DEVICE_FLAG_INT_RX) + { + rt_memset(serial_rx.rx_buffer, 0, + sizeof(serial_rx.rx_buffer)); + serial_rx.read_index = 0; + serial_rx.save_index = 0; + } + + dev->flag |= RT_DEVICE_FLAG_ACTIVATED; + } + return RT_EOK; +} + +static rt_err_t rt_serial_open(rt_device_t dev, rt_uint16_t oflag) +{ +#if _DEBUG_SERIAL==1 + printf("in rt_serial_open()\n"); +#endif + return RT_EOK; +} + +static rt_err_t rt_serial_close(rt_device_t dev) +{ +#if _DEBUG_SERIAL==1 + printf("in rt_serial_close()\n"); +#endif + return RT_EOK; +} +static rt_size_t rt_serial_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size) +{ + rt_uint8_t *ptr; + rt_err_t err_code; + + ptr = buffer; + err_code = RT_EOK; + + if (dev->flag & RT_DEVICE_FLAG_INT_RX) + { + /* interrupt mode Rx */ + while (size) + { + rt_base_t level; + + /* disable interrupt */ + level = rt_hw_interrupt_disable(); + + if (serial_rx.read_index != serial_rx.save_index) + { + /* read a character */ + *ptr++ = serial_rx.rx_buffer[serial_rx.read_index]; + size--; + + /* move to next position */ + serial_rx.read_index ++; + if (serial_rx.read_index >= SERIAL_RX_BUFFER_SIZE) + serial_rx.read_index = 0; + } + else + { + /* set error code */ + err_code = -RT_EEMPTY; + + /* enable interrupt */ + rt_hw_interrupt_enable(level); + break; + } + + /* enable interrupt */ + rt_hw_interrupt_enable(level); + } + } + + + /* set error code */ + rt_set_errno(err_code); + return (rt_uint32_t)ptr - (rt_uint32_t)buffer; +} + +static rt_size_t rt_serial_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size) +{ +#if _DEBUG_SERIAL==1 + printf("in rt_serial_write()\n"); +#endif +#if 0 + if (fp == NULL) + fp = fopen("log.txt", "wb+"); + + if (fp != NULL) + fwrite(buffer, size, 1, fp); +#endif + + printf("%s", (char *)buffer); + return size; +} + +static rt_err_t rt_serial_control(rt_device_t dev, rt_uint8_t cmd, void *args) +{ + RT_ASSERT(dev != RT_NULL); + + switch (cmd) + { + case RT_DEVICE_CTRL_SUSPEND: + /* suspend device */ + dev->flag |= RT_DEVICE_FLAG_SUSPENDED; + break; + + case RT_DEVICE_CTRL_RESUME: + /* resume device */ + dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED; + break; + } + + return RT_EOK; +} + +/* + * serial register + */ +static rt_err_t rt_hw_serial_register(rt_device_t device, const char *name, rt_uint32_t flag) +{ + RT_ASSERT(device != RT_NULL); +#if _DEBUG_SERIAL==1 + printf("in rt_serial_register()\n"); +#endif + device->type = RT_Device_Class_Char; + device->rx_indicate = RT_NULL; + device->tx_complete = RT_NULL; + device->init = rt_serial_init; + device->open = rt_serial_open; + device->close = rt_serial_close; + device->read = rt_serial_read; + device->write = rt_serial_write; + device->control = rt_serial_control; + device->user_data = RT_NULL; + + /* register a character device */ + return rt_device_register(device, name, (rt_uint16_t)(RT_DEVICE_FLAG_RDWR | flag)); +} + +rt_err_t rt_hw_serial_init(void) +{ + return rt_hw_serial_register(&serial_device, RT_CONSOLE_DEVICE_NAME, + RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM); +} diff --git a/bsp/simlinux/drivers/serial.h b/bsp/simlinux/drivers/serial.h new file mode 100755 index 0000000000..010631ab32 --- /dev/null +++ b/bsp/simlinux/drivers/serial.h @@ -0,0 +1,22 @@ +/* +********************************************************************************************************* +* MC9S12DP256/DG128 Specific code +* BANKED MEMORY MODEL +* +* File : rthw.c +* By : parai +* email:parai@foxmail.com +*******************************************************************************************************/ + +#ifndef __RT_HW_SERIAL_H__ +#define __RT_HW_SERIAL_H__ + +#define SERIAL_RX_BUFFER_SIZE 80 +struct serial_int_rx +{ + rt_uint8_t rx_buffer[SERIAL_RX_BUFFER_SIZE]; + rt_uint32_t read_index, save_index; +}; + +rt_err_t rt_hw_serial_init(void); +#endif diff --git a/bsp/simlinux/drivers/sst25vfxx_mtd.h b/bsp/simlinux/drivers/sst25vfxx_mtd.h new file mode 100755 index 0000000000..cb0bb84129 --- /dev/null +++ b/bsp/simlinux/drivers/sst25vfxx_mtd.h @@ -0,0 +1,24 @@ +/* + * File : sst25vfxx_mtd.h + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2011-12-16 aozima the first version + * 2012-02-01 mbbill MTD driver version + */ + +#ifndef SST25VFXX_MTD_H +#define SST25VFXX_MTD_H + +#include +#include + +rt_err_t sst25vfxx_mtd_init(const char *spi_device_name, rt_uint32_t block_start, rt_uint32_t block_end); + +#endif diff --git a/bsp/simlinux/drivers/sst25vfxx_mtd_sim.c b/bsp/simlinux/drivers/sst25vfxx_mtd_sim.c new file mode 100755 index 0000000000..9271c1bb03 --- /dev/null +++ b/bsp/simlinux/drivers/sst25vfxx_mtd_sim.c @@ -0,0 +1,227 @@ +/* + * File : rtdef.h + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2012-10-21 prife the first version + */ + +#include +#include +#include +#include +#include "sst25vfxx_mtd.h" + +#ifdef RT_USING_MTD_NOR +#define NOR_SIM "nor.bin" +/* JEDEC Manufacturers ID */ +#define MF_ID (0xBF) +/* JEDEC Device ID : Memory Type */ +#define MT_ID (0x25) +/* JEDEC Device ID: Memory Capacity */ +#define MC_ID_SST25VF016 (0x41) +#define MC_ID_SST25VF032 (0x4A) +#define MC_ID_SST25VF064 (0x4B) + +#define BLOCK_SIZE (64*1024) + + +#define SST25_MTD(device) ((struct sst25_mtd*)(device)) +struct sst25_mtd +{ + struct rt_mtd_nor_device parent; + FILE *file; +}; +static struct sst25_mtd _sst25_mtd; + +static struct rt_mutex flash_lock; + +/* RT-Thread MTD device interface */ +static rt_uint32_t sst25vfxx_read_id(struct rt_mtd_nor_device *device) +{ + rt_uint8_t id_recv[3] = {MF_ID, MT_ID, MC_ID_SST25VF016}; + + return (id_recv[0] << 16) | (id_recv[1] << 8) | id_recv[2]; +} + +static int sst25vfxx_read(struct rt_mtd_nor_device *device, rt_off_t position, rt_uint8_t *data, rt_size_t size) +{ + struct sst25_mtd *sst25; + int result; + + sst25 = SST25_MTD(device); + RT_ASSERT(sst25 != RT_NULL); + + rt_mutex_take(&flash_lock, RT_WAITING_FOREVER); + + fseek(sst25->file, position, SEEK_SET); + result = fread(data, size, 1, sst25->file); + if (result < 0) + rt_kprintf("sst read error.\n"); + + rt_mutex_release(&flash_lock); + return size; +} + +static int sst25vfxx_write(struct rt_mtd_nor_device *device, rt_off_t position, + const rt_uint8_t *data, rt_size_t size) +{ + struct sst25_mtd *sst25; + int result; + + sst25 = SST25_MTD(device); + RT_ASSERT(sst25 != RT_NULL); + + rt_mutex_take(&flash_lock, RT_WAITING_FOREVER); + + fseek(sst25->file, position, SEEK_SET); + result = fwrite(data, size, 1, sst25->file); + if (result < 0) + rt_kprintf("sst write error.\n"); + + rt_mutex_release(&flash_lock); + return size; +} + +static char block_buffer[BLOCK_SIZE]; +static rt_err_t sst25vfxx_erase_block(struct rt_mtd_nor_device *device, rt_off_t offset, rt_uint32_t length) +{ + struct sst25_mtd *sst25; + int result; + + sst25 = SST25_MTD(device); + + RT_ASSERT(sst25 != RT_NULL); + + rt_mutex_take(&flash_lock, RT_WAITING_FOREVER); + + memset(block_buffer, 0xFF, BLOCK_SIZE); + fseek(sst25->file, offset, SEEK_SET); + + result = fwrite(block_buffer, BLOCK_SIZE, 1, sst25->file); + if (result < 0) + rt_kprintf("sst write error.\n"); + + rt_mutex_release(&flash_lock); + return RT_EOK; +} + +const static struct rt_mtd_nor_driver_ops sst25vfxx_mtd_ops = +{ + sst25vfxx_read_id, + sst25vfxx_read, + sst25vfxx_write, + sst25vfxx_erase_block, +}; +static rt_err_t sst25vfxx_hw_init(struct sst25_mtd *mtd) +{ + mtd = mtd; + return RT_EOK; +} + +/** + * SST25vfxx API + */ +rt_err_t sst25vfxx_mtd_init(const char *nor_name, + rt_uint32_t block_start, + rt_uint32_t block_end) +{ + rt_uint32_t id, total_block; + struct sst25_mtd *sst25; + struct rt_mtd_nor_device *mtd; + + + sst25 = &_sst25_mtd; + mtd = &(sst25->parent); + + /* set page size and block size */ + mtd->block_size = 64 * 1024; /* 64kByte */ + mtd->ops = &sst25vfxx_mtd_ops; + + /* initialize mutex */ + if (rt_mutex_init(&flash_lock, nor_name, RT_IPC_FLAG_FIFO) != RT_EOK) + { + rt_kprintf("init sd lock mutex failed\n"); + } + + /* initialize flash */ + id = sst25vfxx_read_id(mtd); + switch (id & 0xff) + { + case MC_ID_SST25VF016: + total_block = (16 * 1024 * 1024 / 8) / mtd->block_size; + break; + case MC_ID_SST25VF032: + total_block = (32 * 1024 * 1024 / 8) / mtd->block_size; + break; + case MC_ID_SST25VF064: + total_block = (64 * 1024 * 1024 / 8) / mtd->block_size; + break; + default: + rt_kprintf("SST25 detection error, id: %x\n", id); + return -RT_ERROR; + } + + if ((block_end == RT_UINT32_MAX) || (block_end == 0)) + { + block_end = total_block; + } + else if (block_end > total_block) + { + rt_kprintf("SST25 total block: %d, out of block\n", total_block); + return -RT_ERROR; + } + + mtd->block_start = block_start; + mtd->block_end = block_end; + + /* open nor file, if not exist, then create it */ + sst25->file = fopen(NOR_SIM, "rb+"); + if (sst25->file == NULL) + { + rt_uint32_t i; + /* create a file to simulate nor */ + sst25->file = fopen(NOR_SIM, "wb+"); + + memset(block_buffer, 0xFF, sizeof(block_buffer)); + for (i = 0; i < total_block; i++) + { + fseek(sst25->file, i * BLOCK_SIZE, SEEK_SET); + fwrite(block_buffer, BLOCK_SIZE, 1, sst25->file); + } + } + + fseek(sst25->file, 0, SEEK_SET); + + /* initialize hardware */ + sst25vfxx_hw_init(&_sst25_mtd); + + /* register MTD device */ + rt_mtd_nor_register_device("nor", mtd); + + return RT_EOK; +} + +#ifdef RT_USING_FINSH +#include +void nor_erase(void) +{ + rt_uint32_t index; + struct rt_mtd_nor_device *mtd; + + mtd = RT_MTD_NOR_DEVICE(&_sst25_mtd); + for (index = mtd->block_start; index < mtd->block_end; index ++) + { + sst25vfxx_erase_block(mtd, index * mtd->block_size, BLOCK_SIZE); + } +} +FINSH_FUNCTION_EXPORT(nor_erase, erase all block in SPI flash); +#endif + +#endif diff --git a/bsp/simlinux/drivers/usart_sim.c b/bsp/simlinux/drivers/usart_sim.c new file mode 100755 index 0000000000..6a2564786a --- /dev/null +++ b/bsp/simlinux/drivers/usart_sim.c @@ -0,0 +1,138 @@ +#include +#include + +#ifdef _WIN32 +#include +#include +#endif + +#include +#include + +#include "serial.h" + +struct serial_int_rx serial_rx; +extern struct rt_device serial_device; + +/* + * Handler for OSKey Thread + */ +static HANDLE OSKey_Thread; +static DWORD OSKey_ThreadID; + +static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam); +void rt_hw_usart_init(void) +{ + + /* + * create serial thread that receive key input from keyboard + */ + + OSKey_Thread = CreateThread(NULL, + 0, + (LPTHREAD_START_ROUTINE)ThreadforKeyGet, + 0, + CREATE_SUSPENDED, + &OSKey_ThreadID); + if (OSKey_Thread == NULL) + { + //Display Error Message + + return; + } + SetThreadPriority(OSKey_Thread, + THREAD_PRIORITY_NORMAL); + SetThreadPriorityBoost(OSKey_Thread, + TRUE); + SetThreadAffinityMask(OSKey_Thread, + 0x01); + /* + * Start OS get key Thread + */ + ResumeThread(OSKey_Thread); + +} + +/* + * () 0xe04b + * () 0xe048 + * () 0xe04d + * () 0xe050 + */ +static int savekey(unsigned char key) +{ + /* save on rx buffer */ + { + rt_base_t level; + + /* disable interrupt */ + //ʱرжϣΪҪuartݽṹ + level = rt_hw_interrupt_disable(); + + /* save character */ + serial_rx.rx_buffer[serial_rx.save_index] = key; + serial_rx.save_index ++; + //Ĵsave_indexǷѾβתͷΪһλ + if (serial_rx.save_index >= SERIAL_RX_BUFFER_SIZE) + serial_rx.save_index = 0; + + //ʾתsave_index׷read_indexread_indexһɵ + /* if the next position is read index, discard this 'read char' */ + if (serial_rx.save_index == serial_rx.read_index) + { + serial_rx.read_index ++; + if (serial_rx.read_index >= SERIAL_RX_BUFFER_SIZE) + serial_rx.read_index = 0; + } + + /* enable interrupt */ + //uartݽṹѾɣʹж + rt_hw_interrupt_enable(level); + } + + /* invoke callback */ + if (serial_device.rx_indicate != RT_NULL) + { + rt_size_t rx_length; + + /* get rx length */ + rx_length = serial_rx.read_index > serial_rx.save_index ? + SERIAL_RX_BUFFER_SIZE - serial_rx.read_index + serial_rx.save_index : + serial_rx.save_index - serial_rx.read_index; + + serial_device.rx_indicate(&serial_device, rx_length); + } + return 0; +} +static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam) +{ + unsigned char key; + + (void)lpParam; //prevent compiler warnings + + for (;;) + { + key = getch(); + if (key == 0xE0) + { + key = getch(); + + if (key == 0x48) //up key , 0x1b 0x5b 0x41 + { + savekey(0x1b); + savekey(0x5b); + savekey(0x41); + } + else if (key == 0x50)//0x1b 0x5b 0x42 + { + savekey(0x1b); + savekey(0x5b); + savekey(0x42); + } + + continue; + } + + savekey(key); + } +} /*** ThreadforKeyGet ***/ diff --git a/bsp/simlinux/rtconfig.h b/bsp/simlinux/rtconfig.h new file mode 100755 index 0000000000..ae213813ec --- /dev/null +++ b/bsp/simlinux/rtconfig.h @@ -0,0 +1,228 @@ +/* RT-Thread config file */ +#ifndef __RTTHREAD_CFG_H__ +#define __RTTHREAD_CFG_H__ + +#define RT_HEAP_SIZE (1024*1024*2) + +#if defined(_MSC_VER) +/* SECTION: port for visual studio */ +#undef RT_USING_NEWLIB +#undef RT_USING_MINILIBC +#define NORESOURCE //RT_VESRION in winuser.h +#define _CRT_ERRNO_DEFINED //errno macro redefinition + +/* disable some warning in MSC */ +#pragma warning(disable:4273) /* to ignore: warning C4273: inconsistent dll linkage */ +#pragma warning(disable:4312) /* to ignore: warning C4312: 'type cast' : conversion from 'rt_uint32_t' to 'rt_uint32_t *' */ +#pragma warning(disable:4311) /* to ignore: warning C4311: 'type cast' : pointer truncation from 'short *__w64 ' to 'long' */ +#pragma warning(disable:4996) /* to ignore: warning C4996: The POSIX name for this item is deprecated. */ +#pragma warning(disable:4267) /* to ignore: warning C4267: conversion from 'size_t' to 'rt_size_t', possible loss of data */ +#pragma warning(disable:4244) /* to ignore: warning C4244: '=' : conversion from '__w64 int' to 'rt_size_t', possible loss of data */ + +#elif defined(__GNUC__) +#define RT_USING_NOLIBC +#endif + +/* SECTION: basic kernel options */ +/* RT_NAME_MAX*/ +#define RT_NAME_MAX 8 + +/* RT_ALIGN_SIZE*/ +#define RT_ALIGN_SIZE 4 + +/* PRIORITY_MAX */ +#define RT_THREAD_PRIORITY_MAX 32 + +/* Tick per Second */ +#define RT_TICK_PER_SECOND 2 + +/* SECTION: RT_DEBUG */ +/* Thread Debug */ +#define RT_DEBUG +//#define RT_DEBUG_SCHEDULER 1 +#define RT_THREAD_DEBUG + +#define RT_USING_OVERFLOW_CHECK + +/* Using Hook */ +#define RT_USING_HOOK + +/* Using Software Timer */ +/* #define RT_USING_TIMER_SOFT */ +#define RT_TIMER_THREAD_PRIO 4 +#define RT_TIMER_THREAD_STACK_SIZE 512 +#define RT_TIMER_TICK_PER_SECOND 10 + +/* SECTION: IPC */ +/* Using Semaphore*/ +#define RT_USING_SEMAPHORE + +/* Using Mutex */ +#define RT_USING_MUTEX + +/* Using Event */ +#define RT_USING_EVENT + +/* Using MailBox */ +#define RT_USING_MAILBOX + +/* Using Message Queue */ +#define RT_USING_MESSAGEQUEUE + +/* SECTION: Memory Management */ +/* Using Memory Pool Management*/ +/* #define RT_USING_MEMPOOL */ + +/* Using Dynamic Heap Management */ +#define RT_USING_HEAP + +/* Using Small MM */ +#define RT_USING_SMALL_MEM +/* #define RT_TINY_SIZE */ + +/* SECTION: Device System */ +/* Using Device System */ +#define RT_USING_DEVICE +/* #define RT_USING_UART1 */ + +/* SECTION: Console options */ +#define RT_USING_CONSOLE +/* the buffer size of console*/ +#define RT_CONSOLEBUF_SIZE 128 +#define RT_CONSOLE_DEVICE_NAME "sci0" + +/* SECTION: component options */ +#define RT_USING_COMPONENTS_INIT + +/* SECTION: MTD interface options */ +/* using mtd nand flash */ +/* #define RT_USING_MTD_NAND */ +/* using mtd nor flash */ +/* #define RT_USING_MTD_NOR */ + +/* SECTION: finsh, a C-Express shell */ +/* #define RT_USING_FINSH */ +/* Using symbol table */ +#define FINSH_USING_SYMTAB +#define FINSH_USING_DESCRIPTION + +/* SECTION: device file system */ +/* #define RT_USING_DFS */ +#define DFS_FILESYSTEM_TYPES_MAX 8 + +/* DFS: ELM FATFS options */ +#define RT_USING_DFS_ELMFAT +#define RT_DFS_ELM_WORD_ACCESS +/* Reentrancy (thread safe) of the FatFs module. */ +#define RT_DFS_ELM_REENTRANT +/* Number of volumes (logical drives) to be used. */ +#define RT_DFS_ELM_DRIVES 2 +/* #define RT_DFS_ELM_USE_LFN 1 */ +#define RT_DFS_ELM_MAX_LFN 255 +/* Maximum sector size to be handled. */ +#define RT_DFS_ELM_MAX_SECTOR_SIZE 512 + +/* DFS: network file system options */ +/* #define RT_USING_DFS_NFS */ + +/* DFS: UFFS nand file system options */ +#define RT_USING_DFS_UFFS +/* configuration for uffs, more to see dfs_uffs.h and uffs_config.h */ +#define RT_CONFIG_UFFS_ECC_MODE UFFS_ECC_HW_AUTO +/* enable this ,you need provide a mark_badblock/check_block function */ +/* #define RT_UFFS_USE_CHECK_MARK_FUNCITON */ + +/* DFS: JFFS2 nor flash file system options */ +#define RT_USING_DFS_JFFS2 + +/* DFS: windows share directory mounted to rt-thread/dfs */ +/* only used in bsp/simulator */ +#define RT_USING_DFS_WINSHAREDIR + +/* the max number of mounted file system */ +#define DFS_FILESYSTEMS_MAX 4 +/* the max number of opened files */ +#define DFS_FD_MAX 4 + +/* SECTION: lwip, a lightweight TCP/IP protocol stack */ +/* #define RT_USING_LWIP */ +/* LwIP uses RT-Thread Memory Management */ +#define RT_LWIP_USING_RT_MEM +/* Enable ICMP protocol*/ +#define RT_LWIP_ICMP +/* Enable UDP protocol*/ +#define RT_LWIP_UDP +/* Enable TCP protocol*/ +#define RT_LWIP_TCP +/* Enable DNS */ +#define RT_LWIP_DNS + +/* the number of simultaneously active TCP connections*/ +#define RT_LWIP_TCP_PCB_NUM 5 + +/* Using DHCP */ +/* #define RT_LWIP_DHCP */ + +/* ip address of target*/ +#define RT_LWIP_IPADDR0 192 +#define RT_LWIP_IPADDR1 168 +#define RT_LWIP_IPADDR2 126 +#define RT_LWIP_IPADDR3 30 + +/* gateway address of target*/ +#define RT_LWIP_GWADDR0 192 +#define RT_LWIP_GWADDR1 168 +#define RT_LWIP_GWADDR2 126 +#define RT_LWIP_GWADDR3 1 + +/* mask address of target*/ +#define RT_LWIP_MSKADDR0 255 +#define RT_LWIP_MSKADDR1 255 +#define RT_LWIP_MSKADDR2 255 +#define RT_LWIP_MSKADDR3 0 + +/* tcp thread options */ +#define RT_LWIP_TCPTHREAD_PRIORITY 12 +#define RT_LWIP_TCPTHREAD_MBOX_SIZE 10 +#define RT_LWIP_TCPTHREAD_STACKSIZE 1024 + +/* Ethernet if thread options */ +#define RT_LWIP_ETHTHREAD_PRIORITY 15 +#define RT_LWIP_ETHTHREAD_MBOX_SIZE 10 +#define RT_LWIP_ETHTHREAD_STACKSIZE 512 + +/* TCP sender buffer space */ +#define RT_LWIP_TCP_SND_BUF 8192 +/* TCP receive window. */ +#define RT_LWIP_TCP_WND 8192 + +/* SECTION: RT-Thread/GUI */ +/* #define RT_USING_RTGUI */ + +/* name length of RTGUI object */ +#define RTGUI_NAME_MAX 12 +/* support 16 weight font */ +#define RTGUI_USING_FONT16 +/* support Chinese font */ +#define RTGUI_USING_FONTHZ +/* use DFS as file interface */ +#define RTGUI_USING_DFS_FILERW +/* use font file as Chinese font */ +/* #define RTGUI_USING_HZ_FILE */ +/* use Chinese bitmap font */ +#define RTGUI_USING_HZ_BMP +/* use small size in RTGUI */ +#define RTGUI_USING_SMALL_SIZE +/* use mouse cursor */ +/* #define RTGUI_USING_MOUSE_CURSOR */ +/* default font size in RTGUI */ +#define RTGUI_DEFAULT_FONT_SIZE 16 + +/* image support */ +#define RTGUI_IMAGE_XPM +#define RTGUI_IMAGE_BMP +/* #define RTGUI_IMAGE_JPEG */ +/* #define RTGUI_IMAGE_PNG */ +#define RTGUI_USING_NOTEBOOK_IMAGE + +#endif diff --git a/bsp/simlinux/rtconfig.py b/bsp/simlinux/rtconfig.py new file mode 100755 index 0000000000..020649f329 --- /dev/null +++ b/bsp/simlinux/rtconfig.py @@ -0,0 +1,80 @@ +# toolchains options +ARCH='sim' +#CPU='win32' +#CPU='posix' +CPU='posix' +CROSS_TOOL='gcc' #msvc # gcc + +# lcd panel options +# 'FMT0371','ILI932X', 'SSD1289' +# RT_USING_LCD_TYPE = 'SSD1289' + +# cross_tool provides the cross compiler +# EXEC_PATH is the compiler execute path, for example, CodeSourcery, Keil MDK, IAR +if CROSS_TOOL == 'gcc': + PLATFORM = 'gcc' + EXEC_PATH = '/usr/bin/gcc' + +if CROSS_TOOL == 'msvc': + PLATFORM = 'cl' + EXEC_PATH = '' + +BUILD = 'debug' +#BUILD = '' + +if PLATFORM == 'gcc': + # toolchains + PREFIX = '' + CC = PREFIX + 'gcc' + AS = PREFIX + 'gcc' + AR = PREFIX + 'ar' + LINK = PREFIX + 'gcc' + TARGET_EXT = 'axf' + SIZE = PREFIX + 'size' + OBJDUMP = PREFIX + 'objdump' + OBJCPY = PREFIX + 'objcopy' + + DEVICE = ' -ffunction-sections -fdata-sections' + CFLAGS = DEVICE + ' -I/usr/include -w -D_REENTRANT' + AFLAGS = ' -c' + DEVICE + ' -x assembler-with-cpp' + #LFLAGS = DEVICE + ' -Wl,--gc-sections,-Map=rtthread-linux.map,-cref,-u,Reset_Handler -T stm32_rom.ld' + #LFLAGS = DEVICE + ' -Wl,--gc-sections,-Map=rtthread-linux.map -lpthread' + LFLAGS = DEVICE + ' -Wl,--gc-sections,-Map=rtthread-linux.map -pthread' + + CPATH = '' + LPATH = '' + + if BUILD == 'debug': + CFLAGS += ' -g -O0 -gdwarf-2' + AFLAGS += ' -gdwarf-2' + else: + CFLAGS += ' -O2' + + POST_ACTION = OBJCPY + ' -O binary $TARGET rtthread.bin\n' + SIZE + ' $TARGET \n' + +elif PLATFORM == 'cl': + # toolchains + PREFIX = '' + TARGET_EXT = 'exe' + AS = PREFIX + 'cl' + CC = PREFIX + 'cl' + AR = PREFIX + 'cl' + LINK = PREFIX + 'cl' + AFLAGS = '' + CFLAGS = '' + LFLAGS = '' + + if BUILD == 'debug': + CFLAGS += ' /MTd' + LFLAGS += ' /DEBUG' + else: + CFLAGS += ' /MT' + LFLAGS += '' + + CFLAGS += ' /ZI /Od /W 3 /WL ' + LFLAGS += ' /SUBSYSTEM:CONSOLE /MACHINE:X86 ' + + CPATH = '' + LPATH = '' + + POST_ACTION = '' From 5685a395be4bbbf8363a19a5c380f6c35e075474 Mon Sep 17 00:00:00 2001 From: prife Date: Mon, 14 Jan 2013 01:23:08 +0800 Subject: [PATCH 03/19] add some comments and clean code in cpu_port.c --- libcpu/sim/posix/cpu_port.c | 121 ++++++++++++++++++------------------ 1 file changed, 59 insertions(+), 62 deletions(-) mode change 100755 => 100644 libcpu/sim/posix/cpu_port.c diff --git a/libcpu/sim/posix/cpu_port.c b/libcpu/sim/posix/cpu_port.c old mode 100755 new mode 100644 index 919abc5209..ca25747c5a --- a/libcpu/sim/posix/cpu_port.c +++ b/libcpu/sim/posix/cpu_port.c @@ -1,3 +1,8 @@ +/* + * author : prife (goprife@gmail.com) + * date : 2013/01/14 01:18:50 + * version: v 0.1.0 + */ #include #include #include @@ -10,7 +15,6 @@ //#define TRACE printf #define TRACE(...) -#define _DEBUG typedef struct _thread { @@ -31,10 +35,11 @@ typedef struct _thread #define TIMER_TYPE ITIMER_REAL #define MAX_INTERRUPT_NUM ((unsigned int)sizeof(unsigned int) * 8) +#define INTERRUPT_ENABLE 0 +#define INTERRUPT_DISABLE 1 +/* interrupt flag, if 1, disable, if 0, enable */ +static long interrupt_disable_flag; -/* #define INT_ENABLE 0 - * #define INT_DISABLE 1 - */ /* flag in interrupt handling */ rt_uint32_t rt_interrupt_from_thread, rt_interrupt_to_thread; rt_uint32_t rt_thread_switch_interrupt_flag; @@ -181,29 +186,28 @@ rt_uint8_t *rt_hw_stack_init( return (rt_uint8_t *) thread; } +static int dis_count; +static int en_count; -/* interrupt contex switch hit, value 1 or 0 */ -static long int_cs_hit; - +/* TODO: 此函数还没有真正实现 */ rt_base_t rt_hw_interrupt_disable(void) { - if (ptr_int_mutex != NULL) - { -// pthread_mutex_lock(ptr_int_mutex); //FIXME - } + long back; - /*TODO: It may need to mask the signal */ - return 0; + back = interrupt_disable_flag; + interrupt_disable_flag = INTERRUPT_DISABLE; + + /*TODO: It may need to unmask the signal */ + dis_count++; + return back; } +/* TODO: 此函数还没有真正实现 */ void rt_hw_interrupt_enable(rt_base_t level) { - level = level; + interrupt_disable_flag = level; - if (ptr_int_mutex != NULL) - { -// pthread_mutex_unlock(ptr_int_mutex); //FIXME - } + en_count++; /*TODO: It may need to unmask the signal */ } @@ -224,6 +228,7 @@ void rt_hw_context_switch(rt_uint32_t from, RT_ASSERT(from != to); #if 0 + //TODO: 还需要考虑嵌套切换的情况 if (rt_thread_switch_interrupt_flag != 1) { rt_thread_switch_interrupt_flag = 1; @@ -242,19 +247,32 @@ void rt_hw_context_switch(rt_uint32_t from, tid = rt_thread_self(); pid = pthread_self(); - /* 注意,只有两种可可能,一种是线程函数中调用此函数,一种是在中断中调用 - * 而在中断调用是在主线程的信号处理函数中实现(目前只有tick中断)。 - * 即, 只有如下两种可能: - * 1)普通RTT线程间接调用,如rt_thread_delay函数 - * 2)或者主线程信号处理函数,如rt_tick_increase中调用 - */ + /* + * FIXME: 这段代码应该删除,因为rt_schedule函数中是先关闭中断,然后再开启的 + * 开启就会出发PendSV(以stm32为例)中断,cpu_port.c的win32版本可以模拟这种 + * 情形,因为它的中断是在主线程代码实现的,而本设计中,切换分为两种情况, + * 1) 普通rtt线程调用rt_thread_delay/rt_sem_take等主动挂起时,即刻实现切换 + * 2) 当rt_thread_delay定时到期时,主线程的SIGALRM信号处理函数中实现线程切换 + * + * 在第一种情况下,如果添加打开如下代码,由于if中语句为真,会导致失败退出。 + * + if (interrupt_disable_flag != 0) + { + printf("dis_count=%d, en_count=%d\n", dis_count, en_count); + printf("interrupt_disable_flag = %d\n", interrupt_disable_flag); + printf("bug! interrupt is disabled! You may forget enable interrupt\n"); + exit(EXIT_FAILURE); + } + */ + if (pid != mainthread_pid) { + /* FIXME: 注意这段代码是在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); - /* from线程就是当前rtt线程 */ /* 确定一下,这两个值一定是相等的! */ RT_ASSERT(thread_from->pthread == pid); @@ -263,14 +281,14 @@ void rt_hw_context_switch(rt_uint32_t from, sem_post(& thread_to->sem); /* 挂起from线程, 既然from线程就是当前线程,所以应该直接 - * 挂起在这里 - */ + * 挂起在这里 */ sem_wait(& thread_from->sem); } else { /* FIXME: 注意这段代码是在system tick 函数中执行的, - * 即此时位于主线程的SIGALRM信号处理函数中 */ + * 即此时位于主线程的SIGALRM信号处理函数中 + */ TRACE("conswitch: S in pid<%x> ,suspend <%s>, resume <%s>!\n", (unsigned int)pid, thread_from->rtthread->name, @@ -293,7 +311,11 @@ void rt_hw_context_switch_to(rt_uint32_t to) rt_interrupt_from_thread = 0; //set interrupt to 1 - rt_thread_switch_interrupt_flag = 0; //<------ + 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(); @@ -315,21 +337,19 @@ static int mainthread_scheduler(void) mainthread_pid = pthread_self(); TRACE("pid <%08x> mainthread\n", (unsigned int)(mainthread_pid)); - /* register interrupts which is simulated for yield and systick */ - //register_interrupt(CPU_INTERRUPT_YIELD, yield_interrupt_isr); - //register_interrupt(CPU_INTERRUPT_TICK, tick_interrupt_isr); - /* install signal handler of system tick */ signal_install(SIGALRM, mthread_signal_tick); /* install signal handler used to suspend itself */ signal_install(MSG_SUSPEND, thread_switch_handler); +#if 0 /* 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); pthread_mutex_init(ptr_int_mutex, &mutexattr); pthread_cond_init(&cond_int_hit, NULL); +#endif /* start timer */ start_sys_timer(); @@ -341,18 +361,6 @@ static int mainthread_scheduler(void) for (;;) { -#if 0 - pthread_mutex_lock(ptr_int_mutex); - /*Lock mutex and wait for signal. Note that the pthread_cond_wait - *routine will automatically and atomically unlock mutex while it waits. - */ - TRACE("mthread: wait cond val!\n"); - pthread_cond_wait(&cond_int_hit, ptr_int_mutex); - TRACE("mthread: got cond val!\n"); - - pthread_mutex_unlock(ptr_int_mutex); -#endif - //printf("main thread...\n"); sleep(1); } @@ -402,7 +410,13 @@ static void mthread_signal_tick(int sig) if (sig == SIGALRM) { TRACE("pid <%x> signal: SIGALRM enter!\n", (unsigned int)pid); + + /* FIXME: 下面这条语句不能打开,打开就会导致失败tick_interrupt_isry + * 永远无法执行 + */ + /* if (! interrupt_disable_flag) */ tick_interrupt_isr(); + TRACE("pid <%x> signal: SIGALRM leave!\n", (unsigned int)pid); } else @@ -428,20 +442,3 @@ static int tick_interrupt_isr(void) return 0; } -#if 0 - -static void trigger_interrupt(int index) -{ - if ((index < MAX_INTERRUPT_NUM) && ptr_int_mutex != NULL) - { - pthread_mutex_lock(ptr_int_mutex); - cpu_pending_interrupts |= (1 << index); - - /* signal the condition val */ - pthread_cond_signal(&cond_int_hit); - - pthread_mutex_unlock(ptr_int_mutex); - } -} -#endif - From 7b42f926a0c2358d1d74f94f8aec6aaccd87ec49 Mon Sep 17 00:00:00 2001 From: prife Date: Mon, 14 Jan 2013 14:14:40 +0800 Subject: [PATCH 04/19] re-write the rt_hw_interrupt_enable/disable --- libcpu/sim/posix/cpu_port.c | 165 +++++++++++++++++++----------------- 1 file changed, 89 insertions(+), 76 deletions(-) diff --git a/libcpu/sim/posix/cpu_port.c b/libcpu/sim/posix/cpu_port.c index ca25747c5a..e3dea83eee 100644 --- a/libcpu/sim/posix/cpu_port.c +++ b/libcpu/sim/posix/cpu_port.c @@ -1,6 +1,6 @@ /* * author : prife (goprife@gmail.com) - * date : 2013/01/14 01:18:50 + * date : 2013/01/14 01:18:50 * version: v 0.1.0 */ #include @@ -186,28 +186,96 @@ rt_uint8_t *rt_hw_stack_init( return (rt_uint8_t *) thread; } -static int dis_count; -static int en_count; - -/* TODO: 此函数还没有真正实现 */ 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 */ - dis_count++; return back; } -/* TODO: 此函数还没有真正实现 */ 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; - en_count++; + 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(); + + if (pid != mainthread_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); + + /* 确定一下,这两个值一定是相等的! */ + RT_ASSERT(thread_from->pthread == pid); + + /* 唤醒被挂起的线程 */ + sem_post(& thread_to ->sem); + cpu_pending_interrupts --; + + pthread_mutex_unlock(ptr_int_mutex); + + /* 挂起当前的线程 */ + sem_wait(& thread_from->sem); + } + else + { + /* 注意这段代码是在system tick 函数中执行的, + * 即此时位于主线程的SIGALRM信号处理函数中 + */ + TRACE("conswitch: S in pid<%x> ,suspend <%s>, resume <%s>!\n", + (unsigned int)pid, + thread_from->rtthread->name, + thread_to->rtthread->name); + /* 挂起from线程 */ + pthread_kill(thread_from->pthread, MSG_SUSPEND); + cpu_pending_interrupts --; + + pthread_mutex_unlock(ptr_int_mutex); + + /* 唤醒to线程 */ + sem_post(& thread_to->sem); + + } /*TODO: It may need to unmask the signal */ } @@ -240,66 +308,12 @@ void rt_hw_context_switch(rt_uint32_t from, rt_interrupt_from_thread = *((rt_uint32_t *)from); rt_interrupt_to_thread = *((rt_uint32_t *)to); - thread_from = (thread_t *) rt_interrupt_from_thread; - thread_to = (thread_t *) rt_interrupt_to_thread; - - /* FIXME note: now, rt_current_thread is the thread_to! scheduler.c:272 */ - tid = rt_thread_self(); - pid = pthread_self(); - - /* - * FIXME: 这段代码应该删除,因为rt_schedule函数中是先关闭中断,然后再开启的 - * 开启就会出发PendSV(以stm32为例)中断,cpu_port.c的win32版本可以模拟这种 - * 情形,因为它的中断是在主线程代码实现的,而本设计中,切换分为两种情况, - * 1) 普通rtt线程调用rt_thread_delay/rt_sem_take等主动挂起时,即刻实现切换 - * 2) 当rt_thread_delay定时到期时,主线程的SIGALRM信号处理函数中实现线程切换 - * - * 在第一种情况下,如果添加打开如下代码,由于if中语句为真,会导致失败退出。 - * - if (interrupt_disable_flag != 0) - { - printf("dis_count=%d, en_count=%d\n", dis_count, en_count); - printf("interrupt_disable_flag = %d\n", interrupt_disable_flag); - printf("bug! interrupt is disabled! You may forget enable interrupt\n"); - exit(EXIT_FAILURE); - } - */ - - if (pid != mainthread_pid) - { - /* FIXME: 注意这段代码是在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); - - /* 确定一下,这两个值一定是相等的! */ - RT_ASSERT(thread_from->pthread == pid); - - /* 唤醒to线程 */ - sem_post(& thread_to->sem); - - /* 挂起from线程, 既然from线程就是当前线程,所以应该直接 - * 挂起在这里 */ - sem_wait(& thread_from->sem); - } - else - { - /* FIXME: 注意这段代码是在system tick 函数中执行的, - * 即此时位于主线程的SIGALRM信号处理函数中 - */ - TRACE("conswitch: S in pid<%x> ,suspend <%s>, resume <%s>!\n", - (unsigned int)pid, - thread_from->rtthread->name, - thread_to->rtthread->name); - - /* 挂起from线程 */ - pthread_kill(thread_from->pthread, MSG_SUSPEND); - - /* 唤醒to线程 */ - sem_post(& thread_to->sem); - } + /* 这个函数只是并不会真正执行中断处理函数,而只是简单的 + * 设置一下中断挂起标志位 + */ + pthread_mutex_lock(ptr_int_mutex); + cpu_pending_interrupts ++; + pthread_mutex_unlock(ptr_int_mutex); } void rt_hw_context_switch_to(rt_uint32_t to) @@ -331,7 +345,6 @@ static int mainthread_scheduler(void) thread_t *thread_to; pthread_mutex_t mutex; pthread_mutexattr_t mutexattr; - unsigned int contex_switch_mask; /* save the main thread id */ mainthread_pid = pthread_self(); @@ -342,14 +355,11 @@ static int mainthread_scheduler(void) /* install signal handler used to suspend itself */ signal_install(MSG_SUSPEND, thread_switch_handler); -#if 0 /* 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); pthread_mutex_init(ptr_int_mutex, &mutexattr); - pthread_cond_init(&cond_int_hit, NULL); -#endif /* start timer */ start_sys_timer(); @@ -405,17 +415,20 @@ static void start_sys_timer(void) 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); - /* FIXME: 下面这条语句不能打开,打开就会导致失败tick_interrupt_isry - * 永远无法执行 - */ - /* if (! interrupt_disable_flag) */ + if (pthread_mutex_trylock(ptr_int_mutex) != 0) + { + TRACE("try lock failed.\n"); + return; + } tick_interrupt_isr(); + pthread_mutex_unlock(ptr_int_mutex); TRACE("pid <%x> signal: SIGALRM leave!\n", (unsigned int)pid); } From 8f70786c3069b2afa10161f950942aa3f02198eb Mon Sep 17 00:00:00 2001 From: prife Date: Mon, 14 Jan 2013 16:50:40 +0800 Subject: [PATCH 05/19] add finsh, but still cannot work, only can be built with gcc --- bsp/simlinux/drivers/SConscript | 4 +-- bsp/simlinux/drivers/board.c | 9 +++--- bsp/simlinux/drivers/usart_sim.c | 54 +++++++++++++++++++++++++++++--- bsp/simlinux/rtconfig.h | 2 +- libcpu/sim/posix/cpu_port.c | 2 +- 5 files changed, 59 insertions(+), 12 deletions(-) diff --git a/bsp/simlinux/drivers/SConscript b/bsp/simlinux/drivers/SConscript index 34b1a0bda7..079d329c0e 100755 --- a/bsp/simlinux/drivers/SConscript +++ b/bsp/simlinux/drivers/SConscript @@ -12,8 +12,8 @@ if GetDepend('RT_USING_DFS') == False or GetDepend('RT_USING_MTD_NAND') == False SrcRemove(src, 'nanddrv_file.c') if GetDepend('RT_USING_DFS') == False or GetDepend('RT_USING_MTD_NOR') == False: SrcRemove(src, 'sst25vfxx_mtd_sim.c') -if GetDepend('RT_USING_SERIAL') == False: - SrcRemove(src, 'usart_sim.c') +#if GetDepend('RT_USING_SERIAL') == False: +# SrcRemove(src, 'usart_sim.c') CPPPATH = [cwd] diff --git a/bsp/simlinux/drivers/board.c b/bsp/simlinux/drivers/board.c index 5506acb55d..1ece5c8831 100755 --- a/bsp/simlinux/drivers/board.c +++ b/bsp/simlinux/drivers/board.c @@ -53,15 +53,16 @@ void rt_hw_win32_low_cpu(void) #endif } -#if defined(RT_USING_FINSH) - +#ifdef _WIN32 #ifndef _CRT_TERMINATE_DEFINED #define _CRT_TERMINATE_DEFINED _CRTIMP __declspec(noreturn) void __cdecl exit(__in int _Code); _CRTIMP __declspec(noreturn) void __cdecl _exit(__in int _Code); _CRTIMP void __cdecl abort(void); #endif +#endif +#if defined(RT_USING_FINSH) #include void rt_hw_exit(void) { @@ -79,9 +80,9 @@ void rt_hw_board_init() /* init system memory */ heap = rt_hw_sram_init(); -#if defined(RT_USING_USART) +//#if defined(RT_USING_USART) rt_hw_usart_init(); -#endif +//#endif #if defined(RT_USING_CONSOLE) rt_hw_serial_init(); diff --git a/bsp/simlinux/drivers/usart_sim.c b/bsp/simlinux/drivers/usart_sim.c index 6a2564786a..5331f34679 100755 --- a/bsp/simlinux/drivers/usart_sim.c +++ b/bsp/simlinux/drivers/usart_sim.c @@ -4,16 +4,16 @@ #ifdef _WIN32 #include #include +#include #endif #include -#include - #include "serial.h" struct serial_int_rx serial_rx; extern struct rt_device serial_device; +#ifdef _WIN32 /* * Handler for OSKey Thread */ @@ -23,7 +23,6 @@ static DWORD OSKey_ThreadID; static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam); void rt_hw_usart_init(void) { - /* * create serial thread that receive key input from keyboard */ @@ -50,9 +49,52 @@ void rt_hw_usart_init(void) * Start OS get key Thread */ ResumeThread(OSKey_Thread); - } +#else /* POSIX version */ + +#include +#include +#include +#include /* for tcxxxattr, ECHO, etc */ +#include /* for STDIN_FILENO */ + +/*simulate windows' getch(), it works!!*/ +int getch(void) +{ + int ch; + struct termios oldt, newt; + + // get terminal input's attribute + tcgetattr(STDIN_FILENO, &oldt); + newt = oldt; + + //set termios' local mode + newt.c_lflag &= ~(ECHO|ICANON); + tcsetattr(STDIN_FILENO, TCSANOW, &newt); + + //read character from terminal input + ch = getchar(); + + //recover terminal's attribute + tcsetattr(STDIN_FILENO, TCSANOW, &oldt); + + return ch; +} + +static void * ThreadforKeyGet(void * lpParam); +static pthread_t OSKey_Thread; +void rt_hw_usart_init(void) +{ + int res; + res = pthread_create(&OSKey_Thread, NULL, &ThreadforKeyGet, NULL); + if (res) + { + printf("pthread create faild, <%d>\n", res); + exit(EXIT_FAILURE); + } +} +#endif /* * () 0xe04b * () 0xe048 @@ -104,7 +146,11 @@ static int savekey(unsigned char key) } return 0; } +#ifdef _WIN32 static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam) +#else +static void * ThreadforKeyGet(void * lpParam) +#endif { unsigned char key; diff --git a/bsp/simlinux/rtconfig.h b/bsp/simlinux/rtconfig.h index ae213813ec..fcbda8a2a4 100755 --- a/bsp/simlinux/rtconfig.h +++ b/bsp/simlinux/rtconfig.h @@ -101,7 +101,7 @@ /* #define RT_USING_MTD_NOR */ /* SECTION: finsh, a C-Express shell */ -/* #define RT_USING_FINSH */ +#define RT_USING_FINSH /* Using symbol table */ #define FINSH_USING_SYMTAB #define FINSH_USING_DESCRIPTION diff --git a/libcpu/sim/posix/cpu_port.c b/libcpu/sim/posix/cpu_port.c index e3dea83eee..6d2b517cff 100644 --- a/libcpu/sim/posix/cpu_port.c +++ b/libcpu/sim/posix/cpu_port.c @@ -1,7 +1,7 @@ /* * author : prife (goprife@gmail.com) * date : 2013/01/14 01:18:50 - * version: v 0.1.0 + * version: v 0.2.0 */ #include #include From 07da6caafd4f907edd57f5737e0e7f703494ed8c Mon Sep 17 00:00:00 2001 From: prife Date: Mon, 14 Jan 2013 22:38:54 +0800 Subject: [PATCH 06/19] surport finsh --- bsp/simlinux/applications/application.c | 39 +++----------------- bsp/simlinux/drivers/serial.c | 1 + bsp/simlinux/drivers/usart_sim.c | 48 ++++++++++++------------- bsp/simlinux/rtconfig.py | 4 ++- libcpu/sim/posix/cpu_port.c | 15 ++++---- 5 files changed, 37 insertions(+), 70 deletions(-) diff --git a/bsp/simlinux/applications/application.c b/bsp/simlinux/applications/application.c index c8ced1accb..e5ddc6d16b 100755 --- a/bsp/simlinux/applications/application.c +++ b/bsp/simlinux/applications/application.c @@ -18,6 +18,7 @@ #include + void rt_init_thread_entry(void *parameter) { #ifdef RT_USING_LWIP @@ -74,16 +75,8 @@ void rt_init_thread_entry(void *parameter) } #endif -#if 0 - { - extern void application_init(void); - rt_thread_delay(RT_TICK_PER_SECOND); - application_init(); - } -#endif - #if defined(RT_USING_RTGUI) - rt_thread_delay(3000); + rt_thread_delay(RT_TICK_PER_SECOND); snake_main(); #endif } @@ -91,56 +84,32 @@ void rt_init_thread_entry(void *parameter) static void rt_test_thread_entry(void *parameter) { int i; - for (i = 0; i < 10; i++) + for (i = 0; i < 5; i++) { rt_kprintf("hello, world\n"); rt_thread_delay(RT_TICK_PER_SECOND); } } -static void rt_high_thread_entry(void *parameter) -{ - int i; - for (i = 0; i < 3; i++) - { - rt_kprintf("high thread <%d> \n", i); - rt_thread_delay(2*RT_TICK_PER_SECOND); - } -} int rt_application_init() { rt_thread_t tid; -#if 0 tid = rt_thread_create("init", rt_init_thread_entry, RT_NULL, 2048, RT_THREAD_PRIORITY_MAX / 3, 20); if (tid != RT_NULL) rt_thread_startup(tid); + tid = rt_thread_create("test", rt_test_thread_entry, RT_NULL, 2048, RT_THREAD_PRIORITY_MAX * 3 / 4, 20); if (tid != RT_NULL) rt_thread_startup(tid); -#endif - - tid = rt_thread_create("test1", - rt_high_thread_entry, RT_NULL, - 2048, RT_THREAD_PRIORITY_MAX / 2, 20); - if (tid != RT_NULL) - rt_thread_startup(tid); - - tid = rt_thread_create("test2", - rt_test_thread_entry, RT_NULL, - 2048, RT_THREAD_PRIORITY_MAX / 2, 20); - if (tid != RT_NULL) - rt_thread_startup(tid); - return 0; } - /*@}*/ diff --git a/bsp/simlinux/drivers/serial.c b/bsp/simlinux/drivers/serial.c index 2bcdb15866..87de4d0498 100755 --- a/bsp/simlinux/drivers/serial.c +++ b/bsp/simlinux/drivers/serial.c @@ -122,6 +122,7 @@ static rt_size_t rt_serial_write(rt_device_t dev, rt_off_t pos, const void *buff #endif printf("%s", (char *)buffer); + fflush(stdout); return size; } diff --git a/bsp/simlinux/drivers/usart_sim.c b/bsp/simlinux/drivers/usart_sim.c index 5331f34679..e554fbb55a 100755 --- a/bsp/simlinux/drivers/usart_sim.c +++ b/bsp/simlinux/drivers/usart_sim.c @@ -59,28 +59,6 @@ void rt_hw_usart_init(void) #include /* for tcxxxattr, ECHO, etc */ #include /* for STDIN_FILENO */ -/*simulate windows' getch(), it works!!*/ -int getch(void) -{ - int ch; - struct termios oldt, newt; - - // get terminal input's attribute - tcgetattr(STDIN_FILENO, &oldt); - newt = oldt; - - //set termios' local mode - newt.c_lflag &= ~(ECHO|ICANON); - tcsetattr(STDIN_FILENO, TCSANOW, &newt); - - //read character from terminal input - ch = getchar(); - - //recover terminal's attribute - tcsetattr(STDIN_FILENO, TCSANOW, &oldt); - - return ch; -} static void * ThreadforKeyGet(void * lpParam); static pthread_t OSKey_Thread; @@ -149,16 +127,36 @@ static int savekey(unsigned char key) #ifdef _WIN32 static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam) #else + +/*simulate windows' getch(), it works!!*/ +static void setgetchar(void) +{ + struct termios oldt, newt; + + // get terminal input's attribute + tcgetattr(STDIN_FILENO, &oldt); + newt = oldt; + + //set termios' local mode + newt.c_lflag &= ~(ECHO|ICANON); + tcsetattr(STDIN_FILENO, TCSANOW, &newt); +} +#define getch getchar + static void * ThreadforKeyGet(void * lpParam) -#endif +#endif /* not _WIN32*/ { unsigned char key; (void)lpParam; //prevent compiler warnings - +#ifndef _WIN32 + /* set the getchar without buffer */ + setgetchar(); +#endif for (;;) { key = getch(); +#ifdef _WIN32 if (key == 0xE0) { key = getch(); @@ -178,7 +176,7 @@ static void * ThreadforKeyGet(void * lpParam) continue; } - +#endif savekey(key); } } /*** ThreadforKeyGet ***/ diff --git a/bsp/simlinux/rtconfig.py b/bsp/simlinux/rtconfig.py index 020649f329..6a660e832f 100755 --- a/bsp/simlinux/rtconfig.py +++ b/bsp/simlinux/rtconfig.py @@ -35,11 +35,13 @@ if PLATFORM == 'gcc': OBJCPY = PREFIX + 'objcopy' DEVICE = ' -ffunction-sections -fdata-sections' + DEVICE = ' ' CFLAGS = DEVICE + ' -I/usr/include -w -D_REENTRANT' AFLAGS = ' -c' + DEVICE + ' -x assembler-with-cpp' #LFLAGS = DEVICE + ' -Wl,--gc-sections,-Map=rtthread-linux.map,-cref,-u,Reset_Handler -T stm32_rom.ld' #LFLAGS = DEVICE + ' -Wl,--gc-sections,-Map=rtthread-linux.map -lpthread' - LFLAGS = DEVICE + ' -Wl,--gc-sections,-Map=rtthread-linux.map -pthread' + #LFLAGS = DEVICE + ' -Wl,--gc-sections,-Map=rtthread-linux.map -pthread' + LFLAGS = DEVICE + ' -Wl,-Map=rtthread-linux.map -pthread -T gcc.ld' CPATH = '' LPATH = '' diff --git a/libcpu/sim/posix/cpu_port.c b/libcpu/sim/posix/cpu_port.c index 6d2b517cff..622a3bdc29 100644 --- a/libcpu/sim/posix/cpu_port.c +++ b/libcpu/sim/posix/cpu_port.c @@ -94,9 +94,9 @@ static void thread_switch_handler(int sig) thread_from = (thread_t *) rt_interrupt_from_thread; thread_to = (thread_t *) rt_interrupt_to_thread; - /* FIXME 注意!此时 rt_thread_self的值是to线程的值! */ + /* 注意!此时 rt_thread_self的值是to线程的值! */ tid = rt_thread_self(); - RT_ASSERT(thread_from->pthread == pid); + /* 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); @@ -124,7 +124,6 @@ static void *thread_run(void *parameter) thread->task(thread->para); TRACE("pid <%08x> tid <%s> exit...\n", (unsigned int)(thread->pthread), tid->name); - //FIXME thread->exit(); //sem_destroy(&thread->sem); //<-------------- @@ -235,7 +234,7 @@ void rt_hw_interrupt_enable(rt_base_t level) tid = rt_thread_self(); pid = pthread_self(); - if (pid != mainthread_pid) + if (pid != mainthread_pid && thread_from->pthread == pid) { /* 注意这段代码是在RTT普通线程函数总函数中执行的, * from线程就是当前rtt线程 */ @@ -245,9 +244,6 @@ void rt_hw_interrupt_enable(rt_base_t level) thread_from->rtthread->name, thread_to->rtthread->name); - /* 确定一下,这两个值一定是相等的! */ - RT_ASSERT(thread_from->pthread == pid); - /* 唤醒被挂起的线程 */ sem_post(& thread_to ->sem); cpu_pending_interrupts --; @@ -259,8 +255,9 @@ void rt_hw_interrupt_enable(rt_base_t level) } else { - /* 注意这段代码是在system tick 函数中执行的, - * 即此时位于主线程的SIGALRM信号处理函数中 + /* 注意这段代码可能在多种情况下运行: + * 1. 在system tick中执行, 即主线程的SIGALRM信号处理函数中执行 + * 2. 其他线程中调用,比如用于获取按键输入的线程中调用 */ TRACE("conswitch: S in pid<%x> ,suspend <%s>, resume <%s>!\n", (unsigned int)pid, From f0d6e0e17366987a8c5c5bb0883e52f6801663f3 Mon Sep 17 00:00:00 2001 From: prife Date: Mon, 14 Jan 2013 22:41:36 +0800 Subject: [PATCH 07/19] add gcc.ld --- bsp/simlinux/gcc.ld | 216 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 216 insertions(+) create mode 100644 bsp/simlinux/gcc.ld diff --git a/bsp/simlinux/gcc.ld b/bsp/simlinux/gcc.ld new file mode 100644 index 0000000000..74f033935a --- /dev/null +++ b/bsp/simlinux/gcc.ld @@ -0,0 +1,216 @@ +/* Script for -z combreloc: combine and sort reloc sections */ +OUTPUT_FORMAT("elf32-i386", "elf32-i386", + "elf32-i386") +OUTPUT_ARCH(i386) +ENTRY(_start) +SEARCH_DIR("/usr/i686-linux-gnu/lib32"); SEARCH_DIR("=/usr/local/lib32"); SEARCH_DIR("=/lib32"); SEARCH_DIR("=/usr/lib32"); SEARCH_DIR("=/usr/local/lib/i386-linux-gnu"); SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib/i386-linux-gnu"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib/i386-linux-gnu"); SEARCH_DIR("=/usr/lib"); +SECTIONS +{ + /* Read-only sections, merged into text segment: */ + PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x08048000)); . = SEGMENT_START("text-segment", 0x08048000) + SIZEOF_HEADERS; + .interp : { *(.interp) } + .note.gnu.build-id : { *(.note.gnu.build-id) } + .hash : { *(.hash) } + .gnu.hash : { *(.gnu.hash) } + .dynsym : { *(.dynsym) } + .dynstr : { *(.dynstr) } + .gnu.version : { *(.gnu.version) } + .gnu.version_d : { *(.gnu.version_d) } + .gnu.version_r : { *(.gnu.version_r) } + .rel.dyn : + { + *(.rel.init) + *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*) + *(.rel.fini) + *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*) + *(.rel.data.rel.ro* .rel.gnu.linkonce.d.rel.ro.*) + *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*) + *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*) + *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*) + *(.rel.ctors) + *(.rel.dtors) + *(.rel.got) + *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*) + *(.rel.ifunc) + } + .rel.plt : + { + *(.rel.plt) + PROVIDE_HIDDEN (__rel_iplt_start = .); + *(.rel.iplt) + PROVIDE_HIDDEN (__rel_iplt_end = .); + } + .init : + { + KEEP (*(.init)) + } =0x90909090 + .plt : { *(.plt) *(.iplt) } + .text : + { + *(.text.unlikely .text.*_unlikely) + *(.text.exit .text.exit.*) + *(.text.startup .text.startup.*) + *(.text.hot .text.hot.*) + *(.text .stub .text.* .gnu.linkonce.t.*) + /* .gnu.warning sections are handled specially by elf32.em. */ + *(.gnu.warning) + } =0x90909090 + .fini : + { + KEEP (*(.fini)) + } =0x90909090 + PROVIDE (__etext = .); + PROVIDE (_etext = .); + PROVIDE (etext = .); + .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) } + .rodata1 : { *(.rodata1) } + + /* setction information for finsh shell begin */ + . = ALIGN(4); + __fsymtab_start = .; + FSymTab : {KEEP(*(FSymTab))} + __fsymtab_end = .; + . = ALIGN(4); + __vsymtab_start = .; + VSymTab : {KEEP(*(VSymTab))} + __vsymtab_end = .; + . = ALIGN(4); + /* setction information for finsh shell end */ + + .eh_frame_hdr : { *(.eh_frame_hdr) } + .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) } + .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table + .gcc_except_table.*) } + /* These sections are generated by the Sun/Oracle C++ compiler. */ + .exception_ranges : ONLY_IF_RO { *(.exception_ranges + .exception_ranges*) } + /* Adjust the address for the data segment. We want to adjust up to + the same address within the page on the next page up. */ + . = ALIGN (CONSTANT (MAXPAGESIZE)) - ((CONSTANT (MAXPAGESIZE) - .) & (CONSTANT (MAXPAGESIZE) - 1)); . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); + /* Exception handling */ + .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) } + .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) } + .exception_ranges : ONLY_IF_RW { *(.exception_ranges .exception_ranges*) } + /* Thread Local Storage sections */ + .tdata : { *(.tdata .tdata.* .gnu.linkonce.td.*) } + .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) } + .preinit_array : + { + PROVIDE_HIDDEN (__preinit_array_start = .); + KEEP (*(.preinit_array)) + PROVIDE_HIDDEN (__preinit_array_end = .); + } + .init_array : + { + PROVIDE_HIDDEN (__init_array_start = .); + KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) + KEEP (*(.init_array)) + KEEP (*(EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)) + PROVIDE_HIDDEN (__init_array_end = .); + } + .fini_array : + { + PROVIDE_HIDDEN (__fini_array_start = .); + KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*))) + KEEP (*(.fini_array)) + KEEP (*(EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors)) + PROVIDE_HIDDEN (__fini_array_end = .); + } + .ctors : + { + /* gcc uses crtbegin.o to find the start of + the constructors, so we make sure it is + first. Because this is a wildcard, it + doesn't matter if the user does not + actually link against crtbegin.o; the + linker won't look for a file to match a + wildcard. The wildcard also means that it + doesn't matter which directory crtbegin.o + is in. */ + KEEP (*crtbegin.o(.ctors)) + KEEP (*crtbegin?.o(.ctors)) + /* We don't want to include the .ctor section from + the crtend.o file until after the sorted ctors. + The .ctor section from the crtend file contains the + end of ctors marker and it must be last */ + KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors)) + KEEP (*(SORT(.ctors.*))) + KEEP (*(.ctors)) + } + .dtors : + { + KEEP (*crtbegin.o(.dtors)) + KEEP (*crtbegin?.o(.dtors)) + KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors)) + KEEP (*(SORT(.dtors.*))) + KEEP (*(.dtors)) + } + .jcr : { KEEP (*(.jcr)) } + .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro* .gnu.linkonce.d.rel.ro.*) } + .dynamic : { *(.dynamic) } + .got : { *(.got) *(.igot) } + . = DATA_SEGMENT_RELRO_END (12, .); + .got.plt : { *(.got.plt) *(.igot.plt) } + .data : + { + *(.data .data.* .gnu.linkonce.d.*) + SORT(CONSTRUCTORS) + } + .data1 : { *(.data1) } + _edata = .; PROVIDE (edata = .); + __bss_start = .; + .bss : + { + *(.dynbss) + *(.bss .bss.* .gnu.linkonce.b.*) + *(COMMON) + /* Align here to ensure that the .bss section occupies space up to + _end. Align after .bss to ensure correct alignment even if the + .bss section disappears because there are no input sections. + FIXME: Why do we need it? When there is no .bss section, we don't + pad the .data section. */ + . = ALIGN(. != 0 ? 32 / 8 : 1); + } + . = ALIGN(32 / 8); + . = ALIGN(32 / 8); + _end = .; PROVIDE (end = .); + . = DATA_SEGMENT_END (.); + /* Stabs debugging sections. */ + .stab 0 : { *(.stab) } + .stabstr 0 : { *(.stabstr) } + .stab.excl 0 : { *(.stab.excl) } + .stab.exclstr 0 : { *(.stab.exclstr) } + .stab.index 0 : { *(.stab.index) } + .stab.indexstr 0 : { *(.stab.indexstr) } + .comment 0 : { *(.comment) } + /* DWARF debug sections. + Symbols in the DWARF debugging sections are relative to the beginning + of the section so we begin them at 0. */ + /* DWARF 1 */ + .debug 0 : { *(.debug) } + .line 0 : { *(.line) } + /* GNU DWARF 1 extensions */ + .debug_srcinfo 0 : { *(.debug_srcinfo) } + .debug_sfnames 0 : { *(.debug_sfnames) } + /* DWARF 1.1 and DWARF 2 */ + .debug_aranges 0 : { *(.debug_aranges) } + .debug_pubnames 0 : { *(.debug_pubnames) } + /* DWARF 2 */ + .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } + .debug_abbrev 0 : { *(.debug_abbrev) } + .debug_line 0 : { *(.debug_line) } + .debug_frame 0 : { *(.debug_frame) } + .debug_str 0 : { *(.debug_str) } + .debug_loc 0 : { *(.debug_loc) } + .debug_macinfo 0 : { *(.debug_macinfo) } + /* SGI/MIPS DWARF 2 extensions */ + .debug_weaknames 0 : { *(.debug_weaknames) } + .debug_funcnames 0 : { *(.debug_funcnames) } + .debug_typenames 0 : { *(.debug_typenames) } + .debug_varnames 0 : { *(.debug_varnames) } + /* DWARF 3 */ + .debug_pubtypes 0 : { *(.debug_pubtypes) } + .debug_ranges 0 : { *(.debug_ranges) } + .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) } + /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) } +} From 395178ebfae6033d578193ed8b855fa196a1f408 Mon Sep 17 00:00:00 2001 From: prife Date: Tue, 15 Jan 2013 21:38:54 +0800 Subject: [PATCH 08/19] rewrite cpu_port.c (but still has bug) --- bsp/simlinux/drivers/usart_sim.c | 8 +- libcpu/sim/posix/cpu_port.c | 152 +++++++++++++++++++++++++------ 2 files changed, 132 insertions(+), 28 deletions(-) diff --git a/bsp/simlinux/drivers/usart_sim.c b/bsp/simlinux/drivers/usart_sim.c index e554fbb55a..5ce223fb2b 100755 --- a/bsp/simlinux/drivers/usart_sim.c +++ b/bsp/simlinux/drivers/usart_sim.c @@ -56,6 +56,7 @@ void rt_hw_usart_init(void) #include #include #include +#include #include /* for tcxxxattr, ECHO, etc */ #include /* for STDIN_FILENO */ @@ -122,7 +123,7 @@ static int savekey(unsigned char key) serial_device.rx_indicate(&serial_device, rx_length); } - return 0; + return 0; } #ifdef _WIN32 static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam) @@ -148,11 +149,14 @@ static void * ThreadforKeyGet(void * lpParam) { unsigned char key; - (void)lpParam; //prevent compiler warnings #ifndef _WIN32 + sigset_t sigmask, oldmask; /* set the getchar without buffer */ + sigfillset(&sigmask); + pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask); setgetchar(); #endif + (void)lpParam; //prevent compiler warnings for (;;) { key = getch(); diff --git a/libcpu/sim/posix/cpu_port.c b/libcpu/sim/posix/cpu_port.c index 622a3bdc29..70dc3572e1 100644 --- a/libcpu/sim/posix/cpu_port.c +++ b/libcpu/sim/posix/cpu_port.c @@ -24,21 +24,29 @@ typedef struct _thread 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_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_uint32_t rt_interrupt_from_thread, rt_interrupt_to_thread; @@ -78,8 +86,9 @@ int signal_mask(void) sigaddset(&sigmask, SIGALRM); pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask); } -static void thread_switch_handler(int sig) +static void thread_suspend_signal_handler(int sig) { + sigset_t sigmask; pthread_t pid = pthread_self(); thread_t *thread_from; thread_t *thread_to; @@ -100,10 +109,44 @@ static void thread_switch_handler(int sig) RT_ASSERT((thread_t *)(tid->sp) == thread_to); TRACE("signal: SIGSUSPEND suspend <%s>\n", thread_from->rtthread->name); - sem_wait(&thread_from->sem); + + /* 使用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; @@ -114,6 +157,7 @@ static void *thread_run(void *parameter) /* FIXME set signal mask, mask the timer! */ signal_mask(); + thread->status = SUSPEND_SIGWAIT; TRACE("pid <%08x> stop on sem...\n", (unsigned int)(thread->pthread)); sem_wait(&thread->sem); @@ -234,7 +278,8 @@ void rt_hw_interrupt_enable(rt_base_t level) tid = rt_thread_self(); pid = pthread_self(); - if (pid != mainthread_pid && thread_from->pthread == pid) + //pid != mainthread_pid && + if (thread_from->pthread == pid) { /* 注意这段代码是在RTT普通线程函数总函数中执行的, * from线程就是当前rtt线程 */ @@ -245,19 +290,36 @@ void rt_hw_interrupt_enable(rt_base_t level) thread_to->rtthread->name); /* 唤醒被挂起的线程 */ - sem_post(& thread_to ->sem); + 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); + } cpu_pending_interrupts --; - + thread_from->status = SUSPEND_LOCK; pthread_mutex_unlock(ptr_int_mutex); /* 挂起当前的线程 */ sem_wait(& thread_from->sem); + //TRACE("rttask:%s suspend!\n", thread_from->rtthread->name); + pthread_mutex_lock(ptr_int_mutex); + thread_from->status = THREAD_RUNNING; + pthread_mutex_unlock(ptr_int_mutex); + //TRACE("rttask:%s resume!\n", thread_to->rtthread->name); } else { /* 注意这段代码可能在多种情况下运行: - * 1. 在system tick中执行, 即主线程的SIGALRM信号处理函数中执行 - * 2. 其他线程中调用,比如用于获取按键输入的线程中调用 + * 1. 在system tick中执行, 即主线程的SIGALRM信号处理函数中执行 + * 2. 其他线程中调用,比如用于获取按键输入的线程中调用 */ TRACE("conswitch: S in pid<%x> ,suspend <%s>, resume <%s>!\n", (unsigned int)pid, @@ -267,11 +329,22 @@ void rt_hw_interrupt_enable(rt_base_t level) pthread_kill(thread_from->pthread, MSG_SUSPEND); cpu_pending_interrupts --; - pthread_mutex_unlock(ptr_int_mutex); - /* 唤醒to线程 */ - sem_post(& thread_to->sem); + 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); + } + pthread_mutex_unlock(ptr_int_mutex); } /*TODO: It may need to unmask the signal */ } @@ -302,13 +375,13 @@ void rt_hw_context_switch(rt_uint32_t from, rt_interrupt_from_thread = *((rt_uint32_t *)from); } #endif + pthread_mutex_lock(ptr_int_mutex); rt_interrupt_from_thread = *((rt_uint32_t *)from); rt_interrupt_to_thread = *((rt_uint32_t *)to); /* 这个函数只是并不会真正执行中断处理函数,而只是简单的 * 设置一下中断挂起标志位 */ - pthread_mutex_lock(ptr_int_mutex); cpu_pending_interrupts ++; pthread_mutex_unlock(ptr_int_mutex); } @@ -337,20 +410,31 @@ void rt_hw_context_switch_to(rt_uint32_t to) static int mainthread_scheduler(void) { - int i, res; + 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 itself */ - signal_install(MSG_SUSPEND, thread_switch_handler); + /* 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; @@ -365,10 +449,34 @@ static int mainthread_scheduler(void) /* trigger_interrupt(CPU_INTERRUPT_YIELD); */ thread_to = (thread_t *) rt_interrupt_to_thread; thread_resume(thread_to); - for (;;) { - sleep(1); +#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; @@ -418,15 +526,7 @@ static void mthread_signal_tick(int sig) if (sig == SIGALRM) { TRACE("pid <%x> signal: SIGALRM enter!\n", (unsigned int)pid); - - if (pthread_mutex_trylock(ptr_int_mutex) != 0) - { - TRACE("try lock failed.\n"); - return; - } - tick_interrupt_isr(); - pthread_mutex_unlock(ptr_int_mutex); - + //systick_signal_flag = 1; TRACE("pid <%x> signal: SIGALRM leave!\n", (unsigned int)pid); } else From b8bd5c8309482ce6a835f58708a221ecd0e5960c Mon Sep 17 00:00:00 2001 From: prife Date: Tue, 15 Jan 2013 23:59:14 +0800 Subject: [PATCH 09/19] fix bug in cpu_port.c(can work very well) --- libcpu/sim/posix/cpu_port.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/libcpu/sim/posix/cpu_port.c b/libcpu/sim/posix/cpu_port.c index 70dc3572e1..fdc8fc13e8 100644 --- a/libcpu/sim/posix/cpu_port.c +++ b/libcpu/sim/posix/cpu_port.c @@ -157,7 +157,7 @@ static void *thread_run(void *parameter) /* FIXME set signal mask, mask the timer! */ signal_mask(); - thread->status = SUSPEND_SIGWAIT; + thread->status = SUSPEND_LOCK; TRACE("pid <%08x> stop on sem...\n", (unsigned int)(thread->pthread)); sem_wait(&thread->sem); @@ -289,6 +289,9 @@ void rt_hw_interrupt_enable(rt_base_t level) 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) { @@ -303,9 +306,6 @@ void rt_hw_interrupt_enable(rt_base_t level) printf("conswitch: should not be here! %d\n", __LINE__); exit(EXIT_FAILURE); } - cpu_pending_interrupts --; - thread_from->status = SUSPEND_LOCK; - pthread_mutex_unlock(ptr_int_mutex); /* 挂起当前的线程 */ sem_wait(& thread_from->sem); @@ -325,9 +325,20 @@ void rt_hw_interrupt_enable(rt_base_t level) (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); - cpu_pending_interrupts --; + /* TODO 这里需要确保线程被挂起了, 否则304行就很可能就会报错退出 + * 因为这里挂起线程是通过信号实现的,所以一定要确保线程挂起才行 */ + while (thread_from->status != SUSPEND_SIGWAIT) + { + sched_yield(); + } /* 唤醒to线程 */ if (thread_to->status == SUSPEND_SIGWAIT) @@ -344,7 +355,6 @@ void rt_hw_interrupt_enable(rt_base_t level) exit(EXIT_FAILURE); } - pthread_mutex_unlock(ptr_int_mutex); } /*TODO: It may need to unmask the signal */ } From b8aaa6e7306e6f5b51603f868421898a07bd3245 Mon Sep 17 00:00:00 2001 From: prife Date: Wed, 16 Jan 2013 18:49:07 +0800 Subject: [PATCH 10/19] clean code in cpu_port.c, add some comments --- libcpu/sim/posix/cpu_port.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/libcpu/sim/posix/cpu_port.c b/libcpu/sim/posix/cpu_port.c index fdc8fc13e8..e094edb6bc 100644 --- a/libcpu/sim/posix/cpu_port.c +++ b/libcpu/sim/posix/cpu_port.c @@ -46,7 +46,7 @@ typedef struct _thread /* interrupt flag, if 1, disable, if 0, enable */ static long interrupt_disable_flag; -static int systick_signal_flag; +//static int systick_signal_flag; /* flag in interrupt handling */ rt_uint32_t rt_interrupt_from_thread, rt_interrupt_to_thread; @@ -154,7 +154,7 @@ static void *thread_run(void *parameter) thread = THREAD_T(parameter); int res; - /* FIXME set signal mask, mask the timer! */ + /* set signal mask, mask the timer! */ signal_mask(); thread->status = SUSPEND_LOCK; @@ -169,7 +169,13 @@ static void *thread_run(void *parameter) TRACE("pid <%08x> tid <%s> exit...\n", (unsigned int)(thread->pthread), tid->name); thread->exit(); - //sem_destroy(&thread->sem); //<-------------- + + /*TODO: + * 最后一行的pthread_exit永远没有机会执行,这是因为在threead->exit函数中 + * 会发生线程切换,并永久将此pthread线程挂起,所以更完美的解决方案是在这 + * 里发送信号给主线程,主线程中再次唤醒此线程令其自动退出。 + */ + //sem_destroy(&thread->sem); pthread_exit(NULL); } @@ -309,11 +315,9 @@ void rt_hw_interrupt_enable(rt_base_t level) /* 挂起当前的线程 */ sem_wait(& thread_from->sem); - //TRACE("rttask:%s suspend!\n", thread_from->rtthread->name); pthread_mutex_lock(ptr_int_mutex); thread_from->status = THREAD_RUNNING; pthread_mutex_unlock(ptr_int_mutex); - //TRACE("rttask:%s resume!\n", thread_to->rtthread->name); } else { @@ -333,7 +337,7 @@ void rt_hw_interrupt_enable(rt_base_t level) /* 挂起from线程 */ pthread_kill(thread_from->pthread, MSG_SUSPEND); - /* TODO 这里需要确保线程被挂起了, 否则304行就很可能就会报错退出 + /* 注意:这里需要确保线程被挂起了, 否则312行就很可能就会报错退出 * 因为这里挂起线程是通过信号实现的,所以一定要确保线程挂起才行 */ while (thread_from->status != SUSPEND_SIGWAIT) { @@ -376,7 +380,7 @@ void rt_hw_context_switch(rt_uint32_t from, RT_ASSERT(from != to); #if 0 - //TODO: 还需要考虑嵌套切换的情况 + //TODO: 可能还需要考虑嵌套切换的情况 if (rt_thread_switch_interrupt_flag != 1) { rt_thread_switch_interrupt_flag = 1; @@ -449,14 +453,12 @@ static int mainthread_scheduler(void) /* 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); + pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE_NP); pthread_mutex_init(ptr_int_mutex, &mutexattr); /* start timer */ start_sys_timer(); - /* FIXME: note that, cond var could not released earlier than pthread_con_wait */ - /* trigger_interrupt(CPU_INTERRUPT_YIELD); */ thread_to = (thread_t *) rt_interrupt_to_thread; thread_resume(thread_to); for (;;) @@ -473,11 +475,11 @@ static int mainthread_scheduler(void) /* signal mask sigalrm 屏蔽SIGALRM信号 */ pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask); -// if (systick_signal_flag != 0) + // if (systick_signal_flag != 0) if (pthread_mutex_trylock(ptr_int_mutex) == 0) { tick_interrupt_isr(); - systick_signal_flag = 0; + // systick_signal_flag = 0; pthread_mutex_unlock(ptr_int_mutex); } else From 8999948650a3c1ba1417a68fb31122404dde4b89 Mon Sep 17 00:00:00 2001 From: prife Date: Thu, 17 Jan 2013 12:06:23 +0800 Subject: [PATCH 11/19] recover the terminal's behaviour when runing exit in finsh --- bsp/simlinux/drivers/board.c | 20 ++++++++++++++++---- bsp/simlinux/drivers/usart_sim.c | 18 ++++++++++++------ 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/bsp/simlinux/drivers/board.c b/bsp/simlinux/drivers/board.c index 1ece5c8831..c39053e5da 100755 --- a/bsp/simlinux/drivers/board.c +++ b/bsp/simlinux/drivers/board.c @@ -32,7 +32,7 @@ rt_uint8_t *rt_hw_sram_init(void) #ifdef _WIN32 _exit(1); #else - exit(1); + exit(1); #endif } return heap; @@ -45,11 +45,11 @@ rt_uint8_t *rt_hw_sram_init(void) void rt_hw_win32_low_cpu(void) { #ifdef _WIN32 - /* in windows */ + /* in windows */ Sleep(1000); #else - /* in linux */ - sleep(1); + /* in linux */ + sleep(1); #endif } @@ -67,6 +67,18 @@ _CRTIMP void __cdecl abort(void); void rt_hw_exit(void) { rt_kprintf("RT-Thread, bye\n"); +#if !defined(_WIN32) && defined(__GNUC__) + /* * + * getchar reads key from buffer, while finsh need an non-buffer getchar + * in windows, getch is such an function, in linux, we had to change + * the behaviour of terminal to get an non-buffer getchar. + * in usart_sim.c, set_stty is called to do this work + * */ + { + extern void restore_stty(void); + restore_stty(); + } +#endif exit(0); } FINSH_FUNCTION_EXPORT_ALIAS(rt_hw_exit, exit, exit rt - thread); diff --git a/bsp/simlinux/drivers/usart_sim.c b/bsp/simlinux/drivers/usart_sim.c index 5ce223fb2b..0e9c528efe 100755 --- a/bsp/simlinux/drivers/usart_sim.c +++ b/bsp/simlinux/drivers/usart_sim.c @@ -129,19 +129,25 @@ static int savekey(unsigned char key) static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam) #else +static struct termios oldt, newt; /*simulate windows' getch(), it works!!*/ -static void setgetchar(void) +void set_stty(void) { - struct termios oldt, newt; - - // get terminal input's attribute + /* get terminal input's attribute */ tcgetattr(STDIN_FILENO, &oldt); newt = oldt; - //set termios' local mode + /* set termios' local mode */ newt.c_lflag &= ~(ECHO|ICANON); tcsetattr(STDIN_FILENO, TCSANOW, &newt); } + +void restore_stty(void) +{ + /* recover terminal's attribute */ + tcsetattr(STDIN_FILENO, TCSANOW, &oldt); +} + #define getch getchar static void * ThreadforKeyGet(void * lpParam) @@ -154,7 +160,7 @@ static void * ThreadforKeyGet(void * lpParam) /* set the getchar without buffer */ sigfillset(&sigmask); pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask); - setgetchar(); + set_stty(); #endif (void)lpParam; //prevent compiler warnings for (;;) From 4f45c460d880c9c4ed0ad0e91f1ac6aae54871cc Mon Sep 17 00:00:00 2001 From: prife Date: Tue, 22 Jan 2013 12:08:19 +0800 Subject: [PATCH 12/19] use sd as '/' mount ponit --- bsp/simlinux/applications/application.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bsp/simlinux/applications/application.c b/bsp/simlinux/applications/application.c index e5ddc6d16b..a920daffa0 100755 --- a/bsp/simlinux/applications/application.c +++ b/bsp/simlinux/applications/application.c @@ -50,7 +50,8 @@ void rt_init_thread_entry(void *parameter) #ifdef RT_USING_DFS_ELMFAT /* mount sd card fatfs as root directory */ - if (dfs_mount("sd0", "/disk/sd", "elm", 0, 0) == 0) + //if (dfs_mount("sd0", "/disk/sd", "elm", 0, 0) == 0) + if (dfs_mount("sd0", "/", "elm", 0, 0) == 0) rt_kprintf("fatfs initialized!\n"); else rt_kprintf("fatfs initialization failed!\n"); From e3cc345ecfcd79b03e36bf547fce6fb49802b6aa Mon Sep 17 00:00:00 2001 From: prife Date: Tue, 22 Jan 2013 12:09:43 +0800 Subject: [PATCH 13/19] clean code in sd_sim.c to make sure it can be built with gcc in linux --- bsp/simlinux/drivers/sd_sim.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bsp/simlinux/drivers/sd_sim.c b/bsp/simlinux/drivers/sd_sim.c index 7c9844bae3..1f39843d3e 100755 --- a/bsp/simlinux/drivers/sd_sim.c +++ b/bsp/simlinux/drivers/sd_sim.c @@ -1,7 +1,7 @@ #include #include -#include -#include +//#include +//#include #include // #define SD_TRACE rt_kprintf From 236099e3ecfa3636050a2131ab93b736be881e97 Mon Sep 17 00:00:00 2001 From: prife Date: Tue, 22 Jan 2013 12:11:35 +0800 Subject: [PATCH 14/19] enable FAT and uffs in simlinux/rtconfig.h --- bsp/simlinux/rtconfig.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bsp/simlinux/rtconfig.h b/bsp/simlinux/rtconfig.h index fcbda8a2a4..1019f84abf 100755 --- a/bsp/simlinux/rtconfig.h +++ b/bsp/simlinux/rtconfig.h @@ -34,7 +34,7 @@ #define RT_THREAD_PRIORITY_MAX 32 /* Tick per Second */ -#define RT_TICK_PER_SECOND 2 +#define RT_TICK_PER_SECOND 100 /* SECTION: RT_DEBUG */ /* Thread Debug */ @@ -96,9 +96,9 @@ /* SECTION: MTD interface options */ /* using mtd nand flash */ -/* #define RT_USING_MTD_NAND */ +#define RT_USING_MTD_NAND /* using mtd nor flash */ -/* #define RT_USING_MTD_NOR */ +#define RT_USING_MTD_NOR /* SECTION: finsh, a C-Express shell */ #define RT_USING_FINSH @@ -107,7 +107,7 @@ #define FINSH_USING_DESCRIPTION /* SECTION: device file system */ -/* #define RT_USING_DFS */ +#define RT_USING_DFS #define DFS_FILESYSTEM_TYPES_MAX 8 /* DFS: ELM FATFS options */ @@ -133,11 +133,11 @@ /* #define RT_UFFS_USE_CHECK_MARK_FUNCITON */ /* DFS: JFFS2 nor flash file system options */ -#define RT_USING_DFS_JFFS2 +//#define RT_USING_DFS_JFFS2 /* DFS: windows share directory mounted to rt-thread/dfs */ /* only used in bsp/simulator */ -#define RT_USING_DFS_WINSHAREDIR +//#define RT_USING_DFS_WINSHAREDIR /* the max number of mounted file system */ #define DFS_FILESYSTEMS_MAX 4 From 7c3f2fd6c7500e4841a76fa3eec6611f0f40120b Mon Sep 17 00:00:00 2001 From: prife Date: Tue, 22 Jan 2013 15:49:49 +0800 Subject: [PATCH 15/19] fix sdl_fb.c to support gcc under linux --- bsp/simlinux/drivers/sdl_fb.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/bsp/simlinux/drivers/sdl_fb.c b/bsp/simlinux/drivers/sdl_fb.c index 790797f2af..752a6fb93c 100755 --- a/bsp/simlinux/drivers/sdl_fb.c +++ b/bsp/simlinux/drivers/sdl_fb.c @@ -1,6 +1,10 @@ #include +#ifdef _WIN32 #include +#else +#include +#endif #include #include @@ -104,7 +108,7 @@ static void sdlfb_hw_init(void) //_putenv("SDL_VIDEODRIVER=windib"); //if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO) < 0) - if (SDL_Init(SDL_INIT_EVERYTHING) < 0) + if (SDL_Init(SDL_INIT_VIDEO) < 0) { fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); exit(1); @@ -132,16 +136,24 @@ static void sdlfb_hw_init(void) sdllock = rt_mutex_create("fb", RT_IPC_FLAG_FIFO); } +#ifdef _WIN32 #include #include +#else +#include +#endif + #include -#include #include #include #include #include +#ifdef _WIN32 static DWORD WINAPI sdl_loop(LPVOID lpParam) +#else +static void *sdl_loop(void *lpParam) +#endif { int quit = 0; SDL_Event event; @@ -284,6 +296,7 @@ static DWORD WINAPI sdl_loop(LPVOID lpParam) /* start sdl thread */ void rt_hw_sdl_start(void) { +#ifdef _WIN32 HANDLE thread; DWORD thread_id; @@ -301,4 +314,15 @@ void rt_hw_sdl_start(void) return; } ResumeThread(thread); +#else + /* Linux */ + pthread_t pid; + int res; + res = pthread_create(&pid, NULL, &sdl_loop, NULL); + if (res) + { + printf("pthread create sdl thread faild, <%d>\n", res); + exit(EXIT_FAILURE); + } +#endif } From 3f3b6b7deb2b87f88c2b4a1ed65bbfa25c2b3621 Mon Sep 17 00:00:00 2001 From: prife Date: Tue, 22 Jan 2013 15:51:15 +0800 Subject: [PATCH 16/19] enable RTGUI in simlinux --- bsp/simlinux/applications/application.c | 4 ++-- bsp/simlinux/rtconfig.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bsp/simlinux/applications/application.c b/bsp/simlinux/applications/application.c index a920daffa0..6394cf736b 100755 --- a/bsp/simlinux/applications/application.c +++ b/bsp/simlinux/applications/application.c @@ -77,8 +77,8 @@ void rt_init_thread_entry(void *parameter) #endif #if defined(RT_USING_RTGUI) - rt_thread_delay(RT_TICK_PER_SECOND); - snake_main(); + //rt_thread_delay(RT_TICK_PER_SECOND); + //snake_main(); #endif } diff --git a/bsp/simlinux/rtconfig.h b/bsp/simlinux/rtconfig.h index 1019f84abf..145cc442fc 100755 --- a/bsp/simlinux/rtconfig.h +++ b/bsp/simlinux/rtconfig.h @@ -197,7 +197,7 @@ #define RT_LWIP_TCP_WND 8192 /* SECTION: RT-Thread/GUI */ -/* #define RT_USING_RTGUI */ +#define RT_USING_RTGUI /* name length of RTGUI object */ #define RTGUI_NAME_MAX 12 From fbbc4c8d183cbe12b649eb2dc750708f534da9bc Mon Sep 17 00:00:00 2001 From: prife Date: Tue, 22 Jan 2013 16:57:47 +0800 Subject: [PATCH 17/19] merge bsp/simlinux into bsp/simulator --- bsp/simlinux/drivers/board.c | 4 +- bsp/simlinux/drivers/usart_sim.c | 4 +- bsp/simulator/SConstruct | 56 ++++++++++--------- bsp/simulator/applications/SConscript | 6 +- bsp/simulator/applications/application.c | 22 +++----- bsp/simulator/drivers/board.c | 37 +++++++++++-- bsp/simulator/drivers/sd_sim.c | 4 +- bsp/simulator/drivers/sdl_fb.c | 28 +++++++++- bsp/simulator/drivers/serial.c | 9 ++- bsp/simulator/drivers/usart_sim.c | 70 ++++++++++++++++++++++-- bsp/simulator/rtconfig.h | 16 ++++-- bsp/simulator/rtconfig.py | 15 +++-- 12 files changed, 201 insertions(+), 70 deletions(-) mode change 100644 => 100755 bsp/simulator/SConstruct mode change 100644 => 100755 bsp/simulator/applications/application.c mode change 100644 => 100755 bsp/simulator/drivers/board.c mode change 100644 => 100755 bsp/simulator/drivers/sd_sim.c mode change 100644 => 100755 bsp/simulator/drivers/sdl_fb.c mode change 100644 => 100755 bsp/simulator/drivers/serial.c mode change 100644 => 100755 bsp/simulator/drivers/usart_sim.c mode change 100644 => 100755 bsp/simulator/rtconfig.h mode change 100644 => 100755 bsp/simulator/rtconfig.py diff --git a/bsp/simlinux/drivers/board.c b/bsp/simlinux/drivers/board.c index c39053e5da..f497561bcb 100755 --- a/bsp/simlinux/drivers/board.c +++ b/bsp/simlinux/drivers/board.c @@ -71,8 +71,8 @@ void rt_hw_exit(void) /* * * getchar reads key from buffer, while finsh need an non-buffer getchar * in windows, getch is such an function, in linux, we had to change - * the behaviour of terminal to get an non-buffer getchar. - * in usart_sim.c, set_stty is called to do this work + * the behaviour of terminal to get an non-buffer getchar. + * in usart_sim.c, set_stty is called to do this work * */ { extern void restore_stty(void); diff --git a/bsp/simlinux/drivers/usart_sim.c b/bsp/simlinux/drivers/usart_sim.c index 0e9c528efe..2cbb1ed345 100755 --- a/bsp/simlinux/drivers/usart_sim.c +++ b/bsp/simlinux/drivers/usart_sim.c @@ -123,7 +123,7 @@ static int savekey(unsigned char key) serial_device.rx_indicate(&serial_device, rx_length); } - return 0; + return 0; } #ifdef _WIN32 static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam) @@ -131,7 +131,7 @@ static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam) static struct termios oldt, newt; /*simulate windows' getch(), it works!!*/ -void set_stty(void) +void set_stty(void) { /* get terminal input's attribute */ tcgetattr(STDIN_FILENO, &oldt); diff --git a/bsp/simulator/SConstruct b/bsp/simulator/SConstruct old mode 100644 new mode 100755 index 0bd7e05ec8..4dd5342cc6 --- a/bsp/simulator/SConstruct +++ b/bsp/simulator/SConstruct @@ -18,38 +18,44 @@ else: sys.path = sys.path + [os.path.join(RTT_ROOT, 'tools')] from building import * -TARGET = 'rtthread-win32.' + rtconfig.TARGET_EXT env = Environment() Export('RTT_ROOT') Export('rtconfig') -libs = Split(''' -winmm -gdi32 -winspool -comdlg32 -advapi32 -shell32 -ole32 -oleaut32 -uuid -odbc32 -odbccp32 -''') -definitions = Split(''' -WIN32 -_DEBUG -_CONSOLE -MSVC -_TIME_T_DEFINED -''') +if rtconfig.PLATFORM == 'cl': + TARGET = 'rtthread-win32.' + rtconfig.TARGET_EXT + + libs = Split(''' + winmm + gdi32 + winspool + comdlg32 + advapi32 + shell32 + ole32 + oleaut32 + uuid + odbc32 + odbccp32 + ''') + definitions = Split(''' + WIN32 + _DEBUG + _CONSOLE + MSVC + _TIME_T_DEFINED + ''') + env.Append(CCFLAGS=rtconfig.CFLAGS) + env.Append(LINKFLAGS=rtconfig.LFLAGS) + env['LIBS']=libs + env['CPPDEFINES']=definitions +else: + TARGET = 'rtthread' + env.Append(CCFLAGS=rtconfig.CFLAGS) + env.Append(LINKFLAGS=rtconfig.LFLAGS) -env.Append(CCFLAGS=rtconfig.CFLAGS) -env.Append(LINKFLAGS=rtconfig.LFLAGS) -env['LIBS']=libs -env['CPPDEFINES']=definitions # prepare building environment diff --git a/bsp/simulator/applications/SConscript b/bsp/simulator/applications/SConscript index 4ccb177207..828b49f77f 100644 --- a/bsp/simulator/applications/SConscript +++ b/bsp/simulator/applications/SConscript @@ -1,7 +1,11 @@ from building import * -cwd = GetCurrentDir() +cwd = GetCurrentDir() src = Glob('*.c') + +# remove no need file. +if GetDepend('RT_USING_DFS_WINSHAREDIR') == False: + SrcRemove(src, 'dfs_win32.c') CPPPATH = [cwd, str(Dir('#'))] group = DefineGroup('Applications', src, depend = [''], CPPPATH = CPPPATH) diff --git a/bsp/simulator/applications/application.c b/bsp/simulator/applications/application.c old mode 100644 new mode 100755 index abd582c3ce..6394cf736b --- a/bsp/simulator/applications/application.c +++ b/bsp/simulator/applications/application.c @@ -18,6 +18,7 @@ #include + void rt_init_thread_entry(void *parameter) { #ifdef RT_USING_LWIP @@ -49,7 +50,8 @@ void rt_init_thread_entry(void *parameter) #ifdef RT_USING_DFS_ELMFAT /* mount sd card fatfs as root directory */ - if (dfs_mount("sd0", "/disk/sd", "elm", 0, 0) == 0) + //if (dfs_mount("sd0", "/disk/sd", "elm", 0, 0) == 0) + if (dfs_mount("sd0", "/", "elm", 0, 0) == 0) rt_kprintf("fatfs initialized!\n"); else rt_kprintf("fatfs initialization failed!\n"); @@ -74,30 +76,23 @@ void rt_init_thread_entry(void *parameter) } #endif -#if 0 - { - extern void application_init(void); - rt_thread_delay(RT_TICK_PER_SECOND); - application_init(); - } -#endif - #if defined(RT_USING_RTGUI) - rt_thread_delay(3000); - snake_main(); + //rt_thread_delay(RT_TICK_PER_SECOND); + //snake_main(); #endif } static void rt_test_thread_entry(void *parameter) { int i; - for (i = 0; i < 10; i++) + for (i = 0; i < 5; i++) { rt_kprintf("hello, world\n"); - rt_thread_delay(100); + rt_thread_delay(RT_TICK_PER_SECOND); } } + int rt_application_init() { rt_thread_t tid; @@ -118,5 +113,4 @@ int rt_application_init() return 0; } - /*@}*/ diff --git a/bsp/simulator/drivers/board.c b/bsp/simulator/drivers/board.c old mode 100644 new mode 100755 index c6320d27b5..dc24d39e20 --- a/bsp/simulator/drivers/board.c +++ b/bsp/simulator/drivers/board.c @@ -16,7 +16,6 @@ #include #include "board.h" #include -#include /** * @addtogroup simulator on win32 @@ -30,29 +29,56 @@ rt_uint8_t *rt_hw_sram_init(void) if (heap == RT_NULL) { rt_kprintf("there is no memory in pc."); +#ifdef _WIN32 _exit(1); +#else + exit(1); +#endif } return heap; } +#ifdef _WIN32 +#include +#endif + void rt_hw_win32_low_cpu(void) { +#ifdef _WIN32 + /* in windows */ Sleep(1000); +#else + /* in linux */ + sleep(1); +#endif } -#if defined(RT_USING_FINSH) - +#ifdef _WIN32 #ifndef _CRT_TERMINATE_DEFINED #define _CRT_TERMINATE_DEFINED _CRTIMP __declspec(noreturn) void __cdecl exit(__in int _Code); _CRTIMP __declspec(noreturn) void __cdecl _exit(__in int _Code); _CRTIMP void __cdecl abort(void); #endif +#endif +#if defined(RT_USING_FINSH) #include void rt_hw_exit(void) { rt_kprintf("RT-Thread, bye\n"); +#if !defined(_WIN32) && defined(__GNUC__) + /* * + * getchar reads key from buffer, while finsh need an non-buffer getchar + * in windows, getch is such an function, in linux, we had to change + * the behaviour of terminal to get an non-buffer getchar. + * in usart_sim.c, set_stty is called to do this work + * */ + { + extern void restore_stty(void); + restore_stty(); + } +#endif exit(0); } FINSH_FUNCTION_EXPORT_ALIAS(rt_hw_exit, exit, exit rt - thread); @@ -66,8 +92,11 @@ void rt_hw_board_init() /* init system memory */ heap = rt_hw_sram_init(); -#if defined(RT_USING_CONSOLE) +//#if defined(RT_USING_USART) rt_hw_usart_init(); +//#endif + +#if defined(RT_USING_CONSOLE) rt_hw_serial_init(); rt_console_set_device(RT_CONSOLE_DEVICE_NAME); #endif diff --git a/bsp/simulator/drivers/sd_sim.c b/bsp/simulator/drivers/sd_sim.c old mode 100644 new mode 100755 index 7c9844bae3..1f39843d3e --- a/bsp/simulator/drivers/sd_sim.c +++ b/bsp/simulator/drivers/sd_sim.c @@ -1,7 +1,7 @@ #include #include -#include -#include +//#include +//#include #include // #define SD_TRACE rt_kprintf diff --git a/bsp/simulator/drivers/sdl_fb.c b/bsp/simulator/drivers/sdl_fb.c old mode 100644 new mode 100755 index 790797f2af..752a6fb93c --- a/bsp/simulator/drivers/sdl_fb.c +++ b/bsp/simulator/drivers/sdl_fb.c @@ -1,6 +1,10 @@ #include +#ifdef _WIN32 #include +#else +#include +#endif #include #include @@ -104,7 +108,7 @@ static void sdlfb_hw_init(void) //_putenv("SDL_VIDEODRIVER=windib"); //if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO) < 0) - if (SDL_Init(SDL_INIT_EVERYTHING) < 0) + if (SDL_Init(SDL_INIT_VIDEO) < 0) { fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); exit(1); @@ -132,16 +136,24 @@ static void sdlfb_hw_init(void) sdllock = rt_mutex_create("fb", RT_IPC_FLAG_FIFO); } +#ifdef _WIN32 #include #include +#else +#include +#endif + #include -#include #include #include #include #include +#ifdef _WIN32 static DWORD WINAPI sdl_loop(LPVOID lpParam) +#else +static void *sdl_loop(void *lpParam) +#endif { int quit = 0; SDL_Event event; @@ -284,6 +296,7 @@ static DWORD WINAPI sdl_loop(LPVOID lpParam) /* start sdl thread */ void rt_hw_sdl_start(void) { +#ifdef _WIN32 HANDLE thread; DWORD thread_id; @@ -301,4 +314,15 @@ void rt_hw_sdl_start(void) return; } ResumeThread(thread); +#else + /* Linux */ + pthread_t pid; + int res; + res = pthread_create(&pid, NULL, &sdl_loop, NULL); + if (res) + { + printf("pthread create sdl thread faild, <%d>\n", res); + exit(EXIT_FAILURE); + } +#endif } diff --git a/bsp/simulator/drivers/serial.c b/bsp/simulator/drivers/serial.c old mode 100644 new mode 100755 index 5a18d98bde..87de4d0498 --- a/bsp/simulator/drivers/serial.c +++ b/bsp/simulator/drivers/serial.c @@ -13,8 +13,12 @@ #include "serial.h" #include struct rt_device serial_device; -extern struct serial_int_rx serial_rx; +//extern struct serial_int_rx serial_rx; +struct serial_int_rx serial_rx; + +#if 0 static FILE *fp = RT_NULL; +#endif /*@{*/ @@ -109,13 +113,16 @@ static rt_size_t rt_serial_write(rt_device_t dev, rt_off_t pos, const void *buff #if _DEBUG_SERIAL==1 printf("in rt_serial_write()\n"); #endif +#if 0 if (fp == NULL) fp = fopen("log.txt", "wb+"); if (fp != NULL) fwrite(buffer, size, 1, fp); +#endif printf("%s", (char *)buffer); + fflush(stdout); return size; } diff --git a/bsp/simulator/drivers/usart_sim.c b/bsp/simulator/drivers/usart_sim.c old mode 100644 new mode 100755 index efc04614cb..2cbb1ed345 --- a/bsp/simulator/drivers/usart_sim.c +++ b/bsp/simulator/drivers/usart_sim.c @@ -1,15 +1,19 @@ #include #include + +#ifdef _WIN32 #include #include -#include #include +#endif +#include #include "serial.h" struct serial_int_rx serial_rx; extern struct rt_device serial_device; +#ifdef _WIN32 /* * Handler for OSKey Thread */ @@ -19,7 +23,6 @@ static DWORD OSKey_ThreadID; static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam); void rt_hw_usart_init(void) { - /* * create serial thread that receive key input from keyboard */ @@ -46,9 +49,31 @@ void rt_hw_usart_init(void) * Start OS get key Thread */ ResumeThread(OSKey_Thread); - } +#else /* POSIX version */ + +#include +#include +#include +#include +#include /* for tcxxxattr, ECHO, etc */ +#include /* for STDIN_FILENO */ + + +static void * ThreadforKeyGet(void * lpParam); +static pthread_t OSKey_Thread; +void rt_hw_usart_init(void) +{ + int res; + res = pthread_create(&OSKey_Thread, NULL, &ThreadforKeyGet, NULL); + if (res) + { + printf("pthread create faild, <%d>\n", res); + exit(EXIT_FAILURE); + } +} +#endif /* * () 0xe04b * () 0xe048 @@ -100,15 +125,48 @@ static int savekey(unsigned char key) } return 0; } +#ifdef _WIN32 static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam) +#else + +static struct termios oldt, newt; +/*simulate windows' getch(), it works!!*/ +void set_stty(void) +{ + /* get terminal input's attribute */ + tcgetattr(STDIN_FILENO, &oldt); + newt = oldt; + + /* set termios' local mode */ + newt.c_lflag &= ~(ECHO|ICANON); + tcsetattr(STDIN_FILENO, TCSANOW, &newt); +} + +void restore_stty(void) +{ + /* recover terminal's attribute */ + tcsetattr(STDIN_FILENO, TCSANOW, &oldt); +} + +#define getch getchar + +static void * ThreadforKeyGet(void * lpParam) +#endif /* not _WIN32*/ { unsigned char key; +#ifndef _WIN32 + sigset_t sigmask, oldmask; + /* set the getchar without buffer */ + sigfillset(&sigmask); + pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask); + set_stty(); +#endif (void)lpParam; //prevent compiler warnings - for (;;) { key = getch(); +#ifdef _WIN32 if (key == 0xE0) { key = getch(); @@ -128,7 +186,7 @@ static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam) continue; } - +#endif savekey(key); } -} /*** ThreadforKeyGet ***/ \ No newline at end of file +} /*** ThreadforKeyGet ***/ diff --git a/bsp/simulator/rtconfig.h b/bsp/simulator/rtconfig.h old mode 100644 new mode 100755 index c138acb438..145cc442fc --- a/bsp/simulator/rtconfig.h +++ b/bsp/simulator/rtconfig.h @@ -2,15 +2,15 @@ #ifndef __RTTHREAD_CFG_H__ #define __RTTHREAD_CFG_H__ +#define RT_HEAP_SIZE (1024*1024*2) + +#if defined(_MSC_VER) /* SECTION: port for visual studio */ -#ifdef _MSC_VER #undef RT_USING_NEWLIB #undef RT_USING_MINILIBC #define NORESOURCE //RT_VESRION in winuser.h #define _CRT_ERRNO_DEFINED //errno macro redefinition -#define RT_HEAP_SIZE (1024*1024*2) - /* disable some warning in MSC */ #pragma warning(disable:4273) /* to ignore: warning C4273: inconsistent dll linkage */ #pragma warning(disable:4312) /* to ignore: warning C4312: 'type cast' : conversion from 'rt_uint32_t' to 'rt_uint32_t *' */ @@ -18,6 +18,9 @@ #pragma warning(disable:4996) /* to ignore: warning C4996: The POSIX name for this item is deprecated. */ #pragma warning(disable:4267) /* to ignore: warning C4267: conversion from 'size_t' to 'rt_size_t', possible loss of data */ #pragma warning(disable:4244) /* to ignore: warning C4244: '=' : conversion from '__w64 int' to 'rt_size_t', possible loss of data */ + +#elif defined(__GNUC__) +#define RT_USING_NOLIBC #endif /* SECTION: basic kernel options */ @@ -31,11 +34,12 @@ #define RT_THREAD_PRIORITY_MAX 32 /* Tick per Second */ -#define RT_TICK_PER_SECOND 1000 +#define RT_TICK_PER_SECOND 100 /* SECTION: RT_DEBUG */ /* Thread Debug */ #define RT_DEBUG +//#define RT_DEBUG_SCHEDULER 1 #define RT_THREAD_DEBUG #define RT_USING_OVERFLOW_CHECK @@ -129,11 +133,11 @@ /* #define RT_UFFS_USE_CHECK_MARK_FUNCITON */ /* DFS: JFFS2 nor flash file system options */ -#define RT_USING_DFS_JFFS2 +//#define RT_USING_DFS_JFFS2 /* DFS: windows share directory mounted to rt-thread/dfs */ /* only used in bsp/simulator */ -#define RT_USING_DFS_WINSHAREDIR +//#define RT_USING_DFS_WINSHAREDIR /* the max number of mounted file system */ #define DFS_FILESYSTEMS_MAX 4 diff --git a/bsp/simulator/rtconfig.py b/bsp/simulator/rtconfig.py old mode 100644 new mode 100755 index 543026c549..da63b843e2 --- a/bsp/simulator/rtconfig.py +++ b/bsp/simulator/rtconfig.py @@ -1,7 +1,9 @@ # toolchains options ARCH='sim' -CPU='win32' #CPU='posix' -CROSS_TOOL='msvc' #win32 +#CPU='win32' +#CPU='posix' +CPU='posix' +CROSS_TOOL='gcc' #msvc # gcc # lcd panel options # 'FMT0371','ILI932X', 'SSD1289' @@ -33,16 +35,19 @@ if PLATFORM == 'gcc': OBJCPY = PREFIX + 'objcopy' DEVICE = ' -ffunction-sections -fdata-sections' - CFLAGS = DEVICE + DEVICE = ' ' + CFLAGS = DEVICE + ' -I/usr/include -w -D_REENTRANT' AFLAGS = ' -c' + DEVICE + ' -x assembler-with-cpp' #LFLAGS = DEVICE + ' -Wl,--gc-sections,-Map=rtthread-linux.map,-cref,-u,Reset_Handler -T stm32_rom.ld' - LFLAGS = DEVICE + ' -Wl,--gc-sections,-Map=rtthread-linux.map -lpthread' + #LFLAGS = DEVICE + ' -Wl,--gc-sections,-Map=rtthread-linux.map -lpthread' + #LFLAGS = DEVICE + ' -Wl,--gc-sections,-Map=rtthread-linux.map -pthread' + LFLAGS = DEVICE + ' -Wl,-Map=rtthread-linux.map -pthread -T gcc.ld' CPATH = '' LPATH = '' if BUILD == 'debug': - CFLAGS += ' -O0 -gdwarf-2' + CFLAGS += ' -g -O0 -gdwarf-2' AFLAGS += ' -gdwarf-2' else: CFLAGS += ' -O2' From f2636ea813cac0b63729ae25127928895d3bb040 Mon Sep 17 00:00:00 2001 From: prife Date: Tue, 22 Jan 2013 17:07:12 +0800 Subject: [PATCH 18/19] clean code in bsp/simulator/application/application.c --- bsp/simulator/applications/application.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/bsp/simulator/applications/application.c b/bsp/simulator/applications/application.c index 6394cf736b..86db4113aa 100755 --- a/bsp/simulator/applications/application.c +++ b/bsp/simulator/applications/application.c @@ -50,8 +50,11 @@ void rt_init_thread_entry(void *parameter) #ifdef RT_USING_DFS_ELMFAT /* mount sd card fatfs as root directory */ - //if (dfs_mount("sd0", "/disk/sd", "elm", 0, 0) == 0) +#ifdef _WIN32 + if (dfs_mount("sd0", "/disk/sd", "elm", 0, 0) == 0) +#else if (dfs_mount("sd0", "/", "elm", 0, 0) == 0) +#endif rt_kprintf("fatfs initialized!\n"); else rt_kprintf("fatfs initialization failed!\n"); @@ -75,11 +78,6 @@ void rt_init_thread_entry(void *parameter) } #endif - -#if defined(RT_USING_RTGUI) - //rt_thread_delay(RT_TICK_PER_SECOND); - //snake_main(); -#endif } static void rt_test_thread_entry(void *parameter) From 832bbb9fe2a0d6aa04729627018536abb75f8dd3 Mon Sep 17 00:00:00 2001 From: prife Date: Tue, 22 Jan 2013 17:08:54 +0800 Subject: [PATCH 19/19] remove bsp/simlinux --- bsp/simlinux/SConscript | 12 - bsp/simlinux/SConstruct | 91 ------ bsp/simlinux/applications/SConscript | 9 - bsp/simlinux/applications/application.c | 116 ------- bsp/simlinux/applications/platform.c | 29 -- bsp/simlinux/applications/startup.c | 92 ------ bsp/simlinux/drivers/SConscript | 22 -- bsp/simlinux/drivers/board.c | 104 ------ bsp/simlinux/drivers/board.h | 33 -- bsp/simlinux/drivers/nanddrv_file.c | 399 ----------------------- bsp/simlinux/drivers/sd_sim.c | 193 ----------- bsp/simlinux/drivers/sdl_fb.c | 328 ------------------- bsp/simlinux/drivers/serial.c | 177 ---------- bsp/simlinux/drivers/serial.h | 22 -- bsp/simlinux/drivers/sst25vfxx_mtd.h | 24 -- bsp/simlinux/drivers/sst25vfxx_mtd_sim.c | 227 ------------- bsp/simlinux/drivers/usart_sim.c | 192 ----------- bsp/simlinux/gcc.ld | 216 ------------ bsp/simlinux/rtconfig.h | 228 ------------- bsp/simlinux/rtconfig.py | 82 ----- 20 files changed, 2596 deletions(-) delete mode 100755 bsp/simlinux/SConscript delete mode 100755 bsp/simlinux/SConstruct delete mode 100755 bsp/simlinux/applications/SConscript delete mode 100755 bsp/simlinux/applications/application.c delete mode 100755 bsp/simlinux/applications/platform.c delete mode 100755 bsp/simlinux/applications/startup.c delete mode 100755 bsp/simlinux/drivers/SConscript delete mode 100755 bsp/simlinux/drivers/board.c delete mode 100755 bsp/simlinux/drivers/board.h delete mode 100755 bsp/simlinux/drivers/nanddrv_file.c delete mode 100755 bsp/simlinux/drivers/sd_sim.c delete mode 100755 bsp/simlinux/drivers/sdl_fb.c delete mode 100755 bsp/simlinux/drivers/serial.c delete mode 100755 bsp/simlinux/drivers/serial.h delete mode 100755 bsp/simlinux/drivers/sst25vfxx_mtd.h delete mode 100755 bsp/simlinux/drivers/sst25vfxx_mtd_sim.c delete mode 100755 bsp/simlinux/drivers/usart_sim.c delete mode 100644 bsp/simlinux/gcc.ld delete mode 100755 bsp/simlinux/rtconfig.h delete mode 100755 bsp/simlinux/rtconfig.py diff --git a/bsp/simlinux/SConscript b/bsp/simlinux/SConscript deleted file mode 100755 index 0992612410..0000000000 --- a/bsp/simlinux/SConscript +++ /dev/null @@ -1,12 +0,0 @@ -from building import * - -cwd = GetCurrentDir() -objs = [] -list = os.listdir(cwd) - -for d in list: - path = os.path.join(cwd, d) - if os.path.isfile(os.path.join(path, 'SConscript')): - objs = objs + SConscript(os.path.join(d, 'SConscript')) - -Return('objs') diff --git a/bsp/simlinux/SConstruct b/bsp/simlinux/SConstruct deleted file mode 100755 index 4dd5342cc6..0000000000 --- a/bsp/simlinux/SConstruct +++ /dev/null @@ -1,91 +0,0 @@ -import os -import sys -import rtconfig - -if os.getenv('RTT_ROOT'): - RTT_ROOT = os.getenv('RTT_ROOT') -else: - RTT_ROOT = os.path.normpath(os.getcwd() + '/../..') - -if os.getenv('RTT_RTGUI'): - RTT_RTGUI = os.getenv('RTT_RTGUI') -else: - # set the rtgui root directory by hand - # empty string means use the RTGUI in svn - # RTT_RTGUI = os.path.normpath(r'F:\Project\git\rt-gui\components\rtgui') - RTT_RTGUI ='' - -sys.path = sys.path + [os.path.join(RTT_ROOT, 'tools')] -from building import * - - -env = Environment() - -Export('RTT_ROOT') -Export('rtconfig') - -if rtconfig.PLATFORM == 'cl': - TARGET = 'rtthread-win32.' + rtconfig.TARGET_EXT - - libs = Split(''' - winmm - gdi32 - winspool - comdlg32 - advapi32 - shell32 - ole32 - oleaut32 - uuid - odbc32 - odbccp32 - ''') - definitions = Split(''' - WIN32 - _DEBUG - _CONSOLE - MSVC - _TIME_T_DEFINED - ''') - env.Append(CCFLAGS=rtconfig.CFLAGS) - env.Append(LINKFLAGS=rtconfig.LFLAGS) - env['LIBS']=libs - env['CPPDEFINES']=definitions -else: - TARGET = 'rtthread' - env.Append(CCFLAGS=rtconfig.CFLAGS) - env.Append(LINKFLAGS=rtconfig.LFLAGS) - - -# prepare building environment - -objs = PrepareBuilding(env, RTT_ROOT, has_libcpu=False, remove_components=['rtgui']) -if GetDepend('RT_USING_RTGUI'): - sdl_lib = ['SDL', 'SDLmain'] - sdl_lib_path = [os.path.abspath('SDL/lib/x86')] - sdl_include_path = [os.path.abspath('SDL/include')] - env.Append(LIBS=sdl_lib) - env.Append(LIBPATH=sdl_lib_path) - env.Append(CPPPATH=sdl_include_path) - - if RTT_RTGUI: - objs += SConscript(os.path.join(RTT_RTGUI, 'SConscript'), - variant_dir='build/components/rtgui', - duplicate=0) - objs = objs + SConscript(RTT_RTGUI+'/../../demo/examples/SConscript', - variant_dir='build/examples/gui', duplicate=0) - else: - objs += SConscript(os.path.join(RTT_ROOT + '/components/rtgui', 'SConscript'), - variant_dir='build/components/rtgui', - duplicate=0) - objs = objs + SConscript(RTT_ROOT + '/examples/gui/SConscript', - variant_dir='build/examples/gui', duplicate=0) - -if GetDepend('RT_USING_TC'): - objs = objs + SConscript(RTT_ROOT + '/examples/kernel/SConscript', variant_dir = 'build/tc/kernel', duplicate=0) - -# build program -program = env.Program(TARGET, objs) - -# end building -EndBuilding(TARGET, program) diff --git a/bsp/simlinux/applications/SConscript b/bsp/simlinux/applications/SConscript deleted file mode 100755 index 4ccb177207..0000000000 --- a/bsp/simlinux/applications/SConscript +++ /dev/null @@ -1,9 +0,0 @@ -from building import * - -cwd = GetCurrentDir() -src = Glob('*.c') -CPPPATH = [cwd, str(Dir('#'))] - -group = DefineGroup('Applications', src, depend = [''], CPPPATH = CPPPATH) - -Return('group') diff --git a/bsp/simlinux/applications/application.c b/bsp/simlinux/applications/application.c deleted file mode 100755 index 6394cf736b..0000000000 --- a/bsp/simlinux/applications/application.c +++ /dev/null @@ -1,116 +0,0 @@ -/* - * File : application.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006, RT-Thread Development Team - * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE - * - * Change Logs: - * Date Author Notes - * 2009-01-05 Bernard the first version - */ - -#include -#include -#include - -#include - - -void rt_init_thread_entry(void *parameter) -{ -#ifdef RT_USING_LWIP - pcap_netif_hw_init(); -#endif - - /* initialization RT-Thread Components */ - rt_components_init(); - - rt_platform_init(); - - /* File system Initialization */ -#ifdef RT_USING_DFS - { -#ifdef RT_USING_DFS_WINSHAREDIR - { - extern rt_err_t rt_win_sharedir_init(const char *name); - extern int dfs_win32_init(void); - - rt_win_sharedir_init("wdd"); - dfs_win32_init(); - - if (dfs_mount("wdd", "/", "wdir", 0, 0) == 0) - rt_kprintf("win32 share directory initialized!\n"); - else - rt_kprintf("win32 share directory initialized failed!\n"); - } -#endif - -#ifdef RT_USING_DFS_ELMFAT - /* mount sd card fatfs as root directory */ - //if (dfs_mount("sd0", "/disk/sd", "elm", 0, 0) == 0) - if (dfs_mount("sd0", "/", "elm", 0, 0) == 0) - rt_kprintf("fatfs initialized!\n"); - else - rt_kprintf("fatfs initialization failed!\n"); -#endif - -#ifdef RT_USING_DFS_UFFS - /* mount uffs as the nand flash file system */ - if (dfs_mount("nand0", "/disk/nand", "uffs", 0, 0) == 0) - rt_kprintf("uffs initialized!\n"); - else - rt_kprintf("uffs initialization failed!\n"); -#endif - -#ifdef RT_USING_DFS_JFFS2 - /* mount jffs2 as the nor flash file system */ - if (dfs_mount("nor", "/disk/nor", "jffs2", 0, 0) == 0) - rt_kprintf("jffs2 initialized!\n"); - else - rt_kprintf("jffs2 initialization failed!\n"); -#endif - - } -#endif - -#if defined(RT_USING_RTGUI) - //rt_thread_delay(RT_TICK_PER_SECOND); - //snake_main(); -#endif -} - -static void rt_test_thread_entry(void *parameter) -{ - int i; - for (i = 0; i < 5; i++) - { - rt_kprintf("hello, world\n"); - rt_thread_delay(RT_TICK_PER_SECOND); - } -} - - -int rt_application_init() -{ - rt_thread_t tid; - - tid = rt_thread_create("init", - rt_init_thread_entry, RT_NULL, - 2048, RT_THREAD_PRIORITY_MAX / 3, 20); - - if (tid != RT_NULL) - rt_thread_startup(tid); - - tid = rt_thread_create("test", - rt_test_thread_entry, RT_NULL, - 2048, RT_THREAD_PRIORITY_MAX * 3 / 4, 20); - if (tid != RT_NULL) - rt_thread_startup(tid); - - return 0; -} - -/*@}*/ diff --git a/bsp/simlinux/applications/platform.c b/bsp/simlinux/applications/platform.c deleted file mode 100755 index 3c7ffdb541..0000000000 --- a/bsp/simlinux/applications/platform.c +++ /dev/null @@ -1,29 +0,0 @@ -#include -#include "board.h" - -void rt_platform_init(void) -{ -#ifdef RT_USING_DFS - /* initialize sd card */ - rt_hw_sdcard_init(); - -#if defined(RT_USING_MTD_NAND) - rt_hw_mtd_nand_init(); -#endif - -#if defined(RT_USING_MTD_NOR) - sst25vfxx_mtd_init("nor", 0, RT_UINT32_MAX); -#endif - -#endif /* RT_USING_DFS */ - -#ifdef RT_USING_RTGUI - /* start sdl thread to simulate an LCD */ - rt_hw_sdl_start(); -#endif /* RT_USING_RTGUI */ - -#ifdef _WIN32 - rt_thread_idle_sethook(rt_hw_win32_low_cpu); -#endif -} - diff --git a/bsp/simlinux/applications/startup.c b/bsp/simlinux/applications/startup.c deleted file mode 100755 index 2b21c69016..0000000000 --- a/bsp/simlinux/applications/startup.c +++ /dev/null @@ -1,92 +0,0 @@ -/* - * File : startup.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006, RT-Thread Develop Team - * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://openlab.rt-thread.com/license/LICENSE - * - * Change Logs: - * Date Author Notes - * 2012-09-03 prife first implementation - */ - -#include -#include - -#include "board.h" - -/** - * @addtogroup win32 - */ - -/*@{*/ - -extern int rt_application_init(void); -#ifdef RT_USING_FINSH -extern void finsh_system_init(void); -extern void finsh_set_device(const char *device); -#endif - -extern rt_uint8_t *heap; -/** - * This function will startup RT-Thread RTOS. - */ -void rtthread_startup(void) -{ - /* init board */ - rt_hw_board_init(); - - /* show version */ - rt_show_version(); - - /* init tick */ - rt_system_tick_init(); - - /* init kernel object */ - rt_system_object_init(); - - /* init timer system */ - rt_system_timer_init(); - -#ifdef RT_USING_HEAP - /* init memory system */ - rt_system_heap_init((void *)heap, (void *)&heap[RT_HEAP_SIZE - 1]); -#endif - - /* init scheduler system */ - rt_system_scheduler_init(); - - /* init all device */ -#ifdef RT_USING_DEVICE - rt_device_init_all(); -#endif - /* init application */ - rt_application_init(); - - /* init timer thread */ - rt_system_timer_thread_init(); - - /* init idle thread */ - rt_thread_idle_init(); - - /* start scheduler */ - rt_system_scheduler_start(); - - /* never reach here */ - return ; -} - -int main(void) -{ - /* disable interrupt first */ - rt_hw_interrupt_disable(); - - /* startup RT-Thread RTOS */ - rtthread_startup(); - - return 0; -} - -/*@}*/ diff --git a/bsp/simlinux/drivers/SConscript b/bsp/simlinux/drivers/SConscript deleted file mode 100755 index 079d329c0e..0000000000 --- a/bsp/simlinux/drivers/SConscript +++ /dev/null @@ -1,22 +0,0 @@ -from building import * - -cwd = GetCurrentDir() -src = Glob('*.c') - -# remove no need file. -if GetDepend('RT_USING_RTGUI') == False: - SrcRemove(src, 'sdl_fb.c') -if GetDepend('RT_USING_DFS') == False or GetDepend('RT_USING_DFS_ELMFAT') == False: - SrcRemove(src, 'sd_sim.c') -if GetDepend('RT_USING_DFS') == False or GetDepend('RT_USING_MTD_NAND') == False: - SrcRemove(src, 'nanddrv_file.c') -if GetDepend('RT_USING_DFS') == False or GetDepend('RT_USING_MTD_NOR') == False: - SrcRemove(src, 'sst25vfxx_mtd_sim.c') -#if GetDepend('RT_USING_SERIAL') == False: -# SrcRemove(src, 'usart_sim.c') - -CPPPATH = [cwd] - -group = DefineGroup('Drivers', src, depend = [''], CPPPATH = CPPPATH) - -Return('group') diff --git a/bsp/simlinux/drivers/board.c b/bsp/simlinux/drivers/board.c deleted file mode 100755 index f497561bcb..0000000000 --- a/bsp/simlinux/drivers/board.c +++ /dev/null @@ -1,104 +0,0 @@ -/* - * File : board.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2009 RT-Thread Develop Team - * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE - * - * Change Logs: - * Date Author Notes - * 2009-01-05 Bernard first implementation - */ - -#include -#include -#include "board.h" -#include - -/** - * @addtogroup simulator on win32 - */ -rt_uint8_t *heap; - -rt_uint8_t *rt_hw_sram_init(void) -{ - rt_uint8_t *heap; - heap = malloc(RT_HEAP_SIZE); - if (heap == RT_NULL) - { - rt_kprintf("there is no memory in pc."); -#ifdef _WIN32 - _exit(1); -#else - exit(1); -#endif - } - return heap; -} - -#ifdef _WIN32 -#include -#endif - -void rt_hw_win32_low_cpu(void) -{ -#ifdef _WIN32 - /* in windows */ - Sleep(1000); -#else - /* in linux */ - sleep(1); -#endif -} - -#ifdef _WIN32 -#ifndef _CRT_TERMINATE_DEFINED -#define _CRT_TERMINATE_DEFINED -_CRTIMP __declspec(noreturn) void __cdecl exit(__in int _Code); -_CRTIMP __declspec(noreturn) void __cdecl _exit(__in int _Code); -_CRTIMP void __cdecl abort(void); -#endif -#endif - -#if defined(RT_USING_FINSH) -#include -void rt_hw_exit(void) -{ - rt_kprintf("RT-Thread, bye\n"); -#if !defined(_WIN32) && defined(__GNUC__) - /* * - * getchar reads key from buffer, while finsh need an non-buffer getchar - * in windows, getch is such an function, in linux, we had to change - * the behaviour of terminal to get an non-buffer getchar. - * in usart_sim.c, set_stty is called to do this work - * */ - { - extern void restore_stty(void); - restore_stty(); - } -#endif - exit(0); -} -FINSH_FUNCTION_EXPORT_ALIAS(rt_hw_exit, exit, exit rt - thread); -#endif /* RT_USING_FINSH */ - -/** - * This function will initial win32 - */ -void rt_hw_board_init() -{ - /* init system memory */ - heap = rt_hw_sram_init(); - -//#if defined(RT_USING_USART) - rt_hw_usart_init(); -//#endif - -#if defined(RT_USING_CONSOLE) - rt_hw_serial_init(); - rt_console_set_device(RT_CONSOLE_DEVICE_NAME); -#endif -} -/*@}*/ diff --git a/bsp/simlinux/drivers/board.h b/bsp/simlinux/drivers/board.h deleted file mode 100755 index 7edb856505..0000000000 --- a/bsp/simlinux/drivers/board.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * File : board.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2009, RT-Thread Development Team - * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE - * - * Change Logs: - * Date Author Notes - * 2009-09-22 Bernard add board.h to this bsp - */ - -#ifndef __BOARD_H__ -#define __BOARD_H__ -void rt_hw_board_init(void); -rt_uint8_t *rt_hw_sram_init(void); - -/* SD Card init function */ -void rt_hw_sdcard_init(void); - -int rt_hw_mtd_nand_init(void); -int sst25vfxx_mtd_init(const char *, unsigned int , unsigned int); -void pcap_netif_hw_init(void); -void rt_platform_init(void); -void rt_hw_usart_init(void); -void rt_hw_serial_init(void); -void rt_hw_sdl_start(void); -void rt_hw_win32_low_cpu(void); - -void rt_hw_exit(void); -#endif diff --git a/bsp/simlinux/drivers/nanddrv_file.c b/bsp/simlinux/drivers/nanddrv_file.c deleted file mode 100755 index 770751ac79..0000000000 --- a/bsp/simlinux/drivers/nanddrv_file.c +++ /dev/null @@ -1,399 +0,0 @@ -#include -#include -#include -#include - -#define NAND_SIM "nand.bin" -#if 1 -#define OOB_SIZE 64 -#define PAGE_DATA_SIZE 2048 -#define PAGE_SIZE (2048 + 64) -#define PAGE_PER_BLOCK 64 -#define BLOCK_SIZE (PAGE_SIZE * PAGE_PER_BLOCK) -#define BLOCK_NUM 512 -// #define BLOCK_NUM 2048 -#else -#define OOB_SIZE 16 -#define PAGE_SIZE (512 + OOB_SIZE) -#define PAGE_PER_BLOCK 32 -#define BLOCK_SIZE (PAGE_SIZE * PAGE_PER_BLOCK) -#define BLOCK_NUM 512 -#endif - -#define ECC_SIZE ((PAGE_DATA_SIZE) * 3 / 256) - -static unsigned char block_data[BLOCK_SIZE]; -static struct rt_mtd_nand_device _nanddrv_file_device; -static FILE *file = NULL; - -static rt_uint8_t CountBitsInByte(rt_uint8_t byte) -{ - rt_uint8_t count = 0; - - while (byte > 0) - { - if (byte & 1) - { - count++; - } - byte >>= 1; - } - - return count; -} - -static void Compute256(const rt_uint8_t *data, rt_uint8_t *code) -{ - rt_uint32_t i; - rt_uint8_t columnSum = 0; - rt_uint8_t evenLineCode = 0; - rt_uint8_t oddLineCode = 0; - rt_uint8_t evenColumnCode = 0; - rt_uint8_t oddColumnCode = 0; - - // Xor all bytes together to get the column sum; - // At the same time, calculate the even and odd line codes - for (i = 0; i < 256; i++) - { - columnSum ^= data[i]; - - // If the xor sum of the byte is 0, then this byte has no incidence on - // the computed code; so check if the sum is 1. - if ((CountBitsInByte(data[i]) & 1) == 1) - { - // Parity groups are formed by forcing a particular index bit to 0 - // (even) or 1 (odd). - // Example on one byte: - // - // bits (dec) 7 6 5 4 3 2 1 0 - // (bin) 111 110 101 100 011 010 001 000 - // '---'---'---'----------. - // | - // groups P4' ooooooooooooooo eeeeeeeeeeeeeee P4 | - // P2' ooooooo eeeeeee ooooooo eeeeeee P2 | - // P1' ooo eee ooo eee ooo eee ooo eee P1 | - // | - // We can see that: | - // - P4 -> bit 2 of index is 0 --------------------' - // - P4' -> bit 2 of index is 1. - // - P2 -> bit 1 of index if 0. - // - etc... - // We deduce that a bit position has an impact on all even Px if - // the log2(x)nth bit of its index is 0 - // ex: log2(4) = 2, bit2 of the index must be 0 (-> 0 1 2 3) - // and on all odd Px' if the log2(x)nth bit of its index is 1 - // ex: log2(2) = 1, bit1 of the index must be 1 (-> 0 1 4 5) - // - // As such, we calculate all the possible Px and Px' values at the - // same time in two variables, evenLineCode and oddLineCode, such as - // evenLineCode bits: P128 P64 P32 P16 P8 P4 P2 P1 - // oddLineCode bits: P128' P64' P32' P16' P8' P4' P2' P1' - // - evenLineCode ^= (255 - i); - oddLineCode ^= i; - } - } - - // At this point, we have the line parities, and the column sum. First, We - // must caculate the parity group values on the column sum. - for (i = 0; i < 8; i++) - { - if (columnSum & 1) - { - evenColumnCode ^= (7 - i); - oddColumnCode ^= i; - } - columnSum >>= 1; - } - - // Now, we must interleave the parity values, to obtain the following layout: - // Code[0] = Line1 - // Code[1] = Line2 - // Code[2] = Column - // Line = Px' Px P(x-1)- P(x-1) ... - // Column = P4' P4 P2' P2 P1' P1 PadBit PadBit - code[0] = 0; - code[1] = 0; - code[2] = 0; - - for (i = 0; i < 4; i++) - { - code[0] <<= 2; - code[1] <<= 2; - code[2] <<= 2; - - // Line 1 - if ((oddLineCode & 0x80) != 0) - { - code[0] |= 2; - } - - if ((evenLineCode & 0x80) != 0) - { - code[0] |= 1; - } - - // Line 2 - if ((oddLineCode & 0x08) != 0) - { - code[1] |= 2; - } - - if ((evenLineCode & 0x08) != 0) - { - code[1] |= 1; - } - - // Column - if ((oddColumnCode & 0x04) != 0) - { - code[2] |= 2; - } - - if ((evenColumnCode & 0x04) != 0) - { - code[2] |= 1; - } - - oddLineCode <<= 1; - evenLineCode <<= 1; - oddColumnCode <<= 1; - evenColumnCode <<= 1; - } - - // Invert codes (linux compatibility) - code[0] = (~(rt_uint32_t)code[0]); - code[1] = (~(rt_uint32_t)code[1]); - code[2] = (~(rt_uint32_t)code[2]); -} - -void ecc_hamming_compute256x(const rt_uint8_t *pucData, rt_uint32_t dwSize, rt_uint8_t *puCode) -{ - while (dwSize > 0) - { - Compute256(pucData, puCode) ; - - pucData += 256; - puCode += 3; - dwSize -= 256; - } -} - -/* read chip id */ -static rt_uint32_t nanddrv_file_read_id(struct rt_mtd_nand_device *device) -{ - return 0x00; -} - -/* read/write/move page */ -static rt_err_t nanddrv_file_read_page(struct rt_mtd_nand_device *device, - rt_off_t page, - rt_uint8_t *data, rt_uint32_t data_len, - rt_uint8_t *spare, rt_uint32_t spare_len) -{ - rt_uint32_t offset; - rt_uint8_t oob_ecc [ECC_SIZE]; - rt_uint8_t ecc [ECC_SIZE]; - - page = page + device->block_start * device->pages_per_block; - - if (page / device->pages_per_block > device->block_end) - { - return -RT_EIO; - } - - /* write page */ - offset = page * PAGE_SIZE; - if (data != NULL && data_len != 0) - { - fseek(file, offset, SEEK_SET); - fread(data, data_len, 1, file); - if (data_len == PAGE_DATA_SIZE) - { - /* read ecc size */ - fread(oob_ecc, ECC_SIZE, 1, file); - - /* verify ECC */ - ecc_hamming_compute256x(data, PAGE_DATA_SIZE, &ecc[0]); - if (memcmp(&oob_ecc[0], &ecc[0], ECC_SIZE) != 0) - return -RT_MTD_EECC; - } - } - - if (spare != NULL && spare_len) - { - offset = page * PAGE_SIZE + PAGE_DATA_SIZE; - fseek(file, offset, SEEK_SET); - fread(spare, spare_len, 1, file); - } - - return RT_EOK; -} - -static rt_err_t nanddrv_file_write_page(struct rt_mtd_nand_device *device, - rt_off_t page, - const rt_uint8_t *data, rt_uint32_t data_len, - const rt_uint8_t *oob, rt_uint32_t spare_len) -{ - rt_uint32_t offset; - rt_uint8_t ecc[ECC_SIZE]; - - page = page + device->block_start * device->pages_per_block; - if (page / device->pages_per_block > device->block_end) - { - return -RT_EIO; - } - - /* write page */ - offset = page * PAGE_SIZE; - if (data != RT_NULL && data_len != 0) - { - fseek(file, offset, SEEK_SET); - fwrite(data, data_len, 1, file); - - if (data_len == PAGE_DATA_SIZE) - { - /*write the ecc information */ - ecc_hamming_compute256x(data, PAGE_DATA_SIZE, ecc); - - fwrite(ecc, ECC_SIZE, 1, file); - } - } - - if (oob != RT_NULL && spare_len != 0) - { - offset = page * PAGE_SIZE + PAGE_DATA_SIZE + ECC_SIZE; - fseek(file, offset, SEEK_SET); - fwrite(&oob[ECC_SIZE], spare_len-ECC_SIZE, 1, file); - } - - return RT_EOK; -} - -static rt_err_t nanddrv_file_move_page(struct rt_mtd_nand_device *device, rt_off_t from, rt_off_t to) -{ - rt_uint32_t offset; - rt_uint8_t page_buffer[PAGE_DATA_SIZE]; - rt_uint8_t oob_buffer[OOB_SIZE]; - - from = from + device->block_start * device->pages_per_block; - to = to + device->block_start * device->pages_per_block; - - if (from / device->pages_per_block > device->block_end || - to / device->pages_per_block > device->block_end) - { - return -RT_EIO; - } - - if (device->plane_num > 1) - { - rt_uint32_t mask; - rt_uint16_t from_block, to_block; - - from_block = (rt_uint16_t)(from / PAGE_PER_BLOCK); - to_block = (rt_uint16_t)(to / PAGE_PER_BLOCK); - mask = device->plane_num - 1; - - if ((from_block & mask) != (to_block & mask)) - { - rt_kprintf("invalid page copy on the block. from [%d] --> to[%d]\n", from_block, to_block); - return -RT_EIO; - } - } - - /* read page */ - offset = from * PAGE_SIZE; - fseek(file, offset, SEEK_SET); - fread(page_buffer, sizeof(page_buffer), 1, file); - fread(oob_buffer, sizeof(oob_buffer), 1, file); - - /* write page */ - offset = to * PAGE_SIZE; - fseek(file, offset, SEEK_SET); - fwrite(page_buffer, sizeof(page_buffer), 1, file); - fwrite(oob_buffer, sizeof(oob_buffer), 1, file); - - return RT_EOK; -} - -/* erase block */ -static rt_err_t nanddrv_file_erase_block(struct rt_mtd_nand_device *device, rt_uint32_t block) -{ - if (block > BLOCK_NUM) return -RT_EIO; - - /* add the start blocks */ - block = block + device->block_start; - - fseek(file, block * BLOCK_SIZE, SEEK_SET); - fwrite(block_data, sizeof(block_data), 1, file); - - return RT_EOK; -} - -const static struct rt_mtd_nand_driver_ops _ops = -{ - nanddrv_file_read_id, - nanddrv_file_read_page, - nanddrv_file_write_page, - nanddrv_file_move_page, - nanddrv_file_erase_block, - RT_NULL, - RT_NULL, -}; - -void nand_eraseall(void); - -void rt_hw_mtd_nand_init(void) -{ - rt_uint16_t ecc_size; - rt_uint32_t size; - - memset(block_data, 0xff, sizeof(block_data)); - /* open file */ - file = fopen(NAND_SIM, "rb+"); - if (file == NULL) - { - file = fopen(NAND_SIM, "wb+"); - } - fseek(file, 0, SEEK_END); - size = ftell(file); - - fseek(file, 0, SEEK_SET); - if (size < BLOCK_NUM * BLOCK_SIZE) - { - rt_uint32_t index; - fseek(file, 0, SEEK_SET); - for (index = 0; index < BLOCK_NUM; index ++) - { - fwrite(block_data, sizeof(block_data), 1, file); - } - } - fseek(file, 0, SEEK_SET); - - ecc_size = (PAGE_DATA_SIZE) * 3 / 256; - _nanddrv_file_device.plane_num = 2; - _nanddrv_file_device.oob_size = OOB_SIZE; - _nanddrv_file_device.oob_free = OOB_SIZE - ecc_size; - _nanddrv_file_device.page_size = PAGE_DATA_SIZE; - _nanddrv_file_device.pages_per_block = PAGE_PER_BLOCK; - _nanddrv_file_device.block_start = 0; - _nanddrv_file_device.block_end = BLOCK_NUM / 2; - _nanddrv_file_device.block_total = _nanddrv_file_device.block_end - _nanddrv_file_device.block_start; - _nanddrv_file_device.ops = &_ops; - - rt_mtd_nand_register_device("nand0", &_nanddrv_file_device); -} - -#if defined(RT_USING_FINSH) -#include -void nand_eraseall() -{ - int index; - for (index = 0; index < _nanddrv_file_device.block_total; index ++) - { - nanddrv_file_erase_block(&_nanddrv_file_device, index); - } -} -FINSH_FUNCTION_EXPORT(nand_eraseall, erase all of block in the nand flash); - -#endif //RT_USING_FINSH diff --git a/bsp/simlinux/drivers/sd_sim.c b/bsp/simlinux/drivers/sd_sim.c deleted file mode 100755 index 1f39843d3e..0000000000 --- a/bsp/simlinux/drivers/sd_sim.c +++ /dev/null @@ -1,193 +0,0 @@ -#include -#include -//#include -//#include -#include - -// #define SD_TRACE rt_kprintf -#define SD_TRACE(...) - -//#define SDCARD_SIM "F:\\Project\\tools\\SDCARD" -#define SDCARD_SIM "sd.bin" -#define SDCARD_SIZE (16*1024*1024) //16M - -struct sdcard_device -{ - struct rt_device parent; - FILE *file; -}; -static struct sdcard_device _sdcard; - -#define SDCARD_DEVICE(device) (( struct sdcard_device*)(device)) - -static rt_mutex_t lock; - -/* RT-Thread device interface */ - -static rt_err_t rt_sdcard_init(rt_device_t dev) -{ - return RT_EOK; -} - -static rt_err_t rt_sdcard_open(rt_device_t dev, rt_uint16_t oflag) -{ - return RT_EOK; -} - -static rt_err_t rt_sdcard_close(rt_device_t dev) -{ - return RT_EOK; -} - -/* position: block page address, not bytes address - * buffer: - * size : how many blocks - */ -static rt_size_t rt_sdcard_read(rt_device_t device, rt_off_t position, void *buffer, rt_size_t size) -{ - struct sdcard_device *sd; - int result = 0; - - SD_TRACE("sd read: pos %d, size %d\n", position, size); - - rt_mutex_take(lock, RT_WAITING_FOREVER); - sd = SDCARD_DEVICE(device); - fseek(sd->file, position * SECTOR_SIZE, SEEK_SET); - - result = fread(buffer, size * SECTOR_SIZE, 1, sd->file); - if (result < 0) - goto _err; - - rt_mutex_release(lock); - return size; - -_err: - SD_TRACE("sd read errors!\n"); - rt_mutex_release(lock); - return 0; -} - -/* position: block page address, not bytes address - * buffer: - * size : how many blocks - */ -static rt_size_t rt_sdcard_write(rt_device_t device, rt_off_t position, const void *buffer, rt_size_t size) -{ - struct sdcard_device *sd; - int result = 0; - - SD_TRACE("sst write: pos %d, size %d\n", position, size); - - rt_mutex_take(lock, RT_WAITING_FOREVER); - sd = SDCARD_DEVICE(device); - fseek(sd->file, position * SECTOR_SIZE, SEEK_SET); - - result = fwrite(buffer, size * SECTOR_SIZE, 1, sd->file); - if (result < 0) - goto _err; - - rt_mutex_release(lock); - return size; - -_err: - SD_TRACE("sd write errors!\n"); - rt_mutex_release(lock); - return 0; -} - -static rt_err_t rt_sdcard_control(rt_device_t dev, rt_uint8_t cmd, void *args) -{ - struct sdcard_device *sd; - unsigned int size; - - RT_ASSERT(dev != RT_NULL); - - sd = SDCARD_DEVICE(dev); - - if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME) - { - struct rt_device_blk_geometry *geometry; - - geometry = (struct rt_device_blk_geometry *)args; - if (geometry == RT_NULL) return -RT_ERROR; - - geometry->bytes_per_sector = SECTOR_SIZE; - geometry->block_size = SECTOR_SIZE; - - fseek(sd->file, 0, SEEK_END); - size = ftell(sd->file); - - geometry->sector_count = size / SECTOR_SIZE; - } - return RT_EOK; -} - - -rt_err_t rt_hw_sdcard_init(const char *spi_device_name) -{ - int size; - struct sdcard_device *sd; - struct rt_device *device; - - sd = &_sdcard; - device = &(sd->parent); - - lock = rt_mutex_create("lock", RT_IPC_FLAG_FIFO); - - /* open sd card file, if not exist, then create it */ - sd->file = fopen(SDCARD_SIM, "rb+"); - if (sd->file == NULL) - { - /* create a file to simulate sd card */ - sd->file = fopen(SDCARD_SIM, "wb+"); - - fseek(sd->file, 0, SEEK_END); - size = ftell(sd->file); - - fseek(sd->file, 0, SEEK_SET); - if (size < SDCARD_SIZE) - { - int i; - unsigned char *ptr; - - ptr = (unsigned char *) malloc(1024 * 1024); - if (ptr == NULL) - { - SD_TRACE("malloc error, no memory!\n"); - return RT_ERROR; - } - memset(ptr, 0x0, 1024 * 1024); - - fseek(sd->file, 0, SEEK_SET); - - for (i = 0; i < (SDCARD_SIZE / (1024 * 1024)); i++) - fwrite(ptr, 1024 * 1024, 1, sd->file); - - free(ptr); - } - } - fseek(sd->file, 0, SEEK_SET); - - device->type = RT_Device_Class_Block; - device->init = rt_sdcard_init; - device->open = rt_sdcard_open; - device->close = rt_sdcard_close; - device->read = rt_sdcard_read; - device->write = rt_sdcard_write; - device->control = rt_sdcard_control; - device->user_data = NULL; - - rt_device_register(device, "sd0", - RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE); - - return RT_EOK; -} - -#ifdef RT_USING_FINSH -#include -void eraseall(void) -{ - printf("had not implemented yet!\n"); -} -FINSH_FUNCTION_EXPORT(eraseall, erase all block in SPI flash); -#endif diff --git a/bsp/simlinux/drivers/sdl_fb.c b/bsp/simlinux/drivers/sdl_fb.c deleted file mode 100755 index 752a6fb93c..0000000000 --- a/bsp/simlinux/drivers/sdl_fb.c +++ /dev/null @@ -1,328 +0,0 @@ -#include - -#ifdef _WIN32 -#include -#else -#include -#endif -#include -#include - -#define SDL_SCREEN_WIDTH 800 -#define SDL_SCREEN_HEIGHT 480 - -struct sdlfb_device -{ - struct rt_device parent; - - SDL_Surface *screen; - rt_uint16_t width; - rt_uint16_t height; -}; -struct sdlfb_device _device; - -/* common device interface */ -static rt_err_t sdlfb_init(rt_device_t dev) -{ - return RT_EOK; -} -static rt_err_t sdlfb_open(rt_device_t dev, rt_uint16_t oflag) -{ - return RT_EOK; -} -static rt_err_t sdlfb_close(rt_device_t dev) -{ - SDL_Quit(); - return RT_EOK; -} - -static rt_mutex_t sdllock; -static rt_err_t sdlfb_control(rt_device_t dev, rt_uint8_t cmd, void *args) -{ - struct sdlfb_device *device; - - rt_mutex_take(sdllock, RT_WAITING_FOREVER); - device = (struct sdlfb_device *)dev; - RT_ASSERT(device != RT_NULL); - RT_ASSERT(device->screen != RT_NULL); - - switch (cmd) - { - case RTGRAPHIC_CTRL_GET_INFO: - { - struct rt_device_graphic_info *info; - - info = (struct rt_device_graphic_info *) args; - info->bits_per_pixel = 16; - info->pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565P; - info->framebuffer = device->screen->pixels; - info->width = device->screen->w; - info->height = device->screen->h; - } - break; - case RTGRAPHIC_CTRL_RECT_UPDATE: - { - struct rt_device_rect_info *rect; - rect = (struct rt_device_rect_info *)args; - - /* SDL_UpdateRect(_device.screen, rect->x, rect->y, rect->x + rect->w, rect->y + rect->h); */ - SDL_UpdateRect(_device.screen, 0, 0, device->width, device->height); - } - break; - case RTGRAPHIC_CTRL_SET_MODE: - { -#if 0 - struct rt_device_rect_info *rect; - - rect = (struct rt_device_rect_info *)args; - if ((_device.width == rect->width) && (_device.height == rect->height)) return -RT_ERROR; - - _device.width = rect->width; - _device.height = rect->height; - - if (_device.screen != RT_NULL) - { - SDL_FreeSurface(_device.screen); - - /* re-create screen surface */ - _device.screen = SDL_SetVideoMode(_device.width, _device.height, 16, SDL_SWSURFACE | SDL_DOUBLEBUF); - if (_device.screen == NULL) - { - fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError()); - exit(1); - } - - SDL_WM_SetCaption("RT-Thread/GUI Simulator", NULL); - } -#endif - } - break; - } - rt_mutex_release(sdllock); - return RT_EOK; -} - -static void sdlfb_hw_init(void) -{ - /* set video driver for VC++ debug */ - //_putenv("SDL_VIDEODRIVER=windib"); - - //if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO) < 0) - if (SDL_Init(SDL_INIT_VIDEO) < 0) - { - fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); - exit(1); - } - - _device.parent.init = sdlfb_init; - _device.parent.open = sdlfb_open; - _device.parent.close = sdlfb_close; - _device.parent.read = RT_NULL; - _device.parent.write = RT_NULL; - _device.parent.control = sdlfb_control; - - _device.width = SDL_SCREEN_WIDTH; - _device.height = SDL_SCREEN_HEIGHT; - _device.screen = SDL_SetVideoMode(_device.width, _device.height, 16, SDL_SWSURFACE | SDL_DOUBLEBUF); - if (_device.screen == NULL) - { - fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError()); - exit(1); - } - - SDL_WM_SetCaption("RT-Thread/GUI Simulator", NULL); - rt_device_register(RT_DEVICE(&_device), "sdl", RT_DEVICE_FLAG_RDWR); - - sdllock = rt_mutex_create("fb", RT_IPC_FLAG_FIFO); -} - -#ifdef _WIN32 -#include -#include -#else -#include -#endif - -#include -#include -#include -#include -#include - -#ifdef _WIN32 -static DWORD WINAPI sdl_loop(LPVOID lpParam) -#else -static void *sdl_loop(void *lpParam) -#endif -{ - int quit = 0; - SDL_Event event; - int button_state = 0; - - rt_device_t device; - sdlfb_hw_init(); - - device = rt_device_find("sdl"); - rtgui_graphic_set_device(device); - - /* handle SDL event */ - while (!quit) - { - SDL_WaitEvent(&event); - - switch (event.type) - { - case SDL_MOUSEMOTION: -#if 0 - { - struct rtgui_event_mouse emouse; - emouse.parent.type = RTGUI_EVENT_MOUSE_MOTION; - emouse.parent.sender = RT_NULL; - emouse.wid = RT_NULL; - - emouse.x = ((SDL_MouseMotionEvent *)&event)->x; - emouse.y = ((SDL_MouseMotionEvent *)&event)->y; - - /* init mouse button */ - emouse.button = button_state; - - /* send event to server */ - rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse)); - } -#endif - break; - - case SDL_MOUSEBUTTONDOWN: - case SDL_MOUSEBUTTONUP: - { - struct rtgui_event_mouse emouse; - SDL_MouseButtonEvent *mb; - - emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON; - emouse.parent.sender = RT_NULL; - emouse.wid = RT_NULL; - - mb = (SDL_MouseButtonEvent *)&event; - - emouse.x = mb->x; - emouse.y = mb->y; - - /* init mouse button */ - emouse.button = 0; - - /* set emouse button */ - if (mb->button & (1 << (SDL_BUTTON_LEFT - 1))) - { - emouse.button |= RTGUI_MOUSE_BUTTON_LEFT; - } - else if (mb->button & (1 << (SDL_BUTTON_RIGHT - 1))) - { - emouse.button |= RTGUI_MOUSE_BUTTON_RIGHT; - } - else if (mb->button & (1 << (SDL_BUTTON_MIDDLE - 1))) - { - emouse.button |= RTGUI_MOUSE_BUTTON_MIDDLE; - } - - if (mb->type == SDL_MOUSEBUTTONDOWN) - { - emouse.button |= RTGUI_MOUSE_BUTTON_DOWN; - button_state = emouse.button; - } - else - { - emouse.button |= RTGUI_MOUSE_BUTTON_UP; - button_state = 0; - } - - - /* send event to server */ - rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse)); - } - break; - - case SDL_KEYUP: - { - struct rtgui_event_kbd ekbd; - ekbd.parent.type = RTGUI_EVENT_KBD; - ekbd.parent.sender = RT_NULL; - ekbd.type = RTGUI_KEYUP; - ekbd.wid = RT_NULL; - ekbd.mod = event.key.keysym.mod; - ekbd.key = event.key.keysym.sym; - - /* FIXME: unicode */ - ekbd.unicode = 0; - - /* send event to server */ - rtgui_server_post_event(&ekbd.parent, sizeof(struct rtgui_event_kbd)); - } - break; - - case SDL_KEYDOWN: - { - struct rtgui_event_kbd ekbd; - ekbd.parent.type = RTGUI_EVENT_KBD; - ekbd.parent.sender = RT_NULL; - ekbd.type = RTGUI_KEYDOWN; - ekbd.wid = RT_NULL; - ekbd.mod = event.key.keysym.mod; - ekbd.key = event.key.keysym.sym; - - /* FIXME: unicode */ - ekbd.unicode = 0; - - /* send event to server */ - rtgui_server_post_event(&ekbd.parent, sizeof(struct rtgui_event_kbd)); - } - break; - - case SDL_QUIT: - SDL_Quit(); - quit = 1; - break; - - default: - break; - } - - if (quit) - break; - } - //exit(0); - return 0; -} - -/* start sdl thread */ -void rt_hw_sdl_start(void) -{ -#ifdef _WIN32 - HANDLE thread; - DWORD thread_id; - - /* create thread that loop sdl event */ - thread = CreateThread(NULL, - 0, - (LPTHREAD_START_ROUTINE)sdl_loop, - 0, - CREATE_SUSPENDED, - &thread_id); - if (thread == NULL) - { - //Display Error Message - - return; - } - ResumeThread(thread); -#else - /* Linux */ - pthread_t pid; - int res; - res = pthread_create(&pid, NULL, &sdl_loop, NULL); - if (res) - { - printf("pthread create sdl thread faild, <%d>\n", res); - exit(EXIT_FAILURE); - } -#endif -} diff --git a/bsp/simlinux/drivers/serial.c b/bsp/simlinux/drivers/serial.c deleted file mode 100755 index 87de4d0498..0000000000 --- a/bsp/simlinux/drivers/serial.c +++ /dev/null @@ -1,177 +0,0 @@ -/* -****************************************************************************** -* By : parai -* email:parai@foxmail.com -* virtual serial driver -****************************************************************************** -*/ - -#include -#include - -#define _DEBUG_SERIAL 0 -#include "serial.h" -#include -struct rt_device serial_device; -//extern struct serial_int_rx serial_rx; -struct serial_int_rx serial_rx; - -#if 0 -static FILE *fp = RT_NULL; -#endif - -/*@{*/ - -/* RT-Thread Device Interface */ -/** - * This function initializes serial - */ -static rt_err_t rt_serial_init(rt_device_t dev) -{ - if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED)) - { - if (dev->flag & RT_DEVICE_FLAG_INT_RX) - { - rt_memset(serial_rx.rx_buffer, 0, - sizeof(serial_rx.rx_buffer)); - serial_rx.read_index = 0; - serial_rx.save_index = 0; - } - - dev->flag |= RT_DEVICE_FLAG_ACTIVATED; - } - return RT_EOK; -} - -static rt_err_t rt_serial_open(rt_device_t dev, rt_uint16_t oflag) -{ -#if _DEBUG_SERIAL==1 - printf("in rt_serial_open()\n"); -#endif - return RT_EOK; -} - -static rt_err_t rt_serial_close(rt_device_t dev) -{ -#if _DEBUG_SERIAL==1 - printf("in rt_serial_close()\n"); -#endif - return RT_EOK; -} -static rt_size_t rt_serial_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size) -{ - rt_uint8_t *ptr; - rt_err_t err_code; - - ptr = buffer; - err_code = RT_EOK; - - if (dev->flag & RT_DEVICE_FLAG_INT_RX) - { - /* interrupt mode Rx */ - while (size) - { - rt_base_t level; - - /* disable interrupt */ - level = rt_hw_interrupt_disable(); - - if (serial_rx.read_index != serial_rx.save_index) - { - /* read a character */ - *ptr++ = serial_rx.rx_buffer[serial_rx.read_index]; - size--; - - /* move to next position */ - serial_rx.read_index ++; - if (serial_rx.read_index >= SERIAL_RX_BUFFER_SIZE) - serial_rx.read_index = 0; - } - else - { - /* set error code */ - err_code = -RT_EEMPTY; - - /* enable interrupt */ - rt_hw_interrupt_enable(level); - break; - } - - /* enable interrupt */ - rt_hw_interrupt_enable(level); - } - } - - - /* set error code */ - rt_set_errno(err_code); - return (rt_uint32_t)ptr - (rt_uint32_t)buffer; -} - -static rt_size_t rt_serial_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size) -{ -#if _DEBUG_SERIAL==1 - printf("in rt_serial_write()\n"); -#endif -#if 0 - if (fp == NULL) - fp = fopen("log.txt", "wb+"); - - if (fp != NULL) - fwrite(buffer, size, 1, fp); -#endif - - printf("%s", (char *)buffer); - fflush(stdout); - return size; -} - -static rt_err_t rt_serial_control(rt_device_t dev, rt_uint8_t cmd, void *args) -{ - RT_ASSERT(dev != RT_NULL); - - switch (cmd) - { - case RT_DEVICE_CTRL_SUSPEND: - /* suspend device */ - dev->flag |= RT_DEVICE_FLAG_SUSPENDED; - break; - - case RT_DEVICE_CTRL_RESUME: - /* resume device */ - dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED; - break; - } - - return RT_EOK; -} - -/* - * serial register - */ -static rt_err_t rt_hw_serial_register(rt_device_t device, const char *name, rt_uint32_t flag) -{ - RT_ASSERT(device != RT_NULL); -#if _DEBUG_SERIAL==1 - printf("in rt_serial_register()\n"); -#endif - device->type = RT_Device_Class_Char; - device->rx_indicate = RT_NULL; - device->tx_complete = RT_NULL; - device->init = rt_serial_init; - device->open = rt_serial_open; - device->close = rt_serial_close; - device->read = rt_serial_read; - device->write = rt_serial_write; - device->control = rt_serial_control; - device->user_data = RT_NULL; - - /* register a character device */ - return rt_device_register(device, name, (rt_uint16_t)(RT_DEVICE_FLAG_RDWR | flag)); -} - -rt_err_t rt_hw_serial_init(void) -{ - return rt_hw_serial_register(&serial_device, RT_CONSOLE_DEVICE_NAME, - RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM); -} diff --git a/bsp/simlinux/drivers/serial.h b/bsp/simlinux/drivers/serial.h deleted file mode 100755 index 010631ab32..0000000000 --- a/bsp/simlinux/drivers/serial.h +++ /dev/null @@ -1,22 +0,0 @@ -/* -********************************************************************************************************* -* MC9S12DP256/DG128 Specific code -* BANKED MEMORY MODEL -* -* File : rthw.c -* By : parai -* email:parai@foxmail.com -*******************************************************************************************************/ - -#ifndef __RT_HW_SERIAL_H__ -#define __RT_HW_SERIAL_H__ - -#define SERIAL_RX_BUFFER_SIZE 80 -struct serial_int_rx -{ - rt_uint8_t rx_buffer[SERIAL_RX_BUFFER_SIZE]; - rt_uint32_t read_index, save_index; -}; - -rt_err_t rt_hw_serial_init(void); -#endif diff --git a/bsp/simlinux/drivers/sst25vfxx_mtd.h b/bsp/simlinux/drivers/sst25vfxx_mtd.h deleted file mode 100755 index cb0bb84129..0000000000 --- a/bsp/simlinux/drivers/sst25vfxx_mtd.h +++ /dev/null @@ -1,24 +0,0 @@ -/* - * File : sst25vfxx_mtd.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team - * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE - * - * Change Logs: - * Date Author Notes - * 2011-12-16 aozima the first version - * 2012-02-01 mbbill MTD driver version - */ - -#ifndef SST25VFXX_MTD_H -#define SST25VFXX_MTD_H - -#include -#include - -rt_err_t sst25vfxx_mtd_init(const char *spi_device_name, rt_uint32_t block_start, rt_uint32_t block_end); - -#endif diff --git a/bsp/simlinux/drivers/sst25vfxx_mtd_sim.c b/bsp/simlinux/drivers/sst25vfxx_mtd_sim.c deleted file mode 100755 index 9271c1bb03..0000000000 --- a/bsp/simlinux/drivers/sst25vfxx_mtd_sim.c +++ /dev/null @@ -1,227 +0,0 @@ -/* - * File : rtdef.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team - * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE - * - * Change Logs: - * Date Author Notes - * 2012-10-21 prife the first version - */ - -#include -#include -#include -#include -#include "sst25vfxx_mtd.h" - -#ifdef RT_USING_MTD_NOR -#define NOR_SIM "nor.bin" -/* JEDEC Manufacturers ID */ -#define MF_ID (0xBF) -/* JEDEC Device ID : Memory Type */ -#define MT_ID (0x25) -/* JEDEC Device ID: Memory Capacity */ -#define MC_ID_SST25VF016 (0x41) -#define MC_ID_SST25VF032 (0x4A) -#define MC_ID_SST25VF064 (0x4B) - -#define BLOCK_SIZE (64*1024) - - -#define SST25_MTD(device) ((struct sst25_mtd*)(device)) -struct sst25_mtd -{ - struct rt_mtd_nor_device parent; - FILE *file; -}; -static struct sst25_mtd _sst25_mtd; - -static struct rt_mutex flash_lock; - -/* RT-Thread MTD device interface */ -static rt_uint32_t sst25vfxx_read_id(struct rt_mtd_nor_device *device) -{ - rt_uint8_t id_recv[3] = {MF_ID, MT_ID, MC_ID_SST25VF016}; - - return (id_recv[0] << 16) | (id_recv[1] << 8) | id_recv[2]; -} - -static int sst25vfxx_read(struct rt_mtd_nor_device *device, rt_off_t position, rt_uint8_t *data, rt_size_t size) -{ - struct sst25_mtd *sst25; - int result; - - sst25 = SST25_MTD(device); - RT_ASSERT(sst25 != RT_NULL); - - rt_mutex_take(&flash_lock, RT_WAITING_FOREVER); - - fseek(sst25->file, position, SEEK_SET); - result = fread(data, size, 1, sst25->file); - if (result < 0) - rt_kprintf("sst read error.\n"); - - rt_mutex_release(&flash_lock); - return size; -} - -static int sst25vfxx_write(struct rt_mtd_nor_device *device, rt_off_t position, - const rt_uint8_t *data, rt_size_t size) -{ - struct sst25_mtd *sst25; - int result; - - sst25 = SST25_MTD(device); - RT_ASSERT(sst25 != RT_NULL); - - rt_mutex_take(&flash_lock, RT_WAITING_FOREVER); - - fseek(sst25->file, position, SEEK_SET); - result = fwrite(data, size, 1, sst25->file); - if (result < 0) - rt_kprintf("sst write error.\n"); - - rt_mutex_release(&flash_lock); - return size; -} - -static char block_buffer[BLOCK_SIZE]; -static rt_err_t sst25vfxx_erase_block(struct rt_mtd_nor_device *device, rt_off_t offset, rt_uint32_t length) -{ - struct sst25_mtd *sst25; - int result; - - sst25 = SST25_MTD(device); - - RT_ASSERT(sst25 != RT_NULL); - - rt_mutex_take(&flash_lock, RT_WAITING_FOREVER); - - memset(block_buffer, 0xFF, BLOCK_SIZE); - fseek(sst25->file, offset, SEEK_SET); - - result = fwrite(block_buffer, BLOCK_SIZE, 1, sst25->file); - if (result < 0) - rt_kprintf("sst write error.\n"); - - rt_mutex_release(&flash_lock); - return RT_EOK; -} - -const static struct rt_mtd_nor_driver_ops sst25vfxx_mtd_ops = -{ - sst25vfxx_read_id, - sst25vfxx_read, - sst25vfxx_write, - sst25vfxx_erase_block, -}; -static rt_err_t sst25vfxx_hw_init(struct sst25_mtd *mtd) -{ - mtd = mtd; - return RT_EOK; -} - -/** - * SST25vfxx API - */ -rt_err_t sst25vfxx_mtd_init(const char *nor_name, - rt_uint32_t block_start, - rt_uint32_t block_end) -{ - rt_uint32_t id, total_block; - struct sst25_mtd *sst25; - struct rt_mtd_nor_device *mtd; - - - sst25 = &_sst25_mtd; - mtd = &(sst25->parent); - - /* set page size and block size */ - mtd->block_size = 64 * 1024; /* 64kByte */ - mtd->ops = &sst25vfxx_mtd_ops; - - /* initialize mutex */ - if (rt_mutex_init(&flash_lock, nor_name, RT_IPC_FLAG_FIFO) != RT_EOK) - { - rt_kprintf("init sd lock mutex failed\n"); - } - - /* initialize flash */ - id = sst25vfxx_read_id(mtd); - switch (id & 0xff) - { - case MC_ID_SST25VF016: - total_block = (16 * 1024 * 1024 / 8) / mtd->block_size; - break; - case MC_ID_SST25VF032: - total_block = (32 * 1024 * 1024 / 8) / mtd->block_size; - break; - case MC_ID_SST25VF064: - total_block = (64 * 1024 * 1024 / 8) / mtd->block_size; - break; - default: - rt_kprintf("SST25 detection error, id: %x\n", id); - return -RT_ERROR; - } - - if ((block_end == RT_UINT32_MAX) || (block_end == 0)) - { - block_end = total_block; - } - else if (block_end > total_block) - { - rt_kprintf("SST25 total block: %d, out of block\n", total_block); - return -RT_ERROR; - } - - mtd->block_start = block_start; - mtd->block_end = block_end; - - /* open nor file, if not exist, then create it */ - sst25->file = fopen(NOR_SIM, "rb+"); - if (sst25->file == NULL) - { - rt_uint32_t i; - /* create a file to simulate nor */ - sst25->file = fopen(NOR_SIM, "wb+"); - - memset(block_buffer, 0xFF, sizeof(block_buffer)); - for (i = 0; i < total_block; i++) - { - fseek(sst25->file, i * BLOCK_SIZE, SEEK_SET); - fwrite(block_buffer, BLOCK_SIZE, 1, sst25->file); - } - } - - fseek(sst25->file, 0, SEEK_SET); - - /* initialize hardware */ - sst25vfxx_hw_init(&_sst25_mtd); - - /* register MTD device */ - rt_mtd_nor_register_device("nor", mtd); - - return RT_EOK; -} - -#ifdef RT_USING_FINSH -#include -void nor_erase(void) -{ - rt_uint32_t index; - struct rt_mtd_nor_device *mtd; - - mtd = RT_MTD_NOR_DEVICE(&_sst25_mtd); - for (index = mtd->block_start; index < mtd->block_end; index ++) - { - sst25vfxx_erase_block(mtd, index * mtd->block_size, BLOCK_SIZE); - } -} -FINSH_FUNCTION_EXPORT(nor_erase, erase all block in SPI flash); -#endif - -#endif diff --git a/bsp/simlinux/drivers/usart_sim.c b/bsp/simlinux/drivers/usart_sim.c deleted file mode 100755 index 2cbb1ed345..0000000000 --- a/bsp/simlinux/drivers/usart_sim.c +++ /dev/null @@ -1,192 +0,0 @@ -#include -#include - -#ifdef _WIN32 -#include -#include -#include -#endif - -#include -#include "serial.h" - -struct serial_int_rx serial_rx; -extern struct rt_device serial_device; - -#ifdef _WIN32 -/* - * Handler for OSKey Thread - */ -static HANDLE OSKey_Thread; -static DWORD OSKey_ThreadID; - -static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam); -void rt_hw_usart_init(void) -{ - /* - * create serial thread that receive key input from keyboard - */ - - OSKey_Thread = CreateThread(NULL, - 0, - (LPTHREAD_START_ROUTINE)ThreadforKeyGet, - 0, - CREATE_SUSPENDED, - &OSKey_ThreadID); - if (OSKey_Thread == NULL) - { - //Display Error Message - - return; - } - SetThreadPriority(OSKey_Thread, - THREAD_PRIORITY_NORMAL); - SetThreadPriorityBoost(OSKey_Thread, - TRUE); - SetThreadAffinityMask(OSKey_Thread, - 0x01); - /* - * Start OS get key Thread - */ - ResumeThread(OSKey_Thread); -} - -#else /* POSIX version */ - -#include -#include -#include -#include -#include /* for tcxxxattr, ECHO, etc */ -#include /* for STDIN_FILENO */ - - -static void * ThreadforKeyGet(void * lpParam); -static pthread_t OSKey_Thread; -void rt_hw_usart_init(void) -{ - int res; - res = pthread_create(&OSKey_Thread, NULL, &ThreadforKeyGet, NULL); - if (res) - { - printf("pthread create faild, <%d>\n", res); - exit(EXIT_FAILURE); - } -} -#endif -/* - * () 0xe04b - * () 0xe048 - * () 0xe04d - * () 0xe050 - */ -static int savekey(unsigned char key) -{ - /* save on rx buffer */ - { - rt_base_t level; - - /* disable interrupt */ - //ʱرжϣΪҪuartݽṹ - level = rt_hw_interrupt_disable(); - - /* save character */ - serial_rx.rx_buffer[serial_rx.save_index] = key; - serial_rx.save_index ++; - //Ĵsave_indexǷѾβתͷΪһλ - if (serial_rx.save_index >= SERIAL_RX_BUFFER_SIZE) - serial_rx.save_index = 0; - - //ʾתsave_index׷read_indexread_indexһɵ - /* if the next position is read index, discard this 'read char' */ - if (serial_rx.save_index == serial_rx.read_index) - { - serial_rx.read_index ++; - if (serial_rx.read_index >= SERIAL_RX_BUFFER_SIZE) - serial_rx.read_index = 0; - } - - /* enable interrupt */ - //uartݽṹѾɣʹж - rt_hw_interrupt_enable(level); - } - - /* invoke callback */ - if (serial_device.rx_indicate != RT_NULL) - { - rt_size_t rx_length; - - /* get rx length */ - rx_length = serial_rx.read_index > serial_rx.save_index ? - SERIAL_RX_BUFFER_SIZE - serial_rx.read_index + serial_rx.save_index : - serial_rx.save_index - serial_rx.read_index; - - serial_device.rx_indicate(&serial_device, rx_length); - } - return 0; -} -#ifdef _WIN32 -static DWORD WINAPI ThreadforKeyGet(LPVOID lpParam) -#else - -static struct termios oldt, newt; -/*simulate windows' getch(), it works!!*/ -void set_stty(void) -{ - /* get terminal input's attribute */ - tcgetattr(STDIN_FILENO, &oldt); - newt = oldt; - - /* set termios' local mode */ - newt.c_lflag &= ~(ECHO|ICANON); - tcsetattr(STDIN_FILENO, TCSANOW, &newt); -} - -void restore_stty(void) -{ - /* recover terminal's attribute */ - tcsetattr(STDIN_FILENO, TCSANOW, &oldt); -} - -#define getch getchar - -static void * ThreadforKeyGet(void * lpParam) -#endif /* not _WIN32*/ -{ - unsigned char key; - -#ifndef _WIN32 - sigset_t sigmask, oldmask; - /* set the getchar without buffer */ - sigfillset(&sigmask); - pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask); - set_stty(); -#endif - (void)lpParam; //prevent compiler warnings - for (;;) - { - key = getch(); -#ifdef _WIN32 - if (key == 0xE0) - { - key = getch(); - - if (key == 0x48) //up key , 0x1b 0x5b 0x41 - { - savekey(0x1b); - savekey(0x5b); - savekey(0x41); - } - else if (key == 0x50)//0x1b 0x5b 0x42 - { - savekey(0x1b); - savekey(0x5b); - savekey(0x42); - } - - continue; - } -#endif - savekey(key); - } -} /*** ThreadforKeyGet ***/ diff --git a/bsp/simlinux/gcc.ld b/bsp/simlinux/gcc.ld deleted file mode 100644 index 74f033935a..0000000000 --- a/bsp/simlinux/gcc.ld +++ /dev/null @@ -1,216 +0,0 @@ -/* Script for -z combreloc: combine and sort reloc sections */ -OUTPUT_FORMAT("elf32-i386", "elf32-i386", - "elf32-i386") -OUTPUT_ARCH(i386) -ENTRY(_start) -SEARCH_DIR("/usr/i686-linux-gnu/lib32"); SEARCH_DIR("=/usr/local/lib32"); SEARCH_DIR("=/lib32"); SEARCH_DIR("=/usr/lib32"); SEARCH_DIR("=/usr/local/lib/i386-linux-gnu"); SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib/i386-linux-gnu"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib/i386-linux-gnu"); SEARCH_DIR("=/usr/lib"); -SECTIONS -{ - /* Read-only sections, merged into text segment: */ - PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x08048000)); . = SEGMENT_START("text-segment", 0x08048000) + SIZEOF_HEADERS; - .interp : { *(.interp) } - .note.gnu.build-id : { *(.note.gnu.build-id) } - .hash : { *(.hash) } - .gnu.hash : { *(.gnu.hash) } - .dynsym : { *(.dynsym) } - .dynstr : { *(.dynstr) } - .gnu.version : { *(.gnu.version) } - .gnu.version_d : { *(.gnu.version_d) } - .gnu.version_r : { *(.gnu.version_r) } - .rel.dyn : - { - *(.rel.init) - *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*) - *(.rel.fini) - *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*) - *(.rel.data.rel.ro* .rel.gnu.linkonce.d.rel.ro.*) - *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*) - *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*) - *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*) - *(.rel.ctors) - *(.rel.dtors) - *(.rel.got) - *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*) - *(.rel.ifunc) - } - .rel.plt : - { - *(.rel.plt) - PROVIDE_HIDDEN (__rel_iplt_start = .); - *(.rel.iplt) - PROVIDE_HIDDEN (__rel_iplt_end = .); - } - .init : - { - KEEP (*(.init)) - } =0x90909090 - .plt : { *(.plt) *(.iplt) } - .text : - { - *(.text.unlikely .text.*_unlikely) - *(.text.exit .text.exit.*) - *(.text.startup .text.startup.*) - *(.text.hot .text.hot.*) - *(.text .stub .text.* .gnu.linkonce.t.*) - /* .gnu.warning sections are handled specially by elf32.em. */ - *(.gnu.warning) - } =0x90909090 - .fini : - { - KEEP (*(.fini)) - } =0x90909090 - PROVIDE (__etext = .); - PROVIDE (_etext = .); - PROVIDE (etext = .); - .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) } - .rodata1 : { *(.rodata1) } - - /* setction information for finsh shell begin */ - . = ALIGN(4); - __fsymtab_start = .; - FSymTab : {KEEP(*(FSymTab))} - __fsymtab_end = .; - . = ALIGN(4); - __vsymtab_start = .; - VSymTab : {KEEP(*(VSymTab))} - __vsymtab_end = .; - . = ALIGN(4); - /* setction information for finsh shell end */ - - .eh_frame_hdr : { *(.eh_frame_hdr) } - .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) } - .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table - .gcc_except_table.*) } - /* These sections are generated by the Sun/Oracle C++ compiler. */ - .exception_ranges : ONLY_IF_RO { *(.exception_ranges - .exception_ranges*) } - /* Adjust the address for the data segment. We want to adjust up to - the same address within the page on the next page up. */ - . = ALIGN (CONSTANT (MAXPAGESIZE)) - ((CONSTANT (MAXPAGESIZE) - .) & (CONSTANT (MAXPAGESIZE) - 1)); . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); - /* Exception handling */ - .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) } - .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) } - .exception_ranges : ONLY_IF_RW { *(.exception_ranges .exception_ranges*) } - /* Thread Local Storage sections */ - .tdata : { *(.tdata .tdata.* .gnu.linkonce.td.*) } - .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) } - .preinit_array : - { - PROVIDE_HIDDEN (__preinit_array_start = .); - KEEP (*(.preinit_array)) - PROVIDE_HIDDEN (__preinit_array_end = .); - } - .init_array : - { - PROVIDE_HIDDEN (__init_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) - KEEP (*(.init_array)) - KEEP (*(EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)) - PROVIDE_HIDDEN (__init_array_end = .); - } - .fini_array : - { - PROVIDE_HIDDEN (__fini_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*))) - KEEP (*(.fini_array)) - KEEP (*(EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors)) - PROVIDE_HIDDEN (__fini_array_end = .); - } - .ctors : - { - /* gcc uses crtbegin.o to find the start of - the constructors, so we make sure it is - first. Because this is a wildcard, it - doesn't matter if the user does not - actually link against crtbegin.o; the - linker won't look for a file to match a - wildcard. The wildcard also means that it - doesn't matter which directory crtbegin.o - is in. */ - KEEP (*crtbegin.o(.ctors)) - KEEP (*crtbegin?.o(.ctors)) - /* We don't want to include the .ctor section from - the crtend.o file until after the sorted ctors. - The .ctor section from the crtend file contains the - end of ctors marker and it must be last */ - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors)) - KEEP (*(SORT(.ctors.*))) - KEEP (*(.ctors)) - } - .dtors : - { - KEEP (*crtbegin.o(.dtors)) - KEEP (*crtbegin?.o(.dtors)) - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors)) - KEEP (*(SORT(.dtors.*))) - KEEP (*(.dtors)) - } - .jcr : { KEEP (*(.jcr)) } - .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro* .gnu.linkonce.d.rel.ro.*) } - .dynamic : { *(.dynamic) } - .got : { *(.got) *(.igot) } - . = DATA_SEGMENT_RELRO_END (12, .); - .got.plt : { *(.got.plt) *(.igot.plt) } - .data : - { - *(.data .data.* .gnu.linkonce.d.*) - SORT(CONSTRUCTORS) - } - .data1 : { *(.data1) } - _edata = .; PROVIDE (edata = .); - __bss_start = .; - .bss : - { - *(.dynbss) - *(.bss .bss.* .gnu.linkonce.b.*) - *(COMMON) - /* Align here to ensure that the .bss section occupies space up to - _end. Align after .bss to ensure correct alignment even if the - .bss section disappears because there are no input sections. - FIXME: Why do we need it? When there is no .bss section, we don't - pad the .data section. */ - . = ALIGN(. != 0 ? 32 / 8 : 1); - } - . = ALIGN(32 / 8); - . = ALIGN(32 / 8); - _end = .; PROVIDE (end = .); - . = DATA_SEGMENT_END (.); - /* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } - /* DWARF debug sections. - Symbols in the DWARF debugging sections are relative to the beginning - of the section so we begin them at 0. */ - /* DWARF 1 */ - .debug 0 : { *(.debug) } - .line 0 : { *(.line) } - /* GNU DWARF 1 extensions */ - .debug_srcinfo 0 : { *(.debug_srcinfo) } - .debug_sfnames 0 : { *(.debug_sfnames) } - /* DWARF 1.1 and DWARF 2 */ - .debug_aranges 0 : { *(.debug_aranges) } - .debug_pubnames 0 : { *(.debug_pubnames) } - /* DWARF 2 */ - .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_line 0 : { *(.debug_line) } - .debug_frame 0 : { *(.debug_frame) } - .debug_str 0 : { *(.debug_str) } - .debug_loc 0 : { *(.debug_loc) } - .debug_macinfo 0 : { *(.debug_macinfo) } - /* SGI/MIPS DWARF 2 extensions */ - .debug_weaknames 0 : { *(.debug_weaknames) } - .debug_funcnames 0 : { *(.debug_funcnames) } - .debug_typenames 0 : { *(.debug_typenames) } - .debug_varnames 0 : { *(.debug_varnames) } - /* DWARF 3 */ - .debug_pubtypes 0 : { *(.debug_pubtypes) } - .debug_ranges 0 : { *(.debug_ranges) } - .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) } - /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) } -} diff --git a/bsp/simlinux/rtconfig.h b/bsp/simlinux/rtconfig.h deleted file mode 100755 index 145cc442fc..0000000000 --- a/bsp/simlinux/rtconfig.h +++ /dev/null @@ -1,228 +0,0 @@ -/* RT-Thread config file */ -#ifndef __RTTHREAD_CFG_H__ -#define __RTTHREAD_CFG_H__ - -#define RT_HEAP_SIZE (1024*1024*2) - -#if defined(_MSC_VER) -/* SECTION: port for visual studio */ -#undef RT_USING_NEWLIB -#undef RT_USING_MINILIBC -#define NORESOURCE //RT_VESRION in winuser.h -#define _CRT_ERRNO_DEFINED //errno macro redefinition - -/* disable some warning in MSC */ -#pragma warning(disable:4273) /* to ignore: warning C4273: inconsistent dll linkage */ -#pragma warning(disable:4312) /* to ignore: warning C4312: 'type cast' : conversion from 'rt_uint32_t' to 'rt_uint32_t *' */ -#pragma warning(disable:4311) /* to ignore: warning C4311: 'type cast' : pointer truncation from 'short *__w64 ' to 'long' */ -#pragma warning(disable:4996) /* to ignore: warning C4996: The POSIX name for this item is deprecated. */ -#pragma warning(disable:4267) /* to ignore: warning C4267: conversion from 'size_t' to 'rt_size_t', possible loss of data */ -#pragma warning(disable:4244) /* to ignore: warning C4244: '=' : conversion from '__w64 int' to 'rt_size_t', possible loss of data */ - -#elif defined(__GNUC__) -#define RT_USING_NOLIBC -#endif - -/* SECTION: basic kernel options */ -/* RT_NAME_MAX*/ -#define RT_NAME_MAX 8 - -/* RT_ALIGN_SIZE*/ -#define RT_ALIGN_SIZE 4 - -/* PRIORITY_MAX */ -#define RT_THREAD_PRIORITY_MAX 32 - -/* Tick per Second */ -#define RT_TICK_PER_SECOND 100 - -/* SECTION: RT_DEBUG */ -/* Thread Debug */ -#define RT_DEBUG -//#define RT_DEBUG_SCHEDULER 1 -#define RT_THREAD_DEBUG - -#define RT_USING_OVERFLOW_CHECK - -/* Using Hook */ -#define RT_USING_HOOK - -/* Using Software Timer */ -/* #define RT_USING_TIMER_SOFT */ -#define RT_TIMER_THREAD_PRIO 4 -#define RT_TIMER_THREAD_STACK_SIZE 512 -#define RT_TIMER_TICK_PER_SECOND 10 - -/* SECTION: IPC */ -/* Using Semaphore*/ -#define RT_USING_SEMAPHORE - -/* Using Mutex */ -#define RT_USING_MUTEX - -/* Using Event */ -#define RT_USING_EVENT - -/* Using MailBox */ -#define RT_USING_MAILBOX - -/* Using Message Queue */ -#define RT_USING_MESSAGEQUEUE - -/* SECTION: Memory Management */ -/* Using Memory Pool Management*/ -/* #define RT_USING_MEMPOOL */ - -/* Using Dynamic Heap Management */ -#define RT_USING_HEAP - -/* Using Small MM */ -#define RT_USING_SMALL_MEM -/* #define RT_TINY_SIZE */ - -/* SECTION: Device System */ -/* Using Device System */ -#define RT_USING_DEVICE -/* #define RT_USING_UART1 */ - -/* SECTION: Console options */ -#define RT_USING_CONSOLE -/* the buffer size of console*/ -#define RT_CONSOLEBUF_SIZE 128 -#define RT_CONSOLE_DEVICE_NAME "sci0" - -/* SECTION: component options */ -#define RT_USING_COMPONENTS_INIT - -/* SECTION: MTD interface options */ -/* using mtd nand flash */ -#define RT_USING_MTD_NAND -/* using mtd nor flash */ -#define RT_USING_MTD_NOR - -/* SECTION: finsh, a C-Express shell */ -#define RT_USING_FINSH -/* Using symbol table */ -#define FINSH_USING_SYMTAB -#define FINSH_USING_DESCRIPTION - -/* SECTION: device file system */ -#define RT_USING_DFS -#define DFS_FILESYSTEM_TYPES_MAX 8 - -/* DFS: ELM FATFS options */ -#define RT_USING_DFS_ELMFAT -#define RT_DFS_ELM_WORD_ACCESS -/* Reentrancy (thread safe) of the FatFs module. */ -#define RT_DFS_ELM_REENTRANT -/* Number of volumes (logical drives) to be used. */ -#define RT_DFS_ELM_DRIVES 2 -/* #define RT_DFS_ELM_USE_LFN 1 */ -#define RT_DFS_ELM_MAX_LFN 255 -/* Maximum sector size to be handled. */ -#define RT_DFS_ELM_MAX_SECTOR_SIZE 512 - -/* DFS: network file system options */ -/* #define RT_USING_DFS_NFS */ - -/* DFS: UFFS nand file system options */ -#define RT_USING_DFS_UFFS -/* configuration for uffs, more to see dfs_uffs.h and uffs_config.h */ -#define RT_CONFIG_UFFS_ECC_MODE UFFS_ECC_HW_AUTO -/* enable this ,you need provide a mark_badblock/check_block function */ -/* #define RT_UFFS_USE_CHECK_MARK_FUNCITON */ - -/* DFS: JFFS2 nor flash file system options */ -//#define RT_USING_DFS_JFFS2 - -/* DFS: windows share directory mounted to rt-thread/dfs */ -/* only used in bsp/simulator */ -//#define RT_USING_DFS_WINSHAREDIR - -/* the max number of mounted file system */ -#define DFS_FILESYSTEMS_MAX 4 -/* the max number of opened files */ -#define DFS_FD_MAX 4 - -/* SECTION: lwip, a lightweight TCP/IP protocol stack */ -/* #define RT_USING_LWIP */ -/* LwIP uses RT-Thread Memory Management */ -#define RT_LWIP_USING_RT_MEM -/* Enable ICMP protocol*/ -#define RT_LWIP_ICMP -/* Enable UDP protocol*/ -#define RT_LWIP_UDP -/* Enable TCP protocol*/ -#define RT_LWIP_TCP -/* Enable DNS */ -#define RT_LWIP_DNS - -/* the number of simultaneously active TCP connections*/ -#define RT_LWIP_TCP_PCB_NUM 5 - -/* Using DHCP */ -/* #define RT_LWIP_DHCP */ - -/* ip address of target*/ -#define RT_LWIP_IPADDR0 192 -#define RT_LWIP_IPADDR1 168 -#define RT_LWIP_IPADDR2 126 -#define RT_LWIP_IPADDR3 30 - -/* gateway address of target*/ -#define RT_LWIP_GWADDR0 192 -#define RT_LWIP_GWADDR1 168 -#define RT_LWIP_GWADDR2 126 -#define RT_LWIP_GWADDR3 1 - -/* mask address of target*/ -#define RT_LWIP_MSKADDR0 255 -#define RT_LWIP_MSKADDR1 255 -#define RT_LWIP_MSKADDR2 255 -#define RT_LWIP_MSKADDR3 0 - -/* tcp thread options */ -#define RT_LWIP_TCPTHREAD_PRIORITY 12 -#define RT_LWIP_TCPTHREAD_MBOX_SIZE 10 -#define RT_LWIP_TCPTHREAD_STACKSIZE 1024 - -/* Ethernet if thread options */ -#define RT_LWIP_ETHTHREAD_PRIORITY 15 -#define RT_LWIP_ETHTHREAD_MBOX_SIZE 10 -#define RT_LWIP_ETHTHREAD_STACKSIZE 512 - -/* TCP sender buffer space */ -#define RT_LWIP_TCP_SND_BUF 8192 -/* TCP receive window. */ -#define RT_LWIP_TCP_WND 8192 - -/* SECTION: RT-Thread/GUI */ -#define RT_USING_RTGUI - -/* name length of RTGUI object */ -#define RTGUI_NAME_MAX 12 -/* support 16 weight font */ -#define RTGUI_USING_FONT16 -/* support Chinese font */ -#define RTGUI_USING_FONTHZ -/* use DFS as file interface */ -#define RTGUI_USING_DFS_FILERW -/* use font file as Chinese font */ -/* #define RTGUI_USING_HZ_FILE */ -/* use Chinese bitmap font */ -#define RTGUI_USING_HZ_BMP -/* use small size in RTGUI */ -#define RTGUI_USING_SMALL_SIZE -/* use mouse cursor */ -/* #define RTGUI_USING_MOUSE_CURSOR */ -/* default font size in RTGUI */ -#define RTGUI_DEFAULT_FONT_SIZE 16 - -/* image support */ -#define RTGUI_IMAGE_XPM -#define RTGUI_IMAGE_BMP -/* #define RTGUI_IMAGE_JPEG */ -/* #define RTGUI_IMAGE_PNG */ -#define RTGUI_USING_NOTEBOOK_IMAGE - -#endif diff --git a/bsp/simlinux/rtconfig.py b/bsp/simlinux/rtconfig.py deleted file mode 100755 index 6a660e832f..0000000000 --- a/bsp/simlinux/rtconfig.py +++ /dev/null @@ -1,82 +0,0 @@ -# toolchains options -ARCH='sim' -#CPU='win32' -#CPU='posix' -CPU='posix' -CROSS_TOOL='gcc' #msvc # gcc - -# lcd panel options -# 'FMT0371','ILI932X', 'SSD1289' -# RT_USING_LCD_TYPE = 'SSD1289' - -# cross_tool provides the cross compiler -# EXEC_PATH is the compiler execute path, for example, CodeSourcery, Keil MDK, IAR -if CROSS_TOOL == 'gcc': - PLATFORM = 'gcc' - EXEC_PATH = '/usr/bin/gcc' - -if CROSS_TOOL == 'msvc': - PLATFORM = 'cl' - EXEC_PATH = '' - -BUILD = 'debug' -#BUILD = '' - -if PLATFORM == 'gcc': - # toolchains - PREFIX = '' - CC = PREFIX + 'gcc' - AS = PREFIX + 'gcc' - AR = PREFIX + 'ar' - LINK = PREFIX + 'gcc' - TARGET_EXT = 'axf' - SIZE = PREFIX + 'size' - OBJDUMP = PREFIX + 'objdump' - OBJCPY = PREFIX + 'objcopy' - - DEVICE = ' -ffunction-sections -fdata-sections' - DEVICE = ' ' - CFLAGS = DEVICE + ' -I/usr/include -w -D_REENTRANT' - AFLAGS = ' -c' + DEVICE + ' -x assembler-with-cpp' - #LFLAGS = DEVICE + ' -Wl,--gc-sections,-Map=rtthread-linux.map,-cref,-u,Reset_Handler -T stm32_rom.ld' - #LFLAGS = DEVICE + ' -Wl,--gc-sections,-Map=rtthread-linux.map -lpthread' - #LFLAGS = DEVICE + ' -Wl,--gc-sections,-Map=rtthread-linux.map -pthread' - LFLAGS = DEVICE + ' -Wl,-Map=rtthread-linux.map -pthread -T gcc.ld' - - CPATH = '' - LPATH = '' - - if BUILD == 'debug': - CFLAGS += ' -g -O0 -gdwarf-2' - AFLAGS += ' -gdwarf-2' - else: - CFLAGS += ' -O2' - - POST_ACTION = OBJCPY + ' -O binary $TARGET rtthread.bin\n' + SIZE + ' $TARGET \n' - -elif PLATFORM == 'cl': - # toolchains - PREFIX = '' - TARGET_EXT = 'exe' - AS = PREFIX + 'cl' - CC = PREFIX + 'cl' - AR = PREFIX + 'cl' - LINK = PREFIX + 'cl' - AFLAGS = '' - CFLAGS = '' - LFLAGS = '' - - if BUILD == 'debug': - CFLAGS += ' /MTd' - LFLAGS += ' /DEBUG' - else: - CFLAGS += ' /MT' - LFLAGS += '' - - CFLAGS += ' /ZI /Od /W 3 /WL ' - LFLAGS += ' /SUBSYSTEM:CONSOLE /MACHINE:X86 ' - - CPATH = '' - LPATH = '' - - POST_ACTION = ''