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