1 /*
2  * Copyright (c) 2006-2018, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  *
9  */
10 
11 #include <MK64F12.h>
12 #include "led.h"
13 
14 const rt_uint32_t led_mask[] = {1 << 21, 1 << 22, 1 << 26};
15 
rt_hw_led_init(void)16 void rt_hw_led_init(void)
17 {
18     SIM->SCGC5 |= (1 << SIM_SCGC5_PORTB_SHIFT);
19     SIM->SCGC5 |= (1 << SIM_SCGC5_PORTE_SHIFT);
20 
21     PORTB->PCR[21] &= ~PORT_PCR_MUX_MASK;
22     PORTB->PCR[21] |= PORT_PCR_MUX(1);   //PTB21 is GPIO pin
23 
24     PORTB->PCR[22] &= ~PORT_PCR_MUX_MASK;
25     PORTB->PCR[22] |= PORT_PCR_MUX(1);  //PTB22 is GPIO pin
26 
27     PORTE->PCR[26] &= ~PORT_PCR_MUX_MASK;
28     PORTE->PCR[26] |= PORT_PCR_MUX(1);  //PTE26 is GPIO pin
29 
30     /* Switch LEDs off and enable output*/
31     PTB->PDDR |= GPIO_PDDR_PDD(led_mask[1] | led_mask[0]);
32     PTE->PDDR |= GPIO_PDDR_PDD(led_mask[2]);
33 
34     rt_hw_led_off(LED_RED);
35     rt_hw_led_off(LED_GREEN);
36     rt_hw_led_off(LED_BLUE);
37 }
38 
rt_hw_led_uninit(void)39 void rt_hw_led_uninit(void)
40 {
41     PORTB->PCR[21] &= ~PORT_PCR_MUX_MASK;
42 
43     PORTB->PCR[22] &= ~PORT_PCR_MUX_MASK;
44 
45     PORTE->PCR[26] &= ~PORT_PCR_MUX_MASK;
46 }
47 
rt_hw_led_on(rt_uint32_t n)48 void rt_hw_led_on(rt_uint32_t n)
49 {
50     if (n != LED_GREEN)
51     {
52         PTB->PCOR |= led_mask[n];
53     }
54     else
55     {
56         PTE->PCOR |= led_mask[n];
57     }
58 }
59 
rt_hw_led_off(rt_uint32_t n)60 void rt_hw_led_off(rt_uint32_t n)
61 {
62     if (n != LED_GREEN)
63     {
64         PTB->PSOR |= led_mask[n];
65     }
66     else
67     {
68         PTE->PSOR |= led_mask[n];
69     }
70 }
71