sensor.c 15.7 KB
Newer Older
G
guozhanxin 已提交
1
/*
mysterywolf's avatar
mysterywolf 已提交
2
 * Copyright (c) 2006-2022, RT-Thread Development Team
G
guozhanxin 已提交
3 4 5 6 7 8
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2019-01-31     flybreak     first version
L
luhuadong 已提交
9
 * 2020-02-22     luhuadong    support custom commands
10
 * 2022-12-17     Meco Man     re-implement sensor framework
G
guozhanxin 已提交
11 12
 */

mysterywolf's avatar
mysterywolf 已提交
13
#include <drivers/sensor.h>
G
guozhanxin 已提交
14

15 16
#define DBG_TAG  "sensor"
#define DBG_LVL DBG_INFO
G
guozhanxin 已提交
17 18 19 20 21 22
#include <rtdbg.h>

#include <string.h>

static char *const sensor_name_str[] =
{
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
    "None",
    "ac-",       /* Accelerometer     */
    "gy-",       /* Gyroscope         */
    "ma-",       /* Magnetometer      */
    "tm-",       /* Temperature       */
    "hm-",       /* Relative Humidity */
    "br-",       /* Barometer         */
    "li-",       /* Ambient light     */
    "pr-",       /* Proximity         */
    "hr-",       /* Heart Rate        */
    "tv-",       /* TVOC Level        */
    "ni-",       /* Noise Loudness    */
    "st-",       /* Step sensor       */
    "fr-",       /* Force sensor      */
    "du-",       /* Dust sensor       */
    "ec-",       /* eCO2 sensor       */
    "gn-",       /* GPS/GNSS sensor   */
    "tf-",       /* TOF sensor        */
    "sp-",       /* SpO2 sensor       */
    "ia-",       /* IAQ sensor        */
    "et-",       /* EtOH sensor       */
    "bp-"        /* Blood Pressure    */
G
guozhanxin 已提交
45 46
};

47 48
/* sensor interrupt handler function */
static void _sensor_cb(rt_sensor_t sen)
G
guozhanxin 已提交
49 50 51 52 53
{
    if (sen->parent.rx_indicate == RT_NULL)
    {
        return;
    }
54

55 56 57 58
    if (sen->irq_handle != RT_NULL)
    {
        sen->irq_handle(sen);
    }
G
guozhanxin 已提交
59 60 61 62 63 64

    /* The buffer is not empty. Read the data in the buffer first */
    if (sen->data_len > 0)
    {
        sen->parent.rx_indicate(&sen->parent, sen->data_len / sizeof(struct rt_sensor_data));
    }
65
    else if (RT_SENSOR_MODE_GET_FETCH(sen->info.mode) == RT_SENSOR_MODE_FETCH_INT)
G
guozhanxin 已提交
66 67 68 69
    {
        /* The interrupt mode only produces one data at a time */
        sen->parent.rx_indicate(&sen->parent, 1);
    }
70
    else if (RT_SENSOR_MODE_GET_FETCH(sen->info.mode) == RT_SENSOR_MODE_FETCH_FIFO)
G
guozhanxin 已提交
71 72 73 74 75 76
    {
        sen->parent.rx_indicate(&sen->parent, sen->info.fifo_max);
    }
}

/* ISR for sensor interrupt */
77
static void _irq_callback(void *args)
G
guozhanxin 已提交
78
{
79
    rt_sensor_t sensor = (rt_sensor_t)args;
G
guozhanxin 已提交
80 81 82 83 84 85 86
    rt_uint8_t i;

    if (sensor->module)
    {
        /* Invoke a callback for all sensors in the module */
        for (i = 0; i < sensor->module->sen_num; i++)
        {
87
            _sensor_cb(sensor->module->sen[i]);
G
guozhanxin 已提交
88 89 90 91
        }
    }
    else
    {
92
        _sensor_cb(sensor);
G
guozhanxin 已提交
93 94 95 96
    }
}

