led.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * File : led.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2009-01-05 Bernard the first version
  13. */
  14. #include <rtthread.h>
  15. #include <stm32f10x.h>
  16. // led define
  17. #ifdef STM32_SIMULATOR
  18. #define led1_rcc RCC_APB2Periph_GPIOA
  19. #define led1_gpio GPIOA
  20. #define led1_pin (GPIO_Pin_5)
  21. #define led2_rcc RCC_APB2Periph_GPIOA
  22. #define led2_gpio GPIOA
  23. #define led2_pin (GPIO_Pin_6)
  24. #else
  25. #define led1_rcc RCC_APB2Periph_GPIOE
  26. #define led1_gpio GPIOE
  27. #define led1_pin (GPIO_Pin_2)
  28. #define led2_rcc RCC_APB2Periph_GPIOE
  29. #define led2_gpio GPIOE
  30. #define led2_pin (GPIO_Pin_3)
  31. #endif // led define #ifdef STM32_SIMULATOR
  32. void rt_hw_led_init(void)
  33. {
  34. GPIO_InitTypeDef GPIO_InitStructure;
  35. RCC_APB2PeriphClockCmd(led1_rcc|led2_rcc,ENABLE);
  36. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  37. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  38. GPIO_InitStructure.GPIO_Pin = led1_pin;
  39. GPIO_Init(led1_gpio, &GPIO_InitStructure);
  40. GPIO_InitStructure.GPIO_Pin = led2_pin;
  41. GPIO_Init(led2_gpio, &GPIO_InitStructure);
  42. }
  43. void rt_hw_led_on(rt_uint32_t n)
  44. {
  45. switch (n)
  46. {
  47. case 0:
  48. GPIO_SetBits(led1_gpio, led1_pin);
  49. break;
  50. case 1:
  51. GPIO_SetBits(led2_gpio, led2_pin);
  52. break;
  53. default:
  54. break;
  55. }
  56. }
  57. void rt_hw_led_off(rt_uint32_t n)
  58. {
  59. switch (n)
  60. {
  61. case 0:
  62. GPIO_ResetBits(led1_gpio, led1_pin);
  63. break;
  64. case 1:
  65. GPIO_ResetBits(led2_gpio, led2_pin);
  66. break;
  67. default:
  68. break;
  69. }
  70. }
  71. #ifdef RT_USING_FINSH
  72. #include <finsh.h>
  73. static rt_uint8_t led_inited = 0;
  74. void led(rt_uint32_t led, rt_uint32_t value)
  75. {
  76. /* init led configuration if it's not inited. */
  77. if (!led_inited)
  78. {
  79. rt_hw_led_init();
  80. led_inited = 1;
  81. }
  82. if ( led == 0 )
  83. {
  84. /* set led status */
  85. switch (value)
  86. {
  87. case 0:
  88. rt_hw_led_off(0);
  89. break;
  90. case 1:
  91. rt_hw_led_on(0);
  92. break;
  93. default:
  94. break;
  95. }
  96. }
  97. if ( led == 1 )
  98. {
  99. /* set led status */
  100. switch (value)
  101. {
  102. case 0:
  103. rt_hw_led_off(1);
  104. break;
  105. case 1:
  106. rt_hw_led_on(1);
  107. break;
  108. default:
  109. break;
  110. }
  111. }
  112. }
  113. FINSH_FUNCTION_EXPORT(led, set led[0 - 1] on[1] or off[0].)
  114. #endif