Browse Source

fix: fix wrong clock input

192.168.1.134 3 years ago
parent
commit
9fa72d0b81

+ 18 - 65
bsp/n32g452xx/Libraries/rt_drivers/drv_hwtimer.c

@@ -49,34 +49,6 @@ enum
 #ifdef BSP_USING_HW_TIM8
     TIM8_INDEX,
 #endif
-
-#ifdef BSP_USING_HWTIM9
-    TIM9_INDEX,
-#endif
-
-#ifdef BSP_USING_HWTIM10
-    TIM10_INDEX,
-#endif
-
-#ifdef BSP_USING_HWTIM11
-    TIM11_INDEX,
-#endif
-
-#ifdef BSP_USING_HWTIM12
-    TIM12_INDEX,
-#endif
-
-#ifdef BSP_USING_HWTIM13
-    TIM13_INDEX,
-#endif
-
-#ifdef BSP_USING_HWTIM14
-    TIM14_INDEX,
-#endif
-
-#ifdef BSP_USING_HWTIM15
-    TIM15_INDEX,
-#endif
 };
 
 struct n32_hwtimer
@@ -120,34 +92,6 @@ static struct n32_hwtimer n32_hwtimer_obj[] =
 #ifdef BSP_USING_HWTIM8
     TIM8_CONFIG,
 #endif
-
-#ifdef BSP_USING_HWTIM9
-    TIM9_CONFIG,
-#endif
-
-#ifdef BSP_USING_HWTIM10
-    TIM10_CONFIG,
-#endif
-
-#ifdef BSP_USING_HWTIM11
-    TIM11_CONFIG,
-#endif
-
-#ifdef BSP_USING_HWTIM12
-    TIM12_CONFIG,
-#endif
-
-#ifdef BSP_USING_HWTIM13
-    TIM13_CONFIG,
-#endif
-
-#ifdef BSP_USING_HWTIM14
-    TIM14_CONFIG,
-#endif
-
-#ifdef BSP_USING_HWTIM15
-    TIM15_CONFIG,
-#endif
 };
 
 static void n32_timer_init(struct rt_hwtimer_device *timer, rt_uint32_t state)
@@ -155,6 +99,8 @@ static void n32_timer_init(struct rt_hwtimer_device *timer, rt_uint32_t state)
     RCC_ClocksType RCC_ClockStruct;
     TIM_TimeBaseInitType TIM_TimeBaseStructure;
     NVIC_InitType NVIC_InitStructure;
+    uint32_t freq = 0;
+    uint32_t input_clock;
     uint32_t prescaler_value = 0;
     TIM_Module *tim = RT_NULL;
     struct n32_hwtimer *tim_device = RT_NULL;
@@ -165,18 +111,22 @@ static void n32_timer_init(struct rt_hwtimer_device *timer, rt_uint32_t state)
         tim = (TIM_Module *)timer->parent.user_data;
         tim_device = (struct n32_hwtimer *)timer;
 
+        RT_ASSERT((tim == TIM2) || (tim == TIM3) || (tim == TIM4) || (tim == TIM5)
+            || (tim == TIM6) || (tim == TIM7));
+
         /* timer clock enable */
         n32_msp_hwtim_init(tim);
 
-        /* timer init */
+        freq = timer->freq;
         RCC_GetClocksFreqValue(&RCC_ClockStruct);
-        /* Set timer clock is 1Mhz */
-        prescaler_value = (uint32_t)(RCC_ClockStruct.SysclkFreq / 10000) - 1;
+        if (1 == (RCC_ClockStruct.HclkFreq / RCC_ClockStruct.Pclk1Freq))
+            input_clock = RCC_ClockStruct.Pclk1Freq;
+        else
+            input_clock = RCC_ClockStruct.Pclk1Freq * 2;
+        prescaler_value = (uint32_t)(input_clock / freq) - 1;
 
-        TIM_TimeBaseStructure.Period = 10000 - 1;
-        rt_kprintf("Period=[%d]", TIM_TimeBaseStructure.Period);
+        TIM_TimeBaseStructure.Period = freq - 1;
         TIM_TimeBaseStructure.Prescaler = prescaler_value;
-        rt_kprintf("Prescaler=[%d]", TIM_TimeBaseStructure.Prescaler);
         TIM_TimeBaseStructure.ClkDiv = TIM_CLK_DIV1;
         TIM_TimeBaseStructure.RepetCnt = 0;
 
@@ -274,6 +224,7 @@ static rt_err_t n32_timer_ctrl(rt_hwtimer_t *timer, rt_uint32_t cmd, void *arg)
     {
     case HWTIMER_CTRL_FREQ_SET:
     {
+        rt_uint32_t input_clock;
         rt_uint32_t freq;
         rt_uint16_t val;
 
@@ -282,9 +233,11 @@ static rt_err_t n32_timer_ctrl(rt_hwtimer_t *timer, rt_uint32_t cmd, void *arg)
 
         /* time init */
         RCC_GetClocksFreqValue(&RCC_ClockStruct);
-
-        val = RCC_ClockStruct.SysclkFreq / freq;
-
+        if (1 == (RCC_ClockStruct.HclkFreq / RCC_ClockStruct.Pclk1Freq))
+            input_clock = RCC_ClockStruct.Pclk1Freq;
+        else
+            input_clock = RCC_ClockStruct.Pclk1Freq * 2;
+        val = input_clock / freq;
         TIM_ConfigPrescaler(tim, val - 1, TIM_PSC_RELOAD_MODE_IMMEDIATE);
     }
     break;

+ 1 - 1
bsp/n32g452xx/Libraries/rt_drivers/drv_hwtimer.h

@@ -22,7 +22,7 @@ extern "C" {
 #define TIM_DEV_INFO_CONFIG                     \
     {                                           \
         .maxfreq = 1000000,                     \
-        .minfreq = 4000,                        \
+        .minfreq = 1000,                        \
         .maxcnt  = 0xFFFF,                      \
         .cntmode = HWTIMER_CNTMODE_UP,          \
     }