driver/sensor: replace lower->buffer_size with lower->buffer_number

more efficient

Change-Id: I0823b10248caf75e4dd6a5086ad230ba4a7298f6
Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
This commit is contained in:
Jiuzhu Dong 2021-02-08 16:26:25 +08:00 committed by Alan Carvalho de Assis
parent 6edeb9ebd9
commit 54ea96da6b
5 changed files with 40 additions and 51 deletions

View File

@ -558,8 +558,7 @@ int l3gd20_register(int devno, FAR struct spi_dev_s *spi,
priv->timestamp = 0;
priv->lower.type = SENSOR_TYPE_GYROSCOPE;
priv->lower.buffer_size = sizeof(struct sensor_event_gyro) *
CONFIG_SENSORS_L3GD20_BUFFER_SIZE;
priv->lower.buffer_number = CONFIG_SENSORS_L3GD20_BUFFER_SIZE;
priv->lower.ops = &g_l2gd20_ops;
priv->lower.uncalibrated = true;

View File

@ -195,7 +195,8 @@ static int sensor_open(FAR struct file *filep)
{
/* Initialize sensor buffer */
ret = circbuf_init(&upper->buffer, NULL, lower->buffer_size);
ret = circbuf_init(&upper->buffer, NULL, lower->buffer_number *
upper->esize);
if (ret < 0)
{
goto err;
@ -314,11 +315,12 @@ static ssize_t sensor_read(FAR struct file *filep, FAR char *buffer,
* in buffer is less than the number of bytes origin.
*/
uint32_t buffer_size = lower->buffer_number * upper->esize;
if (upper->latency == 0 &&
circbuf_size(&upper->buffer) > lower->buffer_size &&
circbuf_used(&upper->buffer) <= lower->buffer_size)
circbuf_size(&upper->buffer) > buffer_size &&
circbuf_used(&upper->buffer) <= buffer_size)
{
ret = circbuf_resize(&upper->buffer, lower->buffer_size);
ret = circbuf_resize(&upper->buffer, buffer_size);
}
}
@ -333,7 +335,6 @@ static int sensor_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
FAR struct sensor_upperhalf_s *upper = inode->i_private;
FAR struct sensor_lowerhalf_s *lower = upper->lower;
FAR unsigned int *val = (unsigned int *)(uintptr_t)arg;
size_t bytes;
int ret;
sninfo("cmd=%x arg=%08lx\n", cmd, arg);
@ -400,11 +401,12 @@ static int sensor_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
{
/* Adjust length of buffer in batch mode */
bytes = ROUNDUP(ROUNDUP(*val, upper->interval) /
upper->interval * upper->esize +
lower->buffer_size, upper->esize);
uint32_t buffer_size = (ROUNDUP(*val, upper->interval) /
upper->interval +
lower->buffer_number) *
upper->esize;
ret = circbuf_resize(&upper->buffer, bytes);
ret = circbuf_resize(&upper->buffer, buffer_size);
}
}
}
@ -412,20 +414,16 @@ static int sensor_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
case SNIOC_GET_NEVENTBUF:
{
*val = lower->buffer_size / upper->esize;
*val = lower->buffer_number;
}
break;
case SNIOC_SET_BUFFER_SIZE:
case SNIOC_SET_BUFFER_NUMBER:
{
if (*val != 0)
if (arg != 0)
{
lower->buffer_size = ROUNDUP(*val, upper->esize);
ret = circbuf_resize(&upper->buffer, lower->buffer_size);
if (ret >= 0)
{
*val = lower->buffer_size;
}
lower->buffer_number = arg;
ret = circbuf_resize(&upper->buffer, arg * upper->esize);
}
}
break;
@ -680,13 +678,9 @@ int sensor_custom_register(FAR struct sensor_lowerhalf_s *lower,
if (!lower->ops->fetch)
{
if (!lower->buffer_size)
if (!lower->buffer_number)
{
lower->buffer_size = esize;
}
else
{
lower->buffer_size = ROUNDUP(lower->buffer_size, esize);
lower->buffer_number = 1;
}
lower->push_event = sensor_push_event;
@ -694,7 +688,7 @@ int sensor_custom_register(FAR struct sensor_lowerhalf_s *lower,
else
{
lower->notify_event = sensor_notify_event;
lower->buffer_size = 0;
lower->buffer_number = 0;
}
sninfo("Registering %s\n", path);

View File

@ -479,7 +479,7 @@ int wtgahrs2_initialize(FAR const char *path, int devno)
tmp = &rtdata->dev[WTGAHRS2_ACCEL_IDX];
tmp->lower.ops = &g_wtgahrs2_ops;
tmp->lower.type = SENSOR_TYPE_ACCELEROMETER;
tmp->lower.buffer_size = sizeof(struct sensor_event_accel);
tmp->lower.buffer_number = 1;
ret = sensor_register(&tmp->lower, devno);
if (ret < 0)
{
@ -491,7 +491,7 @@ int wtgahrs2_initialize(FAR const char *path, int devno)
tmp = &rtdata->dev[WTGAHRS2_GYRO_IDX];
tmp->lower.ops = &g_wtgahrs2_ops;
tmp->lower.type = SENSOR_TYPE_GYROSCOPE;
tmp->lower.buffer_size = sizeof(struct sensor_event_gyro);
tmp->lower.buffer_number = 1;
ret = sensor_register(&tmp->lower, devno);
if (ret < 0)
{
@ -503,7 +503,7 @@ int wtgahrs2_initialize(FAR const char *path, int devno)
tmp = &rtdata->dev[WTGAHRS2_MAG_IDX];
tmp->lower.ops = &g_wtgahrs2_ops;
tmp->lower.type = SENSOR_TYPE_MAGNETIC_FIELD;
tmp->lower.buffer_size = sizeof(struct sensor_event_mag);
tmp->lower.buffer_number = 1;
ret = sensor_register(&tmp->lower, devno);
if (ret < 0)
{
@ -515,7 +515,7 @@ int wtgahrs2_initialize(FAR const char *path, int devno)
tmp = &rtdata->dev[WTGAHRS2_BARO_IDX];
tmp->lower.ops = &g_wtgahrs2_ops;
tmp->lower.type = SENSOR_TYPE_BAROMETER;
tmp->lower.buffer_size = sizeof(struct sensor_event_baro);
tmp->lower.buffer_number = 1;
ret = sensor_register(&tmp->lower, devno);
if (ret < 0)
{
@ -527,7 +527,7 @@ int wtgahrs2_initialize(FAR const char *path, int devno)
tmp = &rtdata->dev[WTGAHRS2_GPS_IDX];
tmp->lower.ops = &g_wtgahrs2_ops;
tmp->lower.type = SENSOR_TYPE_GPS;
tmp->lower.buffer_size = sizeof(struct sensor_event_gps);
tmp->lower.buffer_number = 1;
ret = sensor_register(&tmp->lower, devno);
if (ret < 0)
{

View File

@ -261,16 +261,14 @@
#define SNIOC_GET_NEVENTBUF _SNIOC(0x0070)
/* Command: SNIOC_SET_BUFFER_SIZE
* Description: Set size of intermediate circualr buffer in upper half
* driver.
* Argument: This is the size of buffer pointer.
* Note: The application layer can set size of intermediate circualr
* buffer
* by this ioctl command. The size is in bytes, it should be a
* multiple of an event.
/* Command: SNIOC_SET_BUFFER_NUMBER
* Description: Set the number of events intermediate circualr buffer can
* hold in upper half driver.
* Argument: This is the number of events that buffer can hold.
* Note: The application layer can set number of events intermediate
* circualr buffer can hold by this ioctl command.
*/
#define SNIOC_SET_BUFFER_SIZE _SNIOC(0x0071)
#define SNIOC_SET_BUFFER_NUMBER _SNIOC(0x0071)
#endif /* __INCLUDE_NUTTX_SENSORS_IOCTL_H */

View File

@ -573,18 +573,16 @@ struct sensor_lowerhalf_s
int type;
/* The size of the circular buffer used, in bytes units.
* This sensor circular buffer is used to solve the issue where the
* application can't read sensor event in time. If this length of buffer
* is too large, the latency of the sensor event will be too large.
* If the length of buffer is too small, the events will be overwriten
* before the application can read them.
* So, it's recommended to set the size according to sensor ODR. If ODR is
* low, you can set to a length of sensor event. If ODR is high, you can
* set to two or three times the length of sensor event.
/* The number of events that the circular buffer can hold.
* This sensor circular buffer is used to slove issue that application
* can't read sensor event in time. If this number of events is too large,
* the latency of sensor event will be too larage. If the number of events
* is too small, the event will be overwrite before application read them.
* So, it's recommended to set according to sensor odr. If odr is low, you
* can set to one. If odr is high, you can set to two or three.
*/
uint32_t buffer_size;
uint32_t buffer_number;
/* The uncalibrated use to describe whether the sensor event is
* uncalibrated. True is uncalibrated data, false is calibrated data,