Example #1
0
/*
 * This function will set errno
 *
 * @param error the errno shall be set
 */
void rt_set_errno(rt_err_t error)
{
	rt_thread_t tid;

	if (rt_interrupt_get_nest() != 0)
	{
		/* it's in interrupt context */
		_errno = error;

		return;
	}

	tid = rt_thread_self();
	if (tid == RT_NULL)
	{
		_errno = error;
		
		return;
	}

	tid->error = error;
}
Example #2
0
void *rtgui_realloc(void *ptr, rt_size_t size)
{
    void *new_ptr;

#ifdef RTGUI_MEM_TRACE
    new_ptr = rtgui_malloc(size);
    if ((new_ptr != RT_NULL) && (ptr != RT_NULL))
    {
        rt_memcpy(new_ptr, ptr, size);
        rtgui_free(ptr);
    }
#else
    new_ptr = rt_realloc(ptr, size);
#endif

#ifdef DEBUG_MEMLEAK
    rt_kprintf("realloc %p to %p (%d) on %p %*.s\n",
               ptr, new_ptr, size, __builtin_return_address(0),
               RT_NAME_MAX, rt_thread_self()->name);
#endif

    return new_ptr;
}
Example #3
0
/**
 * This function will notify kernel there is one tick passed. Normally,
 * this function is invoked by clock ISR.
 */
void rt_tick_increase(void)
{
    struct rt_thread *thread;

    /* increase the global tick */
    ++ rt_tick;

    /* check time slice */
    thread = rt_thread_self();

    -- thread->remaining_tick;
    if (thread->remaining_tick == 0)
    {
        /* change to initialized tick */
        thread->remaining_tick = thread->init_tick;

        /* yield */
        rt_thread_yield();
    }

    /* check timer */
    rt_timer_check();
}
Example #4
0
/**
 * System initialization thread.
 *
 * @param parameter parameter
 */
