1
0

console.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. */
  23. #include <rtthread.h>
  24. struct console_device
  25. {
  26. struct rt_device parent;
  27. rt_device_t device; /* the actual device */
  28. };
  29. struct console_device _console;
  30. /* common device interface */
  31. static rt_err_t console_init(rt_device_t dev)
  32. {
  33. return RT_EOK;
  34. }
  35. static rt_err_t console_open(rt_device_t dev, rt_uint16_t oflag)
  36. {
  37. return RT_EOK;
  38. }
  39. static rt_err_t console_close(rt_device_t dev)
  40. {
  41. return RT_EOK;
  42. }
  43. static rt_size_t console_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  44. {
  45. struct console_device* device;
  46. device = (struct console_device*)dev;
  47. RT_ASSERT(device != RT_NULL);
  48. return rt_device_read(device->device, pos, buffer, size);
  49. }
  50. static rt_size_t console_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  51. {
  52. struct console_device* device;
  53. device = (struct console_device*)dev;
  54. RT_ASSERT(device != RT_NULL);
  55. return rt_device_write(device->device, pos, buffer, size);
  56. }
  57. static rt_err_t console_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  58. {
  59. return rt_device_control(_console.device, cmd, args);
  60. }
  61. void rt_console_init(const char* device_name)
  62. {
  63. rt_device_t device;
  64. /* register to device framework */
  65. device = rt_device_find(device_name);
  66. if (device != RT_NULL)
  67. {
  68. struct console_device* console;
  69. /* get console device */
  70. console = &_console;
  71. rt_memset(console, 0, sizeof(_console));
  72. /* device initialization */
  73. console->parent.type = RT_Device_Class_Char;
  74. /* set device interface */
  75. console->parent.init = console_init;
  76. console->parent.open = console_open;
  77. console->parent.close = console_close;
  78. console->parent.read = console_read;
  79. console->parent.write = console_write;
  80. console->parent.control = console_control;
  81. console->parent.user_data = RT_NULL;
  82. console->device = device;
  83. rt_device_register(&console->parent, "console", RT_DEVICE_FLAG_RDWR);
  84. }
  85. }