dev_led.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /******************************************************************//**
  2. * @file dev_led.c
  3. * @brief LED driver of RT-Thread RTOS for EFM32
  4. * COPYRIGHT (C) 2011, RT-Thread Development Team
  5. * @author Bernard, onelife
  6. * @version 0.4 beta
  7. **********************************************************************
  8. * @section License
  9. * The license and distribution terms for this file may be found in the file LICENSE in this
  10. * distribution or at http://www.rt-thread.org/license/LICENSE
  11. **********************************************************************
  12. * @section Change Logs
  13. * Date Author Notes
  14. * 2009-01-05 Bernard the first version
  15. * 2010-12-27 onelife modify for EFM32
  16. *********************************************************************/
  17. /******************************************************************//**
  18. * @addtogroup efm32
  19. * @{
  20. *********************************************************************/
  21. /* Includes -------------------------------------------------------------------*/
  22. #include "board.h"
  23. #include "dev_led.h"
  24. /* Private typedef -------------------------------------------------------------*/
  25. /* Private define --------------------------------------------------------------*/
  26. /* Private macro --------------------------------------------------------------*/
  27. /* Private constants -----------------------------------------------------------*/
  28. static const rt_uint8_t leds_list[LEDS_MAX_NUMBER][2] = \
  29. {
  30. {LEDS_PIN_PORT_0, LEDS_PIN_NUMBER_0},
  31. {LEDS_PIN_PORT_1, LEDS_PIN_NUMBER_1},
  32. {LEDS_PIN_PORT_2, LEDS_PIN_NUMBER_2},
  33. {LEDS_PIN_PORT_3, LEDS_PIN_NUMBER_3}
  34. };
  35. /* Private variables ------------------------------------------------------------*/
  36. /* Private function prototypes ---------------------------------------------------*/
  37. /* Private functions ------------------------------------------------------------*/
  38. /******************************************************************//**
  39. * @brief
  40. * Turn on a LED
  41. *
  42. * @details
  43. *
  44. * @note
  45. *
  46. * @param[in] num
  47. * LED number
  48. *
  49. *********************************************************************/
  50. void rt_hw_led_on(rt_uint8_t num)
  51. {
  52. RT_ASSERT(num < LEDS_MAX_NUMBER);
  53. GPIO_PinOutSet(leds_list[num][0], leds_list[num][1]);
  54. }
  55. /******************************************************************//**
  56. * @brief
  57. * Turn off a LED
  58. *
  59. * @details
  60. *
  61. * @note
  62. *
  63. * @param[in] num
  64. * LED number
  65. *
  66. *********************************************************************/
  67. void rt_hw_led_off(rt_uint8_t num)
  68. {
  69. RT_ASSERT(num < LEDS_MAX_NUMBER);
  70. GPIO_PinOutClear(leds_list[num][0], leds_list[num][1]);
  71. }
  72. /******************************************************************//**
  73. * @brief
  74. * Toggle the state of a LED
  75. *
  76. * @details
  77. *
  78. * @note
  79. *
  80. * @param[in] num
  81. * LED number
  82. *
  83. *********************************************************************/
  84. void rt_hw_led_toggle(rt_uint8_t num)
  85. {
  86. RT_ASSERT(num < LEDS_MAX_NUMBER);
  87. GPIO_PinOutToggle(leds_list[num][0], leds_list[num][1]);
  88. }
  89. rt_uint8_t rt_hw_led_state(rt_uint8_t num)
  90. {
  91. RT_ASSERT(num < LEDS_MAX_NUMBER);
  92. return (rt_uint8_t)GPIO_PinInGet(leds_list[num][0], leds_list[num][1]);
  93. }
  94. /******************************************************************//**
  95. * @brief
  96. * Initialize the LEDs related GPIO
  97. *
  98. * @details
  99. *
  100. * @note
  101. *
  102. * @return
  103. * Error code
  104. *********************************************************************/
  105. rt_err_t rt_hw_led_init(void)
  106. {
  107. rt_uint8_t i;
  108. /* Configure GPIO */
  109. for (i = 0; i < LEDS_MAX_NUMBER; i++)
  110. {
  111. GPIO_PinModeSet(
  112. leds_list[i][0],
  113. leds_list[i][1],
  114. gpioModePushPull,
  115. 0);
  116. }
  117. return RT_EOK;
  118. }
  119. /*********************************************************************
  120. * Export to FINSH
  121. *********************************************************************/
  122. #ifdef RT_USING_FINSH
  123. #include <finsh.h>
  124. void list_leds(void)
  125. {
  126. rt_uint8_t i;
  127. rt_kprintf(" led \t port \t pin \t state\n");
  128. rt_kprintf(" -----\t -----\t -----\t -----\n");
  129. for (i = 0; i < LEDS_MAX_NUMBER; i++)
  130. {
  131. rt_kprintf(" %d \t %x \t %x \t %x \n",
  132. i, leds_list[i][0], leds_list[i][1], rt_hw_led_state(i));
  133. }
  134. }
  135. FINSH_FUNCTION_EXPORT(list_leds, list all the LEDs.)
  136. void set_led(rt_uint32_t led, rt_uint32_t state)
  137. {
  138. /* set led status */
  139. switch (state)
  140. {
  141. case 0:
  142. rt_hw_led_off(led);
  143. break;
  144. case 1:
  145. rt_hw_led_on(led);
  146. break;
  147. default:
  148. break;
  149. }
  150. }
  151. FINSH_FUNCTION_EXPORT(set_led, turn led (0 - 3) on (1) or off (0).)
  152. #endif
  153. /******************************************************************//**
  154. * @}
  155. *********************************************************************/