Skip to content

Commit 9883ce7

Browse files
committed
Bring FPGA-Test-Shield tests into Mbed-os master.
1 parent f18e336 commit 9883ce7

File tree

8 files changed

+1623
-0
lines changed

8 files changed

+1623
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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 */
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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.01f // 1%
43+
44+
const PinList *form_factor = pinmap_ff_default_pins();
45+
const PinList *restricted = pinmap_restricted_pins();
46+
47+
MbedTester tester(form_factor, restricted);
48+
49+
void analogout_init_free(PinName pin)
50+
{
51+
dac_t analogout;
52+
53+
analogout_debug_printf("Analog output init/free test on pin=%s (%i)\r\n", pinmap_ff_default_pin_to_string(pin), pin);
54+
55+
analogout_init(&analogout, pin);
56+
analogout_free(&analogout);
57+
}
58+
59+
void analogout_test(PinName pin)
60+
{
61+
analogout_debug_printf("Analog input test on pin %s (%i)\r\n", pinmap_ff_default_pin_to_string(pin), pin);
62+
63+
tester.reset();
64+
tester.peripherals_reset();
65+
66+
/* Test analog input */
67+
68+
dac_t analogout;
69+
analogout_init(&analogout, pin);
70+
71+
tester.set_sample_adc(true);//begin ADC sampling on the FPGA
72+
73+
float anin;
74+
float i;
75+
for (i = 0.0f; i < 0.304f; i += 0.0303f) {//0V-1V, float
76+
analogout_write(&analogout, i);
77+
anin = tester.get_analog_in();
78+
TEST_ASSERT_FLOAT_WITHIN(DELTA_FLOAT, (i * 3.3f), anin);
79+
}
80+
81+
i = 0.0f;
82+
for (uint16_t i_d16 = 0; i_d16 < 19851; i_d16 += 1985) {//0V-1V, 16-bit
83+
analogout_write_u16(&analogout, i_d16);
84+
anin = tester.get_analog_in();
85+
TEST_ASSERT_FLOAT_WITHIN(DELTA_FLOAT, (i * 3.3f), anin);
86+
i += 0.0303f;
87+
}
88+
89+
analogout_free(&analogout);
90+
91+
tester.set_sample_adc(false);//stop ADC sampling on the FPGA
92+
93+
// power analysis
94+
// uint64_t sum;
95+
// uint32_t samples;
96+
// uint64_t cycles;
97+
// tester.get_anin_sum_samples_cycles(0, &sum, &samples, &cycles);
98+
// printf("ANIN0\r\n");
99+
// printf("Sum: %llu\r\n", sum);
100+
// printf("Num power samples: %d\r\n", samples);
101+
// printf("Num power cycles: %llu\r\n", cycles);
102+
// printf("ANIN0 voltage: %.6fV\r\n", tester.get_anin_voltage(0));
103+
}
104+
105+
Case cases[] = {
106+
// This will be run for all pins
107+
Case("Analogout - init test", all_ports<AnalogoutPort, DefaultFormFactor, analogout_init_free>),
108+
109+
// This will be run for single pin
110+
Case("Analogout - write/read test", all_ports<AnalogoutPort, DefaultFormFactor, analogout_test>),
111+
};
112+
113+
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
114+
{
115+
GREENTEA_SETUP(120, "default_auto");
116+
return greentea_test_setup_handler(number_of_cases);
117+
}
118+
119+
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
120+
121+
int main()
122+
{
123+
Harness::run(specification);
124+
}
125+
126+
#endif /* !DEVICE_ANALOGOUT */

0 commit comments

Comments
 (0)