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 */
9 #include <rtthread.h>
10 #include "board.h"
11 #include "drv_led.h"
12
13
14 /**
15 *
16 * LED1 <==> GPIO4[12]
17 * LED2 <==> GPIO4[13]
18 *
19 **/
20
21 #define LED_NUM 8
22
23 #define LED1_PIN 12
24 #define LED1_PORT 4
25
26 #define LED2_PIN 13
27 #define LED2_PORT 4
28
29 struct led_ctrl
30 {
31 uint8_t num;
32 uint8_t port;
33 };
34
35 struct lpc_led
36 {
37 /* inherit from rt_device */
38 struct rt_device parent;
39
40 struct led_ctrl ctrl[LED_NUM];
41 };
42
43 static struct lpc_led led;
44
rt_led_init(rt_device_t dev)45 static rt_err_t rt_led_init(rt_device_t dev)
46 {
47 /* Enable clock and init GPIO outputs */
48 LPC_CCU1->CLK_M4_GPIO_CFG = CCU_CLK_CFG_AUTO | CCU_CLK_CFG_RUN;
49 while (!(LPC_CCU1->CLK_M4_GPIO_STAT & CCU_CLK_STAT_RUN));
50
51 /* set GPIO4[12] GPIO4[13] as GPIO. */
52 LPC_SCU->SFSP9_0 = 0; /* GPIO4[12] */
53 LPC_SCU->SFSP9_1 = 0; /* GPIO4[13] */
54 /* set GPIO4[12] GPIO4[13] output. */
55 LPC_GPIO_PORT->DIR[LED1_PORT] |= 0x01 << LED1_PIN;
56 LPC_GPIO_PORT->DIR[LED2_PORT] |= 0x01 << LED2_PIN;
57 /* turn off all the led */
58 LPC_GPIO_PORT->CLR[LED1_PORT] |= 0x01 << LED1_PIN;
59 LPC_GPIO_PORT->CLR[LED2_PORT] |= 0x01 << LED2_PIN;
60
61 led.ctrl[0].num = LED1_PIN;
62 led.ctrl[0].port = LED1_PORT;
63 led.ctrl[1].num = LED2_PIN;
64 led.ctrl[1].port = LED2_PORT;
65
66 return RT_EOK;
67 }
68
rt_led_open(rt_device_t dev,rt_uint16_t oflag)69 static rt_err_t rt_led_open(rt_device_t dev, rt_uint16_t oflag)
70 {
71 return RT_EOK;
72 }
73
rt_led_close(rt_device_t dev)74 static rt_err_t rt_led_close(rt_device_t dev)
75 {
76 return RT_EOK;
77 }
78
rt_led_read(rt_device_t dev,rt_off_t pos,void * buffer,rt_size_t size)79 static rt_ssize_t rt_led_read(rt_device_t dev, rt_off_t pos, void *buffer,
80 rt_size_t size)
81 {
82 rt_ubase_t index = 0;
83 rt_ubase_t nr = size;
84 rt_uint8_t *value = buffer;
85
86 RT_ASSERT(dev == &led.parent);
87 RT_ASSERT((pos + size) <= LED_NUM);
88
89 for (index = 0; index < nr; index++)
90 {
91 if ((LPC_GPIO_PORT->PIN[led.ctrl[pos + index].port] & (1 << led.ctrl[pos + index].num)) != 0)
92 {
93 *value = 0;
94 }
95 else
96 {
97 *value = 1;
98 }
99 value++;
100 }
101 return index;
102 }
103
rt_led_write(rt_device_t dev,rt_off_t pos,const void * buffer,rt_size_t size)104 static rt_ssize_t rt_led_write(rt_device_t dev, rt_off_t pos,
105 const void *buffer, rt_size_t size)
106 {
107 rt_ubase_t index = 0;
108 rt_ubase_t nw = size;
109 const rt_uint8_t *value = buffer;
110
111 RT_ASSERT(dev == &led.parent);
112 RT_ASSERT((pos + size) <= LED_NUM);
113
114 for (index = 0; index < nw; index++)
115 {
116 if (*value++)
117 {
118 LPC_GPIO_PORT->CLR[led.ctrl[pos + index].port] = (1 << led.ctrl[pos + index].num);
119 }
120 else
121 {
122 LPC_GPIO_PORT->SET[led.ctrl[pos + index].port] = (1 << led.ctrl[pos + index].num);
123 }
124 }
125 return index;
126 }
127
rt_led_control(rt_device_t dev,int cmd,void * args)128 static rt_err_t rt_led_control(rt_device_t dev, int cmd, void *args)
129 {
130 RT_ASSERT(dev == &led.parent);
131
132 if (cmd == LED_DEVICE_CTRL)
133 {
134 rt_uint32_t *led_num = args;
135 *led_num = LED_NUM;
136 }
137 return RT_EOK;
138 }
139
rt_led_hw_init(void)140 int rt_led_hw_init(void)
141 {
142 led.parent.type = RT_Device_Class_Char;
143 led.parent.rx_indicate = RT_NULL;
144 led.parent.tx_complete = RT_NULL;
145 led.parent.init = rt_led_init;
146 led.parent.open = rt_led_open;
147 led.parent.close = rt_led_close;
148 led.parent.read = rt_led_read;
149 led.parent.write = rt_led_write;
150 led.parent.control = rt_led_control;
151 led.parent.user_data = RT_NULL;
152
153 /* register a character device */
154 rt_device_register(&led.parent, "led", RT_DEVICE_FLAG_RDWR);
155 /* init led device */
156 rt_led_init(&led.parent);
157 return 0;
158 }
159 INIT_DEVICE_EXPORT(rt_led_hw_init);
160
161 #ifdef RT_USING_FINSH
162 #include <finsh.h>
led_test(rt_uint32_t led_num,rt_uint32_t value)163 void led_test(rt_uint32_t led_num, rt_uint32_t value)
164 {
165 rt_uint8_t led_value = value;
166 rt_led_write(&led.parent, led_num, &led_value, 1);
167 }
168 FINSH_FUNCTION_EXPORT(led_test, e.g: led_test(0, 100).)
169 #endif
170