From d3764db892e4e97b6d599bda867eaa49b0ad4786 Mon Sep 17 00:00:00 2001 From: liruncong Date: Tue, 23 Oct 2018 20:03:18 +0800 Subject: 解决以下问题: 1) usb驱动中时间未与RT_TICK_PER_SECOND关联问题,RT_TICK_PER_SECOND变化时,这些时间将发生变化 解决: 增加USB_TIMEOUT_BASIC/USB_TIMEOUT_LONG/USB_DEBOUNCE_TIME 2) 修正rt_usbh_detach_instance函数使用device->cfg_desc前未判断是否有效. 可导致abort 3) rt_usbh_hub_port_change增加device->port初始化,否则struct uhcd_ops相关函数无法直接得到端口号 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/include/drivers/usb_common.h | 13 +++++++++++++ components/drivers/include/drivers/usb_device.h | 9 +++++++++ 2 files changed, 22 insertions(+) (limited to 'components/drivers/include') diff --git a/components/drivers/include/drivers/usb_common.h b/components/drivers/include/drivers/usb_common.h index 7398fc3814..764dcd5612 100644 --- a/components/drivers/include/drivers/usb_common.h +++ b/components/drivers/include/drivers/usb_common.h @@ -216,6 +216,19 @@ extern "C" { #define HID_REPORT_ID_GENERAL 5 #define HID_REPORT_ID_MOUSE 6 +/* + * Time of usb timeout + */ +#ifndef USB_TIMEOUT_BASIC +#define USB_TIMEOUT_BASIC (RT_TICK_PER_SECOND) /* 1s */ +#endif +#ifndef USB_TIMEOUT_LONG +#define USB_TIMEOUT_LONG (RT_TICK_PER_SECOND * 5) /* 5s */ +#endif +#ifndf USB_DEBOUNCE_TIME +#define USB_DEBOUNCE_TIME (RT_TICK_PER_SECOND / 5) /* 0.2s */ +#endif + #define uswap_32(x) \ ((((x) & 0xff000000) >> 24) | \ (((x) & 0x00ff0000) >> 8) | \ diff --git a/components/drivers/include/drivers/usb_device.h b/components/drivers/include/drivers/usb_device.h index cb4122fdaf..26f0c4cf4a 100644 --- a/components/drivers/include/drivers/usb_device.h +++ b/components/drivers/include/drivers/usb_device.h @@ -17,6 +17,10 @@ #include #include "drivers/usb_common.h" +#ifdef __cplusplus +extern "C" { +#endif + /* Vendor ID */ #ifdef USB_VENDOR_ID #define _VENDOR_ID USB_VENDOR_ID @@ -453,4 +457,9 @@ rt_inline void usbd_os_proerty_descriptor_send(ufunction_t func, ureq_t setup, u } rt_usbd_ep0_write(func->device, data, setup->wLength); } + +#ifdef __cplusplus +} +#endif + #endif -- cgit v1.3.1 From d950ffc4859e8bfc0f3a562d5495e6e78cd5c46e Mon Sep 17 00:00:00 2001 From: zyh Date: Fri, 26 Oct 2018 11:41:54 +0800 Subject: [Components][USB]修复拼写错误 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/include/drivers/usb_common.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'components/drivers/include') diff --git a/components/drivers/include/drivers/usb_common.h b/components/drivers/include/drivers/usb_common.h index 764dcd5612..c26d83391e 100644 --- a/components/drivers/include/drivers/usb_common.h +++ b/components/drivers/include/drivers/usb_common.h @@ -225,7 +225,7 @@ extern "C" { #ifndef USB_TIMEOUT_LONG #define USB_TIMEOUT_LONG (RT_TICK_PER_SECOND * 5) /* 5s */ #endif -#ifndf USB_DEBOUNCE_TIME +#ifndef USB_DEBOUNCE_TIME #define USB_DEBOUNCE_TIME (RT_TICK_PER_SECOND / 5) /* 0.2s */ #endif -- cgit v1.3.1 From 2d59fe031038e94dd626f3b172653bcb9bab9a2d Mon Sep 17 00:00:00 2001 From: armink Date: Mon, 29 Oct 2018 09:49:12 +0800 Subject: [DeviceDriver] Add power management device driver. --- components/drivers/Kconfig | 4 + components/drivers/include/drivers/pm.h | 181 +++++++++++ components/drivers/include/rtdevice.h | 4 + components/drivers/pm/SConscript | 14 + components/drivers/pm/pm.c | 543 ++++++++++++++++++++++++++++++++ examples/pm/timer_app.c | 54 ++++ 6 files changed, 800 insertions(+) create mode 100644 components/drivers/include/drivers/pm.h create mode 100644 components/drivers/pm/SConscript create mode 100644 components/drivers/pm/pm.c create mode 100644 examples/pm/timer_app.c (limited to 'components/drivers/include') diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index f70ed4961f..382959a2e2 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -104,6 +104,10 @@ config RT_USING_MTD default n endif +config RT_USING_PM + bool "Using Power Management device drivers" + default n + config RT_USING_RTC bool "Using RTC device drivers" default n diff --git a/components/drivers/include/drivers/pm.h b/components/drivers/include/drivers/pm.h new file mode 100644 index 0000000000..94d4c8559b --- /dev/null +++ b/components/drivers/include/drivers/pm.h @@ -0,0 +1,181 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2012-06-02 Bernard the first version + * 2018-08-02 Tanek split run and sleep modes, support custom mode + */ + +#ifndef __PM_H__ +#define __PM_H__ + +#include + +#ifndef PM_HAS_CUSTOM_CONFIG + +/* All modes used for rt_pm_request() adn rt_pm_release() */ +enum +{ + /* run modes */ + PM_RUN_MODE_NORMAL = 0, + + /* sleep modes */ + PM_SLEEP_MODE_SLEEP, + PM_SLEEP_MODE_TIMER, + PM_SLEEP_MODE_SHUTDOWN, +}; + +/* The name of all modes used in the msh command "pm_dump" */ +#define PM_MODE_NAMES \ +{ \ + "Running Mode", \ + \ + "Sleep Mode", \ + "Timer Mode", \ + "Shutdown Mode", \ +} + +/* run mode count : 1 */ +#define PM_RUN_MODE_COUNT 1 +/* sleep mode count : 3 */ +#define PM_SLEEP_MODE_COUNT 3 + +/* support redefining default run mode */ +#ifndef PM_RUN_MODE_DEFAULT +#define PM_RUN_MODE_DEFAULT 0 +#endif + +/* support redefining default sleep mode */ +#ifndef PM_SLEEP_MODE_DEFAULT +#define PM_SLEEP_MODE_DEFAULT (PM_SLEEP_MODE_START) +#endif + +/* support redefining the minimum tick into sleep mode */ +#ifndef PM_MIN_ENTER_SLEEP_TICK +#define PM_MIN_ENTER_SLEEP_TICK (1) +#endif + +#else /* PM_HAS_CUSTOM_CONFIG */ + +#include + +#ifndef PM_RUN_MODE_COUNT +#error "You must defined PM_RUN_MODE_COUNT on pm_cfg.h" +#endif + +#ifndef PM_SLEEP_MODE_COUNT +#error "You must defined PM_SLEEP_MODE_COUNT on pm_cfg.h" +#endif + +#ifndef PM_MODE_DEFAULT +#error "You must defined PM_MODE_DEFAULT on pm_cfg.h" +#endif + +#ifndef PM_MODE_NAMES +#error "You must defined PM_MODE_NAMES on pm_cfg.h" +#endif + +#ifndef PM_RUN_MODE_DEFAULT +#error "You must defined PM_RUN_MODE_DEFAULT on pm_cfg.h" +#endif + +/* The default sleep mode(PM_SLEEP_MODE_DEFAULT) are not required. + * If the default mode is defined, it is requested once in rt_system_pm_init() + */ + +#endif /* PM_HAS_CUSTOM_CONFIG */ + +/* run mode must start at 0 */ +#define PM_RUN_MODE_START 0 +/* the values of the run modes and sleep mode must be consecutive */ +#define PM_SLEEP_MODE_START PM_RUN_MODE_COUNT +/* all mode count */ +#define PM_MODE_COUNT (PM_RUN_MODE_COUNT + PM_SLEEP_MODE_COUNT) +/* The last mode, will be request in rt_system_pm_init() */ +#define PM_MODE_MAX (PM_RUN_MODE_COUNT + PM_SLEEP_MODE_COUNT - 1) + +#if PM_MODE_COUNT > 32 +#error "The number of modes cannot exceed 32" +#endif + +/** + * device control flag to request or release power + */ +#define RT_PM_DEVICE_CTRL_REQUEST 0x01 +#define RT_PM_DEVICE_CTRL_RELEASE 0x02 + +struct rt_pm; + +/** + * low power mode operations + */ +struct rt_pm_ops +{ + void (*enter)(struct rt_pm *pm); + void (*exit)(struct rt_pm *pm); + +#if PM_RUN_MODE_COUNT > 1 + void (*frequency_change)(struct rt_pm *pm, rt_uint32_t frequency); +#endif + + void (*timer_start)(struct rt_pm *pm, rt_uint32_t timeout); + void (*timer_stop)(struct rt_pm *pm); + rt_tick_t (*timer_get_tick)(struct rt_pm *pm); +}; + +struct rt_device_pm_ops +{ +#if PM_RUN_MODE_COUNT > 1 + void (*frequency_change)(const struct rt_device* device); +#endif + + void (*suspend)(const struct rt_device* device); + void (*resume) (const struct rt_device* device); +}; + +struct rt_device_pm +{ + const struct rt_device* device; + const struct rt_device_pm_ops* ops; +}; + +/** + * power management + */ +struct rt_pm +{ + struct rt_device parent; + + /* modes */ + rt_uint8_t modes[PM_MODE_COUNT]; + rt_uint8_t current_mode; /* current pm mode */ + rt_uint8_t exit_count; + + /* the list of device, which has PM feature */ + rt_uint8_t device_pm_number; + struct rt_device_pm* device_pm; + struct rt_semaphore device_lock; + + /* if the mode has timer, the corresponding bit is 1*/ + rt_uint32_t timer_mask; + + const struct rt_pm_ops *ops; +}; + +void rt_pm_enter(void); +void rt_pm_exit(void); + +void rt_pm_request(rt_ubase_t mode); +void rt_pm_release(rt_ubase_t mode); + +void rt_pm_register_device(struct rt_device* device, const struct rt_device_pm_ops* ops); +void rt_pm_unregister_device(struct rt_device* device); + +void rt_system_pm_init(const struct rt_pm_ops *ops, + rt_uint8_t timer_mask, + void *user_data); + +#endif /* __PM_H__ */ diff --git a/components/drivers/include/rtdevice.h b/components/drivers/include/rtdevice.h index e0499dcd20..1843432768 100644 --- a/components/drivers/include/rtdevice.h +++ b/components/drivers/include/rtdevice.h @@ -103,6 +103,10 @@ extern "C" { #include "drivers/rt_drv_pwm.h" #endif +#ifdef RT_USING_PM +#include "drivers/pm.h" +#endif + #ifdef RT_USING_WIFI #include "drivers/wlan.h" #endif diff --git a/components/drivers/pm/SConscript b/components/drivers/pm/SConscript new file mode 100644 index 0000000000..a94fc11f57 --- /dev/null +++ b/components/drivers/pm/SConscript @@ -0,0 +1,14 @@ +from building import * + +cwd = GetCurrentDir() +src = [] +CPPPATH = [cwd + '/../include'] +group = [] + +if GetDepend(['RT_USING_PM']): + src = src + ['pm.c'] + +if len(src): + group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH) + +Return('group') diff --git a/components/drivers/pm/pm.c b/components/drivers/pm/pm.c new file mode 100644 index 0000000000..bd8a0a5e95 --- /dev/null +++ b/components/drivers/pm/pm.c @@ -0,0 +1,543 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2012-06-02 Bernard the first version + * 2018-08-02 Tanek split run and sleep modes, support custom mode + */ + +#include +#include +#include + +#ifdef RT_USING_PM + +static struct rt_pm _pm; + +/** + * This function will suspend all registered devices + */ +static void _pm_device_suspend(void) +{ + int index; + + for (index = 0; index < _pm.device_pm_number; index++) + { + if (_pm.device_pm[index].ops->suspend != RT_NULL) + { + _pm.device_pm[index].ops->suspend(_pm.device_pm[index].device); + } + } +} + +/** + * This function will resume all registered devices + */ +static void _pm_device_resume(void) +{ + int index; + + for (index = 0; index < _pm.device_pm_number; index++) + { + if (_pm.device_pm[index].ops->resume != RT_NULL) + { + _pm.device_pm[index].ops->resume(_pm.device_pm[index].device); + } + } +} + +#if PM_RUN_MODE_COUNT > 1 +/** + * This function will update the frequency of all registered devices + */ +static void _pm_device_frequency_change(void) +{ + rt_uint32_t index; + + /* make the frequency change */ + for (index = 0; index < _pm.device_pm_number; index ++) + { + if (_pm.device_pm[index].ops->frequency_change != RT_NULL) + _pm.device_pm[index].ops->frequency_change(_pm.device_pm[index].device); + } +} +#endif + +/** + * This function will enter corresponding power mode. + */ +void rt_pm_enter(void) +{ + rt_ubase_t level; + struct rt_pm *pm; + rt_uint32_t index; + rt_tick_t timeout_tick; + + pm = &_pm; + + /* disable interrupt before check run modes */ + level = rt_hw_interrupt_disable(); + /* check each run mode, and decide to swithc to run mode or sleep mode */ + for (index = 0; index < PM_RUN_MODE_COUNT; index++) + { + if (pm->modes[index]) + { + if (index > pm->current_mode) + { + pm->ops->exit(pm); + pm->current_mode = index; + pm->ops->enter(pm); +#if PM_RUN_MODE_COUNT > 1 + pm->ops->frequency_change(pm, 0); + _pm_device_frequency_change(); +#endif + } + + rt_hw_interrupt_enable(level); + /* The current mode is run mode, no need to check sleep mode */ + return ; + } + } + /* enable interrupt after check run modes */ + rt_hw_interrupt_enable(level); + + level = rt_hw_interrupt_disable(); + /* check each sleep mode to decide which mode can system sleep. */ + for (index = PM_SLEEP_MODE_START; index < PM_SLEEP_MODE_START + PM_SLEEP_MODE_COUNT; index++) + { + if (pm->modes[index]) + { + /* let mcu sleep when system is idle */ + + /* run mode to sleep mode */ + if (pm->current_mode < PM_SLEEP_MODE_START) + { + /* exit run mode */ + pm->ops->exit(pm); + } + + /* set current power mode */ + pm->current_mode = index; + pm->exit_count = 1; + + /* suspend all of devices with PM feature */ + _pm_device_suspend(); + + /* should start pm timer */ + if (pm->timer_mask & (1 << index)) + { + /* get next os tick */ + timeout_tick = rt_timer_next_timeout_tick(); + if (timeout_tick != RT_TICK_MAX) + { + timeout_tick -= rt_tick_get(); + +#if defined(PM_MIN_ENTER_SLEEP_TICK) && PM_MIN_ENTER_SLEEP_TICK > 0 + if (timeout_tick < PM_MIN_ENTER_SLEEP_TICK) + { + rt_hw_interrupt_enable(level); + /* limit the minimum time to enter timer sleep mode */ + return ; + } +#endif + } + /* startup pm timer */ + pm->ops->timer_start(pm, timeout_tick); + } + + /* enter sleep and wait to be waken up */ + pm->ops->enter(pm); + + rt_hw_interrupt_enable(level); + + /* exit from low power mode */ + rt_pm_exit(); + + return ; + } + } + + rt_hw_interrupt_enable(level); +} + +/** + * This function exits from sleep mode. + */ +void rt_pm_exit(void) +{ + rt_ubase_t level; + struct rt_pm *pm; + rt_tick_t delta_tick; + + pm = &_pm; + + level = rt_hw_interrupt_disable(); + + if (pm->exit_count) + { + pm->exit_count = 0; + + if (pm->current_mode >= PM_SLEEP_MODE_START) + { + /* sleep mode with timer */ + if (pm->timer_mask & (1 << pm->current_mode)) + { + /* get the tick of pm timer */ + delta_tick = pm->ops->timer_get_tick(pm); + + /* stop pm timer */ + pm->ops->timer_stop(pm); + + if (delta_tick) + { + /* adjust OS tick */ + rt_tick_set(rt_tick_get() + delta_tick); + /* check system timer */ + rt_timer_check(); + } + } + + /* exit from sleep mode */ + pm->ops->exit(pm); + /* resume the device with PM feature */ + _pm_device_resume(); + } + } + + rt_hw_interrupt_enable(level); +} + +/** + * Upper application or device driver requests the system + * stall in corresponding power mode. + * + * @param parameter the parameter of run mode or sleep mode + */ +void rt_pm_request(rt_ubase_t mode) +{ + rt_ubase_t level; + struct rt_pm *pm; + + pm = &_pm; + + if (mode > PM_MODE_MAX) + return; + + level = rt_hw_interrupt_disable(); + + /* update pm modes table */ + pm->modes[mode] ++; + + /* request higter mode with a smaller mode value*/ + if (mode < pm->current_mode) + { + /* the old current mode is RUN mode, need to all pm->ops->exit(), + * if not, it has already called in rt_pm_exit() + */ + if (pm->current_mode < PM_SLEEP_MODE_START) + pm->ops->exit(pm); + + /* update current mode */ + pm->current_mode = mode; + + /* current mode is higher run mode */ + if (mode < PM_SLEEP_MODE_START) + { + /* enter run mode */ + pm->ops->enter(pm); +#if PM_RUN_MODE_COUNT > 1 + /* frequency change */ + pm->ops->frequency_change(pm, 0); + _pm_device_frequency_change(); +#endif + } + else + { + /* do nothing when request higher sleep mode, + * and swithc to new sleep mode in rt_pm_enter() + */ + } + } + + rt_hw_interrupt_enable(level); +} + +/** + * Upper application or device driver releases the stall + * of corresponding power mode. + * + * @param parameter the parameter of run mode or sleep mode + * + */ +void rt_pm_release(rt_ubase_t mode) +{ + rt_ubase_t level; + struct rt_pm *pm; + + pm = &_pm; + + if (mode > PM_MODE_MAX) + return; + + level = rt_hw_interrupt_disable(); + + if (pm->modes[mode] > 0) + pm->modes[mode] --; + + rt_hw_interrupt_enable(level); +} + +/** + * Register a device with PM feature + * + * @param device the device with PM feature + * @param ops the PM ops for device + */ +void rt_pm_register_device(struct rt_device *device, const struct rt_device_pm_ops *ops) +{ + rt_ubase_t level; + struct rt_device_pm *device_pm; + + RT_DEBUG_NOT_IN_INTERRUPT; + + level = rt_hw_interrupt_disable(); + + device_pm = (struct rt_device_pm *)RT_KERNEL_REALLOC(_pm.device_pm, + (_pm.device_pm_number + 1) * sizeof(struct rt_device_pm)); + if (device_pm != RT_NULL) + { + _pm.device_pm = device_pm; + _pm.device_pm[_pm.device_pm_number].device = device; + _pm.device_pm[_pm.device_pm_number].ops = ops; + _pm.device_pm_number += 1; + } + + rt_sem_release(&(_pm.device_lock)); + + rt_hw_interrupt_enable(level); +} + +/** + * Unregister device from PM manager. + * + * @param device the device with PM feature + */ +void rt_pm_unregister_device(struct rt_device *device) +{ + rt_ubase_t level; + rt_uint32_t index; + RT_DEBUG_NOT_IN_INTERRUPT; + + level = rt_hw_interrupt_disable(); + + for (index = 0; index < _pm.device_pm_number; index ++) + { + if (_pm.device_pm[index].device == device) + { + /* remove current entry */ + for (; index < _pm.device_pm_number - 1; index ++) + { + _pm.device_pm[index] = _pm.device_pm[index + 1]; + } + + _pm.device_pm[_pm.device_pm_number - 1].device = RT_NULL; + _pm.device_pm[_pm.device_pm_number - 1].ops = RT_NULL; + + _pm.device_pm_number -= 1; + /* break out and not touch memory */ + break; + } + } + + rt_hw_interrupt_enable(level); +} + +/** + * RT-Thread device interface for PM device + */ +static rt_size_t _rt_pm_device_read(rt_device_t dev, + rt_off_t pos, + void *buffer, + rt_size_t size) +{ + struct rt_pm *pm; + rt_size_t length; + + length = 0; + pm = (struct rt_pm *)dev; + RT_ASSERT(pm != RT_NULL); + + if (pos <= PM_MODE_MAX) + { + int mode; + + mode = pm->modes[pos]; + length = rt_snprintf(buffer, size, "%d", mode); + } + + return length; +} + +static rt_size_t _rt_pm_device_write(rt_device_t dev, + rt_off_t pos, + const void *buffer, + rt_size_t size) +{ + unsigned char request; + + if (size) + { + /* get request */ + request = *(unsigned char *)buffer; + if (request == '1') + { + rt_pm_request(pos); + } + else if (request == '0') + { + rt_pm_release(pos); + } + } + + return 1; +} + +static rt_err_t _rt_pm_device_control(rt_device_t dev, + int cmd, + void *args) +{ + rt_uint32_t mode; + + switch (cmd) + { + case RT_PM_DEVICE_CTRL_REQUEST: + mode = (rt_uint32_t)args; + rt_pm_request(mode); + break; + + case RT_PM_DEVICE_CTRL_RELEASE: + mode = (rt_uint32_t)args; + rt_pm_release(mode); + break; + } + + return RT_EOK; +} + +/** + * This function will initialize power management. + * + * @param ops the PM operations. + * @param timer_mask indicates which mode has timer feature. + * @param user_data user data + */ +void rt_system_pm_init(const struct rt_pm_ops *ops, + rt_uint8_t timer_mask, + void *user_data) +{ + struct rt_device *device; + struct rt_pm *pm; + + pm = &_pm; + device = &(_pm.parent); + + device->type = RT_Device_Class_PM; + device->rx_indicate = RT_NULL; + device->tx_complete = RT_NULL; + + device->init = RT_NULL; + device->open = RT_NULL; + device->close = RT_NULL; + device->read = _rt_pm_device_read; + device->write = _rt_pm_device_write; + device->control = _rt_pm_device_control; + device->user_data = user_data; + + /* register PM device to the system */ + rt_device_register(device, "pm", RT_DEVICE_FLAG_RDWR); + + /* todo : add to kernel source code */ + rt_thread_idle_sethook(rt_pm_enter); + + rt_memset(pm->modes, 0, sizeof(pm->modes)); + pm->current_mode = PM_RUN_MODE_DEFAULT; + + pm->timer_mask = timer_mask; + + pm->ops = ops; + + pm->device_pm = RT_NULL; + pm->device_pm_number = 0; + + /* initialize semaphore */ + rt_sem_init(&(pm->device_lock), "pm", 1, RT_IPC_FLAG_FIFO); + + /* request in default running mode */ + rt_pm_request(PM_RUN_MODE_DEFAULT); + +#ifdef PM_SLEEP_MODE_DEFAULT + /* request in default sleep mode */ + rt_pm_request(PM_SLEEP_MODE_DEFAULT); +#endif + + /* must hold on deep shutdown mode */ + rt_pm_request(PM_MODE_MAX); +} + +#ifdef RT_USING_FINSH +#include + +static void rt_pm_release_mode(int argc, char **argv) +{ + int mode = 0; + if (argc >= 2) + { + mode = atoi(argv[1]); + } + + rt_pm_release(mode); +} +MSH_CMD_EXPORT_ALIAS(rt_pm_release_mode, pm_release, release power management mode); + +static void rt_pm_request_mode(int argc, char **argv) +{ + int mode = 0; + if (argc >= 2) + { + mode = atoi(argv[1]); + } + + rt_pm_request(mode); +} +MSH_CMD_EXPORT_ALIAS(rt_pm_request_mode, pm_request, request power management mode); + +static void rt_pm_dump_status(void) +{ + static const char *pm_str[] = PM_MODE_NAMES; + rt_uint32_t index; + struct rt_pm *pm; + + pm = &_pm; + + rt_kprintf("| Power Management Mode | Counter | Timer |\n"); + rt_kprintf("+-----------------------+---------+-------+\n"); + for (index = 0; index <= PM_MODE_MAX; index ++) + { + int has_timer = 0; + if (pm->timer_mask & (1 << index)) + has_timer = 1; + + rt_kprintf("| %021s | %7d | %5d |\n", pm_str[index], pm->modes[index], has_timer); + } + rt_kprintf("+-----------------------+---------+-------+\n"); + + rt_kprintf("pm current mode: %s\n", pm_str[pm->current_mode]); +} +FINSH_FUNCTION_EXPORT_ALIAS(rt_pm_dump_status, pm_dump, dump power management status); +MSH_CMD_EXPORT_ALIAS(rt_pm_dump_status, pm_dump, dump power management status); +#endif + +#endif /* RT_USING_PM */ diff --git a/examples/pm/timer_app.c b/examples/pm/timer_app.c new file mode 100644 index 0000000000..73faf52d0a --- /dev/null +++ b/examples/pm/timer_app.c @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-08-07 Tanek first implementation + */ + +#include +#include +#include + +#ifndef RT_USING_TIMER_SOFT + #error "Please enable soft timer feature!" +#endif + +#define TIMER_APP_DEFAULT_TICK (RT_TICK_PER_SECOND * 2) + +#ifdef RT_USING_PM + +static rt_timer_t timer1; + +static void _timeout_entry(void *parameter) +{ + rt_kprintf("current tick: %ld\n", rt_tick_get()); +} + +static int timer_app_init(void) +{ + timer1 = rt_timer_create("timer_app", + _timeout_entry, + RT_NULL, + TIMER_APP_DEFAULT_TICK, + RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER); + if (timer1 != RT_NULL) + { + rt_timer_start(timer1); + + /* keep in timer mode */ + rt_pm_request(PM_SLEEP_MODE_TIMER); + + return 0; + } + else + { + return -1; + } +} +INIT_EXPORT_APP(timer_app_init); + +#endif /* RT_USING_PM */ + -- cgit v1.3.1 From 049944529fe8bda17235c0185b0a6ac1b8ae7598 Mon Sep 17 00:00:00 2001 From: "chenchaoqun@rt-thread.com" Date: Mon, 19 Nov 2018 19:04:44 +0800 Subject: [add] adc framework --- components/drivers/Kconfig | 4 + components/drivers/include/drivers/adc.h | 42 ++++++ components/drivers/include/rtdevice.h | 4 + components/drivers/misc/SConscript | 5 +- components/drivers/misc/adc.c | 219 +++++++++++++++++++++++++++++++ 5 files changed, 273 insertions(+), 1 deletion(-) create mode 100644 components/drivers/include/drivers/adc.h create mode 100644 components/drivers/misc/adc.c (limited to 'components/drivers/include') diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index f3b814fa08..94af352410 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -72,6 +72,10 @@ config RT_USING_PIN bool "Using generic GPIO device drivers" default y +config RT_USING_ADC + bool "Using ADC device drivers" + default n + config RT_USING_PWM bool "Using PWM device drivers" default n diff --git a/components/drivers/include/drivers/adc.h b/components/drivers/include/drivers/adc.h new file mode 100644 index 0000000000..8e3498e791 --- /dev/null +++ b/components/drivers/include/drivers/adc.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-05-07 aozima the first version + * 2018-11-16 Ernest Chen add finsh command and update adc function + */ + +#ifndef __ADC_H__ +#define __ADC_H__ +#include + +struct rt_adc_device; +struct rt_adc_ops +{ + rt_err_t (*enabled)(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled); + rt_err_t (*convert)(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value); +}; + +struct rt_adc_device +{ + struct rt_device parent; + const struct rt_adc_ops *ops; +}; +typedef struct rt_adc_device *rt_adc_device_t; + +typedef enum +{ + RT_ADC_CMD_ENABLE, + RT_ADC_CMD_DISABLE, +} rt_adc_cmd_t; + +rt_err_t rt_hw_adc_register(rt_adc_device_t adc,const char *name, const struct rt_adc_ops *ops, const void *user_data); + +rt_uint32_t rt_adc_read(rt_adc_device_t dev, rt_uint32_t channel); +rt_err_t rt_adc_enable(rt_adc_device_t dev, rt_uint32_t channel); +rt_err_t rt_adc_disable(rt_adc_device_t dev, rt_uint32_t channel); + +#endif /* __ADC_H__ */ diff --git a/components/drivers/include/rtdevice.h b/components/drivers/include/rtdevice.h index 1843432768..d59b40cc47 100644 --- a/components/drivers/include/rtdevice.h +++ b/components/drivers/include/rtdevice.h @@ -99,6 +99,10 @@ extern "C" { #include "drivers/cputime.h" #endif +#ifdef RT_USING_ADC +#include "drivers/adc.h" +#endif + #ifdef RT_USING_PWM #include "drivers/rt_drv_pwm.h" #endif diff --git a/components/drivers/misc/SConscript b/components/drivers/misc/SConscript index 4cc5f80084..c45ba524c5 100644 --- a/components/drivers/misc/SConscript +++ b/components/drivers/misc/SConscript @@ -7,7 +7,10 @@ group = [] if GetDepend(['RT_USING_PIN']): src = src + ['pin.c'] - + +if GetDepend(['RT_USING_ADC']): + src = src + ['adc.c'] + if GetDepend(['RT_USING_PWM']): src = src + ['rt_drv_pwm.c'] diff --git a/components/drivers/misc/adc.c b/components/drivers/misc/adc.c new file mode 100644 index 0000000000..aa3a3b7801 --- /dev/null +++ b/components/drivers/misc/adc.c @@ -0,0 +1,219 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-05-07 aozima the first version + * 2018-11-16 Ernest Chen add finsh command and update adc function + */ + +#include +#include + +#include +#include + +#define DBG_ENABLE +#define DBG_SECTION_NAME "adc" +#define DBG_LEVEL DBG_INFO +#define DBG_COLOR +#include + +static rt_size_t _adc_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size) +{ + rt_err_t result = RT_EOK; + rt_size_t i; + struct rt_adc_device *adc = (struct rt_adc_device *)dev; + rt_uint32_t *value = (rt_uint32_t *)buffer; + + for (i = 0; i < size; i += sizeof(int)) + { + result = adc->ops->convert(adc, pos + i, value); + if (result != RT_EOK) + { + return 0; + } + value++; + } + + return i; +} + +static rt_err_t _adc_control(rt_device_t dev, int cmd, void *args) +{ + rt_err_t result = RT_EOK; + rt_adc_device_t adc = (struct rt_adc_device *)dev; + + if (adc->ops->enabled != RT_NULL) + { + return -RT_ENOSYS; + } + if (cmd == RT_ADC_CMD_ENABLE) + { + result = adc->ops->enabled(adc, (rt_uint32_t)args, RT_TRUE); + } + else if (cmd == RT_ADC_CMD_DISABLE) + { + result = adc->ops->enabled(adc, (rt_uint32_t)args, RT_FALSE); + } + + return result; +} + +rt_err_t rt_hw_adc_register(rt_adc_device_t device, const char *name, const struct rt_adc_ops *ops, const void *user_data) +{ + rt_err_t result = RT_EOK; + RT_ASSERT(ops != RT_NULL && ops->convert != RT_NULL); + + device->parent.type = RT_Device_Class_Miscellaneous; + device->parent.init = RT_NULL; + device->parent.open = RT_NULL; + device->parent.close = RT_NULL; + device->parent.read = _adc_read; + device->parent.write = RT_NULL; + device->parent.control = _adc_control; + + device->ops = ops; + device->parent.user_data = (void *)user_data; + + result = rt_device_register(&device->parent, name, RT_DEVICE_FLAG_RDWR); + + return result; +} + +rt_uint32_t rt_adc_read(rt_adc_device_t dev, rt_uint32_t channel) +{ + rt_uint32_t value; + + RT_ASSERT(dev); + + dev->ops->convert(dev, channel, &value); + + return value; +} + +rt_err_t rt_adc_enable(rt_adc_device_t dev, rt_uint32_t channel) +{ + rt_err_t result = RT_EOK; + + RT_ASSERT(dev); + if (dev->ops->enabled != RT_NULL) + { + result = dev->ops->enabled(dev, channel, RT_TRUE); + } + else + { + result = -RT_ENOSYS; + } + + return result; +} + +rt_err_t rt_adc_disable(rt_adc_device_t dev, rt_uint32_t channel) +{ + rt_err_t result = RT_EOK; + + RT_ASSERT(dev); + if (dev->ops->enabled != RT_NULL) + { + result = dev->ops->enabled(dev, channel, RT_FALSE); + } + else + { + result = -RT_ENOSYS; + } + + return result; +} + +#ifdef FINSH_USING_MSH + +static int adc(int argc, char **argv) +{ + int value = 0; + int result = RT_EOK; + static rt_adc_device_t adc_device = RT_NULL; + char *result_str; + + if (argc > 1) + { + if (!strcmp(argv[1], "probe")) + { + if (argc == 3) + { + adc_device = (rt_adc_device_t)rt_device_find(argv[2]); + result_str = (adc_device == RT_NULL) ? "failure" : "success"; + rt_kprintf("probe %s %s \n", argv[2], result_str); + } + else + { + rt_kprintf("adc probe - probe adc by name\n"); + } + } + else + { + if (adc_device == RT_NULL) + { + rt_kprintf("Please using 'adc probe ' first\n"); + return -RT_ERROR; + } + if (!strcmp(argv[1], "enable")) + { + if (argc == 3) + { + result = rt_adc_enable(adc_device, atoi(argv[2])); + result_str = (result == RT_EOK) ? "success" : "failure"; + rt_kprintf("%s channel %d enables %s \n", adc_device->parent.parent.name, atoi(argv[2]), result_str); + } + else + { + rt_kprintf("adc enable - enable adc channel\n"); + } + } + else if (!strcmp(argv[1], "read")) + { + if (argc == 3) + { + value = rt_adc_read(adc_device, atoi(argv[2])); + rt_kprintf("%s channel %d read value is 0x%08X \n", adc_device->parent.parent.name, atoi(argv[2]), value); + } + else + { + rt_kprintf("adc read - read adc value on the channel\n"); + } + } + else if (!strcmp(argv[1], "disable")) + { + if (argc == 3) + { + result = rt_adc_disable(adc_device, atoi(argv[2])); + result_str = (result == RT_EOK) ? "success" : "failure"; + rt_kprintf("%s channel %d disable %s \n", adc_device->parent.parent.name, atoi(argv[2]), result_str); + } + else + { + rt_kprintf("adc disable - disable adc channel\n"); + } + } + else + { + rt_kprintf("Unknown command. Please enter 'adc' for help\n"); + } + } + } + else + { + rt_kprintf("Usage: \n"); + rt_kprintf("adc probe - probe adc by name\n"); + rt_kprintf("adc read - read adc value on the channel\n"); + rt_kprintf("adc disable - disable adc channel\n"); + rt_kprintf("adc enable - enable adc channel\n"); + result = -RT_ERROR; + } + return RT_EOK; +} +MSH_CMD_EXPORT(adc, adc function); + +#endif /* FINSH_USING_MSH */ -- cgit v1.3.1 From 257ce972d0d54cb311274f04c270630e5c015288 Mon Sep 17 00:00:00 2001 From: zylx Date: Thu, 22 Nov 2018 11:19:08 +0800 Subject: [DeviceDrivers][spi]:add qspi support --- components/drivers/include/drivers/spi.h | 108 +++++++++++++ components/drivers/spi/qspi_core.c | 265 +++++++++++++++++++++++++++++++ components/drivers/spi/spi_core.c | 2 + 3 files changed, 375 insertions(+) create mode 100644 components/drivers/spi/qspi_core.c (limited to 'components/drivers/include') diff --git a/components/drivers/include/drivers/spi.h b/components/drivers/include/drivers/spi.h index 84b589ace0..7394710581 100644 --- a/components/drivers/include/drivers/spi.h +++ b/components/drivers/include/drivers/spi.h @@ -45,6 +45,9 @@ extern "C"{ #define RT_SPI_MODE_MASK (RT_SPI_CPHA | RT_SPI_CPOL | RT_SPI_MSB) +#define RT_SPI_BUS_MODE_SPI (1<<0) +#define RT_SPI_BUS_MODE_QSPI (1<<1) + #define RT_SPI_CS_HIGH (1<<4) /* Chipselect active high */ #define RT_SPI_NO_CS (1<<5) /* No chipselect */ #define RT_SPI_3WIRE (1<<6) /* SI/SO pin shared */ @@ -80,6 +83,7 @@ struct rt_spi_ops; struct rt_spi_bus { struct rt_device parent; + rt_uint8_t mode; const struct rt_spi_ops *ops; struct rt_mutex lock; @@ -106,6 +110,55 @@ struct rt_spi_device struct rt_spi_configuration config; void *user_data; }; + +struct rt_qspi_message +{ + struct rt_spi_message parent; + + /* instruction stage */ + struct + { + rt_uint8_t content; + rt_uint8_t qspi_lines; + } instruction; + + /* address and alternate_bytes stage */ + struct + { + rt_uint32_t content; + rt_uint8_t size; + rt_uint8_t qspi_lines; + } address, alternate_bytes; + + /* dummy_cycles stage */ + rt_uint32_t dummy_cycles; + + /* number of lines in qspi data stage, the other configuration items are in parent */ + rt_uint8_t qspi_data_lines; +}; + +struct rt_qspi_configuration +{ + struct rt_spi_configuration parent; + /* The size of medium */ + rt_uint32_t medium_size; + /* double data rate mode */ + rt_uint8_t ddr_mode; + /* the number of lines connected to the hardware */ + rt_uint8_t qspi_hw_lines; +}; + +struct rt_qspi_device +{ + struct rt_spi_device parent; + + struct rt_qspi_configuration config; + + void (*enter_qspi_mode)(struct rt_qspi_device *device); + + void (*exit_qspi_mode)(struct rt_qspi_device *device); +}; + #define SPI_DEVICE(dev) ((struct rt_spi_device *)(dev)) /* register a SPI bus */ @@ -255,6 +308,61 @@ rt_inline void rt_spi_message_append(struct rt_spi_message *list, message->next = RT_NULL; } +/** + * This function can set configuration on QSPI device. + * + * @param device the QSPI device attached to QSPI bus. + * @param cfg the configuration pointer. + * + * @return the actual length of transmitted. + */ +rt_err_t rt_qspi_configure(struct rt_qspi_device *device, struct rt_qspi_configuration *cfg); + +/** + * This function can register a SPI bus for QSPI mode. + * + * @param bus the SPI bus for QSPI mode. + * @param name The name of the spi bus. + * @param ops the SPI bus instance to be registered. + * + * @return the actual length of transmitted. + */ +rt_err_t rt_qspi_bus_register(struct rt_spi_bus *bus, const char *name, const struct rt_spi_ops *ops); + +/** + * This function transmits data to QSPI device. + * + * @param device the QSPI device attached to QSPI bus. + * @param message the message pointer. + * + * @return the actual length of transmitted. + */ +rt_size_t rt_qspi_transfer_message(struct rt_qspi_device *device, struct rt_qspi_message *message); + +/** + * This function can send data then receive data from QSPI device + * + * @param device the QSPI device attached to QSPI bus. + * @param send_buf the buffer to be transmitted to QSPI device. + * @param send_length the number of data to be transmitted. + * @param recv_buf the buffer to be recivied from QSPI device. + * @param recv_length the data to be recivied. + * + * @return the status of transmit. + */ +rt_err_t rt_qspi_send_then_recv(struct rt_qspi_device *device, const void *send_buf, rt_size_t send_length,void *recv_buf, rt_size_t recv_length); + +/** + * This function can send data to QSPI device + * + * @param device the QSPI device attached to QSPI bus. + * @param send_buf the buffer to be transmitted to QSPI device. + * @param send_length the number of data to be transmitted. + * + * @return the status of transmit. + */ +rt_err_t rt_qspi_send(struct rt_qspi_device *device, const void *send_buf, rt_size_t length); + #ifdef __cplusplus } #endif diff --git a/components/drivers/spi/qspi_core.c b/components/drivers/spi/qspi_core.c new file mode 100644 index 0000000000..7676764b59 --- /dev/null +++ b/components/drivers/spi/qspi_core.c @@ -0,0 +1,265 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-11-16 zylx first version. + */ + +#include + +rt_err_t rt_qspi_configure(struct rt_qspi_device *device, struct rt_qspi_configuration *cfg) +{ + RT_ASSERT(device != RT_NULL); + RT_ASSERT(cfg != RT_NULL); + + struct rt_qspi_device *qspi_device = (struct rt_qspi_device *)device; + rt_err_t result = RT_EOK; + + /* copy configuration items */ + qspi_device->config.parent.mode = cfg->parent.mode; + qspi_device->config.parent.max_hz = cfg->parent.max_hz; + qspi_device->config.parent.data_width = cfg->parent.data_width; + qspi_device->config.parent.reserved = cfg->parent.reserved; + qspi_device->config.medium_size = cfg->medium_size; + qspi_device->config.ddr_mode = cfg->ddr_mode; + qspi_device->config.qspi_hw_lines = cfg->qspi_hw_lines; + + result = rt_spi_configure(&device->parent, &cfg->parent); + + return result; +} + +rt_err_t rt_qspi_bus_register(struct rt_spi_bus *bus, const char *name, const struct rt_spi_ops *ops) +{ + rt_err_t result = RT_EOK; + + result = rt_spi_bus_register(bus, name, ops); + if(result == RT_EOK) + { + /* set SPI bus to qspi modes */ + bus->mode = RT_SPI_BUS_MODE_QSPI; + } + + return result; +} + +rt_size_t rt_qspi_transfer_message(struct rt_qspi_device *device, struct rt_qspi_message *message) +{ + rt_err_t result; + + RT_ASSERT(device != RT_NULL); + RT_ASSERT(message != RT_NULL); + + result = rt_mutex_take(&(device->parent.bus->lock), RT_WAITING_FOREVER); + if (result != RT_EOK) + { + rt_set_errno(-RT_EBUSY); + + return 0; + } + + /* reset errno */ + rt_set_errno(RT_EOK); + + /* configure SPI bus */ + if (device->parent.bus->owner != &device->parent) + { + /* not the same owner as current, re-configure SPI bus */ + result = device->parent.bus->ops->configure(&device->parent, &device->parent.config); + if (result == RT_EOK) + { + /* set SPI bus owner */ + device->parent.bus->owner = &device->parent; + } + else + { + /* configure SPI bus failed */ + rt_set_errno(-RT_EIO); + goto __exit; + } + } + + /* transmit each SPI message */ + + result = device->parent.bus->ops->xfer(&device->parent, &message->parent); + if (result == 0) + { + rt_set_errno(-RT_EIO); + } + +__exit: + /* release bus lock */ + rt_mutex_release(&(device->parent.bus->lock)); + + return result; +} + +rt_err_t rt_qspi_send_then_recv(struct rt_qspi_device *device, const void *send_buf, rt_size_t send_length, void *recv_buf, rt_size_t recv_length) +{ + RT_ASSERT(send_buf); + RT_ASSERT(recv_buf); + RT_ASSERT(send_length != 0); + + struct rt_qspi_message message; + unsigned char *ptr = (unsigned char *)send_buf; + rt_size_t count = 0; + rt_err_t result = 0; + + message.instruction.content = ptr[0]; + message.instruction.qspi_lines = 1; + count++; + + /* get address */ + if (send_length > 1) + { + if (device->config.medium_size > 0x1000000 && send_length >= 5) + { + /* medium size greater than 16Mb, address size is 4 Byte */ + message.address.content = (ptr[1] << 24) | (ptr[2] << 16) | (ptr[3] << 8) | (ptr[4]); + message.address.size = 32; + count += 4; + } + else if (send_length >= 4) + { + /* address size is 3 Byte */ + message.address.content = (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]); + message.address.size = 24; + count += 3; + } + else + { + return -RT_ERROR; + } + message.address.qspi_lines = 1; + } + else + { + /* no address stage */ + message.address.content = 0 ; + message.address.qspi_lines = 0; + message.address.size = 0; + } + + message.alternate_bytes.content = 0; + message.alternate_bytes.size = 0; + message.alternate_bytes.qspi_lines = 0; + + /* set dummy cycles */ + if (count != send_length) + { + message.dummy_cycles = (send_length - count) * 8; + + } + else + { + message.dummy_cycles = 0; + } + + /* set recv buf and recv size */ + message.parent.recv_buf = recv_buf; + message.parent.send_buf = RT_NULL; + message.parent.length = recv_length; + message.parent.cs_take = 1; + message.parent.cs_release = 1; + + message.qspi_data_lines = 1; + + result = rt_qspi_transfer_message(device, &message); + if (result == 0) + { + result = -RT_EIO; + } + else + { + result = recv_length; + } + + return result; +} + +rt_err_t rt_qspi_send(struct rt_qspi_device *device, const void *send_buf, rt_size_t length) +{ + RT_ASSERT(send_buf); + RT_ASSERT(length != 0); + + struct rt_qspi_message message; + char *ptr = (char *)send_buf; + rt_size_t count = 0; + rt_err_t result = 0; + + message.instruction.content = ptr[0]; + message.instruction.qspi_lines = 1; + count++; + + /* get address */ + if (length > 1) + { + if (device->config.medium_size > 0x1000000 && length >= 5) + { + /* medium size greater than 16Mb, address size is 4 Byte */ + message.address.content = (ptr[1] << 24) | (ptr[2] << 16) | (ptr[3] << 8) | (ptr[4]); + message.address.size = 32; + message.address.qspi_lines = 1; + count += 4; + } + else if (length >= 4) + { + /* address size is 3 Byte */ + message.address.content = (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]); + message.address.size = 24; + message.address.qspi_lines = 1; + count += 3; + } + else + { + return -RT_ERROR; + } + + } + else + { + /* no address stage */ + message.address.content = 0 ; + message.address.qspi_lines = 0; + message.address.size = 0; + } + + message.alternate_bytes.content = 0; + message.alternate_bytes.size = 0; + message.alternate_bytes.qspi_lines = 0; + + message.dummy_cycles = 0; + + /* determine if there is data to send */ + if (length - count > 0) + { + message.qspi_data_lines = 1; + } + else + { + message.qspi_data_lines = 0; + + } + + /* set send buf and send size */ + message.parent.send_buf = ptr + count; + message.parent.recv_buf = RT_NULL; + message.parent.length = length - count; + message.parent.cs_take = 1; + message.parent.cs_release = 1; + + result = rt_qspi_transfer_message(device, &message); + if (result == 0) + { + result = -RT_EIO; + } + else + { + result = length; + } + + return result; +} diff --git a/components/drivers/spi/spi_core.c b/components/drivers/spi/spi_core.c index 9e404d7c94..64b0d2e496 100644 --- a/components/drivers/spi/spi_core.c +++ b/components/drivers/spi/spi_core.c @@ -34,6 +34,8 @@ rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus, bus->ops = ops; /* initialize owner */ bus->owner = RT_NULL; + /* set bus mode */ + bus->mode = RT_SPI_BUS_MODE_SPI; return RT_EOK; } -- cgit v1.3.1 From 74232f2de4cc551f0c3453675a0717ecfde406d4 Mon Sep 17 00:00:00 2001 From: armink Date: Thu, 22 Nov 2018 18:08:12 +0800 Subject: [DeviceDriver][rbb] Add some comments and rt_rbb_blk_size/rt_rbb_blk_buf API. Signed-off-by: armink --- components/drivers/include/ipc/ringblk_buf.h | 2 ++ components/drivers/src/ringblk_buf.c | 40 +++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) (limited to 'components/drivers/include') diff --git a/components/drivers/include/ipc/ringblk_buf.h b/components/drivers/include/ipc/ringblk_buf.h index 653fa2a60b..94fc2cfff3 100644 --- a/components/drivers/include/ipc/ringblk_buf.h +++ b/components/drivers/include/ipc/ringblk_buf.h @@ -84,6 +84,8 @@ rt_size_t rt_rbb_get_buf_size(rt_rbb_t rbb); rt_rbb_blk_t rt_rbb_blk_alloc(rt_rbb_t rbb, rt_size_t blk_size); void rt_rbb_blk_put(rt_rbb_blk_t block); rt_rbb_blk_t rt_rbb_blk_get(rt_rbb_t rbb); +rt_size_t rt_rbb_blk_size(rt_rbb_blk_t block); +rt_uint8_t *rt_rbb_blk_buf(rt_rbb_blk_t block); void rt_rbb_blk_free(rt_rbb_t rbb, rt_rbb_blk_t block); /* rbb block queue API */ diff --git a/components/drivers/src/ringblk_buf.c b/components/drivers/src/ringblk_buf.c index 1a74bf1d24..74ced674fe 100644 --- a/components/drivers/src/ringblk_buf.c +++ b/components/drivers/src/ringblk_buf.c @@ -18,8 +18,10 @@ * @param rbb ring block buffer object * @param buf buffer * @param buf_size buffer size - * @param block_set - * @param blk_max_num + * @param block_set block set + * @param blk_max_num max block number + * + * @note When your application need align access, please make the buffer address is aligned. */ void rt_rbb_init(rt_rbb_t rbb, rt_uint8_t *buf, rt_size_t buf_size, rt_rbb_blk_t block_set, rt_size_t blk_max_num) { @@ -123,6 +125,8 @@ static rt_rbb_blk_t find_empty_blk_in_set(rt_rbb_t rbb) * @param rbb ring block buffer object * @param blk_size block size * + * @note When your application need align access, please make the blk_szie is aligned. + * * @return != NULL: allocated block * NULL: allocate failed */ @@ -133,7 +137,7 @@ rt_rbb_blk_t rt_rbb_blk_alloc(rt_rbb_t rbb, rt_size_t blk_size) rt_rbb_blk_t head, tail, new = NULL; RT_ASSERT(rbb); - RT_ASSERT(blk_size < 1L << 24); + RT_ASSERT(blk_size < (1L << 24)); level = rt_hw_interrupt_disable(); @@ -277,6 +281,36 @@ __exit: } RTM_EXPORT(rt_rbb_blk_get); +/** + * return the block size + * + * @param block the block + * + * @return block size + */ +rt_size_t rt_rbb_blk_size(rt_rbb_blk_t block) +{ + RT_ASSERT(block); + + return block->size; +} +RTM_EXPORT(rt_rbb_blk_size); + +/** + * return the block buffer + * + * @param block the block + * + * @return block buffer + */ +rt_uint8_t *rt_rbb_blk_buf(rt_rbb_blk_t block) +{ + RT_ASSERT(block); + + return block->buf; +} +RTM_EXPORT(rt_rbb_blk_buf); + /** * free the block * -- cgit v1.3.1 From 15b2feb60dc4841a612625da3ab7f2bcb188a5a3 Mon Sep 17 00:00:00 2001 From: armink Date: Sat, 24 Nov 2018 15:02:53 +0800 Subject: [DeviceDriver] Update QSPI driver framework. --- components/drivers/Kconfig | 4 ++++ components/drivers/include/drivers/spi.h | 4 ++-- components/drivers/spi/SConscript | 2 +- components/drivers/spi/qspi_core.c | 2 +- 4 files changed, 8 insertions(+), 4 deletions(-) (limited to 'components/drivers/include') diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index 94af352410..8c54fff033 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -185,6 +185,10 @@ config RT_USING_SPI config RT_SFUD_USING_FLASH_INFO_TABLE bool "Using defined supported flash chip information table" default y + + config RT_SFUD_USING_QSPI + bool "Using QSPI mode support" + default n config RT_DEBUG_SFUD bool "Show more SFUD debug information" diff --git a/components/drivers/include/drivers/spi.h b/components/drivers/include/drivers/spi.h index 7394710581..04a65421a5 100644 --- a/components/drivers/include/drivers/spi.h +++ b/components/drivers/include/drivers/spi.h @@ -144,8 +144,8 @@ struct rt_qspi_configuration rt_uint32_t medium_size; /* double data rate mode */ rt_uint8_t ddr_mode; - /* the number of lines connected to the hardware */ - rt_uint8_t qspi_hw_lines; + /* the data lines max width which QSPI bus supported, such as 1, 2, 4 */ + rt_uint8_t qspi_dl_width ; }; struct rt_qspi_device diff --git a/components/drivers/spi/SConscript b/components/drivers/spi/SConscript index ee423c33db..0343fb8d13 100644 --- a/components/drivers/spi/SConscript +++ b/components/drivers/spi/SConscript @@ -2,7 +2,7 @@ from building import * import rtconfig cwd = GetCurrentDir() -src = ['spi_core.c', 'spi_dev.c'] +src = ['spi_core.c', 'spi_dev.c', 'qspi_core.c'] CPPPATH = [cwd, cwd + '/../include'] LOCAL_CCFLAGS = '' diff --git a/components/drivers/spi/qspi_core.c b/components/drivers/spi/qspi_core.c index 7676764b59..c9ff6a9637 100644 --- a/components/drivers/spi/qspi_core.c +++ b/components/drivers/spi/qspi_core.c @@ -25,7 +25,7 @@ rt_err_t rt_qspi_configure(struct rt_qspi_device *device, struct rt_qspi_configu qspi_device->config.parent.reserved = cfg->parent.reserved; qspi_device->config.medium_size = cfg->medium_size; qspi_device->config.ddr_mode = cfg->ddr_mode; - qspi_device->config.qspi_hw_lines = cfg->qspi_hw_lines; + qspi_device->config.qspi_dl_width = cfg->qspi_dl_width; result = rt_spi_configure(&device->parent, &cfg->parent); -- cgit v1.3.1 From a3e4d57a548b2eddd6bc730bf3b1084184073bd7 Mon Sep 17 00:00:00 2001 From: Owen Date: Fri, 30 Nov 2018 11:07:56 +0800 Subject: Update serial.h correct definition of struct serial_configure --- components/drivers/include/drivers/serial.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'components/drivers/include') diff --git a/components/drivers/include/drivers/serial.h b/components/drivers/include/drivers/serial.h index af5d626251..45095af63f 100644 --- a/components/drivers/include/drivers/serial.h +++ b/components/drivers/include/drivers/serial.h @@ -100,7 +100,7 @@ struct serial_configure rt_uint32_t bit_order :1; rt_uint32_t invert :1; rt_uint32_t bufsz :16; - rt_uint32_t reserved :4; + rt_uint32_t reserved :6; }; /* -- cgit v1.3.1 From c7ccb4f5323b577d080021f364fef66e9514b6be Mon Sep 17 00:00:00 2001 From: EvalZero Date: Sun, 17 Mar 2019 16:01:12 +0800 Subject: [components][drivers][workqueue] add system default workqueue and delayed work. --- components/drivers/Kconfig | 14 ++ components/drivers/include/ipc/workqueue.h | 52 ++++-- components/drivers/src/workqueue.c | 258 ++++++++++++++++++++++++----- 3 files changed, 269 insertions(+), 55 deletions(-) (limited to 'components/drivers/include') diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index 137402489a..39ed9c7709 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -8,6 +8,20 @@ if RT_USING_DEVICE_IPC config RT_PIPE_BUFSZ int "Set pipe buffer size" default 512 + + config RT_USING_SYSTEM_WORKQUEUE + bool "Using system default workqueue + default n + + if RT_USING_SYSTEM_WORKQUEUE + config RT_SYSTEM_WORKQUEUE_STACKSIZE + int "The stack size for system workqueue thread" + default 512 + + config RT_SYSTEM_WORKQUEUE_PRIORITY + int "The priority level of system workqueue thread" + default "23 + endif endif config RT_USING_SERIAL diff --git a/components/drivers/include/ipc/workqueue.h b/components/drivers/include/ipc/workqueue.h index 9e23e181b1..09dc4354a8 100644 --- a/components/drivers/include/ipc/workqueue.h +++ b/components/drivers/include/ipc/workqueue.h @@ -11,13 +11,26 @@ #include +enum +{ + RT_WORK_STATE_PENDING, /* Work item pending state */ +}; + +/** + * work type defitions + */ +enum +{ + RT_WORK_TYPE_DELAYED, +}; + /* workqueue implementation */ struct rt_workqueue { rt_list_t work_list; struct rt_work *work_current; /* current work */ - struct rt_semaphore sem; + struct rt_semaphore sem; rt_thread_t work_thread; }; @@ -25,27 +38,46 @@ struct rt_work { rt_list_t list; - void (*work_func)(struct rt_work* work, void* work_data); + void (*work_func)(struct rt_work *work, void *work_data); void *work_data; + rt_uint16_t flags; + rt_uint16_t type; +}; + +struct rt_delayed_work +{ + struct rt_work work; + struct rt_timer timer; + struct rt_workqueue *workqueue; }; #ifdef RT_USING_HEAP /** * WorkQueue for DeviceDriver */ -struct rt_workqueue *rt_workqueue_create(const char* name, rt_uint16_t stack_size, rt_uint8_t priority); -rt_err_t rt_workqueue_destroy(struct rt_workqueue* queue); -rt_err_t rt_workqueue_dowork(struct rt_workqueue* queue, struct rt_work* work); -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_inline void rt_work_init(struct rt_work* work, void (*work_func)(struct rt_work* work, void* work_data), - void* work_data) +struct rt_workqueue *rt_workqueue_create(const char *name, rt_uint16_t stack_size, rt_uint8_t priority); +rt_err_t rt_workqueue_destroy(struct rt_workqueue *queue); +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); + +#ifdef RT_USING_SYSTEM_WORKQUEUE +rt_err_t rt_work_submit(struct rt_work *work, rt_tick_t time); +rt_err_t rt_work_cancel(struct rt_work *work); +#endif + +rt_inline void rt_work_init(struct rt_work *work, void (*work_func)(struct rt_work *work, void *work_data), + void *work_data) { rt_list_init(&(work->list)); work->work_func = work_func; work->work_data = work_data; } + +void rt_delayed_work_init(struct rt_delayed_work *work, void (*work_func)(struct rt_work *work, + void *work_data), void *work_data); + #endif #endif diff --git a/components/drivers/src/workqueue.c b/components/drivers/src/workqueue.c index 158ea8b69b..f2b1ae81e4 100644 --- a/components/drivers/src/workqueue.c +++ b/components/drivers/src/workqueue.c @@ -13,11 +13,12 @@ #include #ifdef RT_USING_HEAP + rt_inline rt_err_t _workqueue_work_completion(struct rt_workqueue *queue) { rt_err_t result; - - rt_enter_critical(); + + rt_enter_critical(); while (1) { /* try to take condition semaphore */ @@ -40,17 +41,17 @@ rt_inline rt_err_t _workqueue_work_completion(struct rt_workqueue *queue) } } rt_exit_critical(); - + return result; } -static void _workqueue_thread_entry(void* parameter) +static void _workqueue_thread_entry(void *parameter) { rt_base_t level; - struct rt_work* work; - struct rt_workqueue* queue; + struct rt_work *work; + struct rt_workqueue *queue; - queue = (struct rt_workqueue*) parameter; + queue = (struct rt_workqueue *) parameter; RT_ASSERT(queue != RT_NULL); while (1) @@ -67,6 +68,7 @@ static void _workqueue_thread_entry(void* parameter) work = rt_list_entry(queue->work_list.next, struct rt_work, list); rt_list_remove(&(work->list)); queue->work_current = work; + work->flags &= ~RT_WORK_STATE_PENDING; rt_hw_interrupt_enable(level); /* do work */ @@ -81,11 +83,156 @@ static void _workqueue_thread_entry(void* parameter) } } -struct rt_workqueue *rt_workqueue_create(const char* name, rt_uint16_t stack_size, rt_uint8_t priority) +static rt_err_t _workqueue_submit_work(struct rt_workqueue *queue, struct rt_work *work) +{ + 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 */ + 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) + { + 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; +} + +static rt_err_t _workqueue_cancel_work(struct rt_workqueue *queue, 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_delayed_work *work) +{ + rt_base_t level; + int ret = RT_EOK; + + if (!work->workqueue) + { + ret = -EINVAL; + goto __exit; + } + + if (work->work.flags & RT_WORK_STATE_PENDING) + { + /* Remove from the queue if already submitted */ + ret = rt_workqueue_cancel_work(work->workqueue, &(work->work)); + if (ret) + { + goto __exit; + } + } + else + { + rt_timer_stop(&(work->timer)); + } + + level = rt_hw_interrupt_disable(); + /* Detach from workqueue */ + work->workqueue = RT_NULL; + work->work.flags &= ~(RT_WORK_STATE_PENDING); + rt_hw_interrupt_enable(level); + +__exit: + return ret; +} + +static rt_err_t _workqueue_submit_delayed_work(struct rt_workqueue *queue, + struct rt_delayed_work *work, rt_tick_t ticks) +{ + rt_base_t level; + int 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 */ + _workqueue_submit_work(work->workqueue, &(work->work)); + } + else + { + /* Add timeout */ + rt_timer_control(&(work->timer), RT_TIMER_CTRL_SET_TIME, &ticks); + rt_timer_start(&(work->timer)); + } + +__exit: + return ret; +} + +static void _delayed_work_timeout_handler(void *parameter) +{ + struct rt_delayed_work *delayed_work; + + delayed_work = (struct rt_delayed_work *)parameter; + rt_timer_stop(&(delayed_work->timer)); + _workqueue_submit_work(delayed_work->workqueue, &(delayed_work->work)); +} + +struct rt_workqueue *rt_workqueue_create(const char *name, rt_uint16_t stack_size, rt_uint8_t priority) { struct rt_workqueue *queue = RT_NULL; - queue = (struct rt_workqueue*)RT_KERNEL_MALLOC(sizeof(struct rt_workqueue)); + queue = (struct rt_workqueue *)RT_KERNEL_MALLOC(sizeof(struct rt_workqueue)); if (queue != RT_NULL) { /* initialize work list */ @@ -107,7 +254,7 @@ struct rt_workqueue *rt_workqueue_create(const char* name, rt_uint16_t stack_siz return queue; } -rt_err_t rt_workqueue_destroy(struct rt_workqueue* queue) +rt_err_t rt_workqueue_destroy(struct rt_workqueue *queue) { RT_ASSERT(queue != RT_NULL); @@ -117,37 +264,30 @@ rt_err_t rt_workqueue_destroy(struct rt_workqueue* queue) return RT_EOK; } -rt_err_t rt_workqueue_dowork(struct rt_workqueue* queue, struct rt_work* work) +rt_err_t rt_workqueue_dowork(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) - { - rt_hw_interrupt_enable(level); - return -RT_EBUSY; - } + return _workqueue_submit_work(queue, work); +} - /* NOTE: the work MUST be initialized firstly */ - rt_list_remove(&(work->list)); +rt_err_t rt_workqueue_submit_work(struct rt_workqueue *queue, struct rt_work *work, rt_tick_t time) +{ + RT_ASSERT(queue != RT_NULL); + RT_ASSERT(work != RT_NULL); - rt_list_insert_after(queue->work_list.prev, &(work->list)); - /* whether the workqueue is doing work */ - if (queue->work_current == RT_NULL) + if (work->type & RT_WORK_TYPE_DELAYED) { - rt_hw_interrupt_enable(level); - /* resume work thread */ - rt_thread_resume(queue->work_thread); - rt_schedule(); + return _workqueue_submit_delayed_work(queue, (struct rt_delayed_work *)work, time); + } + else + { + return _workqueue_submit_work(queue, work); } - else rt_hw_interrupt_enable(level); - - return RT_EOK; } -rt_err_t rt_workqueue_critical_work(struct rt_workqueue* queue, struct rt_work* work) +rt_err_t rt_workqueue_critical_work(struct rt_workqueue *queue, struct rt_work *work) { rt_base_t level; RT_ASSERT(queue != RT_NULL); @@ -176,26 +316,22 @@ rt_err_t rt_workqueue_critical_work(struct rt_workqueue* queue, struct rt_work* return RT_EOK; } -rt_err_t rt_workqueue_cancel_work(struct rt_workqueue* queue, struct rt_work* work) +rt_err_t rt_workqueue_cancel_work(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) + if (work->type & RT_WORK_TYPE_DELAYED) { - rt_hw_interrupt_enable(level); - return -RT_EBUSY; + return _workqueue_cancel_delayed_work((struct rt_delayed_work *)work); + } + else + { + return _workqueue_cancel_work(queue, work); } - rt_list_remove(&(work->list)); - rt_hw_interrupt_enable(level); - - return RT_EOK; } -rt_err_t rt_workqueue_cancel_work_sync(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_base_t level; @@ -212,12 +348,13 @@ rt_err_t rt_workqueue_cancel_work_sync(struct rt_workqueue* queue, struct rt_wor { rt_list_remove(&(work->list)); } + work->flags &= ~RT_WORK_STATE_PENDING; rt_hw_interrupt_enable(level); return RT_EOK; } -rt_err_t rt_workqueue_cancel_all_work(struct rt_workqueue* queue) +rt_err_t rt_workqueue_cancel_all_work(struct rt_workqueue *queue) { struct rt_list_node *node, *next; RT_ASSERT(queue != RT_NULL); @@ -233,5 +370,36 @@ rt_err_t rt_workqueue_cancel_all_work(struct rt_workqueue* queue) return RT_EOK; } -#endif +void rt_delayed_work_init(struct rt_delayed_work *work, void (*work_func)(struct rt_work *work, + void *work_data), void *work_data) +{ + rt_work_init(&(work->work), work_func, work_data); + work->work.type = RT_WORK_TYPE_DELAYED; + rt_timer_init(&(work->timer), "work", _delayed_work_timeout_handler, work, 0, + RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER); +} +#ifdef RT_USING_SYSTEM_WORKQUEUE +static struct rt_workqueue *sys_workq; + +rt_err_t rt_work_submit(struct rt_work *work, rt_tick_t time) +{ + return rt_workqueue_submit_work(sys_workq, work, time); +} + +rt_err_t rt_work_cancel(struct rt_work *work) +{ + return rt_workqueue_cancel_work(sys_workq, work); +} + +static int rt_work_sys_workqueue_init(void) +{ + sys_workq = rt_workqueue_create("sys_work", RT_SYSTEM_WORKQUEUE_STACKSIZE * 4, + RT_SYSTEM_WORKQUEUE_PRIORITY); + + return RT_EOK; +} + +INIT_DEVICE_EXPORT(rt_work_sys_workqueue_init); +#endif +#endif -- cgit v1.3.1 From ff0037872874fe6be1a5a3609938f956ac9acd6f Mon Sep 17 00:00:00 2001 From: Bernard Xiong Date: Sun, 31 Mar 2019 15:21:49 +0800 Subject: [libc] Fix the unamed pipe close issue --- components/drivers/include/ipc/pipe.h | 1 + components/drivers/src/pipe.c | 8 ++++++++ 2 files changed, 9 insertions(+) (limited to 'components/drivers/include') diff --git a/components/drivers/include/ipc/pipe.h b/components/drivers/include/ipc/pipe.h index 25b19f540d..93ba95cd2c 100644 --- a/components/drivers/include/ipc/pipe.h +++ b/components/drivers/include/ipc/pipe.h @@ -24,6 +24,7 @@ struct rt_pipe_device { struct rt_device parent; + rt_bool_t is_named; /* ring buffer in pipe device */ struct rt_ringbuffer *fifo; diff --git a/components/drivers/src/pipe.c b/components/drivers/src/pipe.c index 065fd21dc1..16fb82dbfb 100644 --- a/components/drivers/src/pipe.c +++ b/components/drivers/src/pipe.c @@ -97,6 +97,12 @@ static int pipe_fops_close(struct dfs_fd *fd) rt_mutex_release(&(pipe->lock)); + if (device->ref_count == 0 && pipe->is_named == RT_FALSE) + { + /* delete the unamed pipe */ + rt_pipe_delete(device->parent.name); + } + return 0; } @@ -423,6 +429,7 @@ rt_pipe_t *rt_pipe_create(const char *name, int bufsz) if (pipe == RT_NULL) return RT_NULL; rt_memset(pipe, 0, sizeof(rt_pipe_t)); + pipe->is_named = RT_TRUE; /* initialize as a named pipe */ rt_mutex_init(&(pipe->lock), name, RT_IPC_FLAG_FIFO); rt_wqueue_init(&(pipe->reader_queue)); rt_wqueue_init(&(pipe->writer_queue)); @@ -517,6 +524,7 @@ int pipe(int fildes[2]) return -1; } + pipe->is_named = RT_FALSE; /* unamed pipe */ rt_snprintf(dev_name, sizeof(dev_name), "/dev/%s", dname); fildes[0] = open(dev_name, O_RDONLY, 0); if (fildes[0] < 0) -- cgit v1.3.1 From 83b6260b3f05efafa8936c6c185480afc874d6cc Mon Sep 17 00:00:00 2001 From: EvalZero Date: Mon, 22 Apr 2019 11:41:17 +0800 Subject: [components][workqueue]improve delayed work for reinit. --- components/drivers/include/ipc/workqueue.h | 6 +++--- components/drivers/src/workqueue.c | 24 ++++++++++++++++++++---- 2 files changed, 23 insertions(+), 7 deletions(-) (limited to 'components/drivers/include') diff --git a/components/drivers/include/ipc/workqueue.h b/components/drivers/include/ipc/workqueue.h index 09dc4354a8..31dbb53ee3 100644 --- a/components/drivers/include/ipc/workqueue.h +++ b/components/drivers/include/ipc/workqueue.h @@ -13,7 +13,8 @@ enum { - RT_WORK_STATE_PENDING, /* Work item pending state */ + RT_WORK_STATE_PENDING = 0x0001, /* Work item pending state */ + RT_WORK_STATE_SUBMITTING = 0x0002, /* Work item submitting state */ }; /** @@ -21,7 +22,7 @@ enum */ enum { - RT_WORK_TYPE_DELAYED, + RT_WORK_TYPE_DELAYED = 0x0001, }; /* workqueue implementation */ @@ -81,4 +82,3 @@ void rt_delayed_work_init(struct rt_delayed_work *work, void (*work_func)(struct #endif #endif - diff --git a/components/drivers/src/workqueue.c b/components/drivers/src/workqueue.c index d2e7dce329..7bf4bd81a7 100644 --- a/components/drivers/src/workqueue.c +++ b/components/drivers/src/workqueue.c @@ -14,6 +14,8 @@ #ifdef RT_USING_HEAP +static void _delayed_work_timeout_handler(void *parameter); + rt_inline rt_err_t _workqueue_work_completion(struct rt_workqueue *queue) { rt_err_t result; @@ -161,7 +163,14 @@ static rt_err_t _workqueue_cancel_delayed_work(struct rt_delayed_work *work) } else { - rt_timer_stop(&(work->timer)); + if (work->work.flags & RT_WORK_STATE_SUBMITTING) + { + level = rt_hw_interrupt_disable(); + rt_timer_stop(&(work->timer)); + rt_timer_detach(&(work->timer)); + work->work.flags &= ~RT_WORK_STATE_SUBMITTING; + rt_hw_interrupt_enable(level); + } } level = rt_hw_interrupt_disable(); @@ -210,8 +219,12 @@ static rt_err_t _workqueue_submit_delayed_work(struct rt_workqueue *queue, } else { + level = rt_hw_interrupt_disable(); /* Add timeout */ - rt_timer_control(&(work->timer), RT_TIMER_CTRL_SET_TIME, &ticks); + work->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)); } @@ -222,9 +235,14 @@ __exit: static void _delayed_work_timeout_handler(void *parameter) { struct rt_delayed_work *delayed_work; + rt_base_t level; delayed_work = (struct rt_delayed_work *)parameter; + level = rt_hw_interrupt_disable(); rt_timer_stop(&(delayed_work->timer)); + rt_timer_detach(&(delayed_work->timer)); + delayed_work->work.flags &= ~RT_WORK_STATE_SUBMITTING; + rt_hw_interrupt_enable(level); _workqueue_submit_work(delayed_work->workqueue, &(delayed_work->work)); } @@ -375,8 +393,6 @@ void rt_delayed_work_init(struct rt_delayed_work *work, void (*work_func)(struct { rt_work_init(&(work->work), work_func, work_data); work->work.type = RT_WORK_TYPE_DELAYED; - rt_timer_init(&(work->timer), "work", _delayed_work_timeout_handler, work, 0, - RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER); } #ifdef RT_USING_SYSTEM_WORKQUEUE -- cgit v1.3.1 From 91345d62afb43ab2b57a894637e2a48ef3c9afca Mon Sep 17 00:00:00 2001 From: tangyuxin <462747508@qq.com> Date: Sun, 28 Apr 2019 14:31:57 +0800 Subject: [components][drivers][ipc] 初始化工作对象所有成员 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/include/ipc/workqueue.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'components/drivers/include') diff --git a/components/drivers/include/ipc/workqueue.h b/components/drivers/include/ipc/workqueue.h index 31dbb53ee3..354a30c04e 100644 --- a/components/drivers/include/ipc/workqueue.h +++ b/components/drivers/include/ipc/workqueue.h @@ -74,6 +74,8 @@ rt_inline void rt_work_init(struct rt_work *work, void (*work_func)(struct rt_wo rt_list_init(&(work->list)); work->work_func = work_func; work->work_data = work_data; + work->flags = 0; + work->type = 0; } void rt_delayed_work_init(struct rt_delayed_work *work, void (*work_func)(struct rt_work *work, -- cgit v1.3.1 From c57f2370adb07b450fbff36657f5c5bbba8e0c1a Mon Sep 17 00:00:00 2001 From: EvalZero Date: Mon, 6 May 2019 18:17:55 +0800 Subject: [components][pm]improve low power management components --- components/drivers/include/drivers/pm.h | 170 ++++++------- components/drivers/pm/pm.c | 410 ++++++++++++++++---------------- src/idle.c | 4 + 3 files changed, 287 insertions(+), 297 deletions(-) (limited to 'components/drivers/include') diff --git a/components/drivers/include/drivers/pm.h b/components/drivers/include/drivers/pm.h index 94d4c8559b..5cffd8d465 100644 --- a/components/drivers/include/drivers/pm.h +++ b/components/drivers/include/drivers/pm.h @@ -7,6 +7,7 @@ * Date Author Notes * 2012-06-02 Bernard the first version * 2018-08-02 Tanek split run and sleep modes, support custom mode + * 2019-04-28 Zero-Free improve PM mode and device ops interface */ #ifndef __PM_H__ @@ -16,91 +17,62 @@ #ifndef PM_HAS_CUSTOM_CONFIG -/* All modes used for rt_pm_request() adn rt_pm_release() */ +/* All modes used for rt_pm_request() and rt_pm_release() */ enum { - /* run modes */ - PM_RUN_MODE_NORMAL = 0, - /* sleep modes */ - PM_SLEEP_MODE_SLEEP, - PM_SLEEP_MODE_TIMER, + PM_SLEEP_MODE_NONE = 0, + PM_SLEEP_MODE_IDLE, + PM_SLEEP_MODE_LIGHT, + PM_SLEEP_MODE_DEEP, + PM_SLEEP_MODE_STANDBY, PM_SLEEP_MODE_SHUTDOWN, + PM_SLEEP_MODE_MAX, +}; + +enum +{ + /* run modes*/ + PM_RUN_MODE_HIGH_SPEED = 0, + PM_RUN_MODE_NORMAL_SPEED, + PM_RUN_MODE_MEDIUM_SPEED, + PM_RUN_MODE_LOW_SPEED, + PM_RUN_MODE_MAX, +}; + +enum +{ + RT_PM_FREQUENCY_PENDING = 0x01, }; +#define RT_PM_DEFAULT_SLEEP_MODE PM_SLEEP_MODE_IDLE +#define RT_PM_DEFAULT_RUN_MODE PM_RUN_MODE_NORMAL_SPEED + /* The name of all modes used in the msh command "pm_dump" */ -#define PM_MODE_NAMES \ +#define PM_SLEEP_MODE_NAMES \ { \ - "Running Mode", \ - \ - "Sleep Mode", \ - "Timer Mode", \ + "None Mode", \ + "Idle Mode", \ + "LightSleep Mode", \ + "DeepSleep Mode", \ + "Standby Mode", \ "Shutdown Mode", \ } -/* run mode count : 1 */ -#define PM_RUN_MODE_COUNT 1 -/* sleep mode count : 3 */ -#define PM_SLEEP_MODE_COUNT 3 - -/* support redefining default run mode */ -#ifndef PM_RUN_MODE_DEFAULT -#define PM_RUN_MODE_DEFAULT 0 -#endif - -/* support redefining default sleep mode */ -#ifndef PM_SLEEP_MODE_DEFAULT -#define PM_SLEEP_MODE_DEFAULT (PM_SLEEP_MODE_START) -#endif - -/* support redefining the minimum tick into sleep mode */ -#ifndef PM_MIN_ENTER_SLEEP_TICK -#define PM_MIN_ENTER_SLEEP_TICK (1) -#endif +#define PM_RUN_MODE_NAMES \ +{ \ + "High Speed", \ + "Normal Speed", \ + "Medium Speed", \ + "Low Mode", \ +} #else /* PM_HAS_CUSTOM_CONFIG */ #include -#ifndef PM_RUN_MODE_COUNT -#error "You must defined PM_RUN_MODE_COUNT on pm_cfg.h" -#endif - -#ifndef PM_SLEEP_MODE_COUNT -#error "You must defined PM_SLEEP_MODE_COUNT on pm_cfg.h" -#endif - -#ifndef PM_MODE_DEFAULT -#error "You must defined PM_MODE_DEFAULT on pm_cfg.h" -#endif - -#ifndef PM_MODE_NAMES -#error "You must defined PM_MODE_NAMES on pm_cfg.h" -#endif - -#ifndef PM_RUN_MODE_DEFAULT -#error "You must defined PM_RUN_MODE_DEFAULT on pm_cfg.h" -#endif - -/* The default sleep mode(PM_SLEEP_MODE_DEFAULT) are not required. - * If the default mode is defined, it is requested once in rt_system_pm_init() - */ - #endif /* PM_HAS_CUSTOM_CONFIG */ -/* run mode must start at 0 */ -#define PM_RUN_MODE_START 0 -/* the values of the run modes and sleep mode must be consecutive */ -#define PM_SLEEP_MODE_START PM_RUN_MODE_COUNT -/* all mode count */ -#define PM_MODE_COUNT (PM_RUN_MODE_COUNT + PM_SLEEP_MODE_COUNT) -/* The last mode, will be request in rt_system_pm_init() */ -#define PM_MODE_MAX (PM_RUN_MODE_COUNT + PM_SLEEP_MODE_COUNT - 1) - -#if PM_MODE_COUNT > 32 -#error "The number of modes cannot exceed 32" -#endif - /** * device control flag to request or release power */ @@ -114,13 +86,8 @@ struct rt_pm; */ struct rt_pm_ops { - void (*enter)(struct rt_pm *pm); - void (*exit)(struct rt_pm *pm); - -#if PM_RUN_MODE_COUNT > 1 - void (*frequency_change)(struct rt_pm *pm, rt_uint32_t frequency); -#endif - + void (*sleep)(struct rt_pm *pm, uint8_t mode); + void (*run)(struct rt_pm *pm, uint8_t mode, uint32_t frequency); void (*timer_start)(struct rt_pm *pm, rt_uint32_t timeout); void (*timer_stop)(struct rt_pm *pm); rt_tick_t (*timer_get_tick)(struct rt_pm *pm); @@ -128,18 +95,15 @@ struct rt_pm_ops struct rt_device_pm_ops { -#if PM_RUN_MODE_COUNT > 1 - void (*frequency_change)(const struct rt_device* device); -#endif - - void (*suspend)(const struct rt_device* device); - void (*resume) (const struct rt_device* device); + int (*suspend)(const struct rt_device *device, uint8_t mode); + void (*resume)(const struct rt_device *device, uint8_t mode); + int (*frequency_change)(const struct rt_device *device, uint8_t mode, uint32_t frequency); }; struct rt_device_pm { - const struct rt_device* device; - const struct rt_device_pm_ops* ops; + const struct rt_device *device; + const struct rt_device_pm_ops *ops; }; /** @@ -150,32 +114,46 @@ struct rt_pm struct rt_device parent; /* modes */ - rt_uint8_t modes[PM_MODE_COUNT]; - rt_uint8_t current_mode; /* current pm mode */ - rt_uint8_t exit_count; + rt_uint8_t modes[PM_SLEEP_MODE_MAX]; + rt_uint8_t sleep_mode; /* current sleep mode */ + rt_uint8_t run_mode; /* current running mode */ /* the list of device, which has PM feature */ rt_uint8_t device_pm_number; - struct rt_device_pm* device_pm; - struct rt_semaphore device_lock; + struct rt_device_pm *device_pm; + rt_uint32_t frequency; /* if the mode has timer, the corresponding bit is 1*/ - rt_uint32_t timer_mask; + rt_uint8_t timer_mask; + rt_uint8_t flags; const struct rt_pm_ops *ops; }; -void rt_pm_enter(void); -void rt_pm_exit(void); +enum +{ + RT_PM_ENTER_SLEEP = 0, + RT_PM_EXIT_SLEEP, +}; + +struct rt_pm_notify +{ + void (*notify)(uint8_t event, uint8_t mode, void *data); + void *data; +}; + +void rt_pm_request(uint8_t sleep_mode); +void rt_pm_release(uint8_t sleep_mode); +int rt_pm_run_mode_set(uint8_t run_mode, uint32_t frequency); -void rt_pm_request(rt_ubase_t mode); -void rt_pm_release(rt_ubase_t mode); +void rt_pm_device_register(struct rt_device *device, const struct rt_device_pm_ops *ops); +void rt_pm_device_unregister(struct rt_device *device); -void rt_pm_register_device(struct rt_device* device, const struct rt_device_pm_ops* ops); -void rt_pm_unregister_device(struct rt_device* device); +void rt_pm_notify_set(void (*notify)(uint8_t event, uint8_t mode, void *data), void *data); +void rt_pm_default_set(uint8_t sleep_mode); void rt_system_pm_init(const struct rt_pm_ops *ops, - rt_uint8_t timer_mask, - void *user_data); + uint8_t timer_mask, + void *user_data); #endif /* __PM_H__ */ diff --git a/components/drivers/pm/pm.c b/components/drivers/pm/pm.c index c300414444..c273754914 100644 --- a/components/drivers/pm/pm.c +++ b/components/drivers/pm/pm.c @@ -7,6 +7,7 @@ * Date Author Notes * 2012-06-02 Bernard the first version * 2018-08-02 Tanek split run and sleep modes, support custom mode + * 2019-04-28 Zero-Free improve PM mode and device ops interface */ #include @@ -16,11 +17,25 @@ #ifdef RT_USING_PM static struct rt_pm _pm; +static uint8_t _pm_default_sleep = RT_PM_DEFAULT_SLEEP_MODE; +static struct rt_pm_notify _pm_notify; + +#define RT_PM_TICKLESS_THRESH (2) + +RT_WEAK uint32_t rt_pm_enter_critical(void) +{ + return rt_hw_interrupt_disable(); +} + +RT_WEAK void rt_pm_exit_critical(uint32_t ctx) +{ + rt_hw_interrupt_enable(ctx); +} /** * This function will suspend all registered devices */ -static void _pm_device_suspend(void) +static int _pm_device_suspend(uint8_t mode) { int index; @@ -28,15 +43,17 @@ static void _pm_device_suspend(void) { if (_pm.device_pm[index].ops->suspend != RT_NULL) { - _pm.device_pm[index].ops->suspend(_pm.device_pm[index].device); + return _pm.device_pm[index].ops->suspend(_pm.device_pm[index].device, mode); } } + + return RT_EOK; } /** * This function will resume all registered devices */ -static void _pm_device_resume(void) +static void _pm_device_resume(uint8_t mode) { int index; @@ -44,16 +61,15 @@ static void _pm_device_resume(void) { if (_pm.device_pm[index].ops->resume != RT_NULL) { - _pm.device_pm[index].ops->resume(_pm.device_pm[index].device); + _pm.device_pm[index].ops->resume(_pm.device_pm[index].device, mode); } } } -#if PM_RUN_MODE_COUNT > 1 /** * This function will update the frequency of all registered devices */ -static void _pm_device_frequency_change(void) +static void _pm_device_frequency_change(uint8_t mode, uint32_t frequency) { rt_uint32_t index; @@ -61,152 +77,148 @@ static void _pm_device_frequency_change(void) for (index = 0; index < _pm.device_pm_number; index ++) { if (_pm.device_pm[index].ops->frequency_change != RT_NULL) - _pm.device_pm[index].ops->frequency_change(_pm.device_pm[index].device); + _pm.device_pm[index].ops->frequency_change(_pm.device_pm[index].device, mode, frequency); } } -#endif /** - * This function will enter corresponding power mode. + * This function will update the system clock frequency when idle */ -void rt_pm_enter(void) +static void _pm_frequency_scaling(struct rt_pm *pm) { - rt_ubase_t level; - struct rt_pm *pm; - rt_uint32_t index; - rt_tick_t timeout_tick; + rt_base_t level; - pm = &_pm; - - /* disable interrupt before check run modes */ - level = rt_hw_interrupt_disable(); - /* check each run mode, and decide to swithc to run mode or sleep mode */ - for (index = 0; index < PM_RUN_MODE_COUNT; index++) + if (pm->flags & RT_PM_FREQUENCY_PENDING) { - if (pm->modes[index]) - { - if (index > pm->current_mode) - { - pm->ops->exit(pm); - pm->current_mode = index; - pm->ops->enter(pm); -#if PM_RUN_MODE_COUNT > 1 - pm->ops->frequency_change(pm, 0); - _pm_device_frequency_change(); -#endif - } - - rt_hw_interrupt_enable(level); - /* The current mode is run mode, no need to check sleep mode */ - return ; - } + level = rt_hw_interrupt_disable(); + /* change system runing mode */ + pm->ops->run(pm, pm->run_mode, pm->frequency); + /* changer device frequency */ + _pm_device_frequency_change(pm->run_mode, pm->frequency); + pm->flags &= ~RT_PM_FREQUENCY_PENDING; + rt_hw_interrupt_enable(level); } - /* enable interrupt after check run modes */ - rt_hw_interrupt_enable(level); +} - level = rt_hw_interrupt_disable(); - /* check each sleep mode to decide which mode can system sleep. */ - for (index = PM_SLEEP_MODE_START; index < PM_SLEEP_MODE_START + PM_SLEEP_MODE_COUNT; index++) +/** + * This function selects the sleep mode according to the rt_pm_request/rt_pm_release count. + */ +static uint8_t _pm_select_sleep_mode(struct rt_pm *pm) +{ + int index; + uint8_t mode; + + mode = _pm_default_sleep; + for (index = PM_SLEEP_MODE_NONE; index < PM_SLEEP_MODE_MAX; index ++) { if (pm->modes[index]) { - /* let mcu sleep when system is idle */ - - /* run mode to sleep mode */ - if (pm->current_mode < PM_SLEEP_MODE_START) - { - /* exit run mode */ - pm->ops->exit(pm); - } - - /* set current power mode */ - pm->current_mode = index; - pm->exit_count = 1; - - /* suspend all of devices with PM feature */ - _pm_device_suspend(); - - /* should start pm timer */ - if (pm->timer_mask & (1 << index)) - { - /* get next os tick */ - timeout_tick = rt_timer_next_timeout_tick(); - if (timeout_tick != RT_TICK_MAX) - { - timeout_tick -= rt_tick_get(); - -#if defined(PM_MIN_ENTER_SLEEP_TICK) && PM_MIN_ENTER_SLEEP_TICK > 0 - if (timeout_tick < PM_MIN_ENTER_SLEEP_TICK) - { - rt_hw_interrupt_enable(level); - /* limit the minimum time to enter timer sleep mode */ - return ; - } -#endif - } - /* startup pm timer */ - pm->ops->timer_start(pm, timeout_tick); - } - - /* enter sleep and wait to be waken up */ - pm->ops->enter(pm); - - /* exit from low power mode */ - rt_pm_exit(); - - rt_hw_interrupt_enable(level); - return ; + mode = index; + break; } } + pm->sleep_mode = mode; - rt_hw_interrupt_enable(level); + return mode; } /** - * This function exits from sleep mode. + * This function changes the power sleep mode base on the result of selection */ -void rt_pm_exit(void) +static void _pm_change_sleep_mode(struct rt_pm *pm, uint8_t mode) { - rt_ubase_t level; - struct rt_pm *pm; - rt_tick_t delta_tick; + rt_tick_t timeout_tick, delta_tick; + rt_base_t level; + int ret = RT_EOK; - pm = &_pm; - - level = rt_hw_interrupt_disable(); - - if (pm->exit_count) + if (mode == PM_SLEEP_MODE_NONE) + { + pm->sleep_mode = mode; + pm->ops->sleep(pm, PM_SLEEP_MODE_NONE); + } + else { - pm->exit_count = 0; + level = rt_pm_enter_critical(); + + /* Notify app will enter sleep mode */ + if (_pm_notify.notify) + _pm_notify.notify(RT_PM_ENTER_SLEEP, mode, _pm_notify.data); - if (pm->current_mode >= PM_SLEEP_MODE_START) + /* Suspend all peripheral device */ + ret = _pm_device_suspend(mode); + if (ret != RT_EOK) { - /* sleep mode with timer */ - if (pm->timer_mask & (1 << pm->current_mode)) - { - /* get the tick of pm timer */ - delta_tick = pm->ops->timer_get_tick(pm); + _pm_device_resume(mode); + if (_pm_notify.notify) + _pm_notify.notify(RT_PM_EXIT_SLEEP, mode, _pm_notify.data); + rt_pm_exit_critical(level); - /* stop pm timer */ - pm->ops->timer_stop(pm); + return; + } - if (delta_tick) + /* Tickless*/ + if (pm->timer_mask & (0x01 << mode)) + { + timeout_tick = rt_timer_next_timeout_tick(); + if (timeout_tick == RT_TICK_MAX) + { + if (pm->ops->timer_start) + { + pm->ops->timer_start(pm, RT_TICK_MAX); + } + } + else + { + timeout_tick = timeout_tick - rt_tick_get(); + if (timeout_tick < RT_PM_TICKLESS_THRESH) + { + mode = PM_SLEEP_MODE_IDLE; + } + else { - /* adjust OS tick */ - rt_tick_set(rt_tick_get() + delta_tick); - /* check system timer */ - rt_timer_check(); + pm->ops->timer_start(pm, timeout_tick); } } + } + + /* enter lower power state */ + pm->ops->sleep(pm, mode); - /* exit from sleep mode */ - pm->ops->exit(pm); - /* resume the device with PM feature */ - _pm_device_resume(); + /* wake up from lower power state*/ + if (pm->timer_mask & (0x01 << mode)) + { + delta_tick = pm->ops->timer_get_tick(pm); + pm->ops->timer_stop(pm); + if (delta_tick) + { + rt_tick_set(rt_tick_get() + delta_tick); + rt_timer_check(); + } } + + /* resume all device */ + _pm_device_resume(pm->sleep_mode); + + if (_pm_notify.notify) + _pm_notify.notify(RT_PM_EXIT_SLEEP, mode, _pm_notify.data); + + rt_pm_exit_critical(level); } +} - rt_hw_interrupt_enable(level); +/** + * This function will enter corresponding power mode. + */ +void rt_system_power_manager(void) +{ + uint8_t mode; + + /* CPU frequency scaling according to the runing mode settings */ + _pm_frequency_scaling(&_pm); + + /* Low Power Mode Processing */ + mode = _pm_select_sleep_mode(&_pm); + _pm_change_sleep_mode(&_pm, mode); } /** @@ -215,60 +227,18 @@ void rt_pm_exit(void) * * @param parameter the parameter of run mode or sleep mode */ -void rt_pm_request(rt_ubase_t mode) +void rt_pm_request(uint8_t mode) { - rt_ubase_t level; + rt_base_t level; struct rt_pm *pm; - pm = &_pm; - - if (mode > PM_MODE_MAX) + if (mode > (PM_SLEEP_MODE_MAX - 1)) return; level = rt_hw_interrupt_disable(); - - /* update pm modes table */ - pm->modes[mode] ++; - - /* request higter mode with a smaller mode value*/ - if (mode < pm->current_mode) - { - /* the old current mode is RUN mode, need to all pm->ops->exit(), - * if not, it has already called in rt_pm_exit() - */ - if (pm->current_mode < PM_SLEEP_MODE_START) - { - pm->ops->exit(pm); - } - else if (pm->exit_count) - { - /* call exeit when global interrupt is disable */ - pm->ops->exit(pm); - pm->exit_count = 0; - } - - /* update current mode */ - pm->current_mode = mode; - - /* current mode is higher run mode */ - if (mode < PM_SLEEP_MODE_START) - { - /* enter run mode */ - pm->ops->enter(pm); -#if PM_RUN_MODE_COUNT > 1 - /* frequency change */ - pm->ops->frequency_change(pm, 0); - _pm_device_frequency_change(); -#endif - } - else - { - /* do nothing when request higher sleep mode, - * and swithc to new sleep mode in rt_pm_enter() - */ - } - } - + pm = &_pm; + if (pm->modes[mode] < 255) + pm->modes[mode] ++; rt_hw_interrupt_enable(level); } @@ -279,21 +249,18 @@ void rt_pm_request(rt_ubase_t mode) * @param parameter the parameter of run mode or sleep mode * */ -void rt_pm_release(rt_ubase_t mode) +void rt_pm_release(uint8_t mode) { rt_ubase_t level; struct rt_pm *pm; - pm = &_pm; - - if (mode > PM_MODE_MAX) + if (mode > (PM_SLEEP_MODE_MAX - 1)) return; level = rt_hw_interrupt_disable(); - + pm = &_pm; if (pm->modes[mode] > 0) pm->modes[mode] --; - rt_hw_interrupt_enable(level); } @@ -303,9 +270,9 @@ void rt_pm_release(rt_ubase_t mode) * @param device the device with PM feature * @param ops the PM ops for device */ -void rt_pm_register_device(struct rt_device *device, const struct rt_device_pm_ops *ops) +void rt_pm_device_register(struct rt_device *device, const struct rt_device_pm_ops *ops) { - rt_ubase_t level; + rt_base_t level; struct rt_device_pm *device_pm; RT_DEBUG_NOT_IN_INTERRUPT; @@ -322,8 +289,6 @@ void rt_pm_register_device(struct rt_device *device, const struct rt_device_pm_o _pm.device_pm_number += 1; } - rt_sem_release(&(_pm.device_lock)); - rt_hw_interrupt_enable(level); } @@ -332,7 +297,7 @@ void rt_pm_register_device(struct rt_device *device, const struct rt_device_pm_o * * @param device the device with PM feature */ -void rt_pm_unregister_device(struct rt_device *device) +void rt_pm_device_unregister(struct rt_device *device) { rt_ubase_t level; rt_uint32_t index; @@ -362,6 +327,23 @@ void rt_pm_unregister_device(struct rt_device *device) rt_hw_interrupt_enable(level); } +/** + * This function set notification callback for application + */ +void rt_pm_notify_set(void (*notify)(uint8_t event, uint8_t mode, void *data), void *data) +{ + _pm_notify.notify = notify; + _pm_notify.data = data; +} + +/** + * This function set default sleep mode when no pm_request + */ +void rt_pm_default_set(uint8_t sleep_mode) +{ + _pm_default_sleep = sleep_mode; +} + /** * RT-Thread device interface for PM device */ @@ -377,7 +359,7 @@ static rt_size_t _rt_pm_device_read(rt_device_t dev, pm = (struct rt_pm *)dev; RT_ASSERT(pm != RT_NULL); - if (pos <= PM_MODE_MAX) + if (pos < PM_SLEEP_MODE_MAX) { int mode; @@ -434,6 +416,34 @@ static rt_err_t _rt_pm_device_control(rt_device_t dev, return RT_EOK; } +int rt_pm_run_mode_set(uint8_t mode, uint32_t frequency) +{ + rt_base_t level; + struct rt_pm *pm; + + if (mode > PM_RUN_MODE_MAX) + return -RT_EINVAL; + + level = rt_hw_interrupt_disable(); + pm = &_pm; + if (mode < pm->run_mode) + { + /* change system runing mode */ + pm->ops->run(pm, mode, frequency); + /* changer device frequency */ + _pm_device_frequency_change(mode, frequency); + } + else + { + pm->flags |= RT_PM_FREQUENCY_PENDING; + } + pm->frequency = frequency; + pm->run_mode = mode; + rt_hw_interrupt_enable(level); + + return RT_EOK; +} + /** * This function will initialize power management. * @@ -466,37 +476,23 @@ void rt_system_pm_init(const struct rt_pm_ops *ops, /* register PM device to the system */ rt_device_register(device, "pm", RT_DEVICE_FLAG_RDWR); - /* todo : add to kernel source code */ - rt_thread_idle_sethook(rt_pm_enter); - rt_memset(pm->modes, 0, sizeof(pm->modes)); - pm->current_mode = PM_RUN_MODE_DEFAULT; - + pm->sleep_mode = _pm_default_sleep; + pm->run_mode = RT_PM_DEFAULT_RUN_MODE; pm->timer_mask = timer_mask; pm->ops = ops; pm->device_pm = RT_NULL; pm->device_pm_number = 0; - - /* initialize semaphore */ - rt_sem_init(&(pm->device_lock), "pm", 1, RT_IPC_FLAG_FIFO); - - /* request in default running mode */ - rt_pm_request(PM_RUN_MODE_DEFAULT); - -#ifdef PM_SLEEP_MODE_DEFAULT - /* request in default sleep mode */ - rt_pm_request(PM_SLEEP_MODE_DEFAULT); -#endif - - /* must hold on deep shutdown mode */ - rt_pm_request(PM_MODE_MAX); } #ifdef RT_USING_FINSH #include +static const char *_pm_sleep_str[] = PM_SLEEP_MODE_NAMES; +static const char *_pm_run_str[] = PM_RUN_MODE_NAMES; + static void rt_pm_release_mode(int argc, char **argv) { int mode = 0; @@ -521,9 +517,20 @@ static void rt_pm_request_mode(int argc, char **argv) } MSH_CMD_EXPORT_ALIAS(rt_pm_request_mode, pm_request, request power management mode); +static void rt_pm_run_mode_switch(int argc, char **argv) +{ + int mode = 0; + if (argc >= 2) + { + mode = atoi(argv[1]); + } + + rt_pm_run_mode_set(mode, 0); +} +MSH_CMD_EXPORT_ALIAS(rt_pm_run_mode_switch, pm_run_set, switch power management run mode); + static void rt_pm_dump_status(void) { - static const char *pm_str[] = PM_MODE_NAMES; rt_uint32_t index; struct rt_pm *pm; @@ -531,17 +538,18 @@ static void rt_pm_dump_status(void) rt_kprintf("| Power Management Mode | Counter | Timer |\n"); rt_kprintf("+-----------------------+---------+-------+\n"); - for (index = 0; index <= PM_MODE_MAX; index ++) + for (index = 0; index < PM_SLEEP_MODE_MAX; index ++) { int has_timer = 0; if (pm->timer_mask & (1 << index)) has_timer = 1; - rt_kprintf("| %021s | %7d | %5d |\n", pm_str[index], pm->modes[index], has_timer); + rt_kprintf("| %021s | %7d | %5d |\n", _pm_sleep_str[index], pm->modes[index], has_timer); } rt_kprintf("+-----------------------+---------+-------+\n"); - rt_kprintf("pm current mode: %s\n", pm_str[pm->current_mode]); + rt_kprintf("pm current sleep mode: %s\n", _pm_sleep_str[pm->sleep_mode]); + rt_kprintf("pm current run mode: %s\n", _pm_run_str[pm->run_mode]); } FINSH_FUNCTION_EXPORT_ALIAS(rt_pm_dump_status, pm_dump, dump power management status); MSH_CMD_EXPORT_ALIAS(rt_pm_dump_status, pm_dump, dump power management status); diff --git a/src/idle.c b/src/idle.c index e685a7ddfc..77b0424892 100644 --- a/src/idle.c +++ b/src/idle.c @@ -228,6 +228,7 @@ void rt_thread_idle_excute(void) } } +extern void rt_system_power_manager(void); static void rt_thread_idle_entry(void *parameter) { #ifdef RT_USING_SMP @@ -255,6 +256,9 @@ static void rt_thread_idle_entry(void *parameter) #endif rt_thread_idle_excute(); +#ifdef RT_USING_PM + rt_system_power_manager(); +#endif } } -- cgit v1.3.1 From d51e0783babc4ffa7286e8dadceb2b2be2129b33 Mon Sep 17 00:00:00 2001 From: EvalZero Date: Tue, 7 May 2019 21:03:48 +0800 Subject: [components][pm]fix some issues with low power components --- bsp/es32f0334/drivers/drv_pm.c | 1 + bsp/es32f0654/drivers/drv_pm.c | 1 + bsp/stm32/libraries/HAL_Drivers/drv_lptim.c | 2 +- bsp/stm32/libraries/HAL_Drivers/drv_lptim.h | 2 +- bsp/stm32/libraries/HAL_Drivers/drv_pm.c | 5 ++-- components/drivers/include/drivers/pm.h | 9 +++---- components/drivers/pm/pm.c | 41 +++++++++++++++-------------- examples/pm/timer_app.c | 1 + 8 files changed, 32 insertions(+), 30 deletions(-) (limited to 'components/drivers/include') diff --git a/bsp/es32f0334/drivers/drv_pm.c b/bsp/es32f0334/drivers/drv_pm.c index 1841ce2050..22f2c43073 100644 --- a/bsp/es32f0334/drivers/drv_pm.c +++ b/bsp/es32f0334/drivers/drv_pm.c @@ -6,6 +6,7 @@ * Change Logs: * Date Author Notes * 2019-04-08 wangyq the first version + * 2019-05-06 Zero-Free adapt to the new power management interface */ #include diff --git a/bsp/es32f0654/drivers/drv_pm.c b/bsp/es32f0654/drivers/drv_pm.c index 926ffc05c0..aa2f07ec68 100644 --- a/bsp/es32f0654/drivers/drv_pm.c +++ b/bsp/es32f0654/drivers/drv_pm.c @@ -6,6 +6,7 @@ * Change Logs: * Date Author Notes * 2019-04-01 wangyq the first version + * 2019-05-06 Zero-Free adapt to the new power management interface */ #include diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_lptim.c b/bsp/stm32/libraries/HAL_Drivers/drv_lptim.c index 2360cdad78..64e602fa52 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_lptim.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_lptim.c @@ -5,7 +5,7 @@ * * Change Logs: * Date Author Notes - * 2017-08-01 tanek the first version + * 2019-05-06 Zero-Free first version */ #include diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_lptim.h b/bsp/stm32/libraries/HAL_Drivers/drv_lptim.h index 32853c9e73..0fd51b5eac 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_lptim.h +++ b/bsp/stm32/libraries/HAL_Drivers/drv_lptim.h @@ -5,7 +5,7 @@ * * Change Logs: * Date Author Notes - * 2012-08-21 heyuanjie87 the first version + * 2019-05-06 Zero-Free first version */ #ifndef __DRV_PMTIMER_H__ diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_pm.c b/bsp/stm32/libraries/HAL_Drivers/drv_pm.c index 66f00b3e02..84ddf6cd41 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_pm.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_pm.c @@ -5,8 +5,7 @@ * * Change Logs: * Date Author Notes - * 2018-07-31 tanek first version - * 2018-05-05 Zero-Free adapt to the new power management interface + * 2019-05-06 Zero-Free first version */ #include @@ -79,7 +78,7 @@ static uint8_t run_speed[PM_RUN_MODE_MAX][2] = {2, 3}, }; -static void run(struct rt_pm *pm, uint8_t mode, uint32_t frequency) +static void run(struct rt_pm *pm, uint8_t mode) { static uint8_t last_mode; static char *run_str[] = PM_RUN_MODE_NAMES; diff --git a/components/drivers/include/drivers/pm.h b/components/drivers/include/drivers/pm.h index 5cffd8d465..e35b21cd48 100644 --- a/components/drivers/include/drivers/pm.h +++ b/components/drivers/include/drivers/pm.h @@ -77,7 +77,7 @@ enum * device control flag to request or release power */ #define RT_PM_DEVICE_CTRL_REQUEST 0x01 -#define RT_PM_DEVICE_CTRL_RELEASE 0x02 +#define RT_PM_DEVICE_CTRL_RELEASE 0x00 struct rt_pm; @@ -87,7 +87,7 @@ struct rt_pm; struct rt_pm_ops { void (*sleep)(struct rt_pm *pm, uint8_t mode); - void (*run)(struct rt_pm *pm, uint8_t mode, uint32_t frequency); + void (*run)(struct rt_pm *pm, uint8_t mode); void (*timer_start)(struct rt_pm *pm, rt_uint32_t timeout); void (*timer_stop)(struct rt_pm *pm); rt_tick_t (*timer_get_tick)(struct rt_pm *pm); @@ -97,7 +97,7 @@ struct rt_device_pm_ops { int (*suspend)(const struct rt_device *device, uint8_t mode); void (*resume)(const struct rt_device *device, uint8_t mode); - int (*frequency_change)(const struct rt_device *device, uint8_t mode, uint32_t frequency); + int (*frequency_change)(const struct rt_device *device, uint8_t mode); }; struct rt_device_pm @@ -122,7 +122,6 @@ struct rt_pm rt_uint8_t device_pm_number; struct rt_device_pm *device_pm; - rt_uint32_t frequency; /* if the mode has timer, the corresponding bit is 1*/ rt_uint8_t timer_mask; rt_uint8_t flags; @@ -144,7 +143,7 @@ struct rt_pm_notify void rt_pm_request(uint8_t sleep_mode); void rt_pm_release(uint8_t sleep_mode); -int rt_pm_run_mode_set(uint8_t run_mode, uint32_t frequency); +int rt_pm_run_enter(uint8_t run_mode); void rt_pm_device_register(struct rt_device *device, const struct rt_device_pm_ops *ops); void rt_pm_device_unregister(struct rt_device *device); diff --git a/components/drivers/pm/pm.c b/components/drivers/pm/pm.c index c273754914..98a993b526 100644 --- a/components/drivers/pm/pm.c +++ b/components/drivers/pm/pm.c @@ -22,12 +22,12 @@ static struct rt_pm_notify _pm_notify; #define RT_PM_TICKLESS_THRESH (2) -RT_WEAK uint32_t rt_pm_enter_critical(void) +RT_WEAK uint32_t rt_pm_enter_critical(uint8_t sleep_mode) { return rt_hw_interrupt_disable(); } -RT_WEAK void rt_pm_exit_critical(uint32_t ctx) +RT_WEAK void rt_pm_exit_critical(uint32_t ctx, uint8_t sleep_mode) { rt_hw_interrupt_enable(ctx); } @@ -37,17 +37,19 @@ RT_WEAK void rt_pm_exit_critical(uint32_t ctx) */ static int _pm_device_suspend(uint8_t mode) { - int index; + int index, ret = RT_EOK; for (index = 0; index < _pm.device_pm_number; index++) { if (_pm.device_pm[index].ops->suspend != RT_NULL) { - return _pm.device_pm[index].ops->suspend(_pm.device_pm[index].device, mode); + ret = _pm.device_pm[index].ops->suspend(_pm.device_pm[index].device, mode); + if(ret != RT_EOK) + break; } } - return RT_EOK; + return ret; } /** @@ -69,7 +71,7 @@ static void _pm_device_resume(uint8_t mode) /** * This function will update the frequency of all registered devices */ -static void _pm_device_frequency_change(uint8_t mode, uint32_t frequency) +static void _pm_device_frequency_change(uint8_t mode) { rt_uint32_t index; @@ -77,7 +79,7 @@ static void _pm_device_frequency_change(uint8_t mode, uint32_t frequency) for (index = 0; index < _pm.device_pm_number; index ++) { if (_pm.device_pm[index].ops->frequency_change != RT_NULL) - _pm.device_pm[index].ops->frequency_change(_pm.device_pm[index].device, mode, frequency); + _pm.device_pm[index].ops->frequency_change(_pm.device_pm[index].device, mode); } } @@ -92,9 +94,9 @@ static void _pm_frequency_scaling(struct rt_pm *pm) { level = rt_hw_interrupt_disable(); /* change system runing mode */ - pm->ops->run(pm, pm->run_mode, pm->frequency); + pm->ops->run(pm, pm->run_mode); /* changer device frequency */ - _pm_device_frequency_change(pm->run_mode, pm->frequency); + _pm_device_frequency_change(pm->run_mode); pm->flags &= ~RT_PM_FREQUENCY_PENDING; rt_hw_interrupt_enable(level); } @@ -138,7 +140,7 @@ static void _pm_change_sleep_mode(struct rt_pm *pm, uint8_t mode) } else { - level = rt_pm_enter_critical(); + level = rt_pm_enter_critical(mode); /* Notify app will enter sleep mode */ if (_pm_notify.notify) @@ -151,7 +153,7 @@ static void _pm_change_sleep_mode(struct rt_pm *pm, uint8_t mode) _pm_device_resume(mode); if (_pm_notify.notify) _pm_notify.notify(RT_PM_EXIT_SLEEP, mode, _pm_notify.data); - rt_pm_exit_critical(level); + rt_pm_exit_critical(level, mode); return; } @@ -202,7 +204,7 @@ static void _pm_change_sleep_mode(struct rt_pm *pm, uint8_t mode) if (_pm_notify.notify) _pm_notify.notify(RT_PM_EXIT_SLEEP, mode, _pm_notify.data); - rt_pm_exit_critical(level); + rt_pm_exit_critical(level, mode); } } @@ -381,11 +383,11 @@ static rt_size_t _rt_pm_device_write(rt_device_t dev, { /* get request */ request = *(unsigned char *)buffer; - if (request == '1') + if (request == 0x01) { rt_pm_request(pos); } - else if (request == '0') + else if (request == 0x00) { rt_pm_release(pos); } @@ -416,7 +418,7 @@ static rt_err_t _rt_pm_device_control(rt_device_t dev, return RT_EOK; } -int rt_pm_run_mode_set(uint8_t mode, uint32_t frequency) +int rt_pm_run_enter(uint8_t mode) { rt_base_t level; struct rt_pm *pm; @@ -429,15 +431,14 @@ int rt_pm_run_mode_set(uint8_t mode, uint32_t frequency) if (mode < pm->run_mode) { /* change system runing mode */ - pm->ops->run(pm, mode, frequency); + pm->ops->run(pm, mode); /* changer device frequency */ - _pm_device_frequency_change(mode, frequency); + _pm_device_frequency_change(mode); } else { pm->flags |= RT_PM_FREQUENCY_PENDING; } - pm->frequency = frequency; pm->run_mode = mode; rt_hw_interrupt_enable(level); @@ -525,9 +526,9 @@ static void rt_pm_run_mode_switch(int argc, char **argv) mode = atoi(argv[1]); } - rt_pm_run_mode_set(mode, 0); + rt_pm_run_enter(mode); } -MSH_CMD_EXPORT_ALIAS(rt_pm_run_mode_switch, pm_run_set, switch power management run mode); +MSH_CMD_EXPORT_ALIAS(rt_pm_run_mode_switch, pm_run, switch power management run mode); static void rt_pm_dump_status(void) { diff --git a/examples/pm/timer_app.c b/examples/pm/timer_app.c index 66f4c8d4d2..b7ff675b39 100644 --- a/examples/pm/timer_app.c +++ b/examples/pm/timer_app.c @@ -6,6 +6,7 @@ * Change Logs: * Date Author Notes * 2018-08-07 Tanek first implementation + * 2019-05-06 Zero-Free adapt to the new power management interface */ #include -- cgit v1.3.1 From 1cb3c89bce6caa2bea7cd31259af8b3e3e3ba51b Mon Sep 17 00:00:00 2001 From: tangyuxin <462747508@qq.com> Date: Fri, 24 May 2019 20:04:27 +0800 Subject: [components][drivers] add hardware crypto device --- components/drivers/Kconfig | 168 +++++++++++++ components/drivers/hwcrypto/SConscript | 34 +++ components/drivers/hwcrypto/hw_bignum.c | 353 ++++++++++++++++++++++++++++ components/drivers/hwcrypto/hw_bignum.h | 208 ++++++++++++++++ components/drivers/hwcrypto/hw_crc.c | 117 +++++++++ components/drivers/hwcrypto/hw_crc.h | 148 ++++++++++++ components/drivers/hwcrypto/hw_gcm.c | 218 +++++++++++++++++ components/drivers/hwcrypto/hw_gcm.h | 182 ++++++++++++++ components/drivers/hwcrypto/hw_hash.c | 111 +++++++++ components/drivers/hwcrypto/hw_hash.h | 110 +++++++++ components/drivers/hwcrypto/hw_rng.c | 110 +++++++++ components/drivers/hwcrypto/hw_rng.h | 79 +++++++ components/drivers/hwcrypto/hw_symmetric.c | 276 ++++++++++++++++++++++ components/drivers/hwcrypto/hw_symmetric.h | 189 +++++++++++++++ components/drivers/hwcrypto/hwcrypto.c | 255 ++++++++++++++++++++ components/drivers/hwcrypto/hwcrypto.h | 193 +++++++++++++++ components/drivers/include/drivers/crypto.h | 23 ++ components/drivers/include/rtdevice.h | 4 + 18 files changed, 2778 insertions(+) create mode 100644 components/drivers/hwcrypto/SConscript create mode 100644 components/drivers/hwcrypto/hw_bignum.c create mode 100644 components/drivers/hwcrypto/hw_bignum.h create mode 100644 components/drivers/hwcrypto/hw_crc.c create mode 100644 components/drivers/hwcrypto/hw_crc.h create mode 100644 components/drivers/hwcrypto/hw_gcm.c create mode 100644 components/drivers/hwcrypto/hw_gcm.h create mode 100644 components/drivers/hwcrypto/hw_hash.c create mode 100644 components/drivers/hwcrypto/hw_hash.h create mode 100644 components/drivers/hwcrypto/hw_rng.c create mode 100644 components/drivers/hwcrypto/hw_rng.h create mode 100644 components/drivers/hwcrypto/hw_symmetric.c create mode 100644 components/drivers/hwcrypto/hw_symmetric.h create mode 100644 components/drivers/hwcrypto/hwcrypto.c create mode 100644 components/drivers/hwcrypto/hwcrypto.h create mode 100644 components/drivers/include/drivers/crypto.h (limited to 'components/drivers/include') diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index 792dc1b028..d83e54a8da 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -269,6 +269,174 @@ if RT_USING_SENSOR default y endif +menu "Using Hardware Crypto drivers" + config RT_USING_HWCRYPTO + bool "Using Hardware Crypto" + default n + + if RT_USING_HWCRYPTO + config RT_HWCRYPTO_DEFAULT_NAME + string "Hardware crypto device name" + default "hwcryto" + + config RT_HWCRYPTO_IV_MAX_SIZE + int "IV max size" + default "16" + + config HWCRYPTO_KEYBIT_MAX_SIZE + int "Key max bit length" + default 256 + + config RT_HWCRYPTO_USING_GCM + bool "Using Hardware GCM" + default n + + config RT_HWCRYPTO_USING_AES + bool "Using Hardware AES" + default n + + if RT_HWCRYPTO_USING_AES + config RT_HWCRYPTO_USING_AES_ECB + bool "Using Hardware AES ECB mode" + default y + + config RT_HWCRYPTO_USING_AES_CBC + bool "Using Hardware AES CBC mode" + default n + + config RT_HWCRYPTO_USING_AES_CFB + bool "Using Hardware AES CFB mode" + default n + + config RT_HWCRYPTO_USING_AES_CTR + bool "Using Hardware AES CTR mode" + default n + + config RT_HWCRYPTO_USING_AES_OFB + bool "Using Hardware AES OFB mode" + default n + endif + + config RT_HWCRYPTO_USING_DES + bool "Using Hardware DES" + default n + + if RT_HWCRYPTO_USING_DES + config RT_HWCRYPTO_USING_DES_ECB + bool "Using Hardware DES ECB mode" + default y + + config RT_HWCRYPTO_USING_DES_CBC + bool "Using Hardware DES CBC mode" + default n + endif + + config RT_HWCRYPTO_USING_3DES + bool "Using Hardware 3DES" + default n + + if RT_HWCRYPTO_USING_3DES + config RT_HWCRYPTO_USING_3DES_ECB + bool "Using Hardware 3DES ECB mode" + default y + + config RT_HWCRYPTO_USING_3DES_CBC + bool "Using Hardware 3DES CBC mode" + default n + endif + + config RT_HWCRYPTO_USING_RC4 + bool "Using Hardware RC4" + default n + + config RT_HWCRYPTO_USING_MD5 + bool "Using Hardware MD5" + default n + + config RT_HWCRYPTO_USING_SHA1 + bool "Using Hardware SHA1" + default n + + config RT_HWCRYPTO_USING_SHA2 + bool "Using Hardware SHA2" + default n + + if RT_HWCRYPTO_USING_SHA2 + config RT_HWCRYPTO_USING_SHA2_224 + bool "Using Hardware SHA2_224 mode" + default n + + config RT_HWCRYPTO_USING_SHA2_256 + bool "Using Hardware SHA2_256 mode" + default y + + config RT_HWCRYPTO_USING_SHA2_384 + bool "Using Hardware SHA2_384 mode" + default n + + config RT_HWCRYPTO_USING_SHA2_512 + bool "Using Hardware SHA2_512 mode" + default n + endif + + config RT_HWCRYPTO_USING_RNG + bool "Using Hardware RNG" + default n + + config RT_HWCRYPTO_USING_CRC + bool "Using Hardware CRC" + default n + + if RT_HWCRYPTO_USING_CRC + config RT_HWCRYPTO_USING_CRC_07 + bool "Using Hardware CRC-8 0x07 polynomial" + default n + + config RT_HWCRYPTO_USING_CRC_8005 + bool "Using Hardware CRC-16 0x8005 polynomial" + default n + + config RT_HWCRYPTO_USING_CRC_1021 + bool "Using Hardware CRC-16 0x1021 polynomial" + default n + + config RT_HWCRYPTO_USING_CRC_3D65 + bool "Using Hardware CRC-16 0x3D65 polynomial" + default n + + config RT_HWCRYPTO_USING_CRC_04C11DB7 + bool "Using Hardware CRC-32 0x04C11DB7 polynomial" + default n + endif + + config RT_HWCRYPTO_USING_BIGNUM + bool "Using Hardware bignum" + default n + + if RT_HWCRYPTO_USING_BIGNUM + config RT_HWCRYPTO_USING_BIGNUM_EXPTMOD + bool "Using Hardware bignum expt_mod operation" + default y + + config RT_HWCRYPTO_USING_BIGNUM_MULMOD + bool "Using Hardware bignum mul_mod operation" + default y + + config RT_HWCRYPTO_USING_BIGNUM_MUL + bool "Using Hardware bignum mul operation" + default n + + config RT_HWCRYPTO_USING_BIGNUM_ADD + bool "Using Hardware bignum add operation" + default n + + config RT_HWCRYPTO_USING_BIGNUM_SUB + bool "Using Hardware bignum sub operation" + default n + endif + endif +endmenu + menu "Using WiFi" config RT_USING_WIFI bool "Using Wi-Fi framework" diff --git a/components/drivers/hwcrypto/SConscript b/components/drivers/hwcrypto/SConscript new file mode 100644 index 0000000000..90feff50b3 --- /dev/null +++ b/components/drivers/hwcrypto/SConscript @@ -0,0 +1,34 @@ +Import('RTT_ROOT') +Import('rtconfig') +from building import * + +cwd = GetCurrentDir() +CPPPATH = [cwd, str(Dir('#'))] + +src = ['hwcrypto.c'] + +if (GetDepend(['RT_HWCRYPTO_USING_AES']) or + GetDepend(['RT_HWCRYPTO_USING_DES']) or + GetDepend(['RT_HWCRYPTO_USING_3DES']) or + GetDepend(['RT_HWCRYPTO_USING_RC4'])): + src += ['hw_symmetric.c'] + if GetDepend(['RT_HWCRYPTO_USING_GCM']): + src += ['hw_gcm.c'] + +if (GetDepend(['RT_HWCRYPTO_USING_MD5']) or + GetDepend(['RT_HWCRYPTO_USING_SHA1']) or + GetDepend(['RT_HWCRYPTO_USING_SHA2'])): + src += ['hw_hash.c'] + +if GetDepend(['RT_HWCRYPTO_USING_RNG']): + src += ['hw_rng.c'] + +if GetDepend(['RT_HWCRYPTO_USING_CRC']): + src += ['hw_crc.c'] + +if GetDepend(['RT_HWCRYPTO_USING_BIGNUM']): + src += ['hw_bignum.c'] + +group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_HWCRYPTO'], CPPPATH = CPPPATH) + +Return('group') diff --git a/components/drivers/hwcrypto/hw_bignum.c b/components/drivers/hwcrypto/hw_bignum.c new file mode 100644 index 0000000000..173975c70a --- /dev/null +++ b/components/drivers/hwcrypto/hw_bignum.c @@ -0,0 +1,353 @@ +/* + * Copyright (c) 2006-2019, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-04-25 tyx the first version + */ + +#include +#include +#include + +static struct rt_hwcrypto_ctx *bignum_default; + +rt_inline rt_err_t rt_hwcrypto_bignum_init(void) +{ + struct rt_hwcrypto_device *dev; + + if (bignum_default) + { + return RT_EOK; + } + dev = rt_hwcrypto_dev_dufault(); + if (dev == RT_NULL) + { + return -RT_ERROR; + } + return rt_hwcrypto_bignum_default(dev); +} + +/** + * @brief Setting bignum default devices + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_bignum_default(struct rt_hwcrypto_device *device) +{ + if (bignum_default) + { + rt_hwcrypto_ctx_destroy(bignum_default); + bignum_default = RT_NULL; + } + if (device == RT_NULL) + { + return RT_EOK; + } + bignum_default = rt_hwcrypto_ctx_create(device, HWCRYPTO_TYPE_BIGNUM, sizeof(struct hwcrypto_bignum)); + if (bignum_default == RT_NULL) + { + return -RT_ERROR; + } + return RT_EOK; +} + +/** + * @brief Allocate memory for bignum + * + * @return Pointer to allocated bignum obj + */ +struct hw_bignum_mpi *rt_hwcrypto_bignum_alloc(void) +{ + struct hw_bignum_mpi *n; + + n = rt_malloc(sizeof(struct hw_bignum_mpi)); + if (n) + { + rt_memset(n, 0, sizeof(struct hw_bignum_mpi)); + } + return n; +} + +/** + * @brief free a bignum obj + * + * @param Pointer to bignum obj + */ +void rt_hwcrypto_bignum_free(struct hw_bignum_mpi *n) +{ + if (n) + { + rt_free(n->p); + rt_free(n); + } +} + +/** + * @brief Get length of bignum as an unsigned binary buffer + * + * @param n bignum obj + * + * @return binary buffer Length + */ +int rt_hwcrypto_bignum_get_len(const struct hw_bignum_mpi *n) +{ + int tmp_len, total; + + if (n == RT_NULL || n->p == RT_NULL) + { + return 0; + } + tmp_len = 0; + total = n->total; + while ((total > 0) && (n->p[total - 1] == 0)) + { + tmp_len++; + total--; + } + return n->total - tmp_len; +} + +/** + * @brief Get length of bignum as an unsigned binary buffer + * + * @param n bignum obj + * @param buf Buffer for the binary number + * @param len Length of the buffer + * + * @return binary buffer Length + */ +int rt_hwcrypto_bignum_get_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len) +{ + int cp_len; + + if (n == RT_NULL || n->p == RT_NULL || buf == RT_NULL) + { + return 0; + } + cp_len = n->total > len ? len : n->total; + rt_memcpy(n->p, buf, cp_len); + return cp_len; +} + +/** + * @brief Set binary buffer to unsigned bignum + * + * @param n bignum obj + * @param buf Buffer for the binary number + * @param len Length of the buffer + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_bignum_set_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len) +{ + void *temp_p; + + if (n == RT_NULL) + { + return -RT_EINVAL; + } + if (n->p && n->total >= len) + { + rt_memcpy(n->p, buf, len); + return RT_EOK; + } + temp_p = rt_malloc(len); + if (temp_p == RT_NULL) + { + return -RT_ENOMEM; + } + if (n->p) + { + rt_free(n->p); + n->p = temp_p; + n->total = 0; + } + rt_memcpy(n->p, buf, len); + n->total = len; + return RT_EOK; +} + +/** + * @brief Unsigned comparison + * + * @param a bignum obj + * @param b bignum obj + * + * @return 0 is equal + */ +int rt_hwcrypto_bignum_cmp(const struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b) +{ + int a_len, b_len; + + if (a == RT_NULL || a->p == RT_NULL + || b == RT_NULL || b->p == RT_NULL) + { + return -1; + } + a_len = rt_hwcrypto_bignum_get_len(a); + b_len = rt_hwcrypto_bignum_get_len(b); + if (a_len != b_len) + { + return a_len - b_len; + } + return rt_memcmp(a->p, b->p, a_len); +} + +/** + * @brief Compare bignum to standard Unsigned integer + * + * @param a bignum obj + * @param b Unsigned integer + * + * @return 0 is equal + */ +int rt_hwcrypto_bignum_cmp_d(const struct hw_bignum_mpi *a, unsigned long b) +{ + struct hw_bignum_mpi tmp_b; + + b = b <= 0 ? -b : b; + tmp_b.total = sizeof(unsigned long); + tmp_b.p = &b; + return rt_hwcrypto_bignum_cmp(a, &tmp_b); +} + +/** + * @brief a = b + c + * + * @param a bignum obj + * @param b bignum obj + * @param c bignum obj + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_bignum_add(struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b, + const struct hw_bignum_mpi *c) +{ + struct hwcrypto_bignum *bignum_ctx; + + if (rt_hwcrypto_bignum_init() != RT_EOK) + { + return -RT_ERROR; + } + bignum_ctx = (struct hwcrypto_bignum *)bignum_default; + if (bignum_ctx->ops->add) + { + return bignum_ctx->ops->add(bignum_ctx, a, b, c); + } + return -RT_ERROR; +} + +/** + * @brief a = b - c + * + * @param a bignum obj + * @param b bignum obj + * @param c bignum obj + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_bignum_sub(struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b, + const struct hw_bignum_mpi *c) +{ + struct hwcrypto_bignum *bignum_ctx; + + if (rt_hwcrypto_bignum_init() != RT_EOK) + { + return -RT_ERROR; + } + bignum_ctx = (struct hwcrypto_bignum *)bignum_default; + if (bignum_ctx->ops->sub) + { + return bignum_ctx->ops->sub(bignum_ctx, a, b, c); + } + return -RT_ERROR; +} + +/** + * @brief a = b * c + * + * @param a bignum obj + * @param b bignum obj + * @param c bignum obj + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_bignum_mul(struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b, + const struct hw_bignum_mpi *c) +{ + struct hwcrypto_bignum *bignum_ctx; + + if (rt_hwcrypto_bignum_init() != RT_EOK) + { + return -RT_ERROR; + } + bignum_ctx = (struct hwcrypto_bignum *)bignum_default; + if (bignum_ctx->ops->mul) + { + return bignum_ctx->ops->mul(bignum_ctx, a, b, c); + } + return -RT_ERROR; +} + +/** + * @brief a = b * c (mod d) + * + * @param a bignum obj + * @param b bignum obj + * @param c bignum obj + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_bignum_mulmod(struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b, + const struct hw_bignum_mpi *c, + const struct hw_bignum_mpi *d) +{ + struct hwcrypto_bignum *bignum_ctx; + + if (rt_hwcrypto_bignum_init() != RT_EOK) + { + return -RT_ERROR; + } + bignum_ctx = (struct hwcrypto_bignum *)bignum_default; + if (bignum_ctx->ops->mulmod) + { + return bignum_ctx->ops->mulmod(bignum_ctx, a, b, c, d); + } + return -RT_ERROR; +} + +/** + * @brief a = b ^ c (mod d) + * + * @param a bignum obj + * @param b bignum obj + * @param c bignum obj + * + * @return RT_EOK on success. + */ +rt_err_t bignum_exptmod(struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b, + const struct hw_bignum_mpi *c, + const struct hw_bignum_mpi *d) +{ + struct hwcrypto_bignum *bignum_ctx; + + if (rt_hwcrypto_bignum_init() != RT_EOK) + { + return -RT_ERROR; + } + bignum_ctx = (struct hwcrypto_bignum *)bignum_default; + if (bignum_ctx->ops->exptmod) + { + return bignum_ctx->ops->exptmod(bignum_ctx, a, b, c, d); + } + return -RT_ERROR; +} diff --git a/components/drivers/hwcrypto/hw_bignum.h b/components/drivers/hwcrypto/hw_bignum.h new file mode 100644 index 0000000000..92bd4dd3b6 --- /dev/null +++ b/components/drivers/hwcrypto/hw_bignum.h @@ -0,0 +1,208 @@ +/* + * Copyright (c) 2006-2019, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-04-25 tyx the first version + */ + +#ifndef __HW_BIGNUM_H__ +#define __HW_BIGNUM_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +struct hwcrypto_bignum; + +/* bignum obj */ +struct hw_bignum_mpi +{ + rt_size_t total; /**< Total length of data */ + rt_ubase_t *p; /**< pointer to data */ +}; + +struct hwcrypto_bignum_ops +{ + rt_err_t (*add)(struct hwcrypto_bignum *bignum_ctx, + struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b, + const struct hw_bignum_mpi *c); /**< a = b + c */ + rt_err_t (*sub)(struct hwcrypto_bignum *bignum_ctx, + struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b, + const struct hw_bignum_mpi *c); /**< a = b - c */ + rt_err_t (*mul)(struct hwcrypto_bignum *bignum_ctx, + struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b, + const struct hw_bignum_mpi *c); /**< a = b * c */ + rt_err_t (*mulmod)(struct hwcrypto_bignum *bignum_ctx, + struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b, + const struct hw_bignum_mpi *c, + const struct hw_bignum_mpi *d); /**< a = b * c (mod d) */ + rt_err_t (*exptmod)(struct hwcrypto_bignum *bignum_ctx, + struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b, + const struct hw_bignum_mpi *c, + const struct hw_bignum_mpi *d); /**< a = b ^ c (mod d) */ +}; + +/** + * @brief bignum context. Hardware driver usage + */ +struct hwcrypto_bignum +{ + struct rt_hwcrypto_ctx parent; /**< Inheritance from hardware crypto context */ + const struct hwcrypto_bignum_ops *ops; /**< !! Hardware initializes this value when creating context !! */ +}; + +/** + * @brief Setting bignum default devices + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_bignum_default(struct rt_hwcrypto_device *device); + +/** + * @brief Allocate memory for bignum + * + * @return Pointer to allocated bignum obj + */ +struct hw_bignum_mpi *rt_hwcrypto_bignum_alloc(void); + +/** + * @brief free a bignum obj + * + * @param Pointer to bignum obj + */ +void rt_hwcrypto_bignum_free(struct hw_bignum_mpi *n); + +/** + * @brief Get length of bignum as an unsigned binary buffer + * + * @param n bignum obj + * + * @return binary buffer Length + */ +int rt_hwcrypto_bignum_get_len(const struct hw_bignum_mpi *n); + +/** + * @brief Get length of bignum as an unsigned binary buffer + * + * @param n bignum obj + * @param buf Buffer for the binary number + * @param len Length of the buffer + * + * @return binary buffer Length + */ +int rt_hwcrypto_bignum_get_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len); + +/** + * @brief Set binary buffer to unsigned bignum + * + * @param n bignum obj + * @param buf Buffer for the binary number + * @param len Length of the buffer + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_bignum_set_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len); + +/** + * @brief Unsigned comparison + * + * @param a bignum obj + * @param b bignum obj + * + * @return 0 is equal + */ +int rt_hwcrypto_bignum_cmp(const struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b); + +/** + * @brief Compare bignum to standard Unsigned integer + * + * @param a bignum obj + * @param b Unsigned integer + * + * @return 0 is equal + */ +int rt_hwcrypto_bignum_cmp_d(const struct hw_bignum_mpi *a, unsigned long b); + +/** + * @brief a = b + c + * + * @param a bignum obj + * @param b bignum obj + * @param c bignum obj + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_bignum_add(struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b, + const struct hw_bignum_mpi *c); + +/** + * @brief a = b - c + * + * @param a bignum obj + * @param b bignum obj + * @param c bignum obj + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_bignum_sub(struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b, + const struct hw_bignum_mpi *c); + +/** + * @brief a = b * c + * + * @param a bignum obj + * @param b bignum obj + * @param c bignum obj + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_bignum_mul(struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b, + const struct hw_bignum_mpi *c); + +/** + * @brief a = b * c (mod d) + * + * @param a bignum obj + * @param b bignum obj + * @param c bignum obj + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_bignum_mulmod(struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b, + const struct hw_bignum_mpi *c, + const struct hw_bignum_mpi *d); + +/** + * @brief a = b ^ c (mod d) + * + * @param a bignum obj + * @param b bignum obj + * @param c bignum obj + * + * @return RT_EOK on success. + */ +rt_err_t bignum_exptmod(struct hw_bignum_mpi *a, + const struct hw_bignum_mpi *b, + const struct hw_bignum_mpi *c, + const struct hw_bignum_mpi *d); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/components/drivers/hwcrypto/hw_crc.c b/components/drivers/hwcrypto/hw_crc.c new file mode 100644 index 0000000000..20579711e2 --- /dev/null +++ b/components/drivers/hwcrypto/hw_crc.c @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2006-2019, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-04-25 tyx the first version + */ + +#include +#include +#include + +/** + * @brief Creating CRC Context + * + * @param device Hardware crypto device + * @param mode Setting default mode or custom mode + * + * @return CRC context + */ +struct rt_hwcrypto_ctx *rt_hwcrypto_crc_create(struct rt_hwcrypto_device *device, + hwcrypto_crc_mode mode) +{ + struct hwcrypto_crc *crc_ctx; + + crc_ctx = (struct hwcrypto_crc *)rt_hwcrypto_ctx_create(device, HWCRYPTO_TYPE_CRC, sizeof(struct hwcrypto_crc)); + if (crc_ctx == RT_NULL) + { + return RT_NULL; + } + + switch (mode) + { + case HWCRYPTO_CRC_CRC8: + { + struct hwcrypto_crc_cfg temp = HWCRYPTO_CRC8_CFG; + crc_ctx->crc_cfg = temp; + break; + } + case HWCRYPTO_CRC_CRC16: + { + struct hwcrypto_crc_cfg temp = HWCRYPTO_CRC16_CFG; + crc_ctx->crc_cfg = temp; + break; + } + case HWCRYPTO_CRC_CRC32: + { + struct hwcrypto_crc_cfg temp = HWCRYPTO_CRC32_CFG; + crc_ctx->crc_cfg = temp; + break; + } + case HWCRYPTO_CRC_CCITT: + { + struct hwcrypto_crc_cfg temp = HWCRYPTO_CRC_CCITT_CFG; + crc_ctx->crc_cfg = temp; + break; + } + case HWCRYPTO_CRC_DNP: + { + struct hwcrypto_crc_cfg temp = HWCRYPTO_CRC_DNP_CFG; + crc_ctx->crc_cfg = temp; + break; + } + default: + break; + } + + return &crc_ctx->parent; +} + +/** + * @brief Destroy CRC Context + * + * @param ctx CRC context + */ +void rt_hwcrypto_crc_destroy(struct rt_hwcrypto_ctx *ctx) +{ + rt_hwcrypto_ctx_destroy(ctx); +} + +/** + * @brief Processing a packet of data + * + * @param ctx CRC context + * @param input Data buffer to be Processed + * @param length Data Buffer length + * + * @return RT_EOK on success. + */ +rt_uint32_t rt_hwcrypto_crc_update(struct rt_hwcrypto_ctx *ctx, + const rt_uint8_t *input, + rt_size_t length) +{ + struct hwcrypto_crc *crc_ctx = (struct hwcrypto_crc *)ctx; + if (ctx && crc_ctx->ops->update) + { + return crc_ctx->ops->update(crc_ctx, input, length); + } + return 0; +} + +/** + * @brief CRC context configuration + * + * @param ctx CRC context + * @param cfg CRC config + */ +void rt_hwcrypto_crc_cfg(struct rt_hwcrypto_ctx *ctx, + struct hwcrypto_crc_cfg *cfg) +{ + if (cfg) + { + ((struct hwcrypto_crc *)ctx)->crc_cfg = *cfg; + } +} diff --git a/components/drivers/hwcrypto/hw_crc.h b/components/drivers/hwcrypto/hw_crc.h new file mode 100644 index 0000000000..a6df92a2ef --- /dev/null +++ b/components/drivers/hwcrypto/hw_crc.h @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2006-2019, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-04-25 tyx the first version + */ + +#ifndef __HW_CRC_H__ +#define __HW_CRC_H__ + +#include + +#define CRC_FLAG_REFIN (0x1 << 0) +#define CRC_FLAG_REFOUT (0x1 << 1) + +#define HWCRYPTO_CRC8_CFG \ +{ \ + .last_val = 0x00, \ + .poly = 0x07, \ + .width = 8, \ + .xorout = 0x00, \ + .flags = 0, \ +} + +#define HWCRYPTO_CRC16_CFG \ +{ \ + .last_val = 0x0000, \ + .poly = 0x8005, \ + .width = 16, \ + .xorout = 0x0000, \ + .flags = 0, \ +} + +#define HWCRYPTO_CRC32_CFG \ +{ \ + .last_val = 0x00000000, \ + .poly = 0x04C11DB7, \ + .width = 32, \ + .xorout = 0x00000000, \ + .flags = 0, \ +} + +#define HWCRYPTO_CRC_CCITT_CFG \ +{ \ + .last_val = 0x0000, \ + .poly = 0x1021, \ + .width = 16, \ + .xorout = 0x0000, \ + .flags = CRC_FLAG_REFIN | CRC_FLAG_REFOUT, \ +} + +#define HWCRYPTO_CRC_DNP_CFG \ +{ \ + .last_val = 0x0000, \ + .poly = 0x3D65, \ + .width = 16, \ + .xorout = 0xffff, \ + .flags = CRC_FLAG_REFIN | CRC_FLAG_REFOUT, \ +} + +#ifdef __cplusplus +extern "C" { +#endif + +struct hwcrypto_crc; + +typedef enum +{ + HWCRYPTO_CRC_CUSTOM, /**< Custom CRC mode */ + HWCRYPTO_CRC_CRC8, /**< poly : 0x07 */ + HWCRYPTO_CRC_CRC16, /**< poly : 0x8005 */ + HWCRYPTO_CRC_CRC32, /**< poly : 0x04C11DB7 */ + HWCRYPTO_CRC_CCITT, /**< poly : 0x1021 */ + HWCRYPTO_CRC_DNP, /**< poly : 0x3D65 */ +} hwcrypto_crc_mode; + +struct hwcrypto_crc_cfg +{ + rt_uint32_t last_val; /**< Last CRC value cache */ + rt_uint32_t poly; /**< CRC polynomial */ + rt_uint16_t width; /**< CRC value width */ + rt_uint32_t xorout; /**< Result XOR Value */ + rt_uint16_t flags; /**< Input or output data reverse. CRC_FLAG_REFIN or CRC_FLAG_REFOUT */ +}; + +struct hwcrypto_crc_ops +{ + rt_uint32_t (*update)(struct hwcrypto_crc *ctx, + const rt_uint8_t *in, rt_size_t length); /**< Perform a CRC calculation. return CRC value */ +}; + +/** + * @brief CRC context. Hardware driver usage + */ +struct hwcrypto_crc +{ + struct rt_hwcrypto_ctx parent; /**< Inherited from the standard device */ + struct hwcrypto_crc_cfg crc_cfg; /**< CRC configure */ + const struct hwcrypto_crc_ops *ops; /**< !! Hardware initializes this value when creating context !! */ +}; + +/** + * @brief Creating CRC Context + * + * @param device Hardware crypto device + * @param mode Setting default mode or custom mode + * + * @return CRC context + */ +struct rt_hwcrypto_ctx *rt_hwcrypto_crc_create(struct rt_hwcrypto_device *device, + hwcrypto_crc_mode mode); + +/** + * @brief Destroy CRC Context + * + * @param ctx CRC context + */ +void rt_hwcrypto_crc_destroy(struct rt_hwcrypto_ctx *ctx); + +/** + * @brief Processing a packet of data + * + * @param ctx CRC context + * @param input Data buffer to be Processed + * @param length Data Buffer length + * + * @return CRC value + */ +rt_uint32_t rt_hwcrypto_crc_update(struct rt_hwcrypto_ctx *ctx, + const rt_uint8_t *input, rt_size_t length); + +/** + * @brief CRC context configuration + * + * @param ctx CRC context + * @param cfg CRC config + */ +void rt_hwcrypto_crc_cfg(struct rt_hwcrypto_ctx *ctx, + struct hwcrypto_crc_cfg *cfg); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/components/drivers/hwcrypto/hw_gcm.c b/components/drivers/hwcrypto/hw_gcm.c new file mode 100644 index 0000000000..8d0f49149a --- /dev/null +++ b/components/drivers/hwcrypto/hw_gcm.c @@ -0,0 +1,218 @@ +/* + * Copyright (c) 2006-2019, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-05-14 tyx the first version + */ + +#include +#include +#include + +/** + * @brief Creating GCM Context + * + * @param device Hardware crypto device + * @param type Type of symmetric crypto context + * + * @return GCM context + */ +struct rt_hwcrypto_ctx *rt_hwcrypto_gcm_create(struct rt_hwcrypto_device *device, + hwcrypto_type crypt_type) +{ + struct rt_hwcrypto_ctx *ctx; + + ctx = rt_hwcrypto_ctx_create(device, HWCRYPTO_TYPE_GCM, sizeof(struct hwcrypto_gcm)); + if (ctx) + { + ((struct hwcrypto_gcm *)ctx)->crypt_type = crypt_type; + } + return ctx; +} + +/** + * @brief Destroy GCM Context + * + * @param ctx GCM context + */ +void rt_hwcrypto_gcm_destroy(struct rt_hwcrypto_ctx *ctx) +{ + rt_hwcrypto_ctx_destroy(ctx); +} + +/** + * @brief This function starts a GCM encryption or decryption operation + * + * @param ctx GCM context + * @param add The buffer holding the additional data + * @param add_len The length of the additional data + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_gcm_start(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *add, + rt_size_t add_len) +{ + struct hwcrypto_gcm *gcm_ctx = (struct hwcrypto_gcm *)ctx; + + if (gcm_ctx && gcm_ctx->ops->start) + { + return gcm_ctx->ops->start(gcm_ctx, add, add_len); + } + return -RT_EINVAL; +} + +/** + * @brief This function finishes the GCM operation and generates the authentication tag + * + * @param ctx GCM context + * @param tag The buffer for holding the tag + * @param tag_len The length of the tag to generate + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_gcm_finish(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *tag, + rt_size_t tag_len) +{ + struct hwcrypto_gcm *gcm_ctx = (struct hwcrypto_gcm *)ctx; + + if (gcm_ctx && gcm_ctx->ops->finish) + { + return gcm_ctx->ops->finish(gcm_ctx, tag, tag_len); + } + return -RT_EINVAL; +} + +/** + * @brief This function performs a symmetric encryption or decryption operation + * + * @param ctx GCM context + * @param mode Operation mode. HWCRYPTO_MODE_ENCRYPT or HWCRYPTO_MODE_DECRYPT + * @param length The length of the input data in Bytes. This must be a multiple of the block size + * @param in The buffer holding the input data + * @param out The buffer holding the output data + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_gcm_crypt(struct rt_hwcrypto_ctx *ctx, hwcrypto_mode mode, + rt_size_t length, const rt_uint8_t *in, rt_uint8_t *out) +{ + return rt_hwcrypto_symmetric_crypt(ctx, mode, length, in, out); +} + +/** + * @brief Set Symmetric Encryption and Decryption Key + * + * @param ctx GCM context + * @param key The crypto key + * @param bitlen The crypto key bit length + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_gcm_setkey(struct rt_hwcrypto_ctx *ctx, + const rt_uint8_t *key, rt_uint32_t bitlen) +{ + return rt_hwcrypto_symmetric_setkey(ctx, key, bitlen); +} + +/** + * @brief Get Symmetric Encryption and Decryption Key + * + * @param ctx GCM context + * @param key The crypto key buffer + * @param bitlen The crypto key bit length + * + * @return Key length of copy + */ +rt_err_t rt_hwcrypto_gcm_getkey(struct rt_hwcrypto_ctx *ctx, + rt_uint8_t *key, rt_uint32_t bitlen) +{ + return rt_hwcrypto_symmetric_getkey(ctx, key, bitlen); +} + +/** + * @brief Set Symmetric Encryption and Decryption initialization vector + * + * @param ctx GCM context + * @param iv The crypto initialization vector + * @param len The crypto initialization vector length + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_gcm_setiv(struct rt_hwcrypto_ctx *ctx, + const rt_uint8_t *iv, rt_size_t len) +{ + return rt_hwcrypto_symmetric_setiv(ctx, iv, len); +} + +/** + * @brief Get Symmetric Encryption and Decryption initialization vector + * + * @param ctx GCM context + * @param iv The crypto initialization vector buffer + * @param len The crypto initialization vector buffer length + * + * @return IV length of copy + */ +rt_err_t rt_hwcrypto_gcm_getiv(struct rt_hwcrypto_ctx *ctx, + rt_uint8_t *iv, rt_size_t len) +{ + return rt_hwcrypto_symmetric_getiv(ctx, iv, len); +} + +/** + * @brief Set offset in initialization vector + * + * @param ctx GCM context + * @param iv_off The offset in IV + */ +void rt_hwcrypto_gcm_set_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t iv_off) +{ + rt_hwcrypto_symmetric_set_ivoff(ctx, iv_off); +} + +/** + * @brief Get offset in initialization vector + * + * @param ctx GCM context + * @param iv_off It must point to a valid memory + */ +void rt_hwcrypto_gcm_get_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t *iv_off) +{ + rt_hwcrypto_symmetric_get_ivoff(ctx, iv_off); +} + +/** + * @brief This function copy GCM context + * + * @param des The destination GCM context + * @param src The GCM context to be copy + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_gcm_cpy(struct rt_hwcrypto_ctx *des, + const struct rt_hwcrypto_ctx *src) +{ + struct hwcrypto_gcm *gcm_des = (struct hwcrypto_gcm *)des; + struct hwcrypto_gcm *gcm_src = (struct hwcrypto_gcm *)src; + + if (des != RT_NULL && src != RT_NULL) + { + gcm_des->crypt_type = gcm_src->crypt_type; + /* symmetric crypto context copy */ + return rt_hwcrypto_symmetric_cpy(des, src); + } + return -RT_EINVAL; +} + +/** + * @brief Reset GCM context + * + * @param ctx GCM context + */ +void rt_hwcrypto_gcm_reset(struct rt_hwcrypto_ctx *ctx) +{ + rt_hwcrypto_symmetric_reset(ctx); +} diff --git a/components/drivers/hwcrypto/hw_gcm.h b/components/drivers/hwcrypto/hw_gcm.h new file mode 100644 index 0000000000..a73b2b0634 --- /dev/null +++ b/components/drivers/hwcrypto/hw_gcm.h @@ -0,0 +1,182 @@ +/* + * Copyright (c) 2006-2019, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-05-14 tyx the first version + */ + +#ifndef __HW_GCM_H__ +#define __HW_GCM_H__ + +#include "hw_symmetric.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct hwcrypto_gcm; + +struct hwcrypto_gcm_ops +{ + rt_err_t (*start)(struct hwcrypto_gcm *gcm_ctx, + const unsigned char *add, size_t add_len); /**< Set additional data. start GCM operation */ + rt_err_t (*finish)(struct hwcrypto_gcm *gcm_ctx, + const unsigned char *tag, size_t tag_len); /**< finish GCM operation. get tag */ +}; + +/** + * @brief GCM context. Hardware driver usage + */ +struct hwcrypto_gcm +{ + struct hwcrypto_symmetric parent; /**< Inheritance from hardware symmetric crypto context */ + hwcrypto_type crypt_type; /**< symmetric crypto type. eg: AES/DES */ + const struct hwcrypto_gcm_ops *ops; /**< !! Hardware initializes this value when creating context !! */ +}; + +/** + * @brief Creating GCM Context + * + * @param device Hardware crypto device + * @param type Type of symmetric crypto context + * + * @return GCM context + */ +struct rt_hwcrypto_ctx *rt_hwcrypto_gcm_create(struct rt_hwcrypto_device *device, + hwcrypto_type crypt_type); + +/** + * @brief Destroy GCM Context + * + * @param ctx GCM context + */ +void rt_hwcrypto_gcm_destroy(struct rt_hwcrypto_ctx *ctx); + +/** + * @brief This function starts a GCM encryption or decryption operation + * + * @param ctx GCM context + * @param add The buffer holding the additional data + * @param add_len The length of the additional data + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_gcm_start(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *add, + rt_size_t add_len); + +/** + * @brief This function finishes the GCM operation and generates the authentication tag + * + * @param ctx GCM context + * @param tag The buffer for holding the tag + * @param tag_len The length of the tag to generate + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_gcm_finish(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *tag, + rt_size_t tag_len); + +/** + * @brief This function performs a symmetric encryption or decryption operation + * + * @param ctx GCM context + * @param mode Operation mode. HWCRYPTO_MODE_ENCRYPT or HWCRYPTO_MODE_DECRYPT + * @param length The length of the input data in Bytes. This must be a multiple of the block size + * @param in The buffer holding the input data + * @param out The buffer holding the output data + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_gcm_crypt(struct rt_hwcrypto_ctx *ctx, hwcrypto_mode mode, + rt_size_t length, const rt_uint8_t *in, rt_uint8_t *out); + +/** + * @brief Set Symmetric Encryption and Decryption Key + * + * @param ctx GCM context + * @param key The crypto key + * @param bitlen The crypto key bit length + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_gcm_setkey(struct rt_hwcrypto_ctx *ctx, + const rt_uint8_t *key, rt_uint32_t bitlen); + +/** + * @brief Get Symmetric Encryption and Decryption Key + * + * @param ctx GCM context + * @param key The crypto key buffer + * @param bitlen The crypto key bit length + * + * @return Key length of copy + */ +rt_err_t rt_hwcrypto_gcm_getkey(struct rt_hwcrypto_ctx *ctx, + rt_uint8_t *key, rt_uint32_t bitlen); + +/** + * @brief Set Symmetric Encryption and Decryption initialization vector + * + * @param ctx GCM context + * @param iv The crypto initialization vector + * @param len The crypto initialization vector length + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_gcm_setiv(struct rt_hwcrypto_ctx *ctx, + const rt_uint8_t *iv, rt_size_t len); + +/** + * @brief Get Symmetric Encryption and Decryption initialization vector + * + * @param ctx GCM context + * @param iv The crypto initialization vector buffer + * @param len The crypto initialization vector buffer length + * + * @return IV length of copy + */ +rt_err_t rt_hwcrypto_gcm_getiv(struct rt_hwcrypto_ctx *ctx, + rt_uint8_t *iv, rt_size_t len); + +/** + * @brief Set offset in initialization vector + * + * @param ctx GCM context + * @param iv_off The offset in IV + */ +void rt_hwcrypto_gcm_set_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t iv_off); + +/** + * @brief Get offset in initialization vector + * + * @param ctx GCM context + * @param iv_off It must point to a valid memory + */ +void rt_hwcrypto_gcm_get_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t *iv_off); + +/** + * @brief This function copy GCM context + * + * @param des The destination GCM context + * @param src The GCM context to be copy + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_gcm_cpy(struct rt_hwcrypto_ctx *des, + const struct rt_hwcrypto_ctx *src); + +/** + * @brief Reset GCM context + * + * @param ctx GCM context + */ +void rt_hwcrypto_gcm_reset(struct rt_hwcrypto_ctx *ctx); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/components/drivers/hwcrypto/hw_hash.c b/components/drivers/hwcrypto/hw_hash.c new file mode 100644 index 0000000000..b20069ac0e --- /dev/null +++ b/components/drivers/hwcrypto/hw_hash.c @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2006-2019, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-04-23 tyx the first version + */ + +#include +#include +#include + +/** + * @brief Creating hash Context + * + * @param device Hardware crypto device + * @param type Type of hash context + * + * @return Hash context + */ +struct rt_hwcrypto_ctx *rt_hwcrypto_hash_create(struct rt_hwcrypto_device *device, hwcrypto_type type) +{ + struct rt_hwcrypto_ctx *ctx; + + ctx = rt_hwcrypto_ctx_create(device, type, sizeof(struct hwcrypto_hash)); + return ctx; +} + +/** + * @brief Destroy hash Context + * + * @param ctx Hash context + */ +void rt_hwcrypto_hash_destroy(struct rt_hwcrypto_ctx *ctx) +{ + rt_hwcrypto_ctx_destroy(ctx); +} + +/** + * @brief Get the final hash value + * + * @param ctx Hash context + * @param output Hash value buffer + * @param length Hash value buffer length + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_hash_finish(struct rt_hwcrypto_ctx *ctx, rt_uint8_t *output, rt_size_t length) +{ + if (ctx && ((struct hwcrypto_hash *)ctx)->ops->finish) + { + return ((struct hwcrypto_hash *)ctx)->ops->finish((struct hwcrypto_hash *)ctx, output, length); + } + return -RT_ERROR; +} + +/** + * @brief Processing a packet of data + * + * @param ctx Hash context + * @param input Data buffer to be Processed + * @param length Data Buffer length + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_hash_update(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *input, rt_size_t length) +{ + if (ctx && ((struct hwcrypto_hash *)ctx)->ops->update) + { + return ((struct hwcrypto_hash *)ctx)->ops->update((struct hwcrypto_hash *)ctx, input, length); + } + return -RT_ERROR; +} + +/** + * @brief This function copy hash context + * + * @param des The destination hash context + * @param src The hash context to be copy + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_hash_cpy(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src) +{ + return rt_hwcrypto_ctx_cpy(des, src); +} + +/** + * @brief Reset hash context + * + * @param ctx Hash context + */ +void rt_hwcrypto_hash_reset(struct rt_hwcrypto_ctx *ctx) +{ + rt_hwcrypto_ctx_reset(ctx); +} + +/** + * @brief Setting hash context type + * + * @param ctx Hash context + * @param type Types of settings + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_hash_set_type(struct rt_hwcrypto_ctx *ctx, hwcrypto_type type) +{ + return rt_hwcrypto_set_type(ctx, type); +} diff --git a/components/drivers/hwcrypto/hw_hash.h b/components/drivers/hwcrypto/hw_hash.h new file mode 100644 index 0000000000..b913f4ebd4 --- /dev/null +++ b/components/drivers/hwcrypto/hw_hash.h @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2006-2019, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-04-23 tyx the first version + */ + +#ifndef __HW_HASH_H__ +#define __HW_HASH_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +struct hwcrypto_hash; + +struct hwcrypto_hash_ops +{ + rt_err_t (*update)(struct hwcrypto_hash *hash_ctx, + const rt_uint8_t *in, rt_size_t length); /**< Processing a packet of data */ + rt_err_t (*finish)(struct hwcrypto_hash *hash_ctx, + rt_uint8_t *out, rt_size_t length); /**< Get the final hash value */ +}; + +/** + * @brief hash context. Hardware driver usage + */ +struct hwcrypto_hash +{ + struct rt_hwcrypto_ctx parent; /**< Inheritance from hardware crypto context */ + const struct hwcrypto_hash_ops *ops; /**< !! Hardware initializes this value when creating context !! */ +}; + +/** + * @brief Creating hash Context + * + * @param device Hardware crypto device + * @param type Type of hash context + * + * @return Hash context + */ +struct rt_hwcrypto_ctx *rt_hwcrypto_hash_create(struct rt_hwcrypto_device *device, + hwcrypto_type type); + +/** + * @brief Destroy hash Context + * + * @param ctx Hash context + */ +void rt_hwcrypto_hash_destroy(struct rt_hwcrypto_ctx *ctx); + +/** + * @brief Get the final hash value + * + * @param ctx Hash context + * @param output Hash value buffer + * @param length Hash value buffer length + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_hash_finish(struct rt_hwcrypto_ctx *ctx, rt_uint8_t *output, rt_size_t length); + +/** + * @brief Processing a packet of data + * + * @param ctx Hash context + * @param input Data buffer to be Processed + * @param length Data Buffer length + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_hash_update(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *input, rt_size_t length); + +/** + * @brief This function copy hash context + * + * @param des The destination hash context + * @param src The hash context to be copy + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_hash_cpy(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src); + +/** + * @brief Reset hash context + * + * @param ctx Hash context + */ +void rt_hwcrypto_hash_reset(struct rt_hwcrypto_ctx *ctx); + +/** + * @brief Setting hash context type + * + * @param ctx Hash context + * @param type Types of settings + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_hash_type(struct rt_hwcrypto_ctx *ctx, hwcrypto_type type); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/components/drivers/hwcrypto/hw_rng.c b/components/drivers/hwcrypto/hw_rng.c new file mode 100644 index 0000000000..f64bdf8831 --- /dev/null +++ b/components/drivers/hwcrypto/hw_rng.c @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2006-2019, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-04-25 tyx the first version + */ + +#include +#include +#include + +/* Used to save default RNG Context */ +static struct rt_hwcrypto_ctx *ctx_default; + +/** + * @brief Creating RNG Context + * + * @param device Hardware crypto device + * + * @return RNG context + */ +struct rt_hwcrypto_ctx *rt_hwcrypto_rng_create(struct rt_hwcrypto_device *device) +{ + struct rt_hwcrypto_ctx *ctx; + + ctx = rt_hwcrypto_ctx_create(device, HWCRYPTO_TYPE_RNG, sizeof(struct hwcrypto_rng)); + return ctx; +} + +/** + * @brief Destroy RNG Context + * + * @param ctx RNG context + */ +void rt_hwcrypto_rng_destroy(struct rt_hwcrypto_ctx *ctx) +{ + /* Destroy the defaule RNG Context ? */ + if (ctx == ctx_default) + { + ctx_default = RT_NULL; + } + rt_hwcrypto_ctx_destroy(ctx); +} + +/** + * @brief Setting RNG default devices + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_rng_default(struct rt_hwcrypto_device *device) +{ + struct rt_hwcrypto_ctx *tmp_ctx; + + /* if device is null, destroy default RNG Context */ + if (device == RT_NULL) + { + if (ctx_default) + { + rt_hwcrypto_rng_destroy(ctx_default); + ctx_default = RT_NULL; + } + return RT_EOK; + } + /* Try create RNG Context */ + tmp_ctx = rt_hwcrypto_rng_create(device); + if (tmp_ctx == RT_NULL) + { + return -RT_ERROR; + } + /* create RNG Context success, update default RNG Context */ + rt_hwcrypto_rng_destroy(ctx_default); + ctx_default = tmp_ctx; + + return RT_EOK; +} + +/** + * @brief Getting Random Numbers from RNG Context + * + * @param ctx RNG context + * + * @return Random number + */ +rt_uint32_t rt_hwcrypto_rng_update_ctx(struct rt_hwcrypto_ctx *ctx) +{ + if (ctx) + { + return ((struct hwcrypto_rng *)ctx)->ops->update((struct hwcrypto_rng *)ctx); + } + return 0; +} + +/** + * @brief Return a random number + * + * @return Random number + */ +rt_uint32_t rt_hwcrypto_rng_update(void) +{ + /* Default device does not exist ? */ + if (ctx_default == RT_NULL) + { + /* try create Context from dufault device */ + rt_hwcrypto_rng_default(rt_hwcrypto_dev_dufault()); + } + return rt_hwcrypto_rng_update_ctx(ctx_default); +} diff --git a/components/drivers/hwcrypto/hw_rng.h b/components/drivers/hwcrypto/hw_rng.h new file mode 100644 index 0000000000..fc28afdb1e --- /dev/null +++ b/components/drivers/hwcrypto/hw_rng.h @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2006-2019, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-04-25 tyx the first version + */ + +#ifndef __HW_RNG_H__ +#define __HW_RNG_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +struct hwcrypto_rng; + +struct hwcrypto_rng_ops +{ + rt_uint32_t (*update)(struct hwcrypto_rng *ctx); /**< Return a random number */ +}; + +/** + * @brief random context. Hardware driver usage + */ +struct hwcrypto_rng +{ + struct rt_hwcrypto_ctx parent; /**< Inheritance from hardware crypto context */ + const struct hwcrypto_rng_ops *ops; /**< !! Hardware initializes this value when creating context !! */ +}; + +/** + * @brief Creating RNG Context + * + * @param device Hardware crypto device + * + * @return RNG context + */ +struct rt_hwcrypto_ctx *rt_hwcrypto_rng_create(struct rt_hwcrypto_device *device); + +/** + * @brief Destroy RNG Context + * + * @param ctx RNG context + */ +void rt_hwcrypto_rng_destroy(struct rt_hwcrypto_ctx *ctx); + +/** + * @brief Setting RNG default devices + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_rng_default(struct rt_hwcrypto_device *device); + +/** + * @brief Getting Random Numbers from RNG Context + * + * @param ctx RNG context + * + * @return Random number + */ +rt_uint32_t rt_hwcrypto_rng_update_ctx(struct rt_hwcrypto_ctx *ctx); + +/** + * @brief Return a random number + * + * @return Random number + */ +rt_uint32_t rt_hwcrypto_rng_update(void); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/components/drivers/hwcrypto/hw_symmetric.c b/components/drivers/hwcrypto/hw_symmetric.c new file mode 100644 index 0000000000..789c5c1a2c --- /dev/null +++ b/components/drivers/hwcrypto/hw_symmetric.c @@ -0,0 +1,276 @@ +/* + * Copyright (c) 2006-2019, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-04-25 tyx the first version + */ + +#include +#include +#include + +/** + * @brief Creating Symmetric Encryption and Decryption Context + * + * @param device Hardware crypto device + * @param type Type of symmetric crypto context + * + * @return Symmetric crypto context + */ +struct rt_hwcrypto_ctx *rt_hwcrypto_symmetric_create(struct rt_hwcrypto_device *device, hwcrypto_type type) +{ + struct rt_hwcrypto_ctx *ctx; + + ctx = rt_hwcrypto_ctx_create(device, type, sizeof(struct hwcrypto_symmetric)); + return ctx; +} + +/** + * @brief Destroy Symmetric Encryption and Decryption Context + * + * @param ctx Symmetric crypto context + */ +void rt_hwcrypto_symmetric_destroy(struct rt_hwcrypto_ctx *ctx) +{ + rt_hwcrypto_ctx_destroy(ctx); +} + +/** + * @brief This function performs a symmetric encryption or decryption operation + * + * @param ctx Symmetric crypto context + * @param mode Operation mode. HWCRYPTO_MODE_ENCRYPT or HWCRYPTO_MODE_DECRYPT + * @param length The length of the input data in Bytes. This must be a multiple of the block size + * @param in The buffer holding the input data + * @param out The buffer holding the output data + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_symmetric_crypt(struct rt_hwcrypto_ctx *ctx, hwcrypto_mode mode, rt_size_t length, const rt_uint8_t *in, rt_uint8_t *out) +{ + struct hwcrypto_symmetric *symmetric_ctx; + struct hwcrypto_symmetric_info symmetric_info; + rt_err_t err; + + if (ctx == RT_NULL) + { + return -RT_EINVAL; + } + symmetric_ctx = (struct hwcrypto_symmetric *)ctx; + if (symmetric_ctx->ops->crypt == RT_NULL) + { + return -RT_ERROR; + } + if (mode != HWCRYPTO_MODE_ENCRYPT && mode != HWCRYPTO_MODE_DECRYPT) + { + return -EINVAL; + } + + /* Input information packaging */ + symmetric_info.mode = mode; + symmetric_info.in = in; + symmetric_info.out = out; + symmetric_info.length = length; + + /* Calling Hardware Encryption and Decryption Function */ + err = symmetric_ctx->ops->crypt(symmetric_ctx, &symmetric_info); + + /* clean up flags */ + symmetric_ctx->flags &= ~(SYMMTRIC_MODIFY_KEY | SYMMTRIC_MODIFY_IV | SYMMTRIC_MODIFY_IVOFF); + return err; +} + +/** + * @brief Set Symmetric Encryption and Decryption Key + * + * @param ctx Symmetric crypto context + * @param key The crypto key + * @param bitlen The crypto key bit length + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_symmetric_setkey(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *key, rt_uint32_t bitlen) +{ + struct hwcrypto_symmetric *symmetric_ctx; + + if (ctx && bitlen <= RT_HWCRYPTO_KEYBIT_MAX_SIZE) + { + symmetric_ctx = (struct hwcrypto_symmetric *)ctx; + rt_memcpy(symmetric_ctx->key, key, bitlen >> 3); + /* Record key length */ + symmetric_ctx->key_bitlen = bitlen; + /* Key change flag set up */ + symmetric_ctx->flags |= SYMMTRIC_MODIFY_KEY; + return RT_EOK; + } + + return -RT_EINVAL; +} + +/** + * @brief Get Symmetric Encryption and Decryption Key + * + * @param ctx Symmetric crypto context + * @param key The crypto key buffer + * @param bitlen The crypto key bit length + * + * @return Key length of copy + */ +int rt_hwcrypto_symmetric_getkey(struct rt_hwcrypto_ctx *ctx, rt_uint8_t *key, rt_uint32_t bitlen) +{ + struct hwcrypto_symmetric *symmetric_ctx = (struct hwcrypto_symmetric *)ctx; + + if (ctx && bitlen >= symmetric_ctx->key_bitlen) + { + rt_memcpy(key, symmetric_ctx->key, symmetric_ctx->key_bitlen >> 3); + return symmetric_ctx->key_bitlen; + } + + return 0; +} + +/** + * @brief Set Symmetric Encryption and Decryption initialization vector + * + * @param ctx Symmetric crypto context + * @param iv The crypto initialization vector + * @param len The crypto initialization vector length + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_symmetric_setiv(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *iv, rt_size_t len) +{ + struct hwcrypto_symmetric *symmetric_ctx; + + if (ctx && len <= RT_HWCRYPTO_IV_MAX_SIZE) + { + symmetric_ctx = (struct hwcrypto_symmetric *)ctx; + rt_memcpy(symmetric_ctx->iv, iv, len); + symmetric_ctx->iv_len = len; + /* IV change flag set up */ + symmetric_ctx->flags |= SYMMTRIC_MODIFY_IV; + return RT_EOK; + } + + return -RT_EINVAL; +} + +/** + * @brief Get Symmetric Encryption and Decryption initialization vector + * + * @param ctx Symmetric crypto context + * @param iv The crypto initialization vector buffer + * @param len The crypto initialization vector buffer length + * + * @return IV length of copy + */ +int rt_hwcrypto_symmetric_getiv(struct rt_hwcrypto_ctx *ctx, rt_uint8_t *iv, rt_size_t len) +{ + struct hwcrypto_symmetric *symmetric_ctx = (struct hwcrypto_symmetric *)ctx;; + + if (ctx && len >= symmetric_ctx->iv_len) + { + rt_memcpy(iv, symmetric_ctx->iv, symmetric_ctx->iv_len); + return symmetric_ctx->iv_len; + } + + return 0; +} + +/** + * @brief Set offset in initialization vector + * + * @param ctx Symmetric crypto context + * @param iv_off The offset in IV + */ +void rt_hwcrypto_symmetric_set_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t iv_off) +{ + if (ctx) + { + ((struct hwcrypto_symmetric *)ctx)->iv_off = iv_off; + /* iv_off change flag set up */ + ((struct hwcrypto_symmetric *)ctx)->flags |= SYMMTRIC_MODIFY_IVOFF; + } +} + +/** + * @brief Get offset in initialization vector + * + * @param ctx Symmetric crypto context + * @param iv_off It must point to a valid memory + */ +void rt_hwcrypto_symmetric_get_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t *iv_off) +{ + if (ctx && iv_off) + { + *iv_off = ((struct hwcrypto_symmetric *)ctx)->iv_off; + } +} + +/** + * @brief This function copy symmetric crypto context + * + * @param des The destination symmetric crypto context + * @param src The symmetric crypto context to be copy + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_symmetric_cpy(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src) +{ + struct hwcrypto_symmetric *symmetric_des = (struct hwcrypto_symmetric *)des; + struct hwcrypto_symmetric *symmetric_src = (struct hwcrypto_symmetric *)src; + + if (des != RT_NULL && src != RT_NULL) + { + /* Copy Symmetric Encryption and Decryption Context Information */ + symmetric_des->flags = symmetric_src->flags ; + symmetric_des->iv_len = symmetric_src->iv_len ; + symmetric_des->iv_off = symmetric_src->iv_off ; + symmetric_des->key_bitlen = symmetric_src->key_bitlen; + rt_memcpy(symmetric_des->iv, symmetric_src->iv, symmetric_src->iv_len); + rt_memcpy(symmetric_des->key, symmetric_src->key, symmetric_src->key_bitlen >> 3); + + /* Hardware context copy */ + return rt_hwcrypto_ctx_cpy(des, src); + } + return -RT_EINVAL; +} + +/** + * @brief Reset symmetric crypto context + * + * @param ctx Symmetric crypto context + */ +void rt_hwcrypto_symmetric_reset(struct rt_hwcrypto_ctx *ctx) +{ + struct hwcrypto_symmetric *symmetric_ctx = (struct hwcrypto_symmetric *)ctx; + if (ctx != RT_NULL) + { + /* Copy Symmetric Encryption and Decryption Context Information */ + symmetric_ctx->flags = 0x00; + symmetric_ctx->iv_len = 0x00; + symmetric_ctx->iv_off = 0x00; + symmetric_ctx->key_bitlen = 0x00; + rt_memset(symmetric_ctx->iv, 0, RT_HWCRYPTO_IV_MAX_SIZE); + rt_memset(symmetric_ctx->key, 0, RT_HWCRYPTO_KEYBIT_MAX_SIZE >> 3); + + /* Hardware context reset */ + rt_hwcrypto_ctx_reset(ctx); + } +} + +/** + * @brief Setting symmetric crypto context type + * + * @param ctx Symmetric crypto context + * @param type Types of settings + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_symmetric_set_type(struct rt_hwcrypto_ctx *ctx, hwcrypto_type type) +{ + return rt_hwcrypto_set_type(ctx, type); +} diff --git a/components/drivers/hwcrypto/hw_symmetric.h b/components/drivers/hwcrypto/hw_symmetric.h new file mode 100644 index 0000000000..12f4dbec30 --- /dev/null +++ b/components/drivers/hwcrypto/hw_symmetric.h @@ -0,0 +1,189 @@ +/* + * Copyright (c) 2006-2019, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-04-25 tyx the first version + */ + +#ifndef __HW_SYMMETRIC_H__ +#define __HW_SYMMETRIC_H__ + +#include + +#ifndef RT_HWCRYPTO_IV_MAX_SIZE +#define RT_HWCRYPTO_IV_MAX_SIZE (16) +#endif +#ifndef RT_HWCRYPTO_KEYBIT_MAX_SIZE +#define RT_HWCRYPTO_KEYBIT_MAX_SIZE (256) +#endif + +#define SYMMTRIC_MODIFY_KEY (0x1 << 0) +#define SYMMTRIC_MODIFY_IV (0x1 << 1) +#define SYMMTRIC_MODIFY_IVOFF (0x1 << 2) + +#ifdef __cplusplus +extern "C" { +#endif + +struct hwcrypto_symmetric; +struct hwcrypto_symmetric_info; + +struct hwcrypto_symmetric_ops +{ + rt_err_t (*crypt)(struct hwcrypto_symmetric *symmetric_ctx, + struct hwcrypto_symmetric_info *symmetric_info); /**< Hardware Symmetric Encryption and Decryption Callback */ +}; + +/** + * @brief Hardware driver usage, including input and output information + */ +struct hwcrypto_symmetric_info +{ + hwcrypto_mode mode; /**< crypto mode. HWCRYPTO_MODE_ENCRYPT or HWCRYPTO_MODE_DECRYPT */ + const rt_uint8_t *in; /**< Input data */ + rt_uint8_t *out; /**< Output data will be written */ + rt_size_t length; /**< The length of the input data in Bytes. It's a multiple of block size. */ +}; + +/** + * @brief Symmetric crypto context. Hardware driver usage + */ +struct hwcrypto_symmetric +{ + struct rt_hwcrypto_ctx parent; /**< Inheritance from hardware crypto context */ + rt_uint32_t flags; /**< key or iv or ivoff has been changed. The flag will be set up */ + rt_int32_t iv_len; /**< initialization vector effective length */ + rt_int32_t iv_off; /**< The offset in IV */ + rt_uint8_t iv[RT_HWCRYPTO_IV_MAX_SIZE]; /**< The initialization vector */ + rt_uint8_t key[RT_HWCRYPTO_KEYBIT_MAX_SIZE >> 3]; /**< The crypto key */ + rt_int32_t key_bitlen; /**< The crypto key bit length */ + const struct hwcrypto_symmetric_ops *ops; /**< !! Hardware initializes this value when creating context !! */ +}; + +/** + * @brief Creating Symmetric Encryption and Decryption Context + * + * @param device Hardware crypto device + * @param type Type of symmetric crypto context + * + * @return Symmetric crypto context + */ +struct rt_hwcrypto_ctx *rt_hwcrypto_symmetric_create(struct rt_hwcrypto_device *device, + hwcrypto_type type); + +/** + * @brief Destroy Symmetric Encryption and Decryption Context + * + * @param ctx Symmetric crypto context + */ +void rt_hwcrypto_symmetric_destroy(struct rt_hwcrypto_ctx *ctx); + +/** + * @brief This function performs a symmetric encryption or decryption operation + * + * @param ctx Symmetric crypto context + * @param mode Operation mode. HWCRYPTO_MODE_ENCRYPT or HWCRYPTO_MODE_DECRYPT + * @param length The length of the input data in Bytes. This must be a multiple of the block size + * @param in The buffer holding the input data + * @param out The buffer holding the output data + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_symmetric_crypt(struct rt_hwcrypto_ctx *ctx, hwcrypto_mode mode, + rt_size_t length, const rt_uint8_t *in, rt_uint8_t *out); + +/** + * @brief Set Symmetric Encryption and Decryption Key + * + * @param ctx Symmetric crypto context + * @param key The crypto key + * @param bitlen The crypto key bit length + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_symmetric_setkey(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *key, rt_uint32_t bitlen); + +/** + * @brief Get Symmetric Encryption and Decryption Key + * + * @param ctx Symmetric crypto context + * @param key The crypto key buffer + * @param bitlen The crypto key bit length + * + * @return Key length of copy + */ +int rt_hwcrypto_symmetric_getkey(struct rt_hwcrypto_ctx *ctx, rt_uint8_t *key, rt_uint32_t bitlen); + +/** + * @brief Set Symmetric Encryption and Decryption initialization vector + * + * @param ctx Symmetric crypto context + * @param iv The crypto initialization vector + * @param len The crypto initialization vector length + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_symmetric_setiv(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *iv, rt_size_t len); + +/** + * @brief Get Symmetric Encryption and Decryption initialization vector + * + * @param ctx Symmetric crypto context + * @param iv The crypto initialization vector buffer + * @param len The crypto initialization vector buffer length + * + * @return IV length of copy + */ +int rt_hwcrypto_symmetric_getiv(struct rt_hwcrypto_ctx *ctx, rt_uint8_t *iv, rt_size_t len); + +/** + * @brief Set offset in initialization vector + * + * @param ctx Symmetric crypto context + * @param iv_off The offset in IV + */ +void rt_hwcrypto_symmetric_set_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t iv_off); + +/** + * @brief Get offset in initialization vector + * + * @param ctx Symmetric crypto context + * @param iv_off It must point to a valid memory + */ +void rt_hwcrypto_symmetric_get_ivoff(struct rt_hwcrypto_ctx *ctx, rt_int32_t *iv_off); + +/** + * @brief This function copy symmetric crypto context + * + * @param des The destination symmetric crypto context + * @param src The symmetric crypto context to be copy + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_symmetric_cpy(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src); + +/** + * @brief Reset symmetric crypto context + * + * @param ctx Symmetric crypto context + */ +void rt_hwcrypto_symmetric_reset(struct rt_hwcrypto_ctx *ctx); + +/** + * @brief Setting symmetric crypto context type + * + * @param ctx Symmetric crypto context + * @param type Types of settings + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_symmetric_set_type(struct rt_hwcrypto_ctx *ctx, hwcrypto_type type); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/components/drivers/hwcrypto/hwcrypto.c b/components/drivers/hwcrypto/hwcrypto.c new file mode 100644 index 0000000000..3dc5649cd6 --- /dev/null +++ b/components/drivers/hwcrypto/hwcrypto.c @@ -0,0 +1,255 @@ +/* + * Copyright (c) 2006-2019, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-04-23 tyx the first version + */ + +#include +#include +#include + +/** + * @brief Setting context type (Direct calls are not recommended) + * + * @param ctx Crypto context + * @param type Types of settings + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_set_type(struct rt_hwcrypto_ctx *ctx, hwcrypto_type type) +{ + if (ctx) + { + /* Is it the same category? */ + if ((ctx->type & HWCRYPTO_MAIN_TYPE_MASK) == (type & HWCRYPTO_MAIN_TYPE_MASK)) + { + ctx->type = type; + return RT_EOK; + } + /* Context is empty type */ + else if (ctx->type == HWCRYPTO_TYPE_NULL) + { + ctx->type = type; + return RT_EOK; + } + else + { + return -RT_ERROR; + } + } + return -RT_EINVAL; +} + +/** + * @brief Reset context type (Direct calls are not recommended) + * + * @param ctx Crypto context + * + */ +void rt_hwcrypto_ctx_reset(struct rt_hwcrypto_ctx *ctx) +{ + if (ctx && ctx->device->ops->reset) + { + ctx->device->ops->reset(ctx); + } +} + +/** + * @brief Init crypto context + * + * @param ctx The context to initialize + * @param device Hardware crypto device + * @param type Type of context + * @param obj_size Size of context object + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_ctx_init(struct rt_hwcrypto_ctx *ctx, struct rt_hwcrypto_device *device, hwcrypto_type type) +{ + rt_err_t err; + + /* Setting context type */ + rt_hwcrypto_set_type(ctx, type); + ctx->device = device; + /* Create hardware context */ + err = ctx->device->ops->create(ctx); + if (err != RT_EOK) + { + return err; + } + return RT_EOK; +} + +/** + * @brief Create crypto context + * + * @param device Hardware crypto device + * @param type Type of context + * @param obj_size Size of context object + * + * @return Crypto context + */ +struct rt_hwcrypto_ctx *rt_hwcrypto_ctx_create(struct rt_hwcrypto_device *device, hwcrypto_type type, rt_uint32_t obj_size) +{ + struct rt_hwcrypto_ctx *ctx; + rt_err_t err; + + /* Parameter checking */ + if (device == RT_NULL || obj_size < sizeof(struct rt_hwcrypto_ctx)) + { + return RT_NULL; + } + ctx = rt_malloc(obj_size); + if (ctx == RT_NULL) + { + return ctx; + } + rt_memset(ctx, 0, obj_size); + /* Init context */ + err = rt_hwcrypto_ctx_init(ctx, device, type); + if (err != RT_EOK) + { + rt_free(ctx); + ctx = RT_NULL; + } + return ctx; +} + +/** + * @brief Destroy crypto context + * + * @param device Crypto context + */ +void rt_hwcrypto_ctx_destroy(struct rt_hwcrypto_ctx *ctx) +{ + if (ctx == RT_NULL) + { + return; + } + /* Destroy hardware context */ + if (ctx->device->ops->destroy) + { + ctx->device->ops->destroy(ctx); + } + /* Free the resources */ + rt_free(ctx); +} + +/** + * @brief Copy crypto context + * + * @param des The destination context + * @param src The context to be copy + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_ctx_cpy(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src) +{ + if (des == RT_NULL || src == RT_NULL) + { + return -RT_EINVAL; + } + + /* The equipment is different or of different types and cannot be copied */ + if (des->device != src->device || + (des->type & HWCRYPTO_MAIN_TYPE_MASK) != (src->type & HWCRYPTO_MAIN_TYPE_MASK)) + { + return -RT_EINVAL; + } + des->type = src->type; + /* Calling Hardware Context Copy Function */ + return src->device->ops->copy(des, src); +} + +/** + * @brief Get the default hardware crypto device + * + * @return Hardware crypto device + * + */ +struct rt_hwcrypto_device *rt_hwcrypto_dev_dufault(void) +{ + static struct rt_hwcrypto_device *hwcrypto_dev; + + /* If there is a default device, return the device */ + if (hwcrypto_dev) + { + return hwcrypto_dev; + } + /* Find by default device name */ + hwcrypto_dev = (struct rt_hwcrypto_device *)rt_device_find(RT_HWCRYPTO_DEFAULT_NAME); + return hwcrypto_dev; +} + +/** + * @brief Get the unique ID of the device + * + * @param device Device object + * + * @return Device unique ID + */ +rt_uint64_t rt_hwcrypto_id(struct rt_hwcrypto_device *device) +{ + if (device) + { + return device->id; + } + return 0; +} + +#ifdef RT_USING_DEVICE_OPS +const static struct rt_device_ops hwcrypto_ops = +{ + RT_NULL, + RT_NULL, + RT_NULL, + RT_NULL, + RT_NULL, + RT_NULL +}; +#endif + +/** + * @brief Register hardware crypto device + * + * @param device Hardware crypto device + * @param name Name of device + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_register(struct rt_hwcrypto_device *device, const char *name) +{ + rt_err_t err; + + RT_ASSERT(device != RT_NULL); + RT_ASSERT(name != RT_NULL); + RT_ASSERT(device->ops != RT_NULL); + RT_ASSERT(device->ops->create != RT_NULL); + RT_ASSERT(device->ops->destroy != RT_NULL); + RT_ASSERT(device->ops->copy != RT_NULL); + RT_ASSERT(device->ops->reset != RT_NULL); + + rt_memset(&device->parent, 0, sizeof(struct rt_device)); +#ifdef RT_USING_DEVICE_OPS + device->parent.ops = &hwcrypto_ops; +#else + device->parent.init = RT_NULL; + device->parent.open = RT_NULL; + device->parent.close = RT_NULL; + device->parent.read = RT_NULL; + device->parent.write = RT_NULL; + device->parent.control = RT_NULL; +#endif + + device->parent.user_data = RT_NULL; + device->parent.type = RT_Device_Class_Miscellaneous; + + /* Register device */ + err = rt_device_register(&device->parent, name, RT_DEVICE_FLAG_RDWR); + + return err; +} diff --git a/components/drivers/hwcrypto/hwcrypto.h b/components/drivers/hwcrypto/hwcrypto.h new file mode 100644 index 0000000000..944fb03c14 --- /dev/null +++ b/components/drivers/hwcrypto/hwcrypto.h @@ -0,0 +1,193 @@ +/* + * Copyright (c) 2006-2019, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-04-23 tyx the first version + */ + +#ifndef __HWCRYPTO_H__ +#define __HWCRYPTO_H__ + +#include + +#ifndef RT_HWCRYPTO_DEFAULT_NAME +#define RT_HWCRYPTO_DEFAULT_NAME ("hwcryto") +#endif + +#define HWCRYPTO_MAIN_TYPE_MASK (0xffffUL << 16) +#define HWCRYPTO_SUB_TYPE_MASK (0xffUL << 8) + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum +{ + HWCRYPTO_TYPE_NULL = 0x00000000, + + /* Main Type */ + /* symmetric Type */ + HWCRYPTO_TYPE_HEAD = __LINE__, + HWCRYPTO_TYPE_AES = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16, /**< AES */ + HWCRYPTO_TYPE_DES = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16, /**< DES */ + HWCRYPTO_TYPE_3DES = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16, /**< 3DES */ + HWCRYPTO_TYPE_RC4 = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16, /**< RC4 */ + HWCRYPTO_TYPE_GCM = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16, /**< GCM */ + /* HASH Type */ + HWCRYPTO_TYPE_MD5 = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16, /**< MD5 */ + HWCRYPTO_TYPE_SHA1 = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16, /**< SHA1 */ + HWCRYPTO_TYPE_SHA2 = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16, /**< SHA2 */ + /* Other Type */ + HWCRYPTO_TYPE_RNG = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16, /**< RNG */ + HWCRYPTO_TYPE_CRC = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16, /**< CRC */ + HWCRYPTO_TYPE_BIGNUM = ((__LINE__ - HWCRYPTO_TYPE_HEAD) & 0xffff) << 16, /**< BIGNUM */ + + /* AES Subtype */ + HWCRYPTO_TYPE_AES_ECB = HWCRYPTO_TYPE_AES | (0x01 << 8), + HWCRYPTO_TYPE_AES_CBC = HWCRYPTO_TYPE_AES | (0x02 << 8), + HWCRYPTO_TYPE_AES_CFB = HWCRYPTO_TYPE_AES | (0x03 << 8), + HWCRYPTO_TYPE_AES_CTR = HWCRYPTO_TYPE_AES | (0x04 << 8), + HWCRYPTO_TYPE_AES_OFB = HWCRYPTO_TYPE_AES | (0x05 << 8), + + /* DES Subtype */ + HWCRYPTO_TYPE_DES_ECB = HWCRYPTO_TYPE_DES | (0x01 << 8), + HWCRYPTO_TYPE_DES_CBC = HWCRYPTO_TYPE_DES | (0x02 << 8), + + /* 3DES Subtype */ + HWCRYPTO_TYPE_3DES_ECB = HWCRYPTO_TYPE_3DES | (0x01 << 8), + HWCRYPTO_TYPE_3DES_CBC = HWCRYPTO_TYPE_3DES | (0x02 << 8), + + /* SHA2 Subtype */ + HWCRYPTO_TYPE_SHA224 = HWCRYPTO_TYPE_SHA2 | (0x01 << 8), + HWCRYPTO_TYPE_SHA256 = HWCRYPTO_TYPE_SHA2 | (0x02 << 8), + HWCRYPTO_TYPE_SHA384 = HWCRYPTO_TYPE_SHA2 | (0x03 << 8), + HWCRYPTO_TYPE_SHA512 = HWCRYPTO_TYPE_SHA2 | (0x04 << 8), +} hwcrypto_type; + +typedef enum +{ + HWCRYPTO_MODE_ENCRYPT = 0x1, /**< Encryption operations */ + HWCRYPTO_MODE_DECRYPT = 0x2, /**< Decryption operations */ + HWCRYPTO_MODE_UNKNOWN = 0x7fffffff, /**< Unknown */ +} hwcrypto_mode; + +struct rt_hwcrypto_ctx; + +struct rt_hwcrypto_ops +{ + rt_err_t (*create)(struct rt_hwcrypto_ctx *ctx); /**< Creating hardware context */ + void (*destroy)(struct rt_hwcrypto_ctx *ctx); /**< Delete hardware context */ + rt_err_t (*copy)(struct rt_hwcrypto_ctx *des, + const struct rt_hwcrypto_ctx *src); /**< Cpoy hardware context */ + void (*reset)(struct rt_hwcrypto_ctx *ctx); /**< Reset hardware context */ +}; + +struct rt_hwcrypto_device +{ + struct rt_device parent; /**< Inherited from the standard device */ + const struct rt_hwcrypto_ops *ops; /**< Hardware crypto ops */ + rt_uint64_t id; /**< Unique id */ + void *user_data; /**< Device user data */ +}; + +struct rt_hwcrypto_ctx +{ + struct rt_hwcrypto_device *device; /**< Binding device */ + hwcrypto_type type; /**< Encryption and decryption types */ + void *contex; /**< Hardware context */ +}; + +/** + * @brief Setting context type (Direct calls are not recommended) + * + * @param ctx Crypto context + * @param type Types of settings + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_set_type(struct rt_hwcrypto_ctx *ctx, hwcrypto_type type); + +/** + * @brief Reset context type (Direct calls are not recommended) + * + * @param ctx Crypto context + */ +void rt_hwcrypto_ctx_reset(struct rt_hwcrypto_ctx *ctx); + +/** + * @brief Init crypto context (Direct calls are not recommended) + * + * @param ctx The context to initialize + * @param device Hardware crypto device + * @param type Type of context + * @param obj_size Size of context object + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_ctx_init(struct rt_hwcrypto_ctx *ctx, + struct rt_hwcrypto_device *device, hwcrypto_type type); + +/** + * @brief Create crypto context (Direct calls are not recommended) + * + * @param device Hardware crypto device + * @param type Type of context + * @param obj_size Size of context object + * + * @return Crypto context + */ +struct rt_hwcrypto_ctx *rt_hwcrypto_ctx_create(struct rt_hwcrypto_device *device, + hwcrypto_type type, rt_uint32_t obj_size); + +/** + * @brief Destroy crypto context (Direct calls are not recommended) + * + * @param device Crypto context + */ +void rt_hwcrypto_ctx_destroy(struct rt_hwcrypto_ctx *ctx); + +/** + * @brief Copy crypto context (Direct calls are not recommended) + * + * @param des The destination context + * @param src The context to be copy + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_ctx_cpy(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src); + +/** + * @brief Register hardware crypto device + * + * @param device Hardware crypto device + * @param name Name of device + * + * @return RT_EOK on success. + */ +rt_err_t rt_hwcrypto_register(struct rt_hwcrypto_device *device, const char *name); + +/** + * @brief Get the default hardware crypto device + * + * @return Hardware crypto device + * + */ +struct rt_hwcrypto_device *rt_hwcrypto_dev_dufault(void); + +/** + * @brief Get the unique ID of the device + * + * @param device Device object + * + * @return Device unique ID + */ +rt_uint64_t rt_hwcrypto_id(struct rt_hwcrypto_device *device); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/components/drivers/include/drivers/crypto.h b/components/drivers/include/drivers/crypto.h new file mode 100644 index 0000000000..5341d9ee48 --- /dev/null +++ b/components/drivers/include/drivers/crypto.h @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-05-17 tyx the first version + */ + +#ifndef __CRYPTO_H__ +#define __CRYPTO_H__ + +#include +#include +#include +#include +#include +#include +#include +#include + +#endif diff --git a/components/drivers/include/rtdevice.h b/components/drivers/include/rtdevice.h index d59b40cc47..f764d30c24 100644 --- a/components/drivers/include/rtdevice.h +++ b/components/drivers/include/rtdevice.h @@ -122,6 +122,10 @@ extern "C" { #include "drivers/mtdnand.h" #endif +#ifdef RT_USING_HWCRYPTO +#include "drivers/crypto.h" +#endif + #ifdef __cplusplus } #endif -- 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 'components/drivers/include') 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 c421757cc4fbe787345acfa1366df9d992f3f197 Mon Sep 17 00:00:00 2001 From: guozhanxin Date: Wed, 19 Jun 2019 17:14:44 +0800 Subject: [driver][mtd_nand] Formatting code.|格式化代码。 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/include/drivers/mtd_nand.h | 50 ++-- components/drivers/mtd/mtd_nand.c | 318 +++++++++++++------------- 2 files changed, 184 insertions(+), 184 deletions(-) (limited to 'components/drivers/include') diff --git a/components/drivers/include/drivers/mtd_nand.h b/components/drivers/include/drivers/mtd_nand.h index 8afed000dd..db5e61043a 100644 --- a/components/drivers/include/drivers/mtd_nand.h +++ b/components/drivers/include/drivers/mtd_nand.h @@ -45,71 +45,71 @@ struct rt_mtd_nand_device rt_uint32_t block_end; /* The end of available block */ /* operations interface */ - const struct rt_mtd_nand_driver_ops* ops; + const struct rt_mtd_nand_driver_ops *ops; }; struct rt_mtd_nand_driver_ops { - rt_err_t (*read_id) (struct rt_mtd_nand_device* device); + rt_err_t (*read_id)(struct rt_mtd_nand_device *device); - rt_err_t (*read_page)(struct rt_mtd_nand_device* device, + rt_err_t (*read_page)(struct rt_mtd_nand_device *device, rt_off_t page, - rt_uint8_t* data, rt_uint32_t data_len, - rt_uint8_t * spare, rt_uint32_t spare_len); + rt_uint8_t *data, rt_uint32_t data_len, + rt_uint8_t *spare, rt_uint32_t spare_len); - rt_err_t (*write_page)(struct rt_mtd_nand_device * device, + rt_err_t (*write_page)(struct rt_mtd_nand_device *device, rt_off_t page, - const rt_uint8_t * data, rt_uint32_t data_len, - const rt_uint8_t * spare, rt_uint32_t spare_len); - rt_err_t (*move_page) (struct rt_mtd_nand_device *device, rt_off_t src_page, rt_off_t dst_page); + const rt_uint8_t *data, rt_uint32_t data_len, + const rt_uint8_t *spare, rt_uint32_t spare_len); + rt_err_t (*move_page)(struct rt_mtd_nand_device *device, rt_off_t src_page, rt_off_t dst_page); - rt_err_t (*erase_block)(struct rt_mtd_nand_device* device, rt_uint32_t block); - rt_err_t (*check_block)(struct rt_mtd_nand_device* device, rt_uint32_t block); - rt_err_t (*mark_badblock)(struct rt_mtd_nand_device* device, rt_uint32_t block); + rt_err_t (*erase_block)(struct rt_mtd_nand_device *device, rt_uint32_t block); + rt_err_t (*check_block)(struct rt_mtd_nand_device *device, rt_uint32_t block); + rt_err_t (*mark_badblock)(struct rt_mtd_nand_device *device, rt_uint32_t block); }; -rt_err_t rt_mtd_nand_register_device(const char* name, struct rt_mtd_nand_device* device); +rt_err_t rt_mtd_nand_register_device(const char *name, struct rt_mtd_nand_device *device); -rt_inline rt_uint32_t rt_mtd_nand_read_id(struct rt_mtd_nand_device* device) +rt_inline rt_uint32_t rt_mtd_nand_read_id(struct rt_mtd_nand_device *device) { return device->ops->read_id(device); } rt_inline rt_err_t rt_mtd_nand_read( - struct rt_mtd_nand_device* device, + struct rt_mtd_nand_device *device, rt_off_t page, - rt_uint8_t* data, rt_uint32_t data_len, - rt_uint8_t * spare, rt_uint32_t spare_len) + rt_uint8_t *data, rt_uint32_t data_len, + rt_uint8_t *spare, rt_uint32_t spare_len) { return device->ops->read_page(device, page, data, data_len, spare, spare_len); } rt_inline rt_err_t rt_mtd_nand_write( - struct rt_mtd_nand_device* device, + struct rt_mtd_nand_device *device, rt_off_t page, - const rt_uint8_t* data, rt_uint32_t data_len, - const rt_uint8_t * spare, rt_uint32_t spare_len) + const rt_uint8_t *data, rt_uint32_t data_len, + const rt_uint8_t *spare, rt_uint32_t spare_len) { return device->ops->write_page(device, page, data, data_len, spare, spare_len); } -rt_inline rt_err_t rt_mtd_nand_move_page(struct rt_mtd_nand_device* device, - rt_off_t src_page, rt_off_t dst_page) +rt_inline rt_err_t rt_mtd_nand_move_page(struct rt_mtd_nand_device *device, + rt_off_t src_page, rt_off_t dst_page) { return device->ops->move_page(device, src_page, dst_page); } -rt_inline rt_err_t rt_mtd_nand_erase_block(struct rt_mtd_nand_device* device, rt_uint32_t block) +rt_inline rt_err_t rt_mtd_nand_erase_block(struct rt_mtd_nand_device *device, rt_uint32_t block) { return device->ops->erase_block(device, block); } -rt_inline rt_err_t rt_mtd_nand_check_block(struct rt_mtd_nand_device* device, rt_uint32_t block) +rt_inline rt_err_t rt_mtd_nand_check_block(struct rt_mtd_nand_device *device, rt_uint32_t block) { return device->ops->check_block(device, block); } -rt_inline rt_err_t rt_mtd_nand_mark_badblock(struct rt_mtd_nand_device* device, rt_uint32_t block) +rt_inline rt_err_t rt_mtd_nand_mark_badblock(struct rt_mtd_nand_device *device, rt_uint32_t block) { return device->ops->mark_badblock(device, block); } diff --git a/components/drivers/mtd/mtd_nand.c b/components/drivers/mtd/mtd_nand.c index 3e4582ceaf..ffcb398aa9 100644 --- a/components/drivers/mtd/mtd_nand.c +++ b/components/drivers/mtd/mtd_nand.c @@ -56,7 +56,7 @@ static rt_err_t _mtd_control(rt_device_t dev, int cmd, void *args) } #ifdef RT_USING_DEVICE_OPS -const static struct rt_device_ops mtd_nand_ops = +const static struct rt_device_ops mtd_nand_ops = { _mtd_init, _mtd_open, @@ -78,7 +78,7 @@ rt_err_t rt_mtd_nand_register_device(const char *name, /* set device class and generic device interface */ dev->type = RT_Device_Class_MTD; #ifdef RT_USING_DEVICE_OPS - dev->ops = &mtd_nand_ops; + dev->ops = &mtd_nand_ops; #else dev->init = _mtd_init; dev->open = _mtd_open; @@ -101,186 +101,186 @@ rt_err_t rt_mtd_nand_register_device(const char *name, static void mtd_dump_hex(const rt_uint8_t *ptr, rt_size_t buflen) { - unsigned char *buf = (unsigned char*)ptr; - int i, j; - for (i=0; ipage_size + nand->oob_size); - if (page_ptr == RT_NULL) - { - rt_kprintf("out of memory!\n"); - return -RT_ENOMEM; - } - - oob_ptr = page_ptr + nand->page_size; - rt_memset(page_ptr, 0xff, nand->page_size + nand->oob_size); - - /* calculate the page number */ - page = block * nand->pages_per_block + page; - result = rt_mtd_nand_read(nand, page, page_ptr, nand->page_size, - oob_ptr, nand->oob_size); - - rt_kprintf("read page, rc=%d\n", result); - mtd_dump_hex(page_ptr, nand->page_size); - mtd_dump_hex(oob_ptr, nand->oob_size); - - rt_free(page_ptr); - return 0; + rt_err_t result; + rt_uint8_t *page_ptr; + rt_uint8_t *oob_ptr; + struct rt_mtd_nand_device *nand; + + nand = RT_MTD_NAND_DEVICE(rt_device_find(name)); + if (nand == RT_NULL) + { + rt_kprintf("no nand device found!\n"); + return -RT_ERROR; + } + + page_ptr = rt_malloc(nand->page_size + nand->oob_size); + if (page_ptr == RT_NULL) + { + rt_kprintf("out of memory!\n"); + return -RT_ENOMEM; + } + + oob_ptr = page_ptr + nand->page_size; + rt_memset(page_ptr, 0xff, nand->page_size + nand->oob_size); + + /* calculate the page number */ + page = block * nand->pages_per_block + page; + result = rt_mtd_nand_read(nand, page, page_ptr, nand->page_size, + oob_ptr, nand->oob_size); + + rt_kprintf("read page, rc=%d\n", result); + mtd_dump_hex(page_ptr, nand->page_size); + mtd_dump_hex(oob_ptr, nand->oob_size); + + rt_free(page_ptr); + return 0; } FINSH_FUNCTION_EXPORT_ALIAS(mtd_nand_read, nand_read, read page in nand - nand_read(name, block, page)); -int mtd_nand_readoob(const char* name, int block, int page) +int mtd_nand_readoob(const char *name, int block, int page) { - struct rt_mtd_nand_device *nand; - rt_uint8_t *oob_ptr; - - nand = RT_MTD_NAND_DEVICE(rt_device_find(name)); - if (nand == RT_NULL) - { - rt_kprintf("no nand device found!\n"); - return -RT_ERROR; - } - - oob_ptr = rt_malloc(nand->oob_size); - if (oob_ptr == RT_NULL) - { - rt_kprintf("out of memory!\n"); - return -RT_ENOMEM; - } - - /* calculate the page number */ - page = block * nand->pages_per_block + page; - rt_mtd_nand_read(nand, page, RT_NULL, nand->page_size, - oob_ptr, nand->oob_size); - mtd_dump_hex(oob_ptr, nand->oob_size); - - rt_free(oob_ptr); - return 0; + struct rt_mtd_nand_device *nand; + rt_uint8_t *oob_ptr; + + nand = RT_MTD_NAND_DEVICE(rt_device_find(name)); + if (nand == RT_NULL) + { + rt_kprintf("no nand device found!\n"); + return -RT_ERROR; + } + + oob_ptr = rt_malloc(nand->oob_size); + if (oob_ptr == RT_NULL) + { + rt_kprintf("out of memory!\n"); + return -RT_ENOMEM; + } + + /* calculate the page number */ + page = block * nand->pages_per_block + page; + rt_mtd_nand_read(nand, page, RT_NULL, nand->page_size, + oob_ptr, nand->oob_size); + mtd_dump_hex(oob_ptr, nand->oob_size); + + rt_free(oob_ptr); + return 0; } FINSH_FUNCTION_EXPORT_ALIAS(mtd_nand_readoob, nand_readoob, read spare data in nand - nand_readoob(name, block, page)); -int mtd_nand_write(const char* name, int block, int page) +int mtd_nand_write(const char *name, int block, int page) { - rt_err_t result; - rt_uint8_t *page_ptr; - rt_uint8_t *oob_ptr; - rt_uint32_t index; - struct rt_mtd_nand_device *nand; - - nand = RT_MTD_NAND_DEVICE(rt_device_find(name)); - if (nand == RT_NULL) - { - rt_kprintf("no nand device found!\n"); - return -RT_ERROR; - } - - page_ptr = rt_malloc(nand->page_size + nand->oob_size); - if (page_ptr == RT_NULL) - { - rt_kprintf("out of memory!\n"); - return -RT_ENOMEM; - } - - oob_ptr = page_ptr + nand->page_size; - /* prepare page data */ - for (index = 0; index < nand->page_size; index ++) - { - page_ptr[index] = index & 0xff; - } - /* prepare oob data */ - for (index = 0; index < nand->oob_size; index ++) - { - oob_ptr[index] = index & 0xff; - } - - /* calculate the page number */ - page = block * nand->pages_per_block + page; - result = rt_mtd_nand_write(nand, page, page_ptr, nand->page_size, - oob_ptr, nand->oob_size); - if (result != RT_MTD_EOK) - { - rt_kprintf("write page failed!, rc=%d\n", result); - } - - rt_free(page_ptr); - return 0; + rt_err_t result; + rt_uint8_t *page_ptr; + rt_uint8_t *oob_ptr; + rt_uint32_t index; + struct rt_mtd_nand_device *nand; + + nand = RT_MTD_NAND_DEVICE(rt_device_find(name)); + if (nand == RT_NULL) + { + rt_kprintf("no nand device found!\n"); + return -RT_ERROR; + } + + page_ptr = rt_malloc(nand->page_size + nand->oob_size); + if (page_ptr == RT_NULL) + { + rt_kprintf("out of memory!\n"); + return -RT_ENOMEM; + } + + oob_ptr = page_ptr + nand->page_size; + /* prepare page data */ + for (index = 0; index < nand->page_size; index ++) + { + page_ptr[index] = index & 0xff; + } + /* prepare oob data */ + for (index = 0; index < nand->oob_size; index ++) + { + oob_ptr[index] = index & 0xff; + } + + /* calculate the page number */ + page = block * nand->pages_per_block + page; + result = rt_mtd_nand_write(nand, page, page_ptr, nand->page_size, + oob_ptr, nand->oob_size); + if (result != RT_MTD_EOK) + { + rt_kprintf("write page failed!, rc=%d\n", result); + } + + rt_free(page_ptr); + return 0; } FINSH_FUNCTION_EXPORT_ALIAS(mtd_nand_write, nand_write, write dump data to nand - nand_write(name, block, page)); -int mtd_nand_erase(const char* name, int block) +int mtd_nand_erase(const char *name, int block) { - struct rt_mtd_nand_device *nand; - nand = RT_MTD_NAND_DEVICE(rt_device_find(name)); - if (nand == RT_NULL) - { - rt_kprintf("no nand device found!\n"); - return -RT_ERROR; - } - - return rt_mtd_nand_erase_block(nand, block); + struct rt_mtd_nand_device *nand; + nand = RT_MTD_NAND_DEVICE(rt_device_find(name)); + if (nand == RT_NULL) + { + rt_kprintf("no nand device found!\n"); + return -RT_ERROR; + } + + return rt_mtd_nand_erase_block(nand, block); } FINSH_FUNCTION_EXPORT_ALIAS(mtd_nand_erase, nand_erase, nand_erase(name, block)); -int mtd_nand_erase_all(const char* name) +int mtd_nand_erase_all(const char *name) { - rt_uint32_t index = 0; - struct rt_mtd_nand_device *nand; - - nand = RT_MTD_NAND_DEVICE(rt_device_find(name)); - if (nand == RT_NULL) - { - rt_kprintf("no nand device found!\n"); - return -RT_ERROR; - } - - for (index = 0; index < (nand->block_end - nand->block_start); index ++) - { - rt_mtd_nand_erase_block(nand, index); - } - - return 0; + rt_uint32_t index = 0; + struct rt_mtd_nand_device *nand; + + nand = RT_MTD_NAND_DEVICE(rt_device_find(name)); + if (nand == RT_NULL) + { + rt_kprintf("no nand device found!\n"); + return -RT_ERROR; + } + + for (index = 0; index < (nand->block_end - nand->block_start); index ++) + { + rt_mtd_nand_erase_block(nand, index); + } + + return 0; } FINSH_FUNCTION_EXPORT_ALIAS(mtd_nand_erase_all, nand_erase_all, erase all of nand device - nand_erase_all(name, block)); #endif -- cgit v1.3.1 From 608ead86f459055ab63a2eabfa5ceda4b4a1fc56 Mon Sep 17 00:00:00 2001 From: guozhanxin Date: Wed, 19 Jun 2019 17:18:21 +0800 Subject: [driver][nand] Fix hardfault errors.|修复 hardfault 错误。 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/include/drivers/mtd_nand.h | 38 ++++++++++++++++++++------- 1 file changed, 29 insertions(+), 9 deletions(-) (limited to 'components/drivers/include') diff --git a/components/drivers/include/drivers/mtd_nand.h b/components/drivers/include/drivers/mtd_nand.h index db5e61043a..f7675aede6 100644 --- a/components/drivers/include/drivers/mtd_nand.h +++ b/components/drivers/include/drivers/mtd_nand.h @@ -21,13 +21,13 @@ struct rt_mtd_nand_driver_ops; #define RT_MTD_NAND_DEVICE(device) ((struct rt_mtd_nand_device*)(device)) -#define RT_MTD_EOK 0 /* NO error */ -#define RT_MTD_EECC 1 /* ECC error */ -#define RT_MTD_EBUSY 2 /* hardware busy */ -#define RT_MTD_EIO 3 /* generic IO issue */ -#define RT_MTD_ENOMEM 4 /* out of memory */ -#define RT_MTD_ESRC 5 /* source issue */ -#define RT_MTD_EECC_CORRECT 6 /* ECC error but correct */ +#define RT_MTD_EOK 0 /* NO error */ +#define RT_MTD_EECC 101 /* ECC error */ +#define RT_MTD_EBUSY 102 /* hardware busy */ +#define RT_MTD_EIO 103 /* generic IO issue */ +#define RT_MTD_ENOMEM 104 /* out of memory */ +#define RT_MTD_ESRC 105 /* source issue */ +#define RT_MTD_EECC_CORRECT 106 /* ECC error but correct */ struct rt_mtd_nand_device { @@ -41,6 +41,7 @@ struct rt_mtd_nand_device rt_uint32_t pages_per_block; /* The number of page a block */ rt_uint16_t block_total; + /* Only be touched by driver */ rt_uint32_t block_start; /* The start of available block*/ rt_uint32_t block_end; /* The end of available block */ @@ -72,6 +73,7 @@ rt_err_t rt_mtd_nand_register_device(const char *name, struct rt_mtd_nand_device rt_inline rt_uint32_t rt_mtd_nand_read_id(struct rt_mtd_nand_device *device) { + RT_ASSERT(device->ops->read_id); return device->ops->read_id(device); } @@ -81,6 +83,7 @@ rt_inline rt_err_t rt_mtd_nand_read( rt_uint8_t *data, rt_uint32_t data_len, rt_uint8_t *spare, rt_uint32_t spare_len) { + RT_ASSERT(device->ops->read_page); return device->ops->read_page(device, page, data, data_len, spare, spare_len); } @@ -90,28 +93,45 @@ rt_inline rt_err_t rt_mtd_nand_write( const rt_uint8_t *data, rt_uint32_t data_len, const rt_uint8_t *spare, rt_uint32_t spare_len) { + RT_ASSERT(device->ops->write_page); return device->ops->write_page(device, page, data, data_len, spare, spare_len); } rt_inline rt_err_t rt_mtd_nand_move_page(struct rt_mtd_nand_device *device, rt_off_t src_page, rt_off_t dst_page) { + RT_ASSERT(device->ops->move_page); return device->ops->move_page(device, src_page, dst_page); } rt_inline rt_err_t rt_mtd_nand_erase_block(struct rt_mtd_nand_device *device, rt_uint32_t block) { + RT_ASSERT(device->ops->erase_block); return device->ops->erase_block(device, block); } rt_inline rt_err_t rt_mtd_nand_check_block(struct rt_mtd_nand_device *device, rt_uint32_t block) { - return device->ops->check_block(device, block); + if (device->ops->check_block) + { + return device->ops->check_block(device, block); + } + else + { + return -RT_ENOSYS; + } } rt_inline rt_err_t rt_mtd_nand_mark_badblock(struct rt_mtd_nand_device *device, rt_uint32_t block) { - return device->ops->mark_badblock(device, block); + if (device->ops->mark_badblock) + { + return device->ops->mark_badblock(device, block); + } + else + { + return -RT_ENOSYS; + } } #endif /* MTD_NAND_H_ */ -- cgit v1.3.1 From 0c07c2005d8c1c3613a76b843f8fc29cc7a636bc Mon Sep 17 00:00:00 2001 From: BernardXiong Date: Thu, 27 Jun 2019 07:36:25 +0000 Subject: [DeviceDrivers] Remove backup mtd code --- components/drivers/Kconfig | 14 - components/drivers/include/drivers/mtd.h | 105 ----- components/drivers/include/drivers/mtdnand.h | 101 ----- components/drivers/include/drivers/mtdnor.h | 35 -- components/drivers/mtd/mtd.c | 236 ---------- components/drivers/mtd/mtdnand.c | 645 --------------------------- components/drivers/mtd/mtdnor.c | 74 --- 7 files changed, 1210 deletions(-) delete mode 100644 components/drivers/include/drivers/mtd.h delete mode 100644 components/drivers/include/drivers/mtdnand.h delete mode 100644 components/drivers/include/drivers/mtdnor.h delete mode 100644 components/drivers/mtd/mtd.c delete mode 100644 components/drivers/mtd/mtdnand.c delete mode 100644 components/drivers/mtd/mtdnor.c (limited to 'components/drivers/include') diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index 43729875b4..d4f9f5c506 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -119,20 +119,6 @@ config RT_USING_MTD_NAND default n endif -config RT_USING_MTD - bool "Using Memory Technology Device (MTD)" - default n - - if RT_USING_MTD - config MTD_USING_NOR - bool "Using MTD Nor Flash device" - default n - - config MTD_USING_NAND - bool "Using MTD Nand Flash device" - default n - endif - config RT_USING_PM bool "Using Power Management device drivers" default n diff --git a/components/drivers/include/drivers/mtd.h b/components/drivers/include/drivers/mtd.h deleted file mode 100644 index fb7ea4389d..0000000000 --- a/components/drivers/include/drivers/mtd.h +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - 2018-09-10 heyuanjie87 first version - - */ - -#ifndef __MTD_H__ -#define __MTD_H__ - -#include -#include -#include - -#define MTD_TYPE_NOR 1 -#define MTD_TYPE_NAND 2 - - /** - * MTD operation modes - * - * @MTD_OPM_PLACE_OOB: OOB data are placed at the given offset (default) - * @MTD_OPM_AUTO_OOB: OOB data are automatically placed at the free areas - * @MTD_OPM_RAW: data are transferred as-is, with no error correction; - */ -enum mtd_opm -{ - MTD_OPM_PLACE_OOB = 0, - MTD_OPM_AUTO_OOB = 1, - MTD_OPM_RAW = 2, -}; - -#ifndef loff_t -typedef long loff_t; -#endif - -struct mtd_oob_region -{ - uint8_t offset; - uint8_t length; -}; - -typedef struct mtd_info -{ - struct rt_device parent; - - const struct mtd_ops *ops; - - uint16_t oob_size; - uint16_t sector_size; /* Minimal writable flash unit size */ - uint32_t block_size:28; /* Erase size for the device */ - uint32_t type:4; - - size_t size; /* Total size of the MTD */ - loff_t offset; /* At which this MTD starts, from the beginning of the MEMORY */ - struct mtd_info *master; - - void *priv; -}rt_mtd_t; - -struct mtd_io_desc -{ - uint8_t mode; /* operation mode(enum mtd_opm) */ - uint8_t ooblen; /* number of oob bytes to write/read */ - uint8_t oobretlen; /* number of oob bytes written/read */ - uint8_t ooboffs; /* offset in the oob area */ - uint8_t *oobbuf; - - size_t datlen; /* number of data bytes to write/read */ - size_t datretlen; /* number of data bytes written/read */ - uint8_t *datbuf; /* if NULL only oob are read/written */ -}; - -struct mtd_ops -{ - int(*erase)(rt_mtd_t *mtd, loff_t addr, size_t len); /* return 0 if success */ - int(*read) (rt_mtd_t *mtd, loff_t from, struct mtd_io_desc *ops); /* return 0 if success */ - int(*write) (rt_mtd_t *mtd, loff_t to, struct mtd_io_desc *ops); /* return 0 if success */ - int(*isbad) (rt_mtd_t *mtd, uint32_t block); /* return 1 if bad, 0 not bad */ - int(*markbad) (rt_mtd_t *mtd, uint32_t block); /* return 0 if success */ -}; - -struct mtd_part -{ - const char *name; /* name of the MTD partion */ - loff_t offset; /* start addr of partion */ - size_t size; /* size of partion */ -}; - -int rt_mtd_erase(rt_mtd_t *mtd, loff_t addr, size_t size); -int rt_mtd_block_erase(rt_mtd_t *mtd, uint32_t block); -int rt_mtd_read_oob(rt_mtd_t *mtd, loff_t from, struct mtd_io_desc *desc); -int rt_mtd_write_oob(rt_mtd_t *mtd, loff_t to, struct mtd_io_desc *desc); -int rt_mtd_read(rt_mtd_t *mtd, loff_t from, uint8_t *buf, size_t len); -int rt_mtd_write(rt_mtd_t *mtd, loff_t to, const uint8_t *buf, size_t len); -int rt_mtd_block_markbad(rt_mtd_t *mtd, uint32_t block); -int rt_mtd_block_isbad(rt_mtd_t *mtd, uint32_t block); - -rt_mtd_t* rt_mtd_get(const char *name); -int rt_mtd_register(rt_mtd_t *master, const struct mtd_part *parts, int np); - -#endif diff --git a/components/drivers/include/drivers/mtdnand.h b/components/drivers/include/drivers/mtdnand.h deleted file mode 100644 index 2e836cb2fb..0000000000 --- a/components/drivers/include/drivers/mtdnand.h +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - 2018-09-10 heyuanjie87 first version - - */ - -#ifndef _MTDNAND_H_ -#define _MTDNAND_H_ - -#include "mtd.h" - - /* Status bits */ -#define NAND_STATUS_FAIL 0x01 -#define NAND_STATUS_FAIL_N1 0x02 -#define NAND_STATUS_WP 0x80 - -typedef enum -{ - NAND_CMD_PAGE_RD, /* read data to chip's page buffer,do WaitBusy after this cmd in low driver */ - NAND_CMD_PAGE_WR0, /* write data to chip's page buffer */ - NAND_CMD_PAGE_WR1, /* do flash programe */ - NAND_CMD_BLK_ERASE, /* erase block */ - NAND_CMD_ECC_EN, /* enable gen HWECC */ - NAND_CMD_ECC_DIS /* disable gen HWECC */ -} nand_cmd_t; - -typedef enum -{ - NAND_ECCM_NONE, - NAND_ECCM_HW, -} nand_eccmode_t; - -struct nand_chip; -struct nand_ops; - -/** -* struct nand_buffers - buffer structure for read/write -* @ecccalc: buffer pointer for calculated ECC, size is oobsize. -* @ecccode: buffer pointer for ECC read from flash, size is oobsize. -* -*/ -struct nand_buffers -{ - uint8_t *ecccalc; - uint8_t *ecccode; -}; - -struct nand_ecc -{ - uint8_t mode; /* nand_eccmode_t */ - uint8_t bytes; /* gen ecc bytes per ecc step(usually 3) */ - uint16_t stepsize:12; /* min 256 */ - uint16_t _step:4; /* */ - - /* driver must set the two interface if HWECC */ - void (*calculate)(struct nand_chip *chip, const uint8_t *dat, uint8_t *ecc_code); - /* return max bit flips if can't correct,return -1 ECC is error(0 success) */ - int(*correct)(struct nand_chip *chip, uint8_t *dat, uint8_t *read_ecc, uint8_t *calc_ecc); - /* ignore if NONECC */ - const struct mtd_oob_region *layout; -}; - -typedef struct nand_chip -{ - rt_mtd_t parent; - - /* driver must init these */ - const struct nand_ops *ops; - struct nand_ecc ecc; - const struct mtd_oob_region *freelayout; - - /* driver do not touch */ - struct nand_buffers buffers; - uint8_t *oob_poi; - uint8_t *pagebuf; - uint32_t size; - uint16_t oobsize; - uint8_t pages_pb; - uint16_t page_size; - int(*read_page)(struct nand_chip *chip, uint8_t *buf, int oob_required, int page); - int(*write_page)(struct nand_chip *chip, const uint8_t *buf, int oob_required, int page); -}rt_nand_t; - -struct nand_ops -{ - int(*cmdfunc)(rt_nand_t *nand, int cmd, int page, int offset); /* send nand operation cmd, return Status bits(0 success), - if nand is busy please wait in low driver */ - int(*read_buf)(rt_nand_t *nand, uint8_t *buf, int len); /* read data from nand chip's page buffer */ - int(*write_buf)(rt_nand_t *nand, const uint8_t *buf, int len);/* write data to nand chip's page buffer */ - int(*isbad)(rt_nand_t *nand, uint32_t blk); /* if NULL OOB[0] used as bad mark(not 0xff is bad) */ - int(*markbad)(rt_nand_t *nand, uint32_t blk); /* if NULL OOB[0] used as bad mark(set to 0x00) */ -}; - -int rt_mtd_nand_init(rt_nand_t *nand, int blk_size, int page_size, int oob_size); - -#endif /* MTD_NAND_H_ */ diff --git a/components/drivers/include/drivers/mtdnor.h b/components/drivers/include/drivers/mtdnor.h deleted file mode 100644 index ccb7466b4b..0000000000 --- a/components/drivers/include/drivers/mtdnor.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - 2018-09-10 heyuanjie87 first version - - */ - -#ifndef __MTDNOR_H__ -#define __MTDNOR_H__ - -#include - -struct nor_ops; - -typedef struct -{ - rt_mtd_t parent; - - const struct nor_ops *ops; /* operations interface */ -}rt_nor_t; - -struct nor_ops -{ - int (*erase)(rt_nor_t *nor, loff_t addr, size_t len); /* return success erased len or error code */ - int (*read)(rt_nor_t *nor, loff_t addr, uint8_t *buf, size_t len); /* return success data size or error code */ - int (*write)(rt_nor_t *nor, loff_t addr, const uint8_t *buf, size_t len); /* return success data size or error code */ -}; - -int rt_mtd_nor_init(rt_nor_t *nor, int blksize); - -#endif diff --git a/components/drivers/mtd/mtd.c b/components/drivers/mtd/mtd.c deleted file mode 100644 index 3e7b4f2b54..0000000000 --- a/components/drivers/mtd/mtd.c +++ /dev/null @@ -1,236 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - 2018-09-10 heyuanjie87 first version - - */ - -#include - -static rt_mtd_t* mtd_part_alloc(rt_mtd_t *master, const struct mtd_part *part) -{ - rt_mtd_t *slave; - - slave = rt_malloc(sizeof(rt_mtd_t)); - if (slave == RT_NULL) - goto out; - - slave->master = master; - - *slave = *master; - slave->size = part->size; - slave->offset = part->offset; - -out: - - return slave; -} - -rt_mtd_t* rt_mtd_get(const char *name) -{ - rt_mtd_t *mtd; - - mtd = (rt_mtd_t *)rt_device_find(name); - if (mtd == RT_NULL) - return RT_NULL; - - if (mtd->parent.type != RT_Device_Class_MTD) - return RT_NULL; - - return mtd; -} - -/* - * Register MTD driver - * - * @parts partion description - * @np number of partitions - * @return number of unregistered partitions - * -*/ -int rt_mtd_register(rt_mtd_t *master, const struct mtd_part *parts, int np) -{ - int ret; - rt_mtd_t *slave; - - master->master = master; - master->parent.type = RT_Device_Class_MTD; - - if (np > 0) - { - master->offset = parts->offset; - master->size = parts->size; - - ret = rt_device_register((rt_device_t)master, parts->name, 0); - if (ret != 0) - goto _out; - - np --; - parts ++; - } - - while (np > 0) - { - slave = mtd_part_alloc(master, parts); - if (!slave) - break; - ret = rt_device_register((rt_device_t)slave, parts->name, 0); - if (ret) - break; - parts ++; - np --; - } - -_out: - return np; -} - -int rt_mtd_block_erase(rt_mtd_t *mtd, uint32_t block) -{ - uint32_t total_blks; - loff_t addr; - - total_blks = mtd->size/mtd->block_size; - if (block >= total_blks) - return -EINVAL; - addr = mtd->offset + mtd->block_size * block; - - return mtd->ops->erase(mtd->master, addr, mtd->block_size); -} - -int rt_mtd_block_isbad(rt_mtd_t *mtd, uint32_t block) -{ - uint32_t total_blks, offset_blk; - - if (!mtd->ops->isbad) - return 0; - - total_blks = mtd->size / mtd->block_size; - if (block >= total_blks) - return -EINVAL; - offset_blk = mtd->offset / mtd->block_size; - - return mtd->ops->isbad(mtd->master, block + offset_blk); -} - -int rt_mtd_block_markbad(rt_mtd_t *mtd, uint32_t block) -{ - uint32_t total_blks, offset_blk; - - if (!mtd->ops->markbad) - return -EOPNOTSUPP; - - total_blks = mtd->size / mtd->block_size; - if (block >= total_blks) - return -EINVAL; - offset_blk = mtd->offset / mtd->block_size; - - return mtd->ops->markbad(mtd->master, block + offset_blk); -} - -int rt_mtd_erase(rt_mtd_t *mtd, loff_t addr, size_t size) -{ - if (addr > mtd->size || (addr + size) > mtd->size) - return -EINVAL; - addr += mtd->offset; - - return mtd->ops->erase(mtd->master, addr, size); -} - -/* -* Read data only -* -* @from offset to read from -* @return success size or error code -*/ -int rt_mtd_read(rt_mtd_t *mtd, loff_t from, uint8_t *buf, size_t len) -{ - int ret; - struct mtd_io_desc desc = {0}; - - if (from < 0 || from >= (loff_t)mtd->size || len > mtd->size - from) - return -EINVAL; - if (!len) - return 0; - - desc.datbuf = buf; - desc.datlen = len; - ret = mtd->ops->read(mtd->master, from + mtd->offset, &desc); - if (ret) - return ret; - - return desc.datretlen; -} - -/** -* Write data only -* -* @to offset to write from -* @return success size or error code -*/ -int rt_mtd_write(rt_mtd_t *mtd, loff_t to, const uint8_t *buf, size_t len) -{ - int ret; - struct mtd_io_desc desc = {0}; - - if (to < 0 || to >= (loff_t)mtd->size || len > mtd->size - to) - return -EINVAL; - if (!mtd->ops->write) - return -EROFS; - if (!len) - return 0; - - desc.datbuf = (uint8_t*)buf; - desc.datlen = len; - ret = mtd->ops->write(mtd->master, to + mtd->offset, &desc); - if (ret) - return ret; - - return desc.datretlen; -} - -/** -* Read data and/or out-of-band -* -* @from offset to read from -* @desc sector operation description structure -* @return error code, 0 success -*/ -int rt_mtd_read_oob(rt_mtd_t *mtd, loff_t from, struct mtd_io_desc *desc) -{ - desc->datretlen = 0; - desc->oobretlen = 0; - - if (from < 0 || from >= (loff_t)mtd->size) - return -EINVAL; - - if (desc->datbuf && (desc->datlen > (mtd->size - from))) - return -EINVAL; - - return mtd->ops->read(mtd->master, from + mtd->offset, desc); -} - -/** -* Write data and/or out-of-band -* -* @to offset to read from -* @desc sector operation description structure -* @return error code, 0 success -*/ -int rt_mtd_write_oob(rt_mtd_t *mtd, loff_t to, struct mtd_io_desc *desc) -{ - desc->datretlen = 0; - desc->oobretlen = 0; - - if (to < 0 || to >= (loff_t)mtd->size) - return -EINVAL; - - if (desc->datbuf && (desc->datlen >(mtd->size - to))) - return -EINVAL; - - return mtd->ops->write(mtd->master, to + mtd->offset, desc); -} diff --git a/components/drivers/mtd/mtdnand.c b/components/drivers/mtd/mtdnand.c deleted file mode 100644 index 50012219fb..0000000000 --- a/components/drivers/mtd/mtdnand.c +++ /dev/null @@ -1,645 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2018-09-10 heyuanjie87 first version - */ - -#include - -#define MTDTONAND(x) ((rt_nand_t*)(x)) -#define NOTALIGNED(x) ((x & (chip->page_size - 1)) != 0) -#ifndef min -#define min(a,b) (a>b? b:a) -#endif - -static uint8_t *nand_fill_oob(rt_nand_t *chip, uint8_t *oob, size_t len, struct mtd_io_desc *desc) -{ - rt_memset(chip->oob_poi, 0xff, chip->oobsize); - - switch (desc->mode) - { - case MTD_OPM_PLACE_OOB: - case MTD_OPM_RAW: - rt_memcpy(chip->oob_poi + desc->ooboffs, oob, len); - return oob + len; - - case MTD_OPM_AUTO_OOB: - { - const struct mtd_oob_region *free = chip->freelayout; - uint32_t boffs; - size_t bytes; - - bytes = min(len, free->length); - boffs = free->offset; - - rt_memcpy(chip->oob_poi + boffs, oob, bytes); - oob += bytes; - - return oob; - } - } - - return NULL; -} - -static uint8_t *nand_transfer_oob(rt_nand_t *chip, uint8_t *oob, struct mtd_io_desc *desc, size_t len) -{ - switch (desc->mode) - { - case MTD_OPM_PLACE_OOB: - case MTD_OPM_RAW: - rt_memcpy(oob, chip->oob_poi + desc->ooboffs, len); - return oob + len; - - case MTD_OPM_AUTO_OOB: - { - struct mtd_oob_region *free = (struct mtd_oob_region *)chip->freelayout; - uint32_t boffs = 0, roffs = desc->ooboffs; - size_t bytes = 0; - - for (; free->length && len; free++, len -= bytes) - { - /* Read request not from offset 0? */ - if (roffs) - { - if (roffs >= free->length) - { - roffs -= free->length; - continue; - } - boffs = free->offset + roffs; - bytes = min(len, (free->length - roffs)); - roffs = 0; - } - else - { - bytes = min(len, free->length); - boffs = free->offset; - } - - rt_memcpy(oob, chip->oob_poi + boffs, bytes); - oob += bytes; - } - - return oob; - } - } - - return NULL; -} - -static int nand_read_page_raw(rt_nand_t *chip, uint8_t *buf, int oob_required, int page) -{ - chip->ops->read_buf(chip, buf, chip->page_size); - - if (oob_required) - chip->ops->read_buf(chip, chip->oob_poi, chip->oobsize); - - return 0; -} - -static int nand_write_page_raw(rt_nand_t *chip, const uint8_t *buf, int oob_required, int page) -{ - chip->ops->write_buf(chip, buf, chip->page_size); - - if (oob_required) - chip->ops->write_buf(chip, chip->oob_poi, chip->oobsize); - - return 0; -} - -static int nand_write_page_hwecc(rt_nand_t *chip, const uint8_t *buf, int oob_required, int page) -{ - uint16_t i; - uint16_t stepsize = chip->ecc.stepsize; - uint16_t eccbytes = chip->ecc.bytes; - uint16_t eccsteps = chip->ecc._step; - uint16_t eccpos = chip->ecc.layout->offset; - uint8_t *ecc_calc = chip->buffers.ecccalc; - const uint8_t *p = buf; - - for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += stepsize) - { - chip->ops->cmdfunc(chip, NAND_CMD_ECC_EN, 0, 0); - chip->ops->write_buf(chip, p, stepsize); - chip->ecc.calculate(chip, p, &ecc_calc[i]); - chip->ops->cmdfunc(chip, NAND_CMD_ECC_DIS, 0, 0); - } - - rt_memcpy(&chip->oob_poi[eccpos], ecc_calc, chip->ecc.layout->length); - - chip->ops->write_buf(chip, chip->oob_poi, chip->oobsize); - - return 0; -} - -static int nand_read_page_hwecc(rt_nand_t *chip, uint8_t *buf, int oob_required, int page) -{ - uint16_t i; - uint16_t eccsize = chip->ecc.stepsize; - uint16_t eccbytes = chip->ecc.bytes; - uint16_t eccsteps = chip->ecc._step; - uint16_t eccpos = chip->ecc.layout->offset; - uint8_t *p = buf; - uint8_t *ecc_calc = chip->buffers.ecccalc; - uint8_t *ecc_code = chip->buffers.ecccode; - int ret = 0; - - for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) - { - chip->ops->cmdfunc(chip, NAND_CMD_ECC_EN, 0, 0); - chip->ops->read_buf(chip, p, eccsize); - chip->ecc.calculate(chip, p, &ecc_calc[i]); - chip->ops->cmdfunc(chip, NAND_CMD_ECC_DIS, 0, 0); - } - - chip->ops->read_buf(chip, chip->oob_poi, chip->oobsize); - rt_memcpy(ecc_code, &chip->oob_poi[eccpos], chip->ecc.layout->length); - - eccsteps = chip->ecc._step; - p = buf; - - for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) - { - int stat; - - stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]); - if (stat != 0) - ret = -1; - } - - return ret; -} - -static int nand_write_page(rt_nand_t *chip, const uint8_t *buf, - int oob_required, int page, int raw) -{ - int status; - - chip->ops->cmdfunc(chip, NAND_CMD_PAGE_WR0, page, 0x00); - - if (raw) - { - nand_write_page_raw(chip, buf, oob_required, page); - } - else - { - chip->write_page(chip, buf, oob_required, page); - } - - status = chip->ops->cmdfunc(chip, NAND_CMD_PAGE_WR1, -1, -1); - - return status; -} - -static int nand_do_read_desc(rt_nand_t *chip, loff_t from, struct mtd_io_desc *desc) -{ - int page, bytes; - char oob_required; - char ecc_fail = 0; - int ret = 0; - uint32_t readlen = desc->datlen; - uint16_t oobreadlen = desc->ooblen; - uint16_t max_oobsize = desc->mode == MTD_OPM_AUTO_OOB ? - chip->freelayout->length : chip->oobsize; - - uint8_t *oob, *buf, *notalign = 0; - - /* Reject reads, which are not page aligned */ - if (NOTALIGNED(from)) - { - return -EINVAL; - } - - buf = desc->datbuf; - if (NOTALIGNED(desc->datlen) && !chip->pagebuf) - { - chip->pagebuf = rt_malloc(chip->page_size); - if (!chip->pagebuf) - return -ENOMEM; - } - - page = (int)(from / chip->page_size); - - oob = desc->oobbuf; - oob_required = oob ? 1 : 0; - - while (1) - { - bytes = min(chip->page_size, readlen); - - chip->ops->cmdfunc(chip, NAND_CMD_PAGE_RD, page, 0x00); - if (NOTALIGNED(bytes)) - { - notalign = buf; - buf = chip->pagebuf; - } - /* - * Now read the page into the buffer. Absent an error, - * the read methods return max bitflips per ecc step. - */ - if (desc->mode == MTD_OPM_RAW) - { - ret = nand_read_page_raw(chip, buf, oob_required, page); - } - else - { - ret = chip->read_page(chip, buf, oob_required, page); - } - - if (ret != 0) - { - ret = -EBADMSG; - break; - } - - if (oob) - { - int toread = min(oobreadlen, max_oobsize); - - if (toread) - { - oob = nand_transfer_oob(chip, oob, desc, toread); - oobreadlen -= toread; - } - } - - if (notalign) - { - rt_memcpy(notalign, buf, bytes); - } - - buf += bytes; - readlen -= bytes; - - if (!readlen) - break; - - page++; - } - - desc->datretlen = desc->datlen - (size_t)readlen; - if (oob) - desc->oobretlen = desc->ooblen - oobreadlen; - - return ret; -} - -/* - * write with ECC - * -*/ -static int nand_do_write_desc(rt_nand_t *chip, loff_t to, struct mtd_io_desc *desc) -{ - int page; - uint16_t writelen = desc->datlen; - uint16_t oob_required = desc->oobbuf ? 1 : 0; - uint16_t oobwritelen = desc->ooblen; - uint16_t oobmaxlen = desc->mode == MTD_OPM_AUTO_OOB ? - chip->freelayout->length : chip->oobsize; - - uint8_t *oob = desc->oobbuf; - uint8_t *buf = desc->datbuf; - int ret; - - if (!writelen) - return 0; - - /* Reject writes, which are not page aligned */ - if (NOTALIGNED(to)) - { - return -EINVAL; - } - - page = (int)(to / chip->page_size); - - /* Don't allow multipage oob writes with offset */ - if (oob && desc->ooboffs && (desc->ooboffs + desc->ooblen > oobmaxlen)) - { - ret = -EINVAL; - goto err_out; - } - - if (NOTALIGNED(desc->datlen) && !chip->pagebuf) - { - chip->pagebuf = rt_malloc(chip->page_size); - if (!chip->pagebuf) - return -ENOMEM; - } - - while (1) - { - uint16_t bytes = min(chip->page_size, writelen); - - if (oob) - { - size_t len = min(oobwritelen, oobmaxlen); - oob = nand_fill_oob(chip, oob, len, desc); - oobwritelen -= len; - } - else - { - /* We still need to erase leftover OOB data */ - rt_memset(chip->oob_poi, 0xff, chip->oobsize); - } - - if (NOTALIGNED(bytes)) - { - uint8_t *dbtmp = buf; - buf = chip->pagebuf; - rt_memset(&buf[bytes], 0xff, chip->page_size - bytes); - rt_memcpy(buf, dbtmp, bytes); - } - ret = nand_write_page(chip, buf, oob_required, page, (desc->mode == MTD_OPM_RAW)); - if (ret) - break; - - writelen -= bytes; - if (!writelen) - break; - - buf += bytes; - page++; - } - - desc->datretlen = desc->datlen - writelen; - if (oob) - desc->oobretlen = desc->ooblen; - -err_out: - - return ret; -} - -static int nand_read_oob_std(rt_nand_t *chip, int page) -{ - chip->ops->cmdfunc(chip, NAND_CMD_PAGE_RD, page, chip->page_size); - chip->ops->read_buf(chip, chip->oob_poi, chip->oobsize); - - return 0; -} - -/* - * read one page of OOB -*/ -static int nand_only_read_oob(rt_nand_t *chip, loff_t from, struct mtd_io_desc *desc) -{ - int page; - int readlen = desc->ooblen; - int len; - uint8_t *buf = desc->oobbuf; - int ret = 0; - - if (desc->mode == MTD_OPM_AUTO_OOB) - len = chip->freelayout->length; - else - len = chip->oobsize; - - if (desc->ooboffs >= len) //attempt to start read outside oob - { - return -EINVAL; - } - - page = (int)(from / chip->page_size); - - ret = nand_read_oob_std(chip, page); - if (ret == 0) - { - len = min(len, readlen); - buf = nand_transfer_oob(chip, buf, desc, len); - desc->oobretlen = len; - } - - return ret; -} - -static int nand_write_oob_std(rt_nand_t *chip, int page) -{ - int status; - - chip->ops->cmdfunc(chip, NAND_CMD_PAGE_WR0, page, chip->page_size); - chip->ops->write_buf(chip, chip->oob_poi, chip->oobsize); - /* Send command to program the OOB data */ - status = chip->ops->cmdfunc(chip, NAND_CMD_PAGE_WR1, -1, -1); - - return status & NAND_STATUS_FAIL ? -EIO : 0; -} - -static int nand_only_write_oob(rt_nand_t *chip, loff_t to, struct mtd_io_desc *desc) -{ - int page, ret, len; - - if (desc->mode == MTD_OPM_AUTO_OOB) - len = chip->freelayout->length; - else - len = chip->oobsize; - - /* Do not allow write past end of page */ - if ((desc->ooboffs + desc->ooblen) > len) - { - return -EINVAL; - } - - if (desc->ooblen == 0) - { - return -EINVAL; - } - - /* get page */ - page = (int)(to / chip->page_size); - - nand_fill_oob(chip, desc->oobbuf, desc->ooblen, desc); - - ret = nand_write_oob_std(chip, page); - if (ret == 0) - desc->oobretlen = len; - - return ret; -} - -static int nand_erase(rt_mtd_t *mtd, loff_t addr, size_t size) -{ - rt_nand_t *chip; - int status; - int page; - uint32_t blksize; - - chip = MTDTONAND(mtd); - blksize = mtd->block_size; - page = addr / chip->page_size; - - while (size >= blksize) - { - status = chip->ops->cmdfunc(chip, NAND_CMD_BLK_ERASE, page, 0); - if (status & NAND_STATUS_FAIL) - { - break; - } - size -= blksize; - page += chip->pages_pb; - } - - return size; -} - -static int nand_read(rt_mtd_t *mtd, loff_t from, struct mtd_io_desc *desc) -{ - int ret = -ENOTSUP; - rt_nand_t *chip; - - chip = MTDTONAND(mtd); - - switch (desc->mode) - { - case MTD_OPM_PLACE_OOB: - case MTD_OPM_AUTO_OOB: - case MTD_OPM_RAW: - break; - - default: - goto out; - } - - if (!desc->datbuf || !desc->datlen) - ret = nand_only_read_oob(chip, from, desc); - else - ret = nand_do_read_desc(chip, from, desc); - -out: - - return ret; -} - -static int nand_write(rt_mtd_t *mtd, loff_t to, struct mtd_io_desc *desc) -{ - int ret = -ENOTSUP; - rt_nand_t *chip; - - chip = MTDTONAND(mtd); - - switch (desc->mode) - { - case MTD_OPM_PLACE_OOB: - case MTD_OPM_AUTO_OOB: - case MTD_OPM_RAW: - break; - - default: - goto out; - } - - if (!desc->datbuf || !desc->datlen) - ret = nand_only_write_oob(chip, to, desc); - else - ret = nand_do_write_desc(chip, to, desc); - -out: - - return ret; -} - -static int nand_block_isbad(rt_mtd_t *mtd, uint32_t blk) -{ - int ret; - rt_nand_t *chip = MTDTONAND(mtd); - - if (chip->ops->isbad) - { - ret = chip->ops->isbad(chip, blk); - } - else - { - int page; - - page = blk * chip->pages_pb; - nand_read_oob_std(chip, page); - ret = chip->oob_poi[0] != 0xFF; - } - - return ret; -} - -static int nand_block_markbad(rt_mtd_t *mtd, uint32_t blk) -{ - int ret; - rt_nand_t *chip; - - chip = MTDTONAND(mtd); - - if (chip->ops->markbad) - { - ret = chip->ops->markbad(chip, blk); - } - else - { - int page; - - page = blk * chip->pages_pb; - rt_memset(chip->oob_poi, 0xff, chip->oobsize); - chip->oob_poi[0] = 0; - ret = nand_write_oob_std(chip, page); - } - - return ret; -} - -static const struct mtd_ops _ops = -{ - nand_erase, - nand_read, - nand_write, - nand_block_isbad, - nand_block_markbad, -}; - -int rt_mtd_nand_init(rt_nand_t *nand, int blk_size, int page_size, int oob_size) -{ - uint8_t *buf; - - buf = rt_malloc(oob_size * 3); - if (buf == RT_NULL) - return -ENOMEM; - - nand->oob_poi = buf; - buf += oob_size; - nand->buffers.ecccalc = buf; - buf += oob_size; - nand->buffers.ecccode = buf; - nand->pagebuf = 0; /* alloc when unaligen access */ - - nand->pages_pb = blk_size / page_size; - nand->ecc._step = page_size / nand->ecc.stepsize; - nand->page_size = page_size; - nand->oobsize = oob_size; - - nand->parent.type = MTD_TYPE_NAND; - nand->parent.ops = &_ops; - nand->parent.sector_size = page_size; - nand->parent.block_size = blk_size; - nand->parent.oob_size = oob_size; - - switch (nand->ecc.mode) - { - case NAND_ECCM_NONE: - { - nand->read_page = nand_read_page_raw; - nand->write_page = nand_write_page_raw; - }break; - case NAND_ECCM_HW: - { - nand->read_page = nand_read_page_hwecc; - nand->write_page = nand_write_page_hwecc; - }break; - default: - { - rt_free(buf); - return -1; - } - } - - return 0; -} diff --git a/components/drivers/mtd/mtdnor.c b/components/drivers/mtd/mtdnor.c deleted file mode 100644 index 481d26167e..0000000000 --- a/components/drivers/mtd/mtdnor.c +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - 2018-09-10 heyuanjie87 first version - - */ - -#include - -#ifdef MTD_USING_NOR -static int _nor_erase(rt_mtd_t *mtd, loff_t addr, size_t len) -{ - rt_nor_t *nor; - - nor = (rt_nor_t *)mtd; - return nor->ops->erase(nor, addr, len); -} - -static int _nor_read(rt_mtd_t *mtd, loff_t from, struct mtd_io_desc *desc) -{ - rt_nor_t *nor; - int ret; - - nor = (rt_nor_t *)mtd; - ret = nor->ops->read(nor, from, desc->datbuf, desc->datlen); - if (ret > 0) - { - desc->datretlen = ret; - ret = 0; - } - - return ret; -} - -static int _nor_write(rt_mtd_t *mtd, loff_t to, struct mtd_io_desc *desc) -{ - rt_nor_t *nor; - int ret; - - nor = (rt_nor_t *)mtd; - ret = nor->ops->write(nor, to, desc->datbuf, desc->datlen); - if (ret > 0) - { - desc->datretlen = ret; - ret = 0; - } - - return ret; -} - -static const struct mtd_ops _ops = -{ - _nor_erase, - _nor_read, - _nor_write, - 0, - 0 -}; - -int rt_mtd_nor_init(rt_nor_t *nor, int blksize) -{ - nor->parent.sector_size = 1; - nor->parent.block_size = blksize; - nor->parent.ops = &_ops; - nor->parent.type = MTD_TYPE_NOR; - nor->parent.oob_size = 0; - - return 0; -} -#endif -- cgit v1.3.1 From badd2114575190d8ec4ab9ee0e71f6452d2bf666 Mon Sep 17 00:00:00 2001 From: tangyuxin <462747508@qq.com> Date: Mon, 5 Aug 2019 14:18:15 +0800 Subject: [components][drivers][ipc] 完善工作队列,增强稳定性 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 可直接提交延时任务, 无需额外调用延时初始化 2. 修复延时任务 PENDING 时,取消任务会递归调用的问题 --- components/drivers/include/ipc/workqueue.h | 6 ++-- components/drivers/src/workqueue.c | 48 +++++++++++++++++------------- 2 files changed, 32 insertions(+), 22 deletions(-) (limited to 'components/drivers/include') diff --git a/components/drivers/include/ipc/workqueue.h b/components/drivers/include/ipc/workqueue.h index 354a30c04e..23e256c420 100644 --- a/components/drivers/include/ipc/workqueue.h +++ b/components/drivers/include/ipc/workqueue.h @@ -43,13 +43,13 @@ struct rt_work void *work_data; rt_uint16_t flags; rt_uint16_t type; + struct rt_timer timer; + struct rt_workqueue *workqueue; }; struct rt_delayed_work { struct rt_work work; - struct rt_timer timer; - struct rt_workqueue *workqueue; }; #ifdef RT_USING_HEAP @@ -74,6 +74,7 @@ rt_inline void rt_work_init(struct rt_work *work, void (*work_func)(struct rt_wo rt_list_init(&(work->list)); work->work_func = work_func; work->work_data = work_data; + work->workqueue = RT_NULL; work->flags = 0; work->type = 0; } @@ -81,6 +82,7 @@ rt_inline void rt_work_init(struct rt_work *work, void (*work_func)(struct rt_wo void rt_delayed_work_init(struct rt_delayed_work *work, void (*work_func)(struct rt_work *work, void *work_data), void *work_data); +int rt_work_sys_workqueue_init(void); #endif #endif diff --git a/components/drivers/src/workqueue.c b/components/drivers/src/workqueue.c index 7bf4bd81a7..2e0280df3a 100644 --- a/components/drivers/src/workqueue.c +++ b/components/drivers/src/workqueue.c @@ -71,6 +71,7 @@ static void _workqueue_thread_entry(void *parameter) rt_list_remove(&(work->list)); queue->work_current = work; work->flags &= ~RT_WORK_STATE_PENDING; + work->workqueue = RT_NULL; rt_hw_interrupt_enable(level); /* do work */ @@ -141,7 +142,7 @@ static rt_err_t _workqueue_cancel_work(struct rt_workqueue *queue, struct rt_wor return RT_EOK; } -static rt_err_t _workqueue_cancel_delayed_work(struct rt_delayed_work *work) +static rt_err_t _workqueue_cancel_delayed_work(struct rt_work *work) { rt_base_t level; int ret = RT_EOK; @@ -152,10 +153,10 @@ static rt_err_t _workqueue_cancel_delayed_work(struct rt_delayed_work *work) goto __exit; } - if (work->work.flags & RT_WORK_STATE_PENDING) + if (work->flags & RT_WORK_STATE_PENDING) { /* Remove from the queue if already submitted */ - ret = rt_workqueue_cancel_work(work->workqueue, &(work->work)); + ret = _workqueue_cancel_work(work->workqueue, work); if (ret) { goto __exit; @@ -163,12 +164,12 @@ static rt_err_t _workqueue_cancel_delayed_work(struct rt_delayed_work *work) } else { - if (work->work.flags & RT_WORK_STATE_SUBMITTING) + if (work->flags & RT_WORK_STATE_SUBMITTING) { level = rt_hw_interrupt_disable(); rt_timer_stop(&(work->timer)); rt_timer_detach(&(work->timer)); - work->work.flags &= ~RT_WORK_STATE_SUBMITTING; + work->flags &= ~RT_WORK_STATE_SUBMITTING; rt_hw_interrupt_enable(level); } } @@ -176,7 +177,7 @@ static rt_err_t _workqueue_cancel_delayed_work(struct rt_delayed_work *work) level = rt_hw_interrupt_disable(); /* Detach from workqueue */ work->workqueue = RT_NULL; - work->work.flags &= ~(RT_WORK_STATE_PENDING); + work->flags &= ~(RT_WORK_STATE_PENDING); rt_hw_interrupt_enable(level); __exit: @@ -184,11 +185,10 @@ __exit: } static rt_err_t _workqueue_submit_delayed_work(struct rt_workqueue *queue, - struct rt_delayed_work *work, rt_tick_t ticks) + struct rt_work *work, rt_tick_t ticks) { rt_base_t level; - int ret = RT_EOK; - + rt_err_t ret = RT_EOK; /* Work cannot be active in multiple queues */ if (work->workqueue && work->workqueue != queue) @@ -215,13 +215,13 @@ static rt_err_t _workqueue_submit_delayed_work(struct rt_workqueue *queue, if (!ticks) { /* Submit work if no ticks is 0 */ - _workqueue_submit_work(work->workqueue, &(work->work)); + ret = _workqueue_submit_work(work->workqueue, work); } else { level = rt_hw_interrupt_disable(); /* Add timeout */ - work->work.flags |= RT_WORK_STATE_SUBMITTING; + 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); @@ -234,16 +234,17 @@ __exit: static void _delayed_work_timeout_handler(void *parameter) { - struct rt_delayed_work *delayed_work; + struct rt_work *delayed_work; rt_base_t level; - delayed_work = (struct rt_delayed_work *)parameter; + delayed_work = (struct rt_work *)parameter; level = rt_hw_interrupt_disable(); rt_timer_stop(&(delayed_work->timer)); rt_timer_detach(&(delayed_work->timer)); - delayed_work->work.flags &= ~RT_WORK_STATE_SUBMITTING; + 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->work)); + _workqueue_submit_work(delayed_work->workqueue, delayed_work); } struct rt_workqueue *rt_workqueue_create(const char *name, rt_uint16_t stack_size, rt_uint8_t priority) @@ -295,9 +296,14 @@ 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_delayed_work(queue, (struct rt_delayed_work *)work, time); + return _workqueue_submit_delayed_work(queue, work, time); } else { @@ -341,7 +347,7 @@ 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((struct rt_delayed_work *)work); + return _workqueue_cancel_delayed_work(work); } else { @@ -391,8 +397,7 @@ rt_err_t rt_workqueue_cancel_all_work(struct rt_workqueue *queue) void rt_delayed_work_init(struct rt_delayed_work *work, void (*work_func)(struct rt_work *work, void *work_data), void *work_data) { - rt_work_init(&(work->work), work_func, work_data); - work->work.type = RT_WORK_TYPE_DELAYED; + rt_work_init(&work->work, work_func, work_data); } #ifdef RT_USING_SYSTEM_WORKQUEUE @@ -408,8 +413,11 @@ rt_err_t rt_work_cancel(struct rt_work *work) return rt_workqueue_cancel_work(sys_workq, work); } -static int rt_work_sys_workqueue_init(void) +int rt_work_sys_workqueue_init(void) { + if (sys_workq != RT_NULL) + return 0; + sys_workq = rt_workqueue_create("sys_work", RT_SYSTEM_WORKQUEUE_STACKSIZE, RT_SYSTEM_WORKQUEUE_PRIORITY); -- cgit v1.3.1 From e855fb8536d107ef6597fcb5c2262f7622ee1270 Mon Sep 17 00:00:00 2001 From: EvalZero Date: Tue, 6 Aug 2019 11:36:50 +0800 Subject: [components][audio]improve device ops interface and data flows. --- components/drivers/Kconfig | 14 + components/drivers/audio/audio.c | 641 ++++++++++++++++------------- components/drivers/audio/audio_pipe.c | 32 +- components/drivers/include/drivers/audio.h | 146 +++---- 4 files changed, 440 insertions(+), 393 deletions(-) (limited to 'components/drivers/include') diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index 4aceee3b4e..250d661e9f 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -238,6 +238,20 @@ config RT_USING_WDT config RT_USING_AUDIO bool "Using Audio device drivers" default n + + if RT_USING_AUDIO + config RT_AUDIO_REPLAY_MP_BLOCK_SIZE + int "Replay memmory pool block size" + default 4096 + + config RT_AUDIO_REPLAY_MP_BLOCK_COUNT + int "Replay memmory pool block count" + default 2 + + config RT_AUDIO_RECORD_PIPE_SIZE + int "Record pipe size" + default 2048 + endif config RT_USING_SENSOR bool "Using Sensor device drivers" diff --git a/components/drivers/audio/audio.c b/components/drivers/audio/audio.c index 6da0a3da91..28ddf0fe56 100644 --- a/components/drivers/audio/audio.c +++ b/components/drivers/audio/audio.c @@ -5,83 +5,208 @@ * * Change Logs: * Date Author Notes - * 2017-05-09 Urey first version + * 2017-05-09 Urey first version + * 2019-07-09 Zero-Free improve device ops interface and data flows */ #include +#include #include #include #include -#include +#define DBG_TAG "audio" +#define DBG_LVL DBG_INFO +#include -#include "audio_pipe.h" +#define MIN(a, b) ((a) < (b) ? (a) : (b)) - -#define AUDIO_DEBUG 0 -#if AUDIO_DEBUG -#define AUDIO_DBG(...) printf("[AUDIO]:"),printf(__VA_ARGS__) -#else -#define AUDIO_DBG(...) -#endif - -static struct rt_audio_pipe audio_pipe; +enum +{ + REPLAY_EVT_NONE = 0x00, + REPLAY_EVT_START = 0x01, + REPLAY_EVT_STOP = 0x02, +}; static rt_err_t _audio_send_replay_frame(struct rt_audio_device *audio) { rt_err_t result = RT_EOK; - rt_base_t level; - struct rt_audio_frame frame; + rt_uint8_t *data; + rt_size_t dst_size, src_size; + rt_uint16_t position, remain_bytes, index = 0; + struct rt_audio_buf_info *buf_info; RT_ASSERT(audio != RT_NULL); - //check repaly queue is empty - if (rt_data_queue_peak(&audio->replay->queue, &frame.data_ptr, &frame.data_size) != RT_EOK) + buf_info = &audio->replay->buf_info; + /* save current pos */ + position = audio->replay->pos; + dst_size = buf_info->block_size; + + /* check repaly queue is empty */ + if (rt_data_queue_peak(&audio->replay->queue, (const void **)&data, &src_size) != RT_EOK) { - AUDIO_DBG("TX queue is empty\n"); - result = -RT_EEMPTY; + /* ack stop event */ + if (audio->replay->event & REPLAY_EVT_STOP) + rt_completion_done(&audio->replay->cmp); - level = rt_hw_interrupt_disable(); - audio->replay->activated = RT_FALSE; - rt_hw_interrupt_enable(level); + /* send zero frames */ + memset(&buf_info->buffer[audio->replay->pos], 0, dst_size); - goto _exit; + audio->replay->pos += dst_size; + audio->replay->pos %= buf_info->total_size; } - - if (audio->ops->transmit != RT_NULL) + else { - AUDIO_DBG("audio transmit...\n"); - if (audio->ops->transmit(audio, frame.data_ptr, RT_NULL, frame.data_size) != frame.data_size) + memset(&buf_info->buffer[audio->replay->pos], 0, dst_size); + + /* copy data from memory pool to hardware device fifo */ + while (index < dst_size) { - result = -RT_EBUSY; + result = rt_data_queue_peak(&audio->replay->queue, (const void **)&data, &src_size); + if (result != RT_EOK) + { + LOG_D("under run %d, remain %d", audio->replay->pos, remain_bytes); + audio->replay->pos -= remain_bytes; + audio->replay->pos += dst_size; + audio->replay->pos %= buf_info->total_size; + audio->replay->read_index = 0; + result = -RT_EEMPTY; + break; + } + + remain_bytes = MIN((dst_size - index), (src_size - audio->replay->read_index)); + memcpy(&buf_info->buffer[audio->replay->pos], + &data[audio->replay->read_index], remain_bytes); - goto _exit; + index += remain_bytes; + audio->replay->read_index += remain_bytes; + audio->replay->pos += remain_bytes; + audio->replay->pos %= buf_info->total_size; + + if (audio->replay->read_index == src_size) + { + /* free memory */ + audio->replay->read_index = 0; + rt_data_queue_pop(&audio->replay->queue, (const void **)&data, &src_size, RT_WAITING_NO); + rt_mp_free(data); + + /* notify transmitted complete. */ + if (audio->parent.tx_complete != RT_NULL) + audio->parent.tx_complete(&audio->parent, (void *)data); + } } } - //pop the head frame... - rt_data_queue_pop(&audio->replay->queue, &frame.data_ptr, &frame.data_size, RT_WAITING_FOREVER); + if (audio->ops->transmit != RT_NULL) + { + if (audio->ops->transmit(audio, &buf_info->buffer[position], RT_NULL, dst_size) != dst_size) + result = -RT_ERROR; + } - _exit: return result; + return result; } static rt_err_t _audio_flush_replay_frame(struct rt_audio_device *audio) { - struct rt_audio_frame frame; + rt_err_t result = RT_EOK; - if (audio->replay == RT_NULL) - return -RT_EIO; - while (rt_data_queue_peak(&audio->replay->queue, &frame.data_ptr, &frame.data_size) == RT_EOK) + if (audio->replay->write_index) { - //pop the head frame... - rt_data_queue_pop(&audio->replay->queue, &frame.data_ptr, &frame.data_size, RT_WAITING_FOREVER); + result = rt_data_queue_push(&audio->replay->queue, + (const void **)audio->replay->write_data, + audio->replay->write_index, + RT_WAITING_FOREVER); - /* notify transmitted complete. */ - if (audio->parent.tx_complete != RT_NULL) - audio->parent.tx_complete(&audio->parent, (void *) frame.data_ptr); + audio->replay->write_index = 0; } - return RT_EOK; + return result; +} + +static rt_err_t _aduio_replay_start(struct rt_audio_device *audio) +{ + rt_err_t result = RT_EOK; + + if (audio->replay->activated != RT_TRUE) + { + /* start playback hardware device */ + if (audio->ops->start) + result = audio->ops->start(audio, AUDIO_STREAM_REPLAY); + + audio->replay->activated = RT_TRUE; + LOG_D("start audio replay device"); + } + + return result; +} + +static rt_err_t _aduio_replay_stop(struct rt_audio_device *audio) +{ + rt_err_t result = RT_EOK; + + if (audio->replay->activated == RT_TRUE) + { + /* flush replay remian frames */ + _audio_flush_replay_frame(audio); + + /* notify irq(or thread) to stop the data transmission */ + audio->replay->event |= REPLAY_EVT_STOP; + + /* waiting for the remaining data transfer to complete */ + rt_completion_init(&audio->replay->cmp); + rt_completion_wait(&audio->replay->cmp, RT_WAITING_FOREVER); + audio->replay->event &= ~REPLAY_EVT_STOP; + + /* stop playback hardware device */ + if (audio->ops->stop) + result = audio->ops->stop(audio, AUDIO_STREAM_REPLAY); + + audio->replay->activated = RT_FALSE; + LOG_D("stop audio replay device"); + } + + return result; +} + +static rt_err_t _audio_record_start(struct rt_audio_device *audio) +{ + rt_err_t result = RT_EOK; + + if (audio->record->activated != RT_TRUE) + { + /* open audio record pipe */ + rt_device_open(RT_DEVICE(&audio->record->pipe), RT_DEVICE_OFLAG_RDONLY); + + /* start record hardware device */ + if (audio->ops->start) + result = audio->ops->start(audio, AUDIO_STREAM_RECORD); + + audio->record->activated = RT_TRUE; + LOG_D("start audio record device"); + } + + return result; +} + +static rt_err_t _audio_record_stop(struct rt_audio_device *audio) +{ + rt_err_t result = RT_EOK; + + if (audio->record->activated == RT_TRUE) + { + /* stop record hardware device */ + if (audio->ops->stop) + result = audio->ops->stop(audio, AUDIO_STREAM_RECORD); + + /* close audio record pipe */ + rt_device_close(RT_DEVICE(&audio->record->pipe)); + + audio->record->activated = RT_FALSE; + LOG_D("stop audio record device"); + } + + return result; } static rt_err_t _audio_dev_init(struct rt_device *dev) @@ -96,9 +221,68 @@ static rt_err_t _audio_dev_init(struct rt_device *dev) audio->replay = RT_NULL; audio->record = RT_NULL; - /* apply configuration */ + /* initialize replay */ + if (dev->flag & RT_DEVICE_FLAG_WRONLY) + { + struct rt_audio_replay *replay = (struct rt_audio_replay *) rt_malloc(sizeof(struct rt_audio_replay)); + + if (replay == RT_NULL) + return -RT_ENOMEM; + memset(replay, 0, sizeof(struct rt_audio_replay)); + + /* init memory pool for replay */ + replay->mp = rt_mp_create("adu_mp", RT_AUDIO_REPLAY_MP_BLOCK_COUNT, RT_AUDIO_REPLAY_MP_BLOCK_SIZE); + if (replay->mp == RT_NULL) + { + rt_free(replay); + LOG_E("create memory pool for repaly failed"); + return -RT_ENOMEM; + } + + /* init queue for audio replay */ + rt_data_queue_init(&replay->queue, CFG_AUDIO_REPLAY_QUEUE_COUNT, 0, RT_NULL); + + /* init mutex lock for audio replay */ + rt_mutex_init(&replay->lock, "replay", RT_IPC_FLAG_PRIO); + + replay->activated = RT_FALSE; + audio->replay = replay; + } + + /* initialize record */ + if (dev->flag & RT_DEVICE_FLAG_RDONLY) + { + struct rt_audio_record *record = (struct rt_audio_record *) rt_malloc(sizeof(struct rt_audio_record)); + rt_uint8_t *buffer; + + if (record == RT_NULL) + return -RT_ENOMEM; + memset(record, 0, sizeof(struct rt_audio_record)); + + /* init pipe for record*/ + buffer = rt_malloc(RT_AUDIO_RECORD_PIPE_SIZE); + if (buffer == RT_NULL) + { + rt_free(record); + LOG_E("malloc memory for for record pipe failed"); + return -RT_ENOMEM; + } + rt_audio_pipe_init(&record->pipe, "record", + (rt_int32_t)(RT_PIPE_FLAG_FORCE_WR | RT_PIPE_FLAG_BLOCK_RD), + buffer, + RT_AUDIO_RECORD_PIPE_SIZE); + + record->activated = RT_FALSE; + audio->record = record; + } + + /* initialize hardware configuration */ if (audio->ops->init) - result = audio->ops->init(audio); + audio->ops->init(audio); + + /* get replay buffer information */ + if (audio->ops->buffer_info) + audio->ops->buffer_info(audio, &audio->replay->buf_info); return result; } @@ -122,65 +306,27 @@ static rt_err_t _audio_dev_open(struct rt_device *dev, rt_uint16_t oflag) /* initialize the Rx/Tx structure according to open flag */ if (oflag & RT_DEVICE_OFLAG_WRONLY) { - AUDIO_DBG("open audio device ,oflag = %x\n",oflag); - if (audio->replay == RT_NULL) + if (audio->replay->activated != RT_TRUE) { - struct rt_audio_replay *replay = (struct rt_audio_replay *) rt_malloc(sizeof(struct rt_audio_replay)); - - if (replay == RT_NULL) - { - AUDIO_DBG("request memory for replay error\n"); - return -RT_ENOMEM; - } - - //init queue for audio replay - rt_data_queue_init(&replay->queue, CFG_AUDIO_REPLAY_QUEUE_COUNT, CFG_AUDIO_REPLAY_QUEUE_COUNT / 2, RT_NULL); - - replay->activated = RT_FALSE; - audio->replay = replay; + LOG_D("open audio replay device, oflag = %x\n", oflag); + audio->replay->write_index = 0; + audio->replay->read_index = 0; + audio->replay->pos = 0; + audio->replay->event = REPLAY_EVT_NONE; } - dev->open_flag |= RT_DEVICE_OFLAG_WRONLY; } if (oflag & RT_DEVICE_OFLAG_RDONLY) { - if (audio->record == RT_NULL) + /* open record pipe */ + if (audio->record->activated != RT_TRUE) { - struct rt_audio_record *record = (struct rt_audio_record *) rt_malloc(sizeof(struct rt_audio_record)); - - if (record == RT_NULL) - { - AUDIO_DBG("request memory for record error\n"); - return -RT_ENOMEM; - } - - //init pipe for record - { - rt_uint8_t *buf = (rt_uint8_t *)rt_malloc(CFG_AUDIO_RECORD_PIPE_SIZE); - - if (buf == RT_NULL) - { - rt_free(record); - AUDIO_DBG("request pipe memory error\n"); - - return -RT_ENOMEM; - } - - rt_audio_pipe_init(&audio_pipe, "recpipe", (rt_int32_t)(RT_PIPE_FLAG_FORCE_WR | RT_PIPE_FLAG_BLOCK_RD), buf, - CFG_AUDIO_RECORD_PIPE_SIZE); - } + LOG_D("open audio record device ,oflag = %x\n", oflag); - record->activated = RT_FALSE; - audio->record = record; + _audio_record_start(audio); + audio->record->activated = RT_TRUE; } - - //open record pipe - if (audio->record != RT_NULL) - { - rt_device_open(RT_DEVICE(&audio_pipe), RT_DEVICE_OFLAG_RDONLY); - } - dev->open_flag |= RT_DEVICE_OFLAG_RDONLY; } @@ -193,39 +339,17 @@ static rt_err_t _audio_dev_close(struct rt_device *dev) RT_ASSERT(dev != RT_NULL); audio = (struct rt_audio_device *) dev; - //shutdown the lower device - if (audio->ops->shutdown != RT_NULL) - audio->ops->shutdown(audio); - if (dev->open_flag & RT_DEVICE_OFLAG_WRONLY) { - struct rt_audio_frame frame; - //stop replay stream - audio->ops->stop(audio, AUDIO_STREAM_REPLAY); - - //flush all frame - while (rt_data_queue_peak(&audio->replay->queue, &frame.data_ptr, &frame.data_size) == RT_EOK) - { - //pop the head frame... - rt_data_queue_pop(&audio->replay->queue, &frame.data_ptr, &frame.data_size, RT_WAITING_FOREVER); - - /* notify transmitted complete. */ - if (audio->parent.tx_complete != RT_NULL) - audio->parent.tx_complete(&audio->parent, (void *) frame.data_ptr); - } - + /* stop replay stream */ + _aduio_replay_stop(audio); dev->open_flag &= ~RT_DEVICE_OFLAG_WRONLY; } if (dev->open_flag & RT_DEVICE_OFLAG_RDONLY) { - //stop record stream - audio->ops->stop(audio, AUDIO_STREAM_RECORD); - - //close record pipe - if (audio->record != RT_NULL) - rt_device_close(RT_DEVICE(&audio_pipe)); - + /* stop record stream */ + _audio_record_stop(audio); dev->open_flag &= ~RT_DEVICE_OFLAG_RDONLY; } @@ -237,17 +361,19 @@ static rt_size_t _audio_dev_read(struct rt_device *dev, rt_off_t pos, void *buff struct rt_audio_device *audio; RT_ASSERT(dev != RT_NULL); audio = (struct rt_audio_device *) dev; + if (!(dev->open_flag & RT_DEVICE_OFLAG_RDONLY) || (audio->record == RT_NULL)) return 0; - return rt_device_read(RT_DEVICE(&audio_pipe), pos, buffer, size); + return rt_device_read(RT_DEVICE(&audio->record->pipe), pos, buffer, size); } static rt_size_t _audio_dev_write(struct rt_device *dev, rt_off_t pos, const void *buffer, rt_size_t size) { - rt_err_t result = RT_EOK; - rt_base_t level; + struct rt_audio_device *audio; + rt_uint8_t *ptr; + rt_uint16_t block_size, remain_bytes, index = 0; RT_ASSERT(dev != RT_NULL); audio = (struct rt_audio_device *) dev; @@ -255,34 +381,46 @@ static rt_size_t _audio_dev_write(struct rt_device *dev, rt_off_t pos, const voi if (!(dev->open_flag & RT_DEVICE_OFLAG_WRONLY) || (audio->replay == RT_NULL)) return 0; - AUDIO_DBG("audio write : pos = %d,buffer = %x,size = %d\n",pos,(rt_uint32_t)buffer,size); - //push a new frame to tx queue + /* push a new frame to replay data queue */ + ptr = (rt_uint8_t *)buffer; + block_size = RT_AUDIO_REPLAY_MP_BLOCK_SIZE; + + rt_mutex_take(&audio->replay->lock, RT_WAITING_FOREVER); + while (index < size) { - result = rt_data_queue_push(&audio->replay->queue, buffer, size, - RT_WAITING_FOREVER); - if (result != RT_EOK) + /* request buffer from replay memory pool */ + if (audio->replay->write_index % block_size == 0) + { + audio->replay->write_data = rt_mp_alloc(audio->replay->mp, RT_WAITING_FOREVER); + memset(audio->replay->write_data, 0, block_size); + } + + /* copy data to replay memory pool */ + remain_bytes = MIN((block_size - audio->replay->write_index), (size - index)); + memcpy(&audio->replay->write_data[audio->replay->write_index], &ptr[index], remain_bytes); + + index += remain_bytes; + audio->replay->write_index += remain_bytes; + audio->replay->write_index %= block_size; + + if (audio->replay->write_index == 0) { - AUDIO_DBG("TX frame queue push error\n"); - rt_set_errno(-RT_EFULL); - return 0; + rt_data_queue_push(&audio->replay->queue, + audio->replay->write_data, + block_size, + RT_WAITING_FOREVER); } } + rt_mutex_release(&audio->replay->lock); - //check tx state... - level = rt_hw_interrupt_disable(); + /* check replay state */ if (audio->replay->activated != RT_TRUE) { + _aduio_replay_start(audio); audio->replay->activated = RT_TRUE; - rt_hw_interrupt_enable(level); - - _audio_send_replay_frame(audio); - } - else - { - rt_hw_interrupt_enable(level); } - return size; + return index; } static rt_err_t _audio_dev_control(struct rt_device *dev, int cmd, void *args) @@ -292,112 +430,70 @@ static rt_err_t _audio_dev_control(struct rt_device *dev, int cmd, void *args) RT_ASSERT(dev != RT_NULL); audio = (struct rt_audio_device *) dev; - //dev stat... + /* dev stat...*/ switch (cmd) { - case AUDIO_CTL_GETCAPS: - { - struct rt_audio_caps *caps = (struct rt_audio_caps *) args; + case AUDIO_CTL_GETCAPS: + { + struct rt_audio_caps *caps = (struct rt_audio_caps *) args; - AUDIO_DBG("AUDIO_CTL_GETCAPS: main_type = %d,sub_type = %d\n",caps->main_type,caps->sub_type); - if (audio->ops->getcaps != RT_NULL) - { - result = audio->ops->getcaps(audio, caps); - } - } - break; - case AUDIO_CTL_CONFIGURE: + LOG_D("AUDIO_CTL_GETCAPS: main_type = %d,sub_type = %d", caps->main_type, caps->sub_type); + if (audio->ops->getcaps != RT_NULL) { - struct rt_audio_caps *caps = (struct rt_audio_caps *) args; - - AUDIO_DBG("AUDIO_CTL_CONFIGURE: main_type = %d,sub_type = %d\n",caps->main_type,caps->sub_type); - if (audio->ops->configure != RT_NULL) - { - result = audio->ops->configure(audio, caps); - } + result = audio->ops->getcaps(audio, caps); } break; - case AUDIO_CTL_SHUTDOWN: - { - AUDIO_DBG("AUDIO_CTL_SHUTDOWN\n"); - - if (audio->ops->shutdown != RT_NULL) - result = audio->ops->shutdown(audio); + } - //flush replay frame... - _audio_flush_replay_frame(audio); - } - break; + case AUDIO_CTL_CONFIGURE: + { + struct rt_audio_caps *caps = (struct rt_audio_caps *) args; - case AUDIO_CTL_START: + LOG_D("AUDIO_CTL_CONFIGURE: main_type = %d,sub_type = %d", caps->main_type, caps->sub_type); + if (audio->ops->configure != RT_NULL) { - int stream = *(int *) args; - - AUDIO_DBG("AUDIO_CTL_START: stream = %d\n",stream); - if (audio->ops->start != RT_NULL) - result = audio->ops->start(audio, stream); + result = audio->ops->configure(audio, caps); } + break; - case AUDIO_CTL_STOP: - { - int stream = *(int *) args; + } - AUDIO_DBG("AUDIO_CTL_STOP: stream = %d\n",stream); - if (audio->ops->start != RT_NULL) - result = audio->ops->stop(audio, stream); + case AUDIO_CTL_START: + { + int stream = *(int *) args; - if (stream == AUDIO_STREAM_REPLAY) - { - _audio_flush_replay_frame(audio); - } - } - break; - case AUDIO_CTL_PAUSE: + LOG_D("AUDIO_CTL_START: stream = %d", stream); + if (stream == AUDIO_STREAM_REPLAY) { - int stream = *(int *) args; - - AUDIO_DBG("AUDIO_CTL_PAUSE: stream = %d\n",stream); - if (audio->ops->start != RT_NULL) - result = audio->ops->suspend(audio, stream); + result = _aduio_replay_start(audio); } - break; - case AUDIO_CTL_RESUME: + else { - int stream = *(int *) args; - - AUDIO_DBG("AUDIO_CTL_RESUME: stream = %d\n",stream); - if (audio->ops->start != RT_NULL) - result = audio->ops->resume(audio, stream); - - //resume tx frame... - if (stream == AUDIO_STREAM_REPLAY) - _audio_send_replay_frame(audio); + result = _audio_record_start(audio); } + break; - case AUDIO_CTL_ALLOCBUFFER: - { - struct rt_audio_buf_desc *desc = (struct rt_audio_buf_desc *) args; + } - if (desc) - { - desc->data_size = AUDIO_DEVICE_DECODE_MP_BLOCK_SZ * 2; - desc->data_ptr = (rt_uint8_t *)rt_mp_alloc(&audio->mp, RT_WAITING_FOREVER); + case AUDIO_CTL_STOP: + { + int stream = *(int *) args; - result = RT_EOK; - } - else result = -RT_EIO; + LOG_D("AUDIO_CTL_STOP: stream = %d", stream); + if (stream == AUDIO_STREAM_REPLAY) + { + result = _aduio_replay_stop(audio); } - break; - case AUDIO_CTL_FREEBUFFER: + else { - rt_uint8_t *data_ptr = (rt_uint8_t *) args; - if (data_ptr) - rt_mp_free(data_ptr); + result = _audio_record_stop(audio); } + break; - default: - result = audio->ops->control(audio, cmd, args); + } + + default: break; } @@ -418,7 +514,9 @@ const static struct rt_device_ops audio_ops = rt_err_t rt_audio_register(struct rt_audio_device *audio, const char *name, rt_uint32_t flag, void *data) { + rt_err_t result = RT_EOK; struct rt_device *device; + RT_ASSERT(audio != RT_NULL); device = &(audio->parent); @@ -438,15 +536,14 @@ rt_err_t rt_audio_register(struct rt_audio_device *audio, const char *name, rt_u #endif device->user_data = data; - //init memory pool for replay - { - rt_uint8_t *mempool = (rt_uint8_t *)rt_malloc(AUDIO_DEVICE_DECODE_MP_SZ); - rt_mp_init(&audio->mp, "adu_mp", mempool, AUDIO_DEVICE_DECODE_MP_SZ, - AUDIO_DEVICE_DECODE_MP_BLOCK_SZ * 2); - } - /* register a character device */ - return rt_device_register(device, name, flag | RT_DEVICE_FLAG_REMOVABLE); + result = rt_device_register(device, name, flag | RT_DEVICE_FLAG_REMOVABLE); + + /* initialize audio device */ + if (result == RT_EOK) + result = rt_device_init(device); + + return result; } int rt_audio_samplerate_to_speed(rt_uint32_t bitValue) @@ -454,43 +551,43 @@ int rt_audio_samplerate_to_speed(rt_uint32_t bitValue) int speed = 0; switch (bitValue) { - case AUDIO_SAMP_RATE_8K: - speed = 8000; + case AUDIO_SAMP_RATE_8K: + speed = 8000; break; - case AUDIO_SAMP_RATE_11K: - speed = 11052; + case AUDIO_SAMP_RATE_11K: + speed = 11052; break; - case AUDIO_SAMP_RATE_16K: - speed = 16000; + case AUDIO_SAMP_RATE_16K: + speed = 16000; break; - case AUDIO_SAMP_RATE_22K: - speed = 22050; + case AUDIO_SAMP_RATE_22K: + speed = 22050; break; - case AUDIO_SAMP_RATE_32K: - speed = 32000; + case AUDIO_SAMP_RATE_32K: + speed = 32000; break; - case AUDIO_SAMP_RATE_44K: - speed = 44100; + case AUDIO_SAMP_RATE_44K: + speed = 44100; break; - case AUDIO_SAMP_RATE_48K: - speed = 48000; + case AUDIO_SAMP_RATE_48K: + speed = 48000; break; - case AUDIO_SAMP_RATE_96K: - speed = 96000; + case AUDIO_SAMP_RATE_96K: + speed = 96000; break; - case AUDIO_SAMP_RATE_128K: - speed = 128000; + case AUDIO_SAMP_RATE_128K: + speed = 128000; break; - case AUDIO_SAMP_RATE_160K: - speed = 160000; + case AUDIO_SAMP_RATE_160K: + speed = 160000; break; - case AUDIO_SAMP_RATE_172K: - speed = 176400; + case AUDIO_SAMP_RATE_172K: + speed = 176400; break; - case AUDIO_SAMP_RATE_192K: - speed = 192000; + case AUDIO_SAMP_RATE_192K: + speed = 192000; break; - default: + default: break; } @@ -498,46 +595,18 @@ int rt_audio_samplerate_to_speed(rt_uint32_t bitValue) return speed; } -rt_uint32_t rt_audio_format_to_bits(rt_uint32_t format) +void rt_audio_tx_complete(struct rt_audio_device *audio) { - switch (format) - { - case AUDIO_FMT_PCM_U8: - case AUDIO_FMT_PCM_S8: - return 8; - case AUDIO_FMT_PCM_S16_LE: - case AUDIO_FMT_PCM_S16_BE: - case AUDIO_FMT_PCM_U16_LE: - case AUDIO_FMT_PCM_U16_BE: - return 16; - default: - return 32; - }; -} - -void rt_audio_tx_complete(struct rt_audio_device *audio, rt_uint8_t *pbuf) -{ - rt_err_t result; - AUDIO_DBG("audio tx complete ptr=%x...\n",(rt_uint32_t)pbuf); - - //try to send all frame - do - { - result = _audio_send_replay_frame(audio); - } while (result == RT_EOK); - - /* notify transmitted complete. */ - if (audio->parent.tx_complete != RT_NULL) - audio->parent.tx_complete(&audio->parent, (void *) pbuf); + /* try to send next frame */ + _audio_send_replay_frame(audio); } void rt_audio_rx_done(struct rt_audio_device *audio, rt_uint8_t *pbuf, rt_size_t len) { - //save data to record pipe - rt_device_write(RT_DEVICE(RT_DEVICE(&audio_pipe)), 0, pbuf, len); + /* save data to record pipe */ + rt_device_write(RT_DEVICE(&audio->record->pipe), 0, pbuf, len); /* invoke callback */ if (audio->parent.rx_indicate != RT_NULL) audio->parent.rx_indicate(&audio->parent, len); } - diff --git a/components/drivers/audio/audio_pipe.c b/components/drivers/audio/audio_pipe.c index d6975e962c..d7c37009b6 100644 --- a/components/drivers/audio/audio_pipe.c +++ b/components/drivers/audio/audio_pipe.c @@ -24,8 +24,8 @@ static void _rt_pipe_resume_writer(struct rt_audio_pipe *pipe) /* get suspended thread */ thread = rt_list_entry(pipe->suspended_write_list.next, - struct rt_thread, - tlist); + struct rt_thread, + tlist); /* resume the write thread */ rt_thread_resume(thread); @@ -66,7 +66,8 @@ static rt_size_t rt_pipe_read(rt_device_t dev, /* current context checking */ RT_DEBUG_NOT_IN_INTERRUPT; - do { + do + { level = rt_hw_interrupt_disable(); read_nbytes = rt_ringbuffer_get(&(pipe->ringbuffer), (rt_uint8_t *)buffer, size); if (read_nbytes == 0) @@ -85,7 +86,8 @@ static rt_size_t rt_pipe_read(rt_device_t dev, rt_hw_interrupt_enable(level); break; } - } while (read_nbytes == 0); + } + while (read_nbytes == 0); return read_nbytes; } @@ -104,8 +106,8 @@ static void _rt_pipe_resume_reader(struct rt_audio_pipe *pipe) /* get suspended thread */ thread = rt_list_entry(pipe->suspended_read_list.next, - struct rt_thread, - tlist); + struct rt_thread, + tlist); /* resume the read thread */ rt_thread_resume(thread); @@ -128,7 +130,7 @@ static rt_size_t rt_pipe_write(rt_device_t dev, RT_ASSERT(pipe != RT_NULL); if ((pipe->flag & RT_PIPE_FLAG_FORCE_WR) || - !(pipe->flag & RT_PIPE_FLAG_BLOCK_WR)) + !(pipe->flag & RT_PIPE_FLAG_BLOCK_WR)) { level = rt_hw_interrupt_disable(); @@ -151,7 +153,8 @@ static rt_size_t rt_pipe_write(rt_device_t dev, /* current context checking */ RT_DEBUG_NOT_IN_INTERRUPT; - do { + do + { level = rt_hw_interrupt_disable(); write_nbytes = rt_ringbuffer_put(&(pipe->ringbuffer), (const rt_uint8_t *)buffer, size); if (write_nbytes == 0) @@ -171,7 +174,8 @@ static rt_size_t rt_pipe_write(rt_device_t dev, rt_hw_interrupt_enable(level); break; } - } while (write_nbytes == 0); + } + while (write_nbytes == 0); return write_nbytes; } @@ -183,7 +187,7 @@ static rt_err_t rt_pipe_control(rt_device_t dev, int cmd, void *args) pipe = (struct rt_audio_pipe *)dev; if (cmd == PIPE_CTRL_GET_SPACE && args) - *(rt_size_t*)args = rt_ringbuffer_space_len(&pipe->ringbuffer); + *(rt_size_t *)args = rt_ringbuffer_space_len(&pipe->ringbuffer); return RT_EOK; } @@ -212,10 +216,10 @@ const static struct rt_device_ops audio_pipe_ops = * @return the operation status, RT_EOK on successful */ rt_err_t rt_audio_pipe_init(struct rt_audio_pipe *pipe, - const char *name, - rt_int32_t flag, - rt_uint8_t *buf, - rt_size_t size) + const char *name, + rt_int32_t flag, + rt_uint8_t *buf, + rt_size_t size) { RT_ASSERT(pipe); RT_ASSERT(buf); diff --git a/components/drivers/include/drivers/audio.h b/components/drivers/include/drivers/audio.h index 7d2cb36728..86057e7626 100644 --- a/components/drivers/include/drivers/audio.h +++ b/components/drivers/include/drivers/audio.h @@ -5,52 +5,30 @@ * * Change Logs: * Date Author Notes - * 2017-05-09 Urey first version + * 2017-05-09 Urey first version + * 2019-07-09 Zero-Free improve device ops interface and data flows + * */ #ifndef __AUDIO_H__ #define __AUDIO_H__ +#include "audio_pipe.h" + /* AUDIO command */ #define _AUDIO_CTL(a) (0x10 + a) #define AUDIO_CTL_GETCAPS _AUDIO_CTL(1) #define AUDIO_CTL_CONFIGURE _AUDIO_CTL(2) -#define AUDIO_CTL_SHUTDOWN _AUDIO_CTL(3) -#define AUDIO_CTL_START _AUDIO_CTL(4) -#define AUDIO_CTL_STOP _AUDIO_CTL(5) -#define AUDIO_CTL_PAUSE _AUDIO_CTL(6) -#define AUDIO_CTL_RESUME _AUDIO_CTL(7) -#define AUDIO_CTL_GETBUFFERINFO _AUDIO_CTL(8) -#define AUDIO_CTL_ALLOCBUFFER _AUDIO_CTL(9) -#define AUDIO_CTL_FREEBUFFER _AUDIO_CTL(10) -#define AUDIO_CTL_HWRESET _AUDIO_CTL(11) - +#define AUDIO_CTL_START _AUDIO_CTL(3) +#define AUDIO_CTL_STOP _AUDIO_CTL(4) +#define AUDIO_CTL_GETBUFFERINFO _AUDIO_CTL(5) /* Audio Device Types */ #define AUDIO_TYPE_QUERY 0x00 #define AUDIO_TYPE_INPUT 0x01 #define AUDIO_TYPE_OUTPUT 0x02 #define AUDIO_TYPE_MIXER 0x04 -#define AUDIO_TYPE_SELECTOR 0x08 -#define AUDIO_TYPE_EFFECT 0x10 - -/* Audio Format Types */ -#define AUDIO_FMT_PCM_U8 0x0001 -#define AUDIO_FMT_PCM_S8 0x0002 - -#define AUDIO_FMT_PCM_U16_LE 0x0010 -#define AUDIO_FMT_PCM_S16_BE 0x0020 -#define AUDIO_FMT_PCM_S16_LE 0x0040 -#define AUDIO_FMT_PCM_U16_BE 0x0080 -#define AUDIO_FMT_PCM_U24_LE 0x0100 -#define AUDIO_FMT_PCM_S24_BE 0x0200 -#define AUDIO_FMT_PCM_S24_LE 0x0400 -#define AUDIO_FMT_PCM_U24_BE 0x0800 -#define AUDIO_FMT_PCM_U32_LE 0x1000 -#define AUDIO_FMT_PCM_S32_BE 0x2000 -#define AUDIO_FMT_PCM_S32_LE 0x4000 -#define AUDIO_FMT_PCM_U32_BE 0x8000 /* Supported Sampling Rates */ #define AUDIO_SAMP_RATE_8K 0x0001 @@ -76,13 +54,11 @@ #define AUDIO_BIT_RATE_172K 0x40 #define AUDIO_BIT_RATE_192K 0x80 - - /* Support Dsp(input/output) Units controls */ #define AUDIO_DSP_PARAM 0 /* get/set all params */ -#define AUDIO_DSP_SAMPLERATE 1 /* 采样频率 */ -#define AUDIO_DSP_FMT 2 -#define AUDIO_DSP_CHANNELS 3 +#define AUDIO_DSP_SAMPLERATE 1 /* samplerate */ +#define AUDIO_DSP_CHANNELS 2 /* channels */ +#define AUDIO_DSP_SAMPLEBITS 3 /* sample bits width */ /* Supported Mixer Units controls */ #define AUDIO_MIXER_QUERY 0x0000 @@ -95,15 +71,13 @@ #define AUDIO_MIXER_LINE 0x0040 #define AUDIO_MIXER_DIGITAL 0x0080 #define AUDIO_MIXER_MIC 0x0100 +#define AUDIO_MIXER_VITURAL 0x0200 +#define AUDIO_MIXER_EXTEND 0x8000 /* extend mixer command */ -#define AUDIO_MIXER_EXTEND 0x8000 //extend mixer command +#define AUDIO_VOLUME_MAX (100) +#define AUDIO_VOLUME_MIN (0) #define CFG_AUDIO_REPLAY_QUEUE_COUNT 4 -#define CFG_AUDIO_RECORD_PIPE_SIZE (8 * 1024) -#define AUDIO_DEVICE_MP_CNT (4) -#define AUDIO_DEVICE_DECODE_MP_BLOCK_SZ (4352 * 4) -#define AUDIO_DEVICE_DECODE_MP_SZ ((AUDIO_DEVICE_DECODE_MP_BLOCK_SZ*2 + 4)*AUDIO_DEVICE_MP_CNT) - enum { @@ -115,19 +89,10 @@ enum /* the preferred number and size of audio pipeline buffer for the audio device */ struct rt_audio_buf_info { - rt_uint32_t buffer_size; /* Preferred qty of buffers */ - rt_uint32_t buffer_count; /* Preferred size of the buffers */ -}; -struct rt_audio_buf_desc -{ - rt_uint8_t *data_ptr; - rt_size_t data_size; -}; - -struct rt_audio_frame -{ - const void *data_ptr; - rt_size_t data_size; + rt_uint8_t *buffer; + rt_uint16_t block_size; + rt_uint16_t block_count; + rt_uint32_t total_size; }; struct rt_audio_device; @@ -135,73 +100,68 @@ struct rt_audio_caps; struct rt_audio_configure; struct rt_audio_ops { - rt_err_t (*getcaps) (struct rt_audio_device *audio,struct rt_audio_caps *caps); - rt_err_t (*configure) (struct rt_audio_device *audio,struct rt_audio_caps *caps); - - rt_err_t (*init) (struct rt_audio_device *audio); - rt_err_t (*shutdown) (struct rt_audio_device *audio); - rt_err_t (*start) (struct rt_audio_device *audio,int stream); - rt_err_t (*stop) (struct rt_audio_device *audio,int stream); - rt_err_t (*suspend) (struct rt_audio_device *audio,int stream); - rt_err_t (*resume) (struct rt_audio_device *audio,int stream); - - rt_err_t (*control) (struct rt_audio_device *audio, int cmd, void *arg); - rt_size_t (*transmit) (struct rt_audio_device *audio, const void *writeBuf,void *readBuf, rt_size_t size); - - //get page size of codec or private buffer's info - void (*buffer_info) (struct rt_audio_device *audio,struct rt_audio_buf_info *info ); + rt_err_t (*getcaps)(struct rt_audio_device *audio, struct rt_audio_caps *caps); + rt_err_t (*configure)(struct rt_audio_device *audio, struct rt_audio_caps *caps); + rt_err_t (*init)(struct rt_audio_device *audio); + rt_err_t (*start)(struct rt_audio_device *audio, int stream); + rt_err_t (*stop)(struct rt_audio_device *audio, int stream); + rt_size_t (*transmit)(struct rt_audio_device *audio, const void *writeBuf, void *readBuf, rt_size_t size); + /* get page size of codec or private buffer's info */ + void (*buffer_info)(struct rt_audio_device *audio, struct rt_audio_buf_info *info); }; - struct rt_audio_configure { - rt_uint32_t channels; - - rt_uint32_t samplefmt; rt_uint32_t samplerate; - rt_uint32_t samplefmts; + rt_uint16_t channels; + rt_uint16_t samplebits; }; struct rt_audio_caps { - int main_type; - int sub_type; - - union - { - rt_uint32_t mask; - int value; - struct rt_audio_configure config; - }udata; + int main_type; + int sub_type; + + union + { + rt_uint32_t mask; + int value; + struct rt_audio_configure config; + } udata; }; struct rt_audio_replay { - rt_bool_t activated; + struct rt_mempool *mp; struct rt_data_queue queue; + struct rt_mutex lock; + struct rt_completion cmp; + struct rt_audio_buf_info buf_info; + rt_uint8_t *write_data; + rt_uint16_t write_index; + rt_uint16_t read_index; + rt_uint32_t pos; + rt_uint8_t event; + rt_bool_t activated; }; struct rt_audio_record { + struct rt_audio_pipe pipe; rt_bool_t activated; }; struct rt_audio_device { - struct rt_device parent; + struct rt_device parent; struct rt_audio_ops *ops; - - struct rt_mempool mp; - struct rt_audio_replay *replay; struct rt_audio_record *record; }; -rt_err_t rt_audio_register (struct rt_audio_device *audio, const char *name, rt_uint32_t flag, void *data); -void rt_audio_tx_complete (struct rt_audio_device *audio,rt_uint8_t *pbuf); -void rt_audio_rx_done (struct rt_audio_device *audio,rt_uint8_t *pbuf,rt_size_t len); -rt_uint32_t rt_audio_format_to_bits (rt_uint32_t format); - +rt_err_t rt_audio_register(struct rt_audio_device *audio, const char *name, rt_uint32_t flag, void *data); +void rt_audio_tx_complete(struct rt_audio_device *audio); +void rt_audio_rx_done(struct rt_audio_device *audio, rt_uint8_t *pbuf, rt_size_t len); /* Device Control Commands */ #define CODEC_CMD_RESET 0 -- cgit v1.3.1 From cc565da5a178d598d35cc9b7d4a1a36be5fb17f5 Mon Sep 17 00:00:00 2001 From: tangweikang Date: Fri, 9 Aug 2019 18:37:16 +0800 Subject: [components][drivers] add encoder driven framework --- components/drivers/Kconfig | 4 + components/drivers/include/drivers/encoder.h | 65 +++++++++++++ components/drivers/include/rtdevice.h | 4 + components/drivers/misc/SConscript | 3 + components/drivers/misc/encoder.c | 135 +++++++++++++++++++++++++++ 5 files changed, 211 insertions(+) create mode 100644 components/drivers/include/drivers/encoder.h create mode 100644 components/drivers/misc/encoder.c (limited to 'components/drivers/include') diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index 250d661e9f..ff2639b8fa 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -434,6 +434,10 @@ menuconfig RT_USING_HWCRYPTO endif endif +config RT_USING_ENCODER + bool "Using ENCODER device drivers" + default n + menuconfig RT_USING_WIFI bool "Using Wi-Fi framework" default n diff --git a/components/drivers/include/drivers/encoder.h b/components/drivers/include/drivers/encoder.h new file mode 100644 index 0000000000..c6c3e4d708 --- /dev/null +++ b/components/drivers/include/drivers/encoder.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-08-08 balanceTWK the first version + */ + +#ifndef __ENCODER_H__ +#define __ENCODER_H__ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* encoder control command */ +typedef enum +{ + ENCODER_INFO_GET = 0x01, /* get a encoder feature information */ + ENCODER_SWITCH_ON, /* switch on encoder */ + ENCODER_SWITCH_OFF, /* switch off encoder */ + ENCODER_COUNT_CLEAR, /* clear encoder count */ +} rt_encoder_ctrl_t; + +/* encoder type */ +typedef enum +{ + SINGLE_PHASE_ENCODER = 0x01, /* single phase encoder */ + AB_PHASE_ENCODER /* two phase encoder */ +} rt_encoder_type_t; + +struct rt_encoder_device; + +struct rt_encoder_ops +{ + rt_err_t (*init)(struct rt_encoder_device *encoder); + rt_int32_t (*get_count)(struct rt_encoder_device *encoder); + rt_err_t (*control)(struct rt_encoder_device *encoder, rt_uint32_t cmd, void *args); +}; + +/* Encoder feature information */ +struct rt_encoder_info +{ + rt_encoder_type_t type; /* the type of encoder */ +}; + +typedef struct rt_encoder_device +{ + struct rt_device parent; + const struct rt_encoder_ops *ops; + const struct rt_encoder_info *info; +} rt_encoder_t; + +rt_err_t rt_device_encoder_register(rt_encoder_t *encoder, const char *name, void *user_data); + +#ifdef __cplusplus +} +#endif + +#endif /* __ENCODER_H__ */ diff --git a/components/drivers/include/rtdevice.h b/components/drivers/include/rtdevice.h index f764d30c24..0c009e9580 100644 --- a/components/drivers/include/rtdevice.h +++ b/components/drivers/include/rtdevice.h @@ -126,6 +126,10 @@ extern "C" { #include "drivers/crypto.h" #endif +#ifdef RT_USING_ENCODER +#include "drivers/encoder.h" +#endif + #ifdef __cplusplus } #endif diff --git a/components/drivers/misc/SConscript b/components/drivers/misc/SConscript index c45ba524c5..3bfb73fea0 100644 --- a/components/drivers/misc/SConscript +++ b/components/drivers/misc/SConscript @@ -13,6 +13,9 @@ if GetDepend(['RT_USING_ADC']): if GetDepend(['RT_USING_PWM']): src = src + ['rt_drv_pwm.c'] + +if GetDepend(['RT_USING_ENCODER']): + src = src + ['encoder.c'] if len(src): group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH) diff --git a/components/drivers/misc/encoder.c b/components/drivers/misc/encoder.c new file mode 100644 index 0000000000..d0b2ef9522 --- /dev/null +++ b/components/drivers/misc/encoder.c @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-08-08 balanceTWK the first version + */ + +#include +#include + +static rt_err_t rt_encoder_init(struct rt_device *dev) +{ + rt_encoder_t *encoder; + + encoder = (rt_encoder_t *)dev; + if (encoder->ops->init) + { + return encoder->ops->init(encoder); + } + else + { + return -RT_ENOSYS; + } +} + +static rt_err_t rt_encoder_open(struct rt_device *dev, rt_uint16_t oflag) +{ + rt_encoder_t *encoder; + + encoder = (rt_encoder_t *)dev; + if (encoder->ops->control) + { + return encoder->ops->control(encoder, ENCODER_SWITCH_ON, RT_NULL); + } + else + { + return -RT_ENOSYS; + } +} + +static rt_err_t rt_encoder_close(struct rt_device *dev) +{ + rt_encoder_t *encoder; + + encoder = (rt_encoder_t *)dev; + if (encoder->ops->control) + { + return encoder->ops->control(encoder, ENCODER_SWITCH_OFF, RT_NULL); + } + else + { + return -RT_ENOSYS; + } +} + +static rt_size_t rt_encoder_read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size) +{ + rt_encoder_t *encoder; + + encoder = (rt_encoder_t *)dev; + if (encoder->ops->get_count) + { + *(rt_int32_t *)buffer = encoder->ops->get_count(encoder); + } + return 1; +} + +static rt_err_t rt_encoder_control(struct rt_device *dev, int cmd, void *args) +{ + rt_err_t result; + rt_encoder_t *encoder; + + result = RT_EOK; + encoder = (rt_encoder_t *)dev; + switch (cmd) + { + case ENCODER_INFO_GET: + *(struct rt_encoder_info *)args = *encoder->info; + break; + case ENCODER_SWITCH_ON: + case ENCODER_SWITCH_OFF: + case ENCODER_COUNT_CLEAR: + result = encoder->ops->control(encoder, cmd, args); + break; + default: + result = -RT_ENOSYS; + break; + } + + return result; +} + +#ifdef RT_USING_DEVICE_OPS +const static struct rt_device_ops encoder_ops = +{ + rt_encoder_init, + rt_encoder_open, + rt_encoder_close, + rt_encoder_read, + RT_NULL, + rt_encoder_control +}; +#endif + +rt_err_t rt_device_encoder_register(rt_encoder_t *encoder, const char *name, void *user_data) +{ + struct rt_device *device; + + RT_ASSERT(encoder != RT_NULL); + RT_ASSERT(encoder->ops != RT_NULL); + RT_ASSERT(encoder->info != RT_NULL); + + device = &(encoder->parent); + + device->type = RT_Device_Class_Miscellaneous; + device->rx_indicate = RT_NULL; + device->tx_complete = RT_NULL; + +#ifdef RT_USING_DEVICE_OPS + device->ops = &encoder_ops; +#else + device->init = rt_encoder_init; + device->open = rt_encoder_open; + device->close = rt_encoder_close; + device->read = rt_encoder_read; + device->write = RT_NULL; + device->control = rt_encoder_control; +#endif + device->user_data = user_data; + + return rt_device_register(device, name, RT_DEVICE_FLAG_RDONLY | RT_DEVICE_FLAG_STANDALONE); +} -- cgit v1.3.1 From e5dc60e3cfc5551c18308dd4d2dad1069c6e5f2f Mon Sep 17 00:00:00 2001 From: tangweikang Date: Sun, 11 Aug 2019 15:34:51 +0800 Subject: [components][drivers] update encoder driven framework --- components/drivers/include/drivers/encoder.h | 32 +++++++++--------------- components/drivers/misc/encoder.c | 37 ++++++++++++++-------------- 2 files changed, 30 insertions(+), 39 deletions(-) (limited to 'components/drivers/include') diff --git a/components/drivers/include/drivers/encoder.h b/components/drivers/include/drivers/encoder.h index c6c3e4d708..57cb47e6fb 100644 --- a/components/drivers/include/drivers/encoder.h +++ b/components/drivers/include/drivers/encoder.h @@ -19,20 +19,18 @@ extern "C" { #endif /* encoder control command */ -typedef enum -{ - ENCODER_INFO_GET = 0x01, /* get a encoder feature information */ - ENCODER_SWITCH_ON, /* switch on encoder */ - ENCODER_SWITCH_OFF, /* switch off encoder */ - ENCODER_COUNT_CLEAR, /* clear encoder count */ -} rt_encoder_ctrl_t; +#define ENCODER_CMD_GET_TYPE (128 + 0) /* get a encoder type information */ +#define ENCODER_CMD_ENABLE (128 + 1) /* enable encoder */ +#define ENCODER_CMD_DISABLE (128 + 2) /* disable encoder */ +#define ENCODER_CMD_CLEAR_COUNT (128 + 3) /* clear encoder count */ /* encoder type */ -typedef enum +enum rt_encoder_type { - SINGLE_PHASE_ENCODER = 0x01, /* single phase encoder */ + UNKNOWN_ENCODER_TYPE = 0x00, /* Unknown encoder type */ + SINGLE_PHASE_ENCODER, /* single phase encoder */ AB_PHASE_ENCODER /* two phase encoder */ -} rt_encoder_type_t; +}; struct rt_encoder_device; @@ -43,20 +41,14 @@ struct rt_encoder_ops rt_err_t (*control)(struct rt_encoder_device *encoder, rt_uint32_t cmd, void *args); }; -/* Encoder feature information */ -struct rt_encoder_info -{ - rt_encoder_type_t type; /* the type of encoder */ -}; - -typedef struct rt_encoder_device +struct rt_encoder_device { struct rt_device parent; const struct rt_encoder_ops *ops; - const struct rt_encoder_info *info; -} rt_encoder_t; + enum rt_encoder_type type; +}; -rt_err_t rt_device_encoder_register(rt_encoder_t *encoder, const char *name, void *user_data); +rt_err_t rt_device_encoder_register(struct rt_encoder_device *encoder, const char *name, void *user_data); #ifdef __cplusplus } diff --git a/components/drivers/misc/encoder.c b/components/drivers/misc/encoder.c index d0b2ef9522..ec799bd5fe 100644 --- a/components/drivers/misc/encoder.c +++ b/components/drivers/misc/encoder.c @@ -13,9 +13,9 @@ static rt_err_t rt_encoder_init(struct rt_device *dev) { - rt_encoder_t *encoder; + struct rt_encoder_device *encoder; - encoder = (rt_encoder_t *)dev; + encoder = (struct rt_encoder_device *)dev; if (encoder->ops->init) { return encoder->ops->init(encoder); @@ -28,12 +28,12 @@ static rt_err_t rt_encoder_init(struct rt_device *dev) static rt_err_t rt_encoder_open(struct rt_device *dev, rt_uint16_t oflag) { - rt_encoder_t *encoder; + struct rt_encoder_device *encoder; - encoder = (rt_encoder_t *)dev; + encoder = (struct rt_encoder_device *)dev; if (encoder->ops->control) { - return encoder->ops->control(encoder, ENCODER_SWITCH_ON, RT_NULL); + return encoder->ops->control(encoder, ENCODER_CMD_ENABLE, RT_NULL); } else { @@ -43,12 +43,12 @@ static rt_err_t rt_encoder_open(struct rt_device *dev, rt_uint16_t oflag) static rt_err_t rt_encoder_close(struct rt_device *dev) { - rt_encoder_t *encoder; + struct rt_encoder_device *encoder; - encoder = (rt_encoder_t *)dev; + encoder = (struct rt_encoder_device *)dev; if (encoder->ops->control) { - return encoder->ops->control(encoder, ENCODER_SWITCH_OFF, RT_NULL); + return encoder->ops->control(encoder, ENCODER_CMD_DISABLE, RT_NULL); } else { @@ -58,9 +58,9 @@ static rt_err_t rt_encoder_close(struct rt_device *dev) static rt_size_t rt_encoder_read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size) { - rt_encoder_t *encoder; + struct rt_encoder_device *encoder; - encoder = (rt_encoder_t *)dev; + encoder = (struct rt_encoder_device *)dev; if (encoder->ops->get_count) { *(rt_int32_t *)buffer = encoder->ops->get_count(encoder); @@ -71,18 +71,18 @@ static rt_size_t rt_encoder_read(struct rt_device *dev, rt_off_t pos, void *buff static rt_err_t rt_encoder_control(struct rt_device *dev, int cmd, void *args) { rt_err_t result; - rt_encoder_t *encoder; + struct rt_encoder_device *encoder; result = RT_EOK; - encoder = (rt_encoder_t *)dev; + encoder = (struct rt_encoder_device *)dev; switch (cmd) { - case ENCODER_INFO_GET: - *(struct rt_encoder_info *)args = *encoder->info; + case ENCODER_CMD_GET_TYPE: + *(enum rt_encoder_type *)args = encoder->type; break; - case ENCODER_SWITCH_ON: - case ENCODER_SWITCH_OFF: - case ENCODER_COUNT_CLEAR: + case ENCODER_CMD_ENABLE: + case ENCODER_CMD_DISABLE: + case ENCODER_CMD_CLEAR_COUNT: result = encoder->ops->control(encoder, cmd, args); break; default: @@ -105,13 +105,12 @@ const static struct rt_device_ops encoder_ops = }; #endif -rt_err_t rt_device_encoder_register(rt_encoder_t *encoder, const char *name, void *user_data) +rt_err_t rt_device_encoder_register(struct rt_encoder_device *encoder, const char *name, void *user_data) { struct rt_device *device; RT_ASSERT(encoder != RT_NULL); RT_ASSERT(encoder->ops != RT_NULL); - RT_ASSERT(encoder->info != RT_NULL); device = &(encoder->parent); -- cgit v1.3.1 From 2b9a9e0163a833ca426109738507442d31a089fb Mon Sep 17 00:00:00 2001 From: tangweikang Date: Mon, 12 Aug 2019 13:47:03 +0800 Subject: [components][drivers] update : Move clear_count() function from ops->control() to ops->clear_count() --- components/drivers/include/drivers/encoder.h | 1 + components/drivers/misc/encoder.c | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) (limited to 'components/drivers/include') diff --git a/components/drivers/include/drivers/encoder.h b/components/drivers/include/drivers/encoder.h index 57cb47e6fb..f338f9e845 100644 --- a/components/drivers/include/drivers/encoder.h +++ b/components/drivers/include/drivers/encoder.h @@ -38,6 +38,7 @@ struct rt_encoder_ops { rt_err_t (*init)(struct rt_encoder_device *encoder); rt_int32_t (*get_count)(struct rt_encoder_device *encoder); + rt_err_t (*clear_count)(struct rt_encoder_device *encoder); rt_err_t (*control)(struct rt_encoder_device *encoder, rt_uint32_t cmd, void *args); }; diff --git a/components/drivers/misc/encoder.c b/components/drivers/misc/encoder.c index ec799bd5fe..e9e1da0f40 100644 --- a/components/drivers/misc/encoder.c +++ b/components/drivers/misc/encoder.c @@ -77,12 +77,14 @@ static rt_err_t rt_encoder_control(struct rt_device *dev, int cmd, void *args) encoder = (struct rt_encoder_device *)dev; switch (cmd) { + case ENCODER_CMD_CLEAR_COUNT: + result = encoder->ops->clear_count(encoder); + break; case ENCODER_CMD_GET_TYPE: *(enum rt_encoder_type *)args = encoder->type; break; case ENCODER_CMD_ENABLE: case ENCODER_CMD_DISABLE: - case ENCODER_CMD_CLEAR_COUNT: result = encoder->ops->control(encoder, cmd, args); break; default: -- cgit v1.3.1 From 473505053feebb4eb1e2e870d5e042776556d24c Mon Sep 17 00:00:00 2001 From: tangweikang Date: Tue, 13 Aug 2019 20:14:45 +0800 Subject: [components][drivers] add capture driven framework --- components/drivers/Kconfig | 10 ++ components/drivers/include/drivers/rt_capture.h | 61 ++++++++ components/drivers/include/rtdevice.h | 4 + components/drivers/misc/SConscript | 3 + components/drivers/misc/rt_capture.c | 184 ++++++++++++++++++++++++ 5 files changed, 262 insertions(+) create mode 100644 components/drivers/include/drivers/rt_capture.h create mode 100644 components/drivers/misc/rt_capture.c (limited to 'components/drivers/include') diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index ff2639b8fa..cc3919ccec 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -438,6 +438,16 @@ config RT_USING_ENCODER bool "Using ENCODER device drivers" default n +config RT_USING_CAPTURE + bool "Using CAPTURE device drivers" + default n + +if RT_USING_CAPTURE + config RT_CAPTURE_RB_SIZE + int "Set capture rinbuffer size" + default 100 +endif + menuconfig RT_USING_WIFI bool "Using Wi-Fi framework" default n diff --git a/components/drivers/include/drivers/rt_capture.h b/components/drivers/include/drivers/rt_capture.h new file mode 100644 index 0000000000..efc2ae4db1 --- /dev/null +++ b/components/drivers/include/drivers/rt_capture.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-08-13 balanceTWK first version. + */ + +#ifndef __RT_CAPTURE_H__ +#define __RT_CAPTURE_H__ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* capture control command */ +#define CAPTURE_CMD_CLEAR_BUF (128 + 0) /* clear capture buf */ +#define CAPTURE_CMD_SET_WATERMARK (128 + 1) /* Set the callback threshold */ + +struct capture_data_struct +{ + rt_bool_t is_high; + rt_uint32_t pulsewidth_us; +}; + +struct rt_capture_device +{ + struct rt_device parent; + + const struct rt_capture_ops *ops; + struct rt_ringbuffer *ringbuff; + rt_size_t watermark; +}; + +/** + * capture operators + */ +struct rt_capture_ops +{ + rt_err_t (*init)(struct rt_capture_device *capture); + rt_err_t (*open)(struct rt_capture_device *capture); + rt_err_t (*close)(struct rt_capture_device *capture); + rt_err_t (*get_pulsewidth)(struct rt_capture_device *capture, rt_uint32_t *pulsewidth_us); +}; + +void rt_hw_capture_isr(struct rt_capture_device *capture, rt_bool_t level); + +rt_err_t rt_hw_capture_register(struct rt_capture_device *capture, + const char *name, + rt_uint32_t flag, + void *data); +#ifdef __cplusplus +} +#endif + +#endif /* __RT_CAPTURE_H__ */ diff --git a/components/drivers/include/rtdevice.h b/components/drivers/include/rtdevice.h index 0c009e9580..3ba7f72357 100644 --- a/components/drivers/include/rtdevice.h +++ b/components/drivers/include/rtdevice.h @@ -130,6 +130,10 @@ extern "C" { #include "drivers/encoder.h" #endif +#ifdef RT_USING_CAPTURE +#include "drivers/rt_capture.h" +#endif + #ifdef __cplusplus } #endif diff --git a/components/drivers/misc/SConscript b/components/drivers/misc/SConscript index 3bfb73fea0..7bc638f427 100644 --- a/components/drivers/misc/SConscript +++ b/components/drivers/misc/SConscript @@ -17,6 +17,9 @@ if GetDepend(['RT_USING_PWM']): if GetDepend(['RT_USING_ENCODER']): src = src + ['encoder.c'] +if GetDepend(['RT_USING_CAPTURE']): + src = src + ['rt_capture.c'] + if len(src): group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH) diff --git a/components/drivers/misc/rt_capture.c b/components/drivers/misc/rt_capture.c new file mode 100644 index 0000000000..69337fc319 --- /dev/null +++ b/components/drivers/misc/rt_capture.c @@ -0,0 +1,184 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-08-13 balanceTWK the first version + */ + +#include +#include + +static rt_err_t rt_capture_init(struct rt_device *dev) +{ + struct rt_capture_device *capture; + + RT_ASSERT(dev != RT_NULL); + + capture = (struct rt_capture_device *)dev; + capture->watermark = RT_CAPTURE_RB_SIZE/2; + if(capture->ops->init) + { + return capture->ops->init(capture); + } + else + { + return -RT_ENOSYS; + } +} + +static rt_err_t rt_capture_open(struct rt_device *dev, rt_uint16_t oflag) +{ + struct rt_capture_device *capture; + + RT_ASSERT(dev != RT_NULL); + + capture = (struct rt_capture_device *)dev; + if(capture->ringbuff == RT_NULL) + { + capture->ringbuff = rt_ringbuffer_create(sizeof(struct capture_data_struct)*RT_CAPTURE_RB_SIZE); + } + if(capture->ops->open) + { + return capture->ops->open(capture); + } + else + { + return -RT_ENOSYS; + } +} + +static rt_err_t rt_capture_close(struct rt_device *dev) +{ + rt_err_t ret; + struct rt_capture_device *capture; + + RT_ASSERT(dev != RT_NULL); + + ret = -RT_ERROR; + + capture = (struct rt_capture_device *)dev; + + if (capture->ops->close) + { + ret = capture->ops->close(capture); + } + else + { + return -RT_ENOSYS; + } + if(ret == RT_EOK) + { + if(capture->ringbuff) + { + rt_ringbuffer_destroy(capture->ringbuff); + capture->ringbuff = RT_NULL; + } + } + return ret; +} + +static rt_size_t rt_capture_read(struct rt_device *dev, + rt_off_t pos, + void *buffer, + rt_size_t size) +{ + rt_size_t receive_size; + struct rt_capture_device *capture; + + RT_ASSERT(dev != RT_NULL); + + capture = (struct rt_capture_device *)dev; + receive_size = rt_ringbuffer_get(capture->ringbuff, (rt_uint8_t*)buffer, sizeof(struct capture_data_struct)*size); + + return receive_size/sizeof(struct capture_data_struct); +} + +static rt_err_t rt_capture_control(struct rt_device *dev, int cmd, void *args) +{ + rt_err_t result; + struct rt_capture_device *capture; + + RT_ASSERT(dev != RT_NULL); + + result = RT_EOK; + capture = (struct rt_capture_device *)dev; + switch (cmd) + { + case CAPTURE_CMD_CLEAR_BUF: + rt_ringbuffer_reset(capture->ringbuff); + break; + case CAPTURE_CMD_SET_WATERMARK: + capture->watermark = *(rt_size_t *)args; + break; + default: + result = -RT_ENOSYS; + break; + } + + return result; +} + +#ifdef RT_USING_DEVICE_OPS +const static struct rt_device_ops capture_ops = +{ + rt_capture_init, + rt_capture_open, + rt_capture_close, + rt_capture_read, + RT_NULL, + rt_capture_control +}; +#endif + +rt_err_t rt_device_capture_register(struct rt_capture_device *capture, const char *name, void *user_data) +{ + struct rt_device *device; + + RT_ASSERT(capture != RT_NULL); + RT_ASSERT(capture->ops != RT_NULL); + + device = &(capture->parent); + + device->type = RT_Device_Class_Miscellaneous; + device->rx_indicate = RT_NULL; + device->tx_complete = RT_NULL; + +#ifdef RT_USING_DEVICE_OPS + device->ops = &capture_ops; +#else + device->init = rt_capture_init; + device->open = rt_capture_open; + device->close = rt_capture_close; + device->read = rt_capture_read; + device->write = RT_NULL; + device->control = rt_capture_control; +#endif + device->user_data = user_data; + + return rt_device_register(device, name, RT_DEVICE_FLAG_RDONLY | RT_DEVICE_FLAG_STANDALONE); +} + +/* ISR for capture interrupt */ +void rt_hw_capture_isr(struct rt_capture_device *capture, rt_bool_t level) +{ + struct capture_data_struct data; + rt_size_t receive_size; + if(capture->ops->get_pulsewidth(capture, &data.pulsewidth_us) != RT_EOK) + { + return; + } + + data.is_high = level; + rt_ringbuffer_put(capture->ringbuff, (rt_uint8_t*)&data.pulsewidth_us, sizeof(rt_uint32_t)); + + receive_size = rt_ringbuffer_data_len(capture->ringbuff); + if(receive_size >= capture->watermark) + { + /* indicate to upper layer application */ + if (capture->parent.rx_indicate != RT_NULL) + capture->parent.rx_indicate(&capture->parent, receive_size); + } +} -- cgit v1.3.1 From e1587de6f77936a6795fc0d4042fb641e00ecba8 Mon Sep 17 00:00:00 2001 From: tangweikang Date: Wed, 14 Aug 2019 08:56:48 +0800 Subject: [components][drivers] update capture driven framework --- components/drivers/Kconfig | 2 +- components/drivers/include/drivers/rt_capture.h | 19 +++--- components/drivers/misc/rt_capture.c | 83 ++++++++++++++----------- 3 files changed, 57 insertions(+), 47 deletions(-) (limited to 'components/drivers/include') diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index cc3919ccec..8f8ed92a23 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -444,7 +444,7 @@ config RT_USING_CAPTURE if RT_USING_CAPTURE config RT_CAPTURE_RB_SIZE - int "Set capture rinbuffer size" + int "Set capture ringbuffer size" default 100 endif diff --git a/components/drivers/include/drivers/rt_capture.h b/components/drivers/include/drivers/rt_capture.h index efc2ae4db1..fe72997b43 100644 --- a/components/drivers/include/drivers/rt_capture.h +++ b/components/drivers/include/drivers/rt_capture.h @@ -22,19 +22,19 @@ extern "C" { #define CAPTURE_CMD_CLEAR_BUF (128 + 0) /* clear capture buf */ #define CAPTURE_CMD_SET_WATERMARK (128 + 1) /* Set the callback threshold */ -struct capture_data_struct +struct capture_data { - rt_bool_t is_high; rt_uint32_t pulsewidth_us; + rt_bool_t is_high; }; struct rt_capture_device { - struct rt_device parent; + struct rt_device parent; - const struct rt_capture_ops *ops; - struct rt_ringbuffer *ringbuff; - rt_size_t watermark; + const struct rt_capture_ops *ops; + struct rt_ringbuffer *ringbuff; + rt_size_t watermark; }; /** @@ -50,10 +50,9 @@ struct rt_capture_ops void rt_hw_capture_isr(struct rt_capture_device *capture, rt_bool_t level); -rt_err_t rt_hw_capture_register(struct rt_capture_device *capture, - const char *name, - rt_uint32_t flag, - void *data); +rt_err_t rt_device_capture_register(struct rt_capture_device *capture, + const char *name, + void *data); #ifdef __cplusplus } #endif diff --git a/components/drivers/misc/rt_capture.c b/components/drivers/misc/rt_capture.c index 69337fc319..994def0cce 100644 --- a/components/drivers/misc/rt_capture.c +++ b/components/drivers/misc/rt_capture.c @@ -10,44 +10,45 @@ #include #include +#include static rt_err_t rt_capture_init(struct rt_device *dev) { + rt_err_t ret; struct rt_capture_device *capture; RT_ASSERT(dev != RT_NULL); + ret = RT_EOK; capture = (struct rt_capture_device *)dev; - capture->watermark = RT_CAPTURE_RB_SIZE/2; - if(capture->ops->init) - { - return capture->ops->init(capture); - } - else + capture->watermark = RT_CAPTURE_RB_SIZE / 2; + if (capture->ops->init) { - return -RT_ENOSYS; + ret = capture->ops->init(capture); } + + return ret; } static rt_err_t rt_capture_open(struct rt_device *dev, rt_uint16_t oflag) { + rt_err_t ret; struct rt_capture_device *capture; RT_ASSERT(dev != RT_NULL); + ret = RT_EOK; capture = (struct rt_capture_device *)dev; - if(capture->ringbuff == RT_NULL) + if (capture->ringbuff == RT_NULL) { - capture->ringbuff = rt_ringbuffer_create(sizeof(struct capture_data_struct)*RT_CAPTURE_RB_SIZE); + capture->ringbuff = rt_ringbuffer_create(sizeof(struct capture_data) * RT_CAPTURE_RB_SIZE); } - if(capture->ops->open) + if (capture->ops->open) { - return capture->ops->open(capture); - } - else - { - return -RT_ENOSYS; + ret = capture->ops->open(capture); } + + return ret; } static rt_err_t rt_capture_close(struct rt_device *dev) @@ -58,32 +59,30 @@ static rt_err_t rt_capture_close(struct rt_device *dev) RT_ASSERT(dev != RT_NULL); ret = -RT_ERROR; - capture = (struct rt_capture_device *)dev; if (capture->ops->close) { ret = capture->ops->close(capture); } - else + + if (ret != RT_EOK) { - return -RT_ENOSYS; + return ret; } - if(ret == RT_EOK) + + if (capture->ringbuff) { - if(capture->ringbuff) - { - rt_ringbuffer_destroy(capture->ringbuff); - capture->ringbuff = RT_NULL; - } + rt_ringbuffer_destroy(capture->ringbuff); + capture->ringbuff = RT_NULL; } return ret; } static rt_size_t rt_capture_read(struct rt_device *dev, - rt_off_t pos, - void *buffer, - rt_size_t size) + rt_off_t pos, + void *buffer, + rt_size_t size) { rt_size_t receive_size; struct rt_capture_device *capture; @@ -91,9 +90,9 @@ static rt_size_t rt_capture_read(struct rt_device *dev, RT_ASSERT(dev != RT_NULL); capture = (struct rt_capture_device *)dev; - receive_size = rt_ringbuffer_get(capture->ringbuff, (rt_uint8_t*)buffer, sizeof(struct capture_data_struct)*size); + receive_size = rt_ringbuffer_get(capture->ringbuff, (rt_uint8_t *)buffer, sizeof(struct capture_data) * size); - return receive_size/sizeof(struct capture_data_struct); + return receive_size / sizeof(struct capture_data); } static rt_err_t rt_capture_control(struct rt_device *dev, int cmd, void *args) @@ -108,7 +107,10 @@ static rt_err_t rt_capture_control(struct rt_device *dev, int cmd, void *args) switch (cmd) { case CAPTURE_CMD_CLEAR_BUF: - rt_ringbuffer_reset(capture->ringbuff); + if (capture->ringbuff) + { + rt_ringbuffer_reset(capture->ringbuff); + } break; case CAPTURE_CMD_SET_WATERMARK: capture->watermark = *(rt_size_t *)args; @@ -139,12 +141,14 @@ rt_err_t rt_device_capture_register(struct rt_capture_device *capture, const cha RT_ASSERT(capture != RT_NULL); RT_ASSERT(capture->ops != RT_NULL); + RT_ASSERT(capture->ops->get_pulsewidth != RT_NULL); device = &(capture->parent); device->type = RT_Device_Class_Miscellaneous; device->rx_indicate = RT_NULL; device->tx_complete = RT_NULL; + capture->ringbuff = RT_NULL; #ifdef RT_USING_DEVICE_OPS device->ops = &capture_ops; @@ -161,21 +165,28 @@ rt_err_t rt_device_capture_register(struct rt_capture_device *capture, const cha return rt_device_register(device, name, RT_DEVICE_FLAG_RDONLY | RT_DEVICE_FLAG_STANDALONE); } -/* ISR for capture interrupt */ +/** + * This function is ISR for capture interrupt. + * level: RT_TRUE denotes high level pulse, and RT_FALSE denotes low level pulse. + */ void rt_hw_capture_isr(struct rt_capture_device *capture, rt_bool_t level) { - struct capture_data_struct data; + struct capture_data data; rt_size_t receive_size; - if(capture->ops->get_pulsewidth(capture, &data.pulsewidth_us) != RT_EOK) + if (capture->ops->get_pulsewidth(capture, &data.pulsewidth_us) != RT_EOK) { return; } data.is_high = level; - rt_ringbuffer_put(capture->ringbuff, (rt_uint8_t*)&data.pulsewidth_us, sizeof(rt_uint32_t)); + if (rt_ringbuffer_put(capture->ringbuff, (rt_uint8_t *)&data, sizeof(struct capture_data)) == 0) + { + LOG_W("capture ringbuffer doesn't have enough space."); + } + + receive_size = rt_ringbuffer_data_len(capture->ringbuff) / sizeof(struct capture_data); - receive_size = rt_ringbuffer_data_len(capture->ringbuff); - if(receive_size >= capture->watermark) + if (receive_size >= capture->watermark) { /* indicate to upper layer application */ if (capture->parent.rx_indicate != RT_NULL) -- cgit v1.3.1 From c81eebcd5d8a36e69afbe94319abaa8a04fca3f8 Mon Sep 17 00:00:00 2001 From: tyustli <1225613647@qq.com> Date: Fri, 16 Aug 2019 11:56:26 +0800 Subject: [bsp] [stm32] fix drv_can.c --- bsp/stm32/libraries/HAL_Drivers/drv_can.c | 1018 +++++++++++++------------- bsp/stm32/libraries/HAL_Drivers/drv_can.h | 43 +- bsp/stm32/libraries/STM32H7xx_HAL/SConscript | 8 +- components/drivers/can/can.c | 148 ++-- components/drivers/include/drivers/can.h | 6 +- components/finsh/cmd.c | 2 +- 6 files changed, 639 insertions(+), 586 deletions(-) (limited to 'components/drivers/include') diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_can.c b/bsp/stm32/libraries/HAL_Drivers/drv_can.c index fd700e6c69..d0221b9623 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_can.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_can.c @@ -14,13 +14,14 @@ */ #include "drv_can.h" +#ifdef BSP_USING_CAN -#ifdef RT_USING_CAN +#define LOG_TAG "drv_can" +#include -static void drv_rx_isr(struct rt_can_device *can, rt_uint32_t fifo); - -#if defined (SOC_SERIES_STM32F1) -static const struct stm_baud_rate_tab can_baud_rate_tab[] = +/* attention !!! baud calculation example: Tclk / ((ss + bs1 + bs2) * brp) 36 / ((1 + 8 + 3) * 3) = 1MHz*/ +#if defined (SOC_SERIES_STM32F1)/* APB1 36MHz(max) */ +static const struct stm32_baud_rate_tab can_baud_rate_tab[] = { {CAN1MBaud, (CAN_SJW_2TQ | CAN_BS1_8TQ | CAN_BS2_3TQ | 3)}, {CAN800kBaud, (CAN_SJW_2TQ | CAN_BS1_5TQ | CAN_BS2_3TQ | 5)}, @@ -32,8 +33,8 @@ static const struct stm_baud_rate_tab can_baud_rate_tab[] = {CAN20kBaud, (CAN_SJW_2TQ | CAN_BS1_8TQ | CAN_BS2_3TQ | 150)}, {CAN10kBaud, (CAN_SJW_2TQ | CAN_BS1_8TQ | CAN_BS2_3TQ | 300)} }; -#elif defined (SOC_STM32F429IG) -static const struct stm_baud_rate_tab can_baud_rate_tab[] = +#elif defined (SOC_SERIES_STM32F4)/* APB1 45MHz(max) */ +static const struct stm32_baud_rate_tab can_baud_rate_tab[] = { {CAN1MBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_5TQ | 3)}, {CAN800kBaud, (CAN_SJW_2TQ | CAN_BS1_8TQ | CAN_BS2_5TQ | 4)}, @@ -45,368 +46,112 @@ static const struct stm_baud_rate_tab can_baud_rate_tab[] = {CAN20kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_5TQ | 150)}, {CAN10kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_5TQ | 300)} }; -#elif defined (SOC_SERIES_STM32F4) -static const struct stm_baud_rate_tab can_baud_rate_tab[] = +#elif defined (SOC_SERIES_STM32F7)/* APB1 54MHz(max) */ +static const struct stm32_baud_rate_tab can_baud_rate_tab[] = { - {CAN1MBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_4TQ | 3)}, - {CAN800kBaud, (CAN_SJW_2TQ | CAN_BS1_8TQ | CAN_BS2_4TQ | 4)}, - {CAN500kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_4TQ | 6)}, - {CAN250kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_4TQ | 12)}, - {CAN125kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_4TQ | 24)}, - {CAN100kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_4TQ | 30)}, - {CAN50kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_4TQ | 60)}, - {CAN20kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_4TQ | 150)}, - {CAN10kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_4TQ | 300)} + {CAN1MBaud, (CAN_SJW_2TQ | CAN_BS1_10TQ | CAN_BS2_7TQ | 3)}, + {CAN800kBaud, (CAN_SJW_2TQ | CAN_BS1_9TQ | CAN_BS2_7TQ | 4)}, + {CAN500kBaud, (CAN_SJW_2TQ | CAN_BS1_10TQ | CAN_BS2_7TQ | 6)}, + {CAN250kBaud, (CAN_SJW_2TQ | CAN_BS1_10TQ | CAN_BS2_7TQ | 12)}, + {CAN125kBaud, (CAN_SJW_2TQ | CAN_BS1_10TQ | CAN_BS2_7TQ | 24)}, + {CAN100kBaud, (CAN_SJW_2TQ | CAN_BS1_10TQ | CAN_BS2_7TQ | 30)}, + {CAN50kBaud, (CAN_SJW_2TQ | CAN_BS1_10TQ | CAN_BS2_7TQ | 60)}, + {CAN20kBaud, (CAN_SJW_2TQ | CAN_BS1_10TQ | CAN_BS2_7TQ | 150)}, + {CAN10kBaud, (CAN_SJW_2TQ | CAN_BS1_10TQ | CAN_BS2_7TQ | 300)} }; #endif -#define BAUD_DATA(TYPE,NO) \ - ((can_baud_rate_tab[NO].confdata & TYPE##MASK)) - -static rt_uint32_t get_can_baud_index(rt_uint32_t baud) -{ - rt_uint32_t len, index, default_index; - - len = sizeof(can_baud_rate_tab) / sizeof(can_baud_rate_tab[0]); - default_index = len; - - for (index = 0; index < len; index++) - { - if (can_baud_rate_tab[index].baud_rate == baud) - return index; - - if (can_baud_rate_tab[index].baud_rate == 1000UL * 250) - default_index = index; - } - - if (default_index != len) - return default_index; - - return 0; -} - #ifdef BSP_USING_CAN1 -static struct stm32_drv_can drv_can1; -struct rt_can_device dev_can1; -/** - * @brief This function handles CAN1 TX interrupts. - */ -void CAN1_TX_IRQHandler(void) -{ - rt_interrupt_enter(); - CAN_HandleTypeDef *hcan; - hcan = &drv_can1.CanHandle; - if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_RQCP0)) - { - if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_TXOK0)) - { - rt_hw_can_isr(&dev_can1, RT_CAN_EVENT_TX_DONE | 0 << 8); - } - else - { - rt_hw_can_isr(&dev_can1, RT_CAN_EVENT_TX_FAIL | 0 << 8); - } - /* Write 0 to Clear transmission status flag RQCPx */ - SET_BIT(hcan->Instance->TSR, CAN_TSR_RQCP0); - } - else if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_RQCP1)) - { - if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_TXOK1)) - { - rt_hw_can_isr(&dev_can1, RT_CAN_EVENT_TX_DONE | 1 << 8); - } - else - { - rt_hw_can_isr(&dev_can1, RT_CAN_EVENT_TX_FAIL | 1 << 8); - } - /* Write 0 to Clear transmission status flag RQCPx */ - SET_BIT(hcan->Instance->TSR, CAN_TSR_RQCP1); - } - else if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_RQCP2)) - { - if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_TXOK2)) - { - rt_hw_can_isr(&dev_can1, RT_CAN_EVENT_TX_DONE | 2 << 8); - } - else - { - rt_hw_can_isr(&dev_can1, RT_CAN_EVENT_TX_FAIL | 2 << 8); - } - /* Write 0 to Clear transmission status flag RQCPx */ - SET_BIT(hcan->Instance->TSR, CAN_TSR_RQCP2); - } - rt_interrupt_leave(); -} - -/** - * @brief This function handles CAN1 RX0 interrupts. - */ -void CAN1_RX0_IRQHandler(void) -{ - rt_interrupt_enter(); - drv_rx_isr(&dev_can1, CAN_RX_FIFO0); - rt_interrupt_leave(); -} - -/** - * @brief This function handles CAN1 RX1 interrupts. - */ -void CAN1_RX1_IRQHandler(void) +static struct stm32_can drv_can1 = { - rt_interrupt_enter(); - drv_rx_isr(&dev_can1, CAN_RX_FIFO1); - rt_interrupt_leave(); -} - -/** - * @brief This function handles CAN1 SCE interrupts. - */ -void CAN1_SCE_IRQHandler(void) -{ - rt_uint32_t errtype; - CAN_HandleTypeDef *hcan; - - hcan = &drv_can1.CanHandle; - errtype = hcan->Instance->ESR; - - rt_interrupt_enter(); - HAL_CAN_IRQHandler(hcan); - - if (errtype & 0x70 && dev_can1.status.lasterrtype == (errtype & 0x70)) - { - switch ((errtype & 0x70) >> 4) - { - case RT_CAN_BUS_BIT_PAD_ERR: - dev_can1.status.bitpaderrcnt++; - break; - case RT_CAN_BUS_FORMAT_ERR: - dev_can1.status.formaterrcnt++; - break; - case RT_CAN_BUS_ACK_ERR: - dev_can1.status.ackerrcnt++; - break; - case RT_CAN_BUS_IMPLICIT_BIT_ERR: - case RT_CAN_BUS_EXPLICIT_BIT_ERR: - dev_can1.status.biterrcnt++; - break; - case RT_CAN_BUS_CRC_ERR: - dev_can1.status.crcerrcnt++; - break; - } - dev_can1.status.lasterrtype = errtype & 0x70; - hcan->Instance->ESR &= ~0x70; - } - dev_can1.status.rcverrcnt = errtype >> 24; - dev_can1.status.snderrcnt = (errtype >> 16 & 0xFF); - dev_can1.status.errcode = errtype & 0x07; - hcan->Instance->MSR |= CAN_MSR_ERRI; - rt_interrupt_leave(); -} -#endif /* BSP_USING_CAN1 */ + .name = "can1", + .CanHandle.Instance = CAN1, +}; +#endif #ifdef BSP_USING_CAN2 -static struct stm32_drv_can drv_can2; -struct rt_can_device dev_can2; -/** - * @brief This function handles CAN2 TX interrupts. - */ -void CAN2_TX_IRQHandler(void) +static struct stm32_can drv_can2 = { - rt_interrupt_enter(); - CAN_HandleTypeDef *hcan; - hcan = &drv_can2.CanHandle; - if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_RQCP0)) - { - if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_TXOK0)) - { - rt_hw_can_isr(&dev_can2, RT_CAN_EVENT_TX_DONE | 0 << 8); - } - else - { - rt_hw_can_isr(&dev_can2, RT_CAN_EVENT_TX_FAIL | 0 << 8); - } - /* Write 0 to Clear transmission status flag RQCPx */ - SET_BIT(hcan->Instance->TSR, CAN_TSR_RQCP0); - } - else if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_RQCP1)) - { - if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_TXOK1)) - { - rt_hw_can_isr(&dev_can2, RT_CAN_EVENT_TX_DONE | 1 << 8); - } - else - { - rt_hw_can_isr(&dev_can2, RT_CAN_EVENT_TX_FAIL | 1 << 8); - } - /* Write 0 to Clear transmission status flag RQCPx */ - SET_BIT(hcan->Instance->TSR, CAN_TSR_RQCP1); - } - else if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_RQCP2)) - { - if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_TXOK2)) - { - rt_hw_can_isr(&dev_can2, RT_CAN_EVENT_TX_DONE | 2 << 8); - } - else - { - rt_hw_can_isr(&dev_can2, RT_CAN_EVENT_TX_FAIL | 2 << 8); - } - /* Write 0 to Clear transmission status flag RQCPx */ - SET_BIT(hcan->Instance->TSR, CAN_TSR_RQCP2); - } - rt_interrupt_leave(); -} - -/** - * @brief This function handles CAN2 RX0 interrupts. - */ -void CAN2_RX0_IRQHandler(void) -{ - rt_interrupt_enter(); - drv_rx_isr(&dev_can2, CAN_RX_FIFO0); - rt_interrupt_leave(); -} - -/** - * @brief This function handles CAN2 RX1 interrupts. - */ -void CAN2_RX1_IRQHandler(void) -{ - rt_interrupt_enter(); - drv_rx_isr(&dev_can2, CAN_RX_FIFO1); - rt_interrupt_leave(); -} + "can2", + .CanHandle.Instance = CAN2, +}; +#endif -/** - * @brief This function handles CAN2 SCE interrupts. - */ -void CAN2_SCE_IRQHandler(void) +static rt_uint32_t get_can_baud_index(rt_uint32_t baud) { - rt_uint32_t errtype; - CAN_HandleTypeDef *hcan; - - hcan = &drv_can2.CanHandle; - errtype = hcan->Instance->ESR; + rt_uint32_t len, index; - rt_interrupt_enter(); - HAL_CAN_IRQHandler(hcan); - - if (errtype & 0x70 && dev_can2.status.lasterrtype == (errtype & 0x70)) + len = sizeof(can_baud_rate_tab) / sizeof(can_baud_rate_tab[0]); + for (index = 0; index < len; index++) { - switch ((errtype & 0x70) >> 4) - { - case RT_CAN_BUS_BIT_PAD_ERR: - dev_can2.status.bitpaderrcnt++; - break; - case RT_CAN_BUS_FORMAT_ERR: - dev_can2.status.formaterrcnt++; - break; - case RT_CAN_BUS_ACK_ERR: - dev_can2.status.ackerrcnt++; - break; - case RT_CAN_BUS_IMPLICIT_BIT_ERR: - case RT_CAN_BUS_EXPLICIT_BIT_ERR: - dev_can2.status.biterrcnt++; - break; - case RT_CAN_BUS_CRC_ERR: - dev_can2.status.crcerrcnt++; - break; - } - dev_can2.status.lasterrtype = errtype & 0x70; - hcan->Instance->ESR &= ~0x70; + if (can_baud_rate_tab[index].baud_rate == baud) + return index; } - dev_can2.status.rcverrcnt = errtype >> 24; - dev_can2.status.snderrcnt = (errtype >> 16 & 0xFF); - dev_can2.status.errcode = errtype & 0x07; - hcan->Instance->MSR |= CAN_MSR_ERRI; - rt_interrupt_leave(); -} -#endif /* BSP_USING_CAN2 */ -/** - * @brief Error CAN callback. - * @param hcan pointer to a CAN_HandleTypeDef structure that contains - * the configuration information for the specified CAN. - * @retval None - */ -void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan) -{ - __HAL_CAN_ENABLE_IT(hcan, CAN_IT_ERROR_WARNING | - CAN_IT_ERROR_PASSIVE | - CAN_IT_BUSOFF | - CAN_IT_LAST_ERROR_CODE | - CAN_IT_ERROR | - CAN_IT_RX_FIFO0_MSG_PENDING| - CAN_IT_RX_FIFO0_OVERRUN| - CAN_IT_RX_FIFO1_MSG_PENDING| - CAN_IT_RX_FIFO1_OVERRUN| - CAN_IT_TX_MAILBOX_EMPTY); + return 0; /* default baud is CAN1MBaud */ } - - - -static rt_err_t drv_configure(struct rt_can_device *dev_can, - struct can_configure *cfg) +static rt_err_t _can_config(struct rt_can_device *can, struct can_configure *cfg) { - struct stm32_drv_can *drv_can; + struct stm32_can *drv_can; rt_uint32_t baud_index; - CAN_InitTypeDef *drv_init; - RT_ASSERT(dev_can); + RT_ASSERT(can); RT_ASSERT(cfg); + drv_can = (struct stm32_can *)can->parent.user_data; + RT_ASSERT(drv_can); - drv_can = (struct stm32_drv_can *)dev_can->parent.user_data; - drv_init = &drv_can->CanHandle.Init; - - drv_init->TimeTriggeredMode = DISABLE; - drv_init->AutoBusOff = ENABLE; - drv_init->AutoWakeUp = DISABLE; - drv_init->AutoRetransmission = DISABLE; - drv_init->ReceiveFifoLocked = DISABLE; - drv_init->TransmitFifoPriority = ENABLE; + drv_can->CanHandle.Init.TimeTriggeredMode = DISABLE; + drv_can->CanHandle.Init.AutoBusOff = ENABLE; + drv_can->CanHandle.Init.AutoWakeUp = DISABLE; + drv_can->CanHandle.Init.AutoRetransmission = DISABLE; + drv_can->CanHandle.Init.ReceiveFifoLocked = DISABLE; + drv_can->CanHandle.Init.TransmitFifoPriority = ENABLE; switch (cfg->mode) { case RT_CAN_MODE_NORMAL: - drv_init->Mode = CAN_MODE_NORMAL; + drv_can->CanHandle.Init.Mode = CAN_MODE_NORMAL; break; case RT_CAN_MODE_LISEN: - drv_init->Mode = CAN_MODE_SILENT; + drv_can->CanHandle.Init.Mode = CAN_MODE_SILENT; break; case RT_CAN_MODE_LOOPBACK: - drv_init->Mode = CAN_MODE_LOOPBACK; + drv_can->CanHandle.Init.Mode = CAN_MODE_LOOPBACK; break; case RT_CAN_MODE_LOOPBACKANLISEN: - drv_init->Mode = CAN_MODE_SILENT_LOOPBACK; + drv_can->CanHandle.Init.Mode = CAN_MODE_SILENT_LOOPBACK; break; } baud_index = get_can_baud_index(cfg->baud_rate); - drv_init->SyncJumpWidth = BAUD_DATA(SJW, baud_index); - drv_init->TimeSeg1 = BAUD_DATA(BS1, baud_index); - drv_init->TimeSeg2 = BAUD_DATA(BS2, baud_index); - drv_init->Prescaler = BAUD_DATA(RRESCL, baud_index); + drv_can->CanHandle.Init.SyncJumpWidth = BAUD_DATA(SJW, baud_index); + drv_can->CanHandle.Init.TimeSeg1 = BAUD_DATA(BS1, baud_index); + drv_can->CanHandle.Init.TimeSeg2 = BAUD_DATA(BS2, baud_index); + drv_can->CanHandle.Init.Prescaler = BAUD_DATA(RRESCL, baud_index); + /* init can */ if (HAL_CAN_Init(&drv_can->CanHandle) != HAL_OK) { - return RT_ERROR; + return -RT_ERROR; } - - /* Filter conf */ + + /* default filter config */ HAL_CAN_ConfigFilter(&drv_can->CanHandle, &drv_can->FilterConfig); /* can start */ HAL_CAN_Start(&drv_can->CanHandle); + return RT_EOK; } -static rt_err_t drv_control(struct rt_can_device *can, int cmd, void *arg) +static rt_err_t _can_control(struct rt_can_device *can, int cmd, void *arg) { - struct stm32_drv_can *drv_can = RT_NULL; rt_uint32_t argval; - struct rt_can_filter_config *filter_cfg = RT_NULL; - CAN_FilterTypeDef can_filter; - drv_can = (struct stm32_drv_can *) can->parent.user_data; - assert_param(drv_can != RT_NULL); + struct stm32_can *drv_can; + struct rt_can_filter_config *filter_cfg; + + RT_ASSERT(can != RT_NULL); + drv_can = (struct stm32_can *)can->parent.user_data; + RT_ASSERT(drv_can != RT_NULL); switch (cmd) { @@ -419,13 +164,13 @@ static rt_err_t drv_control(struct rt_can_device *can, int cmd, void *arg) HAL_NVIC_DisableIRQ(CAN1_RX0_IRQn); HAL_NVIC_DisableIRQ(CAN1_RX1_IRQn); } - #ifdef CAN2 - else +#ifdef CAN2 + if (CAN2 == drv_can->CanHandle.Instance) { HAL_NVIC_DisableIRQ(CAN2_RX0_IRQn); HAL_NVIC_DisableIRQ(CAN2_RX1_IRQn); } - #endif +#endif __HAL_CAN_DISABLE_IT(&drv_can->CanHandle, CAN_IT_RX_FIFO0_MSG_PENDING); __HAL_CAN_DISABLE_IT(&drv_can->CanHandle, CAN_IT_RX_FIFO0_FULL); __HAL_CAN_DISABLE_IT(&drv_can->CanHandle, CAN_IT_RX_FIFO0_OVERRUN); @@ -439,12 +184,12 @@ static rt_err_t drv_control(struct rt_can_device *can, int cmd, void *arg) { HAL_NVIC_DisableIRQ(CAN1_TX_IRQn); } - #ifdef CAN2 - else +#ifdef CAN2 + if (CAN2 == drv_can->CanHandle.Instance) { HAL_NVIC_DisableIRQ(CAN2_TX_IRQn); } - #endif +#endif __HAL_CAN_DISABLE_IT(&drv_can->CanHandle, CAN_IT_TX_MAILBOX_EMPTY); } else if (argval == RT_DEVICE_CAN_INT_ERR) @@ -453,12 +198,14 @@ static rt_err_t drv_control(struct rt_can_device *can, int cmd, void *arg) { NVIC_DisableIRQ(CAN1_SCE_IRQn); } - #ifdef CAN2 - else +#ifdef CAN2 + if (CAN2 == drv_can->CanHandle.Instance) { NVIC_DisableIRQ(CAN2_SCE_IRQn); } - #endif +#endif + __HAL_CAN_DISABLE_IT(&drv_can->CanHandle, CAN_IT_ERROR_WARNING); + __HAL_CAN_DISABLE_IT(&drv_can->CanHandle, CAN_IT_ERROR_PASSIVE); __HAL_CAN_DISABLE_IT(&drv_can->CanHandle, CAN_IT_BUSOFF); __HAL_CAN_DISABLE_IT(&drv_can->CanHandle, CAN_IT_LAST_ERROR_CODE); __HAL_CAN_DISABLE_IT(&drv_can->CanHandle, CAN_IT_ERROR); @@ -482,15 +229,15 @@ static rt_err_t drv_control(struct rt_can_device *can, int cmd, void *arg) HAL_NVIC_SetPriority(CAN1_RX1_IRQn, 1, 0); HAL_NVIC_EnableIRQ(CAN1_RX1_IRQn); } - #ifdef CAN2 - else +#ifdef CAN2 + if (CAN2 == drv_can->CanHandle.Instance) { HAL_NVIC_SetPriority(CAN2_RX0_IRQn, 1, 0); HAL_NVIC_EnableIRQ(CAN2_RX0_IRQn); HAL_NVIC_SetPriority(CAN2_RX1_IRQn, 1, 0); HAL_NVIC_EnableIRQ(CAN2_RX1_IRQn); } - #endif +#endif } else if (argval == RT_DEVICE_FLAG_INT_TX) { @@ -501,16 +248,18 @@ static rt_err_t drv_control(struct rt_can_device *can, int cmd, void *arg) HAL_NVIC_SetPriority(CAN1_TX_IRQn, 1, 0); HAL_NVIC_EnableIRQ(CAN1_TX_IRQn); } - #ifdef CAN2 - else +#ifdef CAN2 + if (CAN2 == drv_can->CanHandle.Instance) { HAL_NVIC_SetPriority(CAN2_TX_IRQn, 1, 0); HAL_NVIC_EnableIRQ(CAN2_TX_IRQn); } - #endif +#endif } else if (argval == RT_DEVICE_CAN_INT_ERR) { + __HAL_CAN_ENABLE_IT(&drv_can->CanHandle, CAN_IT_ERROR_WARNING); + __HAL_CAN_ENABLE_IT(&drv_can->CanHandle, CAN_IT_ERROR_PASSIVE); __HAL_CAN_ENABLE_IT(&drv_can->CanHandle, CAN_IT_BUSOFF); __HAL_CAN_ENABLE_IT(&drv_can->CanHandle, CAN_IT_LAST_ERROR_CODE); __HAL_CAN_ENABLE_IT(&drv_can->CanHandle, CAN_IT_ERROR); @@ -520,100 +269,98 @@ static rt_err_t drv_control(struct rt_can_device *can, int cmd, void *arg) HAL_NVIC_SetPriority(CAN1_SCE_IRQn, 1, 0); HAL_NVIC_EnableIRQ(CAN1_SCE_IRQn); } - #ifdef CAN2 - else +#ifdef CAN2 + if (CAN2 == drv_can->CanHandle.Instance) { HAL_NVIC_SetPriority(CAN2_SCE_IRQn, 1, 0); HAL_NVIC_EnableIRQ(CAN2_SCE_IRQn); } - #endif +#endif } break; case RT_CAN_CMD_SET_FILTER: if (RT_NULL == arg) { - /* default Filter conf */ + /* default filter config */ HAL_CAN_ConfigFilter(&drv_can->CanHandle, &drv_can->FilterConfig); } else { filter_cfg = (struct rt_can_filter_config *)arg; /* get default filter */ - can_filter = drv_can->FilterConfig; - for (int i = 0; i < filter_cfg->count; ++i) + for (int i = 0; i < filter_cfg->count; i++) { - can_filter.FilterBank = filter_cfg->items[i].hdr; - can_filter.FilterIdHigh = (filter_cfg->items[i].id >> 13) & 0xFFFF; - can_filter.FilterIdLow = ((filter_cfg->items[i].id << 3) | - (filter_cfg->items[i].ide << 2) | - (filter_cfg->items[i].rtr << 1)) & 0xFFFF; - can_filter.FilterMaskIdHigh = (filter_cfg->items[i].mask >> 16) & 0xFFFF; - can_filter.FilterMaskIdLow = filter_cfg->items[i].mask & 0xFFFF; - can_filter.FilterMode = filter_cfg->items[i].mode; + drv_can->FilterConfig.FilterBank = filter_cfg->items[i].hdr; + drv_can->FilterConfig.FilterIdHigh = (filter_cfg->items[i].id >> 13) & 0xFFFF; + drv_can->FilterConfig.FilterIdLow = ((filter_cfg->items[i].id << 3) | + (filter_cfg->items[i].ide << 2) | + (filter_cfg->items[i].rtr << 1)) & 0xFFFF; + drv_can->FilterConfig.FilterMaskIdHigh = (filter_cfg->items[i].mask >> 16) & 0xFFFF; + drv_can->FilterConfig.FilterMaskIdLow = filter_cfg->items[i].mask & 0xFFFF; + drv_can->FilterConfig.FilterMode = filter_cfg->items[i].mode; /* Filter conf */ - HAL_CAN_ConfigFilter(&drv_can->CanHandle, &can_filter); + HAL_CAN_ConfigFilter(&drv_can->CanHandle, &drv_can->FilterConfig); } } break; case RT_CAN_CMD_SET_MODE: argval = (rt_uint32_t) arg; if (argval != RT_CAN_MODE_NORMAL && - argval != RT_CAN_MODE_LISEN && - argval != RT_CAN_MODE_LOOPBACK && - argval != RT_CAN_MODE_LOOPBACKANLISEN) + argval != RT_CAN_MODE_LISEN && + argval != RT_CAN_MODE_LOOPBACK && + argval != RT_CAN_MODE_LOOPBACKANLISEN) { - return RT_ERROR; + return -RT_ERROR; } - if (argval != can->config.mode) + if (argval != drv_can->device.config.mode) { - can->config.mode = argval; - return drv_configure(can, &can->config); + drv_can->device.config.mode = argval; + return _can_config(&drv_can->device, &drv_can->device.config); } break; case RT_CAN_CMD_SET_BAUD: argval = (rt_uint32_t) arg; if (argval != CAN1MBaud && - argval != CAN800kBaud && - argval != CAN500kBaud && - argval != CAN250kBaud && - argval != CAN125kBaud && - argval != CAN100kBaud && - argval != CAN50kBaud && - argval != CAN20kBaud && - argval != CAN10kBaud) + argval != CAN800kBaud && + argval != CAN500kBaud && + argval != CAN250kBaud && + argval != CAN125kBaud && + argval != CAN100kBaud && + argval != CAN50kBaud && + argval != CAN20kBaud && + argval != CAN10kBaud) { - return RT_ERROR; + return -RT_ERROR; } - if (argval != can->config.baud_rate) + if (argval != drv_can->device.config.baud_rate) { - can->config.baud_rate = argval; - return drv_configure(can, &can->config); + drv_can->device.config.baud_rate = argval; + return _can_config(&drv_can->device, &drv_can->device.config); } break; case RT_CAN_CMD_SET_PRIV: argval = (rt_uint32_t) arg; if (argval != RT_CAN_MODE_PRIV && - argval != RT_CAN_MODE_NOPRIV) + argval != RT_CAN_MODE_NOPRIV) { - return RT_ERROR; + return -RT_ERROR; } - if (argval != can->config.privmode) + if (argval != drv_can->device.config.privmode) { - can->config.privmode = argval; - return drv_configure(can, &can->config); + drv_can->device.config.privmode = argval; + return _can_config(&drv_can->device, &drv_can->device.config); } break; case RT_CAN_CMD_GET_STATUS: { rt_uint32_t errtype; errtype = drv_can->CanHandle.Instance->ESR; - can->status.rcverrcnt = errtype >> 24; - can->status.snderrcnt = (errtype >> 16 & 0xFF); - can->status.errcode = errtype & 0x07; - if (arg != &can->status) - { - rt_memcpy(arg, &can->status, sizeof(can->status)); - } + drv_can->device.status.rcverrcnt = errtype >> 24; + drv_can->device.status.snderrcnt = (errtype >> 16 & 0xFF); + drv_can->device.status.lasterrtype = errtype & 0x70; + drv_can->device.status.errcode = errtype & 0x07; + + rt_memcpy(arg, &drv_can->device.status, sizeof(drv_can->device.status)); } break; } @@ -621,19 +368,28 @@ static rt_err_t drv_control(struct rt_can_device *can, int cmd, void *arg) return RT_EOK; } -static int drv_sendmsg(struct rt_can_device *can, const void *buf, rt_uint32_t boxno) +static int _can_sendmsg(struct rt_can_device *can, const void *buf, rt_uint32_t box_num) { - CAN_HandleTypeDef *hcan = RT_NULL; - hcan = &((struct stm32_drv_can *) can->parent.user_data)->CanHandle; + CAN_HandleTypeDef *hcan; + hcan = &((struct stm32_can *) can->parent.user_data)->CanHandle; struct rt_can_msg *pmsg = (struct rt_can_msg *) buf; CAN_TxHeaderTypeDef txheader = {0}; - - /*check Select mailbox is empty */ - switch (1 << boxno) + HAL_CAN_StateTypeDef state = hcan->State; + + /* Check the parameters */ + RT_ASSERT(IS_CAN_IDTYPE(pmsg->ide)); + RT_ASSERT(IS_CAN_RTR(pmsg->rtr)); + RT_ASSERT(IS_CAN_DLC(pmsg->len)); + + if ((state == HAL_CAN_STATE_READY) || + (state == HAL_CAN_STATE_LISTENING)) { - case CAN_TX_MAILBOX0: - if (HAL_IS_BIT_SET(hcan->Instance->TSR, CAN_TSR_TME0) != SET) - { + /*check select mailbox is empty */ + switch (1 << box_num) + { + case CAN_TX_MAILBOX0: + if (HAL_IS_BIT_SET(hcan->Instance->TSR, CAN_TSR_TME0) != SET) + { /* Change CAN state */ hcan->State = HAL_CAN_STATE_ERROR; /* Return function status */ @@ -641,8 +397,8 @@ static int drv_sendmsg(struct rt_can_device *can, const void *buf, rt_uint32_t b } break; case CAN_TX_MAILBOX1: - if (HAL_IS_BIT_SET(hcan->Instance->TSR, CAN_TSR_TME1) != SET) - { + if (HAL_IS_BIT_SET(hcan->Instance->TSR, CAN_TSR_TME1) != SET) + { /* Change CAN state */ hcan->State = HAL_CAN_STATE_ERROR; /* Return function status */ @@ -650,8 +406,8 @@ static int drv_sendmsg(struct rt_can_device *can, const void *buf, rt_uint32_t b } break; case CAN_TX_MAILBOX2: - if (HAL_IS_BIT_SET(hcan->Instance->TSR, CAN_TSR_TME2) != SET) - { + if (HAL_IS_BIT_SET(hcan->Instance->TSR, CAN_TSR_TME2) != SET) + { /* Change CAN state */ hcan->State = HAL_CAN_STATE_ERROR; /* Return function status */ @@ -661,62 +417,124 @@ static int drv_sendmsg(struct rt_can_device *can, const void *buf, rt_uint32_t b default: RT_ASSERT(0); break; - } - - if (RT_CAN_STDID == pmsg->ide) - { - txheader.IDE = CAN_ID_STD; - txheader.StdId = pmsg->id; + } + + if (RT_CAN_STDID == pmsg->ide) + { + txheader.IDE = CAN_ID_STD; + RT_ASSERT(IS_CAN_STDID(pmsg->id)); + txheader.StdId = pmsg->id; + } + else + { + txheader.IDE = CAN_ID_EXT; + RT_ASSERT(IS_CAN_EXTID(pmsg->id)); + txheader.ExtId = pmsg->id; + } + + if (RT_CAN_DTR == pmsg->rtr) + { + txheader.RTR = CAN_RTR_DATA; + } + else + { + txheader.RTR = CAN_RTR_REMOTE; + } + /* Set up the Id */ + if (RT_CAN_STDID == pmsg->ide) + { + hcan->Instance->sTxMailBox[box_num].TIR |= (txheader.StdId << CAN_TI0R_STID_Pos) | txheader.RTR; + } + else + { + hcan->Instance->sTxMailBox[box_num].TIR |= (txheader.ExtId << CAN_TI0R_EXID_Pos) | txheader.IDE | txheader.RTR; + } + /* Set up the DLC */ + hcan->Instance->sTxMailBox[box_num].TDTR = pmsg->len & 0x0FU; + /* Set up the data field */ + WRITE_REG(hcan->Instance->sTxMailBox[box_num].TDHR, + ((uint32_t)pmsg->data[7] << CAN_TDH0R_DATA7_Pos) | + ((uint32_t)pmsg->data[6] << CAN_TDH0R_DATA6_Pos) | + ((uint32_t)pmsg->data[5] << CAN_TDH0R_DATA5_Pos) | + ((uint32_t)pmsg->data[4] << CAN_TDH0R_DATA4_Pos)); + WRITE_REG(hcan->Instance->sTxMailBox[box_num].TDLR, + ((uint32_t)pmsg->data[3] << CAN_TDL0R_DATA3_Pos) | + ((uint32_t)pmsg->data[2] << CAN_TDL0R_DATA2_Pos) | + ((uint32_t)pmsg->data[1] << CAN_TDL0R_DATA1_Pos) | + ((uint32_t)pmsg->data[0] << CAN_TDL0R_DATA0_Pos)); + /* Request transmission */ + SET_BIT(hcan->Instance->sTxMailBox[box_num].TIR, CAN_TI0R_TXRQ); + + return RT_EOK; } else { - txheader.IDE = CAN_ID_EXT; - txheader.ExtId = pmsg->id; + /* Update error code */ + hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED; + + return -RT_ERROR; } - - if (RT_CAN_DTR == pmsg->rtr) +} + +static int _can_recvmsg(struct rt_can_device *can, void *buf, rt_uint32_t fifo) +{ + HAL_StatusTypeDef status; + CAN_HandleTypeDef *hcan; + struct rt_can_msg *pmsg; + CAN_RxHeaderTypeDef rxheader = {0}; + + RT_ASSERT(can); + + hcan = &((struct stm32_can *)can->parent.user_data)->CanHandle; + pmsg = (struct rt_can_msg *) buf; + + /* get data */ + status = HAL_CAN_GetRxMessage(hcan, fifo, &rxheader, pmsg->data); + if (HAL_OK != status) + return -RT_ERROR; + /* get id */ + if (CAN_ID_STD == rxheader.IDE) { - txheader.RTR = CAN_RTR_DATA; + pmsg->ide = RT_CAN_STDID; + pmsg->id = rxheader.StdId; } else { - txheader.RTR = CAN_RTR_REMOTE; + pmsg->ide = RT_CAN_EXTID; + pmsg->id = rxheader.ExtId; } - - /* clear TIR */ - hcan->Instance->sTxMailBox[boxno].TIR &= CAN_TI0R_TXRQ; - /* Set up the Id */ - if (RT_CAN_STDID == pmsg->ide) + /* get type */ + if (CAN_RTR_DATA == rxheader.RTR) { - hcan->Instance->sTxMailBox[boxno].TIR |= (txheader.StdId << CAN_TI0R_STID_Pos) | txheader.IDE | txheader.RTR; + pmsg->rtr = RT_CAN_DTR; } else { - hcan->Instance->sTxMailBox[boxno].TIR |= (txheader.ExtId << CAN_TI0R_EXID_Pos) | txheader.IDE | txheader.RTR; + pmsg->rtr = RT_CAN_RTR; } - /* Set up the DLC */ - hcan->Instance->sTxMailBox[boxno].TDTR = pmsg->len & 0x0FU; - /* Set up the data field */ - WRITE_REG(hcan->Instance->sTxMailBox[boxno].TDHR, - ((uint32_t)pmsg->data[7] << CAN_TDH0R_DATA7_Pos) | - ((uint32_t)pmsg->data[6] << CAN_TDH0R_DATA6_Pos) | - ((uint32_t)pmsg->data[5] << CAN_TDH0R_DATA5_Pos) | - ((uint32_t)pmsg->data[4] << CAN_TDH0R_DATA4_Pos)); - WRITE_REG(hcan->Instance->sTxMailBox[boxno].TDLR, - ((uint32_t)pmsg->data[3] << CAN_TDL0R_DATA3_Pos) | - ((uint32_t)pmsg->data[2] << CAN_TDL0R_DATA2_Pos) | - ((uint32_t)pmsg->data[1] << CAN_TDL0R_DATA1_Pos) | - ((uint32_t)pmsg->data[0] << CAN_TDL0R_DATA0_Pos)); - /* Request transmission */ - SET_BIT(hcan->Instance->sTxMailBox[boxno].TIR, CAN_TI0R_TXRQ); + /* get len */ + pmsg->len = rxheader.DLC; + /* get hdr */ + pmsg->hdr = rxheader.FilterMatchIndex; + return RT_EOK; } -static void drv_rx_isr(struct rt_can_device *can, rt_uint32_t fifo) + +static const struct rt_can_ops _can_ops = +{ + _can_config, + _can_control, + _can_sendmsg, + _can_recvmsg, +}; + +static void _can_rx_isr(struct rt_can_device *can, rt_uint32_t fifo) { CAN_HandleTypeDef *hcan; - hcan = &((struct stm32_drv_can *) can->parent.user_data)->CanHandle; - + RT_ASSERT(can); + hcan = &((struct stm32_can *) can->parent.user_data)->CanHandle; + switch (fifo) { case CAN_RX_FIFO0: @@ -731,7 +549,7 @@ static void drv_rx_isr(struct rt_can_device *can, rt_uint32_t fifo) /* Clear FIFO0 FULL Flag */ __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FF0); } - + /* Check Overrun flag for FIFO0 */ if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_FOV0) && __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_RX_FIFO0_OVERRUN)) { @@ -752,7 +570,7 @@ static void drv_rx_isr(struct rt_can_device *can, rt_uint32_t fifo) /* Clear FIFO1 FULL Flag */ __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FF1); } - + /* Check Overrun flag for FIFO1 */ if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_FOV1) && __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_RX_FIFO1_OVERRUN)) { @@ -764,105 +582,321 @@ static void drv_rx_isr(struct rt_can_device *can, rt_uint32_t fifo) } } -static int drv_recvmsg(struct rt_can_device *can, void *buf, rt_uint32_t fifo) +#ifdef BSP_USING_CAN1 +/** + * @brief This function handles CAN1 TX interrupts. transmit fifo0/1/2 is empty can trigger this interrupt + */ +void CAN1_TX_IRQHandler(void) { - HAL_StatusTypeDef status; - CAN_HandleTypeDef *hcan = RT_NULL; - struct rt_can_msg *pmsg = (struct rt_can_msg *) buf; - hcan = &((struct stm32_drv_can *) can->parent.user_data)->CanHandle; - CAN_RxHeaderTypeDef rxheader = {0}; - /* get data */ - status = HAL_CAN_GetRxMessage(hcan, fifo, &rxheader, pmsg->data); - if (HAL_OK != status) return -RT_ERROR; - /* get id */ - if (CAN_ID_STD == rxheader.IDE) + rt_interrupt_enter(); + CAN_HandleTypeDef *hcan; + hcan = &drv_can1.CanHandle; + if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_RQCP0)) { - pmsg->ide = RT_CAN_STDID; - pmsg->id = rxheader.StdId; + if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_TXOK0)) + { + rt_hw_can_isr(&drv_can1.device, RT_CAN_EVENT_TX_DONE | 0 << 8); + } + else + { + rt_hw_can_isr(&drv_can1.device, RT_CAN_EVENT_TX_FAIL | 0 << 8); + } + /* Write 0 to Clear transmission status flag RQCPx */ + SET_BIT(hcan->Instance->TSR, CAN_TSR_RQCP0); } - else + else if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_RQCP1)) { - pmsg->ide = RT_CAN_EXTID; - pmsg->id = rxheader.ExtId; + if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_TXOK1)) + { + rt_hw_can_isr(&drv_can1.device, RT_CAN_EVENT_TX_DONE | 1 << 8); + } + else + { + rt_hw_can_isr(&drv_can1.device, RT_CAN_EVENT_TX_FAIL | 1 << 8); + } + /* Write 0 to Clear transmission status flag RQCPx */ + SET_BIT(hcan->Instance->TSR, CAN_TSR_RQCP1); } - /* get type */ - if (CAN_RTR_DATA == rxheader.RTR) + else if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_RQCP2)) { - pmsg->rtr = RT_CAN_DTR; + if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_TXOK2)) + { + rt_hw_can_isr(&drv_can1.device, RT_CAN_EVENT_TX_DONE | 2 << 8); + } + else + { + rt_hw_can_isr(&drv_can1.device, RT_CAN_EVENT_TX_FAIL | 2 << 8); + } + /* Write 0 to Clear transmission status flag RQCPx */ + SET_BIT(hcan->Instance->TSR, CAN_TSR_RQCP2); } - else + rt_interrupt_leave(); +} + +/** + * @brief This function handles CAN1 RX0 interrupts. + */ +void CAN1_RX0_IRQHandler(void) +{ + rt_interrupt_enter(); + _can_rx_isr(&drv_can1.device, CAN_RX_FIFO0); + rt_interrupt_leave(); +} + +/** + * @brief This function handles CAN1 RX1 interrupts. + */ +void CAN1_RX1_IRQHandler(void) +{ + rt_interrupt_enter(); + _can_rx_isr(&drv_can1.device, CAN_RX_FIFO1); + rt_interrupt_leave(); +} + +/** + * @brief This function handles CAN1 SCE interrupts. + */ +void CAN1_SCE_IRQHandler(void) +{ + rt_uint32_t errtype; + CAN_HandleTypeDef *hcan; + + hcan = &drv_can1.CanHandle; + errtype = hcan->Instance->ESR; + + rt_interrupt_enter(); + HAL_CAN_IRQHandler(hcan); + + switch ((errtype & 0x70) >> 4) { - pmsg->rtr = RT_CAN_RTR; + case RT_CAN_BUS_BIT_PAD_ERR: + drv_can1.device.status.bitpaderrcnt++; + break; + case RT_CAN_BUS_FORMAT_ERR: + drv_can1.device.status.formaterrcnt++; + break; + case RT_CAN_BUS_ACK_ERR:/* attention !!! test ack err's unit is transmit unit */ + drv_can1.device.status.ackerrcnt++; + if (!READ_BIT(drv_can1.CanHandle.Instance->TSR, CAN_FLAG_TXOK0)) + rt_hw_can_isr(&drv_can1.device, RT_CAN_EVENT_TX_FAIL | 0 << 8); + else if (!READ_BIT(drv_can1.CanHandle.Instance->TSR, CAN_FLAG_TXOK0)) + rt_hw_can_isr(&drv_can1.device, RT_CAN_EVENT_TX_FAIL | 1 << 8); + else if (!READ_BIT(drv_can1.CanHandle.Instance->TSR, CAN_FLAG_TXOK0)) + rt_hw_can_isr(&drv_can1.device, RT_CAN_EVENT_TX_FAIL | 2 << 8); + break; + case RT_CAN_BUS_IMPLICIT_BIT_ERR: + case RT_CAN_BUS_EXPLICIT_BIT_ERR: + drv_can1.device.status.biterrcnt++; + break; + case RT_CAN_BUS_CRC_ERR: + drv_can1.device.status.crcerrcnt++; + break; } - /* get len */ - pmsg->len = rxheader.DLC; - /* get hdr */ - pmsg->hdr = rxheader.FilterMatchIndex; - return RT_EOK; + + drv_can1.device.status.lasterrtype = errtype & 0x70; + drv_can1.device.status.rcverrcnt = errtype >> 24; + drv_can1.device.status.snderrcnt = (errtype >> 16 & 0xFF); + drv_can1.device.status.errcode = errtype & 0x07; + hcan->Instance->MSR |= CAN_MSR_ERRI; + rt_interrupt_leave(); } +#endif /* BSP_USING_CAN1 */ -static const struct rt_can_ops drv_can_ops = +#ifdef BSP_USING_CAN2 +/** + * @brief This function handles CAN2 TX interrupts. + */ +void CAN2_TX_IRQHandler(void) { - drv_configure, - drv_control, - drv_sendmsg, - drv_recvmsg, -}; + rt_interrupt_enter(); + CAN_HandleTypeDef *hcan; + hcan = &drv_can2.CanHandle; + if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_RQCP0)) + { + if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_TXOK0)) + { + rt_hw_can_isr(&drv_can2.device, RT_CAN_EVENT_TX_DONE | 0 << 8); + } + else + { + rt_hw_can_isr(&drv_can2.device, RT_CAN_EVENT_TX_FAIL | 0 << 8); + } + /* Write 0 to Clear transmission status flag RQCPx */ + SET_BIT(hcan->Instance->TSR, CAN_TSR_RQCP0); + } + else if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_RQCP1)) + { + if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_TXOK1)) + { + rt_hw_can_isr(&drv_can2.device, RT_CAN_EVENT_TX_DONE | 1 << 8); + } + else + { + rt_hw_can_isr(&drv_can2.device, RT_CAN_EVENT_TX_FAIL | 1 << 8); + } + /* Write 0 to Clear transmission status flag RQCPx */ + SET_BIT(hcan->Instance->TSR, CAN_TSR_RQCP1); + } + else if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_RQCP2)) + { + if (__HAL_CAN_GET_FLAG(hcan, CAN_FLAG_TXOK2)) + { + rt_hw_can_isr(&drv_can2.device, RT_CAN_EVENT_TX_DONE | 2 << 8); + } + else + { + rt_hw_can_isr(&drv_can2.device, RT_CAN_EVENT_TX_FAIL | 2 << 8); + } + /* Write 0 to Clear transmission status flag RQCPx */ + SET_BIT(hcan->Instance->TSR, CAN_TSR_RQCP2); + } + rt_interrupt_leave(); +} + +/** + * @brief This function handles CAN2 RX0 interrupts. + */ +void CAN2_RX0_IRQHandler(void) +{ + rt_interrupt_enter(); + _can_rx_isr(&drv_can2.device, CAN_RX_FIFO0); + rt_interrupt_leave(); +} + +/** + * @brief This function handles CAN2 RX1 interrupts. + */ +void CAN2_RX1_IRQHandler(void) +{ + rt_interrupt_enter(); + _can_rx_isr(&drv_can2.device, CAN_RX_FIFO1); + rt_interrupt_leave(); +} + +/** + * @brief This function handles CAN2 SCE interrupts. + */ +void CAN2_SCE_IRQHandler(void) +{ + rt_uint32_t errtype; + CAN_HandleTypeDef *hcan; + + hcan = &drv_can2.CanHandle; + errtype = hcan->Instance->ESR; + + rt_interrupt_enter(); + HAL_CAN_IRQHandler(hcan); + + switch ((errtype & 0x70) >> 4) + { + case RT_CAN_BUS_BIT_PAD_ERR: + drv_can2.device.status.bitpaderrcnt++; + break; + case RT_CAN_BUS_FORMAT_ERR: + drv_can2.device.status.formaterrcnt++; + break; + case RT_CAN_BUS_ACK_ERR: + drv_can2.device.status.ackerrcnt++; + if (!READ_BIT(drv_can1.CanHandle.Instance->TSR, CAN_FLAG_TXOK0)) + rt_hw_can_isr(&drv_can2.device, RT_CAN_EVENT_TX_FAIL | 0 << 8); + else if (!READ_BIT(drv_can2.CanHandle.Instance->TSR, CAN_FLAG_TXOK0)) + rt_hw_can_isr(&drv_can2.device, RT_CAN_EVENT_TX_FAIL | 1 << 8); + else if (!READ_BIT(drv_can2.CanHandle.Instance->TSR, CAN_FLAG_TXOK0)) + rt_hw_can_isr(&drv_can2.device, RT_CAN_EVENT_TX_FAIL | 2 << 8); + break; + case RT_CAN_BUS_IMPLICIT_BIT_ERR: + case RT_CAN_BUS_EXPLICIT_BIT_ERR: + drv_can2.device.status.biterrcnt++; + break; + case RT_CAN_BUS_CRC_ERR: + drv_can2.device.status.crcerrcnt++; + break; + } + + drv_can2.device.status.lasterrtype = errtype & 0x70; + drv_can2.device.status.rcverrcnt = errtype >> 24; + drv_can2.device.status.snderrcnt = (errtype >> 16 & 0xFF); + drv_can2.device.status.errcode = errtype & 0x07; + hcan->Instance->MSR |= CAN_MSR_ERRI; + rt_interrupt_leave(); +} +#endif /* BSP_USING_CAN2 */ + +/** + * @brief Error CAN callback. + * @param hcan pointer to a CAN_HandleTypeDef structure that contains + * the configuration information for the specified CAN. + * @retval None + */ +void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan) +{ + __HAL_CAN_ENABLE_IT(hcan, CAN_IT_ERROR_WARNING | + CAN_IT_ERROR_PASSIVE | + CAN_IT_BUSOFF | + CAN_IT_LAST_ERROR_CODE | + CAN_IT_ERROR | + CAN_IT_RX_FIFO0_MSG_PENDING | + CAN_IT_RX_FIFO0_OVERRUN | + CAN_IT_RX_FIFO0_FULL | + CAN_IT_RX_FIFO1_MSG_PENDING | + CAN_IT_RX_FIFO1_OVERRUN | + CAN_IT_RX_FIFO1_FULL | + CAN_IT_TX_MAILBOX_EMPTY); +} int rt_hw_can_init(void) { - struct stm32_drv_can *drv_can; struct can_configure config = CANDEFAULTCONFIG; - config.privmode = 0; + config.privmode = RT_CAN_MODE_NOPRIV; config.ticks = 50; - config.sndboxnumber = 3; - config.msgboxsz = 32; #ifdef RT_CAN_USING_HDR config.maxhdr = 14; - #ifdef CAN2 +#ifdef CAN2 config.maxhdr = 28; - #endif +#endif #endif /* config default filter */ CAN_FilterTypeDef filterConf = {0}; - filterConf.FilterBank = 0; - filterConf.FilterMode = CAN_FILTERMODE_IDMASK; - filterConf.FilterScale = CAN_FILTERSCALE_32BIT; filterConf.FilterIdHigh = 0x0000; filterConf.FilterIdLow = 0x0000; filterConf.FilterMaskIdHigh = 0x0000; filterConf.FilterMaskIdLow = 0x0000; filterConf.FilterFIFOAssignment = CAN_FILTER_FIFO0; + filterConf.FilterBank = 0; + filterConf.FilterMode = CAN_FILTERMODE_IDMASK; + filterConf.FilterScale = CAN_FILTERSCALE_32BIT; filterConf.FilterActivation = ENABLE; filterConf.SlaveStartFilterBank = 14; - + #ifdef BSP_USING_CAN1 filterConf.FilterBank = 0; + drv_can1.FilterConfig = filterConf; - drv_can = &drv_can1; - drv_can->CanHandle.Instance = CAN1; - dev_can1.ops = &drv_can_ops; - dev_can1.config = config; + drv_can1.device.config = config; /* register CAN1 device */ - rt_hw_can_register(&dev_can1, "can1", - &drv_can_ops, - drv_can); + rt_hw_can_register(&drv_can1.device, + drv_can1.name, + &_can_ops, + &drv_can1); #endif /* BSP_USING_CAN1 */ #ifdef BSP_USING_CAN2 filterConf.FilterBank = filterConf.SlaveStartFilterBank; + drv_can2.FilterConfig = filterConf; - drv_can = &drv_can2; - drv_can->CanHandle.Instance = CAN2; - dev_can2.ops = &drv_can_ops; - dev_can2.config = config; + drv_can2.device.config = config; /* register CAN2 device */ - rt_hw_can_register(&dev_can2, "can2", - &drv_can_ops, - drv_can); + rt_hw_can_register(&drv_can2.device, + drv_can2.name, + &_can_ops, + &drv_can2); #endif /* BSP_USING_CAN2 */ return 0; } + INIT_BOARD_EXPORT(rt_hw_can_init); -#endif /* RT_USING_CAN */ + +#endif /* BSP_USING_CAN */ + +/************************** end of file ******************/ diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_can.h b/bsp/stm32/libraries/HAL_Drivers/drv_can.h index 736df7dec5..b0ae3072e6 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_can.h +++ b/bsp/stm32/libraries/HAL_Drivers/drv_can.h @@ -11,40 +11,49 @@ * 2019-02-19 YLZ port to BSP [stm32] * 2019-06-17 YLZ modify struct stm32_drv_can. */ + #ifndef __DRV_CAN_H__ #define __DRV_CAN_H__ + +#ifdef __cplusplus +extern "C" { +#endif + #include #include -#include #include -#define BS1SHIFT 16 -#define BS2SHIFT 20 -#define RRESCLSHIFT 0 -#define SJWSHIFT 24 -#define BS1MASK ( (0x0F) << BS1SHIFT ) -#define BS2MASK ( (0x07) << BS2SHIFT ) -#define RRESCLMASK ( 0x3FF << RRESCLSHIFT ) -#define SJWMASK ( 0x3 << SJWSHIFT ) +#define BS1SHIFT 16 +#define BS2SHIFT 20 +#define RRESCLSHIFT 0 +#define SJWSHIFT 24 +#define BS1MASK ((0x0F) << BS1SHIFT ) +#define BS2MASK ((0x07) << BS2SHIFT ) +#define RRESCLMASK (0x3FF << RRESCLSHIFT ) +#define SJWMASK (0x3 << SJWSHIFT ) -struct stm_baud_rate_tab +struct stm32_baud_rate_tab { rt_uint32_t baud_rate; - rt_uint32_t confdata; + rt_uint32_t config_data; }; +#define BAUD_DATA(TYPE,NO) ((can_baud_rate_tab[NO].config_data & TYPE##MASK)) -/* STM32 can driver */ -struct stm32_drv_can +/* stm32 can device */ +struct stm32_can { + char *name; CAN_HandleTypeDef CanHandle; CAN_FilterTypeDef FilterConfig; + struct rt_can_device device; /* inherit from can device */ }; -#ifdef __cplusplus - extern "C" { -#endif int rt_hw_can_init(void); + #ifdef __cplusplus - } +} #endif + #endif /*__DRV_CAN_H__ */ + +/************************** end of file ******************/ diff --git a/bsp/stm32/libraries/STM32H7xx_HAL/SConscript b/bsp/stm32/libraries/STM32H7xx_HAL/SConscript index aacf24b3fe..f1548a3525 100644 --- a/bsp/stm32/libraries/STM32H7xx_HAL/SConscript +++ b/bsp/stm32/libraries/STM32H7xx_HAL/SConscript @@ -29,9 +29,9 @@ STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_gpio.c ''') if GetDepend(['RT_USING_SERIAL']): - src += ['STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_uart.c'] - src += ['STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_usart.c'] - src += ['STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_uart_ex.c'] + src += ['STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_uart.c'] + src += ['STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_usart.c'] + src += ['STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_uart_ex.c'] if GetDepend(['RT_USING_I2C']): src += ['STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_i2c.c'] @@ -49,7 +49,7 @@ if GetDepend(['RT_USING_USB_HOST']) or GetDepend(['RT_USING_USB_DEVICE']): src += ['STM32H7xx_HAL_Driver/Src/stm32h7xx_ll_usb.c'] if GetDepend(['RT_USING_CAN']): - src += ['STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_can.c'] + src += ['STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_fdcan.c'] if GetDepend(['RT_USING_HWTIMER']) or GetDepend(['RT_USING_PWM']): src += ['STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_tim.c'] diff --git a/components/drivers/can/can.c b/components/drivers/can/can.c index 0769c79fd7..48604993b9 100644 --- a/components/drivers/can/can.c +++ b/components/drivers/can/can.c @@ -27,10 +27,15 @@ static rt_err_t rt_can_init(struct rt_device *dev) /* initialize rx/tx */ can->can_rx = RT_NULL; can->can_tx = RT_NULL; +#ifdef RT_CAN_USING_HDR + can->hdr = RT_NULL; +#endif /* apply configuration */ if (can->ops->configure) result = can->ops->configure(can, &can->config); + else + result = -RT_ENOSYS; return result; } @@ -293,7 +298,7 @@ static rt_err_t rt_can_open(struct rt_device *dev, rt_uint16_t oflag) can->can_rx = rx_fifo; dev->open_flag |= RT_DEVICE_FLAG_INT_RX; - /* configure low level device */ + /* open can rx interrupt */ can->ops->control(can, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_RX); } } @@ -311,7 +316,7 @@ static rt_err_t rt_can_open(struct rt_device *dev, rt_uint16_t oflag) tx_fifo->buffer = (struct rt_can_sndbxinx_list *)(tx_fifo + 1); rt_memset(tx_fifo->buffer, 0, - can->config.sndboxnumber * sizeof(struct rt_can_sndbxinx_list)); + can->config.sndboxnumber * sizeof(struct rt_can_sndbxinx_list)); rt_list_init(&tx_fifo->freelist); for (i = 0; i < can->config.sndboxnumber; i++) { @@ -325,7 +330,7 @@ static rt_err_t rt_can_open(struct rt_device *dev, rt_uint16_t oflag) can->can_tx = tx_fifo; dev->open_flag |= RT_DEVICE_FLAG_INT_TX; - /* configure low level device */ + /* open can tx interrupt */ can->ops->control(can, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_TX); } } @@ -406,7 +411,7 @@ static rt_err_t rt_can_close(struct rt_device *dev) rt_free(rx_fifo); dev->open_flag &= ~RT_DEVICE_FLAG_INT_RX; can->can_rx = RT_NULL; - /* configure low level device */ + /* clear can rx interrupt */ can->ops->control(can, RT_DEVICE_CTRL_CLR_INT, (void *)RT_DEVICE_FLAG_INT_RX); } @@ -420,7 +425,7 @@ static rt_err_t rt_can_close(struct rt_device *dev) rt_free(tx_fifo); dev->open_flag &= ~RT_DEVICE_FLAG_INT_TX; can->can_tx = RT_NULL; - /* configure low level device */ + /* clear can tx interrupt */ can->ops->control(can, RT_DEVICE_CTRL_CLR_INT, (void *)RT_DEVICE_FLAG_INT_TX); } @@ -484,6 +489,7 @@ static rt_err_t rt_can_control(struct rt_device *dev, struct rt_can_device *can; rt_err_t res; + res = RT_EOK; RT_ASSERT(dev != RT_NULL); can = (struct rt_can_device *)dev; @@ -501,8 +507,9 @@ static rt_err_t rt_can_control(struct rt_device *dev, case RT_DEVICE_CTRL_CONFIG: /* configure device */ - can->ops->configure(can, (struct can_configure *)args); + res = can->ops->configure(can, (struct can_configure *)args); break; + case RT_CAN_CMD_SET_PRIV: /* configure device */ if ((rt_uint32_t)args != can->config.privmode) @@ -513,7 +520,6 @@ static rt_err_t rt_can_control(struct rt_device *dev, res = can->ops->control(can, cmd, args); if (res != RT_EOK) return res; - tx_fifo = (struct rt_can_tx_fifo *) can->can_tx; if (can->config.privmode) { @@ -522,11 +528,11 @@ static rt_err_t rt_can_control(struct rt_device *dev, level = rt_hw_interrupt_disable(); if(rt_list_isempty(&tx_fifo->buffer[i].list)) { - rt_sem_release(&(tx_fifo->sem)); + rt_sem_release(&(tx_fifo->sem)); } else { - rt_list_remove(&tx_fifo->buffer[i].list); + rt_list_remove(&tx_fifo->buffer[i].list); } rt_hw_interrupt_enable(level); } @@ -544,7 +550,6 @@ static rt_err_t rt_can_control(struct rt_device *dev, rt_hw_interrupt_enable(level); } } - return RT_EOK; } break; @@ -561,74 +566,73 @@ static rt_err_t rt_can_control(struct rt_device *dev, return res; } - { - struct rt_can_filter_config *pfilter; - struct rt_can_filter_item *pitem; - rt_uint32_t count; - rt_base_t level; + struct rt_can_filter_config *pfilter; + struct rt_can_filter_item *pitem; + rt_uint32_t count; + rt_base_t level; - pfilter = (struct rt_can_filter_config *)args; - count = pfilter->count; - pitem = pfilter->items; - if (pfilter->actived) + pfilter = (struct rt_can_filter_config *)args; + RT_ASSERT(pfilter); + count = pfilter->count; + pitem = pfilter->items; + if (pfilter->actived) + { + while (count) { - while (count) + if (pitem->hdr >= can->config.maxhdr || pitem->hdr < 0) { - if (pitem->hdr >= can->config.maxhdr || pitem->hdr < 0) - { - count--; - pitem++; - continue; - } - - level = rt_hw_interrupt_disable(); - if (!can->hdr[pitem->hdr].connected) - { - rt_hw_interrupt_enable(level); - rt_memcpy(&can->hdr[pitem->hdr].filter, pitem, - sizeof(struct rt_can_filter_item)); - level = rt_hw_interrupt_disable(); - can->hdr[pitem->hdr].connected = 1; - can->hdr[pitem->hdr].msgs = 0; - rt_list_init(&can->hdr[pitem->hdr].list); - } - rt_hw_interrupt_enable(level); - count--; pitem++; + continue; + } + + level = rt_hw_interrupt_disable(); + if (!can->hdr[pitem->hdr].connected) + { + rt_hw_interrupt_enable(level); + rt_memcpy(&can->hdr[pitem->hdr].filter, pitem, + sizeof(struct rt_can_filter_item)); + level = rt_hw_interrupt_disable(); + can->hdr[pitem->hdr].connected = 1; + can->hdr[pitem->hdr].msgs = 0; + rt_list_init(&can->hdr[pitem->hdr].list); } + rt_hw_interrupt_enable(level); + + count--; + pitem++; } - else + } + else + { + while (count) { - while (count) + if (pitem->hdr >= can->config.maxhdr || pitem->hdr < 0) { - if (pitem->hdr >= can->config.maxhdr || pitem->hdr < 0) - { - count--; - pitem++; - continue; - } - level = rt_hw_interrupt_disable(); + count--; + pitem++; + continue; + } + level = rt_hw_interrupt_disable(); - if (can->hdr[pitem->hdr].connected) + if (can->hdr[pitem->hdr].connected) + { + can->hdr[pitem->hdr].connected = 0; + can->hdr[pitem->hdr].msgs = 0; + if (!rt_list_isempty(&can->hdr[pitem->hdr].list)) { - can->hdr[pitem->hdr].connected = 0; - can->hdr[pitem->hdr].msgs = 0; - if (!rt_list_isempty(&can->hdr[pitem->hdr].list)) - { - rt_list_remove(can->hdr[pitem->hdr].list.next); - } - rt_hw_interrupt_enable(level); - rt_memset(&can->hdr[pitem->hdr].filter, 0, - sizeof(struct rt_can_filter_item)); + rt_list_remove(can->hdr[pitem->hdr].list.next); } - else - { - rt_hw_interrupt_enable(level); - } - count--; - pitem++; + rt_hw_interrupt_enable(level); + rt_memset(&can->hdr[pitem->hdr].filter, 0, + sizeof(struct rt_can_filter_item)); + } + else + { + rt_hw_interrupt_enable(level); } + count--; + pitem++; } } break; @@ -642,12 +646,16 @@ static rt_err_t rt_can_control(struct rt_device *dev, /* control device */ if (can->ops->control != RT_NULL) { - can->ops->control(can, cmd, args); + res = can->ops->control(can, cmd, args); + } + else + { + res = -RT_ENOSYS; } break; } - return RT_EOK; + return res; } /* @@ -655,8 +663,10 @@ static rt_err_t rt_can_control(struct rt_device *dev, */ static void cantimeout(void *arg) { - rt_can_t can = (rt_can_t)arg; + rt_can_t can; + can = (rt_can_t)arg; + RT_ASSERT(can); rt_device_control((rt_device_t)can, RT_CAN_CMD_GET_STATUS, (void *)&can->status); if (can->status_indicate.ind != RT_NULL) @@ -690,7 +700,7 @@ const static struct rt_device_ops can_device_ops = /* * can register */ -rt_err_t rt_hw_can_register(struct rt_can_device *can, +rt_err_t rt_hw_can_register(struct rt_can_device *can, const char *name, const struct rt_can_ops *ops, void *data) diff --git a/components/drivers/include/drivers/can.h b/components/drivers/include/drivers/can.h index c61fb19eaa..d4fcc845b4 100644 --- a/components/drivers/include/drivers/can.h +++ b/components/drivers/include/drivers/can.h @@ -221,7 +221,7 @@ struct rt_can_msg rt_uint32_t rsv : 1; rt_uint32_t len : 8; rt_uint32_t priv : 8; - rt_uint32_t hdr : 8; + rt_int32_t hdr : 8; rt_uint32_t reserved : 8; rt_uint8_t data[8]; }; @@ -252,7 +252,7 @@ struct rt_can_rx_fifo #define RT_CAN_EVENT_RX_IND 0x01 /* Rx indication */ #define RT_CAN_EVENT_TX_DONE 0x02 /* Tx complete */ -#define RT_CAN_EVENT_TX_FAIL 0x03 /* Tx complete */ +#define RT_CAN_EVENT_TX_FAIL 0x03 /* Tx fail */ #define RT_CAN_EVENT_RX_TIMEOUT 0x05 /* Rx timeout */ #define RT_CAN_EVENT_RXOF_IND 0x06 /* Rx overflow */ @@ -278,7 +278,7 @@ struct rt_can_ops int (*recvmsg)(struct rt_can_device *can, void *buf, rt_uint32_t boxno); }; -rt_err_t rt_hw_can_register(struct rt_can_device *can, +rt_err_t rt_hw_can_register(struct rt_can_device *can, const char *name, const struct rt_can_ops *ops, void *data); diff --git a/components/finsh/cmd.c b/components/finsh/cmd.c index 608e962f6c..570f525bca 100644 --- a/components/finsh/cmd.c +++ b/components/finsh/cmd.c @@ -847,7 +847,7 @@ long list_device(void) device->parent.name, (device->type <= RT_Device_Class_Unknown) ? device_type_str[device->type] : - device_type_str[RT_Device_Class_Unknown], + device_type_str[RT_Device_Class_Unknown - 1], device->ref_count); } -- cgit v1.3.1 From f9bad31d57132fce38cc1036efaec045ca21c79b Mon Sep 17 00:00:00 2001 From: tangweikang Date: Wed, 21 Aug 2019 10:40:20 +0800 Subject: [components][drivers] change name: capture -> inputcapture --- components/drivers/Kconfig | 10 +- components/drivers/include/drivers/rt_capture.h | 60 ------- .../drivers/include/drivers/rt_inputcapture.h | 60 +++++++ components/drivers/include/rtdevice.h | 4 +- components/drivers/misc/SConscript | 4 +- components/drivers/misc/rt_capture.c | 195 --------------------- components/drivers/misc/rt_inputcapture.c | 195 +++++++++++++++++++++ 7 files changed, 264 insertions(+), 264 deletions(-) delete mode 100644 components/drivers/include/drivers/rt_capture.h create mode 100644 components/drivers/include/drivers/rt_inputcapture.h delete mode 100644 components/drivers/misc/rt_capture.c create mode 100644 components/drivers/misc/rt_inputcapture.c (limited to 'components/drivers/include') diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index 8f8ed92a23..ff30fa6162 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -438,13 +438,13 @@ config RT_USING_ENCODER bool "Using ENCODER device drivers" default n -config RT_USING_CAPTURE - bool "Using CAPTURE device drivers" +config RT_USING_INPUT_CAPTURE + bool "Using INPUT CAPTURE device drivers" default n -if RT_USING_CAPTURE - config RT_CAPTURE_RB_SIZE - int "Set capture ringbuffer size" +if RT_USING_INPUT_CAPTURE + config RT_INPUT_CAPTURE_RB_SIZE + int "Set input capture ringbuffer size" default 100 endif diff --git a/components/drivers/include/drivers/rt_capture.h b/components/drivers/include/drivers/rt_capture.h deleted file mode 100644 index fe72997b43..0000000000 --- a/components/drivers/include/drivers/rt_capture.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2019-08-13 balanceTWK first version. - */ - -#ifndef __RT_CAPTURE_H__ -#define __RT_CAPTURE_H__ - -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/* capture control command */ -#define CAPTURE_CMD_CLEAR_BUF (128 + 0) /* clear capture buf */ -#define CAPTURE_CMD_SET_WATERMARK (128 + 1) /* Set the callback threshold */ - -struct capture_data -{ - rt_uint32_t pulsewidth_us; - rt_bool_t is_high; -}; - -struct rt_capture_device -{ - struct rt_device parent; - - const struct rt_capture_ops *ops; - struct rt_ringbuffer *ringbuff; - rt_size_t watermark; -}; - -/** - * capture operators - */ -struct rt_capture_ops -{ - rt_err_t (*init)(struct rt_capture_device *capture); - rt_err_t (*open)(struct rt_capture_device *capture); - rt_err_t (*close)(struct rt_capture_device *capture); - rt_err_t (*get_pulsewidth)(struct rt_capture_device *capture, rt_uint32_t *pulsewidth_us); -}; - -void rt_hw_capture_isr(struct rt_capture_device *capture, rt_bool_t level); - -rt_err_t rt_device_capture_register(struct rt_capture_device *capture, - const char *name, - void *data); -#ifdef __cplusplus -} -#endif - -#endif /* __RT_CAPTURE_H__ */ diff --git a/components/drivers/include/drivers/rt_inputcapture.h b/components/drivers/include/drivers/rt_inputcapture.h new file mode 100644 index 0000000000..0cadf6444c --- /dev/null +++ b/components/drivers/include/drivers/rt_inputcapture.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-08-13 balanceTWK first version. + */ + +#ifndef __RT_INPUT_CAPTURE_H__ +#define __RT_INPUT_CAPTURE_H__ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* capture control command */ +#define INPUTCAPTURE_CMD_CLEAR_BUF (128 + 0) /* clear capture buf */ +#define INPUTCAPTURE_CMD_SET_WATERMARK (128 + 1) /* Set the callback threshold */ + +struct rt_inputcapture_data +{ + rt_uint32_t pulsewidth_us; + rt_bool_t is_high; +}; + +struct rt_inputcapture_device +{ + struct rt_device parent; + + const struct rt_inputcapture_ops *ops; + struct rt_ringbuffer *ringbuff; + rt_size_t watermark; +}; + +/** + * capture operators + */ +struct rt_inputcapture_ops +{ + rt_err_t (*init)(struct rt_inputcapture_device *inputcapture); + rt_err_t (*open)(struct rt_inputcapture_device *inputcapture); + rt_err_t (*close)(struct rt_inputcapture_device *inputcapture); + rt_err_t (*get_pulsewidth)(struct rt_inputcapture_device *inputcapture, rt_uint32_t *pulsewidth_us); +}; + +void rt_hw_capture_isr(struct rt_inputcapture_device *inputcapture, rt_bool_t level); + +rt_err_t rt_device_inputcapture_register(struct rt_inputcapture_device *inputcapture, + const char *name, + void *data); +#ifdef __cplusplus +} +#endif + +#endif /* __RT_INPUT_CAPTURE_H__ */ diff --git a/components/drivers/include/rtdevice.h b/components/drivers/include/rtdevice.h index 3ba7f72357..bb302ac2af 100644 --- a/components/drivers/include/rtdevice.h +++ b/components/drivers/include/rtdevice.h @@ -130,8 +130,8 @@ extern "C" { #include "drivers/encoder.h" #endif -#ifdef RT_USING_CAPTURE -#include "drivers/rt_capture.h" +#ifdef RT_USING_INPUT_CAPTURE +#include "drivers/rt_inputcapture.h" #endif #ifdef __cplusplus diff --git a/components/drivers/misc/SConscript b/components/drivers/misc/SConscript index 7bc638f427..f0d177f761 100644 --- a/components/drivers/misc/SConscript +++ b/components/drivers/misc/SConscript @@ -17,8 +17,8 @@ if GetDepend(['RT_USING_PWM']): if GetDepend(['RT_USING_ENCODER']): src = src + ['encoder.c'] -if GetDepend(['RT_USING_CAPTURE']): - src = src + ['rt_capture.c'] +if GetDepend(['RT_USING_INPUT_CAPTURE']): + src = src + ['rt_inputcapture.c'] if len(src): group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH) diff --git a/components/drivers/misc/rt_capture.c b/components/drivers/misc/rt_capture.c deleted file mode 100644 index 994def0cce..0000000000 --- a/components/drivers/misc/rt_capture.c +++ /dev/null @@ -1,195 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2019-08-13 balanceTWK the first version - */ - -#include -#include -#include - -static rt_err_t rt_capture_init(struct rt_device *dev) -{ - rt_err_t ret; - struct rt_capture_device *capture; - - RT_ASSERT(dev != RT_NULL); - - ret = RT_EOK; - capture = (struct rt_capture_device *)dev; - capture->watermark = RT_CAPTURE_RB_SIZE / 2; - if (capture->ops->init) - { - ret = capture->ops->init(capture); - } - - return ret; -} - -static rt_err_t rt_capture_open(struct rt_device *dev, rt_uint16_t oflag) -{ - rt_err_t ret; - struct rt_capture_device *capture; - - RT_ASSERT(dev != RT_NULL); - - ret = RT_EOK; - capture = (struct rt_capture_device *)dev; - if (capture->ringbuff == RT_NULL) - { - capture->ringbuff = rt_ringbuffer_create(sizeof(struct capture_data) * RT_CAPTURE_RB_SIZE); - } - if (capture->ops->open) - { - ret = capture->ops->open(capture); - } - - return ret; -} - -static rt_err_t rt_capture_close(struct rt_device *dev) -{ - rt_err_t ret; - struct rt_capture_device *capture; - - RT_ASSERT(dev != RT_NULL); - - ret = -RT_ERROR; - capture = (struct rt_capture_device *)dev; - - if (capture->ops->close) - { - ret = capture->ops->close(capture); - } - - if (ret != RT_EOK) - { - return ret; - } - - if (capture->ringbuff) - { - rt_ringbuffer_destroy(capture->ringbuff); - capture->ringbuff = RT_NULL; - } - return ret; -} - -static rt_size_t rt_capture_read(struct rt_device *dev, - rt_off_t pos, - void *buffer, - rt_size_t size) -{ - rt_size_t receive_size; - struct rt_capture_device *capture; - - RT_ASSERT(dev != RT_NULL); - - capture = (struct rt_capture_device *)dev; - receive_size = rt_ringbuffer_get(capture->ringbuff, (rt_uint8_t *)buffer, sizeof(struct capture_data) * size); - - return receive_size / sizeof(struct capture_data); -} - -static rt_err_t rt_capture_control(struct rt_device *dev, int cmd, void *args) -{ - rt_err_t result; - struct rt_capture_device *capture; - - RT_ASSERT(dev != RT_NULL); - - result = RT_EOK; - capture = (struct rt_capture_device *)dev; - switch (cmd) - { - case CAPTURE_CMD_CLEAR_BUF: - if (capture->ringbuff) - { - rt_ringbuffer_reset(capture->ringbuff); - } - break; - case CAPTURE_CMD_SET_WATERMARK: - capture->watermark = *(rt_size_t *)args; - break; - default: - result = -RT_ENOSYS; - break; - } - - return result; -} - -#ifdef RT_USING_DEVICE_OPS -const static struct rt_device_ops capture_ops = -{ - rt_capture_init, - rt_capture_open, - rt_capture_close, - rt_capture_read, - RT_NULL, - rt_capture_control -}; -#endif - -rt_err_t rt_device_capture_register(struct rt_capture_device *capture, const char *name, void *user_data) -{ - struct rt_device *device; - - RT_ASSERT(capture != RT_NULL); - RT_ASSERT(capture->ops != RT_NULL); - RT_ASSERT(capture->ops->get_pulsewidth != RT_NULL); - - device = &(capture->parent); - - device->type = RT_Device_Class_Miscellaneous; - device->rx_indicate = RT_NULL; - device->tx_complete = RT_NULL; - capture->ringbuff = RT_NULL; - -#ifdef RT_USING_DEVICE_OPS - device->ops = &capture_ops; -#else - device->init = rt_capture_init; - device->open = rt_capture_open; - device->close = rt_capture_close; - device->read = rt_capture_read; - device->write = RT_NULL; - device->control = rt_capture_control; -#endif - device->user_data = user_data; - - return rt_device_register(device, name, RT_DEVICE_FLAG_RDONLY | RT_DEVICE_FLAG_STANDALONE); -} - -/** - * This function is ISR for capture interrupt. - * level: RT_TRUE denotes high level pulse, and RT_FALSE denotes low level pulse. - */ -void rt_hw_capture_isr(struct rt_capture_device *capture, rt_bool_t level) -{ - struct capture_data data; - rt_size_t receive_size; - if (capture->ops->get_pulsewidth(capture, &data.pulsewidth_us) != RT_EOK) - { - return; - } - - data.is_high = level; - if (rt_ringbuffer_put(capture->ringbuff, (rt_uint8_t *)&data, sizeof(struct capture_data)) == 0) - { - LOG_W("capture ringbuffer doesn't have enough space."); - } - - receive_size = rt_ringbuffer_data_len(capture->ringbuff) / sizeof(struct capture_data); - - if (receive_size >= capture->watermark) - { - /* indicate to upper layer application */ - if (capture->parent.rx_indicate != RT_NULL) - capture->parent.rx_indicate(&capture->parent, receive_size); - } -} diff --git a/components/drivers/misc/rt_inputcapture.c b/components/drivers/misc/rt_inputcapture.c new file mode 100644 index 0000000000..d1886186d7 --- /dev/null +++ b/components/drivers/misc/rt_inputcapture.c @@ -0,0 +1,195 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-08-13 balanceTWK the first version + */ + +#include +#include +#include + +static rt_err_t rt_inputcapture_init(struct rt_device *dev) +{ + rt_err_t ret; + struct rt_inputcapture_device *inputcapture; + + RT_ASSERT(dev != RT_NULL); + + ret = RT_EOK; + inputcapture = (struct rt_inputcapture_device *)dev; + inputcapture->watermark = RT_INPUT_CAPTURE_RB_SIZE / 2; + if (inputcapture->ops->init) + { + ret = inputcapture->ops->init(inputcapture); + } + + return ret; +} + +static rt_err_t rt_inputcapture_open(struct rt_device *dev, rt_uint16_t oflag) +{ + rt_err_t ret; + struct rt_inputcapture_device *inputcapture; + + RT_ASSERT(dev != RT_NULL); + + ret = RT_EOK; + inputcapture = (struct rt_inputcapture_device *)dev; + if (inputcapture->ringbuff == RT_NULL) + { + inputcapture->ringbuff = rt_ringbuffer_create(sizeof(struct rt_inputcapture_data) * RT_INPUT_CAPTURE_RB_SIZE); + } + if (inputcapture->ops->open) + { + ret = inputcapture->ops->open(inputcapture); + } + + return ret; +} + +static rt_err_t rt_inputcapture_close(struct rt_device *dev) +{ + rt_err_t ret; + struct rt_inputcapture_device *inputcapture; + + RT_ASSERT(dev != RT_NULL); + + ret = -RT_ERROR; + inputcapture = (struct rt_inputcapture_device *)dev; + + if (inputcapture->ops->close) + { + ret = inputcapture->ops->close(inputcapture); + } + + if (ret != RT_EOK) + { + return ret; + } + + if (inputcapture->ringbuff) + { + rt_ringbuffer_destroy(inputcapture->ringbuff); + inputcapture->ringbuff = RT_NULL; + } + return ret; +} + +static rt_size_t rt_inputcapture_read(struct rt_device *dev, + rt_off_t pos, + void *buffer, + rt_size_t size) +{ + rt_size_t receive_size; + struct rt_inputcapture_device *inputcapture; + + RT_ASSERT(dev != RT_NULL); + + inputcapture = (struct rt_inputcapture_device *)dev; + receive_size = rt_ringbuffer_get(inputcapture->ringbuff, (rt_uint8_t *)buffer, sizeof(struct rt_inputcapture_data) * size); + + return receive_size / sizeof(struct rt_inputcapture_data); +} + +static rt_err_t rt_inputcapture_control(struct rt_device *dev, int cmd, void *args) +{ + rt_err_t result; + struct rt_inputcapture_device *inputcapture; + + RT_ASSERT(dev != RT_NULL); + + result = RT_EOK; + inputcapture = (struct rt_inputcapture_device *)dev; + switch (cmd) + { + case INPUTCAPTURE_CMD_CLEAR_BUF: + if (inputcapture->ringbuff) + { + rt_ringbuffer_reset(inputcapture->ringbuff); + } + break; + case INPUTCAPTURE_CMD_SET_WATERMARK: + inputcapture->watermark = *(rt_size_t *)args; + break; + default: + result = -RT_ENOSYS; + break; + } + + return result; +} + +#ifdef RT_USING_DEVICE_OPS +const static struct rt_device_ops inputcapture_ops = +{ + rt_inputcapture_init, + rt_inputcapture_open, + rt_inputcapture_close, + rt_inputcapture_read, + RT_NULL, + rt_inputcapture_control +}; +#endif + +rt_err_t rt_device_inputcapture_register(struct rt_inputcapture_device *inputcapture, const char *name, void *user_data) +{ + struct rt_device *device; + + RT_ASSERT(inputcapture != RT_NULL); + RT_ASSERT(inputcapture->ops != RT_NULL); + RT_ASSERT(inputcapture->ops->get_pulsewidth != RT_NULL); + + device = &(inputcapture->parent); + + device->type = RT_Device_Class_Miscellaneous; + device->rx_indicate = RT_NULL; + device->tx_complete = RT_NULL; + inputcapture->ringbuff = RT_NULL; + +#ifdef RT_USING_DEVICE_OPS + device->ops = &inputcapture_ops; +#else + device->init = rt_inputcapture_init; + device->open = rt_inputcapture_open; + device->close = rt_inputcapture_close; + device->read = rt_inputcapture_read; + device->write = RT_NULL; + device->control = rt_inputcapture_control; +#endif + device->user_data = user_data; + + return rt_device_register(device, name, RT_DEVICE_FLAG_RDONLY | RT_DEVICE_FLAG_STANDALONE); +} + +/** + * This function is ISR for inputcapture interrupt. + * level: RT_TRUE denotes high level pulse, and RT_FALSE denotes low level pulse. + */ +void rt_hw_inputcapture_isr(struct rt_inputcapture_device *inputcapture, rt_bool_t level) +{ + struct rt_inputcapture_data data; + rt_size_t receive_size; + if (inputcapture->ops->get_pulsewidth(inputcapture, &data.pulsewidth_us) != RT_EOK) + { + return; + } + + data.is_high = level; + if (rt_ringbuffer_put(inputcapture->ringbuff, (rt_uint8_t *)&data, sizeof(struct rt_inputcapture_data)) == 0) + { + LOG_W("inputcapture ringbuffer doesn't have enough space."); + } + + receive_size = rt_ringbuffer_data_len(inputcapture->ringbuff) / sizeof(struct rt_inputcapture_data); + + if (receive_size >= inputcapture->watermark) + { + /* indicate to upper layer application */ + if (inputcapture->parent.rx_indicate != RT_NULL) + inputcapture->parent.rx_indicate(&inputcapture->parent, receive_size); + } +} -- cgit v1.3.1 From b76d3dac8a92b36993169e5d38a6960b3210af04 Mon Sep 17 00:00:00 2001 From: tangweikang Date: Fri, 30 Aug 2019 15:38:05 +0800 Subject: [components][drivers] change name: encoder -> pulse_encoder --- bsp/stm32/libraries/HAL_Drivers/SConscript | 4 +- .../HAL_Drivers/config/f1/encoder_config.h | 64 -------- .../HAL_Drivers/config/f1/pulse_encoder_config.h | 64 ++++++++ .../HAL_Drivers/config/f4/encoder_config.h | 64 -------- .../HAL_Drivers/config/f4/pulse_encoder_config.h | 64 ++++++++ bsp/stm32/libraries/HAL_Drivers/drv_config.h | 4 +- bsp/stm32/libraries/HAL_Drivers/drv_encoder.c | 176 --------------------- .../libraries/HAL_Drivers/drv_pulse_encoder.c | 176 +++++++++++++++++++++ bsp/stm32/libraries/STM32F1xx_HAL/SConscript | 2 +- bsp/stm32/libraries/STM32F4xx_HAL/SConscript | 2 +- bsp/stm32/stm32f103-atk-warshipv3/board/Kconfig | 12 +- bsp/stm32/stm32f407-atk-explorer/board/Kconfig | 12 +- components/drivers/Kconfig | 4 +- components/drivers/include/drivers/encoder.h | 58 ------- components/drivers/include/drivers/pulse_encoder.h | 58 +++++++ components/drivers/include/rtdevice.h | 4 +- components/drivers/misc/SConscript | 4 +- components/drivers/misc/encoder.c | 136 ---------------- components/drivers/misc/pulse_encoder.c | 136 ++++++++++++++++ 19 files changed, 522 insertions(+), 522 deletions(-) delete mode 100644 bsp/stm32/libraries/HAL_Drivers/config/f1/encoder_config.h create mode 100644 bsp/stm32/libraries/HAL_Drivers/config/f1/pulse_encoder_config.h delete mode 100644 bsp/stm32/libraries/HAL_Drivers/config/f4/encoder_config.h create mode 100644 bsp/stm32/libraries/HAL_Drivers/config/f4/pulse_encoder_config.h delete mode 100644 bsp/stm32/libraries/HAL_Drivers/drv_encoder.c create mode 100644 bsp/stm32/libraries/HAL_Drivers/drv_pulse_encoder.c delete mode 100644 components/drivers/include/drivers/encoder.h create mode 100644 components/drivers/include/drivers/pulse_encoder.h delete mode 100644 components/drivers/misc/encoder.c create mode 100644 components/drivers/misc/pulse_encoder.c (limited to 'components/drivers/include') diff --git a/bsp/stm32/libraries/HAL_Drivers/SConscript b/bsp/stm32/libraries/HAL_Drivers/SConscript index cb675a16d3..1d37075fcd 100644 --- a/bsp/stm32/libraries/HAL_Drivers/SConscript +++ b/bsp/stm32/libraries/HAL_Drivers/SConscript @@ -85,8 +85,8 @@ if GetDepend(['BSP_USING_SDIO']): if GetDepend(['BSP_USING_USBD_FS']): src += ['drv_usbd_fs.c'] -if GetDepend(['BSP_USING_ENCODER']): - src += ['drv_encoder.c'] +if GetDepend(['BSP_USING_PULSE_ENCODER']): + src += ['drv_pulse_encoder.c'] src += ['drv_common.c'] diff --git a/bsp/stm32/libraries/HAL_Drivers/config/f1/encoder_config.h b/bsp/stm32/libraries/HAL_Drivers/config/f1/encoder_config.h deleted file mode 100644 index 80d515955f..0000000000 --- a/bsp/stm32/libraries/HAL_Drivers/config/f1/encoder_config.h +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2019-08-23 balanceTWK first version - */ - -#ifndef __ENCODER_CONFIG_H__ -#define __ENCODER_CONFIG_H__ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef BSP_USING_ENCODER1 -#ifndef ENCODER1_CONFIG -#define ENCODER1_CONFIG \ - { \ - .tim_handler.Instance = TIM1, \ - .name = "encoder1" \ - } -#endif /* ENCODER1_CONFIG */ -#endif /* BSP_USING_ENCODER1 */ - -#ifdef BSP_USING_ENCODER2 -#ifndef ENCODER2_CONFIG -#define ENCODER2_CONFIG \ - { \ - .tim_handler.Instance = TIM2, \ - .name = "encoder2" \ - } -#endif /* ENCODER2_CONFIG */ -#endif /* BSP_USING_ENCODER2 */ - -#ifdef BSP_USING_ENCODER3 -#ifndef ENCODER3_CONFIG -#define ENCODER3_CONFIG \ - { \ - .tim_handler.Instance = TIM3, \ - .name = "encoder3" \ - } -#endif /* ENCODER3_CONFIG */ -#endif /* BSP_USING_ENCODER3 */ - -#ifdef BSP_USING_ENCODER4 -#ifndef ENCODER4_CONFIG -#define ENCODER4_CONFIG \ - { \ - .tim_handler.Instance = TIM4, \ - .name = "encoder4" \ - } -#endif /* ENCODER4_CONFIG */ -#endif /* BSP_USING_ENCODER4 */ - -#ifdef __cplusplus -} -#endif - -#endif /* __ENCODER_CONFIG_H__ */ diff --git a/bsp/stm32/libraries/HAL_Drivers/config/f1/pulse_encoder_config.h b/bsp/stm32/libraries/HAL_Drivers/config/f1/pulse_encoder_config.h new file mode 100644 index 0000000000..4621240c98 --- /dev/null +++ b/bsp/stm32/libraries/HAL_Drivers/config/f1/pulse_encoder_config.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-08-23 balanceTWK first version + */ + +#ifndef __PULSE_ENCODER_CONFIG_H__ +#define __PULSE_ENCODER_CONFIG_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef BSP_USING_PULSE_ENCODER1 +#ifndef PULSE_ENCODER1_CONFIG +#define PULSE_ENCODER1_CONFIG \ + { \ + .tim_handler.Instance = TIM1, \ + .name = "pulse1" \ + } +#endif /* PULSE_ENCODER1_CONFIG */ +#endif /* BSP_USING_PULSE_ENCODER1 */ + +#ifdef BSP_USING_PULSE_ENCODER2 +#ifndef PULSE_ENCODER2_CONFIG +#define PULSE_ENCODER2_CONFIG \ + { \ + .tim_handler.Instance = TIM2, \ + .name = "pulse2" \ + } +#endif /* PULSE_ENCODER2_CONFIG */ +#endif /* BSP_USING_PULSE_ENCODER2 */ + +#ifdef BSP_USING_PULSE_ENCODER3 +#ifndef PULSE_ENCODER3_CONFIG +#define PULSE_ENCODER3_CONFIG \ + { \ + .tim_handler.Instance = TIM3, \ + .name = "pulse3" \ + } +#endif /* PULSE_ENCODER3_CONFIG */ +#endif /* BSP_USING_PULSE_ENCODER3 */ + +#ifdef BSP_USING_PULSE_ENCODER4 +#ifndef PULSE_ENCODER4_CONFIG +#define PULSE_ENCODER4_CONFIG \ + { \ + .tim_handler.Instance = TIM4, \ + .name = "pulse4" \ + } +#endif /* PULSE_ENCODER4_CONFIG */ +#endif /* BSP_USING_PULSE_ENCODER4 */ + +#ifdef __cplusplus +} +#endif + +#endif /* __PULSE_ENCODER_CONFIG_H__ */ diff --git a/bsp/stm32/libraries/HAL_Drivers/config/f4/encoder_config.h b/bsp/stm32/libraries/HAL_Drivers/config/f4/encoder_config.h deleted file mode 100644 index 80d515955f..0000000000 --- a/bsp/stm32/libraries/HAL_Drivers/config/f4/encoder_config.h +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2019-08-23 balanceTWK first version - */ - -#ifndef __ENCODER_CONFIG_H__ -#define __ENCODER_CONFIG_H__ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef BSP_USING_ENCODER1 -#ifndef ENCODER1_CONFIG -#define ENCODER1_CONFIG \ - { \ - .tim_handler.Instance = TIM1, \ - .name = "encoder1" \ - } -#endif /* ENCODER1_CONFIG */ -#endif /* BSP_USING_ENCODER1 */ - -#ifdef BSP_USING_ENCODER2 -#ifndef ENCODER2_CONFIG -#define ENCODER2_CONFIG \ - { \ - .tim_handler.Instance = TIM2, \ - .name = "encoder2" \ - } -#endif /* ENCODER2_CONFIG */ -#endif /* BSP_USING_ENCODER2 */ - -#ifdef BSP_USING_ENCODER3 -#ifndef ENCODER3_CONFIG -#define ENCODER3_CONFIG \ - { \ - .tim_handler.Instance = TIM3, \ - .name = "encoder3" \ - } -#endif /* ENCODER3_CONFIG */ -#endif /* BSP_USING_ENCODER3 */ - -#ifdef BSP_USING_ENCODER4 -#ifndef ENCODER4_CONFIG -#define ENCODER4_CONFIG \ - { \ - .tim_handler.Instance = TIM4, \ - .name = "encoder4" \ - } -#endif /* ENCODER4_CONFIG */ -#endif /* BSP_USING_ENCODER4 */ - -#ifdef __cplusplus -} -#endif - -#endif /* __ENCODER_CONFIG_H__ */ diff --git a/bsp/stm32/libraries/HAL_Drivers/config/f4/pulse_encoder_config.h b/bsp/stm32/libraries/HAL_Drivers/config/f4/pulse_encoder_config.h new file mode 100644 index 0000000000..4621240c98 --- /dev/null +++ b/bsp/stm32/libraries/HAL_Drivers/config/f4/pulse_encoder_config.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-08-23 balanceTWK first version + */ + +#ifndef __PULSE_ENCODER_CONFIG_H__ +#define __PULSE_ENCODER_CONFIG_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef BSP_USING_PULSE_ENCODER1 +#ifndef PULSE_ENCODER1_CONFIG +#define PULSE_ENCODER1_CONFIG \ + { \ + .tim_handler.Instance = TIM1, \ + .name = "pulse1" \ + } +#endif /* PULSE_ENCODER1_CONFIG */ +#endif /* BSP_USING_PULSE_ENCODER1 */ + +#ifdef BSP_USING_PULSE_ENCODER2 +#ifndef PULSE_ENCODER2_CONFIG +#define PULSE_ENCODER2_CONFIG \ + { \ + .tim_handler.Instance = TIM2, \ + .name = "pulse2" \ + } +#endif /* PULSE_ENCODER2_CONFIG */ +#endif /* BSP_USING_PULSE_ENCODER2 */ + +#ifdef BSP_USING_PULSE_ENCODER3 +#ifndef PULSE_ENCODER3_CONFIG +#define PULSE_ENCODER3_CONFIG \ + { \ + .tim_handler.Instance = TIM3, \ + .name = "pulse3" \ + } +#endif /* PULSE_ENCODER3_CONFIG */ +#endif /* BSP_USING_PULSE_ENCODER3 */ + +#ifdef BSP_USING_PULSE_ENCODER4 +#ifndef PULSE_ENCODER4_CONFIG +#define PULSE_ENCODER4_CONFIG \ + { \ + .tim_handler.Instance = TIM4, \ + .name = "pulse4" \ + } +#endif /* PULSE_ENCODER4_CONFIG */ +#endif /* BSP_USING_PULSE_ENCODER4 */ + +#ifdef __cplusplus +} +#endif + +#endif /* __PULSE_ENCODER_CONFIG_H__ */ diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_config.h b/bsp/stm32/libraries/HAL_Drivers/drv_config.h index 9f5194d960..fa4555fe50 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_config.h +++ b/bsp/stm32/libraries/HAL_Drivers/drv_config.h @@ -34,7 +34,7 @@ extern "C" { #include "f1/sdio_config.h" #include "f1/pwm_config.h" #include "f1/usbd_fs_config.h" -#include "f1/encoder_config.h" +#include "f1/pulse_encoder_config.h" #elif defined(SOC_SERIES_STM32F2) #include "f2/dma_config.h" #include "f2/uart_config.h" @@ -53,7 +53,7 @@ extern "C" { #include "f4/tim_config.h" #include "f4/sdio_config.h" #include "f4/pwm_config.h" -#include "f4/encoder_config.h" +#include "f4/pulse_encoder_config.h" #elif defined(SOC_SERIES_STM32F7) #include "f7/dma_config.h" #include "f7/uart_config.h" diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_encoder.c b/bsp/stm32/libraries/HAL_Drivers/drv_encoder.c deleted file mode 100644 index ca42c151fa..0000000000 --- a/bsp/stm32/libraries/HAL_Drivers/drv_encoder.c +++ /dev/null @@ -1,176 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2019-08-23 balanceTWK first version - */ - -#include "board.h" -#include "drv_config.h" -#ifdef RT_USING_ENCODER - -//#define DRV_DEBUG -#define LOG_TAG "drv.encoder" -#include - -#if !defined(BSP_USING_ENCODER1) && !defined(BSP_USING_ENCODER2) && !defined(BSP_USING_ENCODER3) \ - && !defined(BSP_USING_ENCODER4) && !defined(BSP_USING_ENCODER5) && !defined(BSP_USING_ENCODER6) && !defined(BSP_USING_LPENCODER1) - #error "Please define at least one BSP_USING_ENCODERx" - /* this driver can be disabled at menuconfig → RT-Thread Components → Device Drivers */ -#endif - -enum -{ -#ifdef BSP_USING_ENCODER1 - ENCODER1_INDEX, -#endif -#ifdef BSP_USING_ENCODER2 - ENCODER2_INDEX, -#endif -#ifdef BSP_USING_ENCODER3 - ENCODER3_INDEX, -#endif -#ifdef BSP_USING_ENCODER4 - ENCODER4_INDEX, -#endif -#ifdef BSP_USING_ENCODER5 - ENCODER5_INDEX, -#endif -#ifdef BSP_USING_ENCODER6 - ENCODER6_INDEX, -#endif -}; - -struct stm32_encoder_device -{ - struct rt_encoder_device encoder; - TIM_HandleTypeDef tim_handler; - char *name; -}; - -static struct stm32_encoder_device stm32_encoder_obj[] = -{ -#ifdef BSP_USING_ENCODER1 - ENCODER1_CONFIG, -#endif -#ifdef BSP_USING_ENCODER2 - ENCODER2_CONFIG, -#endif -#ifdef BSP_USING_ENCODER3 - ENCODER3_CONFIG, -#endif -#ifdef BSP_USING_ENCODER4 - ENCODER4_CONFIG, -#endif -#ifdef BSP_USING_ENCODER5 - ENCODER5_CONFIG, -#endif -#ifdef BSP_USING_ENCODER6 - ENCODER6_CONFIG, -#endif -}; - -rt_err_t encoder_init(struct rt_encoder_device *encoder) -{ - TIM_Encoder_InitTypeDef sConfig; - TIM_MasterConfigTypeDef sMasterConfig; - TIM_HandleTypeDef *tim_handler = (TIM_HandleTypeDef *)encoder->parent.user_data; - - tim_handler->Init.Prescaler = 0; - tim_handler->Init.CounterMode = TIM_COUNTERMODE_UP; - tim_handler->Init.Period = 0xffff; - tim_handler->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; - - sConfig.EncoderMode = TIM_ENCODERMODE_TI12; - sConfig.IC1Polarity = TIM_ICPOLARITY_RISING; - sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI; - sConfig.IC1Prescaler = TIM_ICPSC_DIV1; - sConfig.IC1Filter = 3; - sConfig.IC2Polarity = TIM_ICPOLARITY_RISING; - sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI; - sConfig.IC2Prescaler = TIM_ICPSC_DIV1; - sConfig.IC2Filter = 3; - - if (HAL_TIM_Encoder_Init(tim_handler, &sConfig) != HAL_OK) - { - LOG_E("encoder init failed"); - return -RT_ERROR; - } - - sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; - sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; - - if (HAL_TIMEx_MasterConfigSynchronization(tim_handler, &sMasterConfig)) - { - LOG_E("TIMx master config failed"); - return -RT_ERROR; - } - - return RT_EOK; -} - -rt_int32_t encoder_get_count(struct rt_encoder_device *encoder) -{ - TIM_HandleTypeDef *tim_handler = (TIM_HandleTypeDef *)encoder->parent.user_data; - return (rt_int16_t)__HAL_TIM_GET_COUNTER(tim_handler); -} - -rt_err_t encoder_control(struct rt_encoder_device *encoder, rt_uint32_t cmd, void *args) -{ - rt_err_t result; - TIM_HandleTypeDef *tim_handler = (TIM_HandleTypeDef *)encoder->parent.user_data; - - result = RT_EOK; - - switch (cmd) - { - case ENCODER_CMD_ENABLE: - HAL_TIM_Encoder_Start(tim_handler, TIM_CHANNEL_ALL); - break; - case ENCODER_CMD_DISABLE: - HAL_TIM_Encoder_Stop(tim_handler, TIM_CHANNEL_ALL); - break; - case ENCODER_CMD_CLEAR_COUNT: - __HAL_TIM_SET_COUNTER(tim_handler, 0); - break; - default: - result = -RT_ENOSYS; - break; - } - - return result; -} - -static const struct rt_encoder_ops _ops = -{ - .init = encoder_init, - .get_count = encoder_get_count, - .control = encoder_control, -}; - -int hw_encoder_init(void) -{ - int i; - int result; - - result = RT_EOK; - for (i = 0; i < sizeof(stm32_encoder_obj) / sizeof(stm32_encoder_obj[0]); i++) - { - stm32_encoder_obj[i].encoder.type = AB_PHASE_ENCODER; - stm32_encoder_obj[i].encoder.ops = &_ops; - - if (rt_device_encoder_register(&stm32_encoder_obj[i].encoder, stm32_encoder_obj[i].name, &stm32_encoder_obj[i].tim_handler) != RT_EOK) - { - LOG_E("%s register failed", stm32_encoder_obj[i].name); - result = -RT_ERROR; - } - } - - return result; -} -INIT_BOARD_EXPORT(hw_encoder_init); - -#endif diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_pulse_encoder.c b/bsp/stm32/libraries/HAL_Drivers/drv_pulse_encoder.c new file mode 100644 index 0000000000..432b8b2a05 --- /dev/null +++ b/bsp/stm32/libraries/HAL_Drivers/drv_pulse_encoder.c @@ -0,0 +1,176 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-08-23 balanceTWK first version + */ + +#include "board.h" +#include "drv_config.h" +#ifdef RT_USING_PULSE_ENCODER + +//#define DRV_DEBUG +#define LOG_TAG "drv.pulse_encoder" +#include + +#if !defined(BSP_USING_PULSE_ENCODER1) && !defined(BSP_USING_PULSE_ENCODER2) && !defined(BSP_USING_PULSE_ENCODER3) \ + && !defined(BSP_USING_PULSE_ENCODER4) && !defined(BSP_USING_PULSE_ENCODER5) && !defined(BSP_USING_PULSE_ENCODER6) + #error "Please define at least one BSP_USING_PULSE_ENCODERx" + /* this driver can be disabled at menuconfig → RT-Thread Components → Device Drivers */ +#endif + +enum +{ +#ifdef BSP_USING_PULSE_ENCODER1 + PULSE_ENCODER1_INDEX, +#endif +#ifdef BSP_USING_PULSE_ENCODER2 + PULSE_ENCODER2_INDEX, +#endif +#ifdef BSP_USING_PULSE_ENCODER3 + PULSE_ENCODER3_INDEX, +#endif +#ifdef BSP_USING_PULSE_ENCODER4 + PULSE_ENCODER4_INDEX, +#endif +#ifdef BSP_USING_PULSE_ENCODER5 + PULSE_ENCODER5_INDEX, +#endif +#ifdef BSP_USING_PULSE_ENCODER6 + PULSE_ENCODER6_INDEX, +#endif +}; + +struct stm32_pulse_encoder_device +{ + struct rt_pulse_encoder_device pulse_encoder; + TIM_HandleTypeDef tim_handler; + char *name; +}; + +static struct stm32_pulse_encoder_device stm32_pulse_encoder_obj[] = +{ +#ifdef BSP_USING_PULSE_ENCODER1 + PULSE_ENCODER1_CONFIG, +#endif +#ifdef BSP_USING_PULSE_ENCODER2 + PULSE_ENCODER2_CONFIG, +#endif +#ifdef BSP_USING_PULSE_ENCODER3 + PULSE_ENCODER3_CONFIG, +#endif +#ifdef BSP_USING_PULSE_ENCODER4 + PULSE_ENCODER4_CONFIG, +#endif +#ifdef BSP_USING_PULSE_ENCODER5 + PULSE_ENCODER5_CONFIG, +#endif +#ifdef BSP_USING_PULSE_ENCODER6 + PULSE_ENCODER6_CONFIG, +#endif +}; + +rt_err_t pulse_encoder_init(struct rt_pulse_encoder_device *pulse_encoder) +{ + TIM_Encoder_InitTypeDef sConfig; + TIM_MasterConfigTypeDef sMasterConfig; + TIM_HandleTypeDef *tim_handler = (TIM_HandleTypeDef *)pulse_encoder->parent.user_data; + + tim_handler->Init.Prescaler = 0; + tim_handler->Init.CounterMode = TIM_COUNTERMODE_UP; + tim_handler->Init.Period = 0xffff; + tim_handler->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + + sConfig.EncoderMode = TIM_ENCODERMODE_TI12; + sConfig.IC1Polarity = TIM_ICPOLARITY_RISING; + sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI; + sConfig.IC1Prescaler = TIM_ICPSC_DIV1; + sConfig.IC1Filter = 3; + sConfig.IC2Polarity = TIM_ICPOLARITY_RISING; + sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI; + sConfig.IC2Prescaler = TIM_ICPSC_DIV1; + sConfig.IC2Filter = 3; + + if (HAL_TIM_Encoder_Init(tim_handler, &sConfig) != HAL_OK) + { + LOG_E("pulse_encoder init failed"); + return -RT_ERROR; + } + + sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; + sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; + + if (HAL_TIMEx_MasterConfigSynchronization(tim_handler, &sMasterConfig)) + { + LOG_E("TIMx master config failed"); + return -RT_ERROR; + } + + return RT_EOK; +} + +rt_int32_t pulse_encoder_get_count(struct rt_pulse_encoder_device *pulse_encoder) +{ + TIM_HandleTypeDef *tim_handler = (TIM_HandleTypeDef *)pulse_encoder->parent.user_data; + return (rt_int16_t)__HAL_TIM_GET_COUNTER(tim_handler); +} + +rt_err_t pulse_encoder_control(struct rt_pulse_encoder_device *pulse_encoder, rt_uint32_t cmd, void *args) +{ + rt_err_t result; + TIM_HandleTypeDef *tim_handler = (TIM_HandleTypeDef *)pulse_encoder->parent.user_data; + + result = RT_EOK; + + switch (cmd) + { + case PULSE_ENCODER_CMD_ENABLE: + HAL_TIM_Encoder_Start(tim_handler, TIM_CHANNEL_ALL); + break; + case PULSE_ENCODER_CMD_DISABLE: + HAL_TIM_Encoder_Stop(tim_handler, TIM_CHANNEL_ALL); + break; + case PULSE_ENCODER_CMD_CLEAR_COUNT: + __HAL_TIM_SET_COUNTER(tim_handler, 0); + break; + default: + result = -RT_ENOSYS; + break; + } + + return result; +} + +static const struct rt_pulse_encoder_ops _ops = +{ + .init = pulse_encoder_init, + .get_count = pulse_encoder_get_count, + .control = pulse_encoder_control, +}; + +int hw_pulse_encoder_init(void) +{ + int i; + int result; + + result = RT_EOK; + for (i = 0; i < sizeof(stm32_pulse_encoder_obj) / sizeof(stm32_pulse_encoder_obj[0]); i++) + { + stm32_pulse_encoder_obj[i].pulse_encoder.type = AB_PHASE_PULSE_ENCODER; + stm32_pulse_encoder_obj[i].pulse_encoder.ops = &_ops; + + if (rt_device_pulse_encoder_register(&stm32_pulse_encoder_obj[i].pulse_encoder, stm32_pulse_encoder_obj[i].name, &stm32_pulse_encoder_obj[i].tim_handler) != RT_EOK) + { + LOG_E("%s register failed", stm32_pulse_encoder_obj[i].name); + result = -RT_ERROR; + } + } + + return result; +} +INIT_BOARD_EXPORT(hw_pulse_encoder_init); + +#endif diff --git a/bsp/stm32/libraries/STM32F1xx_HAL/SConscript b/bsp/stm32/libraries/STM32F1xx_HAL/SConscript index 8d554cac85..f629479376 100644 --- a/bsp/stm32/libraries/STM32F1xx_HAL/SConscript +++ b/bsp/stm32/libraries/STM32F1xx_HAL/SConscript @@ -44,7 +44,7 @@ if GetDepend(['RT_USING_USB_HOST']) or GetDepend(['RT_USING_USB_DEVICE']): if GetDepend(['RT_USING_CAN']): src += ['STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_can.c'] -if GetDepend(['RT_USING_HWTIMER']) or GetDepend(['RT_USING_PWM']) or GetDepend(['RT_USING_ENCODER']): +if GetDepend(['RT_USING_HWTIMER']) or GetDepend(['RT_USING_PWM']) or GetDepend(['RT_USING_PULSE_ENCODER']): src += ['STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c'] src += ['STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c'] diff --git a/bsp/stm32/libraries/STM32F4xx_HAL/SConscript b/bsp/stm32/libraries/STM32F4xx_HAL/SConscript index 7bf1638169..87c000ae18 100644 --- a/bsp/stm32/libraries/STM32F4xx_HAL/SConscript +++ b/bsp/stm32/libraries/STM32F4xx_HAL/SConscript @@ -46,7 +46,7 @@ if GetDepend(['RT_USING_USB_HOST']) or GetDepend(['RT_USING_USB_DEVICE']): if GetDepend(['RT_USING_CAN']): src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_can.c'] -if GetDepend(['RT_USING_HWTIMER']) or GetDepend(['RT_USING_PWM']) or GetDepend(['RT_USING_ENCODER']): +if GetDepend(['RT_USING_HWTIMER']) or GetDepend(['RT_USING_PWM']) or GetDepend(['RT_USING_PULSE_ENCODER']): src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c'] src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c'] src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_lptim.c'] diff --git a/bsp/stm32/stm32f103-atk-warshipv3/board/Kconfig b/bsp/stm32/stm32f103-atk-warshipv3/board/Kconfig index 8f34aca198..e02250d243 100644 --- a/bsp/stm32/stm32f103-atk-warshipv3/board/Kconfig +++ b/bsp/stm32/stm32f103-atk-warshipv3/board/Kconfig @@ -201,13 +201,13 @@ menu "On-chip Peripheral Drivers" select RT_USING_WDT default n - menuconfig BSP_USING_ENCODER - bool "Enable Encoder" + menuconfig BSP_USING_PULSE_ENCODER + bool "Enable Pulse Encoder" default n - select RT_USING_ENCODER - if BSP_USING_ENCODER - config BSP_USING_ENCODER3 - bool "Enable Encoder3" + select RT_USING_PULSE_ENCODER + if BSP_USING_PULSE_ENCODER + config BSP_USING_PULSE_ENCODER3 + bool "Enable Pulse Encoder3" default n endif diff --git a/bsp/stm32/stm32f407-atk-explorer/board/Kconfig b/bsp/stm32/stm32f407-atk-explorer/board/Kconfig index 1f0de499ed..2a235f0550 100644 --- a/bsp/stm32/stm32f407-atk-explorer/board/Kconfig +++ b/bsp/stm32/stm32f407-atk-explorer/board/Kconfig @@ -294,13 +294,13 @@ menu "On-chip Peripheral Drivers" select RT_USING_DFS default n - menuconfig BSP_USING_ENCODER - bool "Enable Encoder" + menuconfig BSP_USING_PULSE_ENCODER + bool "Enable Pulse Encoder" default n - select RT_USING_ENCODER - if BSP_USING_ENCODER - config BSP_USING_ENCODER4 - bool "Enable Encoder4" + select RT_USING_PULSE_ENCODER + if BSP_USING_PULSE_ENCODER + config BSP_USING_PULSE_ENCODER4 + bool "Enable Pulse Encoder4" default n endif diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index ff30fa6162..ea928f452d 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -434,8 +434,8 @@ menuconfig RT_USING_HWCRYPTO endif endif -config RT_USING_ENCODER - bool "Using ENCODER device drivers" +config RT_USING_PULSE_ENCODER + bool "Using PULSE ENCODER device drivers" default n config RT_USING_INPUT_CAPTURE diff --git a/components/drivers/include/drivers/encoder.h b/components/drivers/include/drivers/encoder.h deleted file mode 100644 index f338f9e845..0000000000 --- a/components/drivers/include/drivers/encoder.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2019-08-08 balanceTWK the first version - */ - -#ifndef __ENCODER_H__ -#define __ENCODER_H__ - -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/* encoder control command */ -#define ENCODER_CMD_GET_TYPE (128 + 0) /* get a encoder type information */ -#define ENCODER_CMD_ENABLE (128 + 1) /* enable encoder */ -#define ENCODER_CMD_DISABLE (128 + 2) /* disable encoder */ -#define ENCODER_CMD_CLEAR_COUNT (128 + 3) /* clear encoder count */ - -/* encoder type */ -enum rt_encoder_type -{ - UNKNOWN_ENCODER_TYPE = 0x00, /* Unknown encoder type */ - SINGLE_PHASE_ENCODER, /* single phase encoder */ - AB_PHASE_ENCODER /* two phase encoder */ -}; - -struct rt_encoder_device; - -struct rt_encoder_ops -{ - rt_err_t (*init)(struct rt_encoder_device *encoder); - rt_int32_t (*get_count)(struct rt_encoder_device *encoder); - rt_err_t (*clear_count)(struct rt_encoder_device *encoder); - rt_err_t (*control)(struct rt_encoder_device *encoder, rt_uint32_t cmd, void *args); -}; - -struct rt_encoder_device -{ - struct rt_device parent; - const struct rt_encoder_ops *ops; - enum rt_encoder_type type; -}; - -rt_err_t rt_device_encoder_register(struct rt_encoder_device *encoder, const char *name, void *user_data); - -#ifdef __cplusplus -} -#endif - -#endif /* __ENCODER_H__ */ diff --git a/components/drivers/include/drivers/pulse_encoder.h b/components/drivers/include/drivers/pulse_encoder.h new file mode 100644 index 0000000000..0c7df04cd6 --- /dev/null +++ b/components/drivers/include/drivers/pulse_encoder.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-08-08 balanceTWK the first version + */ + +#ifndef __PULSE_ENCODER_H__ +#define __PULSE_ENCODER_H__ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* pulse_encoder control command */ +#define PULSE_ENCODER_CMD_GET_TYPE (128 + 0) /* get a pulse_encoder type information */ +#define PULSE_ENCODER_CMD_ENABLE (128 + 1) /* enable pulse_encoder */ +#define PULSE_ENCODER_CMD_DISABLE (128 + 2) /* disable pulse_encoder */ +#define PULSE_ENCODER_CMD_CLEAR_COUNT (128 + 3) /* clear pulse_encoder count */ + +/* pulse_encoder type */ +enum rt_pulse_encoder_type +{ + UNKNOWN_PULSE_ENCODER_TYPE = 0x00, /* Unknown pulse_encoder type */ + SINGLE_PHASE_PULSE_ENCODER, /* single phase pulse_encoder */ + AB_PHASE_PULSE_ENCODER /* two phase pulse_encoder */ +}; + +struct rt_pulse_encoder_device; + +struct rt_pulse_encoder_ops +{ + rt_err_t (*init)(struct rt_pulse_encoder_device *pulse_encoder); + rt_int32_t (*get_count)(struct rt_pulse_encoder_device *pulse_encoder); + rt_err_t (*clear_count)(struct rt_pulse_encoder_device *pulse_encoder); + rt_err_t (*control)(struct rt_pulse_encoder_device *pulse_encoder, rt_uint32_t cmd, void *args); +}; + +struct rt_pulse_encoder_device +{ + struct rt_device parent; + const struct rt_pulse_encoder_ops *ops; + enum rt_pulse_encoder_type type; +}; + +rt_err_t rt_device_pulse_encoder_register(struct rt_pulse_encoder_device *pulse_encoder, const char *name, void *user_data); + +#ifdef __cplusplus +} +#endif + +#endif /* __PULSE_ENCODER_H__ */ diff --git a/components/drivers/include/rtdevice.h b/components/drivers/include/rtdevice.h index bb302ac2af..623c085430 100644 --- a/components/drivers/include/rtdevice.h +++ b/components/drivers/include/rtdevice.h @@ -126,8 +126,8 @@ extern "C" { #include "drivers/crypto.h" #endif -#ifdef RT_USING_ENCODER -#include "drivers/encoder.h" +#ifdef RT_USING_PULSE_ENCODER +#include "drivers/pulse_encoder.h" #endif #ifdef RT_USING_INPUT_CAPTURE diff --git a/components/drivers/misc/SConscript b/components/drivers/misc/SConscript index f0d177f761..8a9fdb5eb9 100644 --- a/components/drivers/misc/SConscript +++ b/components/drivers/misc/SConscript @@ -14,8 +14,8 @@ if GetDepend(['RT_USING_ADC']): if GetDepend(['RT_USING_PWM']): src = src + ['rt_drv_pwm.c'] -if GetDepend(['RT_USING_ENCODER']): - src = src + ['encoder.c'] +if GetDepend(['RT_USING_PULSE_ENCODER']): + src = src + ['pulse_encoder.c'] if GetDepend(['RT_USING_INPUT_CAPTURE']): src = src + ['rt_inputcapture.c'] diff --git a/components/drivers/misc/encoder.c b/components/drivers/misc/encoder.c deleted file mode 100644 index e9e1da0f40..0000000000 --- a/components/drivers/misc/encoder.c +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2019-08-08 balanceTWK the first version - */ - -#include -#include - -static rt_err_t rt_encoder_init(struct rt_device *dev) -{ - struct rt_encoder_device *encoder; - - encoder = (struct rt_encoder_device *)dev; - if (encoder->ops->init) - { - return encoder->ops->init(encoder); - } - else - { - return -RT_ENOSYS; - } -} - -static rt_err_t rt_encoder_open(struct rt_device *dev, rt_uint16_t oflag) -{ - struct rt_encoder_device *encoder; - - encoder = (struct rt_encoder_device *)dev; - if (encoder->ops->control) - { - return encoder->ops->control(encoder, ENCODER_CMD_ENABLE, RT_NULL); - } - else - { - return -RT_ENOSYS; - } -} - -static rt_err_t rt_encoder_close(struct rt_device *dev) -{ - struct rt_encoder_device *encoder; - - encoder = (struct rt_encoder_device *)dev; - if (encoder->ops->control) - { - return encoder->ops->control(encoder, ENCODER_CMD_DISABLE, RT_NULL); - } - else - { - return -RT_ENOSYS; - } -} - -static rt_size_t rt_encoder_read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size) -{ - struct rt_encoder_device *encoder; - - encoder = (struct rt_encoder_device *)dev; - if (encoder->ops->get_count) - { - *(rt_int32_t *)buffer = encoder->ops->get_count(encoder); - } - return 1; -} - -static rt_err_t rt_encoder_control(struct rt_device *dev, int cmd, void *args) -{ - rt_err_t result; - struct rt_encoder_device *encoder; - - result = RT_EOK; - encoder = (struct rt_encoder_device *)dev; - switch (cmd) - { - case ENCODER_CMD_CLEAR_COUNT: - result = encoder->ops->clear_count(encoder); - break; - case ENCODER_CMD_GET_TYPE: - *(enum rt_encoder_type *)args = encoder->type; - break; - case ENCODER_CMD_ENABLE: - case ENCODER_CMD_DISABLE: - result = encoder->ops->control(encoder, cmd, args); - break; - default: - result = -RT_ENOSYS; - break; - } - - return result; -} - -#ifdef RT_USING_DEVICE_OPS -const static struct rt_device_ops encoder_ops = -{ - rt_encoder_init, - rt_encoder_open, - rt_encoder_close, - rt_encoder_read, - RT_NULL, - rt_encoder_control -}; -#endif - -rt_err_t rt_device_encoder_register(struct rt_encoder_device *encoder, const char *name, void *user_data) -{ - struct rt_device *device; - - RT_ASSERT(encoder != RT_NULL); - RT_ASSERT(encoder->ops != RT_NULL); - - device = &(encoder->parent); - - device->type = RT_Device_Class_Miscellaneous; - device->rx_indicate = RT_NULL; - device->tx_complete = RT_NULL; - -#ifdef RT_USING_DEVICE_OPS - device->ops = &encoder_ops; -#else - device->init = rt_encoder_init; - device->open = rt_encoder_open; - device->close = rt_encoder_close; - device->read = rt_encoder_read; - device->write = RT_NULL; - device->control = rt_encoder_control; -#endif - device->user_data = user_data; - - return rt_device_register(device, name, RT_DEVICE_FLAG_RDONLY | RT_DEVICE_FLAG_STANDALONE); -} diff --git a/components/drivers/misc/pulse_encoder.c b/components/drivers/misc/pulse_encoder.c new file mode 100644 index 0000000000..3145c24675 --- /dev/null +++ b/components/drivers/misc/pulse_encoder.c @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-08-08 balanceTWK the first version + */ + +#include +#include + +static rt_err_t rt_pulse_encoder_init(struct rt_device *dev) +{ + struct rt_pulse_encoder_device *pulse_encoder; + + pulse_encoder = (struct rt_pulse_encoder_device *)dev; + if (pulse_encoder->ops->init) + { + return pulse_encoder->ops->init(pulse_encoder); + } + else + { + return -RT_ENOSYS; + } +} + +static rt_err_t rt_pulse_encoder_open(struct rt_device *dev, rt_uint16_t oflag) +{ + struct rt_pulse_encoder_device *pulse_encoder; + + pulse_encoder = (struct rt_pulse_encoder_device *)dev; + if (pulse_encoder->ops->control) + { + return pulse_encoder->ops->control(pulse_encoder, PULSE_ENCODER_CMD_ENABLE, RT_NULL); + } + else + { + return -RT_ENOSYS; + } +} + +static rt_err_t rt_pulse_encoder_close(struct rt_device *dev) +{ + struct rt_pulse_encoder_device *pulse_encoder; + + pulse_encoder = (struct rt_pulse_encoder_device *)dev; + if (pulse_encoder->ops->control) + { + return pulse_encoder->ops->control(pulse_encoder, PULSE_ENCODER_CMD_DISABLE, RT_NULL); + } + else + { + return -RT_ENOSYS; + } +} + +static rt_size_t rt_pulse_encoder_read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size) +{ + struct rt_pulse_encoder_device *pulse_encoder; + + pulse_encoder = (struct rt_pulse_encoder_device *)dev; + if (pulse_encoder->ops->get_count) + { + *(rt_int32_t *)buffer = pulse_encoder->ops->get_count(pulse_encoder); + } + return 1; +} + +static rt_err_t rt_pulse_encoder_control(struct rt_device *dev, int cmd, void *args) +{ + rt_err_t result; + struct rt_pulse_encoder_device *pulse_encoder; + + result = RT_EOK; + pulse_encoder = (struct rt_pulse_encoder_device *)dev; + switch (cmd) + { + case PULSE_ENCODER_CMD_CLEAR_COUNT: + result = pulse_encoder->ops->clear_count(pulse_encoder); + break; + case PULSE_ENCODER_CMD_GET_TYPE: + *(enum rt_pulse_encoder_type *)args = pulse_encoder->type; + break; + case PULSE_ENCODER_CMD_ENABLE: + case PULSE_ENCODER_CMD_DISABLE: + result = pulse_encoder->ops->control(pulse_encoder, cmd, args); + break; + default: + result = -RT_ENOSYS; + break; + } + + return result; +} + +#ifdef RT_USING_DEVICE_OPS +const static struct rt_device_ops pulse_encoder_ops = +{ + rt_pulse_encoder_init, + rt_pulse_encoder_open, + rt_pulse_encoder_close, + rt_pulse_encoder_read, + RT_NULL, + rt_pulse_encoder_control +}; +#endif + +rt_err_t rt_device_pulse_encoder_register(struct rt_pulse_encoder_device *pulse_encoder, const char *name, void *user_data) +{ + struct rt_device *device; + + RT_ASSERT(pulse_encoder != RT_NULL); + RT_ASSERT(pulse_encoder->ops != RT_NULL); + + device = &(pulse_encoder->parent); + + device->type = RT_Device_Class_Miscellaneous; + device->rx_indicate = RT_NULL; + device->tx_complete = RT_NULL; + +#ifdef RT_USING_DEVICE_OPS + device->ops = &pulse_encoder_ops; +#else + device->init = rt_pulse_encoder_init; + device->open = rt_pulse_encoder_open; + device->close = rt_pulse_encoder_close; + device->read = rt_pulse_encoder_read; + device->write = RT_NULL; + device->control = rt_pulse_encoder_control; +#endif + device->user_data = user_data; + + return rt_device_register(device, name, RT_DEVICE_FLAG_RDONLY | RT_DEVICE_FLAG_STANDALONE); +} -- cgit v1.3.1 From 260a61d36ab5703b573ccc912287747112f213f6 Mon Sep 17 00:00:00 2001 From: MurphyZhao Date: Mon, 23 Sep 2019 18:01:58 +0800 Subject: [components/drivers][pm] fix data type problem Signed-off-by: MurphyZhao --- components/drivers/include/drivers/pm.h | 24 +++++++++++----------- components/drivers/pm/pm.c | 36 ++++++++++++++++----------------- 2 files changed, 30 insertions(+), 30 deletions(-) (limited to 'components/drivers/include') diff --git a/components/drivers/include/drivers/pm.h b/components/drivers/include/drivers/pm.h index e35b21cd48..dd5875aa1d 100644 --- a/components/drivers/include/drivers/pm.h +++ b/components/drivers/include/drivers/pm.h @@ -86,8 +86,8 @@ struct rt_pm; */ struct rt_pm_ops { - void (*sleep)(struct rt_pm *pm, uint8_t mode); - void (*run)(struct rt_pm *pm, uint8_t mode); + void (*sleep)(struct rt_pm *pm, rt_uint8_t mode); + void (*run)(struct rt_pm *pm, rt_uint8_t mode); void (*timer_start)(struct rt_pm *pm, rt_uint32_t timeout); void (*timer_stop)(struct rt_pm *pm); rt_tick_t (*timer_get_tick)(struct rt_pm *pm); @@ -95,9 +95,9 @@ struct rt_pm_ops struct rt_device_pm_ops { - int (*suspend)(const struct rt_device *device, uint8_t mode); - void (*resume)(const struct rt_device *device, uint8_t mode); - int (*frequency_change)(const struct rt_device *device, uint8_t mode); + int (*suspend)(const struct rt_device *device, rt_uint8_t mode); + void (*resume)(const struct rt_device *device, rt_uint8_t mode); + int (*frequency_change)(const struct rt_device *device, rt_uint8_t mode); }; struct rt_device_pm @@ -137,22 +137,22 @@ enum struct rt_pm_notify { - void (*notify)(uint8_t event, uint8_t mode, void *data); + void (*notify)(rt_uint8_t event, rt_uint8_t mode, void *data); void *data; }; -void rt_pm_request(uint8_t sleep_mode); -void rt_pm_release(uint8_t sleep_mode); -int rt_pm_run_enter(uint8_t run_mode); +void rt_pm_request(rt_uint8_t sleep_mode); +void rt_pm_release(rt_uint8_t sleep_mode); +int rt_pm_run_enter(rt_uint8_t run_mode); void rt_pm_device_register(struct rt_device *device, const struct rt_device_pm_ops *ops); void rt_pm_device_unregister(struct rt_device *device); -void rt_pm_notify_set(void (*notify)(uint8_t event, uint8_t mode, void *data), void *data); -void rt_pm_default_set(uint8_t sleep_mode); +void rt_pm_notify_set(void (*notify)(rt_uint8_t event, rt_uint8_t mode, void *data), void *data); +void rt_pm_default_set(rt_uint8_t sleep_mode); void rt_system_pm_init(const struct rt_pm_ops *ops, - uint8_t timer_mask, + rt_uint8_t timer_mask, void *user_data); #endif /* __PM_H__ */ diff --git a/components/drivers/pm/pm.c b/components/drivers/pm/pm.c index 60e71ddfd9..dd7cc2ae63 100644 --- a/components/drivers/pm/pm.c +++ b/components/drivers/pm/pm.c @@ -17,18 +17,18 @@ #ifdef RT_USING_PM static struct rt_pm _pm; -static uint8_t _pm_default_sleep = RT_PM_DEFAULT_SLEEP_MODE; +static rt_uint8_t _pm_default_sleep = RT_PM_DEFAULT_SLEEP_MODE; static struct rt_pm_notify _pm_notify; -static uint8_t _pm_init_flag = 0; +static rt_uint8_t _pm_init_flag = 0; #define RT_PM_TICKLESS_THRESH (2) -RT_WEAK uint32_t rt_pm_enter_critical(uint8_t sleep_mode) +RT_WEAK rt_uint32_t rt_pm_enter_critical(rt_uint8_t sleep_mode) { return rt_hw_interrupt_disable(); } -RT_WEAK void rt_pm_exit_critical(uint32_t ctx, uint8_t sleep_mode) +RT_WEAK void rt_pm_exit_critical(rt_uint32_t ctx, rt_uint8_t sleep_mode) { rt_hw_interrupt_enable(ctx); } @@ -36,7 +36,7 @@ RT_WEAK void rt_pm_exit_critical(uint32_t ctx, uint8_t sleep_mode) /** * This function will suspend all registered devices */ -static int _pm_device_suspend(uint8_t mode) +static int _pm_device_suspend(rt_uint8_t mode) { int index, ret = RT_EOK; @@ -45,8 +45,8 @@ static int _pm_device_suspend(uint8_t mode) if (_pm.device_pm[index].ops->suspend != RT_NULL) { ret = _pm.device_pm[index].ops->suspend(_pm.device_pm[index].device, mode); - if (ret != RT_EOK) - break; + if(ret != RT_EOK) + break; } } @@ -56,7 +56,7 @@ static int _pm_device_suspend(uint8_t mode) /** * This function will resume all registered devices */ -static void _pm_device_resume(uint8_t mode) +static void _pm_device_resume(rt_uint8_t mode) { int index; @@ -72,7 +72,7 @@ static void _pm_device_resume(uint8_t mode) /** * This function will update the frequency of all registered devices */ -static void _pm_device_frequency_change(uint8_t mode) +static void _pm_device_frequency_change(rt_uint8_t mode) { rt_uint32_t index; @@ -106,10 +106,10 @@ static void _pm_frequency_scaling(struct rt_pm *pm) /** * This function selects the sleep mode according to the rt_pm_request/rt_pm_release count. */ -static uint8_t _pm_select_sleep_mode(struct rt_pm *pm) +static rt_uint8_t _pm_select_sleep_mode(struct rt_pm *pm) { int index; - uint8_t mode; + rt_uint8_t mode; mode = _pm_default_sleep; for (index = PM_SLEEP_MODE_NONE; index < PM_SLEEP_MODE_MAX; index ++) @@ -128,7 +128,7 @@ static uint8_t _pm_select_sleep_mode(struct rt_pm *pm) /** * This function changes the power sleep mode base on the result of selection */ -static void _pm_change_sleep_mode(struct rt_pm *pm, uint8_t mode) +static void _pm_change_sleep_mode(struct rt_pm *pm, rt_uint8_t mode) { rt_tick_t timeout_tick, delta_tick; rt_base_t level; @@ -214,7 +214,7 @@ static void _pm_change_sleep_mode(struct rt_pm *pm, uint8_t mode) */ void rt_system_power_manager(void) { - uint8_t mode; + rt_uint8_t mode; if (_pm_init_flag == 0) return; @@ -233,7 +233,7 @@ void rt_system_power_manager(void) * * @param parameter the parameter of run mode or sleep mode */ -void rt_pm_request(uint8_t mode) +void rt_pm_request(rt_uint8_t mode) { rt_base_t level; struct rt_pm *pm; @@ -258,7 +258,7 @@ void rt_pm_request(uint8_t mode) * @param parameter the parameter of run mode or sleep mode * */ -void rt_pm_release(uint8_t mode) +void rt_pm_release(rt_uint8_t mode) { rt_ubase_t level; struct rt_pm *pm; @@ -342,7 +342,7 @@ void rt_pm_device_unregister(struct rt_device *device) /** * This function set notification callback for application */ -void rt_pm_notify_set(void (*notify)(uint8_t event, uint8_t mode, void *data), void *data) +void rt_pm_notify_set(void (*notify)(rt_uint8_t event, rt_uint8_t mode, void *data), void *data) { _pm_notify.notify = notify; _pm_notify.data = data; @@ -351,7 +351,7 @@ void rt_pm_notify_set(void (*notify)(uint8_t event, uint8_t mode, void *data), v /** * This function set default sleep mode when no pm_request */ -void rt_pm_default_set(uint8_t sleep_mode) +void rt_pm_default_set(rt_uint8_t sleep_mode) { _pm_default_sleep = sleep_mode; } @@ -428,7 +428,7 @@ static rt_err_t _rt_pm_device_control(rt_device_t dev, return RT_EOK; } -int rt_pm_run_enter(uint8_t mode) +int rt_pm_run_enter(rt_uint8_t mode) { rt_base_t level; struct rt_pm *pm; -- cgit v1.3.1 From ab981a32b54b5be3426bbf7f6caf62624714daf4 Mon Sep 17 00:00:00 2001 From: a1012112796 <1012112796@qq.com> Date: Tue, 26 Nov 2019 07:41:43 +0800 Subject: [Device/I2C] Chang debug message print way to ulog --- bsp/gd32303e-eval/drivers/drv_i2c.c | 14 +++++-- bsp/gd32e230k-start/drivers/drv_i2c.c | 14 +++++-- components/drivers/Kconfig | 10 +++++ components/drivers/i2c/i2c-bit-ops.c | 64 ++++++++++++++++---------------- components/drivers/i2c/i2c_core.c | 20 +++++++--- components/drivers/i2c/i2c_dev.c | 12 +++++- components/drivers/include/drivers/i2c.h | 6 --- 7 files changed, 89 insertions(+), 51 deletions(-) (limited to 'components/drivers/include') diff --git a/bsp/gd32303e-eval/drivers/drv_i2c.c b/bsp/gd32303e-eval/drivers/drv_i2c.c index 7c8ba277c8..059b211c01 100644 --- a/bsp/gd32303e-eval/drivers/drv_i2c.c +++ b/bsp/gd32303e-eval/drivers/drv_i2c.c @@ -31,6 +31,14 @@ #include +#define DBG_TAG "drv.I2C" +#ifdef RT_I2C_DEBUG +#define DBG_LVL DBG_LOG +#else +#define DBG_LVL DBG_INFO +#endif +#include + #ifdef RT_USING_I2C_BITOPS /*user can change this*/ @@ -256,7 +264,7 @@ static rt_size_t gd32_i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg { if (gd32_i2c_read(gd32_i2c->i2c_periph, msg->addr, msg->buf, msg->len) != 0) { - i2c_dbg("i2c bus write failed,i2c bus stop!\n"); + LOG_E("i2c bus write failed,i2c bus stop!"); goto out; } } @@ -264,7 +272,7 @@ static rt_size_t gd32_i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg { if (gd32_i2c_write(gd32_i2c->i2c_periph, msg->addr, msg->buf, msg->len) != 0) { - i2c_dbg("i2c bus write failed,i2c bus stop!\n"); + LOG_E("i2c bus write failed,i2c bus stop!"); goto out; } } @@ -273,7 +281,7 @@ static rt_size_t gd32_i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg ret = i; out: - i2c_dbg("send stop condition\n"); + LOG_E("send stop condition\n"); return ret; } diff --git a/bsp/gd32e230k-start/drivers/drv_i2c.c b/bsp/gd32e230k-start/drivers/drv_i2c.c index 473a2b1bda..57f49e4633 100644 --- a/bsp/gd32e230k-start/drivers/drv_i2c.c +++ b/bsp/gd32e230k-start/drivers/drv_i2c.c @@ -18,6 +18,14 @@ #include +#define DBG_TAG "drv.I2C" +#ifdef RT_I2C_DEBUG +#define DBG_LVL DBG_LOG +#else +#define DBG_LVL DBG_INFO +#endif +#include + #ifdef RT_USING_I2C_BITOPS /*user can change this*/ @@ -243,7 +251,7 @@ static rt_size_t gd32_i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg { if (gd32_i2c_read(gd32_i2c->i2c_periph, msg->addr, msg->buf, msg->len) != 0) { - i2c_dbg("i2c bus write failed,i2c bus stop!\n"); + LOG_E("i2c bus write failed,i2c bus stop!"); goto out; } } @@ -251,7 +259,7 @@ static rt_size_t gd32_i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg { if (gd32_i2c_write(gd32_i2c->i2c_periph, msg->addr, msg->buf, msg->len) != 0) { - i2c_dbg("i2c bus write failed,i2c bus stop!\n"); + LOG_E("i2c bus write failed,i2c bus stop!"); goto out; } } @@ -260,7 +268,7 @@ static rt_size_t gd32_i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg ret = i; out: - i2c_dbg("send stop condition\n"); + LOG_D("send stop condition"); return ret; } diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index c2e6d3a411..12b82671e3 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -88,11 +88,21 @@ config RT_USING_I2C default n if RT_USING_I2C + config RT_I2C_DEBUG + bool "Use I2C debug mesage" + default n + config RT_USING_I2C_BITOPS bool "Use GPIO to simulate I2C" default y endif +if RT_USING_I2C_BITOPS + config RT_I2C_BITOPS_DEBUG + bool "Use simulate I2C debug mesage" + default n +endif + config RT_USING_PIN bool "Using generic GPIO device drivers" default y diff --git a/components/drivers/i2c/i2c-bit-ops.c b/components/drivers/i2c/i2c-bit-ops.c index 478cf99fd6..99338eef54 100644 --- a/components/drivers/i2c/i2c-bit-ops.c +++ b/components/drivers/i2c/i2c-bit-ops.c @@ -10,11 +10,13 @@ #include -#ifdef RT_I2C_BIT_DEBUG -#define bit_dbg(fmt, ...) rt_kprintf(fmt, ##__VA_ARGS__) +#define DBG_TAG "I2C" +#ifdef RT_I2C_BITOPS_DEBUG +#define DBG_LVL DBG_LOG #else -#define bit_dbg(fmt, ...) +#define DBG_LVL DBG_INFO #endif +#include #define SET_SDA(ops, val) ops->set_sda(ops->data, val) #define SET_SCL(ops, val) ops->set_scl(ops->data, val) @@ -54,11 +56,11 @@ static rt_err_t SCL_H(struct rt_i2c_bit_ops *ops) return -RT_ETIMEOUT; rt_thread_delay((ops->timeout + 1) >> 1); } -#ifdef RT_I2C_BIT_DEBUG +#ifdef RT_I2C_BITOPS_DEBUG if (rt_tick_get() != start) { - bit_dbg("wait %ld tick for SCL line to go high\n", - rt_tick_get() - start); + LOG_D("wait %ld tick for SCL line to go high", + rt_tick_get() - start); } #endif @@ -70,14 +72,14 @@ done: static void i2c_start(struct rt_i2c_bit_ops *ops) { -#ifdef RT_I2C_BIT_DEBUG +#ifdef RT_I2C_BITOPS_DEBUG if (ops->get_scl && !GET_SCL(ops)) { - bit_dbg("I2C bus error, SCL line low\n"); + LOG_E("I2C bus error, SCL line low"); } if (ops->get_sda && !GET_SDA(ops)) { - bit_dbg("I2C bus error, SDA line low\n"); + LOG_E("I2C bus error, SDA line low"); } #endif SDA_L(ops); @@ -114,13 +116,13 @@ rt_inline rt_bool_t i2c_waitack(struct rt_i2c_bit_ops *ops) if (SCL_H(ops) < 0) { - bit_dbg("wait ack timeout\n"); + LOG_W("wait ack timeout"); return -RT_ETIMEOUT; } ack = !GET_SDA(ops); /* ACK : SDA pin is pulled low */ - bit_dbg("%s\n", ack ? "ACK" : "NACK"); + LOG_D("%s", ack ? "ACK" : "NACK"); SCL_L(ops); @@ -142,8 +144,8 @@ static rt_int32_t i2c_writeb(struct rt_i2c_bus_device *bus, rt_uint8_t data) i2c_delay(ops); if (SCL_H(ops) < 0) { - bit_dbg("i2c_writeb: 0x%02x, " - "wait scl pin high timeout at bit %d\n", + LOG_D("i2c_writeb: 0x%02x, " + "wait scl pin high timeout at bit %d", data, i); return -RT_ETIMEOUT; @@ -169,8 +171,8 @@ static rt_int32_t i2c_readb(struct rt_i2c_bus_device *bus) if (SCL_H(ops) < 0) { - bit_dbg("i2c_readb: wait scl pin high " - "timeout at bit %d\n", 7 - i); + LOG_D("i2c_readb: wait scl pin high " + "timeout at bit %d", 7 - i); return -RT_ETIMEOUT; } @@ -205,13 +207,13 @@ static rt_size_t i2c_send_bytes(struct rt_i2c_bus_device *bus, } else if (ret == 0) { - i2c_dbg("send bytes: NACK.\n"); + LOG_D("send bytes: NACK."); return 0; } else { - i2c_dbg("send bytes: error %d\n", ret); + LOG_E("send bytes: error %d", ret); return ret; } @@ -229,7 +231,7 @@ static rt_err_t i2c_send_ack_or_nack(struct rt_i2c_bus_device *bus, int ack) i2c_delay(ops); if (SCL_H(ops) < 0) { - bit_dbg("ACK or NACK timeout\n"); + LOG_E("ACK or NACK timeout."); return -RT_ETIMEOUT; } @@ -263,7 +265,7 @@ static rt_size_t i2c_recv_bytes(struct rt_i2c_bus_device *bus, ptr ++; count --; - bit_dbg("recieve bytes: 0x%02x, %s\n", + LOG_D("recieve bytes: 0x%02x, %s", val, (flags & RT_I2C_NO_READ_ACK) ? "(No ACK/NACK)" : (count ? "ACK" : "NACK")); @@ -291,10 +293,10 @@ static rt_int32_t i2c_send_address(struct rt_i2c_bus_device *bus, ret = i2c_writeb(bus, addr); if (ret == 1 || i == retries) break; - bit_dbg("send stop condition\n"); + LOG_D("send stop condition"); i2c_stop(ops); i2c_delay2(ops); - bit_dbg("send start condition\n"); + LOG_D("send start condition"); i2c_start(ops); } @@ -319,12 +321,12 @@ static rt_err_t i2c_bit_send_address(struct rt_i2c_bus_device *bus, addr1 = 0xf0 | ((msg->addr >> 7) & 0x06); addr2 = msg->addr & 0xff; - bit_dbg("addr1: %d, addr2: %d\n", addr1, addr2); + LOG_D("addr1: %d, addr2: %d", addr1, addr2); ret = i2c_send_address(bus, addr1, retries); if ((ret != 1) && !ignore_nack) { - bit_dbg("NACK: sending first addr\n"); + LOG_W("NACK: sending first addr"); return -RT_EIO; } @@ -332,19 +334,19 @@ static rt_err_t i2c_bit_send_address(struct rt_i2c_bus_device *bus, ret = i2c_writeb(bus, addr2); if ((ret != 1) && !ignore_nack) { - bit_dbg("NACK: sending second addr\n"); + LOG_W("NACK: sending second addr"); return -RT_EIO; } if (flags & RT_I2C_RD) { - bit_dbg("send repeated start condition\n"); + LOG_D("send repeated start condition"); i2c_restart(ops); addr1 |= 0x01; ret = i2c_send_address(bus, addr1, retries); if ((ret != 1) && !ignore_nack) { - bit_dbg("NACK: sending repeated addr\n"); + LOG_E("NACK: sending repeated addr"); return -RT_EIO; } @@ -373,7 +375,7 @@ static rt_size_t i2c_bit_xfer(struct rt_i2c_bus_device *bus, rt_int32_t i, ret; rt_uint16_t ignore_nack; - bit_dbg("send start condition\n"); + LOG_D("send start condition"); i2c_start(ops); for (i = 0; i < num; i++) { @@ -388,7 +390,7 @@ static rt_size_t i2c_bit_xfer(struct rt_i2c_bus_device *bus, ret = i2c_bit_send_address(bus, msg); if ((ret != RT_EOK) && !ignore_nack) { - bit_dbg("receive NACK from device addr 0x%02x msg %d\n", + LOG_D("receive NACK from device addr 0x%02x msg %d", msgs[i].addr, i); goto out; } @@ -397,7 +399,7 @@ static rt_size_t i2c_bit_xfer(struct rt_i2c_bus_device *bus, { ret = i2c_recv_bytes(bus, msg); if (ret >= 1) - bit_dbg("read %d byte%s\n", ret, ret == 1 ? "" : "s"); + LOG_D("read %d byte%s", ret, ret == 1 ? "" : "s"); if (ret < msg->len) { if (ret >= 0) @@ -409,7 +411,7 @@ static rt_size_t i2c_bit_xfer(struct rt_i2c_bus_device *bus, { ret = i2c_send_bytes(bus, msg); if (ret >= 1) - bit_dbg("write %d byte%s\n", ret, ret == 1 ? "" : "s"); + LOG_D("write %d byte%s", ret, ret == 1 ? "" : "s"); if (ret < msg->len) { if (ret >= 0) @@ -421,7 +423,7 @@ static rt_size_t i2c_bit_xfer(struct rt_i2c_bus_device *bus, ret = i; out: - bit_dbg("send stop condition\n"); + LOG_D("send stop condition"); i2c_stop(ops); return ret; diff --git a/components/drivers/i2c/i2c_core.c b/components/drivers/i2c/i2c_core.c index c0f0c70565..f358c2b758 100644 --- a/components/drivers/i2c/i2c_core.c +++ b/components/drivers/i2c/i2c_core.c @@ -10,6 +10,14 @@ #include +#define DBG_TAG "I2C" +#ifdef RT_I2C_DEBUG +#define DBG_LVL DBG_LOG +#else +#define DBG_LVL DBG_INFO +#endif +#include + rt_err_t rt_i2c_bus_device_register(struct rt_i2c_bus_device *bus, const char *bus_name) { @@ -21,7 +29,7 @@ rt_err_t rt_i2c_bus_device_register(struct rt_i2c_bus_device *bus, res = rt_i2c_bus_device_device_init(bus, bus_name); - i2c_dbg("I2C bus [%s] registered\n", bus_name); + LOG_I("I2C bus [%s] registered", bus_name); return res; } @@ -32,7 +40,7 @@ struct rt_i2c_bus_device *rt_i2c_bus_device_find(const char *bus_name) rt_device_t dev = rt_device_find(bus_name); if (dev == RT_NULL || dev->type != RT_Device_Class_I2CBUS) { - i2c_dbg("I2C bus %s not exist\n", bus_name); + LOG_E("I2C bus %s not exist", bus_name); return RT_NULL; } @@ -53,9 +61,9 @@ rt_size_t rt_i2c_transfer(struct rt_i2c_bus_device *bus, #ifdef RT_I2C_DEBUG for (ret = 0; ret < num; ret++) { - i2c_dbg("msgs[%d] %c, addr=0x%02x, len=%d\n", ret, - (msgs[ret].flags & RT_I2C_RD) ? 'R' : 'W', - msgs[ret].addr, msgs[ret].len); + LOG_D("msgs[%d] %c, addr=0x%02x, len=%d", ret, + (msgs[ret].flags & RT_I2C_RD) ? 'R' : 'W', + msgs[ret].addr, msgs[ret].len); } #endif @@ -67,7 +75,7 @@ rt_size_t rt_i2c_transfer(struct rt_i2c_bus_device *bus, } else { - i2c_dbg("I2C bus operation not supported\n"); + LOG_E("I2C bus operation not supported"); return 0; } diff --git a/components/drivers/i2c/i2c_dev.c b/components/drivers/i2c/i2c_dev.c index c784a4e4ee..59f728be09 100644 --- a/components/drivers/i2c/i2c_dev.c +++ b/components/drivers/i2c/i2c_dev.c @@ -11,6 +11,14 @@ #include +#define DBG_TAG "I2C" +#ifdef RT_I2C_DEBUG +#define DBG_LVL DBG_LOG +#else +#define DBG_LVL DBG_INFO +#endif +#include + static rt_size_t i2c_bus_device_read(rt_device_t dev, rt_off_t pos, void *buffer, @@ -23,7 +31,7 @@ static rt_size_t i2c_bus_device_read(rt_device_t dev, RT_ASSERT(bus != RT_NULL); RT_ASSERT(buffer != RT_NULL); - i2c_dbg("I2C bus dev [%s] reading %u bytes.\n", dev->parent.name, count); + LOG_D("I2C bus dev [%s] reading %u bytes.", dev->parent.name, count); addr = pos & 0xffff; flags = (pos >> 16) & 0xffff; @@ -43,7 +51,7 @@ static rt_size_t i2c_bus_device_write(rt_device_t dev, RT_ASSERT(bus != RT_NULL); RT_ASSERT(buffer != RT_NULL); - i2c_dbg("I2C bus dev [%s] writing %u bytes.\n", dev->parent.name, count); + LOG_D("I2C bus dev [%s] writing %u bytes.", dev->parent.name, count); addr = pos & 0xffff; flags = (pos >> 16) & 0xffff; diff --git a/components/drivers/include/drivers/i2c.h b/components/drivers/include/drivers/i2c.h index c6376efa89..17d57288cd 100644 --- a/components/drivers/include/drivers/i2c.h +++ b/components/drivers/include/drivers/i2c.h @@ -67,12 +67,6 @@ struct rt_i2c_client rt_uint16_t client_addr; }; -#ifdef RT_I2C_DEBUG -#define i2c_dbg(fmt, ...) rt_kprintf(fmt, ##__VA_ARGS__) -#else -#define i2c_dbg(fmt, ...) -#endif - rt_err_t rt_i2c_bus_device_register(struct rt_i2c_bus_device *bus, const char *bus_name); struct rt_i2c_bus_device *rt_i2c_bus_device_find(const char *bus_name); -- cgit v1.3.1 From d906bf5b18332cc2ea689158f758afe74d8bfa18 Mon Sep 17 00:00:00 2001 From: Yifeng Zhao Date: Mon, 11 Nov 2019 16:58:45 +0800 Subject: spi: support slave mode and master mode runtime config support slave mode and master mode runtime config. Change-Id: Iabadbade8997bb8e3adef8ce95f6c45f6bb35329 Signed-off-by: Yifeng Zhao --- components/drivers/include/drivers/spi.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'components/drivers/include') diff --git a/components/drivers/include/drivers/spi.h b/components/drivers/include/drivers/spi.h index 04a65421a5..301726fda8 100644 --- a/components/drivers/include/drivers/spi.h +++ b/components/drivers/include/drivers/spi.h @@ -43,7 +43,7 @@ extern "C"{ #define RT_SPI_MODE_2 (RT_SPI_CPOL | 0) /* CPOL = 1, CPHA = 0 */ #define RT_SPI_MODE_3 (RT_SPI_CPOL | RT_SPI_CPHA) /* CPOL = 1, CPHA = 1 */ -#define RT_SPI_MODE_MASK (RT_SPI_CPHA | RT_SPI_CPOL | RT_SPI_MSB) +#define RT_SPI_MODE_MASK (RT_SPI_CPHA | RT_SPI_CPOL | RT_SPI_MSB | RT_SPI_SLAVE) #define RT_SPI_BUS_MODE_SPI (1<<0) #define RT_SPI_BUS_MODE_QSPI (1<<1) -- cgit v1.3.1