drv_led.c 3.8 KB

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