Browse Source

[stm32][l4] add tim7 support | 设备框架以及驱动错误码处理逻辑存在问题 (#5901)

Man, Jianting (Meco) 3 years ago
parent
commit
0d2b96d7cc

+ 11 - 0
bsp/stm32/libraries/HAL_Drivers/config/l4/tim_config.h

@@ -27,6 +27,17 @@ extern "C" {
     }
 #endif /* TIM_DEV_INFO_CONFIG */
 
+#ifdef BSP_USING_TIM7
+#ifndef TIM7_CONFIG
+#define TIM7_CONFIG                                        \
+    {                                                      \
+       .tim_handle.Instance     = TIM7,                    \
+       .tim_irqn                = TIM7_IRQn,               \
+       .name                    = "timer7",                \
+    }
+#endif /* TIM7_CONFIG */
+#endif /* BSP_USING_TIM7 */
+
 #ifdef BSP_USING_TIM15
 #ifndef TIM15_CONFIG
 #define TIM15_CONFIG                                        \

+ 20 - 2
bsp/stm32/libraries/HAL_Drivers/drv_hwtimer.c

@@ -317,7 +317,7 @@ static void timer_stop(rt_hwtimer_t *timer)
 static rt_err_t timer_ctrl(rt_hwtimer_t *timer, rt_uint32_t cmd, void *arg)
 {
     TIM_HandleTypeDef *tim = RT_NULL;
-    rt_err_t result = RT_EOK;
+    rt_err_t result = -RT_ERROR;
     uint32_t pclk1_doubler, pclk2_doubler;
 
     RT_ASSERT(timer != RT_NULL);
@@ -361,11 +361,13 @@ static rt_err_t timer_ctrl(rt_hwtimer_t *timer, rt_uint32_t cmd, void *arg)
 
         /* Update frequency value */
         tim->Instance->EGR |= TIM_EVENTSOURCE_UPDATE;
+
+        result = RT_EOK;
     }
     break;
     default:
     {
-        result = -RT_ENOSYS;
+        result = -RT_EINVAL;
     }
     break;
     }
@@ -435,6 +437,16 @@ void TIM5_IRQHandler(void)
     rt_interrupt_leave();
 }
 #endif
+#ifdef BSP_USING_TIM7
+void TIM7_IRQHandler(void)
+{
+    /* enter interrupt */
+    rt_interrupt_enter();
+    HAL_TIM_IRQHandler(&stm32_hwtimer_obj[TIM7_INDEX].tim_handle);
+    /* leave interrupt */
+    rt_interrupt_leave();
+}
+#endif
 #ifdef BSP_USING_TIM11
 void TIM1_TRG_COM_TIM11_IRQHandler(void)
 {
@@ -534,6 +546,12 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
         rt_device_hwtimer_isr(&stm32_hwtimer_obj[TIM5_INDEX].time_device);
     }
 #endif
