sensor.c 13.2 KB
Newer Older
G
guozhanxin 已提交
1
/*
2
 * Copyright (c) 2006-2021, 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
G
guozhanxin 已提交
10 11 12 13
 */

#include "sensor.h"

14 15
#define DBG_TAG  "sensor"
#define DBG_LVL DBG_INFO
G
guozhanxin 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include <rtdbg.h>

#include <string.h>

static char *const sensor_name_str[] =
{
    "none",
    "acce_",     /* Accelerometer     */
    "gyro_",     /* Gyroscope         */
    "mag_",      /* Magnetometer      */
    "temp_",     /* Temperature       */
    "humi_",     /* Relative Humidity */
    "baro_",     /* Barometer         */
    "li_",       /* Ambient light     */
    "pr_",       /* Proximity         */
    "hr_",       /* Heart Rate        */
    "tvoc_",     /* TVOC Level        */
    "noi_",      /* Noise Loudness    */
34
    "step_",     /* Step sensor       */
35 36
    "forc_",     /* Force sensor      */
    "dust_",     /* Dust sensor       */
37
    "eco2_",     /* eCO2 sensor       */
P
Prry 已提交
38
    "gnss_",     /* GPS/GNSS sensor   */
P
Prry 已提交
39
    "tof_"       /* TOF sensor        */
G
guozhanxin 已提交
40 41 42 43 44 45 46 47 48 49 50 51
};

/* Sensor interrupt correlation function */
/*
 * Sensor interrupt handler function
 */
void rt_sensor_cb(rt_sensor_t sen)
{
    if (sen->parent.rx_indicate == RT_NULL)
    {
        return;
    }
52

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

    /* 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));
    }
G
guozhanxin 已提交
63
    else if (sen->config.mode == RT_SENSOR_MODE_INT)
G
guozhanxin 已提交
64 65 66 67
    {
        /* The interrupt mode only produces one data at a time */
        sen->parent.rx_indicate(&sen->parent, 1);
    }
G
guozhanxin 已提交
68
    else if (sen->config.mode == RT_SENSOR_MODE_FIFO)
G
guozhanxin 已提交
69 70 71 72 73 74 75 76
    {
        sen->parent.rx_indicate(&sen->parent, sen->info.fifo_max);
    }
}

/* ISR for sensor interrupt */
static void irq_callback(void *args)
{
77
    rt_sensor_t sensor = (rt_sensor_t)args;
G
guozhanxin 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    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++)
        {
            rt_sensor_cb(sensor->module->sen[i]);
        }
    }
    else
    {
        rt_sensor_cb(sensor);
    }
}

/* Sensor interrupt initialization function */
static rt_err_t rt_sensor_irq_init(rt_sensor_t sensor)
{
    if (sensor->config.irq_pin.pin == RT_PIN_NONE)
    {
        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)
    {
        rt_pin_attach_irq(sensor->config.irq_pin.pin, PIN_IRQ_MODE_RISING, irq_callback, (void *)sensor);
    }
    else if (sensor->config.irq_pin.mode == PIN_MODE_INPUT_PULLUP)
    {
        rt_pin_attach_irq(sensor->config.irq_pin.pin, PIN_IRQ_MODE_FALLING, irq_callback, (void *)sensor);
    }
    else if (sensor->config.irq_pin.mode == PIN_MODE_INPUT)
    {
        rt_pin_attach_irq(sensor->config.irq_pin.pin, PIN_IRQ_MODE_RISING_FALLING, irq_callback, (void *)sensor);
    }

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

    LOG_I("interrupt init success");

    return 0;
}

W
wangqiang 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
// local rt_sensor_ops

static rt_size_t local_fetch_data(struct rt_sensor_device *sensor, void *buf, rt_size_t len)
{
    LOG_D("Undefined fetch_data");
    return 0;
}
static rt_err_t local_control(struct rt_sensor_device *sensor, int cmd, void *arg)
{
    LOG_D("Undefined control");
    return RT_ERROR;
}
static struct rt_sensor_ops local_ops = {
    .fetch_data = local_fetch_data,
    .control = local_control
};

