Skip to content

Commit 6cad23b

Browse files
committed
[kservice] optimize console_device
1 parent ebe05a9 commit 6cad23b

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

src/kservice.c

+11-16
Original file line numberDiff line numberDiff line change
@@ -187,23 +187,15 @@ RTM_EXPORT(rt_console_get_device);
187187
*/
188188
rt_device_t rt_console_set_device(const char *name)
189189
{
190-
rt_device_t new_device, old_device;
190+
rt_device_t old_device = _console_device;
191+
rt_device_t new_device = rt_device_find(name);
191192

192-
/* save old device */
193-
old_device = _console_device;
194-
195-
/* find new console device */
196-
new_device = rt_device_find(name);
197-
198-
/* check whether it's a same device */
199-
if (new_device == old_device) return RT_NULL;
200-
201-
if (new_device != RT_NULL)
193+
if (new_device != RT_NULL && new_device != old_device)
202194
{
203-
if (_console_device != RT_NULL)
195+
if (old_device != RT_NULL)
204196
{
205197
/* close old console device */
206-
rt_device_close(_console_device);
198+
rt_device_close(old_device);
207199
}
208200

209201
/* set new console device */
@@ -319,20 +311,23 @@ static void _console_release(void)
319311
*/
320312
static void _kputs(const char *str, long len)
321313
{
322-
RT_UNUSED(len);
314+
#ifdef RT_USING_DEVICE
315+
rt_device_t console_device = rt_console_get_device();
316+
#endif /* RT_USING_DEVICE */
323317

324318
CONSOLE_TAKE;
325319

326320
#ifdef RT_USING_DEVICE
327-
if (_console_device == RT_NULL)
321+
if (console_device == RT_NULL)
328322
{
329323
rt_hw_console_output(str);
330324
}
331325
else
332326
{
333-
rt_device_write(_console_device, 0, str, len);
327+
rt_device_write(console_device, 0, str, len);
334328
}
335329
#else
330+
RT_UNUSED(len);
336331
rt_hw_console_output(str);
337332
#endif /* RT_USING_DEVICE */
338333

0 commit comments

Comments
 (0)