768 lines
		
	
	
		
			26 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			768 lines
		
	
	
		
			26 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
|  | /****************************************************************************
 | ||
|  | 
 | ||
|  | Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED. | ||
|  | 
 | ||
|  | This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT | ||
|  | be copied by any method or incorporated into another program without | ||
|  | the express written consent of Aerospace C.Power. This Information or any portion | ||
|  | thereof remains the property of Aerospace C.Power. The Information contained herein | ||
|  | is believed to be accurate and Aerospace C.Power assumes no responsibility or | ||
|  | liability for its use in any way and conveys no license or title under | ||
|  | any patent or copyright and makes no representation or warranty that this | ||
|  | Information is free from patent or copyright infringement. | ||
|  | 
 | ||
|  | ****************************************************************************/ | ||
|  | #include "iot_task_api.h"
 | ||
|  | #include "iot_app_api.h"
 | ||
|  | #include "iot_errno_api.h"
 | ||
|  | #include "iot_io_api.h"
 | ||
|  | #include "iot_uart_api.h"
 | ||
|  | #include "iot_utils_api.h"
 | ||
|  | #include "iot_gpio_api.h"
 | ||
|  | #include "iot_board_api.h"
 | ||
|  | 
 | ||
|  | #include "demo.h"
 | ||
|  | #include "demo_epwm_test.h"
 | ||
|  | #include "iot_pwm_api.h"
 | ||
|  | 
 | ||
|  | #include <stdlib.h>
 | ||
|  | #include <string.h>
 | ||
|  | 
 | ||
|  | /* baud rate */ | ||
|  | #define EPWM_DEMO_UART_BAUND                (115200)
 | ||
|  | #define EPWM_DEMO_UART_RCV_BUF_LEN          256
 | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 | ||
|  | #if (TARGET_VERSION == TARGET_KUNLUN)
 | ||
|  | /* The epwm test case for KL1 is based on WQ IOT module 'LEDC2.0', which with
 | ||
|  |     chip 'WQ3011'. */ | ||
|  | static void print_pwm_ch_status(uint8_t ch) | ||
|  | { | ||
|  |     iot_epwm_ch_status_t status; | ||
|  | 
 | ||
|  |     iot_epwm_ch_status_get(ch, &status); | ||
|  |     iot_cus_printf("ch%d actual value: ch_clk=%d, prd=%d, cmp=%d, cnt=%d, " | ||
|  |         "frq=%d, duty=%d.%d%%\r\n", ch, status.actual_ch_clk, | ||
|  |         status.actual_period, status.actual_compare, status.actual_counter, | ||
|  |         status.actual_freq, status.actual_duty/100, status.actual_duty%100); | ||
|  | } | ||
|  | 
 | ||
|  | 
 | ||
|  | static uint32_t kl1_epwm_isr_ch1_tz_cbc(void) | ||
|  | { | ||
|  |     static uint32_t flag = 0; | ||
|  |     iot_gpio_value_set(10, flag++ % 2); | ||
|  |     return 0; | ||
|  | } | ||
|  | 
 | ||
