PageRenderTime 21ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/viewtool-stm32f107/src/stm32_leds.c

https://bitbucket.org/sanyaade/nuttx_boards
C | 311 lines | 140 code | 42 blank | 129 comment | 19 complexity | fee54e5187e4dfa0b2fe3a6ee30849db MD5 | raw file
  1. /****************************************************************************
  2. * configs/viewtool-stm32f107/src/stm32_leds.c
  3. *
  4. * Copyright (C) 2013, 2015 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <stdint.h>
  40. #include <stdbool.h>
  41. #include <debug.h>
  42. #include <nuttx/board.h>
  43. #include <arch/board/board.h>
  44. #include "stm32_gpio.h"
  45. #include "viewtool_stm32f107.h"
  46. /****************************************************************************
  47. * Pre-processor Definitions
  48. ****************************************************************************/
  49. /* CONFIG_DEBUG_LEDS enables debug output from this file (needs CONFIG_DEBUG
  50. * with CONFIG_DEBUG_VERBOSE too)
  51. */
  52. #ifdef CONFIG_DEBUG_LEDS
  53. # define leddbg lldbg
  54. # define ledvdbg llvdbg
  55. #else
  56. # define leddbg(x...)
  57. # define ledvdbg(x...)
  58. #endif
  59. /****************************************************************************
  60. * Private Data
  61. ****************************************************************************/
  62. /****************************************************************************
  63. * Private Functions
  64. ****************************************************************************/
  65. /****************************************************************************
  66. * Name: led_onbits
  67. *
  68. * Description:
  69. * Clear all LEDs to the bit encoded state
  70. *
  71. ****************************************************************************/
  72. static void led_onbits(unsigned int clrbits)
  73. {
  74. if ((clrbits & BOARD_LED1_BIT) != 0)
  75. {
  76. stm32_gpiowrite(GPIO_LED1, false);
  77. }
  78. if ((clrbits & BOARD_LED2_BIT) != 0)
  79. {
  80. stm32_gpiowrite(GPIO_LED2, false);
  81. }
  82. if ((clrbits & BOARD_LED3_BIT) != 0)
  83. {
  84. stm32_gpiowrite(GPIO_LED3, false);
  85. }
  86. if ((clrbits & BOARD_LED4_BIT) != 0)
  87. {
  88. stm32_gpiowrite(GPIO_LED4, false);
  89. }
  90. }
  91. /****************************************************************************
  92. * Name: led_offbits
  93. *
  94. * Description:
  95. * Clear all LEDs to the bit encoded state
  96. *
  97. ****************************************************************************/
  98. static void led_offbits(unsigned int clrbits)
  99. {
  100. if ((clrbits & BOARD_LED1_BIT) != 0)
  101. {
  102. stm32_gpiowrite(GPIO_LED1, true);
  103. }
  104. if ((clrbits & BOARD_LED2_BIT) != 0)
  105. {
  106. stm32_gpiowrite(GPIO_LED2, true);
  107. }
  108. if ((clrbits & BOARD_LED3_BIT) != 0)
  109. {
  110. stm32_gpiowrite(GPIO_LED3, true);
  111. }
  112. if ((clrbits & BOARD_LED4_BIT) != 0)
  113. {
  114. stm32_gpiowrite(GPIO_LED4, true);
  115. }
  116. }
  117. /****************************************************************************
  118. * Public Functions
  119. ****************************************************************************/
  120. /****************************************************************************
  121. * Name: stm32_led_initialize
  122. *
  123. * Description:
  124. * Configure LEDs. LEDs are left in the OFF state.
  125. *
  126. ****************************************************************************/
  127. void stm32_led_initialize(void)
  128. {
  129. /* Configure LED1-4 GPIOs for output. Initial state is OFF */
  130. stm32_configgpio(GPIO_LED1);
  131. stm32_configgpio(GPIO_LED2);
  132. stm32_configgpio(GPIO_LED3);
  133. stm32_configgpio(GPIO_LED4);
  134. }
  135. /****************************************************************************
  136. * Name: board_autoled_on
  137. *
  138. * Description:
  139. * Select the "logical" ON state:
  140. *
  141. * SYMBOL Val Meaning LED state
  142. * LED1 LED2 LED3 LED4
  143. * ----------------- --- ----------------------- ---- ---- ---- ----
  144. * LED_STARTED 0 NuttX has been started ON OFF OFF OFF
  145. * LED_HEAPALLOCATE 1 Heap has been allocated OFF ON OFF OFF
  146. * LED_IRQSENABLED 2 Interrupts enabled ON ON OFF OFF
  147. * LED_STACKCREATED 3 Idle stack created OFF OFF ON OFF
  148. * LED_INIRQ 4 In an interrupt N/C N/C N/C GLOW
  149. * LED_SIGNAL 4 In a signal handler N/C N/C N/C GLOW
  150. * LED_ASSERTION 4 An assertion failed N/C N/C N/C GLOW
  151. * LED_PANIC 4 The system has crashed N/C N/C N/C FLASH
  152. * ED_IDLE MCU is is sleep mode Not used
  153. *
  154. ****************************************************************************/
  155. #ifdef CONFIG_ARCH_LEDS
  156. void board_autoled_on(int led)
  157. {
  158. switch (led)
  159. {
  160. case 0:
  161. led_offbits(BOARD_LED2_BIT | BOARD_LED3_BIT |BOARD_LED4_BIT);
  162. led_onbits(BOARD_LED1_BIT);
  163. break;
  164. case 1:
  165. led_offbits(BOARD_LED1_BIT | BOARD_LED3_BIT |BOARD_LED4_BIT);
  166. led_onbits(BOARD_LED2_BIT);
  167. break;
  168. case 2:
  169. led_offbits(BOARD_LED3_BIT |BOARD_LED4_BIT);
  170. led_onbits(BOARD_LED1_BIT | BOARD_LED2_BIT);
  171. break;
  172. case 3:
  173. led_offbits(BOARD_LED1_BIT | BOARD_LED2_BIT |BOARD_LED4_BIT);
  174. led_onbits(BOARD_LED3_BIT);
  175. break;
  176. case 4:
  177. stm32_gpiowrite(GPIO_LED4, false);
  178. break;
  179. }
  180. }
  181. #endif
  182. /****************************************************************************
  183. * Name: board_autoled_off
  184. *
  185. * Description:
  186. * Select the "logical" OFF state:
  187. *
  188. * SYMBOL Val Meaning LED state
  189. * LED1 LED2 LED3 LED4
  190. * ----------------- --- ----------------------- ---- ---- ---- ----
  191. * LED_STARTED 0 NuttX has been started ON OFF OFF OFF
  192. * LED_HEAPALLOCATE 1 Heap has been allocated OFF ON OFF OFF
  193. * LED_IRQSENABLED 2 Interrupts enabled ON ON OFF OFF
  194. * LED_STACKCREATED 3 Idle stack created OFF OFF ON OFF
  195. * LED_INIRQ 4 In an interrupt N/C N/C N/C GLOW
  196. * LED_SIGNAL 4 In a signal handler N/C N/C N/C GLOW
  197. * LED_ASSERTION 4 An assertion failed N/C N/C N/C GLOW
  198. * LED_PANIC 4 The system has crashed N/C N/C N/C FLASH
  199. * ED_IDLE MCU is is sleep mode Not used
  200. *
  201. ****************************************************************************/
  202. #ifdef CONFIG_ARCH_LEDS
  203. void board_autoled_off(int led)
  204. {
  205. switch (led)
  206. {
  207. case 0:
  208. case 1:
  209. case 2:
  210. case 3:
  211. break;
  212. case 4:
  213. stm32_gpiowrite(GPIO_LED4, true);
  214. break;
  215. }
  216. }
  217. #endif
  218. /************************************************************************************
  219. * Name: board_userled_initialize, board_userled, and board_userled_all
  220. *
  221. * Description:
  222. * These interfaces allow user control of the board LEDs.
  223. *
  224. * If CONFIG_ARCH_LEDS is defined, then NuttX will control both on-board LEDs up
  225. * until the completion of boot. The it will continue to control LED2; LED1 is
  226. * avaiable for application use.
  227. *
  228. * If CONFIG_ARCH_LEDS is not defined, then both LEDs are available for application
  229. * use.
  230. *
  231. ************************************************************************************/
  232. void board_userled_initialize(void)
  233. {
  234. /* Already initialized by stm32_led_initialize */
  235. }
  236. void board_userled(int led, bool ledon)
  237. {
  238. uint32_t pinset;
  239. switch (led)
  240. {
  241. case BOARD_LED1:
  242. pinset = GPIO_LED1;
  243. break;
  244. case BOARD_LED2:
  245. pinset = GPIO_LED2;
  246. break;
  247. case BOARD_LED3:
  248. pinset = GPIO_LED3;
  249. break;
  250. case BOARD_LED4:
  251. #ifndef CONFIG_ARCH_LEDS
  252. pinset = GPIO_LED4;
  253. break;
  254. #endif
  255. default:
  256. return;
  257. }
  258. stm32_gpiowrite(pinset, !ledon);
  259. }
  260. void board_userled_all(uint8_t ledset)
  261. {
  262. #ifdef CONFIG_ARCH_LEDS
  263. led_onbits(ledset & ~BOARD_LED4_BIT);
  264. led_offbits(~(ledset | BOARD_LED4_BIT));
  265. #else
  266. led_onbits(ledset);
  267. led_offbits(~ledset);
  268. #endif
  269. }