+#ifdef BSP_USING_TIM7
+    if (htim->Instance == TIM7)
+    {
+        rt_device_hwtimer_isr(&stm32_hwtimer_obj[TIM7_INDEX].time_device);
+    }
+#endif
 #ifdef BSP_USING_TIM11
     if (htim->Instance == TIM11)
     {

+ 4 - 3
bsp/stm32/stm32l475-atk-pandora/applications/arduino_pinout/pins_arduino.h

@@ -51,8 +51,9 @@
  * i2c3 - ICM20608, AP3216C (On Board)
  * i2c4 - AHT10 (On Board)
  */
-#define RTDUINO_DEFAULT_IIC_BUS_NAME "i2c4"
-#define RTDUINO_DEFAULT_SPI_BUS_NAME "spi2"
-#define RTDUINO_SERIAL2_DEVICE_NAME "uart2"
+#define RTDUINO_DEFAULT_IIC_BUS_NAME            "i2c4"
+#define RTDUINO_DEFAULT_SPI_BUS_NAME            "spi2"
+#define RTDUINO_DEFAULT_HWTIMER_DEVICE_NAME     "timer7"
+#define RTDUINO_SERIAL2_DEVICE_NAME             "uart2"
 
 #endif /* Pins_Arduino_h */

File diff suppressed because it is too large
+ 0 - 0
bsp/stm32/stm32l475-atk-pandora/board/CubeMX_Config/.mxproject


+ 16 - 12
bsp/stm32/stm32l475-atk-pandora/board/CubeMX_Config/STM32L475VE.ioc

@@ -19,13 +19,14 @@ Mcu.IP13=SYS
 Mcu.IP14=TIM1
 Mcu.IP15=TIM2
 Mcu.IP16=TIM4
-Mcu.IP17=TIM15
-Mcu.IP18=TIM16
-Mcu.IP19=TIM17
+Mcu.IP17=TIM7
+Mcu.IP18=TIM15
+Mcu.IP19=TIM16
 Mcu.IP2=IWDG
-Mcu.IP20=USART1
-Mcu.IP21=USART2
-Mcu.IP22=USB_OTG_FS
+Mcu.IP20=TIM17
+Mcu.IP21=USART1
+Mcu.IP22=USART2
+Mcu.IP23=USB_OTG_FS
 Mcu.IP3=LPTIM1
 Mcu.IP4=NVIC
 Mcu.IP5=QUADSPI
@@ -33,7 +34,7 @@ Mcu.IP6=RCC
 Mcu.IP7=RTC
 Mcu.IP8=SAI1
 Mcu.IP9=SDMMC1
-Mcu.IPNb=23
+Mcu.IPNb=24
 Mcu.Name=STM32L475V(C-E-G)Tx
 Mcu.Package=LQFP100
 Mcu.Pin0=PE2
@@ -90,14 +91,15 @@ Mcu.Pin54=VP_SYS_VS_Systick
 Mcu.Pin55=VP_TIM1_VS_ClockSourceINT
 Mcu.Pin56=VP_TIM2_VS_ClockSourceINT
 Mcu.Pin57=VP_TIM4_VS_ClockSourceINT
-Mcu.Pin58=VP_TIM15_VS_ClockSourceINT
-Mcu.Pin59=VP_TIM16_VS_ClockSourceINT
+Mcu.Pin58=VP_TIM7_VS_ClockSourceINT
+Mcu.Pin59=VP_TIM15_VS_ClockSourceINT
 Mcu.Pin6=PC15-OSC32_OUT (PC15)
-Mcu.Pin60=VP_TIM17_VS_ClockSourceINT
+Mcu.Pin60=VP_TIM16_VS_ClockSourceINT
+Mcu.Pin61=VP_TIM17_VS_ClockSourceINT
 Mcu.Pin7=PH0-OSC_IN (PH0)
 Mcu.Pin8=PH1-OSC_OUT (PH1)
 Mcu.Pin9=PC2
-Mcu.PinsNb=61
+Mcu.PinsNb=62
 Mcu.ThirdPartyNb=0
 Mcu.UserConstants=
 Mcu.UserName=STM32L475VETx
@@ -237,7 +239,7 @@ ProjectManager.FreePins=false
 ProjectManager.HalAssertFull=false
 ProjectManager.HeapSize=0x200
 ProjectManager.KeepUserCode=true
-ProjectManager.LastFirmware=true
+ProjectManager.LastFirmware=false
 ProjectManager.LibraryCopy=0
 ProjectManager.MainLocation=Src
 ProjectManager.NoMain=false
@@ -399,4 +401,6 @@ VP_TIM2_VS_ClockSourceINT.Mode=Internal
 VP_TIM2_VS_ClockSourceINT.Signal=TIM2_VS_ClockSourceINT
 VP_TIM4_VS_ClockSourceINT.Mode=Internal
 VP_TIM4_VS_ClockSourceINT.Signal=TIM4_VS_ClockSourceINT
+VP_TIM7_VS_ClockSourceINT.Mode=Enable_Timer
+VP_TIM7_VS_ClockSourceINT.Signal=TIM7_VS_ClockSourceINT
 board=custom

+ 41 - 0
bsp/stm32/stm32l475-atk-pandora/board/CubeMX_Config/Src/main.c

@@ -85,6 +85,7 @@ SPI_HandleTypeDef hspi3;
 TIM_HandleTypeDef htim1;
 TIM_HandleTypeDef htim2;
 TIM_HandleTypeDef htim4;
+TIM_HandleTypeDef htim7;
 TIM_HandleTypeDef htim15;
 TIM_HandleTypeDef htim16;
 TIM_HandleTypeDef htim17;
@@ -123,6 +124,7 @@ static void MX_USB_OTG_FS_PCD_Init(void);
 static void MX_LPTIM1_Init(void);
 static void MX_SDMMC1_SD_Init(void);
 static void MX_DAC1_Init(void);
+static void MX_TIM7_Init(void);
 /* USER CODE BEGIN PFP */
 /* Private function prototypes -----------------------------------------------*/
 
@@ -184,6 +186,7 @@ int main(void)
   MX_LPTIM1_Init();
   MX_SDMMC1_SD_Init();
   MX_DAC1_Init();
+  MX_TIM7_Init();
   /* USER CODE BEGIN 2 */
 
   /* USER CODE END 2 */
@@ -948,6 +951,44 @@ static void MX_TIM4_Init(void)
 
 }
 
