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"
23 extern int gpio_test();
24 extern int i2c_test();
25 extern int adc_test();
31 peripheral_gpio_h handle = NULL;
33 printf("artik5 : 135 \n");
34 printf("artik10 : 22 \n");
35 printf(">> PIN NUMBER : ");
37 if (scanf("%d", &num) < 0)
39 printf("num %d\n", num);
41 if (peripheral_gpio_open(num, &handle) != PERIPHERAL_ERROR_NONE) {
42 printf("handle is null\n");
46 if (peripheral_gpio_set_direction(handle, PERIPHERAL_GPIO_DIRECTION_OUT) != PERIPHERAL_ERROR_NONE) {
47 printf("set direction error!!!");
53 peripheral_gpio_write(handle, 1);
55 peripheral_gpio_write(handle, 0);
58 printf("write finish\n");
59 peripheral_gpio_close(handle);
63 peripheral_gpio_close(handle);
68 /* Address of GY30 light sensor */
69 #define GY30_ADDR 0x23
71 /* Start measurement at 11x resolution. Measurement time is approx 120ms. */
72 #define GY30_CONT_HIGH_RES_MODE 0x10
74 #define GY30_READ_INTENSITY(buf) ((buf[0] << 8 | buf[1]) / 1.2)
80 unsigned char buf[10];
83 printf(">> I2C bus number : ");
84 if (scanf("%d", &bus_num) < 0)
87 if ((peripheral_i2c_init(bus_num, &i2c)) != 0) {
88 printf("Failed to initialize I2C device\n");
92 if (peripheral_i2c_set_address(i2c, GY30_ADDR) != 0) {
93 printf("Failed to set address\n");
97 buf[0] = GY30_CONT_HIGH_RES_MODE;
98 if (peripheral_i2c_write(i2c, buf, 1) != 0) {
99 printf("Failed to write\n");
106 peripheral_i2c_read(i2c, buf, 2);
107 result = GY30_READ_INTENSITY(buf);
108 printf("Result [%d]\n", result);
111 peripheral_i2c_stop(i2c);
115 peripheral_i2c_stop(i2c);
124 adc_context_h dev = NULL;
126 printf(">>channel :");
127 scanf("%d", &channel);
129 dev = peripheral_adc_open(channel);
132 printf("open error!\n");
136 peripheral_adc_read(dev, &data);
138 peripheral_adc_close(dev);
143 int pwm_test_led(void)
145 int device = 0, channel = 0;
146 int period = 1 * 1000;
147 int duty_cycle = 1 * 1000 / 100;
151 int get_period, get_duty_cycle;
152 peripheral_pwm_context_h dev;
154 printf("<<< pwm_test >>>\n");
156 dev = peripheral_pwm_open(device, channel);
157 peripheral_pwm_set_period(dev, period); /* period: nanosecond */
158 peripheral_pwm_set_duty_cycle(dev, duty_cycle); /* duty_cycle: nanosecond */
159 peripheral_pwm_set_enabled(dev, 1); /* 0: disable, 1: enable */
162 for (set_duty_cycle = period; set_duty_cycle > 0; set_duty_cycle -= 50) {
164 peripheral_pwm_set_duty_cycle(dev, set_duty_cycle);
165 peripheral_pwm_get_period(dev, &get_period);
166 peripheral_pwm_get_duty_cycle(dev, &get_duty_cycle);
167 printf("period(%d), duty_cycle(%d)\n", get_period, get_duty_cycle);
170 for (set_duty_cycle = 0; set_duty_cycle < period; set_duty_cycle += 50) {
172 peripheral_pwm_set_duty_cycle(dev, set_duty_cycle);
173 peripheral_pwm_get_period(dev, &get_period);
174 peripheral_pwm_get_duty_cycle(dev, &get_duty_cycle);
175 printf("period(%d), duty_cycle(%d)\n", get_period, get_duty_cycle);
180 peripheral_pwm_set_enabled(dev, 0); /* 0: disable, 1: enable */
181 peripheral_pwm_close(dev);
186 int pwm_test_motor(void)
188 int device = 0, channel = 0;
189 int period = 20000000;
190 int duty_cycle = 1500000;
191 int cnt = 0, idx = 0;
192 int degree[3] = {0, 45, 90};
193 peripheral_pwm_context_h dev;
195 printf("<<< pwm_test_motor >>>\n");
197 dev = peripheral_pwm_open(device, channel);
198 for (cnt = 0; cnt < 5; cnt++) {
199 for (idx = 0; idx < 3; idx++) {
200 switch (degree[idx]) {
202 duty_cycle = 1000000;
205 duty_cycle = 1500000;
208 duty_cycle = 2000000;
211 duty_cycle = 2000000;
214 printf("set degree: %d\n", degree[idx]);
215 peripheral_pwm_set_period(dev, period);
216 peripheral_pwm_set_duty_cycle(dev, duty_cycle);
217 peripheral_pwm_set_enabled(dev, 1); /* 0: disable, 1: enable */
222 peripheral_pwm_set_enabled(dev, 0); /* 0: disable, 1: enable */
223 peripheral_pwm_close(dev);
228 int main(int argc, char **argv)
233 printf("===================\n");
234 printf(" test Menu\n");
235 printf("===================\n");
236 printf(" 1. GPIO Test\n");
237 printf(" 2. I2C Test\n");
238 printf(" 3. pwm led test\n");
239 printf(" 4. pwm motor test\n");
241 printf(" 11. H/W IF GPIO Test\n");
242 printf(" 12. H/W IF I2C Test\n");
243 printf(" 13. H/W IF PWM Test\n");
244 printf(" 14. H/W IF SPI Test\n");
246 if (scanf("%d", &num) < 0)
257 ret = pwm_test_led();
260 ret = pwm_test_motor();
272 printf("Not support \n");
274 printf(" return : %d\n", ret);