1
0

console.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * File : console.c
  3. * This file is part of Device File System in RT-Thread RTOS
  4. * COPYRIGHT (C) 2004-2011, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2015.03.27 Bernard Add author information.
  23. */
  24. #include <rtthread.h>
  25. struct console_device
  26. {
  27. struct rt_device parent;
  28. rt_device_t device; /* the actual device */
  29. };
  30. struct console_device _console;
  31. /* common device interface */
  32. static rt_err_t console_init(rt_device_t dev)
  33. {
  34. return RT_EOK;
  35. }
  36. static rt_err_t console_open(rt_device_t dev, rt_uint16_t oflag)
  37. {
  38. return RT_EOK;
  39. }
  40. static rt_err_t console_close(rt_device_t dev)
  41. {
  42. return RT_EOK;
  43. }
  44. static rt_size_t console_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  45. {
  46. struct console_device* device;
  47. device = (struct console_device*)dev;
  48. RT_ASSERT(device != RT_NULL);
  49. return rt_device_read(device->device, pos, buffer, size);
  50. }
  51. static rt_size_t console_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  52. {
  53. struct console_device* device;
  54. device = (struct console_device*)dev;
  55. RT_ASSERT(device != RT_NULL);
  56. return rt_device_write(device->device, pos, buffer, size);
  57. }
  58. static rt_err_t console_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  59. {
  60. return rt_device_control(_console.device, cmd, args);
  61. }
  62. void rt_console_init(const char* device_name)
  63. {
  64. rt_device_t device;
  65. /* register to device framework */
  66. device = rt_device_find(device_name);
  67. if (device != RT_NULL)
  68. {
  69. struct console_device* console;
  70. /* get console device */
  71. console = &_console;
  72. rt_memset(console, 0, sizeof(_console));
  73. /* device initialization */
  74. console->parent.type = RT_Device_Class_Char;
  75. /* set device interface */
  76. console->parent.init = console_init;
  77. console->parent.open = console_open;
  78. console->parent.close = console_close;
  79. console->parent.read = console_read;
  80. console->parent.write = console_write;
  81. console->parent.control = console_control;
  82. console->parent.user_data = RT_NULL;
  83. console->device = device;
  84. rt_device_register(&console->parent, "console", RT_DEVICE_FLAG_RDWR);
  85. }
  86. }