|
| 1 | +/* |
| 2 | + * Copyright (c) 2019, Arm Limited and affiliates. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#if !DEVICE_ANALOGIN |
| 19 | +#error [NOT_SUPPORTED] Analog in not supported for this target |
| 20 | +#elif !COMPONENT_FPGA_CI_TEST_SHIELD |
| 21 | +#error [NOT_SUPPORTED] FPGA CI Test Shield is needed to run this test |
| 22 | +#else |
| 23 | + |
| 24 | +#include "utest/utest.h" |
| 25 | +#include "unity/unity.h" |
| 26 | +#include "greentea-client/test_env.h" |
| 27 | + |
| 28 | +#include "mbed.h" |
| 29 | +#include "pinmap.h" |
| 30 | +#include "test_utils.h" |
| 31 | +#include "MbedTester.h" |
| 32 | + |
| 33 | +using namespace utest::v1; |
| 34 | + |
| 35 | +#define analogin_debug_printf(...) |
| 36 | + |
| 37 | +#define DELTA_FLOAT 0.01f // 1% |
| 38 | +#define DELTA_U16 655 // 1% |
| 39 | + |
| 40 | +const PinList *form_factor = pinmap_ff_default_pins(); |
| 41 | +const PinList *restricted = pinmap_restricted_pins(); |
| 42 | + |
| 43 | +MbedTester tester(form_factor, restricted); |
| 44 | + |
| 45 | +void analogin_init(PinName pin) |
| 46 | +{ |
| 47 | + analogin_t analogin; |
| 48 | + |
| 49 | + analogin_init(&analogin, pin); |
| 50 | +} |
| 51 | + |
| 52 | +void analogin_test(PinName pin) |
| 53 | +{ |
| 54 | + tester.reset(); |
| 55 | + tester.pin_map_set(pin, MbedTester::LogicalPinGPIO0); |
| 56 | + tester.select_peripheral(MbedTester::PeripheralGPIO); |
| 57 | + |
| 58 | + /* Test analog input */ |
| 59 | + |
| 60 | + analogin_t analogin; |
| 61 | + analogin_init(&analogin, pin); |
| 62 | + |
| 63 | + tester.gpio_write(MbedTester::LogicalPinGPIO0, 1, true); |
| 64 | + TEST_ASSERT_FLOAT_WITHIN(DELTA_FLOAT, 1.0f, analogin_read(&analogin)); |
| 65 | + TEST_ASSERT_UINT16_WITHIN(DELTA_U16, 65535, analogin_read_u16(&analogin)); |
| 66 | + |
| 67 | + tester.gpio_write(MbedTester::LogicalPinGPIO0, 0, true); |
| 68 | + TEST_ASSERT_FLOAT_WITHIN(DELTA_FLOAT, 0.0f, analogin_read(&analogin)); |
| 69 | + TEST_ASSERT_UINT16_WITHIN(DELTA_U16, 0, analogin_read_u16(&analogin)); |
| 70 | + |
| 71 | + /* Set gpio back to Hi-Z */ |
| 72 | + tester.gpio_write(MbedTester::LogicalPinGPIO0, 0, false); |
| 73 | +} |
| 74 | + |
| 75 | +void analogin_full_test(PinName pin) |
| 76 | +{ |
| 77 | + /* Test analog input */ |
| 78 | + |
| 79 | + printf("Testing AnalogIn\r\n"); |
| 80 | + |
| 81 | + // Remap pins for test |
| 82 | + tester.reset(); |
| 83 | + |
| 84 | + // Reset tester stats and select GPIO |
| 85 | + tester.peripherals_reset(); |
| 86 | + tester.select_peripheral(MbedTester::PeripheralGPIO); |
| 87 | + |
| 88 | + analogin_t analogin; |
| 89 | + analogin_init(&analogin, pin); |
| 90 | + |
| 91 | + //enable analog MUX |
| 92 | + tester.set_mux_enable(true); |
| 93 | + |
| 94 | + //set MUX address |
| 95 | + tester.set_mux_addr(pin); |
| 96 | + |
| 97 | + float voltage = 0.0; |
| 98 | + bool enable = true; |
| 99 | + uint32_t period = 100; |
| 100 | + uint32_t duty_cycle = 0; |
| 101 | + //enable FPGA system PWM |
| 102 | + tester.set_analog_out(voltage, enable); |
| 103 | + |
| 104 | + TEST_ASSERT_EQUAL(true, tester.get_pwm_enable()); |
| 105 | + TEST_ASSERT_EQUAL(period, tester.get_pwm_period()); |
| 106 | + TEST_ASSERT_EQUAL(duty_cycle, tester.get_pwm_cycles_high()); |
| 107 | + // test duty cycles 0-100% at 1000ns period |
| 108 | + for (int i = 0; i < 11; i += 1) { |
| 109 | + voltage = (float)i * 0.1f; |
| 110 | + duty_cycle = 10 * i; |
| 111 | + tester.set_analog_out(voltage, enable); |
| 112 | + wait_us(10); |
| 113 | + //read analog input |
| 114 | + //assert pwm duty_cycle is correct based on set analog voltage |
| 115 | + TEST_ASSERT_EQUAL(duty_cycle, tester.get_pwm_cycles_high()); |
| 116 | + TEST_ASSERT_FLOAT_WITHIN(DELTA_FLOAT, 0.01f * (float)duty_cycle, analogin_read(&analogin)); |
| 117 | + } |
| 118 | + tester.set_pwm_enable(false); |
| 119 | + TEST_ASSERT_EQUAL(false, tester.get_pwm_enable()); |
| 120 | + wait_us(10); |
| 121 | + |
| 122 | + // assert Mbed pin correctly drives AnalogMuxIn |
| 123 | + tester.pin_map_set(pin, MbedTester::LogicalPinGPIO0); |
| 124 | + |
| 125 | + tester.gpio_write(MbedTester::LogicalPinGPIO0, 0, true); |
| 126 | + TEST_ASSERT_EQUAL(0, tester.sys_pin_read(MbedTester::AnalogMuxIn)); |
| 127 | + wait_us(10); |
| 128 | + tester.gpio_write(MbedTester::LogicalPinGPIO0, 1, true); |
| 129 | + TEST_ASSERT_EQUAL(1, tester.sys_pin_read(MbedTester::AnalogMuxIn)); |
| 130 | + wait_us(10); |
| 131 | + tester.gpio_write(MbedTester::LogicalPinGPIO0, 0, true); |
| 132 | + TEST_ASSERT_EQUAL(0, tester.sys_pin_read(MbedTester::AnalogMuxIn)); |
| 133 | + wait_us(10); |
| 134 | + tester.gpio_write(MbedTester::LogicalPinGPIO0, 0, false); |
| 135 | + wait_us(10); |
| 136 | + tester.set_mux_enable(false); |
| 137 | + wait_us(10); |
| 138 | + |
| 139 | + TEST_ASSERT_EQUAL(1, tester.self_test_control_current());//assert control channel still functioning properly |
| 140 | +} |
| 141 | + |
| 142 | +Case cases[] = { |
| 143 | + // This will be run for all pins |
| 144 | + Case("AnalogIn - init test", all_ports<AnaloginPort, DefaultFormFactor, analogin_init>), |
| 145 | +#if defined(FULL_TEST_SHIELD) |
| 146 | + Case("AnalogIn - full test", all_ports<AnaloginPort, DefaultFormFactor, analogin_full_test>), |
| 147 | +#endif |
| 148 | + |
| 149 | + // This will be run for single pin |
| 150 | + Case("AnalogIn - read test", all_ports<AnaloginPort, DefaultFormFactor, analogin_test>), |
| 151 | +}; |
| 152 | + |
| 153 | +utest::v1::status_t greentea_test_setup(const size_t number_of_cases) |
| 154 | +{ |
| 155 | + GREENTEA_SETUP(120, "default_auto"); |
| 156 | + return greentea_test_setup_handler(number_of_cases); |
| 157 | +} |
| 158 | + |
| 159 | +Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); |
| 160 | + |
| 161 | +int main() |
| 162 | +{ |
| 163 | + Harness::run(specification); |
| 164 | +} |
| 165 | + |
| 166 | +#endif /* !DEVICE_ANALOGIN */ |
0 commit comments