RT-Thread Device Interface */
| 143 | |
| 144 | /* RT-Thread Device Interface */ |
| 145 | static rt_err_t _sensor_open(rt_device_t dev, rt_uint16_t oflag) |
| 146 | { |
| 147 | rt_sensor_t sensor = (rt_sensor_t)dev; |
| 148 | RT_ASSERT(dev != RT_NULL); |
| 149 | rt_err_t res = RT_EOK; |
| 150 | rt_err_t (*local_ctrl)(rt_sensor_t sensor, int cmd, void *arg) = _local_control; |
| 151 | |
| 152 | if (sensor->module) |
| 153 | { |
| 154 | /* take the module mutex */ |
| 155 | rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER); |
| 156 | } |
| 157 | |
| 158 | if (sensor->module != RT_NULL && sensor->info.fifo_max > 0 && sensor->data_buf == RT_NULL) |
| 159 | { |
| 160 | /* Allocate memory for the sensor buffer */ |
| 161 | sensor->data_buf = rt_malloc(sizeof(struct rt_sensor_data) * sensor->info.fifo_max); |
| 162 | if (sensor->data_buf == RT_NULL) |
| 163 | { |
| 164 | res = -RT_ENOMEM; |
| 165 | goto __exit; |
| 166 | } |
| 167 | } |
| 168 | if (sensor->ops->control != RT_NULL) |
| 169 | { |
| 170 | local_ctrl = sensor->ops->control; |
| 171 | } |
| 172 | |
| 173 | if (oflag & RT_DEVICE_FLAG_RDONLY && dev->flag & RT_DEVICE_FLAG_RDONLY) |
| 174 | { |
| 175 | /* If polling mode is supported, configure it to polling mode */ |
| 176 | if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_FETCH_MODE, (void *)RT_SENSOR_MODE_FETCH_POLLING) == RT_EOK) |
| 177 | { |
| 178 | RT_SENSOR_MODE_SET_FETCH(sensor->info.mode, RT_SENSOR_MODE_FETCH_POLLING); |
| 179 | } |
| 180 | } |
| 181 | else if (oflag & RT_DEVICE_FLAG_INT_RX && dev->flag & RT_DEVICE_FLAG_INT_RX) |
| 182 | { |
| 183 | /* If interrupt mode is supported, configure it to interrupt mode */ |
| 184 | if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_FETCH_MODE, (void *)RT_SENSOR_MODE_FETCH_INT) == RT_EOK) |
| 185 | { |
| 186 | /* Initialization sensor interrupt */ |
| 187 | _sensor_irq_init(sensor); |
| 188 | RT_SENSOR_MODE_SET_FETCH(sensor->info.mode, RT_SENSOR_MODE_FETCH_INT); |
| 189 | } |
| 190 | } |
| 191 | else if (oflag & RT_DEVICE_FLAG_FIFO_RX && dev->flag & RT_DEVICE_FLAG_FIFO_RX) |
| 192 | { |
| 193 | /* If fifo mode is supported, configure it to fifo mode */ |
| 194 | if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_FETCH_MODE, (void *)RT_SENSOR_MODE_FETCH_FIFO) == RT_EOK) |
| 195 | { |
| 196 | /* Initialization sensor interrupt */ |
| 197 | _sensor_irq_init(sensor); |
| 198 | RT_SENSOR_MODE_SET_FETCH(sensor->info.mode, RT_SENSOR_MODE_FETCH_FIFO); |
| 199 | } |
| 200 | } |
| 201 | else |
| 202 | { |
nothing calls this directly
no test coverage detected