/* Sensor interrupt initialization function */
97
static rt_err_t _sensor_irq_init(rt_sensor_t sensor)
G
guozhanxin 已提交
98
{
99
    if (sensor->config.irq_pin.pin == PIN_IRQ_PIN_NONE)
G
guozhanxin 已提交
100 101 102 103 104 105 106 107
    {
        return -RT_EINVAL;
    }

    rt_pin_mode(sensor->config.irq_pin.pin, sensor->config.irq_pin.mode);

    if (sensor->config.irq_pin.mode == PIN_MODE_INPUT_PULLDOWN)
    {
108
        rt_pin_attach_irq(sensor->config.irq_pin.pin, PIN_IRQ_MODE_RISING, _irq_callback, (void *)sensor);
G
guozhanxin 已提交
109 110 111
    }
    else if (sensor->config.irq_pin.mode == PIN_MODE_INPUT_PULLUP)
    {
112
        rt_pin_attach_irq(sensor->config.irq_pin.pin, PIN_IRQ_MODE_FALLING, _irq_callback, (void *)sensor);
G
guozhanxin 已提交
113 114 115
    }
    else if (sensor->config.irq_pin.mode == PIN_MODE_INPUT)
    {
116
        rt_pin_attach_irq(sensor->config.irq_pin.pin, PIN_IRQ_MODE_RISING_FALLING, _irq_callback, (void *)sensor);
G
guozhanxin 已提交
117 118 119 120 121 122 123 124 125
    }

    rt_pin_irq_enable(sensor->config.irq_pin.pin, RT_TRUE);

    LOG_I("interrupt init success");

    return 0;
}

126
/* sensor local ops */
127
static rt_ssize_t _local_fetch_data(rt_sensor_t sensor, rt_sensor_data_t buf, rt_size_t len)
W
wangqiang 已提交
128 129
{
    LOG_D("Undefined fetch_data");
130
    return -RT_EINVAL;
W
wangqiang 已提交
131
}
132
static rt_err_t _local_control(rt_sensor_t sensor, int cmd, void *arg)
W
wangqiang 已提交
133 134
{
    LOG_D("Undefined control");
135
    return -RT_EINVAL;
W
wangqiang 已提交
136
}
S
Sherman 已提交
137 138
static struct rt_sensor_ops local_ops =
{
139 140
    .fetch_data = _local_fetch_data,
    .control = _local_control
W
wangqiang 已提交
141 142
};

G
guozhanxin 已提交
143
/* RT-Thread Device Interface */
144
static rt_err_t _sensor_open(rt_device_t dev, rt_uint16_t oflag)
G
guozhanxin 已提交
145 146 147
{
    rt_sensor_t sensor = (rt_sensor_t)dev;
    RT_ASSERT(dev != RT_NULL);
148
    rt_err_t res = RT_EOK;
149
    rt_err_t (*local_ctrl)(rt_sensor_t sensor, int cmd, void *arg) =  _local_control;
G
guozhanxin 已提交
150

151 152 153 154 155 156
    if (sensor->module)
    {
        /* take the module mutex */
        rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER);
    }

G
guozhanxin 已提交
157 158 159 160 161 162
    if (sensor->module != RT_NULL && sensor->info.fifo_max > 0 && sensor->data_buf == RT_NULL)
    {
        /* Allocate memory for the sensor buffer */
        sensor->data_buf = rt_malloc(sizeof(struct rt_sensor_data) * sensor->info.fifo_max);
        if (sensor->data_buf == RT_NULL)
        {
163 164
            res = -RT_ENOMEM;
            goto __exit;
G
guozhanxin 已提交
165 166
        }
    }
W
wangqiang 已提交
167 168 169 170
    if (sensor->ops->control != RT_NULL)
    {
        local_ctrl = sensor->ops->control;
    }