void sys_init_thread(void* parameter){
	set_system_status(SYSTEM_STATUS_INIT);

    /* EasyLogger initialization */
    if (elog_init() == ELOG_NO_ERR) {
        /* set enabled format */
        elog_set_fmt(ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME /*| ELOG_FMT_P_INFO*/ | ELOG_FMT_T_INFO | ELOG_FMT_DIR
                /*| ELOG_FMT_FUNC*/ | ELOG_FMT_LINE);
        /* set EasyLogger assert hook */
        elog_assert_set_hook(elog_user_assert_hook);
        /* set hardware exception hook */
        rt_hw_exception_install(exception_hook);
        /* set RT-Thread assert hook */
        rt_assert_set_hook(rtt_user_assert_hook);
        /* initialize OK and switch to running status */
        set_system_status(SYSTEM_STATUS_RUN);
    } else {
        /* initialize fail and switch to fault status */
        set_system_status(SYSTEM_STATUS_FAULT);
    }

    rt_thread_delete(rt_thread_self());
}
Example #5
0
void main(void)
{
	struct rtgui_app* application;
	struct rtgui_win* win;	

	application = rtgui_app_create(rt_thread_self(), "filelist");
	if (application != RT_NULL)
	{			
		struct rtgui_rect rect;
	    rtgui_filelist_view_t *view;

		win = rtgui_mainwin_create(RT_NULL, "filelist", 
			RTGUI_WIN_STYLE_MAINWIN | RTGUI_WIN_STYLE_DESTROY_ON_CLOSE);
        
		rtgui_widget_get_extent(RTGUI_WIDGET(win), &rect);

	    view = rtgui_filelist_view_create("/", "*.*", &rect);
        rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(view));

		rtgui_win_show(win, RT_TRUE);
		rtgui_app_destroy(application);
	}
}
Example #6
0
void app1_entry(void* parameter)
{
    struct rtgui_app* application;
    struct rtgui_win* win;

    application = rtgui_app_create(rt_thread_self(), "ExApp1");
    if (application != RT_NULL)
    {
        struct rtgui_label *label;
        struct rtgui_box *box;

        box = rtgui_box_create(RTGUI_VERTICAL, 10);
        label = rtgui_label_create("Hello World");
        win = rtgui_mainwin_create(RT_NULL, "MainWin", RTGUI_WIN_STYLE_MAINWIN);
        rtgui_container_set_box(RTGUI_CONTAINER(win), box);
        rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(label));
        rtgui_container_layout(RTGUI_CONTAINER(win));
        
        rtgui_win_show(win, RT_TRUE);

        rtgui_app_destroy(application);
    }
}
Example #7
0
/* system timer thread entry */
static void rt_thread_timer_entry(void *parameter)
{
    rt_tick_t next_timeout;

    while (1)
    {
        /* get the next timeout tick */
        next_timeout = rt_timer_list_next_timeout(rt_soft_timer_list);
        if (next_timeout == RT_TICK_MAX)
        {
            /* no software timer exist, suspend self. */
            rt_thread_suspend(rt_thread_self());
            rt_schedule();
        }
        else
        {
            rt_tick_t current_tick;

            /* get current tick */
            current_tick = rt_tick_get();

            if ((next_timeout - current_tick) < RT_TICK_MAX/2)
            {
                /* get the delta timeout tick */
                next_timeout = next_timeout - current_tick;
                rt_thread_delay_hmsm(next_timeout);
            }
        }

        /* lock scheduler */
        rt_enter_critical();
        /* check software timer */
        rt_soft_timer_check();
        /* unlock scheduler */
        rt_exit_critical();
    }
}
Example #8
0
void snake_main(void)
{
    struct rtgui_app *application;
    struct rtgui_win *win;
    rtgui_rect_t rect;

    application = rtgui_app_create(rt_thread_self(), "sanke_app");
    if (application != RT_NULL)
    {
        rtgui_get_screen_rect(&rect);
        rtgui_set_mainwin_rect(&rect);
        win = rtgui_mainwin_create(RT_NULL,
                                   "sanke_win",
                                   RTGUI_WIN_STYLE_MAINWIN | RTGUI_WIN_STYLE_DESTROY_ON_CLOSE);
        if (win == RT_NULL)
        {
            rt_kprintf("sanke_win create fail!\r\n");
            return;
        }

        rtgui_object_set_event_handler(RTGUI_OBJECT(win), event_handler);

        timer = rtgui_timer_create(RT_TICK_PER_SECOND / 2,
                                   RT_TIMER_FLAG_PERIODIC,
                                   timeout,
                                   (void *)win);

        rtgui_win_show(win, RT_TRUE);

        //Í˳öºó²Å·µ»Ø
        map_deinit(map);
        snake_deinit();
        food_deinit();
        rtgui_app_destroy(application);
    }
}
Example #9
0
void *rtgui_malloc(rt_size_t size)
{
    void *ptr;

    ptr = rt_malloc(size);
#ifdef RTGUI_MEM_TRACE
    if (rti_memtrace_inited == 0)
    {
        rti_memtrace_init();
        rti_memtrace_inited = 1;
    }

    if (ptr != RT_NULL)
        rti_malloc_hook(ptr, size);
#endif

#ifdef DEBUG_MEMLEAK
    rt_kprintf("alloc %p (%d) on %p %.*s\n",
               ptr, size, __builtin_return_address(0),
               RT_NAME_MAX, rt_thread_self()->name);
#endif

    return ptr;
}
Example #10
0
void rt_init_thread_entry(void* parameter)
{
    extern void rtgui_startup();
    extern void rt_hw_lcd_init();
    extern void rtgui_touch_hw_init(void);

	struct rtgui_app* app;


	app = rtgui_app_create(
			rt_thread_self(),
			"guiapp");

	RT_ASSERT(app != RT_NULL);

	create_wins(app, parameter);

	window_focus();

	rtgui_app_run(app);

	rtgui_app_destroy(app);
	rt_kprintf("app destroyed\n");
}
Example #11
0
void usb_thread_entry(void* parameter)
{
    int i;
    rt_thread_t tid;
    msd_msg_t msg;
    
    dev = rt_device_find(MOUNT_DEVICE);
    
    if(!dev)
    {
        rt_kprintf("no %s device found!\r\n", MOUNT_DEVICE);
        tid = rt_thread_self();
        rt_thread_delete(tid); 
    }
    
    rt_device_init(dev);
    rt_device_open(dev, RT_DEVICE_OFLAG_RDWR);
    
    usbd_init();                          /* USB Device Initialization          */
    usbd_connect(__TRUE);                 /* USB Device Connect                 */
    while (!usbd_configured ())
    {
        rt_thread_delay(10);
    }
    
    rt_kprintf("usb enum complete\r\n");
    
    while(1)
    {
        if(rt_mq_recv(msd_mq, &msg, sizeof(msd_msg_t), RT_WAITING_FOREVER) == RT_EOK)
        {
            if(msg.dir == 0) USBD_MSC_EP_BULKOUT_Event(0);
                else USBD_MSC_EP_BULKIN_Event (0);
        }
    }
}
void rt_hw_interrupt_enable(rt_base_t level)
{
    struct rt_thread * tid;
    pthread_t pid;
    thread_t *thread_from;
    thread_t *thread_to;

    if (ptr_int_mutex == NULL)
        return;

    interrupt_disable_flag = level;

    pthread_mutex_unlock(ptr_int_mutex);
    /* 如果已经中断仍然关闭 */
    if (interrupt_disable_flag)
    {
        return;
    }

    /* 表示当前中断打开, 检查是否有挂起的中断 */
    pthread_mutex_lock(ptr_int_mutex);
    if (!cpu_pending_interrupts)
    {
        pthread_mutex_unlock(ptr_int_mutex);
        return;
    }

    thread_from = (thread_t *) rt_interrupt_from_thread;
    thread_to = (thread_t *) rt_interrupt_to_thread;
    tid = rt_thread_self();
    pid = pthread_self();

    //pid != mainthread_pid &&
    if (thread_from->pthread == pid)
    {
        /* 注意这段代码是在RTT普通线程函数总函数中执行的,
         * from线程就是当前rtt线程 */
        /* 需要检查是否有挂起的中断需要处理 */
        TRACE("conswitch: P in pid<%x> ,suspend <%s>, resume <%s>!\n",
              (unsigned int)pid,
              thread_from->rtthread->name,
              thread_to->rtthread->name);

        cpu_pending_interrupts --;
        thread_from->status = SUSPEND_LOCK;
        pthread_mutex_unlock(ptr_int_mutex);
        /* 唤醒被挂起的线程 */
        if (thread_to->status == SUSPEND_SIGWAIT)
        {
            pthread_kill(thread_to->pthread, MSG_RESUME);
        }
        else if (thread_to->status == SUSPEND_LOCK)
        {
            sem_post(& thread_to->sem);
        }
        else
        {
            printf("conswitch: should not be here! %d\n", __LINE__);
            exit(EXIT_FAILURE);
        }

        /* 挂起当前的线程 */
        sem_wait(& thread_from->sem);
        pthread_mutex_lock(ptr_int_mutex);
        thread_from->status = THREAD_RUNNING;
        pthread_mutex_unlock(ptr_int_mutex);
    }
    else
    {
        /* 注意这段代码可能在多种情况下运行:
         * 1. 在system tick中执行, 即主线程的SIGALRM信号处理函数中执行
         * 2. 其他线程中调用,比如用于获取按键输入的线程中调用
         */
        TRACE("conswitch: S in pid<%x>  ,suspend <%s>, resume <%s>!\n",
              (unsigned int)pid,
              thread_from->rtthread->name,
              thread_to->rtthread->name);

        cpu_pending_interrupts --;

        /* 需要把解锁函数放在前面,以防止死锁?? */
        pthread_mutex_unlock(ptr_int_mutex);

        /* 挂起from线程 */
        pthread_kill(thread_from->pthread, MSG_SUSPEND);
        /* 注意:这里需要确保线程被挂起了, 否则312行就很可能就会报错退出
         * 因为这里挂起线程是通过信号实现的,所以一定要确保线程挂起才行 */
        while (thread_from->status != SUSPEND_SIGWAIT)
        {
            sched_yield();
        }

        /* 唤醒to线程 */
        if (thread_to->status == SUSPEND_SIGWAIT)
        {
            pthread_kill(thread_to->pthread, MSG_RESUME);
        }
        else if (thread_to->status == SUSPEND_LOCK)
        {
            sem_post(& thread_to->sem);
        }
        else
        {
            printf("conswitch: should not be here! %d\n", __LINE__);
            exit(EXIT_FAILURE);
        }

    }
    /*TODO: It may need to unmask the signal */
}
Example #13
0
/**
 * This function will allocate a block from memory pool
 *
 * @param mp the memory pool object
 * @param time the waiting time
 *
 * @return the allocated memory block or RT_NULL on allocated failed
 */