G
guozhanxin 已提交
141
/* RT-Thread Device Interface */
142
static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag)
G
guozhanxin 已提交
143 144 145
{
    rt_sensor_t sensor = (rt_sensor_t)dev;
    RT_ASSERT(dev != RT_NULL);
146
    rt_err_t res = RT_EOK;
W
wangqiang 已提交
147
    rt_err_t (*local_ctrl)(struct rt_sensor_device *sensor, int cmd, void *arg) =  local_control;
G
guozhanxin 已提交
148

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

G
guozhanxin 已提交
155 156 157 158 159 160
    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)
        {
161 162
            res = -RT_ENOMEM;
            goto __exit;
G
guozhanxin 已提交
163 164
        }
    }
W
wangqiang 已提交
165 166 167 168
    if (sensor->ops->control != RT_NULL)
    {
        local_ctrl = sensor->ops->control;
    }
G
guozhanxin 已提交
169

W
wangqiang 已提交
170
    sensor->config.mode = RT_SENSOR_MODE_POLLING;
G
guozhanxin 已提交
171 172
    if (oflag & RT_DEVICE_FLAG_RDONLY && dev->flag & RT_DEVICE_FLAG_RDONLY)
    {
W
wangqiang 已提交
173 174
        /* If polling mode is supported, configure it to polling mode */
        local_ctrl(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_POLLING);
G
guozhanxin 已提交
175 176 177
    }
    else if (oflag & RT_DEVICE_FLAG_INT_RX && dev->flag & RT_DEVICE_FLAG_INT_RX)
    {
W
wangqiang 已提交
178
        /* If interrupt mode is supported, configure it to interrupt mode */
W
wangqiang 已提交
179
        if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_INT) == RT_EOK)
G
guozhanxin 已提交
180
        {
W
wangqiang 已提交
181 182 183
            /* Initialization sensor interrupt */
            rt_sensor_irq_init(sensor);
            sensor->config.mode = RT_SENSOR_MODE_INT;
G
guozhanxin 已提交
184 185
        }
    }
186
    else if (oflag & RT_DEVICE_FLAG_FIFO_RX && dev->flag & RT_DEVICE_FLAG_FIFO_RX)
G
guozhanxin 已提交
187
    {
W
wangqiang 已提交
188
        /* If fifo mode is supported, configure it to fifo mode */
W
wangqiang 已提交
189
        if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_FIFO) == RT_EOK)
G
guozhanxin 已提交
190
        {
W
wangqiang 已提交
191 192 193
            /* Initialization sensor interrupt */
            rt_sensor_irq_init(sensor);
            sensor->config.mode = RT_SENSOR_MODE_FIFO;
G
guozhanxin 已提交
194 195 196 197
        }
    }
    else
    {
198 199
        res = -RT_EINVAL;
        goto __exit;
G
guozhanxin 已提交
200 201 202
    }

    /* Configure power mode to normal mode */
W
wangqiang 已提交
203
    if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_NORMAL) == RT_EOK)
G
guozhanxin 已提交
204
    {
G
guozhanxin 已提交
205
        sensor->config.power = RT_SENSOR_POWER_NORMAL;
G
guozhanxin 已提交
206 207
    }

208
__exit:
G
guozhanxin 已提交
209 210 211 212 213 214
    if (sensor->module)
    {
        /* release the module mutex */
        rt_mutex_release(sensor->module->lock);
    }

215
    return res;
G
guozhanxin 已提交
216 217
}

218
static rt_err_t rt_sensor_close(rt_device_t dev)
G
guozhanxin 已提交
219 220
{
    rt_sensor_t sensor = (rt_sensor_t)dev;
221
    int i;
W
wangqiang 已提交
222
    rt_err_t (*local_ctrl)(struct rt_sensor_device * sensor, int cmd, void *arg) = local_control;
223

G
guozhanxin 已提交
224 225 226 227 228 229
    RT_ASSERT(dev != RT_NULL);

    if (sensor->module)
    {
        rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER);
    }
