2 * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "peripheral_io.h"
24 extern int gpio_test();
25 extern int i2c_test();
26 extern int adc_test();
34 peripheral_gpio_h handle = NULL;
36 printf("artik5 : 135 \n");
37 printf("artik10 : 22 \n");
38 printf(">> PIN NUMBER : ");
40 if (scanf("%d", &num) < 0)
42 printf("num %d\n", num);
44 if (peripheral_gpio_open(num, &handle) != PERIPHERAL_ERROR_NONE) {
45 printf("handle is null\n");
49 if (peripheral_gpio_set_direction(handle, PERIPHERAL_GPIO_DIRECTION_OUT) != PERIPHERAL_ERROR_NONE) {
50 printf("set direction error!!!");
56 peripheral_gpio_write(handle, 1);
58 peripheral_gpio_write(handle, 0);
61 printf("write finish\n");
62 peripheral_gpio_close(handle);
66 peripheral_gpio_close(handle);
70 void gpio_irq_test_isr(void *user_data)
73 peripheral_gpio_h gpio = user_data;
75 peripheral_gpio_get_pin(gpio, &pin);
77 printf("gpio_irq_test_isr: GPIO %d interrupt occurs.\n", pin);
80 void *gpio_irq_test_thread(void *data)
82 peripheral_gpio_h gpio = data;
85 printf(">> Press any key to exit GPIO IRQ Test : \n");
86 if (scanf("%d", &num) < 0)
89 peripheral_gpio_unregister_cb(gpio);
90 peripheral_gpio_close(gpio);
92 g_main_loop_quit(loop);
96 int gpio_irq_test(void)
100 peripheral_gpio_h gpio = NULL;
101 peripheral_gpio_edge_e edge = PERIPHERAL_GPIO_EDGE_NONE;
103 printf("artik710 : 27 \n");
104 printf(">> PIN NUMBER : ");
106 if (scanf("%d", &num) < 0)
109 if (peripheral_gpio_open(num, &gpio) != PERIPHERAL_ERROR_NONE) {
110 printf("test dev is null\n");
114 if (peripheral_gpio_set_direction(gpio, PERIPHERAL_GPIO_DIRECTION_IN) != 0) {
115 printf("test set direction error!!!");
119 printf(">> Select Edge Mode (0 = None, 1 = Falling, 2 = Rising, 3 = Both) : ");
120 if (scanf("%d", &num) < 0)
123 if (num >= 0 && num <= 3)
126 peripheral_gpio_set_edge_mode( gpio, edge);
127 peripheral_gpio_register_cb(gpio, gpio_irq_test_isr, gpio);
129 test_thread = g_thread_new("key input thread", &gpio_irq_test_thread, gpio);
130 loop = g_main_loop_new(NULL, FALSE);
131 g_main_loop_run(loop);
133 g_thread_join(test_thread);
135 g_main_loop_unref(loop);
140 peripheral_gpio_close(gpio);
144 /* Address of GY30 light sensor */
145 #define GY30_ADDR 0x23
147 /* Start measurement at 11x resolution. Measurement time is approx 120ms. */
148 #define GY30_CONT_HIGH_RES_MODE 0x10
150 #define GY30_READ_INTENSITY(buf) ((buf[0] << 8 | buf[1]) / 1.2)
156 unsigned char buf[10];
157 peripheral_i2c_h i2c;
159 printf(">> I2C bus number : ");
160 if (scanf("%d", &bus_num) < 0)
163 if ((peripheral_i2c_open(bus_num, GY30_ADDR, &i2c)) != 0) {
164 printf("Failed to open I2C communication\n");
168 buf[0] = GY30_CONT_HIGH_RES_MODE;
169 if (peripheral_i2c_write(i2c, buf, 1) != 0) {
170 printf("Failed to write\n");
177 peripheral_i2c_read(i2c, buf, 2);
178 result = GY30_READ_INTENSITY(buf);
179 printf("Result [%d]\n", result);
182 peripheral_i2c_close(i2c);
186 peripheral_i2c_close(i2c);
195 adc_context_h dev = NULL;
197 printf(">>channel :");
198 scanf("%d", &channel);
200 dev = peripheral_adc_open(channel);
203 printf("open error!\n");
207 peripheral_adc_read(dev, &data);
209 peripheral_adc_close(dev);
214 int pwm_test_led(void)
216 int device = 0, channel = 0;
217 int period = 1 * 1000;
218 int duty_cycle = 1 * 1000 / 100;
222 int get_period, get_duty_cycle;
223 peripheral_pwm_h dev;
225 printf("<<< pwm_test >>>\n");
227 dev = peripheral_pwm_open(device, channel);
228 peripheral_pwm_set_period(dev, period); /* period: nanosecond */
229 peripheral_pwm_set_duty_cycle(dev, duty_cycle); /* duty_cycle: nanosecond */
230 peripheral_pwm_set_enabled(dev, 1); /* 0: disable, 1: enable */
233 for (set_duty_cycle = period; set_duty_cycle > 0; set_duty_cycle -= 50) {
235 peripheral_pwm_set_duty_cycle(dev, set_duty_cycle);
236 peripheral_pwm_get_period(dev, &get_period);
237 peripheral_pwm_get_duty_cycle(dev, &get_duty_cycle);
238 printf("period(%d), duty_cycle(%d)\n", get_period, get_duty_cycle);
241 for (set_duty_cycle = 0; set_duty_cycle < period; set_duty_cycle += 50) {
243 peripheral_pwm_set_duty_cycle(dev, set_duty_cycle);
244 peripheral_pwm_get_period(dev, &get_period);
245 peripheral_pwm_get_duty_cycle(dev, &get_duty_cycle);
246 printf("period(%d), duty_cycle(%d)\n", get_period, get_duty_cycle);
251 peripheral_pwm_set_enabled(dev, 0); /* 0: disable, 1: enable */
252 peripheral_pwm_close(dev);
257 int pwm_test_motor(void)
259 int device = 0, channel = 0;
260 int period = 20000000;
261 int duty_cycle = 1500000;
262 int cnt = 0, idx = 0;
263 int degree[3] = {0, 45, 90};
264 peripheral_pwm_h dev;
266 printf("<<< pwm_test_motor >>>\n");
268 dev = peripheral_pwm_open(device, channel);
269 for (cnt = 0; cnt < 5; cnt++) {
270 for (idx = 0; idx < 3; idx++) {
271 switch (degree[idx]) {
273 duty_cycle = 1000000;
276 duty_cycle = 1500000;
279 duty_cycle = 2000000;
282 duty_cycle = 2000000;
285 printf("set degree: %d\n", degree[idx]);
286 peripheral_pwm_set_period(dev, period);
287 peripheral_pwm_set_duty_cycle(dev, duty_cycle);
288 peripheral_pwm_set_enabled(dev, 1); /* 0: disable, 1: enable */
293 peripheral_pwm_set_enabled(dev, 0); /* 0: disable, 1: enable */
294 peripheral_pwm_close(dev);
299 int main(int argc, char **argv)
304 printf("===================\n");
305 printf(" test Menu\n");
306 printf("===================\n");
307 printf(" 1. GPIO Test\n");
308 printf(" 2. I2C Test\n");
309 printf(" 3. pwm led test\n");
310 printf(" 4. pwm motor test\n");
312 printf(" 11. GPIO Interrupt Test\n");
313 printf(" 12. H/W IF I2C Test\n");
314 printf(" 13. H/W IF PWM Test\n");
315 printf(" 14. H/W IF SPI Test\n");
317 if (scanf("%d", &num) < 0)
328 ret = pwm_test_led();
331 ret = pwm_test_motor();
334 ret = gpio_irq_test();
343 printf("Not support \n");
345 printf(" return : %d\n", ret);