Components/drivers: support DM device id management
We add the device name and id set api in DM, now driver could set name like sprintf without know how many devices it is. The misc.h and byteorder.h add some macros to developers that they always use in drivers. Signed-off-by: GuEe-GUI <GuEe-GUI@github.com>
This commit is contained in:
parent
b60d0cf370
commit
179157f4e1
|
@ -8,7 +8,7 @@ if GetDepend(['RT_USING_DEV_BUS']):
|
|||
src = src + ['bus.c']
|
||||
|
||||
if GetDepend(['RT_USING_DM']):
|
||||
src = src + ['driver.c']
|
||||
src = src + ['dm.c', 'driver.c']
|
||||
|
||||
group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_DEVICE'], CPPPATH = CPPPATH)
|
||||
|
||||
|
|
|
@ -0,0 +1,160 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2023-04-20 ErikChan the first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#include <drivers/core/rtdm.h>
|
||||
|
||||
#ifdef RT_USING_SMP
|
||||
static int rti_secondary_cpu_start(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
INIT_EXPORT(rti_secondary_cpu_start, "6.end");
|
||||
|
||||
static int rti_secondary_cpu_end(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
INIT_EXPORT(rti_secondary_cpu_end, "7.end");
|
||||
|
||||
void rt_dm_secondary_cpu_init(void)
|
||||
{
|
||||
#if RT_DEBUG_INIT
|
||||
int result;
|
||||
const struct rt_init_desc *desc;
|
||||
|
||||
rt_kprintf("do secondary cpu initialization.\n");
|
||||
for (desc = &__rt_init_desc_rti_secondary_cpu_start; desc < &__rt_init_desc_rti_secondary_cpu_end; ++desc)
|
||||
{
|
||||
rt_kprintf("initialize %s", desc->fn_name);
|
||||
result = desc->fn();
|
||||
rt_kprintf(":%d done\n", result);
|
||||
}
|
||||
#else
|
||||
volatile const init_fn_t *fn_ptr;
|
||||
|
||||
for (fn_ptr = &__rt_init_rti_secondary_cpu_start; fn_ptr < &__rt_init_rti_secondary_cpu_end; ++fn_ptr)
|
||||
{
|
||||
(*fn_ptr)();
|
||||
}
|
||||
#endif /* RT_DEBUG_INIT */
|
||||
}
|
||||
#endif /* RT_USING_SMP */
|
||||
|
||||
struct prefix_track
|
||||
{
|
||||
rt_list_t list;
|
||||
|
||||
int uid;
|
||||
const char *prefix;
|
||||
};
|
||||
static struct rt_spinlock _prefix_nodes_lock;
|
||||
static rt_list_t _prefix_nodes = RT_LIST_OBJECT_INIT(_prefix_nodes);
|
||||
|
||||
int rt_dm_set_dev_name_auto(rt_device_t dev, const char *prefix)
|
||||
{
|
||||
int uid = -1;
|
||||
struct prefix_track *pt = RT_NULL;
|
||||
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
RT_ASSERT(prefix != RT_NULL);
|
||||
|
||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||
|
||||
rt_spin_lock(&_prefix_nodes_lock);
|
||||
|
||||
rt_list_for_each_entry(pt, &_prefix_nodes, list)
|
||||
{
|
||||
/* caller always input constants string, check ptr is faster */
|
||||
if (pt->prefix == prefix || !rt_strcmp(pt->prefix, prefix))
|
||||
{
|
||||
uid = ++pt->uid;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
rt_spin_unlock(&_prefix_nodes_lock);
|
||||
|
||||
if (uid < 0)
|
||||
{
|
||||
pt = rt_malloc(sizeof(*pt));
|
||||
|
||||
if (!pt)
|
||||
{
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
rt_list_init(&pt->list);
|
||||
|
||||
pt->uid = uid = 0;
|
||||
pt->prefix = prefix;
|
||||
|
||||
rt_spin_lock(&_prefix_nodes_lock);
|
||||
|
||||
rt_list_insert_before(&_prefix_nodes, &pt->list);
|
||||
|
||||
rt_spin_unlock(&_prefix_nodes_lock);
|
||||
}
|
||||
|
||||
return rt_dm_set_dev_name(dev, "%s%u", prefix, uid);
|
||||
}
|
||||
|
||||
int rt_dm_get_dev_name_id(rt_device_t dev)
|
||||
{
|
||||
int id = 0, len;
|
||||
const char *name;
|
||||
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
|
||||
name = rt_dm_get_dev_name(dev);
|
||||
len = rt_strlen(name) - 1;
|
||||
name += len;
|
||||
|
||||
while (len --> 0)
|
||||
{
|
||||
if (*name < '0' || *name > '9')
|
||||
{
|
||||
while (*(++name))
|
||||
{
|
||||
id *= 10;
|
||||
id += *name - '0';
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
--name;
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
int rt_dm_set_dev_name(rt_device_t dev, const char *format, ...)
|
||||
{
|
||||
int n;
|
||||
va_list arg_ptr;
|
||||
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
RT_ASSERT(format != RT_NULL);
|
||||
|
||||
va_start(arg_ptr, format);
|
||||
n = rt_vsnprintf(dev->parent.name, RT_NAME_MAX, format, arg_ptr);
|
||||
va_end(arg_ptr);
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
const char *rt_dm_get_dev_name(rt_device_t dev)
|
||||
{
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
|
||||
return dev->parent.name;
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2023-02-25 GuEe-GUI the first version
|
||||
*/
|
||||
|
||||
#ifndef __BYTEORDER__
|
||||
#define __BYTEORDER__
|
||||
|
||||
#ifdef __CHECKER__
|
||||
#define __bitwise __attribute__((bitwise))
|
||||
#else
|
||||
#define __bitwise
|
||||
#endif
|
||||
|
||||
typedef rt_uint16_t __bitwise rt_le16_t;
|
||||
typedef rt_uint32_t __bitwise rt_le32_t;
|
||||
typedef rt_uint64_t __bitwise rt_le64_t;
|
||||
typedef rt_uint16_t __bitwise rt_be16_t;
|
||||
typedef rt_uint32_t __bitwise rt_be32_t;
|
||||
typedef rt_uint64_t __bitwise rt_be64_t;
|
||||
|
||||
/* gcc defines __BIG_ENDIAN__ on big endian targets */
|
||||
#if defined(__BIG_ENDIAN__) || defined(ARCH_CPU_BIG_ENDIAN)
|
||||
#define rt_cpu_to_be16(x) (x)
|
||||
#define rt_cpu_to_be32(x) (x)
|
||||
#define rt_cpu_to_be64(x) (x)
|
||||
#define rt_be16_to_cpu(x) (x)
|
||||
#define rt_be32_to_cpu(x) (x)
|
||||
#define rt_be64_to_cpu(x) (x)
|
||||
#define rt_le16_to_cpu(x) __builtin_bswap16(x)
|
||||
#define rt_le32_to_cpu(x) __builtin_bswap32(x)
|
||||
#define rt_le64_to_cpu(x) __builtin_bswap64(x)
|
||||
#define rt_cpu_to_le16(x) __builtin_bswap16(x)
|
||||
#define rt_cpu_to_le32(x) __builtin_bswap32(x)
|
||||
#define rt_cpu_to_le64(x) __builtin_bswap64(x)
|
||||
#else
|
||||
#define rt_cpu_to_be16(x) __builtin_bswap16(x)
|
||||
#define rt_cpu_to_be32(x) __builtin_bswap32(x)
|
||||
#define rt_cpu_to_be64(x) __builtin_bswap64(x)
|
||||
#define rt_be16_to_cpu(x) __builtin_bswap16(x)
|
||||
#define rt_be32_to_cpu(x) __builtin_bswap32(x)
|
||||
#define rt_be64_to_cpu(x) __builtin_bswap64(x)
|
||||
#define rt_le16_to_cpu(x) (x)
|
||||
#define rt_le32_to_cpu(x) (x)
|
||||
#define rt_le64_to_cpu(x) (x)
|
||||
#define rt_cpu_to_le16(x) (x)
|
||||
#define rt_cpu_to_le32(x) (x)
|
||||
#define rt_cpu_to_le64(x) (x)
|
||||
#endif /* __BIG_ENDIAN__ || ARCH_CPU_BIG_ENDIAN */
|
||||
|
||||
#undef __bitwise
|
||||
|
||||
#endif /* __BYTEORDER__ */
|
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2023-04-20 ErikChan the first version
|
||||
*/
|
||||
|
||||
#ifndef __RT_DM_H__
|
||||
#define __RT_DM_H__
|
||||
|
||||
#include <rthw.h>
|
||||
#include <rtdef.h>
|
||||
#include <drivers/misc.h>
|
||||
#include <drivers/byteorder.h>
|
||||
|
||||
#ifndef RT_CPUS_NR
|
||||
#define RT_CPUS_NR 1
|
||||
#endif
|
||||
|
||||
#ifndef RT_USING_SMP
|
||||
extern int rt_hw_cpu_id(void);
|
||||
#endif
|
||||
|
||||
void rt_dm_secondary_cpu_init(void);
|
||||
|
||||
int rt_dm_set_dev_name_auto(rt_device_t dev, const char *prefix);
|
||||
int rt_dm_get_dev_name_id(rt_device_t dev);
|
||||
|
||||
int rt_dm_set_dev_name(rt_device_t dev, const char *format, ...);
|
||||
const char *rt_dm_get_dev_name(rt_device_t dev);
|
||||
|
||||
/* init cpu, memory, interrupt-controller, bus... */
|
||||
#define INIT_CORE_EXPORT(fn) INIT_EXPORT(fn, "1.0")
|
||||
/* init pci/pcie, usb platform driver... */
|
||||
#define INIT_FRAMEWORK_EXPORT(fn) INIT_EXPORT(fn, "1.1")
|
||||
/* init platform, user code... */
|
||||
#define INIT_PLATFORM_EXPORT(fn) INIT_EXPORT(fn, "1.2")
|
||||
/* init sys-timer, clk, pinctrl... */
|
||||
#define INIT_SUBSYS_EXPORT(fn) INIT_EXPORT(fn, "1.3")
|
||||
/* init early drivers */
|
||||
#define INIT_DRIVER_EARLY_EXPORT(fn) INIT_EXPORT(fn, "1.4")
|
||||
/* init in secondary_cpu_c_start */
|
||||
#define INIT_SECONDARY_CPU_EXPORT(fn) INIT_EXPORT(fn, "7")
|
||||
/* init after mount fs */
|
||||
#define INIT_FS_EXPORT(fn) INIT_EXPORT(fn, "6.0")
|
||||
|
||||
#endif /* __RT_DM_H__ */
|
|
@ -0,0 +1,86 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2023-02-25 GuEe-GUI the first version
|
||||
*/
|
||||
|
||||
#ifndef __MISC_H__
|
||||
#define __MISC_H__
|
||||
|
||||
#include <rtdef.h>
|
||||
|
||||
#ifdef ARCH_CPU_64BIT
|
||||
#define RT_BITS_PER_LONG 64
|
||||
#else
|
||||
#define RT_BITS_PER_LONG 32
|
||||
#endif
|
||||
#define RT_BITS_PER_LONG_LONG 64
|
||||
|
||||
#define RT_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
|
||||
|
||||
#define RT_DIV_ROUND_CLOSEST(x, divisor) \
|
||||
({ \
|
||||
typeof(x) __x = x; \
|
||||
typeof(divisor) __d = divisor; \
|
||||
(((typeof(x))-1) > 0 || \
|
||||
((typeof(divisor))-1) > 0 || \
|
||||
(((__x) > 0) == ((__d) > 0))) ? \
|
||||
(((__x) + ((__d) / 2)) / (__d)) : \
|
||||
(((__x) - ((__d) / 2)) / (__d)); \
|
||||
})
|
||||
|
||||
#define RT_BIT(n) (1UL << (n))
|
||||
#define RT_BIT_ULL(n) (1ULL << (n))
|
||||
#define RT_BIT_MASK(nr) (1UL << ((nr) % RT_BITS_PER_LONG))
|
||||
#define RT_BIT_WORD(nr) ((nr) / RT_BITS_PER_LONG)
|
||||
|
||||
#define RT_BITS_PER_BYTE 8
|
||||
#define RT_BITS_PER_TYPE(type) (sizeof(type) * RT_BITS_PER_BYTE)
|
||||
#define RT_BITS_TO_BYTES(nr) RT_DIV_ROUND_UP(nr, RT_BITS_PER_TYPE(char))
|
||||
#define RT_BITS_TO_LONGS(nr) RT_DIV_ROUND_UP(nr, RT_BITS_PER_TYPE(long))
|
||||
|
||||
#define RT_GENMASK(h, l) (((~0UL) << (l)) & (~0UL >> (RT_BITS_PER_LONG - 1 - (h))))
|
||||
#define RT_GENMASK_ULL(h, l) (((~0ULL) << (l)) & (~0ULL >> (RT_BITS_PER_LONG_LONG - 1 - (h))))
|
||||
|
||||
#define RT_ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
|
||||
|
||||
#define rt_min(x, y) \
|
||||
({ \
|
||||
typeof(x) _x = (x); \
|
||||
typeof(y) _y = (y); \
|
||||
(void) (&_x == &_y); \
|
||||
_x < _y ? _x : _y; \
|
||||
})
|
||||
|
||||
#define rt_max(x, y) \
|
||||
({ \
|
||||
typeof(x) _x = (x); \
|
||||
typeof(y) _y = (y); \
|
||||
(void) (&_x == &_y); \
|
||||
_x > _y ? _x : _y; \
|
||||
})
|
||||
|
||||
#define rt_min_t(type, x, y) \
|
||||
({ \
|
||||
type _x = (x); \
|
||||
type _y = (y); \
|
||||
_x < _y ? _x: _y; \
|
||||
})
|
||||
|
||||
#define rt_clamp(val, lo, hi) rt_min((typeof(val))rt_max(val, lo), hi)
|
||||
|
||||
#define rt_do_div(n, base) \
|
||||
({ \
|
||||
rt_uint32_t _base = (base), _rem; \
|
||||
_rem = ((rt_uint64_t)(n)) % _base; \
|
||||
(n) = ((rt_uint64_t)(n)) / _base; \
|
||||
if (_rem > _base / 2) \
|
||||
++(n); \
|
||||
_rem; \
|
||||
})
|
||||
|
||||
#endif /* __MISC_H__ */
|
|
@ -169,6 +169,10 @@ extern "C" {
|
|||
#include "drivers/lcd.h"
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_DM
|
||||
#include "drivers/core/rtdm.h"
|
||||
#endif /* RT_USING_DM */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue