driver/sensor: user specifies device register number

N/A

Change-Id: Idd11461f933dd21b7271cd3ca87a2e33127a9d34
Signed-off-by: dongjiuzhu <dongjiuzhu1@xiaomi.com>
This commit is contained in:
dongjiuzhu 2020-11-02 18:02:04 +08:00 committed by Xiang Xiao
parent 0d92e50c42
commit 2482052228
5 changed files with 68 additions and 65 deletions

View file

@ -88,13 +88,13 @@ int board_app_initialize(uintptr_t arg)
#ifdef CONFIG_SIM_WTGAHRS2_UARTN
#if CONFIG_SIM_WTGAHRS2_UARTN == 0
wtgahrs2_initialize(CONFIG_SIM_UART0_NAME);
wtgahrs2_initialize(CONFIG_SIM_UART0_NAME, 0);
#elif CONFIG_SIM_WTGAHRS2_UARTN == 1
wtgahrs2_initialize(CONFIG_SIM_UART1_NAME);
wtgahrs2_initialize(CONFIG_SIM_UART1_NAME, 1);
#elif CONFIG_SIM_WTGAHRS2_UARTN == 2
wtgahrs2_initialize(CONFIG_SIM_UART2_NAME);
wtgahrs2_initialize(CONFIG_SIM_UART2_NAME, 2);
#elif CONFIG_SIM_WTGAHRS2_UARTN == 3
wtgahrs2_initialize(CONFIG_SIM_UART3_NAME);
wtgahrs2_initialize(CONFIG_SIM_UART3_NAME, 3);
#endif
#endif

View file

