drv_led.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-07-19 Magicoe The first version for LPC55S6x
  9. */
  10. #include <rtthread.h>
  11. #include "rtconfig.h"
  12. #include "fsl_common.h"
  13. #include "fsl_iocon.h"
  14. #include "fsl_gpio.h"
  15. #include "fsl_inputmux.h"
  16. #define LED_DEVICE_CTRL 0x81 /*LED control command*/
  17. #define LED_NUM 2
  18. struct led_ctrl
  19. {
  20. uint8_t port;
  21. uint32_t pin;
  22. };
  23. struct lpc_led
  24. {
  25. /* inherit from rt_device */
  26. struct rt_device parent;
  27. struct led_ctrl ctrl[LED_NUM];
  28. };
  29. static struct lpc_led led;
  30. static rt_err_t rt_led_init(rt_device_t dev)
  31. {
  32. gpio_pin_config_t pin_config = {kGPIO_DigitalOutput, 1};
  33. GPIO_PinInit(GPIO, 1, 7, &pin_config);
  34. GPIO_PinInit(GPIO, 1, 6, &pin_config);
  35. led.ctrl[0].pin = 7;
  36. led.ctrl[0].port = 1;
  37. led.ctrl[1].pin = 6;
  38. led.ctrl[1].port = 1;
  39. return RT_EOK;
  40. }
  41. static rt_err_t rt_led_open(rt_device_t dev, rt_uint16_t oflag)
  42. {
  43. return RT_EOK;
  44. }
  45. static rt_err_t rt_led_close(rt_device_t dev)
  46. {
  47. return RT_EOK;
  48. }
  49. static rt_size_t rt_led_read(rt_device_t dev, rt_off_t pos, void *buffer,
  50. rt_size_t size)
  51. {
  52. rt_ubase_t index = 0;
  53. rt_ubase_t nr = size;
  54. rt_uint8_t *value = buffer;
  55. RT_ASSERT(dev == &led.parent);
  56. RT_ASSERT((pos + size) <= LED_NUM);
  57. for (index = 0; index < nr; index++)
  58. {
  59. if(GPIO_PinRead(GPIO, led.ctrl[pos + index].port, led.ctrl[pos + index].pin) == 0)
  60. {
  61. *value = 0;
  62. }
  63. else
  64. {
  65. *value = 1;
  66. }
  67. value++;
  68. }
  69. return index;
  70. }
  71. static rt_size_t rt_led_write(rt_device_t dev, rt_off_t pos,
  72. const void *buffer, rt_size_t size)
  73. {
  74. rt_ubase_t index = 0;
  75. rt_ubase_t nw = size;
  76. const rt_uint8_t *value = buffer;
  77. RT_ASSERT(dev == &led.parent);
  78. RT_ASSERT((pos + size) <= LED_NUM);
  79. for (index = 0; index < nw; index++)
  80. {
  81. if (*value++)
  82. {
  83. GPIO_PinWrite(GPIO, led.ctrl[pos + index].port, led.ctrl[pos + index].pin, 0);
  84. }
  85. else
  86. {
  87. GPIO_PinWrite(GPIO, led.ctrl[pos + index].port, led.ctrl[pos + index].pin, 1);
  88. }
  89. }
  90. return index;
  91. }
  92. static rt_err_t rt_led_control(rt_device_t dev, int cmd, void *args)
  93. {
  94. RT_ASSERT(dev == &led.parent);
  95. if (cmd == LED_DEVICE_CTRL)
  96. {
  97. rt_uint32_t *led_num = args;
  98. *led_num = LED_NUM;
  99. }
  100. return RT_EOK;
  101. }
  102. int rt_hw_led_init(void)
  103. {
  104. led.parent.type = RT_Device_Class_Char;
  105. led.parent.rx_indicate = RT_NULL;
  106. led.parent.tx_complete = RT_NULL;
  107. led.parent.init = rt_led_init;
  108. led.parent.open = rt_led_open;
  109. led.parent.close = rt_led_close;
  110. led.parent.read = rt_led_read;
  111. led.parent.write = rt_led_write;
  112. led.parent.control = rt_led_control;
  113. led.parent.user_data = RT_NULL;
  114. /* register a character device */
  115. rt_device_register(&led.parent, "led", RT_DEVICE_FLAG_RDWR);
  116. /* init led device */
  117. rt_led_init(&led.parent);
  118. return 0;
  119. }
  120. INIT_DEVICE_EXPORT(rt_hw_led_init);
  121. #ifdef RT_USING_FINSH
  122. #include <finsh.h>
  123. #include "msh.h"
  124. void led_test(rt_uint32_t led_num, rt_uint32_t value)
  125. {
  126. rt_uint8_t led_value = value;
  127. rt_led_write(&led.parent, led_num, &led_value, 1);
  128. }
  129. FINSH_FUNCTION_EXPORT(led_test, e.g: led_test(0, 1).);
  130. #endif