From 0c87becb3eed0e890198740589f0db94ab6256cf Mon Sep 17 00:00:00 2001 From: tangyuxin Date: Sat, 30 Jan 2021 18:21:33 +0800 Subject: [PATCH 01/27] =?UTF-8?q?=E5=90=88=E5=B9=B6=20submit=20=E5=92=8C?= =?UTF-8?q?=20submit=5Fdelayed=EF=BC=8C=E4=BC=98=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=BD=93=E7=A7=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将 _workqueue_submit_delayed_work 和 _workqueue_submit_work 函数合并成一个。优化代码体积 --- components/drivers/src/workqueue.c | 134 +++++++++++------------------ 1 file changed, 52 insertions(+), 82 deletions(-) diff --git a/components/drivers/src/workqueue.c b/components/drivers/src/workqueue.c index 2b5630d8d0..557f349e53 100644 --- a/components/drivers/src/workqueue.c +++ b/components/drivers/src/workqueue.c @@ -86,43 +86,61 @@ static void _workqueue_thread_entry(void *parameter) } } -static rt_err_t _workqueue_submit_work(struct rt_workqueue *queue, struct rt_work *work) +static rt_err_t _workqueue_submit_work(struct rt_workqueue *queue, + struct rt_work *work, rt_tick_t ticks) { rt_base_t level; level = rt_hw_interrupt_disable(); - if (work->flags & RT_WORK_STATE_PENDING) - { - rt_hw_interrupt_enable(level); - return -RT_EBUSY; - } - - if (queue->work_current == work) - { - rt_hw_interrupt_enable(level); - return -RT_EBUSY; - } - - /* NOTE: the work MUST be initialized firstly */ + /* remove list */ rt_list_remove(&(work->list)); - - rt_list_insert_after(queue->work_list.prev, &(work->list)); - work->flags |= RT_WORK_STATE_PENDING; - - /* whether the workqueue is doing work */ - if (queue->work_current == RT_NULL) + work->flags &= ~RT_WORK_STATE_PENDING; + /* */ + if (ticks == 0) { - rt_hw_interrupt_enable(level); - /* resume work thread */ - rt_thread_resume(queue->work_thread); - rt_schedule(); - } - else - { - rt_hw_interrupt_enable(level); - } + if (queue->work_current != work) + { + rt_list_insert_after(queue->work_list.prev, &(work->list)); + work->flags |= RT_WORK_STATE_PENDING; + work->workqueue = queue; + } - return RT_EOK; + /* whether the workqueue is doing work */ + if (queue->work_current == RT_NULL && + ((queue->work_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND)) + { + rt_hw_interrupt_enable(level); + /* resume work thread */ + rt_thread_resume(queue->work_thread); + rt_schedule(); + } + else + { + rt_hw_interrupt_enable(level); + } + return RT_EOK; + } + else if (ticks < RT_TICK_MAX / 2) + { + /* Timer started */ + if (work->flags & RT_WORK_STATE_SUBMITTING) + { + rt_timer_stop(&work->timer); + rt_timer_control(&work->timer, RT_TIMER_CTRL_SET_TIME, &ticks); + } + else + { + rt_timer_init(&(work->timer), "work", _delayed_work_timeout_handler, + work, ticks, RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER); + work->flags |= RT_WORK_STATE_SUBMITTING; + } + work->workqueue = queue; + rt_hw_interrupt_enable(level); + rt_timer_start(&(work->timer)); + return RT_EOK; + } + rt_hw_interrupt_enable(level); + return -RT_ERROR; } static rt_err_t _workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *work) @@ -184,54 +202,6 @@ __exit: return ret; } -static rt_err_t _workqueue_submit_delayed_work(struct rt_workqueue *queue, - struct rt_work *work, rt_tick_t ticks) -{ - rt_base_t level; - rt_err_t ret = RT_EOK; - - /* Work cannot be active in multiple queues */ - if (work->workqueue && work->workqueue != queue) - { - ret = -RT_EINVAL; - goto __exit; - } - - /* Cancel if work has been submitted */ - if (work->workqueue == queue) - { - ret = _workqueue_cancel_delayed_work(work); - if (ret < 0) - { - goto __exit; - } - } - - level = rt_hw_interrupt_disable(); - /* Attach workqueue so the timeout callback can submit it */ - work->workqueue = queue; - rt_hw_interrupt_enable(level); - - if (!ticks) - { - /* Submit work if no ticks is 0 */ - ret = _workqueue_submit_work(work->workqueue, work); - } - else - { - level = rt_hw_interrupt_disable(); - /* Add timeout */ - work->flags |= RT_WORK_STATE_SUBMITTING; - rt_timer_init(&(work->timer), "work", _delayed_work_timeout_handler, work, ticks, - RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER); - rt_hw_interrupt_enable(level); - rt_timer_start(&(work->timer)); - } - -__exit: - return ret; -} - static void _delayed_work_timeout_handler(void *parameter) { struct rt_work *delayed_work; @@ -244,7 +214,7 @@ static void _delayed_work_timeout_handler(void *parameter) delayed_work->flags &= ~RT_WORK_STATE_SUBMITTING; delayed_work->type &= ~RT_WORK_TYPE_DELAYED; rt_hw_interrupt_enable(level); - _workqueue_submit_work(delayed_work->workqueue, delayed_work); + _workqueue_submit_work(delayed_work->workqueue, delayed_work, 0); } struct rt_workqueue *rt_workqueue_create(const char *name, rt_uint16_t stack_size, rt_uint8_t priority) @@ -288,7 +258,7 @@ rt_err_t rt_workqueue_dowork(struct rt_workqueue *queue, struct rt_work *work) RT_ASSERT(queue != RT_NULL); RT_ASSERT(work != RT_NULL); - return _workqueue_submit_work(queue, work); + return _workqueue_submit_work(queue, work, 0); } rt_err_t rt_workqueue_submit_work(struct rt_workqueue *queue, struct rt_work *work, rt_tick_t time) @@ -303,11 +273,11 @@ rt_err_t rt_workqueue_submit_work(struct rt_workqueue *queue, struct rt_work *wo if (work->type & RT_WORK_TYPE_DELAYED) { - return _workqueue_submit_delayed_work(queue, work, time); + return _workqueue_submit_work(queue, work, time); } else { - return _workqueue_submit_work(queue, work); + return _workqueue_submit_work(queue, work, 0); } } From 10b5aa8f59fa83f4f053fd2318cf5268544b3304 Mon Sep 17 00:00:00 2001 From: tangyuxin Date: Sat, 30 Jan 2021 18:29:17 +0800 Subject: [PATCH 02/27] =?UTF-8?q?=E5=90=88=E5=B9=B6=20cancel=20=E5=92=8C?= =?UTF-8?q?=20cancel=5Fdelayed=20=E5=87=BD=E6=95=B0=EF=BC=8C=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E4=BB=A3=E7=A0=81=E4=BD=93=E7=A7=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 合并 _workqueue_cancel_work 和 _workqueue_cancel_delayed_work 函数,优化代码体积 --- components/drivers/src/workqueue.c | 58 +++++------------------------- 1 file changed, 9 insertions(+), 49 deletions(-) diff --git a/components/drivers/src/workqueue.c b/components/drivers/src/workqueue.c index 557f349e53..0c3c0e9f53 100644 --- a/components/drivers/src/workqueue.c +++ b/components/drivers/src/workqueue.c @@ -143,63 +143,23 @@ static rt_err_t _workqueue_submit_work(struct rt_workqueue *queue, return -RT_ERROR; } -static rt_err_t _workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *work) +static rt_err_t _workqueue_cancel_work(struct rt_work *work) { rt_base_t level; level = rt_hw_interrupt_disable(); - if (queue->work_current == work) - { - rt_hw_interrupt_enable(level); - return -RT_EBUSY; - } rt_list_remove(&(work->list)); work->flags &= ~RT_WORK_STATE_PENDING; - rt_hw_interrupt_enable(level); - - return RT_EOK; -} - -static rt_err_t _workqueue_cancel_delayed_work(struct rt_work *work) -{ - rt_base_t level; - int ret = RT_EOK; - - if (!work->workqueue) + /* Timer started */ + if (work->flags & RT_WORK_STATE_SUBMITTING) { - ret = -EINVAL; - goto __exit; + rt_timer_stop(&(work->timer)); + rt_timer_detach(&(work->timer)); + work->flags &= ~RT_WORK_STATE_SUBMITTING; } - - if (work->flags & RT_WORK_STATE_PENDING) - { - /* Remove from the queue if already submitted */ - ret = _workqueue_cancel_work(work->workqueue, work); - if (ret) - { - goto __exit; - } - } - else - { - if (work->flags & RT_WORK_STATE_SUBMITTING) - { - level = rt_hw_interrupt_disable(); - rt_timer_stop(&(work->timer)); - rt_timer_detach(&(work->timer)); - work->flags &= ~RT_WORK_STATE_SUBMITTING; - rt_hw_interrupt_enable(level); - } - } - - level = rt_hw_interrupt_disable(); - /* Detach from workqueue */ work->workqueue = RT_NULL; - work->flags &= ~(RT_WORK_STATE_PENDING); rt_hw_interrupt_enable(level); - -__exit: - return ret; + return 0; } static void _delayed_work_timeout_handler(void *parameter) @@ -317,11 +277,11 @@ rt_err_t rt_workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *wo if (work->type & RT_WORK_TYPE_DELAYED) { - return _workqueue_cancel_delayed_work(work); + return _workqueue_cancel_work(work); } else { - return _workqueue_cancel_work(queue, work); + return _workqueue_cancel_work(work); } } From 5a16a2060e5c330066d6d7c499c04d27277b9ae4 Mon Sep 17 00:00:00 2001 From: tangyuxin Date: Mon, 1 Feb 2021 14:26:47 +0800 Subject: [PATCH 03/27] =?UTF-8?q?=E5=AE=9A=E6=97=B6=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E4=B8=B4=E7=95=8C=E5=8C=BA=E4=BF=9D=E6=8A=A4?= =?UTF-8?q?=E6=9B=B4=E5=8A=A0=E5=AE=8C=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 定时器回调函数中,添加工作之前打开了中断。此时在极端情况下,会导致状态不对。 --- components/drivers/src/workqueue.c | 35 +++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/components/drivers/src/workqueue.c b/components/drivers/src/workqueue.c index 0c3c0e9f53..6e8683c9b9 100644 --- a/components/drivers/src/workqueue.c +++ b/components/drivers/src/workqueue.c @@ -164,17 +164,36 @@ static rt_err_t _workqueue_cancel_work(struct rt_work *work) static void _delayed_work_timeout_handler(void *parameter) { - struct rt_work *delayed_work; + struct rt_work *work; + struct rt_workqueue *queue; rt_base_t level; - delayed_work = (struct rt_work *)parameter; + work = (struct rt_work *)parameter; + queue = work->workqueue; + RT_ASSERT(queue != RT_NULL); + level = rt_hw_interrupt_disable(); - rt_timer_stop(&(delayed_work->timer)); - rt_timer_detach(&(delayed_work->timer)); - delayed_work->flags &= ~RT_WORK_STATE_SUBMITTING; - delayed_work->type &= ~RT_WORK_TYPE_DELAYED; - rt_hw_interrupt_enable(level); - _workqueue_submit_work(delayed_work->workqueue, delayed_work, 0); + rt_timer_detach(&(work->timer)); + work->flags &= ~RT_WORK_STATE_SUBMITTING; + /* insert work queue */ + if (queue->work_current != work) + { + rt_list_insert_after(queue->work_list.prev, &(work->list)); + work->flags |= RT_WORK_STATE_PENDING; + } + /* whether the workqueue is doing work */ + if (queue->work_current == RT_NULL && + ((queue->work_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND)) + { + rt_hw_interrupt_enable(level); + /* resume work thread */ + rt_thread_resume(queue->work_thread); + rt_schedule(); + } + else + { + rt_hw_interrupt_enable(level); + } } struct rt_workqueue *rt_workqueue_create(const char *name, rt_uint16_t stack_size, rt_uint8_t priority) From 5b52e6c7e315d25c5a492d5460442e6e3f5c5631 Mon Sep 17 00:00:00 2001 From: tangyuxin Date: Mon, 1 Feb 2021 14:31:45 +0800 Subject: [PATCH 04/27] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E5=8F=96=E6=B6=88=E9=94=81=E4=B8=AD=E6=96=AD=E7=AD=89=E4=BF=A1?= =?UTF-8?q?=E5=8F=B7=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/src/workqueue.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/components/drivers/src/workqueue.c b/components/drivers/src/workqueue.c index 6e8683c9b9..5c497cd5c8 100644 --- a/components/drivers/src/workqueue.c +++ b/components/drivers/src/workqueue.c @@ -306,12 +306,9 @@ rt_err_t rt_workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *wo rt_err_t rt_workqueue_cancel_work_sync(struct rt_workqueue *queue, struct rt_work *work) { - rt_base_t level; - RT_ASSERT(queue != RT_NULL); RT_ASSERT(work != RT_NULL); - level = rt_hw_interrupt_disable(); if (queue->work_current == work) /* it's current work in the queue */ { /* wait for work completion */ @@ -319,10 +316,8 @@ rt_err_t rt_workqueue_cancel_work_sync(struct rt_workqueue *queue, struct rt_wor } else { - rt_list_remove(&(work->list)); + _workqueue_cancel_work(work); } - work->flags &= ~RT_WORK_STATE_PENDING; - rt_hw_interrupt_enable(level); return RT_EOK; } From c5218b5eeed799593485c89234de809858cb6bf2 Mon Sep 17 00:00:00 2001 From: tangyuxin Date: Mon, 1 Feb 2021 14:36:18 +0800 Subject: [PATCH 05/27] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=B4=A7=E6=80=A5?= =?UTF-8?q?=E5=B7=A5=E4=BD=9C=E9=98=9F=E5=88=97=E6=8F=92=E5=85=A5=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 紧急工作仍插入队列尾部,先将其插入队列头部 --- components/drivers/src/workqueue.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/components/drivers/src/workqueue.c b/components/drivers/src/workqueue.c index 5c497cd5c8..d21bf08f4d 100644 --- a/components/drivers/src/workqueue.c +++ b/components/drivers/src/workqueue.c @@ -267,24 +267,22 @@ rt_err_t rt_workqueue_critical_work(struct rt_workqueue *queue, struct rt_work * RT_ASSERT(work != RT_NULL); level = rt_hw_interrupt_disable(); - if (queue->work_current == work) - { - rt_hw_interrupt_enable(level); - return -RT_EBUSY; - } - /* NOTE: the work MUST be initialized firstly */ rt_list_remove(&(work->list)); - - rt_list_insert_after(queue->work_list.prev, &(work->list)); - if (queue->work_current == RT_NULL) + rt_list_insert_after(&queue->work_list, &(work->list)); + /* whether the workqueue is doing work */ + if (queue->work_current == RT_NULL && + ((queue->work_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND)) { rt_hw_interrupt_enable(level); /* resume work thread */ rt_thread_resume(queue->work_thread); rt_schedule(); } - else rt_hw_interrupt_enable(level); + else + { + rt_hw_interrupt_enable(level); + } return RT_EOK; } From b42506fb731c6de683fe27c74cf68064a6919a25 Mon Sep 17 00:00:00 2001 From: tangyuxin Date: Mon, 1 Feb 2021 14:43:12 +0800 Subject: [PATCH 06/27] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/src/workqueue.c | 25 ++----------------------- 1 file changed, 2 insertions(+), 23 deletions(-) diff --git a/components/drivers/src/workqueue.c b/components/drivers/src/workqueue.c index d21bf08f4d..4dc08bb2ea 100644 --- a/components/drivers/src/workqueue.c +++ b/components/drivers/src/workqueue.c @@ -245,19 +245,7 @@ rt_err_t rt_workqueue_submit_work(struct rt_workqueue *queue, struct rt_work *wo RT_ASSERT(queue != RT_NULL); RT_ASSERT(work != RT_NULL); - if (time > 0) - { - work->type |= RT_WORK_TYPE_DELAYED; - } - - if (work->type & RT_WORK_TYPE_DELAYED) - { - return _workqueue_submit_work(queue, work, time); - } - else - { - return _workqueue_submit_work(queue, work, 0); - } + return _workqueue_submit_work(queue, work, time); } rt_err_t rt_workqueue_critical_work(struct rt_workqueue *queue, struct rt_work *work) @@ -289,17 +277,8 @@ rt_err_t rt_workqueue_critical_work(struct rt_workqueue *queue, struct rt_work * rt_err_t rt_workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *work) { - RT_ASSERT(queue != RT_NULL); RT_ASSERT(work != RT_NULL); - - if (work->type & RT_WORK_TYPE_DELAYED) - { - return _workqueue_cancel_work(work); - } - else - { - return _workqueue_cancel_work(work); - } + return _workqueue_cancel_work(work); } rt_err_t rt_workqueue_cancel_work_sync(struct rt_workqueue *queue, struct rt_work *work) From 0230d32ca0a794c4564360fb37006837d1e8f42f Mon Sep 17 00:00:00 2001 From: tangyuxin Date: Mon, 1 Feb 2021 14:44:49 +0800 Subject: [PATCH 07/27] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=AD=E8=A8=80?= =?UTF-8?q?=E5=8F=8A=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/src/workqueue.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/components/drivers/src/workqueue.c b/components/drivers/src/workqueue.c index 4dc08bb2ea..1b7dbea0c1 100644 --- a/components/drivers/src/workqueue.c +++ b/components/drivers/src/workqueue.c @@ -304,6 +304,7 @@ rt_err_t rt_workqueue_cancel_all_work(struct rt_workqueue *queue) struct rt_list_node *node, *next; RT_ASSERT(queue != RT_NULL); + // TODO: cancel delay work rt_enter_critical(); for (node = queue->work_list.next; node != &(queue->work_list); node = next) { @@ -337,14 +338,15 @@ rt_err_t rt_work_cancel(struct rt_work *work) int rt_work_sys_workqueue_init(void) { if (sys_workq != RT_NULL) - return 0; + return RT_EOK; sys_workq = rt_workqueue_create("sys_work", RT_SYSTEM_WORKQUEUE_STACKSIZE, RT_SYSTEM_WORKQUEUE_PRIORITY); + RT_ASSERT(sys_workq != RT_NULL); return RT_EOK; } -INIT_PREV_EXPORT(rt_work_sys_workqueue_init); +INIT_DEVICE_EXPORT(rt_work_sys_workqueue_init); #endif #endif From 360d7e48ab2d3c205b74523a075d92cbe3e72a7b Mon Sep 17 00:00:00 2001 From: tangyuxin Date: Mon, 1 Feb 2021 21:01:14 +0800 Subject: [PATCH 08/27] =?UTF-8?q?[libcpu][cm33]=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E4=B8=8D=E5=90=8C=E4=BC=98=E5=8C=96=E7=AD=89=E7=BA=A7=EF=BC=8C?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E8=A1=8C=E4=B8=BA=E4=B8=8D=E4=B8=80=E8=87=B4?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libcpu/arm/cortex-m33/cpuport.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/libcpu/arm/cortex-m33/cpuport.c b/libcpu/arm/cortex-m33/cpuport.c index 42b5798507..41325105d7 100644 --- a/libcpu/arm/cortex-m33/cpuport.c +++ b/libcpu/arm/cortex-m33/cpuport.c @@ -473,17 +473,13 @@ exit #elif defined(__CLANG_ARM) int __rt_ffs(int value) { - __asm volatile( - "CMP r0, #0x00 \n" - "BEQ 1f \n" + if (value == 0) return value; + __asm volatile( "RBIT r0, r0 \n" "CLZ r0, r0 \n" "ADDS r0, r0, #0x01 \n" - "1: \n" - "BX lr \n" - : "=r"(value) : "r"(value) ); From a50d81827a1a4d7172e761dd64cecc5553f349d9 Mon Sep 17 00:00:00 2001 From: Wayne Date: Thu, 4 Feb 2021 22:53:58 +0800 Subject: [PATCH 09/27] Revert "fix the bug that rx_notice out of sync when data received after last rt_device_read() is zero" --- components/net/at/src/at_client.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/components/net/at/src/at_client.c b/components/net/at/src/at_client.c index b9d0ee89cb..788549392d 100644 --- a/components/net/at/src/at_client.c +++ b/components/net/at/src/at_client.c @@ -434,20 +434,18 @@ static rt_err_t at_client_getchar(at_client_t client, char *ch, rt_int32_t timeo { rt_err_t result = RT_EOK; -__retry: - result = rt_sem_take(client->rx_notice, rt_tick_from_millisecond(timeout)); - if (result != RT_EOK) + while (rt_device_read(client->device, 0, ch, 1) == 0) { - return result; - } - if(rt_device_read(client->device, 0, ch, 1) == 1) - { - return RT_EOK; - } - else - { - goto __retry; + rt_sem_control(client->rx_notice, RT_IPC_CMD_RESET, RT_NULL); + + result = rt_sem_take(client->rx_notice, rt_tick_from_millisecond(timeout)); + if (result != RT_EOK) + { + return result; + } } + + return RT_EOK; } /** From d6b44ca52ed2ddc27e447ae2fe2914b9eb38687f Mon Sep 17 00:00:00 2001 From: tangyuxin <462747508@qq.com> Date: Sat, 6 Feb 2021 20:08:31 +0800 Subject: [PATCH 10/27] =?UTF-8?q?[workqueue]=20=E6=94=AF=E6=8C=81=E5=8F=96?= =?UTF-8?q?=E6=B6=88=E5=BB=B6=E6=97=B6=E5=B7=A5=E4=BD=9C=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/include/ipc/workqueue.h | 1 + components/drivers/src/workqueue.c | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/components/drivers/include/ipc/workqueue.h b/components/drivers/include/ipc/workqueue.h index 23e256c420..51976b4de7 100644 --- a/components/drivers/include/ipc/workqueue.h +++ b/components/drivers/include/ipc/workqueue.h @@ -29,6 +29,7 @@ enum struct rt_workqueue { rt_list_t work_list; + rt_list_t delayed_list; struct rt_work *work_current; /* current work */ struct rt_semaphore sem; diff --git a/components/drivers/src/workqueue.c b/components/drivers/src/workqueue.c index 1b7dbea0c1..eb6cefc006 100644 --- a/components/drivers/src/workqueue.c +++ b/components/drivers/src/workqueue.c @@ -135,6 +135,8 @@ static rt_err_t _workqueue_submit_work(struct rt_workqueue *queue, work->flags |= RT_WORK_STATE_SUBMITTING; } work->workqueue = queue; + /* insert delay work list */ + rt_list_insert_after(queue->delayed_list.prev, &(work->list)); rt_hw_interrupt_enable(level); rt_timer_start(&(work->timer)); return RT_EOK; @@ -175,6 +177,8 @@ static void _delayed_work_timeout_handler(void *parameter) level = rt_hw_interrupt_disable(); rt_timer_detach(&(work->timer)); work->flags &= ~RT_WORK_STATE_SUBMITTING; + /* remove delay list */ + rt_list_remove(&(work->list)); /* insert work queue */ if (queue->work_current != work) { @@ -205,6 +209,7 @@ struct rt_workqueue *rt_workqueue_create(const char *name, rt_uint16_t stack_siz { /* initialize work list */ rt_list_init(&(queue->work_list)); + rt_list_init(&(queue->delayed_list)); queue->work_current = RT_NULL; rt_sem_init(&(queue->sem), "wqueue", 0, RT_IPC_FLAG_FIFO); @@ -302,14 +307,22 @@ rt_err_t rt_workqueue_cancel_work_sync(struct rt_workqueue *queue, struct rt_wor rt_err_t rt_workqueue_cancel_all_work(struct rt_workqueue *queue) { struct rt_list_node *node, *next; + struct rt_work *work; + RT_ASSERT(queue != RT_NULL); - // TODO: cancel delay work + // cancel work rt_enter_critical(); - for (node = queue->work_list.next; node != &(queue->work_list); node = next) + while (rt_list_isempty(&queue->work_list) != RT_FALSE) { - next = node->next; - rt_list_remove(node); + work = rt_list_first_entry(&queue->work_list, struct rt_work, list); + _workqueue_cancel_work(work); + } + // cancel delay work + while (rt_list_isempty(&queue->delayed_list) != RT_FALSE) + { + work = rt_list_first_entry(&queue->delayed_list, struct rt_work, list); + _workqueue_cancel_work(work); } rt_exit_critical(); From 3ef3fb50afdb388918fe7514d287aca753030f27 Mon Sep 17 00:00:00 2001 From: tangyuxin <462747508@qq.com> Date: Sat, 6 Feb 2021 20:15:50 +0800 Subject: [PATCH 11/27] =?UTF-8?q?[workqueue]=20=E9=98=9F=E5=88=97=E9=94=80?= =?UTF-8?q?=E6=AF=81=E6=97=B6=E5=8F=96=E6=B6=88=E6=89=80=E6=9C=89=E5=B7=A5?= =?UTF-8?q?=E4=BD=9C=E5=B9=B6=E9=87=8A=E6=94=BE=E5=86=85=E6=A0=B8=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/src/workqueue.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/components/drivers/src/workqueue.c b/components/drivers/src/workqueue.c index eb6cefc006..474eae556b 100644 --- a/components/drivers/src/workqueue.c +++ b/components/drivers/src/workqueue.c @@ -231,7 +231,9 @@ rt_err_t rt_workqueue_destroy(struct rt_workqueue *queue) { RT_ASSERT(queue != RT_NULL); + rt_workqueue_cancel_all_work(); rt_thread_delete(queue->work_thread); + rt_sem_detach(&(queue->sem)); RT_KERNEL_FREE(queue); return RT_EOK; From 7eabe5c9e9a2a93aa7303bee15c156e623986194 Mon Sep 17 00:00:00 2001 From: tangyuxin <462747508@qq.com> Date: Sat, 6 Feb 2021 20:39:52 +0800 Subject: [PATCH 12/27] =?UTF-8?q?[workqueue]=20=E5=8F=96=E6=B6=88=E6=AD=A3?= =?UTF-8?q?=E5=9C=A8=E6=89=A7=E8=A1=8C=E7=9A=84=E5=B7=A5=E4=BD=9C=E8=BF=94?= =?UTF-8?q?=E5=9B=9EBUSY?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/src/workqueue.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/components/drivers/src/workqueue.c b/components/drivers/src/workqueue.c index 474eae556b..3feeaea0be 100644 --- a/components/drivers/src/workqueue.c +++ b/components/drivers/src/workqueue.c @@ -145,9 +145,10 @@ static rt_err_t _workqueue_submit_work(struct rt_workqueue *queue, return -RT_ERROR; } -static rt_err_t _workqueue_cancel_work(struct rt_work *work) +static rt_err_t _workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *work) { rt_base_t level; + rt_err_t err; level = rt_hw_interrupt_disable(); rt_list_remove(&(work->list)); @@ -159,9 +160,10 @@ static rt_err_t _workqueue_cancel_work(struct rt_work *work) rt_timer_detach(&(work->timer)); work->flags &= ~RT_WORK_STATE_SUBMITTING; } + err = queue->work_current != work ? RT_EOK : -RT_EBUSY; work->workqueue = RT_NULL; rt_hw_interrupt_enable(level); - return 0; + return err; } static void _delayed_work_timeout_handler(void *parameter) @@ -231,7 +233,7 @@ rt_err_t rt_workqueue_destroy(struct rt_workqueue *queue) { RT_ASSERT(queue != RT_NULL); - rt_workqueue_cancel_all_work(); + rt_workqueue_cancel_all_work(queue); rt_thread_delete(queue->work_thread); rt_sem_detach(&(queue->sem)); RT_KERNEL_FREE(queue); @@ -285,7 +287,8 @@ rt_err_t rt_workqueue_critical_work(struct rt_workqueue *queue, struct rt_work * rt_err_t rt_workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *work) { RT_ASSERT(work != RT_NULL); - return _workqueue_cancel_work(work); + RT_ASSERT(queue != RT_NULL); + return _workqueue_cancel_work(queue, work); } rt_err_t rt_workqueue_cancel_work_sync(struct rt_workqueue *queue, struct rt_work *work) @@ -300,7 +303,7 @@ rt_err_t rt_workqueue_cancel_work_sync(struct rt_workqueue *queue, struct rt_wor } else { - _workqueue_cancel_work(work); + _workqueue_cancel_work(queue, work); } return RT_EOK; @@ -308,23 +311,22 @@ rt_err_t rt_workqueue_cancel_work_sync(struct rt_workqueue *queue, struct rt_wor rt_err_t rt_workqueue_cancel_all_work(struct rt_workqueue *queue) { - struct rt_list_node *node, *next; struct rt_work *work; RT_ASSERT(queue != RT_NULL); // cancel work rt_enter_critical(); - while (rt_list_isempty(&queue->work_list) != RT_FALSE) + while (rt_list_isempty(&queue->work_list) == RT_FALSE) { work = rt_list_first_entry(&queue->work_list, struct rt_work, list); - _workqueue_cancel_work(work); + _workqueue_cancel_work(queue, work); } // cancel delay work - while (rt_list_isempty(&queue->delayed_list) != RT_FALSE) + while (rt_list_isempty(&queue->delayed_list) == RT_FALSE) { work = rt_list_first_entry(&queue->delayed_list, struct rt_work, list); - _workqueue_cancel_work(work); + _workqueue_cancel_work(queue, work); } rt_exit_critical(); From 83dca09e1c31637583b99823c86ed86fa3e12491 Mon Sep 17 00:00:00 2001 From: tangyuxin <462747508@qq.com> Date: Sat, 6 Feb 2021 21:54:25 +0800 Subject: [PATCH 13/27] =?UTF-8?q?[workqueue]=20=E4=BF=AE=E5=A4=8D=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=E5=B7=A5=E4=BD=9C=E5=90=8E=E7=AB=8B=E5=8D=B3=E5=8F=96?= =?UTF-8?q?=E6=B6=88=EF=BC=8C=E8=8E=B7=E5=8F=96=E6=97=A0=E6=95=88=E5=B7=A5?= =?UTF-8?q?=E4=BD=9C=E4=BB=BB=E5=8A=A1=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 当高优先级任务提交工作后,立即取消。此时队列被唤醒,下面紧接着获取链表中第一个任务。但此时已经是空链表了。 每次线程被唤醒。检查当前任务链表是否未空 --- components/drivers/src/workqueue.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/drivers/src/workqueue.c b/components/drivers/src/workqueue.c index 3feeaea0be..ce1ca071c3 100644 --- a/components/drivers/src/workqueue.c +++ b/components/drivers/src/workqueue.c @@ -58,15 +58,17 @@ static void _workqueue_thread_entry(void *parameter) while (1) { + level = rt_hw_interrupt_disable(); if (rt_list_isempty(&(queue->work_list))) { + rt_hw_interrupt_enable(level); /* no software timer exist, suspend self. */ rt_thread_suspend(rt_thread_self()); rt_schedule(); + continue; } /* we have work to do with. */ - level = rt_hw_interrupt_disable(); work = rt_list_entry(queue->work_list.next, struct rt_work, list); rt_list_remove(&(work->list)); queue->work_current = work; @@ -76,10 +78,8 @@ static void _workqueue_thread_entry(void *parameter) /* do work */ work->work_func(work, work->work_data); - level = rt_hw_interrupt_disable(); /* clean current work */ queue->work_current = RT_NULL; - rt_hw_interrupt_enable(level); /* ack work completion */ _workqueue_work_completion(queue); From a96156faf11d97e50687732f95a2d589c39dc194 Mon Sep 17 00:00:00 2001 From: tangyuxin <462747508@qq.com> Date: Sun, 7 Feb 2021 10:30:25 +0800 Subject: [PATCH 14/27] =?UTF-8?q?[workqueue]=20=E6=8F=90=E4=BA=A4=E6=AD=A3?= =?UTF-8?q?=E5=9C=A8=E6=89=A7=E8=A1=8C=E7=9A=84=E5=B7=A5=E4=BD=9C=EF=BC=8C?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=20BUSY?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/src/workqueue.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/components/drivers/src/workqueue.c b/components/drivers/src/workqueue.c index ce1ca071c3..f6bb6114aa 100644 --- a/components/drivers/src/workqueue.c +++ b/components/drivers/src/workqueue.c @@ -90,6 +90,7 @@ static rt_err_t _workqueue_submit_work(struct rt_workqueue *queue, struct rt_work *work, rt_tick_t ticks) { rt_base_t level; + rt_err_t err; level = rt_hw_interrupt_disable(); /* remove list */ @@ -103,6 +104,11 @@ static rt_err_t _workqueue_submit_work(struct rt_workqueue *queue, rt_list_insert_after(queue->work_list.prev, &(work->list)); work->flags |= RT_WORK_STATE_PENDING; work->workqueue = queue; + err = RT_EOK; + } + else + { + err = -RT_EBUSY; } /* whether the workqueue is doing work */ @@ -118,7 +124,7 @@ static rt_err_t _workqueue_submit_work(struct rt_workqueue *queue, { rt_hw_interrupt_enable(level); } - return RT_EOK; + return err; } else if (ticks < RT_TICK_MAX / 2) { From 83692a54c444f07eb4a5e980f696f050f5e924e1 Mon Sep 17 00:00:00 2001 From: tangyuxin <462747508@qq.com> Date: Sun, 7 Feb 2021 11:14:59 +0800 Subject: [PATCH 15/27] =?UTF-8?q?=E3=80=90workqueue=E3=80=91=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=87=BD=E6=95=B0=E5=A3=B0=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit rt_err_t rt_workqueue_cancel_all_work(struct rt_workqueue *queue); --- components/drivers/include/ipc/workqueue.h | 1 + 1 file changed, 1 insertion(+) diff --git a/components/drivers/include/ipc/workqueue.h b/components/drivers/include/ipc/workqueue.h index 51976b4de7..11fee489bd 100644 --- a/components/drivers/include/ipc/workqueue.h +++ b/components/drivers/include/ipc/workqueue.h @@ -63,6 +63,7 @@ rt_err_t rt_workqueue_dowork(struct rt_workqueue *queue, struct rt_work *work); rt_err_t rt_workqueue_submit_work(struct rt_workqueue *queue, struct rt_work *work, rt_tick_t time); rt_err_t rt_workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *work); rt_err_t rt_workqueue_cancel_work_sync(struct rt_workqueue *queue, struct rt_work *work); +rt_err_t rt_workqueue_cancel_all_work(struct rt_workqueue *queue); #ifdef RT_USING_SYSTEM_WORKQUEUE rt_err_t rt_work_submit(struct rt_work *work, rt_tick_t time); From 791b8a35deba00ca334ca81c4e451037d05ae883 Mon Sep 17 00:00:00 2001 From: tangyuxin <462747508@qq.com> Date: Mon, 22 Feb 2021 11:23:20 +0800 Subject: [PATCH 16/27] =?UTF-8?q?[workqueue]=20=E7=B3=BB=E7=BB=9F=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E5=B7=A5=E4=BD=9C=E9=98=9F=E5=88=97=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E9=98=B6=E6=AE=B5=E6=8F=90=E5=89=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/src/workqueue.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/components/drivers/src/workqueue.c b/components/drivers/src/workqueue.c index f6bb6114aa..d852b7a4d8 100644 --- a/components/drivers/src/workqueue.c +++ b/components/drivers/src/workqueue.c @@ -369,7 +369,6 @@ int rt_work_sys_workqueue_init(void) return RT_EOK; } - -INIT_DEVICE_EXPORT(rt_work_sys_workqueue_init); +INIT_PREV_EXPORT(rt_work_sys_workqueue_init); #endif #endif From 4f2359d34c502901c56c84ee0b08561aa12f8113 Mon Sep 17 00:00:00 2001 From: tangyuxin <462747508@qq.com> Date: Mon, 22 Feb 2021 17:10:33 +0800 Subject: [PATCH 17/27] =?UTF-8?q?[workqueue]=20=E4=BF=AE=E5=A4=8D=E4=B8=B4?= =?UTF-8?q?=E7=95=8C=E6=9D=A1=E4=BB=B6=E7=BA=BF=E7=A8=8B=E6=84=8F=E5=A4=96?= =?UTF-8?q?=E6=8C=82=E8=B5=B7=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 线程判断任务链表为空时,开中断挂起线程,在挂起线程的过程中,提交的任务不会被执行。 将线程的挂起与恢复放在中断保护内,确保挂起挂起恢复是一个完整的过程 --- components/drivers/src/workqueue.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/components/drivers/src/workqueue.c b/components/drivers/src/workqueue.c index d852b7a4d8..7aea647703 100644 --- a/components/drivers/src/workqueue.c +++ b/components/drivers/src/workqueue.c @@ -61,9 +61,9 @@ static void _workqueue_thread_entry(void *parameter) level = rt_hw_interrupt_disable(); if (rt_list_isempty(&(queue->work_list))) { - rt_hw_interrupt_enable(level); /* no software timer exist, suspend self. */ rt_thread_suspend(rt_thread_self()); + rt_hw_interrupt_enable(level); rt_schedule(); continue; } @@ -115,9 +115,9 @@ static rt_err_t _workqueue_submit_work(struct rt_workqueue *queue, if (queue->work_current == RT_NULL && ((queue->work_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND)) { - rt_hw_interrupt_enable(level); /* resume work thread */ rt_thread_resume(queue->work_thread); + rt_hw_interrupt_enable(level); rt_schedule(); } else @@ -197,9 +197,9 @@ static void _delayed_work_timeout_handler(void *parameter) if (queue->work_current == RT_NULL && ((queue->work_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND)) { - rt_hw_interrupt_enable(level); /* resume work thread */ rt_thread_resume(queue->work_thread); + rt_hw_interrupt_enable(level); rt_schedule(); } else @@ -277,9 +277,9 @@ rt_err_t rt_workqueue_critical_work(struct rt_workqueue *queue, struct rt_work * if (queue->work_current == RT_NULL && ((queue->work_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND)) { - rt_hw_interrupt_enable(level); /* resume work thread */ rt_thread_resume(queue->work_thread); + rt_hw_interrupt_enable(level); rt_schedule(); } else @@ -321,14 +321,14 @@ rt_err_t rt_workqueue_cancel_all_work(struct rt_workqueue *queue) RT_ASSERT(queue != RT_NULL); - // cancel work + /* cancel work */ rt_enter_critical(); while (rt_list_isempty(&queue->work_list) == RT_FALSE) { work = rt_list_first_entry(&queue->work_list, struct rt_work, list); _workqueue_cancel_work(queue, work); } - // cancel delay work + /* cancel delay work */ while (rt_list_isempty(&queue->delayed_list) == RT_FALSE) { work = rt_list_first_entry(&queue->delayed_list, struct rt_work, list); From e03eaec462ed5a890c37c7c8521012f3529e9f4e Mon Sep 17 00:00:00 2001 From: LeeChunHei Date: Tue, 23 Feb 2021 13:42:19 +0800 Subject: [PATCH 18/27] update to new usb api in hid and mouse source file --- components/drivers/Kconfig | 8 ++ components/drivers/usb/usbhost/class/hid.c | 131 ++++++++++-------- components/drivers/usb/usbhost/class/hid.h | 12 +- components/drivers/usb/usbhost/class/umouse.c | 25 +++- 4 files changed, 109 insertions(+), 67 deletions(-) diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index 006497519f..b0caabc73c 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -642,6 +642,14 @@ menu "Using USB" string "Udisk mount dir" default "/" endif + config RT_USBH_HID + bool "Enable HID Drivers" + default n + if RT_USBH_HID + config RT_USBH_HID_MOUSE + bool "Enable HID mouse protocol" + default n + endif endif config RT_USING_USB_DEVICE bool "Using USB device" diff --git a/components/drivers/usb/usbhost/class/hid.c b/components/drivers/usb/usbhost/class/hid.c index e16096877f..b097b01679 100644 --- a/components/drivers/usb/usbhost/class/hid.c +++ b/components/drivers/usb/usbhost/class/hid.c @@ -6,6 +6,7 @@ * Change Logs: * Date Author Notes * 2011-12-12 Yi Qiu first version + * 2021-02-23 Leslie Lee update with current usb api */ #include @@ -26,7 +27,7 @@ static rt_list_t _protocal_list; * * @return the error code, RT_EOK on successfully. */ -rt_err_t rt_usbh_hid_set_idle(struct uintf* intf, int duration, int report_id) +rt_err_t rt_usbh_hid_set_idle(struct uhintf* intf, int duration, int report_id) { struct urequest setup; struct uinstance* device; @@ -40,14 +41,15 @@ rt_err_t rt_usbh_hid_set_idle(struct uintf* intf, int duration, int report_id) setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE; - setup.request = USB_REQ_SET_IDLE; - setup.index = 0; - setup.length = 0; - setup.value = (duration << 8 )| report_id; + setup.bRequest = USB_REQ_SET_IDLE; + setup.wIndex = 0; + setup.wLength = 0; + setup.wValue = (duration << 8 )| report_id; - if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, RT_NULL, 0, - timeout) == 0) return RT_EOK; - else return -RT_FALSE; + if (rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8) + return RT_EOK; + else + return -RT_FALSE; } /** @@ -59,7 +61,7 @@ rt_err_t rt_usbh_hid_set_idle(struct uintf* intf, int duration, int report_id) * * @return the error code, RT_EOK on successfully. */ -rt_err_t rt_usbh_hid_get_report(struct uintf* intf, rt_uint8_t type, +rt_err_t rt_usbh_hid_get_report(struct uhintf* intf, rt_uint8_t type, rt_uint8_t id, rt_uint8_t *buffer, rt_size_t size) { struct urequest setup; @@ -74,14 +76,24 @@ rt_err_t rt_usbh_hid_get_report(struct uintf* intf, rt_uint8_t type, setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE; - setup.request = USB_REQ_GET_REPORT; - setup.index = intf->intf_desc->bInterfaceNumber; - setup.length = size; - setup.value = (type << 8 ) + id; + setup.bRequest = USB_REQ_GET_REPORT; + setup.wIndex = intf->intf_desc->bInterfaceNumber; + setup.wLength = size; + setup.wValue = (type << 8 ) + id; - if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, buffer, size, - timeout) == size) return RT_EOK; - else return -RT_FALSE; + if (rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8) + { + if (rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, buffer, size, timeout) == size) + { + if (rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_out, RT_NULL, 0, timeout) == 0) + { + return RT_EOK; + } + } + } + else + return -RT_FALSE; + return -RT_FALSE; } /** @@ -93,7 +105,7 @@ rt_err_t rt_usbh_hid_get_report(struct uintf* intf, rt_uint8_t type, * * @return the error code, RT_EOK on successfully. */ -rt_err_t rt_usbh_hid_set_report(struct uintf* intf, rt_uint8_t *buffer, rt_size_t size) +rt_err_t rt_usbh_hid_set_report(struct uhintf* intf, rt_uint8_t *buffer, rt_size_t size) { struct urequest setup; struct uinstance* device; @@ -107,14 +119,15 @@ rt_err_t rt_usbh_hid_set_report(struct uintf* intf, rt_uint8_t *buffer, rt_size_ setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE; - setup.request = USB_REQ_SET_REPORT; - setup.index = intf->intf_desc->bInterfaceNumber; - setup.length = size; - setup.value = 0x02 << 8; + setup.bRequest = USB_REQ_SET_REPORT; + setup.wIndex = intf->intf_desc->bInterfaceNumber; + setup.wLength = size; + setup.wValue = 0x02 << 8; - if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, buffer, size, - timeout) == size) return RT_EOK; - else return -RT_FALSE; + if (rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8) + return RT_EOK; + else + return -RT_FALSE; } /** @@ -125,7 +138,7 @@ rt_err_t rt_usbh_hid_set_report(struct uintf* intf, rt_uint8_t *buffer, rt_size_ * * @return the error code, RT_EOK on successfully. */ -rt_err_t rt_usbh_hid_set_protocal(struct uintf* intf, int protocol) +rt_err_t rt_usbh_hid_set_protocal(struct uhintf* intf, int protocol) { struct urequest setup; struct uinstance* device; @@ -139,14 +152,15 @@ rt_err_t rt_usbh_hid_set_protocal(struct uintf* intf, int protocol) setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE; - setup.request = USB_REQ_SET_PROTOCOL; - setup.index = 0; - setup.length = 0; - setup.value = protocol; + setup.bRequest = USB_REQ_SET_PROTOCOL; + setup.wIndex = 0; + setup.wLength = 0; + setup.wValue = protocol; - if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, RT_NULL, 0, - timeout) == 0) return RT_EOK; - else return -RT_FALSE; + if (rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8) + return RT_EOK; + else + return -RT_FALSE; } /** @@ -159,7 +173,7 @@ rt_err_t rt_usbh_hid_set_protocal(struct uintf* intf, int protocol) * * @return the error code, RT_EOK on successfully. */ -rt_err_t rt_usbh_hid_get_report_descriptor(struct uintf* intf, +rt_err_t rt_usbh_hid_get_report_descriptor(struct uhintf* intf, rt_uint8_t *buffer, rt_size_t size) { struct urequest setup; @@ -174,14 +188,24 @@ rt_err_t rt_usbh_hid_get_report_descriptor(struct uintf* intf, setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_STANDARD| USB_REQ_TYPE_INTERFACE; - setup.request = USB_REQ_GET_DESCRIPTOR; - setup.index = 0; - setup.length = size; - setup.value = USB_DESC_TYPE_REPORT << 8; + setup.bRequest = USB_REQ_GET_DESCRIPTOR; + setup.wIndex = 0; + setup.wLength = size; + setup.wValue = USB_DESC_TYPE_REPORT << 8; - if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, buffer, size, - timeout) == size) return RT_EOK; - else return -RT_FALSE; + if (rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8) + { + if (rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, buffer, size, timeout) == size) + { + if (rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_out, RT_NULL, 0, timeout) == 0) + { + return RT_EOK; + } + } + } + else + return -RT_FALSE; + return -RT_FALSE; } /** @@ -220,16 +244,16 @@ static void rt_usbh_hid_callback(void* context) RT_ASSERT(context != RT_NULL); pipe = (upipe_t)context; - hid = (struct uhid*)pipe->intf->user_data; + hid = (struct uhid*)((struct uhintf*)pipe->inst)->user_data; /* invoke protocal callback function */ hid->protocal->callback((void*)hid); /* parameter check */ - RT_ASSERT(pipe->intf->device->hcd != RT_NULL); + RT_ASSERT(((struct uhintf*)pipe->inst)->device->hcd != RT_NULL); - rt_usb_hcd_int_xfer(pipe->intf->device->hcd, pipe, hid->buffer, - pipe->ep.wMaxPacketSize, timeout); + rt_usb_hcd_pipe_xfer(((struct uhintf*)pipe->inst)->device->hcd, pipe, + hid->buffer, pipe->ep.wMaxPacketSize, timeout); } /** @@ -268,9 +292,7 @@ static rt_err_t rt_usbh_hid_enable(void* arg) int i = 0, pro_id; uprotocal_t protocal; struct uhid* hid; - struct uintf* intf = (struct uintf*)arg; - int timeout = USB_TIMEOUT_BASIC; - upipe_t pipe; + struct uhintf* intf = (struct uhintf*)arg; /* parameter check */ if(intf == RT_NULL) @@ -319,19 +341,13 @@ static rt_err_t rt_usbh_hid_enable(void* arg) if(!(ep_desc->bEndpointAddress & USB_DIR_IN)) continue; ret = rt_usb_hcd_alloc_pipe(intf->device->hcd, &hid->pipe_in, - intf, ep_desc, rt_usbh_hid_callback); + intf, ep_desc); if(ret != RT_EOK) return ret; } /* initialize hid protocal */ - hid->protocal->init((void*)intf); - pipe = hid->pipe_in; + hid->protocal->init((void*)intf); - /* parameter check */ - RT_ASSERT(pipe->intf->device->hcd != RT_NULL); - - rt_usb_hcd_int_xfer(pipe->intf->device->hcd, hid->pipe_in, - hid->buffer, hid->pipe_in->ep.wMaxPacketSize, timeout); return RT_EOK; } @@ -346,7 +362,7 @@ static rt_err_t rt_usbh_hid_enable(void* arg) static rt_err_t rt_usbh_hid_disable(void* arg) { struct uhid* hid; - struct uintf* intf = (struct uintf*)arg; + struct uhintf* intf = (struct uhintf*)arg; RT_ASSERT(intf != RT_NULL); @@ -364,9 +380,6 @@ static rt_err_t rt_usbh_hid_disable(void* arg) /* free the hid instance */ rt_free(hid); } - - /* free the instance */ - rt_free(intf); return RT_EOK; } diff --git a/components/drivers/usb/usbhost/class/hid.h b/components/drivers/usb/usbhost/class/hid.h index 8e446c8cea..dbef84abbe 100644 --- a/components/drivers/usb/usbhost/class/hid.h +++ b/components/drivers/usb/usbhost/class/hid.h @@ -31,11 +31,11 @@ typedef struct uhid uhid_t; #define USB_HID_KEYBOARD 1 #define USB_HID_MOUSE 2 -rt_err_t rt_usbh_hid_set_idle(struct uintf* intf, int duration, int report_id); -rt_err_t rt_usbh_hid_get_report(struct uintf* intf, rt_uint8_t type, rt_uint8_t id, rt_uint8_t *buffer, rt_size_t size); -rt_err_t rt_usbh_hid_set_report(struct uintf* intf, rt_uint8_t *buffer, rt_size_t size); -rt_err_t rt_usbh_hid_set_protocal(struct uintf* intf, int protocol); -rt_err_t rt_usbh_hid_get_report_descriptor(struct uintf* intf, rt_uint8_t *buffer, rt_size_t size); +rt_err_t rt_usbh_hid_set_idle(struct uhintf* intf, int duration, int report_id); +rt_err_t rt_usbh_hid_get_report(struct uhintf* intf, rt_uint8_t type, rt_uint8_t id, rt_uint8_t *buffer, rt_size_t size); +rt_err_t rt_usbh_hid_set_report(struct uhintf* intf, rt_uint8_t *buffer, rt_size_t size); +rt_err_t rt_usbh_hid_set_protocal(struct uhintf* intf, int protocol); +rt_err_t rt_usbh_hid_get_report_descriptor(struct uhintf* intf, rt_uint8_t *buffer, rt_size_t size); rt_err_t rt_usbh_hid_protocal_register(uprotocal_t protocal); -#endif +#endif \ No newline at end of file diff --git a/components/drivers/usb/usbhost/class/umouse.c b/components/drivers/usb/usbhost/class/umouse.c index e769475a98..7f50c0451f 100644 --- a/components/drivers/usb/usbhost/class/umouse.c +++ b/components/drivers/usb/usbhost/class/umouse.c @@ -126,15 +126,36 @@ static rt_err_t rt_usbh_hid_mouse_callback(void* arg) return RT_EOK; } +rt_thread_t mouse_thread; +void mouse_task(void* param) +{ + struct uhintf* intf = (struct uhintf*)param; + while (1) + { + if (rt_usb_hcd_pipe_xfer(intf->device->hcd, ((struct uhid*)intf->user_data)->pipe_in, + ((struct uhid*)intf->user_data)->buffer, ((struct uhid*)intf->user_data)->pipe_in->ep.wMaxPacketSize, + USB_TIMEOUT_BASIC) == 0) + { + break; + } + + rt_usbh_hid_mouse_callback(intf->user_data); + } +} + + static rt_err_t rt_usbh_hid_mouse_init(void* arg) { - struct uintf* intf = (struct uintf*)arg; + struct uhintf* intf = (struct uhintf*)arg; RT_ASSERT(intf != RT_NULL); rt_usbh_hid_set_protocal(intf, 0); - rt_usbh_hid_set_idle(intf, 10, 0); + rt_usbh_hid_set_idle(intf, 0, 0); + + mouse_thread = rt_thread_create("mouse0", mouse_task, intf, 500, 8, 100); + rt_thread_startup(mouse_thread); RT_DEBUG_LOG(RT_DEBUG_USB, ("start usb mouse\n")); #ifdef RT_USING_RTGUI From 5a184c19b21bcc946697b8c83f6a43b6200aa02f Mon Sep 17 00:00:00 2001 From: Howard Su Date: Sun, 28 Feb 2021 16:42:27 +0800 Subject: [PATCH 19/27] Return when i2c transfer 0 messages msg->flags is accessed without proper intialized msg variable. --- components/drivers/i2c/i2c-bit-ops.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/components/drivers/i2c/i2c-bit-ops.c b/components/drivers/i2c/i2c-bit-ops.c index 96af567a1e..3a9b4efeb3 100644 --- a/components/drivers/i2c/i2c-bit-ops.c +++ b/components/drivers/i2c/i2c-bit-ops.c @@ -375,6 +375,8 @@ static rt_size_t i2c_bit_xfer(struct rt_i2c_bus_device *bus, rt_int32_t i, ret; rt_uint16_t ignore_nack; + if (num == 0) return 0; + for (i = 0; i < num; i++) { msg = &msgs[i]; From 53dea16e5c6876cc3d133260efb12ebd4b670085 Mon Sep 17 00:00:00 2001 From: thread-liu Date: Mon, 1 Mar 2021 15:56:52 +0800 Subject: [PATCH 20/27] [update] sdio wifi. --- bsp/stm32/libraries/STM32MPxx_HAL/SConscript | 4 + .../CM4/Inc/stm32mp1xx_hal_conf.h | 2 +- .../CM4/Src/stm32mp1xx_hal_msp.c | 118 ++++++++++++ .../stm32mp157a-st-discovery/board/Kconfig | 15 +- .../board/ports/drv_sdio.c | 110 +++++++++-- .../board/ports/drv_sdio.h | 12 +- .../stm32mp157a-st-discovery/project.ewp | 174 +++++++++++++++--- 7 files changed, 387 insertions(+), 48 deletions(-) diff --git a/bsp/stm32/libraries/STM32MPxx_HAL/SConscript b/bsp/stm32/libraries/STM32MPxx_HAL/SConscript index a991e588d0..ce482829bd 100644 --- a/bsp/stm32/libraries/STM32MPxx_HAL/SConscript +++ b/bsp/stm32/libraries/STM32MPxx_HAL/SConscript @@ -119,6 +119,10 @@ if GetDepend(['BSP_USING_CRYP']): src += ['STM32MP1xx_HAL_Driver/Src/stm32mp1xx_hal_cryp.c'] src += ['STM32MP1xx_HAL_Driver/Src/stm32mp1xx_hal_cryp_ex.c'] +if GetDepend(['BSP_USING_RTC']): + src += ['STM32MP1xx_HAL_Driver/Src/stm32mp1xx_hal_rtc.c'] + src += ['STM32MP1xx_HAL_Driver/Src/stm32mp1xx_hal_rtc_ex.c'] + path = [cwd + '/STM32MP1xx_HAL_Driver/Inc', cwd + '/CMSIS/Device/ST/STM32MP1xx/Include', cwd + '/CMSIS/Core/Include', diff --git a/bsp/stm32/stm32mp157a-st-discovery/board/CubeMX_Config/CM4/Inc/stm32mp1xx_hal_conf.h b/bsp/stm32/stm32mp157a-st-discovery/board/CubeMX_Config/CM4/Inc/stm32mp1xx_hal_conf.h index 3ba077b0b5..b7fd4ce81f 100644 --- a/bsp/stm32/stm32mp157a-st-discovery/board/CubeMX_Config/CM4/Inc/stm32mp1xx_hal_conf.h +++ b/bsp/stm32/stm32mp157a-st-discovery/board/CubeMX_Config/CM4/Inc/stm32mp1xx_hal_conf.h @@ -60,7 +60,7 @@ #define HAL_SAI_MODULE_ENABLED #define HAL_SD_MODULE_ENABLED /*#define HAL_MMC_MODULE_ENABLED */ -/*#define HAL_RTC_MODULE_ENABLED */ +#define HAL_RTC_MODULE_ENABLED /*#define HAL_SMBUS_MODULE_ENABLED */ /*#define HAL_SPDIFRX_MODULE_ENABLED */ #define HAL_SPI_MODULE_ENABLED diff --git a/bsp/stm32/stm32mp157a-st-discovery/board/CubeMX_Config/CM4/Src/stm32mp1xx_hal_msp.c b/bsp/stm32/stm32mp157a-st-discovery/board/CubeMX_Config/CM4/Src/stm32mp1xx_hal_msp.c index 3a6fac25d7..5ca08fbbd0 100644 --- a/bsp/stm32/stm32mp157a-st-discovery/board/CubeMX_Config/CM4/Src/stm32mp1xx_hal_msp.c +++ b/bsp/stm32/stm32mp157a-st-discovery/board/CubeMX_Config/CM4/Src/stm32mp1xx_hal_msp.c @@ -1003,7 +1003,66 @@ void HAL_SD_MspInit(SD_HandleTypeDef* hsd) /* USER CODE END SDMMC1_MspInit 1 */ } + if(hsd->Instance==SDMMC2) + { + /* USER CODE BEGIN SDMMC2_MspInit 0 */ + if (IS_ENGINEERING_BOOT_MODE()) + { + /** Initializes the peripherals clock + */ + PeriphClkInit.Sdmmc12ClockSelection = RCC_SDMMC12CLKSOURCE_PLL4; + PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_SDMMC12; + if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) + { + Error_Handler(); + } + } + /* USER CODE END SDMMC2_MspInit 0 */ + /* Peripheral clock enable */ + __HAL_RCC_SDMMC2_CLK_ENABLE(); + + __HAL_RCC_GPIOB_CLK_ENABLE(); + __HAL_RCC_GPIOE_CLK_ENABLE(); + __HAL_RCC_GPIOG_CLK_ENABLE(); + /**SDMMC2 GPIO Configuration + PB14 ------> SDMMC2_D0 + PB15 ------> SDMMC2_D1 + PB3 ------> SDMMC2_D2 + PB4 ------> SDMMC2_D3 + PE3 ------> SDMMC2_CK + PG6 ------> SDMMC2_CMD + */ + GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_14|GPIO_PIN_15; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF9_SDIO2; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + GPIO_InitStruct.Pin = GPIO_PIN_3; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF9_SDIO2; + HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); + + GPIO_InitStruct.Pin = GPIO_PIN_6; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF10_SDIO2; + HAL_GPIO_Init(GPIOG, &GPIO_InitStruct); + + __HAL_RCC_SDMMC2_FORCE_RESET(); + __HAL_RCC_SDMMC2_RELEASE_RESET(); + + /* SDMMC2 interrupt Init */ + HAL_NVIC_SetPriority(SDMMC2_IRQn, 2, 0); + HAL_NVIC_EnableIRQ(SDMMC2_IRQn); + /* USER CODE BEGIN SDMMC2_MspInit 1 */ + + /* USER CODE END SDMMC2_MspInit 1 */ + } } /** @@ -1312,6 +1371,65 @@ void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef* hcryp) } #endif + +/** +* @brief RTC MSP Initialization +* This function configures the hardware resources used in this example +* @param hrtc: RTC handle pointer +* @retval None +*/ +void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc) +{ + RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; + if(hrtc->Instance==RTC) + { + /* USER CODE BEGIN SDMMC1_MspInit 0 */ + if (IS_ENGINEERING_BOOT_MODE()) + { + /** Initializes the peripherals clock + */ + PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; + PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC; + if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) + { + Error_Handler(); + } + } + /* USER CODE BEGIN RTC_MspInit 0 */ + + /* USER CODE END RTC_MspInit 0 */ + /* Peripheral clock enable */ + __HAL_RCC_RTC_ENABLE(); + + /* USER CODE BEGIN RTC_MspInit 1 */ + + /* USER CODE END RTC_MspInit 1 */ + } + +} + +/** +* @brief RTC MSP De-Initialization +* This function freeze the hardware resources used in this example +* @param hrtc: RTC handle pointer +* @retval None +*/ +void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc) +{ + if(hrtc->Instance==RTC) + { + /* USER CODE BEGIN RTC_MspDeInit 0 */ + + /* USER CODE END RTC_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_RTC_DISABLE(); + + /* USER CODE BEGIN RTC_MspDeInit 1 */ + + /* USER CODE END RTC_MspDeInit 1 */ + } + +} /** * @brief This function is executed in case of error occurrence. * @retval None diff --git a/bsp/stm32/stm32mp157a-st-discovery/board/Kconfig b/bsp/stm32/stm32mp157a-st-discovery/board/Kconfig index 9191443847..36ba61a584 100644 --- a/bsp/stm32/stm32mp157a-st-discovery/board/Kconfig +++ b/bsp/stm32/stm32mp157a-st-discovery/board/Kconfig @@ -58,10 +58,23 @@ menu "Onboard Peripheral Drivers" select RT_USING_LWIP config BSP_USING_SDMMC - bool "Enable SDMMC (SD card)" + bool "Enable SDMMC (sd card or sdio wifi)" + default n select RT_USING_SDIO select RT_USING_DFS select RT_USING_DFS_ELMFAT + if BSP_USING_SDMMC + config BSP_USING_SDIO1 + bool "Enable SDIO1 (sd card)" + default n + config BSP_USING_SDIO2 + select BSP_USING_RTC + bool "Enable SDIO2 (sdio wifi)" + default n + endif + + config BSP_USING_RTC + bool "Enable RTC" default n menuconfig BSP_USING_AUDIO diff --git a/bsp/stm32/stm32mp157a-st-discovery/board/ports/drv_sdio.c b/bsp/stm32/stm32mp157a-st-discovery/board/ports/drv_sdio.c index 3e5085c3cd..45de04328f 100644 --- a/bsp/stm32/stm32mp157a-st-discovery/board/ports/drv_sdio.c +++ b/bsp/stm32/stm32mp157a-st-discovery/board/ports/drv_sdio.c @@ -10,11 +10,17 @@ #include "board.h" #include "drv_sdio.h" + +#ifdef BSP_USING_SDIO1 #include +#endif #ifdef BSP_USING_SDMMC -//#define DRV_DEBUG +#ifdef BSP_USING_SDIO2 +#define DRV_DEBUG +#endif + #define DBG_TAG "drv.sdio" #ifdef DRV_DEBUG #define DBG_LVL DBG_LOG @@ -23,8 +29,9 @@ #endif /* DRV_DEBUG */ #include -static SD_HandleTypeDef hsd; -static struct rt_mmcsd_host *host; +static struct rt_mmcsd_host *host1; +static struct rt_mmcsd_host *host2; + #define SDIO_TX_RX_COMPLETE_TIMEOUT_LOOPS (100000) #define RTHW_SDIO_LOCK(_sdio) rt_mutex_take(&_sdio->mutex, RT_WAITING_FOREVER) @@ -47,13 +54,14 @@ struct rthw_sdio }; /* SYSRAM SDMMC1/2 accesses */ +#define SDCARD_ADDR 0x2FFC0000 #if defined(__CC_ARM) || defined(__CLANG_ARM) -rt_uint8_t cache_buf[SDIO_BUFF_SIZE] __attribute__((at(0x2FFC0000))); +__attribute__((at(SDCARD_ADDR))) static rt_uint8_t cache_buf[SDIO_BUFF_SIZE]; +#elif defined ( __GNUC__ ) +static rt_uint8_t cache_buf[SDIO_BUFF_SIZE] __attribute__((section(".SdCardSection"))); #elif defined(__ICCARM__) -#pragma location=0x2FFC0000 -rt_uint8_t cache_buf[SDIO_BUFF_SIZE]; -#elif defined(__GNUC__) -rt_uint8_t cache_buf[SDIO_BUFF_SIZE] __attribute__((at(0x2FFC0000))); +#pragma location = SDCARD_ADDR +__no_init static rt_uint8_t cache_buf[SDIO_BUFF_SIZE]; #endif /** @@ -461,10 +469,20 @@ struct rt_mmcsd_host *sdio_host_create(struct stm32_sdio_des *sdio_des) rt_memcpy(&sdio->sdio_des, sdio_des, sizeof(struct stm32_sdio_des)); - sdio->sdio_des.hw_sdio = (struct stm32_sdio *)SDIO_BASE_ADDRESS; + if(sdio_des->hsd.Instance == SDMMC1) + { + sdio->sdio_des.hw_sdio = (struct stm32_sdio *)SDIO1_BASE_ADDRESS; + rt_event_init(&sdio->event, "sdio1", RT_IPC_FLAG_FIFO); + rt_mutex_init(&sdio->mutex, "sdio1", RT_IPC_FLAG_FIFO); + } + + if(sdio_des->hsd.Instance == SDMMC2) + { + sdio->sdio_des.hw_sdio = (struct stm32_sdio *)SDIO2_BASE_ADDRESS; + rt_event_init(&sdio->event, "sdio2", RT_IPC_FLAG_FIFO); + rt_mutex_init(&sdio->mutex, "sdio2", RT_IPC_FLAG_FIFO); + } - rt_event_init(&sdio->event, "sdio", RT_IPC_FLAG_FIFO); - rt_mutex_init(&sdio->mutex, "sdio", RT_IPC_FLAG_FIFO); /* set host default attributes */ host->ops = &ops; host->freq_min = 400 * 1000; @@ -503,29 +521,83 @@ void SDMMC1_IRQHandler(void) { rt_interrupt_enter(); /* Process All SDIO Interrupt Sources */ - rthw_sdio_irq_process(host); + rthw_sdio_irq_process(host1); rt_interrupt_leave(); } +void SDMMC2_IRQHandler(void) +{ + /* enter interrupt */ + rt_interrupt_enter(); + /* Process All SDIO Interrupt Sources */ + rthw_sdio_irq_process(host2); + /* leave interrupt */ + rt_interrupt_leave(); +} + +#ifdef BSP_USING_SDIO2 +static RTC_HandleTypeDef hrtc; +static void MX_RTC_Init(void) +{ + hrtc.Instance = RTC; + hrtc.Init.HourFormat = RTC_HOURFORMAT_24; + hrtc.Init.AsynchPrediv = 127; + hrtc.Init.SynchPrediv = 255; + hrtc.Init.OutPut = RTC_OUTPUT_DISABLE; + hrtc.Instance->CFGR = 0x02 << 1; + if (HAL_RTC_Init(&hrtc) != HAL_OK) + { + Error_Handler(); + } +} +static int LBEE5KL1DX_init(void) +{ +#define LBEE5KL1DX_WL_REG_ON GET_PIN(H, 4) + + /* enable the WLAN REG pin */ + rt_pin_mode(LBEE5KL1DX_WL_REG_ON, PIN_MODE_OUTPUT); + rt_pin_write(LBEE5KL1DX_WL_REG_ON, PIN_HIGH); + + return 0; +} +#endif + int rt_hw_sdio_init(void) { - struct stm32_sdio_des sdio_des; - - hsd.Instance = SDMMC1; - HAL_SD_MspInit(&hsd); +#ifdef BSP_USING_SDIO1 + struct stm32_sdio_des sdio_des1; + sdio_des1.hsd.Instance = SDMMC1; + HAL_SD_MspInit(&sdio_des1.hsd); - host = sdio_host_create(&sdio_des); - if (host == RT_NULL) + host1 = sdio_host_create(&sdio_des1); + if (host1 == RT_NULL) { LOG_E("host create fail"); return RT_NULL; } +#endif +#ifdef BSP_USING_SDIO2 + MX_RTC_Init(); + LBEE5KL1DX_init(); + + struct stm32_sdio_des sdio_des2; + sdio_des2.hsd.Instance = SDMMC2; + HAL_SD_MspInit(&sdio_des2.hsd); + + host2 = sdio_host_create(&sdio_des2); + if (host2 == RT_NULL) + { + LOG_E("host2 create fail"); + return RT_NULL; + } +#endif return RT_EOK; } INIT_DEVICE_EXPORT(rt_hw_sdio_init); +#ifdef BSP_USING_SDIO1 int mnt_init(void) { rt_device_t sd = RT_NULL; @@ -552,4 +624,6 @@ int mnt_init(void) } INIT_ENV_EXPORT(mnt_init); +#endif /* BSP_USING_SDIO1 */ + #endif /* BSP_USING_SDMMC */ diff --git a/bsp/stm32/stm32mp157a-st-discovery/board/ports/drv_sdio.h b/bsp/stm32/stm32mp157a-st-discovery/board/ports/drv_sdio.h index a4428e4e71..da70bf1257 100644 --- a/bsp/stm32/stm32mp157a-st-discovery/board/ports/drv_sdio.h +++ b/bsp/stm32/stm32mp157a-st-discovery/board/ports/drv_sdio.h @@ -19,11 +19,12 @@ #include #include -#define SDIO_BUFF_SIZE 4096 -#define SDIO_ALIGN_LEN 32 +#ifndef SDIO1_BASE_ADDRESS +#define SDIO1_BASE_ADDRESS (SDMMC1) +#endif -#ifndef SDIO_BASE_ADDRESS -#define SDIO_BASE_ADDRESS (SDMMC1) +#ifndef SDIO2_BASE_ADDRESS +#define SDIO2_BASE_ADDRESS (SDMMC2) #endif #ifndef SDIO_CLOCK_FREQ @@ -39,7 +40,7 @@ #endif #ifndef SDIO_MAX_FREQ -#define SDIO_MAX_FREQ (50 * 1000 * 1000) +#define SDIO_MAX_FREQ (25 * 1000 * 1000) #endif #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) @@ -102,6 +103,7 @@ struct stm32_sdio_des { struct stm32_sdio *hw_sdio; sdio_clk_get clk_get; + SD_HandleTypeDef hsd; }; /* stm32 sdio dirver class */ diff --git a/bsp/stm32/stm32mp157a-st-discovery/project.ewp b/bsp/stm32/stm32mp157a-st-discovery/project.ewp index 3547891da5..3f12a28dbe 100644 --- a/bsp/stm32/stm32mp157a-st-discovery/project.ewp +++ b/bsp/stm32/stm32mp157a-st-discovery/project.ewp @@ -232,6 +232,8 @@ STM32MP157Axx __LOG_TRACE_IO_ __RTTHREAD__ + RT_USING_DLIBC + _DLIB_FILE_DESCRIPTOR USE_HAL_DRIVER