MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / rt_hw_sensor_register

Function rt_hw_sensor_register

components/drivers/sensor/v2/sensor.c:454–522  ·  view source on GitHub ↗

* sensor register */

Source from the content-addressed store, hash-verified

452 * sensor register
453 */
454int rt_hw_sensor_register(rt_sensor_t sensor,
455 const char *name,
456 rt_uint32_t flag,
457 void *data)
458{
459 rt_int8_t result;
460 rt_device_t device;
461 RT_ASSERT(sensor != RT_NULL);
462
463 char *sensor_name = RT_NULL, *device_name = RT_NULL;
464
465 if (sensor->ops == RT_NULL)
466 {
467 sensor->ops = &local_ops;
468 }
469
470 /* Add a type name for the sensor device */
471 sensor_name = sensor_name_str[sensor->info.type];
472 device_name = (char *)rt_calloc(1, rt_strlen(sensor_name) + 1 + rt_strlen(name));
473 if (device_name == RT_NULL)
474 {
475 LOG_E("device_name calloc failed!");
476 return -RT_ERROR;
477 }
478
479 rt_memcpy(device_name, sensor_name, rt_strlen(sensor_name) + 1);
480 strcat(device_name, name);
481
482 if (sensor->module != RT_NULL && sensor->module->lock == RT_NULL)
483 {
484 /* Create a mutex lock for the module */
485 sensor->module->lock = rt_mutex_create(name, RT_IPC_FLAG_PRIO);
486 if (sensor->module->lock == RT_NULL)
487 {
488 rt_free(device_name);
489 return -RT_ERROR;
490 }
491 }
492
493 device = &sensor->parent;
494
495#ifdef RT_USING_DEVICE_OPS
496 device->ops = &rt_sensor_ops;
497#else
498 device->init = RT_NULL;
499 device->open = _sensor_open;
500 device->close = _sensor_close;
501 device->read = _sensor_read;
502 device->write = RT_NULL;
503 device->control = _sensor_control;
504#endif
505 device->type = RT_Device_Class_Sensor;
506 device->rx_indicate = RT_NULL;
507 device->tx_complete = RT_NULL;
508 device->user_data = data;
509
510 result = rt_device_register(device, device_name, flag | RT_DEVICE_FLAG_STANDALONE);
511 if (result != RT_EOK)

Callers 5

rt_hw_max31875_temp_initFunction · 0.50
rt_hw_bmx055_accel_initFunction · 0.50
rt_hw_bmx055_gyro_initFunction · 0.50
rt_hw_bmx055_mag_initFunction · 0.50
rt_hw_nct7717u_temp_initFunction · 0.50

Calls 6

rt_callocFunction · 0.85
rt_strlenFunction · 0.85
rt_memcpyFunction · 0.85
rt_mutex_createFunction · 0.85
rt_freeFunction · 0.85
rt_device_registerFunction · 0.85

Tested by

no test coverage detected