rtthread.h 12.0 KB
Newer Older
1 2 3
/*
 * File      : rtthread.h
 * This file is part of RT-Thread RTOS
D
dzzxzz 已提交
4
 * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
5 6 7
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
D
dzzxzz 已提交
8
 * http://www.rt-thread.org/license/LICENSE
9 10 11 12 13 14 15 16
 *
 * Change Logs:
 * Date           Author       Notes
 * 2006-03-18     Bernard      the first version
 * 2006-04-26     Bernard      add semaphore APIs
 * 2006-08-10     Bernard      add version information
 * 2007-01-28     Bernard      rename RT_OBJECT_Class_Static to RT_Object_Class_Static
 * 2007-03-03     Bernard      clean up the definitions to rtdef.h
D
dzzxzz 已提交
17
 * 2010-04-11     yi.qiu       add module feature
18 19 20 21 22 23
 */

#ifndef __RT_THREAD_H__
#define __RT_THREAD_H__

#include <rtdef.h>
24
#include <rtdebug.h>
25
#include <rtservice.h>
26
#include <rtm.h>
27 28 29 30 31 32 33 34

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @addtogroup KernelObject
 */
D
dzzxzz 已提交
35

36
/*@{*/
D
dzzxzz 已提交
37

38 39 40 41
/*
 * kernel object interface
 */
void rt_system_object_init(void);
42
struct rt_object_information *rt_object_get_information(enum rt_object_class_type type);
D
dzzxzz 已提交
43
void rt_object_init(struct rt_object *object, enum rt_object_class_type type, const char *name);
44
void rt_object_detach(rt_object_t object);
D
dzzxzz 已提交
45
rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name);
46
void rt_object_delete(rt_object_t object);
47
rt_bool_t rt_object_is_systemobject(rt_object_t object);
D
dzzxzz 已提交
48
rt_object_t rt_object_find(const char *name, rt_uint8_t type);
49 50

#ifdef RT_USING_HOOK
D
dzzxzz 已提交
51 52 53 54 55
void rt_object_attach_sethook(void (*hook)(struct rt_object *object));
void rt_object_detach_sethook(void (*hook)(struct rt_object *object));
void rt_object_trytake_sethook(void (*hook)(struct rt_object *object));
void rt_object_take_sethook(void (*hook)(struct rt_object *object));
void rt_object_put_sethook(void (*hook)(struct rt_object *object));
56 57
#endif

58 59 60 61 62
/*@}*/

/**
 * @addtogroup Clock
 */
D
dzzxzz 已提交
63

64
/*@{*/
D
dzzxzz 已提交
65

66 67 68 69 70
/*
 * clock & timer interface
 */
void rt_system_tick_init(void);
rt_tick_t rt_tick_get(void);
71
void rt_tick_set(rt_tick_t tick);
72 73 74 75
void rt_tick_increase(void);
rt_tick_t rt_tick_from_millisecond(rt_uint32_t ms);

void rt_system_timer_init(void);
76 77
void rt_system_timer_thread_init(void);

78
void rt_timer_init(rt_timer_t timer,
D
dzzxzz 已提交
79 80
	const char *name,
	void (*timeout)(void *parameter), void *parameter,
81 82
	rt_tick_t time, rt_uint8_t flag);
rt_err_t rt_timer_detach(rt_timer_t timer);
D
dzzxzz 已提交
83 84
rt_timer_t rt_timer_create(const char *name,
	void (*timeout)(void *parameter), void *parameter,
85 86 87 88
	rt_tick_t time, rt_uint8_t flag);
rt_err_t rt_timer_delete(rt_timer_t timer);
rt_err_t rt_timer_start(rt_timer_t timer);
rt_err_t rt_timer_stop(rt_timer_t timer);
D
dzzxzz 已提交
89
rt_err_t rt_timer_control(rt_timer_t timer, rt_uint8_t cmd, void *arg);
90

91 92 93
rt_tick_t rt_timer_next_timeout_tick(void);
void rt_timer_check(void);

94
#ifdef RT_USING_HOOK
D
dzzxzz 已提交
95
void rt_timer_timeout_sethook(void (*hook)(struct rt_timer *timer));
96
#endif
D
dzzxzz 已提交
97

98 99 100 101 102
/*@}*/

/**
 * @addtogroup Thread
 */
