Last active
December 13, 2018 09:57
-
-
Save JiapengLi/e34258906ff4ec1112058fb9ed8ae398 to your computer and use it in GitHub Desktop.
RT-Thread kernel modification from 2016 to 2017 (2015-11-23 ~ 2017-08-25)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/include/components.h b/include/components.h | |
| index 4113a869..57c0c026 100644 | |
| --- a/include/components.h | |
| +++ b/include/components.h | |
| @@ -63,8 +63,8 @@ | |
| #ifdef RT_USING_DFS_YAFFS2 | |
| #include <dfs_yaffs2.h> | |
| #endif | |
| -#ifdef RT_USING_DFS_ROMFS | |
| -#include <dfs_romfs.h> | |
| +#ifdef RT_USING_DFS_RAMFS | |
| +#include <dfs_ramfs.h> | |
| #endif | |
| #endif | |
| diff --git a/include/rtdef.h b/include/rtdef.h | |
| index a3dad279..77c302df 100644 | |
| --- a/include/rtdef.h | |
| +++ b/include/rtdef.h | |
| @@ -496,7 +496,7 @@ struct rt_thread | |
| void *entry; /**< entry */ | |
| void *parameter; /**< parameter */ | |
| void *stack_addr; /**< stack address */ | |
| - rt_uint16_t stack_size; /**< stack size */ | |
| + rt_uint32_t stack_size; /**< stack size */ | |
| /* error code */ | |
| rt_err_t error; /**< error code */ | |
| @@ -895,6 +895,8 @@ enum | |
| RTGRAPHIC_PIXEL_FORMAT_RGB888, | |
| RTGRAPHIC_PIXEL_FORMAT_ARGB888, | |
| RTGRAPHIC_PIXEL_FORMAT_ABGR888, | |
| + RTGRAPHIC_PIXEL_FORMAT_ARGB565, | |
| + RTGRAPHIC_PIXEL_FORMAT_ALPHA, | |
| }; | |
| /** | |
| diff --git a/include/rtservice.h b/include/rtservice.h | |
| index 6de279e6..99ad0332 100644 | |
| --- a/include/rtservice.h | |
| +++ b/include/rtservice.h | |
| @@ -113,6 +113,27 @@ rt_inline int rt_list_isempty(const rt_list_t *l) | |
| #define rt_list_entry(node, type, member) \ | |
| ((type *)((char *)(node) - (unsigned long)(&((type *)0)->member))) | |
| +/** | |
| + * rt_list_for_each_entry - iterate over list of given type | |
| + * @pos: the type * to use as a loop cursor. | |
| + * @head: the head for your list. | |
| + * @member: the name of the list_struct within the struct. | |
| + */ | |
| +#define rt_list_for_each_entry(pos, head, member) \ | |
| + for (pos = rt_list_entry((head)->next, typeof(*pos), member); \ | |
| + &pos->member != (head); \ | |
| + pos = rt_list_entry(pos->member.next, typeof(*pos), member)) | |
| + | |
| +/** | |
| + * rt_list_first_entry - get the first element from a list | |
| + * @ptr: the list head to take the element from. | |
| + * @type: the type of the struct this is embedded in. | |
| + * @member: the name of the list_struct within the struct. | |
| + * | |
| + * Note, that list is expected to be not empty. | |
| + */ | |
| +#define rt_list_first_entry(ptr, type, member) \ | |
| + rt_list_entry((ptr)->next, type, member) | |
| /*@}*/ | |
| #ifdef __cplusplus | |
| diff --git a/include/rtthread.h b/include/rtthread.h | |
| index c7f35d16..f67bab99 100644 | |
| --- a/include/rtthread.h | |
| +++ b/include/rtthread.h | |
| @@ -26,6 +26,7 @@ | |
| * 2007-03-03 Bernard clean up the definitions to rtdef.h | |
| * 2010-04-11 yi.qiu add module feature | |
| * 2013-06-24 Bernard add rt_kprintf re-define when not use RT_USING_CONSOLE. | |
| + * 2016-08-09 ArdaFu add new thread and interrupt hook. | |
| */ | |
| #ifndef __RT_THREAD_H__ | |
| @@ -45,7 +46,7 @@ extern "C" { | |
| * @addtogroup KernelObject | |
| */ | |
| -/*@{*/ | |
| +/**@{*/ | |
| /* | |
| * kernel object interface | |
| @@ -71,13 +72,13 @@ void rt_object_take_sethook(void (*hook)(struct rt_object *object)); | |
| void rt_object_put_sethook(void (*hook)(struct rt_object *object)); | |
| #endif | |
| -/*@}*/ | |
| +/**@}*/ | |
| /** | |
| * @addtogroup Clock | |
| */ | |
| -/*@{*/ | |
| +/**@{*/ | |
| /* | |
| * clock & timer interface | |
| @@ -115,13 +116,13 @@ void rt_timer_check(void); | |
| void rt_timer_timeout_sethook(void (*hook)(struct rt_timer *timer)); | |
| #endif | |
| -/*@}*/ | |
| +/**@}*/ | |
| /** | |
| * @addtogroup Thread | |
| */ | |
| -/*@{*/ | |
| +/**@{*/ | |
| /* | |
| * thread interface | |
| @@ -153,14 +154,21 @@ rt_err_t rt_thread_suspend(rt_thread_t thread); | |
| rt_err_t rt_thread_resume(rt_thread_t thread); | |
| void rt_thread_timeout(void *parameter); | |
| +#ifdef RT_USING_HOOK | |
| +void rt_thread_suspend_sethook(void (*hook)(rt_thread_t thread)); | |
| +void rt_thread_resume_sethook (void (*hook)(rt_thread_t thread)); | |
| +void rt_thread_inited_sethook (void (*hook)(rt_thread_t thread)); | |
| +#endif | |
| + | |
| /* | |
| * idle thread interface | |
| */ | |
| void rt_thread_idle_init(void); | |
| -#ifdef RT_USING_HOOK | |
| +#if defined(RT_USING_HOOK) || defined(RT_USING_IDLE_HOOK) | |
| void rt_thread_idle_sethook(void (*hook)(void)); | |
| #endif | |
| void rt_thread_idle_excute(void); | |
| +rt_thread_t rt_thread_idle_gethandler(void); | |
| /* | |
| * schedule service | |
| @@ -180,13 +188,13 @@ rt_uint16_t rt_critical_level(void); | |
| void rt_scheduler_sethook(void (*hook)(rt_thread_t from, rt_thread_t to)); | |
| #endif | |
| -/*@}*/ | |
| +/**@}*/ | |
| /** | |
| * @addtogroup MM | |
| */ | |
| -/*@{*/ | |
| +/**@{*/ | |
| /* | |
| * memory management interface | |
| @@ -259,13 +267,13 @@ void *rt_memheap_realloc(struct rt_memheap* heap, void* ptr, rt_size_t newsize); | |
| void rt_memheap_free(void *ptr); | |
| #endif | |
| -/*@}*/ | |
| +/**@}*/ | |
| /** | |
| * @addtogroup IPC | |
| */ | |
| -/*@{*/ | |
| +/**@{*/ | |
| #ifdef RT_USING_SEMAPHORE | |
| /* | |
| @@ -364,14 +372,14 @@ rt_err_t rt_mq_recv(rt_mq_t mq, | |
| rt_err_t rt_mq_control(rt_mq_t mq, rt_uint8_t cmd, void *arg); | |
| #endif | |
| -/*@}*/ | |
| +/**@}*/ | |
| #ifdef RT_USING_DEVICE | |
| /** | |
| * @addtogroup Device | |
| */ | |
| -/*@{*/ | |
| +/**@{*/ | |
| /* | |
| * device (I/O) system interface | |
| @@ -404,7 +412,7 @@ rt_size_t rt_device_write(rt_device_t dev, | |
| rt_size_t size); | |
| rt_err_t rt_device_control(rt_device_t dev, rt_uint8_t cmd, void *arg); | |
| -/*@}*/ | |
| +/**@}*/ | |
| #endif | |
| #ifdef RT_USING_MODULE | |
| @@ -412,7 +420,7 @@ rt_err_t rt_device_control(rt_device_t dev, rt_uint8_t cmd, void *arg); | |
| * @addtogroup Module | |
| */ | |
| -/*@{*/ | |
| +/**@{*/ | |
| /* | |
| * module interface | |
| @@ -442,7 +450,7 @@ rt_err_t rt_module_destroy(rt_module_t module); | |
| */ | |
| int rt_system_module_init(void); | |
| -/*@}*/ | |
| +/**@}*/ | |
| #endif | |
| /* | |
| @@ -460,6 +468,11 @@ void rt_interrupt_leave(void); | |
| */ | |
| rt_uint8_t rt_interrupt_get_nest(void); | |
| +#ifdef RT_USING_HOOK | |
| +void rt_interrupt_enter_sethook(void (*hook)(void)); | |
| +void rt_interrupt_leave_sethook(void (*hook)(void)); | |
| +#endif | |
| + | |
| #ifdef RT_USING_COMPONENTS_INIT | |
| void rt_components_init(void); | |
| void rt_components_board_init(void); | |
| @@ -469,15 +482,17 @@ void rt_components_board_init(void); | |
| * @addtogroup KernelService | |
| */ | |
| -/*@{*/ | |
| +/**@{*/ | |
| /* | |
| * general kernel service | |
| */ | |
| #ifndef RT_USING_CONSOLE | |
| #define rt_kprintf(...) | |
| +#define rt_kputs(str) | |
| #else | |
| void rt_kprintf(const char *fmt, ...); | |
| +void rt_kputs(const char *str); | |
| #endif | |
| rt_int32_t rt_vsprintf(char *dest, const char *format, va_list arg_ptr); | |
| rt_int32_t rt_vsnprintf(char *buf, rt_size_t size, const char *fmt, va_list args); | |
| @@ -492,7 +507,7 @@ rt_device_t rt_console_get_device(void); | |
| rt_err_t rt_get_errno(void); | |
| void rt_set_errno(rt_err_t no); | |
| int *_rt_errno(void); | |
| -#ifndef RT_USING_NEWLIB | |
| +#if !defined(RT_USING_NEWLIB) && !defined(_WIN32) | |
| #ifndef errno | |
| #define errno *_rt_errno() | |
| #endif | |
| @@ -522,7 +537,7 @@ void rt_assert_set_hook(void (*hook)(const char* ex, const char* func, rt_size_t | |
| void rt_assert_handler(const char* ex, const char* func, rt_size_t line); | |
| #endif /* RT_DEBUG */ | |
| -/*@}*/ | |
| +/**@}*/ | |
| #ifdef __cplusplus | |
| } | |
| diff --git a/src/KConfig b/src/KConfig | |
| new file mode 100644 | |
| index 00000000..8c2b73f3 | |
| --- /dev/null | |
| +++ b/src/KConfig | |
| @@ -0,0 +1,157 @@ | |
| +menu "RT-Thread Kernel" | |
| + | |
| +config RT_NAME_MAX | |
| + int "The maximal size of kernel object name" | |
| + range 2 32 | |
| + default 8 | |
| + help | |
| + Each kernel object, such as thread, timer, semaphore etc, has a name, the RT_NAME_MAX is the maximal size of this object name. | |
| + | |
| +config RT_ALIGN_SIZE | |
| + int "Alignment size for CPU architecture data access" | |
| + default 4 | |
| + help | |
| + Alignment size for CPU architecture data access | |
| + | |
| +config RT_THREAD_PRIORITY_MAX | |
| + int "The maximal level value of priority of thread" | |
| + range 8 256 | |
| + default 32 | |
| + | |
| +config RT_TICK_PER_SECOND | |
| + int "Tick frequency, Hz" | |
| + range 10 1000 | |
| + default 100 | |
| + help | |
| + System's tick frequency, Hz. | |
| + | |
| +config RT_DEBUG | |
| + bool "Enable debug features" | |
| + default y | |
| + | |
| +if RT_DEBUG | |
| + | |
| +config RT_USING_OVERFLOW_CHECK | |
| + bool "Using stack overflow checking" | |
| + default y | |
| + help | |
| + Enable thread stack overflow checking. The stack overflow is checking when each thread switch. | |
| + | |
| +config RT_DEBUG_INIT | |
| + bool "Enable system initialization informat print" | |
| + default n | |
| + help | |
| + print the procedure name of initialization | |
| + | |
| +config RT_DEBUG_THREAD | |
| + bool "Enable thread switch information dump" | |
| + default n | |
| + help | |
| + print the switch to/from thread name when each thread context switch | |
| + | |
| +endif | |
| + | |
| +config RT_USING_HOOK | |
| + bool "Enable system hook" | |
| + default y | |
| + help | |
| + Enable the hook function when system running, such as idle thread hook, thread context switch etc. | |
| + | |
| +config RT_USING_TIMER_SOFT | |
| + bool "Enable software timer with a timer thread" | |
| + default n | |
| + help | |
| + the timeout function context of soft-timer is under a high priority timer thread. | |
| + | |
| +if RT_USING_TIMER_SOFT | |
| +config RT_TIMER_THREAD_PRIO | |
| + int "The priority level value of timer thread" | |
| + default 4 | |
| + | |
| +config RT_TIMER_THREAD_STACK_SIZE | |
| + int "The stack size of timer thread" | |
| + default 512 | |
| + | |
| +endif | |
| + | |
| +menu "Inter-Thread communication" | |
| + | |
| +config RT_USING_SEMAPHORE | |
| + bool "Enable semaphore" | |
| + default y | |
| + | |
| +config RT_USING_MUTEX | |
| + bool "Enable mutex" | |
| + default y | |
| + | |
| +config RT_USING_EVENT | |
| + bool "Enable event flag" | |
| + default y | |
| + | |
| +config RT_USING_MAILBOX | |
| + bool "Enable mailbox" | |
| + default y | |
| + | |
| +config RT_USING_MESSAGEQUEUE | |
| + bool "Enable message queue" | |
| + default y | |
| + | |
| +endmenu | |
| + | |
| +menu "Memory Management" | |
| + | |
| + config RT_USING_MEMPOOL | |
| + bool "Using memory pool" | |
| + default y | |
| + help | |
| + Using static memory fixed partition | |
| + | |
| + config RT_USING_MEMHEAP | |
| + bool "Using memory heap object" | |
| + default n | |
| + help | |
| + Using memory heap object to manage dynamic memory heap. | |
| + | |
| + config RT_USING_HEAP | |
| + bool "Using dynamic memory management" | |
| + default y | |
| + | |
| + if RT_USING_HEAP | |
| + | |
| + config RT_USING_SMALL_MEM | |
| + bool "The memory management for small memory" | |
| + | |
| + config RT_USING_SLAB | |
| + bool "Using SLAB memory management for large memory" | |
| + | |
| + endif | |
| + | |
| +endmenu | |
| + | |
| +menu "Kernel Device Object" | |
| + | |
| + config RT_USING_DEVICE | |
| + bool "Using device object" | |
| + default y | |
| + | |
| + config RT_USING_CONSOLE | |
| + bool "Using console for rt_kprintf" | |
| + default y | |
| + | |
| + if RT_USING_CONSOLE | |
| + config RT_CONSOLEBUF_SIZE | |
| + int "the buffer size for console log printf" | |
| + default 128 | |
| + | |
| + config RT_CONSOLE_DEVICE_NAME | |
| + string "the device name for console" | |
| + default "uart" | |
| + endif | |
| + | |
| +endmenu | |
| + | |
| +config RT_USING_MODULE | |
| + bool "The dynamic module feature" | |
| + default n | |
| + | |
| +endmenu | |
| diff --git a/src/clock.c b/src/clock.c | |
| index 63f65b87..36e26238 100644 | |
| --- a/src/clock.c | |
| +++ b/src/clock.c | |
| @@ -50,7 +50,7 @@ void rt_system_tick_init(void) | |
| * @addtogroup Clock | |
| */ | |
| -/*@{*/ | |
| +/**@{*/ | |
| /** | |
| * This function will return current tick from operating system startup | |
| @@ -118,5 +118,5 @@ rt_tick_t rt_tick_from_millisecond(rt_uint32_t ms) | |
| } | |
| RTM_EXPORT(rt_tick_from_millisecond); | |
| -/*@}*/ | |
| +/**@}*/ | |
| diff --git a/src/components.c b/src/components.c | |
| index 4b37bb4f..2e0d8c09 100644 | |
| --- a/src/components.c | |
| +++ b/src/components.c | |
| @@ -33,6 +33,12 @@ | |
| #include <rthw.h> | |
| #include <rtthread.h> | |
| +#ifdef RT_USING_USER_MAIN | |
| +#ifndef RT_MAIN_THREAD_STACK_SIZE | |
| +#define RT_MAIN_THREAD_STACK_SIZE 2048 | |
| +#endif | |
| +#endif | |
| + | |
| #ifdef RT_USING_COMPONENTS_INIT | |
| /* | |
| * Components Initialization will initialize some driver and components as following | |
| @@ -172,7 +178,7 @@ int entry(void) | |
| #ifndef RT_USING_HEAP | |
| /* if there is not enable heap, we should use static thread and stack. */ | |
| ALIGN(8) | |
| -static rt_uint8_t main_stack[2048]; | |
| +static rt_uint8_t main_stack[RT_MAIN_THREAD_STACK_SIZE]; | |
| struct rt_thread main_thread; | |
| #endif | |
| @@ -199,15 +205,15 @@ void rt_application_init(void) | |
| #ifdef RT_USING_HEAP | |
| tid = rt_thread_create("main", main_thread_entry, RT_NULL, | |
| - 2048, RT_THREAD_PRIORITY_MAX / 3, 20); | |
| + RT_MAIN_THREAD_STACK_SIZE, RT_THREAD_PRIORITY_MAX / 3, 20); | |
| RT_ASSERT(tid != RT_NULL); | |
| #else | |
| rt_err_t result; | |
| tid = &main_thread; | |
| result = rt_thread_init(tid, "main", main_thread_entry, RT_NULL, | |
| - 2048, RT_THREAD_PRIORITY_MAX / 3, 20); | |
| - RT_ASSERT(result != RT_EOK); | |
| + main_stack, sizeof(main_stack), RT_THREAD_PRIORITY_MAX / 3, 20); | |
| + RT_ASSERT(result == RT_EOK); | |
| #endif | |
| rt_thread_startup(tid); | |
| diff --git a/src/device.c b/src/device.c | |
| index 957c9fc7..1175a340 100644 | |
| --- a/src/device.c | |
| +++ b/src/device.c | |
| @@ -25,6 +25,7 @@ | |
| * provided by Rob <rdent@iinet.net.au> | |
| * 2012-12-25 Bernard return RT_EOK if the device interface not exist. | |
| * 2013-07-09 Grissiom add ref_count support | |
| + * 2016-04-02 Bernard fix the open_flag initialization issue. | |
| */ | |
| #include <rtthread.h> | |
| @@ -53,6 +54,7 @@ rt_err_t rt_device_register(rt_device_t dev, | |
| rt_object_init(&(dev->parent), RT_Object_Class_Device, name); | |
| dev->flag = flags; | |
| dev->ref_count = 0; | |
| + dev->open_flag = 0; | |
| return RT_EOK; | |
| } | |
| diff --git a/src/idle.c b/src/idle.c | |
| index 867af876..db87baa4 100644 | |
| --- a/src/idle.c | |
| +++ b/src/idle.c | |
| @@ -24,13 +24,20 @@ | |
| * 2012-12-29 Bernard fix compiling warning. | |
| * 2013-12-21 Grissiom let rt_thread_idle_excute loop until there is no | |
| * dead thread. | |
| + * 2016-08-09 ArdaFu add method to get the handler of the idle thread. | |
| */ | |
| #include <rthw.h> | |
| #include <rtthread.h> | |
| +#if defined (RT_USING_HOOK) | |
| +#ifndef RT_USING_IDLE_HOOK | |
| +#define RT_USING_IDLE_HOOK | |
| +#endif | |
| +#endif | |
| + | |
| #ifndef IDLE_THREAD_STACK_SIZE | |
| -#if defined (RT_USING_HOOK) || defined(RT_USING_HEAP) | |
| +#if defined (RT_USING_IDLE_HOOK) || defined(RT_USING_HEAP) | |
| #define IDLE_THREAD_STACK_SIZE 256 | |
| #else | |
| #define IDLE_THREAD_STACK_SIZE 128 | |
| @@ -43,7 +50,7 @@ static rt_uint8_t rt_thread_stack[IDLE_THREAD_STACK_SIZE]; | |
| extern rt_list_t rt_thread_defunct; | |
| -#ifdef RT_USING_HOOK | |
| +#ifdef RT_USING_IDLE_HOOK | |
| static void (*rt_thread_idle_hook)(); | |
| /** | |
| @@ -178,9 +185,11 @@ static void rt_thread_idle_entry(void *parameter) | |
| { | |
| while (1) | |
| { | |
| - #ifdef RT_USING_HOOK | |
| + #ifdef RT_USING_IDLE_HOOK | |
| if (rt_thread_idle_hook != RT_NULL) | |
| + { | |
| rt_thread_idle_hook(); | |
| + } | |
| #endif | |
| rt_thread_idle_excute(); | |
| @@ -209,3 +218,14 @@ void rt_thread_idle_init(void) | |
| /* startup */ | |
| rt_thread_startup(&idle); | |
| } | |
| + | |
| +/** | |
| + * @ingroup Thread | |
| + * | |
| + * This function will get the handler of the idle thread. | |
| + * | |
| + */ | |
| +rt_thread_t rt_thread_idle_gethandler(void) | |
| +{ | |
| + return (rt_thread_t)(&idle); | |
| +} | |
| diff --git a/src/ipc.c b/src/ipc.c | |
| index 8c12f701..9ecd6020 100644 | |
| --- a/src/ipc.c | |
| +++ b/src/ipc.c | |
| @@ -61,7 +61,7 @@ extern void (*rt_object_put_hook)(struct rt_object *object); | |
| * @addtogroup IPC | |
| */ | |
| -/*@{*/ | |
| +/**@{*/ | |
| /** | |
| * This function will initialize an IPC object | |
| @@ -2281,4 +2281,4 @@ rt_err_t rt_mq_control(rt_mq_t mq, rt_uint8_t cmd, void *arg) | |
| RTM_EXPORT(rt_mq_control); | |
| #endif /* end of RT_USING_MESSAGEQUEUE */ | |
| -/*@}*/ | |
| +/**@}*/ | |
| diff --git a/src/irq.c b/src/irq.c | |
| index 70b18e01..9b933e39 100644 | |
| --- a/src/irq.c | |
| +++ b/src/irq.c | |
| @@ -21,18 +21,46 @@ | |
| * Date Author Notes | |
| * 2006-02-24 Bernard first version | |
| * 2006-05-03 Bernard add IRQ_DEBUG | |
| + * 2016-08-09 ArdaFu add interrupt enter and leave hook. | |
| */ | |
| #include <rthw.h> | |
| #include <rtthread.h> | |
| +#ifdef RT_USING_HOOK | |
| + | |
| +static void (*rt_interrupt_enter_hook)(void); | |
| +static void (*rt_interrupt_leave_hook)(void); | |
| + | |
| +/** | |
| + * @ingroup Hook | |
| + * This function set a hook function when the system enter a interrupt | |
| + * | |
| + * @note the hook function must be simple and never be blocked or suspend. | |
| + */ | |
| +void rt_interrupt_enter_sethook(void (*hook)(void)) | |
| +{ | |
| + rt_interrupt_enter_hook = hook; | |
| +} | |
| +/** | |
| + * @ingroup Hook | |
| + * This function set a hook function when the system exit a interrupt. | |
| + * | |
| + * @note the hook function must be simple and never be blocked or suspend. | |
| + */ | |
| +void rt_interrupt_leave_sethook(void (*hook)(void)) | |
| +{ | |
| + rt_interrupt_leave_hook = hook; | |
| +} | |
| +#endif | |
| + | |
| /* #define IRQ_DEBUG */ | |
| /** | |
| * @addtogroup Kernel | |
| */ | |
| -/*@{*/ | |
| +/**@{*/ | |
| volatile rt_uint8_t rt_interrupt_nest; | |
| @@ -52,6 +80,7 @@ void rt_interrupt_enter(void) | |
| level = rt_hw_interrupt_disable(); | |
| rt_interrupt_nest ++; | |
| + RT_OBJECT_HOOK_CALL(rt_interrupt_enter_hook,()); | |
| rt_hw_interrupt_enable(level); | |
| } | |
| RTM_EXPORT(rt_interrupt_enter); | |
| @@ -72,6 +101,7 @@ void rt_interrupt_leave(void) | |
| level = rt_hw_interrupt_disable(); | |
| rt_interrupt_nest --; | |
| + RT_OBJECT_HOOK_CALL(rt_interrupt_leave_hook,()); | |
| rt_hw_interrupt_enable(level); | |
| } | |
| RTM_EXPORT(rt_interrupt_leave); | |
| @@ -93,5 +123,5 @@ RTM_EXPORT(rt_interrupt_get_nest); | |
| RTM_EXPORT(rt_hw_interrupt_disable); | |
| RTM_EXPORT(rt_hw_interrupt_enable); | |
| -/*@}*/ | |
| +/**@}*/ | |
| diff --git a/src/kservice.c b/src/kservice.c | |
| index f18f792d..123c8529 100644 | |
| --- a/src/kservice.c | |
| +++ b/src/kservice.c | |
| @@ -43,7 +43,7 @@ | |
| * @addtogroup KernelService | |
| */ | |
| -/*@{*/ | |
| +/**@{*/ | |
| /* global errno in RT-Thread */ | |
| static volatile int _errno; | |
| @@ -135,7 +135,7 @@ RTM_EXPORT(_rt_errno); | |
| */ | |
| void *rt_memset(void *s, int c, rt_ubase_t count) | |
| { | |
| -#ifdef RT_TINY_SIZE | |
| +#ifdef RT_USING_TINY_SIZE | |
| char *xs = (char *)s; | |
| while (count--) | |
| @@ -218,7 +218,7 @@ RTM_EXPORT(rt_memset); | |
| */ | |
| void *rt_memcpy(void *dst, const void *src, rt_ubase_t count) | |
| { | |
| -#ifdef RT_TINY_SIZE | |
| +#ifdef RT_USING_TINY_SIZE | |
| char *tmp = (char *)dst, *s = (char *)src; | |
| while (count--) | |
| @@ -462,7 +462,26 @@ rt_int32_t rt_strcmp(const char *cs, const char *ct) | |
| return (*cs - *ct); | |
| } | |
| RTM_EXPORT(rt_strcmp); | |
| +/** | |
| + * The strnlen() function returns the number of characters in the | |
| + * string pointed to by s, excluding the terminating null byte ('\0'), | |
| + * but at most maxlen. In doing this, strnlen() looks only at the | |
| + * first maxlen characters in the string pointed to by s and never | |
| + * beyond s+maxlen. | |
| + * | |
| + * @param s the string | |
| + * @param maxlen the max size | |
| + * @return the length of string | |
| + */ | |
| +rt_size_t rt_strnlen(const char *s, rt_ubase_t maxlen) | |
| +{ | |
| + const char *sc; | |
| + for (sc = s; *sc != '\0' && sc - s < maxlen; ++sc) /* nothing */ | |
| + ; | |
| + | |
| + return sc - s; | |
| +} | |
| /** | |
| * This function will return the length of a string, which terminate will | |
| * null character. | |
| @@ -514,7 +533,7 @@ void rt_show_version(void) | |
| rt_kprintf("- RT - Thread Operating System\n"); | |
| rt_kprintf(" / | \\ %d.%d.%d build %s\n", | |
| RT_VERSION, RT_SUBVERSION, RT_REVISION, __DATE__); | |
| - rt_kprintf(" 2006 - 2015 Copyright by rt-thread team\n"); | |
| + rt_kprintf(" 2006 - 2017 Copyright by rt-thread team\n"); | |
| } | |
| RTM_EXPORT(rt_show_version); | |
| @@ -1089,6 +1108,31 @@ WEAK void rt_hw_console_output(const char *str) | |
| RTM_EXPORT(rt_hw_console_output); | |
| /** | |
| + * This function will put string to the console. | |
| + * | |
| + * @param str the string output to the console. | |
| + */ | |
| +void rt_kputs(const char *str) | |
| +{ | |
| +#ifdef RT_USING_DEVICE | |
| + if (_console_device == RT_NULL) | |
| + { | |
| + rt_hw_console_output(str); | |
| + } | |
| + else | |
| + { | |
| + rt_uint16_t old_flag = _console_device->open_flag; | |
| + | |
| + _console_device->open_flag |= RT_DEVICE_FLAG_STREAM; | |
| + rt_device_write(_console_device, 0, str, rt_strlen(str)); | |
| + _console_device->open_flag = old_flag; | |
| + } | |
| +#else | |
| + rt_hw_console_output(str); | |
| +#endif | |
| +} | |
| + | |
| +/** | |
| * This function will print a formatted string on system console | |
| * | |
| * @param fmt the format | |
| @@ -1221,7 +1265,7 @@ const rt_uint8_t __lowest_bit_bitmap[] = | |
| * @return return the index of the first bit set. If value is 0, then this function | |
| * shall return 0. | |
| */ | |
| -int __rt_ffs(int value) | |
| +rt_ubase_t __rt_ffs(rt_ubase_t value) | |
| { | |
| if (value == 0) return 0; | |
| @@ -1309,4 +1353,4 @@ int vsprintf(char *buf, const char *format, va_list arg_ptr) __attribute__((weak | |
| #endif | |
| -/*@}*/ | |
| +/**@}*/ | |
| diff --git a/src/mem.c b/src/mem.c | |
| index c45059c5..b19e46d5 100644 | |
| --- a/src/mem.c | |
| +++ b/src/mem.c | |
| @@ -76,7 +76,7 @@ static void (*rt_free_hook)(void *ptr); | |
| * @addtogroup Hook | |
| */ | |
| -/*@{*/ | |
| +/**@{*/ | |
| /** | |
| * This function will set a hook function, which will be invoked when a memory | |
| @@ -100,7 +100,7 @@ void rt_free_sethook(void (*hook)(void *ptr)) | |
| rt_free_hook = hook; | |
| } | |
| -/*@}*/ | |
| +/**@}*/ | |
| #endif | |
| @@ -234,7 +234,7 @@ void rt_system_heap_init(void *begin_addr, void *end_addr) | |
| * @addtogroup MM | |
| */ | |
| -/*@{*/ | |
| +/**@{*/ | |
| /** | |
| * Allocate a block of memory with a minimum of 'size' bytes. | |
| @@ -585,7 +585,7 @@ FINSH_FUNCTION_EXPORT(list_mem, list memory usage information) | |
| #endif | |
| #endif | |
| -/*@}*/ | |
| +/**@}*/ | |
| #endif /* end of RT_USING_HEAP */ | |
| #endif /* end of RT_USING_MEMHEAP_AS_HEAP */ | |
| diff --git a/src/mempool.c b/src/mempool.c | |
| index b6d83597..a01e2145 100644 | |
| --- a/src/mempool.c | |
| +++ b/src/mempool.c | |
| @@ -43,7 +43,7 @@ static void (*rt_mp_free_hook)(struct rt_mempool *mp, void *block); | |
| * @addtogroup Hook | |
| */ | |
| -/*@{*/ | |
| +/**@{*/ | |
| /** | |
| * This function will set a hook function, which will be invoked when a memory | |
| @@ -67,14 +67,14 @@ void rt_mp_free_sethook(void (*hook)(struct rt_mempool *mp, void *block)) | |
| rt_mp_free_hook = hook; | |
| } | |
| -/*@}*/ | |
| +/**@}*/ | |
| #endif | |
| /** | |
| * @addtogroup MM | |
| */ | |
| -/*@{*/ | |
| +/**@{*/ | |
| /** | |
| * This function will initialize a memory pool object, normally which is used | |
| @@ -465,7 +465,7 @@ void rt_mp_free(void *block) | |
| } | |
| RTM_EXPORT(rt_mp_free); | |
| -/*@}*/ | |
| +/**@}*/ | |
| #endif | |
| diff --git a/src/module.c b/src/module.c | |
| index b03611a4..c1a34756 100644 | |
| --- a/src/module.c | |
| +++ b/src/module.c | |
| @@ -116,7 +116,7 @@ int rt_system_module_init(void) | |
| _rt_module_symtab_end = (struct rt_module_symtab *)&RTMSymTab$$Limit; | |
| #elif defined (__IAR_SYSTEMS_ICC__) | |
| _rt_module_symtab_begin = __section_begin("RTMSymTab"); | |
| - _rt_module_symtab_end = __section_begin("RTMSymTab"); | |
| + _rt_module_symtab_end = __section_end("RTMSymTab"); | |
| #endif | |
| #ifdef RT_USING_SLAB | |
| @@ -366,7 +366,7 @@ static void (*rt_module_unload_hook)(rt_module_t module); | |
| * @addtogroup Hook | |
| */ | |
| -/*@{*/ | |
| +/**@{*/ | |
| /** | |
| * This function will set a hook function, which will be invoked when module | |
| @@ -390,7 +390,7 @@ void rt_module_unload_sethook(void (*hook)(rt_module_t module)) | |
| rt_module_unload_hook = hook; | |
| } | |
| -/*@}*/ | |
| +/**@}*/ | |
| #endif | |
| static struct rt_module *_load_shared_object(const char *name, | |
| diff --git a/src/object.c b/src/object.c | |
| index ae8234f4..d71f792b 100644 | |
| --- a/src/object.c | |
| +++ b/src/object.c | |
| @@ -87,7 +87,7 @@ void (*rt_object_put_hook)(struct rt_object *object); | |
| * @addtogroup Hook | |
| */ | |
| -/*@{*/ | |
| +/**@{*/ | |
| /** | |
| * This function will set a hook function, which will be invoked when object | |
| @@ -159,7 +159,7 @@ void rt_object_put_sethook(void (*hook)(struct rt_object *object)) | |
| rt_object_put_hook = hook; | |
| } | |
| -/*@}*/ | |
| +/**@}*/ | |
| #endif | |
| /** | |
| @@ -178,7 +178,7 @@ void rt_system_object_init(void) | |
| * @addtogroup KernelObject | |
| */ | |
| -/*@{*/ | |
| +/**@{*/ | |
| /** | |
| * This function will return the specified type of object information. | |
| @@ -494,4 +494,4 @@ rt_object_t rt_object_find(const char *name, rt_uint8_t type) | |
| return RT_NULL; | |
| } | |
| -/*@}*/ | |
| +/**@}*/ | |
| diff --git a/src/scheduler.c b/src/scheduler.c | |
| index d371e55a..186d20e5 100644 | |
| --- a/src/scheduler.c | |
| +++ b/src/scheduler.c | |
| @@ -42,7 +42,7 @@ | |
| static rt_int16_t rt_scheduler_lock_nest; | |
| extern volatile rt_uint8_t rt_interrupt_nest; | |
| -extern int __rt_ffs(int value); | |
| +extern rt_ubase_t __rt_ffs(rt_ubase_t value); | |
| rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX]; | |
| struct rt_thread *rt_current_thread; | |
| @@ -67,7 +67,7 @@ static void (*rt_scheduler_hook)(struct rt_thread *from, struct rt_thread *to); | |
| * @addtogroup Hook | |
| */ | |
| -/*@{*/ | |
| +/**@{*/ | |
| /** | |
| * This function will set a hook function, which will be invoked when thread | |
| @@ -81,7 +81,7 @@ rt_scheduler_sethook(void (*hook)(struct rt_thread *from, struct rt_thread *to)) | |
| rt_scheduler_hook = hook; | |
| } | |
| -/*@}*/ | |
| +/**@}*/ | |
| #endif | |
| #ifdef RT_USING_OVERFLOW_CHECK | |
| @@ -89,7 +89,8 @@ static void _rt_scheduler_stack_check(struct rt_thread *thread) | |
| { | |
| RT_ASSERT(thread != RT_NULL); | |
| - if ((rt_uint32_t)thread->sp <= (rt_uint32_t)thread->stack_addr || | |
| + if (*((rt_uint8_t *)thread->stack_addr) != '#' || | |
| + (rt_uint32_t)thread->sp <= (rt_uint32_t)thread->stack_addr || | |
| (rt_uint32_t)thread->sp > | |
| (rt_uint32_t)thread->stack_addr + (rt_uint32_t)thread->stack_size) | |
| { | |
| @@ -182,7 +183,7 @@ void rt_system_scheduler_start(void) | |
| * @addtogroup Thread | |
| */ | |
| -/*@{*/ | |
| +/**@{*/ | |
| /** | |
| * This function will perform one schedule. It will select one thread | |
| @@ -411,5 +412,5 @@ rt_uint16_t rt_critical_level(void) | |
| return rt_scheduler_lock_nest; | |
| } | |
| RTM_EXPORT(rt_critical_level); | |
| -/*@}*/ | |
| +/**@}*/ | |
| diff --git a/src/slab.c b/src/slab.c | |
| index 78355d0e..d1093a7a 100644 | |
| --- a/src/slab.c | |
| +++ b/src/slab.c | |
| @@ -81,7 +81,7 @@ static void (*rt_free_hook)(void *ptr); | |
| * @addtogroup Hook | |
| */ | |
| -/*@{*/ | |
| +/**@{*/ | |
| /** | |
| * This function will set a hook function, which will be invoked when a memory | |
| @@ -107,7 +107,7 @@ void rt_free_sethook(void (*hook)(void *ptr)) | |
| } | |
| RTM_EXPORT(rt_free_sethook); | |
| -/*@}*/ | |
| +/**@}*/ | |
| #endif | |
| @@ -472,7 +472,7 @@ rt_inline int zoneindex(rt_uint32_t *bytes) | |
| * @addtogroup MM | |
| */ | |
| -/*@{*/ | |
| +/**@{*/ | |
| /** | |
| * This function will allocate a block from system heap memory. | |
| @@ -963,6 +963,6 @@ FINSH_FUNCTION_EXPORT(list_mem, list memory usage information) | |
| #endif | |
| #endif | |
| -/*@}*/ | |
| +/**@}*/ | |
| #endif | |
| diff --git a/src/thread.c b/src/thread.c | |
| index f94ad21a..e67fbe04 100644 | |
| --- a/src/thread.c | |
| +++ b/src/thread.c | |
| @@ -35,6 +35,9 @@ | |
| * thread preempted, which reported by Jiaxing Lee. | |
| * 2011-09-08 Bernard fixed the scheduling issue in rt_thread_startup. | |
| * 2012-12-29 Bernard fixed compiling warning. | |
| + * 2016-08-09 ArdaFu add thread suspend and resume hook. | |
| + * 2017-04-10 armink fixed the rt_thread_delete and rt_thread_detach | |
| + bug when thread has not startup. | |
| */ | |
| #include <rtthread.h> | |
| @@ -44,7 +47,52 @@ extern rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX]; | |
| extern struct rt_thread *rt_current_thread; | |
| extern rt_list_t rt_thread_defunct; | |
| -static void rt_thread_exit(void) | |
| +#ifdef RT_USING_HOOK | |
| + | |
| +static void (*rt_thread_suspend_hook)(rt_thread_t thread); | |
| +static void (*rt_thread_resume_hook) (rt_thread_t thread); | |
| +static void (*rt_thread_inited_hook) (rt_thread_t thread); | |
| + | |
| +/** | |
| + * @ingroup Hook | |
| + * This function sets a hook function when the system suspend a thread. | |
| + * | |
| + * @param hook the specified hook function | |
| + * | |
| + * @note the hook function must be simple and never be blocked or suspend. | |
| + */ | |
| +void rt_thread_suspend_sethook(void (*hook)(rt_thread_t thread)) | |
| +{ | |
| + rt_thread_suspend_hook = hook; | |
| +} | |
| + | |
| +/** | |
| + * @ingroup Hook | |
| + * This function sets a hook function when the system resume a thread. | |
| + * | |
| + * @param hook the specified hook function | |
| + * | |
| + * @note the hook function must be simple and never be blocked or suspend. | |
| + */ | |
| +void rt_thread_resume_sethook(void (*hook)(rt_thread_t thread)) | |
| +{ | |
| + rt_thread_resume_hook = hook; | |
| +} | |
| + | |
| +/** | |
| + * @ingroup Hook | |
| + * This function sets a hook function when a thread is initialized. | |
| + * | |
| + * @param hook the specified hook function | |
| + */ | |
| +void rt_thread_inited_sethook(void (*hook)(rt_thread_t thread)) | |
| +{ | |
| + rt_thread_inited_hook = hook; | |
| +} | |
| + | |
| +#endif | |
| + | |
| +void rt_thread_exit(void) | |
| { | |
| struct rt_thread *thread; | |
| register rt_base_t level; | |
| @@ -98,7 +146,7 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread, | |
| /* stack init */ | |
| thread->stack_addr = stack_start; | |
| - thread->stack_size = (rt_uint16_t)stack_size; | |
| + thread->stack_size = stack_size; | |
| /* init thread stack */ | |
| rt_memset(thread->stack_addr, '#', thread->stack_size); | |
| @@ -111,6 +159,12 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread, | |
| thread->init_priority = priority; | |
| thread->current_priority = priority; | |
| + thread->number_mask = 0; | |
| +#if RT_THREAD_PRIORITY_MAX > 32 | |
| + thread->number = 0; | |
| + thread->high_mask = 0; | |
| +#endif | |
| + | |
| /* tick init */ | |
| thread->init_tick = tick; | |
| thread->remaining_tick = tick; | |
| @@ -131,6 +185,8 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread, | |
| 0, | |
| RT_TIMER_FLAG_ONE_SHOT); | |
| + RT_OBJECT_HOOK_CALL(rt_thread_inited_hook,(thread)); | |
| + | |
| return RT_EOK; | |
| } | |
| @@ -138,7 +194,7 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread, | |
| * @addtogroup Thread | |
| */ | |
| -/*@{*/ | |
| +/**@{*/ | |
| /** | |
| * This function will initialize a thread, normally it's used to initialize a | |
| @@ -249,8 +305,11 @@ rt_err_t rt_thread_detach(rt_thread_t thread) | |
| /* thread check */ | |
| RT_ASSERT(thread != RT_NULL); | |
| + if (thread->stat != RT_THREAD_INIT) | |
| + { | |
| /* remove from schedule */ | |
| rt_schedule_remove_thread(thread); | |
| + } | |
| /* release thread timer */ | |
| rt_timer_detach(&(thread->thread_timer)); | |
| @@ -344,8 +403,11 @@ rt_err_t rt_thread_delete(rt_thread_t thread) | |
| /* thread check */ | |
| RT_ASSERT(thread != RT_NULL); | |
| + if (thread->stat != RT_THREAD_INIT) | |
| + { | |
| /* remove from schedule */ | |
| rt_schedule_remove_thread(thread); | |
| + } | |
| /* release thread timer */ | |
| rt_timer_detach(&(thread->thread_timer)); | |
| @@ -581,6 +643,7 @@ rt_err_t rt_thread_suspend(rt_thread_t thread) | |
| /* enable interrupt */ | |
| rt_hw_interrupt_enable(temp); | |
| + RT_OBJECT_HOOK_CALL(rt_thread_suspend_hook,(thread)); | |
| return RT_EOK; | |
| } | |
| RTM_EXPORT(rt_thread_suspend); | |
| @@ -623,6 +686,7 @@ rt_err_t rt_thread_resume(rt_thread_t thread) | |
| /* insert to schedule ready list */ | |
| rt_schedule_insert_thread(thread); | |
| + RT_OBJECT_HOOK_CALL(rt_thread_resume_hook,(thread)); | |
| return RT_EOK; | |
| } | |
| RTM_EXPORT(rt_thread_resume); | |
| @@ -704,4 +768,4 @@ rt_thread_t rt_thread_find(char *name) | |
| } | |
| RTM_EXPORT(rt_thread_find); | |
| -/*@}*/ | |
| +/**@}*/ | |
| diff --git a/src/timer.c b/src/timer.c | |
| index ba280153..e34d229b 100644 | |
| --- a/src/timer.c | |
| +++ b/src/timer.c | |
| @@ -63,7 +63,7 @@ static void (*rt_timer_timeout_hook)(struct rt_timer *timer); | |
| * @addtogroup Hook | |
| */ | |
| -/*@{*/ | |
| +/**@{*/ | |
| /** | |
| * This function will set a hook function, which will be invoked when timer | |
| @@ -76,7 +76,7 @@ void rt_timer_timeout_sethook(void (*hook)(struct rt_timer *timer)) | |
| rt_timer_timeout_hook = hook; | |
| } | |
| -/*@}*/ | |
| +/**@}*/ | |
| #endif | |
| static void _rt_timer_init(rt_timer_t timer, | |
| @@ -164,7 +164,7 @@ void rt_timer_dump(rt_list_t timer_heads[]) | |
| * @addtogroup Clock | |
| */ | |
| -/*@{*/ | |
| +/**@{*/ | |
| /** | |
| * This function will initialize a timer, normally this function is used to | |
| @@ -710,4 +710,4 @@ void rt_system_timer_thread_init(void) | |
| #endif | |
| } | |
| -/*@}*/ | |
| +/**@}*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment