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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | /* * Copyright (c) 2016 Intel Corporation. * * SPDX-License-Identifier: Apache-2.0 */ /* * @addtogroup test_pwm_basic_operations * @{ * @defgroup t_pwm_basic_operations test_pwm_sample * @brief TestPurpose: verify PWM can work well when configure * through usec, nsec, or cycle. * @details * - Test Steps * -# Bind PWM_0 port 0 (This case uses port 1 on D2000). * -# Set PWM period and pulse using pwm_pin_set_cycles(), * pwm_pin_set_usec(), or pwm_pin_set_nsec(). * -# Use multimeter or other instruments to measure the output * from PWM_OUT_0. * - Expected Results * -# The output of PWM_OUT_0 will differ according to the value * of period and pulse. * Always on -> Period : Pulse (1 : 1) -> 3.3V * Half on -> Period : Pulse (2 : 1) -> 1.65V * Always off -> Period : Pulse (1 : 0) -> 0V * @} */ #include <device.h> #include <inttypes.h> #include <pwm.h> #include <zephyr.h> #include <ztest.h> #ifdef CONFIG_PWM_QMSI_DEV_NAME #define PWM_DEV_NAME CONFIG_PWM_QMSI_DEV_NAME #elif defined CONFIG_BOARD_COLIBRI_IMX7D_M4 #define PWM_DEV_NAME PWM_1_LABEL #elif defined CONFIG_BOARD_SAM_E70_XPLAINED #define PWM_DEV_NAME DT_ATMEL_SAM_PWM_0_LABEL #endif #ifdef CONFIG_BOARD_COLIBRI_IMX7D_M4 #define DEFAULT_PERIOD_CYCLE 1024 #define DEFAULT_PULSE_CYCLE 512 #define DEFAULT_PERIOD_USEC 2000 #define DEFAULT_PULSE_USEC 500 #define DEFAULT_PERIOD_NSEC 2000000 #define DEFAULT_PULSE_NSEC 500000 #else #define DEFAULT_PERIOD_CYCLE 64000 #define DEFAULT_PULSE_CYCLE 32000 #define DEFAULT_PERIOD_USEC 2000 #define DEFAULT_PULSE_USEC 1000 #define DEFAULT_PERIOD_NSEC 2000000 #define DEFAULT_PULSE_NSEC 1000000 #endif #ifdef CONFIG_BOARD_QUARK_D2000_CRB #include <pinmux.h> #define PINMUX_NAME CONFIG_PINMUX_NAME #define PWM1_PIN 24 #define DEFAULT_PWM_PORT 1 #elif defined CONFIG_BOARD_SAM_E70_XPLAINED #define DEFAULT_PWM_PORT 2 /* PWM on EXT2 connector, pin 8 */ #else #define DEFAULT_PWM_PORT 0 #endif #define UNIT_CYCLES 0 #define UNIT_USECS 1 #define UNIT_NSECS 2 static int test_task(u32_t port, u32_t period, u32_t pulse, u8_t unit) { TC_PRINT("[PWM]: %" PRIu8 ", [period]: %" PRIu32 ", [pulse]: %" PRIu32 "\n", port, period, pulse); struct device *pwm_dev = device_get_binding(PWM_DEV_NAME); if (!pwm_dev) { TC_PRINT("Cannot get PWM device\n"); return TC_FAIL; } #ifdef CONFIG_BOARD_QUARK_D2000_CRB struct device *pinmux = device_get_binding(PINMUX_NAME); u32_t function; if (!pinmux) { TC_PRINT("Cannot get PINMUX\n"); return TC_FAIL; } if (pinmux_pin_set(pinmux, PWM1_PIN, PINMUX_FUNC_C)) { TC_PRINT("Fail to set pin func, %u : %u\n", PWM1_PIN, PINMUX_FUNC_C); return TC_FAIL; } if (pinmux_pin_get(pinmux, PWM1_PIN, &function)) { TC_PRINT("Fail to get pin func\n"); return TC_FAIL; } if (function != PINMUX_FUNC_C) { TC_PRINT("Error. PINMUX get doesn't match PINMUX set\n"); return TC_FAIL; } #endif if (unit == UNIT_CYCLES) { /* Verify pwm_pin_set_cycles() */ if (pwm_pin_set_cycles(pwm_dev, port, period, pulse)) { TC_PRINT("Fail to set the period and pulse width\n"); return TC_FAIL; } } else if (unit == UNIT_USECS) { /* Verify pwm_pin_set_usec() */ if (pwm_pin_set_usec(pwm_dev, port, period, pulse)) { TC_PRINT("Fail to set the period and pulse width\n"); return TC_FAIL; } } else { /* unit == UNIT_NSECS */ /* Verify pwm_pin_set_nsec() */ if (pwm_pin_set_nsec(pwm_dev, port, period, pulse)) { TC_PRINT("Fail to set the period and pulse width\n"); return TC_FAIL; } } return TC_PASS; } void test_pwm_usec(void) { /* Period : Pulse (2000 : 1000), unit (usec). Voltage : 1.65V */ zassert_true(test_task(DEFAULT_PWM_PORT, DEFAULT_PERIOD_USEC, DEFAULT_PULSE_USEC, UNIT_USECS) == TC_PASS, NULL); k_sleep(1000); /* Period : Pulse (2000 : 2000), unit (usec). Voltage : 3.3V */ zassert_true(test_task(DEFAULT_PWM_PORT, DEFAULT_PERIOD_USEC, DEFAULT_PERIOD_USEC, UNIT_USECS) == TC_PASS, NULL); k_sleep(1000); /* Period : Pulse (2000 : 0), unit (usec). Voltage : 0V */ zassert_true(test_task(DEFAULT_PWM_PORT, DEFAULT_PERIOD_USEC, 0, UNIT_USECS) == TC_PASS, NULL); k_sleep(1000); } void test_pwm_nsec(void) { /* Period : Pulse (2000000 : 1000000), unit (nsec). Voltage : 1.65V */ zassert_true(test_task(DEFAULT_PWM_PORT, DEFAULT_PERIOD_NSEC, DEFAULT_PULSE_NSEC, UNIT_NSECS) == TC_PASS, NULL); k_sleep(1000); /* Period : Pulse (2000000 : 2000000), unit (nsec). Voltage : 3.3V */ zassert_true(test_task(DEFAULT_PWM_PORT, DEFAULT_PERIOD_NSEC, DEFAULT_PERIOD_NSEC, UNIT_NSECS) == TC_PASS, NULL); k_sleep(1000); /* Period : Pulse (2000000 : 0), unit (nsec). Voltage : 0V */ zassert_true(test_task(DEFAULT_PWM_PORT, DEFAULT_PERIOD_NSEC, 0, UNIT_NSECS) == TC_PASS, NULL); k_sleep(1000); } void test_pwm_cycle(void) { /* Period : Pulse (64000 : 32000), unit (cycle). Voltage : 1.65V */ zassert_true(test_task(DEFAULT_PWM_PORT, DEFAULT_PERIOD_CYCLE, DEFAULT_PULSE_CYCLE, UNIT_CYCLES) == TC_PASS, NULL); k_sleep(1000); /* Period : Pulse (64000 : 64000), unit (cycle). Voltage : 3.3V */ zassert_true(test_task(DEFAULT_PWM_PORT, DEFAULT_PERIOD_CYCLE, DEFAULT_PERIOD_CYCLE, UNIT_CYCLES) == TC_PASS, NULL); k_sleep(1000); /* Period : Pulse (64000 : 0), unit (cycle). Voltage : 0V */ zassert_true(test_task(DEFAULT_PWM_PORT, DEFAULT_PERIOD_CYCLE, 0, UNIT_CYCLES) == TC_PASS, NULL); } |