+/**
+  * @brief TIM7 Initialization Function
+  * @param None
+  * @retval None
+  */
+static void MX_TIM7_Init(void)
+{
+
+  /* USER CODE BEGIN TIM7_Init 0 */
+
+  /* USER CODE END TIM7_Init 0 */
+
+  TIM_MasterConfigTypeDef sMasterConfig = {0};
+
+  /* USER CODE BEGIN TIM7_Init 1 */
+
+  /* USER CODE END TIM7_Init 1 */
+  htim7.Instance = TIM7;
+  htim7.Init.Prescaler = 0;
+  htim7.Init.CounterMode = TIM_COUNTERMODE_UP;
+  htim7.Init.Period = 65535;
+  htim7.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
+  if (HAL_TIM_Base_Init(&htim7) != HAL_OK)
+  {
+    Error_Handler();
+  }
+  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
+  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
+  if (HAL_TIMEx_MasterConfigSynchronization(&htim7, &sMasterConfig) != HAL_OK)
+  {
+    Error_Handler();
+  }
+  /* USER CODE BEGIN TIM7_Init 2 */
+
+  /* USER CODE END TIM7_Init 2 */
+
+}
+
 /**
   * @brief TIM15 Initialization Function
   * @param None

+ 22 - 0
bsp/stm32/stm32l475-atk-pandora/board/CubeMX_Config/Src/stm32l4xx_hal_msp.c

@@ -688,6 +688,17 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
 
   /* USER CODE END TIM4_MspInit 1 */
   }
+  else if(htim_base->Instance==TIM7)
+  {
+  /* USER CODE BEGIN TIM7_MspInit 0 */
+
+  /* USER CODE END TIM7_MspInit 0 */
+    /* Peripheral clock enable */
+    __HAL_RCC_TIM7_CLK_ENABLE();
+  /* USER CODE BEGIN TIM7_MspInit 1 */
+
+  /* USER CODE END TIM7_MspInit 1 */
+  }
   else if(htim_base->Instance==TIM15)
   {
   /* USER CODE BEGIN TIM15_MspInit 0 */
@@ -844,6 +855,17 @@ void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
 
   /* USER CODE END TIM4_MspDeInit 1 */
   }
