Files
kunlun/dtest/kl2_pwm_test/kl2_pwm_test.c
2024-09-28 14:24:04 +08:00

156 lines
3.9 KiB
C
Executable File

/* os shim includes */
#include "os_types.h"
#include "dbg_io.h"
#include "iot_io.h"
#include "pwm.h"
#include "iot_pwm_api.h"
#include "clk.h"
#include "pwm_hw.h"
void clk_core_freq_75m_set();
void clk_core_freq_25m_set();
int clk_core_freq_set(uint32_t freq);
void reg_dump(uint32_t baseaddr, uint32_t offset)
{
uint32_t temp, index;
for(index = 0; index < offset; index += 4) {
if((index % 16) == 0) {
iot_printf("\n");
}
iot_printf("%08x ",*(int *)(baseaddr + index));
temp = 100000;
while(temp--);
}
iot_printf("\n");
}
extern int pwm_dead_band_config(uint8_t ch, uint32_t rising_db, uint32_t falling_db,
uint32_t *rising_actual, uint32_t *falling_actual);
extern void pwm_fault_config(uint8_t ch, uint8_t gpio, uint8_t h_state,
uint8_t l_state,pwm_isr cb);
extern int clk_pwm_div_motor_set(uint8_t div);
extern int pwm_set_clk(uint8_t clk_src);
uint32_t pwm_falut_cb(void)
{
iot_printf("fault\r\n");
return 0;
}
void kl2_pwm_test_delay(int n)
{
volatile uint32_t a, b;
for(a = 0; a < n ; a ++) {
for(b = 0; b < 100; b++);
}
}
void pwm_main(void)
{
volatile uint32_t delay = 0;
uint8_t duty = 50;
uint8_t ch = PWM_CHANNEL_1;
uint8_t test_for_single_mode = 0;
uint32_t clk_freq = 0;
uint32_t a_r=0, a_f=0;
dbg_uart_init();
iot_pwm_gpio_config(ch, 38, 40);
if (test_for_single_mode) {
iot_pwm_single_mode_init(ch);
clk_freq = pwm_get_ch_clk(ch);
/* set period 1ms. if not set will use default period 1s. */
iot_pwm_set_period(ch, clk_freq / 1000);
iot_pwm_set_duty(ch, duty * 100);
while (1) {
iot_pwm_start_for_single(ch);
iot_printf("pwm single mode running...\n");
delay = 100000;
while(delay--);
}
} else {
clk_core_freq_set(CPU_FREQ_25M);
iot_pwm_hw_init(ch);
clk_freq = pwm_get_ch_clk(ch);
/* set period 1ms. if not set will use default period 1s. */
iot_pwm_set_period(ch, 1000);
iot_pwm_start(ch);
iot_printf("clk_freq=%d\r\n", clk_freq);
if(pwm_dead_band_config(ch, 250, 350, &a_r, &a_f) < 0) {
iot_printf("dead_band_set err\r\n");
}
iot_printf("dead_band_set a_r=%d, a_f=%d\r\n", a_r, a_f);
//pwm_fault_config(ch, 39, 0, 0, pwm_falut_cb);
if (pwm_set_clk(IOT_PWM_CLK_SRC_2M5) < 0) {
iot_printf("set err %d\r\n", __LINE__);
}
iot_pwm_set_duty(ch, duty * 100);
kl2_pwm_test_delay(10000);
if (pwm_set_clk(IOT_PWM_CLK_SRC_25M) < 0) {
iot_printf("set err %d\r\n", __LINE__);
}
iot_pwm_set_duty(ch, duty * 100);
kl2_pwm_test_delay(10000);
if (pwm_set_clk(IOT_PWM_CLK_SRC_50M) < 0) {
iot_printf("set err %d\r\n", __LINE__);
}
iot_pwm_set_duty(ch, duty * 100);
kl2_pwm_test_delay(10000);
if (pwm_set_clk(IOT_PWM_CLK_SRC_75M) < 0) {
iot_printf("set err %d\r\n", __LINE__);
}
iot_pwm_set_duty(ch, duty * 100);
kl2_pwm_test_delay(10000);
if (pwm_set_clk(IOT_PWM_CLK_SRC_100M) < 0) {
iot_printf("set err %d\r\n", __LINE__);
}
iot_pwm_set_duty(ch, duty * 100);
kl2_pwm_test_delay(10000);
while(1);
while(1) {
for (duty = 0; duty <= 100; duty += 5) {
iot_pwm_set_duty(ch, duty * 100);
iot_printf("PWM Duty: %d%%\r\n",duty);
delay = 10000000;
while (delay--);
}
for (duty = 100; duty > 0; duty -= 5) {
iot_pwm_set_duty(ch, duty * 100);
iot_printf("PWM Duty: %d%%\r\n",duty);
delay = 10000000;
while (delay--);
}
}
}
}
#ifdef __GNUC__
int main(void)
{
pwm_main();
return 0;
}
#endif // __GCC__