17 changed files with 491 additions and 124 deletions
-
3.vscode/settings.json
-
2Core/Inc/main.h
-
2Core/Inc/stm32f1xx_it.h
-
84Core/Src/main.c
-
57Core/Src/stm32f1xx_hal_msp.c
-
65Core/Src/stm32f1xx_hal_timebase_tim.c
-
30Core/Src/stm32f1xx_it.c
-
7README.md
-
62light_src_ctrl_stm32_port.ioc
-
55usrc/light_intensity_ctrl.c
-
23usrc/light_intensity_ctrl.h
-
3usrc/project_configs.h
-
78usrc/umain.c
-
2zlib/zlib.h
-
49zlib/zlog.h
-
84zlib/ztim.c
-
9zlib/ztim.h
@ -0,0 +1,55 @@ |
|||
#include "light_intensity_ctrl.h" |
|||
|
|||
extern TIM_HandleTypeDef LIGHT_INTENSITY_TIM; |
|||
|
|||
static uint32_t m_light_intensity = 50; |
|||
static TIM_HandleTypeDef* m_htim = &LIGHT_INTENSITY_TIM; |
|||
static uint32_t m_freq = 1000; |
|||
|
|||
void LightIntensityCtrl_init() { |
|||
ZASSERT(m_htim->Init.AutoReloadPreload == TIM_AUTORELOAD_PRELOAD_ENABLE); |
|||
ZASSERT(m_htim->Init.CounterMode == TIM_COUNTERMODE_UP); |
|||
} |
|||
void LightIntensityCtrl_setIntensity(uint32_t intensity) { |
|||
if (intensity > 100) { |
|||
intensity = 100; |
|||
} |
|||
m_light_intensity = intensity; |
|||
} |
|||
uint32_t LightIntensityCtrl_getIntensity() { return m_light_intensity; } |
|||
void LightIntensityCtrl_start() { |
|||
uint32_t prescaler = 0; |
|||
uint32_t autoreload = 0; |
|||
|
|||
// ÉèÖÃÆµÂÊ |
|||
uint32_t timClkFreq = chip_get_timer_clock_sorce_freq(m_htim); |
|||
ZASSERT(chip_calculate_prescaler_and_autoreload_by_expect_freq(timClkFreq, m_freq, &prescaler, &autoreload)); |
|||
|
|||
// m_htim-> |
|||
HAL_TIM_PWM_Stop(m_htim, TIM_CHANNEL_1); |
|||
HAL_TIM_PWM_Stop(m_htim, TIM_CHANNEL_2); |
|||
HAL_TIM_PWM_Stop(m_htim, TIM_CHANNEL_3); |
|||
HAL_TIM_PWM_Stop(m_htim, TIM_CHANNEL_4); |
|||
|
|||
__HAL_TIM_SET_AUTORELOAD(m_htim, autoreload); |
|||
__HAL_TIM_SET_PRESCALER(m_htim, prescaler); |
|||
|
|||
// ÅäÖÃģʽ |
|||
TIM_OC_InitTypeDef sConfigOC = {0}; |
|||
sConfigOC.OCMode = TIM_OCMODE_PWM1; |
|||
sConfigOC.Pulse = m_light_intensity / 100.0 * __HAL_TIM_GET_AUTORELOAD(m_htim); |
|||
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; |
|||
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; |
|||
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; |
|||
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET; |
|||
|
|||
ZSTM32_CHECK(HAL_TIM_PWM_ConfigChannel(m_htim, &sConfigOC, TIM_CHANNEL_1)); |
|||
ZSTM32_CHECK(HAL_TIM_PWM_ConfigChannel(m_htim, &sConfigOC, TIM_CHANNEL_2)); |
|||
ZSTM32_CHECK(HAL_TIM_PWM_ConfigChannel(m_htim, &sConfigOC, TIM_CHANNEL_3)); |
|||
ZSTM32_CHECK(HAL_TIM_PWM_ConfigChannel(m_htim, &sConfigOC, TIM_CHANNEL_4)); |
|||
|
|||
ZSTM32_CHECK(HAL_TIM_PWM_Start(m_htim, TIM_CHANNEL_1)); |
|||
ZSTM32_CHECK(HAL_TIM_PWM_Start(m_htim, TIM_CHANNEL_2)); |
|||
ZSTM32_CHECK(HAL_TIM_PWM_Start(m_htim, TIM_CHANNEL_3)); |
|||
ZSTM32_CHECK(HAL_TIM_PWM_Start(m_htim, TIM_CHANNEL_4)); |
|||
} |
@ -0,0 +1,23 @@ |
|||
#pragma once |
|||
#include <stdint.h> |
|||
|
|||
#include "zlib.h" |
|||
#ifdef __cplusplus |
|||
extern "C" { |
|||
#endif |
|||
|
|||
/** |
|||
* @brief |
|||
* 定时器配置参考: https://iflytop1.feishu.cn/wiki/DhNhwteIGijHy5kC53Dcfp83nZ6 |
|||
* |
|||
* |
|||
*/ |
|||
|
|||
void LightIntensityCtrl_init(); |
|||
void LightIntensityCtrl_setIntensity(uint32_t intensity); |
|||
uint32_t LightIntensityCtrl_getIntensity(); |
|||
void LightIntensityCtrl_start(); |
|||
|
|||
#ifdef __cplusplus |
|||
} |
|||
#endif |
@ -0,0 +1,84 @@ |
|||
#include "ztim.h" |
|||
#include "zlog.h" |
|||
static float zfeq(float val0, float val1, float eps) { |
|||
float dv = val0 - val1; |
|||
if (dv < 0) dv = -dv; |
|||
if (dv < eps) return true; |
|||
return false; |
|||
} |
|||
|
|||
bool chip_calculate_prescaler_and_autoreload_by_expect_freq(uint32_t timerInClk, float infreqhz, uint32_t* prescaler, uint32_t* autoreload) { |
|||
/** |
|||
* @brief 计算寄存器数值 |
|||
*/ |
|||
ZASSERT(!zfeq(infreqhz, 0, 0.01)); |
|||
float psc_x_arr = timerInClk / infreqhz; |
|||
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 = timerInClk / psc_x_arr_real; |
|||
|
|||
arr = arr - 1; |
|||
psc = psc - 1; |
|||
// uint16_t comparevalue = 50 / 100.0 * arr; |
|||
|
|||
*prescaler = psc; |
|||
*autoreload = arr; |
|||
return true; |
|||
} |
|||
|
|||
uint32_t chip_get_timer_clock_sorce_freq(TIM_HandleTypeDef* tim) { |
|||
uint32_t timClkFreq = 0; |
|||
#if 0 |
|||
uint32_t pclk1Freq = HAL_RCC_GetPCLK1Freq(); |
|||
uint32_t pclk2Freq = HAL_RCC_GetPCLK2Freq(); |
|||
uint32_t sysClkFreq = HAL_RCC_GetSysClockFreq(); |
|||
#endif |
|||
|
|||
uint32_t pFLatency; |
|||
RCC_ClkInitTypeDef clkconfig; |
|||
HAL_RCC_GetClockConfig(&clkconfig, &pFLatency); |
|||
bool isAPB2 = false; |
|||
|
|||
#ifdef TIM1 |
|||
if (tim->Instance == TIM1) isAPB2 = true; |
|||
#endif |
|||
#ifdef TIM8 |
|||
if (tim->Instance == TIM8) isAPB2 = true; |
|||
#endif |
|||
#ifdef TIM9 |
|||
if (tim->Instance == TIM9) isAPB2 = true; |
|||
#endif |
|||
#ifdef TIM10 |
|||
if (tim->Instance == TIM10) isAPB2 = true; |
|||
#endif |
|||
#ifdef TIM11 |
|||
if (tim->Instance == TIM11) isAPB2 = true; |
|||
#endif |
|||
if (isAPB2) { |
|||
if (clkconfig.APB2CLKDivider == RCC_HCLK_DIV1) { |
|||
timClkFreq = HAL_RCC_GetPCLK2Freq(); |
|||
} else { |
|||
timClkFreq = 2 * HAL_RCC_GetPCLK2Freq(); |
|||
} |
|||
} else { |
|||
if (clkconfig.APB1CLKDivider == RCC_HCLK_DIV1) { |
|||
timClkFreq = HAL_RCC_GetPCLK1Freq(); |
|||
} else { |
|||
timClkFreq = 2 * HAL_RCC_GetPCLK1Freq(); |
|||
} |
|||
} |
|||
return timClkFreq; |
|||
} |
@ -0,0 +1,9 @@ |
|||
#pragma once |
|||
|
|||
#include <stdbool.h> |
|||
#include <stdint.h> |
|||
|
|||
#include "main.h" |
|||
|
|||
bool chip_calculate_prescaler_and_autoreload_by_expect_freq(uint32_t timerInClk, float infreqhz, uint32_t* prescaler, uint32_t* autoreload); |
|||
uint32_t chip_get_timer_clock_sorce_freq(TIM_HandleTypeDef* tim); |
Write
Preview
Loading…
Cancel
Save
Reference in new issue