void *rt_mp_alloc(rt_mp_t mp, rt_int32_t time)
{
	rt_uint8_t *block_ptr;
	register rt_base_t level;
	struct rt_thread *thread;

	/* disable interrupt */
	level = rt_hw_interrupt_disable();

	if (mp->block_free_count)
	{
		/* memory block is available. decrease the free block counter */
		mp->block_free_count --;

		/* get block from block list */
		block_ptr = mp->block_list;
		mp->block_list = *(rt_uint8_t **)block_ptr;

		/* point to memory pool */
		*(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp;
	}
	else
	{
		/* memory block is unavailable. */
		if (time == 0)
		{
			/* enable interrupt */
			rt_hw_interrupt_enable(level);
			return RT_NULL;
		}
		else
		{
			RT_DEBUG_NOT_IN_INTERRUPT;

			/* get current thread */
			thread = rt_thread_self();

			/* need suspend thread */
			rt_thread_suspend(thread);
			rt_list_insert_after(&(mp->suspend_thread), &(thread->tlist));
			mp->suspend_thread_count ++;

			if (time > 0)
			{
				/* init thread timer and start it */
				rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &time);
				rt_timer_start(&(thread->thread_timer));
			}

			/* enable interrupt */
			rt_hw_interrupt_enable(level);

			/* do a schedule */
			rt_schedule();

			if (thread->error != RT_EOK)
				return RT_NULL;

			/* disable interrupt */
			level = rt_hw_interrupt_disable();

			/* decrease free block */
			mp->block_free_count --;

			/* get block from block list */
			block_ptr = mp->block_list;
			mp->block_list = *(rt_uint8_t **)block_ptr;

			/* point to memory pool */
			*(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp;
		}
	}

	/* enable interrupt */
	rt_hw_interrupt_enable(level);

	RT_OBJECT_HOOK_CALL(rt_mp_alloc_hook, (mp, (rt_uint8_t *)(block_ptr + sizeof(rt_uint8_t *))));

	return (rt_uint8_t *)(block_ptr + sizeof(rt_uint8_t *));
}
Example #14
0
/**
 * This function will take a mutex, if the mutex is unavailable, the
 * thread shall wait for a specified time.
 *
 * @param mutex the mutex object
 * @param time the waiting time
 *
 * @return the error code
 */
rt_err_t rt_mutex_take (rt_mutex_t mutex, rt_int32_t time)
{
	register rt_base_t temp;
	struct rt_thread* thread;

	RT_ASSERT(mutex != RT_NULL);

	/* disable interrupt */
	temp = rt_hw_interrupt_disable();

	/* get current thread */
	thread = rt_thread_self();

#ifdef RT_USING_HOOK
	if (rt_object_trytake_hook != RT_NULL) rt_object_trytake_hook(&(mutex->parent.parent));
#endif

#ifdef RT_IPC_DEBUG
	rt_kprintf("mutex_take: current thread %s, mutex value: %d, hold: %d\n", 
		thread->name, mutex->value, mutex->hold);
#endif

	/* reset thread error */
	thread->error = RT_EOK;

	if (mutex->owner == thread)
	{
		/* it's the same thread */
		mutex->hold ++;
	}
	else
	{
		/* in initialization status, the value is 1. Therefore, if the 
		 * value is great than 1, which indicates the mutex is avaible.
		 */
		if (mutex->value > 0)
		{
			/* mutex is available */
			mutex->value --;

			/* set mutex owner and original priority */
			mutex->owner = thread;
			mutex->original_priority = thread->current_priority;
			mutex->hold ++;
		}
		else
		{
			/* no waiting, return with timeout */
			if (time == 0 )
			{
				/* set error as timeout */
				thread->error = -RT_ETIMEOUT;

				/* enable interrupt */
				rt_hw_interrupt_enable(temp);

				return -RT_ETIMEOUT;
			}
			else
			{
				/* mutex is unavailable, push to suspend list */
#ifdef RT_IPC_DEBUG
				rt_kprintf("mutex_take: suspend thread: %s\n", thread->name);
#endif
				/* change the owner thread priority of mutex */
				if (thread->current_priority < mutex->owner->current_priority)
				{
					/* change the owner thread priority */
					rt_thread_control(mutex->owner, RT_THREAD_CTRL_CHANGE_PRIORITY,
						&thread->current_priority);
				}

				/* suspend current thread */
				rt_ipc_object_suspend(&(mutex->parent), thread);

				/* has waiting time, start thread timer */
				if (time > 0)
				{
#ifdef RT_IPC_DEBUG
					rt_kprintf("mutex_take: start the timer of thread:%s\n", thread->name);
#endif
					/* reset the timeout of thread timer and start it */
					rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &time);
					rt_timer_start(&(thread->thread_timer));
				}

				/* enable interrupt */
				rt_hw_interrupt_enable(temp);

				/* do schedule */
				rt_schedule();

				if (thread->error != RT_EOK)
				{
					/* decrease suspended thread count */
					rt_ipc_object_decrease(&(mutex->parent));

					/* return error */
					return thread->error;
				}
				else
				{
					/* the mutex is taken successfully. */
					/* disable interrupt */
					temp = rt_hw_interrupt_disable();
				}
			}
		}
	}

	/* enable interrupt */
	rt_hw_interrupt_enable(temp);

#ifdef RT_USING_HOOK
	if (rt_object_take_hook != RT_NULL) rt_object_take_hook(&(mutex->parent.parent));
#endif

	return RT_EOK;
}
Example #15
0
/**
 * This function will release a mutex, if there are threads suspended on mutex,
 * it will be waked up.
 *
 * @param mutex the mutex object
 *
 * @return the error code
 */
