EFM32 Giant Gecko Software Documentation  efm32gg-doc-5.1.2
bsp_stk_leds.c
Go to the documentation of this file.
1 /***************************************************************************/
16 #include "em_device.h"
17 #include "em_cmu.h"
18 #include "em_gpio.h"
19 #include "bsp.h"
20 #if defined(BSP_IO_EXPANDER)
21 #include "bsp_stk_ioexp.h"
22 #endif
23 
24 #if defined( BSP_GPIO_LEDS ) || defined( BSP_IO_EXPANDER )
25 
27 #if !defined( BSP_IO_EXPANDER )
28 typedef struct
29 {
30  GPIO_Port_TypeDef port;
31  unsigned int pin;
32 } tLedArray;
33 #endif
34 
35 #if !defined( BSP_IO_EXPANDER )
36 static const tLedArray ledArray[ BSP_NO_OF_LEDS ] = BSP_GPIO_LEDARRAY_INIT;
37 #endif
38 
39 int BSP_LedsInit(void)
40 {
41  /* If an I/O expander is used, assume that BSP_Init(BSP_INIT_IOEXP)
42  * and BSP_PeripheralAccess(BSP_IOEXP_LEDS, true) has been called.
43  */
44 #if !defined(BSP_IO_EXPANDER)
45  int i;
46 
49  for ( i=0; i<BSP_NO_OF_LEDS; i++ )
50  {
51  GPIO_PinModeSet(ledArray[i].port, ledArray[i].pin, gpioModePushPull, 0);
52  }
53 #endif
54  return BSP_STATUS_OK;
55 }
56 
57 uint32_t BSP_LedsGet(void)
58 {
59 #if defined(BSP_IO_EXPANDER)
60  uint8_t value;
61  uint32_t retVal;
62 
63  ioexpReadReg(BSP_IOEXP_REG_LED_CTRL, &value);
64  retVal = value & BSP_IOEXP_REG_LED_CTRL_LED_MASK;
65 
66 #else
67  int i;
68  uint32_t retVal, mask;
69 
70  for ( i=0, retVal=0, mask=0x1; i<BSP_NO_OF_LEDS; i++, mask <<= 1 )
71  {
72  if (GPIO_PinOutGet(ledArray[i].port, ledArray[i].pin))
73  retVal |= mask;
74  }
75 #endif
76  return retVal;
77 }
78 
79 int BSP_LedsSet(uint32_t leds)
80 {
81 #if defined(BSP_IO_EXPANDER)
82  uint8_t value;
83 
84  value = (leds & BSP_IOEXP_REG_LED_CTRL_LED_MASK)
85  | BSP_IOEXP_REG_LED_CTRL_DIRECT;
86  return ioexpWriteReg(BSP_IOEXP_REG_LED_CTRL, value);
87 
88 #else
89  int i;
90  uint32_t mask;
91 
92  for ( i=0, mask=0x1; i<BSP_NO_OF_LEDS; i++, mask <<= 1 )
93  {
94  if ( leds & mask )
95  GPIO_PinOutSet(ledArray[i].port, ledArray[i].pin);
96  else
97  GPIO_PinOutClear(ledArray[i].port, ledArray[i].pin);
98  }
99  return BSP_STATUS_OK;
100 #endif
101 }
102 
103 int BSP_LedClear(int ledNo)
104 {
105  if ((ledNo >= 0) && (ledNo < BSP_NO_OF_LEDS))
106  {
107 #if defined(BSP_IO_EXPANDER)
108  return ioexpRegBitsSet(BSP_IOEXP_REG_LED_CTRL, false, 1 << ledNo);
109 #else
110  GPIO_PinOutClear(ledArray[ledNo].port, ledArray[ledNo].pin);
111  return BSP_STATUS_OK;
112 #endif
113  }
115 }
116 
117 int BSP_LedGet(int ledNo)
118 {
119 #if defined(BSP_IO_EXPANDER)
120  uint8_t value;
121 #endif
122  int retVal = BSP_STATUS_ILLEGAL_PARAM;
123 
124  if ((ledNo >= 0) && (ledNo < BSP_NO_OF_LEDS))
125  {
126 #if defined(BSP_IO_EXPANDER)
127  ioexpReadReg(BSP_IOEXP_REG_LED_CTRL, &value);
128  retVal = (value & BSP_IOEXP_REG_LED_CTRL_LED_MASK) >> ledNo;
129 #else
130  retVal = (int)GPIO_PinOutGet(ledArray[ledNo].port, ledArray[ledNo].pin);
131 #endif
132  }
133  return retVal;
134 }
135 
136 int BSP_LedSet(int ledNo)
137 {
138  if ((ledNo >= 0) && (ledNo < BSP_NO_OF_LEDS))
139  {
140 #if defined(BSP_IO_EXPANDER)
141  return ioexpRegBitsSet(BSP_IOEXP_REG_LED_CTRL, true, (1 << ledNo));
142 #else
143  GPIO_PinOutSet(ledArray[ledNo].port, ledArray[ledNo].pin);
144  return BSP_STATUS_OK;
145 #endif
146  }
148 }
149 
150 int BSP_LedToggle(int ledNo)
151 {
152 #if defined(BSP_IO_EXPANDER)
153  uint8_t value;
154 #endif
155 
156  if ((ledNo >= 0) && (ledNo < BSP_NO_OF_LEDS))
157  {
158 #if defined(BSP_IO_EXPANDER)
159  ioexpReadReg(BSP_IOEXP_REG_LED_CTRL, &value);
160  value &= BSP_IOEXP_REG_LED_CTRL_LED_MASK;
161  value ^= (1 << ledNo);
162  value |= BSP_IOEXP_REG_LED_CTRL_DIRECT;
163  return ioexpWriteReg(BSP_IOEXP_REG_LED_CTRL, value);
164 #else
165  GPIO_PinOutToggle(ledArray[ledNo].port, ledArray[ledNo].pin);
166  return BSP_STATUS_OK;
167 #endif
168  }
170 }
171 
173 #endif /* BSP_GPIO_LEDS */
Clock management unit (CMU) API.
Board support package API definitions.
GPIO_Port_TypeDef
Definition: em_gpio.h:345
Board support package API implementation for STK's.
__STATIC_INLINE void GPIO_PinOutToggle(GPIO_Port_TypeDef port, unsigned int pin)
Toggle a single pin in GPIO port data out register.
Definition: em_gpio.h:881
CMSIS Cortex-M Peripheral Access Layer for Silicon Laboratories microcontroller devices.
int BSP_LedsInit(void)
Initialize LED drivers.
Definition: bsp_dk_leds.c:37
#define BSP_STATUS_OK
Definition: bsp.h:45
int BSP_LedsSet(uint32_t leds)
Update all LED's.
Definition: bsp_dk_leds.c:60
void GPIO_PinModeSet(GPIO_Port_TypeDef port, unsigned int pin, GPIO_Mode_TypeDef mode, unsigned int out)
Set the mode for a GPIO pin.
Definition: em_gpio.c:269
General Purpose IO (GPIO) peripheral API.
int BSP_LedSet(int ledNo)
Turn on a single LED.
Definition: bsp_dk_leds.c:111
int BSP_LedToggle(int ledNo)
Toggle a single LED.
Definition: bsp_dk_leds.c:131
__STATIC_INLINE void GPIO_PinOutSet(GPIO_Port_TypeDef port, unsigned int pin)
Set a single pin in GPIO data out register to 1.
Definition: em_gpio.h:856
void CMU_ClockEnable(CMU_Clock_TypeDef clock, bool enable)
Enable/disable a clock.
Definition: em_cmu.c:1453
uint32_t BSP_LedsGet(void)
Get status of all LED's.
Definition: bsp_dk_leds.c:48
#define BSP_STATUS_ILLEGAL_PARAM
Definition: bsp.h:46
__STATIC_INLINE unsigned int GPIO_PinOutGet(GPIO_Port_TypeDef port, unsigned int pin)
Get current setting for a pin in a GPIO port data out register.
Definition: em_gpio.h:834
int BSP_LedGet(int ledNo)
Get current status of a single LED.
Definition: bsp_dk_leds.c:93
int BSP_LedClear(int ledNo)
Turn off a single LED.
Definition: bsp_dk_leds.c:72
__STATIC_INLINE void GPIO_PinOutClear(GPIO_Port_TypeDef port, unsigned int pin)
Set a single pin in GPIO data out port register to 0.
Definition: em_gpio.h:811