1 /*
2 * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3 */
4
5 #include "aos/init.h"
6 #include "board.h"
7 #include <aos/errno.h>
8 #include <aos/kernel.h>
9 #include <aos/gpioc.h>
10 #include <k_api.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13
14 #include "light_driver.h"
15 // PA_28
16 #define RED_LED (28)
17 // PB_4
18 #define GREEN_LED (4)
19 // PB_7
20 #define BLUE_LED (7)
21
light_driver_init(void)22 void light_driver_init(void)
23 {
24 aos_gpioc_ref_t gpioc;
25 uint32_t mode;
26 aos_status_t r;
27
28 r = aos_gpioc_get(&gpioc, 0);
29 if (r) {
30 printf("aos_gpioc_get failed %d\r\n", (int)r);
31 return;
32 }
33
34 mode = AOS_GPIO_DIR_OUTPUT;
35 mode |= AOS_GPIO_OUTPUT_CFG_PP;
36 mode |= AOS_GPIO_OUTPUT_INIT_LOW;
37 r = aos_gpioc_set_mode(&gpioc, RED_LED, mode);
38 if (r) {
39 printf("aos_gpioc_set_mode failed %d\r\n", (int)r);
40 aos_gpioc_put(&gpioc);
41 return;
42 }
43
44 r = aos_gpioc_get(&gpioc, 1);
45 if (r) {
46 printf("aos_gpioc_get failed %d\r\n", (int)r);
47 return;
48 }
49
50 mode = AOS_GPIO_DIR_OUTPUT;
51 mode |= AOS_GPIO_OUTPUT_CFG_PP;
52 mode |= AOS_GPIO_OUTPUT_INIT_LOW;
53 r = aos_gpioc_set_mode(&gpioc, GREEN_LED, mode);
54 if (r) {
55 printf("aos_gpioc_set_mode failed %d\r\n", (int)r);
56 aos_gpioc_put(&gpioc);
57 return;
58 }
59
60 r = aos_gpioc_set_mode(&gpioc, BLUE_LED, mode);
61 if (r) {
62 printf("aos_gpioc_set_mode failed %d\r\n", (int)r);
63 aos_gpioc_put(&gpioc);
64 return;
65 }
66 }
67
light_set(uint8_t onoff)68 void light_set(uint8_t onoff)
69 {
70 aos_gpioc_ref_t gpioc;
71 aos_status_t r;
72
73 r = aos_gpioc_get(&gpioc, 0);
74 if (r) {
75 printf("aos_gpioc_get failed %d\r\n", (int)r);
76 return;
77 }
78
79 r = aos_gpioc_set_value(&gpioc, RED_LED, !onoff);
80
81 r = aos_gpioc_get(&gpioc, 1);
82 if (r) {
83 printf("aos_gpioc_get failed %d\r\n", (int)r);
84 return;
85 }
86
87 r = aos_gpioc_set_value(&gpioc, GREEN_LED, !onoff);
88 r = aos_gpioc_set_value(&gpioc, BLUE_LED, !onoff);
89 printf("aos_gpioc_set_value %d returned %d\r\n", onoff, (int)r);
90 }
91
light_blink(void)92 void light_blink(void)
93 {
94 int out_val = 0;
95 for (int i = 6; i > 0; i--) {
96 light_set(out_val);
97 out_val = !out_val;
98 aos_msleep(500);
99 }
100 }
101