Skip to content

Commit 8a1260c

Browse files
authored
[device] Avoid repeated calls device_open interface. (#7476)
1 parent a5211c7 commit 8a1260c

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

src/device.c

+16-12
Original file line numberDiff line numberDiff line change
@@ -233,15 +233,19 @@ rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
233233
return -RT_EBUSY;
234234
}
235235

236-
/* call device_open interface */
237-
if (device_open != RT_NULL)
236+
/* device is not opened or opened by other oflag, call device_open interface */
237+
if (!(dev->open_flag & RT_DEVICE_OFLAG_OPEN) ||
238+
((dev->open_flag & RT_DEVICE_OFLAG_MASK) | (oflag & RT_DEVICE_OFLAG_MASK)))
238239
{
239-
result = device_open(dev, oflag);
240-
}
241-
else
242-
{
243-
/* set open flag */
244-
dev->open_flag = (oflag & RT_DEVICE_OFLAG_MASK);
240+
if (device_open != RT_NULL)
241+
{
242+
result = device_open(dev, oflag);
243+
}
244+
else
245+
{
246+
/* set open flag */
247+
dev->open_flag = (oflag & RT_DEVICE_OFLAG_MASK);
248+
}
245249
}
246250

247251
/* set open flag */
@@ -251,7 +255,7 @@ rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
251255

252256
dev->ref_count++;
253257
/* don't let bad things happen silently. If you are bitten by this assert,
254-
* please set the ref_count to a bigger type. */
258+
* please set the ref_count to a bigger type. */
255259
RT_ASSERT(dev->ref_count != 0);
256260
}
257261

@@ -475,13 +479,13 @@ rt_err_t rt_device_bind_driver(rt_device_t device, rt_driver_t driver, void *nod
475479
}
476480

477481
device->drv = driver;
478-
#ifdef RT_USING_DEVICE_OPS
482+
#ifdef RT_USING_DEVICE_OPS
479483
device->ops = driver->dev_ops;
480-
#endif
484+
#endif
481485
device->dtb_node = node;
482486

483487
return RT_EOK;
484-
}
488+
}
485489
RTM_EXPORT(rt_device_bind_driver);
486490

487491
/**

0 commit comments

Comments
 (0)