Skip to content

Commit 54cfb56

Browse files
committed
[DM/FEATURE] DM Device IDA management
Drivers can manage their own IDs without having to concern themselves with the register/unregister in system Link: RT-Thread#9534 Signed-off-by: GuEe-GUI <[email protected]>
1 parent fed7c9a commit 54cfb56

File tree

4 files changed

+247
-0
lines changed

4 files changed

+247
-0
lines changed

components/drivers/core/dm.c

+131
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,137 @@ void rt_dm_secondary_cpu_init(void)
5353
}
5454
#endif /* RT_USING_SMP */
5555

56+
/**
57+
* @brief This function will alloc an id in an IDA object
58+
*
59+
* @param ida is the IDA object
60+
*
61+
* @return the id or -RT_EEMPTY
62+
*/
63+
int rt_dm_ida_alloc(struct rt_dm_ida *ida)
64+
{
65+
int id;
66+
RT_ASSERT(ida != RT_NULL);
67+
68+
rt_spin_lock(&ida->lock);
69+
70+
id = rt_bitmap_next_clear_bit(ida->map, 0, RT_DM_IDA_NUM);
71+
72+
if (id != RT_DM_IDA_NUM)
73+
{
74+
rt_bitmap_set_bit(ida->map, id);
75+
}
76+
77+
rt_spin_unlock(&ida->lock);
78+
79+
if (id != RT_DM_IDA_NUM)
80+
{
81+
return id;
82+
}
83+
84+
return -RT_EEMPTY;
85+
}
86+
87+
/**
88+
* @brief This function will take (force) an id in an IDA object
89+
*
90+
* @param ida is the IDA object
91+
*
92+
* @param id is the id that want to take
93+
*
94+
* @return the result of taking
95+
*/
96+
rt_bool_t rt_dm_ida_take(struct rt_dm_ida *ida, int id)
97+
{
98+
RT_ASSERT(ida != RT_NULL);
99+
RT_ASSERT(id >= 0);
100+
101+
rt_spin_lock(&ida->lock);
102+
103+
if (!rt_bitmap_test_bit(ida->map, id))
104+
{
105+
rt_bitmap_set_bit(ida->map, id);
106+
}
107+
else
108+
{
109+
id = RT_DM_IDA_NUM;
110+
}
111+
112+
rt_spin_unlock(&ida->lock);
113+
114+
return id != RT_DM_IDA_NUM;
115+
}
116+
117+
/**
118+
* @brief This function will release an id in an IDA object
119+
*
120+
* @param ida is the IDA object
121+
*
122+
* @param id is the id of IDA object
123+
*/
124+
void rt_dm_ida_free(struct rt_dm_ida *ida, int id)
125+
{
126+
RT_ASSERT(ida != RT_NULL);
127+
RT_ASSERT(id >= 0);
128+
129+
rt_spin_lock(&ida->lock);
130+
131+
rt_bitmap_clear_bit(ida->map, id);
132+
133+
rt_spin_unlock(&ida->lock);
134+
}
135+
136+
/**
137+
* @brief This function will return the specified master id and device id of device.
138+
*
139+
* @param master_id is the master id (0, 255] of device
140+
*
141+
* @param device_id is the device id [-1, 255] of device, when device_id is -1,
142+
* the function will end when find the first device.
143+
*
144+
* @return the device object or RT_NULL
145+
*/
146+
rt_device_t rt_dm_device_find(int master_id, int device_id)
147+
{
148+
struct rt_device *dev, *ret_dev = RT_NULL;
149+
struct rt_object_information *information = RT_NULL;
150+
151+
if (master_id <= 0 || device_id > 255)
152+
{
153+
return RT_NULL;
154+
}
155+
156+
information = rt_object_get_information(RT_Object_Class_Device);
157+
158+
/* parameter check */
159+
if (!information)
160+
{
161+
return RT_NULL;
162+
}
163+
164+
/* which is invoke in interrupt status */
165+
RT_DEBUG_NOT_IN_INTERRUPT;
166+
167+
/* enter critical */
168+
rt_enter_critical();
169+
170+
/* try to find object */
171+
rt_list_for_each_entry(dev, &information->object_list, parent.list)
172+
{
173+
if (master_id == dev->master_id &&
174+
(device_id == -1 || device_id == dev->device_id))
175+
{
176+
ret_dev = dev;
177+
break;
178+
}
179+
}
180+
181+
/* leave critical */
182+
rt_exit_critical();
183+
184+
return ret_dev;
185+
}
186+
56187
struct prefix_track
57188
{
58189
rt_list_t list;

components/drivers/include/drivers/core/dm.h

+19
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <rthw.h>
1515
#include <rtdef.h>
1616
#include <ioremap.h>
17+
#include <bitmap.h>
1718
#include <drivers/misc.h>
1819
#include <drivers/byteorder.h>
1920

@@ -27,6 +28,24 @@ extern int rt_hw_cpu_id(void);
2728

2829
void rt_dm_secondary_cpu_init(void);
2930

31+
/* ID Allocation */
32+
struct rt_dm_ida
33+
{
34+
rt_uint8_t master_id;
35+
36+
#define RT_DM_IDA_NUM 256
37+
RT_BITMAP_DECLARE(map, RT_DM_IDA_NUM);
38+
struct rt_spinlock lock;
39+
};
40+
41+
#define RT_DM_IDA_INIT(id) { .master_id = MASTER_ID_##id }
42+
43+
int rt_dm_ida_alloc(struct rt_dm_ida *ida);
44+
rt_bool_t rt_dm_ida_take(struct rt_dm_ida *ida, int id);
45+
void rt_dm_ida_free(struct rt_dm_ida *ida, int id);
46+
47+
rt_device_t rt_dm_device_find(int master_id, int device_id);
48+
3049
int rt_dm_dev_set_name_auto(rt_device_t dev, const char *prefix);
3150
int rt_dm_dev_get_name_id(rt_device_t dev);
3251

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright (c) 2006-2021, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2023-04-20 GuEe-GUI the first version
9+
*/
10+
11+
#ifndef __RT_DM_MASTER_ID_H__
12+
#define __RT_DM_MASTER_ID_H__
13+
14+
#define MASTER_ID_CUSTOM 0
15+
16+
/* Block */
17+
#define MASTER_ID_NVME 1
18+
#define MASTER_ID_SCSI_SD 2
19+
#define MASTER_ID_SCSI_CDROM 3
20+
#define MASTER_ID_SDIO 4
21+
#define MASTER_ID_VIRTUAL_BLOCK 5
22+
23+
/* Char */
24+
#define MASTER_ID_RPMSG_EPT 11
25+
#define MASTER_ID_RPMSG_CHAR 12
26+
#define MASTER_ID_SERIAL 13
27+
28+
/* Clock Timer */
29+
#define MASTER_ID_HWTIMER 21
30+
#define MASTER_ID_PTP 22
31+
#define MASTER_ID_RTC 23
32+
33+
/* Graphic Display */
34+
#define MASTER_ID_GRAPHIC_BACKLIGHT 31
35+
#define MASTER_ID_GRAPHIC_FRAMEBUFFER 32
36+
#define MASTER_ID_LED 33
37+
38+
/* Hardware Monitor */
39+
#define MASTER_ID_DVFS 41
40+
#define MASTER_ID_SENSOR 42
41+
#define MASTER_ID_THERMAL 43
42+
#define MASTER_ID_WATCHDOG 44
43+
44+
/* I2C */
45+
#define MASTER_ID_I2C_BUS 51
46+
#define MASTER_ID_I2C_DEV 52
47+
48+
/* IO Contorl */
49+
#define MASTER_ID_ADC 61
50+
#define MASTER_ID_DAC 62
51+
#define MASTER_ID_PIN 63
52+
#define MASTER_ID_PWM 64
53+
54+
/* Memory */
55+
#define MASTER_ID_MEM 71
56+
#define MASTER_ID_MTD 72
57+
58+
/* MISC */
59+
#define MASTER_ID_MISC 81
60+
61+
/* Multimedia */
62+
#define MASTER_ID_AUDIO 91
63+
64+
/* Net */
65+
#define MASTER_ID_CAN 101
66+
#define MASTER_ID_ETH 102
67+
#define MASTER_ID_PHY 103
68+
#define MASTER_ID_WLAN 104
69+
70+
/* Input */
71+
#define MASTER_ID_INPUT 111
72+
#define MASTER_ID_TOUCH 112
73+
74+
/* Security */
75+
#define MASTER_ID_HWCRYPTO 121
76+
#define MASTER_ID_RNG 122
77+
#define MASTER_ID_TEE 123
78+
79+
/* SPI */
80+
#define MASTER_ID_SPI_BUS 131
81+
#define MASTER_ID_SPI_DEV 132
82+
83+
/* TTY */
84+
#define MASTER_ID_TTY 141
85+
#define MASTER_ID_TTY_SLAVES 142
86+
#define MASTER_ID_TTY_ALTERNATE 143
87+
#define MASTER_ID_PTMX 144
88+
89+
/* USB */
90+
#define MASTER_ID_USB_DEV 151
91+
#define MASTER_ID_USB_BUS 152
92+
#define MASTER_ID_USB_OTG 153
93+
94+
#endif /* __RT_DM_MASTER_ID_H__ */

include/rtdef.h

+3
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,9 @@ struct rt_device
13741374
rt_uint16_t open_flag; /**< device open flag */
13751375

13761376
rt_uint8_t ref_count; /**< reference count */
1377+
#ifdef RT_USING_DM
1378+
rt_uint8_t master_id; /**< 0 - 255 */
1379+
#endif
13771380
rt_uint8_t device_id; /**< 0 - 255 */
13781381

13791382
/* device call back */

0 commit comments

Comments
 (0)