Skip to content

Commit d51f0bc

Browse files
wuxn9999YiluMao
authored andcommitted
IssueID:1919:improve PWM and ADC driver framework
[Detail] Improve PWM driver framework. Improve ADC driver framework. New VFS support. Userspace support. [Verified Cases] Build Pass: helloworld_demo@haaseduk1 Test Pass: helloworld_demo@haaseduk1
1 parent c0e05e1 commit d51f0bc

File tree

11 files changed

+369
-170
lines changed

11 files changed

+369
-170
lines changed

components/drivers/core/base/include/aos/device.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ typedef enum {
1616
AOS_DEV_TYPE_WATCHDOG,
1717
AOS_DEV_TYPE_ETHERNET,
1818
AOS_DEV_TYPE_WLAN,
19-
AOS_DEV_TYPE_BLOCK,
19+
AOS_DEV_TYPE_DISK,
20+
AOS_DEV_TYPE_DISKPART,
2021
AOS_DEV_TYPE_FLASH,
2122
AOS_DEV_TYPE_FLASHPART,
22-
AOS_DEV_TYPE_MTD,
2323
AOS_DEV_TYPE_RTC,
2424
AOS_DEV_TYPE_SPI,
2525
AOS_DEV_TYPE_I2C,

components/drivers/core/base/include/aos/device_core.h

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,22 @@
88
#include <aos/kernel.h>
99
#include <aos/list.h>
1010
#include <k_rbtree.h>
11+
#include <aos/device.h>
12+
#ifdef AOS_COMP_DEVFS
13+
#include <aos/devfs.h>
14+
#endif
15+
#if defined(CONFIG_DRV_CORE) && CONFIG_DRV_CORE != 0
1116
#include <drivers/u_ld.h>
12-
#ifdef AOS_COMP_VFS
13-
#include <aos/vfs.h>
1417
#endif
15-
#include <aos/device.h>
1618

1719
struct aos_dev_ops;
1820

19-
#ifdef AOS_COMP_VFS
20-
#define AOS_DEV_NAME_MAX_LEN 63
21-
22-
typedef struct {
23-
char name[AOS_DEV_NAME_MAX_LEN + 1];
24-
const struct file_ops *ops;
25-
} aos_dev_vfs_helper_t;
26-
#endif
27-
2821
typedef struct aos_dev {
2922
aos_dev_type_t type;
3023
uint32_t id;
3124
const struct aos_dev_ops *ops;
32-
#ifdef AOS_COMP_VFS
33-
aos_dev_vfs_helper_t vfs_helper;
25+
#ifdef AOS_COMP_DEVFS
26+
aos_devfs_node_t devfs_node;
3427
#endif
3528
struct k_rbtree_node_t rb_node;
3629
aos_sem_t rb_sem;
@@ -53,6 +46,9 @@ typedef struct aos_dev_ops {
5346
extern "C" {
5447
#endif
5548

49+
#if !(defined(CONFIG_DRV_CORE) && CONFIG_DRV_CORE != 0)
50+
aos_status_t aos_dev_core_init(void);
51+
#endif
5652
aos_status_t aos_dev_register(aos_dev_t *dev);
5753
aos_status_t aos_dev_unregister(aos_dev_type_t type, uint32_t id);
5854
aos_status_t aos_dev_ref(aos_dev_ref_t *ref, aos_dev_t *dev);

components/drivers/core/base/src/device.c

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* Copyright (C) 2020-2021 Alibaba Group Holding Limited
33
*/
44

5-
#include <string.h>
65
#include <aos/device_core.h>
76

87
#define REF_COUNT_PENDING UINT32_MAX
@@ -11,6 +10,24 @@
1110
static struct k_rbtree_root_t device_map = RBT_ROOT;
1211
static aos_mutex_t device_map_mutex;
1312

13+
static aos_status_t device_core_init(void)
14+
{
15+
return aos_mutex_new(&device_map_mutex);
16+
}
17+
18+
#if defined(CONFIG_DRV_CORE) && CONFIG_DRV_CORE != 0
19+
static int dev_core_init(void)
20+
{
21+
return (int)device_core_init();
22+
}
23+
CORE_DRIVER_ENTRY(dev_core_init)
24+
#else
25+
aos_status_t aos_dev_core_init(void)
26+
{
27+
return device_core_init();
28+
}
29+
#endif
30+
1431
static void device_map_lock(void)
1532
{
1633
(void)aos_mutex_lock(&device_map_mutex, AOS_WAIT_FOREVER);
@@ -96,37 +113,6 @@ static void remove_device(aos_dev_t *dev)
96113
k_rbtree_erase(&dev->rb_node, &device_map);
97114
}
98115

99-
#ifdef AOS_COMP_VFS
100-
static aos_status_t add_to_vfs(aos_dev_t *dev)
101-
{
102-
const char prefix[] = "/dev/";
103-
char path[sizeof(prefix) - 1 + sizeof(dev->vfs_helper.name)];
104-
105-
if (dev->vfs_helper.name[0] == '\0' || !dev->vfs_helper.ops)
106-
return 0;
107-
108-
memcpy(path, prefix, sizeof(prefix) - 1);
109-
strncpy(&path[sizeof(prefix) - 1], dev->vfs_helper.name, sizeof(dev->vfs_helper.name) - 1);
110-
path[sizeof(path) - 1] = '\0';
111-
112-
return aos_register_driver(path, dev->vfs_helper.ops, dev);
113-
}
114-
115-
static void remove_from_vfs(aos_dev_t *dev)
116-
{
117-
const char prefix[] = "/dev/";
118-
char path[sizeof(prefix) - 1 + sizeof(dev->vfs_helper.name)];
119-
120-
if (dev->vfs_helper.name[0] == '\0' || !dev->vfs_helper.ops)
121-
return;
122-
123-
memcpy(path, prefix, sizeof(prefix) - 1);
124-
strncpy(&path[sizeof(prefix) - 1], dev->vfs_helper.name, sizeof(dev->vfs_helper.name) - 1);
125-
path[sizeof(path) - 1] = '\0';
126-
(void)aos_unregister_driver(path);
127-
}
128-
#endif /* AOS_COMP_VFS */
129-
130116
aos_status_t aos_dev_register(aos_dev_t *dev)
131117
{
132118
aos_status_t ret;
@@ -157,15 +143,17 @@ aos_status_t aos_dev_register(aos_dev_t *dev)
157143
insert_device(dev);
158144
device_map_unlock();
159145

160-
#ifdef AOS_COMP_VFS
161-
ret = add_to_vfs(dev);
162-
if (ret) {
163-
device_map_lock();
164-
remove_device(dev);
165-
device_map_unlock();
166-
aos_mutex_free(&dev->mutex);
167-
aos_sem_free(&dev->rb_sem);
168-
return ret;
146+
#ifdef AOS_COMP_DEVFS
147+
if (aos_devfs_node_is_valid(&dev->devfs_node)) {
148+
ret = aos_devfs_add_node(&dev->devfs_node);
149+
if (ret) {
150+
device_map_lock();
151+
remove_device(dev);
152+
device_map_unlock();
153+
aos_mutex_free(&dev->mutex);
154+
aos_sem_free(&dev->rb_sem);
155+
return ret;
156+
}
169157
}
170158
#endif
171159

@@ -179,7 +167,6 @@ aos_status_t aos_dev_register(aos_dev_t *dev)
179167
aos_status_t aos_dev_unregister(aos_dev_type_t type, uint32_t id)
180168
{
181169
aos_dev_t *dev;
182-
aos_status_t ret;
183170

184171
device_map_lock();
185172
dev = find_device(type, id);
@@ -206,8 +193,9 @@ aos_status_t aos_dev_unregister(aos_dev_type_t type, uint32_t id)
206193

207194
aos_dev_unlock(dev);
208195
device_allow_removal(dev);
209-
#ifdef AOS_COMP_VFS
210-
remove_from_vfs(dev);
196+
#ifdef AOS_COMP_DEVFS
197+
if (aos_devfs_node_is_valid(&dev->devfs_node))
198+
(void)aos_devfs_remove_node(&dev->devfs_node);
211199
#endif
212200
device_map_lock();
213201
device_wait_removal(dev);
@@ -302,10 +290,3 @@ void aos_dev_put(aos_dev_ref_t *ref)
302290
aos_dev_unlock(ref->dev);
303291
aos_dev_ref_init(ref);
304292
}
305-
306-
static int device_core_init(void)
307-
{
308-
return (int)aos_mutex_new(&device_map_mutex);
309-
}
310-
311-
CORE_DRIVER_ENTRY(device_core_init)

components/drivers/peripheral/adc/include/aos/adc.h

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,61 @@
22
* Copyright (C) 2021 Alibaba Group Holding Limited
33
*/
44

5-
#ifndef _AOS_ADC_H
6-
#define _AOS_ADC_H
5+
#ifndef AOS_ADC_H
6+
#define AOS_ADC_H
77

8-
#include <stdint.h>
9-
#include <aos/kernel.h>
8+
#ifdef AOS_KERNEL_BUILD
109
#include <aos/device.h>
11-
12-
#ifdef __cplusplus
13-
extern "C" {
10+
#else
11+
#include <stdint.h>
1412
#endif
1513

16-
/** @defgroup driver_api driver
17-
* @ingroup aos_components
18-
* @{
19-
*/
20-
21-
/** @} */
22-
2314
/**
24-
* @defgroup aos_adc_app ADC
15+
* @defgroup adc_api ADC
2516
* @ingroup driver_api
26-
* 给应用提供ADC操作的AOS API.
27-
*
17+
* @brief AOS API for ADC.
2818
* @{
2919
*/
3020

31-
typedef aos_dev_ref_t aos_adc_ref_t; /**< ADC设备的引用 */
32-
3321
typedef enum {
3422
AOS_ADC_MODE_SINGLE, /**< 单次采样模式 */
3523
AOS_ADC_MODE_CONTINUE, /**< 连续采样模式 */
3624
} aos_adc_mode_t;
3725

26+
#if (defined(AOS_KERNEL_BUILD) && defined(AOS_COMP_DEVFS)) || !defined(AOS_KERNEL_BUILD)
27+
28+
typedef struct {
29+
int32_t channel;
30+
uint32_t time;
31+
} aos_adc_set_sample_time_args_t;
32+
33+
typedef struct {
34+
int32_t channel;
35+
uint32_t range;
36+
} aos_adc_get_range_args_t;
37+
38+
typedef struct {
39+
int32_t channel;
40+
int32_t data;
41+
} aos_adc_read_args_t;
42+
43+
#define AOS_ADC_IOC_SET_SAMPLE_TIME 0x4101
44+
#define AOS_ADC_IOC_SET_MODE 0x4102
45+
#define AOS_ADC_IOC_GET_RESOLUTION 0x4103
46+
#define AOS_ADC_IOC_GET_RANGE 0x4104
47+
#define AOS_ADC_IOC_READ 0x4105
48+
#define AOS_ADC_IOC_READ_VOLTAGE 0x4106
49+
50+
#endif /* (defined(AOS_KERNEL_BUILD) && defined(AOS_COMP_DEVFS)) || !defined(AOS_KERNEL_BUILD) */
51+
52+
#ifdef AOS_KERNEL_BUILD
53+
54+
typedef aos_dev_ref_t aos_adc_ref_t; /**< ADC设备的引用 */
55+
56+
#ifdef __cplusplus
57+
extern "C" {
58+
#endif
59+
3860
/**
3961
* 获取一个ADC设备的引用
4062
*
@@ -120,9 +142,12 @@ aos_status_t aos_adc_read(aos_adc_ref_t *ref, int32_t channel, int32_t *data);
120142
*/
121143
aos_status_t aos_adc_read_voltage(aos_adc_ref_t *ref, int32_t channel, int32_t *data);
122144

123-
/** @} */
124145
#ifdef __cplusplus
125146
}
126147
#endif
127148

128-
#endif /* _AOS_ADC_H */
149+
#endif /* AOS_KERNEL_BUILD */
150+
151+
/** @} */
152+
153+
#endif /* AOS_ADC_H */

components/drivers/peripheral/adc/include/aos/adc_core.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
* Copyright (C) 2021 Alibaba Group Holding Limited
33
*/
44

5-
#ifndef _AOS_ADC_CORE_H
6-
#define _AOS_ADC_CORE_H
5+
#ifndef AOS_ADC_CORE_H
6+
#define AOS_ADC_CORE_H
77

8-
#include <stdint.h>
9-
#include <aos/kernel.h>
108
#include <aos/device_core.h>
119
#include <aos/adc.h>
1210

@@ -84,4 +82,4 @@ aos_status_t aos_adc_unregister(uint32_t id);
8482
}
8583
#endif
8684

87-
#endif /* _AOS_ADC_CORE_H */
85+
#endif /* AOS_ADC_CORE_H */

components/drivers/peripheral/adc/package.yaml

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
21
## 第一部分: 基础信息
32
name: adc # <必选项> 包名称 (符合C语言变量命名规则),长度少于等于64字节
4-
version: master # <必选项> 组件版本号
5-
description: ADC 通用驱动 # <必选项> 建议至少20字以上
3+
version: master # <必选项> 组件版本号
4+
description: ADC通用驱动 # <必选项> 建议至少20字以上
65
type: common # <必选项> 组件类型,为:solution, chip, board, common, sdk
76

87
tag: 第三方驱动 # <可选项> 组件分类,缺省值: ''
98
keywords: # <可选项> 标签,会影响到组件被搜索的效果,合理的标签很重要
10-
- adc core and vfs driver
9+
- adc driver
1110
license: Apache license v2.0 # <可选项> 源代码的许可证,要确保所有代码、文件的许可证不冲突。如:MIT,Apache license v2.0,BSD
1211

1312
## 第二部分:依赖信息
@@ -18,7 +17,7 @@ license: Apache license v2.0 # <可选项> 源代码的
1817
# - aos: >=v7.2.0
1918
depends:
2019
- base: master
21-
- vfs: master
20+
- csi: master ? <AOS_CONFIG_ADC_CSI>
2221

2322
## 第四部分:编译连接信息
2423
# build_config: # <可选项> 编译配置项
@@ -47,29 +46,26 @@ build_config:
4746
# source_file: # <可选项> 指定参与编译的源代码文件,支持通配符,采用相对路径
4847
# - src/*.c # 例:组件 src 目录下所有的扩展名为 c 的源代码文件
4948
source_file:
50-
51-
# ADC
52-
- src/adc_dev.c ? <CONFIG_U_ADC_DEV>
53-
- src/adc.c ? <CONFIG_U_ADC_CORE>
54-
- src/adc_csi.c ? <CONFIG_U_ADC_CORE>
49+
- src/adc.c
50+
- src/adc_csi.c ? <AOS_CONFIG_ADC_CSI>
5551

5652
## 第五部分:配置信息
5753
# def_config: # 组件的可配置项
5854
# CONFIG_DEBUG: y
5955
# CONFIG_PARAM_NOT_CHECK: y
6056
# CONFIG_CLI: y
6157
def_config:
62-
CONFIG_U_ADC_DEV: 1 # ADC device node named witch "/dev/adc[x]"
63-
CONFIG_U_ADC_CORE: 0 # ADC subsys AOS API driver
58+
AOS_COMP_ADC: 1
6459

6560
## 第六部分:安装信息
6661
# install:
6762
# - dest: include/ # 安装的目的路径 dest是相当路径,通常是相对于YoC SDK 安装目录
6863
# source: # 安装源列表
6964
# - src/*.h # 支持通配符,相对路径
7065
install:
71-
- dest: "include/devices"
66+
- dest: "include/aos"
7267
source:
68+
- "include/aos/adc.h"
7369

7470
## 第七部分:导出部分
7571
# export:

0 commit comments

Comments
 (0)