From 43005a1b89c9b10a8b6589b0fd6ebf206aef0c03 Mon Sep 17 00:00:00 2001 From: sakumisu <1203593632@qq.com> Date: Sat, 15 Jan 2022 17:14:26 +0800 Subject: [PATCH] add usb os wrapper --- README.md | 2 + README_zh.md | 2 + osal/usb_osal.h | 49 ++++++++++++++++++++++ osal/usb_osal_freertos.c | 88 ++++++++++++++++++++++++++++++++++++++++ osal/usb_osal_rtthread.c | 88 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 229 insertions(+) create mode 100644 osal/usb_osal.h create mode 100644 osal/usb_osal_freertos.c create mode 100644 osal/usb_osal_rtthread.c diff --git a/README.md b/README.md index cca8425d..ea5486df 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ USB Stack is a tiny, beautiful and portable USB host and device stack for embedd │ └── mm32 │ └── ch32 ├── docs +├── osal ├── packet capture └── port ├── bouffalolab @@ -44,6 +45,7 @@ USB Stack is a tiny, beautiful and portable USB host and device stack for embedd |common | usb spec macros and utils | |core | usb core implementation | |demo | different chip demo | +|osal | os wrapper | |docs | doc for guiding | |packet capture | packet capture file | |port | usb dcd and hcd porting | diff --git a/README_zh.md b/README_zh.md index 770e21b7..73d40741 100644 --- a/README_zh.md +++ b/README_zh.md @@ -26,6 +26,7 @@ USB Stack 是一个小而美的、可移植性高的、用于嵌入式系统的 │ └── mm32 │ └── ch32 ├── docs +├── osal ├── packet capture └── port ├── bouffalolab @@ -45,6 +46,7 @@ USB Stack 是一个小而美的、可移植性高的、用于嵌入式系统的 |core | usb 主从核心实现 | |demo | 示例 | |docs | 文档 | +|osal | os 封装层 | |packet capture | 抓包文件(需要使用力科软件打开)| |port | usb 主从需要实现的 porting 接口 | diff --git a/osal/usb_osal.h b/osal/usb_osal.h new file mode 100644 index 00000000..753867ab --- /dev/null +++ b/osal/usb_osal.h @@ -0,0 +1,49 @@ +/** + * @file usb_osal.h + * @brief + * + * Copyright (c) 2022 sakumisu + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you 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 _USB_OSAL_H +#define _USB_OSAL_H + +#include + +typedef void *usb_osal_thread_t; +typedef void *usb_osal_sem_t; +typedef void *usb_osal_mutex_t; +typedef void (*usb_thread_entry_t)(void *argument); + +usb_osal_thread_t usb_osal_thread_create(const char *name, uint32_t stack_size, uint32_t prio, usb_thread_entry_t entry); +void usb_osal_thread_delete(usb_osal_thread_t thread); + +usb_osal_sem_t usb_osal_sem_create(uint32_t initial_count); +int usb_osal_sem_take(usb_osal_sem_t sem); +int usb_osal_sem_give(usb_osal_sem_t sem); + +usb_osal_mutex_t usb_osal_mutex_create(void); +int usb_osal_mutex_take(usb_osal_mutex_t mutex); +int usb_osal_mutex_give(usb_osal_mutex_t mutex); + +uint32_t usb_osal_enter_critical_section(void); +void usb_osal_leave_critical_section(uint32_t flag); + +void usb_osal_delay_ms(uint32_t delay); + +#endif \ No newline at end of file diff --git a/osal/usb_osal_freertos.c b/osal/usb_osal_freertos.c new file mode 100644 index 00000000..c8118625 --- /dev/null +++ b/osal/usb_osal_freertos.c @@ -0,0 +1,88 @@ +/** + * @file usb_osal_freertos.c + * @brief + * + * Copyright (c) 2022 sakumisu + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you 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. + * + */ +#include "usb_osal.h" +#include +#include "semphr.h" + +usb_osal_thread_t usb_osal_thread_create(const char *name, uint32_t stack_size, uint32_t prio, usb_thread_entry_t entry) +{ + TaskHandle_t htask = NULL; + stack_size /= sizeof(StackType_t); + xTaskCreate(entry, name, stack_size, NULL, prio, &htask); + return (usb_osal_thread_t)htask; +} + +usb_osal_sem_t usb_osal_sem_create(uint32_t initial_count) +{ + return (usb_osal_sem_t)xSemaphoreCreateCounting(1, initial_count); +} + +int usb_osal_sem_take(usb_osal_sem_t sem) +{ + return (xSemaphoreTake((SemaphoreHandle_t)sem, portMAX_DELAY) == pdPASS) ? 0 : -1; +} + +int usb_osal_sem_give(usb_osal_sem_t sem) +{ + return (xSemaphoreGive((SemaphoreHandle_t)sem) == pdPASS) ? 0 : -1; + + // BaseType_t xHigherPriorityTaskWoken = pdFALSE; + + // int ret = xSemaphoreGiveFromISR((SemaphoreHandle_t)sem, &xHigherPriorityTaskWoken); + + // if (ret == pdPASS) { + // portYIELD_FROM_ISR(xHigherPriorityTaskWoken); + // } + // return (ret == pdPASS) ? 0 : -1; +} + +usb_osal_mutex_t usb_osal_mutex_create(void) +{ + return (usb_osal_mutex_t)xSemaphoreCreateMutex(); +} + +int usb_osal_mutex_take(usb_osal_mutex_t mutex) +{ + return (xSemaphoreTake((SemaphoreHandle_t)mutex, portMAX_DELAY) == pdPASS) ? 0 : -1; +} + +int usb_osal_mutex_give(usb_osal_mutex_t mutex) +{ + return (xSemaphoreGive((SemaphoreHandle_t)mutex) == pdPASS) ? 0 : -1; +} + +uint32_t usb_osal_enter_critical_section(void) +{ + taskENTER_CRITICAL(); + return 1; +} + +void usb_osal_leave_critical_section(uint32_t flag) +{ + taskEXIT_CRITICAL(); +} + +void usb_osal_delay_ms(uint32_t delay) +{ + vTaskDelay(delay); +} \ No newline at end of file diff --git a/osal/usb_osal_rtthread.c b/osal/usb_osal_rtthread.c new file mode 100644 index 00000000..30b075e0 --- /dev/null +++ b/osal/usb_osal_rtthread.c @@ -0,0 +1,88 @@ +/** + * @file usb_osal_rtthread.c + * @brief + * + * Copyright (c) 2022 sakumisu + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you 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. + * + */ +#include "usb_osal.h" +#include + +usb_osal_thread_t usb_osal_thread_create(const char *name, uint32_t stack_size, uint32_t prio, usb_thread_entry_t entry) +{ + rt_thread_t htask; + htask = rt_thread_create(name, entry, NULL, stack_size, prio, 10); + rt_thread_startup(htask); + return (usb_osal_thread_t)htask; +} + +usb_osal_sem_t usb_osal_sem_create(uint32_t initial_count) +{ + static uint16_t index = 0; + char str[16]; + + sprintf(str, "usbh_sem%u", index); + index++; + return (usb_osal_sem_t)rt_sem_create(str, initial_count, RT_IPC_FLAG_FIFO); +} + +int usb_osal_sem_take(usb_osal_sem_t sem) +{ + return (int)rt_sem_take(sem, RT_WAITING_FOREVER); +} + +int usb_osal_sem_give(usb_osal_sem_t sem) +{ + return (int)rt_sem_release(sem); +} + +usb_osal_mutex_t usb_osal_mutex_create(void) +{ + static uint16_t index = 0; + char str[16]; + + sprintf(str, "usbh_mutex%u", index); + index++; + return (usb_osal_mutex_t)rt_mutex_create(str, RT_IPC_FLAG_FIFO); +} + +int usb_osal_mutex_take(usb_osal_mutex_t mutex) +{ + return (int)rt_mutex_take(mutex, RT_WAITING_FOREVER); +} + +int usb_osal_mutex_give(usb_osal_mutex_t mutex) +{ + return (int)rt_mutex_release(mutex); +} + +uint32_t usb_osal_enter_critical_section(void) +{ + rt_enter_critical(); + return 1; +} + +void usb_osal_leave_critical_section(uint32_t flag) +{ + rt_exit_critical(); +} + +void usb_osal_delay_ms(uint32_t delay) +{ + rt_thread_mdelay(delay); +} \ No newline at end of file