1 /*
2  * Copyright (c) 2006-2021, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2011-03-03     lgnq
9  */
10 
11 #include <rtthread.h>
12 #include <rthw.h>
13 
14 #include "mb9bf506r.h"
15 #include "led.h"
16 
rt_hw_led_on(rt_uint8_t num)17 void rt_hw_led_on(rt_uint8_t num)
18 {
19     RT_ASSERT(num < LEDS_MAX_NUMBER);
20 
21     switch (num)
22     {
23         case 1:
24             USER_LED_PDOR &= ~USER_LED1;
25         break;
26         case 2:
27             USER_LED_PDOR &= ~USER_LED2;
28         break;
29         case 3:
30             USER_LED_PDOR &= ~USER_LED3;
31         break;
32         default:
33         break;
34     }
35 }
36 
rt_hw_led_off(rt_uint8_t num)37 void rt_hw_led_off(rt_uint8_t num)
38 {
39     RT_ASSERT(num < LEDS_MAX_NUMBER);
40 
41     switch (num)
42     {
43         case 1:
44             USER_LED_PDOR |= USER_LED1;
45         break;
46         case 2:
47             USER_LED_PDOR |= USER_LED2;
48         break;
49         case 3:
50             USER_LED_PDOR |= USER_LED3;
51         break;
52         default:
53         break;
54     }
55 }
56 
rt_hw_led_toggle(rt_uint8_t num)57 void rt_hw_led_toggle(rt_uint8_t num)
58 {
59     RT_ASSERT(num < LEDS_MAX_NUMBER);
60 
61     switch (num)
62     {
63         case 1:
64             if (USER_LED_PDOR&USER_LED1)
65                 USER_LED_PDOR &= ~USER_LED1;
66             else
67                 USER_LED_PDOR |= USER_LED1;
68         break;
69         case 2:
70             if (USER_LED_PDOR&USER_LED2)
71                 USER_LED_PDOR &= ~USER_LED2;
72             else
73                 USER_LED_PDOR |= USER_LED2;
74         break;
75         case 3:
76             if (USER_LED_PDOR&USER_LED3)
77                 USER_LED_PDOR &= ~USER_LED3;
78             else
79                 USER_LED_PDOR |= USER_LED3;
80         break;
81         default:
82         break;
83     }
84 }
85 
led_init(void)86 void led_init(void)
87 {
88     /*Select CPIO function*/
89     USER_LED_PFR &= ~USER_LED_MASK;
90     /* disable analog input */
91     FM3_GPIO->ADE &= ~USER_LED_MASK;
92     /*Set CPIO Pull-Up function*/
93     USER_LED_PCR |= USER_LED_MASK;
94     /*Make led pins outputs*/
95     USER_LED_DDR |= USER_LED_MASK;
96     USER_LED_PDOR |= USER_LED_MASK;
97 }
98 
pwm_update(rt_uint16_t value)99 void pwm_update(rt_uint16_t value)
100 {
101     FM3_BT2_PWM->PDUT  = value;
102 }
103 
led1_thread_entry(void * parameter)104 static void led1_thread_entry(void *parameter)
105 {
106     while (1)
107     {
108         rt_hw_led_toggle(1);
109         rt_thread_delay(RT_TICK_PER_SECOND);
110     }
111 }
112 
led2_thread_entry(void * parameter)113 static void led2_thread_entry(void *parameter)
114 {
115     while (1)
116     {
117         rt_hw_led_toggle(2);
118         rt_thread_delay(RT_TICK_PER_SECOND/2);
119     }
120 }
121 
122 static rt_thread_t led1_thread;
123 static rt_thread_t led2_thread;
rt_hw_led_init(void)124 void rt_hw_led_init(void)
125 {
126     led_init();
127 
128     led1_thread = rt_thread_create("led1", led1_thread_entry, RT_NULL, 384, 29, 5);
129     if (led1_thread != RT_NULL)
130         rt_thread_startup(led1_thread);
131 
132     led2_thread = rt_thread_create("led2", led2_thread_entry, RT_NULL, 384, 30, 5);
133     if (led2_thread != RT_NULL)
134         rt_thread_startup(led2_thread);
135 }
136 
137 
138