D
dzzxzz 已提交
103

104
/*@{*/
D
dzzxzz 已提交
105

106 107 108
/*
 * thread interface
 */
D
dzzxzz 已提交
109 110 111 112
rt_err_t rt_thread_init(struct rt_thread *thread,
	const char *name,
	void (*entry)(void *parameter), void *parameter,
	void *stack_start, rt_uint32_t stack_size,
113
	rt_uint8_t priority, rt_uint32_t tick);
qiuyiuestc's avatar
qiuyiuestc 已提交
114
rt_err_t rt_thread_detach(rt_thread_t thread);
D
dzzxzz 已提交
115 116
rt_thread_t rt_thread_create(const char *name,
	void (*entry)(void *parameter), void *parameter,
117 118
	rt_uint32_t stack_size,
	rt_uint8_t priority, rt_uint32_t tick);
qiuyiuestc's avatar
qiuyiuestc 已提交
119
rt_thread_t rt_thread_self(void);
D
dzzxzz 已提交
120
rt_thread_t rt_thread_find(char *name);
qiuyiuestc's avatar
qiuyiuestc 已提交
121 122 123 124 125
rt_err_t rt_thread_startup(rt_thread_t thread);
rt_err_t rt_thread_delete(rt_thread_t thread);

rt_err_t rt_thread_yield(void);
rt_err_t rt_thread_delay(rt_tick_t tick);
D
dzzxzz 已提交
126
rt_err_t rt_thread_control(rt_thread_t thread, rt_uint8_t cmd, void *arg);
qiuyiuestc's avatar
qiuyiuestc 已提交
127 128
rt_err_t rt_thread_suspend(rt_thread_t thread);
rt_err_t rt_thread_resume(rt_thread_t thread);
D
dzzxzz 已提交
129
void rt_thread_timeout(void *parameter);
130 131 132 133 134 135 136 137

/*
 * idle thread interface
 */
void rt_thread_idle_init(void);
#ifdef RT_USING_HOOK
void rt_thread_idle_sethook(void (*hook)(void));
#endif
B
bernard.xiong 已提交
138
void rt_thread_idle_excute(void);
139 140 141 142 143 144 145 146

/*
 * schedule service
 */
void rt_system_scheduler_init(void);
void rt_system_scheduler_start(void);

void rt_schedule(void);
D
dzzxzz 已提交
147 148
void rt_schedule_insert_thread(struct rt_thread *thread);
void rt_schedule_remove_thread(struct rt_thread *thread);
149 150 151 152 153

void rt_enter_critical(void);
void rt_exit_critical(void);

#ifdef RT_USING_HOOK
154
void rt_scheduler_sethook(void (*hook)(rt_thread_t from, rt_thread_t to));
155
#endif
D
dzzxzz 已提交
156

157 158 159 160 161
/*@}*/

/**
 * @addtogroup MM
 */
D
dzzxzz 已提交
162

163
/*@{*/
D
dzzxzz 已提交
164

165 166 167 168 169 170 171
/*
 * memory management interface
 */
#ifdef RT_USING_MEMPOOL
/*
 * memory pool interface
 */
D
dzzxzz 已提交
172 173 174
rt_err_t rt_mp_init(struct rt_mempool *mp, const char *name, void *start, rt_size_t size, rt_size_t block_size);
rt_err_t rt_mp_detach(struct rt_mempool *mp);
rt_mp_t rt_mp_create(const char *name, rt_size_t block_count, rt_size_t block_size);
175 176
rt_err_t rt_mp_delete(rt_mp_t mp);

D
dzzxzz 已提交
177 178
void *rt_mp_alloc(rt_mp_t mp, rt_int32_t time);
void rt_mp_free(void *block);
179 180

#ifdef RT_USING_HOOK
D
dzzxzz 已提交
181 182
void rt_mp_alloc_sethook(void (*hook)(struct rt_mempool *mp, void *block));
void rt_mp_free_sethook(void (*hook)(struct rt_mempool *mp, void *block));
183
#endif
D
dzzxzz 已提交
184

185 186 187 188 189 190
#endif

#ifdef RT_USING_HEAP
/*
 * heap memory interface
 */
D
dzzxzz 已提交
191
void rt_system_heap_init(void *begin_addr, void *end_addr);
192