G
guozhanxin 已提交
171 172 173

    if (oflag & RT_DEVICE_FLAG_RDONLY && dev->flag & RT_DEVICE_FLAG_RDONLY)
    {
W
wangqiang 已提交
174
        /* If polling mode is supported, configure it to polling mode */
175 176 177 178
        if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_FETCH_MODE, (void *)RT_SENSOR_MODE_FETCH_POLLING) == RT_EOK)
        {
            RT_SENSOR_MODE_SET_FETCH(sensor->info.mode, RT_SENSOR_MODE_FETCH_POLLING);
        }
G
guozhanxin 已提交
179 180 181
    }
    else if (oflag & RT_DEVICE_FLAG_INT_RX && dev->flag & RT_DEVICE_FLAG_INT_RX)
    {
W
wangqiang 已提交
182
        /* If interrupt mode is supported, configure it to interrupt mode */
183
        if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_FETCH_MODE, (void *)RT_SENSOR_MODE_FETCH_INT) == RT_EOK)
G
guozhanxin 已提交
184
        {
W
wangqiang 已提交
185
            /* Initialization sensor interrupt */
186
            _sensor_irq_init(sensor);
187
            RT_SENSOR_MODE_SET_FETCH(sensor->info.mode, RT_SENSOR_MODE_FETCH_INT);
G
guozhanxin 已提交
188 189
        }
    }
190
    else if (oflag & RT_DEVICE_FLAG_FIFO_RX && dev->flag & RT_DEVICE_FLAG_FIFO_RX)
G
guozhanxin 已提交
191
    {
W
wangqiang 已提交
192
        /* If fifo mode is supported, configure it to fifo mode */
193
        if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_FETCH_MODE, (void *)RT_SENSOR_MODE_FETCH_FIFO) == RT_EOK)
G
guozhanxin 已提交
194
        {
W
wangqiang 已提交
195
            /* Initialization sensor interrupt */
196
            _sensor_irq_init(sensor);
197
            RT_SENSOR_MODE_SET_FETCH(sensor->info.mode, RT_SENSOR_MODE_FETCH_FIFO);
G
guozhanxin 已提交
198 199 200 201
        }
    }
    else
    {
202 203
        res = -RT_EINVAL;
        goto __exit;
G
guozhanxin 已提交
204 205
    }

206 207
    /* Configure power mode to highest mode */
    if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER_MODE, (void *)RT_SENSOR_MODE_POWER_HIGHEST) == RT_EOK)
G
guozhanxin 已提交
208
    {
209 210 211 212 213 214 215
        RT_SENSOR_MODE_SET_POWER(sensor->info.mode, RT_SENSOR_MODE_POWER_HIGHEST);
    }

    /* Configure accuracy mode to highest mode */
    if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_ACCURACY_MODE, (void *)RT_SENSOR_MODE_ACCURACY_HIGHEST) == RT_EOK)
    {
        RT_SENSOR_MODE_SET_ACCURACY(sensor->info.mode, RT_SENSOR_MODE_ACCURACY_HIGHEST);
G
guozhanxin 已提交
216 217
    }

218
__exit:
G
guozhanxin 已提交
219 220 221 222 223 224
    if (sensor->module)
    {
        /* release the module mutex */
        rt_mutex_release(sensor->module->lock);
    }

225
    return res;
G
guozhanxin 已提交
226 227
}

228
static rt_err_t _sensor_close(rt_device_t dev)
G
guozhanxin 已提交
229 230
{
    rt_sensor_t sensor = (rt_sensor_t)dev;
231
    int i;
232
    rt_err_t (*local_ctrl)(rt_sensor_t sensor, int cmd, void *arg) = _local_control;
233

G
guozhanxin 已提交
234 235 236 237 238 239
    RT_ASSERT(dev != RT_NULL);

    if (sensor->module)
    {
        rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER);
    }
W
wangqiang 已提交
240
    if (sensor->ops->control != RT_NULL)
