Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | /*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <ztest.h>
#include <gpio.h>
#include <pinmux.h>
#include <aio_comparator.h>
#define AIO_CMP_DEV_NAME CONFIG_AIO_COMPARATOR_0_NAME
#define PINMUX_NAME CONFIG_PINMUX_NAME
#ifdef CONFIG_ARC
#define GPIO_DEV_NAME CONFIG_GPIO_QMSI_SS_0_NAME
#else
#define GPIO_DEV_NAME CONFIG_GPIO_QMSI_0_NAME
#endif
#if defined(CONFIG_BOARD_QUARK_SE_C1000_DEVBOARD)
#define PIN_OUT 15 /* GPIO15 */
#define PIN_IN 10 /* GPIO_SS_AIN_10 */
#elif defined(CONFIG_BOARD_QUARK_SE_C1000_DEVBOARD_SS)
#define PIN_OUT 3 /* GPIO_SS_3 */
#define PIN_IN 10 /* GPIO_SS_AIN_10 */
#elif defined(CONFIG_BOARD_QUARK_D2000_CRB)
#define PIN_OUT 8 /* GPIO_8 */
#define PIN_IN 10 /* AIN_10 */
#elif defined(CONFIG_BOARD_ARDUINO_101)
#define PIN_OUT 16 /* GPIO_16 */
#define PIN_IN 10 /* AIN_10 */
#elif defined(CONFIG_BOARD_ARDUINO_101_SSS)
#define PIN_OUT 3 /* GPIO_SS_3 */
#define PIN_IN 10 /* AIN_10 */
#endif
volatile int cb_cnt;
static struct device *aio_dev;
static struct device *gpio_dev;
static void callback(void *param)
{
int polarity = *(int *)param;
cb_cnt++;
TC_PRINT("*** callback triggered %s\n",
(polarity == AIO_CMP_POL_RISE) ? "rising" : "falling"
);
if (polarity == AIO_CMP_POL_FALL) {
gpio_pin_write(gpio_dev, PIN_OUT, 1);
} else {
gpio_pin_write(gpio_dev, PIN_OUT, 0);
}
if (aio_cmp_get_pending_int(aio_dev)) {
TC_PRINT("Catch aio_cmp pending interrupt\n");
} else {
TC_PRINT("Fail to catch aio_cmp pending interrupt\n");
}
aio_cmp_disable(aio_dev, PIN_IN);
}
static int set_aio_callback(int polarity, int disable)
{
struct device *pinmux = device_get_binding(PINMUX_NAME);
if (!pinmux) {
TC_PRINT("Cannot get PINMUX\n");
return TC_FAIL;
}
aio_dev = device_get_binding(AIO_CMP_DEV_NAME);
if (!aio_dev) {
TC_PRINT("AIO Device binding failed\n");
return TC_FAIL;
}
gpio_dev = device_get_binding(GPIO_DEV_NAME);
if (!gpio_dev) {
TC_PRINT("GPIO Device binding failed\n");
return TC_FAIL;
}
if (gpio_pin_configure(gpio_dev, PIN_OUT, GPIO_DIR_OUT)) {
TC_PRINT("Fail to configure GPIO Pin %d\n", PIN_OUT);
return TC_FAIL;
}
if (pinmux_pin_set(pinmux, PIN_IN, PINMUX_FUNC_B)) {
TC_PRINT("Fail to set pin func, %u\n", PIN_IN);
return TC_FAIL;
}
gpio_pin_write(gpio_dev, PIN_OUT,
(polarity == AIO_CMP_POL_RISE) ? 0 : 1);
/* config AIN callback */
zassert_true(aio_cmp_configure(aio_dev, PIN_IN,
polarity, AIO_CMP_REF_B,
callback, (void *)aio_dev) == 0,
"ERROR registering callback");
if (disable == 1) {
if (aio_cmp_disable(aio_dev, PIN_IN)) {
TC_PRINT("Fail to disable callback\n");
return TC_FAIL;
}
}
k_sleep(100);
cb_cnt = 0;
k_sleep(100);
gpio_pin_write(gpio_dev, PIN_OUT,
(polarity == AIO_CMP_POL_RISE) ? 1 : 0);
k_sleep(1000);
TC_PRINT("... cb_cnt = %d\n", cb_cnt);
return TC_PASS;
}
void test_aio_callback_rise(void)
{
set_aio_callback(AIO_CMP_POL_RISE, 0);
zassert_true(cb_cnt == 1, "callback is not invoked correctly");
}
void test_aio_callback_fall(void)
{
set_aio_callback(AIO_CMP_POL_FALL, 0);
zassert_true(cb_cnt == 1, "callback is not invoked correctly");
}
void test_aio_callback_rise_disable(void)
{
set_aio_callback(AIO_CMP_POL_RISE, 1);
zassert_true(cb_cnt == 0, "callback is not invoked correctly");
}
|