|
| 1 | +/***************************************************************************//** |
| 2 | + * @file basic_example.c |
| 3 | + * @brief Basic example source file for eval-adg1736 project. |
| 4 | + * @author Alexandru Vasile Popa (Alexandruvasile.Popa@analog.com) |
| 5 | +******************************************************************************** |
| 6 | + * Copyright 2026(c) Analog Devices, Inc. |
| 7 | + * |
| 8 | + * Redistribution and use in source and binary forms, with or without |
| 9 | + * modification, are permitted provided that the following conditions are met: |
| 10 | + * |
| 11 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer. |
| 13 | + * |
| 14 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | + * this list of conditions and the following disclaimer in the documentation |
| 16 | + * and/or other materials provided with the distribution. |
| 17 | + * |
| 18 | + * 3. Neither the name of Analog Devices, Inc. nor the names of its |
| 19 | + * contributors may be used to endorse or promote products derived from this |
| 20 | + * software without specific prior written permission. |
| 21 | + * |
| 22 | + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. "AS IS" AND ANY EXPRESS OR |
| 23 | + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 24 | + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 25 | + * EVENT SHALL ANALOG DEVICES, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 26 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
| 28 | + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 31 | + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | +*******************************************************************************/ |
| 33 | + |
| 34 | +#include "common_data.h" |
| 35 | +#include "no_os_delay.h" |
| 36 | +#include "no_os_print_log.h" |
| 37 | +#include "no_os_gpio.h" |
| 38 | +#include "adg1736.h" |
| 39 | + |
| 40 | +int example_main(void) |
| 41 | +{ |
| 42 | + struct adg1736_dev *dev; |
| 43 | + struct no_os_uart_desc *uart; |
| 44 | + struct no_os_gpio_desc *led_green, *led_red; |
| 45 | + struct no_os_gpio_desc *gpio_d, *gpio_s; |
| 46 | + enum adg1736_state target_state; |
| 47 | + enum adg1736_switch test_sw; |
| 48 | + uint8_t s_val; |
| 49 | + int pass; |
| 50 | + int ret; |
| 51 | + |
| 52 | + ret = no_os_uart_init(&uart, &adg1736_uart_ip); |
| 53 | + if (ret) |
| 54 | + return ret; |
| 55 | + no_os_uart_stdio(uart); |
| 56 | + |
| 57 | + pr_info("\r\n=== ADG1736 Switch Test ===\r\n"); |
| 58 | + pr_info("Testing SW%d, %s side\r\n", TEST_SWITCH + 1, |
| 59 | + TEST_SIDE == 0 ? "A" : "B"); |
| 60 | + pr_info("Pins: IN=P1.6, EN=P1.7, D=P1.8, S=P1.9\r\n\r\n"); |
| 61 | + |
| 62 | + /* Initialize LEDs */ |
| 63 | + ret = no_os_gpio_get(&led_green, &led_green_ip); |
| 64 | + if (ret) |
| 65 | + goto err_uart; |
| 66 | + ret = no_os_gpio_direction_output(led_green, NO_OS_GPIO_HIGH); |
| 67 | + if (ret) |
| 68 | + goto err_led_green; |
| 69 | + |
| 70 | + ret = no_os_gpio_get(&led_red, &led_red_ip); |
| 71 | + if (ret) |
| 72 | + goto err_led_green; |
| 73 | + ret = no_os_gpio_direction_output(led_red, NO_OS_GPIO_HIGH); |
| 74 | + if (ret) |
| 75 | + goto err_led_red; |
| 76 | + |
| 77 | + /* Initialize test GPIOs */ |
| 78 | + ret = no_os_gpio_get(&gpio_d, &gpio_d_ip); |
| 79 | + if (ret) |
| 80 | + goto err_led_red; |
| 81 | + ret = no_os_gpio_direction_output(gpio_d, NO_OS_GPIO_HIGH); |
| 82 | + if (ret) |
| 83 | + goto err_d; |
| 84 | + |
| 85 | + ret = no_os_gpio_get(&gpio_s, &gpio_s_ip); |
| 86 | + if (ret) |
| 87 | + goto err_d; |
| 88 | + ret = no_os_gpio_direction_input(gpio_s); |
| 89 | + if (ret) |
| 90 | + goto err_s; |
| 91 | + |
| 92 | + /* Initialize ADG1736 driver */ |
| 93 | + ret = adg1736_init(&dev, &adg1736_ip); |
| 94 | + if (ret) |
| 95 | + goto err_s; |
| 96 | + |
| 97 | + /* Determine target state and switch based on TEST_SIDE and TEST_SWITCH */ |
| 98 | + target_state = (TEST_SIDE == 0) ? ADG1736_CONNECT_A : ADG1736_CONNECT_B; |
| 99 | + test_sw = (TEST_SWITCH == 0) ? ADG1736_SW1 : ADG1736_SW2; |
| 100 | + |
| 101 | + pr_info("Running continuous test...\r\n\r\n"); |
| 102 | + |
| 103 | + while (1) { |
| 104 | + /* Test 1: Set to target position, S should see D (HIGH) */ |
| 105 | + ret = adg1736_set_switch_state(dev, test_sw, target_state); |
| 106 | + if (ret) |
| 107 | + break; |
| 108 | + no_os_mdelay(10); |
| 109 | + ret = no_os_gpio_get_value(gpio_s, &s_val); |
| 110 | + if (ret) |
| 111 | + break; |
| 112 | + pr_info("IN->%c: S=%d (expect 1) %s\r\n", |
| 113 | + TEST_SIDE == 0 ? 'A' : 'B', |
| 114 | + s_val, s_val == 1 ? "[PASS]" : "[FAIL]"); |
| 115 | + pass = (s_val == 1); |
| 116 | + |
| 117 | + no_os_mdelay(1000); |
| 118 | + |
| 119 | + /* Test 2: Set to opposite position, S should be disconnected */ |
| 120 | + ret = adg1736_set_switch_state(dev, test_sw, |
| 121 | + target_state == ADG1736_CONNECT_A ? |
| 122 | + ADG1736_CONNECT_B : ADG1736_CONNECT_A); |
| 123 | + if (ret) |
| 124 | + break; |
| 125 | + no_os_mdelay(10); |
| 126 | + ret = no_os_gpio_get_value(gpio_s, &s_val); |
| 127 | + if (ret) |
| 128 | + break; |
| 129 | + pr_info("IN->%c: S=%d (expect 0) %s\r\n", |
| 130 | + TEST_SIDE == 0 ? 'B' : 'A', |
| 131 | + s_val, s_val == 0 ? "[PASS]" : "[FAIL]"); |
| 132 | + pass = pass && (s_val == 0); |
| 133 | + |
| 134 | + /* Update LEDs */ |
| 135 | + if (pass) { |
| 136 | + no_os_gpio_set_value(led_green, NO_OS_GPIO_LOW); |
| 137 | + no_os_gpio_set_value(led_red, NO_OS_GPIO_HIGH); |
| 138 | + pr_info("=== PASS ===\r\n\r\n"); |
| 139 | + } else { |
| 140 | + no_os_gpio_set_value(led_green, NO_OS_GPIO_HIGH); |
| 141 | + no_os_gpio_set_value(led_red, NO_OS_GPIO_LOW); |
| 142 | + pr_info("=== FAIL ===\r\n\r\n"); |
| 143 | + } |
| 144 | + |
| 145 | + no_os_mdelay(2000); |
| 146 | + } |
| 147 | + |
| 148 | + adg1736_remove(dev); |
| 149 | +err_s: |
| 150 | + no_os_gpio_remove(gpio_s); |
| 151 | +err_d: |
| 152 | + no_os_gpio_remove(gpio_d); |
| 153 | +err_led_red: |
| 154 | + no_os_gpio_remove(led_red); |
| 155 | +err_led_green: |
| 156 | + no_os_gpio_remove(led_green); |
| 157 | +err_uart: |
| 158 | + no_os_uart_remove(uart); |
| 159 | + return ret; |
| 160 | +} |
0 commit comments