W
wangqiang 已提交
230
    if (sensor->ops->control != RT_NULL)
231
    {
W
wangqiang 已提交
232
        local_ctrl = sensor->ops->control;
233
    }
W
wangqiang 已提交
234 235

    /* Configure power mode to power down mode */
W
wangqiang 已提交
236
    if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_DOWN) == RT_EOK)
G
guozhanxin 已提交
237
    {
G
guozhanxin 已提交
238
        sensor->config.power = RT_SENSOR_POWER_DOWN;
G
guozhanxin 已提交
239 240
    }

241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    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 ++)
        {
            if (sensor->module->sen[i]->data_buf != RT_NULL)
            {
                rt_free(sensor->module->sen[i]->data_buf);
                sensor->module->sen[i]->data_buf = RT_NULL;
            }
        }
    }
259
    if (sensor->config.mode != RT_SENSOR_MODE_POLLING)
260
    {
261 262 263 264 265
        /* Sensor disable interrupt */
        if (sensor->config.irq_pin.pin != RT_PIN_NONE)
        {
            rt_pin_irq_enable(sensor->config.irq_pin.pin, RT_FALSE);
        }
266
    }
267 268

__exit:
G
guozhanxin 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
    if (sensor->module)
    {
        rt_mutex_release(sensor->module->lock);
    }

    return RT_EOK;
}

static rt_size_t rt_sensor_read(rt_device_t dev, rt_off_t pos, void *buf, rt_size_t len)
{
    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 已提交
307
    else
G
guozhanxin 已提交
308 309
    {
        /* If the buffer is empty read the data */
W
wangqiang 已提交
310 311 312 313
        if (sensor->ops->fetch_data !=  RT_NULL)
        {
            result = sensor->ops->fetch_data(sensor, buf, len);
        }        
G
guozhanxin 已提交
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
    }

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

    return result;
}

static rt_err_t rt_sensor_control(rt_device_t dev, int cmd, void *args)
{
    rt_sensor_t sensor = (rt_sensor_t)dev;
    rt_err_t result = RT_EOK;
    RT_ASSERT(dev != RT_NULL);
W
wangqiang 已提交
329
    rt_err_t (*local_ctrl)(struct rt_sensor_device * sensor, int cmd, void *arg) = local_control;
G
guozhanxin 已提交
330 331 332 333 334

    if (sensor->module)
    {
        rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER);
    }
W
wangqiang 已提交
335 336 337 338
    if (sensor->ops->control != RT_NULL)
    {
        local_ctrl = sensor->ops->control;
    }
G
guozhanxin 已提交
339 340 341

    switch (cmd)
    {
G
guozhanxin 已提交
342
    case RT_SENSOR_CTRL_GET_ID:
G
guozhanxin 已提交
343 344
        if (args)
        {
W
wangqiang 已提交
345
            result = local_ctrl(sensor, RT_SENSOR_CTRL_GET_ID, args);
G
guozhanxin 已提交
346 347
        }
        break;
G
guozhanxin 已提交
348
    case RT_SENSOR_CTRL_GET_INFO:
G
guozhanxin 已提交
349 350 351 352 353
        if (args)
        {
            rt_memcpy(args, &sensor->info, sizeof(struct rt_sensor_info));
        }
        break;
G
guozhanxin 已提交
354
    case RT_SENSOR_CTRL_SET_RANGE:
W
wangqiang 已提交
355 356 357
        /* Configuration measurement range */
        result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_RANGE, args);
        if (result == RT_EOK)
G
guozhanxin 已提交
358
        {
W
wangqiang 已提交
359 360 361
            sensor->config.range = (rt_int32_t)args;
            LOG_D("set range %d", sensor->config.range);
        }    
