1
0

dev_led.c 6.0 KB

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