diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/linux/.gitignore b/linux/.gitignore new file mode 100644 index 0000000..fa92975 --- /dev/null +++ b/linux/.gitignore @@ -0,0 +1 @@ +*.out \ No newline at end of file diff --git a/linux/README.md b/linux/README.md new file mode 100644 index 0000000..cabac63 --- /dev/null +++ b/linux/README.md @@ -0,0 +1,38 @@ + +# README + +## 编译 + +```bash +./build.sh +``` + +## 使用 + +```bash + +# Usage: ././stm32_timer_pwm_computer.out system_clk(mhz) expect_freq(khz) + +➜ stm32_timer_pwm_computer ./stm32_timer_pwm_computer.out 168 38 +systemclk : 168.000000 mhz +expect freq: 38.000000 khz +****************************************************** +* APB1: TIM2 TIM3 TIM4 TIM5 TIM6 TIM7 TIM12 TIM13 TIM14 +* +* timer_module_clk: 84.000000 mhz +* freq : 38.009051 khz +* period : 0.026310 ms +* prescaler : 0 +* autoreload : 2209 +* +** +* APB2: TIM1 TIM8 TIM9 TIM10 TIM11 +* +* timer_module_clk: 168.000000 mhz +* freq : 38.000453 khz +* period : 0.026315 ms +* prescaler : 0 +* autoreload : 4420 +*** + +``` diff --git a/linux/build.sh b/linux/build.sh new file mode 100644 index 0000000..b1800ac --- /dev/null +++ b/linux/build.sh @@ -0,0 +1 @@ +gcc main.c -o stm32_timer_pwm_computer.out \ No newline at end of file diff --git a/linux/main.c b/linux/main.c new file mode 100644 index 0000000..6739423 --- /dev/null +++ b/linux/main.c @@ -0,0 +1,142 @@ +#include +#include +#include +#if 0 +void trigger_pwm(int32_t freq, float duty, int32_t pluse_num) { + //无论怎样先停止PWM输 + HAL_TIM_PWM_Stop_IT(&htim3, TIM_CHANNEL_1); + if (freq == 0) { + LOGE("E:trigger pwm fail,freq == 0\n"); + return; + } + // 确定预分频参数,重载值 + float psc_x_arr = 90 * 1000 /*系统时钟*/ * 1000 / freq; + uint32_t psc = 0; + uint32_t arr = 65534; + for (; arr > 2; arr--) { + psc = psc_x_arr / arr; + if (psc >= 1) { + uint32_t tmparr = psc_x_arr / psc; + if (tmparr >= 65534) continue; + break; + } + } + if (psc == 0) return; + if (arr <= 3) return; //定时器一周期的分辨率太小了 + arr = psc_x_arr / psc; + + arr = arr - 1; + psc = psc - 1; + + __HAL_TIM_SET_AUTORELOAD(&htim3, arr); + __HAL_TIM_SET_PRESCALER(&htim3, psc); + // 确定PWM通道占空比 + uint16_t comparevalue = duty * arr; + __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, comparevalue); + if (pluse_num == 0) { + g_continue_output_mode = true; + } else { + g_pulse_num = pluse_num - 1; + g_continue_output_mode = false; + } + __HAL_TIM_SET_COUNTER(&htim3, 0); + LOGD("a[%d] p[%d]c[%d]\r\n", arr, psc, comparevalue); + HAL_TIM_PWM_Start_IT(&htim3, TIM_CHANNEL_1); +} +#endif + +typedef struct +{ + int autoreload; + int prescaler; + float realfreq; +} timer_config_t; + +bool compute_timer_parameter(timer_config_t *config, int timer_in_clk /*mhz*/, int infreq /*hz*/) +{ + float psc_x_arr = timer_in_clk * 1000 /*定时器模块时钟*/ * 1000 / infreq; + uint32_t psc = 0; + uint32_t arr = 65534; + for (; arr > 2; arr--) + { + psc = psc_x_arr / arr; + if (psc >= 1) + { + uint32_t tmparr = psc_x_arr / psc; + if (tmparr >= 65534) + continue; + break; + } + } + if (psc == 0) + return false; + if (arr <= 3) + return false; //定时器一周期的分辨率太小了 + + arr = psc_x_arr / psc; + + int psc_x_arr_real = arr * psc; + float realfreq = timer_in_clk * 1000.0 /*定时器模块时钟*/ * 1000 / psc_x_arr_real; + + arr = arr - 1; + psc = psc - 1; + + config->autoreload = arr; + config->prescaler = psc; + config->realfreq = realfreq; + // config->compare = arr / 2; + + return false; +} + +void dumpconfig_info(timer_config_t *config) +{ + printf("* freq : %f \n", config->realfreq); + printf("* prescaler : %d \n", config->prescaler); + printf("* autoreload: %d \n", config->autoreload); +} + +int main(int argc, char const *argv[]) +{ + if (argc != 3) + { + printf("%s system_clk mhz expect_freq khz\n", argv[0]); + return -1; + } + + float expect_freq = 0; + float systemclk = 0; + sscanf(argv[1], "%f", &systemclk); + sscanf(argv[2], "%f", &expect_freq); + printf("systemclk : %f mhz\n", systemclk); + printf("expect freq: %f khz\n", expect_freq); + + int32_t infreq = (int32_t)(expect_freq * 1000); + timer_config_t timerconfig_apb1; + timer_config_t timerconfig_apb2; + + //apb1 + compute_timer_parameter(&timerconfig_apb1, systemclk / 2, infreq); + //apb2 + compute_timer_parameter(&timerconfig_apb2, systemclk, infreq); + + printf("******************************************************\n"); + printf("* APB1: TIM2 TIM3 TIM4 TIM5 TIM6 TIM7 TIM12 TIM13 TIM14\n"); + printf("*\n"); + printf("* timer_module_clk: %f mhz\n", systemclk / 2); + printf("* freq : %f khz\n", timerconfig_apb1.realfreq / 1000.0); + printf("* period : %f ms\n", (1.0 / timerconfig_apb1.realfreq * 1000)); + printf("* prescaler : %d \n", timerconfig_apb1.prescaler); + printf("* autoreload : %d \n", timerconfig_apb1.autoreload); + printf("*\n"); + printf("**\n"); + printf("* APB2: TIM1 TIM8 TIM9 TIM10 TIM11\n"); + printf("*\n"); + printf("* timer_module_clk: %f mhz\n", systemclk); + printf("* freq : %f khz\n", timerconfig_apb2.realfreq / 1000.0); + printf("* period : %f ms\n", (1.0 / timerconfig_apb2.realfreq * 1000)); + printf("* prescaler : %d \n", timerconfig_apb2.prescaler); + printf("* autoreload : %d \n", timerconfig_apb2.autoreload); + printf("***\n"); + return 0; +} diff --git a/release/stm32_pwm_computer.exe b/release/stm32_pwm_computer.exe new file mode 100644 index 0000000..3ccb889 Binary files /dev/null and b/release/stm32_pwm_computer.exe differ diff --git a/.gitattributes b/windows/.gitattributes similarity index 100% rename from .gitattributes rename to windows/.gitattributes diff --git a/.gitignore b/windows/.gitignore similarity index 100% rename from .gitignore rename to windows/.gitignore diff --git a/windows/README.md b/windows/README.md new file mode 100644 index 0000000..2d88757 --- /dev/null +++ b/windows/README.md @@ -0,0 +1,42 @@ + +# README + +## TODO + +支持windows图形界面 + +## 编译 + +```bash +./build.sh +``` + +## 使用 + +```bash + +# Usage: ././stm32_timer_pwm_computer.out system_clk(mhz) expect_freq(khz) + +➜ stm32_timer_pwm_computer ./stm32_timer_pwm_computer.out 168 38 +systemclk : 168.000000 mhz +expect freq: 38.000000 khz +****************************************************** +* APB1: TIM2 TIM3 TIM4 TIM5 TIM6 TIM7 TIM12 TIM13 TIM14 +* +* timer_module_clk: 84.000000 mhz +* freq : 38.009051 khz +* period : 0.026310 ms +* prescaler : 0 +* autoreload : 2209 +* +** +* APB2: TIM1 TIM8 TIM9 TIM10 TIM11 +* +* timer_module_clk: 168.000000 mhz +* freq : 38.000453 khz +* period : 0.026315 ms +* prescaler : 0 +* autoreload : 4420 +*** + +``` diff --git a/stm32_pwm_computer.cpp b/windows/stm32_pwm_computer.cpp similarity index 100% rename from stm32_pwm_computer.cpp rename to windows/stm32_pwm_computer.cpp diff --git a/stm32_pwm_computer.sln b/windows/stm32_pwm_computer.sln similarity index 100% rename from stm32_pwm_computer.sln rename to windows/stm32_pwm_computer.sln diff --git a/stm32_pwm_computer.vcxproj b/windows/stm32_pwm_computer.vcxproj similarity index 100% rename from stm32_pwm_computer.vcxproj rename to windows/stm32_pwm_computer.vcxproj diff --git a/stm32_pwm_computer.vcxproj.filters b/windows/stm32_pwm_computer.vcxproj.filters similarity index 100% rename from stm32_pwm_computer.vcxproj.filters rename to windows/stm32_pwm_computer.vcxproj.filters