@ -55,9 +55,8 @@
struct sensor_info
{
uint8_t idx;
const uint8_t esize;
FAR const char *name;
uint8_t esize;
FAR char *name;
};
/* This structure describes sensor circular buffer */
@ -77,7 +76,6 @@ struct sensor_upperhalf_s
FAR struct sensor_lowerhalf_s *lower; /* the handle of lower half driver */
FAR struct sensor_buffer_s *buffer; /* The circualr buffer of sensor device */
FAR struct pollfd *fds; /* poll structures of threads waiting for driver events. */
uint8_t idx; /* The index number of node path */
uint8_t crefs; /* Number of times the device has been opened */
sem_t exclsem; /* Manages exclusive access to file operations */
sem_t buffersem; /* Wakeup user waiting for data in circular buffer */
@ -105,32 +103,32 @@ static int sensor_poll(FAR struct file *filep, FAR struct pollfd *fds,
* Private Data
****************************************************************************/
static struct sensor_info g_sensor_info[] =
static const struct sensor_info g_sensor_info[] =
{
{0, sizeof(struct sensor_event_accel), "accel"},
{0, sizeof(struct sensor_event_mag), "mag"},
{0, sizeof(struct sensor_event_gyro), "gyro"},
{0, sizeof(struct sensor_event_light), "light"},
{0, sizeof(struct sensor_event_baro), "baro"},
{0, sizeof(struct sensor_event_prox), "prox"},
{0, sizeof(struct sensor_event_humi), "humi"},
{0, sizeof(struct sensor_event_temp), "temp"},
{0, sizeof(struct sensor_event_rgb), "rgb"},
{0, sizeof(struct sensor_event_hall), "hall"},
{0, sizeof(struct sensor_event_ir), "ir"},
{0, sizeof(struct sensor_event_gps), "gps"},
{0, sizeof(struct sensor_event_uv), "uv"},
{0, sizeof(struct sensor_event_noise), "noise"},
{0, sizeof(struct sensor_event_pm25), "pm25"},
{0, sizeof(struct sensor_event_pm1p0), "pm1p0"},
{0, sizeof(struct sensor_event_pm10), "pm10"},
{0, sizeof(struct sensor_event_co2), "co2"},
{0, sizeof(struct sensor_event_hcho), "hcho"},
{0, sizeof(struct sensor_event_tvoc), "tvoc"},
{0, sizeof(struct sensor_event_ph), "ph"},
{0, sizeof(struct sensor_event_dust), "dust"},
{0, sizeof(struct sensor_event_hrate), "hrate"},
{0, sizeof(struct sensor_event_hbeat), "hbeat"},
{sizeof(struct sensor_event_accel), "accel"},
{sizeof(struct sensor_event_mag), "mag"},
{sizeof(struct sensor_event_gyro), "gyro"},
{sizeof(struct sensor_event_light), "light"},
{sizeof(struct sensor_event_baro), "baro"},
{sizeof(struct sensor_event_prox), "prox"},
{sizeof(struct sensor_event_humi), "humi"},
{sizeof(struct sensor_event_temp), "temp"},
{sizeof(struct sensor_event_rgb), "rgb"},
{sizeof(struct sensor_event_hall), "hall"},
{sizeof(struct sensor_event_ir), "ir"},
{sizeof(struct sensor_event_gps), "gps"},
{sizeof(struct sensor_event_uv), "uv"},
{sizeof(struct sensor_event_noise), "noise"},
{sizeof(struct sensor_event_pm25), "pm25"},
{sizeof(struct sensor_event_pm1p0), "pm1p0"},
{sizeof(struct sensor_event_pm10), "pm10"},
{sizeof(struct sensor_event_co2), "co2"},
{sizeof(struct sensor_event_hcho), "hcho"},
{sizeof(struct sensor_event_tvoc), "tvoc"},
{sizeof(struct sensor_event_ph), "ph"},
{sizeof(struct sensor_event_dust), "dust"},
{sizeof(struct sensor_event_hrate), "hrate"},
{sizeof(struct sensor_event_hbeat), "hbeat"},
};
static const struct file_operations g_sensor_fops =
@ -630,9 +628,11 @@ static void sensor_push_event(FAR void *priv, FAR const void *data,
* numbers. eg: accel0, accel1
*
* Input Parameters:
* dev - A pointer to an instance of lower half sensor driver. This
* instance is bound to the sensor driver and must persists as long
* as the driver persists.
* dev - A pointer to an instance of lower half sensor driver. This
* instance is bound to the sensor driver and must persists as long
* as the driver persists.
* devno - The user specifies which device of this type, from 0. If the
* devno alerady exists, -EEXIST will be returned.
*
* Returned Value:
* OK if the driver was successfully register; A negated errno value is
@ -640,7 +640,7 @@ static void sensor_push_event(FAR void *priv, FAR const void *data,
*
****************************************************************************/
int sensor_register(FAR struct sensor_lowerhalf_s *lower)
int sensor_register(FAR struct sensor_lowerhalf_s *lower, int devno)
{
FAR struct sensor_upperhalf_s *upper;
char path[DEVNAME_MAX];
@ -691,11 +691,10 @@ int sensor_register(FAR struct sensor_lowerhalf_s *lower)
goto buf_err;
}
upper->idx = g_sensor_info[lower->type].idx++;
snprintf(path, DEVNAME_MAX, DEVNAME_FMT,
g_sensor_info[lower->type].name,
lower->uncalibrated ? DEVNAME_UNCAL : "",
upper->idx);
devno);
sninfo("Registering %s\n", path);
ret = register_driver(path, &g_sensor_fops, 0666, upper);
@ -708,7 +707,6 @@ int sensor_register(FAR struct sensor_lowerhalf_s *lower)
drv_err:
sensor_buffer_release(upper->buffer);
g_sensor_info[lower->type].idx--;
buf_err:
nxsem_destroy(&upper->exclsem);
nxsem_destroy(&upper->buffersem);
@ -726,12 +724,13 @@ buf_err:
* upper half driver.
*
* Input Parameters:
* dev - A pointer to an instance of lower half sensor driver. This
* instance is bound to the sensor driver and must persists as long
* as the driver persists.
* dev - A pointer to an instance of lower half sensor driver. This
* instance is bound to the sensor driver and must persists as long
* as the driver persists.
* devno - The user specifies which device of this type, from 0.
****************************************************************************/
void sensor_unregister(FAR struct sensor_lowerhalf_s *lower)
void sensor_unregister(FAR struct sensor_lowerhalf_s *lower, int devno)
{
FAR struct sensor_upperhalf_s *upper;
char path[DEVNAME_MAX];
@ -744,7 +743,7 @@ void sensor_unregister(FAR struct sensor_lowerhalf_s *lower)
snprintf(path, DEVNAME_MAX, DEVNAME_FMT,
g_sensor_info[lower->type].name,
lower->uncalibrated ? DEVNAME_UNCAL : "",
upper->idx);
devno);
sninfo("UnRegistering %s\n", path);
unregister_driver(path);

View file

@ -433,7 +433,7 @@ static int wtgahrs2_thread(int argc, FAR char *argv[])
* Public Functions
****************************************************************************/
int wtgahrs2_initialize(FAR const char *path)
int wtgahrs2_initialize(FAR const char *path, int devno)
{
FAR struct wtgahrs2_dev_s *rtdata;
FAR struct wtgahrs2_sensor_s *tmp;
@ -480,7 +480,7 @@ int wtgahrs2_initialize(FAR const char *path)
tmp->lower.ops = &g_wtgahrs2_ops;
tmp->lower.type = SENSOR_TYPE_ACCELEROMETER;
tmp->lower.buffer_bytes = sizeof(struct sensor_event_accel);
ret = sensor_register(&tmp->lower);
ret = sensor_register(&tmp->lower, devno);
if (ret < 0)
{
goto accel_err;
@ -492,7 +492,7 @@ int wtgahrs2_initialize(FAR const char *path)
tmp->lower.ops = &g_wtgahrs2_ops;
tmp->lower.type = SENSOR_TYPE_GYROSCOPE;
tmp->lower.buffer_bytes = sizeof(struct sensor_event_gyro);
ret = sensor_register(&tmp->lower);
ret = sensor_register(&tmp->lower, devno);
if (ret < 0)
{
goto gyro_err;
@ -504,7 +504,7 @@ int wtgahrs2_initialize(FAR const char *path)
tmp->lower.ops = &g_wtgahrs2_ops;
tmp->lower.type = SENSOR_TYPE_MAGNETIC_FIELD;
tmp->lower.buffer_bytes = sizeof(struct sensor_event_mag);
ret = sensor_register(&tmp->lower);
ret = sensor_register(&tmp->lower, devno);
if (ret < 0)
{
goto mag_err;
@ -516,7 +516,7 @@ int wtgahrs2_initialize(FAR const char *path)
tmp->lower.ops = &g_wtgahrs2_ops;
tmp->lower.type = SENSOR_TYPE_BAROMETER;
tmp->lower.buffer_bytes = sizeof(struct sensor_event_baro);
ret = sensor_register(&tmp->lower);
ret = sensor_register(&tmp->lower, devno);
if (ret < 0)
{
goto baro_err;
@ -528,7 +528,7 @@ int wtgahrs2_initialize(FAR const char *path)
tmp->lower.ops = &g_wtgahrs2_ops;
tmp->lower.type = SENSOR_TYPE_GPS;
tmp->lower.buffer_bytes = sizeof(struct sensor_event_gps);
ret = sensor_register(&tmp->lower);
ret = sensor_register(&tmp->lower, devno);
if (ret < 0)
{
goto gps_err;
@ -561,15 +561,15 @@ int wtgahrs2_initialize(FAR const char *path)
return ret;
thr_err:
sensor_unregister(&rtdata->dev[WTGAHRS2_GPS_IDX].lower);
sensor_unregister(&rtdata->dev[WTGAHRS2_GPS_IDX].lower, devno);
gps_err:
sensor_unregister(&rtdata->dev[WTGAHRS2_BARO_IDX].lower);
sensor_unregister(&rtdata->dev[WTGAHRS2_BARO_IDX].lower, devno);
baro_err:
sensor_unregister(&rtdata->dev[WTGAHRS2_MAG_IDX].lower);
sensor_unregister(&rtdata->dev[WTGAHRS2_MAG_IDX].lower, devno);
mag_err:
sensor_unregister(&rtdata->dev[WTGAHRS2_GYRO_IDX].lower);
sensor_unregister(&rtdata->dev[WTGAHRS2_GYRO_IDX].lower, devno);
gyro_err:
sensor_unregister(&rtdata->dev[WTGAHRS2_ACCEL_IDX].lower);
sensor_unregister(&rtdata->dev[WTGAHRS2_ACCEL_IDX].lower, devno);
accel_err:
file_close(&rtdata->file);
open_err:

View file

@ -581,9 +581,11 @@ extern "C"
* numbers. eg: accel0, accel1
*
* Input Parameters:
* dev - A pointer to an instance of lower half sensor driver. This
* instance is bound to the sensor driver and must persists as long
* as the driver persists.
* dev - A pointer to an instance of lower half sensor driver. This
* instance is bound to the sensor driver and must persists as long
* as the driver persists.
* devno - The user specifies which device of this type, from 0. If the
* devno alerady exists, -EEXIST will be returned.
*
* Returned Value:
* OK if the driver was successfully register; A negated errno value is
@ -591,7 +593,7 @@ extern "C"
*
****************************************************************************/
int sensor_register(FAR struct sensor_lowerhalf_s *dev);
int sensor_register(FAR struct sensor_lowerhalf_s *dev, int devno);
/****************************************************************************
* Name: sensor_unregister
@ -601,12 +603,13 @@ int sensor_register(FAR struct sensor_lowerhalf_s *dev);
* upper half driver.
*
* Input Parameters:
* dev - A pointer to an instance of lower half sensor driver. This
* instance is bound to the sensor driver and must persists as long
* as the driver persists.
* dev - A pointer to an instance of lower half sensor driver. This
* instance is bound to the sensor driver and must persists as long
* as the driver persists.
* devno - The user specifies which device of this type, from 0.
****************************************************************************/
void sensor_unregister(FAR struct sensor_lowerhalf_s *dev);
void sensor_unregister(FAR struct sensor_lowerhalf_s *dev, int devno);
#undef EXTERN
#if defined(__cplusplus)

View file

@ -49,13 +49,14 @@ extern "C"
*
* Input Parameters:
* devpath - The full path to the driver to read data source by serial tty.
* devno - The user specifies device number, from 0.
*
* Returned Value:
* OK if the driver was successfully initialize; A negated errno value is
* returned on any failure.
****************************************************************************/
int wtgahrs2_initialize(FAR const char *path);
int wtgahrs2_initialize(FAR const char *path, int devno);
#ifdef __cplusplus
}