|  | static void iot_epwm_kl1_test_case1_update(void) | ||
|  | { | ||
|  |     const uint32_t freq_duty_list[][2] ={ | ||
|  |         {50,        5000},      /* 50Hz,    50% */ | ||
|  |         {10000,     5000},      /* 10KHz,   50% */ | ||
|  |         {100000,    5000},      /* 100KHz,  50% */ | ||
|  |         {1000,      0},         /* 1KHz,    0% */ | ||
|  |         {1000,      100},       /* 1KHz,    1% */ | ||
|  |         {1000,      5000},      /* 1KHz,    50% */ | ||
|  |         {1000,      9900},      /* 1KHz,    99% */ | ||
|  |         {1000,      10000}      /* 1KHz,    100% */ | ||
|  |     }; | ||
|  |     static int list_index = 0; | ||
|  |     uint8_t ch = IOT_EPWM_CHANNEL_1; | ||
|  | 
 | ||
|  |     iot_epwm_ch_update(ch, IOT_EPWM_CH_UPDATER_FREQUENCY_DUTY, | ||
|  |         freq_duty_list[list_index][0], freq_duty_list[list_index][1]); | ||
|  |     iot_cus_printf("\r\ncase1, new frequency and duty: %d Hz, %d.%d%%\r\n ", | ||
|  |         freq_duty_list[list_index][0], freq_duty_list[list_index][1] / 100, | ||
|  |         freq_duty_list[list_index][1] % 100); | ||
|  | 
 | ||
|  |     print_pwm_ch_status(ch); | ||
|  |     list_index++; | ||
|  |     if (list_index >= (sizeof(freq_duty_list)/sizeof(freq_duty_list[0]))) { | ||
|  |         list_index = 0; | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
|  | /* complementary output, cycle-by-cycle trip-zone on IO47 */ | ||
|  | static void iot_epwm_kl1_test_case1_init(void) | ||
|  | { | ||
|  |     uint8_t ch = IOT_EPWM_CHANNEL_1; | ||
|  |     iot_epwm_global_config_t global_config = IOT_EPWM_GLOBAL_CONFIG_DEFAULT; | ||
|  |     iot_epwm_ch_config_t ch_config = IOT_EPWM_CHANNEL_CONFIG_DEFAULT; | ||
|  | 
 | ||
|  |     iot_cus_printf("%s\r\n", __func__); | ||
|  |     iot_gpio_open_as_output(10); | ||
|  | 
 | ||
|  |     iot_pwm_api_mode_select(IOT_API_MODE_EPWM); | ||
|  |     iot_epwm_global_stop(); | ||
|  |     global_config.clk_src = IOT_PWM_CLK_SRC_25M; | ||
|  |     global_config.tz_pins[IOT_EPWM_TZ_1] = 47; | ||
|  |     iot_epwm_global_config_set(&global_config); | ||
|  | 
 | ||
|  |     ch_config.ch_clk_div = 25; | ||
|  |     ch_config.ch_pin_outa = 36; | ||
|  |     ch_config.ch_pin_outb = 28; | ||
|  |     ch_config.ch_polarity_outa = IOT_EPWM_POLARITY_INVERTED; | ||
|  |     ch_config.ch_polarity_outb = IOT_EPWM_POLARITY_NORMAL; | ||
|  |     ch_config.ch_align_mode = IOT_EPWM_ALIGN_EDGE; | ||
|  |     ch_config.ch_duty_load_mode = IOT_EPWM_DUTY_LOAD_CNT_EQU_ZERO; | ||
|  |     ch_config.ch_usage = IOT_EPWM_CH_USEAGE_COMMON; | ||
|  |     ch_config.ch_common_cfg.freq = 1000; | ||
|  |     ch_config.ch_common_cfg.duty = 5000; | ||
|  | 
 | ||
|  |     ch_config.ch_trip_zone.enable = 1; | ||
|  |     ch_config.ch_trip_zone.tz_en_for_cbc[IOT_EPWM_TZ_1] = 1; | ||
|  |     ch_config.ch_trip_zone.act_outa = IOT_EPWM_TZ_ACT_SET_HIGH; | ||
|  |     ch_config.ch_trip_zone.act_outb = IOT_EPWM_TZ_ACT_SET_LOW; | ||
|  |     ch_config.ch_trip_zone.callback_cbc = kl1_epwm_isr_ch1_tz_cbc; | ||
|  | 
 | ||
|  |     iot_epwm_ch_config_set(ch, &ch_config); | ||
|  |     iot_epwm_ch_start(ch); | ||
|  |     iot_epwm_global_start(); | ||
|  |     iot_cus_printf("%s done.\r\n", __func__); | ||
|  |     print_pwm_ch_status(ch); | ||
|  | } | ||
|  | 
 | ||
|  | static void iot_epwm_kl1_test_case2_update(void) | ||
|  | { | ||
|  |     const uint32_t prd_cmp_list[][2] ={ | ||
|  |         /* {period value, compare value} */ | ||
|  |         {10000,          0}, | ||
|  |         {10000,          100}, | ||
|  |         {10000,          200}, | ||
|  |         {10000,          5000}, | ||
|  |         {10000,          10000-200}, | ||
|  |         {10000,          10000-100}, | ||
|  |         {10000,          10000}, | ||
|  |     }; | ||
|  |     static int list_index = 0; | ||
|  |     uint8_t ch = IOT_EPWM_CHANNEL_0; | ||
|  | 
 | ||
|  |     iot_epwm_ch_update(ch, IOT_EPWM_CH_UPDATER_PERIOD_COMPARE_CNT, | ||
|  |         prd_cmp_list[list_index][0], prd_cmp_list[list_index][1]); | ||
|  |     iot_epwm_global_sync(); | ||
|  |     iot_cus_printf("\r\ncase2, new period and compare value: %d, %d\r\n ", | ||
|  |         prd_cmp_list[list_index][0], prd_cmp_list[list_index][1]); | ||
|  | 
 | ||
|  |     print_pwm_ch_status(ch); | ||
|  | 
 | ||
|  |     list_index++; | ||
|  |     if (list_index >= (sizeof(prd_cmp_list)/sizeof(prd_cmp_list[0]))) { | ||
|  |         list_index = 0; | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
|  | /* output 2 channels */ | ||
|  | static void iot_epwm_kl1_test_case2_init(void) | ||
|  | { | ||
|  |     uint8_t ch1; | ||
|  |     uint8_t ch2; | ||
|  |     iot_epwm_global_config_t global_config = IOT_EPWM_GLOBAL_CONFIG_DEFAULT; | ||
|  |     iot_epwm_ch_config_t ch_config_default = IOT_EPWM_CHANNEL_CONFIG_DEFAULT; | ||
|  |     iot_epwm_ch_config_t ch_config = {0}; | ||
|  | 
 | ||
|  |     iot_cus_printf("%s\r\n", __func__); | ||
|  |     iot_pwm_api_mode_select(IOT_API_MODE_EPWM); | ||
|  |     iot_epwm_global_stop(); | ||
|  |     global_config.clk_src = IOT_PWM_CLK_SRC_50M; | ||
|  |     iot_epwm_global_config_set(&global_config); | ||
|  | 
 | ||
|  |     /* config for channel 0 */ | ||
|  |     os_mem_cpy(&ch_config, &ch_config_default, sizeof(iot_epwm_ch_config_t)); | ||
|  |     ch1 = IOT_EPWM_CHANNEL_0; | ||
|  |     ch_config.ch_clk_div = 5; | ||
|  |     ch_config.ch_pin_outa = 36; | ||
|  |     ch_config.ch_pin_outb = 0xFF; | ||
|  |     ch_config.ch_polarity_outa = IOT_EPWM_POLARITY_NORMAL; | ||
|  |     ch_config.ch_align_mode = IOT_EPWM_ALIGN_EDGE; | ||
|  |     ch_config.ch_duty_load_mode = IOT_EPWM_DUTY_LOAD_CNT_EQU_ZERO; | ||
|  |     ch_config.ch_usage = IOT_EPWM_CH_USEAGE_PRECISE; | ||
|  |     ch_config.ch_precise_cfg.period_cnt = 10000; | ||
|  |     ch_config.ch_precise_cfg.compare_cnt = 3000; | ||
|  |     iot_epwm_ch_config_set(ch1, &ch_config); | ||
|  | 
 | ||
|  |     /* config for channel 2 */ | ||
|  |     os_mem_cpy(&ch_config, &ch_config_default, sizeof(iot_epwm_ch_config_t)); | ||
|  |     ch2 = IOT_EPWM_CHANNEL_2; | ||
|  |     ch_config.ch_clk_div = 5; | ||
|  |     ch_config.ch_pin_outa = 28; | ||
|  |     ch_config.ch_pin_outb = 0xFF; | ||
|  |     ch_config.ch_polarity_outa = IOT_EPWM_POLARITY_NORMAL; | ||
|  |     ch_config.ch_align_mode = IOT_EPWM_ALIGN_EDGE; | ||
|  |     ch_config.ch_duty_load_mode = IOT_EPWM_DUTY_LOAD_CNT_EQU_ZERO; | ||
|  |     ch_config.ch_usage = IOT_EPWM_CH_USEAGE_PRECISE; | ||
|  |     ch_config.ch_precise_cfg.period_cnt = 10000; | ||
|  |     ch_config.ch_precise_cfg.compare_cnt = 8000; | ||
|  |     iot_epwm_ch_config_set(ch2, &ch_config); | ||
|  | 
 | ||
|  |     iot_epwm_ch_start(ch1); | ||
|  |     iot_epwm_ch_start(ch2); | ||
|  |     iot_epwm_global_start(); | ||
|  |     iot_cus_printf("%s done.\r\n", __func__); | ||
|  |     print_pwm_ch_status(ch1); | ||
|  |     print_pwm_ch_status(ch2); | ||
|  | } | ||
|  | 
 | ||
|  | static uint32_t kl1_epwm_isr_ch3_tz_osht(void) | ||
|  | { | ||
|  |     static uint32_t flag = 0; | ||
|  |     iot_gpio_value_set(36, flag++ % 2); | ||
|  |     return 0; | ||
|  | } | ||
|  | 
 | ||
|  | /* center align, one-shuct trip-zone on IO28 */ | ||
|  | static void iot_epwm_kl1_test_case3_init(void) | ||
|  | { | ||
|  |     uint8_t ch = IOT_EPWM_CHANNEL_3; | ||
|  |     iot_epwm_global_config_t global_config = IOT_EPWM_GLOBAL_CONFIG_DEFAULT; | ||
|  |     iot_epwm_ch_config_t ch_config = IOT_EPWM_CHANNEL_CONFIG_DEFAULT; | ||
|  | 
 | ||
|  |     iot_cus_printf("%s\r\n", __func__); | ||
|  |     iot_gpio_open_as_output(36); | ||
|  | 
 | ||
|  |     iot_pwm_api_mode_select(IOT_API_MODE_EPWM); | ||
|  |     iot_epwm_global_stop(); | ||
|  |     global_config.clk_src = IOT_PWM_CLK_SRC_2M5; | ||
|  |     global_config.tz_pins[IOT_EPWM_TZ_1] = 28; | ||
|  |     iot_epwm_global_config_set(&global_config); | ||
|  | 
 | ||
|  |     ch_config.ch_clk_div = 25; | ||
|  |     ch_config.ch_pin_outa = 0xFF; | ||
|  |     ch_config.ch_pin_outb = 10; | ||
|  |     ch_config.ch_polarity_outb = IOT_EPWM_POLARITY_NORMAL; | ||
|  |     ch_config.ch_align_mode = IOT_EPWM_ALIGN_CENTER; | ||
|  |     ch_config.ch_usage = IOT_EPWM_CH_USEAGE_PRECISE; | ||
|  |     ch_config.ch_precise_cfg.period_cnt = 1000; | ||
|  |     ch_config.ch_precise_cfg.compare_cnt = 400; | ||
|  | 
 | ||
|  |     ch_config.ch_trip_zone.enable = 1; | ||
|  |     ch_config.ch_trip_zone.tz_en_for_osht[IOT_EPWM_TZ_1] = 1; | ||
|  |     ch_config.ch_trip_zone.act_outb = IOT_EPWM_TZ_ACT_SET_HIGH; | ||
|  |     ch_config.ch_trip_zone.callback_osht = kl1_epwm_isr_ch3_tz_osht; | ||
|  | 
 | ||
|  |     iot_epwm_ch_config_set(ch, &ch_config); | ||
|  |     iot_epwm_ch_start(ch); | ||
|  |     iot_epwm_global_start(); | ||
|  |     iot_cus_printf("%s done.\r\n", __func__); | ||
|  |     print_pwm_ch_status(ch); | ||
|  | } | ||
|  | 
 | ||
|  | static uint32_t kl1_epwm_isr_ch2_int(void) | ||
|  | { | ||
|  |     static uint32_t flag = 0; | ||
|  |     iot_gpio_value_set(28, flag++ % 2); | ||
|  |     return 0; | ||
|  | } | ||
|  | 
 | ||
|  | /* dead band enable, pwm periodic interrupt enable */ | ||
|  | static void iot_epwm_kl1_test_case4_init(void) | ||
|  | { | ||
|  |     uint8_t ch = IOT_EPWM_CHANNEL_2; | ||
|  |     iot_epwm_global_config_t global_config = IOT_EPWM_GLOBAL_CONFIG_DEFAULT; | ||
|  |     iot_epwm_ch_config_t ch_config = IOT_EPWM_CHANNEL_CONFIG_DEFAULT; | ||
|  | 
 | ||
|  |     iot_cus_printf("%s\r\n", __func__); | ||
|  |     iot_gpio_open_as_output(28); | ||
|  | 
 | ||
|  |     iot_pwm_api_mode_select(IOT_API_MODE_EPWM); | ||
|  |     iot_epwm_global_stop(); | ||
|  |     global_config.clk_src = IOT_PWM_CLK_SRC_25M; | ||
|  |     iot_epwm_global_config_set(&global_config); | ||
|  | 
 | ||
|  |     ch_config.ch_clk_div = 250; | ||
|  |     ch_config.ch_pin_outa = 36; | ||
|  |     ch_config.ch_pin_outb = 10; | ||
|  |     ch_config.ch_polarity_outa = IOT_EPWM_POLARITY_NORMAL; | ||
|  |     ch_config.ch_polarity_outb = IOT_EPWM_POLARITY_INVERTED; | ||
|  |     ch_config.ch_align_mode = IOT_EPWM_ALIGN_EDGE; | ||
|  |     ch_config.ch_usage = IOT_EPWM_CH_USEAGE_PRECISE; | ||
|  |     ch_config.ch_precise_cfg.period_cnt = 1000; | ||
|  |     ch_config.ch_precise_cfg.compare_cnt = 600; | ||
|  | 
 | ||
|  |     ch_config.ch_dead_band.enable = 1; | ||
|  |     ch_config.ch_dead_band.rising_delay = 100; | ||
|  |     ch_config.ch_dead_band.falling_delay = 200; | ||
|  | 
 | ||
|  |     ch_config.ch_interrupt.enable = 1; | ||
|  |     ch_config.ch_interrupt.trigger = IOT_EPWM_INT_TRIG_CNT_EQU_PRD; | ||
|  |     ch_config.ch_interrupt.callback = kl1_epwm_isr_ch2_int; | ||
|  | 
 | ||
|  |     iot_epwm_ch_config_set(ch, &ch_config); | ||
|  |     iot_epwm_ch_start(ch); | ||
|  |     iot_epwm_global_start(); | ||
|  |     iot_cus_printf("%s done.\r\n", __func__); | ||
|  |     print_pwm_ch_status(ch); | ||
|  | } | ||
|  | 
 | ||
|  | static const void (*test_case_init_list[])(void) = { | ||
|  |     iot_epwm_kl1_test_case1_init, | ||
|  |     iot_epwm_kl1_test_case2_init, | ||
|  |     iot_epwm_kl1_test_case3_init, | ||
|  |     iot_epwm_kl1_test_case4_init, | ||
|  | }; | ||
|  | 
 | ||
|  | static const void (*test_case_update_list[])(void) = { | ||
|  |     iot_epwm_kl1_test_case1_update, | ||
|  |     iot_epwm_kl1_test_case2_update, | ||
|  |     NULL, | ||
|  |     NULL, | ||
|  | }; | ||
|  | 
 | ||
|  | #elif (TARGET_VERSION == TARGET_KUNLUN3)
 | ||
|  | /* The epwm test case for KL3 is based on WQ IOT module 'LEDC2.0', which with
 | ||
|  |     chip 'WQ3031'. */ | ||
|  | 
 | ||
|  | static uint32_t kl3_epwm_isr_ch1_tz_cbc(void) | ||
|  | { | ||
|  |     iot_cus_printf("isr cbc\r\n"); | ||
|  |     return 0; | ||
|  | } | ||
|  | 
 | ||
|  | static uint32_t kl3_epwm_isr_ch1_soca(void) | ||
|  | { | ||
|  |     iot_cus_printf("isr soca\r\n"); | ||
|  |     return 0; | ||
|  | } | ||
|  | 
 | ||
|  | static void iot_epwm_kl3_test_case1_update(void) | ||
|  | { | ||
|  |     const uint32_t freq_duty_list[][2] ={ | ||
|  |         {4,         5000},      /* 4Hz,     50% */ | ||
|  |         {50,        5000},      /* 50Hz,    50% */ | ||
|  |         {1000,      5000},      /* 1KHz,    50% */ | ||
|  |         {1000000,   5000},      /* 1MHz,    50% */ | ||
|  |         {1000,      0},         /* 1KHz,    0% */ | ||
|  |         {1000,      1},         /* 1KHz,    0.01% */ | ||
|  |         {1000,      100},       /* 1KHz,    1% */ | ||
|  |         {1000,      5000},      /* 1KHz,    50% */ | ||
|  |         {1000,      9900},      /* 1KHz,    99% */ | ||
|  |         {1000,      9999},      /* 1KHz,    99.99% */ | ||
|  |         {1000,      10000}      /* 1KHz,    100% */ | ||
|  |     }; | ||
|  |     static int list_index = 0; | ||
|  |     uint8_t ch = IOT_EPWM_CHANNEL_1; | ||
|  |     iot_epwm_ch_status_t status; | ||
|  | 
 | ||
|  |     iot_epwm_ch_update(ch, IOT_EPWM_CH_UPDATER_FREQUENCY_DUTY, | ||
|  |         freq_duty_list[list_index][0], freq_duty_list[list_index][1]); | ||
|  |     iot_cus_printf("\r\ncase1, new frequency and duty: %d Hz, %d.%d%%\r\n ", | ||
|  |         freq_duty_list[list_index][0], freq_duty_list[list_index][1] / 100, | ||
|  |         freq_duty_list[list_index][1] % 100); | ||
|  | 
 | ||
|  |     iot_epwm_ch_status_get(ch, &status); | ||
|  |     iot_cus_printf("ch%d actual value: ch_clk=%d, prd=%d, cmp=%d, cnt=%d, " | ||
|  |         "frq=%d, duty=%d.%d%%\r\n", ch, status.actual_ch_clk, | ||
|  |         status.actual_period, status.actual_compare, status.actual_counter, | ||
|  |         status.actual_freq, status.actual_duty/100, status.actual_duty%100); | ||
|  | 
 | ||
|  |     list_index++; | ||
|  |     if (list_index >= (sizeof(freq_duty_list)/sizeof(freq_duty_list[0]))) { | ||
|  |         list_index = 0; | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
|  | static void iot_epwm_kl3_test_case1_init(void) | ||
|  | { | ||
|  |     uint8_t ch = IOT_EPWM_CHANNEL_1; | ||
|  |     iot_epwm_global_config_t global_config = IOT_EPWM_GLOBAL_CONFIG_DEFAULT; | ||
|  |     iot_epwm_ch_config_t ch_config = IOT_EPWM_CHANNEL_CONFIG_DEFAULT; | ||
|  | 
 | ||
|  |     iot_cus_printf("%s\r\n", __func__); | ||
|  |     iot_pwm_api_mode_select(IOT_API_MODE_EPWM); | ||
|  |     iot_epwm_global_stop(); | ||
|  |     global_config.clk_src = IOT_PWM_CLK_SRC_25M; | ||
|  |     global_config.tz_pins[IOT_EPWM_TZ_1] = 31; | ||
|  |     iot_epwm_global_config_set(&global_config); | ||
|  |     /* config for channel 1 */ | ||
|  |     ch_config.ch_clk_div = 1; | ||
|  |     ch_config.ch_pin_outa = 4; | ||
|  |     ch_config.ch_pin_outb = 7; | ||
|  |     ch_config.ch_polarity_outa = IOT_EPWM_POLARITY_NORMAL; | ||
|  |     ch_config.ch_polarity_outb = IOT_EPWM_POLARITY_INVERTED; | ||
|  |     ch_config.ch_align_mode = IOT_EPWM_ALIGN_EDGE; | ||
|  |     ch_config.ch_period_load_mode = IOT_EPWM_PERIOD_LOAD_CNT_EQU_ZERO; | ||
|  |     ch_config.ch_duty_load_mode = IOT_EPWM_DUTY_LOAD_CNT_EQU_ZERO; | ||
|  | 
 | ||
|  |     ch_config.ch_usage = IOT_EPWM_CH_USEAGE_COMMON; | ||
|  |     ch_config.ch_common_cfg.freq = 1000; | ||
|  |     ch_config.ch_common_cfg.duty = 5000; | ||
|  | 
 | ||
|  |     ch_config.ch_trip_zone.enable = 1; | ||
|  |     ch_config.ch_trip_zone.tz_en_for_cbc[IOT_EPWM_TZ_1] = 1; | ||
|  |     ch_config.ch_trip_zone.act_outa = IOT_EPWM_TZ_ACT_SET_HIGH; | ||
|  |     ch_config.ch_trip_zone.act_outb = IOT_EPWM_TZ_ACT_SET_LOW; | ||
|  |     ch_config.ch_trip_zone.callback_cbc = kl3_epwm_isr_ch1_tz_cbc; | ||
|  | 
 | ||
|  |     ch_config.ch_soca.enable = 1; | ||
|  |     ch_config.ch_soca.trigger = IOT_EPWM_SOC_TRIG_CNT_EQU_ZERO; | ||
|  |     ch_config.ch_soca.divider = 10; | ||
|  |     ch_config.ch_soca.callback = kl3_epwm_isr_ch1_soca; | ||
|  | 
 | ||
|  |     iot_epwm_ch_config_set(ch, &ch_config); | ||
|  |     iot_epwm_ch_start(ch); | ||
|  |     iot_epwm_global_start(); | ||
|  | } | ||
|  | 
 | ||
|  | static uint32_t kl3_epwm_isr_ch0_tz_osht(void) | ||
|  | { | ||
|  |     iot_cus_printf("isr osht\r\n"); | ||
|  |     return 0; | ||
|  | } | ||
|  | 
 | ||
|  | static uint32_t kl3_epwm_isr_ch0_socb(void) | ||
|  | { | ||
|  |     iot_cus_printf("isr socb\r\n"); | ||
|  |     return 0; | ||
|  | } | ||
|  | 
 | ||
|  | static uint32_t kl3_epwm_isr_ch2_int(void) | ||
|  | { | ||
|  |     iot_cus_printf("isr int\r\n"); | ||
|  |     return 0; | ||
|  | } | ||
|  | 
 | ||
|  | static void iot_epwm_kl3_test_case2_update(void) | ||
|  | { | ||
|  |     const uint32_t prd_cmp_list[][2] ={ | ||
|  |         /* {period value, compare value} */ | ||
|  |         {2,             1}, | ||
|  |         {100,           100/2}, | ||
|  |         {1000,          1000/2}, | ||
|  |         {0xFFFFFF/2,    0xFFFFFF/4}, | ||
|  |         {0xFFFFFF-2,    (0xFFFFFF-2)/2}, | ||
|  |         {0xFFFFFF-1,    (0xFFFFFF-1)/2}, | ||
|  |         {0xFFFFFF,      0xFFFFFF/2}, | ||
|  |         {1000,          0}, | ||
|  |         {1000,          1}, | ||
|  |         {1000,          2}, | ||
|  |         {1000,          1000-2}, | ||
|  |         {1000,          1000-1}, | ||
|  |         {1000,          1000}, | ||
|  |     }; | ||
|  |     static int list_index = 0; | ||
|  |     uint8_t ch = IOT_EPWM_CHANNEL_0; | ||
|  |     iot_epwm_ch_status_t status; | ||
|  | 
 | ||
|  |     iot_epwm_ch_update(ch, IOT_EPWM_CH_UPDATER_PERIOD_COMPARE_CNT, | ||
|  |         prd_cmp_list[list_index][0], prd_cmp_list[list_index][1]); | ||
|  |     iot_epwm_global_sync(); | ||
|  |     iot_cus_printf("\r\ncase2, new period and compare value: %d, %d\r\n ", | ||
|  |         prd_cmp_list[list_index][0], prd_cmp_list[list_index][1]); | ||
|  | 
 | ||
|  |     iot_epwm_ch_status_get(ch, &status); | ||
|  |     iot_cus_printf("ch%d actual value: ch_clk=%d, prd=%d, cmp=%d, cnt=%d, " | ||
|  |         "frq=%d, duty=%d.%d%%\r\n", ch, status.actual_ch_clk, | ||
|  |         status.actual_period, status.actual_compare, status.actual_counter, | ||
|  |         status.actual_freq, status.actual_duty/100, status.actual_duty%100); | ||
|  | 
 | ||
|  |     list_index++; | ||
|  |     if (list_index >= (sizeof(prd_cmp_list)/sizeof(prd_cmp_list[0]))) { | ||
|  |         list_index = 0; | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
|  | static void iot_epwm_kl3_test_case2_init(void) | ||
|  | { | ||
|  |     uint8_t ch1; | ||
|  |     uint8_t ch2; | ||
|  |     iot_epwm_global_config_t global_config = IOT_EPWM_GLOBAL_CONFIG_DEFAULT; | ||
|  |     iot_epwm_ch_config_t ch_config_default = IOT_EPWM_CHANNEL_CONFIG_DEFAULT; | ||
|  |     iot_epwm_ch_config_t ch_config = {0}; | ||
|  | 
 | ||
|  |     iot_cus_printf("%s\r\n", __func__); | ||
|  |     iot_pwm_api_mode_select(IOT_API_MODE_EPWM); | ||
|  |     iot_epwm_global_stop(); | ||
|  |     global_config.clk_src = IOT_PWM_CLK_SRC_150M; | ||
|  |     /* extern sync signal, take effect at rising adge.
 | ||
|  |         Note: sync signal must be input to channel 0. */ | ||
|  |     global_config.ext_sync_pin = 31; | ||
|  |     global_config.tz_pins[IOT_EPWM_TZ_1] = 69; | ||
|  |     iot_epwm_global_config_set(&global_config); | ||
|  |     /* config for channel 0 */ | ||
|  |     os_mem_cpy(&ch_config, &ch_config_default, sizeof(iot_epwm_ch_config_t)); | ||
|  |     ch1 = IOT_EPWM_CHANNEL_0; | ||
|  |     ch_config.ch_clk_div = 150; | ||
|  |     ch_config.ch_pin_outa = 4; | ||
|  |     ch_config.ch_pin_outb = 0xFF; | ||
|  |     ch_config.ch_polarity_outa = IOT_EPWM_POLARITY_NORMAL; | ||
|  |     ch_config.ch_align_mode = IOT_EPWM_ALIGN_EDGE; | ||
|  |     ch_config.ch_period_load_mode = IOT_EPWM_PERIOD_LOAD_CNT_EQU_ZERO; | ||
|  |     ch_config.ch_duty_load_mode = IOT_EPWM_DUTY_LOAD_CNT_EQU_ZERO; | ||
|  | 
 | ||
|  |     ch_config.ch_usage = IOT_EPWM_CH_USEAGE_PRECISE; | ||
|  |     ch_config.ch_precise_cfg.period_cnt = 10000; | ||
|  |     ch_config.ch_precise_cfg.compare_cnt = 5000; | ||
|  | 
 | ||
|  |     ch_config.ch_sync_mode = IOT_EPWM_CH_SOC_SYNC; | ||
|  |     ch_config.ch_phs_en = 1; | ||
|  |     ch_config.ch_phase = 0; | ||
|  | 
 | ||
|  |     ch_config.ch_trip_zone.enable = 1; | ||
|  |     ch_config.ch_trip_zone.tz_en_for_osht[IOT_EPWM_TZ_1] = 1; | ||
|  |     ch_config.ch_trip_zone.act_outa = IOT_EPWM_TZ_ACT_SET_HIGH; | ||
|  |     ch_config.ch_trip_zone.act_outb = IOT_EPWM_TZ_ACT_SET_LOW; | ||
|  |     ch_config.ch_trip_zone.callback_osht = kl3_epwm_isr_ch0_tz_osht; | ||
|  | 
 | ||
|  |     ch_config.ch_socb.enable = 1; | ||
|  |     ch_config.ch_socb.trigger = IOT_EPWM_SOC_TRIG_CNT_EQU_PRD; | ||
|  |     ch_config.ch_socb.divider = 10; | ||
|  |     ch_config.ch_socb.callback = kl3_epwm_isr_ch0_socb; | ||
|  | 
 | ||
|  |     iot_epwm_ch_config_set(ch1, &ch_config); | ||
|  | 
 | ||
|  |     /* config for channel 2 */ | ||
|  |     os_mem_cpy(&ch_config, &ch_config_default, sizeof(iot_epwm_ch_config_t)); | ||
|  |     ch2 = IOT_EPWM_CHANNEL_2; | ||
|  |     ch_config.ch_clk_div = 150; | ||
|  |     ch_config.ch_pin_outa = 7; | ||
|  |     ch_config.ch_pin_outb = 0xFF; | ||
|  |     ch_config.ch_polarity_outa = IOT_EPWM_POLARITY_NORMAL; | ||
|  |     ch_config.ch_align_mode = IOT_EPWM_ALIGN_EDGE; | ||
|  |     ch_config.ch_period_load_mode = IOT_EPWM_PERIOD_LOAD_CNT_EQU_ZERO; | ||
|  |     ch_config.ch_duty_load_mode = IOT_EPWM_DUTY_LOAD_CNT_EQU_ZERO; | ||
|  | 
 | ||
|  |     ch_config.ch_usage = IOT_EPWM_CH_USEAGE_PRECISE; | ||
|  |     ch_config.ch_precise_cfg.period_cnt = 10000; | ||
|  |     ch_config.ch_precise_cfg.compare_cnt = 5000; | ||
|  | 
 | ||
|  |     ch_config.ch_sync_mode = IOT_EPWM_CH_SOC_SYNC; | ||
|  |     ch_config.ch_phs_en = 1; | ||
|  |     ch_config.ch_phase = 2000; | ||
|  | 
 | ||
|  |     ch_config.ch_chopper.enable = 1; | ||
|  |     ch_config.ch_chopper.divider = 1; | ||
|  |     ch_config.ch_chopper.duty = 3; | ||
|  |     ch_config.ch_chopper.osht_width = 10; | ||
|  | 
 | ||
|  |     ch_config.ch_interrupt.enable = 1; | ||
|  |     ch_config.ch_interrupt.trigger = IOT_EPWM_INT_TRIG_CNT_UP_EQU_CMPA; | ||
|  |     ch_config.ch_interrupt.divider = 100; | ||
|  |     ch_config.ch_interrupt.callback = kl3_epwm_isr_ch2_int; | ||
|  | 
 | ||
|  |     iot_epwm_ch_config_set(ch2, &ch_config); | ||
|  | 
 | ||
|  |     iot_epwm_global_sync(); | ||
|  |     iot_epwm_ch_start(ch1); | ||
|  |     iot_epwm_ch_start(ch2); | ||
|  |     iot_epwm_global_start(); | ||
|  | } | ||
|  | 
 | ||
|  | /* auto duty enable. */ | ||
|  | static void iot_epwm_kl3_test_case3_init(void) | ||
|  | { | ||
|  |     uint8_t ch = IOT_EPWM_CHANNEL_3; | ||
|  |     iot_epwm_global_config_t global_config = IOT_EPWM_GLOBAL_CONFIG_DEFAULT; | ||
|  |     iot_epwm_ch_config_t ch_config = IOT_EPWM_CHANNEL_CONFIG_DEFAULT; | ||
|  | 
 | ||
|  |     iot_cus_printf("%s\r\n", __func__); | ||
|  |     iot_pwm_api_mode_select(IOT_API_MODE_EPWM); | ||
|  |     iot_epwm_global_stop(); | ||
|  |     global_config.clk_src = IOT_PWM_CLK_SRC_25M; | ||
|  |     iot_epwm_global_config_set(&global_config); | ||
|  | 
 | ||
|  |     ch_config.ch_clk_div = 250; | ||
|  |     ch_config.ch_pin_outa = 4; | ||
|  |     ch_config.ch_pin_outb = 7; | ||
|  |     ch_config.ch_polarity_outa = IOT_EPWM_POLARITY_INVERTED; | ||
|  |     ch_config.ch_polarity_outb = IOT_EPWM_POLARITY_NORMAL; | ||
|  |     ch_config.ch_usage = IOT_EPWM_CH_USEAGE_PRECISE; | ||
|  |     ch_config.ch_precise_cfg.period_cnt = 1000; | ||
|  |     ch_config.ch_align_mode = IOT_EPWM_ALIGN_EDGE; | ||
|  | 
 | ||
|  |     ch_config.ch_auto_duty.enable = 1; | ||
|  |     ch_config.ch_auto_duty.cmp_start = 100; | ||
|  |     ch_config.ch_auto_duty.cmp_end = 1000; | ||
|  |     ch_config.ch_auto_duty.cmp_step = 10; | ||
|  | 
 | ||
|  |     iot_epwm_ch_config_set(ch, &ch_config); | ||
|  |     iot_epwm_ch_start(ch); | ||
|  |     iot_epwm_global_start(); | ||
|  | } | ||
|  | 
 | ||
|  | /* dead band enable. */ | ||
|  | static void iot_epwm_kl3_test_case4_init(void) | ||
|  | { | ||
|  |     uint8_t ch = IOT_EPWM_CHANNEL_4; | ||
|  |     iot_epwm_global_config_t global_config = IOT_EPWM_GLOBAL_CONFIG_DEFAULT; | ||
|  |     iot_epwm_ch_config_t ch_config = IOT_EPWM_CHANNEL_CONFIG_DEFAULT; | ||
|  | 
 | ||
|  |     iot_cus_printf("%s\r\n", __func__); | ||
|  |     iot_pwm_api_mode_select(IOT_API_MODE_EPWM); | ||
|  |     iot_epwm_global_stop(); | ||
|  |     global_config.clk_src = IOT_PWM_CLK_SRC_150M; | ||
|  |     iot_epwm_global_config_set(&global_config); | ||
|  | 
 | ||
|  |     ch_config.ch_clk_div = 150; | ||
|  |     ch_config.ch_pin_outa = 4; | ||
|  |     ch_config.ch_pin_outb = 7; | ||
|  |     ch_config.ch_polarity_outa = IOT_EPWM_POLARITY_NORMAL; | ||
|  |     ch_config.ch_polarity_outb = IOT_EPWM_POLARITY_INVERTED; | ||
|  |     ch_config.ch_usage = IOT_EPWM_CH_USEAGE_PRECISE; | ||
|  |     ch_config.ch_precise_cfg.period_cnt = 1000; | ||
|  |     ch_config.ch_precise_cfg.compare_cnt = 500; | ||
|  |     ch_config.ch_align_mode = IOT_EPWM_ALIGN_CENTER; | ||
|  | 
 | ||
|  |     ch_config.ch_dead_band.enable = 1; | ||
|  |     ch_config.ch_dead_band.rising_delay = 100; | ||
|  |     ch_config.ch_dead_band.falling_delay = 200; | ||
|  | 
 | ||
|  |     iot_epwm_ch_config_set(ch, &ch_config); | ||
|  |     iot_epwm_ch_start(ch); | ||
|  |     iot_epwm_global_start(); | ||
|  | } | ||
|  | 
 | ||
|  | /* single pulse mode enable. */ | ||
|  | static void iot_epwm_kl3_test_case5_init(void) | ||
|  | { | ||
|  |     uint8_t ch = IOT_EPWM_CHANNEL_5; | ||
|  |     iot_epwm_global_config_t global_config = IOT_EPWM_GLOBAL_CONFIG_DEFAULT; | ||
|  |     iot_epwm_ch_config_t ch_config = IOT_EPWM_CHANNEL_CONFIG_DEFAULT; | ||
|  | 
 | ||
|  |     iot_cus_printf("%s\r\n", __func__); | ||
|  |     iot_pwm_api_mode_select(IOT_API_MODE_EPWM); | ||
|  |     iot_epwm_global_stop(); | ||
|  |     global_config.clk_src = IOT_PWM_CLK_SRC_150M; | ||
|  |     iot_epwm_global_config_set(&global_config); | ||
|  | 
 | ||
|  |     ch_config.ch_clk_div = 15; | ||
|  |     ch_config.ch_pin_outa = 4; | ||
|  |     ch_config.ch_pin_outb = 7; | ||
|  |     ch_config.ch_polarity_outa = IOT_EPWM_POLARITY_NORMAL; | ||
|  |     ch_config.ch_polarity_outb = IOT_EPWM_POLARITY_INVERTED; | ||
|  |     ch_config.ch_usage = IOT_EPWM_CH_USEAGE_COMMON; | ||
|  |     ch_config.ch_common_cfg.freq = 1000; | ||
|  |     ch_config.ch_common_cfg.duty = 5000; | ||
|  |     ch_config.ch_align_mode = IOT_EPWM_ALIGN_EDGE; | ||
|  | 
 | ||
|  |     ch_config.ch_single_pulse_en = 1; | ||
|  |     iot_epwm_ch_config_set(ch, &ch_config); | ||
|  |     iot_epwm_ch_start(ch); | ||
|  |     iot_epwm_global_start(); | ||
|  | } | ||
|  | 
 | ||
|  | static const void (*test_case_init_list[])(void) = { | ||
|  |     iot_epwm_kl3_test_case1_init, | ||
|  |     iot_epwm_kl3_test_case2_init, | ||
|  |     iot_epwm_kl3_test_case3_init, | ||
|  |     iot_epwm_kl3_test_case4_init, | ||
|  |     iot_epwm_kl3_test_case5_init, | ||
|  | }; | ||
|  | 
 | ||
|  | static const void (*test_case_update_list[])(void) = { | ||
|  |     iot_epwm_kl3_test_case1_update, | ||
|  |     iot_epwm_kl3_test_case2_update, | ||
|  |     NULL, | ||
|  |     NULL, | ||
|  |     NULL, | ||
|  | }; | ||
|  | #else
 | ||
|  | static const void (*test_case_init_list[])(void) = {}; | ||
|  | static const void (*test_case_update_list[])(void) = {}; | ||
|  | #endif
 | ||
|  | 
 | ||
|  | static void (*init_handler)(void) = NULL; | ||
|  | static void (*update_handler)(void) = NULL; | ||
|  | static iot_uart_h uart_epwm = NULL; | ||
|  | 
 | ||
|  | static uint32_t app_epwm_demo_uart_recv(uint8_t* buffer, uint32_t buffer_len, | ||
|  |     bool_t is_full_frame, uint32_t invalid_data_len) | ||
|  | { | ||
|  |     (void)is_full_frame; | ||
|  |     (void)invalid_data_len; | ||
|  |     char c = 0; | ||
|  | 
 | ||
|  |     if (buffer_len > 0) { | ||
|  |         c = buffer[0]; | ||
|  |     } else { | ||
|  |         return ERR_OK; | ||
|  |     } | ||
|  | 
 | ||
|  |     /* Select test case n by char 'n' from uart, and char 'Q','q' or ESC to
 | ||
|  |         stop the case. Any other char to update parameter. | ||
|  |         To run another case, a power on reset is required.*/ | ||
|  |     if (init_handler == NULL) { | ||
|  |         uint8_t index = c - '1'; | ||
|  |         if ((index < sizeof(test_case_init_list)/sizeof(test_case_init_list[0])) | ||
|  |             && (test_case_init_list[index] != NULL)) { | ||
|  |             init_handler = test_case_init_list[index]; | ||
|  |             update_handler = test_case_update_list[index]; | ||
|  | 
 | ||
|  |             iot_cus_printf("Run test case %d.\r\n", index + 1); | ||
|  |             init_handler(); | ||
|  |         } | ||
|  |     }else { | ||
|  |         if ('q' == c || 'Q' == c || 0x1B == c) { | ||
|  |             /* press key Q or ESC to stop test case. */ | ||
|  |             iot_epwm_global_stop(); | ||
|  |             init_handler = NULL; | ||
|  |             update_handler = NULL; | ||
|  |         } else { | ||
|  |             /* press any other key to update epwm config. */ | ||
|  |             if (NULL != update_handler) { | ||
|  |                 update_handler(); | ||
|  |             } | ||
|  |         } | ||
|  |     } | ||
|  | 
 | ||
|  |     return ERR_OK; | ||
|  | } | ||
|  | 
 | ||
|  | static uint32_t app_epwm_demo_uart_init(void) | ||
|  | { | ||
|  |     uint8_t port; | ||
|  |     uint8_t port_name = UART_METER_PORT; | ||
|  | 
 | ||
|  |     /* Get uart port for tranceiving data, not for printf() used. */ | ||
|  |     if((port = iot_board_get_uart(port_name)) >= iot_uart_get_max_port_num()) | ||
|  |     { | ||
|  |         DEMO_ERROR("INVALID UART#%d !!", port, 2,3,4,5,6); | ||
|  |         return ERR_FAIL; | ||
|  |     } | ||
|  | 
 | ||
|  |     if(NULL == (uart_epwm = iot_uart_open | ||
|  |         (port, (iot_uart_recv_func_t)app_epwm_demo_uart_recv, | ||
|  |         EPWM_DEMO_UART_RCV_BUF_LEN, NULL))) | ||
|  |     { | ||
|  |         DEMO_ERROR("OPEN UART#%d FAILED !!", port, 2,3,4,5,6); | ||
|  |         return ERR_FAIL; | ||
|  |     } | ||
|  | 
 | ||
|  |     if (iot_uart_set_config(uart_epwm, EPWM_DEMO_UART_BAUND, 0, 8, 1)) { | ||
|  |         iot_uart_set_threshold(uart_epwm, UART_THR_RXFULL, 1); | ||
|  |         iot_uart_set_threshold(uart_epwm, UART_THR_NO_FMT_TIMEOUT, 20); | ||
|  |         return ERR_OK; | ||
|  |     } | ||
|  | 
 | ||
|  |     return ERR_FAIL; | ||
|  | } | ||
|  | 
 | ||
|  | extern void iot_print_config(bool_t enable); | ||
|  | 
 | ||
|  | int app_demo_epwm_init(void) | ||
|  | { | ||
|  |     int ret = ERR_FAIL; | ||
|  | 
 | ||
|  |     /* Enable the customer-printf(). */ | ||
|  |     iot_cus_print_config(true); | ||
|  |     iot_print_config(true); | ||
|  | 
 | ||
|  |     do | ||
|  |     { | ||
|  |         if(ERR_OK != (ret = app_epwm_demo_uart_init())) { | ||
|  |             DEMO_ERROR("UART INIT FAILED !!", 1,2,3,4,5,6); | ||
|  |             break; | ||
|  |         } | ||
|  | 
 | ||
|  |         iot_cus_printf("Press 1~%d to select a test case:\r\n", | ||
|  |             sizeof(test_case_init_list)/sizeof(test_case_init_list[0])); | ||
|  |         if (NULL != init_handler) { | ||
|  |             init_handler(); | ||
|  |         } | ||
|  | 
 | ||
|  |         ret = ERR_OK; | ||
|  |     }while(0); | ||
|  | 
 | ||
|  |     return ret; | ||
|  | } |