D
dzzxzz 已提交
193 194 195
void *rt_malloc(rt_size_t nbytes);
void rt_free(void *ptr);
void *rt_realloc(void *ptr, rt_size_t nbytes);
196
void *rt_calloc(rt_size_t count, rt_size_t size);
D
dzzxzz 已提交
197 198
void *rt_malloc_align(rt_size_t size, rt_size_t align);
void rt_free_align(void *ptr);
199

D
dzzxzz 已提交
200
void rt_memory_info(rt_uint32_t *total, rt_uint32_t *used, rt_uint32_t *max_used);
201

qiuyiuestc's avatar
qiuyiuestc 已提交
202 203 204 205 206
#ifdef RT_USING_SLAB
void *rt_page_alloc(rt_size_t npages);
void rt_page_free(void *addr, rt_size_t npages);
#endif

207 208 209 210
#ifdef RT_USING_HOOK
void rt_malloc_sethook(void (*hook)(void *ptr, rt_uint32_t size));
void rt_free_sethook(void (*hook)(void *ptr));
#endif
qiuyiuestc's avatar
qiuyiuestc 已提交
211

212
#endif
D
dzzxzz 已提交
213

214 215 216 217 218 219 220 221 222 223 224 225
#ifdef RT_USING_MEMHEAP
/**
 * memory heap object interface
 */
rt_err_t rt_memheap_init(struct rt_memheap* memheap, const char* name,
	void *start_addr, rt_uint32_t size);
rt_err_t rt_memheap_detach(struct rt_memheap* heap);
void* rt_memheap_alloc(struct rt_memheap *heap, 	rt_uint32_t size);
void rt_memheap_free(void* ptr);

#endif

226 227 228 229 230
/*@}*/

/**
 * @addtogroup IPC
 */
D
dzzxzz 已提交
231

232
/*@{*/
D
dzzxzz 已提交
233

234 235 236 237
#ifdef RT_USING_SEMAPHORE
/*
 * semaphore interface
 */
D
dzzxzz 已提交
238 239 240 241
rt_err_t rt_sem_init(rt_sem_t sem, const char *name, rt_uint32_t value, rt_uint8_t flag);
rt_err_t rt_sem_detach(rt_sem_t sem);
rt_sem_t rt_sem_create(const char *name, rt_uint32_t value, rt_uint8_t flag);
rt_err_t rt_sem_delete(rt_sem_t sem);
242

D
dzzxzz 已提交
243
rt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t time);
244 245
rt_err_t rt_sem_trytake(rt_sem_t sem);
rt_err_t rt_sem_release(rt_sem_t sem);
D
dzzxzz 已提交
246
rt_err_t rt_sem_control(rt_sem_t sem, rt_uint8_t cmd, void *arg);
247 248 249 250 251 252
#endif

#ifdef RT_USING_MUTEX
/*
 * mutex interface
 */
D
dzzxzz 已提交
253 254 255 256
rt_err_t rt_mutex_init(rt_mutex_t mutex, const char *name, rt_uint8_t flag);
rt_err_t rt_mutex_detach(rt_mutex_t mutex);
rt_mutex_t rt_mutex_create(const char *name, rt_uint8_t flag);
rt_err_t rt_mutex_delete(rt_mutex_t mutex);
257

D
dzzxzz 已提交
258
rt_err_t rt_mutex_take(rt_mutex_t mutex, rt_int32_t time);
259
rt_err_t rt_mutex_release(rt_mutex_t mutex);
D
dzzxzz 已提交
260
rt_err_t rt_mutex_control(rt_mutex_t mutex, rt_uint8_t cmd, void *arg);
261 262 263 264 265 266
#endif

#ifdef RT_USING_EVENT
/*
 * event interface
 */
D
dzzxzz 已提交
267
rt_err_t rt_event_init(rt_event_t event, const char *name, rt_uint8_t flag);
268
rt_err_t rt_event_detach(rt_event_t event);
D
dzzxzz 已提交
269 270
rt_event_t rt_event_create(const char *name, rt_uint8_t flag);
rt_err_t rt_event_delete(rt_event_t event);
271 272

rt_err_t rt_event_send(rt_event_t event, rt_uint32_t set);
D
dzzxzz 已提交
273 274
rt_err_t rt_event_recv(rt_event_t event, rt_uint32_t set, rt_uint8_t opt, rt_int32_t timeout, rt_uint32_t *recved);
rt_err_t rt_event_control(rt_event_t event, rt_uint8_t cmd, void *arg);
275 276 277 278 279 280
#endif