rt_err_t rt_mutex_release(rt_mutex_t mutex)
{
	register rt_base_t temp;
	struct rt_thread* thread;
	rt_bool_t need_schedule;

	need_schedule = RT_FALSE;

	/* get current thread */
	thread = rt_thread_self();

	/* disable interrupt */
	temp = rt_hw_interrupt_disable();

#ifdef RT_IPC_DEBUG
	rt_kprintf("mutex_release:current thread %s, mutex value: %d, hold: %d\n", 
		thread->name, mutex->value, mutex->hold);
#endif

#ifdef RT_USING_HOOK
	if (rt_object_put_hook != RT_NULL) rt_object_put_hook(&(mutex->parent.parent));
#endif

	/* mutex only can be released by owner */
	if (thread != mutex->owner)
	{
		thread->error = -RT_ERROR;

		/* enable interrupt */
		rt_hw_interrupt_enable(temp);

		return -RT_ERROR;
	}

	/* decrease hold */
	mutex->hold --;
	/* if no hold */
	if (mutex->hold == 0)
	{
		/* change the owner thread to original priority */
		if (mutex->owner->init_priority != mutex->owner->current_priority)
		{
			rt_thread_control(mutex->owner, RT_THREAD_CTRL_CHANGE_PRIORITY,
				&(mutex->owner->init_priority));
		}

		/* wakeup suspended thread */
		if (mutex->parent.suspend_thread_count > 0)
		{
			/* get thread entry */
			thread = rt_list_entry(mutex->parent.suspend_thread.next, struct rt_thread, tlist);

#ifdef RT_IPC_DEBUG
		rt_kprintf("mutex_release: resume thread: %s\n", thread->name);
#endif
			/* set new owner and priority */
			mutex->owner = thread;
			mutex->original_priority = thread->current_priority;
			mutex->hold ++;

			/* resume thread */
			rt_ipc_object_resume(&(mutex->parent));

			need_schedule = RT_TRUE;
		}
		else
		{
Example #16
0
void sys_arch_assert(const char *file, int line)
{
    rt_kprintf("\nAssertion: %d in %s, thread %s\n",
               line, file, rt_thread_self()->name);
    RT_ASSERT(0);
}
Example #17
0
/**
 * This function will take a semaphore, if the semaphore is unavailable, the
 * thread shall wait for a specified time.
 *
 * @param sem the semaphore object
 * @param time the waiting time
 *
 * @return the error code
 */
rt_err_t rt_sem_take (rt_sem_t sem, rt_int32_t time)
{
	register rt_base_t temp;
	struct rt_thread* thread;

	RT_ASSERT(sem != RT_NULL);

#ifdef RT_USING_HOOK
	if (rt_object_trytake_hook != RT_NULL) rt_object_trytake_hook(&(sem->parent.parent));
#endif

	/* disable interrupt */
	temp = rt_hw_interrupt_disable();

#ifdef RT_IPC_DEBUG
	rt_kprintf("thread %s take sem:%s, which value is: %d\n", rt_thread_self()->name, 
		((struct rt_object*)sem)->name, sem->value);
#endif
	if (sem->value > 0)
	{
		/* semaphore is available */
		sem->value --;

		/* enable interrupt */
		rt_hw_interrupt_enable(temp);
	}
	else
	{
		/* no waiting, return with timeout */
		if (time == 0 )
		{
			rt_hw_interrupt_enable(temp);
			return -RT_ETIMEOUT;
		}
		else
		{
			/* semaphore is unavailable, push to suspend list */
			/* get current thread */
			thread = rt_thread_self();

			/* reset thread error number */
			thread->error = RT_EOK;

#ifdef RT_IPC_DEBUG
			rt_kprintf("sem take: suspend thread - %s\n", thread->name);
#endif

			/* suspend thread */
			rt_ipc_object_suspend(&(sem->parent), thread);

			/* has waiting time, start thread timer */
			if (time > 0)
			{
#ifdef RT_IPC_DEBUG
				rt_kprintf("set thread:%s to timer list\n", thread->name);
#endif
				/* reset the timeout of thread timer and start it */
				rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &time);
				rt_timer_start(&(thread->thread_timer));
			}

			/* enable interrupt */
			rt_hw_interrupt_enable(temp);

			/* do schedule */
			rt_schedule();

			if (thread->error != RT_EOK)
			{
				/* decrease suspended thread count */
				rt_ipc_object_decrease(&(sem->parent));
				return thread->error;
			}
		}
	}

#ifdef RT_USING_HOOK
	if (rt_object_take_hook != RT_NULL) rt_object_take_hook(&(sem->parent.parent));
#endif

	return RT_EOK;
}
/// Return the thread ID of the current running thread
osThreadId osThreadGetId(void)
{
	return rt_thread_self();
}
Example #19
0
struct rtgui_app* rtgui_app_create(
        rt_thread_t tid,
        const char *title)
{
	rt_thread_t srv_tid;
	struct rtgui_app *app;
	struct rtgui_event_application event;

	RT_ASSERT(tid != RT_NULL);
	RT_ASSERT(title != RT_NULL);

	/* create application */
	app = RTGUI_APP(rtgui_object_create(RTGUI_APP_TYPE));
	if (app == RT_NULL)
		return RT_NULL;

	/* one thread only can create one rtgui application */
	RT_ASSERT(tid->user_data == 0);
	app->tid = tid;
	/* set user thread */
	tid->user_data = (rt_uint32_t)app;

	app->mq = rt_mq_create("rtgui", sizeof(union rtgui_event_generic), 32, RT_IPC_FLAG_FIFO);
	if (app->mq == RT_NULL)
	{
		rt_kprintf("create msgq failed.\n");
		goto __mq_err;
	}

	/* set application title */
	app->name = (unsigned char*)rt_strdup((char*)title);
	if (app->name == RT_NULL)
		goto __err;

	/* send a message to notify rtgui server */
	srv_tid = rtgui_get_server();
	if (srv_tid == RT_NULL)
	{
		rt_kprintf("gui server is not running.\n");
		goto __err;
	}

	/* create the rtgui server application */
	if (srv_tid == rt_thread_self())
		return app;

	RTGUI_EVENT_APP_CREATE_INIT(&event);
	event.app = app;

	/* notify rtgui server to one application has been created */
	if (rtgui_send_sync(srv_tid, RTGUI_EVENT(&event), sizeof(event)) == RT_EOK)
	{
		return app;
	}

__err:
__mq_err:
	rtgui_object_destroy(RTGUI_OBJECT(app));
	tid->user_data = 0;
	return RT_NULL;
}
Example #20
0
/*
 * Create a UDP based client handle.
 * If *sockp<0, *sockp is set to a newly created UPD socket.
 * If raddr->sin_port is 0 a binder on the remote machine
 * is consulted for the correct port number.
 * NB: It is the clients responsibility to close *sockp.
 * NB: The rpch->cl_auth is initialized to null authentication.
 *     Caller may wish to set this something more useful.
 *
 * wait is the amount of time used between retransmitting a call if
 * no response has been heard;  retransmition occurs until the actual
 * rpc call times out.
 *
 * sendsz and recvsz are the maximum allowable packet sizes that can be
 * sent and received.
 */
CLIENT *clntudp_bufcreate(struct sockaddr_in *raddr, 
	unsigned long program, 
	unsigned long version,
	struct timeval wait, 
	int *sockp, 
	unsigned int sendsz,
	unsigned int recvsz)
{
	CLIENT *cl;
	register struct cu_data *cu = NULL;
	struct rpc_msg call_msg;
	static int xid_count = 0;

	cl = (CLIENT *) rt_malloc (sizeof(CLIENT));
	if (cl == NULL)
	{
		rt_kprintf("clntudp_create: out of memory\n");
		goto fooy;
	}
	sendsz = ((sendsz + 3) / 4) * 4;
	recvsz = ((recvsz + 3) / 4) * 4;
	cu = (struct cu_data *) rt_malloc (sizeof(*cu) + sendsz + recvsz);
	if (cu == NULL)
	{
		rt_kprintf("clntudp_create: out of memory\n");
		goto fooy;
	}
	cu->cu_outbuf = &cu->cu_inbuf[recvsz];

	if (raddr->sin_port == 0) {
		unsigned short port;

		if ((port =
			 pmap_getport(raddr, program, version, IPPROTO_UDP)) == 0) {
			goto fooy;
		}
		raddr->sin_port = htons(port);
	}

	cl->cl_ops = &udp_ops;
	cl->cl_private = (char*) cu;
	cu->cu_raddr = *raddr;
	cu->cu_rlen = sizeof(cu->cu_raddr);
	cu->cu_wait = wait;
	cu->cu_total.tv_sec = -1;
	cu->cu_total.tv_usec = -1;
	cu->cu_sendsz = sendsz;
	cu->cu_recvsz = recvsz;
	call_msg.rm_xid = ((unsigned long)rt_thread_self()) ^ ((unsigned long)rt_tick_get()) ^ (xid_count++);
	call_msg.rm_direction = CALL;
	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
	call_msg.rm_call.cb_prog = program;
	call_msg.rm_call.cb_vers = version;
	xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE);
	if (!xdr_callhdr(&(cu->cu_outxdrs), &call_msg))
	{
		goto fooy;
	}
	cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
	if (*sockp < 0)
	{
		int dontblock = 1;

		*sockp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
		if (*sockp < 0)
		{
			rt_kprintf("create socket error\n");
			goto fooy;
		}
		cu->cu_closeit = TRUE;
	}
	else
	{
		cu->cu_closeit = FALSE;
	}
	cu->cu_sock = *sockp;
	cl->cl_auth = authnone_create();
	return (cl);

fooy:
	if (cu) rt_free(cu);
	if (cl) rt_free(cl);

	return ((CLIENT *) NULL);
}
/// Check if the RTOS kernel is already started
int32_t osKernelRunning(void)
{
	return (rt_thread_self() != RT_NULL) ?  1 : 0;
}
Example #22
0
void calibration_entry(void* parameter)
{
    rt_device_t device;
    struct rtgui_rect rect;
    struct rtgui_app* application;
    struct setup_items setup;

    device = rt_device_find("touch");
    if (device == RT_NULL) return; /* no this device */

    calibration_ptr = (struct calibration_session*)
        rt_malloc(sizeof(struct calibration_session));
    rt_memset(calibration_ptr, 0, sizeof(struct calibration_data));
    calibration_ptr->device = device;
    calibration_ptr->tid = rt_thread_self();
    
    rt_device_control(calibration_ptr->device, RT_TOUCH_CALIBRATION, 
        (void*)calibration_data_post);
    
    rtgui_graphic_driver_get_rect(rtgui_graphic_driver_get_default(), &rect);
    
    /* set screen rect */
    calibration_ptr->width = rect.x2;
    calibration_ptr->height = rect.y2;

    application = rtgui_app_create("calibration");
    if (application != RT_NULL)
    {
        /* create calibration window */
        calibration_ptr->win = rtgui_win_create(RT_NULL,
            "calibration", &rect, 
            RTGUI_WIN_STYLE_NO_TITLE | RTGUI_WIN_STYLE_NO_BORDER | 
            RTGUI_WIN_STYLE_ONTOP | RTGUI_WIN_STYLE_DESTROY_ON_CLOSE);
        if (calibration_ptr->win != RT_NULL)
        {   			
            rtgui_object_set_event_handler(RTGUI_OBJECT(calibration_ptr->win),
                calibration_event_handler);
            rtgui_win_show(calibration_ptr->win, RT_TRUE);
        }
        
        rtgui_app_destroy(application);
    }

    /* set calibration data */
    rt_device_control(calibration_ptr->device, RT_TOUCH_CALIBRATION_DATA, 
        &calibration_ptr->data);

    //save setup
    setup.touch_min_x = calibration_ptr->data.min_x;
    setup.touch_max_x = calibration_ptr->data.max_x;
    setup.touch_min_y = calibration_ptr->data.min_y;
    setup.touch_max_y = calibration_ptr->data.max_y;
    setup_save(&setup);
    
    /* recover to normal */
    rt_device_control(calibration_ptr->device, RT_TOUCH_NORMAL, RT_NULL);

    /* release memory */
    rt_free(calibration_ptr);
    calibration_ptr = RT_NULL;
}
Example #23
0
static void application_entry(void* parameter)
{
	struct rtgui_application *app;
	struct rtgui_rect rect;

	app = rtgui_application_create(rt_thread_self(), "gui_demo");
	if (app == RT_NULL)
		return;

	/* create a full screen window */
	rtgui_graphic_driver_get_rect(rtgui_graphic_driver_get_default(), &rect);

	main_win = rtgui_win_create(RT_NULL, "demo_win", &rect,
                        RTGUI_WIN_STYLE_NO_BORDER | RTGUI_WIN_STYLE_NO_TITLE);
	if (main_win == RT_NULL)
	{
		rtgui_application_destroy(app);
		return;
	}

	rtgui_win_set_onkey(main_win, demo_handle_key);

	/* create a no title notebook that we can switch demo on it easily. */
	the_notebook = rtgui_notebook_create(&rect, RTGUI_NOTEBOOK_NOTAB);
	if (the_notebook == RT_NULL)
	{
		rtgui_win_destroy(main_win);
		rtgui_application_destroy(app);
		return;
	}

	rtgui_container_add_child(RTGUI_CONTAINER(main_win), RTGUI_WIDGET(the_notebook));

	/* 初始化各个例子的视图 */
	demo_view_benchmark();

	demo_view_dc();
#ifdef RTGUI_USING_TTF
	demo_view_ttf();
#endif

#ifndef RTGUI_USING_SMALL_SIZE
	demo_view_dc_buffer();
#endif
	demo_view_animation();
#ifndef RTGUI_USING_SMALL_SIZE
	demo_view_buffer_animation();
	demo_view_instrument_panel();
#endif
	demo_view_window();
	demo_view_label();
	demo_view_button();
	demo_view_checkbox();
	demo_view_progressbar();
	demo_view_scrollbar();
	demo_view_radiobox();
	demo_view_textbox();
	demo_view_listbox();
	demo_view_menu();
	demo_view_listctrl();
	demo_view_combobox();
	demo_view_slider();
	demo_view_notebook();
	demo_view_mywidget();
#if 0
#if defined(RTGUI_USING_DFS_FILERW) || defined(RTGUI_USING_STDIO_FILERW)
	demo_view_image();
#endif
#ifdef RT_USING_MODULE	
#if defined(RTGUI_USING_DFS_FILERW) || defined(RTGUI_USING_STDIO_FILERW)
	demo_view_module();
#endif
#endif
	demo_listview_view();
	demo_listview_icon_view();
#if defined(RTGUI_USING_DFS_FILERW) || defined(RTGUI_USING_STDIO_FILERW)
	demo_fn_view();
#endif
#endif

	rtgui_win_show(main_win, RT_FALSE);

	/* 执行工作台事件循环 */
	rtgui_application_run(app);

	rtgui_application_destroy(app);
}
Example #24
0
/**
 * This function will take a semaphore, if the semaphore is unavailable, the
 * thread shall wait for a specified time.
 *
 * @param sem the semaphore object
 * @param time the waiting time
 *
 * @return the error code
 */
rt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t time)
{
	register rt_base_t temp;
	struct rt_thread *thread;

	RT_ASSERT(sem != RT_NULL);

	RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(sem->parent.parent)));

	/* disable interrupt */
	temp = rt_hw_interrupt_disable();

	RT_DEBUG_LOG(RT_DEBUG_IPC,
		("thread %s take sem:%s, which value is: %d\n", rt_thread_self()->name,
		((struct rt_object *)sem)->name, sem->value));

	if (sem->value > 0)
	{
		/* semaphore is available */
		sem->value --;

		/* enable interrupt */
		rt_hw_interrupt_enable(temp);
	}
	else
	{
		/* no waiting, return with timeout */
		if (time == 0)
		{
			rt_hw_interrupt_enable(temp);
			return -RT_ETIMEOUT;
		}
		else
		{
			/* current context checking */
			RT_DEBUG_NOT_IN_INTERRUPT;

			/* semaphore is unavailable, push to suspend list */
			/* get current thread */
			thread = rt_thread_self();

			/* reset thread error number */
			thread->error = RT_EOK;

			RT_DEBUG_LOG(RT_DEBUG_IPC, ("sem take: suspend thread - %s\n", thread->name));

			/* suspend thread */
			rt_ipc_list_suspend(&(sem->parent.suspend_thread),
				thread, sem->parent.parent.flag);

			/* has waiting time, start thread timer */
			if (time > 0)
			{
				RT_DEBUG_LOG(RT_DEBUG_IPC, ("set thread:%s to timer list\n", thread->name));

				/* reset the timeout of thread timer and start it */
				rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &time);
				rt_timer_start(&(thread->thread_timer));
			}

			/* enable interrupt */
			rt_hw_interrupt_enable(temp);

			/* do schedule */
			rt_schedule();

			if (thread->error != RT_EOK)
			{
				return thread->error;
			}
		}
	}

	RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(sem->parent.parent)));

	return RT_EOK;
}
Example #25
0
static void workbench_entry(void* parameter)
{
	rt_mq_t mq;
	struct rtgui_workbench* workbench;

	/* 创建GUI应用需要的消息队列 */
#ifdef RTGUI_USING_SMALL_SIZE
	mq = rt_mq_create("workbench", 32, 32, RT_IPC_FLAG_FIFO);
#else
	mq = rt_mq_create("workbench", 256, 32, RT_IPC_FLAG_FIFO);
#endif
	/* 注册当前线程为GUI线程 */
	rtgui_thread_register(rt_thread_self(), mq);

	/* 创建一个工作台 */
	workbench = rtgui_workbench_create("main", "workbench");
	if (workbench == RT_NULL) return;

	rtgui_widget_set_event_handler(RTGUI_WIDGET(workbench), demo_workbench_event_handler);

	/* 初始化各个例子的视图 */
#if RT_VERSION == 4
	demo_view_benchmark(workbench);
#endif

	demo_view_dc(workbench);
#if RT_VERSION == 4
#ifdef RTGUI_USING_TTF
	demo_view_ttf(workbench);
#endif
#endif

#ifndef RTGUI_USING_SMALL_SIZE
	demo_view_dc_buffer(workbench);
#endif
	demo_view_animation(workbench);
#ifndef RTGUI_USING_SMALL_SIZE
	demo_view_buffer_animation(workbench);
	// demo_view_instrument_panel(workbench);
#endif
	demo_view_window(workbench);
	demo_view_label(workbench);
	demo_view_button(workbench);
	demo_view_checkbox(workbench);
	demo_view_progressbar(workbench);
	demo_view_scrollbar(workbench);
	demo_view_radiobox(workbench);
	demo_view_textbox(workbench);
	demo_view_listbox(workbench);
	demo_view_menu(workbench);
	demo_view_listctrl(workbench);
	demo_view_combobox(workbench);
	demo_view_slider(workbench);
	demo_view_notebook(workbench);
	demo_view_mywidget(workbench);
#if defined(RTGUI_USING_DFS_FILERW) || defined(RTGUI_USING_STDIO_FILERW)
	demo_view_image(workbench);
#endif
#ifdef RT_USING_MODULE	
#if defined(RTGUI_USING_DFS_FILERW) || defined(RTGUI_USING_STDIO_FILERW)
	demo_view_module(workbench);
#endif
#endif
	demo_listview_view(workbench);
	demo_listview_icon_view(workbench);
#if defined(RTGUI_USING_DFS_FILERW) || defined(RTGUI_USING_STDIO_FILERW)
	demo_fn_view(workbench);
#endif

	/* 显示视图 */
	demo_view_show();

	/* 执行工作台事件循环 */
	rtgui_workbench_event_loop(workbench);

	/* 去注册GUI线程 */
	rtgui_thread_deregister(rt_thread_self());
	rt_mq_delete(mq);
}
Example #26
0
void win_thread_entry(void* parameter)
{
	struct rtgui_app* app;
	struct rtgui_win *win;
	struct rtgui_panel *panel;
    struct rtgui_box *box;
    struct rtgui_label *label;
    struct rtgui_notebook *notebook;

    struct rtgui_rect rect = {50, 50, 350, 350};

	app = rtgui_app_create(rt_thread_self(), "MyApp");
	RT_ASSERT(app != RT_NULL);

    win = rtgui_mainwin_create(RT_NULL, "MyWindow", RTGUI_WIN_STYLE_DEFAULT);
    box = rtgui_box_create(RTGUI_VERTICAL, 10);
    rtgui_container_set_box(RTGUI_CONTAINER(win), box);

    /* create a panel */
    panel = rtgui_panel_create(RTGUI_BORDER_BOX);
    RTGUI_WIDGET_ALIGN(panel) = RTGUI_ALIGN_EXPAND;
    rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(panel));

    /* create sub-child for panel */
    box = rtgui_box_create(RTGUI_VERTICAL, 10);
    rtgui_container_set_box(RTGUI_CONTAINER(panel), box);

    label = rtgui_label_create("hello panel!");
    rtgui_container_add_child(RTGUI_CONTAINER(panel), RTGUI_WIDGET(label));

    /* create a notebook */
    notebook = rtgui_notebook_create(&rect, RTGUI_NOTEBOOK_TOP);
    RTGUI_WIDGET_ALIGN(notebook) = RTGUI_ALIGN_EXPAND;
    rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(notebook));
	_notebook = notebook;

    /* create tab-page for notebook  */
    panel = rtgui_panel_create(RTGUI_BORDER_STATIC);
	_panel = panel;
    box = rtgui_box_create(RTGUI_VERTICAL, 10);
    rtgui_container_set_box(RTGUI_CONTAINER(panel), box);

    label = rtgui_label_create("hello panel!");
    rtgui_container_add_child(RTGUI_CONTAINER(panel), RTGUI_WIDGET(label));
    rtgui_notebook_add(notebook, "Panel", RTGUI_WIDGET(panel));

    /* create another page with label */
    label = rtgui_label_create("hello notebook");
    rtgui_notebook_add(notebook, "Text", RTGUI_WIDGET(label));

    /* layout for window */
    rtgui_container_layout(RTGUI_CONTAINER(win));

	rtgui_win_show(win, RT_FALSE);

	rtgui_app_run(app);

	rtgui_win_destroy(win);
	rtgui_app_destroy(app);
	rt_kprintf("MyApp Quit.\n");
}
rt_err_t rt_data_queue_push(struct rt_data_queue *queue,
                            const void *data_ptr,
                            rt_size_t data_size,
                            rt_int32_t timeout)
{
    rt_uint16_t mask;
    rt_ubase_t  level;
    rt_thread_t thread;
    rt_err_t    result;

    RT_ASSERT(queue != RT_NULL);

    result = RT_EOK;
    thread = rt_thread_self();
    mask = queue->size - 1;

    level = rt_hw_interrupt_disable();
    while (queue->put_index - queue->get_index == queue->size)
    {
        queue->waiting_lwm = RT_TRUE;

        /* queue is full */
        if (timeout == 0)
        {
            result = -RT_ETIMEOUT;

            goto __exit;
        }

        /* current context checking */
        RT_DEBUG_NOT_IN_INTERRUPT;

        /* reset thread error number */
        thread->error = RT_EOK;

        /* suspend thread on the push list */
        rt_thread_suspend(thread);
        rt_list_insert_before(&(queue->suspended_push_list), &(thread->tlist));
        /* start timer */
        if (timeout > 0)
        {
            /* reset the timeout of thread timer and start it */
            rt_timer_control(&(thread->thread_timer),
                             RT_TIMER_CTRL_SET_TIME,
                             &timeout);
            rt_timer_start(&(thread->thread_timer));
        }

        /* enable interrupt */
        rt_hw_interrupt_enable(level);

        /* do schedule */
        rt_schedule();

        /* thread is waked up */
        result = thread->error;
        level = rt_hw_interrupt_disable();
        if (result != RT_EOK) goto __exit;
    }

    queue->queue[queue->put_index & mask].data_ptr  = data_ptr;
    queue->queue[queue->put_index & mask].data_size = data_size;
    queue->put_index += 1;

    if (!rt_list_isempty(&(queue->suspended_pop_list)))
    {
        /* there is at least one thread in suspended list */

        /* get thread entry */
        thread = rt_list_entry(queue->suspended_pop_list.next,
                               struct rt_thread,
                               tlist);

        /* resume it */
        rt_thread_resume(thread);
        rt_hw_interrupt_enable(level);

        /* perform a schedule */
        rt_schedule();

        return result;
    }
Example #28
0
rt_err_t rt_completion_wait(struct rt_completion *completion,
                            rt_int32_t            timeout)
{
    rt_err_t result;
    rt_base_t level;
    rt_thread_t thread;
    RT_ASSERT(completion != RT_NULL);

    result = RT_EOK;
    thread = rt_thread_self();

    level = rt_hw_interrupt_disable();
    if (completion->flag != RT_COMPLETED)
    {
        /* only one thread can suspend on complete */
        RT_ASSERT(rt_list_isempty(&(completion->suspended_list)));

        if (timeout == 0)
        {
            result = -RT_ETIMEOUT;
            goto __exit;
        }
        else
        {
            /* reset thread error number */
            thread->error = RT_EOK;

            /* suspend thread */
            rt_thread_suspend(thread);
            /* add to suspended list */
            rt_list_insert_before(&(completion->suspended_list),
                                  &(thread->tlist));

            /* current context checking */
            RT_DEBUG_NOT_IN_INTERRUPT;

            /* start timer */
            if (timeout > 0)
            {
                /* reset the timeout of thread timer and start it */
                rt_timer_control(&(thread->thread_timer),
                                 RT_TIMER_CTRL_SET_TIME,
                                 &timeout);
                rt_timer_start(&(thread->thread_timer));
            }
            /* enable interrupt */
            rt_hw_interrupt_enable(level);

            /* do schedule */
            rt_schedule();

            /* thread is waked up */
            result = thread->error;

            level = rt_hw_interrupt_disable();
            /* clean completed flag */
            completion->flag = RT_UNCOMPLETED;
        }
    }

__exit:
    rt_hw_interrupt_enable(level);

    return result;
}