Contiki 3.x
leds-arch.c
Go to the documentation of this file.
1 
2 /**
3  * \addtogroup FRDM-KL25Z
4  * @{
5  *
6  * \defgroup FRDM-KL25Z-leds LED driver
7  *
8  * LED driver implementation for the FRDM-KL25Z
9  * @{
10  *
11  * \file
12  * LED driver implementation for the FRDM-KL25Z
13  * \author
14  * Graeme Bragg <g.bragg@ecs.soton.ac.uk>
15  */
16 
17 
18 /*---------------------------------------------------------------------------*/
19 /**
20  * \brief Initialise the required GPIO pins.
21  *
22  * The FRDM-KL25Z has a tri-colour LED:
23  * Red is connected to PTB18
24  * Green is connected to PTB19
25  * Blue is connected to PTD1
26  */
27 
28 #include "contiki.h"
29 #include "dev/leds.h"
30 #include "gpio.h"
31 #include "derivative.h"
32 #include "cpu.h"
33 
34 void
36 {
37  port_enable(PORTB_EN_MASK | PORTD_EN_MASK); /* Enable Port B and Port D for the LEDs. */
38 
39  port_conf_pin(LED_RED_PORT, LED_RED_PIN, (PORT_PCR_MUX_GPIO | PORT_PCR_ISF_MASK)); /* Config Red LED. */
40  gpio_set_output(LED_RED_GPIO, port_pin_to_mask(LED_RED_PIN)); /* Set as output. */
41 
42  port_conf_pin(LED_GREEN_PORT, LED_GREEN_PIN, (PORT_PCR_MUX_GPIO | PORT_PCR_ISF_MASK)); /* Config Green LED. */
43  gpio_set_output(LED_GREEN_GPIO, port_pin_to_mask(LED_GREEN_PIN)); /* Set as output. */
44 
45  port_conf_pin(LED_BLUE_PORT, LED_BLUE_PIN, (PORT_PCR_MUX_GPIO | PORT_PCR_ISF_MASK)); /* Config Blue LED. */
46  gpio_set_output(LED_BLUE_GPIO, port_pin_to_mask(LED_BLUE_PIN)); /* Set as output. */
47 }
48 /*---------------------------------------------------------------------------*/
49 unsigned char
50 leds_arch_get(void)
51 {
52  uint8_t leds;
53 
54  leds = gpio_read_pin(LED_RED_GPIO, LED_RED_PIN) == 0? LEDS_RED : 0;
55  leds |= gpio_read_pin(LED_GREEN_GPIO, LED_GREEN_PIN) == 0? LEDS_GREEN : 0;
56  leds |= gpio_read_pin(LED_BLUE_GPIO, LED_BLUE_PIN) == 0? LEDS_BLUE : 0;
57 
58  return leds;
59 }
60 /*---------------------------------------------------------------------------*/
61 void
62 leds_arch_set(unsigned char leds)
63 {
64  if(leds & LEDS_GREEN) {
65  gpio_clr_pin(LED_GREEN_GPIO, port_pin_to_mask(LED_GREEN_PIN));
66  } else {
67  gpio_set_pin(LED_GREEN_GPIO, port_pin_to_mask(LED_GREEN_PIN));
68  }
69 
70  if(leds & LEDS_BLUE) {
71  gpio_clr_pin(LED_BLUE_GPIO, port_pin_to_mask(LED_BLUE_PIN));
72  } else {
73  gpio_set_pin(LED_BLUE_GPIO, port_pin_to_mask(LED_BLUE_PIN));
74  }
75 
76  if(leds & LEDS_RED) {
77  gpio_clr_pin(LED_RED_GPIO, port_pin_to_mask(LED_RED_PIN));
78  } else {
79  gpio_set_pin(LED_RED_GPIO, port_pin_to_mask(LED_RED_PIN));
80  }
81 }
82 /*---------------------------------------------------------------------------*/
void gpio_set_output(GPIO_Type *Port, uint32_t Pin_Mask)
Set pins with Pin_Mask of port with GPIOn_BASE_PTR to output.
Definition: gpio.c:135
uint32_t gpio_read_pin(GPIO_Type *Port, uint32_t Pin_Mask)
Read pins with Pin_Mask of port with GPIOn_BASE_PTR.
Definition: gpio.c:159
void leds_arch_init(void)
Leds implementation.
Definition: leds-arch.c:48
void gpio_clr_pin(GPIO_Type *Port, uint32_t Pin_Mask)
Clear pins with Pin_Mask of port with GPIOn_BASE_PTR low.
Definition: gpio.c:147
void port_conf_pin(PORT_Type *Port, uint8_t Pin, uint32_t PCR)
Configure the specified Pin of the port with PORTx_BASE_PTR with PCR.
Definition: gpio.c:40
void gpio_set_pin(GPIO_Type *Port, uint32_t Pin_Mask)
Set pins with Pin_Mask of port with GPIOn_BASE_PTR high.
Definition: gpio.c:141
uint32_t port_pin_to_mask(uint8_t pin)
Convert a pin number (0 to 31) to a pin mask.
Definition: gpio.c:46