rt_null.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) 2011-2020, Shanghai Real-Thread Electronic Technology Co.,Ltd
  3. *
  4. * Change Logs:
  5. * Date Author Notes
  6. * 2020-12-03 quanzhao the first version
  7. */
  8. #include <time.h>
  9. #include <string.h>
  10. #include <rtthread.h>
  11. static struct rt_device null_dev;
  12. static rt_size_t null_read (rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  13. {
  14. rt_memset(buffer, 0, size);
  15. return size;
  16. }
  17. static rt_size_t null_write (rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  18. {
  19. return size;
  20. }
  21. static rt_err_t null_control (rt_device_t dev, int cmd, void *args)
  22. {
  23. return RT_EOK;
  24. }
  25. #ifdef RT_USING_DEVICE_OPS
  26. const static struct rt_device_ops null_ops =
  27. {
  28. RT_NULL,
  29. RT_NULL,
  30. RT_NULL,
  31. null_read,
  32. null_write,
  33. null_control
  34. };
  35. #endif
  36. int null_device_init(void)
  37. {
  38. static rt_bool_t init_ok = RT_FALSE;
  39. if (init_ok)
  40. {
  41. return 0;
  42. }
  43. RT_ASSERT(!rt_device_find("null"));
  44. null_dev.type = RT_Device_Class_Miscellaneous;
  45. #ifdef RT_USING_DEVICE_OPS
  46. null_dev.ops = &null_ops;
  47. #else
  48. null_dev.init = RT_NULL;
  49. null_dev.open = RT_NULL;
  50. null_dev.close = RT_NULL;
  51. null_dev.read = null_read;
  52. null_dev.write = null_write;
  53. null_dev.control = null_control;
  54. #endif
  55. /* no private */
  56. null_dev.user_data = RT_NULL;
  57. rt_device_register(&null_dev, "null", RT_DEVICE_FLAG_RDWR);
  58. init_ok = RT_TRUE;
  59. return 0;
  60. }
  61. INIT_DEVICE_EXPORT(null_device_init);