drv_led.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. */
  9. #include <rtthread.h>
  10. #include "board.h"
  11. #include "drv_led.h"
  12. /**
  13. *
  14. * LED1 <==> GPIO4[12]
  15. * LED2 <==> GPIO4[13]
  16. *
  17. **/
  18. #define LED_NUM 8
  19. #define LED1_PIN 12
  20. #define LED1_PORT 4
  21. #define LED2_PIN 13
  22. #define LED2_PORT 4
  23. struct led_ctrl
  24. {
  25. uint8_t num;
  26. uint8_t port;
  27. };
  28. struct lpc_led
  29. {
  30. /* inherit from rt_device */
  31. struct rt_device parent;
  32. struct led_ctrl ctrl[LED_NUM];
  33. };
  34. static struct lpc_led led;
  35. static rt_err_t rt_led_init(rt_device_t dev)
  36. {
  37. /* Enable clock and init GPIO outputs */
  38. LPC_CCU1->CLK_M4_GPIO_CFG = CCU_CLK_CFG_AUTO | CCU_CLK_CFG_RUN;
  39. while (!(LPC_CCU1->CLK_M4_GPIO_STAT & CCU_CLK_STAT_RUN));
  40. /* set GPIO4[12] GPIO4[13] as GPIO. */
  41. LPC_SCU->SFSP9_0 = 0; /* GPIO4[12] */
  42. LPC_SCU->SFSP9_1 = 0; /* GPIO4[13] */
  43. /* set GPIO4[12] GPIO4[13] output. */
  44. LPC_GPIO_PORT->DIR[LED1_PORT] |= 0x01 << LED1_PIN;
  45. LPC_GPIO_PORT->DIR[LED2_PORT] |= 0x01 << LED2_PIN;
  46. /* turn off all the led */
  47. LPC_GPIO_PORT->CLR[LED1_PORT] |= 0x01 << LED1_PIN;
  48. LPC_GPIO_PORT->CLR[LED2_PORT] |= 0x01 << LED2_PIN;
  49. led.ctrl[0].num = LED1_PIN;
  50. led.ctrl[0].port = LED1_PORT;
  51. led.ctrl[1].num = LED2_PIN;
  52. led.ctrl[1].port = LED2_PORT;
  53. return RT_EOK;
  54. }
  55. static rt_err_t rt_led_open(rt_device_t dev, rt_uint16_t oflag)
  56. {
  57. return RT_EOK;
  58. }
  59. static rt_err_t rt_led_close(rt_device_t dev)
  60. {
  61. return RT_EOK;
  62. }
  63. static rt_size_t rt_led_read(rt_device_t dev, rt_off_t pos, void *buffer,
  64. rt_size_t size)
  65. {
  66. rt_ubase_t index = 0;
  67. rt_ubase_t nr = size;
  68. rt_uint8_t *value = buffer;
  69. RT_ASSERT(dev == &led.parent);
  70. RT_ASSERT((pos + size) <= LED_NUM);
  71. for (index = 0; index < nr; index++)
  72. {
  73. if ((LPC_GPIO_PORT->PIN[led.ctrl[pos + index].port] & (1 << led.ctrl[pos + index].num)) != 0)
  74. {
  75. *value = 0;
  76. }
  77. else
  78. {
  79. *value = 1;
  80. }
  81. value++;
  82. }
  83. return index;
  84. }
  85. static rt_size_t rt_led_write(rt_device_t dev, rt_off_t pos,
  86. const void *buffer, rt_size_t size)
  87. {
  88. rt_ubase_t index = 0;
  89. rt_ubase_t nw = size;
  90. const rt_uint8_t *value = buffer;
  91. RT_ASSERT(dev == &led.parent);
  92. RT_ASSERT((pos + size) <= LED_NUM);
  93. for (index = 0; index < nw; index++)
  94. {
  95. if (*value++)
  96. {
  97. LPC_GPIO_PORT->CLR[led.ctrl[pos + index].port] = (1 << led.ctrl[pos + index].num);
  98. }
  99. else
  100. {
  101. LPC_GPIO_PORT->SET[led.ctrl[pos + index].port] = (1 << led.ctrl[pos + index].num);
  102. }
  103. }
  104. return index;
  105. }
  106. static rt_err_t rt_led_control(rt_device_t dev, int cmd, void *args)
  107. {
  108. RT_ASSERT(dev == &led.parent);
  109. if (cmd == LED_DEVICE_CTRL)
  110. {
  111. rt_uint32_t *led_num = args;
  112. *led_num = LED_NUM;
  113. }
  114. return RT_EOK;
  115. }
  116. int rt_led_hw_init(void)
  117. {
  118. led.parent.type = RT_Device_Class_Char;
  119. led.parent.rx_indicate = RT_NULL;
  120. led.parent.tx_complete = RT_NULL;
  121. led.parent.init = rt_led_init;
  122. led.parent.open = rt_led_open;
  123. led.parent.close = rt_led_close;
  124. led.parent.read = rt_led_read;
  125. led.parent.write = rt_led_write;
  126. led.parent.control = rt_led_control;
  127. led.parent.user_data = RT_NULL;
  128. /* register a character device */
  129. rt_device_register(&led.parent, "led", RT_DEVICE_FLAG_RDWR);
  130. /* init led device */
  131. rt_led_init(&led.parent);
  132. return 0;
  133. }
  134. INIT_DEVICE_EXPORT(rt_led_hw_init);
  135. #ifdef RT_USING_FINSH
  136. #include <finsh.h>
  137. void led_test(rt_uint32_t led_num, rt_uint32_t value)
  138. {
  139. rt_uint8_t led_value = value;
  140. rt_led_write(&led.parent, led_num, &led_value, 1);
  141. }
  142. FINSH_FUNCTION_EXPORT(led_test, e.g: led_test(0, 100).)
  143. #endif