|
| 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_ANALOGOUT |
| 19 | +#error [NOT_SUPPORTED] Analog out 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 | +#include <inttypes.h> |
| 28 | +#include "mbed.h" |
| 29 | + |
| 30 | +using namespace utest::v1; |
| 31 | + |
| 32 | +#include "MbedTester.h" |
| 33 | +#include "pinmap.h" |
| 34 | +#include "test_utils.h" |
| 35 | + |
| 36 | +#if !DEVICE_ANALOGOUT |
| 37 | +#error [NOT_SUPPORTED] Analog out not supported for this target |
| 38 | +#endif |
| 39 | + |
| 40 | +#define analogout_debug_printf(...) |
| 41 | + |
| 42 | +#define DELTA_FLOAT 0.03f // 3% |
| 43 | + |
| 44 | +/* Enable for power analysis */ |
| 45 | +#define DEBUG 0 |
| 46 | + |
| 47 | +const PinList *form_factor = pinmap_ff_default_pins(); |
| 48 | +const PinList *restricted = pinmap_restricted_pins(); |
| 49 | + |
| 50 | +MbedTester tester(form_factor, restricted); |
| 51 | + |
| 52 | +void analogout_init_free(PinName pin) |
| 53 | +{ |
| 54 | + dac_t analogout; |
| 55 | + |
| 56 | + analogout_debug_printf("Analog output init/free test on pin=%s (%i)\r\n", pinmap_ff_default_pin_to_string(pin), pin); |
| 57 | + |
| 58 | + analogout_init(&analogout, pin); |
| 59 | + analogout_free(&analogout); |
| 60 | +} |
| 61 | + |
| 62 | +void analogout_test(PinName pin) |
| 63 | +{ |
| 64 | + analogout_debug_printf("Analog input test on pin %s (%i)\r\n", pinmap_ff_default_pin_to_string(pin), pin); |
| 65 | + |
| 66 | + tester.reset(); |
| 67 | + tester.peripherals_reset(); |
| 68 | + |
| 69 | + /* Test analog input */ |
| 70 | + |
| 71 | + dac_t analogout; |
| 72 | + analogout_init(&analogout, pin); |
| 73 | + |
| 74 | + tester.set_sample_adc(true);//begin ADC sampling on the FPGA |
| 75 | + |
| 76 | + float anin; |
| 77 | + float i; |
| 78 | + for (i = 0.0f; i < 0.304f; i += 0.0303f) {//0V-1V, float |
| 79 | + analogout_write(&analogout, i); |
| 80 | + anin = tester.get_analog_in(); |
| 81 | + TEST_ASSERT_FLOAT_WITHIN(DELTA_FLOAT, (i * 3.3f), anin); |
| 82 | + } |
| 83 | + |
| 84 | + i = 0.0f; |
| 85 | + for (uint16_t i_d16 = 0; i_d16 < 19851; i_d16 += 1985) {//0V-1V, 16-bit |
| 86 | + analogout_write_u16(&analogout, i_d16); |
| 87 | + anin = tester.get_analog_in(); |
| 88 | + TEST_ASSERT_FLOAT_WITHIN(DELTA_FLOAT, (i * 3.3f), anin); |
| 89 | + i += 0.0303f; |
| 90 | + } |
| 91 | + |
| 92 | + analogout_free(&analogout); |
| 93 | + |
| 94 | + tester.set_sample_adc(false);//stop ADC sampling on the FPGA |
| 95 | + |
| 96 | +#if DEBUG |
| 97 | + // power analysis |
| 98 | + uint64_t sum; |
| 99 | + uint32_t samples; |
| 100 | + uint64_t cycles; |
| 101 | + tester.get_anin_sum_samples_cycles(0, &sum, &samples, &cycles); |
| 102 | + printf("ANIN0\r\n"); |
| 103 | + printf("Sum: %llu\r\n", sum); |
| 104 | + printf("Num power samples: %d\r\n", samples); |
| 105 | + printf("Num power cycles: %llu\r\n", cycles); |
| 106 | + printf("ANIN0 voltage: %.6fV\r\n", tester.get_anin_voltage(0)); |
| 107 | +#endif |
| 108 | +} |
| 109 | + |
| 110 | +Case cases[] = { |
| 111 | + // This will be run for all pins |
| 112 | + Case("Analogout - init test", all_ports<AnalogoutPort, DefaultFormFactor, analogout_init_free>), |
| 113 | + |
| 114 | + // This will be run for single pin |
| 115 | + Case("Analogout - write/read test", all_ports<AnalogoutPort, DefaultFormFactor, analogout_test>), |
| 116 | +}; |
| 117 | + |
| 118 | +utest::v1::status_t greentea_test_setup(const size_t number_of_cases) |
| 119 | +{ |
| 120 | + GREENTEA_SETUP(120, "default_auto"); |
| 121 | + return greentea_test_setup_handler(number_of_cases); |
| 122 | +} |
| 123 | + |
| 124 | +Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); |
| 125 | + |
| 126 | +int main() |
| 127 | +{ |
| 128 | + Harness::run(specification); |
| 129 | +} |
| 130 | + |
| 131 | +#endif /* !DEVICE_ANALOGOUT */ |
0 commit comments