#ifdef RT_USING_MAILBOX
/*
 * mailbox interface
 */
D
dzzxzz 已提交
281
rt_err_t rt_mb_init(rt_mailbox_t mb, const char *name, void *msgpool, rt_size_t size, rt_uint8_t flag);
282
rt_err_t rt_mb_detach(rt_mailbox_t mb);
D
dzzxzz 已提交
283 284
rt_mailbox_t rt_mb_create(const char *name, rt_size_t size, rt_uint8_t flag);
rt_err_t rt_mb_delete(rt_mailbox_t mb);
285

D
dzzxzz 已提交
286 287 288 289
rt_err_t rt_mb_send(rt_mailbox_t mb, rt_uint32_t value);
rt_err_t rt_mb_send_wait(rt_mailbox_t mb, rt_uint32_t value, rt_int32_t timeout);
rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_uint32_t *value, rt_int32_t timeout);
rt_err_t rt_mb_control(rt_mailbox_t mb, rt_uint8_t cmd, void *arg);
290 291 292 293 294 295
#endif

#ifdef RT_USING_MESSAGEQUEUE
/*
 * message queue interface
 */
D
dzzxzz 已提交
296
rt_err_t rt_mq_init(rt_mq_t mq, const char *name, void *msgpool, rt_size_t msg_size, rt_size_t pool_size, rt_uint8_t flag);
297
rt_err_t rt_mq_detach(rt_mq_t mq);
D
dzzxzz 已提交
298 299
rt_mq_t rt_mq_create(const char *name, rt_size_t msg_size, rt_size_t max_msgs, rt_uint8_t flag);
rt_err_t rt_mq_delete(rt_mq_t mq);
300

D
dzzxzz 已提交
301 302 303 304
rt_err_t rt_mq_send(rt_mq_t mq, void *buffer, rt_size_t size);
rt_err_t rt_mq_urgent(rt_mq_t mq, void *buffer, rt_size_t size);
rt_err_t rt_mq_recv(rt_mq_t mq, void *buffer, rt_size_t size, rt_int32_t timeout);
rt_err_t rt_mq_control(rt_mq_t mq, rt_uint8_t cmd, void *arg);
305
#endif
D
dzzxzz 已提交
306

307 308 309 310 311 312
/*@}*/

#ifdef RT_USING_DEVICE
/**
 * @addtogroup Device
 */
D
dzzxzz 已提交
313

314
/*@{*/
D
dzzxzz 已提交
315

316 317 318
/*
 * device (I/O) system interface
 */
D
dzzxzz 已提交
319
rt_device_t rt_device_find(const char *name);
320

D
dzzxzz 已提交
321
rt_err_t rt_device_register(rt_device_t dev, const char *name, rt_uint16_t flags);
322 323 324 325 326 327
rt_err_t rt_device_unregister(rt_device_t dev);
rt_err_t rt_device_init_all(void);

rt_err_t rt_device_set_rx_indicate(rt_device_t dev, rt_err_t (*rx_ind )(rt_device_t dev, rt_size_t size));
rt_err_t rt_device_set_tx_complete(rt_device_t dev, rt_err_t (*tx_done)(rt_device_t dev, void *buffer));

