led.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include <rtthread.h>
  2. #include <stm32f10x.h>
  3. #define RCC_APB2Periph_GPIO_LED RCC_APB2Periph_GPIOF
  4. #define GPIO_LED GPIOF
  5. #define GPIO_Pin_LED GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9
  6. static const rt_uint16_t led_map[] = {GPIO_Pin_6, GPIO_Pin_7, GPIO_Pin_8, GPIO_Pin_9};
  7. static rt_uint8_t led_inited = 0;
  8. static void GPIO_Configuration(void)
  9. {
  10. GPIO_InitTypeDef GPIO_InitStructure;
  11. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_LED;
  12. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  13. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  14. GPIO_Init(GPIO_LED, &GPIO_InitStructure);
  15. }
  16. void LED_Configuration(void)
  17. {
  18. /* enable led clock */
  19. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_LED, ENABLE);
  20. GPIO_Configuration();
  21. }
  22. void rt_hw_led_init()
  23. {
  24. /* init led configuration if it's not inited. */
  25. if (!led_inited)
  26. {
  27. LED_Configuration();
  28. led_inited = 1;
  29. }
  30. }
  31. void rt_hw_led_on(rt_uint32_t led)
  32. {
  33. if (led < sizeof(led_map)/sizeof(rt_uint16_t))
  34. GPIO_SetBits(GPIO_LED, led_map[led]);
  35. }
  36. void rt_hw_led_off(rt_uint32_t led)
  37. {
  38. if (led < sizeof(led_map)/sizeof(rt_uint16_t))
  39. GPIO_ResetBits(GPIO_LED, led_map[led]);
  40. }
  41. #ifdef RT_USING_FINSH
  42. #include <finsh.h>
  43. void led(rt_uint32_t led, rt_uint32_t value)
  44. {
  45. /* init led configuration if it's not inited. */
  46. if (!led_inited)
  47. {
  48. LED_Configuration();
  49. led_inited = 1;
  50. }
  51. /* set led status */
  52. if (value)
  53. rt_hw_led_on(led);
  54. else
  55. rt_hw_led_off(led);
  56. }
  57. FINSH_FUNCTION_EXPORT(led, set led[0 - 3] on[1] or off[0].)
  58. #endif