241
    {
W
wangqiang 已提交
242
        local_ctrl = sensor->ops->control;
243
    }
W
wangqiang 已提交
244 245

    /* Configure power mode to power down mode */
246
    if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER_MODE, (void *)RT_SENSOR_MODE_POWER_DOWN) == RT_EOK)
G
guozhanxin 已提交
247
    {
248
        RT_SENSOR_MODE_SET_POWER(sensor->info.mode, RT_SENSOR_MODE_POWER_DOWN);
G
guozhanxin 已提交
249 250
    }

251 252 253 254 255 256 257 258 259 260 261
    if (sensor->module != RT_NULL && sensor->info.fifo_max > 0 && sensor->data_buf != RT_NULL)
    {
        for (i = 0; i < sensor->module->sen_num; i ++)
        {
            if (sensor->module->sen[i]->parent.ref_count > 0)
                goto __exit;
        }

        /* Free memory for the sensor buffer */
        for (i = 0; i < sensor->module->sen_num; i ++)
        {
262
            if (sensor->module->sen[i]->data_buf)
263 264 265 266 267 268
            {
                rt_free(sensor->module->sen[i]->data_buf);
                sensor->module->sen[i]->data_buf = RT_NULL;
            }
        }
    }
269
    if (RT_SENSOR_MODE_GET_FETCH(sensor->info.mode) != RT_SENSOR_MODE_FETCH_POLLING)
270
    {
271
        /* Sensor disable interrupt */
272
        if (sensor->config.irq_pin.pin != PIN_IRQ_PIN_NONE)
273 274 275
        {
            rt_pin_irq_enable(sensor->config.irq_pin.pin, RT_FALSE);
        }
276
    }
277 278

__exit:
G
guozhanxin 已提交
279 280 281 282 283 284 285 286
    if (sensor->module)
    {
        rt_mutex_release(sensor->module->lock);
    }

    return RT_EOK;
}

287
static rt_size_t _sensor_read(rt_device_t dev, rt_off_t pos, void *buf, rt_size_t len)
G
guozhanxin 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
{
    rt_sensor_t sensor = (rt_sensor_t)dev;
    rt_size_t result = 0;
    RT_ASSERT(dev != RT_NULL);

    if (buf == NULL || len == 0)
    {
        return 0;
    }

    if (sensor->module)
    {
        rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER);
    }

    /* The buffer is not empty. Read the data in the buffer first */
    if (sensor->data_len > 0)
    {
        if (len > sensor->data_len / sizeof(struct rt_sensor_data))
        {
            len = sensor->data_len / sizeof(struct rt_sensor_data);
        }

        rt_memcpy(buf, sensor->data_buf, len * sizeof(struct rt_sensor_data));

        /* Clear the buffer */
        sensor->data_len = 0;
        result = len;
    }
W
wangqiang 已提交
317
    else
G
guozhanxin 已提交
318
    {
319 320
        /* If the buffer is empty, read the data */
        if (sensor->ops->fetch_data)
W
wangqiang 已提交
321 322
        {
            result = sensor->ops->fetch_data(sensor, buf, len);
S
Sherman 已提交
323
        }
G
guozhanxin 已提交
324 325 326 327 328 329 330 331 332 333
    }

    if (sensor->module)
    {
        rt_mutex_release(sensor->module->lock);
    }

    return result;
}

334
static rt_err_t _sensor_control(rt_device_t dev, int cmd, void *args)
G
guozhanxin 已提交
335 336 337 338
{
    rt_sensor_t sensor = (rt_sensor_t)dev;
    rt_err_t result = RT_EOK;
    RT_ASSERT(dev != RT_NULL);
339
    rt_err_t (*local_ctrl)(rt_sensor_t sensor, int cmd, void *arg) = _local_control;
340
    rt_uint8_t mode;
G
guozhanxin 已提交
341 342 343 344 345

    if (sensor->module)
    {
        rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER);
    }