B
bernard.xiong 已提交
328
rt_err_t  rt_device_init (rt_device_t dev);
329 330
rt_err_t  rt_device_open (rt_device_t dev, rt_uint16_t oflag);
rt_err_t  rt_device_close(rt_device_t dev);
D
dzzxzz 已提交
331 332 333
rt_size_t rt_device_read (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
rt_size_t rt_device_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
rt_err_t  rt_device_control(rt_device_t dev, rt_uint8_t cmd, void *arg);
D
dzzxzz 已提交
334

335 336 337
/*@}*/
#endif

qiuyiuestc's avatar
qiuyiuestc 已提交
338 339 340 341
#ifdef RT_USING_MODULE
/**
 * @addtogroup Module
 */
D
dzzxzz 已提交
342

qiuyiuestc's avatar
qiuyiuestc 已提交
343
/*@{*/
D
dzzxzz 已提交
344

qiuyiuestc's avatar
qiuyiuestc 已提交
345 346 347
/*
 * module interface
 */
D
dzzxzz 已提交
348
rt_module_t rt_module_load(const char *name, void *module_ptr);
qiuyiuestc's avatar
qiuyiuestc 已提交
349
rt_err_t rt_module_unload(rt_module_t module);
D
dzzxzz 已提交
350
rt_module_t rt_module_open(const char *filename);
qiuyiuestc's avatar
qiuyiuestc 已提交
351 352 353
void *rt_module_malloc(rt_size_t size);
void *rt_module_realloc(void *ptr, rt_size_t size);
void rt_module_free(rt_module_t module, void *addr);
D
dzzxzz 已提交
354 355 356
rt_module_t rt_module_self(void);
rt_err_t rt_module_set(rt_module_t module);
rt_module_t rt_module_find(const char *name);
qiuyiuestc's avatar
qiuyiuestc 已提交
357 358 359 360 361 362

#ifdef RT_USING_HOOK
void rt_module_load_sethook(void (*hook)(rt_module_t module));
void rt_module_unload_sethook(void (*hook)(rt_module_t module));
#endif

B
bernard.xiong@gmail.com 已提交
363
/*@}*/
D
dzzxzz 已提交
364
#endif
qiuyiuestc's avatar
qiuyiuestc 已提交
365
 
366 367 368 369 370 371 372 373 374 375
/*
 * interrupt service
 */
typedef void (*rt_isr_handler_t)(int vector);

/*
 * rt_interrupt_enter and rt_interrupt_leave only can be called by BSP
 */
void rt_interrupt_enter(void);
void rt_interrupt_leave(void);
D
dzzxzz 已提交
376

377 378 379 380
/*
 * the number of nested interrupts.
 */
rt_uint8_t rt_interrupt_get_nest(void);
381

382 383 384 385 386
/**
 * application module
 */
void rt_system_module_init(void);

387 388 389
/**
 * @addtogroup KernelService
 */
D
dzzxzz 已提交
390

391
/*@{*/
D
dzzxzz 已提交
392

393 394 395
/*
 * general kernel service
 */
D
dzzxzz 已提交
396
void rt_kprintf(const char *fmt, ...);
397
rt_int32_t rt_vsprintf(char *dest, const char *format, va_list arg_ptr);
D
dzzxzz 已提交
398
rt_int32_t rt_sprintf(char *buf ,const char *format, ...);
qiuyiuestc's avatar
qiuyiuestc 已提交
399 400
rt_int32_t rt_snprintf(char *buf, rt_size_t size, const char *format, ...);

401
#ifdef RT_USING_DEVICE
D
dzzxzz 已提交
402
rt_device_t rt_console_set_device(const char *name);
403
#endif
404 405 406

rt_err_t rt_get_errno(void);
void rt_set_errno(rt_err_t no);
407 408 409 410 411 412
int *_rt_errno(void);
#ifndef RT_USING_NEWLIB
#ifndef errno
#define errno	*_rt_errno()
#endif
#endif
413

D
dzzxzz 已提交
414 415
void *rt_memset(void *src, int c, rt_ubase_t n);
void *rt_memcpy(void *dest, const void *src, rt_ubase_t n);
416

D
dzzxzz 已提交
417
rt_ubase_t rt_strncmp(const char *cs, const char *ct, rt_ubase_t count);
qiuyiuestc's avatar
qiuyiuestc 已提交
418
rt_ubase_t rt_strcmp (const char *cs, const char *ct);
419 420 421
rt_ubase_t rt_strlen (const char *src);
char *rt_strdup(const char *s);

D
dzzxzz 已提交
422 423
char *rt_strstr(const char *str1, const char *str2);
rt_int32_t rt_sscanf(const char *buf, const char *fmt, ...);
424
char *rt_strncpy(char *dest, const char *src, rt_ubase_t n);
D
dzzxzz 已提交
425 426
void *rt_memmove(void *dest, const void *src, rt_ubase_t n);
rt_int32_t rt_memcmp(const void *cs, const void *ct, rt_ubase_t count);
427 428 429 430 431 432 433 434 435
rt_uint32_t rt_strcasecmp(const char *a, const char *b);

void rt_show_version(void);

/*@}*/

#ifdef __cplusplus
}
#endif
D
dzzxzz 已提交
436

437
#endif