From 4660882320f8ac3499f74c30dcbaf8f2b40c202f Mon Sep 17 00:00:00 2001 From: Bernard Xiong Date: Sat, 13 Oct 2018 09:36:23 +0800 Subject: [Kernel] Change number to v4.0.0 --- include/rtdef.h | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'include/rtdef.h') diff --git a/include/rtdef.h b/include/rtdef.h index 92b997151d..99caa1c7d5 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -2,10 +2,6 @@ * Copyright (c) 2006-2018, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 - */ - -/* - * File : rtdef.h * * Change Logs: * Date Author Notes @@ -28,6 +24,7 @@ * 2018-05-31 Bernard change version number to v3.1.0 * 2018-09-04 Bernard change version number to v3.1.1 * 2018-09-14 Bernard apply Apache License v2.0 to RT-Thread Kernel + * 2018-10-13 Bernard change version number to v4.0.0 */ #ifndef __RT_DEF_H__ @@ -47,9 +44,9 @@ extern "C" { /*@{*/ /* RT-Thread version information */ -#define RT_VERSION 3L /**< major version number */ -#define RT_SUBVERSION 1L /**< minor version number */ -#define RT_REVISION 1L /**< revise version number */ +#define RT_VERSION 4L /**< major version number */ +#define RT_SUBVERSION 0L /**< minor version number */ +#define RT_REVISION 0L /**< revise version number */ /* RT-Thread version */ #define RTTHREAD_VERSION ((RT_VERSION * 10000) + \ -- cgit v1.3.1 From 484afe9d2cb07c9e552ff905328eecb7170e8c8a Mon Sep 17 00:00:00 2001 From: Bernard Xiong Date: Fri, 26 Oct 2018 06:35:42 +0800 Subject: [Kernel] Add 64bit CPU support. --- components/finsh/cmd.c | 4 ++-- include/rtdef.h | 18 +++++++++++++----- include/rthw.h | 10 +++++----- include/rtthread.h | 12 ++++++------ libcpu/Kconfig | 15 +++++++++++++++ src/ipc.c | 13 +++++++------ src/kservice.c | 23 ++++++++++++++--------- src/mem.c | 43 ++++++++++++++++++++++++++++--------------- src/memheap.c | 4 ++-- src/scheduler.c | 24 ++++++++++++------------ src/thread.c | 2 +- 11 files changed, 105 insertions(+), 63 deletions(-) (limited to 'include/rtdef.h') diff --git a/components/finsh/cmd.c b/components/finsh/cmd.c index 0e04b794e9..7f12671429 100644 --- a/components/finsh/cmd.c +++ b/components/finsh/cmd.c @@ -102,9 +102,9 @@ static long _list_thread(struct rt_list_node *list) while (*ptr == '#')ptr ++; rt_kprintf(" 0x%08x 0x%08x %02d%% 0x%08x %03d\n", - thread->stack_size + ((rt_uint32_t)thread->stack_addr - (rt_uint32_t)thread->sp), + thread->stack_size + ((rt_ubase_t)thread->stack_addr - (rt_ubase_t)thread->sp), thread->stack_size, - (thread->stack_size - ((rt_uint32_t) ptr - (rt_uint32_t) thread->stack_addr)) * 100 + (thread->stack_size - ((rt_ubase_t) ptr - (rt_ubase_t) thread->stack_addr)) * 100 / thread->stack_size, thread->remaining_tick, thread->error); diff --git a/include/rtdef.h b/include/rtdef.h index 99caa1c7d5..fef08599c7 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -25,6 +25,7 @@ * 2018-09-04 Bernard change version number to v3.1.1 * 2018-09-14 Bernard apply Apache License v2.0 to RT-Thread Kernel * 2018-10-13 Bernard change version number to v4.0.0 + * 2018-10-02 Bernard add 64bit arch support */ #ifndef __RT_DEF_H__ @@ -55,13 +56,20 @@ extern "C" { /* RT-Thread basic data type definitions */ typedef signed char rt_int8_t; /**< 8bit integer type */ typedef signed short rt_int16_t; /**< 16bit integer type */ -typedef signed long rt_int32_t; /**< 32bit integer type */ +typedef signed int rt_int32_t; /**< 32bit integer type */ typedef unsigned char rt_uint8_t; /**< 8bit unsigned integer type */ typedef unsigned short rt_uint16_t; /**< 16bit unsigned integer type */ -typedef unsigned long rt_uint32_t; /**< 32bit unsigned integer type */ -typedef int rt_bool_t; /**< boolean type */ +typedef unsigned int rt_uint32_t; /**< 32bit unsigned integer type */ -/* 32bit CPU */ +#ifdef ARCH_CPU_64BIT +typedef signed long rt_int64_t; /**< 64bit integer type */ +typedef unsigned long rt_uint64_t; /**< 64bit unsigned integer type */ +#else +typedef signed long long rt_int64_t; /**< 64bit integer type */ +typedef unsigned long long rt_uint64_t; /**< 64bit unsigned integer type */ +#endif + +typedef int rt_bool_t; /**< boolean type */ typedef long rt_base_t; /**< Nbit CPU related date type */ typedef unsigned long rt_ubase_t; /**< Nbit unsigned CPU related data type */ @@ -648,7 +656,7 @@ struct rt_mailbox { struct rt_ipc_object parent; /**< inherit from ipc_object */ - rt_uint32_t *msg_pool; /**< start address of message buffer */ + rt_ubase_t *msg_pool; /**< start address of message buffer */ rt_uint16_t size; /**< size of message pool */ diff --git a/include/rthw.h b/include/rthw.h index c6af392e62..17151de99a 100644 --- a/include/rthw.h +++ b/include/rthw.h @@ -98,14 +98,14 @@ void rt_hw_interrupt_enable(rt_base_t level); /* * Context interfaces */ -void rt_hw_context_switch(rt_uint32_t from, rt_uint32_t to); -void rt_hw_context_switch_to(rt_uint32_t to); -void rt_hw_context_switch_interrupt(rt_uint32_t from, rt_uint32_t to); +void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to); +void rt_hw_context_switch_to(rt_ubase_t to); +void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to); void rt_hw_console_output(const char *str); -void rt_hw_backtrace(rt_uint32_t *fp, rt_uint32_t thread_entry); -void rt_hw_show_memory(rt_uint32_t addr, rt_uint32_t size); +void rt_hw_backtrace(rt_uint32_t *fp, rt_ubase_t thread_entry); +void rt_hw_show_memory(rt_uint32_t addr, rt_size_t size); /* * Exception interfaces diff --git a/include/rtthread.h b/include/rtthread.h index fc5029a220..814fa454d8 100644 --- a/include/rtthread.h +++ b/include/rtthread.h @@ -256,7 +256,7 @@ void rt_page_free(void *addr, rt_size_t npages); #endif #ifdef RT_USING_HOOK -void rt_malloc_sethook(void (*hook)(void *ptr, rt_uint32_t size)); +void rt_malloc_sethook(void (*hook)(void *ptr, rt_size_t size)); void rt_free_sethook(void (*hook)(void *ptr)); #endif @@ -269,9 +269,9 @@ void rt_free_sethook(void (*hook)(void *ptr)); rt_err_t rt_memheap_init(struct rt_memheap *memheap, const char *name, void *start_addr, - rt_uint32_t size); + rt_size_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_alloc(struct rt_memheap *heap, rt_size_t size); void *rt_memheap_realloc(struct rt_memheap *heap, void *ptr, rt_size_t newsize); void rt_memheap_free(void *ptr); #endif @@ -347,11 +347,11 @@ rt_err_t rt_mb_detach(rt_mailbox_t mb); 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); -rt_err_t rt_mb_send(rt_mailbox_t mb, rt_uint32_t value); +rt_err_t rt_mb_send(rt_mailbox_t mb, rt_ubase_t value); rt_err_t rt_mb_send_wait(rt_mailbox_t mb, - rt_uint32_t value, + rt_ubase_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_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeout); rt_err_t rt_mb_control(rt_mailbox_t mb, int cmd, void *arg); #endif diff --git a/libcpu/Kconfig b/libcpu/Kconfig index 0a280d4b5c..69d9220da1 100644 --- a/libcpu/Kconfig +++ b/libcpu/Kconfig @@ -1,3 +1,6 @@ +config ARCH_CPU_64BIT + bool + config ARCH_ARM bool @@ -83,6 +86,18 @@ config ARCH_POWERPC config ARCH_RISCV bool +config ARCH_RISCV_FPU + bool + +config ARCH_RISCV32 + select ARCH_RISCV + bool + +config ARCH_RISCV64 + select ARCH_RISCV + select ARCH_CPU_64BIT + bool + config ARCH_IA32 bool diff --git a/src/ipc.c b/src/ipc.c index 56045911a8..7f603cfc4d 100644 --- a/src/ipc.c +++ b/src/ipc.c @@ -32,6 +32,7 @@ * 2010-11-10 Bernard add IPC reset command implementation. * 2011-12-18 Bernard add more parameter checking in message queue * 2013-09-14 Grissiom add an option check in rt_event_recv + * 2018-10-02 Bernard add 64bit support for mailbox */ #include @@ -486,10 +487,10 @@ rt_err_t rt_sem_control(rt_sem_t sem, int cmd, void *arg) if (cmd == RT_IPC_CMD_RESET) { - rt_uint32_t value; + rt_ubase_t value; /* get value */ - value = (rt_uint32_t)arg; + value = (rt_ubase_t)arg; /* disable interrupt */ level = rt_hw_interrupt_disable(); @@ -1373,7 +1374,7 @@ rt_mailbox_t rt_mb_create(const char *name, rt_size_t size, rt_uint8_t flag) /* init mailbox */ mb->size = size; - mb->msg_pool = RT_KERNEL_MALLOC(mb->size * sizeof(rt_uint32_t)); + mb->msg_pool = RT_KERNEL_MALLOC(mb->size * sizeof(rt_ubase_t)); if (mb->msg_pool == RT_NULL) { /* delete mailbox object */ @@ -1436,7 +1437,7 @@ RTM_EXPORT(rt_mb_delete); * @return the error code */ rt_err_t rt_mb_send_wait(rt_mailbox_t mb, - rt_uint32_t value, + rt_ubase_t value, rt_int32_t timeout) { struct rt_thread *thread; @@ -1567,7 +1568,7 @@ RTM_EXPORT(rt_mb_send_wait); * * @return the error code */ -rt_err_t rt_mb_send(rt_mailbox_t mb, rt_uint32_t value) +rt_err_t rt_mb_send(rt_mailbox_t mb, rt_ubase_t value) { return rt_mb_send_wait(mb, value, 0); } @@ -1583,7 +1584,7 @@ RTM_EXPORT(rt_mb_send); * * @return the error code */ -rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_uint32_t *value, rt_int32_t timeout) +rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeout) { struct rt_thread *thread; register rt_ubase_t temp; diff --git a/src/kservice.c b/src/kservice.c index dbc4a2fb0e..95c9308182 100644 --- a/src/kservice.c +++ b/src/kservice.c @@ -1189,31 +1189,36 @@ RTM_EXPORT(rt_kprintf); */ void *rt_malloc_align(rt_size_t size, rt_size_t align) { - void *align_ptr; void *ptr; + void *align_ptr; + int uintptr_size; rt_size_t align_size; - /* align the alignment size to 4 byte */ - align = ((align + 0x03) & ~0x03); + /* sizeof pointer */ + uintptr_size = sizeof(void*); + uintptr_size -= 1; + + /* align the alignment size to uintptr size byte */ + align = ((align + uintptr_size) & ~uintptr_size); /* get total aligned size */ - align_size = ((size + 0x03) & ~0x03) + align; + align_size = ((size + uintptr_size) & ~uintptr_size) + align; /* allocate memory block from heap */ ptr = rt_malloc(align_size); if (ptr != RT_NULL) { /* the allocated memory block is aligned */ - if (((rt_uint32_t)ptr & (align - 1)) == 0) + if (((rt_ubase_t)ptr & (align - 1)) == 0) { - align_ptr = (void *)((rt_uint32_t)ptr + align); + align_ptr = (void *)((rt_ubase_t)ptr + align); } else { - align_ptr = (void *)(((rt_uint32_t)ptr + (align - 1)) & ~(align - 1)); + align_ptr = (void *)(((rt_ubase_t)ptr + (align - 1)) & ~(align - 1)); } /* set the pointer before alignment pointer to the real pointer */ - *((rt_uint32_t *)((rt_uint32_t)align_ptr - sizeof(void *))) = (rt_uint32_t)ptr; + *((rt_ubase_t *)((rt_ubase_t)align_ptr - sizeof(void *))) = (rt_ubase_t)ptr; ptr = align_ptr; } @@ -1232,7 +1237,7 @@ void rt_free_align(void *ptr) { void *real_ptr; - real_ptr = (void *) * (rt_uint32_t *)((rt_uint32_t)ptr - sizeof(void *)); + real_ptr = (void *) * (rt_ubase_t *)((rt_ubase_t)ptr - sizeof(void *)); rt_free(real_ptr); } RTM_EXPORT(rt_free_align); diff --git a/src/mem.c b/src/mem.c index 2fefb48b67..ca5329404e 100644 --- a/src/mem.c +++ b/src/mem.c @@ -11,6 +11,7 @@ * 2010-07-13 Bernard fix RT_ALIGN issue found by kuronca * 2010-10-14 Bernard fix rt_realloc issue when realloc a NULL pointer. * 2017-07-14 armink fix rt_realloc issue when new size is 0 + * 2018-10-02 Bernard Add 64bit support */ /* @@ -97,12 +98,19 @@ struct heap_mem /* magic and used flag */ rt_uint16_t magic; rt_uint16_t used; +#ifdef ARCH_CPU_64BIT + rt_uint32_t resv; +#endif rt_size_t next, prev; #ifdef RT_USING_MEMTRACE +#ifdef ARCH_CPU_64BIT + rt_uint8_t thread[8]; +#else rt_uint8_t thread[4]; /* thread name */ #endif +#endif }; /** pointer to the heap: for alignment, heap_ptr is now a pointer instead of an array */ @@ -111,7 +119,12 @@ static rt_uint8_t *heap_ptr; /** the last entry, always unused! */ static struct heap_mem *heap_end; +#ifdef ARCH_CPU_64BIT +#define MIN_SIZE 24 +#else #define MIN_SIZE 12 +#endif + #define MIN_SIZE_ALIGNED RT_ALIGN(MIN_SIZE, RT_ALIGN_SIZE) #define SIZEOF_STRUCT_MEM RT_ALIGN(sizeof(struct heap_mem), RT_ALIGN_SIZE) @@ -191,8 +204,8 @@ static void plug_holes(struct heap_mem *mem) void rt_system_heap_init(void *begin_addr, void *end_addr) { struct heap_mem *mem; - rt_uint32_t begin_align = RT_ALIGN((rt_uint32_t)begin_addr, RT_ALIGN_SIZE); - rt_uint32_t end_align = RT_ALIGN_DOWN((rt_uint32_t)end_addr, RT_ALIGN_SIZE); + rt_ubase_t begin_align = RT_ALIGN((rt_ubase_t)begin_addr, RT_ALIGN_SIZE); + rt_ubase_t end_align = RT_ALIGN_DOWN((rt_ubase_t)end_addr, RT_ALIGN_SIZE); RT_DEBUG_NOT_IN_INTERRUPT; @@ -206,7 +219,7 @@ void rt_system_heap_init(void *begin_addr, void *end_addr) else { rt_kprintf("mem init, error begin address 0x%x, and end address 0x%x\n", - (rt_uint32_t)begin_addr, (rt_uint32_t)end_addr); + (rt_ubase_t)begin_addr, (rt_ubase_t)end_addr); return; } @@ -215,7 +228,7 @@ void rt_system_heap_init(void *begin_addr, void *end_addr) heap_ptr = (rt_uint8_t *)begin_align; RT_DEBUG_LOG(RT_DEBUG_MEM, ("mem init, heap begin address 0x%x, size %d\n", - (rt_uint32_t)heap_ptr, mem_size_aligned)); + (rt_ubase_t)heap_ptr, mem_size_aligned)); /* initialize the start of the heap */ mem = (struct heap_mem *)heap_ptr; @@ -374,14 +387,14 @@ void *rt_malloc(rt_size_t size) } rt_sem_release(&heap_sem); - RT_ASSERT((rt_uint32_t)mem + SIZEOF_STRUCT_MEM + size <= (rt_uint32_t)heap_end); - RT_ASSERT((rt_uint32_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM) % RT_ALIGN_SIZE == 0); - RT_ASSERT((((rt_uint32_t)mem) & (RT_ALIGN_SIZE - 1)) == 0); + RT_ASSERT((rt_ubase_t)mem + SIZEOF_STRUCT_MEM + size <= (rt_ubase_t)heap_end); + RT_ASSERT((rt_ubase_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM) % RT_ALIGN_SIZE == 0); + RT_ASSERT((((rt_ubase_t)mem) & (RT_ALIGN_SIZE - 1)) == 0); RT_DEBUG_LOG(RT_DEBUG_MEM, ("allocate memory at 0x%x, size: %d\n", - (rt_uint32_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM), - (rt_uint32_t)(mem->next - ((rt_uint8_t *)mem - heap_ptr)))); + (rt_ubase_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM), + (rt_ubase_t)(mem->next - ((rt_uint8_t *)mem - heap_ptr)))); RT_OBJECT_HOOK_CALL(rt_malloc_hook, (((void *)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM)), size)); @@ -539,7 +552,7 @@ void rt_free(void *rmem) RT_DEBUG_NOT_IN_INTERRUPT; - RT_ASSERT((((rt_uint32_t)rmem) & (RT_ALIGN_SIZE - 1)) == 0); + RT_ASSERT((((rt_ubase_t)rmem) & (RT_ALIGN_SIZE - 1)) == 0); RT_ASSERT((rt_uint8_t *)rmem >= (rt_uint8_t *)heap_ptr && (rt_uint8_t *)rmem < (rt_uint8_t *)heap_end); @@ -558,8 +571,8 @@ void rt_free(void *rmem) RT_DEBUG_LOG(RT_DEBUG_MEM, ("release memory 0x%x, size: %d\n", - (rt_uint32_t)rmem, - (rt_uint32_t)(mem->next - ((rt_uint8_t *)mem - heap_ptr)))); + (rt_ubase_t)rmem, + (rt_ubase_t)(mem->next - ((rt_uint8_t *)mem - heap_ptr)))); /* protect the heap from concurrent access */ @@ -624,12 +637,12 @@ FINSH_FUNCTION_EXPORT(list_mem, list memory usage information) int memcheck(void) { int position; - rt_uint32_t level; + rt_ubase_t level; struct heap_mem *mem; level = rt_hw_interrupt_disable(); for (mem = (struct heap_mem *)heap_ptr; mem != heap_end; mem = (struct heap_mem *)&heap_ptr[mem->next]) { - position = (rt_uint32_t)mem - (rt_uint32_t)heap_ptr; + position = (rt_ubase_t)mem - (rt_ubase_t)heap_ptr; if (position < 0) goto __exit; if (position > mem_size_aligned) goto __exit; if (mem->magic != HEAP_MAGIC) goto __exit; @@ -664,7 +677,7 @@ int memtrace(int argc, char **argv) rt_kprintf("\n--memory item information --\n"); for (mem = (struct heap_mem *)heap_ptr; mem != heap_end; mem = (struct heap_mem *)&heap_ptr[mem->next]) { - int position = (rt_uint32_t)mem - (rt_uint32_t)heap_ptr; + int position = (rt_ubase_t)mem - (rt_ubase_t)heap_ptr; int size; rt_kprintf("[0x%08x - ", mem); diff --git a/src/memheap.c b/src/memheap.c index 946f163b64..eb5d710029 100644 --- a/src/memheap.c +++ b/src/memheap.c @@ -50,7 +50,7 @@ rt_err_t rt_memheap_init(struct rt_memheap *memheap, const char *name, void *start_addr, - rt_uint32_t size) + rt_size_t size) { struct rt_memheap_item *item; @@ -135,7 +135,7 @@ rt_err_t rt_memheap_detach(struct rt_memheap *heap) } RTM_EXPORT(rt_memheap_detach); -void *rt_memheap_alloc(struct rt_memheap *heap, rt_uint32_t size) +void *rt_memheap_alloc(struct rt_memheap *heap, rt_size_t size) { rt_err_t result; rt_uint32_t free_size; diff --git a/src/scheduler.c b/src/scheduler.c index 95b5d487df..446b153e1b 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -75,11 +75,11 @@ static void _rt_scheduler_stack_check(struct rt_thread *thread) RT_ASSERT(thread != RT_NULL); 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) + (rt_ubase_t)thread->sp <= (rt_ubase_t)thread->stack_addr || + (rt_ubase_t)thread->sp > + (rt_ubase_t)thread->stack_addr + (rt_ubase_t)thread->stack_size) { - rt_uint32_t level; + rt_ubase_t level; rt_kprintf("thread:%s stack overflow\n", thread->name); #ifdef RT_USING_FINSH @@ -91,7 +91,7 @@ static void _rt_scheduler_stack_check(struct rt_thread *thread) level = rt_hw_interrupt_disable(); while (level); } - else if ((rt_uint32_t)thread->sp <= ((rt_uint32_t)thread->stack_addr + 32)) + else if ((rt_ubase_t)thread->sp <= ((rt_ubase_t)thread->stack_addr + 32)) { rt_kprintf("warning: %s stack is close to end of stack address.\n", thread->name); @@ -159,7 +159,7 @@ void rt_system_scheduler_start(void) rt_current_thread = to_thread; /* switch to new thread */ - rt_hw_context_switch_to((rt_uint32_t)&to_thread->sp); + rt_hw_context_switch_to((rt_ubase_t)&to_thread->sp); /* never come back */ } @@ -214,8 +214,8 @@ void rt_schedule(void) /* switch to new thread */ RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("[%d]switch to priority#%d " - "thread:%.*s(sp:0x%p), " - "from thread:%.*s(sp: 0x%p)\n", + "thread:%.*s(sp:0x%08x), " + "from thread:%.*s(sp: 0x%08x)\n", rt_interrupt_nest, highest_ready_priority, RT_NAME_MAX, to_thread->name, to_thread->sp, RT_NAME_MAX, from_thread->name, from_thread->sp)); @@ -228,8 +228,8 @@ void rt_schedule(void) { extern void rt_thread_handle_sig(rt_bool_t clean_state); - rt_hw_context_switch((rt_uint32_t)&from_thread->sp, - (rt_uint32_t)&to_thread->sp); + rt_hw_context_switch((rt_ubase_t)&from_thread->sp, + (rt_ubase_t)&to_thread->sp); /* enable interrupt */ rt_hw_interrupt_enable(level); @@ -243,8 +243,8 @@ void rt_schedule(void) { RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n")); - rt_hw_context_switch_interrupt((rt_uint32_t)&from_thread->sp, - (rt_uint32_t)&to_thread->sp); + rt_hw_context_switch_interrupt((rt_ubase_t)&from_thread->sp, + (rt_ubase_t)&to_thread->sp); /* enable interrupt */ rt_hw_interrupt_enable(level); } diff --git a/src/thread.c b/src/thread.c index 6d79ece54b..82eb18942c 100644 --- a/src/thread.c +++ b/src/thread.c @@ -137,7 +137,7 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread, /* init thread stack */ rt_memset(thread->stack_addr, '#', thread->stack_size); thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter, - (void *)((char *)thread->stack_addr + thread->stack_size - 4), + (void *)((char *)thread->stack_addr + thread->stack_size - sizeof(rt_ubase_t)), (void *)rt_thread_exit); /* priority init */ -- cgit v1.3.1 From fc6bc1ad398d5fa2085320cab6cc33905f5f943d Mon Sep 17 00:00:00 2001 From: shaojinchun Date: Thu, 22 Nov 2018 14:40:43 +0800 Subject: Add smp support to RT-Thread 4.0 --- bsp/qemu-vexpress-a9/.config | 25 +- bsp/qemu-vexpress-a9/cpu/context_gcc.S | 70 +++ bsp/qemu-vexpress-a9/cpu/cpu.c | 14 + bsp/qemu-vexpress-a9/cpu/gic.c | 19 + bsp/qemu-vexpress-a9/cpu/gic.h | 1 + bsp/qemu-vexpress-a9/cpu/interrupt.c | 10 +- bsp/qemu-vexpress-a9/cpu/start_gcc.S | 193 +++++++- bsp/qemu-vexpress-a9/cpu/trap.c | 1 - bsp/qemu-vexpress-a9/drivers/board.c | 108 ++--- bsp/qemu-vexpress-a9/drivers/drv_timer.c | 130 ++++++ bsp/qemu-vexpress-a9/drivers/drv_timer.h | 17 + bsp/qemu-vexpress-a9/drivers/secondary_cpu.c | 61 +++ bsp/qemu-vexpress-a9/link.lds | 2 +- bsp/qemu-vexpress-a9/qemu-nographic.sh | 2 +- bsp/qemu-vexpress-a9/qemu.bat | 2 +- bsp/qemu-vexpress-a9/qemu.sh | 2 +- bsp/qemu-vexpress-a9/rtconfig.h | 132 +++++- bsp/qemu-vexpress-a9/rtconfig.py | 2 +- components/finsh/cmd.c | 23 +- include/rtdef.h | 56 ++- include/rthw.h | 58 +++ include/rtthread.h | 19 + src/Kconfig | 16 + src/clock.c | 9 + src/components.c | 8 + src/cpu.c | 91 ++++ src/idle.c | 77 ++- src/irq.c | 13 +- src/scheduler.c | 674 ++++++++++++++++++++++----- src/signal.c | 12 +- src/thread.c | 92 ++-- 31 files changed, 1672 insertions(+), 267 deletions(-) create mode 100644 bsp/qemu-vexpress-a9/drivers/drv_timer.c create mode 100644 bsp/qemu-vexpress-a9/drivers/drv_timer.h create mode 100644 bsp/qemu-vexpress-a9/drivers/secondary_cpu.c create mode 100644 src/cpu.c (limited to 'include/rtdef.h') diff --git a/bsp/qemu-vexpress-a9/.config b/bsp/qemu-vexpress-a9/.config index 05071cff27..7c6c2dc1df 100644 --- a/bsp/qemu-vexpress-a9/.config +++ b/bsp/qemu-vexpress-a9/.config @@ -7,6 +7,8 @@ # RT-Thread Kernel # CONFIG_RT_NAME_MAX=8 +CONFIG_RT_USING_SMP=y +CONFIG_RT_CPUS_NR=2 CONFIG_RT_ALIGN_SIZE=4 # CONFIG_RT_THREAD_PRIORITY_8 is not set CONFIG_RT_THREAD_PRIORITY_32=y @@ -15,6 +17,7 @@ CONFIG_RT_THREAD_PRIORITY_MAX=32 CONFIG_RT_TICK_PER_SECOND=100 CONFIG_RT_USING_OVERFLOW_CHECK=y CONFIG_RT_USING_HOOK=y +CONFIG_RT_USING_IDLE_HOOK=y CONFIG_RT_IDEL_HOOK_LIST_SIZE=4 CONFIG_IDLE_THREAD_STACK_SIZE=1024 CONFIG_RT_USING_TIMER_SOFT=y @@ -66,6 +69,7 @@ CONFIG_RT_CONSOLE_DEVICE_NAME="uart0" CONFIG_ARCH_ARM=y CONFIG_ARCH_ARM_CORTEX_A=y CONFIG_ARCH_ARM_CORTEX_A9=y +# CONFIG_ARCH_CPU_STACK_GROWS_UPWARD is not set # # RT-Thread Components @@ -144,14 +148,15 @@ CONFIG_RT_USING_SERIAL=y CONFIG_RT_USING_I2C=y CONFIG_RT_USING_I2C_BITOPS=y CONFIG_RT_USING_PIN=y +# CONFIG_RT_USING_ADC is not set # CONFIG_RT_USING_PWM is not set CONFIG_RT_USING_MTD_NOR=y CONFIG_RT_USING_MTD_NAND=y CONFIG_RT_MTD_NAND_DEBUG=y # CONFIG_RT_USING_MTD is not set +# CONFIG_RT_USING_PM is not set CONFIG_RT_USING_RTC=y CONFIG_RT_USING_SOFT_RTC=y -# CONFIG_RTC_SYNC_USING_NTP is not set CONFIG_RT_USING_SDIO=y CONFIG_RT_SDIO_STACK_SIZE=512 CONFIG_RT_SDIO_THREAD_PRIORITY=15 @@ -216,6 +221,7 @@ CONFIG_SAL_PROTO_FAMILIES_NUM=4 CONFIG_RT_USING_LWIP=y # CONFIG_RT_USING_LWIP141 is not set CONFIG_RT_USING_LWIP202=y +# CONFIG_RT_USING_LWIP210 is not set CONFIG_RT_USING_LWIP_IPV6=y # CONFIG_RT_LWIP_IGMP is not set CONFIG_RT_LWIP_ICMP=y @@ -291,6 +297,7 @@ CONFIG_LOG_TRACE_USING_LEVEL_INFO=y # CONFIG_LOG_TRACE_USING_LEVEL_DEBUG is not set # CONFIG_LOG_TRACE_USING_MEMLOG is not set # CONFIG_RT_USING_RYM is not set +# CONFIG_RT_USING_ULOG is not set # # RT-Thread online packages @@ -376,18 +383,15 @@ CONFIG_LOG_TRACE_USING_LEVEL_INFO=y # CONFIG_PKG_USING_SQLITE is not set # CONFIG_PKG_USING_RTI is not set # CONFIG_PKG_USING_LITTLEVGL2RTT is not set -# CONFIG_PKG_USING_CMSIS is not set -# CONFIG_PKG_USING_DFS_YAFFS is not set # # peripheral libraries and drivers # +# CONFIG_PKG_USING_STM32F4_HAL is not set +# CONFIG_PKG_USING_STM32F4_DRIVERS is not set # CONFIG_PKG_USING_REALTEK_AMEBA is not set # CONFIG_PKG_USING_SHT2X is not set # CONFIG_PKG_USING_AHT10 is not set -# CONFIG_PKG_USING_AP3216C is not set -# CONFIG_PKG_USING_STM32_SDIO is not set -# CONFIG_PKG_USING_ICM20608 is not set # # miscellaneous packages @@ -405,14 +409,7 @@ CONFIG_LOG_TRACE_USING_LEVEL_INFO=y # # sample package # - -# -# samples: kernel and components samples -# -# CONFIG_PKG_USING_KERNEL_SAMPLES is not set -# CONFIG_PKG_USING_FILESYSTEM_SAMPLES is not set -# CONFIG_PKG_USING_NETWORK_SAMPLES is not set -# CONFIG_PKG_USING_PERIPHERAL_SAMPLES is not set +# CONFIG_PKG_USING_SAMPLES is not set # # example package: hello diff --git a/bsp/qemu-vexpress-a9/cpu/context_gcc.S b/bsp/qemu-vexpress-a9/cpu/context_gcc.S index 86efcede9f..2b13690e67 100644 --- a/bsp/qemu-vexpress-a9/cpu/context_gcc.S +++ b/bsp/qemu-vexpress-a9/cpu/context_gcc.S @@ -20,9 +20,19 @@ * Change Logs: * Date Author Notes * 2013-07-05 Bernard the first version + * 2018-11-22 Jesven in the smp version, using macro to + * define rt_hw_interrupt_enable and rt_hw_interrupt_disable + * rt_hw_context_switch_interrupt switches to the new thread directly */ +#include "rtconfig.h" .section .text, "ax" + +#ifdef RT_USING_SMP +#define rt_hw_interrupt_disable rt_hw_local_irq_disable +#define rt_hw_interrupt_enable rt_hw_local_irq_enable +#endif + /* * rt_base_t rt_hw_interrupt_disable(); */ @@ -48,6 +58,11 @@ rt_hw_interrupt_enable: rt_hw_context_switch_to: ldr sp, [r0] @ get new task stack pointer +#ifdef RT_USING_SMP + mov r0, r1 + bl rt_cpus_lock_status_restore +#endif /*RT_USING_SMP*/ + ldmfd sp!, {r4} @ pop new task spsr msr spsr_cxsf, r4 @@ -77,6 +92,11 @@ rt_hw_context_switch: str sp, [r0] @ store sp in preempted tasks TCB ldr sp, [r1] @ get new task stack pointer +#ifdef RT_USING_SMP + mov r0, r2 + bl rt_cpus_lock_status_restore +#endif /*RT_USING_SMP*/ + ldmfd sp!, {r4} @ pop new task cpsr to spsr msr spsr_cxsf, r4 ldmfd sp!, {r0-r12, lr, pc}^ @ pop new task r0-r12, lr & pc, copy spsr to cpsr @@ -84,11 +104,60 @@ rt_hw_context_switch: /* * void rt_hw_context_switch_interrupt(rt_uint32 from, rt_uint32 to); */ +.equ Mode_USR, 0x10 +.equ Mode_FIQ, 0x11 +.equ Mode_IRQ, 0x12 +.equ Mode_SVC, 0x13 +.equ Mode_ABT, 0x17 +.equ Mode_UND, 0x1B +.equ Mode_SYS, 0x1F + +.equ I_Bit, 0x80 @ when I bit is set, IRQ is disabled +.equ F_Bit, 0x40 @ when F bit is set, FIQ is disabled + .globl rt_thread_switch_interrupt_flag .globl rt_interrupt_from_thread .globl rt_interrupt_to_thread .globl rt_hw_context_switch_interrupt rt_hw_context_switch_interrupt: +#ifdef RT_USING_SMP + /* r0 :irq_mod context + * r1 :addr of from_thread's sp + * r2 :addr of to_thread's sp + * r3 :to_thread's tcb + */ + + @ r0 point to {r0-r3} in stack + push {r1 - r3} + mov r1, r0 + add r0, r0, #4*4 + ldmfd r0!, {r4-r12,lr}@ reload saved registers + mrs r3, spsr @ get cpsr of interrupt thread + sub r2, lr, #4 @ save old task's pc to r2 + msr cpsr_c, #I_Bit|F_Bit|Mode_SVC + + stmfd sp!, {r2} @ push old task's pc + stmfd sp!, {r4-r12,lr}@ push old task's lr,r12-r4 + ldmfd r0, {r4-r7} @ restore r0-r3 of the interrupt thread + stmfd sp!, {r4-r7} @ push old task's r0-r3 + stmfd sp!, {r3} @ push old task's cpsr + + msr cpsr_c, #I_Bit|F_Bit|Mode_IRQ + pop {r1 - r3} + mov sp, r0 + msr cpsr_c, #I_Bit|F_Bit|Mode_SVC + str sp, [r1] + + ldr sp, [r2] + mov r0, r3 + bl rt_cpus_lock_status_restore + + ldmfd sp!, {r4} @ pop new task's cpsr to spsr + msr spsr_cxsf, r4 + + ldmfd sp!, {r0-r12,lr,pc}^ @ pop new task's r0-r12,lr & pc, copy spsr to cpsr + +#else /*RT_USING_SMP*/ ldr r2, =rt_thread_switch_interrupt_flag ldr r3, [r2] cmp r3, #1 @@ -101,3 +170,4 @@ _reswitch: ldr r2, =rt_interrupt_to_thread @ set rt_interrupt_to_thread str r1, [r2] bx lr +#endif /*RT_USING_SMP*/ diff --git a/bsp/qemu-vexpress-a9/cpu/cpu.c b/bsp/qemu-vexpress-a9/cpu/cpu.c index 426da965ad..10099809a3 100644 --- a/bsp/qemu-vexpress-a9/cpu/cpu.c +++ b/bsp/qemu-vexpress-a9/cpu/cpu.c @@ -10,12 +10,26 @@ * Change Logs: * Date Author Notes * 2011-09-15 Bernard first version + * 2018-11-22 Jesven add rt_hw_cpu_id() */ #include #include #include +#ifdef RT_USING_SMP +int rt_hw_cpu_id(void) +{ + int cpu_id; + __asm__ volatile ( + "mrc p15, 0, %0, c0, c0, 5" + :"=r"(cpu_id) + ); + cpu_id &= 0xf; + return cpu_id; +}; +#endif + /** * @addtogroup ARM CPU */ diff --git a/bsp/qemu-vexpress-a9/cpu/gic.c b/bsp/qemu-vexpress-a9/cpu/gic.c index cbef00af22..66876518ce 100644 --- a/bsp/qemu-vexpress-a9/cpu/gic.c +++ b/bsp/qemu-vexpress-a9/cpu/gic.c @@ -11,8 +11,11 @@ * Date Author Notes * 2013-07-20 Bernard first version * 2014-04-03 Grissiom many enhancements + * 2018-11-22 Jesven add rt_hw_ipi_send() + * add rt_hw_ipi_handler_install() */ +#include #include #include @@ -278,3 +281,19 @@ void arm_gic_set_group(rt_uint32_t index, int vector, int group) vector) |= (1 << (vector % 32)); } } + +#ifdef RT_USING_SMP +void rt_hw_ipi_send(int ipi_vector, unsigned int cpu_mask) + { + /* note: ipi_vector maybe different with irq_vector */ + GIC_DIST_SOFTINT(_gic_table[0].dist_hw_base) = (cpu_mask << 16) | ipi_vector; +} +#endif + +#ifdef RT_USING_SMP +void rt_hw_ipi_handler_install(int ipi_vector, rt_isr_handler_t ipi_isr_handler) +{ + /* note: ipi_vector maybe different with irq_vector */ + rt_hw_interrupt_install(ipi_vector, ipi_isr_handler, 0, "IPI_HANDLER"); +} +#endif diff --git a/bsp/qemu-vexpress-a9/cpu/gic.h b/bsp/qemu-vexpress-a9/cpu/gic.h index 42c061dc1b..9d0368ec94 100644 --- a/bsp/qemu-vexpress-a9/cpu/gic.h +++ b/bsp/qemu-vexpress-a9/cpu/gic.h @@ -27,6 +27,7 @@ int arm_gic_get_active_irq(rt_uint32_t index); void arm_gic_ack(rt_uint32_t index, int irq); void arm_gic_dump_type(rt_uint32_t index); +void rt_hw_vector_init(void); #endif diff --git a/bsp/qemu-vexpress-a9/cpu/interrupt.c b/bsp/qemu-vexpress-a9/cpu/interrupt.c index e66f746042..febf86a66e 100644 --- a/bsp/qemu-vexpress-a9/cpu/interrupt.c +++ b/bsp/qemu-vexpress-a9/cpu/interrupt.c @@ -10,6 +10,7 @@ * Change Logs: * Date Author Notes * 2013-07-06 Bernard first version + * 2018-11-22 Jesven add smp support */ #include @@ -19,21 +20,21 @@ #define MAX_HANDLERS NR_IRQS_PBA8 -extern volatile rt_uint8_t rt_interrupt_nest; - /* exception and interrupt handler table */ struct rt_irq_desc isr_table[MAX_HANDLERS]; +#ifndef RT_USING_SMP /* Those varibles will be accessed in ISR, so we need to share them. */ rt_uint32_t rt_interrupt_from_thread; rt_uint32_t rt_interrupt_to_thread; rt_uint32_t rt_thread_switch_interrupt_flag; +#endif const unsigned int VECTOR_BASE = 0x00; extern void rt_cpu_vector_set_base(unsigned int addr); extern int system_vectors; -static void rt_hw_vector_init(void) +void rt_hw_vector_init(void) { rt_cpu_vector_set_base((unsigned int)&system_vectors); } @@ -60,10 +61,11 @@ void rt_hw_interrupt_init(void) arm_gic_cpu_init(0, gic_cpu_base); /* init interrupt nest, and context in thread sp */ - rt_interrupt_nest = 0; +#ifndef RT_USING_SMP rt_interrupt_from_thread = 0; rt_interrupt_to_thread = 0; rt_thread_switch_interrupt_flag = 0; +#endif } /** diff --git a/bsp/qemu-vexpress-a9/cpu/start_gcc.S b/bsp/qemu-vexpress-a9/cpu/start_gcc.S index 569b14fbc1..d380d02d08 100644 --- a/bsp/qemu-vexpress-a9/cpu/start_gcc.S +++ b/bsp/qemu-vexpress-a9/cpu/start_gcc.S @@ -20,8 +20,12 @@ * Change Logs: * Date Author Notes * 2013-07-05 Bernard the first version + * 2018-11-22 Jesven in the interrupt context, use rt_scheduler_do_irq_switch checks + * and switches to a new thread */ +#include "rtconfig.h" + .equ Mode_USR, 0x10 .equ Mode_FIQ, 0x11 .equ Mode_IRQ, 0x12 @@ -64,6 +68,17 @@ _reset: orr r0, r0, #0x13 msr cpsr_c, r0 + mrc p15, 0, r1, c1, c0, 1 + mov r0, #(1<<6) + orr r1, r0 + mcr p15, 0, r1, c1, c0, 1 //enable smp + + ldr lr, =after_enable_mmu + ldr r0, =mtbl + b enable_mmu + +after_enable_mmu: + /* setup stack */ bl stack_setup @@ -93,6 +108,7 @@ ctor_loop: ctor_end: /* start RT-Thread Kernel */ + bl flush_cache_all ldr pc, _rtthread_startup _rtthread_startup: .word rtthread_startup @@ -127,6 +143,85 @@ stack_setup: msr cpsr_c, #Mode_SVC|I_Bit|F_Bit bx lr + .global enable_mmu +enable_mmu: + orr r0, #0x18 + mcr p15, 0, r0, c2, c0, 0 @ttbr0 + + mov r0, #(1 << 5) @PD1=1 + mcr p15, 0, r0, c2, c0, 2 @ttbcr + + mov r0, #1 + mcr p15, 0, r0, c3, c0, 0 @dacr + + mov r0, #0 + mcr p15, 0, r0, c8, c7, 0 + mcr p15, 0, r0, c7, c5, 0 @iciallu + mcr p15, 0, r0, c7, c5, 6 @bpiall + + mrc p15, 0, r0, c1, c0, 0 + orr r0, #(1 | 4) + orr r0, #(1 << 12) + mcr p15, 0, r0, c1, c0, 0 + dsb + isb + mov pc, lr + +.global flush_cache_all +flush_cache_all: + stmfd sp!, {r0-r12, lr} + bl v7_flush_dcache_all + mov r0, #0 + mcr p15, 0, r0, c7, c5, 0 @ I+BTB cache invalidate + dsb + isb + ldmfd sp!, {r0-r12, lr} + mov pc, lr + + v7_flush_dcache_all: + dmb @ ensure ordering with previous memory accesses + mrc p15, 1, r0, c0, c0, 1 @ read clidr + ands r3, r0, #0x7000000 @ extract loc from clidr + mov r3, r3, lsr #23 @ left align loc bit field + beq finished @ if loc is 0, then no need to clean + mov r10, #0 @ start clean at cache level 0 +loop1: + add r2, r10, r10, lsr #1 @ work out 3x current cache level + mov r1, r0, lsr r2 @ extract cache type bits from clidr + and r1, r1, #7 @ mask of the bits for current cache only + cmp r1, #2 @ see what cache we have at this level + blt skip @ skip if no cache, or just i-cache + mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr + isb @ isb to sych the new cssr&csidr + mrc p15, 1, r1, c0, c0, 0 @ read the new csidr + and r2, r1, #7 @ extract the length of the cache lines + add r2, r2, #4 @ add 4 (line length offset) + ldr r4, =0x3ff + ands r4, r4, r1, lsr #3 @ find maximum number on the way size + clz r5, r4 @ find bit position of way size increment + ldr r7, =0x7fff + ands r7, r7, r1, lsr #13 @ extract max number of the index size +loop2: + mov r9, r4 @ create working copy of max way size +loop3: + orr r11, r10, r9, lsl r5 @ factor way and cache number into r11 + orr r11, r11, r7, lsl r2 @ factor index number into r11 + mcr p15, 0, r11, c7, c14, 2 @ clean & invalidate by set/way + subs r9, r9, #1 @ decrement the way + bge loop3 + subs r7, r7, #1 @ decrement the index + bge loop2 +skip: + add r10, r10, #2 @ increment cache number + cmp r3, r10 + bgt loop1 +finished: + mov r10, #0 @ swith back to cache level 0 + mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr + dsb + isb + mov pc, lr + /* exception handlers: undef, swi, padt, dabt, resv, irq, fiq */ .section .text.isr, "ax" .align 5 @@ -143,17 +238,25 @@ vector_fiq: .globl rt_interrupt_from_thread .globl rt_interrupt_to_thread -.globl rt_current_thread - .align 5 .globl vector_irq vector_irq: +#ifdef RT_USING_SMP + clrex +#endif stmfd sp!, {r0-r12,lr} bl rt_interrupt_enter bl rt_hw_trap_irq bl rt_interrupt_leave +#ifdef RT_USING_SMP + mov r0, sp + bl rt_scheduler_do_irq_switch + + ldmfd sp!, {r0-r12,lr} + subs pc, lr, #4 +#else @ if rt_thread_switch_interrupt_flag set, jump to @ rt_hw_context_switch_interrupt_do and don't return ldr r0, =rt_thread_switch_interrupt_flag @@ -198,6 +301,8 @@ rt_hw_context_switch_interrupt_do: ldmfd sp!, {r0-r12,lr,pc}^ @ pop new task's r0-r12,lr & pc, copy spsr to cpsr +#endif + .macro push_svc_reg sub sp, sp, #17 * 4 @/* Sizeof(struct rt_hw_exp_stack) */ stmia sp, {r0 - r12} @/* Calling r0-r12 */ @@ -244,3 +349,87 @@ vector_resv: push_svc_reg bl rt_hw_trap_resv b . + +#ifdef RT_USING_SMP +.global set_secondary_cpu_boot_address +set_secondary_cpu_boot_address: + ldr r0, =secondary_cpu_start + + mvn r1, #0 //0xffffffff + ldr r2, =0x10000034 + str r1, [r2] + str r0, [r2, #-4] + mov pc, lr + +.global secondary_cpu_start +secondary_cpu_start: + mrc p15, 0, r1, c1, c0, 1 + mov r0, #(1<<6) + orr r1, r0 + mcr p15, 0, r1, c1, c0, 1 //enable smp + + ldr r0, =mtbl + ldr lr, =1f + + b enable_mmu +1: + mrc p15, 0, r0, c1, c0, 0 + bic r0, #(1<<13) + mcr p15, 0, r0, c1, c0, 0 + + cps #Mode_IRQ + ldr sp, =irq_stack_2_limit + + cps #Mode_FIQ + ldr sp, =irq_stack_2_limit + + cps #Mode_SVC + ldr sp, =svc_stack_2_limit + + b secondary_cpu_c_start +#endif + +.bss +.align 2 //align to 2~2=4 +svc_stack_2: + .space (1 << 10) +svc_stack_2_limit: + +irq_stack_2: + .space (1 << 10) +irq_stack_2_limit: + +.data +#define DEVICE_MEM 0x10406 +#define NORMAL_MEM 0x1140e +.align 14 +mtbl: + + //vaddr: 0x00000000 + .rept 0x100 + .word 0x0 + .endr + + //vaddr: 0x10000000 + .equ mmu_tbl_map_paddr, 0x10000000 + .rept 0x400 + .word mmu_tbl_map_paddr | DEVICE_MEM + .equ mmu_tbl_map_paddr, mmu_tbl_map_paddr + 0x100000 + .endr + + //vaddr: 0x50000000 + .rept 0x100 + .word 0x0 + .endr + + //vaddr: 0x60000000 + .equ mmu_tbl_map_paddr, 0x60000000 + .rept 0x800 + .word mmu_tbl_map_paddr | NORMAL_MEM + .equ mmu_tbl_map_paddr, mmu_tbl_map_paddr + 0x100000 + .endr + + //vaddr: 0xe0000000 + .rept 0x200 + .word 0x0 + .endr diff --git a/bsp/qemu-vexpress-a9/cpu/trap.c b/bsp/qemu-vexpress-a9/cpu/trap.c index e3283d7e7e..a1f00d0075 100644 --- a/bsp/qemu-vexpress-a9/cpu/trap.c +++ b/bsp/qemu-vexpress-a9/cpu/trap.c @@ -20,7 +20,6 @@ #include "gic.h" -extern struct rt_thread *rt_current_thread; #ifdef RT_USING_FINSH extern long list_thread(void); #endif diff --git a/bsp/qemu-vexpress-a9/drivers/board.c b/bsp/qemu-vexpress-a9/drivers/board.c index 8be0a47103..26aad5bda1 100644 --- a/bsp/qemu-vexpress-a9/drivers/board.c +++ b/bsp/qemu-vexpress-a9/drivers/board.c @@ -1,74 +1,24 @@ /* - * File : board.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, 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 + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2012-11-20 Bernard the first version + * 2018-11-22 Jesven add rt_hw_spin_lock + * add rt_hw_spin_unlock + * add smp ipi init */ #include #include #include "board.h" - -#define TIMER_LOAD(hw_base) __REG32(hw_base + 0x00) -#define TIMER_VALUE(hw_base) __REG32(hw_base + 0x04) -#define TIMER_CTRL(hw_base) __REG32(hw_base + 0x08) -#define TIMER_CTRL_ONESHOT (1 << 0) -#define TIMER_CTRL_32BIT (1 << 1) -#define TIMER_CTRL_DIV1 (0 << 2) -#define TIMER_CTRL_DIV16 (1 << 2) -#define TIMER_CTRL_DIV256 (2 << 2) -#define TIMER_CTRL_IE (1 << 5) /* Interrupt Enable (versatile only) */ -#define TIMER_CTRL_PERIODIC (1 << 6) -#define TIMER_CTRL_ENABLE (1 << 7) - -#define TIMER_INTCLR(hw_base) __REG32(hw_base + 0x0c) -#define TIMER_RIS(hw_base) __REG32(hw_base + 0x10) -#define TIMER_MIS(hw_base) __REG32(hw_base + 0x14) -#define TIMER_BGLOAD(hw_base) __REG32(hw_base + 0x18) +#include "drv_timer.h" #define SYS_CTRL __REG32(REALVIEW_SCTL_BASE) -#define TIMER_HW_BASE REALVIEW_TIMER2_3_BASE - -static void rt_hw_timer_isr(int vector, void *param) -{ - rt_tick_increase(); - /* clear interrupt */ - TIMER_INTCLR(TIMER_HW_BASE) = 0x01; -} - -int rt_hw_timer_init(void) -{ - rt_uint32_t val; - - SYS_CTRL |= REALVIEW_REFCLK; - - /* Setup Timer0 for generating irq */ - val = TIMER_CTRL(TIMER_HW_BASE); - val &= ~TIMER_CTRL_ENABLE; - val |= (TIMER_CTRL_32BIT | TIMER_CTRL_PERIODIC | TIMER_CTRL_IE); - TIMER_CTRL(TIMER_HW_BASE) = val; - - TIMER_LOAD(TIMER_HW_BASE) = 1000; - - /* enable timer */ - TIMER_CTRL(TIMER_HW_BASE) |= TIMER_CTRL_ENABLE; - - rt_hw_interrupt_install(IRQ_PBA8_TIMER2_3, rt_hw_timer_isr, RT_NULL, "tick"); - rt_hw_interrupt_umask(IRQ_PBA8_TIMER2_3); - - return 0; -} -INIT_BOARD_EXPORT(rt_hw_timer_init); - void idle_wfi(void) { asm volatile ("wfi"); @@ -79,13 +29,57 @@ void idle_wfi(void) */ void rt_hw_board_init(void) { - /* initialzie hardware interrupt */ + /* initialize hardware interrupt */ rt_hw_interrupt_init(); + /* initialize system heap */ rt_system_heap_init(HEAP_BEGIN, HEAP_END); rt_components_board_init(); rt_console_set_device(RT_CONSOLE_DEVICE_NAME); rt_thread_idle_sethook(idle_wfi); + +#ifdef RT_USING_SMP + /* install IPI interrupt */ + rt_hw_interrupt_install(RT_SCHEDULE_IPI_IRQ, rt_scheduler_ipi_handler, RT_NULL, "ipi"); + rt_hw_interrupt_umask(RT_SCHEDULE_IPI_IRQ); +#endif } +#ifdef RT_USING_SMP +void rt_hw_spin_lock(rt_hw_spinlock_t *lock) +{ + unsigned long tmp; + unsigned long newval; + rt_hw_spinlock_t lockval; + + __asm__ __volatile__( + "pld [%0]" + ::"r"(&lock->slock) + ); + + __asm__ __volatile__( + "1: ldrex %0, [%3]\n" + " add %1, %0, %4\n" + " strex %2, %1, [%3]\n" + " teq %2, #0\n" + " bne 1b" + : "=&r" (lockval), "=&r" (newval), "=&r" (tmp) + : "r" (&lock->slock), "I" (1 << 16) + : "cc"); + + while (lockval.tickets.next != lockval.tickets.owner) { + __asm__ __volatile__("wfe":::"memory"); + lockval.tickets.owner = *(volatile unsigned short *)(&lock->tickets.owner); + } + + __asm__ volatile ("dmb":::"memory"); +} + +void rt_hw_spin_unlock(rt_hw_spinlock_t *lock) +{ + __asm__ volatile ("dmb":::"memory"); + lock->tickets.owner++; + __asm__ volatile ("dsb ishst\nsev":::"memory"); +} +#endif /*RT_USING_SMP*/ diff --git a/bsp/qemu-vexpress-a9/drivers/drv_timer.c b/bsp/qemu-vexpress-a9/drivers/drv_timer.c new file mode 100644 index 0000000000..a470142202 --- /dev/null +++ b/bsp/qemu-vexpress-a9/drivers/drv_timer.c @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-11-22 Jesven first version + */ + +#include +#include +#include + +#include "board.h" + +#define TIMER01_HW_BASE 0x10011000 +#define TIMER23_HW_BASE 0x10012000 + +#define TIMER_LOAD(hw_base) __REG32(hw_base + 0x00) +#define TIMER_VALUE(hw_base) __REG32(hw_base + 0x04) +#define TIMER_CTRL(hw_base) __REG32(hw_base + 0x08) +#define TIMER_CTRL_ONESHOT (1 << 0) +#define TIMER_CTRL_32BIT (1 << 1) +#define TIMER_CTRL_DIV1 (0 << 2) +#define TIMER_CTRL_DIV16 (1 << 2) +#define TIMER_CTRL_DIV256 (2 << 2) +#define TIMER_CTRL_IE (1 << 5) /* Interrupt Enable (versatile only) */ +#define TIMER_CTRL_PERIODIC (1 << 6) +#define TIMER_CTRL_ENABLE (1 << 7) + +#define TIMER_INTCLR(hw_base) __REG32(hw_base + 0x0c) +#define TIMER_RIS(hw_base) __REG32(hw_base + 0x10) +#define TIMER_MIS(hw_base) __REG32(hw_base + 0x14) +#define TIMER_BGLOAD(hw_base) __REG32(hw_base + 0x18) + +#define TIMER_LOAD(hw_base) __REG32(hw_base + 0x00) +#define TIMER_VALUE(hw_base) __REG32(hw_base + 0x04) +#define TIMER_CTRL(hw_base) __REG32(hw_base + 0x08) +#define TIMER_CTRL_ONESHOT (1 << 0) +#define TIMER_CTRL_32BIT (1 << 1) +#define TIMER_CTRL_DIV1 (0 << 2) +#define TIMER_CTRL_DIV16 (1 << 2) +#define TIMER_CTRL_DIV256 (2 << 2) +#define TIMER_CTRL_IE (1 << 5) /* Interrupt Enable (versatile only) */ +#define TIMER_CTRL_PERIODIC (1 << 6) +#define TIMER_CTRL_ENABLE (1 << 7) + +#define TIMER_INTCLR(hw_base) __REG32(hw_base + 0x0c) +#define TIMER_RIS(hw_base) __REG32(hw_base + 0x10) +#define TIMER_MIS(hw_base) __REG32(hw_base + 0x14) +#define TIMER_BGLOAD(hw_base) __REG32(hw_base + 0x18) + +#define SYS_CTRL __REG32(REALVIEW_SCTL_BASE) +#define TIMER_HW_BASE REALVIEW_TIMER2_3_BASE + +static void rt_hw_timer_isr(int vector, void *param) +{ + rt_tick_increase(); + /* clear interrupt */ + TIMER_INTCLR(TIMER_HW_BASE) = 0x01; +} + +int rt_hw_timer_init(void) +{ + rt_uint32_t val; + + SYS_CTRL |= REALVIEW_REFCLK; + + /* Setup Timer0 for generating irq */ + val = TIMER_CTRL(TIMER_HW_BASE); + val &= ~TIMER_CTRL_ENABLE; + val |= (TIMER_CTRL_32BIT | TIMER_CTRL_PERIODIC | TIMER_CTRL_IE); + TIMER_CTRL(TIMER_HW_BASE) = val; + + TIMER_LOAD(TIMER_HW_BASE) = 1000; + + /* enable timer */ + TIMER_CTRL(TIMER_HW_BASE) |= TIMER_CTRL_ENABLE; + + rt_hw_interrupt_install(IRQ_PBA8_TIMER2_3, rt_hw_timer_isr, RT_NULL, "tick"); + rt_hw_interrupt_umask(IRQ_PBA8_TIMER2_3); + + return 0; +} +INIT_BOARD_EXPORT(rt_hw_timer_init); + +void timer_init(int timer, unsigned int preload) +{ + uint32_t val; + + if (timer == 0) + { + /* Setup Timer0 for generating irq */ + val = TIMER_CTRL(TIMER01_HW_BASE); + val &= ~TIMER_CTRL_ENABLE; + val |= (TIMER_CTRL_32BIT | TIMER_CTRL_PERIODIC | TIMER_CTRL_IE); + TIMER_CTRL(TIMER01_HW_BASE) = val; + + TIMER_LOAD(TIMER01_HW_BASE) = preload; + + /* enable timer */ + TIMER_CTRL(TIMER01_HW_BASE) |= TIMER_CTRL_ENABLE; + } + else + { + /* Setup Timer1 for generating irq */ + val = TIMER_CTRL(TIMER23_HW_BASE); + val &= ~TIMER_CTRL_ENABLE; + val |= (TIMER_CTRL_32BIT | TIMER_CTRL_PERIODIC | TIMER_CTRL_IE); + TIMER_CTRL(TIMER23_HW_BASE) = val; + + TIMER_LOAD(TIMER23_HW_BASE) = preload; + + /* enable timer */ + TIMER_CTRL(TIMER23_HW_BASE) |= TIMER_CTRL_ENABLE; + } +} + +void timer_clear_pending(int timer) +{ + if (timer == 0) + { + TIMER_INTCLR(TIMER01_HW_BASE) = 0x01; + } + else + { + TIMER_INTCLR(TIMER23_HW_BASE) = 0x01; + } +} diff --git a/bsp/qemu-vexpress-a9/drivers/drv_timer.h b/bsp/qemu-vexpress-a9/drivers/drv_timer.h new file mode 100644 index 0000000000..718f5d43cf --- /dev/null +++ b/bsp/qemu-vexpress-a9/drivers/drv_timer.h @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-11-22 Jesven first version + */ + +#ifndef DRV_TIMER_H__ +#define DRV_TIMER_H__ + +void timer_init(int timer, unsigned int preload); +void timer_clear_pending(int timer); + +#endif diff --git a/bsp/qemu-vexpress-a9/drivers/secondary_cpu.c b/bsp/qemu-vexpress-a9/drivers/secondary_cpu.c new file mode 100644 index 0000000000..b8b5436929 --- /dev/null +++ b/bsp/qemu-vexpress-a9/drivers/secondary_cpu.c @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-11-22 Jesven first version + */ + +#include +#include +#include + +#include "board.h" +#include "gic.h" +#include "drv_timer.h" + +#ifdef RT_USING_SMP +static void rt_hw_timer2_isr(int vector, void *param) +{ + rt_tick_increase(); + /* clear interrupt */ + timer_clear_pending(0); +} + +void rt_hw_secondary_cpu_up(void) +{ + extern void set_secondary_cpu_boot_address(void); + + set_secondary_cpu_boot_address(); + __asm__ volatile ("dsb":::"memory"); + rt_hw_ipi_send(0, 1 << 1); +} + +void secondary_cpu_c_start(void) +{ + rt_hw_vector_init(); + + rt_hw_spin_lock(&_cpus_lock); + + arm_gic_cpu_init(0, REALVIEW_GIC_CPU_BASE); + arm_gic_set_cpu(0, IRQ_PBA8_TIMER0_1, 0x2); + + timer_init(0, 1000); + rt_hw_interrupt_install(IRQ_PBA8_TIMER0_1, rt_hw_timer2_isr, RT_NULL, "tick"); + rt_hw_interrupt_umask(IRQ_PBA8_TIMER0_1); + + /* install IPI interrupt */ + rt_hw_interrupt_install(RT_SCHEDULE_IPI_IRQ, rt_scheduler_ipi_handler, RT_NULL, "ipi"); + rt_hw_interrupt_umask(RT_SCHEDULE_IPI_IRQ); + + rt_system_scheduler_start(); +} + +void rt_hw_secondary_cpu_idle_exec(void) +{ + asm volatile ("wfe":::"memory", "cc"); +} + +#endif diff --git a/bsp/qemu-vexpress-a9/link.lds b/bsp/qemu-vexpress-a9/link.lds index 51b2ced922..78c38cde11 100644 --- a/bsp/qemu-vexpress-a9/link.lds +++ b/bsp/qemu-vexpress-a9/link.lds @@ -2,7 +2,7 @@ OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-littlearm") OUTPUT_ARCH(arm) SECTIONS { - . = 0x60000000; + . = 0x60010000; __text_start = .; .text : diff --git a/bsp/qemu-vexpress-a9/qemu-nographic.sh b/bsp/qemu-vexpress-a9/qemu-nographic.sh index 61abb6878d..b4168b8d5c 100755 --- a/bsp/qemu-vexpress-a9/qemu-nographic.sh +++ b/bsp/qemu-vexpress-a9/qemu-nographic.sh @@ -2,5 +2,5 @@ if [ ! -f "sd.bin" ]; then dd if=/dev/zero of=sd.bin bs=1024 count=65536 fi -qemu-system-arm -M vexpress-a9 -kernel rtthread.elf -nographic -sd sd.bin -net nic -net tap +qemu-system-arm -M vexpress-a9 -smp cpus=2 -kernel rtthread.bin -nographic -sd sd.bin -net nic -net tap diff --git a/bsp/qemu-vexpress-a9/qemu.bat b/bsp/qemu-vexpress-a9/qemu.bat index 4a4acb3324..460020c151 100644 --- a/bsp/qemu-vexpress-a9/qemu.bat +++ b/bsp/qemu-vexpress-a9/qemu.bat @@ -3,4 +3,4 @@ if exist sd.bin goto run qemu-img create -f raw sd.bin 64M :run -qemu-system-arm -M vexpress-a9 -kernel rtthread.elf -serial stdio -sd sd.bin +qemu-system-arm -M vexpress-a9 -smp cpus=2 -kernel rtthread.bin -serial stdio -sd sd.bin diff --git a/bsp/qemu-vexpress-a9/qemu.sh b/bsp/qemu-vexpress-a9/qemu.sh index c8fef610c7..ff2cec6f81 100755 --- a/bsp/qemu-vexpress-a9/qemu.sh +++ b/bsp/qemu-vexpress-a9/qemu.sh @@ -2,4 +2,4 @@ if [ ! -f "sd.bin" ]; then dd if=/dev/zero of=sd.bin bs=1024 count=65536 fi -qemu-system-arm -M vexpress-a9 -kernel rtthread.elf -serial stdio -sd sd.bin +qemu-system-arm -M vexpress-a9 -smp cpus=2 -kernel rtthread.bin -serial stdio -sd sd.bin diff --git a/bsp/qemu-vexpress-a9/rtconfig.h b/bsp/qemu-vexpress-a9/rtconfig.h index 02d54c452e..2ac692c692 100644 --- a/bsp/qemu-vexpress-a9/rtconfig.h +++ b/bsp/qemu-vexpress-a9/rtconfig.h @@ -7,18 +7,33 @@ /* RT-Thread Kernel */ #define RT_NAME_MAX 8 +#define RT_USING_SMP +#define RT_CPUS_NR 2 #define RT_ALIGN_SIZE 4 +/* RT_THREAD_PRIORITY_8 is not set */ #define RT_THREAD_PRIORITY_32 +/* RT_THREAD_PRIORITY_256 is not set */ #define RT_THREAD_PRIORITY_MAX 32 #define RT_TICK_PER_SECOND 100 #define RT_USING_OVERFLOW_CHECK #define RT_USING_HOOK +#define RT_USING_IDLE_HOOK #define RT_IDEL_HOOK_LIST_SIZE 4 #define IDLE_THREAD_STACK_SIZE 1024 #define RT_USING_TIMER_SOFT #define RT_TIMER_THREAD_PRIO 4 #define RT_TIMER_THREAD_STACK_SIZE 1024 #define RT_DEBUG +/* RT_DEBUG_INIT_CONFIG is not set */ +/* RT_DEBUG_THREAD_CONFIG is not set */ +/* RT_DEBUG_SCHEDULER_CONFIG is not set */ +/* RT_DEBUG_IPC_CONFIG is not set */ +/* RT_DEBUG_TIMER_CONFIG is not set */ +/* RT_DEBUG_IRQ_CONFIG is not set */ +/* RT_DEBUG_MEM_CONFIG is not set */ +/* RT_DEBUG_SLAB_CONFIG is not set */ +/* RT_DEBUG_MEMHEAP_CONFIG is not set */ +/* RT_DEBUG_MODULE_CONFIG is not set */ /* Inter-Thread communication */ @@ -33,13 +48,17 @@ #define RT_USING_MEMPOOL #define RT_USING_MEMHEAP +/* RT_USING_NOHEAP is not set */ #define RT_USING_SMALL_MEM +/* RT_USING_SLAB is not set */ +/* RT_USING_MEMHEAP_AS_HEAP is not set */ #define RT_USING_MEMTRACE #define RT_USING_HEAP /* Kernel Device Object */ #define RT_USING_DEVICE +/* RT_USING_DEVICE_OPS is not set */ #define RT_USING_INTERRUPT_INFO #define RT_USING_CONSOLE #define RT_CONSOLEBUF_SIZE 128 @@ -47,6 +66,7 @@ #define ARCH_ARM #define ARCH_ARM_CORTEX_A #define ARCH_ARM_CORTEX_A9 +/* ARCH_CPU_STACK_GROWS_UPWARD is not set */ /* RT-Thread Components */ @@ -67,11 +87,14 @@ #define FINSH_HISTORY_LINES 5 #define FINSH_USING_SYMTAB #define FINSH_USING_DESCRIPTION +/* FINSH_ECHO_DISABLE_DEFAULT is not set */ #define FINSH_THREAD_PRIORITY 20 #define FINSH_THREAD_STACK_SIZE 4096 #define FINSH_CMD_SIZE 80 +/* FINSH_USING_AUTH is not set */ #define FINSH_USING_MSH #define FINSH_USING_MSH_DEFAULT +/* FINSH_USING_MSH_ONLY is not set */ #define FINSH_ARG_MAX 10 /* Device virtual file system */ @@ -81,33 +104,48 @@ #define DFS_FILESYSTEMS_MAX 2 #define DFS_FILESYSTEM_TYPES_MAX 2 #define DFS_FD_MAX 16 +/* RT_USING_DFS_MNTTABLE is not set */ #define RT_USING_DFS_ELMFAT /* elm-chan's FatFs, Generic FAT Filesystem Module */ #define RT_DFS_ELM_CODE_PAGE 437 #define RT_DFS_ELM_WORD_ACCESS +/* RT_DFS_ELM_USE_LFN_0 is not set */ +/* RT_DFS_ELM_USE_LFN_1 is not set */ +/* RT_DFS_ELM_USE_LFN_2 is not set */ #define RT_DFS_ELM_USE_LFN_3 #define RT_DFS_ELM_USE_LFN 3 #define RT_DFS_ELM_MAX_LFN 255 #define RT_DFS_ELM_DRIVES 2 #define RT_DFS_ELM_MAX_SECTOR_SIZE 4096 +/* RT_DFS_ELM_USE_ERASE is not set */ #define RT_DFS_ELM_REENTRANT #define RT_USING_DFS_DEVFS #define RT_USING_DFS_ROMFS #define RT_USING_DFS_RAMFS +/* RT_USING_DFS_UFFS is not set */ +/* RT_USING_DFS_JFFS2 is not set */ +/* RT_USING_DFS_NFS is not set */ /* Device Drivers */ #define RT_USING_DEVICE_IPC #define RT_PIPE_BUFSZ 512 #define RT_USING_SERIAL +/* RT_USING_CAN is not set */ +/* RT_USING_HWTIMER is not set */ +/* RT_USING_CPUTIME is not set */ #define RT_USING_I2C #define RT_USING_I2C_BITOPS #define RT_USING_PIN +/* RT_USING_ADC is not set */ +/* RT_USING_PWM is not set */ #define RT_USING_MTD_NOR #define RT_USING_MTD_NAND #define RT_MTD_NAND_DEBUG +/* RT_USING_MTD is not set */ +/* RT_USING_PM is not set */ #define RT_USING_RTC #define RT_USING_SOFT_RTC #define RT_USING_SDIO @@ -116,18 +154,28 @@ #define RT_MMCSD_STACK_SIZE 1024 #define RT_MMCSD_THREAD_PREORITY 22 #define RT_MMCSD_MAX_PARTITION 16 +/* RT_SDIO_DEBUG is not set */ #define RT_USING_SPI #define RT_USING_SPI_MSD #define RT_USING_SFUD #define RT_SFUD_USING_SFDP #define RT_SFUD_USING_FLASH_INFO_TABLE +/* RT_DEBUG_SFUD is not set */ +/* RT_USING_W25QXX is not set */ +/* RT_USING_GD is not set */ +/* RT_USING_ENC28J60 is not set */ +/* RT_USING_SPI_WIFI is not set */ #define RT_USING_WDT +/* RT_USING_AUDIO is not set */ /* Using WiFi */ +/* RT_USING_WIFI is not set */ /* Using USB */ +/* RT_USING_USB_HOST is not set */ +/* RT_USING_USB_DEVICE is not set */ /* POSIX layer and C standard library */ @@ -137,6 +185,7 @@ #define RT_USING_POSIX_MMAP #define RT_USING_POSIX_TERMIOS #define RT_USING_POSIX_AIO +/* RT_USING_MODULE is not set */ /* Network */ @@ -153,9 +202,13 @@ /* light weight TCP/IP stack */ #define RT_USING_LWIP +/* RT_USING_LWIP141 is not set */ #define RT_USING_LWIP202 +/* RT_USING_LWIP210 is not set */ #define RT_USING_LWIP_IPV6 +/* RT_LWIP_IGMP is not set */ #define RT_LWIP_ICMP +/* RT_LWIP_SNMP is not set */ #define RT_LWIP_DNS #define RT_LWIP_DHCP #define IP_SOF_BROADCAST 1 @@ -168,6 +221,8 @@ #define RT_LWIP_MSKADDR "255.255.255.0" #define RT_LWIP_UDP #define RT_LWIP_TCP +/* RT_LWIP_RAW is not set */ +/* RT_LWIP_PPP is not set */ #define RT_MEMP_NUM_NETCONN 8 #define RT_LWIP_PBUF_NUM 16 #define RT_LWIP_RAW_PCB_NUM 4 @@ -179,6 +234,8 @@ #define RT_LWIP_TCPTHREAD_PRIORITY 10 #define RT_LWIP_TCPTHREAD_MBOX_SIZE 8 #define RT_LWIP_TCPTHREAD_STACKSIZE 1024 +/* LWIP_NO_RX_THREAD is not set */ +/* LWIP_NO_TX_THREAD is not set */ #define RT_LWIP_ETHTHREAD_PRIORITY 12 #define RT_LWIP_ETHTHREAD_STACKSIZE 1024 #define RT_LWIP_ETHTHREAD_MBOX_SIZE 8 @@ -188,70 +245,141 @@ #define LWIP_SO_RCVTIMEO 1 #define LWIP_SO_SNDTIMEO 1 #define LWIP_SO_RCVBUF 1 +/* RT_LWIP_NETIF_LOOPBACK is not set */ #define LWIP_NETIF_LOOPBACK 0 +/* RT_LWIP_STATS is not set */ +/* RT_LWIP_DEBUG is not set */ /* Modbus master and slave stack */ +/* RT_USING_MODBUS is not set */ /* AT commands */ +/* RT_USING_AT is not set */ +/* LWIP_USING_DHCPD is not set */ /* VBUS(Virtual Software BUS) */ +/* RT_USING_VBUS is not set */ /* Utilities */ #define RT_USING_LOGTRACE #define LOG_TRACE_MAX_SESSION 16 +/* LOG_TRACE_USING_LEVEL_NOTRACE is not set */ +/* LOG_TRACE_USING_LEVEL_ERROR is not set */ +/* LOG_TRACE_USING_LEVEL_WARNING is not set */ #define LOG_TRACE_USING_LEVEL_INFO +/* LOG_TRACE_USING_LEVEL_VERBOSE is not set */ +/* LOG_TRACE_USING_LEVEL_DEBUG is not set */ +/* LOG_TRACE_USING_MEMLOG is not set */ +/* RT_USING_RYM is not set */ +/* RT_USING_ULOG is not set */ /* RT-Thread online packages */ /* IoT - internet of things */ +/* PKG_USING_PAHOMQTT is not set */ +/* PKG_USING_WEBCLIENT is not set */ +/* PKG_USING_MONGOOSE is not set */ +/* PKG_USING_WEBTERMINAL is not set */ +/* PKG_USING_CJSON is not set */ +/* PKG_USING_JSMN is not set */ +/* PKG_USING_LJSON is not set */ +/* PKG_USING_EZXML is not set */ +/* PKG_USING_NANOPB is not set */ /* Wi-Fi */ /* Marvell WiFi */ +/* PKG_USING_WLANMARVELL is not set */ /* Wiced WiFi */ +/* PKG_USING_WLAN_WICED is not set */ +/* PKG_USING_COAP is not set */ +/* PKG_USING_NOPOLL is not set */ +/* PKG_USING_NETUTILS is not set */ +/* PKG_USING_AT_DEVICE is not set */ /* IoT Cloud */ +/* PKG_USING_ONENET is not set */ +/* PKG_USING_GAGENT_CLOUD is not set */ +/* PKG_USING_ALI_IOTKIT is not set */ +/* PKG_USING_AZURE is not set */ /* security packages */ +/* PKG_USING_MBEDTLS is not set */ +/* PKG_USING_libsodium is not set */ +/* PKG_USING_TINYCRYPT is not set */ /* language packages */ +/* PKG_USING_LUA is not set */ +/* PKG_USING_JERRYSCRIPT is not set */ +/* PKG_USING_MICROPYTHON is not set */ /* multimedia packages */ +/* PKG_USING_OPENMV is not set */ +/* PKG_USING_MUPDF is not set */ /* tools packages */ +/* PKG_USING_CMBACKTRACE is not set */ +/* PKG_USING_EASYFLASH is not set */ +/* PKG_USING_EASYLOGGER is not set */ +/* PKG_USING_SYSTEMVIEW is not set */ /* system packages */ +/* PKG_USING_GUIENGINE is not set */ +/* PKG_USING_PERSIMMON is not set */ +/* PKG_USING_CAIRO is not set */ +/* PKG_USING_PIXMAN is not set */ +/* PKG_USING_LWEXT4 is not set */ +/* PKG_USING_PARTITION is not set */ +/* PKG_USING_FAL is not set */ +/* PKG_USING_SQLITE is not set */ +/* PKG_USING_RTI is not set */ +/* PKG_USING_LITTLEVGL2RTT is not set */ /* peripheral libraries and drivers */ +/* PKG_USING_STM32F4_HAL is not set */ +/* PKG_USING_STM32F4_DRIVERS is not set */ +/* PKG_USING_REALTEK_AMEBA is not set */ +/* PKG_USING_SHT2X is not set */ +/* PKG_USING_AHT10 is not set */ /* miscellaneous packages */ +/* PKG_USING_LIBCSV is not set */ +/* PKG_USING_OPTPARSE is not set */ +/* PKG_USING_FASTLZ is not set */ +/* PKG_USING_MINILZO is not set */ +/* PKG_USING_QUICKLZ is not set */ +/* PKG_USING_MULTIBUTTON is not set */ +/* PKG_USING_CANFESTIVAL is not set */ +/* PKG_USING_ZLIB is not set */ +/* PKG_USING_DSTR is not set */ /* sample package */ -/* samples: kernel and components samples */ - +/* PKG_USING_SAMPLES is not set */ /* example package: hello */ +/* PKG_USING_HELLO is not set */ #define SOC_VEXPRESS_A9 #define RT_USING_UART0 #define RT_USING_UART1 #define BSP_DRV_EMAC +/* BSP_DRV_AUDIO is not set */ #endif diff --git a/bsp/qemu-vexpress-a9/rtconfig.py b/bsp/qemu-vexpress-a9/rtconfig.py index 545ba76af9..3f833d682d 100644 --- a/bsp/qemu-vexpress-a9/rtconfig.py +++ b/bsp/qemu-vexpress-a9/rtconfig.py @@ -32,7 +32,7 @@ if PLATFORM == 'gcc': DEVICE = ' -march=armv7-a -marm -msoft-float' CFLAGS = DEVICE + ' -Wall' - AFLAGS = ' -c' + DEVICE + ' -x assembler-with-cpp -D__ASSEMBLY__' + AFLAGS = ' -c' + DEVICE + ' -x assembler-with-cpp -D__ASSEMBLY__ -I.' LINK_SCRIPT = 'link.lds' LFLAGS = DEVICE + ' -nostartfiles -Wl,--gc-sections,-Map=rtthread.map,-cref,-u,system_vectors'+\ ' -T %s' % LINK_SCRIPT diff --git a/components/finsh/cmd.c b/components/finsh/cmd.c index 41e8a01f41..0c06303b14 100644 --- a/components/finsh/cmd.c +++ b/components/finsh/cmd.c @@ -25,8 +25,10 @@ * 2012-06-02 lgnq add list_memheap * 2012-10-22 Bernard add MS VC++ patch. * 2016-06-02 armink beautify the list_thread command + * 2018-11-22 Jesven list_thread add smp support */ +#include #include #ifdef RT_USING_FINSH @@ -87,14 +89,26 @@ static long _list_thread(struct rt_list_node *list) maxlen = object_name_maxlen(item_title, list); +#ifdef RT_USING_SMP + rt_kprintf("%-*.s cpu pri status sp stack size max used left tick error\n", maxlen, item_title); object_split(maxlen); + rt_kprintf( " --- --- ------- ---------- ---------- ------ ---------- ---\n"); +#else rt_kprintf("%-*.s pri status sp stack size max used left tick error\n", maxlen, item_title); object_split(maxlen); rt_kprintf( " --- ------- ---------- ---------- ------ ---------- ---\n"); +#endif /*RT_USING_SMP*/ for (node = list->next; node != list; node = node->next) { rt_uint8_t stat; thread = rt_list_entry(node, struct rt_thread, list); - rt_kprintf("%-*.*s %3d ", maxlen, RT_NAME_MAX, thread->name, thread->current_priority); +#ifdef RT_USING_SMP + if (thread->oncpu != RT_CPU_DETACHED) + rt_kprintf("%-*.*s %3d %3d ", maxlen, RT_NAME_MAX, thread->name, thread->oncpu, thread->current_priority); + else + rt_kprintf("%-*.*s N/A %3d ", maxlen, RT_NAME_MAX, thread->name, thread->current_priority); +#else + rt_kprintf("%-*.*s %3d ", maxlen, RT_NAME_MAX, thread->name, thread->current_priority); +#endif /*RT_USING_SMP*/ stat = (thread->stat & RT_THREAD_STAT_MASK); if (stat == RT_THREAD_READY) rt_kprintf(" ready "); else if (stat == RT_THREAD_SUSPEND) rt_kprintf(" suspend"); @@ -130,10 +144,15 @@ static long _list_thread(struct rt_list_node *list) long list_thread(void) { + rt_ubase_t level; struct rt_object_information *info; + long ret; + level = rt_hw_interrupt_disable(); info = rt_object_get_information(RT_Object_Class_Thread); - return _list_thread(&info->object_list); + ret = _list_thread(&info->object_list); + rt_hw_interrupt_enable(level); + return ret; } FINSH_FUNCTION_EXPORT(list_thread, list thread); MSH_CMD_EXPORT(list_thread, list thread); diff --git a/include/rtdef.h b/include/rtdef.h index fef08599c7..253ec36968 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -26,6 +26,9 @@ * 2018-09-14 Bernard apply Apache License v2.0 to RT-Thread Kernel * 2018-10-13 Bernard change version number to v4.0.0 * 2018-10-02 Bernard add 64bit arch support + * 2018-11-22 Jesven add smp member to struct rt_thread + * add struct rt_cpu + * add smp relevant macros */ #ifndef __RT_DEF_H__ @@ -130,11 +133,11 @@ typedef rt_base_t rt_off_t; /**< Type for offset */ #include #else /* the version of GNU GCC must be greater than 4.x */ - typedef __builtin_va_list __gnuc_va_list; - typedef __gnuc_va_list va_list; - #define va_start(v,l) __builtin_va_start(v,l) - #define va_end(v) __builtin_va_end(v) - #define va_arg(v,l) __builtin_va_arg(v,l) + typedef __builtin_va_list __gnuc_va_list; + typedef __gnuc_va_list va_list; + #define va_start(v,l) __builtin_va_start(v,l) + #define va_end(v) __builtin_va_end(v) + #define va_arg(v,l) __builtin_va_arg(v,l) #endif #define SECTION(x) __attribute__((section(x))) @@ -497,6 +500,41 @@ typedef siginfo_t rt_siginfo_t; #define RT_THREAD_CTRL_CLOSE 0x01 /**< Close thread. */ #define RT_THREAD_CTRL_CHANGE_PRIORITY 0x02 /**< Change thread priority. */ #define RT_THREAD_CTRL_INFO 0x03 /**< Get thread information. */ +#define RT_THREAD_CTRL_BIND_CPU 0x03 /**< Set thread bind cpu. */ + +#ifdef RT_USING_SMP + +#define RT_CPU_DETACHED RT_CPUS_NR /**< The thread not running on cpu. */ +#define RT_CPU_MASK ((1 << RT_CPUS_NR) - 1) /**< All CPUs mask bit. */ + +#ifndef RT_SCHEDULE_IPI_IRQ +#define RT_SCHEDULE_IPI_IRQ 0 +#endif + +/** + * CPUs definitions + * + */ +struct rt_cpu +{ + struct rt_thread *current_thread; + + rt_uint16_t irq_nest; + rt_uint8_t irq_switch_flag; + + rt_uint8_t current_priority; + rt_list_t priority_table[RT_THREAD_PRIORITY_MAX]; +#if RT_THREAD_PRIORITY_MAX > 32 + rt_uint32_t priority_group; + rt_uint8_t ready_table[32]; +#else + rt_uint32_t priority_group; +#endif + + rt_tick_t tick; +}; + +#endif /** * Thread structure @@ -527,6 +565,14 @@ struct rt_thread rt_uint8_t stat; /**< thread status */ +#ifdef RT_USING_SMP + rt_uint8_t bind_cpu; /**< thread is bind to cpu */ + rt_uint8_t oncpu; /**< process on cpu` */ + + rt_uint16_t scheduler_lock_nest; /**< scheduler lock count */ + rt_uint16_t cpus_lock_nest; /**< cpus lock count */ +#endif /*RT_USING_SMP*/ + /* priority */ rt_uint8_t current_priority; /**< current priority */ rt_uint8_t init_priority; /**< initialized priority */ diff --git a/include/rthw.h b/include/rthw.h index 17151de99a..3fa0ba301c 100644 --- a/include/rthw.h +++ b/include/rthw.h @@ -10,6 +10,8 @@ * 2006-09-24 Bernard add rt_hw_context_switch_to declaration * 2012-12-29 Bernard add rt_hw_exception_install declaration * 2017-10-17 Hichard add some micros + * 2018-11-17 Jesven add rt_hw_spinlock_t + * add smp support */ #ifndef __RT_HW_H__ @@ -92,15 +94,30 @@ rt_isr_handler_t rt_hw_interrupt_install(int vector, void *param, char *name); +#ifdef RT_USING_SMP +rt_base_t rt_hw_local_irq_disable(); +void rt_hw_local_irq_enable(rt_base_t level); + +#define rt_hw_interrupt_disable rt_cpus_lock +#define rt_hw_interrupt_enable rt_cpus_unlock + +#else rt_base_t rt_hw_interrupt_disable(void); void rt_hw_interrupt_enable(rt_base_t level); +#endif /*RT_USING_SMP*/ /* * Context interfaces */ +#ifdef RT_USING_SMP +void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to, struct rt_thread *to_thread); +void rt_hw_context_switch_to(rt_ubase_t to, struct rt_thread *to_thread); +void rt_hw_context_switch_interrupt(void *context, rt_ubase_t from, rt_ubase_t to, struct rt_thread *to_thread); +#else void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to); void rt_hw_context_switch_to(rt_ubase_t to); void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to); +#endif /*RT_USING_SMP*/ void rt_hw_console_output(const char *str); @@ -117,6 +134,47 @@ void rt_hw_exception_install(rt_err_t (*exception_handle)(void *context)); */ void rt_hw_us_delay(rt_uint32_t us); +#ifdef RT_USING_SMP +typedef union { + unsigned long slock; + struct __arch_tickets { + unsigned short owner; + unsigned short next; + } tickets; +} rt_hw_spinlock_t; + +void rt_hw_spin_lock(rt_hw_spinlock_t *lock); +void rt_hw_spin_unlock(rt_hw_spinlock_t *lock); + +int rt_hw_cpu_id(void); + +extern rt_hw_spinlock_t _cpus_lock; +extern rt_hw_spinlock_t _rt_critical_lock; + +#define __RT_HW_SPIN_LOCK_INITIALIZER(lockname) {0} + +#define __RT_HW_SPIN_LOCK_UNLOCKED(lockname) \ + (struct rt_hw_spinlock ) __RT_HW_SPIN_LOCK_INITIALIZER(lockname) + +#define RT_DEFINE_SPINLOCK(x) struct rt_hw_spinlock x = __RT_HW_SPIN_LOCK_UNLOCKED(x) + +/** + * ipi function + */ +void rt_hw_ipi_send(int ipi_vector, unsigned int cpu_mask); + +/** + * boot secondary cpu + */ +void rt_hw_secondary_cpu_up(void); + +/** + * secondary cpu idle function + */ +void rt_hw_secondary_cpu_idle_exec(void); + +#endif + #ifdef __cplusplus } #endif diff --git a/include/rtthread.h b/include/rtthread.h index 3fc77d343c..d2356b8eda 100644 --- a/include/rtthread.h +++ b/include/rtthread.h @@ -13,6 +13,7 @@ * 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. + * 2018-11-22 Jesven add all cpu's lock and ipi handler */ #ifndef __RT_THREAD_H__ @@ -184,6 +185,10 @@ rt_uint16_t rt_critical_level(void); void rt_scheduler_sethook(void (*hook)(rt_thread_t from, rt_thread_t to)); #endif +#ifdef RT_USING_SMP +void rt_scheduler_ipi_handler(int vector, void *param); +#endif + /**@}*/ /** @@ -439,6 +444,20 @@ rt_err_t rt_device_control(rt_device_t dev, int cmd, void *arg); void rt_interrupt_enter(void); void rt_interrupt_leave(void); +#ifdef RT_USING_SMP + +/* + * smp cpus lock service + */ + +rt_base_t rt_cpus_lock(void); +void rt_cpus_unlock(rt_base_t level); + +struct rt_cpu *rt_cpu_self(void); +struct rt_cpu *rt_cpu_index(int index); + +#endif + /* * the number of nested interrupts. */ diff --git a/src/Kconfig b/src/Kconfig index 78a3bf6d67..bf0bd5a3fd 100644 --- a/src/Kconfig +++ b/src/Kconfig @@ -8,6 +8,22 @@ config RT_NAME_MAX 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_USING_SMP + bool "Enable SMP(Symmetric multiprocessing)" + default n + help + This option should be selected by machines which have an SMP- + capable CPU. + The only effect of this option is to make the SMP-related + options available to the user for configuration. + +config RT_CPUS_NR + int "Number of CPUs" + default 2 + depends on RT_USING_SMP + help + Number of CPUs in the system + config RT_ALIGN_SIZE int "Alignment size for CPU architecture data access" default 4 diff --git a/src/clock.c b/src/clock.c index 651465e0a8..1bedb4e06a 100644 --- a/src/clock.c +++ b/src/clock.c @@ -12,12 +12,17 @@ * 2010-05-20 Bernard fix the tick exceeds the maximum limits * 2010-07-13 Bernard fix rt_tick_from_millisecond issue found by kuronca * 2011-06-26 Bernard add rt_tick_set function. + * 2018-11-22 Jesven add per cpu tick */ #include #include +#ifdef RT_USING_SMP +#define rt_tick rt_cpu_index(0)->tick +#else static rt_tick_t rt_tick = 0; +#endif extern void rt_timer_check(void); @@ -71,7 +76,11 @@ void rt_tick_increase(void) struct rt_thread *thread; /* increase the global tick */ +#ifdef RT_USING_SMP + rt_cpu_self()->tick ++; +#else ++ rt_tick; +#endif /* check time slice */ thread = rt_thread_self(); diff --git a/src/components.c b/src/components.c index 68e5799f59..d77ef8300f 100644 --- a/src/components.c +++ b/src/components.c @@ -14,6 +14,7 @@ * 2015-05-04 Bernard Rename it to components.c because compiling issue * in some IDEs. * 2015-07-29 Arda.Fu Add support to use RT_USING_USER_MAIN with IAR + * 2018-11-22 Jesven Add secondary cpu boot up */ #include @@ -180,6 +181,9 @@ void main_thread_entry(void *parameter) /* RT-Thread components initialization */ rt_components_init(); +#ifdef RT_USING_SMP + rt_hw_secondary_cpu_up(); +#endif /* invoke system main function */ #if defined(__CC_ARM) || defined(__CLANG_ARM) $Super$$main(); /* for ARMCC. */ @@ -243,6 +247,10 @@ int rtthread_startup(void) /* idle thread initialization */ rt_thread_idle_init(); +#ifdef RT_USING_SMP + rt_hw_spin_lock(&_cpus_lock); +#endif /*RT_USING_SMP*/ + /* start scheduler */ rt_system_scheduler_start(); diff --git a/src/cpu.c b/src/cpu.c new file mode 100644 index 0000000000..ef6dce1787 --- /dev/null +++ b/src/cpu.c @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-10-30 Bernard The first version + */ + +#include +#include + +#ifdef RT_USING_SMP + +static struct rt_cpu rt_cpus[RT_CPUS_NR]; +rt_hw_spinlock_t _cpus_lock; + +/** + * This fucntion will return current cpu. + */ +struct rt_cpu *rt_cpu_self(void) +{ + return &rt_cpus[rt_hw_cpu_id()]; +} + +struct rt_cpu *rt_cpu_index(int index) +{ + return &rt_cpus[index]; +} + +/** + * This function will lock all cpus's scheduler and disable local irq. + */ +rt_base_t rt_cpus_lock(void) +{ + rt_base_t level; + struct rt_cpu* pcpu; + + level = rt_hw_local_irq_disable(); + + pcpu = rt_cpu_self(); + if (pcpu->current_thread != RT_NULL) + { + if (pcpu->current_thread->cpus_lock_nest++ == 0) + { + pcpu->current_thread->scheduler_lock_nest++; + rt_hw_spin_lock(&_cpus_lock); + } + } + return level; +} +RTM_EXPORT(rt_cpus_lock); + +/** + * This function will restore all cpus's scheduler and restore local irq. + */ +void rt_cpus_unlock(rt_base_t level) +{ + struct rt_cpu* pcpu = rt_cpu_self(); + + if (pcpu->current_thread != RT_NULL) + { + if (--pcpu->current_thread->cpus_lock_nest == 0) + { + pcpu->current_thread->scheduler_lock_nest--; + rt_hw_spin_unlock(&_cpus_lock); + } + } + rt_hw_local_irq_enable(level); +} +RTM_EXPORT(rt_cpus_unlock); + +/** + * This function is invoked by scheduler. + * It will restore the lock state to whatever the thread's counter expects. + * If target thread not locked the cpus then unlock the cpus lock. + */ +void rt_cpus_lock_status_restore(struct rt_thread *thread) +{ + struct rt_cpu* pcpu = rt_cpu_self(); + + pcpu->current_thread = thread; + if (!thread->cpus_lock_nest) + { + rt_hw_spin_unlock(&_cpus_lock); + } +} +RTM_EXPORT(rt_post_switch); + +#endif diff --git a/src/idle.c b/src/idle.c index ec7d6bea6e..7d985e3816 100644 --- a/src/idle.c +++ b/src/idle.c @@ -13,6 +13,8 @@ * 2016-08-09 ArdaFu add method to get the handler of the idle thread. * 2018-02-07 Bernard lock scheduler to protect tid->cleanup. * 2018-07-14 armink add idle hook list + * 2018-11-22 Jesven add per cpu idle task + * combine the code of primary and secondary cpu */ #include @@ -28,6 +30,11 @@ #endif #endif +#ifdef RT_USING_IDLE_HOOK +#ifndef RT_IDEL_HOOK_LIST_SIZE +#define RT_IDEL_HOOK_LIST_SIZE 4 +#endif + #ifndef IDLE_THREAD_STACK_SIZE #if defined (RT_USING_IDLE_HOOK) || defined(RT_USING_HEAP) #define IDLE_THREAD_STACK_SIZE 256 @@ -36,18 +43,17 @@ #endif #endif -static struct rt_thread idle; -ALIGN(RT_ALIGN_SIZE) -static rt_uint8_t rt_thread_stack[IDLE_THREAD_STACK_SIZE]; +#ifdef RT_USING_SMP +#define _CPUS_NR RT_CPUS_NR +#else +#define _CPUS_NR 1 +#endif extern rt_list_t rt_thread_defunct; -#ifdef RT_USING_IDLE_HOOK - -#ifndef RT_IDEL_HOOK_LIST_SIZE -#define RT_IDEL_HOOK_LIST_SIZE 4 -#endif - +static struct rt_thread idle[_CPUS_NR]; +ALIGN(RT_ALIGN_SIZE) +static rt_uint8_t rt_thread_stack[_CPUS_NR][IDLE_THREAD_STACK_SIZE]; static void (*idle_hook_list[RT_IDEL_HOOK_LIST_SIZE])(); /** @@ -221,14 +227,21 @@ void rt_thread_idle_excute(void) static void rt_thread_idle_entry(void *parameter) { -#ifdef RT_USING_IDLE_HOOK - rt_size_t i; +#ifdef RT_USING_SMP + if (rt_hw_cpu_id() != 0) + { + while (1) + { + rt_hw_secondary_cpu_idle_exec(); + } + } #endif while (1) { - #ifdef RT_USING_IDLE_HOOK + rt_size_t i; + for (i = 0; i < RT_IDEL_HOOK_LIST_SIZE; i++) { if (idle_hook_list[i] != RT_NULL) @@ -251,18 +264,26 @@ static void rt_thread_idle_entry(void *parameter) */ void rt_thread_idle_init(void) { - /* initialize thread */ - rt_thread_init(&idle, - "tidle", - rt_thread_idle_entry, - RT_NULL, - &rt_thread_stack[0], - sizeof(rt_thread_stack), - RT_THREAD_PRIORITY_MAX - 1, - 32); - - /* startup */ - rt_thread_startup(&idle); + int i; + char tidle_name[RT_NAME_MAX]; + + for (i = 0; i < _CPUS_NR; i++) + { + rt_sprintf(tidle_name, "tidle%d", i); + rt_thread_init(&idle[i], + tidle_name, + rt_thread_idle_entry, + RT_NULL, + &rt_thread_stack[i][0], + sizeof(rt_thread_stack[i]), + RT_THREAD_PRIORITY_MAX - 1, + 32); +#ifdef RT_USING_SMP + rt_thread_control(&idle[i], RT_THREAD_CTRL_BIND_CPU, (void*)i); +#endif + /* startup */ + rt_thread_startup(&idle[i]); + } } /** @@ -273,5 +294,11 @@ void rt_thread_idle_init(void) */ rt_thread_t rt_thread_idle_gethandler(void) { - return (rt_thread_t)(&idle); +#ifdef RT_USING_SMP + register int id = rt_hw_cpu_id(); +#else + register int id = 0; +#endif + + return (rt_thread_t)(&idle[id]); } diff --git a/src/irq.c b/src/irq.c index 72d220bd92..ee03204819 100644 --- a/src/irq.c +++ b/src/irq.c @@ -8,6 +8,7 @@ * 2006-02-24 Bernard first version * 2006-05-03 Bernard add IRQ_DEBUG * 2016-08-09 ArdaFu add interrupt enter and leave hook. + * 2018-11-22 Jesven rt_interrupt_get_nest function add disable irq */ #include @@ -48,7 +49,11 @@ void rt_interrupt_leave_sethook(void (*hook)(void)) /**@{*/ +#ifdef RT_USING_SMP +#define rt_interrupt_nest rt_cpu_self()->irq_nest +#else volatile rt_uint8_t rt_interrupt_nest; +#endif /** * This function will be invoked by BSP, when enter interrupt service routine @@ -102,7 +107,13 @@ RTM_EXPORT(rt_interrupt_leave); */ rt_uint8_t rt_interrupt_get_nest(void) { - return rt_interrupt_nest; + rt_uint8_t ret; + rt_base_t level; + + level = rt_hw_interrupt_disable(); + ret = rt_interrupt_nest; + rt_hw_interrupt_enable(level); + return ret; } RTM_EXPORT(rt_interrupt_get_nest); diff --git a/src/scheduler.c b/src/scheduler.c index fd068a6671..e177bde6b7 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -21,28 +21,36 @@ * 2010-12-13 Bernard add defunct list initialization even if not use heap. * 2011-05-10 Bernard clean scheduler debug log. * 2013-12-21 Grissiom add rt_critical_level + * 2018-11-22 Jesven remove the current task from ready queue + * add per cpu ready queue + * add _get_highest_priority_thread to find highest priority task + * rt_schedule_insert_thread won't insert current task to ready queue + * in smp version, rt_hw_context_switch_interrupt maybe switch to + * new task directly + * */ #include #include -static rt_int16_t rt_scheduler_lock_nest; -extern volatile rt_uint8_t rt_interrupt_nest; +#ifdef RT_USING_SMP +rt_hw_spinlock_t _rt_critical_lock; +#endif /*RT_USING_SMP*/ rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX]; -struct rt_thread *rt_current_thread; - -rt_uint8_t rt_current_priority; - +rt_uint32_t rt_thread_ready_priority_group; #if RT_THREAD_PRIORITY_MAX > 32 /* Maximum priority level, 256 */ -rt_uint32_t rt_thread_ready_priority_group; rt_uint8_t rt_thread_ready_table[32]; -#else -/* Maximum priority level, 32 */ -rt_uint32_t rt_thread_ready_priority_group; #endif +#ifndef RT_USING_SMP +extern volatile rt_uint8_t rt_interrupt_nest; +static rt_int16_t rt_scheduler_lock_nest; +struct rt_thread *rt_current_thread; +rt_uint8_t rt_current_priority; +#endif /*RT_USING_SMP*/ + rt_list_t rt_thread_defunct; #ifdef RT_USING_HOOK @@ -91,19 +99,84 @@ static void _rt_scheduler_stack_check(struct rt_thread *thread) level = rt_hw_interrupt_disable(); while (level); } -#if defined(ARCH_CPU_STACK_GROWS_UPWARD) - else if ((rt_ubase_t)thread->sp > ((rt_ubase_t)thread->stack_addr + thread->stack_size)) + else if ((rt_ubase_t)thread->sp <= ((rt_ubase_t)thread->stack_addr + 32)) { - rt_kprintf("warning: %s stack is close to the top of stack address.\n", + rt_kprintf("warning: %s stack is close to end of stack address.\n", thread->name); } +} +#endif + +/* + * get the highest priority thread in ready queue + */ +#ifdef RT_USING_SMP +static struct rt_thread* _get_highest_priority_thread(rt_ubase_t *highest_prio) +{ + register struct rt_thread *highest_priority_thread; + register rt_ubase_t highest_ready_priority, local_highest_ready_priority; + struct rt_cpu* pcpu = rt_cpu_self(); + +#if RT_THREAD_PRIORITY_MAX > 32 + register rt_ubase_t number; + + if (rt_thread_ready_priority_group == 0 && pcpu->priority_group == 0) + { + *highest_prio = pcpu->current_thread->current_priority; + /* only local IDLE is readly */ + return pcpu->current_thread; + } + + number = __rt_ffs(rt_thread_ready_priority_group) - 1; + highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1; + number = __rt_ffs(pcpu->priority_group) - 1; + local_highest_ready_priority = (number << 3) + __rt_ffs(pcpu->ready_table[number]) - 1; #else - else if ((rt_ubase_t)thread->sp <= ((rt_ubase_t)thread->stack_addr + 32)) + highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1; + local_highest_ready_priority = __rt_ffs(pcpu->priority_group) - 1; +#endif + + /* get highest ready priority thread */ + if (highest_ready_priority < local_highest_ready_priority) { - rt_kprintf("warning: %s stack is close to the bottom of stack address.\n", - thread->name); + *highest_prio = highest_ready_priority; + highest_priority_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next, + struct rt_thread, + tlist); + } + else + { + *highest_prio = local_highest_ready_priority; + highest_priority_thread = rt_list_entry(pcpu->priority_table[local_highest_ready_priority].next, + struct rt_thread, + tlist); } + + return highest_priority_thread; +} +#else +static struct rt_thread* _get_highest_priority_thread(rt_ubase_t *highest_prio) +{ + register struct rt_thread *highest_priority_thread; + register rt_ubase_t highest_ready_priority; + +#if RT_THREAD_PRIORITY_MAX > 32 + register rt_ubase_t number; + + number = __rt_ffs(rt_thread_ready_priority_group) - 1; + highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1; +#else + highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1; #endif + + /* get highest ready priority thread */ + highest_priority_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next, + struct rt_thread, + tlist); + + *highest_prio = highest_ready_priority; + + return highest_priority_thread; } #endif @@ -113,9 +186,14 @@ static void _rt_scheduler_stack_check(struct rt_thread *thread) */ void rt_system_scheduler_init(void) { +#ifdef RT_USING_SMP + int cpu; +#endif /*RT_USING_SMP*/ register rt_base_t offset; +#ifndef RT_USING_SMP rt_scheduler_lock_nest = 0; +#endif /*RT_USING_SMP*/ RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("start scheduler: max priority 0x%02x\n", RT_THREAD_PRIORITY_MAX)); @@ -124,9 +202,25 @@ void rt_system_scheduler_init(void) { rt_list_init(&rt_thread_priority_table[offset]); } +#ifdef RT_USING_SMP + for (cpu = 0; cpu < RT_CPUS_NR; cpu++) + { + struct rt_cpu *pcpu = rt_cpu_index(cpu); + for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++) + { + rt_list_init(&pcpu->priority_table[offset]); + } - rt_current_priority = RT_THREAD_PRIORITY_MAX - 1; - rt_current_thread = RT_NULL; + pcpu->irq_switch_flag = 0; + pcpu->current_priority = RT_THREAD_PRIORITY_MAX - 1; + pcpu->current_thread = RT_NULL; + pcpu->priority_group = 0; + +#if RT_THREAD_PRIORITY_MAX > 32 + rt_memset(pcpu->ready_table, 0, sizeof(pcpu->ready_table)); +#endif + } +#endif /*RT_USING_SMP*/ /* initialize ready priority group */ rt_thread_ready_priority_group = 0; @@ -148,26 +242,24 @@ void rt_system_scheduler_init(void) void rt_system_scheduler_start(void) { register struct rt_thread *to_thread; - register rt_ubase_t highest_ready_priority; + rt_ubase_t highest_ready_priority; -#if RT_THREAD_PRIORITY_MAX > 32 - register rt_ubase_t number; + to_thread = _get_highest_priority_thread(&highest_ready_priority); - number = __rt_ffs(rt_thread_ready_priority_group) - 1; - highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1; +#ifdef RT_USING_SMP + to_thread->oncpu = rt_hw_cpu_id(); #else - highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1; -#endif - - /* get switch to thread */ - to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next, - struct rt_thread, - tlist); - rt_current_thread = to_thread; +#endif /*RT_USING_SMP*/ + + rt_schedule_remove_thread(to_thread); /* switch to new thread */ - rt_hw_context_switch_to((rt_ubase_t)&to_thread->sp); +#ifdef RT_USING_SMP + rt_hw_context_switch_to((rt_uint32_t)&to_thread->sp, to_thread); +#else + rt_hw_context_switch_to((rt_uint32_t)&to_thread->sp); +#endif /*RT_USING_SMP*/ /* never come back */ } @@ -178,6 +270,114 @@ void rt_system_scheduler_start(void) /**@{*/ + +#ifdef RT_USING_SMP +/** + * This function will handle IPI interrupt and do a scheduling in system; + * + * @param vector, the number of IPI interrupt for system scheduling + * @param param, use RT_NULL + * + * NOTE: this function should be invoke or register as ISR in BSP. + */ +void rt_scheduler_ipi_handler(int vector, void *param) +{ + rt_schedule(); +} + +/** + * This function will perform one scheduling. It will select one thread + * with the highest priority level in global ready queue or local ready queue, + * then switch to it. + */ +void rt_schedule(void) +{ + rt_base_t level; + struct rt_thread *to_thread; + struct rt_thread *current_thread; + struct rt_cpu *pcpu; + int cpu_id; + + /* disable interrupt */ + level = rt_hw_interrupt_disable(); + + cpu_id = rt_hw_cpu_id(); + pcpu = rt_cpu_index(cpu_id); + current_thread = pcpu->current_thread; + + /* whether do switch in interrupt */ + if (pcpu->irq_nest) + { + pcpu->irq_switch_flag = 1; + } + else if (current_thread->scheduler_lock_nest == 1) /* whether lock scheduler */ + { + rt_ubase_t highest_ready_priority; + + if (rt_thread_ready_priority_group != 0 || pcpu->priority_group != 0) + { + to_thread = _get_highest_priority_thread(&highest_ready_priority); + current_thread->oncpu = RT_CPU_DETACHED; + if ((current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY) + { + if (current_thread->current_priority < highest_ready_priority) + { + to_thread = current_thread; + } + else + { + rt_schedule_insert_thread(current_thread); + } + } + to_thread->oncpu = cpu_id; + if (to_thread != current_thread) + { + /* if the destination thread is not the same as current thread */ + pcpu->current_priority = (rt_uint8_t)highest_ready_priority; + + RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (current_thread, to_thread)); + + rt_schedule_remove_thread(to_thread); + + /* switch to new thread */ + RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, + ("[%d]switch to priority#%d " + "thread:%.*s(sp:0x%08x), " + "from thread:%.*s(sp: 0x%08x)\n", + pcpu->irq_nest, highest_ready_priority, + RT_NAME_MAX, to_thread->name, to_thread->sp, + RT_NAME_MAX, current_thread->name, current_thread->sp)); + +#ifdef RT_USING_OVERFLOW_CHECK + _rt_scheduler_stack_check(to_thread); +#endif + + { + extern void rt_thread_handle_sig(rt_bool_t clean_state); + + rt_hw_context_switch((rt_ubase_t)¤t_thread->sp, + (rt_ubase_t)&to_thread->sp, to_thread); + + /* enable interrupt */ + rt_hw_interrupt_enable(level); + +#ifdef RT_USING_SIGNALS + /* check signal status */ + rt_thread_handle_sig(RT_TRUE); +#endif + goto __exit; + } + } + } + } + + /* enable interrupt */ + rt_hw_interrupt_enable(level); + +__exit: + return ; +} +#else /** * This function will perform one schedule. It will select one thread * with the highest priority level, then switch to it. @@ -194,81 +394,169 @@ void rt_schedule(void) /* check the scheduler is enabled or not */ if (rt_scheduler_lock_nest == 0) { - register rt_ubase_t highest_ready_priority; + rt_ubase_t highest_ready_priority; -#if RT_THREAD_PRIORITY_MAX <= 32 - highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1; -#else - register rt_ubase_t number; + if (rt_thread_ready_priority_group != 0) + { + int need_insert_from_thread = 0; - number = __rt_ffs(rt_thread_ready_priority_group) - 1; - highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1; -#endif + to_thread = _get_highest_priority_thread(&highest_ready_priority); - /* get switch to thread */ - to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next, - struct rt_thread, - tlist); + if ((rt_current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY) + { + if (rt_current_thread->current_priority < highest_ready_priority) + { + to_thread = rt_current_thread; + } + else + { + need_insert_from_thread = 1; + } + } - /* if the destination thread is not the same as current thread */ - if (to_thread != rt_current_thread) - { - rt_current_priority = (rt_uint8_t)highest_ready_priority; - from_thread = rt_current_thread; - rt_current_thread = to_thread; + if (to_thread != rt_current_thread) + { + /* if the destination thread is not the same as current thread */ + rt_current_priority = (rt_uint8_t)highest_ready_priority; + from_thread = rt_current_thread; + rt_current_thread = to_thread; + + RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread)); - RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread)); + if (need_insert_from_thread) + { + rt_schedule_insert_thread(from_thread); + } - /* switch to new thread */ - RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, - ("[%d]switch to priority#%d " - "thread:%.*s(sp:0x%08x), " - "from thread:%.*s(sp: 0x%08x)\n", - rt_interrupt_nest, highest_ready_priority, - RT_NAME_MAX, to_thread->name, to_thread->sp, - RT_NAME_MAX, from_thread->name, from_thread->sp)); + rt_schedule_remove_thread(to_thread); + + /* switch to new thread */ + RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, + ("[%d]switch to priority#%d " + "thread:%.*s(sp:0x%p), " + "from thread:%.*s(sp: 0x%p)\n", + rt_interrupt_nest, highest_ready_priority, + RT_NAME_MAX, to_thread->name, to_thread->sp, + RT_NAME_MAX, from_thread->name, from_thread->sp)); #ifdef RT_USING_OVERFLOW_CHECK - _rt_scheduler_stack_check(to_thread); + _rt_scheduler_stack_check(to_thread); #endif - if (rt_interrupt_nest == 0) - { - extern void rt_thread_handle_sig(rt_bool_t clean_state); + if (rt_interrupt_nest == 0) + { + extern void rt_thread_handle_sig(rt_bool_t clean_state); - rt_hw_context_switch((rt_ubase_t)&from_thread->sp, - (rt_ubase_t)&to_thread->sp); + rt_hw_context_switch((rt_ubase_t)&from_thread->sp, + (rt_ubase_t)&to_thread->sp); - /* enable interrupt */ - rt_hw_interrupt_enable(level); + /* enable interrupt */ + rt_hw_interrupt_enable(level); #ifdef RT_USING_SIGNALS - /* check signal status */ - rt_thread_handle_sig(RT_TRUE); + /* check signal status */ + rt_thread_handle_sig(RT_TRUE); #endif + goto __exit; + } + else + { + RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n")); + + rt_hw_context_switch_interrupt((rt_ubase_t)&from_thread->sp, + (rt_ubase_t)&to_thread->sp); + } } else { - RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n")); - - rt_hw_context_switch_interrupt((rt_ubase_t)&from_thread->sp, - (rt_ubase_t)&to_thread->sp); - /* enable interrupt */ - rt_hw_interrupt_enable(level); + rt_schedule_remove_thread(rt_current_thread); } } - else - { - /* enable interrupt */ - rt_hw_interrupt_enable(level); - } } - else + + /* enable interrupt */ + rt_hw_interrupt_enable(level); + +__exit: + return; +} +#endif /*RT_USING_SMP*/ + +/** + * This function checks if a scheduling is needed after IRQ context. If yes, + * it will select one thread with the highest priority level, and then switch + * to it. + */ +#ifdef RT_USING_SMP +void rt_scheduler_do_irq_switch(void *context) +{ + int cpu_id; + rt_base_t level; + struct rt_cpu* pcpu; + struct rt_thread *to_thread; + struct rt_thread *current_thread; + + level = rt_hw_interrupt_disable(); + + cpu_id = rt_hw_cpu_id(); + pcpu = rt_cpu_index(cpu_id); + current_thread = pcpu->current_thread; + + if (pcpu->irq_switch_flag == 0) { - /* enable interrupt */ rt_hw_interrupt_enable(level); + return; + } + + if (current_thread->scheduler_lock_nest == 1 && pcpu->irq_nest == 0) + { + rt_ubase_t highest_ready_priority; + + /* clear irq switch flag */ + pcpu->irq_switch_flag = 0; + + if (rt_thread_ready_priority_group != 0 || pcpu->priority_group != 0) + { + to_thread = _get_highest_priority_thread(&highest_ready_priority); + current_thread->oncpu = RT_CPU_DETACHED; + if ((current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY) + { + if (current_thread->current_priority < highest_ready_priority) + { + to_thread = current_thread; + } + else + { + rt_schedule_insert_thread(current_thread); + } + } + to_thread->oncpu = cpu_id; + if (to_thread != current_thread) + { + /* if the destination thread is not the same as current thread */ + + pcpu->current_priority = (rt_uint8_t)highest_ready_priority; + + RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (current_thread, to_thread)); + + rt_schedule_remove_thread(to_thread); + +#ifdef RT_USING_OVERFLOW_CHECK + _rt_scheduler_stack_check(to_thread); +#endif + RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n")); + + current_thread->cpus_lock_nest--; + current_thread->scheduler_lock_nest--; + + rt_hw_context_switch_interrupt(context, (rt_ubase_t)¤t_thread->sp, + (rt_ubase_t)&to_thread->sp, to_thread); + } + } } + rt_hw_interrupt_enable(level); } +#endif /*RT_USING_SMP*/ /* * This function will insert a thread to system ready queue. The state of @@ -277,6 +565,70 @@ void rt_schedule(void) * @param thread the thread to be inserted * @note Please do not invoke this function in user application. */ +#ifdef RT_USING_SMP +void rt_schedule_insert_thread(struct rt_thread *thread) +{ + int cpu_id; + int bind_cpu; + rt_uint32_t cpu_mask; + register rt_base_t level; + + RT_ASSERT(thread != RT_NULL); + + /* disable interrupt */ + level = rt_hw_interrupt_disable(); + + /* change stat */ + thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK); + + if (thread->oncpu != RT_CPU_DETACHED) + { + goto __exit; + } + + cpu_id = rt_hw_cpu_id(); + bind_cpu = thread->bind_cpu ; + + /* insert thread to ready list */ + if (bind_cpu == RT_CPUS_NR) + { +#if RT_THREAD_PRIORITY_MAX > 32 + rt_thread_ready_table[thread->number] |= thread->high_mask; +#endif + rt_thread_ready_priority_group |= thread->number_mask; + + rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]), + &(thread->tlist)); + cpu_mask = RT_CPU_MASK ^ (1 << cpu_id); + rt_hw_ipi_send(RT_SCHEDULE_IPI_IRQ, cpu_mask); + } + else + { + struct rt_cpu *pcpu = rt_cpu_index(bind_cpu); + +#if RT_THREAD_PRIORITY_MAX > 32 + pcpu->ready_table[thread->number] |= thread->high_mask; +#endif + pcpu->priority_group |= thread->number_mask; + + rt_list_insert_before(&(rt_cpu_index(bind_cpu)->priority_table[thread->current_priority]), + &(thread->tlist)); + + if (cpu_id != bind_cpu) + { + cpu_mask = 1 << bind_cpu; + rt_hw_ipi_send(RT_SCHEDULE_IPI_IRQ, cpu_mask); + } + } + + RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("insert thread[%.*s], the priority: %d\n", + RT_NAME_MAX, thread->name, thread->current_priority)); + +__exit: + /* enable interrupt */ + rt_hw_interrupt_enable(level); +} +#else void rt_schedule_insert_thread(struct rt_thread *thread) { register rt_base_t temp; @@ -289,32 +641,29 @@ void rt_schedule_insert_thread(struct rt_thread *thread) /* change stat */ thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK); + if (thread == rt_current_thread) + { + goto __exit; + } + /* insert thread to ready list */ rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]), &(thread->tlist)); - /* set priority mask */ -#if RT_THREAD_PRIORITY_MAX <= 32 RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("insert thread[%.*s], the priority: %d\n", RT_NAME_MAX, thread->name, thread->current_priority)); -#else - RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, - ("insert thread[%.*s], the priority: %d 0x%x %d\n", - RT_NAME_MAX, - thread->name, - thread->number, - thread->number_mask, - thread->high_mask)); -#endif + /* set priority mask */ #if RT_THREAD_PRIORITY_MAX > 32 rt_thread_ready_table[thread->number] |= thread->high_mask; #endif rt_thread_ready_priority_group |= thread->number_mask; +__exit: /* enable interrupt */ rt_hw_interrupt_enable(temp); } +#endif /*RT_USING_SMP*/ /* * This function will remove a thread from system ready queue. @@ -323,28 +672,71 @@ void rt_schedule_insert_thread(struct rt_thread *thread) * * @note Please do not invoke this function in user application. */ +#ifdef RT_USING_SMP void rt_schedule_remove_thread(struct rt_thread *thread) { - register rt_base_t temp; + register rt_base_t level; RT_ASSERT(thread != RT_NULL); /* disable interrupt */ - temp = rt_hw_interrupt_disable(); + level = rt_hw_interrupt_disable(); -#if RT_THREAD_PRIORITY_MAX <= 32 RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("remove thread[%.*s], the priority: %d\n", RT_NAME_MAX, thread->name, thread->current_priority)); + + /* remove thread from ready list */ + rt_list_remove(&(thread->tlist)); + if (thread->bind_cpu == RT_CPUS_NR) + { + if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority]))) + { +#if RT_THREAD_PRIORITY_MAX > 32 + rt_thread_ready_table[thread->number] &= ~thread->high_mask; + if (rt_thread_ready_table[thread->number] == 0) + { + rt_thread_ready_priority_group &= ~thread->number_mask; + } +#else + rt_thread_ready_priority_group &= ~thread->number_mask; +#endif + } + } + else + { + struct rt_cpu *pcpu = rt_cpu_index(thread->bind_cpu); + + if (rt_list_isempty(&(pcpu->priority_table[thread->current_priority]))) + { +#if RT_THREAD_PRIORITY_MAX > 32 + pcpu->ready_table[thread->number] &= ~thread->high_mask; + if (rt_thread_ready_table[thread->number] == 0) + { + pcpu->priority_group &= ~thread->number_mask; + } #else - RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, - ("remove thread[%.*s], the priority: %d 0x%x %d\n", - RT_NAME_MAX, - thread->name, - thread->number, - thread->number_mask, - thread->high_mask)); + pcpu->priority_group &= ~thread->number_mask; #endif + } + } + + /* enable interrupt */ + rt_hw_interrupt_enable(level); +} +#else +void rt_schedule_remove_thread(struct rt_thread *thread) +{ + register rt_base_t level; + + RT_ASSERT(thread != RT_NULL); + + /* disable interrupt */ + level = rt_hw_interrupt_disable(); + + RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("remove thread[%.*s], the priority: %d\n", + RT_NAME_MAX, thread->name, + thread->current_priority)); /* remove thread from ready list */ rt_list_remove(&(thread->tlist)); @@ -362,12 +754,41 @@ void rt_schedule_remove_thread(struct rt_thread *thread) } /* enable interrupt */ - rt_hw_interrupt_enable(temp); + rt_hw_interrupt_enable(level); } +#endif /*RT_USING_SMP*/ /** * This function will lock the thread scheduler. */ +#ifdef RT_USING_SMP +void rt_enter_critical(void) +{ + register rt_base_t level; + struct rt_thread *current_thread; + + /* disable interrupt */ + level = rt_hw_local_irq_disable(); + + current_thread = rt_cpu_self()->current_thread; + /* + * the maximal number of nest is RT_UINT16_MAX, which is big + * enough and does not check here + */ + + /* lock scheduler for all cpus */ + if (current_thread->scheduler_lock_nest == !!current_thread->cpus_lock_nest) + { + rt_hw_spin_lock(&_rt_critical_lock); + } + + /* lock scheduler for local cpu */ + current_thread->scheduler_lock_nest ++; + + /* enable interrupt */ + rt_hw_local_irq_enable(level); +} +#else void rt_enter_critical(void) { register rt_base_t level; @@ -384,11 +805,45 @@ void rt_enter_critical(void) /* enable interrupt */ rt_hw_interrupt_enable(level); } +#endif /*RT_USING_SMP*/ RTM_EXPORT(rt_enter_critical); /** * This function will unlock the thread scheduler. */ +#ifdef RT_USING_SMP +void rt_exit_critical(void) +{ + register rt_base_t level; + struct rt_thread *current_thread; + + /* disable interrupt */ + level = rt_hw_local_irq_disable(); + + current_thread = rt_cpu_self()->current_thread; + + current_thread->scheduler_lock_nest --; + + if (current_thread->scheduler_lock_nest == !!current_thread->cpus_lock_nest) + { + rt_hw_spin_unlock(&_rt_critical_lock); + } + + if (current_thread->scheduler_lock_nest <= 0) + { + current_thread->scheduler_lock_nest = 0; + /* enable interrupt */ + rt_hw_local_irq_enable(level); + + rt_schedule(); + } + else + { + /* enable interrupt */ + rt_hw_local_irq_enable(level); + } +} +#else void rt_exit_critical(void) { register rt_base_t level; @@ -412,6 +867,7 @@ void rt_exit_critical(void) rt_hw_interrupt_enable(level); } } +#endif /*RT_USING_SMP*/ RTM_EXPORT(rt_exit_critical); /** @@ -421,8 +877,14 @@ RTM_EXPORT(rt_exit_critical); */ rt_uint16_t rt_critical_level(void) { - return rt_scheduler_lock_nest; +#ifdef RT_USING_SMP + struct rt_thread *current_thread = rt_cpu_self()->current_thread; + + return current_thread->scheduler_lock_nest; +#else + return rt_scheduler_lock_nest; +#endif /*RT_USING_SMP*/ } RTM_EXPORT(rt_critical_level); -/**@}*/ +/**@}*/ diff --git a/src/signal.c b/src/signal.c index 890f16f497..05632b2c4a 100644 --- a/src/signal.c +++ b/src/signal.c @@ -6,6 +6,8 @@ * Change Logs: * Date Author Notes * 2017/10/5 Bernard the first version + * 2018/09/17 Jesven fix: in _signal_deliver RT_THREAD_STAT_MASK to RT_THREAD_STAT_SIGNAL_MASK + * 2018/11/22 Jesven in smp version rt_hw_context_switch_to add a param */ #include @@ -60,10 +62,14 @@ static void _signal_entry(void *parameter) tid->sp = tid->sig_ret; tid->sig_ret = RT_NULL; - LOG_D("switch back to: 0x%08x", tid->sp); + LOG_D("switch back to: 0x%08x\n", tid->sp); tid->stat &= ~RT_THREAD_STAT_SIGNAL; - rt_hw_context_switch_to((rt_uint32_t) & (tid->sp)); +#ifdef RT_USING_SMP + rt_hw_context_switch_to((rt_ubase_t)&(tid->sp), tid); +#else + rt_hw_context_switch_to((rt_ubase_t)&(tid->sp)); +#endif /*RT_USING_SMP*/ } /* @@ -108,7 +114,7 @@ static void _signal_deliver(rt_thread_t tid) /* do signal action in self thread context */ rt_thread_handle_sig(RT_TRUE); } - else if (!((tid->stat & RT_THREAD_STAT_MASK) & RT_THREAD_STAT_SIGNAL)) + else if (!((tid->stat & RT_THREAD_STAT_SIGNAL_MASK) & RT_THREAD_STAT_SIGNAL)) { /* add signal state */ tid->stat |= RT_THREAD_STAT_SIGNAL; diff --git a/src/thread.c b/src/thread.c index 07c7619c6f..df9f151834 100644 --- a/src/thread.c +++ b/src/thread.c @@ -23,18 +23,17 @@ * 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. + * bug when thread has not startup. + * 2018-11-22 Jesven yield is same to rt_schedule + * add support for tasks bound to cpu */ -#include #include +#include -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; #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); @@ -84,7 +83,7 @@ void rt_thread_exit(void) register rt_base_t level; /* get current thread */ - thread = rt_current_thread; + thread = rt_thread_self(); /* disable interrupt */ level = rt_hw_interrupt_disable(); @@ -165,6 +164,16 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread, thread->error = RT_EOK; thread->stat = RT_THREAD_INIT; +#ifdef RT_USING_SMP + /* not bind on any cpu */ + thread->bind_cpu = RT_CPUS_NR; + thread->oncpu = RT_CPU_DETACHED; + + /* lock init */ + thread->scheduler_lock_nest = 0; + thread->cpus_lock_nest = 0; +#endif /*RT_USING_SMP*/ + /* initialize cleanup function and user data */ thread->cleanup = 0; thread->user_data = 0; @@ -251,7 +260,19 @@ RTM_EXPORT(rt_thread_init); */ rt_thread_t rt_thread_self(void) { +#ifdef RT_USING_SMP + rt_base_t lock; + rt_thread_t self; + + lock = rt_hw_local_irq_disable(); + self = rt_cpu_self()->current_thread; + rt_hw_local_irq_enable(lock); + return self; +#else + extern rt_thread_t rt_current_thread; + return rt_current_thread; +#endif } RTM_EXPORT(rt_thread_self); @@ -449,36 +470,7 @@ RTM_EXPORT(rt_thread_delete); */ rt_err_t rt_thread_yield(void) { - register rt_base_t level; - struct rt_thread *thread; - - /* disable interrupt */ - level = rt_hw_interrupt_disable(); - - /* set to current thread */ - thread = rt_current_thread; - - /* if the thread stat is READY and on ready queue list */ - if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY && - thread->tlist.next != thread->tlist.prev) - { - /* remove thread from thread list */ - rt_list_remove(&(thread->tlist)); - - /* put thread to end of ready queue */ - rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]), - &(thread->tlist)); - - /* enable interrupt */ - rt_hw_interrupt_enable(level); - - rt_schedule(); - - return RT_EOK; - } - - /* enable interrupt */ - rt_hw_interrupt_enable(level); + rt_schedule(); return RT_EOK; } @@ -496,13 +488,14 @@ rt_err_t rt_thread_sleep(rt_tick_t tick) register rt_base_t temp; struct rt_thread *thread; - /* disable interrupt */ - temp = rt_hw_interrupt_disable(); /* set to current thread */ - thread = rt_current_thread; + thread = rt_thread_self(); RT_ASSERT(thread != RT_NULL); RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread); + /* disable interrupt */ + temp = rt_hw_interrupt_disable(); + /* suspend thread */ rt_thread_suspend(thread); @@ -559,7 +552,8 @@ RTM_EXPORT(rt_thread_mdelay); * @param cmd the control command, which includes * RT_THREAD_CTRL_CHANGE_PRIORITY for changing priority level of thread; * RT_THREAD_CTRL_STARTUP for starting a thread; - * RT_THREAD_CTRL_CLOSE for delete a thread. + * RT_THREAD_CTRL_CLOSE for delete a thread; + * RT_THREAD_CTRL_BIND_CPU for bind the thread to a CPU. * @param arg the argument of control command * * @return RT_EOK @@ -625,6 +619,22 @@ rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg) return rt_thread_delete(thread); #endif +#ifdef RT_USING_SMP + case RT_THREAD_CTRL_BIND_CPU: + { + rt_uint8_t cpu; + + if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT) + { + /* we only support bind cpu before started phase. */ + return RT_ERROR; + } + + cpu = (rt_uint8_t)(size_t)arg; + thread->bind_cpu = cpu > RT_CPUS_NR? RT_CPUS_NR : cpu; + break; + } +#endif /*RT_USING_SMP*/ default: break; } @@ -661,6 +671,8 @@ rt_err_t rt_thread_suspend(rt_thread_t thread) return -RT_ERROR; } + RT_ASSERT(thread == rt_thread_self()); + /* disable interrupt */ temp = rt_hw_interrupt_disable(); -- cgit v1.3.1 From b3e1507bcff974167a34ca78382ac85b5e66b890 Mon Sep 17 00:00:00 2001 From: shaojinchun Date: Wed, 12 Dec 2018 09:36:39 +0800 Subject: 1 RT_THREAD_CTRL_BIND_CPU define to 4 2 RTM_EXPORT(rt_cpus_lock_status_restore) 3 sync ARCH_CPU_STACK_GROWS_UPWARD in _rt_scheduler_stack_check() --- include/rtdef.h | 2 +- src/cpu.c | 2 +- src/scheduler.c | 8 ++++++++ 3 files changed, 10 insertions(+), 2 deletions(-) (limited to 'include/rtdef.h') diff --git a/include/rtdef.h b/include/rtdef.h index 253ec36968..6d5263ef62 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -500,7 +500,7 @@ typedef siginfo_t rt_siginfo_t; #define RT_THREAD_CTRL_CLOSE 0x01 /**< Close thread. */ #define RT_THREAD_CTRL_CHANGE_PRIORITY 0x02 /**< Change thread priority. */ #define RT_THREAD_CTRL_INFO 0x03 /**< Get thread information. */ -#define RT_THREAD_CTRL_BIND_CPU 0x03 /**< Set thread bind cpu. */ +#define RT_THREAD_CTRL_BIND_CPU 0x04 /**< Set thread bind cpu. */ #ifdef RT_USING_SMP diff --git a/src/cpu.c b/src/cpu.c index ef6dce1787..3e2d65dfb9 100644 --- a/src/cpu.c +++ b/src/cpu.c @@ -86,6 +86,6 @@ void rt_cpus_lock_status_restore(struct rt_thread *thread) rt_hw_spin_unlock(&_cpus_lock); } } -RTM_EXPORT(rt_post_switch); +RTM_EXPORT(rt_cpus_lock_status_restore); #endif diff --git a/src/scheduler.c b/src/scheduler.c index e177bde6b7..aafa1f4529 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -99,11 +99,19 @@ static void _rt_scheduler_stack_check(struct rt_thread *thread) level = rt_hw_interrupt_disable(); while (level); } +#if defined(ARCH_CPU_STACK_GROWS_UPWARD) + else if ((rt_ubase_t)thread->sp > ((rt_ubase_t)thread->stack_addr + thread->stack_size)) + { + rt_kprintf("warning: %s stack is close to the top of stack address.\n", + thread->name); + } +#else else if ((rt_ubase_t)thread->sp <= ((rt_ubase_t)thread->stack_addr + 32)) { rt_kprintf("warning: %s stack is close to end of stack address.\n", thread->name); } +#endif } #endif -- cgit v1.3.1 From eff767a0742b25116f4d0ac766107d4a21ed19cb Mon Sep 17 00:00:00 2001 From: shaojinchun Date: Mon, 17 Dec 2018 10:21:40 +0800 Subject: remove rt_hw_mb define sync smp skeleton --- bsp/qemu-vexpress-a9/cpu/cpu.c | 51 --------------------- bsp/qemu-vexpress-a9/cpu/cpuport.c | 89 ++++++++++++++++++++++++++++++++++++ bsp/qemu-vexpress-a9/drivers/board.c | 54 ++-------------------- include/rtdef.h | 4 +- include/rthw.h | 2 - src/scheduler.c | 4 +- 6 files changed, 97 insertions(+), 107 deletions(-) delete mode 100644 bsp/qemu-vexpress-a9/cpu/cpu.c create mode 100644 bsp/qemu-vexpress-a9/cpu/cpuport.c (limited to 'include/rtdef.h') diff --git a/bsp/qemu-vexpress-a9/cpu/cpu.c b/bsp/qemu-vexpress-a9/cpu/cpu.c deleted file mode 100644 index 10099809a3..0000000000 --- a/bsp/qemu-vexpress-a9/cpu/cpu.c +++ /dev/null @@ -1,51 +0,0 @@ -/* - * File : cpu.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://www.rt-thread.org/license/LICENSE - * - * Change Logs: - * Date Author Notes - * 2011-09-15 Bernard first version - * 2018-11-22 Jesven add rt_hw_cpu_id() - */ - -#include -#include -#include - -#ifdef RT_USING_SMP -int rt_hw_cpu_id(void) -{ - int cpu_id; - __asm__ volatile ( - "mrc p15, 0, %0, c0, c0, 5" - :"=r"(cpu_id) - ); - cpu_id &= 0xf; - return cpu_id; -}; -#endif - -/** - * @addtogroup ARM CPU - */ -/*@{*/ - -/** shutdown CPU */ -void rt_hw_cpu_shutdown() -{ - rt_uint32_t level; - rt_kprintf("shutdown...\n"); - - level = rt_hw_interrupt_disable(); - while (level) - { - RT_ASSERT(0); - } -} - -/*@}*/ diff --git a/bsp/qemu-vexpress-a9/cpu/cpuport.c b/bsp/qemu-vexpress-a9/cpu/cpuport.c new file mode 100644 index 0000000000..22ba8346da --- /dev/null +++ b/bsp/qemu-vexpress-a9/cpu/cpuport.c @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2011-09-15 Bernard first version + * 2018-11-22 Jesven add rt_hw_cpu_id() + */ + +#include +#include +#include + +#ifdef RT_USING_SMP +int rt_hw_cpu_id(void) +{ + int cpu_id; + __asm__ volatile ( + "mrc p15, 0, %0, c0, c0, 5" + :"=r"(cpu_id) + ); + cpu_id &= 0xf; + return cpu_id; +}; + +void rt_hw_spin_lock(rt_hw_spinlock_t *lock) +{ + unsigned long tmp; + unsigned long newval; + rt_hw_spinlock_t lockval; + + __asm__ __volatile__( + "pld [%0]" + ::"r"(&lock->slock) + ); + + __asm__ __volatile__( + "1: ldrex %0, [%3]\n" + " add %1, %0, %4\n" + " strex %2, %1, [%3]\n" + " teq %2, #0\n" + " bne 1b" + : "=&r" (lockval), "=&r" (newval), "=&r" (tmp) + : "r" (&lock->slock), "I" (1 << 16) + : "cc"); + + while (lockval.tickets.next != lockval.tickets.owner) { + __asm__ __volatile__("wfe":::"memory"); + lockval.tickets.owner = *(volatile unsigned short *)(&lock->tickets.owner); + } + + __asm__ volatile ("dmb":::"memory"); +} + +void rt_hw_spin_unlock(rt_hw_spinlock_t *lock) +{ + __asm__ volatile ("dmb":::"memory"); + lock->tickets.owner++; + __asm__ volatile ("dsb ishst\nsev":::"memory"); +} + +#endif /*RT_USING_SMP*/ + +void idle_wfi(void) +{ + asm volatile ("wfi"); +} + +/** + * @addtogroup ARM CPU + */ +/*@{*/ + +/** shutdown CPU */ +void rt_hw_cpu_shutdown() +{ + rt_uint32_t level; + rt_kprintf("shutdown...\n"); + + level = rt_hw_interrupt_disable(); + while (level) + { + RT_ASSERT(0); + } +} + +/*@}*/ diff --git a/bsp/qemu-vexpress-a9/drivers/board.c b/bsp/qemu-vexpress-a9/drivers/board.c index 7daa881c35..26445d33f1 100644 --- a/bsp/qemu-vexpress-a9/drivers/board.c +++ b/bsp/qemu-vexpress-a9/drivers/board.c @@ -19,10 +19,8 @@ #define SYS_CTRL __REG32(REALVIEW_SCTL_BASE) -void idle_wfi(void) -{ - asm volatile ("wfi"); -} +extern void idle_wfi(void); +extern void rt_hw_ipi_handler_install(int ipi_vector, rt_isr_handler_t ipi_isr_handler); /** * This function will initialize beaglebone board @@ -40,52 +38,8 @@ void rt_hw_board_init(void) rt_thread_idle_sethook(idle_wfi); #ifdef RT_USING_SMP - /* install IPI interrupt */ - rt_hw_interrupt_install(RT_SCHEDULE_IPI_IRQ, rt_scheduler_ipi_handler, RT_NULL, "ipi"); - rt_hw_interrupt_umask(RT_SCHEDULE_IPI_IRQ); + /* install IPI handle */ + rt_hw_ipi_handler_install(RT_SCHEDULE_IPI, rt_scheduler_ipi_handler); #endif } -#ifdef RT_USING_SMP -void rt_hw_spin_lock(rt_hw_spinlock_t *lock) -{ - unsigned long tmp; - unsigned long newval; - rt_hw_spinlock_t lockval; - - __asm__ __volatile__( - "pld [%0]" - ::"r"(&lock->slock) - ); - - __asm__ __volatile__( - "1: ldrex %0, [%3]\n" - " add %1, %0, %4\n" - " strex %2, %1, [%3]\n" - " teq %2, #0\n" - " bne 1b" - : "=&r" (lockval), "=&r" (newval), "=&r" (tmp) - : "r" (&lock->slock), "I" (1 << 16) - : "cc"); - - while (lockval.tickets.next != lockval.tickets.owner) { - __asm__ __volatile__("wfe":::"memory"); - lockval.tickets.owner = *(volatile unsigned short *)(&lock->tickets.owner); - } - - __asm__ volatile ("dmb":::"memory"); -} - -void rt_hw_spin_unlock(rt_hw_spinlock_t *lock) -{ - __asm__ volatile ("dmb":::"memory"); - lock->tickets.owner++; - __asm__ volatile ("dsb ishst\nsev":::"memory"); -} - -void rt_hw_mb(void) -{ - __asm__ volatile ("dmb":::"memory"); -} - -#endif /*RT_USING_SMP*/ diff --git a/include/rtdef.h b/include/rtdef.h index 253ec36968..8ae4e2ad4f 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -507,8 +507,8 @@ typedef siginfo_t rt_siginfo_t; #define RT_CPU_DETACHED RT_CPUS_NR /**< The thread not running on cpu. */ #define RT_CPU_MASK ((1 << RT_CPUS_NR) - 1) /**< All CPUs mask bit. */ -#ifndef RT_SCHEDULE_IPI_IRQ -#define RT_SCHEDULE_IPI_IRQ 0 +#ifndef RT_SCHEDULE_IPI +#define RT_SCHEDULE_IPI 0 #endif /** diff --git a/include/rthw.h b/include/rthw.h index 8c731e6c53..dee10b0bee 100644 --- a/include/rthw.h +++ b/include/rthw.h @@ -146,8 +146,6 @@ typedef union { void rt_hw_spin_lock(rt_hw_spinlock_t *lock); void rt_hw_spin_unlock(rt_hw_spinlock_t *lock); -void rt_hw_mb(void); - int rt_hw_cpu_id(void); extern rt_hw_spinlock_t _cpus_lock; diff --git a/src/scheduler.c b/src/scheduler.c index e177bde6b7..f2f77203f8 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -600,7 +600,7 @@ void rt_schedule_insert_thread(struct rt_thread *thread) rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]), &(thread->tlist)); cpu_mask = RT_CPU_MASK ^ (1 << cpu_id); - rt_hw_ipi_send(RT_SCHEDULE_IPI_IRQ, cpu_mask); + rt_hw_ipi_send(RT_SCHEDULE_IPI, cpu_mask); } else { @@ -617,7 +617,7 @@ void rt_schedule_insert_thread(struct rt_thread *thread) if (cpu_id != bind_cpu) { cpu_mask = 1 << bind_cpu; - rt_hw_ipi_send(RT_SCHEDULE_IPI_IRQ, cpu_mask); + rt_hw_ipi_send(RT_SCHEDULE_IPI, cpu_mask); } } -- cgit v1.3.1 From e716bbfa22a735f5496c9a63f3e84f993bbc9c2f Mon Sep 17 00:00:00 2001 From: Owen Date: Wed, 26 Dec 2018 10:13:26 +0800 Subject: Update rtdef.h MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更正为正确的Doxygen组定义语法 --- include/rtdef.h | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'include/rtdef.h') diff --git a/include/rtdef.h b/include/rtdef.h index 49551de9ee..f4949c2dff 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -45,7 +45,7 @@ extern "C" { * @addtogroup BasicDef */ -/*@{*/ +/**@{*/ /* RT-Thread version information */ #define RT_VERSION 4L /**< major version number */ @@ -88,7 +88,7 @@ typedef rt_base_t rt_off_t; /**< Type for offset */ #define RT_TRUE 1 /**< boolean true */ #define RT_FALSE 0 /**< boolean fails */ -/*@}*/ +/**@}*/ /* maximum value of base type */ #define RT_UINT8_MAX 0xff /**< Maxium number of UINT8 */ @@ -259,7 +259,7 @@ typedef int (*init_fn_t)(void); * @addtogroup Error */ -/*@{*/ +/**@{*/ /* RT-Thread error code definitions */ #define RT_EOK 0 /**< There is no error */ @@ -274,7 +274,7 @@ typedef int (*init_fn_t)(void); #define RT_EINTR 9 /**< Interrupted system call */ #define RT_EINVAL 10 /**< Invalid argument */ -/*@}*/ +/**@}*/ /** * @ingroup BasicDef @@ -325,7 +325,7 @@ typedef struct rt_slist_node rt_slist_t; /**< Type for single lis * @addtogroup KernelObject */ -/*@{*/ +/**@{*/ /* * kernel object macros @@ -402,13 +402,13 @@ struct rt_object_information #define RT_OBJECT_HOOK_CALL(func, argv) #endif -/*@}*/ +/**@}*/ /** * @addtogroup Clock */ -/*@{*/ +/**@{*/ /** * clock & timer macros @@ -452,7 +452,7 @@ struct rt_timer }; typedef struct rt_timer *rt_timer_t; -/*@}*/ +/**@}*/ /** * @addtogroup Signal @@ -465,13 +465,13 @@ typedef siginfo_t rt_siginfo_t; #define RT_SIG_MAX 32 #endif -/*@}*/ +/**@}*/ /** * @addtogroup Thread */ -/*@{*/ +/**@{*/ /* * Thread @@ -613,13 +613,13 @@ struct rt_thread }; typedef struct rt_thread *rt_thread_t; -/*@}*/ +/**@}*/ /** * @addtogroup IPC */ -/*@{*/ +/**@{*/ /** * IPC flags and control command definitions @@ -737,13 +737,13 @@ struct rt_messagequeue typedef struct rt_messagequeue *rt_mq_t; #endif -/*@}*/ +/**@}*/ /** * @addtogroup MM */ -/*@{*/ +/**@{*/ /* * memory management @@ -811,14 +811,14 @@ struct rt_mempool typedef struct rt_mempool *rt_mp_t; #endif -/*@}*/ +/**@}*/ #ifdef RT_USING_DEVICE /** * @addtogroup Device */ -/*@{*/ +/**@{*/ /** * device (I/O) class type @@ -1063,7 +1063,7 @@ struct rt_device_graphic_ops }; #define rt_graphix_ops(device) ((struct rt_device_graphic_ops *)(device->user_data)) -/*@}*/ +/**@}*/ #endif /* definitions for libc */ -- cgit v1.3.1 From ed9a558da979035c38fac68d715bfa3ea553822d Mon Sep 17 00:00:00 2001 From: shaojinchun Date: Thu, 27 Dec 2018 16:31:22 +0800 Subject: list_*时,提供对第一层object的原子保护 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/finsh/cmd.c | 946 ++++++++++++++++++++++++++++++------------------- include/rtdef.h | 3 +- 2 files changed, 574 insertions(+), 375 deletions(-) (limited to 'include/rtdef.h') diff --git a/components/finsh/cmd.c b/components/finsh/cmd.c index 1adefea652..aa6ee6c2fc 100644 --- a/components/finsh/cmd.c +++ b/components/finsh/cmd.c @@ -26,6 +26,8 @@ * 2012-10-22 Bernard add MS VC++ patch. * 2016-06-02 armink beautify the list_thread command * 2018-11-22 Jesven list_thread add smp support + * 2018-12-27 Jesven Fix the problem that disable interrupt too long in list_thread + * Provide protection for the "first layer of objects" when list_* */ #include @@ -35,6 +37,8 @@ #include "finsh.h" +#define LIST_FIND_OBJ_NR 8 + long hello(void) { rt_kprintf("Hello RT-Thread!\n"); @@ -53,45 +57,51 @@ long version(void) FINSH_FUNCTION_EXPORT(version, show RT-Thread version information); MSH_CMD_EXPORT(version, show RT-Thread version information); -static int object_name_maxlen(const char *type_name, struct rt_list_node *list) +rt_inline void object_split(int len) { - struct rt_list_node *node; - struct rt_object *object = NULL; - int max_length = rt_strlen(type_name), length; - - rt_enter_critical(); - for (node = list->next; node != list; node = node->next) - { - object = rt_list_entry(node, struct rt_object, list); - - length = rt_strlen(object->name); - if (length > max_length) max_length = length; - } - rt_exit_critical(); - - if (max_length > RT_NAME_MAX || max_length == 0) max_length = RT_NAME_MAX; - - return max_length; + while (len--) rt_kprintf("-"); } -rt_inline void object_split(int len) +typedef struct { - while (len--) rt_kprintf("-"); + rt_list_t *list; + rt_list_t **array; + rt_uint8_t type; + int nr; /* input: max nr, can't be 0 */ + int nr_out; /* out: got nr */ +} list_get_next_t; + +static void list_find_init(list_get_next_t *p, rt_uint8_t type, rt_list_t **array, int nr) +{ + struct rt_object_information *info; + rt_list_t *list; + + info = rt_object_get_information(type); + list = &info->object_list; + + p->list = list; + p->type = type; + p->array = array; + p->nr = nr; + p->nr_out = 0; } -static rt_list_t *list_thread_next(rt_list_t *current, int *max_name_len) +static rt_list_t *list_get_next(rt_list_t *current, list_get_next_t *arg) { int first_flag = 0; rt_ubase_t level; - struct rt_object_information *info; - rt_list_t *node, *iter, *list; - struct rt_thread *thread; - const char *item_title = "thread"; - int maxlen; - struct rt_thread thread_info; + rt_list_t *node, *list; + rt_list_t **array; + int nr; - info = rt_object_get_information(RT_Object_Class_Thread); - list = &info->object_list; + arg->nr_out = 0; + + if (!arg->nr || !arg->type) + { + return (rt_list_t *)RT_NULL; + } + + list = arg->list; if (!current) /* find first */ { @@ -105,112 +115,132 @@ static rt_list_t *list_thread_next(rt_list_t *current, int *max_name_len) level = rt_hw_interrupt_disable(); - if (first_flag) - { - /* is find first */ - maxlen = object_name_maxlen(item_title, list); - *max_name_len = maxlen; - } - else + if (!first_flag) { + struct rt_object *obj; /* The node in the list? */ - for (iter = list->next; iter != list; iter = iter->next) - { - if (iter == node) - { - break; - } - } - if (iter == list) + obj = rt_list_entry(node, struct rt_object, list); + if ((obj->type & ~RT_Object_Class_Static) != arg->type) { - /* not found , error! */ rt_hw_interrupt_enable(level); return (rt_list_t *)RT_NULL; } - maxlen = *max_name_len; } - node = node->next; - if (node == list) + nr = 0; + array = arg->array; + while (1) { - /* At the end of list */ - rt_hw_interrupt_enable(level); - return (rt_list_t *)RT_NULL; + node = node->next; + + if (node == list) + { + node = (rt_list_t *)RT_NULL; + break; + } + nr++; + *array++ = node; + if (nr == arg->nr) + { + break; + } } + + rt_hw_interrupt_enable(level); + arg->nr_out = nr; + return node; +} - /* ok, found a thread */ - thread = rt_list_entry(node, struct rt_thread, list); - /* copy info */ - memcpy(&thread_info, thread, sizeof thread_info); +long list_thread(void) +{ + rt_ubase_t level; + list_get_next_t find_arg; + rt_list_t *obj_list[LIST_FIND_OBJ_NR]; + rt_list_t *next = (rt_list_t*)RT_NULL; + const char *item_title = "thread"; + int maxlen; - rt_hw_interrupt_enable(level); + list_find_init(&find_arg, RT_Object_Class_Thread, obj_list, sizeof(obj_list)/sizeof(obj_list[0])); - /* now output thread info */ - { - rt_uint8_t stat; - rt_uint8_t *ptr; + maxlen = RT_NAME_MAX; - if (first_flag) - { #ifdef RT_USING_SMP - rt_kprintf("%-*.s cpu pri status sp stack size max used left tick error\n", maxlen, item_title); object_split(maxlen); - rt_kprintf( " --- --- ------- ---------- ---------- ------ ---------- ---\n"); + rt_kprintf("%-*.s cpu pri status sp stack size max used left tick error\n", maxlen, item_title); object_split(maxlen); + rt_kprintf( " --- --- ------- ---------- ---------- ------ ---------- ---\n"); #else - rt_kprintf("%-*.s pri status sp stack size max used left tick error\n", maxlen, item_title); object_split(maxlen); - rt_kprintf( " --- ------- ---------- ---------- ------ ---------- ---\n"); + rt_kprintf("%-*.s pri status sp stack size max used left tick error\n", maxlen, item_title); object_split(maxlen); + rt_kprintf( " --- ------- ---------- ---------- ------ ---------- ---\n"); #endif /*RT_USING_SMP*/ - } + + do + { + next = list_get_next(next, &find_arg); + { + int i; + for (i = 0; i < find_arg.nr_out; i++) + { + struct rt_object *obj; + struct rt_thread thread_info, *thread; + + obj = rt_list_entry(obj_list[i], struct rt_object, list); + level = rt_hw_interrupt_disable(); + + if ((obj->type & ~RT_Object_Class_Static) != find_arg.type) + { + rt_hw_interrupt_enable(level); + continue; + } + /* copy info */ + memcpy(&thread_info, obj, sizeof thread_info); + rt_hw_interrupt_enable(level); + + thread = (struct rt_thread*)obj; + { + rt_uint8_t stat; + rt_uint8_t *ptr; + #ifdef RT_USING_SMP - if (thread_info.oncpu != RT_CPU_DETACHED) - rt_kprintf("%-*.*s %3d %3d ", maxlen, RT_NAME_MAX, thread_info.name, thread_info.oncpu, thread_info.current_priority); - else - rt_kprintf("%-*.*s N/A %3d ", maxlen, RT_NAME_MAX, thread_info.name, thread_info.current_priority); + if (thread->oncpu != RT_CPU_DETACHED) + rt_kprintf("%-*.*s %3d %3d ", maxlen, RT_NAME_MAX, thread->name, thread->oncpu, thread->current_priority); + else + rt_kprintf("%-*.*s N/A %3d ", maxlen, RT_NAME_MAX, thread->name, thread->current_priority); #else - rt_kprintf("%-*.*s %3d ", maxlen, RT_NAME_MAX, thread_info.name, thread_info.current_priority); + rt_kprintf("%-*.*s %3d ", maxlen, RT_NAME_MAX, thread->name, thread->current_priority); #endif /*RT_USING_SMP*/ - stat = (thread_info.stat & RT_THREAD_STAT_MASK); - if (stat == RT_THREAD_READY) rt_kprintf(" ready "); - else if (stat == RT_THREAD_SUSPEND) rt_kprintf(" suspend"); - else if (stat == RT_THREAD_INIT) rt_kprintf(" init "); - else if (stat == RT_THREAD_CLOSE) rt_kprintf(" close "); + stat = (thread->stat & RT_THREAD_STAT_MASK); + if (stat == RT_THREAD_READY) rt_kprintf(" ready "); + else if (stat == RT_THREAD_SUSPEND) rt_kprintf(" suspend"); + else if (stat == RT_THREAD_INIT) rt_kprintf(" init "); + else if (stat == RT_THREAD_CLOSE) rt_kprintf(" close "); #if defined(ARCH_CPU_STACK_GROWS_UPWARD) - ptr = (rt_uint8_t *)thread_info.stack_addr + thread_info.stack_size - 1; - while (*ptr == '#')ptr --; - - rt_kprintf(" 0x%08x 0x%08x %02d%% 0x%08x %03d\n", - ((rt_ubase_t)thread_info.sp - (rt_ubase_t)thread_info.stack_addr), - thread_info.stack_size, - ((rt_ubase_t)ptr - (rt_ubase_t)thread_info.stack_addr) * 100 / thread_info.stack_size, - thread_info.remaining_tick, - thread_info.error); + ptr = (rt_uint8_t *)thread->stack_addr + thread->stack_size - 1; + while (*ptr == '#')ptr --; + + rt_kprintf(" 0x%08x 0x%08x %02d%% 0x%08x %03d\n", + ((rt_ubase_t)thread->sp - (rt_ubase_t)thread->stack_addr), + thread->stack_size, + ((rt_ubase_t)ptr - (rt_ubase_t)thread->stack_addr) * 100 / thread->stack_size, + thread->remaining_tick, + thread->error); #else - ptr = (rt_uint8_t *)thread_info.stack_addr; - while (*ptr == '#')ptr ++; - - rt_kprintf(" 0x%08x 0x%08x %02d%% 0x%08x %03d\n", - thread_info.stack_size + ((rt_ubase_t)thread_info.stack_addr - (rt_ubase_t)thread_info.sp), - thread_info.stack_size, - (thread_info.stack_size - ((rt_ubase_t) ptr - (rt_ubase_t) thread_info.stack_addr)) * 100 - / thread_info.stack_size, - thread_info.remaining_tick, - thread_info.error); + ptr = (rt_uint8_t *)thread->stack_addr; + while (*ptr == '#')ptr ++; + + rt_kprintf(" 0x%08x 0x%08x %02d%% 0x%08x %03d\n", + thread->stack_size + ((rt_ubase_t)thread->stack_addr - (rt_ubase_t)thread->sp), + thread->stack_size, + (thread->stack_size - ((rt_ubase_t) ptr - (rt_ubase_t) thread->stack_addr)) * 100 + / thread->stack_size, + thread->remaining_tick, + thread->error); #endif + } + } + } } - return node; -} - -long list_thread(void) -{ - int maxlen; - rt_list_t *thread_list = (rt_list_t*)RT_NULL; - - do - { - thread_list = list_thread_next(thread_list, &maxlen); - } - while (thread_list != (rt_list_t*)RT_NULL); + while (next != (rt_list_t*)RT_NULL); return 0; } @@ -233,427 +263,595 @@ static void show_wait_queue(struct rt_list_node *list) } #ifdef RT_USING_SEMAPHORE -static long _list_sem(struct rt_list_node *list) +long list_sem(void) { + rt_ubase_t level; + list_get_next_t find_arg; + rt_list_t *obj_list[LIST_FIND_OBJ_NR]; + rt_list_t *next = (rt_list_t*)RT_NULL; + int maxlen; - struct rt_semaphore *sem; - struct rt_list_node *node; const char *item_title = "semaphore"; - maxlen = object_name_maxlen(item_title, list); + list_find_init(&find_arg, RT_Object_Class_Semaphore, obj_list, sizeof(obj_list)/sizeof(obj_list[0])); + + maxlen = RT_NAME_MAX; rt_kprintf("%-*.s v suspend thread\n", maxlen, item_title); object_split(maxlen); rt_kprintf( " --- --------------\n"); - for (node = list->next; node != list; node = node->next) + + do { - sem = (struct rt_semaphore *)(rt_list_entry(node, struct rt_object, list)); - if (!rt_list_isempty(&sem->parent.suspend_thread)) - { - rt_kprintf("%-*.*s %03d %d:", - maxlen, RT_NAME_MAX, - sem->parent.parent.name, - sem->value, - rt_list_len(&sem->parent.suspend_thread)); - show_wait_queue(&(sem->parent.suspend_thread)); - rt_kprintf("\n"); - } - else + next = list_get_next(next, &find_arg); { - rt_kprintf("%-*.*s %03d %d\n", - maxlen, RT_NAME_MAX, - sem->parent.parent.name, - sem->value, - rt_list_len(&sem->parent.suspend_thread)); + int i; + for (i = 0; i < find_arg.nr_out; i++) + { + struct rt_object *obj; + struct rt_semaphore *sem; + + obj = rt_list_entry(obj_list[i], struct rt_object, list); + level = rt_hw_interrupt_disable(); + if ((obj->type & ~RT_Object_Class_Static) != find_arg.type) + { + rt_hw_interrupt_enable(level); + continue; + } + rt_hw_interrupt_enable(level); + + sem = (struct rt_semaphore*)obj; + if (!rt_list_isempty(&sem->parent.suspend_thread)) + { + rt_kprintf("%-*.*s %03d %d:", + maxlen, RT_NAME_MAX, + sem->parent.parent.name, + sem->value, + rt_list_len(&sem->parent.suspend_thread)); + show_wait_queue(&(sem->parent.suspend_thread)); + rt_kprintf("\n"); + } + else + { + rt_kprintf("%-*.*s %03d %d\n", + maxlen, RT_NAME_MAX, + sem->parent.parent.name, + sem->value, + rt_list_len(&sem->parent.suspend_thread)); + } + } } } + while (next != (rt_list_t*)RT_NULL); return 0; } - -long list_sem(void) -{ - struct rt_object_information *info; - - info = rt_object_get_information(RT_Object_Class_Semaphore); - - return _list_sem(&info->object_list); -} -FINSH_FUNCTION_EXPORT(list_sem, list semaphone in system); +FINSH_FUNCTION_EXPORT(list_sem, list semaphore in system); MSH_CMD_EXPORT(list_sem, list semaphore in system); #endif #ifdef RT_USING_EVENT -static long _list_event(struct rt_list_node *list) +long list_event(void) { + rt_ubase_t level; + list_get_next_t find_arg; + rt_list_t *obj_list[LIST_FIND_OBJ_NR]; + rt_list_t *next = (rt_list_t*)RT_NULL; + int maxlen; - struct rt_event *e; - struct rt_list_node *node; const char *item_title = "event"; - maxlen = object_name_maxlen(item_title, list); + list_find_init(&find_arg, RT_Object_Class_Event, obj_list, sizeof(obj_list)/sizeof(obj_list[0])); + + maxlen = RT_NAME_MAX; rt_kprintf("%-*.s set suspend thread\n", maxlen, item_title); object_split(maxlen); rt_kprintf( " ---------- --------------\n"); - for (node = list->next; node != list; node = node->next) + + do { - e = (struct rt_event *)(rt_list_entry(node, struct rt_object, list)); - if (!rt_list_isempty(&e->parent.suspend_thread)) - { - rt_kprintf("%-*.*s 0x%08x %03d:", - maxlen, RT_NAME_MAX, - e->parent.parent.name, - e->set, - rt_list_len(&e->parent.suspend_thread)); - show_wait_queue(&(e->parent.suspend_thread)); - rt_kprintf("\n"); - } - else + next = list_get_next(next, &find_arg); { - rt_kprintf("%-*.*s 0x%08x 0\n", - maxlen, RT_NAME_MAX, e->parent.parent.name, e->set); + int i; + for (i = 0; i < find_arg.nr_out; i++) + { + struct rt_object *obj; + struct rt_event *e; + + obj = rt_list_entry(obj_list[i], struct rt_object, list); + level = rt_hw_interrupt_disable(); + if ((obj->type & ~RT_Object_Class_Static) != find_arg.type) + { + rt_hw_interrupt_enable(level); + continue; + } + + rt_hw_interrupt_enable(level); + + e = (struct rt_event *)obj; + if (!rt_list_isempty(&e->parent.suspend_thread)) + { + rt_kprintf("%-*.*s 0x%08x %03d:", + maxlen, RT_NAME_MAX, + e->parent.parent.name, + e->set, + rt_list_len(&e->parent.suspend_thread)); + show_wait_queue(&(e->parent.suspend_thread)); + rt_kprintf("\n"); + } + else + { + rt_kprintf("%-*.*s 0x%08x 0\n", + maxlen, RT_NAME_MAX, e->parent.parent.name, e->set); + } + } } } + while (next != (rt_list_t*)RT_NULL); return 0; } - -long list_event(void) -{ - struct rt_object_information *info; - - info = rt_object_get_information(RT_Object_Class_Event); - return _list_event(&info->object_list); -} FINSH_FUNCTION_EXPORT(list_event, list event in system); MSH_CMD_EXPORT(list_event, list event in system); #endif #ifdef RT_USING_MUTEX -static long _list_mutex(struct rt_list_node *list) +long list_mutex(void) { + rt_ubase_t level; + list_get_next_t find_arg; + rt_list_t *obj_list[LIST_FIND_OBJ_NR]; + rt_list_t *next = (rt_list_t*)RT_NULL; + int maxlen; - struct rt_mutex *m; - struct rt_list_node *node; const char *item_title = "mutex"; - maxlen = object_name_maxlen(item_title, list); + list_find_init(&find_arg, RT_Object_Class_Mutex, obj_list, sizeof(obj_list)/sizeof(obj_list[0])); + + maxlen = RT_NAME_MAX; rt_kprintf("%-*.s owner hold suspend thread\n", maxlen, item_title); object_split(maxlen); rt_kprintf( " -------- ---- --------------\n"); - for (node = list->next; node != list; node = node->next) + + do { - m = (struct rt_mutex *)(rt_list_entry(node, struct rt_object, list)); - rt_kprintf("%-*.*s %-8.*s %04d %d\n", - maxlen, RT_NAME_MAX, - m->parent.parent.name, - RT_NAME_MAX, - m->owner->name, - m->hold, - rt_list_len(&m->parent.suspend_thread)); - } + next = list_get_next(next, &find_arg); + { + int i; + for (i = 0; i < find_arg.nr_out; i++) + { + struct rt_object *obj; + struct rt_mutex *m; - return 0; -} + obj = rt_list_entry(obj_list[i], struct rt_object, list); + level = rt_hw_interrupt_disable(); + if ((obj->type & ~RT_Object_Class_Static) != find_arg.type) + { + rt_hw_interrupt_enable(level); + continue; + } -long list_mutex(void) -{ - struct rt_object_information *info; + rt_hw_interrupt_enable(level); - info = rt_object_get_information(RT_Object_Class_Mutex); + m = (struct rt_mutex *)obj; + rt_kprintf("%-*.*s %-8.*s %04d %d\n", + maxlen, RT_NAME_MAX, + m->parent.parent.name, + RT_NAME_MAX, + m->owner->name, + m->hold, + rt_list_len(&m->parent.suspend_thread)); + + } + } + } + while (next != (rt_list_t*)RT_NULL); - return _list_mutex(&info->object_list); + return 0; } FINSH_FUNCTION_EXPORT(list_mutex, list mutex in system); MSH_CMD_EXPORT(list_mutex, list mutex in system); #endif #ifdef RT_USING_MAILBOX -static long _list_mailbox(struct rt_list_node *list) +long list_mailbox(void) { + rt_ubase_t level; + list_get_next_t find_arg; + rt_list_t *obj_list[LIST_FIND_OBJ_NR]; + rt_list_t *next = (rt_list_t*)RT_NULL; + int maxlen; - struct rt_mailbox *m; - struct rt_list_node *node; const char *item_title = "mailbox"; - maxlen = object_name_maxlen(item_title, list); + list_find_init(&find_arg, RT_Object_Class_MailBox, obj_list, sizeof(obj_list)/sizeof(obj_list[0])); + + maxlen = RT_NAME_MAX; rt_kprintf("%-*.s entry size suspend thread\n", maxlen, item_title); object_split(maxlen); rt_kprintf( " ---- ---- --------------\n"); - for (node = list->next; node != list; node = node->next) + + do { - m = (struct rt_mailbox *)(rt_list_entry(node, struct rt_object, list)); - if (!rt_list_isempty(&m->parent.suspend_thread)) - { - rt_kprintf("%-*.*s %04d %04d %d:", - maxlen, RT_NAME_MAX, - m->parent.parent.name, - m->entry, - m->size, - rt_list_len(&m->parent.suspend_thread)); - show_wait_queue(&(m->parent.suspend_thread)); - rt_kprintf("\n"); - } - else + next = list_get_next(next, &find_arg); { - rt_kprintf("%-*.*s %04d %04d %d\n", - maxlen, RT_NAME_MAX, - m->parent.parent.name, - m->entry, - m->size, - rt_list_len(&m->parent.suspend_thread)); + int i; + for (i = 0; i < find_arg.nr_out; i++) + { + struct rt_object *obj; + struct rt_mailbox *m; + + obj = rt_list_entry(obj_list[i], struct rt_object, list); + level = rt_hw_interrupt_disable(); + if ((obj->type & ~RT_Object_Class_Static) != find_arg.type) + { + rt_hw_interrupt_enable(level); + continue; + } + + rt_hw_interrupt_enable(level); + + m = (struct rt_mailbox *)obj; + if (!rt_list_isempty(&m->parent.suspend_thread)) + { + rt_kprintf("%-*.*s %04d %04d %d:", + maxlen, RT_NAME_MAX, + m->parent.parent.name, + m->entry, + m->size, + rt_list_len(&m->parent.suspend_thread)); + show_wait_queue(&(m->parent.suspend_thread)); + rt_kprintf("\n"); + } + else + { + rt_kprintf("%-*.*s %04d %04d %d\n", + maxlen, RT_NAME_MAX, + m->parent.parent.name, + m->entry, + m->size, + rt_list_len(&m->parent.suspend_thread)); + } + + } } } + while (next != (rt_list_t*)RT_NULL); return 0; } - -long list_mailbox(void) -{ - struct rt_object_information *info; - - info = rt_object_get_information(RT_Object_Class_MailBox); - return _list_mailbox(&info->object_list); -} FINSH_FUNCTION_EXPORT(list_mailbox, list mail box in system); MSH_CMD_EXPORT(list_mailbox, list mail box in system); #endif #ifdef RT_USING_MESSAGEQUEUE -static long _list_msgqueue(struct rt_list_node *list) +long list_msgqueue(void) { + rt_ubase_t level; + list_get_next_t find_arg; + rt_list_t *obj_list[LIST_FIND_OBJ_NR]; + rt_list_t *next = (rt_list_t*)RT_NULL; + int maxlen; - struct rt_messagequeue *m; - struct rt_list_node *node; const char *item_title = "msgqueue"; - maxlen = object_name_maxlen(item_title, list); + list_find_init(&find_arg, RT_Object_Class_MessageQueue, obj_list, sizeof(obj_list)/sizeof(obj_list[0])); + + maxlen = RT_NAME_MAX; rt_kprintf("%-*.s entry suspend thread\n", maxlen, item_title); object_split(maxlen); rt_kprintf( " ---- --------------\n"); - for (node = list->next; node != list; node = node->next) + do { - m = (struct rt_messagequeue *)(rt_list_entry(node, struct rt_object, list)); - if (!rt_list_isempty(&m->parent.suspend_thread)) - { - rt_kprintf("%-*.*s %04d %d:", - maxlen, RT_NAME_MAX, - m->parent.parent.name, - m->entry, - rt_list_len(&m->parent.suspend_thread)); - show_wait_queue(&(m->parent.suspend_thread)); - rt_kprintf("\n"); - } - else + next = list_get_next(next, &find_arg); { - rt_kprintf("%-*.*s %04d %d\n", - maxlen, RT_NAME_MAX, - m->parent.parent.name, - m->entry, - rt_list_len(&m->parent.suspend_thread)); + int i; + for (i = 0; i < find_arg.nr_out; i++) + { + struct rt_object *obj; + struct rt_messagequeue *m; + + obj = rt_list_entry(obj_list[i], struct rt_object, list); + level = rt_hw_interrupt_disable(); + if ((obj->type & ~RT_Object_Class_Static) != find_arg.type) + { + rt_hw_interrupt_enable(level); + continue; + } + + rt_hw_interrupt_enable(level); + + m = (struct rt_messagequeue *)obj; + if (!rt_list_isempty(&m->parent.suspend_thread)) + { + rt_kprintf("%-*.*s %04d %d:", + maxlen, RT_NAME_MAX, + m->parent.parent.name, + m->entry, + rt_list_len(&m->parent.suspend_thread)); + show_wait_queue(&(m->parent.suspend_thread)); + rt_kprintf("\n"); + } + else + { + rt_kprintf("%-*.*s %04d %d\n", + maxlen, RT_NAME_MAX, + m->parent.parent.name, + m->entry, + rt_list_len(&m->parent.suspend_thread)); + } + } } } + while (next != (rt_list_t*)RT_NULL); return 0; } - -long list_msgqueue(void) -{ - struct rt_object_information *info; - - info = rt_object_get_information(RT_Object_Class_MessageQueue); - return _list_msgqueue(&info->object_list); -} FINSH_FUNCTION_EXPORT(list_msgqueue, list message queue in system); MSH_CMD_EXPORT(list_msgqueue, list message queue in system); #endif #ifdef RT_USING_MEMHEAP -static long _list_memheap(struct rt_list_node *list) +long list_memheap(void) { + rt_ubase_t level; + list_get_next_t find_arg; + rt_list_t *obj_list[LIST_FIND_OBJ_NR]; + rt_list_t *next = (rt_list_t*)RT_NULL; + int maxlen; - struct rt_memheap *mh; - struct rt_list_node *node; const char *item_title = "memheap"; - maxlen = object_name_maxlen(item_title, list); + list_find_init(&find_arg, RT_Object_Class_MemHeap, obj_list, sizeof(obj_list)/sizeof(obj_list[0])); + + maxlen = RT_NAME_MAX; rt_kprintf("%-*.s pool size max used size available size\n", maxlen, item_title); object_split(maxlen); rt_kprintf( " ---------- ------------- --------------\n"); - for (node = list->next; node != list; node = node->next) + do { - mh = (struct rt_memheap *)rt_list_entry(node, struct rt_object, list); - - rt_kprintf("%-*.*s %-010d %-013d %-05d\n", - maxlen, RT_NAME_MAX, - mh->parent.name, - mh->pool_size, - mh->max_used_size, - mh->available_size); - } + next = list_get_next(next, &find_arg); + { + int i; + for (i = 0; i < find_arg.nr_out; i++) + { + struct rt_object *obj; + struct rt_memheap *mh; - return 0; -} + obj = rt_list_entry(obj_list[i], struct rt_object, list); + level = rt_hw_interrupt_disable(); + if ((obj->type & ~RT_Object_Class_Static) != find_arg.type) + { + rt_hw_interrupt_enable(level); + continue; + } -long list_memheap(void) -{ - struct rt_object_information *info; + rt_hw_interrupt_enable(level); + + mh = (struct rt_memheap *)obj; + + rt_kprintf("%-*.*s %-010d %-013d %-05d\n", + maxlen, RT_NAME_MAX, + mh->parent.name, + mh->pool_size, + mh->max_used_size, + mh->available_size); + + } + } + } + while (next != (rt_list_t*)RT_NULL); - info = rt_object_get_information(RT_Object_Class_MemHeap); - return _list_memheap(&info->object_list); + return 0; } FINSH_FUNCTION_EXPORT(list_memheap, list memory heap in system); MSH_CMD_EXPORT(list_memheap, list memory heap in system); #endif #ifdef RT_USING_MEMPOOL -static long _list_mempool(struct rt_list_node *list) +long list_mempool(void) { + rt_ubase_t level; + list_get_next_t find_arg; + rt_list_t *obj_list[LIST_FIND_OBJ_NR]; + rt_list_t *next = (rt_list_t*)RT_NULL; + int maxlen; - struct rt_mempool *mp; - struct rt_list_node *node; const char *item_title = "mempool"; - maxlen = object_name_maxlen(item_title, list); + list_find_init(&find_arg, RT_Object_Class_MemPool, obj_list, sizeof(obj_list)/sizeof(obj_list[0])); + + maxlen = RT_NAME_MAX; rt_kprintf("%-*.s block total free suspend thread\n", maxlen, item_title); object_split(maxlen); rt_kprintf( " ---- ---- ---- --------------\n"); - for (node = list->next; node != list; node = node->next) + do { - mp = (struct rt_mempool *)rt_list_entry(node, struct rt_object, list); - if (mp->suspend_thread_count > 0) - { - rt_kprintf("%-*.*s %04d %04d %04d %d:", - maxlen, RT_NAME_MAX, - mp->parent.name, - mp->block_size, - mp->block_total_count, - mp->block_free_count, - mp->suspend_thread_count); - show_wait_queue(&(mp->suspend_thread)); - rt_kprintf("\n"); - } - else + next = list_get_next(next, &find_arg); { - rt_kprintf("%-*.*s %04d %04d %04d %d\n", - maxlen, RT_NAME_MAX, - mp->parent.name, - mp->block_size, - mp->block_total_count, - mp->block_free_count, - mp->suspend_thread_count); + int i; + for (i = 0; i < find_arg.nr_out; i++) + { + struct rt_object *obj; + struct rt_mempool *mp; + + obj = rt_list_entry(obj_list[i], struct rt_object, list); + level = rt_hw_interrupt_disable(); + if ((obj->type & ~RT_Object_Class_Static) != find_arg.type) + { + rt_hw_interrupt_enable(level); + continue; + } + + rt_hw_interrupt_enable(level); + + mp = (struct rt_mempool *)obj; + if (mp->suspend_thread_count > 0) + { + rt_kprintf("%-*.*s %04d %04d %04d %d:", + maxlen, RT_NAME_MAX, + mp->parent.name, + mp->block_size, + mp->block_total_count, + mp->block_free_count, + mp->suspend_thread_count); + show_wait_queue(&(mp->suspend_thread)); + rt_kprintf("\n"); + } + else + { + rt_kprintf("%-*.*s %04d %04d %04d %d\n", + maxlen, RT_NAME_MAX, + mp->parent.name, + mp->block_size, + mp->block_total_count, + mp->block_free_count, + mp->suspend_thread_count); + } + } } } + while (next != (rt_list_t*)RT_NULL); return 0; } - -long list_mempool(void) -{ - struct rt_object_information *info; - - info = rt_object_get_information(RT_Object_Class_MemPool); - return _list_mempool(&info->object_list); -} FINSH_FUNCTION_EXPORT(list_mempool, list memory pool in system) MSH_CMD_EXPORT(list_mempool, list memory pool in system); #endif -static long _list_timer(struct rt_list_node *list) +long list_timer(void) { + rt_ubase_t level; + list_get_next_t find_arg; + rt_list_t *obj_list[LIST_FIND_OBJ_NR]; + rt_list_t *next = (rt_list_t*)RT_NULL; + int maxlen; - struct rt_timer *timer; - struct rt_list_node *node; const char *item_title = "timer"; - maxlen = object_name_maxlen(item_title, list); + list_find_init(&find_arg, RT_Object_Class_Timer, obj_list, sizeof(obj_list)/sizeof(obj_list[0])); + + maxlen = RT_NAME_MAX; rt_kprintf("%-*.s periodic timeout flag\n", maxlen, item_title); object_split(maxlen); rt_kprintf( " ---------- ---------- -----------\n"); - for (node = list->next; node != list; node = node->next) - { - timer = (struct rt_timer *)(rt_list_entry(node, struct rt_object, list)); - rt_kprintf("%-*.*s 0x%08x 0x%08x ", - maxlen, RT_NAME_MAX, - timer->parent.name, - timer->init_tick, - timer->timeout_tick); - if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED) - rt_kprintf("activated\n"); - else - rt_kprintf("deactivated\n"); + do { + next = list_get_next(next, &find_arg); + { + int i; + for (i = 0; i < find_arg.nr_out; i++) + { + struct rt_object *obj; + struct rt_timer *timer; + + obj = rt_list_entry(obj_list[i], struct rt_object, list); + level = rt_hw_interrupt_disable(); + if ((obj->type & ~RT_Object_Class_Static) != find_arg.type) + { + rt_hw_interrupt_enable(level); + continue; + } + + rt_hw_interrupt_enable(level); + + timer = (struct rt_timer *)obj; + rt_kprintf("%-*.*s 0x%08x 0x%08x ", + maxlen, RT_NAME_MAX, + timer->parent.name, + timer->init_tick, + timer->timeout_tick); + if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED) + rt_kprintf("activated\n"); + else + rt_kprintf("deactivated\n"); + + } + } } + while (next != (rt_list_t*)RT_NULL); rt_kprintf("current tick:0x%08x\n", rt_tick_get()); return 0; } - -long list_timer(void) -{ - struct rt_object_information *info; - - info = rt_object_get_information(RT_Object_Class_Timer); - return _list_timer(&info->object_list); -} FINSH_FUNCTION_EXPORT(list_timer, list timer in system); MSH_CMD_EXPORT(list_timer, list timer in system); #ifdef RT_USING_DEVICE -static long _list_device(struct rt_list_node *list) +static char *const device_type_str[] = +{ + "Character Device", + "Block Device", + "Network Interface", + "MTD Device", + "CAN Device", + "RTC", + "Sound Device", + "Graphic Device", + "I2C Bus", + "USB Slave Device", + "USB Host Bus", + "SPI Bus", + "SPI Device", + "SDIO Bus", + "PM Pseudo Device", + "Pipe", + "Portal Device", + "Timer Device", + "Miscellaneous Device", + "Unknown" +}; + +long list_device(void) { + rt_ubase_t level; + list_get_next_t find_arg; + rt_list_t *obj_list[LIST_FIND_OBJ_NR]; + rt_list_t *next = (rt_list_t*)RT_NULL; + int maxlen; - struct rt_device *device; - struct rt_list_node *node; - char *const device_type_str[] = - { - "Character Device", - "Block Device", - "Network Interface", - "MTD Device", - "CAN Device", - "RTC", - "Sound Device", - "Graphic Device", - "I2C Bus", - "USB Slave Device", - "USB Host Bus", - "SPI Bus", - "SPI Device", - "SDIO Bus", - "PM Pseudo Device", - "Pipe", - "Portal Device", - "Timer Device", - "Miscellaneous Device", - "Unknown" - }; const char *item_title = "device"; - maxlen = object_name_maxlen(item_title, list); + list_find_init(&find_arg, RT_Object_Class_Device, obj_list, sizeof(obj_list)/sizeof(obj_list[0])); + + maxlen = RT_NAME_MAX; rt_kprintf("%-*.s type ref count\n", maxlen, item_title); object_split(maxlen); rt_kprintf( " -------------------- ----------\n"); - for (node = list->next; node != list; node = node->next) + do { - device = (struct rt_device *)(rt_list_entry(node, struct rt_object, list)); - rt_kprintf("%-*.*s %-20s %-8d\n", - maxlen, RT_NAME_MAX, - device->parent.name, - (device->type <= RT_Device_Class_Unknown) ? - device_type_str[device->type] : - device_type_str[RT_Device_Class_Unknown], - device->ref_count); - } + next = list_get_next(next, &find_arg); + { + int i; + for (i = 0; i < find_arg.nr_out; i++) + { + struct rt_object *obj; + struct rt_device *device; - return 0; -} + obj = rt_list_entry(obj_list[i], struct rt_object, list); + level = rt_hw_interrupt_disable(); + if ((obj->type & ~RT_Object_Class_Static) != find_arg.type) + { + rt_hw_interrupt_enable(level); + continue; + } -long list_device(void) -{ - struct rt_object_information *info; + rt_hw_interrupt_enable(level); - info = rt_object_get_information(RT_Object_Class_Device); - return _list_device(&info->object_list); + device = (struct rt_device *)obj; + rt_kprintf("%-*.*s %-20s %-8d\n", + maxlen, RT_NAME_MAX, + device->parent.name, + (device->type <= RT_Device_Class_Unknown) ? + device_type_str[device->type] : + device_type_str[RT_Device_Class_Unknown], + device->ref_count); + + } + } + } + while (next != (rt_list_t*)RT_NULL); + + return 0; } FINSH_FUNCTION_EXPORT(list_device, list device in system); MSH_CMD_EXPORT(list_device, list device in system); diff --git a/include/rtdef.h b/include/rtdef.h index 49551de9ee..ecdabd4490 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -367,7 +367,8 @@ typedef struct rt_object *rt_object_t; /**< Type for kernel obj */ enum rt_object_class_type { - RT_Object_Class_Thread = 0, /**< The object is a thread. */ + RT_Object_Class_Null = 0, /**< The object is not used. */ + RT_Object_Class_Thread, /**< The object is a thread. */ RT_Object_Class_Semaphore, /**< The object is a semaphore. */ RT_Object_Class_Mutex, /**< The object is a mutex. */ RT_Object_Class_Event, /**< The object is a event. */ -- cgit v1.3.1 From d68c0f225dc50bcc0d4d1935f6609c3e2c874cae Mon Sep 17 00:00:00 2001 From: Bernard Xiong Date: Sun, 27 Jan 2019 21:58:07 +0800 Subject: Update rtdef.h Update version number to v4.0.1 --- include/rtdef.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'include/rtdef.h') diff --git a/include/rtdef.h b/include/rtdef.h index 39deb47d9f..859278e849 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -29,6 +29,7 @@ * 2018-11-22 Jesven add smp member to struct rt_thread * add struct rt_cpu * add smp relevant macros + * 2019-01-27 Bernard change version number to v4.0.1 */ #ifndef __RT_DEF_H__ @@ -50,7 +51,7 @@ extern "C" { /* RT-Thread version information */ #define RT_VERSION 4L /**< major version number */ #define RT_SUBVERSION 0L /**< minor version number */ -#define RT_REVISION 0L /**< revise version number */ +#define RT_REVISION 1L /**< revise version number */ /* RT-Thread version */ #define RTTHREAD_VERSION ((RT_VERSION * 10000) + \ -- cgit v1.3.1 From 64813b6b07f32bbee1a5f0743c5ea08458294d13 Mon Sep 17 00:00:00 2001 From: guozhanxin Date: Tue, 12 Feb 2019 10:15:26 +0800 Subject: add first version --- components/drivers/Kconfig | 11 + components/drivers/sensors/SConscript | 9 +- components/drivers/sensors/bmi055_sensor.cpp | 451 --------- components/drivers/sensors/bmi055_sensor.h | 334 ------- components/drivers/sensors/mpu6050_sensor.cpp | 458 --------- components/drivers/sensors/mpu6050_sensor.h | 189 ---- components/drivers/sensors/sensor.c | 449 +++++++++ components/drivers/sensors/sensor.cpp | 228 ----- components/drivers/sensors/sensor.h | 1243 +++---------------------- components/drivers/sensors/sensor_test.c | 361 +++++++ components/finsh/cmd.c | 1 + include/rtdef.h | 1 + 12 files changed, 979 insertions(+), 2756 deletions(-) delete mode 100644 components/drivers/sensors/bmi055_sensor.cpp delete mode 100644 components/drivers/sensors/bmi055_sensor.h delete mode 100644 components/drivers/sensors/mpu6050_sensor.cpp delete mode 100644 components/drivers/sensors/mpu6050_sensor.h create mode 100644 components/drivers/sensors/sensor.c delete mode 100644 components/drivers/sensors/sensor.cpp create mode 100644 components/drivers/sensors/sensor_test.c (limited to 'include/rtdef.h') diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index 8f66738260..6246a97160 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -239,6 +239,17 @@ config RT_USING_AUDIO bool "Using Audio device drivers" default n +config RT_USING_SENSOR + bool "Using Sensor device drivers" + select RT_USING_PIN + default n + +if RT_USING_SENSOR + config RT_USING_SENSOR_TEST + bool "Use Sensor test" + default y +endif + menu "Using WiFi" config RT_USING_WIFI bool "Using Wi-Fi framework" diff --git a/components/drivers/sensors/SConscript b/components/drivers/sensors/SConscript index db409cfd2b..e727b14136 100644 --- a/components/drivers/sensors/SConscript +++ b/components/drivers/sensors/SConscript @@ -3,14 +3,11 @@ from building import * cwd = GetCurrentDir() -src = ['sensor.cpp'] +src = ['sensor.c'] CPPPATH = [cwd, cwd + '/../include'] -if GetDepend('SENSOR_USING_MPU6050') and GetDepend('RT_USING_I2C'): - src += ['mpu6050_sensor.cpp']; - -if GetDepend('SENSOR_USING_BMI055') and GetDepend('RT_USING_I2C'): - src += ['bmi055_sensor.cpp'] +if GetDepend('RT_USING_SENSOR_TEST'): + src += ['sensor_test.c']; group = DefineGroup('Sensors', src, depend = ['RT_USING_SENSOR', 'RT_USING_DEVICE'], CPPPATH = CPPPATH) diff --git a/components/drivers/sensors/bmi055_sensor.cpp b/components/drivers/sensors/bmi055_sensor.cpp deleted file mode 100644 index b6e7f99da5..0000000000 --- a/components/drivers/sensors/bmi055_sensor.cpp +++ /dev/null @@ -1,451 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2015-1-11 RT_learning the first version - */ - -#include -#include -#include -#include "bmi055_sensor.h" - - -const static sensor_t _BMI055_sensor[] = -{ - { - .name = "Accelerometer", - .vendor = "Bosch", - .version = sizeof(sensor_t), - .handle = 0, - .type = SENSOR_TYPE_ACCELEROMETER, - .maxRange = SENSOR_ACCEL_RANGE_16G, - .resolution = 1.0f, - .power = 0.5f, - .minDelay = 10000, - .fifoReservedEventCount = 0, - .fifoMaxEventCount = 64, - }, - { - .name = "Gyroscope", - .vendor = "Bosch", - .version = sizeof(sensor_t), - .handle = 0, - .type = SENSOR_TYPE_GYROSCOPE, - .maxRange = SENSOR_GYRO_RANGE_2000DPS, - .resolution = 1.0f, - .power = 0.5f, - .minDelay = 10000, - .fifoReservedEventCount = 0, - .fifoMaxEventCount = 64, - } -}; - -BMI055::BMI055(int sensor_type, const char* iic_bus, int addr) - : SensorBase(sensor_type) -{ - this->i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(iic_bus); - if (this->i2c_bus == NULL) - { - printf("BMI055: No IIC device:%s\n", iic_bus); - return; - } - - this->i2c_addr = addr; - - /* register to sensor manager */ - SensorManager::registerSensor(this); -} - -int BMI055::read_reg(rt_uint8_t reg, rt_uint8_t *value) -{ - struct rt_i2c_msg msgs[2]; - - msgs[0].addr = this->i2c_addr; - msgs[0].flags = RT_I2C_WR; - msgs[0].buf = ® - msgs[0].len = 1; - - msgs[1].addr = this->i2c_addr; - msgs[1].flags = RT_I2C_RD; /* Read from slave */ - msgs[1].buf = (rt_uint8_t *)value; - msgs[1].len = 1; - - if (rt_i2c_transfer(this->i2c_bus, msgs, 2) == 2) - return RT_EOK; - - return -RT_ERROR; -} - -int BMI055::read_buffer(rt_uint8_t reg, rt_uint8_t* value, rt_size_t size) -{ - struct rt_i2c_msg msgs[2]; - - msgs[0].addr = this->i2c_addr; - msgs[0].flags = RT_I2C_WR; - msgs[0].buf = ® - msgs[0].len = 1; - - msgs[1].addr = this->i2c_addr; - msgs[1].flags = RT_I2C_RD; /* Read from slave */ - msgs[1].buf = (rt_uint8_t *)value; - msgs[1].len = size; - - if (rt_i2c_transfer(this->i2c_bus, msgs, 2) == 2) - return RT_EOK; - - return -RT_ERROR; -} - -int BMI055::write_reg(rt_uint8_t reg, rt_uint8_t value) -{ - struct rt_i2c_msg msgs[2]; - - msgs[0].addr = this->i2c_addr; - msgs[0].flags = RT_I2C_WR; - msgs[0].buf = ® - msgs[0].len = 1; - - msgs[1].addr = this->i2c_addr; - msgs[1].flags = RT_I2C_WR | RT_I2C_NO_START; - msgs[1].buf = (rt_uint8_t *)&value; - msgs[1].len = 1; - - if (rt_i2c_transfer(this->i2c_bus, msgs, 2) == 2) - return RT_EOK; - - return -RT_ERROR; -} - - -BMI055_Accelerometer::BMI055_Accelerometer(const char* iic_name, int addr) - : BMI055(SENSOR_TYPE_ACCELEROMETER, iic_name, addr) -{ - int index; - uint8_t id; - rt_uint8_t value[6] = {0}; - rt_int32_t x, y, z; - SensorConfig config = {SENSOR_MODE_NORMAL, SENSOR_DATARATE_400HZ, SENSOR_ACCEL_RANGE_2G}; - - write_reg(BMI055_BGW_SOFTRESET, 0xB6); /* reset of the sensor P57 */ - write_reg(BMI055_PMU_LPW, 0x00); /* PMU_LPW NORMAL mode P55 */ - write_reg(BMI055_PMU_BW, 0x0A); /* 01010b 31.25 Hz P55 */ - write_reg(BMI055_PMU_RANGE, 0x05); /* 0101b 卤4g range PMU_RANGE set acc +-4g/s P54 */ - - - x_offset = y_offset = z_offset = 0; - x = y = z = 0; - - /* read BMI055 id */ - read_buffer(BMI055_ACC_BGW_CHIPID, &id, 1); /* BGW_CHIPID P47*/ - if (id != BMI055_ACC_BGW_CHIPID_VALUE) - { - printf("Warning: not found BMI055 id: %02x\n", id); - } - - /* get offset */ - for (index = 0; index < 200; index ++) - { - read_buffer(BMI055_ACCD_X_LSB, value, 6); /*ACCD_X_LSB P47 */ - - x += (((rt_int16_t)value[1] << 8) | value[0]); - y += (((rt_int16_t)value[3] << 8) | value[2]); - z += (((rt_int16_t)value[5] << 8) | value[4]); - } - x_offset = x / 200; - y_offset = y / 200; - z_offset = z / 200; - - this->enable = RT_FALSE; - this->sensitivity = SENSOR_ACCEL_SENSITIVITY_2G; - this->config = config; -} - -int -BMI055_Accelerometer::configure(SensorConfig *config) -{ - int range; - uint8_t value; - - if (config == RT_NULL) return -1; - - /* TODO: set datarate */ - - /* get range and calc the sensitivity */ - range = config->range.accel_range; - switch (range) - { - case SENSOR_ACCEL_RANGE_2G: - this->sensitivity = SENSOR_ACCEL_SENSITIVITY_2G; - range = 0x03; //0011b - break; - case SENSOR_ACCEL_RANGE_4G: - this->sensitivity = SENSOR_ACCEL_SENSITIVITY_4G; - range = 0x05; //0101b - break; - case SENSOR_ACCEL_RANGE_8G: - this->sensitivity = SENSOR_ACCEL_SENSITIVITY_8G; - range = 0x01 << 3; //1000b - break; - case SENSOR_ACCEL_RANGE_16G: - this->sensitivity = SENSOR_ACCEL_SENSITIVITY_16G; - range = 0x03 << 2; //1100b - break; - default: - return -1; - } - - /* set range to sensor */ - read_reg(BMI055_PMU_RANGE, &value); /* PMU_RANGE P54 */ - value &= 0xF0; - value |= range; - write_reg(BMI055_PMU_RANGE, value); - - return 0; -} - -int -BMI055_Accelerometer::activate(int enable) -{ - uint8_t value; - - if (enable && this->enable == RT_FALSE) - { - /* enable accelerometer */ - read_reg(BMI055_PMU_LPW, &value); /* P55 */ - value &= ~(0x07 << 7); - write_reg(BMI055_PMU_LPW, value); - } - - if (!enable && this->enable == RT_TRUE) - { - /* disable accelerometer */ - read_reg(BMI055_PMU_LPW, &value); - value &= ~(0x07 << 7); - value |= (0x01 << 7); - write_reg(BMI055_PMU_LPW, value); - } - - if (enable) this->enable = RT_TRUE; - else this->enable = RT_FALSE; - - return 0; -} - -int -BMI055_Accelerometer::poll(sensors_event_t *event) -{ - rt_uint8_t value[6]; - rt_int16_t x, y, z; - - /* parameters check */ - if (event == NULL) return -1; - - /* get event data */ - event->version = sizeof(sensors_event_t); - event->sensor = (int32_t) this; - event->timestamp = rt_tick_get(); - event->type = SENSOR_TYPE_ACCELEROMETER; - - read_buffer(0x02, value, 6); - - /* get raw data */ - x = (((rt_int16_t)value[1] << 8) | value[0]); - y = (((rt_int16_t)value[3] << 8) | value[2]); - z = (((rt_int16_t)value[5] << 8) | value[4]); - - if (config.mode == SENSOR_MODE_RAW) - { - event->raw_acceleration.x = x; - event->raw_acceleration.y = y; - event->raw_acceleration.z = z; - } - else - { - - x -= x_offset; y -= y_offset; z -= z_offset; - event->acceleration.x = x * this->sensitivity * SENSORS_GRAVITY_STANDARD; - event->acceleration.y = y * this->sensitivity * SENSORS_GRAVITY_STANDARD; - event->acceleration.z = z * this->sensitivity * SENSORS_GRAVITY_STANDARD; - } - - return 0; -} - -void -BMI055_Accelerometer::getSensor(sensor_t *sensor) -{ - /* get sensor description */ - if (sensor) - { - memcpy(sensor, &_BMI055_sensor[0], sizeof(sensor_t)); - } -} - -BMI055_Gyroscope::BMI055_Gyroscope(const char* iic_name, int addr) - : BMI055(SENSOR_TYPE_GYROSCOPE, iic_name, addr) -{ - int index; - uint8_t id; - rt_uint8_t value[6]; - rt_int32_t x, y, z; - - /* initialize BMI055 */ - write_reg(BMI055_MODE_LPM1_ADDR, 0x00); /* normal mode */ - write_reg(BMI055_MODE_LPM2_ADDR, 0x80); /* fast powerup */ - write_reg(BMI055_BW_ADDR, 0x03); /* ODR:400Hz Filter Bandwidth:47Hz */ - write_reg(BMI055_RANGE_ADDR, 0x00); /* 2000dps */ - - - x_offset = y_offset = z_offset = 0; - x = y = z = 0; - - /* read BMI055 id */ - read_buffer(BMI055_CHIP_ID_ADDR, &id, 1); - if (id != BMI055_GRRO_CHIP_ID) - { - printf("Warning: not found BMI055 id: %02x\n", id); - } - - /* get offset */ - for (index = 0; index < 200; index ++) - { - read_buffer(BMI055_RATE_X_LSB_ADDR, value, 6); - - x += (((rt_int16_t)value[1] << 8) | value[0]); - y += (((rt_int16_t)value[3] << 8) | value[2]); - z += (((rt_int16_t)value[5] << 8) | value[4]); - } - x_offset = x / 200; - y_offset = y / 200; - z_offset = z / 200; - - this->enable = RT_FALSE; - this->sensitivity = SENSOR_GYRO_SENSITIVITY_250DPS; -} - -int -BMI055_Gyroscope::configure(SensorConfig *config) -{ - int range; - uint8_t value; - - if (config == RT_NULL) return -1; - - /* TODO: set datarate */ - - /* get range and calc the sensitivity */ - range = config->range.gyro_range; - switch (range) - { - //to do add more range e.g 125DPS - //case - case SENSOR_GYRO_RANGE_250DPS: - this->sensitivity = SENSOR_GYRO_SENSITIVITY_250DPS; - range = 0x11; - break; - case SENSOR_GYRO_RANGE_500DPS: - this->sensitivity = SENSOR_GYRO_SENSITIVITY_500DPS; - range = 0x10; - break; - case SENSOR_GYRO_RANGE_1000DPS: - this->sensitivity = SENSOR_GYRO_SENSITIVITY_1000DPS; - range = 0x01; - break; - case SENSOR_GYRO_RANGE_2000DPS: - this->sensitivity = SENSOR_GYRO_SENSITIVITY_2000DPS; - range = 0x00; - break; - default: - return -1; - } - - /* set range to sensor */ - read_reg(BMI055_RANGE_ADDR, &value); - value &= ~0x07; - value |= range; - write_reg(BMI055_RANGE_ADDR, value); - - return 0; -} - -int -BMI055_Gyroscope::activate(int enable) -{ - uint8_t value; - - if (enable && this->enable == RT_FALSE) - { - /* enable gyroscope */ - read_reg(BMI055_MODE_LPM1_ADDR, &value); - value &= ~(0x1010 << 4); //{0; 0} NORMAL mode - write_reg(BMI055_MODE_LPM1_ADDR, value); //P101 NORMAL mode - } - - if (!enable && this->enable == RT_TRUE) - { - /* disable gyroscope */ - read_reg(BMI055_MODE_LPM1_ADDR, &value); - value &= ~(0x01 << 5); //set bit5 deep_suspend 0 - value |= (0x01 << 7); //set bit1 suspend 1 - write_reg(BMI055_MODE_LPM1_ADDR, value); //{1; 0} SUSPEND mode - } - - if (enable) this->enable = RT_TRUE; - else this->enable = RT_FALSE; - - return 0; -} - -int -BMI055_Gyroscope::poll(sensors_event_t *event) -{ - rt_uint8_t value[6]; - rt_int16_t x, y, z; - - /* parameters check */ - if (event == NULL) return -1; - - /* get event data */ - event->version = sizeof(sensors_event_t); - event->sensor = (int32_t) this; - event->timestamp = rt_tick_get(); - event->type = SENSOR_TYPE_GYROSCOPE; - - read_buffer(BMI055_RATE_X_LSB_ADDR, value, 6); - - /* get raw data */ - x = (((rt_int16_t)value[1] << 8) | value[0]); - y = (((rt_int16_t)value[3] << 8) | value[2]); - z = (((rt_int16_t)value[5] << 8) | value[4]); - - - if (config.mode == SENSOR_MODE_RAW) - { - event->raw_gyro.x = x; - event->raw_gyro.y = y; - event->raw_gyro.z = z; - } - else - { - x -= x_offset; y -= y_offset; z -= z_offset; - event->gyro.x = x * this->sensitivity * SENSORS_DPS_TO_RADS; - event->gyro.y = y * this->sensitivity * SENSORS_DPS_TO_RADS; - event->gyro.z = z * this->sensitivity * SENSORS_DPS_TO_RADS; - } - - return 0; -} - -void -BMI055_Gyroscope::getSensor(sensor_t *sensor) -{ - /* get sensor description */ - if (sensor) - { - memcpy(sensor, &_BMI055_sensor[1], sizeof(sensor_t)); - } -} diff --git a/components/drivers/sensors/bmi055_sensor.h b/components/drivers/sensors/bmi055_sensor.h deleted file mode 100644 index 713aa74ad0..0000000000 --- a/components/drivers/sensors/bmi055_sensor.h +++ /dev/null @@ -1,334 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2015-1-11 RT_learning the first version - */ -#ifndef __BMI055_H__ -#define __BMI055_H__ - -#include - -/**************************************************************************************************/ -/************************************Register map accelerometer************************************/ - -#define BMI055_ACC_I2C_ADDR1 0x18 //SDO is low(GND) -#define BMI055_ACC_I2C_ADDR2 0x19 //SDO is high(VCC) - -#define BMI055_ACC_DEFAULT_ADDRESS BMI055_ACC_I2C_ADDR2 //in the LPC54102 SPM-S - -#define BMI055_ACC_BGW_CHIPID_VALUE 0xFA - -#define BMI055_ACC_BGW_CHIPID 0x00 -/**
-#include -#include - -#include "mpu6050_sensor.h" - - -const static sensor_t _MPU6050_sensor[] = -{ - { - .name = "Accelerometer", - .vendor = "Invensense", - .version = sizeof(sensor_t), - .handle = 0, - .type = SENSOR_TYPE_ACCELEROMETER, - .maxRange = SENSOR_ACCEL_RANGE_16G, - .resolution = 1.0f, - .power = 0.5f, - .minDelay = 10000, - .fifoReservedEventCount = 0, - .fifoMaxEventCount = 64, - }, - { - .name = "Gyroscope", - .vendor = "Invensense", - .version = sizeof(sensor_t), - .handle = 0, - .type = SENSOR_TYPE_GYROSCOPE, - .maxRange = SENSOR_GYRO_RANGE_2000DPS, - .resolution = 1.0f, - .power = 0.5f, - .minDelay = 10000, - .fifoReservedEventCount = 0, - .fifoMaxEventCount = 64, - } -}; - -MPU6050::MPU6050(int sensor_type, const char* iic_bus, int addr) - : SensorBase(sensor_type) -{ - this->i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(iic_bus); - if (this->i2c_bus == NULL) - { - printf("MPU6050: No IIC device:%s\n", iic_bus); - return; - } - - this->i2c_addr = addr; - - /* register to sensor manager */ - SensorManager::registerSensor(this); -} - -int MPU6050::read_reg(rt_uint8_t reg, rt_uint8_t *value) -{ - struct rt_i2c_msg msgs[2]; - - msgs[0].addr = this->i2c_addr; - msgs[0].flags = RT_I2C_WR; - msgs[0].buf = ® - msgs[0].len = 1; - - msgs[1].addr = this->i2c_addr; - msgs[1].flags = RT_I2C_RD; /* Read from slave */ - msgs[1].buf = (rt_uint8_t *)value; - msgs[1].len = 1; - - if (rt_i2c_transfer(this->i2c_bus, msgs, 2) == 2) - return RT_EOK; - - return -RT_ERROR; -} - -int MPU6050::read_buffer(rt_uint8_t reg, rt_uint8_t* value, rt_size_t size) -{ - struct rt_i2c_msg msgs[2]; - - msgs[0].addr = this->i2c_addr; - msgs[0].flags = RT_I2C_WR; - msgs[0].buf = ® - msgs[0].len = 1; - - msgs[1].addr = this->i2c_addr; - msgs[1].flags = RT_I2C_RD; /* Read from slave */ - msgs[1].buf = (rt_uint8_t *)value; - msgs[1].len = size; - - if (rt_i2c_transfer(this->i2c_bus, msgs, 2) == 2) - return RT_EOK; - - return -RT_ERROR; -} - -int MPU6050::write_reg(rt_uint8_t reg, rt_uint8_t value) -{ - struct rt_i2c_msg msgs[2]; - - msgs[0].addr = this->i2c_addr; - msgs[0].flags = RT_I2C_WR; - msgs[0].buf = ® - msgs[0].len = 1; - - msgs[1].addr = this->i2c_addr; - msgs[1].flags = RT_I2C_WR | RT_I2C_NO_START; - msgs[1].buf = (rt_uint8_t *)&value; - msgs[1].len = 1; - - if (rt_i2c_transfer(this->i2c_bus, msgs, 2) == 2) - return RT_EOK; - - return -RT_ERROR; -} - - -MPU6050_Accelerometer::MPU6050_Accelerometer(const char* iic_name, int addr) - : MPU6050(SENSOR_TYPE_ACCELEROMETER, iic_name, addr) -{ - int index; - uint8_t id; - rt_uint8_t value[6] = {0}; - rt_int32_t x, y, z; - SensorConfig config = {SENSOR_MODE_NORMAL, SENSOR_DATARATE_400HZ, SENSOR_ACCEL_RANGE_2G}; - - /* initialize MPU6050 */ - write_reg(MPU6050_PWR_MGMT_1, 0x80); /* reset mpu6050 device */ - write_reg(MPU6050_SMPLRT_DIV, 0x00); /* Sample Rate = Gyroscope Output Rate / (1 + SMPLRT_DIV) */ - write_reg(MPU6050_PWR_MGMT_1, 0x03); /* Wake up device , set device clock Z axis gyroscope */ - write_reg(MPU6050_CONFIG, 0x03); /* set DLPF_CFG 42Hz */ - write_reg(MPU6050_GYRO_CONFIG, 0x18); /* set gyro 2000deg/s */ - write_reg(MPU6050_ACCEL_CONFIG, 0x08); /* set acc +-4g/s */ - - - x_offset = y_offset = z_offset = 0; - x = y = z = 0; - - /* read MPU6050 id */ - read_buffer(MPU6050_WHOAMI, &id, 1); - if (id != MPU6050_ID) - { - printf("Warning: not found MPU6050 id: %02x\n", id); - } - - /* get offset */ - for (index = 0; index < 200; index ++) - { - read_buffer(MPU6050_ACCEL_XOUT_H, value, 6); - - x += (((rt_int16_t)value[0] << 8) | value[1]); - y += (((rt_int16_t)value[2] << 8) | value[3]); - z += (((rt_int16_t)value[4] << 8) | value[5]); - } - x_offset = x / 200; - y_offset = y / 200; - z_offset = z / 200; - - this->enable = RT_FALSE; - this->sensitivity = SENSOR_ACCEL_SENSITIVITY_2G; - this->config = config; -} - -int -MPU6050_Accelerometer::configure(SensorConfig *config) -{ - int range; - uint8_t value; - - if (config == RT_NULL) return -1; - - /* TODO: set datarate */ - - /* get range and calc the sensitivity */ - range = config->range.accel_range; - switch (range) - { - case SENSOR_ACCEL_RANGE_2G: - this->sensitivity = SENSOR_ACCEL_SENSITIVITY_2G; - range = 0; - break; - case SENSOR_ACCEL_RANGE_4G: - this->sensitivity = SENSOR_ACCEL_SENSITIVITY_4G; - range = 0x01 << 2; - break; - case SENSOR_ACCEL_RANGE_8G: - this->sensitivity = SENSOR_ACCEL_SENSITIVITY_8G; - range = 0x02 << 2; - break; - case SENSOR_ACCEL_RANGE_16G: - this->sensitivity = SENSOR_ACCEL_SENSITIVITY_16G; - range = 0x03 << 2; - break; - default: - return -1; - } - - /* set range to sensor */ - read_reg(MPU6050_ACCEL_CONFIG, &value); - value &= ~(0x3 << 2); - value |= range; - write_reg(MPU6050_ACCEL_CONFIG, value); - - return 0; -} - -int -MPU6050_Accelerometer::activate(int enable) -{ - uint8_t value; - - if (enable && this->enable == RT_FALSE) - { - /* enable accelerometer */ - read_reg(MPU6050_PWR_MGMT_2, &value); - value &= ~(0x07 << 2); - write_reg(MPU6050_PWR_MGMT_2, value); - } - - if (!enable && this->enable == RT_TRUE) - { - /* disable accelerometer */ - read_reg(MPU6050_PWR_MGMT_2, &value); - value |= (0x07 << 2); - write_reg(MPU6050_PWR_MGMT_2, value); - } - - if (enable) this->enable = RT_TRUE; - else this->enable = RT_FALSE; - - return 0; -} - -int -MPU6050_Accelerometer::poll(sensors_event_t *event) -{ - rt_uint8_t value[6]; - rt_int16_t x, y, z; - - /* parameters check */ - if (event == NULL) return -1; - - /* get event data */ - event->version = sizeof(sensors_event_t); - event->sensor = (int32_t) this; - event->timestamp = rt_tick_get(); - event->type = SENSOR_TYPE_ACCELEROMETER; - - read_buffer(MPU6050_ACCEL_XOUT_H, value, 6); - - /* get raw data */ - x = (((rt_int16_t)value[0] << 8) | value[1]); - y = (((rt_int16_t)value[2] << 8) | value[3]); - z = (((rt_int16_t)value[4] << 8) | value[5]); - - if (config.mode == SENSOR_MODE_RAW) - { - event->raw_acceleration.x = x; - event->raw_acceleration.y = y; - event->raw_acceleration.z = z; - } - else - { - - x -= x_offset; y -= y_offset; z -= z_offset; - event->acceleration.x = x * this->sensitivity * SENSORS_GRAVITY_STANDARD; - event->acceleration.y = y * this->sensitivity * SENSORS_GRAVITY_STANDARD; - event->acceleration.z = z * this->sensitivity * SENSORS_GRAVITY_STANDARD; - } - - return 0; -} - -void -MPU6050_Accelerometer::getSensor(sensor_t *sensor) -{ - /* get sensor description */ - if (sensor) - { - memcpy(sensor, &_MPU6050_sensor[0], sizeof(sensor_t)); - } -} - -MPU6050_Gyroscope::MPU6050_Gyroscope(const char* iic_name, int addr) - : MPU6050(SENSOR_TYPE_GYROSCOPE, iic_name, addr) -{ - int index; - uint8_t id; - rt_uint8_t value[6]; - rt_int32_t x, y, z; - - /* initialize MPU6050 */ - write_reg(MPU6050_PWR_MGMT_1, 0x80); /* reset mpu6050 device */ - write_reg(MPU6050_SMPLRT_DIV, 0x00); /* Sample Rate = Gyroscope Output Rate / (1 + SMPLRT_DIV) */ - write_reg(MPU6050_PWR_MGMT_1, 0x03); /* Wake up device , set device clock Z axis gyroscope */ - write_reg(MPU6050_CONFIG, 0x03); /* set DLPF_CFG 42Hz */ - write_reg(MPU6050_GYRO_CONFIG, 0x18); /* set gyro 2000deg/s */ - write_reg(MPU6050_ACCEL_CONFIG, 0x08); /* set acc +-4g/s */ - - x_offset = y_offset = z_offset = 0; - x = y = z = 0; - - /* read MPU6050 id */ - read_reg(MPU6050_WHOAMI, &id); - if (id != MPU6050_ID) - { - printf("Warning: not found MPU6050 id: %02x\n", id); - } - - /* get offset */ - for (index = 0; index < 200; index ++) - { - read_buffer(MPU6050_GYRO_XOUT_H, value, 6); - - x += (((rt_int16_t)value[0] << 8) | value[1]); - y += (((rt_int16_t)value[2] << 8) | value[3]); - z += (((rt_int16_t)value[4] << 8) | value[5]); - } - x_offset = x / 200; - y_offset = y / 200; - z_offset = z / 200; - - this->enable = RT_FALSE; - this->sensitivity = SENSOR_GYRO_SENSITIVITY_250DPS; -} - -int -MPU6050_Gyroscope::configure(SensorConfig *config) -{ - int range; - uint8_t value; - - if (config == RT_NULL) return -1; - - /* TODO: set datarate */ - - /* get range and calc the sensitivity */ - range = config->range.gyro_range; - switch (range) - { - case SENSOR_GYRO_RANGE_250DPS: - this->sensitivity = SENSOR_GYRO_SENSITIVITY_250DPS; - range = 0; - break; - case SENSOR_GYRO_RANGE_500DPS: - this->sensitivity = SENSOR_GYRO_SENSITIVITY_500DPS; - range = 0x01 << 2; - break; - case SENSOR_GYRO_RANGE_1000DPS: - this->sensitivity = SENSOR_GYRO_SENSITIVITY_1000DPS; - range = 0x02 << 2; - break; - case SENSOR_GYRO_RANGE_2000DPS: - this->sensitivity = SENSOR_GYRO_SENSITIVITY_2000DPS; - range = 0x03 << 2; - break; - default: - return -1; - } - - /* set range to sensor */ - read_reg(MPU6050_GYRO_CONFIG, &value); - value &= ~(0x3 << 2); - value |= range; - write_reg(MPU6050_GYRO_CONFIG, value); - - return 0; -} - -int -MPU6050_Gyroscope::activate(int enable) -{ - uint8_t value; - - if (enable && this->enable == RT_FALSE) - { - /* enable gyroscope */ - read_reg(MPU6050_PWR_MGMT_1, &value); - value &= ~(0x01 << 4); - write_reg(MPU6050_PWR_MGMT_1, value); - - read_reg(MPU6050_PWR_MGMT_2, &value); - value &= ~(0x07 << 0); - write_reg(MPU6050_PWR_MGMT_2, value); - } - - if (!enable && this->enable == RT_TRUE) - { - /* disable gyroscope */ - read_reg(MPU6050_PWR_MGMT_2, &value); - value |= (0x07 << 0); - write_reg(MPU6050_PWR_MGMT_2, value); - } - - if (enable) this->enable = RT_TRUE; - else this->enable = RT_FALSE; - - return 0; -} - -int -MPU6050_Gyroscope::poll(sensors_event_t *event) -{ - rt_uint8_t value[6]; - rt_int16_t x, y, z; - - /* parameters check */ - if (event == NULL) return -1; - - /* get event data */ - event->version = sizeof(sensors_event_t); - event->sensor = (int32_t) this; - event->timestamp = rt_tick_get(); - event->type = SENSOR_TYPE_GYROSCOPE; - - read_buffer(MPU6050_GYRO_XOUT_H, value, 6); - - /* get raw data */ - x = (((rt_int16_t)value[0] << 8) | value[1]); - y = (((rt_int16_t)value[2] << 8) | value[3]); - z = (((rt_int16_t)value[4] << 8) | value[5]); - - - if (config.mode == SENSOR_MODE_RAW) - { - event->raw_gyro.x = x; - event->raw_gyro.y = y; - event->raw_gyro.z = z; - } - else - { - x -= x_offset; y -= y_offset; z -= z_offset; - event->gyro.x = x * this->sensitivity * SENSORS_DPS_TO_RADS; - event->gyro.y = y * this->sensitivity * SENSORS_DPS_TO_RADS; - event->gyro.z = z * this->sensitivity * SENSORS_DPS_TO_RADS; - } - - return 0; -} - -void -MPU6050_Gyroscope::getSensor(sensor_t *sensor) -{ - /* get sensor description */ - if (sensor) - { - memcpy(sensor, &_MPU6050_sensor[1], sizeof(sensor_t)); - } -} - diff --git a/components/drivers/sensors/mpu6050_sensor.h b/components/drivers/sensors/mpu6050_sensor.h deleted file mode 100644 index 8f70c3e5ed..0000000000 --- a/components/drivers/sensors/mpu6050_sensor.h +++ /dev/null @@ -1,189 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2014-12-20 Bernard the first version - * 2015-1-11 RT_learning modify the mpu6050 ID - */ - -#ifndef MPU6050_SENSOR_H__ -#define MPU6050_SENSOR_H__ - -#include - -#define MPU6050_ADDRESS_AD0_LOW 0x68 // address pin low (GND), default for InvenSense evaluation board -#define MPU6050_ADDRESS_AD0_HIGH 0x69 // address pin high (VCC) -#define MPU6050_DEFAULT_ADDRESS MPU6050_ADDRESS_AD0_LOW - -#define MPU6050_XG_OFFS_TC 0x00 -#define MPU6050_YG_OFFS_TC 0x01 -#define MPU6050_ZG_OFFS_TC 0x02 -#define MPU6050_X_FINE_GAIN 0x03 -#define MPU6050_Y_FINE_GAIN 0x04 -#define MPU6050_Z_FINE_GAIN 0x05 -#define MPU6050_XA_OFFS_H 0x06 -#define MPU6050_XA_OFFS_L 0x07 -#define MPU6050_YA_OFFS_H 0x08 -#define MPU6050_YA_OFFS_L 0x09 -#define MPU6050_ZA_OFFS_H 0x0A -#define MPU6050_ZA_OFFS_L 0x0B -#define MPU6050_PRODUCT_ID 0x0C -#define MPU6050_SELF_TEST_X 0x0D -#define MPU6050_SELF_TEST_Y 0x0E -#define MPU6050_SELF_TEST_Z 0x0F -#define MPU6050_SELF_TEST_A 0x10 -#define MPU6050_XG_OFFS_USRH 0x13 -#define MPU6050_XG_OFFS_USRL 0x14 -#define MPU6050_YG_OFFS_USRH 0x15 -#define MPU6050_YG_OFFS_USRL 0x16 -#define MPU6050_ZG_OFFS_USRH 0x17 -#define MPU6050_ZG_OFFS_USRL 0x18 -#define MPU6050_SMPLRT_DIV 0x19 -#define MPU6050_CONFIG 0x1A -#define MPU6050_GYRO_CONFIG 0x1B -#define MPU6050_ACCEL_CONFIG 0x1C -#define MPU6050_ACCEL_CONFIG_2 0x1D -#define MPU6050_LP_ACCEL_ODR 0x1E -#define MPU6050_MOT_THR 0x1F -#define MPU6050_FIFO_EN 0x23 -#define MPU6050_I2C_MST_CTRL 0x24 -#define MPU6050_I2C_SLV0_ADDR 0x25 -#define MPU6050_I2C_SLV0_REG 0x26 -#define MPU6050_I2C_SLV0_CTRL 0x27 -#define MPU6050_I2C_SLV1_ADDR 0x28 -#define MPU6050_I2C_SLV1_REG 0x29 -#define MPU6050_I2C_SLV1_CTRL 0x2A -#define MPU6050_I2C_SLV2_ADDR 0x2B -#define MPU6050_I2C_SLV2_REG 0x2C -#define MPU6050_I2C_SLV2_CTRL 0x2D -#define MPU6050_I2C_SLV3_ADDR 0x2E -#define MPU6050_I2C_SLV3_REG 0x2F -#define MPU6050_I2C_SLV3_CTRL 0x30 -#define MPU6050_I2C_SLV4_ADDR 0x31 -#define MPU6050_I2C_SLV4_REG 0x32 -#define MPU6050_I2C_SLV4_DO 0x33 -#define MPU6050_I2C_SLV4_CTRL 0x34 -#define MPU6050_I2C_SLV4_DI 0x35 -#define MPU6050_I2C_MST_STATUS 0x36 -#define MPU6050_INT_PIN_CFG 0x37 -#define MPU6050_INT_ENABLE 0x38 -#define MPU6050_ACCEL_XOUT_H 0x3B -#define MPU6050_ACCEL_XOUT_L 0x3C -#define MPU6050_ACCEL_YOUT_H 0x3D -#define MPU6050_ACCEL_YOUT_L 0x3E -#define MPU6050_ACCEL_ZOUT_H 0x3F -#define MPU6050_ACCEL_ZOUT_L 0x40 -#define MPU6050_TEMP_OUT_H 0x41 -#define MPU6050_TEMP_OUT_L 0x42 -#define MPU6050_GYRO_XOUT_H 0x43 -#define MPU6050_GYRO_XOUT_L 0x44 -#define MPU6050_GYRO_YOUT_H 0x45 -#define MPU6050_GYRO_YOUT_L 0x46 -#define MPU6050_GYRO_ZOUT_H 0x47 -#define MPU6050_GYRO_ZOUT_L 0x48 -#define MPU6050_EXT_SENS_DATA_00 0x49 -#define MPU6050_EXT_SENS_DATA_01 0x4A -#define MPU6050_EXT_SENS_DATA_02 0x4B -#define MPU6050_EXT_SENS_DATA_03 0x4C -#define MPU6050_EXT_SENS_DATA_04 0x4D -#define MPU6050_EXT_SENS_DATA_05 0x4E -#define MPU6050_EXT_SENS_DATA_06 0x4F -#define MPU6050_EXT_SENS_DATA_07 0x50 -#define MPU6050_EXT_SENS_DATA_08 0x51 -#define MPU6050_EXT_SENS_DATA_09 0x52 -#define MPU6050_EXT_SENS_DATA_10 0x53 -#define MPU6050_EXT_SENS_DATA_11 0x54 -#define MPU6050_EXT_SENS_DATA_12 0x55 -#define MPU6050_EXT_SENS_DATA_13 0x56 -#define MPU6050_EXT_SENS_DATA_14 0x57 -#define MPU6050_EXT_SENS_DATA_15 0x58 -#define MPU6050_EXT_SENS_DATA_16 0x59 -#define MPU6050_EXT_SENS_DATA_17 0x5A -#define MPU6050_EXT_SENS_DATA_18 0x5B -#define MPU6050_EXT_SENS_DATA_19 0x5C -#define MPU6050_EXT_SENS_DATA_20 0x5D -#define MPU6050_EXT_SENS_DATA_21 0x5E -#define MPU6050_EXT_SENS_DATA_22 0x5F -#define MPU6050_EXT_SENS_DATA_23 0x60 -#define MPU6050_I2C_SLV0_DO 0x63 -#define MPU6050_I2C_SLV1_DO 0x64 -#define MPU6050_I2C_SLV2_DO 0x65 -#define MPU6050_I2C_SLV3_DO 0x66 -#define MPU6050_I2C_MST_DELAY_CTRL 0x67 -#define MPU6050_SIGNAL_PATH_RESET 0x68 -#define MPU6050_MOT_DETECT_CTRL 0x69 -#define MPU6050_USER_CTRL 0x6A -#define MPU6050_PWR_MGMT_1 0x6B -#define MPU6050_PWR_MGMT_2 0x6C -#define MPU6050_BANK_SEL 0x6D -#define MPU6050_MEM_START_ADDR 0x6E -#define MPU6050_MEM_R_W 0x6F -#define MPU6050_DMP_CFG_1 0x70 -#define MPU6050_DMP_CFG_2 0x71 -#define MPU6050_FIFO_COUNTH 0x72 -#define MPU6050_FIFO_COUNTL 0x73 -#define MPU6050_FIFO_R_W 0x74 -#define MPU6050_WHOAMI 0x75 -#define MPU6050_XA_OFFSET_H 0x77 -#define MPU6050_XA_OFFSET_L 0x78 -#define MPU6050_YA_OFFSET_H 0x7A -#define MPU6050_YA_OFFSET_L 0x7B -#define MPU6050_ZA_OFFSET_H 0x7D -#define MPU6050_ZA_OFFSET_L 0x7E - -#define MPU6050_ID 0x68 - -class MPU6050 :public SensorBase -{ -public: - MPU6050(int sensor_type, const char* iic_bus, int addr); - - int read_reg(rt_uint8_t reg, rt_uint8_t* value); - int write_reg(rt_uint8_t reg, rt_uint8_t value); - int read_buffer(rt_uint8_t reg, rt_uint8_t* value, rt_size_t size); - -private: - struct rt_i2c_bus_device *i2c_bus; - int i2c_addr; -}; - -class MPU6050_Accelerometer:public MPU6050 -{ -public: - MPU6050_Accelerometer(const char* iic_name, int addr); - - virtual int configure(SensorConfig *config); - virtual int activate(int enable); - - virtual int poll(sensors_event_t *event); - virtual void getSensor(sensor_t *sensor); - -private: - rt_int16_t x_offset, y_offset, z_offset; - - rt_bool_t enable; - float sensitivity; -}; - -class MPU6050_Gyroscope:public MPU6050 -{ -public: - MPU6050_Gyroscope(const char* iic_name, int addr); - - virtual int configure(SensorConfig *config); - virtual int activate(int enable); - - virtual int poll(sensors_event_t *event); - virtual void getSensor(sensor_t *sensor); - -private: - rt_int16_t x_offset, y_offset, z_offset; - - rt_bool_t enable; - float sensitivity; -}; - -#endif diff --git a/components/drivers/sensors/sensor.c b/components/drivers/sensors/sensor.c new file mode 100644 index 0000000000..80c9a60ae0 --- /dev/null +++ b/components/drivers/sensors/sensor.c @@ -0,0 +1,449 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-01-31 flybreak first version + */ + +#include "sensor.h" + +#define DBG_ENABLE +#define DBG_LEVEL DBG_INFO +#define DBG_SECTION_NAME "sensor" +#define DBG_COLOR +#include + +#include + +static char *const sensor_name_str[] = +{ + "none", + "acce_", /* Accelerometer */ + "gyro_", /* Gyroscope */ + "mag_", /* Magnetometer */ + "temp_", /* Temperature */ + "humi_", /* Relative Humidity */ + "baro_", /* Barometer */ + "li_", /* Ambient light */ + "pr_", /* Proximity */ + "hr_", /* Heart Rate */ + "tvoc_", /* TVOC Level */ + "noi_", /* Noise Loudness */ + "step_" /* Step sensor */ +}; + +/* Sensor interrupt correlation function */ +/* + * Sensor interrupt handler function + */ +void rt_sensor_cb(rt_sensor_t sen) +{ + if (sen->parent.rx_indicate == RT_NULL) + { + return; + } + + /* The buffer is not empty. Read the data in the buffer first */ + if (sen->data_len > 0) + { + sen->parent.rx_indicate(&sen->parent, sen->data_len / sizeof(struct rt_sensor_data)); + } + else if (sen->config.mode == RT_SEN_MODE_INT) + { + /* The interrupt mode only produces one data at a time */ + sen->parent.rx_indicate(&sen->parent, 1); + } + else if (sen->config.mode == RT_SEN_MODE_FIFO) + { + sen->parent.rx_indicate(&sen->parent, sen->info.fifo_max); + } +} + +/* ISR for sensor interrupt */ +static void irq_callback(void *args) +{ + rt_sensor_t sensor = args; + rt_uint8_t i; + + if (sensor->module) + { + /* Invoke a callback for all sensors in the module */ + for (i = 0; i < sensor->module->sen_num; i++) + { + rt_sensor_cb(sensor->module->sen[i]); + } + } + else + { + rt_sensor_cb(sensor); + } +} + +/* Sensor interrupt initialization function */ +static rt_err_t rt_sensor_irq_init(rt_sensor_t sensor) +{ + if (sensor->config.irq_pin.pin == RT_PIN_NONE) + { + return -RT_EINVAL; + } + + rt_pin_mode(sensor->config.irq_pin.pin, sensor->config.irq_pin.mode); + + if (sensor->config.irq_pin.mode == PIN_MODE_INPUT_PULLDOWN) + { + rt_pin_attach_irq(sensor->config.irq_pin.pin, PIN_IRQ_MODE_RISING, irq_callback, (void *)sensor); + } + else if (sensor->config.irq_pin.mode == PIN_MODE_INPUT_PULLUP) + { + rt_pin_attach_irq(sensor->config.irq_pin.pin, PIN_IRQ_MODE_FALLING, irq_callback, (void *)sensor); + } + else if (sensor->config.irq_pin.mode == PIN_MODE_INPUT) + { + rt_pin_attach_irq(sensor->config.irq_pin.pin, PIN_IRQ_MODE_RISING_FALLING, irq_callback, (void *)sensor); + } + + rt_pin_irq_enable(sensor->config.irq_pin.pin, RT_TRUE); + + LOG_I("interrupt init success"); + + return 0; +} + +/* Sensor interrupt enable */ +static void rt_sensor_irq_enable(rt_sensor_t sensor) +{ + if (sensor->config.irq_pin.pin != RT_PIN_NONE) + { + rt_pin_irq_enable(sensor->config.irq_pin.pin, RT_TRUE); + } +} + +/* Sensor interrupt disable */ +static void rt_sensor_irq_disable(rt_sensor_t sensor) +{ + if (sensor->config.irq_pin.pin != RT_PIN_NONE) + { + rt_pin_irq_enable(sensor->config.irq_pin.pin, RT_FALSE); + } +} + +/* RT-Thread Device Interface */ + +static rt_err_t rt_sensor_init(rt_device_t dev) +{ + rt_sensor_t sensor = (rt_sensor_t)dev; + RT_ASSERT(dev != RT_NULL); + + if (sensor->module != RT_NULL && sensor->info.fifo_max > 0 && sensor->data_buf == RT_NULL) + { + /* Allocate memory for the sensor buffer */ + sensor->data_buf = rt_malloc(sizeof(struct rt_sensor_data) * sensor->info.fifo_max); + if (sensor->data_buf == RT_NULL) + { + return -RT_ENOMEM; + } + } + + return RT_EOK; +} + +static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag) +{ + rt_sensor_t sensor = (rt_sensor_t)dev; + RT_ASSERT(dev != RT_NULL); + + if (sensor->module) + { + /* take the module mutex */ + rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER); + } + + if (oflag & RT_DEVICE_FLAG_RDONLY && dev->flag & RT_DEVICE_FLAG_RDONLY) + { + /* If polling mode is supported, configure it to polling mode */ + if (sensor->ops->control(sensor, RT_SEN_CTRL_SET_MODE, (void *)RT_SEN_MODE_POLLING) == RT_EOK) + { + sensor->config.mode = RT_SEN_MODE_POLLING; + } + } + else if (oflag & RT_DEVICE_FLAG_INT_RX && dev->flag & RT_DEVICE_FLAG_INT_RX) + { + /* If interrupt mode is supported, configure it to interrupt mode */ + if (sensor->ops->control(sensor, RT_SEN_CTRL_SET_MODE, (void *)RT_SEN_MODE_INT) == RT_EOK) + { + sensor->config.mode = RT_SEN_MODE_INT; + /* Initialization sensor interrupt */ + rt_sensor_irq_init(sensor); + } + } + else if (oflag & RT_SEN_FLAG_FIFO && dev->flag & RT_SEN_FLAG_FIFO) + { + /* If fifo mode is supported, configure it to fifo mode */ + if (sensor->ops->control(sensor, RT_SEN_CTRL_SET_MODE, (void *)RT_SEN_MODE_FIFO) == RT_EOK) + { + sensor->config.mode = RT_SEN_MODE_FIFO; + /* Initialization sensor interrupt */ + rt_sensor_irq_init(sensor); + } + } + else + { + return -RT_EINVAL; + } + + /* Configure power mode to normal mode */ + if (sensor->ops->control(sensor, RT_SEN_CTRL_SET_POWER, (void *)RT_SEN_POWER_NORMAL) == RT_EOK) + { + sensor->config.power = RT_SEN_POWER_NORMAL; + } + + if (sensor->module) + { + /* release the module mutex */ + rt_mutex_release(sensor->module->lock); + } + + return RT_EOK; +} + +static rt_err_t rt_sensor_close(rt_device_t dev) +{ + rt_sensor_t sensor = (rt_sensor_t)dev; + RT_ASSERT(dev != RT_NULL); + + if (sensor->module) + { + rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER); + } + + /* Configure power mode to power down mode */ + if (sensor->ops->control(sensor, RT_SEN_CTRL_SET_POWER, (void *)RT_SEN_POWER_DOWN) == RT_EOK) + { + sensor->config.power = RT_SEN_POWER_DOWN; + } + + /* Sensor disable interrupt */ + rt_sensor_irq_disable(sensor); + + if (sensor->module) + { + rt_mutex_release(sensor->module->lock); + } + + return RT_EOK; +} + +static rt_size_t rt_sensor_read(rt_device_t dev, rt_off_t pos, void *buf, rt_size_t len) +{ + rt_sensor_t sensor = (rt_sensor_t)dev; + rt_size_t result = 0; + RT_ASSERT(dev != RT_NULL); + + if (buf == NULL || len == 0) + { + return 0; + } + + if (sensor->module) + { + rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER); + } + + /* The buffer is not empty. Read the data in the buffer first */ + if (sensor->data_len > 0) + { + if (len > sensor->data_len / sizeof(struct rt_sensor_data)) + { + len = sensor->data_len / sizeof(struct rt_sensor_data); + } + + rt_memcpy(buf, sensor->data_buf, len * sizeof(struct rt_sensor_data)); + + /* Clear the buffer */ + sensor->data_len = 0; + result = len; + } + else + { + /* If the buffer is empty read the data */ + result = sensor->ops->fetch_data(sensor, buf, len); + } + + if (sensor->module) + { + rt_mutex_release(sensor->module->lock); + } + + return result; +} + +static rt_err_t rt_sensor_control(rt_device_t dev, int cmd, void *args) +{ + rt_sensor_t sensor = (rt_sensor_t)dev; + rt_err_t result = RT_EOK; + RT_ASSERT(dev != RT_NULL); + + if (sensor->module) + { + rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER); + } + + switch (cmd) + { + case RT_SEN_CTRL_GET_ID: + if (args) + { + sensor->ops->control(sensor, RT_SEN_CTRL_GET_ID, args); + } + break; + case RT_SEN_CTRL_GET_INFO: + if (args) + { + rt_memcpy(args, &sensor->info, sizeof(struct rt_sensor_info)); + } + break; + case RT_SEN_CTRL_SET_RANGE: + + /* Configuration measurement range */ + result = sensor->ops->control(sensor, RT_SEN_CTRL_SET_RANGE, args); + if (result == RT_EOK) + { + sensor->config.range = (rt_int32_t)args; + LOG_D("set range %d", sensor->config.range); + } + break; + case RT_SEN_CTRL_SET_ODR: + + /* Configuration data output rate */ + result = sensor->ops->control(sensor, RT_SEN_CTRL_SET_ODR, args); + if (result == RT_EOK) + { + sensor->config.odr = (rt_uint32_t)args & 0xFFFF; + LOG_D("set odr %d", sensor->config.odr); + } + break; + case RT_SEN_CTRL_SET_MODE: + + /* Configuration sensor work mode */ + result = sensor->ops->control(sensor, RT_SEN_CTRL_SET_MODE, args); + if (result == RT_EOK) + { + sensor->config.mode = (rt_uint32_t)args & 0xFF; + LOG_D("set work mode code:", sensor->config.mode); + + if (sensor->config.mode == RT_SEN_MODE_POLLING) + { + rt_sensor_irq_disable(sensor); + } + else if (sensor->config.mode == RT_SEN_MODE_INT || sensor->config.mode == RT_SEN_MODE_FIFO) + { + rt_sensor_irq_enable(sensor); + } + } + break; + case RT_SEN_CTRL_SET_POWER: + + /* Configuration sensor power mode */ + result = sensor->ops->control(sensor, RT_SEN_CTRL_SET_POWER, args); + if (result == RT_EOK) + { + sensor->config.power = (rt_uint32_t)args & 0xFF; + LOG_D("set power mode code:", sensor->config.power); + } + break; + case RT_SEN_CTRL_SELF_TEST: + + /* Device self-test */ + result = sensor->ops->control(sensor, RT_SEN_CTRL_SELF_TEST, args); + break; + default: + return -RT_ERROR; + } + + if (sensor->module) + { + rt_mutex_release(sensor->module->lock); + } + + return result; +} + +#ifdef RT_USING_DEVICE_OPS +const static struct rt_device_ops rt_sensor_ops = +{ + rt_sensor_init, + rt_sensor_open, + rt_sensor_close, + rt_sensor_read, + RT_NULL, + rt_sensor_control +}; +#endif + +/* + * sensor register + */ +int rt_hw_sensor_register(rt_sensor_t sensor, + const char *name, + rt_uint32_t flag, + void *data) +{ + rt_int8_t result; + rt_device_t device; + RT_ASSERT(sensor != RT_NULL); + + char *sensor_name = RT_NULL, *device_name = RT_NULL; + + /* Add a type name for the sensor device */ + sensor_name = sensor_name_str[sensor->info.type]; + device_name = rt_calloc(1, rt_strlen(sensor_name) + 1 + rt_strlen(name)); + if (device_name == RT_NULL) + { + LOG_E("device_name calloc failed!"); + return -RT_ERROR; + } + + rt_memcpy(device_name, sensor_name, rt_strlen(sensor_name) + 1); + strcat(device_name, name); + + if (sensor->module != RT_NULL && sensor->module->lock == RT_NULL) + { + /* Create a mutex lock for the module */ + sensor->module->lock = rt_mutex_create(name, RT_IPC_FLAG_FIFO); + if (sensor->module->lock == RT_NULL) + { + rt_free(device_name); + return -RT_ERROR; + } + } + + device = &sensor->parent; + +#ifdef RT_USING_DEVICE_OPS + device->ops = &rt_sensor_ops; +#else + device->init = rt_sensor_init; + device->open = rt_sensor_open; + device->close = rt_sensor_close; + device->read = rt_sensor_read; + device->write = RT_NULL; + device->control = rt_sensor_control; +#endif + device->type = RT_Device_Class_Sensor; + device->rx_indicate = RT_NULL; + device->tx_complete = RT_NULL; + device->user_data = data; + + result = rt_device_register(device, device_name, flag | RT_DEVICE_FLAG_STANDALONE); + if (result != RT_EOK) + { + LOG_E("rt_sensor register err code: %d", result); + return result; + } + + LOG_I("rt_sensor init success"); + return RT_EOK; +} diff --git a/components/drivers/sensors/sensor.cpp b/components/drivers/sensors/sensor.cpp deleted file mode 100644 index f986510f63..0000000000 --- a/components/drivers/sensors/sensor.cpp +++ /dev/null @@ -1,228 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2014-08-03 Bernard the first version - */ - -#include -#include - -#include "sensor.h" - -/** - * SensorBase - */ -SensorBase::SensorBase(int type) -{ - memset(&(this->config), 0x0, sizeof(SensorConfig)); - - this->type = type; - this->next = this->prev = NULL; - subscribe(NULL, NULL); -} - -SensorBase::~SensorBase() -{ -} - -int SensorBase::getType(void) -{ - return this->type; -} - -int SensorBase::setConfig(SensorConfig *config) -{ - int result; - - /* configure to the low level sensor */ - result = this->configure(config); - if (result == 0) - { - this->config = *config; - } - - return result; -} - -int SensorBase::getConfig(SensorConfig *config) -{ - *config = this->config; - return 0; -} - -int SensorBase::subscribe(SensorEventHandler_t handler, void *user_data) -{ - this->evtHandler = handler; - this->userData = user_data; - - return 0; -} - -int SensorBase::publish(void) -{ - if (this->evtHandler != NULL) - { - /* invoke subscribed handler */ - (*evtHandler)(this->userData); - } - - return 0; -} - -/** - * Sensor Manager - */ -/* sensors list */ -static SensorBase *sensor_list = NULL; - -SensorManager::SensorManager() -{ -} - -SensorManager::~SensorManager() -{ -} - -int SensorManager::registerSensor(SensorBase *sensor) -{ - RT_ASSERT(sensor != RT_NULL); - - /* add sensor into the list */ - if (sensor_list == NULL) - { - sensor->prev = sensor->next = sensor; - } - else - { - sensor_list->prev->next = sensor; - sensor->prev = sensor_list->prev; - - sensor_list->prev = sensor; - sensor->next = sensor_list; - } - - /* point the sensorList to this sensor */ - sensor_list = sensor; - - return 0; -} - -int SensorManager::unregisterSensor(SensorBase *sensor) -{ - /* disconnect sensor list */ - sensor->next->prev = sensor->prev; - sensor->prev->next = sensor->next; - - /* check the sensorList */ - if (sensor == sensor_list) - { - if (sensor->next == sensor) sensor_list = NULL; /* empty list */ - else sensor_list = sensor->next; - } - - /* re-initialize sensor node */ - sensor->next = sensor->prev = sensor; - - return 0; -} - -SensorBase *SensorManager::getDefaultSensor(int type) -{ - SensorBase *sensor = sensor_list; - - if (sensor == NULL) return NULL; - - do - { - /* find the same type */ - if (sensor->getType() == type) return sensor; - - sensor = sensor->next; - }while (sensor != sensor_list); - - return NULL; -} - -int SensorManager::subscribe(int type, SensorEventHandler_t handler, void *user_data) -{ - SensorBase *sensor; - - sensor = SensorManager::getDefaultSensor(type); - if (sensor != NULL) - { - sensor->subscribe(handler, user_data); - return 0; - } - - return -1; -} - -int SensorManager::sensorEventReady(SensorBase *sensor) -{ - return 0; -} - -int SensorManager::pollSensor(SensorBase *sensor, sensors_event_t *events, int number, int duration) -{ - rt_tick_t tick; - int result, index; - - if (sensor == NULL) return -1; - - tick = rt_tick_get(); - for (index = 0; index < number; index ++) - { - result = sensor->poll(&events[index]); - if (result < 0) break; - - if (rt_tick_get() - tick > duration) break; - } - - return index; -} - -rt_sensor_t rt_sensor_get_default(int type) -{ - return (rt_sensor_t)SensorManager::getDefaultSensor(type); -} - -int rt_sensor_subscribe(rt_sensor_t sensor, SensorEventHandler_t handler, void *user_data) -{ - SensorBase *sensor_base; - if (sensor == NULL) return -1; - - sensor_base = (SensorBase*)sensor; - - return sensor_base->subscribe(handler, user_data); -} - -int rt_sensor_poll(rt_sensor_t sensor, sensors_event_t *event) -{ - SensorBase *sensor_base; - if (sensor == NULL || event == NULL) return -1; - - sensor_base = (SensorBase*)sensor; - return sensor_base->poll(event); -} - -int rt_sensor_configure(rt_sensor_t sensor, SensorConfig *config) -{ - SensorBase *sensor_base; - if (sensor == NULL || config == NULL) return -1; - - sensor_base = (SensorBase*)sensor; - return sensor_base->setConfig(config); -} - -int rt_sensor_activate(rt_sensor_t sensor, int enable) -{ - SensorBase *sensor_base; - if (sensor == NULL) return -1; - - sensor_base = (SensorBase*)sensor; - return sensor_base->activate(enable); -} diff --git a/components/drivers/sensors/sensor.h b/components/drivers/sensors/sensor.h index 916360d34b..15dd79ccdf 100644 --- a/components/drivers/sensors/sensor.h +++ b/components/drivers/sensors/sensor.h @@ -5,1125 +5,188 @@ * * Change Logs: * Date Author Notes - * 2014-08-03 Bernard the first version + * 2019-01-31 flybreak first version */ + +#ifndef __SENSOR_H__ +#define __SENSOR_H__ -/* Modified from: https://github.com/android/platform_hardware_libhardware/blob/master/include/hardware/sensors.h */ - -/* - * Copyright (C) 2012 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef SENSORS_H__ -#define SENSORS_H__ - +#include #include -#include -#ifdef __CC_ARM /* skip warning in armcc */ -#pragma anon_unions +#ifdef RT_USING_RTC +#define rt_sen_get_timestamp() time() /* API for the sensor to get the timestamp */ +#else +#define rt_sen_get_timestamp() rt_tick_get() /* API for the sensor to get the timestamp */ #endif -/** - * Handles must be higher than SENSORS_HANDLE_BASE and must be unique. - * A Handle identifies a given sensors. The handle is used to activate - * and/or deactivate sensors. - * In this version of the API there can only be 256 handles. - */ -#define SENSORS_HANDLE_BASE 0 -#define SENSORS_HANDLE_BITS 8 -#define SENSORS_HANDLE_COUNT (1<0 - * ^ - * | - * +-----------+--> y>0 - * | | - * | | - * | | - * | | / z<0 - * | | / - * | | / - * O-----------+/ - * |[] [ ] []/ - * +----------/+ y<0 - * / - * / - * |/ z>0 (toward the sky) - * - * O: Origin (x=0,y=0,z=0) - * - */ - -/* - * Interaction with suspend mode - * - * Unless otherwise noted, an enabled sensor shall not prevent the - * SoC to go into suspend mode. It is the responsibility of applications - * to keep a partial wake-lock should they wish to receive sensor - * events while the screen is off. While in suspend mode, and unless - * otherwise noted (batch mode, sensor particularities, ...), enabled sensors' - * events are lost. - * - * Note that conceptually, the sensor itself is not de-activated while in - * suspend mode -- it's just that the data it returns are lost. As soon as - * the SoC gets out of suspend mode, operations resume as usual. Of course, - * in practice sensors shall be disabled while in suspend mode to - * save power, unless batch mode is active, in which case they must - * continue fill their internal FIFO (see the documentation of batch() to - * learn how suspend interacts with batch mode). - * - * In batch mode, and only when the flag SENSORS_BATCH_WAKE_UPON_FIFO_FULL is - * set and supported, the specified sensor must be able to wake-up the SoC and - * be able to buffer at least 10 seconds worth of the requested sensor events. - * - * There are notable exceptions to this behavior, which are sensor-dependent - * (see sensor types definitions below) - * - * - * The sensor type documentation below specifies the wake-up behavior of - * each sensor: - * wake-up: yes this sensor must wake-up the SoC to deliver events - * wake-up: no this sensor shall not wake-up the SoC, events are dropped - * - */ - -/* - * Sensor type - * - * Each sensor has a type which defines what this sensor measures and how - * measures are reported. All types are defined below. - * - * Device manufacturers (OEMs) can define their own sensor types, for - * their private use by applications or services provided by them. Such - * sensor types are specific to an OEM and can't be exposed in the SDK. - * These types must start at SENSOR_TYPE_DEVICE_PRIVATE_BASE. - */ - -/* - * Base for device manufacturers private sensor types. - * These sensor types can't be exposed in the SDK. - */ -#define SENSOR_TYPE_DEVICE_PRIVATE_BASE 0x10000 - -/* - * Sensor fusion and virtual sensors - * - * Many sensor types are or can be implemented as virtual sensors from - * physical sensors on the device. For instance the rotation vector sensor, - * orientation sensor, step-detector, step-counter, etc... - * - * From the point of view of this API these virtual sensors MUST appear as - * real, individual sensors. It is the responsibility of the driver and HAL - * to make sure this is the case. - * - * In particular, all sensors must be able to function concurrently. - * For example, if defining both an accelerometer and a step counter, - * then both must be able to work concurrently. - */ - -/* - * Trigger modes - * - * Sensors can report events in different ways called trigger modes, - * each sensor type has one and only one trigger mode associated to it. - * Currently there are four trigger modes defined: - * - * continuous: events are reported at a constant rate defined by setDelay(). - * eg: accelerometers, gyroscopes. - * on-change: events are reported only if the sensor's value has changed. - * setDelay() is used to set a lower limit to the reporting - * period (minimum time between two events). - * The HAL must return an event immediately when an on-change - * sensor is activated. - * eg: proximity, light sensors - * one-shot: upon detection of an event, the sensor deactivates itself and - * then sends a single event. Order matters to avoid race - * conditions. No other event is sent until the sensor get - * reactivated. setDelay() is ignored. - * eg: significant motion sensor - * special: see details in the sensor type specification below - * - */ - -/* - * SENSOR_TYPE_META_DATA - * trigger-mode: n/a - * wake-up sensor: n/a - * - * NO SENSOR OF THAT TYPE MUST BE RETURNED (*get_sensors_list)() - * - * SENSOR_TYPE_META_DATA is a special token used to populate the - * sensors_meta_data_event structure. It doesn't correspond to a physical - * sensor. sensors_meta_data_event are special, they exist only inside - * the HAL and are generated spontaneously, as opposed to be related to - * a physical sensor. - * - * sensors_meta_data_event_t.version must be META_DATA_VERSION - * sensors_meta_data_event_t.sensor must be 0 - * sensors_meta_data_event_t.type must be SENSOR_TYPE_META_DATA - * sensors_meta_data_event_t.reserved must be 0 - * sensors_meta_data_event_t.timestamp must be 0 - * - * The payload is a meta_data_event_t, where: - * meta_data_event_t.what can take the following values: - * - * META_DATA_FLUSH_COMPLETE - * This event indicates that a previous (*flush)() call has completed for the sensor - * handle specified in meta_data_event_t.sensor. - * see (*flush)() for more details - * - * All other values for meta_data_event_t.what are reserved and - * must not be used. - * - */ -#define SENSOR_TYPE_META_DATA (0) - -/* - * SENSOR_TYPE_ACCELEROMETER - * trigger-mode: continuous - * wake-up sensor: no - * - * All values are in SI units (m/s^2) and measure the acceleration of the - * device minus the force of gravity. - * - * Acceleration sensors return sensor events for all 3 axes at a constant - * rate defined by setDelay(). - * - * x: Acceleration on the x-axis - * y: Acceleration on the y-axis - * z: Acceleration on the z-axis - * - * Note that the readings from the accelerometer include the acceleration - * due to gravity (which is opposite to the direction of the gravity vector). - * - * Examples: - * The norm of should be close to 0 when in free fall. - * - * When the device lies flat on a table and is pushed on its left side - * toward the right, the x acceleration value is positive. - * - * When the device lies flat on a table, the acceleration value is +9.81, - * which correspond to the acceleration of the device (0 m/s^2) minus the - * force of gravity (-9.81 m/s^2). - * - * When the device lies flat on a table and is pushed toward the sky, the - * acceleration value is greater than +9.81, which correspond to the - * acceleration of the device (+A m/s^2) minus the force of - * gravity (-9.81 m/s^2). - */ -#define SENSOR_TYPE_ACCELEROMETER (1) - -/* - * SENSOR_TYPE_GEOMAGNETIC_FIELD - * trigger-mode: continuous - * wake-up sensor: no - * - * All values are in micro-Tesla (uT) and measure the geomagnetic - * field in the X, Y and Z axis. - * - * Returned values include calibration mechanisms such that the vector is - * aligned with the magnetic declination and heading of the earth's - * geomagnetic field. - * - * Magnetic Field sensors return sensor events for all 3 axes at a constant - * rate defined by setDelay(). - */ -#define SENSOR_TYPE_GEOMAGNETIC_FIELD (2) -#define SENSOR_TYPE_MAGNETIC_FIELD SENSOR_TYPE_GEOMAGNETIC_FIELD - -/* - * SENSOR_TYPE_ORIENTATION - * trigger-mode: continuous - * wake-up sensor: no - * - * All values are angles in degrees. - * - * Orientation sensors return sensor events for all 3 axes at a constant - * rate defined by setDelay(). - * - * azimuth: angle between the magnetic north direction and the Y axis, around - * the Z axis (0<=azimuth<360). - * 0=North, 90=East, 180=South, 270=West - * - * pitch: Rotation around X axis (-180<=pitch<=180), with positive values when - * the z-axis moves toward the y-axis. - * - * roll: Rotation around Y axis (-90<=roll<=90), with positive values when - * the x-axis moves towards the z-axis. - * - * Note: For historical reasons the roll angle is positive in the clockwise - * direction (mathematically speaking, it should be positive in the - * counter-clockwise direction): - * - * Z - * ^ - * (+roll) .--> | - * / | - * | | roll: rotation around Y axis - * X <-------(.) - * Y - * note that +Y == -roll - * - * - * - * Note: This definition is different from yaw, pitch and roll used in aviation - * where the X axis is along the long side of the plane (tail to nose). - */ -#define SENSOR_TYPE_ORIENTATION (3) - -/* - * SENSOR_TYPE_GYROSCOPE - * trigger-mode: continuous - * wake-up sensor: no - * - * All values are in radians/second and measure the rate of rotation - * around the X, Y and Z axis. The coordinate system is the same as is - * used for the acceleration sensor. Rotation is positive in the - * counter-clockwise direction (right-hand rule). That is, an observer - * looking from some positive location on the x, y or z axis at a device - * positioned on the origin would report positive rotation if the device - * appeared to be rotating counter clockwise. Note that this is the - * standard mathematical definition of positive rotation and does not agree - * with the definition of roll given earlier. - * The range should at least be 17.45 rad/s (ie: ~1000 deg/s). - * - * automatic gyro-drift compensation is allowed but not required. - */ -#define SENSOR_TYPE_GYROSCOPE (4) - -/* - * SENSOR_TYPE_LIGHT - * trigger-mode: on-change - * wake-up sensor: no - * - * The light sensor value is returned in SI lux units. - */ -#define SENSOR_TYPE_LIGHT (5) - -/* - * SENSOR_TYPE_PRESSURE - * trigger-mode: continuous - * wake-up sensor: no - * - * The pressure sensor return the athmospheric pressure in hectopascal (hPa) - */ -#define SENSOR_TYPE_PRESSURE (6) - -/* SENSOR_TYPE_TEMPERATURE is deprecated in the HAL */ -#define SENSOR_TYPE_TEMPERATURE (7) - -/* - * SENSOR_TYPE_PROXIMITY - * trigger-mode: on-change - * wake-up sensor: yes - * - * The distance value is measured in centimeters. Note that some proximity - * sensors only support a binary "close" or "far" measurement. In this case, - * the sensor should report its maxRange value in the "far" state and a value - * less than maxRange in the "near" state. - */ -#define SENSOR_TYPE_PROXIMITY (8) - -/* - * SENSOR_TYPE_GRAVITY - * trigger-mode: continuous - * wake-up sensor: no - * - * A gravity output indicates the direction of and magnitude of gravity in - * the devices's coordinates. On Earth, the magnitude is 9.8 m/s^2. - * Units are m/s^2. The coordinate system is the same as is used for the - * acceleration sensor. When the device is at rest, the output of the - * gravity sensor should be identical to that of the accelerometer. - */ -#define SENSOR_TYPE_GRAVITY (9) - -/* - * SENSOR_TYPE_LINEAR_ACCELERATION - * trigger-mode: continuous - * wake-up sensor: no - * - * Indicates the linear acceleration of the device in device coordinates, - * not including gravity. - * - * The output is conceptually: - * output of TYPE_ACCELERATION - output of TYPE_GRAVITY - * - * Readings on all axes should be close to 0 when device lies on a table. - * Units are m/s^2. - * The coordinate system is the same as is used for the acceleration sensor. - */ -#define SENSOR_TYPE_LINEAR_ACCELERATION (10) - -/* - * SENSOR_TYPE_ROTATION_VECTOR - * trigger-mode: continuous - * wake-up sensor: no - * - * The rotation vector symbolizes the orientation of the device relative to the - * East-North-Up coordinates frame. It is usually obtained by integration of - * accelerometer, gyroscope and magnetometer readings. - * - * The East-North-Up coordinate system is defined as a direct orthonormal basis - * where: - * - X points east and is tangential to the ground. - * - Y points north and is tangential to the ground. - * - Z points towards the sky and is perpendicular to the ground. - * - * The orientation of the phone is represented by the rotation necessary to - * align the East-North-Up coordinates with the phone's coordinates. That is, - * applying the rotation to the world frame (X,Y,Z) would align them with the - * phone coordinates (x,y,z). - * - * The rotation can be seen as rotating the phone by an angle theta around - * an axis rot_axis to go from the reference (East-North-Up aligned) device - * orientation to the current device orientation. - * - * The rotation is encoded as the 4 (reordered) components of a unit quaternion: - * sensors_event_t.data[0] = rot_axis.x*sin(theta/2) - * sensors_event_t.data[1] = rot_axis.y*sin(theta/2) - * sensors_event_t.data[2] = rot_axis.z*sin(theta/2) - * sensors_event_t.data[3] = cos(theta/2) - * where - * - rot_axis.x,y,z are the North-East-Up coordinates of a unit length vector - * representing the rotation axis - * - theta is the rotation angle - * - * The quaternion must be of norm 1 (it is a unit quaternion). Failure to ensure - * this will cause erratic client behaviour. - * - * In addition, this sensor reports an estimated heading accuracy. - * sensors_event_t.data[4] = estimated_accuracy (in radians) - * The heading error must be less than estimated_accuracy 95% of the time - * - * This sensor must use a gyroscope and an accelerometer as main orientation - * change input. - * - * This sensor can also include magnetometer input to make up for gyro drift, - * but it cannot be implemented using only a magnetometer. - */ -#define SENSOR_TYPE_ROTATION_VECTOR (11) - -/* - * SENSOR_TYPE_RELATIVE_HUMIDITY - * trigger-mode: on-change - * wake-up sensor: no - * - * A relative humidity sensor measures relative ambient air humidity and - * returns a value in percent. - */ -#define SENSOR_TYPE_RELATIVE_HUMIDITY (12) - -/* - * SENSOR_TYPE_AMBIENT_TEMPERATURE - * trigger-mode: on-change - * wake-up sensor: no - * - * The ambient (room) temperature in degree Celsius. - */ -#define SENSOR_TYPE_AMBIENT_TEMPERATURE (13) - -/* - * SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED - * trigger-mode: continuous - * wake-up sensor: no - * - * Similar to SENSOR_TYPE_MAGNETIC_FIELD, but the hard iron calibration is - * reported separately instead of being included in the measurement. - * Factory calibration and temperature compensation should still be applied to - * the "uncalibrated" measurement. - * Separating away the hard iron calibration estimation allows the system to - * better recover from bad hard iron estimation. - * - * All values are in micro-Tesla (uT) and measure the ambient magnetic - * field in the X, Y and Z axis. Assumptions that the the magnetic field - * is due to the Earth's poles should be avoided. - * - * The uncalibrated_magnetic event contains - * - 3 fields for uncalibrated measurement: x_uncalib, y_uncalib, z_uncalib. - * Each is a component of the measured magnetic field, with soft iron - * and temperature compensation applied, but not hard iron calibration. - * These values should be continuous (no re-calibration should cause a jump). - * - 3 fields for hard iron bias estimates: x_bias, y_bias, z_bias. - * Each field is a component of the estimated hard iron calibration. - * They represent the offsets to apply to the calibrated readings to obtain - * uncalibrated readings (x_uncalib ~= x_calibrated + x_bias) - * These values are expected to jump as soon as the estimate of the hard iron - * changes, and they should be stable the rest of the time. - * - * If this sensor is present, then the corresponding - * SENSOR_TYPE_MAGNETIC_FIELD must be present and both must return the - * same sensor_t::name and sensor_t::vendor. - * - * Minimum filtering should be applied to this sensor. In particular, low pass - * filters should be avoided. - * - * See SENSOR_TYPE_MAGNETIC_FIELD for more information - */ -#define SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED (14) - -/* - * SENSOR_TYPE_GAME_ROTATION_VECTOR - * trigger-mode: continuous - * wake-up sensor: no - * - * Similar to SENSOR_TYPE_ROTATION_VECTOR, but not using the geomagnetic - * field. Therefore the Y axis doesn't point north, but instead to some other - * reference. That reference is allowed to drift by the same order of - * magnitude than the gyroscope drift around the Z axis. - * - * This sensor does not report an estimated heading accuracy: - * sensors_event_t.data[4] is reserved and should be set to 0 - * - * In the ideal case, a phone rotated and returning to the same real-world - * orientation should report the same game rotation vector - * (without using the earth's geomagnetic field). - * - * This sensor must be based on a gyroscope. It cannot be implemented using - * a magnetometer. - * - * see SENSOR_TYPE_ROTATION_VECTOR for more details - */ -#define SENSOR_TYPE_GAME_ROTATION_VECTOR (15) - -/* - * SENSOR_TYPE_GYROSCOPE_UNCALIBRATED - * trigger-mode: continuous - * wake-up sensor: no - * - * All values are in radians/second and measure the rate of rotation - * around the X, Y and Z axis. An estimation of the drift on each axis is - * reported as well. - * - * No gyro-drift compensation shall be performed. - * Factory calibration and temperature compensation should still be applied - * to the rate of rotation (angular speeds). - * - * The coordinate system is the same as is - * used for the acceleration sensor. Rotation is positive in the - * counter-clockwise direction (right-hand rule). That is, an observer - * looking from some positive location on the x, y or z axis at a device - * positioned on the origin would report positive rotation if the device - * appeared to be rotating counter clockwise. Note that this is the - * standard mathematical definition of positive rotation and does not agree - * with the definition of roll given earlier. - * The range should at least be 17.45 rad/s (ie: ~1000 deg/s). - * - * Content of an uncalibrated_gyro event: (units are rad/sec) - * x_uncalib : angular speed (w/o drift compensation) around the X axis - * y_uncalib : angular speed (w/o drift compensation) around the Y axis - * z_uncalib : angular speed (w/o drift compensation) around the Z axis - * x_bias : estimated drift around X axis in rad/s - * y_bias : estimated drift around Y axis in rad/s - * z_bias : estimated drift around Z axis in rad/s - * - * IMPLEMENTATION NOTES: - * - * If the implementation is not able to estimate the drift, then this - * sensor MUST NOT be reported by this HAL. Instead, the regular - * SENSOR_TYPE_GYROSCOPE is used without drift compensation. - * - * If this sensor is present, then the corresponding - * SENSOR_TYPE_GYROSCOPE must be present and both must return the - * same sensor_t::name and sensor_t::vendor. - */ -#define SENSOR_TYPE_GYROSCOPE_UNCALIBRATED (16) - -/* - * SENSOR_TYPE_SIGNIFICANT_MOTION - * trigger-mode: one-shot - * wake-up sensor: yes - * - * A sensor of this type triggers an event each time significant motion - * is detected and automatically disables itself. - * The only allowed value to return is 1.0. - * - * A significant motion is a motion that might lead to a change in the user - * location. - * Examples of such motions are: - * walking, biking, sitting in a moving car, coach or train. - * Examples of situations that should not trigger significant motion: - * - phone in pocket and person is not moving - * - phone is on a table, even if the table shakes a bit due to nearby traffic - * or washing machine - * - * A note on false positive / false negative / power consumption tradeoff - * - The goal of this sensor is to save power. - * - Triggering an event when the user is not moving (false positive) is costly - * in terms of power, so it should be avoided. - * - Not triggering an event when the user is moving (false negative) is - * acceptable as long as it is not done repeatedly. If the user has been - * walking for 10 seconds, not triggering an event within those 10 seconds - * is not acceptable. - * - * IMPORTANT NOTE: this sensor type is very different from other types - * in that it must work when the screen is off without the need of - * holding a partial wake-lock and MUST allow the SoC to go into suspend. - * When significant motion is detected, the sensor must awaken the SoC and - * the event be reported. - * - * If a particular hardware cannot support this mode of operation then this - * sensor type MUST NOT be reported by the HAL. ie: it is not acceptable - * to "emulate" this sensor in the HAL. - * - * The whole point of this sensor type is to save power by keeping the - * SoC in suspend mode when the device is at rest. - * - * When the sensor is not activated, it must also be deactivated in the - * hardware: it must not wake up the SoC anymore, even in case of - * significant motion. - * - * setDelay() has no effect and is ignored. - * Once a "significant motion" event is returned, a sensor of this type - * must disables itself automatically, as if activate(..., 0) had been called. - */ - -#define SENSOR_TYPE_SIGNIFICANT_MOTION (17) - -/* - * SENSOR_TYPE_STEP_DETECTOR - * trigger-mode: special - * wake-up sensor: no - * - * A sensor of this type triggers an event each time a step is taken - * by the user. The only allowed value to return is 1.0 and an event is - * generated for each step. Like with any other event, the timestamp - * indicates when the event (here the step) occurred, this corresponds to when - * the foot hit the ground, generating a high variation in acceleration. - * - * While this sensor operates, it shall not disrupt any other sensors, in - * particular, but not limited to, the accelerometer; which might very well - * be in use as well. - * - * This sensor must be low power. That is, if the step detection cannot be - * done in hardware, this sensor should not be defined. Also, when the - * step detector is activated and the accelerometer is not, only steps should - * trigger interrupts (not accelerometer data). - * - * setDelay() has no impact on this sensor type - */ - -#define SENSOR_TYPE_STEP_DETECTOR (18) - -/* - * SENSOR_TYPE_STEP_COUNTER - * trigger-mode: on-change - * wake-up sensor: no - * - * A sensor of this type returns the number of steps taken by the user since - * the last reboot while activated. The value is returned as a uint64_t and is - * reset to zero only on a system / android reboot. - * - * The timestamp of the event is set to the time when the first step - * for that event was taken. - * See SENSOR_TYPE_STEP_DETECTOR for the signification of the time of a step. - * - * The minimum size of the hardware's internal counter shall be 16 bits - * (this restriction is here to avoid too frequent wake-ups when the - * delay is very large). - * - * IMPORTANT NOTE: this sensor type is different from other types - * in that it must work when the screen is off without the need of - * holding a partial wake-lock and MUST allow the SoC to go into suspend. - * Unlike other sensors, while in suspend mode this sensor must stay active, - * no events are reported during that time but, steps continue to be - * accounted for; an event will be reported as soon as the SoC resumes if - * the timeout has expired. - * - * In other words, when the screen is off and the device allowed to - * go into suspend mode, we don't want to be woken up, regardless of the - * setDelay() value, but the steps shall continue to be counted. - * - * The driver must however ensure that the internal step count never - * overflows. It is allowed in this situation to wake the SoC up so the - * driver can do the counter maintenance. - * - * While this sensor operates, it shall not disrupt any other sensors, in - * particular, but not limited to, the accelerometer; which might very well - * be in use as well. - * - * If a particular hardware cannot support these modes of operation then this - * sensor type MUST NOT be reported by the HAL. ie: it is not acceptable - * to "emulate" this sensor in the HAL. - * - * This sensor must be low power. That is, if the step detection cannot be - * done in hardware, this sensor should not be defined. Also, when the - * step counter is activated and the accelerometer is not, only steps should - * trigger interrupts (not accelerometer data). - * - * The whole point of this sensor type is to save power by keeping the - * SoC in suspend mode when the device is at rest. - */ - -#define SENSOR_TYPE_STEP_COUNTER (19) - -/* - * SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR - * trigger-mode: continuous - * wake-up sensor: no - * - * Similar to SENSOR_TYPE_ROTATION_VECTOR, but using a magnetometer instead - * of using a gyroscope. - * - * This sensor must be based on a magnetometer. It cannot be implemented using - * a gyroscope, and gyroscope input cannot be used by this sensor, as the - * goal of this sensor is to be low power. - * The accelerometer can be (and usually is) used. - * - * Just like SENSOR_TYPE_ROTATION_VECTOR, this sensor reports an estimated - * heading accuracy: - * sensors_event_t.data[4] = estimated_accuracy (in radians) - * The heading error must be less than estimated_accuracy 95% of the time - * - * see SENSOR_TYPE_ROTATION_VECTOR for more details - */ -#define SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR (20) - -/** - * Values returned by the accelerometer in various locations in the universe. - * all values are in SI units (m/s^2) - */ -#define SENSORS_GRAVITY_SUN (275.0f) -#define SENSORS_GRAVITY_MOON (1.6f) -#define SENSORS_GRAVITY_EARTH (9.80665f) -#define SENSORS_GRAVITY_STANDARD (SENSORS_GRAVITY_EARTH) - -/** Maximum magnetic field on Earth's surface */ -#define MAGNETIC_FIELD_EARTH_MAX (60.0f) - -/** Minimum magnetic field on Earth's surface */ -#define MAGNETIC_FIELD_EARTH_MIN (30.0f) - -/** Average sea level pressure is 1013.25 hPa */ -#define SENSORS_PRESSURE_SEALEVELHPA (1013.25F) - -/** Degrees/s to rad/s multiplier */ -#define SENSORS_DPS_TO_RADS (0.017453293F) -/** Gauss to micro-Tesla multiplier */ -#define SENSORS_GAUSS_TO_MICROTESLA (100) - -/** - * status of orientation sensor - */ -#define SENSOR_STATUS_UNRELIABLE 0 -#define SENSOR_STATUS_ACCURACY_LOW 1 -#define SENSOR_STATUS_ACCURACY_MEDIUM 2 -#define SENSOR_STATUS_ACCURACY_HIGH 3 - -/** - * sensor event data - */ -typedef struct +struct rt_sensor_config { - union - { - float v[3]; - struct - { - float x; - float y; - float z; - }; - struct - { - float azimuth; - float pitch; - float roll; - }; - }; - int8_t status; - uint8_t reserved[3]; -} sensors_vec_t; - -/** - * sensor raw vector data - */ -typedef struct -{ - struct - { - int16_t x; - int16_t y; - int16_t z; - }; - - int8_t status; - uint8_t reserved[1]; -} sensors_raw_vec_t; - -/** - * uncalibrated gyroscope and magnetometer event data - */ -typedef struct -{ - union - { - float uncalib[3]; - struct - { - float x_uncalib; - float y_uncalib; - float z_uncalib; - }; - }; - union - { - float bias[3]; - struct - { - float x_bias; - float y_bias; - float z_bias; - }; - }; -} uncalibrated_event_t; - -typedef struct meta_data_event -{ - int32_t what; - int32_t sensor; -} meta_data_event_t; - -/** - * Union of the various types of sensor data - * that can be returned. - */ -typedef struct sensors_event_t -{ - /* must be sizeof(struct sensors_event_t) */ - int32_t version; - - /* sensor identifier */ - int32_t sensor; - - /* sensor type */ - int32_t type; - - /* reserved */ - int32_t reserved0; - - /* time is in nanosecond */ - int64_t timestamp; - - union - { - union - { - float data[16]; - - /* acceleration values are in meter per second per second (m/s^2) */ - sensors_vec_t acceleration; - /* raw acceleration data */ - sensors_raw_vec_t raw_acceleration; - - /* magnetic vector values are in micro-Tesla (uT) */ - sensors_vec_t magnetic; - /* raw magnetic data */ - sensors_raw_vec_t raw_magnetic; - - /* orientation values are in degrees */ - sensors_vec_t orientation; - - /* gyroscope values are in rad/s */ - sensors_vec_t gyro; - /* raw gyroscope data */ - sensors_raw_vec_t raw_gyro; - - /* temperature is in degrees centigrade (Celsius) */ - float temperature; - - /* distance in centimeters */ - float distance; - - /* light in SI lux units */ - float light; - - /* pressure in hectopascal (hPa) */ - float pressure; - - /* relative humidity in percent */ - float relative_humidity; - - /* uncalibrated gyroscope values are in rad/s */ - uncalibrated_event_t uncalibrated_gyro; - - /* uncalibrated magnetometer values are in micro-Teslas */ - uncalibrated_event_t uncalibrated_magnetic; - - /* this is a special event. see SENSOR_TYPE_META_DATA above. - * sensors_meta_data_event_t events are all reported with a type of - * SENSOR_TYPE_META_DATA. The handle is ignored and must be zero. - */ - meta_data_event_t meta_data; - }; - - union - { - uint64_t data[8]; - - /* step-counter */ - uint64_t step_counter; - } u64; - }; - uint32_t reserved1[4]; -} sensors_event_t; - -/* see SENSOR_TYPE_META_DATA */ -typedef sensors_event_t sensors_meta_data_event_t; - -typedef struct sensor_t -{ - /* Name of this sensor. - * All sensors of the same "type" must have a different "name". - */ - const char *name; - - /* vendor of the hardware part */ - const char *vendor; - - /* version of the hardware part + driver. The value of this field - * must increase when the driver is updated in a way that changes the - * output of this sensor. This is important for fused sensors when the - * fusion algorithm is updated. - */ - int version; - - /* handle that identifies this sensors. This handle is used to reference - * this sensor throughout the HAL API. - */ - int handle; - - /* this sensor's type. */ - int type; - - /* maximum range of this sensor's value in SI units */ - float maxRange; - - /* smallest difference between two values reported by this sensor */ - float resolution; - - /* rough estimate of this sensor's power consumption in mA */ - float power; - - /* this value depends on the trigger mode: - * - * continuous: minimum sample period allowed in microseconds - * on-change : 0 - * one-shot :-1 - * special : 0, unless otherwise noted - */ - int32_t minDelay; - - /* number of events reserved for this sensor in the batch mode FIFO. - * If there is a dedicated FIFO for this sensor, then this is the - * size of this FIFO. If the FIFO is shared with other sensors, - * this is the size reserved for that sensor and it can be zero. - */ - uint32_t fifoReservedEventCount; - - /* maximum number of events of this sensor that could be batched. - * This is especially relevant when the FIFO is shared between - * several sensors; this value is then set to the size of that FIFO. - */ - uint32_t fifoMaxEventCount; - - /* reserved fields, must be zero */ - void *reserved[6]; -} sensor_t; - -enum SensorMode -{ - SENSOR_MODE_RAW, - SENSOR_MODE_CALIBRATED, - SENSOR_MODE_NORMAL, + struct rt_sensor_intf intf; /* sensor interface config */ + struct rt_device_pin_mode irq_pin; /* Interrupt pin, The purpose of this pin is to notification read data */ + rt_uint8_t mode; /* sensor work mode */ + rt_uint8_t power; /* sensor power mode */ + rt_uint16_t odr; /* sensor out data rate */ + rt_int32_t range; /* sensor range of measurement */ }; -enum SensorAccelRange +struct rt_sensor_device { - SENSOR_ACCEL_RANGE_2G, - SENSOR_ACCEL_RANGE_4G, - SENSOR_ACCEL_RANGE_8G, - SENSOR_ACCEL_RANGE_16G, + struct rt_device parent; /* The standard device */ + + struct rt_sensor_info info; /* The sensor info data */ + struct rt_sensor_config config; /* The sensor config data */ + + void *data_buf; /* The buf of the data received */ + rt_size_t data_len; /* The size of the data received */ + + struct rt_sensor_ops *ops; /* The sensor ops */ + + struct rt_sensor_module *module; /* The sensor module */ }; -#define SENSOR_ACCEL_SENSITIVITY_2G ((float)2/32768) -#define SENSOR_ACCEL_SENSITIVITY_4G ((float)4/32768) -#define SENSOR_ACCEL_SENSITIVITY_8G ((float)8/32768) -#define SENSOR_ACCEL_SENSITIVITY_16G ((float)16/32768) +typedef struct rt_sensor_device *rt_sensor_t; -enum SensorGyroRange +struct rt_sensor_module { - SENSOR_GYRO_RANGE_250DPS, - SENSOR_GYRO_RANGE_500DPS, - SENSOR_GYRO_RANGE_1000DPS, - SENSOR_GYRO_RANGE_2000DPS, + rt_mutex_t lock; /* The module lock */ + + rt_sensor_t sen[RT_SEN_MODULE_MAX]; /* The module contains a list of sensors */ + rt_uint8_t sen_num; /* Number of sensors contained in the module */ }; -#define SENSOR_GYRO_SENSITIVITY_250DPS (0.00875F) -#define SENSOR_GYRO_SENSITIVITY_500DPS (0.0175F) -#define SENSOR_GYRO_SENSITIVITY_1000DPS (0.035F) -#define SENSOR_GYRO_SENSITIVITY_2000DPS (0.070F) -enum SensorDataRate +/* 3-axis Data Type */ +struct sensor_3_axis { - SENSOR_DATARATE_3200HZ, - SENSOR_DATARATE_1600HZ, - SENSOR_DATARATE_800HZ, - SENSOR_DATARATE_400HZ, - SENSOR_DATARATE_200HZ, - SENSOR_DATARATE_100HZ, - SENSOR_DATARATE_50HZ, - SENSOR_DATARATE_25HZ, - SENSOR_DATARATE_12_5HZ, - SENSOR_DATARATE_6_25HZ, - SENSOR_DATARATE_3_13HZ, - SENSOR_DATARATE_1_56HZ, - SENSOR_DATARATE_0_78HZ, - SENSOR_DATARATE_0_39HZ, - SENSOR_DATARATE_0_20HZ, - SENSOR_DATARATE_0_10HZ, + rt_int32_t x; + rt_int32_t y; + rt_int32_t z; }; -/** - * Sensor Configuration - */ -typedef struct SensorConfig +struct rt_sensor_data { - int mode; - - enum SensorDataRate data_rate; - - union range + rt_uint32_t timestamp; /* The timestamp when the data was received */ + rt_uint8_t type; /* The sensor type of the data */ + union { - int range; - enum SensorAccelRange accel_range; - enum SensorGyroRange gyro_range; - } range; -}SensorConfig; - -typedef void (*SensorEventHandler_t)(void *user_data); - -#ifdef __cplusplus -class SensorBase; -class SensorManager; - -/** - * Sensor Base Class - */ -class SensorBase -{ -private: - int type; - -public: - SensorBase(int type); - ~SensorBase(); - - virtual int configure(SensorConfig *config) = 0; - virtual int activate(int enable) = 0; - - virtual int poll(sensors_event_t *events) = 0; - virtual void getSensor(struct sensor_t *sensor) = 0; - - int getType(void); - - int setConfig(SensorConfig *config); - int getConfig(SensorConfig *config); - - int subscribe(SensorEventHandler_t handler, void *user_data); - int publish(void); - -protected: - SensorBase *next; - SensorBase *prev; - - /* sensor configuration */ - SensorConfig config; - - SensorEventHandler_t evtHandler; - void *userData; - - friend class SensorManager; + struct sensor_3_axis acce; /* Accelerometer. unit: mG */ + struct sensor_3_axis gyro; /* Gyroscope. unit: mdps */ + struct sensor_3_axis mag; /* Magnetometer. unit: mGauss */ + rt_int32_t temp; /* Temperature. unit: dCelsius */ + rt_int32_t humi; /* Relative humidity. unit: permillage */ + rt_int32_t baro; /* Pressure. unit: pascal (Pa) */ + rt_int32_t light; /* Light. unit: lux */ + rt_int32_t proximity; /* Distance. unit: centimeters */ + rt_int32_t hr; /* Heat rate. unit: HZ */ + rt_int32_t tvoc; /* TVOC. unit: permillage */ + rt_int32_t noise; /* Noise Loudness. unit: HZ */ + rt_uint32_t step; /* Step sensor. unit: 1 */ + }data; }; -/** - * Sensor Manager - */ -class SensorManager +struct rt_sensor_ops { -public: - SensorManager(); - ~SensorManager(); - - static int registerSensor(SensorBase *sensor); - static int unregisterSensor(SensorBase *sensor); - - static SensorBase *getDefaultSensor(int type); - static int subscribe(int type, SensorEventHandler_t handler, void *user_data); - - static int sensorEventReady(SensorBase *sensor); - static int pollSensor(SensorBase *sensor, sensors_event_t *events, int number, int duration); + rt_size_t (*fetch_data)(struct rt_sensor_device *sensor, void *buf, rt_size_t len); + rt_err_t (*control)(struct rt_sensor_device *sensor, int cmd, void *arg); }; -#endif - -/* C programming language APIs */ -/* rt_sensor_t is a C typedef for SensorBase */ -typedef void* rt_sensor_t; - -#ifdef __cplusplus -extern "C" { -#endif - -rt_sensor_t rt_sensor_get_default(int type); - -int rt_sensor_subscribe(rt_sensor_t sensor, SensorEventHandler_t handler, void *user_data); -int rt_sensor_activate (rt_sensor_t sensor, int enable); -int rt_sensor_configure(rt_sensor_t sensor, SensorConfig *config); -int rt_sensor_poll(rt_sensor_t sensor, sensors_event_t *event); - -#ifdef __cplusplus -} -#endif +int rt_hw_sensor_register(rt_sensor_t sensor, + const char *name, + rt_uint32_t flag, + void *data); #endif diff --git a/components/drivers/sensors/sensor_test.c b/components/drivers/sensors/sensor_test.c new file mode 100644 index 0000000000..0221167e83 --- /dev/null +++ b/components/drivers/sensors/sensor_test.c @@ -0,0 +1,361 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-01-31 flybreak first version + */ + +#include "sensor.h" + +#define DBG_ENABLE +#define DBG_LEVEL DBG_INFO +#define DBG_SECTION_NAME "sensor.test" +#define DBG_COLOR +#include + +#include +#include + +static rt_sem_t sensor_rx_sem = RT_NULL; + +static void sensor_show_data(rt_size_t num, rt_sensor_t sensor, struct rt_sensor_data *sensor_data) +{ + switch (sensor->info.type) + { + case RT_SEN_CLASS_ACCE: + LOG_I("num:%3d, x:%5d, y:%5d, z:%5d, timestamp:%5d", num, sensor_data->data.acce.x, sensor_data->data.acce.y, sensor_data->data.acce.z, sensor_data->timestamp); + break; + case RT_SEN_CLASS_GYRO: + LOG_I("num:%3d, x:%8d, y:%8d, z:%8d, timestamp:%5d", num, sensor_data->data.gyro.x, sensor_data->data.gyro.y, sensor_data->data.gyro.z, sensor_data->timestamp); + break; + case RT_SEN_CLASS_MAG: + LOG_I("num:%3d, x:%5d, y:%5d, z:%5d, timestamp:%5d", num, sensor_data->data.mag.x, sensor_data->data.mag.y, sensor_data->data.mag.z, sensor_data->timestamp); + break; + case RT_SEN_CLASS_HUMI: + LOG_I("num:%3d, humi:%3d.%d%%, timestamp:%5d", num, sensor_data->data.humi / 10, sensor_data->data.humi % 10, sensor_data->timestamp); + break; + case RT_SEN_CLASS_TEMP: + LOG_I("num:%3d, temp:%3d.%dC, timestamp:%5d", num, sensor_data->data.temp / 10, sensor_data->data.temp % 10, sensor_data->timestamp); + break; + case RT_SEN_CLASS_BARO: + LOG_I("num:%3d, press:%5d, timestamp:%5d", num, sensor_data->data.baro, sensor_data->timestamp); + break; + case RT_SEN_CLASS_STEP: + LOG_I("num:%3d, step:%5d, timestamp:%5d", num, sensor_data->data.step, sensor_data->timestamp); + break; + default: + break; + } +} + +rt_err_t rx_callback(rt_device_t dev, rt_size_t size) +{ + rt_sem_release(sensor_rx_sem); + return 0; +} + +static void sensor_fifo_rx_entry(void *parameter) +{ + rt_device_t dev = parameter; + rt_sensor_t sensor = parameter; + struct rt_sensor_data *data = RT_NULL; + struct rt_sensor_info info; + rt_size_t res, i; + + rt_device_control(dev, RT_SEN_CTRL_GET_INFO, &info); + + data = rt_malloc(sizeof(struct rt_sensor_data) * info.fifo_max); + if (data == RT_NULL) + { + LOG_E("Memory allocation failed!"); + } + + while (1) + { + rt_sem_take(sensor_rx_sem, RT_WAITING_FOREVER); + + res = rt_device_read(dev, 0, data, info.fifo_max); + for (i = 0; i < res; i++) + { + sensor_show_data(i, sensor, &data[i]); + } + } +} + +static void sensor_fifo(int argc, char **argv) +{ + static rt_thread_t tid1 = RT_NULL; + rt_device_t dev = RT_NULL; + rt_sensor_t sensor; + + dev = rt_device_find(argv[1]); + if (dev == RT_NULL) + { + LOG_E("Can't find device:%s", argv[1]); + return; + } + sensor = (rt_sensor_t)dev; + + if (sensor_rx_sem == RT_NULL) + { + sensor_rx_sem = rt_sem_create("sen_rx_sem", 0, RT_IPC_FLAG_FIFO); + } + else + { + LOG_E("The thread is running, please reboot and try again"); + return; + } + + tid1 = rt_thread_create("sen_rx_thread", + sensor_fifo_rx_entry, sensor, + 1024, + 15, 5); + + if (tid1 != RT_NULL) + rt_thread_startup(tid1); + + rt_device_set_rx_indicate(dev, rx_callback); + + if (rt_device_open(dev, RT_SEN_FLAG_FIFO) != RT_EOK) + { + LOG_E("open device failed!"); + return; + } + rt_device_control(dev, RT_SEN_CTRL_SET_ODR, (void *)20); +} +#ifdef FINSH_USING_MSH +MSH_CMD_EXPORT(sensor_fifo, Sensor fifo mode test function); +#endif + +static void sensor_irq_rx_entry(void *parameter) +{ + rt_device_t dev = parameter; + rt_sensor_t sensor = parameter; + struct rt_sensor_data data; + rt_size_t res, i = 0; + + while (1) + { + rt_sem_take(sensor_rx_sem, RT_WAITING_FOREVER); + + res = rt_device_read(dev, 0, &data, 1); + if (res == 1) + { + sensor_show_data(i++, sensor, &data); + } + } +} + +static void sensor_int(int argc, char **argv) +{ + static rt_thread_t tid1 = RT_NULL; + rt_device_t dev = RT_NULL; + rt_sensor_t sensor; + + dev = rt_device_find(argv[1]); + if (dev == RT_NULL) + { + LOG_E("Can't find device:%s", argv[1]); + return; + } + sensor = (rt_sensor_t)dev; + + if (sensor_rx_sem == RT_NULL) + { + sensor_rx_sem = rt_sem_create("sen_rx_sem", 0, RT_IPC_FLAG_FIFO); + } + else + { + LOG_E("The thread is running, please reboot and try again"); + return; + } + + tid1 = rt_thread_create("sen_rx_thread", + sensor_irq_rx_entry, sensor, + 1024, + 15, 5); + + if (tid1 != RT_NULL) + rt_thread_startup(tid1); + + rt_device_set_rx_indicate(dev, rx_callback); + + if (rt_device_open(dev, RT_DEVICE_FLAG_INT_RX) != RT_EOK) + { + LOG_E("open device failed!"); + return; + } + rt_device_control(dev, RT_SEN_CTRL_SET_ODR, (void *)20); +} +#ifdef FINSH_USING_MSH +MSH_CMD_EXPORT(sensor_int, Sensor interrupt mode test function); +#endif + +static void sensor_polling(int argc, char **argv) +{ + uint16_t num = 10; + rt_device_t dev = RT_NULL; + rt_sensor_t sensor; + struct rt_sensor_data data; + rt_size_t res, i; + + dev = rt_device_find(argv[1]); + if (dev == RT_NULL) + { + LOG_E("Can't find device:%s", argv[1]); + return; + } + if (argc > 2) + num = atoi(argv[2]); + + sensor = (rt_sensor_t)dev; + + if (rt_device_open(dev, RT_DEVICE_FLAG_RDWR) != RT_EOK) + { + LOG_E("open device failed!"); + return; + } + rt_device_control(dev, RT_SEN_CTRL_SET_ODR, (void *)100); + + for (i = 0; i < num; i++) + { + res = rt_device_read(dev, 0, &data, 1); + if (res != 1) + { + LOG_E("read data failed!size is %d", res); + } + else + { + sensor_show_data(i, sensor, &data); + } + rt_thread_mdelay(100); + } + rt_device_close(dev); +} +#ifdef FINSH_USING_MSH +MSH_CMD_EXPORT(sensor_polling, Sensor polling mode test function); +#endif + +static void sensor(int argc, char **argv) +{ + static rt_device_t dev = RT_NULL; + struct rt_sensor_data data; + rt_size_t res, i; + + /* If the number of arguments less than 2 */ + if (argc < 2) + { + rt_kprintf("\n"); + rt_kprintf("sensor [OPTION] [PARAM]\n"); + rt_kprintf(" probe Probe sensor by given name\n"); + rt_kprintf(" info Get sensor info\n"); + rt_kprintf(" sr Set range to var\n"); + rt_kprintf(" sm Set work mode to var\n"); + rt_kprintf(" sp Set power mode to var\n"); + rt_kprintf(" sodr Set output date rate to var\n"); + rt_kprintf(" read [num] Read [num] times sensor\n"); + rt_kprintf(" num default 5\n"); + return ; + } + else if (!strcmp(argv[1], "info")) + { + struct rt_sensor_info info; + rt_device_control(dev, RT_SEN_CTRL_GET_INFO, &info); + rt_kprintf("vendor :%d\n", info.vendor); + rt_kprintf("model :%s\n", info.model); + rt_kprintf("unit :%d\n", info.unit); + rt_kprintf("range_max :%d\n", info.range_max); + rt_kprintf("range_min :%d\n", info.range_min); + rt_kprintf("period_min:%d\n", info.period_min); + rt_kprintf("fifo_max :%d\n", info.fifo_max); + } + else if (!strcmp(argv[1], "read")) + { + uint16_t num = 5; + + if (dev == RT_NULL) + { + LOG_W("Please probe sensor device first!"); + return ; + } + if (argc == 3) + { + num = atoi(argv[2]); + } + + for (i = 0; i < num; i++) + { + res = rt_device_read(dev, 0, &data, 1); + if (res != 1) + { + LOG_E("read data failed!size is %d", res); + } + else + { + sensor_show_data(i, (rt_sensor_t)dev, &data); + } + rt_thread_mdelay(100); + } + } + else if (argc == 3) + { + if (!strcmp(argv[1], "probe")) + { + rt_uint8_t reg = 0xFF; + if (dev) + { + rt_device_close(dev); + } + + dev = rt_device_find(argv[2]); + if (dev == RT_NULL) + { + LOG_E("Can't find device:%s", argv[1]); + return; + } + if (rt_device_open(dev, RT_DEVICE_FLAG_RDWR) != RT_EOK) + { + LOG_E("open device failed!"); + return; + } + rt_device_control(dev, RT_SEN_CTRL_GET_ID, ®); + LOG_I("device id: 0x%x!", reg); + + } + else if (dev == RT_NULL) + { + LOG_W("Please probe sensor first!"); + return ; + } + else if (!strcmp(argv[1], "sr")) + { + rt_device_control(dev, RT_SEN_CTRL_SET_RANGE, (void *)atoi(argv[2])); + } + else if (!strcmp(argv[1], "sm")) + { + rt_device_control(dev, RT_SEN_CTRL_SET_MODE, (void *)atoi(argv[2])); + } + else if (!strcmp(argv[1], "sp")) + { + rt_device_control(dev, RT_SEN_CTRL_SET_POWER, (void *)atoi(argv[2])); + } + else if (!strcmp(argv[1], "sodr")) + { + rt_device_control(dev, RT_SEN_CTRL_SET_ODR, (void *)atoi(argv[2])); + } + else + { + LOG_W("Unknown command, please enter 'sensor' get help information!"); + } + } + else + { + LOG_W("Unknown command, please enter 'sensor' get help information!"); + } +} +#ifdef FINSH_USING_MSH +MSH_CMD_EXPORT(sensor, sensor test function); +#endif diff --git a/components/finsh/cmd.c b/components/finsh/cmd.c index 0a30aee809..680d9d01d7 100644 --- a/components/finsh/cmd.c +++ b/components/finsh/cmd.c @@ -800,6 +800,7 @@ static char *const device_type_str[] = "Portal Device", "Timer Device", "Miscellaneous Device", + "Sensor Device", "Unknown" }; diff --git a/include/rtdef.h b/include/rtdef.h index 859278e849..bb11bf88ff 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -846,6 +846,7 @@ enum rt_device_class_type RT_Device_Class_Portal, /**< Portal device */ RT_Device_Class_Timer, /**< Timer device */ RT_Device_Class_Miscellaneous, /**< Miscellaneous device */ + RT_Device_Class_Sensor, /**< Sensor device */ RT_Device_Class_Unknown /**< unknown device */ }; -- cgit v1.3.1 From 1487a4f732d9fa5382d979a5b08f981d93a58aee Mon Sep 17 00:00:00 2001 From: BernardXiong Date: Tue, 26 Mar 2019 01:03:39 +0000 Subject: [Kernel] Support ARCH data types definitions --- include/rtdef.h | 2 ++ src/Kconfig | 9 +++++++++ 2 files changed, 11 insertions(+) (limited to 'include/rtdef.h') diff --git a/include/rtdef.h b/include/rtdef.h index bb11bf88ff..9af4320112 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -58,6 +58,7 @@ extern "C" { (RT_SUBVERSION * 100) + RT_REVISION) /* RT-Thread basic data type definitions */ +#ifndef RT_USING_ARCH_DATA_TYPE typedef signed char rt_int8_t; /**< 8bit integer type */ typedef signed short rt_int16_t; /**< 16bit integer type */ typedef signed int rt_int32_t; /**< 32bit integer type */ @@ -72,6 +73,7 @@ typedef unsigned long rt_uint64_t; /**< 64bit unsigned inte typedef signed long long rt_int64_t; /**< 64bit integer type */ typedef unsigned long long rt_uint64_t; /**< 64bit unsigned integer type */ #endif +#endif typedef int rt_bool_t; /**< boolean type */ typedef long rt_base_t; /**< Nbit CPU related date type */ diff --git a/src/Kconfig b/src/Kconfig index fbfae65882..c123f53b92 100644 --- a/src/Kconfig +++ b/src/Kconfig @@ -8,6 +8,15 @@ config RT_NAME_MAX 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_USING_ARCH_DATA_TYPE + bool "Use the data types defined in ARCH_CPU" + default n + help + For the data type like, `rt_uint8/int8_t, rt_uint16/int16_t, rt_uint32/int32_t`, + BSP can define these basic data types in ARCH_CPU level. + + Please re-define these data types in bsp_project.h file. + config RT_USING_SMP bool "Enable SMP(Symmetric multiprocessing)" default n -- cgit v1.3.1 From fde1346d5b55845fa6dee9da6de489b4f66d977a Mon Sep 17 00:00:00 2001 From: yangfasheng Date: Sun, 14 Apr 2019 13:33:29 +0800 Subject: add RTGRAPHIC_PIXEL_FORMAT_COLOR to graphic deice Signed-off-by: yangfasheng --- include/rtdef.h | 1 + 1 file changed, 1 insertion(+) (limited to 'include/rtdef.h') diff --git a/include/rtdef.h b/include/rtdef.h index 9af4320112..9b12093dc7 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -1020,6 +1020,7 @@ enum RTGRAPHIC_PIXEL_FORMAT_ABGR888, RTGRAPHIC_PIXEL_FORMAT_ARGB565, RTGRAPHIC_PIXEL_FORMAT_ALPHA, + RTGRAPHIC_PIXEL_FORMAT_COLOR, }; /** -- cgit v1.3.1 From fa697a763f5cbece6b886cf2eca54b2dd28f718d Mon Sep 17 00:00:00 2001 From: shaojinchun Date: Fri, 19 Apr 2019 14:57:50 +0800 Subject: 修改sem中的value成员变量和赋值参数类型相同 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/rtdef.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/rtdef.h') diff --git a/include/rtdef.h b/include/rtdef.h index 9b12093dc7..8c7e6e8c43 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -655,7 +655,7 @@ struct rt_semaphore { struct rt_ipc_object parent; /**< inherit from ipc_object */ - rt_uint16_t value; /**< value of semaphore. */ + rt_uint32_t value; /**< value of semaphore. */ }; typedef struct rt_semaphore *rt_sem_t; #endif -- cgit v1.3.1 From 7bcee9bcb14f337efb6fcacecc1501d01624c0a3 Mon Sep 17 00:00:00 2001 From: shaojinchun Date: Wed, 24 Apr 2019 09:14:00 +0800 Subject: semaphore结构中value仍改为uint16_t,加入对其设置范围的检测,加入对齐变量 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/rtdef.h | 3 ++- src/ipc.c | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) (limited to 'include/rtdef.h') diff --git a/include/rtdef.h b/include/rtdef.h index 8c7e6e8c43..743b91d959 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -655,7 +655,8 @@ struct rt_semaphore { struct rt_ipc_object parent; /**< inherit from ipc_object */ - rt_uint32_t value; /**< value of semaphore. */ + rt_uint16_t value; /**< value of semaphore. */ + rt_uint16_t reserved; /**< reserved field */ }; typedef struct rt_semaphore *rt_sem_t; #endif diff --git a/src/ipc.c b/src/ipc.c index 7f603cfc4d..74faf6ac7e 100644 --- a/src/ipc.c +++ b/src/ipc.c @@ -201,6 +201,7 @@ rt_err_t rt_sem_init(rt_sem_t sem, rt_uint8_t flag) { RT_ASSERT(sem != RT_NULL); + RT_ASSERT(value < 0x10000U); /* init object */ rt_object_init(&(sem->parent.parent), RT_Object_Class_Semaphore, name); @@ -209,7 +210,7 @@ rt_err_t rt_sem_init(rt_sem_t sem, rt_ipc_object_init(&(sem->parent)); /* set init value */ - sem->value = value; + sem->value = (rt_uint16_t)value; /* set parent */ sem->parent.parent.flag = flag; @@ -261,6 +262,7 @@ rt_sem_t rt_sem_create(const char *name, rt_uint32_t value, rt_uint8_t flag) rt_sem_t sem; RT_DEBUG_NOT_IN_INTERRUPT; + RT_ASSERT(value < 0x10000U); /* allocate object */ sem = (rt_sem_t)rt_object_allocate(RT_Object_Class_Semaphore, name); -- cgit v1.3.1 From 6cdfb2ac92d8422136f926024b2c2f969fce6b9e Mon Sep 17 00:00:00 2001 From: shaojinchun Date: Thu, 9 May 2019 19:28:52 +0800 Subject: fix signal code --- include/rtdef.h | 3 + libcpu/arm/cortex-a/context_gcc.S | 81 ++++++------------ libcpu/arm/cortex-a/start_gcc.S | 39 +++++++-- src/scheduler.c | 84 +++++++++++++----- src/signal.c | 176 ++++++++++++++++++++++++++++++-------- src/thread.c | 2 + 6 files changed, 267 insertions(+), 118 deletions(-) (limited to 'include/rtdef.h') diff --git a/include/rtdef.h b/include/rtdef.h index 743b91d959..253333253a 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -495,6 +495,7 @@ typedef siginfo_t rt_siginfo_t; #define RT_THREAD_STAT_SIGNAL 0x10 #define RT_THREAD_STAT_SIGNAL_READY (RT_THREAD_STAT_SIGNAL | RT_THREAD_READY) #define RT_THREAD_STAT_SIGNAL_WAIT 0x20 +#define RT_THREAD_STAT_SIGNAL_PENDING 0x40 #define RT_THREAD_STAT_SIGNAL_MASK 0xf0 /** @@ -596,7 +597,9 @@ struct rt_thread rt_sigset_t sig_pending; /**< the pending signals */ rt_sigset_t sig_mask; /**< the mask bits of signal */ +#ifndef RT_USING_SMP void *sig_ret; /**< the return stack pointer from signal */ +#endif rt_sighandler_t *sig_vectors; /**< vectors of signal handler */ void *si_list; /**< the signal infor list */ #endif diff --git a/libcpu/arm/cortex-a/context_gcc.S b/libcpu/arm/cortex-a/context_gcc.S index af5c516e4d..dff7a1de68 100644 --- a/libcpu/arm/cortex-a/context_gcc.S +++ b/libcpu/arm/cortex-a/context_gcc.S @@ -46,16 +46,7 @@ rt_hw_context_switch_to: mov r0, r1 bl rt_cpus_lock_status_restore #endif /*RT_USING_SMP*/ - -#ifdef RT_USING_LWP - ldmfd sp, {r13, r14}^ @ pop usr_sp usr_lr - add sp, #8 -#endif - - ldmfd sp!, {r4} @ pop new task spsr - msr spsr_cxsf, r4 - - ldmfd sp!, {r0-r12, lr, pc}^ @ pop new task r0-r12, lr & pc + b rt_hw_context_switch_exit .section .bss.share.isr _guest_switch_lvl: @@ -93,15 +84,7 @@ rt_hw_context_switch: mov r0, r2 bl rt_cpus_lock_status_restore #endif /*RT_USING_SMP*/ - -#ifdef RT_USING_LWP - ldmfd sp, {r13, r14}^ @ pop usr_sp usr_lr - add sp, #8 -#endif - - ldmfd sp!, {r4} @ pop new task cpsr to spsr - msr spsr_cxsf, r4 - ldmfd sp!, {r0-r12, lr, pc}^ @ pop new task r0-r12, lr & pc, copy spsr to cpsr + b rt_hw_context_switch_exit /* * void rt_hw_context_switch_interrupt(rt_uint32 from, rt_uint32 to); @@ -123,51 +106,19 @@ rt_hw_context_switch: .globl rt_hw_context_switch_interrupt rt_hw_context_switch_interrupt: #ifdef RT_USING_SMP - /* r0 :irq_mod context + /* r0 :svc_mod context * r1 :addr of from_thread's sp * r2 :addr of to_thread's sp * r3 :to_thread's tcb */ - @ r0 point to {r0-r3} in stack - push {r1 - r3} - mov r1, r0 - add r0, r0, #4*4 - ldmfd r0!, {r4-r12,lr}@ reload saved registers - mrs r3, spsr @ get cpsr of interrupt thread - sub r2, lr, #4 @ save old task's pc to r2 - msr cpsr_c, #I_Bit|F_Bit|Mode_SVC - - stmfd sp!, {r2} @ push old task's pc - stmfd sp!, {r4-r12,lr}@ push old task's lr,r12-r4 - ldmfd r1, {r4-r7} @ restore r0-r3 of the interrupt thread - stmfd sp!, {r4-r7} @ push old task's r0-r3 - stmfd sp!, {r3} @ push old task's cpsr - -#ifdef RT_USING_LWP - stmfd sp, {r13,r14}^ @push usr_sp usr_lr - sub sp, #8 -#endif - - msr cpsr_c, #I_Bit|F_Bit|Mode_IRQ - pop {r1 - r3} - mov sp, r0 - msr cpsr_c, #I_Bit|F_Bit|Mode_SVC - str sp, [r1] + str r0, [r1] ldr sp, [r2] mov r0, r3 bl rt_cpus_lock_status_restore -#ifdef RT_USING_LWP - ldmfd sp, {r13,r14}^ @pop usr_sp usr_lr - add sp, #8 -#endif - - ldmfd sp!, {r4} @ pop new task's cpsr to spsr - msr spsr_cxsf, r4 - - ldmfd sp!, {r0-r12,lr,pc}^ @ pop new task's r0-r12,lr & pc, copy spsr to cpsr + b rt_hw_context_switch_exit #else /*RT_USING_SMP*/ ldr r2, =rt_thread_switch_interrupt_flag @@ -183,3 +134,25 @@ _reswitch: str r1, [r2] bx lr #endif /*RT_USING_SMP*/ + +.global rt_hw_context_switch_exit +rt_hw_context_switch_exit: + +#ifdef RT_USING_SMP +#ifdef RT_USING_SIGNALS + mov r0, sp + cps #Mode_IRQ + bl rt_signal_check + cps #Mode_SVC + mov sp, r0 +#endif +#endif + +#ifdef RT_USING_LWP + ldmfd sp, {r13, r14}^ /* usr_sp, usr_lr */ + add sp, #8 +#endif + ldmfd sp!, {r1} + msr spsr_cxsf, r1 /* original mode */ + ldmfd sp!, {r0-r12,lr,pc}^ /* irq return */ + diff --git a/libcpu/arm/cortex-a/start_gcc.S b/libcpu/arm/cortex-a/start_gcc.S index 51026b7ed5..014846f3aa 100644 --- a/libcpu/arm/cortex-a/start_gcc.S +++ b/libcpu/arm/cortex-a/start_gcc.S @@ -11,7 +11,6 @@ */ #include "rtconfig.h" - .equ Mode_USR, 0x10 .equ Mode_FIQ, 0x11 .equ Mode_IRQ, 0x12 @@ -158,20 +157,48 @@ vector_fiq: vector_irq: #ifdef RT_USING_SMP clrex + + stmfd sp!, {r0, r1} + cps #Mode_SVC + mov r0, sp /* svc_sp */ + mov r1, lr /* svc_lr */ + + cps #Mode_IRQ + sub lr, #4 + stmfd r0!, {r1, lr} /* svc_lr, svc_pc */ + stmfd r0!, {r2 - r12} + ldmfd sp!, {r1, r2} /* original r0, r1 */ + stmfd r0!, {r1 - r2} + mrs r1, spsr /* original mode */ + stmfd r0!, {r1} + +#ifdef RT_USING_LWP + stmfd r0, {r13, r14}^ /* usr_sp, usr_lr */ + sub r0, #8 #endif - stmfd sp!, {r0-r12,lr} + /* now irq stack is clean */ + /* r0 is task svc_sp */ + /* backup r0 -> r8 */ + mov r8, r0 bl rt_interrupt_enter bl rt_hw_trap_irq bl rt_interrupt_leave -#ifdef RT_USING_SMP - mov r0, sp + cps #Mode_SVC + mov sp, r8 + mov r0, r8 bl rt_scheduler_do_irq_switch - ldmfd sp!, {r0-r12,lr} - subs pc, lr, #4 + b rt_hw_context_switch_exit + #else + stmfd sp!, {r0-r12,lr} + + bl rt_interrupt_enter + bl rt_hw_trap_irq + bl rt_interrupt_leave + @ if rt_thread_switch_interrupt_flag set, jump to @ rt_hw_context_switch_interrupt_do and don't return ldr r0, =rt_thread_switch_interrupt_flag diff --git a/src/scheduler.c b/src/scheduler.c index be29d9a7e0..3da723687c 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -322,8 +322,23 @@ void rt_schedule(void) if (pcpu->irq_nest) { pcpu->irq_switch_flag = 1; + rt_hw_interrupt_enable(level); + goto __exit; } - else if (current_thread->scheduler_lock_nest == 1) /* whether lock scheduler */ + +#ifdef RT_USING_SIGNALS + if ((current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND) + { + /* if current_thread signal is in pending */ + + if ((current_thread->stat & RT_THREAD_STAT_SIGNAL_MASK) & RT_THREAD_STAT_SIGNAL_PENDING) + { + rt_thread_resume(current_thread); + } + } +#endif + + if (current_thread->scheduler_lock_nest == 1) /* whether lock scheduler */ { rt_ubase_t highest_ready_priority; @@ -366,27 +381,32 @@ void rt_schedule(void) _rt_scheduler_stack_check(to_thread); #endif - { - extern void rt_thread_handle_sig(rt_bool_t clean_state); - - rt_hw_context_switch((rt_ubase_t)¤t_thread->sp, - (rt_ubase_t)&to_thread->sp, to_thread); - - /* enable interrupt */ - rt_hw_interrupt_enable(level); - -#ifdef RT_USING_SIGNALS - /* check signal status */ - rt_thread_handle_sig(RT_TRUE); -#endif - goto __exit; - } + rt_hw_context_switch((rt_ubase_t)¤t_thread->sp, + (rt_ubase_t)&to_thread->sp, to_thread); } } } +#ifdef RT_USING_SIGNALS + if (current_thread->stat & RT_THREAD_STAT_SIGNAL_PENDING) + { + extern void rt_thread_handle_sig(rt_bool_t clean_state); + + current_thread->stat &= ~RT_THREAD_STAT_SIGNAL_PENDING; + + rt_hw_interrupt_enable(level); + + /* check signal status */ + rt_thread_handle_sig(RT_TRUE); + } + else + { + rt_hw_interrupt_enable(level); + } +#else /* enable interrupt */ rt_hw_interrupt_enable(level); +#endif __exit: return ; @@ -465,13 +485,25 @@ void rt_schedule(void) rt_hw_context_switch((rt_ubase_t)&from_thread->sp, (rt_ubase_t)&to_thread->sp); +#ifdef RT_USING_SIGNALS + if (rt_current_thread->stat & RT_THREAD_STAT_SIGNAL_PENDING) + { + extern void rt_thread_handle_sig(rt_bool_t clean_state); + + rt_current_thread->stat &= ~RT_THREAD_STAT_SIGNAL_PENDING; + rt_hw_interrupt_enable(level); + + /* check signal status */ + rt_thread_handle_sig(RT_TRUE); + } + else + { + rt_hw_interrupt_enable(level); + } +#else /* enable interrupt */ rt_hw_interrupt_enable(level); - -#ifdef RT_USING_SIGNALS - /* check signal status */ - rt_thread_handle_sig(RT_TRUE); #endif goto __exit; } @@ -519,6 +551,18 @@ void rt_scheduler_do_irq_switch(void *context) pcpu = rt_cpu_index(cpu_id); current_thread = pcpu->current_thread; +#ifdef RT_USING_SIGNALS + if ((current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND) + { + /* if current_thread signal is in pending */ + + if ((current_thread->stat & RT_THREAD_STAT_SIGNAL_MASK) & RT_THREAD_STAT_SIGNAL_PENDING) + { + rt_thread_resume(current_thread); + } + } +#endif + if (pcpu->irq_switch_flag == 0) { rt_hw_interrupt_enable(level); diff --git a/src/signal.c b/src/signal.c index 06c2055d26..0cab6215af 100644 --- a/src/signal.c +++ b/src/signal.c @@ -52,17 +52,27 @@ static void _signal_entry(void *parameter) /* handle signal */ rt_thread_handle_sig(RT_FALSE); - /* never come back... */ - rt_hw_interrupt_disable(); +#ifdef RT_USING_SMP + { + struct rt_cpu* pcpu = rt_cpu_self(); + + if (--pcpu->current_thread->cpus_lock_nest == 0) + { + pcpu->current_thread->scheduler_lock_nest--; + } + + } +#else /* return to thread */ tid->sp = tid->sig_ret; tid->sig_ret = RT_NULL; +#endif LOG_D("switch back to: 0x%08x\n", tid->sp); tid->stat &= ~RT_THREAD_STAT_SIGNAL; #ifdef RT_USING_SMP - rt_hw_context_switch_to((rt_ubase_t)&(tid->sp), tid); + rt_hw_context_switch_to((rt_base_t)¶meter, tid); #else rt_hw_context_switch_to((rt_ubase_t)&(tid->sp)); #endif /*RT_USING_SMP*/ @@ -82,16 +92,21 @@ static void _signal_deliver(rt_thread_t tid) { rt_ubase_t level; + level = rt_hw_interrupt_disable(); + /* thread is not interested in pended signals */ - if (!(tid->sig_pending & tid->sig_mask)) return; + if (!(tid->sig_pending & tid->sig_mask)) + { + rt_hw_interrupt_enable(level); + return; + } - level = rt_hw_interrupt_disable(); if ((tid->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND) { /* resume thread to handle signal */ rt_thread_resume(tid); /* add signal state */ - tid->stat |= RT_THREAD_STAT_SIGNAL; + tid->stat |= (RT_THREAD_STAT_SIGNAL | RT_THREAD_STAT_SIGNAL_PENDING); rt_hw_interrupt_enable(level); @@ -108,17 +123,36 @@ static void _signal_deliver(rt_thread_t tid) rt_hw_interrupt_enable(level); /* do signal action in self thread context */ - rt_thread_handle_sig(RT_TRUE); + if (rt_interrupt_get_nest() == 0) + { + rt_thread_handle_sig(RT_TRUE); + } } else if (!((tid->stat & RT_THREAD_STAT_SIGNAL_MASK) & RT_THREAD_STAT_SIGNAL)) { /* add signal state */ - tid->stat |= RT_THREAD_STAT_SIGNAL; + tid->stat |= (RT_THREAD_STAT_SIGNAL | RT_THREAD_STAT_SIGNAL_PENDING); +#ifdef RT_USING_SMP + { + int cpu_id; + + cpu_id = tid->oncpu; + if ((cpu_id != RT_CPU_DETACHED) && (cpu_id != rt_hw_cpu_id())) + { + rt_uint32_t cpu_mask; + + cpu_mask = RT_CPU_MASK ^ (1 << cpu_id); + rt_hw_ipi_send(RT_SCHEDULE_IPI, cpu_mask); + } + } +#else /* point to the signal handle entry */ + tid->stat &= ~RT_THREAD_STAT_SIGNAL_PENDING; tid->sig_ret = tid->sp; tid->sp = rt_hw_stack_init((void *)_signal_entry, RT_NULL, (void *)((char *)tid->sig_ret - 32), RT_NULL); +#endif rt_hw_interrupt_enable(level); LOG_D("signal stack pointer @ 0x%08x", tid->sp); @@ -133,14 +167,53 @@ static void _signal_deliver(rt_thread_t tid) } } +#ifdef RT_USING_SMP +void *rt_signal_check(void* context) +{ + rt_base_t level; + int cpu_id; + struct rt_cpu* pcpu; + struct rt_thread *current_thread; + + level = rt_hw_interrupt_disable(); + cpu_id = rt_hw_cpu_id(); + pcpu = rt_cpu_index(cpu_id); + current_thread = pcpu->current_thread; + + if (pcpu->irq_nest) + { + rt_hw_interrupt_enable(level); + return context; + } + + if (current_thread->cpus_lock_nest == 1) + { + if (current_thread->stat & RT_THREAD_STAT_SIGNAL_PENDING) + { + void *sig_context; + + current_thread->stat &= ~RT_THREAD_STAT_SIGNAL_PENDING; + + rt_hw_interrupt_enable(level); + sig_context = rt_hw_stack_init((void *)_signal_entry, context, + (void *)(context - 32), RT_NULL); + return sig_context; + } + } + rt_hw_interrupt_enable(level); + return context; +} +#endif + rt_sighandler_t rt_signal_install(int signo, rt_sighandler_t handler) { + rt_base_t level; rt_sighandler_t old = RT_NULL; rt_thread_t tid = rt_thread_self(); if (!sig_valid(signo)) return SIG_ERR; - rt_enter_critical(); + level = rt_hw_interrupt_disable(); if (tid->sig_vectors == RT_NULL) { rt_thread_alloc_sig(tid); @@ -154,7 +227,7 @@ rt_sighandler_t rt_signal_install(int signo, rt_sighandler_t handler) else if (handler == SIG_DFL) tid->sig_vectors[signo] = _signal_default_handler; else tid->sig_vectors[signo] = handler; } - rt_exit_critical(); + rt_hw_interrupt_enable(level); return old; } @@ -272,7 +345,20 @@ __done: LOG_D("sigwait: %d sig raised!", signo); if (si_prev) si_prev->list.next = si_node->list.next; - else tid->si_list = si_node->list.next; + else + { + struct siginfo_node *node_next; + + if (si_node->list.next) + { + node_next = (void *)rt_slist_entry(si_node->list.next, struct siginfo_node, list); + tid->si_list = node_next; + } + else + { + tid->si_list = RT_NULL; + } + } /* clear pending */ tid->sig_pending &= ~sig_mask(signo); @@ -281,7 +367,14 @@ __done: } si_prev = si_node; - si_node = (void *)rt_slist_entry(si_node->list.next, struct siginfo_node, list); + if (si_node->list.next) + { + si_node = (void *)rt_slist_entry(si_node->list.next, struct siginfo_node, list); + } + else + { + si_node = RT_NULL; + } } __done_int: @@ -320,13 +413,13 @@ void rt_thread_handle_sig(rt_bool_t clean_state) signo = si_node->si.si_signo; handler = tid->sig_vectors[signo]; + tid->sig_pending &= ~sig_mask(signo); rt_hw_interrupt_enable(level); LOG_D("handle signal: %d, handler 0x%08x", signo, handler); if (handler) handler(signo); level = rt_hw_interrupt_disable(); - tid->sig_pending &= ~sig_mask(signo); error = -RT_EINTR; rt_mp_free(si_node); /* release this siginfo node */ @@ -335,10 +428,16 @@ void rt_thread_handle_sig(rt_bool_t clean_state) } /* whether clean signal status */ - if (clean_state == RT_TRUE) tid->stat &= ~RT_THREAD_STAT_SIGNAL; + if (clean_state == RT_TRUE) + { + tid->stat &= ~RT_THREAD_STAT_SIGNAL; + } + else + { + return; + } } } - rt_hw_interrupt_enable(level); } @@ -364,30 +463,30 @@ void rt_thread_alloc_sig(rt_thread_t tid) void rt_thread_free_sig(rt_thread_t tid) { rt_base_t level; - struct siginfo_node *si_list; + struct siginfo_node *si_node; rt_sighandler_t *sig_vectors; level = rt_hw_interrupt_disable(); - si_list = (struct siginfo_node *)tid->si_list; + si_node = (struct siginfo_node *)tid->si_list; tid->si_list = RT_NULL; sig_vectors = tid->sig_vectors; tid->sig_vectors = RT_NULL; rt_hw_interrupt_enable(level); - if (si_list) + if (si_node) { struct rt_slist_node *node; - struct siginfo_node *si_node; + struct rt_slist_node *node_to_free; LOG_D("free signal info list"); - node = &(si_list->list); + node = &(si_node->list); do { - si_node = rt_slist_entry(node, struct siginfo_node, list); - rt_mp_free(si_node); - + node_to_free = node; node = node->next; + si_node = rt_slist_entry(node_to_free, struct siginfo_node, list); + rt_mp_free(si_node); } while (node); } @@ -418,30 +517,23 @@ int rt_thread_kill(rt_thread_t tid, int sig) struct rt_slist_node *node; struct siginfo_node *entry; - node = (struct rt_slist_node *)tid->si_list; - rt_hw_interrupt_enable(level); + si_node = (struct siginfo_node *)tid->si_list; + if (si_node) + node = (struct rt_slist_node *)&si_node->list; + else + node = RT_NULL; /* update sig info */ - rt_enter_critical(); for (; (node) != RT_NULL; node = node->next) { entry = rt_slist_entry(node, struct siginfo_node, list); if (entry->si.si_signo == sig) { memcpy(&(entry->si), &si, sizeof(siginfo_t)); - rt_exit_critical(); + rt_hw_interrupt_enable(level); return 0; } } - rt_exit_critical(); - - /* disable interrupt to protect tcb */ - level = rt_hw_interrupt_disable(); - } - else - { - /* a new signal */ - tid->sig_pending |= sig_mask(sig); } rt_hw_interrupt_enable(level); @@ -452,14 +544,22 @@ int rt_thread_kill(rt_thread_t tid, int sig) memcpy(&(si_node->si), &si, sizeof(siginfo_t)); level = rt_hw_interrupt_disable(); - if (!tid->si_list) tid->si_list = si_node; - else + + if (tid->si_list) { struct siginfo_node *si_list; si_list = (struct siginfo_node *)tid->si_list; rt_slist_append(&(si_list->list), &(si_node->list)); } + else + { + tid->si_list = si_node; + } + + /* a new signal */ + tid->sig_pending |= sig_mask(sig); + rt_hw_interrupt_enable(level); } else diff --git a/src/thread.c b/src/thread.c index 0f2e1dc3d0..276bba9938 100644 --- a/src/thread.c +++ b/src/thread.c @@ -191,7 +191,9 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread, thread->sig_mask = 0x00; thread->sig_pending = 0x00; +#ifndef RT_USING_SMP thread->sig_ret = RT_NULL; +#endif thread->sig_vectors = RT_NULL; thread->si_list = RT_NULL; #endif -- cgit v1.3.1 From c5249157d7b92ed29cee9df440b4a9928e1a4e4f Mon Sep 17 00:00:00 2001 From: shaojinchun Date: Fri, 10 May 2019 10:41:03 +0800 Subject: 增加signal的状态位说明及一处代码修改,以防止编译警告 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/rtdef.h | 6 +++--- src/signal.c | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'include/rtdef.h') diff --git a/include/rtdef.h b/include/rtdef.h index 253333253a..2aff4afdb0 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -492,10 +492,10 @@ typedef siginfo_t rt_siginfo_t; #define RT_THREAD_CLOSE 0x04 /**< Closed status */ #define RT_THREAD_STAT_MASK 0x0f -#define RT_THREAD_STAT_SIGNAL 0x10 +#define RT_THREAD_STAT_SIGNAL 0x10 /**< task hold signals */ #define RT_THREAD_STAT_SIGNAL_READY (RT_THREAD_STAT_SIGNAL | RT_THREAD_READY) -#define RT_THREAD_STAT_SIGNAL_WAIT 0x20 -#define RT_THREAD_STAT_SIGNAL_PENDING 0x40 +#define RT_THREAD_STAT_SIGNAL_WAIT 0x20 /**< task is waiting for signals */ +#define RT_THREAD_STAT_SIGNAL_PENDING 0x40 /**< signals is held and it has not been procressed */ #define RT_THREAD_STAT_SIGNAL_MASK 0xf0 /** diff --git a/src/signal.c b/src/signal.c index 0cab6215af..0c0f990f46 100644 --- a/src/signal.c +++ b/src/signal.c @@ -56,7 +56,8 @@ static void _signal_entry(void *parameter) { struct rt_cpu* pcpu = rt_cpu_self(); - if (--pcpu->current_thread->cpus_lock_nest == 0) + pcpu->current_thread->cpus_lock_nest--; + if (pcpu->current_thread->cpus_lock_nest == 0) { pcpu->current_thread->scheduler_lock_nest--; } -- cgit v1.3.1 From b4e141fee15d21c53c6f22fde6e33aed37732e13 Mon Sep 17 00:00:00 2001 From: Bernard Xiong Date: Fri, 17 May 2019 08:34:10 +0800 Subject: Update rtdef.h --- include/rtdef.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'include/rtdef.h') diff --git a/include/rtdef.h b/include/rtdef.h index 2aff4afdb0..94c9b5f413 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -30,6 +30,7 @@ * add struct rt_cpu * add smp relevant macros * 2019-01-27 Bernard change version number to v4.0.1 + * 2019-05-17 Bernard change version number to v4.0.2 */ #ifndef __RT_DEF_H__ @@ -51,7 +52,7 @@ extern "C" { /* RT-Thread version information */ #define RT_VERSION 4L /**< major version number */ #define RT_SUBVERSION 0L /**< minor version number */ -#define RT_REVISION 1L /**< revise version number */ +#define RT_REVISION 2L /**< revise version number */ /* RT-Thread version */ #define RTTHREAD_VERSION ((RT_VERSION * 10000) + \ -- cgit v1.3.1 From 89b90592ee0d2db6a5c1fc8b38e5904751754ffe Mon Sep 17 00:00:00 2001 From: tyustli <1225613647@qq.com> Date: Wed, 12 Jun 2019 09:38:32 +0800 Subject: add touch framework --- components/drivers/include/drivers/i2c.h | 7 + components/drivers/touch/SConscript | 11 ++ components/drivers/touch/touch.c | 275 +++++++++++++++++++++++++++++++ components/drivers/touch/touch.h | 105 ++++++++++++ components/finsh/cmd.c | 1 + include/rtdef.h | 1 + 6 files changed, 400 insertions(+) create mode 100644 components/drivers/touch/SConscript create mode 100644 components/drivers/touch/touch.c create mode 100644 components/drivers/touch/touch.h (limited to 'include/rtdef.h') diff --git a/components/drivers/include/drivers/i2c.h b/components/drivers/include/drivers/i2c.h index e2a9e7d0b8..c6376efa89 100644 --- a/components/drivers/include/drivers/i2c.h +++ b/components/drivers/include/drivers/i2c.h @@ -60,6 +60,13 @@ struct rt_i2c_bus_device void *priv; }; +struct rt_i2c_client +{ + struct rt_device parent; + struct rt_i2c_bus_device *bus; + rt_uint16_t client_addr; +}; + #ifdef RT_I2C_DEBUG #define i2c_dbg(fmt, ...) rt_kprintf(fmt, ##__VA_ARGS__) #else diff --git a/components/drivers/touch/SConscript b/components/drivers/touch/SConscript new file mode 100644 index 0000000000..f370b2aba6 --- /dev/null +++ b/components/drivers/touch/SConscript @@ -0,0 +1,11 @@ +# SConscript for touch framework + +from building import * + +cwd = GetCurrentDir() +src = ['touch.c'] +CPPPATH = [cwd, cwd + '/../include'] + +group = DefineGroup('Touch', src, depend = ['RT_USING_TOUCH', 'RT_USING_DEVICE'], CPPPATH = CPPPATH) + +Return('group') diff --git a/components/drivers/touch/touch.c b/components/drivers/touch/touch.c new file mode 100644 index 0000000000..37456609dc --- /dev/null +++ b/components/drivers/touch/touch.c @@ -0,0 +1,275 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-05-20 tyustli the first version + */ + +#include "touch.h" +#include + +#define DBG_TAG "touch" +#define DBG_LVL DBG_INFO +#include + +/* touch interrupt handler function */ +void rt_touch_cb(rt_touch_t touch) +{ + if (touch->parent.rx_indicate == RT_NULL) + { + return; + } + + if (touch->irq_handle != RT_NULL) + { + touch->irq_handle(touch); + } + + touch->parent.rx_indicate(&touch->parent, 1); +} + +/* ISR for touch interrupt */ +static void irq_callback(void *args) +{ + rt_touch_t touch; + + touch = (rt_touch_t)args; + + rt_touch_cb(touch); +} + +/* touch interrupt initialization function */ +static rt_err_t rt_touch_irq_init(rt_touch_t touch) +{ + if (touch->config.irq_pin.pin == RT_PIN_NONE) + { + return -RT_EINVAL; + } + + rt_pin_mode(touch->config.irq_pin.pin, touch->config.irq_pin.mode); + + if (touch->config.irq_pin.mode == PIN_MODE_INPUT_PULLDOWN) + { + rt_pin_attach_irq(touch->config.irq_pin.pin, PIN_IRQ_MODE_RISING, irq_callback, (void *)touch); + } + else if (touch->config.irq_pin.mode == PIN_MODE_INPUT_PULLUP) + { + rt_pin_attach_irq(touch->config.irq_pin.pin, PIN_IRQ_MODE_FALLING, irq_callback, (void *)touch); + } + else if (touch->config.irq_pin.mode == PIN_MODE_INPUT) + { + rt_pin_attach_irq(touch->config.irq_pin.pin, PIN_IRQ_MODE_RISING_FALLING, irq_callback, (void *)touch); + } + + rt_pin_irq_enable(touch->config.irq_pin.pin, PIN_IRQ_ENABLE); + + LOG_I("interrupt init success"); + + return 0; +} + +/* touch interrupt enable */ +static void rt_touch_irq_enable(rt_touch_t touch) +{ + if (touch->config.irq_pin.pin != RT_PIN_NONE) + { + rt_pin_irq_enable(touch->config.irq_pin.pin, RT_TRUE); + } +} + +/* touch interrupt disable */ +static void rt_touch_irq_disable(rt_touch_t touch) +{ + if (touch->config.irq_pin.pin != RT_PIN_NONE) + { + rt_pin_irq_enable(touch->config.irq_pin.pin, RT_FALSE); + } +} + +static rt_err_t rt_touch_open(rt_device_t dev, rt_uint16_t oflag) +{ + rt_touch_t touch; + RT_ASSERT(dev != RT_NULL); + touch = (rt_touch_t)dev; + + if (oflag & RT_DEVICE_FLAG_INT_RX && dev->flag & RT_DEVICE_FLAG_INT_RX) + { + /* Initialization touch interrupt */ + rt_touch_irq_init(touch); + } + + return RT_EOK; +} + +static rt_err_t rt_touch_close(rt_device_t dev) +{ + rt_touch_t touch; + RT_ASSERT(dev != RT_NULL); + touch = (rt_touch_t)dev; + + /* touch disable interrupt */ + rt_touch_irq_disable(touch); + + return RT_EOK; +} + +static rt_size_t rt_touch_read(rt_device_t dev, rt_off_t pos, void *buf, rt_size_t len) +{ + rt_touch_t touch; + rt_size_t result = 0; + RT_ASSERT(dev != RT_NULL); + touch = (rt_touch_t)dev; + + if (buf == NULL || len == 0) + { + return 0; + } + + result = touch->ops->touch_readpoint(touch, buf, len); + + return result; +} + +static rt_err_t rt_touch_control(rt_device_t dev, int cmd, void *args) +{ + rt_touch_t touch; + rt_err_t result = RT_EOK; + RT_ASSERT(dev != RT_NULL); + touch = (rt_touch_t)dev; + + switch (cmd) + { + case RT_TOUCH_CTRL_GET_ID: + if (args) + { + result = touch->ops->touch_control(touch, RT_TOUCH_CTRL_GET_ID, args); + } + else + { + result = RT_ERROR; + } + + break; + case RT_TOUCH_CTRL_GET_INFO: + if (args) + { + result = touch->ops->touch_control(touch, RT_TOUCH_CTRL_GET_INFO, args); + } + else + { + result = RT_ERROR; + } + + break; + case RT_TOUCH_CTRL_SET_MODE: + result = touch->ops->touch_control(touch, RT_TOUCH_CTRL_SET_MODE, args); + + if (result == RT_EOK) + { + rt_uint16_t mode; + mode = *(rt_uint16_t*)args; + if (mode == RT_DEVICE_FLAG_INT_RX) + { + rt_touch_irq_enable(touch); /* enable interrupt */ + } + } + + break; + case RT_TOUCH_CTRL_SET_X_RANGE: + result = touch->ops->touch_control(touch, RT_TOUCH_CTRL_SET_X_RANGE, args); + + if (result == RT_EOK) + { + touch->info.range_x = *(rt_int32_t *)args; + LOG_D("set x coordinate range :%d\n", touch->info.range_x); + } + + break; + case RT_TOUCH_CTRL_SET_Y_RANGE: + result = touch->ops->touch_control(touch, RT_TOUCH_CTRL_SET_Y_RANGE, args); + + if (result == RT_EOK) + { + touch->info.range_y = *(rt_uint32_t *)args; + LOG_D("set y coordinate range :%d \n", touch->info.range_x); + } + + break; + default: + return -RT_ERROR; + } + + return result; +} + +#ifdef RT_USING_DEVICE_OPS +const static struct rt_device_ops rt_touch_ops = +{ + RT_NULL, + rt_touch_open, + rt_touch_close, + rt_touch_read, + RT_NULL, + rt_touch_control +}; +#endif + +/* + * touch register + */ +int rt_hw_touch_register(rt_touch_t touch, + const char *name, + rt_uint32_t flag, + void *data) +{ + rt_int8_t result; + rt_device_t device; + RT_ASSERT(touch != RT_NULL); + + char *touch_name = RT_NULL; + char *device_name = RT_NULL; + + touch_name = "touch_"; + device_name = rt_calloc(1, rt_strlen(touch_name) + 1 + rt_strlen(name)); + + if (device_name == RT_NULL) + { + LOG_E("device_name calloc failed!"); + return -RT_ERROR; + } + + rt_memcpy(device_name, touch_name, rt_strlen(touch_name) + 1); + strcat(device_name, name); + + device = &touch->parent; + +#ifdef RT_USING_DEVICE_OPS + device->ops = &rt_touch_ops; +#else + device->init = RT_NULL; + device->open = rt_touch_open; + device->close = rt_touch_close; + device->read = rt_touch_read; + device->write = RT_NULL; + device->control = rt_touch_control; +#endif + device->type = RT_Device_Class_Touch; + device->rx_indicate = RT_NULL; + device->tx_complete = RT_NULL; + device->user_data = data; + + result = rt_device_register(device, device_name, flag | RT_DEVICE_FLAG_STANDALONE); + + if (result != RT_EOK) + { + LOG_E("rt_touch register err code: %d", result); + return result; + } + + LOG_I("rt_touch init success"); + + return RT_EOK; +} diff --git a/components/drivers/touch/touch.h b/components/drivers/touch/touch.h new file mode 100644 index 0000000000..4fa19aed47 --- /dev/null +++ b/components/drivers/touch/touch.h @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-05-20 tyustli the first version + */ + +#ifndef __TOUCH_H__ +#define __TOUCH_H__ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef RT_USING_RTC +#define rt_touch_get_ts() time(RT_NULL) /* API for the touch to get the timestamp */ +#else +#define rt_touch_get_ts() rt_tick_get() /* API for the touch to get the timestamp */ +#endif + +#define RT_PIN_NONE 0xFFFF /* RT PIN NONE */ + +/* Touch vendor types */ +#define RT_TOUCH_VENDOR_UNKNOWN (0) /* unknown */ +#define RT_TOUCH_VENDOR_GT (1) /* GTxx series */ +#define RT_TOUCH_VENDOR_FT (2) /* FTxx series */ + +/* Touch ic type*/ +#define RT_TOUCH_TYPE_NONE (0) /* touch ic none */ +#define RT_TOUCH_TYPE_CAPACITANCE (1) /* capacitance ic */ +#define RT_TOUCH_TYPE_RESISTANCE (2) /* resistance ic */ + +/* Touch control cmd types */ +#define RT_TOUCH_CTRL_GET_ID (0) /* Get device id */ +#define RT_TOUCH_CTRL_GET_INFO (1) /* Get touch info */ +#define RT_TOUCH_CTRL_SET_MODE (2) /* Set touch's work mode. ex. RT_TOUCH_MODE_POLLING,RT_TOUCH_MODE_INT */ +#define RT_TOUCH_CTRL_SET_X_RANGE (3) /* Set x coordinate range */ +#define RT_TOUCH_CTRL_SET_Y_RANGE (4) /* Set y coordinate range */ +#define RT_TOUCH_CTRL_SET_X_TO_Y (5) /* Set X Y coordinate exchange */ + +/* Touch event */ +#define RT_TOUCH_EVENT_NONE (0) /* Touch none */ +#define RT_TOUCH_EVENT_UP (1) /* Touch up event */ +#define RT_TOUCH_EVENT_DOWN (2) /* Touch down event */ +#define RT_TOUCH_EVENT_MOVE (3) /* Touch move event */ + +struct rt_touch_info +{ + rt_uint8_t type; /* The touch type */ + rt_uint8_t vendor; /* Vendor of touchs */ + rt_uint8_t point_num; /* Support point num */ + rt_int32_t range_x; /* X coordinate range */ + rt_int32_t range_y; /* Y coordinate range */ +}; + +struct rt_touch_config +{ + struct rt_device_pin_mode irq_pin; /* Interrupt pin, The purpose of this pin is to notification read data */ + char *dev_name; /* The name of the communication device */ + void *user_data; +}; + +typedef struct rt_touch_device *rt_touch_t; +struct rt_touch_device +{ + struct rt_device parent; /* The standard device */ + struct rt_touch_info info; /* The touch info data */ + struct rt_touch_config config; /* The touch config data */ + + const struct rt_touch_ops *ops; /* The touch ops */ + rt_err_t (*irq_handle)(rt_touch_t touch); /* Called when an interrupt is generated, registered by the driver */ +}; + +struct rt_touch_data +{ + rt_uint8_t event; /* The touch event of the data */ + rt_uint8_t track_id; /* Track id of point */ + rt_uint8_t width; /* Point of width */ + rt_uint16_t x_coordinate; /* Point of x coordinate */ + rt_uint16_t y_coordinate; /* Point of y coordinate */ + rt_tick_t timestamp; /* The timestamp when the data was received */ +}; + +struct rt_touch_ops +{ + rt_size_t (*touch_readpoint)(struct rt_touch_device *touch, void *buf, rt_size_t touch_num); + rt_err_t (*touch_control)(struct rt_touch_device *touch, int cmd, void *arg); +}; + +int rt_hw_touch_register(rt_touch_t touch, + const char *name, + rt_uint32_t flag, + void *data); + +#ifdef __cplusplus +} +#endif + +#endif /* __TOUCH_H__ */ diff --git a/components/finsh/cmd.c b/components/finsh/cmd.c index 680d9d01d7..608e962f6c 100644 --- a/components/finsh/cmd.c +++ b/components/finsh/cmd.c @@ -801,6 +801,7 @@ static char *const device_type_str[] = "Timer Device", "Miscellaneous Device", "Sensor Device", + "Touch Device", "Unknown" }; diff --git a/include/rtdef.h b/include/rtdef.h index 94c9b5f413..c97e938f75 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -854,6 +854,7 @@ enum rt_device_class_type RT_Device_Class_Timer, /**< Timer device */ RT_Device_Class_Miscellaneous, /**< Miscellaneous device */ RT_Device_Class_Sensor, /**< Sensor device */ + RT_Device_Class_Touch, /**< Touch device */ RT_Device_Class_Unknown /**< unknown device */ }; -- cgit v1.3.1 From ae1fdc70f0086c0e12bfe02c9b69153e7743dc22 Mon Sep 17 00:00:00 2001 From: shaojinchun Date: Mon, 16 Sep 2019 16:55:40 +0800 Subject: 修正rt_mp_free有可能唤醒一个错误任务指针的问题 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/finsh/cmd.c | 15 ++++++++++++--- include/rtdef.h | 1 - src/mempool.c | 14 +------------- 3 files changed, 13 insertions(+), 17 deletions(-) (limited to 'include/rtdef.h') diff --git a/components/finsh/cmd.c b/components/finsh/cmd.c index 680d9d01d7..f8b774bd6f 100644 --- a/components/finsh/cmd.c +++ b/components/finsh/cmd.c @@ -675,6 +675,8 @@ long list_mempool(void) { struct rt_object *obj; struct rt_mempool *mp; + int suspend_thread_count; + rt_list_t *node; obj = rt_list_entry(obj_list[i], struct rt_object, list); level = rt_hw_interrupt_disable(); @@ -687,7 +689,14 @@ long list_mempool(void) rt_hw_interrupt_enable(level); mp = (struct rt_mempool *)obj; - if (mp->suspend_thread_count > 0) + + suspend_thread_count = 0; + for (node = mp->suspend_thread.next; node != &mp->suspend_thread; node = node->next) + { + suspend_thread_count++; + } + + if (suspend_thread_count > 0) { rt_kprintf("%-*.*s %04d %04d %04d %d:", maxlen, RT_NAME_MAX, @@ -695,7 +704,7 @@ long list_mempool(void) mp->block_size, mp->block_total_count, mp->block_free_count, - mp->suspend_thread_count); + suspend_thread_count); show_wait_queue(&(mp->suspend_thread)); rt_kprintf("\n"); } @@ -707,7 +716,7 @@ long list_mempool(void) mp->block_size, mp->block_total_count, mp->block_free_count, - mp->suspend_thread_count); + suspend_thread_count); } } } diff --git a/include/rtdef.h b/include/rtdef.h index 94c9b5f413..4ac9209231 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -815,7 +815,6 @@ struct rt_mempool rt_size_t block_free_count; /**< numbers of free memory block */ rt_list_t suspend_thread; /**< threads pended on this resource */ - rt_size_t suspend_thread_count; /**< numbers of thread pended on this resource */ }; typedef struct rt_mempool *rt_mp_t; #endif diff --git a/src/mempool.c b/src/mempool.c index 2184586aba..62cdb69269 100644 --- a/src/mempool.c +++ b/src/mempool.c @@ -103,7 +103,6 @@ rt_err_t rt_mp_init(struct rt_mempool *mp, /* initialize suspended thread list */ rt_list_init(&(mp->suspend_thread)); - mp->suspend_thread_count = 0; /* initialize free block list */ block_ptr = (rt_uint8_t *)mp->start_address; @@ -157,9 +156,6 @@ rt_err_t rt_mp_detach(struct rt_mempool *mp) */ rt_thread_resume(thread); - /* decrease suspended thread count */ - mp->suspend_thread_count --; - /* enable interrupt */ rt_hw_interrupt_enable(temp); } @@ -219,7 +215,6 @@ rt_mp_t rt_mp_create(const char *name, /* initialize suspended thread list */ rt_list_init(&(mp->suspend_thread)); - mp->suspend_thread_count = 0; /* initialize free block list */ block_ptr = (rt_uint8_t *)mp->start_address; @@ -275,9 +270,6 @@ rt_err_t rt_mp_delete(rt_mp_t mp) */ rt_thread_resume(thread); - /* decrease suspended thread count */ - mp->suspend_thread_count --; - /* enable interrupt */ rt_hw_interrupt_enable(temp); } @@ -334,7 +326,6 @@ void *rt_mp_alloc(rt_mp_t mp, rt_int32_t time) /* need suspend thread */ rt_thread_suspend(thread); rt_list_insert_after(&(mp->suspend_thread), &(thread->tlist)); - mp->suspend_thread_count++; if (time > 0) { @@ -418,7 +409,7 @@ void rt_mp_free(void *block) *block_ptr = mp->block_list; mp->block_list = (rt_uint8_t *)block_ptr; - if (mp->suspend_thread_count > 0) + if (!rt_list_isempty(&(mp->suspend_thread))) { /* get the suspended thread */ thread = rt_list_entry(mp->suspend_thread.next, @@ -431,9 +422,6 @@ void rt_mp_free(void *block) /* resume thread */ rt_thread_resume(thread); - /* decrease suspended thread count */ - mp->suspend_thread_count --; - /* enable interrupt */ rt_hw_interrupt_enable(level); -- cgit v1.3.1 From e4671d291689e71967afa5dec4ff25d310faa3fb Mon Sep 17 00:00:00 2001 From: tangyuxin <462747508@qq.com> Date: Mon, 16 Sep 2019 15:12:42 +0800 Subject: [kernel][ipc] add send wait support for message queue --- include/rtdef.h | 2 + include/rtthread.h | 4 ++ src/ipc.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 131 insertions(+), 5 deletions(-) (limited to 'include/rtdef.h') diff --git a/include/rtdef.h b/include/rtdef.h index 7c05178e3c..b25a6a87d5 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -742,6 +742,8 @@ struct rt_messagequeue void *msg_queue_head; /**< list head */ void *msg_queue_tail; /**< list tail */ void *msg_queue_free; /**< pointer indicated the free node of queue */ + + rt_list_t suspend_sender_thread; /**< sender thread suspended on this message queue */ }; typedef struct rt_messagequeue *rt_mq_t; #endif diff --git a/include/rtthread.h b/include/rtthread.h index 04db77da46..d703f36268 100644 --- a/include/rtthread.h +++ b/include/rtthread.h @@ -379,6 +379,10 @@ rt_mq_t rt_mq_create(const char *name, rt_err_t rt_mq_delete(rt_mq_t mq); rt_err_t rt_mq_send(rt_mq_t mq, const void *buffer, rt_size_t size); +rt_err_t rt_mq_send_wait(rt_mq_t mq, + const void *buffer, + rt_size_t size, + rt_int32_t timeout); rt_err_t rt_mq_urgent(rt_mq_t mq, const void *buffer, rt_size_t size); rt_err_t rt_mq_recv(rt_mq_t mq, void *buffer, diff --git a/src/ipc.c b/src/ipc.c index c6a05e96a4..16d714b3f4 100644 --- a/src/ipc.c +++ b/src/ipc.c @@ -33,6 +33,7 @@ * 2011-12-18 Bernard add more parameter checking in message queue * 2013-09-14 Grissiom add an option check in rt_event_recv * 2018-10-02 Bernard add 64bit support for mailbox + * 2019-09-16 tyx add send wait support for message queue */ #include @@ -1823,6 +1824,9 @@ rt_err_t rt_mq_init(rt_mq_t mq, /* the initial entry is zero */ mq->entry = 0; + /* init an additional list of sender suspend thread */ + rt_list_init(&(mq->suspend_sender_thread)); + return RT_EOK; } RTM_EXPORT(rt_mq_init); @@ -1843,6 +1847,8 @@ rt_err_t rt_mq_detach(rt_mq_t mq) /* resume all suspended thread */ rt_ipc_list_resume_all(&mq->parent.suspend_thread); + /* also resume all message queue private suspended thread */ + rt_ipc_list_resume_all(&(mq->suspend_sender_thread)); /* detach message queue object */ rt_object_detach(&(mq->parent.parent)); @@ -1916,6 +1922,9 @@ rt_mq_t rt_mq_create(const char *name, /* the initial entry is zero */ mq->entry = 0; + /* init an additional list of sender suspend thread */ + rt_list_init(&(mq->suspend_sender_thread)); + return mq; } RTM_EXPORT(rt_mq_create); @@ -1938,6 +1947,8 @@ rt_err_t rt_mq_delete(rt_mq_t mq) /* resume all suspended thread */ rt_ipc_list_resume_all(&(mq->parent.suspend_thread)); + /* also resume all message queue private suspended thread */ + rt_ipc_list_resume_all(&(mq->suspend_sender_thread)); /* free message queue pool */ RT_KERNEL_FREE(mq->msg_pool); @@ -1951,19 +1962,25 @@ RTM_EXPORT(rt_mq_delete); #endif /** - * This function will send a message to message queue object, if there are - * threads suspended on message queue object, it will be waked up. + * This function will send a message to message queue object. If the message queue is full, + * current thread will be suspended until timeout. * * @param mq the message queue object * @param buffer the message * @param size the size of buffer + * @param timeout the waiting time * * @return the error code */ -rt_err_t rt_mq_send(rt_mq_t mq, const void *buffer, rt_size_t size) +rt_err_t rt_mq_send_wait(rt_mq_t mq, + const void *buffer, + rt_size_t size, + rt_int32_t timeout) { register rt_ubase_t temp; struct rt_mq_message *msg; + rt_uint32_t tick_delta; + struct rt_thread *thread; /* parameter check */ RT_ASSERT(mq != RT_NULL); @@ -1975,6 +1992,11 @@ rt_err_t rt_mq_send(rt_mq_t mq, const void *buffer, rt_size_t size) if (size > mq->msg_size) return -RT_ERROR; + /* initialize delta tick */ + tick_delta = 0; + /* get current thread */ + thread = rt_thread_self(); + RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mq->parent.parent))); /* disable interrupt */ @@ -1982,14 +2004,78 @@ rt_err_t rt_mq_send(rt_mq_t mq, const void *buffer, rt_size_t size) /* get a free list, there must be an empty item */ msg = (struct rt_mq_message *)mq->msg_queue_free; - /* message queue is full */ - if (msg == RT_NULL) + /* for non-blocking call */ + if (msg == RT_NULL && timeout == 0) { /* enable interrupt */ rt_hw_interrupt_enable(temp); return -RT_EFULL; } + + /* message queue is full */ + while ((msg = mq->msg_queue_free) == RT_NULL) + { + /* reset error number in thread */ + thread->error = RT_EOK; + + /* no waiting, return timeout */ + if (timeout == 0) + { + /* enable interrupt */ + rt_hw_interrupt_enable(temp); + + return -RT_EFULL; + } + + RT_DEBUG_IN_THREAD_CONTEXT; + /* suspend current thread */ + rt_ipc_list_suspend(&(mq->suspend_sender_thread), + thread, + mq->parent.parent.flag); + + /* has waiting time, start thread timer */ + if (timeout > 0) + { + /* get the start tick of timer */ + tick_delta = rt_tick_get(); + + RT_DEBUG_LOG(RT_DEBUG_IPC, ("mq_send_wait: start timer of thread:%s\n", + thread->name)); + + /* 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(temp); + + /* re-schedule */ + rt_schedule(); + + /* resume from suspend state */ + if (thread->error != RT_EOK) + { + /* return error */ + return thread->error; + } + + /* disable interrupt */ + temp = rt_hw_interrupt_disable(); + + /* if it's not waiting forever and then re-calculate timeout tick */ + if (timeout > 0) + { + tick_delta = rt_tick_get() - tick_delta; + timeout -= tick_delta; + if (timeout < 0) + timeout = 0; + } + } + /* move free list pointer */ mq->msg_queue_free = msg->next; @@ -2037,6 +2123,22 @@ rt_err_t rt_mq_send(rt_mq_t mq, const void *buffer, rt_size_t size) return RT_EOK; } +RTM_EXPORT(rt_mq_send_wait) + +/** + * This function will send a message to message queue object, if there are + * threads suspended on message queue object, it will be waked up. + * + * @param mq the message queue object + * @param buffer the message + * @param size the size of buffer + * + * @return the error code + */ +rt_err_t rt_mq_send(rt_mq_t mq, const void *buffer, rt_size_t size) +{ + return rt_mq_send_wait(mq, buffer, size, 0); +} RTM_EXPORT(rt_mq_send); /** @@ -2257,6 +2359,22 @@ rt_err_t rt_mq_recv(rt_mq_t mq, /* put message to free list */ msg->next = (struct rt_mq_message *)mq->msg_queue_free; mq->msg_queue_free = msg; + + /* resume suspended thread */ + if (!rt_list_isempty(&(mq->suspend_sender_thread))) + { + rt_ipc_list_resume(&(mq->suspend_sender_thread)); + + /* enable interrupt */ + rt_hw_interrupt_enable(temp); + + RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mq->parent.parent))); + + rt_schedule(); + + return RT_EOK; + } + /* enable interrupt */ rt_hw_interrupt_enable(temp); @@ -2292,6 +2410,8 @@ rt_err_t rt_mq_control(rt_mq_t mq, int cmd, void *arg) /* resume all waiting thread */ rt_ipc_list_resume_all(&mq->parent.suspend_thread); + /* also resume all message queue private suspended thread */ + rt_ipc_list_resume_all(&(mq->suspend_sender_thread)); /* release all message in the queue */ while (mq->msg_queue_head != RT_NULL) -- cgit v1.3.1 From cb07e5fb24afebdf233847da551d3ede80727345 Mon Sep 17 00:00:00 2001 From: shaojinchun Date: Thu, 26 Sep 2019 18:13:07 +0800 Subject: 开放spinlock相关函数 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/rtdef.h | 1 + include/rthw.h | 14 +++++ libcpu/arm/cortex-a/cpu.c | 6 +++ libcpu/risc-v/k210/cpuport_smp.c | 5 ++ src/cpu.c | 113 +++++++++++++++++++++++++++++++++++++++ src/scheduler.c | 11 ++-- src/thread.c | 1 + 7 files changed, 148 insertions(+), 3 deletions(-) (limited to 'include/rtdef.h') diff --git a/include/rtdef.h b/include/rtdef.h index b25a6a87d5..3dc8a2a2a9 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -577,6 +577,7 @@ struct rt_thread rt_uint16_t scheduler_lock_nest; /**< scheduler lock count */ rt_uint16_t cpus_lock_nest; /**< cpus lock count */ + rt_uint16_t critical_lock_nest; /**< critical lock count */ #endif /*RT_USING_SMP*/ /* priority */ diff --git a/include/rthw.h b/include/rthw.h index ef3dc087ad..a77f2c1e46 100644 --- a/include/rthw.h +++ b/include/rthw.h @@ -143,6 +143,12 @@ typedef union { } tickets; } rt_hw_spinlock_t; +typedef struct +{ + rt_hw_spinlock_t lock; +} rt_spinlock_t; + +void rt_hw_spin_lock_init(rt_hw_spinlock_t *lock); void rt_hw_spin_lock(rt_hw_spinlock_t *lock); void rt_hw_spin_unlock(rt_hw_spinlock_t *lock); @@ -181,8 +187,16 @@ void rt_hw_secondary_cpu_idle_exec(void); #define rt_hw_spin_lock(lock) *(lock) = rt_hw_interrupt_disable() #define rt_hw_spin_unlock(lock) rt_hw_interrupt_enable(*(lock)) +typedef int rt_spinlock_t; + #endif +void rt_spin_lock_init(rt_spinlock_t *lock); +void rt_spin_lock(rt_spinlock_t *lock); +void rt_spin_unlock(rt_spinlock_t *lock); +rt_base_t rt_spin_lock_irqsave(rt_spinlock_t *lock); +void rt_spin_unlock_irqrestore(rt_spinlock_t *lock, rt_base_t level); + #ifdef __cplusplus } #endif diff --git a/libcpu/arm/cortex-a/cpu.c b/libcpu/arm/cortex-a/cpu.c index 6b129f89ee..7c6bf58a4b 100644 --- a/libcpu/arm/cortex-a/cpu.c +++ b/libcpu/arm/cortex-a/cpu.c @@ -14,6 +14,7 @@ #include #ifdef RT_USING_SMP + int rt_hw_cpu_id(void) { int cpu_id; @@ -25,6 +26,11 @@ int rt_hw_cpu_id(void) return cpu_id; }; +void rt_hw_spin_lock_init(rt_hw_spinlock_t *lock) +{ + lock->slock = 0; +} + void rt_hw_spin_lock(rt_hw_spinlock_t *lock) { unsigned long tmp; diff --git a/libcpu/risc-v/k210/cpuport_smp.c b/libcpu/risc-v/k210/cpuport_smp.c index a31314a13f..8c57dfeafc 100644 --- a/libcpu/risc-v/k210/cpuport_smp.c +++ b/libcpu/risc-v/k210/cpuport_smp.c @@ -25,6 +25,11 @@ int rt_hw_cpu_id(void) return read_csr(mhartid); } +void rt_hw_spin_lock_init(rt_hw_spinlock_t *lock) +{ + ((spinlock_t *)lock)->lock = 0; +} + void rt_hw_spin_lock(rt_hw_spinlock_t *lock) { spinlock_lock((spinlock_t *)lock); diff --git a/src/cpu.c b/src/cpu.c index fe3ba21bb0..a4075108e1 100644 --- a/src/cpu.c +++ b/src/cpu.c @@ -11,6 +11,119 @@ #include #include +#ifdef RT_USING_SMP +/*********************************** + * disable scheduler + ***********************************/ +static void rt_preempt_disable(void) +{ + register rt_base_t level; + struct rt_thread *current_thread; + + /* disable interrupt */ + level = rt_hw_local_irq_disable(); + + current_thread = rt_cpu_self()->current_thread; + if (!current_thread) + { + rt_hw_local_irq_enable(level); + return; + } + + /* lock scheduler for local cpu */ + current_thread->scheduler_lock_nest ++; + + /* enable interrupt */ + rt_hw_local_irq_enable(level); +} + +/*********************************** + * restore scheduler + ***********************************/ +static void rt_preempt_enable(void) +{ + register rt_base_t level; + struct rt_thread *current_thread; + + /* disable interrupt */ + level = rt_hw_local_irq_disable(); + + current_thread = rt_cpu_self()->current_thread; + if (!current_thread) + { + rt_hw_local_irq_enable(level); + return; + } + + /* unlock scheduler for local cpu */ + current_thread->scheduler_lock_nest --; + + rt_schedule(); + /* enable interrupt */ + rt_hw_local_irq_enable(level); +} +#endif + +void rt_spin_lock_init(rt_spinlock_t *lock) +{ +#ifdef RT_USING_SMP + rt_hw_spin_lock_init(&lock->lock); +#endif +} +RTM_EXPORT(rt_spin_lock_init) + +void rt_spin_lock(rt_spinlock_t *lock) +{ +#ifdef RT_USING_SMP + rt_preempt_disable(); + rt_hw_spin_lock(&lock->lock); +#else + rt_enter_critical(); +#endif +} +RTM_EXPORT(rt_spin_lock) + +void rt_spin_unlock(rt_spinlock_t *lock) +{ +#ifdef RT_USING_SMP + rt_hw_spin_unlock(&lock->lock); + rt_preempt_enable(); +#else + rt_exit_critical(); +#endif +} +RTM_EXPORT(rt_spin_unlock) + +rt_base_t rt_spin_lock_irqsave(rt_spinlock_t *lock) +{ + unsigned long level; + +#ifdef RT_USING_SMP + rt_preempt_disable(); + + level = rt_hw_local_irq_disable(); + rt_hw_spin_lock(&lock->lock); + + return level; +#else + return rt_hw_interrupt_disable(); +#endif +} +RTM_EXPORT(rt_spin_lock_irqsave) + +void rt_spin_unlock_irqrestore(rt_spinlock_t *lock, rt_base_t level) +{ +#ifdef RT_USING_SMP + rt_hw_spin_unlock(&lock->lock); + rt_hw_local_irq_enable(level); + + rt_preempt_enable(); +#else + rt_hw_interrupt_enable(level); +#endif +} +RTM_EXPORT(rt_spin_unlock_irqrestore) + #ifdef RT_USING_SMP static struct rt_cpu rt_cpus[RT_CPUS_NR]; diff --git a/src/scheduler.c b/src/scheduler.c index b487bd4b60..0faafaa2fc 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -840,11 +840,14 @@ void rt_enter_critical(void) */ /* lock scheduler for all cpus */ - if (current_thread->scheduler_lock_nest == !!current_thread->cpus_lock_nest) + if (current_thread->critical_lock_nest == 0) { rt_hw_spin_lock(&_rt_critical_lock); } + /* critical for local cpu */ + current_thread->critical_lock_nest ++; + /* lock scheduler for local cpu */ current_thread->scheduler_lock_nest ++; @@ -892,7 +895,9 @@ void rt_exit_critical(void) current_thread->scheduler_lock_nest --; - if (current_thread->scheduler_lock_nest == !!current_thread->cpus_lock_nest) + current_thread->critical_lock_nest --; + + if (current_thread->critical_lock_nest == 0) { rt_hw_spin_unlock(&_rt_critical_lock); } @@ -951,7 +956,7 @@ rt_uint16_t rt_critical_level(void) #ifdef RT_USING_SMP struct rt_thread *current_thread = rt_cpu_self()->current_thread; - return current_thread->scheduler_lock_nest; + return current_thread->critical_lock_nest; #else return rt_scheduler_lock_nest; #endif /*RT_USING_SMP*/ diff --git a/src/thread.c b/src/thread.c index ab203b1d49..a88729e52c 100644 --- a/src/thread.c +++ b/src/thread.c @@ -172,6 +172,7 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread, /* lock init */ thread->scheduler_lock_nest = 0; thread->cpus_lock_nest = 0; + thread->critical_lock_nest = 0; #endif /*RT_USING_SMP*/ /* initialize cleanup function and user data */ -- cgit v1.3.1 From 234af6aaa2ec2fd5c8c758a8f9fa29553ddeaf3f Mon Sep 17 00:00:00 2001 From: shaojinchun Date: Fri, 11 Oct 2019 09:13:40 +0800 Subject: 修正相同优先级任务切换太频繁的问题 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/rtdef.h | 1 + src/clock.c | 2 ++ src/scheduler.c | 15 +++++++++++++++ src/thread.c | 1 + 4 files changed, 19 insertions(+) (limited to 'include/rtdef.h') diff --git a/include/rtdef.h b/include/rtdef.h index 3dc8a2a2a9..90604e9b1d 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -608,6 +608,7 @@ struct rt_thread rt_ubase_t init_tick; /**< thread's initialized tick */ rt_ubase_t remaining_tick; /**< remaining tick */ + rt_ubase_t can_yield; /**< indicate whether remaining_tick has been reloaded since last schedule */ struct rt_timer thread_timer; /**< built-in thread timer */ diff --git a/src/clock.c b/src/clock.c index a652282b6e..d56f4b00f3 100644 --- a/src/clock.c +++ b/src/clock.c @@ -89,6 +89,8 @@ void rt_tick_increase(void) /* change to initialized tick */ thread->remaining_tick = thread->init_tick; + thread->can_yield = 1; + /* yield */ rt_thread_yield(); } diff --git a/src/scheduler.c b/src/scheduler.c index f671a49623..a4b4800d76 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -344,8 +344,13 @@ void rt_schedule(void) { to_thread = current_thread; } + else if (current_thread->current_priority == highest_ready_priority && current_thread->can_yield == 0) + { + to_thread = current_thread; + } else { + current_thread->can_yield = 0; rt_schedule_insert_thread(current_thread); } } @@ -435,8 +440,13 @@ void rt_schedule(void) { to_thread = rt_current_thread; } + else if (current_thread->current_priority == highest_ready_priority && current_thread->can_yield == 0) + { + to_thread = current_thread; + } else { + current_thread->can_yield = 0; need_insert_from_thread = 1; } } @@ -578,8 +588,13 @@ void rt_scheduler_do_irq_switch(void *context) { to_thread = current_thread; } + else if (current_thread->current_priority == highest_ready_priority && current_thread->can_yield == 0) + { + to_thread = current_thread; + } else { + current_thread->can_yield = 0; rt_schedule_insert_thread(current_thread); } } diff --git a/src/thread.c b/src/thread.c index a88729e52c..cf42c72a1b 100644 --- a/src/thread.c +++ b/src/thread.c @@ -159,6 +159,7 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread, /* tick init */ thread->init_tick = tick; thread->remaining_tick = tick; + thread->can_yield = 0; /* error and flags */ thread->error = RT_EOK; -- cgit v1.3.1 From baeda71eaa8c00b7741365dd5fef09207ce4d743 Mon Sep 17 00:00:00 2001 From: Dryad <513205481@qq.com> Date: Mon, 28 Oct 2019 10:38:05 +0800 Subject: 修复了g++编译器无法展开自动初始化宏的错误 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/rtdef.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'include/rtdef.h') diff --git a/include/rtdef.h b/include/rtdef.h index 3dc8a2a2a9..ca7887e40d 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -200,11 +200,11 @@ typedef int (*init_fn_t)(void); }; #define INIT_EXPORT(fn, level) \ const char __rti_##fn##_name[] = #fn; \ - RT_USED const struct rt_init_desc __rt_init_desc_##fn SECTION(".rti_fn."level) = \ + RT_USED const struct rt_init_desc __rt_init_desc_##fn SECTION(".rti_fn." level) = \ { __rti_##fn##_name, fn}; #else #define INIT_EXPORT(fn, level) \ - RT_USED const init_fn_t __rt_init_##fn SECTION(".rti_fn."level) = fn + RT_USED const init_fn_t __rt_init_##fn SECTION(".rti_fn." level) = fn #endif #endif #else -- cgit v1.3.1 From e03ac83ad3d22c578e367550d2ff714b53e79799 Mon Sep 17 00:00:00 2001 From: shaojinchun Date: Tue, 5 Nov 2019 08:41:38 +0800 Subject: 将yield状态置于tcb的stat位域中 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/rtdef.h | 6 ++++-- src/clock.c | 2 +- src/scheduler.c | 12 ++++++------ src/thread.c | 1 - 4 files changed, 11 insertions(+), 10 deletions(-) (limited to 'include/rtdef.h') diff --git a/include/rtdef.h b/include/rtdef.h index 90604e9b1d..119e9d7893 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -491,7 +491,10 @@ typedef siginfo_t rt_siginfo_t; #define RT_THREAD_RUNNING 0x03 /**< Running status */ #define RT_THREAD_BLOCK RT_THREAD_SUSPEND /**< Blocked status */ #define RT_THREAD_CLOSE 0x04 /**< Closed status */ -#define RT_THREAD_STAT_MASK 0x0f +#define RT_THREAD_STAT_MASK 0x07 + +#define RT_THREAD_STAT_YIELD 0x08 /**< indicate whether remaining_tick has been reloaded since last schedule */ +#define RT_THREAD_STAT_YIELD_MASK RT_THREAD_STAT_YIELD #define RT_THREAD_STAT_SIGNAL 0x10 /**< task hold signals */ #define RT_THREAD_STAT_SIGNAL_READY (RT_THREAD_STAT_SIGNAL | RT_THREAD_READY) @@ -608,7 +611,6 @@ struct rt_thread rt_ubase_t init_tick; /**< thread's initialized tick */ rt_ubase_t remaining_tick; /**< remaining tick */ - rt_ubase_t can_yield; /**< indicate whether remaining_tick has been reloaded since last schedule */ struct rt_timer thread_timer; /**< built-in thread timer */ diff --git a/src/clock.c b/src/clock.c index e288ff58a9..9b54101363 100644 --- a/src/clock.c +++ b/src/clock.c @@ -89,7 +89,7 @@ void rt_tick_increase(void) /* change to initialized tick */ thread->remaining_tick = thread->init_tick; - thread->can_yield = RT_TRUE; + thread->stat |= RT_THREAD_STAT_YIELD; /* yield */ rt_thread_yield(); diff --git a/src/scheduler.c b/src/scheduler.c index c58ecf6209..edcf11dba5 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -344,13 +344,13 @@ void rt_schedule(void) { to_thread = current_thread; } - else if (current_thread->current_priority == highest_ready_priority && current_thread->can_yield == RT_FALSE) + else if (current_thread->current_priority == highest_ready_priority && (current_thread->stat & RT_THREAD_STAT_YIELD_MASK) == 0) { to_thread = current_thread; } else { - current_thread->can_yield = RT_FALSE; + current_thread->stat &= ~RT_THREAD_STAT_YIELD_MASK; rt_schedule_insert_thread(current_thread); } } @@ -440,13 +440,13 @@ void rt_schedule(void) { to_thread = rt_current_thread; } - else if (rt_current_thread->current_priority == highest_ready_priority && rt_current_thread->can_yield == RT_FALSE) + else if (rt_current_thread->current_priority == highest_ready_priority && (rt_current_thread->stat & RT_THREAD_STAT_YIELD_MASK) == 0) { to_thread = rt_current_thread; } else { - rt_current_thread->can_yield = RT_FALSE; + rt_current_thread->stat &= ~RT_THREAD_STAT_YIELD_MASK; need_insert_from_thread = 1; } } @@ -588,13 +588,13 @@ void rt_scheduler_do_irq_switch(void *context) { to_thread = current_thread; } - else if (current_thread->current_priority == highest_ready_priority && current_thread->can_yield == RT_FALSE) + else if (current_thread->current_priority == highest_ready_priority && (current_thread->stat & RT_THREAD_STAT_YIELD_MASK) == 0) { to_thread = current_thread; } else { - current_thread->can_yield = RT_FALSE; + current_thread->stat &= ~RT_THREAD_STAT_YIELD_MASK; rt_schedule_insert_thread(current_thread); } } diff --git a/src/thread.c b/src/thread.c index 6f6fa1029a..a88729e52c 100644 --- a/src/thread.c +++ b/src/thread.c @@ -159,7 +159,6 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread, /* tick init */ thread->init_tick = tick; thread->remaining_tick = tick; - thread->can_yield = RT_FALSE; /* error and flags */ thread->error = RT_EOK; -- cgit v1.3.1 From 992d3af3aee1e8567080e33e27011170f60dfab9 Mon Sep 17 00:00:00 2001 From: Bernard Xiong Date: Mon, 16 Dec 2019 13:59:46 +0800 Subject: [kernel] code and comments cleanup --- include/libc/libc_signal.h | 6 +-- include/rtdef.h | 2 +- include/rthw.h | 2 +- src/clock.c | 2 +- src/device.c | 16 ++++---- src/ipc.c | 94 +++++++++++++++++++++++----------------------- src/kservice.c | 2 +- src/signal.c | 4 +- src/thread.c | 6 +-- 9 files changed, 67 insertions(+), 67 deletions(-) (limited to 'include/rtdef.h') diff --git a/include/libc/libc_signal.h b/include/libc/libc_signal.h index 71f8de125d..c77f36798c 100644 --- a/include/libc/libc_signal.h +++ b/include/libc/libc_signal.h @@ -24,7 +24,7 @@ extern "C" { NOTE: P1003.1c/D10, p. 34 adds sigev_notify_function and sigev_notify_attributes to the sigevent structure. */ -union sigval +union sigval { int sival_int; /* Integer signal value */ void *sival_ptr; /* Pointer signal value */ @@ -107,7 +107,7 @@ typedef unsigned long sigset_t; typedef void (*_sig_func_ptr)(int); -struct sigaction +struct sigaction { _sig_func_ptr sa_handler; sigset_t sa_mask; @@ -163,7 +163,7 @@ typedef unsigned long sigset_t; typedef void (*_sig_func_ptr)(int); -struct sigaction +struct sigaction { _sig_func_ptr sa_handler; sigset_t sa_mask; diff --git a/include/rtdef.h b/include/rtdef.h index 3f0b116785..b3aa38d3af 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -522,7 +522,7 @@ typedef siginfo_t rt_siginfo_t; /** * CPUs definitions - * + * */ struct rt_cpu { diff --git a/include/rthw.h b/include/rthw.h index 2a602dcfe7..89877faf90 100644 --- a/include/rthw.h +++ b/include/rthw.h @@ -37,7 +37,7 @@ extern "C" { #endif #ifndef RT_CPU_CACHE_LINE_SZ -#define RT_CPU_CACHE_LINE_SZ 32 +#define RT_CPU_CACHE_LINE_SZ 32 #endif enum RT_HW_CACHE_OPS diff --git a/src/clock.c b/src/clock.c index 9b54101363..97a09ddeaf 100644 --- a/src/clock.c +++ b/src/clock.c @@ -25,7 +25,7 @@ static rt_tick_t rt_tick = 0; #endif /** - * This function will init system tick and set it to zero. + * This function will initialize system tick and set it to zero. * @ingroup SystemInit * * @deprecated since 1.1.0, this function does not need to be invoked diff --git a/src/device.c b/src/device.c index 51fc6176a6..32c06b380c 100644 --- a/src/device.c +++ b/src/device.c @@ -95,7 +95,7 @@ RTM_EXPORT(rt_device_unregister); * @return the error code, RT_EOK on successfully. * * @deprecated since 1.2.x, this function is not needed because the initialization - * of a device is performed when applicaiton opens it. + * of a device is performed when application opens it. */ rt_err_t rt_device_init_all(void) { @@ -162,7 +162,7 @@ rt_device_t rt_device_create(int type, int attach_size) size = RT_ALIGN(sizeof(struct rt_device), RT_ALIGN_SIZE); attach_size = RT_ALIGN(attach_size, RT_ALIGN_SIZE); - /* use the totoal size */ + /* use the total size */ size += attach_size; device = (rt_device_t)rt_malloc(size); @@ -208,7 +208,7 @@ rt_err_t rt_device_init(rt_device_t dev) RT_ASSERT(dev != RT_NULL); - /* get device init handler */ + /* get device_init handler */ if (device_init != RT_NULL) { if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED)) @@ -269,7 +269,7 @@ rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag) return -RT_EBUSY; } - /* call device open interface */ + /* call device_open interface */ if (device_open != RT_NULL) { result = device_open(dev, oflag); @@ -317,7 +317,7 @@ rt_err_t rt_device_close(rt_device_t dev) if (dev->ref_count != 0) return RT_EOK; - /* call device close interface */ + /* call device_close interface */ if (device_close != RT_NULL) { result = device_close(dev); @@ -357,7 +357,7 @@ rt_size_t rt_device_read(rt_device_t dev, return 0; } - /* call device read interface */ + /* call device_read interface */ if (device_read != RT_NULL) { return device_read(dev, pos, buffer, size); @@ -396,7 +396,7 @@ rt_size_t rt_device_write(rt_device_t dev, return 0; } - /* call device write interface */ + /* call device_write interface */ if (device_write != RT_NULL) { return device_write(dev, pos, buffer, size); @@ -423,7 +423,7 @@ rt_err_t rt_device_control(rt_device_t dev, int cmd, void *arg) RT_ASSERT(dev != RT_NULL); RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device); - /* call device write interface */ + /* call device_write interface */ if (device_control != RT_NULL) { return device_control(dev, cmd, arg); diff --git a/src/ipc.c b/src/ipc.c index 6a8234a747..e6955bae37 100644 --- a/src/ipc.c +++ b/src/ipc.c @@ -60,7 +60,7 @@ extern void (*rt_object_put_hook)(struct rt_object *object); */ rt_inline rt_err_t rt_ipc_object_init(struct rt_ipc_object *ipc) { - /* init ipc object */ + /* initialize ipc object */ rt_list_init(&(ipc->suspend_thread)); return RT_EOK; @@ -159,13 +159,13 @@ rt_inline rt_err_t rt_ipc_list_resume_all(rt_list_t *list) struct rt_thread *thread; register rt_ubase_t temp; - /* wakeup all suspend threads */ + /* wakeup all suspended threads */ while (!rt_list_isempty(list)) { /* disable interrupt */ temp = rt_hw_interrupt_disable(); - /* get next suspend thread */ + /* get next suspended thread */ thread = rt_list_entry(list->next, struct rt_thread, tlist); /* set error code to RT_ERROR */ thread->error = -RT_ERROR; @@ -173,7 +173,7 @@ rt_inline rt_err_t rt_ipc_list_resume_all(rt_list_t *list) /* * resume thread * In rt_thread_resume function, it will remove current thread from - * suspend list + * suspended list */ rt_thread_resume(thread); @@ -191,7 +191,7 @@ rt_inline rt_err_t rt_ipc_list_resume_all(rt_list_t *list) * * @param sem the semaphore object * @param name the name of semaphore - * @param value the init value of semaphore + * @param value the initial value of semaphore * @param flag the flag of semaphore * * @return the operation status, RT_EOK on successful @@ -204,13 +204,13 @@ rt_err_t rt_sem_init(rt_sem_t sem, RT_ASSERT(sem != RT_NULL); RT_ASSERT(value < 0x10000U); - /* init object */ + /* initialize object */ rt_object_init(&(sem->parent.parent), RT_Object_Class_Semaphore, name); - /* init ipc object */ + /* initialize ipc object */ rt_ipc_object_init(&(sem->parent)); - /* set init value */ + /* set initial value */ sem->value = (rt_uint16_t)value; /* set parent */ @@ -236,7 +236,7 @@ rt_err_t rt_sem_detach(rt_sem_t sem) RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore); RT_ASSERT(rt_object_is_systemobject(&sem->parent.parent)); - /* wakeup all suspend threads */ + /* wakeup all suspended threads */ rt_ipc_list_resume_all(&(sem->parent.suspend_thread)); /* detach semaphore object */ @@ -251,7 +251,7 @@ RTM_EXPORT(rt_sem_detach); * This function will create a semaphore from system resource * * @param name the name of semaphore - * @param value the init value of semaphore + * @param value the initial value of semaphore * @param flag the flag of semaphore * * @return the created semaphore, RT_NULL on error happen @@ -270,10 +270,10 @@ rt_sem_t rt_sem_create(const char *name, rt_uint32_t value, rt_uint8_t flag) if (sem == RT_NULL) return sem; - /* init ipc object */ + /* initialize ipc object */ rt_ipc_object_init(&(sem->parent)); - /* set init value */ + /* set initial value */ sem->value = value; /* set parent */ @@ -301,7 +301,7 @@ rt_err_t rt_sem_delete(rt_sem_t sem) RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore); RT_ASSERT(rt_object_is_systemobject(&sem->parent.parent) == RT_FALSE); - /* wakeup all suspend threads */ + /* wakeup all suspended threads */ rt_ipc_list_resume_all(&(sem->parent.suspend_thread)); /* delete semaphore object */ @@ -532,10 +532,10 @@ rt_err_t rt_mutex_init(rt_mutex_t mutex, const char *name, rt_uint8_t flag) /* parameter check */ RT_ASSERT(mutex != RT_NULL); - /* init object */ + /* initialize object */ rt_object_init(&(mutex->parent.parent), RT_Object_Class_Mutex, name); - /* init ipc object */ + /* initialize ipc object */ rt_ipc_object_init(&(mutex->parent)); mutex->value = 1; @@ -566,10 +566,10 @@ rt_err_t rt_mutex_detach(rt_mutex_t mutex) RT_ASSERT(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex); RT_ASSERT(rt_object_is_systemobject(&mutex->parent.parent)); - /* wakeup all suspend threads */ + /* wakeup all suspended threads */ rt_ipc_list_resume_all(&(mutex->parent.suspend_thread)); - /* detach mutex object */ + /* detach semaphore object */ rt_object_detach(&(mutex->parent.parent)); return RT_EOK; @@ -598,7 +598,7 @@ rt_mutex_t rt_mutex_create(const char *name, rt_uint8_t flag) if (mutex == RT_NULL) return mutex; - /* init ipc object */ + /* initialize ipc object */ rt_ipc_object_init(&(mutex->parent)); mutex->value = 1; @@ -631,7 +631,7 @@ rt_err_t rt_mutex_delete(rt_mutex_t mutex) RT_ASSERT(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex); RT_ASSERT(rt_object_is_systemobject(&mutex->parent.parent) == RT_FALSE); - /* wakeup all suspend threads */ + /* wakeup all suspended threads */ rt_ipc_list_resume_all(&(mutex->parent.suspend_thread)); /* delete mutex object */ @@ -917,16 +917,16 @@ rt_err_t rt_event_init(rt_event_t event, const char *name, rt_uint8_t flag) /* parameter check */ RT_ASSERT(event != RT_NULL); - /* init object */ + /* initialize object */ rt_object_init(&(event->parent.parent), RT_Object_Class_Event, name); /* set parent flag */ event->parent.parent.flag = flag; - /* init ipc object */ + /* initialize ipc object */ rt_ipc_object_init(&(event->parent)); - /* init event */ + /* initialize event */ event->set = 0; return RT_EOK; @@ -980,10 +980,10 @@ rt_event_t rt_event_create(const char *name, rt_uint8_t flag) /* set parent */ event->parent.parent.flag = flag; - /* init ipc object */ + /* initialize ipc object */ rt_ipc_object_init(&(event->parent)); - /* init event */ + /* initialize event */ event->set = 0; return event; @@ -1050,7 +1050,7 @@ rt_err_t rt_event_send(rt_event_t event, rt_uint32_t set) event->set |= set; RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(event->parent.parent))); - + if (!rt_list_isempty(&event->parent.suspend_thread)) { /* search thread list to resume thread */ @@ -1073,7 +1073,7 @@ rt_err_t rt_event_send(rt_event_t event, rt_uint32_t set) { if (thread->event_set & event->set) { - /* save recieved event set */ + /* save the received event set */ thread->event_set = thread->event_set & event->set; /* received an OR event */ @@ -1143,7 +1143,7 @@ rt_err_t rt_event_recv(rt_event_t event, if (set == 0) return -RT_ERROR; - /* init status */ + /* initialize status */ status = -RT_ERROR; /* get current thread */ thread = rt_thread_self(); @@ -1253,7 +1253,7 @@ rt_err_t rt_event_control(rt_event_t event, int cmd, void *arg) /* parameter check */ RT_ASSERT(event != RT_NULL); RT_ASSERT(rt_object_get_type(&event->parent.parent) == RT_Object_Class_Event); - + if (cmd == RT_IPC_CMD_RESET) { /* disable interrupt */ @@ -1262,7 +1262,7 @@ rt_err_t rt_event_control(rt_event_t event, int cmd, void *arg) /* resume all waiting thread */ rt_ipc_list_resume_all(&event->parent.suspend_thread); - /* init event set */ + /* initialize event set */ event->set = 0; /* enable interrupt */ @@ -1299,23 +1299,23 @@ rt_err_t rt_mb_init(rt_mailbox_t mb, { RT_ASSERT(mb != RT_NULL); - /* init object */ + /* initialize object */ rt_object_init(&(mb->parent.parent), RT_Object_Class_MailBox, name); /* set parent flag */ mb->parent.parent.flag = flag; - /* init ipc object */ + /* initialize ipc object */ rt_ipc_object_init(&(mb->parent)); - /* init mailbox */ + /* initialize mailbox */ mb->msg_pool = (rt_ubase_t *)msgpool; mb->size = size; mb->entry = 0; mb->in_offset = 0; mb->out_offset = 0; - /* init an additional list of sender suspend thread */ + /* initialize an additional list of sender suspend thread */ rt_list_init(&(mb->suspend_sender_thread)); return RT_EOK; @@ -1372,10 +1372,10 @@ rt_mailbox_t rt_mb_create(const char *name, rt_size_t size, rt_uint8_t flag) /* set parent */ mb->parent.parent.flag = flag; - /* init ipc object */ + /* initialize ipc object */ rt_ipc_object_init(&(mb->parent)); - /* init mailbox */ + /* initialize mailbox */ mb->size = size; mb->msg_pool = (rt_ubase_t *)RT_KERNEL_MALLOC(mb->size * sizeof(rt_ubase_t)); if (mb->msg_pool == RT_NULL) @@ -1389,7 +1389,7 @@ rt_mailbox_t rt_mb_create(const char *name, rt_size_t size, rt_uint8_t flag) mb->in_offset = 0; mb->out_offset = 0; - /* init an additional list of sender suspend thread */ + /* initialize an additional list of sender suspend thread */ rt_list_init(&(mb->suspend_sender_thread)); return mb; @@ -1791,27 +1791,27 @@ rt_err_t rt_mq_init(rt_mq_t mq, /* parameter check */ RT_ASSERT(mq != RT_NULL); - /* init object */ + /* initialize object */ rt_object_init(&(mq->parent.parent), RT_Object_Class_MessageQueue, name); /* set parent flag */ mq->parent.parent.flag = flag; - /* init ipc object */ + /* initialize ipc object */ rt_ipc_object_init(&(mq->parent)); - /* set messasge pool */ + /* set message pool */ mq->msg_pool = msgpool; /* get correct message size */ mq->msg_size = RT_ALIGN(msg_size, RT_ALIGN_SIZE); mq->max_msgs = pool_size / (mq->msg_size + sizeof(struct rt_mq_message)); - /* init message list */ + /* initialize message list */ mq->msg_queue_head = RT_NULL; mq->msg_queue_tail = RT_NULL; - /* init message empty list */ + /* initialize message empty list */ mq->msg_queue_free = RT_NULL; for (temp = 0; temp < mq->max_msgs; temp ++) { @@ -1824,7 +1824,7 @@ rt_err_t rt_mq_init(rt_mq_t mq, /* the initial entry is zero */ mq->entry = 0; - /* init an additional list of sender suspend thread */ + /* initialize an additional list of sender suspend thread */ rt_list_init(&(mq->suspend_sender_thread)); return RT_EOK; @@ -1887,10 +1887,10 @@ rt_mq_t rt_mq_create(const char *name, /* set parent */ mq->parent.parent.flag = flag; - /* init ipc object */ + /* initialize ipc object */ rt_ipc_object_init(&(mq->parent)); - /* init message queue */ + /* initialize message queue */ /* get correct message size */ mq->msg_size = RT_ALIGN(msg_size, RT_ALIGN_SIZE); @@ -1905,11 +1905,11 @@ rt_mq_t rt_mq_create(const char *name, return RT_NULL; } - /* init message list */ + /* initialize message list */ mq->msg_queue_head = RT_NULL; mq->msg_queue_tail = RT_NULL; - /* init message empty list */ + /* initialize message empty list */ mq->msg_queue_free = RT_NULL; for (temp = 0; temp < mq->max_msgs; temp ++) { @@ -1922,7 +1922,7 @@ rt_mq_t rt_mq_create(const char *name, /* the initial entry is zero */ mq->entry = 0; - /* init an additional list of sender suspend thread */ + /* initialize an additional list of sender suspend thread */ rt_list_init(&(mq->suspend_sender_thread)); return mq; diff --git a/src/kservice.c b/src/kservice.c index 202bd97f83..58f04330f1 100644 --- a/src/kservice.c +++ b/src/kservice.c @@ -316,7 +316,7 @@ RTM_EXPORT(rt_memmove); * This function will compare two areas of memory * * @param cs one area of memory - * @param ct znother area of memory + * @param ct another area of memory * @param count the size of the area * * @return the result diff --git a/src/signal.c b/src/signal.c index bea3615d34..bb9339ab6c 100644 --- a/src/signal.c +++ b/src/signal.c @@ -22,8 +22,8 @@ #define RT_SIG_INFO_MAX 32 #endif -#define DBG_TAG "SIGN" -#define DBG_LVL DBG_WARNING +#define DBG_TAG "SIGN" +#define DBG_LVL DBG_WARNING #include #define sig_mask(sig_no) (1u << sig_no) diff --git a/src/thread.c b/src/thread.c index c5f75a1176..7596906fae 100644 --- a/src/thread.c +++ b/src/thread.c @@ -179,7 +179,7 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread, thread->cleanup = 0; thread->user_data = 0; - /* init thread timer */ + /* initialize thread timer */ rt_timer_init(&(thread->thread_timer), thread->name, rt_thread_timeout, @@ -242,7 +242,7 @@ rt_err_t rt_thread_init(struct rt_thread *thread, RT_ASSERT(thread != RT_NULL); RT_ASSERT(stack_start != RT_NULL); - /* init thread object */ + /* initialize thread object */ rt_object_init((rt_object_t)thread, RT_Object_Class_Thread, name); return _rt_thread_init(thread, @@ -293,7 +293,7 @@ rt_err_t rt_thread_startup(rt_thread_t thread) RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_INIT); RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread); - /* set current priority to init priority */ + /* set current priority to initialize priority */ thread->current_priority = thread->init_priority; /* calculate priority attribute */ -- cgit v1.3.1 From 64945ba882d651a14933eb4e7b3d93d10d6daae1 Mon Sep 17 00:00:00 2001 From: Bernard Xiong Date: Fri, 20 Dec 2019 08:24:45 +0800 Subject: [Kernel] Update version number. --- include/rtdef.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'include/rtdef.h') diff --git a/include/rtdef.h b/include/rtdef.h index b3aa38d3af..c8184a5a4b 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -31,6 +31,7 @@ * add smp relevant macros * 2019-01-27 Bernard change version number to v4.0.1 * 2019-05-17 Bernard change version number to v4.0.2 + * 2019-12-20 Bernard change version number to v4.0.3 */ #ifndef __RT_DEF_H__ @@ -52,7 +53,7 @@ extern "C" { /* RT-Thread version information */ #define RT_VERSION 4L /**< major version number */ #define RT_SUBVERSION 0L /**< minor version number */ -#define RT_REVISION 2L /**< revise version number */ +#define RT_REVISION 3L /**< revise version number */ /* RT-Thread version */ #define RTTHREAD_VERSION ((RT_VERSION * 10000) + \ -- cgit v1.3.1 From 720d8479a574746020747ef36bc33a8159206289 Mon Sep 17 00:00:00 2001 From: longtengmcu Date: Fri, 7 Feb 2020 16:43:39 +0800 Subject: 给软件定时器增加定时器工作状态查询的功能 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/rtdef.h | 1 + src/timer.c | 13 +++++++++++++ 2 files changed, 14 insertions(+) (limited to 'include/rtdef.h') diff --git a/include/rtdef.h b/include/rtdef.h index c8184a5a4b..8b86213082 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -431,6 +431,7 @@ struct rt_object_information #define RT_TIMER_CTRL_GET_TIME 0x1 /**< get timer control command */ #define RT_TIMER_CTRL_SET_ONESHOT 0x2 /**< change timer to one shot */ #define RT_TIMER_CTRL_SET_PERIODIC 0x3 /**< change timer to periodic */ +#define RT_TIMER_CTRL_GET_STATE 0x4 /**< get timer run state active or deactive*/ #ifndef RT_TIMER_SKIP_LIST_LEVEL #define RT_TIMER_SKIP_LIST_LEVEL 1 diff --git a/src/timer.c b/src/timer.c index d85ee080b5..0034f0246c 100644 --- a/src/timer.c +++ b/src/timer.c @@ -488,6 +488,19 @@ rt_err_t rt_timer_control(rt_timer_t timer, int cmd, void *arg) case RT_TIMER_CTRL_SET_PERIODIC: timer->parent.flag |= RT_TIMER_FLAG_PERIODIC; break; + /* zhaoshimin 20191204 add query the timer state */ + case RT_TIMER_CTRL_GET_STATE: + if(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED) + { + /*timer is start and run*/ + *(rt_tick_t *)arg = RT_TIMER_FLAG_ACTIVATED; + } + else + { + /*timer is stop*/ + *(rt_tick_t *)arg = RT_TIMER_FLAG_DEACTIVATED; + } + break; } return RT_EOK; -- cgit v1.3.1 From e5c1183350966cd242093c626f2f2c3944122498 Mon Sep 17 00:00:00 2001 From: SummerGift Date: Mon, 23 Mar 2020 15:35:27 +0800 Subject: [optimize] stm32 uart driver --- bsp/stm32/libraries/HAL_Drivers/drv_usart.c | 288 ++++++++++++++++------------ bsp/stm32/libraries/HAL_Drivers/drv_usart.h | 4 +- components/drivers/serial/serial.c | 14 +- include/rtdef.h | 1 + 4 files changed, 182 insertions(+), 125 deletions(-) (limited to 'include/rtdef.h') diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_usart.c b/bsp/stm32/libraries/HAL_Drivers/drv_usart.c index 64ca8fe4b3..faf010c138 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_usart.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_usart.c @@ -6,6 +6,8 @@ * Change Logs: * Date Author Notes * 2018-10-30 SummerGift first version + * 2020-03-16 SummerGift add device close feature + * 2020-03-20 SummerGift fix bug caused by ORE */ #include "board.h" @@ -21,12 +23,12 @@ #if !defined(BSP_USING_UART1) && !defined(BSP_USING_UART2) && !defined(BSP_USING_UART3) && \ !defined(BSP_USING_UART4) && !defined(BSP_USING_UART5) && !defined(BSP_USING_UART6) && \ !defined(BSP_USING_UART7) && !defined(BSP_USING_UART8) && !defined(BSP_USING_LPUART1) - #error "Please define at least one BSP_USING_UARTx" - /* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */ +#error "Please define at least one BSP_USING_UARTx" +/* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */ #endif #ifdef RT_SERIAL_USING_DMA - static void stm32_dma_config(struct rt_serial_device *serial, rt_ubase_t flag); +static void stm32_dma_config(struct rt_serial_device *serial, rt_ubase_t flag); #endif enum @@ -100,12 +102,12 @@ static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_c RT_ASSERT(cfg != RT_NULL); uart = rt_container_of(serial, struct stm32_uart, serial); - uart->handle.Instance = uart->config->Instance; uart->handle.Init.BaudRate = cfg->baud_rate; uart->handle.Init.HwFlowCtl = UART_HWCONTROL_NONE; uart->handle.Init.Mode = UART_MODE_TX_RX; uart->handle.Init.OverSampling = UART_OVERSAMPLING_16; + switch (cfg->data_bits) { case DATA_BITS_8: @@ -118,6 +120,7 @@ static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_c uart->handle.Init.WordLength = UART_WORDLENGTH_8B; break; } + switch (cfg->stop_bits) { case STOP_BITS_1: @@ -130,6 +133,7 @@ static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_c uart->handle.Init.StopBits = UART_STOPBITS_1; break; } + switch (cfg->parity) { case PARITY_NONE: @@ -146,6 +150,10 @@ static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_c break; } +#ifdef RT_SERIAL_USING_DMA + uart->dma_rx.last_index = 0; +#endif + if (HAL_UART_Init(&uart->handle) != HAL_OK) { return -RT_ERROR; @@ -172,7 +180,33 @@ static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *ar NVIC_DisableIRQ(uart->config->irq_type); /* disable interrupt */ __HAL_UART_DISABLE_IT(&(uart->handle), UART_IT_RXNE); + +#ifdef RT_SERIAL_USING_DMA + /* disable DMA */ + if (ctrl_arg == RT_DEVICE_FLAG_DMA_RX) + { + HAL_NVIC_DisableIRQ(uart->config->dma_rx->dma_irq); + if (HAL_DMA_Abort(&(uart->dma_rx.handle)) != HAL_OK) + { + RT_ASSERT(0); + } + + if (HAL_DMA_DeInit(&(uart->dma_rx.handle)) != HAL_OK) + { + RT_ASSERT(0); + } + } + else if(ctrl_arg == RT_DEVICE_FLAG_DMA_TX) + { + HAL_NVIC_DisableIRQ(uart->config->dma_tx->dma_irq); + if (HAL_DMA_DeInit(&(uart->dma_tx.handle)) != HAL_OK) + { + RT_ASSERT(0); + } + } +#endif break; + /* enable interrupt */ case RT_DEVICE_CTRL_SET_INT: /* enable rx irq */ @@ -186,6 +220,14 @@ static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *ar stm32_dma_config(serial, ctrl_arg); break; #endif + + case RT_DEVICE_CTRL_CLOSE: + if (HAL_UART_DeInit(&(uart->handle)) != HAL_OK ) + { + RT_ASSERT(0) + } + break; + } return RT_EOK; } @@ -233,13 +275,14 @@ static rt_size_t stm32_dma_transmit(struct rt_serial_device *serial, rt_uint8_t { struct stm32_uart *uart; RT_ASSERT(serial != RT_NULL); + RT_ASSERT(buf != RT_NULL); uart = rt_container_of(serial, struct stm32_uart, serial); - + if (size == 0) { return 0; } - + if (RT_SERIAL_DMA_TX == direction) { if (HAL_UART_Transmit_DMA(&uart->handle, buf, size) == HAL_OK) @@ -254,15 +297,6 @@ static rt_size_t stm32_dma_transmit(struct rt_serial_device *serial, rt_uint8_t return 0; } -static const struct rt_uart_ops stm32_uart_ops = -{ - .configure = stm32_configure, - .control = stm32_control, - .putc = stm32_putc, - .getc = stm32_getc, - .dma_transmit = stm32_dma_transmit -}; - /** * Uart common interrupt process. This need add to uart ISR. * @@ -301,16 +335,14 @@ static void uart_isr(struct rt_serial_device *serial) } __HAL_UART_CLEAR_IDLEFLAG(&uart->handle); } - else if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TC) != RESET) + else if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TC) && + (__HAL_UART_GET_IT_SOURCE(&(uart->handle), UART_IT_TC) != RESET)) { if ((serial->parent.open_flag & RT_DEVICE_FLAG_DMA_TX) != 0) { HAL_UART_IRQHandler(&(uart->handle)); } - else - { - UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TC); - } + UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TC); } #endif else @@ -714,6 +746,93 @@ void LPUART1_DMA_RX_IRQHandler(void) #endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_LPUART1_RX_USING_DMA) */ #endif /* BSP_USING_LPUART1*/ +static void stm32_uart_get_dma_config(void) +{ +#ifdef BSP_USING_UART1 + uart_obj[UART1_INDEX].uart_dma_flag = 0; +#ifdef BSP_UART1_RX_USING_DMA + uart_obj[UART1_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX; + static struct dma_config uart1_dma_rx = UART1_DMA_RX_CONFIG; + uart_config[UART1_INDEX].dma_rx = &uart1_dma_rx; +#endif +#ifdef BSP_UART1_TX_USING_DMA + uart_obj[UART1_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX; + static struct dma_config uart1_dma_tx = UART1_DMA_TX_CONFIG; + uart_config[UART1_INDEX].dma_tx = &uart1_dma_tx; +#endif +#endif + +#ifdef BSP_USING_UART2 + uart_obj[UART2_INDEX].uart_dma_flag = 0; +#ifdef BSP_UART2_RX_USING_DMA + uart_obj[UART2_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX; + static struct dma_config uart2_dma_rx = UART2_DMA_RX_CONFIG; + uart_config[UART2_INDEX].dma_rx = &uart2_dma_rx; +#endif +#ifdef BSP_UART2_TX_USING_DMA + uart_obj[UART2_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX; + static struct dma_config uart2_dma_tx = UART2_DMA_TX_CONFIG; + uart_config[UART2_INDEX].dma_tx = &uart2_dma_tx; +#endif +#endif + +#ifdef BSP_USING_UART3 + uart_obj[UART3_INDEX].uart_dma_flag = 0; +#ifdef BSP_UART3_RX_USING_DMA + uart_obj[UART3_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX; + static struct dma_config uart3_dma_rx = UART3_DMA_RX_CONFIG; + uart_config[UART3_INDEX].dma_rx = &uart3_dma_rx; +#endif +#ifdef BSP_UART3_TX_USING_DMA + uart_obj[UART3_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX; + static struct dma_config uart3_dma_tx = UART3_DMA_TX_CONFIG; + uart_config[UART3_INDEX].dma_tx = &uart3_dma_tx; +#endif +#endif + +#ifdef BSP_USING_UART4 + uart_obj[UART4_INDEX].uart_dma_flag = 0; +#ifdef BSP_UART4_RX_USING_DMA + uart_obj[UART4_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX; + static struct dma_config uart4_dma_rx = UART4_DMA_RX_CONFIG; + uart_config[UART4_INDEX].dma_rx = &uart4_dma_rx; +#endif +#ifdef BSP_UART4_TX_USING_DMA + uart_obj[UART4_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX; + static struct dma_config uart4_dma_tx = UART4_DMA_TX_CONFIG; + uart_config[UART4_INDEX].dma_tx = &uart4_dma_tx; +#endif +#endif + +#ifdef BSP_USING_UART5 + uart_obj[UART5_INDEX].uart_dma_flag = 0; +#ifdef BSP_UART5_RX_USING_DMA + uart_obj[UART5_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX; + static struct dma_config uart5_dma_rx = UART5_DMA_RX_CONFIG; + uart_config[UART5_INDEX].dma_rx = &uart5_dma_rx; +#endif +#ifdef BSP_UART5_TX_USING_DMA + uart_obj[UART5_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX; + static struct dma_config uart5_dma_tx = UART5_DMA_TX_CONFIG; + uart_config[UART5_INDEX].dma_tx = &uart5_dma_tx; +#endif +#endif + +#ifdef BSP_USING_UART6 + uart_obj[UART6_INDEX].uart_dma_flag = 0; +#ifdef BSP_UART6_RX_USING_DMA + uart_obj[UART6_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX; + static struct dma_config uart6_dma_rx = UART6_DMA_RX_CONFIG; + uart_config[UART6_INDEX].dma_rx = &uart6_dma_rx; +#endif +#ifdef BSP_UART6_TX_USING_DMA + uart_obj[UART6_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX; + static struct dma_config uart6_dma_tx = UART6_DMA_TX_CONFIG; + uart_config[UART6_INDEX].dma_tx = &uart6_dma_tx; +#endif +#endif +} + #ifdef RT_SERIAL_USING_DMA static void stm32_dma_config(struct rt_serial_device *serial, rt_ubase_t flag) { @@ -721,7 +840,7 @@ static void stm32_dma_config(struct rt_serial_device *serial, rt_ubase_t flag) DMA_HandleTypeDef *DMA_Handle; struct dma_config *dma_config; struct stm32_uart *uart; - + RT_ASSERT(serial != RT_NULL); uart = rt_container_of(serial, struct stm32_uart, serial); @@ -781,7 +900,7 @@ static void stm32_dma_config(struct rt_serial_device *serial, rt_ubase_t flag) DMA_Handle->Init.MemInc = DMA_MINC_ENABLE; DMA_Handle->Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; DMA_Handle->Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; - + if (RT_DEVICE_FLAG_DMA_RX == flag) { DMA_Handle->Init.Direction = DMA_PERIPH_TO_MEMORY; @@ -792,7 +911,7 @@ static void stm32_dma_config(struct rt_serial_device *serial, rt_ubase_t flag) DMA_Handle->Init.Direction = DMA_MEMORY_TO_PERIPH; DMA_Handle->Init.Mode = DMA_NORMAL; } - + DMA_Handle->Init.Priority = DMA_PRIORITY_MEDIUM; #if defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) DMA_Handle->Init.FIFOMode = DMA_FIFOMODE_DISABLE; @@ -820,8 +939,8 @@ static void stm32_dma_config(struct rt_serial_device *serial, rt_ubase_t flag) CLEAR_BIT(uart->handle.Instance->CR3, USART_CR3_EIE); __HAL_UART_ENABLE_IT(&(uart->handle), UART_IT_IDLE); } - - /* enable irq */ + + /* DMA irq should set in DMA TX mode, or HAL_UART_TxCpltCallback function will not be called */ HAL_NVIC_SetPriority(dma_config->dma_irq, 0, 0); HAL_NVIC_EnableIRQ(dma_config->dma_irq); @@ -865,7 +984,7 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) /** * @brief Rx Half transfer completed callback * @param huart: UART handle - * @note This example shows a simple way to report end of DMA Rx Half transfer, + * @note This example shows a simple way to report end of DMA Rx Half transfer, * and you can add your own implementation. * @retval None */ @@ -886,20 +1005,23 @@ static void _dma_tx_complete(struct rt_serial_device *serial) RT_ASSERT(serial != RT_NULL); uart = rt_container_of(serial, struct stm32_uart, serial); - if ((__HAL_DMA_GET_IT_SOURCE(&(uart->dma_tx.handle), DMA_IT_TC) != RESET) || - (__HAL_DMA_GET_IT_SOURCE(&(uart->dma_tx.handle), DMA_IT_HT) != RESET)) - { - level = rt_hw_interrupt_disable(); - trans_total_index = __HAL_DMA_GET_COUNTER(&(uart->dma_tx.handle)); - rt_hw_interrupt_enable(level); + level = rt_hw_interrupt_disable(); + trans_total_index = __HAL_DMA_GET_COUNTER(&(uart->dma_tx.handle)); + rt_hw_interrupt_enable(level); - if (trans_total_index == 0) - { - rt_hw_serial_isr(serial, RT_SERIAL_EVENT_TX_DMADONE); - } + if (trans_total_index == 0) + { + rt_hw_serial_isr(serial, RT_SERIAL_EVENT_TX_DMADONE); } } +/** + * @brief HAL_UART_TxCpltCallback + * @param huart: UART handle + * @note This callback can be called by two functions, first in UART_EndTransmit_IT when + * UART Tx complete and second in UART_DMATransmitCplt function in DMA Circular mode. + * @retval None + */ void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) { struct stm32_uart *uart; @@ -909,92 +1031,14 @@ void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) } #endif /* RT_SERIAL_USING_DMA */ -static void stm32_uart_get_dma_config(void) +static const struct rt_uart_ops stm32_uart_ops = { -#ifdef BSP_USING_UART1 - uart_obj[UART1_INDEX].uart_dma_flag = 0; -#ifdef BSP_UART1_RX_USING_DMA - uart_obj[UART1_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX; - static struct dma_config uart1_dma_rx = UART1_DMA_RX_CONFIG; - uart_config[UART1_INDEX].dma_rx = &uart1_dma_rx; -#endif -#ifdef BSP_UART1_TX_USING_DMA - uart_obj[UART1_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX; - static struct dma_config uart1_dma_tx = UART1_DMA_TX_CONFIG; - uart_config[UART1_INDEX].dma_tx = &uart1_dma_tx; -#endif -#endif - -#ifdef BSP_USING_UART2 - uart_obj[UART2_INDEX].uart_dma_flag = 0; -#ifdef BSP_UART2_RX_USING_DMA - uart_obj[UART2_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX; - static struct dma_config uart2_dma_rx = UART2_DMA_RX_CONFIG; - uart_config[UART2_INDEX].dma_rx = &uart2_dma_rx; -#endif -#ifdef BSP_UART2_TX_USING_DMA - uart_obj[UART2_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX; - static struct dma_config uart2_dma_tx = UART2_DMA_TX_CONFIG; - uart_config[UART2_INDEX].dma_tx = &uart2_dma_tx; -#endif -#endif - -#ifdef BSP_USING_UART3 - uart_obj[UART3_INDEX].uart_dma_flag = 0; -#ifdef BSP_UART3_RX_USING_DMA - uart_obj[UART3_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX; - static struct dma_config uart3_dma_rx = UART3_DMA_RX_CONFIG; - uart_config[UART3_INDEX].dma_rx = &uart3_dma_rx; -#endif -#ifdef BSP_UART3_TX_USING_DMA - uart_obj[UART3_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX; - static struct dma_config uart3_dma_tx = UART3_DMA_TX_CONFIG; - uart_config[UART3_INDEX].dma_tx = &uart3_dma_tx; -#endif -#endif - -#ifdef BSP_USING_UART4 - uart_obj[UART4_INDEX].uart_dma_flag = 0; -#ifdef BSP_UART4_RX_USING_DMA - uart_obj[UART4_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX; - static struct dma_config uart4_dma_rx = UART4_DMA_RX_CONFIG; - uart_config[UART4_INDEX].dma_rx = &uart4_dma_rx; -#endif -#ifdef BSP_UART4_TX_USING_DMA - uart_obj[UART4_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX; - static struct dma_config uart4_dma_tx = UART4_DMA_TX_CONFIG; - uart_config[UART4_INDEX].dma_tx = &uart4_dma_tx; -#endif -#endif - -#ifdef BSP_USING_UART5 - uart_obj[UART5_INDEX].uart_dma_flag = 0; -#ifdef BSP_UART5_RX_USING_DMA - uart_obj[UART5_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX; - static struct dma_config uart5_dma_rx = UART5_DMA_RX_CONFIG; - uart_config[UART5_INDEX].dma_rx = &uart5_dma_rx; -#endif -#ifdef BSP_UART5_TX_USING_DMA - uart_obj[UART5_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX; - static struct dma_config uart5_dma_tx = UART5_DMA_TX_CONFIG; - uart_config[UART5_INDEX].dma_tx = &uart5_dma_tx; -#endif -#endif - -#ifdef BSP_USING_UART6 - uart_obj[UART6_INDEX].uart_dma_flag = 0; -#ifdef BSP_UART6_RX_USING_DMA - uart_obj[UART6_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX; - static struct dma_config uart6_dma_rx = UART6_DMA_RX_CONFIG; - uart_config[UART6_INDEX].dma_rx = &uart6_dma_rx; -#endif -#ifdef BSP_UART6_TX_USING_DMA - uart_obj[UART6_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX; - static struct dma_config uart6_dma_tx = UART6_DMA_TX_CONFIG; - uart_config[UART6_INDEX].dma_tx = &uart6_dma_tx; -#endif -#endif -} + .configure = stm32_configure, + .control = stm32_control, + .putc = stm32_putc, + .getc = stm32_getc, + .dma_transmit = stm32_dma_transmit +}; int rt_hw_usart_init(void) { @@ -1006,9 +1050,11 @@ int rt_hw_usart_init(void) for (int i = 0; i < obj_num; i++) { + /* init UART object */ uart_obj[i].config = &uart_config[i]; uart_obj[i].serial.ops = &stm32_uart_ops; uart_obj[i].serial.config = config; + /* register UART device */ result = rt_hw_serial_register(&uart_obj[i].serial, uart_obj[i].config->name, RT_DEVICE_FLAG_RDWR diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_usart.h b/bsp/stm32/libraries/HAL_Drivers/drv_usart.h index f8b0703a33..b6e4060288 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_usart.h +++ b/bsp/stm32/libraries/HAL_Drivers/drv_usart.h @@ -6,7 +6,7 @@ * Change Logs: * Date Author Notes * 2018.10.30 SummerGift first version - * 2019.03.05 whj4674672 add stm32h7 + * 2019.03.05 whj4674672 add stm32h7 */ #ifndef __DRV_USART_H__ @@ -50,7 +50,7 @@ struct stm32_uart { UART_HandleTypeDef handle; struct stm32_uart_config *config; - + #ifdef RT_SERIAL_USING_DMA struct { diff --git a/components/drivers/serial/serial.c b/components/drivers/serial/serial.c index d1c04dec6f..7dd2918b9b 100644 --- a/components/drivers/serial/serial.c +++ b/components/drivers/serial/serial.c @@ -733,6 +733,7 @@ static rt_err_t rt_serial_close(struct rt_device *dev) rt_free(rx_fifo); serial->serial_rx = RT_NULL; dev->open_flag &= ~RT_DEVICE_FLAG_INT_RX; + /* configure low level device */ serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void*)RT_DEVICE_FLAG_INT_RX); } @@ -754,10 +755,11 @@ static rt_err_t rt_serial_close(struct rt_device *dev) rt_free(rx_fifo); } - /* configure low level device */ - serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void *) RT_DEVICE_FLAG_DMA_RX); serial->serial_rx = RT_NULL; dev->open_flag &= ~RT_DEVICE_FLAG_DMA_RX; + + /* configure low level device */ + serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void *) RT_DEVICE_FLAG_DMA_RX); } #endif /* RT_SERIAL_USING_DMA */ @@ -771,6 +773,7 @@ static rt_err_t rt_serial_close(struct rt_device *dev) rt_free(tx_fifo); serial->serial_tx = RT_NULL; dev->open_flag &= ~RT_DEVICE_FLAG_INT_TX; + /* configure low level device */ serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void*)RT_DEVICE_FLAG_INT_TX); } @@ -785,7 +788,14 @@ static rt_err_t rt_serial_close(struct rt_device *dev) rt_free(tx_dma); serial->serial_tx = RT_NULL; dev->open_flag &= ~RT_DEVICE_FLAG_DMA_TX; + + /* configure low level device */ + serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void *) RT_DEVICE_FLAG_DMA_TX); } + + serial->ops->control(serial, RT_DEVICE_CTRL_CLOSE, RT_NULL); + dev->flag &= ~RT_DEVICE_FLAG_ACTIVATED; + #endif /* RT_SERIAL_USING_DMA */ return RT_EOK; } diff --git a/include/rtdef.h b/include/rtdef.h index 8b86213082..f2b546f75e 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -898,6 +898,7 @@ enum rt_device_class_type #define RT_DEVICE_CTRL_RESUME 0x01 /**< resume device */ #define RT_DEVICE_CTRL_SUSPEND 0x02 /**< suspend device */ #define RT_DEVICE_CTRL_CONFIG 0x03 /**< configure device */ +#define RT_DEVICE_CTRL_CLOSE 0x04 /**< close device */ #define RT_DEVICE_CTRL_SET_INT 0x10 /**< set interrupt */ #define RT_DEVICE_CTRL_CLR_INT 0x11 /**< clear interrupt */ -- cgit v1.3.1