W
wangqiang 已提交
346 347 348 349
    if (sensor->ops->control != RT_NULL)
    {
        local_ctrl = sensor->ops->control;
    }
G
guozhanxin 已提交
350 351 352

    switch (cmd)
    {
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
        case RT_SENSOR_CTRL_GET_ID:
            if (args)
            {
                result = local_ctrl(sensor, RT_SENSOR_CTRL_GET_ID, args);
            }
            break;
        case RT_SENSOR_CTRL_SET_ACCURACY_MODE:
            /* Configuration sensor power mode */
            mode = (rt_uint32_t)args;
            if (!(mode == RT_SENSOR_MODE_ACCURACY_HIGHEST || mode == RT_SENSOR_MODE_ACCURACY_HIGH ||\
                  mode == RT_SENSOR_MODE_ACCURACY_MEDIUM  || mode == RT_SENSOR_MODE_ACCURACY_LOW  ||\
                  mode == RT_SENSOR_MODE_ACCURACY_LOWEST  || mode == RT_SENSOR_MODE_ACCURACY_NOTRUST))
            {
                LOG_E("sensor accuracy mode illegal: %d", mode);
                return -RT_EINVAL;
            }
            result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_ACCURACY_MODE, args);
            if (result == RT_EOK)
            {
                RT_SENSOR_MODE_SET_ACCURACY(sensor->info.mode, (rt_uint32_t)args & 0x0F);
                LOG_D("set accuracy mode code: %d", RT_SENSOR_MODE_GET_ACCURACY(sensor->info.mode));
            }
            break;
        case RT_SENSOR_CTRL_SET_POWER_MODE:
            /* Configuration sensor power mode */
            mode = (rt_uint32_t)args;
            if (!(mode == RT_SENSOR_MODE_POWER_HIGHEST || mode == RT_SENSOR_MODE_POWER_HIGH ||\
                  mode == RT_SENSOR_MODE_POWER_MEDIUM  || mode == RT_SENSOR_MODE_POWER_LOW  ||\
                  mode == RT_SENSOR_MODE_POWER_LOWEST  || mode == RT_SENSOR_MODE_POWER_DOWN))
            {
                LOG_E("sensor power mode illegal: %d", mode);
                return -RT_EINVAL;
            }
            result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER_MODE, args);
            if (result == RT_EOK)
            {
                RT_SENSOR_MODE_SET_POWER(sensor->info.mode, (rt_uint32_t)args & 0x0F);
                LOG_D("set power mode code: %d", RT_SENSOR_MODE_GET_POWER(sensor->info.mode));
            }
            break;
        case RT_SENSOR_CTRL_SET_FETCH_MODE:
            /* Configuration sensor power mode */
            mode = (rt_uint32_t)args;
            if (!(mode == RT_SENSOR_MODE_FETCH_POLLING || mode == RT_SENSOR_MODE_FETCH_INT ||\
                  mode == RT_SENSOR_MODE_FETCH_FIFO))
            {
                LOG_E("sensor fetch data mode illegal: %d", mode);
                return -RT_EINVAL;
            }
            result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_FETCH_MODE, args);
            if (result == RT_EOK)
            {
                RT_SENSOR_MODE_SET_FETCH(sensor->info.mode, (rt_uint32_t)args & 0x0F);
                LOG_D("set fetch mode code: %d", RT_SENSOR_MODE_GET_FETCH(sensor->info.mode));
            }
            break;
        case RT_SENSOR_CTRL_SELF_TEST:
            /* device self test */
            result = local_ctrl(sensor, RT_SENSOR_CTRL_SELF_TEST, args);
            break;
        case RT_SENSOR_CTRL_SOFT_RESET:
            /* device soft reset */
            result = local_ctrl(sensor, RT_SENSOR_CTRL_SOFT_RESET, args);
            break;
        default:
            if (cmd > RT_SENSOR_CTRL_USER_CMD_START)
            {
                /* Custom commands */
                result = local_ctrl(sensor, cmd, args);
            }
            else
            {
                result = -RT_EINVAL;
            }
            break;
