dev_led.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. * 2011-05-06 onelife Add EFM32 development kit support
  17. *********************************************************************/
  18. /******************************************************************//**
  19. * @addtogroup efm32
  20. * @{
  21. *********************************************************************/
  22. /* Includes -------------------------------------------------------------------*/
  23. #include "board.h"
  24. #include "dev_led.h"
  25. /* Private typedef -------------------------------------------------------------*/
  26. /* Private define --------------------------------------------------------------*/
  27. /* Private macro --------------------------------------------------------------*/
  28. /* Private constants -----------------------------------------------------------*/
  29. #if defined(EFM32_G890_STK)
  30. static const rt_uint8_t leds_list[LEDS_MAX_NUMBER][2] = \
  31. {
  32. {LEDS_PIN_PORT_0, LEDS_PIN_NUMBER_0},
  33. {LEDS_PIN_PORT_1, LEDS_PIN_NUMBER_1},
  34. {LEDS_PIN_PORT_2, LEDS_PIN_NUMBER_2},
  35. {LEDS_PIN_PORT_3, LEDS_PIN_NUMBER_3}
  36. };
  37. #endif
  38. /* Private variables ------------------------------------------------------------*/
  39. /* Private function prototypes ---------------------------------------------------*/
  40. /* Private functions ------------------------------------------------------------*/
  41. /******************************************************************//**
  42. * @brief
  43. * Turn on a LED
  44. *
  45. * @details
  46. *
  47. * @note
  48. *
  49. * @param[in] num
  50. * LED number
  51. *
  52. *********************************************************************/
  53. void rt_hw_led_on(rt_uint8_t num)
  54. {
  55. RT_ASSERT(num < LEDS_MAX_NUMBER);
  56. #if defined(EFM32_G890_STK)
  57. GPIO_PinOutSet(leds_list[num][0], leds_list[num][1]);
  58. #elif defined(EFM32_G290_DK)
  59. {
  60. rt_uint16_t leds;
  61. leds = DVK_getLEDs() | (rt_uint16_t)(1 << num);
  62. DVK_setLEDs(leds);
  63. }
  64. #endif
  65. }
  66. /******************************************************************//**
  67. * @brief
  68. * Turn off a LED
  69. *
  70. * @details
  71. *
  72. * @note
  73. *
  74. * @param[in] num
  75. * LED number
  76. *
  77. *********************************************************************/
  78. void rt_hw_led_off(rt_uint8_t num)
  79. {
  80. RT_ASSERT(num < LEDS_MAX_NUMBER);
  81. #if defined(EFM32_G890_STK)
  82. GPIO_PinOutClear(leds_list[num][0], leds_list[num][1]);
  83. #elif defined(EFM32_G290_DK)
  84. {
  85. rt_uint16_t leds;
  86. leds = DVK_getLEDs() & ~(rt_uint16_t)(1 << num);
  87. DVK_setLEDs(leds);
  88. }
  89. #endif
  90. }
  91. /******************************************************************//**
  92. * @brief
  93. * Toggle the state of a LED
  94. *
  95. * @details
  96. *
  97. * @note
  98. *
  99. * @param[in] num
  100. * LED number
  101. *
  102. *********************************************************************/
  103. void rt_hw_led_toggle(rt_uint8_t num)
  104. {
  105. RT_ASSERT(num < LEDS_MAX_NUMBER);
  106. #if defined(EFM32_G890_STK)
  107. GPIO_PinOutToggle(leds_list[num][0], leds_list[num][1]);
  108. #elif defined(EFM32_G290_DK)
  109. {
  110. rt_uint16_t leds;
  111. leds = DVK_getLEDs() ^ (rt_uint16_t)(1 << num);
  112. DVK_setLEDs(leds);
  113. }
  114. #endif
  115. }
  116. rt_uint8_t rt_hw_led_state(rt_uint8_t num)
  117. {
  118. RT_ASSERT(num < LEDS_MAX_NUMBER);
  119. #if defined(EFM32_G890_STK)
  120. return (rt_uint8_t)GPIO_PinInGet(leds_list[num][0], leds_list[num][1]);
  121. #elif defined(EFM32_G290_DK)
  122. return ((DVK_getLEDs() & (rt_uint16_t)(1 << num)) >> num);
  123. #endif
  124. }
  125. /******************************************************************//**
  126. * @brief
  127. * Initialize the LEDs related GPIO
  128. *
  129. * @details
  130. *
  131. * @note
  132. *
  133. * @return
  134. * Error code
  135. *********************************************************************/
  136. rt_err_t rt_hw_led_init(void)
  137. {
  138. #if defined(EFM32_G890_STK)
  139. rt_uint8_t i;
  140. /* Configure GPIO */
  141. for (i = 0; i < LEDS_MAX_NUMBER; i++)
  142. {
  143. GPIO_PinModeSet(
  144. leds_list[i][0],
  145. leds_list[i][1],
  146. gpioModePushPull,
  147. 0);
  148. }
  149. #endif
  150. return RT_EOK;
  151. }
  152. /*********************************************************************
  153. * Export to FINSH
  154. *********************************************************************/
  155. #ifdef RT_USING_FINSH
  156. #include <finsh.h>
  157. void list_leds(void)
  158. {
  159. rt_uint8_t i;
  160. rt_kprintf(" led \t port \t pin \t state\n");
  161. rt_kprintf(" -----\t -----\t -----\t -----\n");
  162. for (i = 0; i < LEDS_MAX_NUMBER; i++)
  163. {
  164. #if defined(EFM32_G890_STK)
  165. rt_kprintf(" %d \t %x \t %x \t %x \n",
  166. i, leds_list[i][0], leds_list[i][1], rt_hw_led_state(i));
  167. #elif defined(EFM32_G290_DK)
  168. rt_uint16_t leds;
  169. leds = DVK_getLEDs();
  170. rt_kprintf(" %d \t FPGA \t FPGA \t %x \n",
  171. i, (leds & (1 << i))? 1 : 0);
  172. #endif
  173. }
  174. }
  175. FINSH_FUNCTION_EXPORT(list_leds, list all the LEDs.)
  176. void set_led(rt_uint32_t led, rt_uint32_t state)
  177. {
  178. /* set led status */
  179. switch (state)
  180. {
  181. case 0:
  182. rt_hw_led_off(led);
  183. break;
  184. case 1:
  185. rt_hw_led_on(led);
  186. break;
  187. default:
  188. break;
  189. }
  190. }
  191. FINSH_FUNCTION_EXPORT(set_led, turn led (0 - 3) on (1) or off (0).)
  192. #endif
  193. /******************************************************************//**
  194. * @}
  195. *********************************************************************/