2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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.
20 #include <peripheral_io.h>
23 #include "lap_counter/lap_counter.h"
28 typedef struct _lap_counter_s {
29 peripheral_gpio_h gpio;
30 uint32_t gpio_initial_value;
31 uint32_t gpio_prev_value;
34 static lap_counter_s s_info = { 0, };
36 #define CHECK_GPIO_ERROR(pin, ret) \
37 if (ret != PERIPHERAL_ERROR_NONE) { \
38 peripheral_gpio_close(pin); \
39 _E("GPIO ERROR: %s, %s[%d]", get_error_message(ret), __FILE__, __LINE__); \
43 static void _gpio_interrupted_cb(peripheral_gpio_h gpio, peripheral_error_e error, void *user_data)
45 uint32_t gpio_value = 0;
46 int ret = peripheral_gpio_read(s_info.gpio, &gpio_value);
47 CHECK_GPIO_ERROR(s_info.gpio, ret);
49 _D("GPIO_%d value: %d initial: %d, prev: %d", GPIO_PIN, gpio_value, s_info.gpio_initial_value, s_info.gpio_prev_value);
51 if (gpio_value != s_info.gpio_initial_value && s_info.gpio_prev_value == s_info.gpio_initial_value) {
52 lap_counter_get_lap_time();
56 s_info.gpio_prev_value = gpio_value;
59 void resource_lap_counter_init(void)
61 int ret = PERIPHERAL_ERROR_NONE;
63 ret = peripheral_gpio_open(GPIO_PIN, &s_info.gpio);
64 retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
66 ret = peripheral_gpio_set_direction(s_info.gpio, PERIPHERAL_GPIO_DIRECTION_IN);
67 CHECK_GPIO_ERROR(s_info.gpio, ret);
69 ret = peripheral_gpio_set_edge_mode(s_info.gpio, PERIPHERAL_GPIO_EDGE_BOTH);
70 CHECK_GPIO_ERROR(s_info.gpio, ret);
72 ret = peripheral_gpio_set_interrupted_cb(s_info.gpio, _gpio_interrupted_cb, NULL);
73 CHECK_GPIO_ERROR(s_info.gpio, ret);
75 ret = peripheral_gpio_read(s_info.gpio, &s_info.gpio_initial_value);
76 CHECK_GPIO_ERROR(s_info.gpio, ret);
78 _D("GPIO_%d initial value: %d", GPIO_PIN, s_info.gpio_initial_value);
79 s_info.gpio_prev_value = s_info.gpio_initial_value;
82 void resource_lap_counter_destroy(void)
84 peripheral_gpio_unset_interrupted_cb(s_info.gpio);
85 peripheral_gpio_close(s_info.gpio);