G
guozhanxin 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440
    }

    if (sensor->module)
    {
        rt_mutex_release(sensor->module->lock);
    }

    return result;
}

#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops rt_sensor_ops =
{
441
    RT_NULL,
442 443 444
    _sensor_open,
    _sensor_close,
    _sensor_read,
G
guozhanxin 已提交
445
    RT_NULL,
446
    _sensor_control
G
guozhanxin 已提交
447 448 449 450 451 452
};
#endif

/*
 * sensor register
 */
mysterywolf's avatar
mysterywolf 已提交
453 454 455 456
int rt_hw_sensor_register(rt_sensor_t    sensor,
                          const char    *name,
                          rt_uint32_t    flag,
                          void          *data)
G
guozhanxin 已提交
457 458 459 460 461 462 463
{
    rt_int8_t result;
    rt_device_t device;
    RT_ASSERT(sensor != RT_NULL);

    char *sensor_name = RT_NULL, *device_name = RT_NULL;

W
wangqiang 已提交
464 465 466 467 468
    if (sensor->ops == RT_NULL)
    {
        sensor->ops = &local_ops;
    }

G
guozhanxin 已提交
469 470
    /* Add a type name for the sensor device */
    sensor_name = sensor_name_str[sensor->info.type];
471
    device_name = (char *)rt_calloc(1, rt_strlen(sensor_name) + 1 + rt_strlen(name));
G
guozhanxin 已提交
472 473 474 475 476 477 478 479 480 481 482 483
    if (device_name == RT_NULL)
    {
        LOG_E("device_name calloc failed!");
        return -RT_ERROR;
    }

    rt_memcpy(device_name, sensor_name, rt_strlen(sensor_name) + 1);
    strcat(device_name, name);

    if (sensor->module != RT_NULL && sensor->module->lock == RT_NULL)
    {
        /* Create a mutex lock for the module */
484
        sensor->module->lock = rt_mutex_create(name, RT_IPC_FLAG_PRIO);
G
guozhanxin 已提交
485 486 487 488 489 490 491 492 493 494 495 496
        if (sensor->module->lock == RT_NULL)
        {
            rt_free(device_name);
            return -RT_ERROR;
        }
    }

    device = &sensor->parent;

#ifdef RT_USING_DEVICE_OPS
    device->ops         = &rt_sensor_ops;
#else
497
    device->init        = RT_NULL;
498 499 500
    device->open        = _sensor_open;
    device->close       = _sensor_close;
    device->read        = _sensor_read;
G
guozhanxin 已提交
501
    device->write       = RT_NULL;
502
    device->control     = _sensor_control;
G
guozhanxin 已提交
503 504 505 506 507 508 509 510 511
#endif
    device->type        = RT_Device_Class_Sensor;
    device->rx_indicate = RT_NULL;
    device->tx_complete = RT_NULL;
    device->user_data   = data;

    result = rt_device_register(device, device_name, flag | RT_DEVICE_FLAG_STANDALONE);
    if (result != RT_EOK)
    {
512
        LOG_E("sensor[%s] register err code: %d", device_name, result);
W
wangk-ge 已提交
513
        rt_free(device_name);
G
guozhanxin 已提交
514 515 516
        return result;
    }

517
    LOG_I("sensor[%s] init success", device_name);
G
guo 已提交
518
    rt_free(device_name);
519 520 521 522 523

    /* set sensor accuracy and power as the hightest */
    rt_device_control(device, RT_SENSOR_CTRL_SET_ACCURACY_MODE, RT_SENSOR_MODE_ACCURACY_HIGHEST);
    rt_device_control(device, RT_SENSOR_CTRL_SET_POWER_MODE, RT_SENSOR_MODE_POWER_HIGHEST);

G
guozhanxin 已提交
524 525
    return RT_EOK;
}