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

Function rt_sensor_open

components/drivers/sensor/v1/sensor.c:150–224  ·  view source on GitHub ↗

RT-Thread Device Interface */

Source from the content-addressed store, hash-verified

148
149/* RT-Thread Device Interface */
150static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag)
151{
152 rt_sensor_t sensor = (rt_sensor_t)dev;
153 RT_ASSERT(dev != RT_NULL);
154 rt_err_t res = RT_EOK;
155 rt_err_t (*local_ctrl)(struct rt_sensor_device * sensor, int cmd, void *arg) = local_control;
156
157 if (sensor->module)
158 {
159 /* take the module mutex */
160 rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER);
161 }
162
163 if (sensor->module != RT_NULL && sensor->info.fifo_max > 0 && sensor->data_buf == RT_NULL)
164 {
165 /* Allocate memory for the sensor buffer */
166 sensor->data_buf = rt_malloc(sizeof(struct rt_sensor_data) * sensor->info.fifo_max);
167 if (sensor->data_buf == RT_NULL)
168 {
169 res = -RT_ENOMEM;
170 goto __exit;
171 }
172 }
173 if (sensor->ops->control != RT_NULL)
174 {
175 local_ctrl = sensor->ops->control;
176 }
177
178 sensor->config.mode = RT_SENSOR_MODE_POLLING;
179 if (oflag & RT_DEVICE_FLAG_RDONLY && dev->flag & RT_DEVICE_FLAG_RDONLY)
180 {
181 /* If polling mode is supported, configure it to polling mode */
182 local_ctrl(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_POLLING);
183 }
184 else if (oflag & RT_DEVICE_FLAG_INT_RX && dev->flag & RT_DEVICE_FLAG_INT_RX)
185 {
186 /* If interrupt mode is supported, configure it to interrupt mode */
187 if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_INT) == RT_EOK)
188 {
189 /* Initialization sensor interrupt */
190 rt_sensor_irq_init(sensor);
191 sensor->config.mode = RT_SENSOR_MODE_INT;
192 }
193 }
194 else if (oflag & RT_DEVICE_FLAG_FIFO_RX && dev->flag & RT_DEVICE_FLAG_FIFO_RX)
195 {
196 /* If fifo mode is supported, configure it to fifo mode */
197 if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_FIFO) == RT_EOK)
198 {
199 /* Initialization sensor interrupt */
200 rt_sensor_irq_init(sensor);
201 sensor->config.mode = RT_SENSOR_MODE_FIFO;
202 }
203 }
204 else
205 {
206 res = -RT_EINVAL;
207 goto __exit;

Callers

nothing calls this directly

Calls 4

rt_mutex_takeFunction · 0.85
rt_mallocFunction · 0.85
rt_sensor_irq_initFunction · 0.85
rt_mutex_releaseFunction · 0.85

Tested by

no test coverage detected