G
guozhanxin 已提交
362
        break;
G
guozhanxin 已提交
363
    case RT_SENSOR_CTRL_SET_ODR:
W
wangqiang 已提交
364 365 366
        /* Configuration data output rate */
        result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_ODR, args);
        if (result == RT_EOK)
G
guozhanxin 已提交
367
        {
W
wangqiang 已提交
368 369
            sensor->config.odr = (rt_uint32_t)args & 0xFFFF;
            LOG_D("set odr %d", sensor->config.odr);
G
guozhanxin 已提交
370 371
        }
        break;
G
guozhanxin 已提交
372
    case RT_SENSOR_CTRL_SET_POWER:
W
wangqiang 已提交
373 374 375
        /* Configuration sensor power mode */
        result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER, args);
        if (result == RT_EOK)
G
guozhanxin 已提交
376
        {
W
wangqiang 已提交
377 378
            sensor->config.power = (rt_uint32_t)args & 0xFF;
            LOG_D("set power mode code:", sensor->config.power);
G
guozhanxin 已提交
379 380
        }
        break;
G
guozhanxin 已提交
381
    case RT_SENSOR_CTRL_SELF_TEST:
W
wangqiang 已提交
382 383
        /* Device self-test */
        result = local_ctrl(sensor, RT_SENSOR_CTRL_SELF_TEST, args);
G
guozhanxin 已提交
384 385
        break;
    default:
386

387 388
        if (cmd > RT_SENSOR_CTRL_USER_CMD_START)
        {
W
wangqiang 已提交
389 390
            /* Custom commands */
            result = local_ctrl(sensor, cmd, args);
391 392 393 394 395
        }
        else
        {
            result = -RT_ERROR;
        }
396
        break;
G
guozhanxin 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409
    }

    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 =
{
410
    RT_NULL,
G
guozhanxin 已提交
411 412 413 414 415 416 417 418
    rt_sensor_open,
    rt_sensor_close,
    rt_sensor_read,
    RT_NULL,
    rt_sensor_control
};
#endif

W
wangqiang 已提交
419

G
guozhanxin 已提交
420 421 422 423 424 425 426 427 428 429 430 431 432 433
/*
 * sensor register
 */
int rt_hw_sensor_register(rt_sensor_t sensor,
                          const char              *name,
                          rt_uint32_t              flag,
                          void                    *data)
{
    rt_int8_t result;
    rt_device_t device;
    RT_ASSERT(sensor != RT_NULL);

    char *sensor_name = RT_NULL, *device_name = RT_NULL;

W
wangqiang 已提交
434 435 436 437 438
    if (sensor->ops == RT_NULL)
    {
        sensor->ops = &local_ops;
    }

G
guozhanxin 已提交
439 440
    /* Add a type name for the sensor device */
    sensor_name = sensor_name_str[sensor->info.type];
441
    device_name = (char *)rt_calloc(1, rt_strlen(sensor_name) + 1 + rt_strlen(name));
G
guozhanxin 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
    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 */
        sensor->module->lock = rt_mutex_create(name, RT_IPC_FLAG_FIFO);
        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
467
    device->init        = RT_NULL;
G
guozhanxin 已提交
468 469 470 471 472 473 474 475 476 477 478 479 480 481
    device->open        = rt_sensor_open;
    device->close       = rt_sensor_close;
    device->read        = rt_sensor_read;
    device->write       = RT_NULL;
    device->control     = rt_sensor_control;
#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)
    {
482
        LOG_E("rt_sensor[%s] register err code: %d", device_name, result);
W
wangk-ge 已提交
483
        rt_free(device_name);
G
guozhanxin 已提交
484 485 486
        return result;
    }

W
wangk-ge 已提交
487
    rt_free(device_name);
488
    LOG_I("rt_sensor[%s] init success", device_name);
G
guozhanxin 已提交
489 490
    return RT_EOK;
}