+  else if(htim_base->Instance==TIM7)
+  {
+  /* USER CODE BEGIN TIM7_MspDeInit 0 */
+
+  /* USER CODE END TIM7_MspDeInit 0 */
+    /* Peripheral clock disable */
+    __HAL_RCC_TIM7_CLK_DISABLE();
+  /* USER CODE BEGIN TIM7_MspDeInit 1 */
+
+  /* USER CODE END TIM7_MspDeInit 1 */
+  }
   else if(htim_base->Instance==TIM15)
   {
   /* USER CODE BEGIN TIM15_MspDeInit 0 */

+ 6 - 0
bsp/stm32/stm32l475-atk-pandora/board/Kconfig

@@ -44,6 +44,8 @@ menu "Onboard Peripheral Drivers"
         select BSP_USING_DAC1
         select BSP_USING_SPI
         select BSP_USING_SPI2
+        select BSP_USING_TIM
+        select BSP_USING_TIM7
         imply BSP_SPI2_TX_USING_DMA
         imply BSP_SPI2_RX_USING_DMA
         imply RTDUINO_USING_SERVO
@@ -386,6 +388,10 @@ menu "On-chip Peripheral Drivers"
         default n
         select RT_USING_HWTIMER
         if BSP_USING_TIM
+            config BSP_USING_TIM7
+                bool "Enable TIM7"
+                default n
+
             config BSP_USING_TIM15
                 bool "Enable TIM15"
                 default n

+ 0 - 97
components/drivers/hwtimer/README_CN.md

@@ -1,97 +0,0 @@
-# 定时器设备
-
-## 功能
-
-* 时间测量
-* 周期或单次执行回调函数
-  
-## 编译
-
-1. 在rtconfig.h添加 `#define RT_USING_HWTIMER`
-
-## 使用流程
-
-1. 以读写方式打开设备
-2. 设置超时回调函数(如果需要)
-3. 根据需要设置定时模式(单次/周期)
-4. 设置计数频率(可选)
-5. 写入超时值,定时器随即启动
-6. 停止定时器(可选)
-7. 关闭设备(如果需要)
-
-应用参考 [hwtimer_test] (/examples/test/hwtimer\_test.c)
-
-## 驱动编写指南
-
-### 操作接口
-
-``` 
-struct rt_hwtimer_ops
-{
-    void (*init)(struct rt_hwtimer_device *timer, rt_uint32_t state);
-    rt_err_t (*start)(struct rt_hwtimer_device *timer, rt_uint32_t cnt, rt_hwtimer_mode_t mode);
-    void (*stop)(struct rt_hwtimer_device *timer);
-    rt_uint32_t (*count_get)(struct rt_hwtimer_device *timer);
-    rt_err_t (*control)(struct rt_hwtimer_device *timer, rt_uint32_t cmd, void *args);
-};
-```
-
-* init - state <1 打开设备 0 关闭设备> 
-* start - cnt <超时值> - mode <单次/周期>
-* stop - <停止计数>
-* count_get - <读取计数器值>
-* control - <设置计数频率 >
-
-### 定时器特征信息
-
-```
-struct rt_hwtimer_info
-{
-    rt_int32_t maxfreq;   
-    rt_int32_t minfreq;    
-    rt_uint32_t maxcnt;     
-    rt_uint8_t  cntmode;  
- };
-```
-
-* maxfreq <设备支持的最大计数频率>
-* minfreq <设备支持的最小计数频率>
-* maxcnt  <计数器最大计数值>
-* cntmode <递增计数/递减计数>
-  
-### 注册设备
-
-```
-static rt_hwtimer_t _timer0;
-int stm32_hwtimer_init(void)
-{
-    _timer0.info = &_info;
-    _timer0.ops  = &_ops;
-
-    rt_device_hwtimer_register(&_timer0, "timer0", TIM2);
-
-    return 0;
-}
-```
-
-### 定时器中断
-
-```
-void timer_irq_handler(void)
-{
-    //其它操作
-    
-    rt_device_hwtimer_isr(&_timer0);
-}
-```
-
-## 注意事项
-
-**可能出现定时误差**
-
-误差原因:
-
-假设计数器最大值0xFFFF,计数频率1Mhz,定时时间1秒又1微秒。
-
-由于定时器一次最多只能计时到65535us,对于1000001us的定时要求。
-可以50000us定时20次完成,此时将会出现计算误差1us。

+ 6 - 2
components/drivers/hwtimer/hwtimer.c

@@ -8,10 +8,13 @@
  * 2015-08-31     heyuanjie87    first version
  */
 
-#include <rtthread.h>
 #include <rtdevice.h>
 #include <rthw.h>
 
+#define DBG_TAG "hwtimer"
+#define DBG_LVL DBG_INFO
+#include <rtdbg.h>
+
 rt_inline rt_uint32_t timeout_calc(rt_hwtimer_t *timer, rt_hwtimerval_t *tv)
 {
     float overflow;
@@ -234,7 +237,8 @@ static rt_err_t rt_hwtimer_control(struct rt_device *dev, int cmd, void *args)
         f = (rt_uint32_t*)args;
         if ((*f > timer->info->maxfreq) || (*f < timer->info->minfreq))
         {
-            result = -RT_ERROR;
+            LOG_W("frequency setting out of range! It will maintain at %d Hz", timer->freq);
+            result = -RT_EINVAL;
             break;
         }
 

+ 0 - 1
components/drivers/misc/dac.c

@@ -8,7 +8,6 @@
  * 2020-06-19     thread-liu   the first version
  */
 
-#include <rtthread.h>
 #include <rtdevice.h>
 
 #include <string.h>

Some files were not shown because too many files changed in this diff