led.c 1.5 KB

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