PageRenderTime 28ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/configs/spark/src/stm32_autoleds.c

https://bitbucket.org/hg42/nuttx
C | 287 lines | 137 code | 57 blank | 93 comment | 16 complexity | 0301f3b12493028a91fc7023bf0c8a4d MD5 | raw file
Possible License(s): 0BSD
  1. /****************************************************************************
  2. * configs/spark/src/stm32_autoleds.c
  3. *
  4. * Copyright (C) 2012-2013, 2015 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. * David Sidrane <david_s5@nscdg.com>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * 3. Neither the name NuttX nor the names of its contributors may be
  19. * used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ****************************************************************************/
  36. /****************************************************************************
  37. * Included Files
  38. ****************************************************************************/
  39. #include <nuttx/config.h>
  40. #include <stdint.h>
  41. #include <stdbool.h>
  42. #include <debug.h>
  43. #include <nuttx/board.h>
  44. #include <arch/board/board.h>
  45. #include "chip.h"
  46. #include "up_arch.h"
  47. #include "up_internal.h"
  48. #include "stm32.h"
  49. #include "spark.h"
  50. #ifdef CONFIG_ARCH_LEDS
  51. /****************************************************************************
  52. * Pre-processor Definitions
  53. ****************************************************************************/
  54. /* The following definitions map the encoded LED setting to bit sets used to
  55. * manipulate the LEDs. All terms are in true logic, the led polarity is dealt
  56. * with in the phy_xxx operations
  57. */
  58. #define ON_SETBITS_SHIFT (0)
  59. #define ON_CLRBITS_SHIFT (4)
  60. #define OFF_SETBITS_SHIFT (8)
  61. #define OFF_CLRBITS_SHIFT (12)
  62. #define ON_BITS(v) ((v) & 0xff)
  63. #define OFF_BITS(v) (((v) >> 8) & 0x0ff)
  64. #define SETBITS(b) ((b) & 0x0f)
  65. #define CLRBITS(b) (((b) >> 4) & 0x0f)
  66. #define ON_SETBITS(v) (SETBITS(ON_BITS(v))
  67. #define ON_CLRBITS(v) (CLRBITS(ON_BITS(v))
  68. #define OFF_SETBITS(v) (SETBITS(OFF_BITS(v))
  69. #define OFF_CLRBITS(v) (CLRBITS(OFF_BITS(v))
  70. #define LED_STARTED_ON_SETBITS ((BOARD_RED_LED_BIT) << ON_SETBITS_SHIFT)
  71. #define LED_STARTED_ON_CLRBITS ((BOARD_GREEN_LED_BIT|BOARD_BLUE_LED_BIT|BOARD_USR_LED_BIT) << ON_CLRBITS_SHIFT)
  72. #define LED_STARTED_OFF_SETBITS (0 << OFF_SETBITS_SHIFT)
  73. #define LED_STARTED_OFF_CLRBITS ((BOARD_RED_LED_BIT|BOARD_GREEN_LED_BIT|BOARD_BLUE_LED_BIT|BOARD_USR_LED_BIT) << OFF_CLRBITS_SHIFT)
  74. #define LED_HEAPALLOCATE_ON_SETBITS ((BOARD_BLUE_LED_BIT) << ON_SETBITS_SHIFT)
  75. #define LED_HEAPALLOCATE_ON_CLRBITS ((BOARD_RED_LED_BIT|BOARD_GREEN_LED_BIT|BOARD_USR_LED_BIT) << ON_CLRBITS_SHIFT)
  76. #define LED_HEAPALLOCATE_OFF_SETBITS ((BOARD_RED_LED_BIT) << OFF_SETBITS_SHIFT)
  77. #define LED_HEAPALLOCATE_OFF_CLRBITS ((BOARD_GREEN_LED_BIT|BOARD_BLUE_LED_BIT|BOARD_USR_LED_BIT) << OFF_CLRBITS_SHIFT)
  78. #define LED_IRQSENABLED_ON_SETBITS ((BOARD_RED_LED_BIT|BOARD_GREEN_LED_BIT) << ON_SETBITS_SHIFT)
  79. #define LED_IRQSENABLED_ON_CLRBITS ((BOARD_BLUE_LED_BIT|BOARD_USR_LED_BIT) << ON_CLRBITS_SHIFT)
  80. #define LED_IRQSENABLED_OFF_SETBITS ((BOARD_BLUE_LED_BIT) << OFF_SETBITS_SHIFT)
  81. #define LED_IRQSENABLED_OFF_CLRBITS ((BOARD_RED_LED_BIT|BOARD_GREEN_LED_BIT|BOARD_USR_LED_BIT) << OFF_CLRBITS_SHIFT)
  82. #define LED_STACKCREATED_ON_SETBITS ((BOARD_GREEN_LED_BIT) << ON_SETBITS_SHIFT)
  83. #define LED_STACKCREATED_ON_CLRBITS ((BOARD_RED_LED_BIT|BOARD_BLUE_LED_BIT|BOARD_USR_LED_BIT) << ON_CLRBITS_SHIFT)
  84. #define LED_STACKCREATED_OFF_SETBITS ((BOARD_RED_LED_BIT|BOARD_GREEN_LED_BIT) << OFF_SETBITS_SHIFT)
  85. #define LED_STACKCREATED_OFF_CLRBITS ((BOARD_BLUE_LED_BIT|BOARD_USR_LED_BIT) << OFF_CLRBITS_SHIFT)
  86. #define LED_INIRQ_ON_SETBITS ((BOARD_RED_LED_BIT) << ON_SETBITS_SHIFT)
  87. #define LED_INIRQ_ON_CLRBITS ((0) << ON_CLRBITS_SHIFT)
  88. #define LED_INIRQ_OFF_SETBITS ((0) << OFF_SETBITS_SHIFT)
  89. #define LED_INIRQ_OFF_CLRBITS ((BOARD_RED_LED_BIT) << OFF_CLRBITS_SHIFT)
  90. #define LED_SIGNAL_ON_SETBITS ((BOARD_BLUE_LED_BIT) << ON_SETBITS_SHIFT)
  91. #define LED_SIGNAL_ON_CLRBITS ((0) << ON_CLRBITS_SHIFT)
  92. #define LED_SIGNAL_OFF_SETBITS ((0) << OFF_SETBITS_SHIFT)
  93. #define LED_SIGNAL_OFF_CLRBITS ((BOARD_BLUE_LED_BIT) << OFF_CLRBITS_SHIFT)
  94. #define LED_ASSERTION_ON_SETBITS ((BOARD_RED_LED_BIT|BOARD_GREEN_LED_BIT|BOARD_BLUE_LED_BIT) << ON_SETBITS_SHIFT)
  95. #define LED_ASSERTION_ON_CLRBITS ((0) << ON_CLRBITS_SHIFT)
  96. #define LED_ASSERTION_OFF_SETBITS ((0) << OFF_SETBITS_SHIFT)
  97. #define LED_ASSERTION_OFF_CLRBITS ((BOARD_RED_LED_BIT|BOARD_GREEN_LED_BIT|BOARD_BLUE_LED_BIT) << OFF_CLRBITS_SHIFT)
  98. #define LED_PANIC_ON_SETBITS ((BOARD_RED_LED_BIT) << ON_SETBITS_SHIFT)
  99. #define LED_PANIC_ON_CLRBITS ((0) << ON_CLRBITS_SHIFT)
  100. #define LED_PANIC_OFF_SETBITS ((0) << OFF_SETBITS_SHIFT)
  101. #define LED_PANIC_OFF_CLRBITS ((BOARD_RED_LED_BIT) << OFF_CLRBITS_SHIFT)
  102. /**************************************************************************************
  103. * Private Function Prototypes
  104. **************************************************************************************/
  105. /* LED State Controls */
  106. static inline void phy_led_on(unsigned int on_bitset);
  107. static inline void phy_led_off(unsigned int off_bitset);
  108. static void led_setonoff(unsigned int bitset);
  109. /****************************************************************************
  110. * Private Data
  111. ****************************************************************************/
  112. static const uint16_t g_ledbits[LED_NUM_CODES] =
  113. {
  114. (LED_STARTED_ON_SETBITS | LED_STARTED_ON_CLRBITS |
  115. LED_STARTED_OFF_SETBITS | LED_STARTED_OFF_CLRBITS),
  116. (LED_HEAPALLOCATE_ON_SETBITS | LED_HEAPALLOCATE_ON_CLRBITS |
  117. LED_HEAPALLOCATE_OFF_SETBITS | LED_HEAPALLOCATE_OFF_CLRBITS),
  118. (LED_IRQSENABLED_ON_SETBITS | LED_IRQSENABLED_ON_CLRBITS |
  119. LED_IRQSENABLED_OFF_SETBITS | LED_IRQSENABLED_OFF_CLRBITS),
  120. (LED_STACKCREATED_ON_SETBITS | LED_STACKCREATED_ON_CLRBITS |
  121. LED_STACKCREATED_OFF_SETBITS | LED_STACKCREATED_OFF_CLRBITS),
  122. (LED_INIRQ_ON_SETBITS | LED_INIRQ_ON_CLRBITS |
  123. LED_INIRQ_OFF_SETBITS | LED_INIRQ_OFF_CLRBITS),
  124. (LED_SIGNAL_ON_SETBITS | LED_SIGNAL_ON_CLRBITS |
  125. LED_SIGNAL_OFF_SETBITS | LED_SIGNAL_OFF_CLRBITS),
  126. (LED_ASSERTION_ON_SETBITS | LED_ASSERTION_ON_CLRBITS |
  127. LED_ASSERTION_OFF_SETBITS | LED_ASSERTION_OFF_CLRBITS),
  128. (LED_PANIC_ON_SETBITS | LED_PANIC_ON_CLRBITS |
  129. LED_PANIC_OFF_SETBITS | LED_PANIC_OFF_CLRBITS)
  130. };
  131. /****************************************************************************
  132. * Private Functions
  133. ****************************************************************************/
  134. /****************************************************************************
  135. * Name: phy_led_on
  136. *
  137. * Description:
  138. * Performs the physical IO to turn On LEDs to the bit encoded state
  139. *
  140. ****************************************************************************/
  141. static inline void phy_led_on(unsigned int on_bitset)
  142. {
  143. /* All RGB LEDs are pulled up and, hence, active low */
  144. if ((on_bitset & BOARD_RED_LED_BIT) != 0)
  145. {
  146. stm32_gpiowrite(GPIO_LED2, false);
  147. }
  148. if ((on_bitset & BOARD_GREEN_LED_BIT) != 0)
  149. {
  150. stm32_gpiowrite(GPIO_LED4, false);
  151. }
  152. if ((on_bitset & BOARD_BLUE_LED_BIT) != 0)
  153. {
  154. stm32_gpiowrite(GPIO_LED3, false);
  155. }
  156. /* USR LED is pulled down and, hence, active high */
  157. if ((on_bitset & BOARD_USR_LED_BIT) != 0)
  158. {
  159. stm32_gpiowrite(GPIO_LED1, true);
  160. }
  161. }
  162. /****************************************************************************
  163. * Name: phy_led_off
  164. *
  165. * Description:
  166. * Performs the physical IO to turn Off LEDs to the bit encoded state
  167. *
  168. ****************************************************************************/
  169. static inline void phy_led_off(unsigned int off_bitset)
  170. {
  171. /* All RGB LEDs are pulled up and, hence, active low */
  172. if ((off_bitset & BOARD_RED_LED_BIT) != 0)
  173. {
  174. stm32_gpiowrite(GPIO_LED2, true);
  175. }
  176. if ((off_bitset & BOARD_GREEN_LED_BIT) != 0)
  177. {
  178. stm32_gpiowrite(GPIO_LED4, true);
  179. }
  180. if ((off_bitset & BOARD_BLUE_LED_BIT) != 0)
  181. {
  182. stm32_gpiowrite(GPIO_LED3, true);
  183. }
  184. /* USR LED is pulled down and, hence, active high */
  185. if ((off_bitset & BOARD_USR_LED_BIT) != 0)
  186. {
  187. stm32_gpiowrite(GPIO_LED1, false);
  188. }
  189. }
  190. /****************************************************************************
  191. * Name: led_setonoff
  192. *
  193. * Description:
  194. * Set/clear all LEDs to the bit encoded state
  195. *
  196. ****************************************************************************/
  197. static void led_setonoff(unsigned int bits)
  198. {
  199. phy_led_off(CLRBITS(bits));
  200. phy_led_on(SETBITS(bits));
  201. }
  202. /****************************************************************************
  203. * Public Functions
  204. ****************************************************************************/
  205. /****************************************************************************
  206. * Name: board_autoled_initialize
  207. ****************************************************************************/
  208. void board_autoled_initialize(void)
  209. {
  210. /* Configure LED1-4 GPIOs for output */
  211. stm32_configgpio(GPIO_LED1);
  212. stm32_configgpio(GPIO_LED2);
  213. stm32_configgpio(GPIO_LED3);
  214. stm32_configgpio(GPIO_LED4);
  215. }
  216. /****************************************************************************
  217. * Name: board_autoled_on
  218. ****************************************************************************/
  219. void board_autoled_on(int led)
  220. {
  221. led_setonoff(ON_BITS(g_ledbits[led]));
  222. }
  223. /****************************************************************************
  224. * Name: board_autoled_off
  225. ****************************************************************************/
  226. void board_autoled_off(int led)
  227. {
  228. led_setonoff(OFF_BITS(g_ledbits[led]));
  229. }
  230. #endif /* CONFIG_ARCH_LEDS */