test_pwm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include "unity.h"
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "soc/soc_caps.h"
  12. #include "hal/gpio_hal.h"
  13. #include "esp_rom_gpio.h"
  14. #include "esp_private/esp_clk.h"
  15. #if SOC_MCPWM_SUPPORTED
  16. #include "soc/mcpwm_periph.h"
  17. #include "driver/pulse_cnt.h"
  18. #include "driver/mcpwm.h"
  19. #include "driver/gpio.h"
  20. #define TEST_PWMA_GPIO (2)
  21. #define TEST_PWMB_GPIO (4)
  22. #define TEST_FAULT_GPIO (21)
  23. #define TEST_SYNC_GPIO_0 (21)
  24. #define TEST_SYNC_GPIO_1 (18)
  25. #define TEST_SYNC_GPIO_2 (19)
  26. #define TEST_CAP_GPIO (21)
  27. #define MCPWM_GROUP_CLK_SRC_HZ 160000000
  28. #define MCPWM_TEST_GROUP_CLK_HZ (MCPWM_GROUP_CLK_SRC_HZ / 16)
  29. #define MCPWM_TEST_TIMER_CLK_HZ (MCPWM_TEST_GROUP_CLK_HZ / 10)
  30. const static mcpwm_io_signals_t pwma[] = {MCPWM0A, MCPWM1A, MCPWM2A};
  31. const static mcpwm_io_signals_t pwmb[] = {MCPWM0B, MCPWM1B, MCPWM2B};
  32. const static mcpwm_fault_signal_t fault_sig_array[] = {MCPWM_SELECT_F0, MCPWM_SELECT_F1, MCPWM_SELECT_F2};
  33. const static mcpwm_io_signals_t fault_io_sig_array[] = {MCPWM_FAULT_0, MCPWM_FAULT_1, MCPWM_FAULT_2};
  34. const static mcpwm_sync_signal_t sync_sig_array[] = {MCPWM_SELECT_GPIO_SYNC0, MCPWM_SELECT_GPIO_SYNC1, MCPWM_SELECT_GPIO_SYNC2};
  35. const static mcpwm_io_signals_t sync_io_sig_array[] = {MCPWM_SYNC_0, MCPWM_SYNC_1, MCPWM_SYNC_2};
  36. const static mcpwm_capture_signal_t cap_sig_array[] = {MCPWM_SELECT_CAP0, MCPWM_SELECT_CAP1, MCPWM_SELECT_CAP2};
  37. const static mcpwm_io_signals_t cap_io_sig_array[] = {MCPWM_CAP_0, MCPWM_CAP_1, MCPWM_CAP_2};
  38. static pcnt_unit_handle_t pcnt_unit_a;
  39. static pcnt_channel_handle_t pcnt_chan_a;
  40. static pcnt_unit_handle_t pcnt_unit_b;
  41. static pcnt_channel_handle_t pcnt_chan_b;
  42. // This GPIO init function is almost the same to public API `mcpwm_gpio_init()`, except that
  43. // this function will configure all MCPWM GPIOs into output and input capable
  44. // which is useful to simulate a trigger source
  45. static esp_err_t test_mcpwm_gpio_init(mcpwm_unit_t mcpwm_num, mcpwm_io_signals_t io_signal, int gpio_num)
  46. {
  47. if (gpio_num < 0) { // ignore on minus gpio number
  48. return ESP_OK;
  49. }
  50. if (io_signal <= MCPWM2B) { // Generator output signal
  51. gpio_set_direction(gpio_num, GPIO_MODE_INPUT_OUTPUT);
  52. int operator_id = io_signal / 2;
  53. int generator_id = io_signal % 2;
  54. esp_rom_gpio_connect_out_signal(gpio_num, mcpwm_periph_signals.groups[mcpwm_num].operators[operator_id].generators[generator_id].pwm_sig, 0, 0);
  55. } else if (io_signal <= MCPWM_SYNC_2) { // External sync input signal
  56. gpio_set_direction(gpio_num, GPIO_MODE_INPUT_OUTPUT);
  57. int gpio_sync_id = io_signal - MCPWM_SYNC_0;
  58. esp_rom_gpio_connect_in_signal(gpio_num, mcpwm_periph_signals.groups[mcpwm_num].gpio_synchros[gpio_sync_id].sync_sig, 0);
  59. } else if (io_signal <= MCPWM_FAULT_2) { // Fault input signal
  60. gpio_set_direction(gpio_num, GPIO_MODE_INPUT_OUTPUT);
  61. int fault_id = io_signal - MCPWM_FAULT_0;
  62. esp_rom_gpio_connect_in_signal(gpio_num, mcpwm_periph_signals.groups[mcpwm_num].gpio_faults[fault_id].fault_sig, 0);
  63. } else if (io_signal >= MCPWM_CAP_0 && io_signal <= MCPWM_CAP_2) { // Capture input signal
  64. gpio_set_direction(gpio_num, GPIO_MODE_INPUT_OUTPUT);
  65. int capture_id = io_signal - MCPWM_CAP_0;
  66. esp_rom_gpio_connect_in_signal(gpio_num, mcpwm_periph_signals.groups[mcpwm_num].captures[capture_id].cap_sig, 0);
  67. }
  68. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[gpio_num], PIN_FUNC_GPIO);
  69. return ESP_OK;
  70. }
  71. static void mcpwm_setup_testbench(mcpwm_unit_t group, mcpwm_timer_t timer, uint32_t pwm_freq, float pwm_duty,
  72. unsigned long int group_resolution, unsigned long int timer_resolution)
  73. {
  74. mcpwm_io_signals_t mcpwm_a = pwma[timer];
  75. TEST_ESP_OK(test_mcpwm_gpio_init(group, mcpwm_a, TEST_PWMA_GPIO));
  76. mcpwm_io_signals_t mcpwm_b = pwmb[timer];
  77. TEST_ESP_OK(test_mcpwm_gpio_init(group, mcpwm_b, TEST_PWMB_GPIO));
  78. // Set PWM freq and duty, start immediately
  79. mcpwm_config_t pwm_config = {
  80. .frequency = pwm_freq,
  81. .cmpr_a = pwm_duty,
  82. .cmpr_b = pwm_duty,
  83. .counter_mode = MCPWM_UP_COUNTER,
  84. .duty_mode = MCPWM_DUTY_MODE_0,
  85. };
  86. mcpwm_group_set_resolution(group, group_resolution);
  87. mcpwm_timer_set_resolution(group, timer, timer_resolution);
  88. TEST_ESP_OK(mcpwm_init(group, timer, &pwm_config));
  89. }
  90. static void pcnt_setup_testbench(void)
  91. {
  92. // PWMA <--> PCNT UNIT0
  93. pcnt_unit_config_t unit_a_config = {
  94. .high_limit = 10000,
  95. .low_limit = -10000,
  96. };
  97. TEST_ESP_OK(pcnt_new_unit(&unit_a_config, &pcnt_unit_a));
  98. pcnt_chan_config_t chan_a_config = {
  99. .edge_gpio_num = TEST_PWMA_GPIO,
  100. .level_gpio_num = -1, // don't care level signal
  101. };
  102. TEST_ESP_OK(pcnt_new_channel(pcnt_unit_a, &chan_a_config, &pcnt_chan_a));
  103. TEST_ESP_OK(pcnt_channel_set_edge_action(pcnt_chan_a, PCNT_CHANNEL_EDGE_ACTION_INCREASE, PCNT_CHANNEL_EDGE_ACTION_HOLD));
  104. TEST_ESP_OK(pcnt_channel_set_level_action(pcnt_chan_a, PCNT_CHANNEL_LEVEL_ACTION_KEEP, PCNT_CHANNEL_LEVEL_ACTION_KEEP));
  105. TEST_ESP_OK(pcnt_unit_enable(pcnt_unit_a));
  106. // PWMB <--> PCNT UNIT1
  107. pcnt_unit_config_t unit_b_config = {
  108. .high_limit = 10000,
  109. .low_limit = -10000,
  110. };
  111. TEST_ESP_OK(pcnt_new_unit(&unit_b_config, &pcnt_unit_b));
  112. pcnt_chan_config_t chan_b_config = {
  113. .edge_gpio_num = TEST_PWMB_GPIO,
  114. .level_gpio_num = -1, // don't care level signal
  115. };
  116. TEST_ESP_OK(pcnt_new_channel(pcnt_unit_b, &chan_b_config, &pcnt_chan_b));
  117. TEST_ESP_OK(pcnt_channel_set_edge_action(pcnt_chan_b, PCNT_CHANNEL_EDGE_ACTION_INCREASE, PCNT_CHANNEL_EDGE_ACTION_HOLD));
  118. TEST_ESP_OK(pcnt_channel_set_level_action(pcnt_chan_b, PCNT_CHANNEL_LEVEL_ACTION_KEEP, PCNT_CHANNEL_LEVEL_ACTION_KEEP));
  119. TEST_ESP_OK(pcnt_unit_enable(pcnt_unit_b));
  120. }
  121. static void pcnt_tear_testbench(void)
  122. {
  123. TEST_ESP_OK(pcnt_unit_disable(pcnt_unit_a));
  124. TEST_ESP_OK(pcnt_unit_disable(pcnt_unit_b));
  125. TEST_ESP_OK(pcnt_del_channel(pcnt_chan_a));
  126. TEST_ESP_OK(pcnt_del_channel(pcnt_chan_b));
  127. TEST_ESP_OK(pcnt_del_unit(pcnt_unit_a));
  128. TEST_ESP_OK(pcnt_del_unit(pcnt_unit_b));
  129. }
  130. static uint32_t pcnt_get_pulse_number(pcnt_unit_handle_t pwm_pcnt_unit, int capture_window_ms)
  131. {
  132. int count_value = 0;
  133. TEST_ESP_OK(pcnt_unit_clear_count(pwm_pcnt_unit));
  134. TEST_ESP_OK(pcnt_unit_start(pwm_pcnt_unit));
  135. usleep(capture_window_ms * 1000);
  136. TEST_ESP_OK(pcnt_unit_stop(pwm_pcnt_unit));
  137. TEST_ESP_OK(pcnt_unit_get_count(pwm_pcnt_unit, &count_value));
  138. printf("count value: %d\r\n", count_value);
  139. return (uint32_t)count_value;
  140. }
  141. static void mcpwm_timer_duty_test(mcpwm_unit_t unit, mcpwm_timer_t timer, unsigned long int group_resolution, unsigned long int timer_resolution)
  142. {
  143. mcpwm_setup_testbench(unit, timer, 1000, 50.0, group_resolution, timer_resolution);
  144. vTaskDelay(pdMS_TO_TICKS(100));
  145. TEST_ESP_OK(mcpwm_set_duty(unit, timer, MCPWM_OPR_A, 10.0));
  146. TEST_ESP_OK(mcpwm_set_duty(unit, timer, MCPWM_OPR_B, 20.0));
  147. TEST_ASSERT_FLOAT_WITHIN(0.1, 10.0, mcpwm_get_duty(unit, timer, MCPWM_OPR_A));
  148. TEST_ASSERT_FLOAT_WITHIN(0.1, 20.0, mcpwm_get_duty(unit, timer, MCPWM_OPR_B));
  149. vTaskDelay(pdMS_TO_TICKS(100));
  150. TEST_ESP_OK(mcpwm_set_duty(unit, timer, MCPWM_OPR_A, 55.5f));
  151. TEST_ESP_OK(mcpwm_set_duty_type(unit, timer, MCPWM_OPR_A, MCPWM_DUTY_MODE_0));
  152. TEST_ASSERT_FLOAT_WITHIN(0.1, 55.5, mcpwm_get_duty(unit, timer, MCPWM_OPR_A));
  153. vTaskDelay(pdMS_TO_TICKS(100));
  154. TEST_ESP_OK(mcpwm_set_duty_in_us(unit, timer, MCPWM_OPR_B, 500));
  155. TEST_ASSERT_INT_WITHIN(5, 500, mcpwm_get_duty_in_us(unit, timer, MCPWM_OPR_B));
  156. vTaskDelay(pdMS_TO_TICKS(100));
  157. TEST_ESP_OK(mcpwm_stop(unit, timer));
  158. vTaskDelay(pdMS_TO_TICKS(100));
  159. }
  160. TEST_CASE("MCPWM duty test", "[mcpwm]")
  161. {
  162. for (int i = 0; i < SOC_MCPWM_GROUPS; i++) {
  163. for (int j = 0; j < SOC_MCPWM_TIMERS_PER_GROUP; j++) {
  164. mcpwm_timer_duty_test(i, j, MCPWM_TEST_GROUP_CLK_HZ, MCPWM_TEST_TIMER_CLK_HZ);
  165. mcpwm_timer_duty_test(i, j, MCPWM_TEST_GROUP_CLK_HZ / 2, MCPWM_TEST_TIMER_CLK_HZ * 2);
  166. }
  167. }
  168. }
  169. // -------------------------------------------------------------------------------------
  170. static void mcpwm_start_stop_test(mcpwm_unit_t unit, mcpwm_timer_t timer)
  171. {
  172. uint32_t pulse_number = 0;
  173. pcnt_setup_testbench();
  174. mcpwm_setup_testbench(unit, timer, 1000, 50.0, MCPWM_TEST_GROUP_CLK_HZ, MCPWM_TEST_TIMER_CLK_HZ); // Period: 1000us, 1ms
  175. // count the pulse number within 100ms
  176. pulse_number = pcnt_get_pulse_number(pcnt_unit_a, 100);
  177. TEST_ASSERT_INT_WITHIN(2, 100, pulse_number);
  178. pulse_number = pcnt_get_pulse_number(pcnt_unit_b, 100);
  179. TEST_ASSERT_INT_WITHIN(2, 100, pulse_number);
  180. TEST_ESP_OK(mcpwm_set_frequency(unit, timer, 100));
  181. pulse_number = pcnt_get_pulse_number(pcnt_unit_b, 100);
  182. TEST_ASSERT_INT_WITHIN(2, 10, pulse_number);
  183. // stop timer, then no pwm pulse should be generating
  184. TEST_ESP_OK(mcpwm_stop(unit, timer));
  185. usleep(10000); // wait until timer stopped
  186. pulse_number = pcnt_get_pulse_number(pcnt_unit_a, 100);
  187. TEST_ASSERT_INT_WITHIN(2, 0, pulse_number);
  188. pulse_number = pcnt_get_pulse_number(pcnt_unit_b, 100);
  189. TEST_ASSERT_INT_WITHIN(2, 0, pulse_number);
  190. pcnt_tear_testbench();
  191. }
  192. TEST_CASE("MCPWM start and stop test", "[mcpwm]")
  193. {
  194. for (int i = 0; i < SOC_MCPWM_GROUPS; i++) {
  195. for (int j = 0; j < SOC_MCPWM_TIMERS_PER_GROUP; j++) {
  196. mcpwm_start_stop_test(i, j);
  197. }
  198. }
  199. }
  200. // -------------------------------------------------------------------------------------
  201. static void mcpwm_deadtime_test(mcpwm_unit_t unit, mcpwm_timer_t timer)
  202. {
  203. mcpwm_setup_testbench(unit, timer, 1000, 50.0, MCPWM_TEST_GROUP_CLK_HZ, MCPWM_TEST_TIMER_CLK_HZ); // Period: 1000us, 1ms
  204. mcpwm_deadtime_type_t deadtime_type[] = {MCPWM_BYPASS_RED, MCPWM_BYPASS_FED, MCPWM_ACTIVE_HIGH_MODE,
  205. MCPWM_ACTIVE_LOW_MODE, MCPWM_ACTIVE_HIGH_COMPLIMENT_MODE, MCPWM_ACTIVE_LOW_COMPLIMENT_MODE,
  206. MCPWM_ACTIVE_RED_FED_FROM_PWMXA, MCPWM_ACTIVE_RED_FED_FROM_PWMXB
  207. };
  208. for (size_t i = 0; i < sizeof(deadtime_type) / sizeof(deadtime_type[0]); i++) {
  209. mcpwm_stop(unit, timer);
  210. usleep(10000);
  211. mcpwm_deadtime_enable(unit, timer, deadtime_type[i], 1000, 1000);
  212. mcpwm_start(unit, timer);
  213. vTaskDelay(pdMS_TO_TICKS(100));
  214. mcpwm_deadtime_disable(unit, timer);
  215. }
  216. mcpwm_stop(unit, timer);
  217. }
  218. TEST_CASE("MCPWM deadtime test", "[mcpwm]")
  219. {
  220. for (int i = 0; i < SOC_MCPWM_GROUPS; i++) {
  221. for (int j = 0; j < SOC_MCPWM_TIMERS_PER_GROUP; j++) {
  222. mcpwm_deadtime_test(i, j);
  223. }
  224. }
  225. }
  226. // -------------------------------------------------------------------------------------
  227. static void mcpwm_carrier_test(mcpwm_unit_t unit, mcpwm_timer_t timer, mcpwm_carrier_out_ivt_t invert_or_not,
  228. uint8_t period, uint8_t duty, uint8_t os_width)
  229. {
  230. uint32_t pulse_number = 0;
  231. pcnt_setup_testbench();
  232. mcpwm_setup_testbench(unit, timer, 1000, 50.0, MCPWM_TEST_GROUP_CLK_HZ, MCPWM_TEST_TIMER_CLK_HZ);
  233. mcpwm_set_signal_high(unit, timer, MCPWM_GEN_A);
  234. mcpwm_set_signal_high(unit, timer, MCPWM_GEN_B);
  235. TEST_ESP_OK(mcpwm_carrier_enable(unit, timer));
  236. TEST_ESP_OK(mcpwm_carrier_set_period(unit, timer, period)); //carrier revolution
  237. TEST_ESP_OK(mcpwm_carrier_set_duty_cycle(unit, timer, duty)); // carrier duty
  238. TEST_ESP_OK(mcpwm_carrier_output_invert(unit, timer, invert_or_not));
  239. TEST_ESP_OK(mcpwm_carrier_oneshot_mode_enable(unit, timer, os_width));
  240. vTaskDelay(pdMS_TO_TICKS(100));
  241. pulse_number = pcnt_get_pulse_number(pcnt_unit_a, 10);
  242. TEST_ASSERT_INT_WITHIN(50, 2500, pulse_number);
  243. usleep(10000);
  244. pulse_number = pcnt_get_pulse_number(pcnt_unit_b, 10);
  245. TEST_ASSERT_INT_WITHIN(50, 2500, pulse_number);
  246. TEST_ESP_OK(mcpwm_carrier_disable(unit, timer));
  247. TEST_ESP_OK(mcpwm_stop(unit, timer));
  248. pcnt_tear_testbench();
  249. }
  250. TEST_CASE("MCPWM carrier test", "[mcpwm]")
  251. {
  252. for (int i = 0; i < SOC_MCPWM_GROUPS; i++) {
  253. for (int j = 0; j < SOC_MCPWM_TIMERS_PER_GROUP; j++) {
  254. // carrier should be 10MHz/8/(4+1) = 250KHz, (10MHz is the group resolution, it's fixed in the driver), carrier duty cycle is 4/8 = 50%
  255. mcpwm_carrier_test(i, j, MCPWM_CARRIER_OUT_IVT_DIS, 4, 4, 3);
  256. mcpwm_carrier_test(i, j, MCPWM_CARRIER_OUT_IVT_EN, 4, 4, 3);
  257. }
  258. }
  259. }
  260. // -------------------------------------------------------------------------------------
  261. static void mcpwm_check_generator_level_on_fault(mcpwm_action_on_pwmxa_t action_a, mcpwm_action_on_pwmxb_t action_b)
  262. {
  263. if (action_a == MCPWM_ACTION_FORCE_LOW) {
  264. TEST_ASSERT_EQUAL(0, gpio_get_level(TEST_PWMA_GPIO));
  265. } else if (action_a == MCPWM_ACTION_FORCE_HIGH) {
  266. TEST_ASSERT_EQUAL(1, gpio_get_level(TEST_PWMA_GPIO));
  267. }
  268. if (action_b == MCPWM_ACTION_FORCE_LOW) {
  269. TEST_ASSERT_EQUAL(0, gpio_get_level(TEST_PWMB_GPIO));
  270. } else if (action_b == MCPWM_ACTION_FORCE_HIGH) {
  271. TEST_ASSERT_EQUAL(1, gpio_get_level(TEST_PWMB_GPIO));
  272. }
  273. }
  274. static void mcpwm_fault_cbc_test(mcpwm_unit_t unit, mcpwm_timer_t timer)
  275. {
  276. mcpwm_action_on_pwmxa_t action_a[] = {MCPWM_ACTION_FORCE_LOW, MCPWM_ACTION_FORCE_HIGH};
  277. mcpwm_action_on_pwmxb_t action_b[] = {MCPWM_ACTION_FORCE_LOW, MCPWM_ACTION_FORCE_HIGH};
  278. mcpwm_fault_signal_t fault_sig = fault_sig_array[timer];
  279. mcpwm_io_signals_t fault_io_sig = fault_io_sig_array[timer];
  280. mcpwm_setup_testbench(unit, timer, 1000, 50.0, MCPWM_TEST_GROUP_CLK_HZ, MCPWM_TEST_TIMER_CLK_HZ);
  281. TEST_ESP_OK(test_mcpwm_gpio_init(unit, fault_io_sig, TEST_FAULT_GPIO));
  282. gpio_set_level(TEST_FAULT_GPIO, 0);
  283. TEST_ESP_OK(mcpwm_fault_init(unit, MCPWM_HIGH_LEVEL_TGR, fault_sig));
  284. for (int i = 0; i < sizeof(action_a) / sizeof(action_a[0]); i++) {
  285. for (int j = 0; j < sizeof(action_b) / sizeof(action_b[0]); j++) {
  286. TEST_ESP_OK(mcpwm_fault_set_cyc_mode(unit, timer, fault_sig, action_a[i], action_b[j]));
  287. gpio_set_level(TEST_FAULT_GPIO, 1); // trigger the fault event
  288. usleep(10000);
  289. mcpwm_check_generator_level_on_fault(action_a[i], action_b[j]);
  290. gpio_set_level(TEST_FAULT_GPIO, 0); // remove the fault signal
  291. usleep(10000);
  292. }
  293. }
  294. TEST_ESP_OK(mcpwm_fault_deinit(unit, fault_sig));
  295. }
  296. TEST_CASE("MCPWM fault cbc test", "[mcpwm]")
  297. {
  298. for (int i = 0; i < SOC_MCPWM_GROUPS; i++) {
  299. for (int j = 0; j < SOC_MCPWM_TIMERS_PER_GROUP; j++) {
  300. mcpwm_fault_cbc_test(i, j);
  301. }
  302. }
  303. }
  304. // -------------------------------------------------------------------------------------
  305. static void mcpwm_fault_ost_test(mcpwm_unit_t unit, mcpwm_timer_t timer)
  306. {
  307. mcpwm_action_on_pwmxa_t action_a[] = {MCPWM_ACTION_FORCE_LOW, MCPWM_ACTION_FORCE_HIGH};
  308. mcpwm_action_on_pwmxb_t action_b[] = {MCPWM_ACTION_FORCE_LOW, MCPWM_ACTION_FORCE_HIGH};
  309. mcpwm_fault_signal_t fault_sig = fault_sig_array[timer];
  310. mcpwm_io_signals_t fault_io_sig = fault_io_sig_array[timer];
  311. mcpwm_setup_testbench(unit, timer, 1000, 50.0, MCPWM_TEST_GROUP_CLK_HZ, MCPWM_TEST_TIMER_CLK_HZ);
  312. TEST_ESP_OK(test_mcpwm_gpio_init(unit, fault_io_sig, TEST_FAULT_GPIO));
  313. gpio_set_level(TEST_FAULT_GPIO, 0);
  314. TEST_ESP_OK(mcpwm_fault_init(unit, MCPWM_HIGH_LEVEL_TGR, fault_sig));
  315. for (int i = 0; i < sizeof(action_a) / sizeof(action_a[0]); i++) {
  316. for (int j = 0; j < sizeof(action_b) / sizeof(action_b[0]); j++) {
  317. TEST_ESP_OK(mcpwm_fault_set_oneshot_mode(unit, timer, fault_sig, action_a[i], action_b[j]));
  318. gpio_set_level(TEST_FAULT_GPIO, 1); // trigger the fault event
  319. usleep(10000);
  320. mcpwm_check_generator_level_on_fault(action_a[i], action_b[j]);
  321. gpio_set_level(TEST_FAULT_GPIO, 0); // remove the fault signal
  322. usleep(10000);
  323. }
  324. }
  325. TEST_ESP_OK(mcpwm_fault_deinit(unit, fault_sig));
  326. }
  327. TEST_CASE("MCPWM fault ost test", "[mcpwm]")
  328. {
  329. for (int i = 0; i < SOC_MCPWM_GROUPS; i++) {
  330. for (int j = 0; j < SOC_MCPWM_TIMERS_PER_GROUP; j++) {
  331. mcpwm_fault_ost_test(i, j);
  332. }
  333. }
  334. }
  335. // -------------------------------------------------------------------------------------
  336. static void mcpwm_sync_test(mcpwm_unit_t unit, mcpwm_timer_t timer)
  337. {
  338. mcpwm_sync_signal_t sync_sig = sync_sig_array[timer];
  339. mcpwm_io_signals_t sync_io_sig = sync_io_sig_array[timer];
  340. mcpwm_setup_testbench(unit, timer, 1000, 50.0, MCPWM_TEST_GROUP_CLK_HZ, MCPWM_TEST_TIMER_CLK_HZ);
  341. TEST_ESP_OK(test_mcpwm_gpio_init(unit, sync_io_sig, TEST_SYNC_GPIO_0));
  342. gpio_set_level(TEST_SYNC_GPIO_0, 0);
  343. mcpwm_sync_config_t sync_conf = {
  344. .sync_sig = sync_sig,
  345. .timer_val = 200,
  346. .count_direction = MCPWM_TIMER_DIRECTION_UP,
  347. };
  348. TEST_ESP_OK(mcpwm_sync_configure(unit, timer, &sync_conf));
  349. vTaskDelay(pdMS_TO_TICKS(50));
  350. gpio_set_level(TEST_SYNC_GPIO_0, 1); // trigger an external sync event
  351. vTaskDelay(pdMS_TO_TICKS(50));
  352. mcpwm_timer_trigger_soft_sync(unit, timer); // trigger a software sync event
  353. vTaskDelay(pdMS_TO_TICKS(50));
  354. TEST_ESP_OK(mcpwm_sync_disable(unit, timer));
  355. TEST_ESP_OK(mcpwm_stop(unit, timer));
  356. }
  357. TEST_CASE("MCPWM timer GPIO sync test", "[mcpwm]")
  358. {
  359. for (int i = 0; i < SOC_MCPWM_GROUPS; i++) {
  360. for (int j = 0; j < SOC_MCPWM_TIMERS_PER_GROUP; j++) {
  361. mcpwm_sync_test(i, j);
  362. }
  363. }
  364. }
  365. // used only in this area but need to be reset every time. mutex is not needed
  366. // store timestamps captured from ISR callback
  367. static uint64_t cap_timestamp[3];
  368. // control the start of capture to avoid unstable data
  369. static volatile bool log_cap;
  370. // cb function, to update capture value
  371. // only log when channel1 comes at first, then channel2, and do not log further more.
  372. static bool test_mcpwm_capture_callback(mcpwm_unit_t mcpwm, mcpwm_capture_channel_id_t cap_channel, const cap_event_data_t *edata,
  373. void *user_data)
  374. {
  375. if (log_cap && (cap_timestamp[1] == 0 || cap_timestamp[2] == 0)) {
  376. if (cap_channel == MCPWM_SELECT_CAP1 && cap_timestamp[1] == 0) {
  377. cap_timestamp[1] = edata->cap_value;
  378. }
  379. if (cap_channel == MCPWM_SELECT_CAP2 && cap_timestamp[1] != 0) {
  380. cap_timestamp[2] = edata->cap_value;
  381. }
  382. }
  383. return false;
  384. }
  385. static void mcpwm_swsync_test(mcpwm_unit_t unit)
  386. {
  387. const uint32_t test_sync_phase = 20;
  388. cap_timestamp[0] = 0;
  389. cap_timestamp[1] = 0;
  390. cap_timestamp[2] = 0;
  391. log_cap = false;
  392. // configure all timer output 10% PWM
  393. for (int i = 0; i < 3; ++i) {
  394. mcpwm_setup_testbench(unit, i, 1000, 10.0, MCPWM_TEST_GROUP_CLK_HZ, MCPWM_TEST_TIMER_CLK_HZ);
  395. }
  396. vTaskDelay(pdMS_TO_TICKS(10));
  397. // configure capture for verification
  398. mcpwm_capture_config_t conf = {
  399. .cap_edge = MCPWM_POS_EDGE,
  400. .cap_prescale = 1,
  401. .capture_cb = test_mcpwm_capture_callback,
  402. .user_data = NULL,
  403. };
  404. TEST_ESP_OK(test_mcpwm_gpio_init(unit, MCPWM_CAP_0, TEST_SYNC_GPIO_0));
  405. TEST_ESP_OK(test_mcpwm_gpio_init(unit, MCPWM_CAP_1, TEST_SYNC_GPIO_1));
  406. TEST_ESP_OK(test_mcpwm_gpio_init(unit, MCPWM_CAP_2, TEST_SYNC_GPIO_2));
  407. TEST_ESP_OK(mcpwm_capture_enable_channel(unit, MCPWM_SELECT_CAP0, &conf));
  408. TEST_ESP_OK(mcpwm_capture_enable_channel(unit, MCPWM_SELECT_CAP1, &conf));
  409. TEST_ESP_OK(mcpwm_capture_enable_channel(unit, MCPWM_SELECT_CAP2, &conf));
  410. // timer0 produce sync sig at TEZ, timer1 and timer2 consume, to make sure last two can be synced precisely
  411. // timer1 and timer2 will be synced with TEZ of timer0 at a known phase.
  412. mcpwm_sync_config_t sync_conf = {
  413. .sync_sig = MCPWM_SELECT_TIMER0_SYNC,
  414. .timer_val = 0,
  415. .count_direction = MCPWM_TIMER_DIRECTION_UP,
  416. };
  417. TEST_ESP_OK(mcpwm_sync_configure(unit, MCPWM_TIMER_1, &sync_conf));
  418. sync_conf.timer_val = 1000 - test_sync_phase;
  419. TEST_ESP_OK(mcpwm_sync_configure(unit, MCPWM_TIMER_2, &sync_conf));
  420. TEST_ESP_OK(mcpwm_set_timer_sync_output(unit, MCPWM_TIMER_0, MCPWM_SWSYNC_SOURCE_TEZ));
  421. // init gpio at the end
  422. TEST_ESP_OK(test_mcpwm_gpio_init(unit, MCPWM0A, TEST_SYNC_GPIO_0));
  423. TEST_ESP_OK(test_mcpwm_gpio_init(unit, MCPWM1A, TEST_SYNC_GPIO_1));
  424. TEST_ESP_OK(test_mcpwm_gpio_init(unit, MCPWM2A, TEST_SYNC_GPIO_2));
  425. vTaskDelay(pdMS_TO_TICKS(100));
  426. log_cap = true;
  427. vTaskDelay(pdMS_TO_TICKS(100));
  428. uint32_t delta_timestamp_us = (cap_timestamp[2] - cap_timestamp[1]) * 1000000 / esp_clk_apb_freq();
  429. uint32_t expected_phase_us = 1000000 / mcpwm_get_frequency(unit, MCPWM_TIMER_0) * test_sync_phase / 1000;
  430. // accept +-2 error
  431. TEST_ASSERT_UINT32_WITHIN(2, expected_phase_us, delta_timestamp_us);
  432. // tear down
  433. for (int i = 0; i < 3; ++i) {
  434. TEST_ESP_OK(mcpwm_capture_disable_channel(unit, i));
  435. TEST_ESP_OK(mcpwm_sync_disable(unit, i));
  436. TEST_ESP_OK(mcpwm_stop(unit, i));
  437. }
  438. }
  439. TEST_CASE("MCPWM timer swsync test", "[mcpwm]")
  440. {
  441. for (int i = 0; i < SOC_MCPWM_GROUPS; i++) {
  442. mcpwm_swsync_test(i);
  443. }
  444. }
  445. // -------------------------------------------------------------------------------------
  446. typedef struct {
  447. mcpwm_unit_t unit;
  448. TaskHandle_t task_hdl;
  449. } test_capture_callback_data_t;
  450. static bool test_mcpwm_intr_handler(mcpwm_unit_t mcpwm, mcpwm_capture_channel_id_t cap_sig, const cap_event_data_t *edata, void *arg)
  451. {
  452. BaseType_t high_task_wakeup = pdFALSE;
  453. test_capture_callback_data_t *cb_data = (test_capture_callback_data_t *)arg;
  454. vTaskNotifyGiveFromISR(cb_data->task_hdl, &high_task_wakeup);
  455. return high_task_wakeup == pdTRUE;
  456. }
  457. static void mcpwm_capture_test(mcpwm_unit_t unit, mcpwm_capture_signal_t cap_chan)
  458. {
  459. test_capture_callback_data_t callback_data = {
  460. .unit = unit,
  461. .task_hdl = xTaskGetCurrentTaskHandle(),
  462. };
  463. //each timer test the capture sig with the same id with it.
  464. mcpwm_io_signals_t cap_io = cap_io_sig_array[cap_chan];
  465. mcpwm_capture_channel_id_t cap_channel = cap_sig_array[cap_chan];
  466. TEST_ESP_OK(test_mcpwm_gpio_init(unit, cap_io, TEST_CAP_GPIO));
  467. mcpwm_capture_config_t conf = {
  468. .cap_edge = MCPWM_POS_EDGE,
  469. .cap_prescale = 1,
  470. .capture_cb = test_mcpwm_intr_handler,
  471. .user_data = &callback_data
  472. };
  473. TEST_ESP_OK(mcpwm_capture_enable_channel(unit, cap_channel, &conf));
  474. // generate an posage
  475. gpio_set_level(TEST_CAP_GPIO, 0);
  476. gpio_set_level(TEST_CAP_GPIO, 1);
  477. vTaskDelay(pdMS_TO_TICKS(100));
  478. TEST_ASSERT_NOT_EQUAL(0, ulTaskNotifyTake(pdFALSE, pdMS_TO_TICKS(40)));
  479. uint32_t cap_val0 = mcpwm_capture_signal_get_value(unit, cap_chan);
  480. // generate another posage
  481. gpio_set_level(TEST_CAP_GPIO, 0);
  482. gpio_set_level(TEST_CAP_GPIO, 1);
  483. TEST_ASSERT_NOT_EQUAL(0, ulTaskNotifyTake(pdFALSE, pdMS_TO_TICKS(40)));
  484. uint32_t cap_val1 = mcpwm_capture_signal_get_value(unit, cap_chan);
  485. // capture clock source is APB (80MHz), 100ms means 8000000 ticks
  486. TEST_ASSERT_UINT_WITHIN(100000, 8000000, cap_val1 - cap_val0);
  487. TEST_ESP_OK(mcpwm_capture_disable_channel(unit, cap_channel));
  488. }
  489. TEST_CASE("MCPWM capture test", "[mcpwm]")
  490. {
  491. // we assume each group has one capture timer
  492. for (int i = 0; i < SOC_MCPWM_GROUPS; i++) {
  493. for (int j = 0; j < SOC_MCPWM_CAPTURE_CHANNELS_PER_TIMER; j++) {
  494. mcpwm_capture_test(i, j);
  495. }
  496. }
  497. }
  498. #endif // SOC_MCPWM_SUPPORTED