你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

基于stm32f411上的定时器使用笔记

[复制链接]
攻城狮Melo 发布时间:2023-3-14 12:59
创建基于F411的芯片空工程。
启用hwtimer:

20200323081218733.png

拷贝以下三个文件到drivers目录,路径分别为
C:\RT-ThreadStudio\download\rt-thread-sdk\rt-thread-src\v4.0.2\bsp\stm32\libraries\HAL_Drivers\config\f4
和 C:\RT-ThreadStudio\download\rt-thread-sdk\rt-thread-src\v4.0.2\bsp\stm32\libraries\HAL_Drivers 。

20200323081634944.png

注释掉drv_hwtimer.c中的#include "drv_config.h"。
取消注释stm32f4xx_hal_conf.h中的#define HAL_TIM_MODULE_ENABLED。
向tim_config.h添加#include <board.h>。
向drv_hwtimer.c添加#include <rtdevice.h>。
向drv_hwtimer.c添加#include <tim_config.h>。

在board.h中定义需要使用的定时器:

20200323082359971.png

根据要使用的定时器,修改tim_config.h:
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date           Author       Notes
  8. * 2018-12-11     zylx         first version
  9. */

  10. #ifndef __TIM_CONFIG_H__
  11. #define __TIM_CONFIG_H__

  12. #include <rtthread.h>
  13. #include <board.h>
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif

  17. #ifndef TIM_DEV_INFO_CONFIG
  18. #define TIM_DEV_INFO_CONFIG                     \
  19.     {                                           \
  20.         .maxfreq = 1000000,                     \
  21.         .minfreq = 3000,                        \
  22.         .maxcnt  = 0xFFFF,                      \
  23.         .cntmode = HWTIMER_CNTMODE_UP,          \
  24.     }
  25. #endif /* TIM_DEV_INFO_CONFIG */


  26. #ifdef BSP_USING_TIM1
  27. #ifndef TIM1_CONFIG
  28. #define TIM1_CONFIG                                        \
  29.     {                                                       \
  30.        .tim_handle.Instance     = TIM1,                    \
  31.        .tim_irqn                = TIM1_UP_TIM10_IRQn,  \
  32.        .name                    = "timer1",                \
  33.     }
  34. #endif /* TIM11_CONFIG */
  35. #endif /* BSP_USING_TIM11 */



  36. #ifdef BSP_USING_TIM2
  37. #ifndef TIM2_CONFIG
  38. #define TIM2_CONFIG                                        \
  39.     {                                                       \
  40.        .tim_handle.Instance     = TIM2,                    \
  41.        .tim_irqn                = TIM2_IRQn,  \
  42.        .name                    = "timer2",                \
  43.     }
  44. #endif /* TIM11_CONFIG */
  45. #endif /* BSP_USING_TIM11 */



  46. #ifdef BSP_USING_TIM3
  47. #ifndef TIM3_CONFIG
  48. #define TIM3_CONFIG                                        \
  49.     {                                                       \
  50.        .tim_handle.Instance     = TIM3,                    \
  51.        .tim_irqn                = TIM3_IRQn,  \
  52.        .name                    = "timer3",                \
  53.     }
  54. #endif /* TIM11_CONFIG */
  55. #endif /* BSP_USING_TIM11 */




  56. #ifdef BSP_USING_TIM4
  57. #ifndef TIM4_CONFIG
  58. #define TIM4_CONFIG                                        \
  59.     {                                                       \
  60.        .tim_handle.Instance     = TIM4,                    \
  61.        .tim_irqn                = TIM4_IRQn,  \
  62.        .name                    = "timer4",                \
  63.     }
  64. #endif /* TIM11_CONFIG */
  65. #endif /* BSP_USING_TIM11 */

  66. #ifdef BSP_USING_TIM5
  67. #ifndef TIM5_CONFIG
  68. #define TIM5_CONFIG                                        \
  69.     {                                                       \
  70.        .tim_handle.Instance     = TIM5,                    \
  71.        .tim_irqn                = TIM5_IRQn,  \
  72.        .name                    = "timer5",                \
  73.     }
  74. #endif /* TIM11_CONFIG */
  75. #endif /* BSP_USING_TIM11 */

  76. #ifdef BSP_USING_TIM9
  77. #ifndef TIM9_CONFIG
  78. #define TIM9_CONFIG                                        \
  79.     {                                                       \
  80.        .tim_handle.Instance     = TIM9,                    \
  81.        .tim_irqn                = TIM1_BRK_TIM9_IRQn,  \
  82.        .name                    = "timer9",                \
  83.     }
  84. #endif /* TIM11_CONFIG */
  85. #endif /* BSP_USING_TIM11 */

  86. #ifdef BSP_USING_TIM10
  87. #ifndef TIM10_CONFIG
  88. #define TIM10_CONFIG                                        \
  89.     {                                                       \
  90.        .tim_handle.Instance     = TIM10,                    \
  91.        .tim_irqn                = TIM1_UP_TIM10_IRQn,  \
  92.        .name                    = "timer10",                \
  93.     }
  94. #endif /* TIM11_CONFIG */
  95. #endif /* BSP_USING_TIM11 */

  96. #ifdef BSP_USING_TIM11
  97. #ifndef TIM11_CONFIG
  98. #define TIM11_CONFIG                                        \
  99.     {                                                       \
  100.        .tim_handle.Instance     = TIM11,                    \
  101.        .tim_irqn                = TIM1_TRG_COM_TIM11_IRQn,  \
  102.        .name                    = "timer11",                \
  103.     }
  104. #endif /* TIM11_CONFIG */
  105. #endif /* BSP_USING_TIM11 */



  106. #ifdef __cplusplus
  107. }
  108. #endif

  109. #endif /* __TIM_CONFIG_H__ */
复制代码

在drv_hwtimer.h中增加CubeMX生成的底层代码:
  1. void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
  2. {
  3.     if (htim_base->Instance == TIM1)
  4.     {
  5.         __HAL_RCC_TIM1_CLK_ENABLE();
  6.     }
  7.     else if (htim_base->Instance == TIM2)
  8.     {
  9.         __HAL_RCC_TIM2_CLK_ENABLE();
  10.     }
  11.     else if (htim_base->Instance == TIM3)
  12.     {
  13.         __HAL_RCC_TIM3_CLK_ENABLE();
  14.     }
  15.     else if (htim_base->Instance == TIM4)
  16.     {
  17.         __HAL_RCC_TIM4_CLK_ENABLE();
  18.     }
  19.     else if (htim_base->Instance == TIM5)
  20.     {
  21.         __HAL_RCC_TIM5_CLK_ENABLE();
  22.     }
  23.     else if (htim_base->Instance == TIM9)
  24.     {
  25.         __HAL_RCC_TIM9_CLK_ENABLE();
  26.     }
  27.     else if (htim_base->Instance == TIM10)
  28.     {
  29.         __HAL_RCC_TIM10_CLK_ENABLE();
  30.     }
  31.     else if (htim_base->Instance == TIM11)
  32.     {
  33.         __HAL_RCC_TIM11_CLK_ENABLE();
  34.     }
  35. }
复制代码

修改drv_hwtimer.h中HAL_TIM_PeriodElapsedCallback,为使用的定时器添加回调函数:
  1. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  2. {
  3. #ifdef BSP_USING_TIM1
  4. if (htim->Instance == TIM1)
  5. {
  6.     rt_device_hwtimer_isr(&stm32_hwtimer_obj[TIM1_INDEX].time_device);
  7. }
  8. #endif
  9. #ifdef BSP_USING_TIM2
  10. if (htim->Instance == TIM2)
  11. {
  12.     rt_device_hwtimer_isr(&stm32_hwtimer_obj[TIM2_INDEX].time_device);
  13. }
  14. #endif
  15. #ifdef BSP_USING_TIM3
  16. if (htim->Instance == TIM3)
  17. {
  18.     rt_device_hwtimer_isr(&stm32_hwtimer_obj[TIM3_INDEX].time_device);
  19. }
  20. #endif
  21. #ifdef BSP_USING_TIM4
  22. if (htim->Instance == TIM4)
  23. {
  24.     rt_device_hwtimer_isr(&stm32_hwtimer_obj[TIM4_INDEX].time_device);
  25. }
  26. #endif
  27. #ifdef BSP_USING_TIM5
  28. if (htim->Instance == TIM5)
  29. {
  30.     rt_device_hwtimer_isr(&stm32_hwtimer_obj[TIM5_INDEX].time_device);
  31. }
  32. #endif
  33. #ifdef BSP_USING_TIM9
  34. if (htim->Instance == TIM9)
  35. {
  36.     rt_device_hwtimer_isr(&stm32_hwtimer_obj[TIM9_INDEX].time_device);
  37. }
  38. #endif
  39. #ifdef BSP_USING_TIM10
  40. if (htim->Instance == TIM10)
  41. {
  42.     rt_device_hwtimer_isr(&stm32_hwtimer_obj[TIM10_INDEX].time_device);
  43. }
  44. #endif
  45. #ifdef BSP_USING_TIM11
  46. if (htim->Instance == TIM11)
  47. {
  48.     rt_device_hwtimer_isr(&stm32_hwtimer_obj[TIM11_INDEX].time_device);
  49. }
  50. #endif
  51. }
复制代码

添加中断函数入口:
  1. #ifdef BSP_USING_TIM2
  2. void TIM2_IRQHandler(void)
  3. {
  4. /* enter interrupt */
  5. rt_interrupt_enter();
  6. HAL_TIM_IRQHandler(&stm32_hwtimer_obj[TIM2_INDEX].tim_handle);
  7. /* leave interrupt */
  8. rt_interrupt_leave();
  9. }
  10. #endif
  11. #ifdef BSP_USING_TIM3
  12. void TIM3_IRQHandler(void)
  13. {
  14. /* enter interrupt */
  15. rt_interrupt_enter();
  16. HAL_TIM_IRQHandler(&stm32_hwtimer_obj[TIM3_INDEX].tim_handle);
  17. /* leave interrupt */
  18. rt_interrupt_leave();
  19. }
  20. #endif
  21. #ifdef BSP_USING_TIM4
  22. void TIM4_IRQHandler(void)
  23. {
  24. /* enter interrupt */
  25. rt_interrupt_enter();
  26. HAL_TIM_IRQHandler(&stm32_hwtimer_obj[TIM4_INDEX].tim_handle);
  27. /* leave interrupt */
  28. rt_interrupt_leave();
  29. }
  30. #endif
  31. #ifdef BSP_USING_TIM5
  32. void TIM5_IRQHandler(void)
  33. {
  34. /* enter interrupt */
  35. rt_interrupt_enter();
  36. HAL_TIM_IRQHandler(&stm32_hwtimer_obj[TIM5_INDEX].tim_handle);
  37. /* leave interrupt */
  38. rt_interrupt_leave();
  39. }
  40. #endif
  41. #ifdef BSP_USING_TIM11
  42. void TIM1_BRK_TIM9_IRQHandler(void)
  43. {
  44. /* enter interrupt */
  45. rt_interrupt_enter();
  46. HAL_TIM_IRQHandler(&stm32_hwtimer_obj[TIM9_INDEX].tim_handle);
  47. /* leave interrupt */
  48. rt_interrupt_leave();
  49. }
  50. #endif

  51. #if defined( BSP_USING_TIM1) ||defined(BSP_USING_TIM10)
  52. void TIM1_UP_TIM10_IRQHandler(void)
  53. {
  54. /* enter interrupt */
  55. rt_interrupt_enter();
  56. #ifdef BSP_USING_TIM1
  57. if (__HAL_TIM_GET_FLAG(&stm32_hwtimer_obj[TIM1_INDEX].tim_handle,TIM_IT_UPDATE) != RESET)
  58.     HAL_TIM_IRQHandler(&stm32_hwtimer_obj[TIM1_INDEX].tim_handle);
  59. #endif
  60. #ifdef BSP_USING_TIM10
  61. if (__HAL_TIM_GET_FLAG(&stm32_hwtimer_obj[TIM10_INDEX].tim_handle,TIM_IT_UPDATE) != RESET)
  62.     HAL_TIM_IRQHandler(&stm32_hwtimer_obj[TIM10_INDEX].tim_handle);
  63. #endif

  64. /* leave interrupt */
  65. rt_interrupt_leave();
  66. }
  67. #endif

  68. #ifdef BSP_USING_TIM11
  69. void TIM1_TRG_COM_TIM11_IRQHandler(void)
  70. {
  71. /* enter interrupt */
  72. rt_interrupt_enter();
  73. HAL_TIM_IRQHandler(&stm32_hwtimer_obj[TIM11_INDEX].tim_handle);
  74. /* leave interrupt */
  75. rt_interrupt_leave();
  76. }
  77. #endif
复制代码

新建一个usertimer.c,添加示例代码,通过msh调用即可看到效果:
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date           Author       Notes
  8. * 2020-03-23     ShineRoyal       the first version
  9. */
  10. #include <rtthread.h>
  11. #include <board.h>
  12. #include <rtdevice.h>

  13. #define DBG_TAG "user"
  14. #define DBG_LVL DBG_LOG
  15. #include <rtdbg.h>

  16. #define LED0_PIN    GET_PIN(C, 0)
  17. #define LED1_PIN    GET_PIN(C, 1)
  18. #define LED2_PIN    GET_PIN(C, 2)
  19. #define LED3_PIN    GET_PIN(C, 3)
  20. #define LED4_PIN    GET_PIN(C, 4)
  21. #define LED5_PIN    GET_PIN(C, 5)
  22. #define LED6_PIN    GET_PIN(C, 6)
  23. #define LED7_PIN    GET_PIN(C, 7)


  24. static int rt_hw_pin_init()
  25. {
  26.     rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);
  27.     rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT);
  28.     rt_pin_mode(LED2_PIN, PIN_MODE_OUTPUT);
  29.     rt_pin_mode(LED3_PIN, PIN_MODE_OUTPUT);
  30.     rt_pin_mode(LED4_PIN, PIN_MODE_OUTPUT);
  31.     rt_pin_mode(LED5_PIN, PIN_MODE_OUTPUT);
  32.     rt_pin_mode(LED6_PIN, PIN_MODE_OUTPUT);
  33.     rt_pin_mode(LED7_PIN, PIN_MODE_OUTPUT);
  34.     return 0;
  35. }
  36. INIT_BOARD_EXPORT(rt_hw_pin_init);

  37. /* 定时器超时回调函数 */
  38. static rt_err_t timeout_cb1(rt_device_t dev, rt_size_t size)
  39. {
  40.     static int i = 0;
  41.     i++;
  42.     if (i % 2)
  43.         rt_pin_write(LED0_PIN, PIN_LOW);
  44.     else
  45.         rt_pin_write(LED0_PIN, PIN_HIGH);
  46.     LOG_D("%d",i);
  47.     return 0;
  48. }

  49. /* 定时器超时回调函数 */
  50. static rt_err_t timeout_cb2(rt_device_t dev, rt_size_t size)
  51. {
  52.     static int i = 0;
  53.     i++;
  54.     if (i % 2)
  55.         rt_pin_write(LED1_PIN, PIN_LOW);
  56.     else
  57.         rt_pin_write(LED1_PIN, PIN_HIGH);
  58.     return 0;
  59. }

  60. /* 定时器超时回调函数 */
  61. static rt_err_t timeout_cb3(rt_device_t dev, rt_size_t size)
  62. {
  63.     static int i = 0;
  64.     i++;
  65.     if (i % 2)
  66.         rt_pin_write(LED2_PIN, PIN_LOW);
  67.     else
  68.         rt_pin_write(LED2_PIN, PIN_HIGH);
  69.     return 0;
  70. }

  71. /* 定时器超时回调函数 */
  72. static rt_err_t timeout_cb4(rt_device_t dev, rt_size_t size)
  73. {
  74.     static int i = 0;
  75.     i++;
  76.     if (i % 2)
  77.         rt_pin_write(LED3_PIN, PIN_LOW);
  78.     else
  79.         rt_pin_write(LED3_PIN, PIN_HIGH);
  80.     return 0;
  81. }

  82. /* 定时器超时回调函数 */
  83. static rt_err_t timeout_cb5(rt_device_t dev, rt_size_t size)
  84. {
  85.     static int i = 0;
  86.     i++;
  87.     if (i % 2)
  88.         rt_pin_write(LED4_PIN, PIN_LOW);
  89.     else
  90.         rt_pin_write(LED4_PIN, PIN_HIGH);
  91.     return 0;
  92. }


  93. /* 定时器超时回调函数 */
  94. static rt_err_t timeout_cb9(rt_device_t dev, rt_size_t size)
  95. {
  96.     static int i = 0;
  97.     i++;
  98.     if (i % 2)
  99.         rt_pin_write(LED5_PIN, PIN_LOW);
  100.     else
  101.         rt_pin_write(LED5_PIN, PIN_HIGH);
  102.     return 0;
  103. }

  104. /* 定时器超时回调函数 */
  105. static rt_err_t timeout_cb10(rt_device_t dev, rt_size_t size)
  106. {
  107.     static int i = 0;
  108.     i++;
  109.     if (i % 2)
  110.         rt_pin_write(LED6_PIN, PIN_LOW);
  111.     else
  112.         rt_pin_write(LED6_PIN, PIN_HIGH);
  113.     return 0;
  114. }

  115. /* 定时器超时回调函数 */
  116. static rt_err_t timeout_cb11(rt_device_t dev, rt_size_t size)
  117. {
  118.     static int i = 0;
  119.     i++;
  120.     if (i % 2)
  121.         rt_pin_write(LED7_PIN, PIN_LOW);
  122.     else
  123.         rt_pin_write(LED7_PIN, PIN_HIGH);
  124.     return 0;
  125. }


  126. static int hw1timer_sample(int argc, char *argv[])
  127. {
  128.     rt_err_t ret = RT_EOK;
  129.     rt_hwtimerval_t timeout_s;      /* 定时器超时值 */
  130.     rt_device_t hw_dev = RT_NULL;   /* 定时器设备句柄 */
  131.     rt_hwtimer_mode_t mode;         /* 定时器模式 */

  132.     /* 查找定时器设备 */
  133.     hw_dev = rt_device_find("timer1");
  134.     /* 以读写方式打开设备 */
  135.     ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
  136.     /* 设置超时回调函数 */
  137.     rt_device_set_rx_indicate(hw_dev, timeout_cb1);
  138.     /* 设置模式为周期性定时器 */
  139.     mode = HWTIMER_MODE_PERIOD;
  140.     ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
  141.     /* 设置定时器超时值为5s并启动定时器 */
  142.     timeout_s.sec = 1;      /* 秒 */
  143.     timeout_s.usec = 0;     /* 微秒 */
  144.     if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
  145.     {
  146.         rt_kprintf("set timeout value failed\n");
  147.         return RT_ERROR;
  148.     }
  149.     return ret;
  150. }
  151. MSH_CMD_EXPORT(hw1timer_sample, hw1timer_sample);

  152. static int hw2timer_sample(int argc, char *argv[])
  153. {
  154.     rt_err_t ret = RT_EOK;
  155.     rt_hwtimerval_t timeout_s;      /* 定时器超时值 */
  156.     rt_device_t hw_dev = RT_NULL;   /* 定时器设备句柄 */
  157.     rt_hwtimer_mode_t mode;         /* 定时器模式 */

  158.     /* 查找定时器设备 */
  159.     hw_dev = rt_device_find("timer2");
  160.     /* 以读写方式打开设备 */
  161.     ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
  162.     /* 设置超时回调函数 */
  163.     rt_device_set_rx_indicate(hw_dev, timeout_cb2);
  164.     /* 设置模式为周期性定时器 */
  165.     mode = HWTIMER_MODE_PERIOD;
  166.     ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
  167.     /* 设置定时器超时值为5s并启动定时器 */
  168.     timeout_s.sec = 1;      /* 秒 */
  169.     timeout_s.usec = 0;     /* 微秒 */
  170.     if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
  171.     {
  172.         rt_kprintf("set timeout value failed\n");
  173.         return RT_ERROR;
  174.     }
  175.     return ret;
  176. }
  177. MSH_CMD_EXPORT(hw2timer_sample, hw2timer_sample);

  178. static int hw3timer_sample(int argc, char *argv[])
  179. {
  180.     rt_err_t ret = RT_EOK;
  181.     rt_hwtimerval_t timeout_s;      /* 定时器超时值 */
  182.     rt_device_t hw_dev = RT_NULL;   /* 定时器设备句柄 */
  183.     rt_hwtimer_mode_t mode;         /* 定时器模式 */

  184.     /* 查找定时器设备 */
  185.     hw_dev = rt_device_find("timer3");
  186.     /* 以读写方式打开设备 */
  187.     ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
  188.     /* 设置超时回调函数 */
  189.     rt_device_set_rx_indicate(hw_dev, timeout_cb3);
  190.     /* 设置模式为周期性定时器 */
  191.     mode = HWTIMER_MODE_PERIOD;
  192.     ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
  193.     /* 设置定时器超时值为5s并启动定时器 */
  194.     timeout_s.sec = 1;      /* 秒 */
  195.     timeout_s.usec = 0;     /* 微秒 */
  196.     if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
  197.     {
  198.         rt_kprintf("set timeout value failed\n");
  199.         return RT_ERROR;
  200.     }
  201.     return ret;
  202. }
  203. MSH_CMD_EXPORT(hw3timer_sample, hw3timer_sample);

  204. static int hw4timer_sample(int argc, char *argv[])
  205. {
  206.     rt_err_t ret = RT_EOK;
  207.     rt_hwtimerval_t timeout_s;      /* 定时器超时值 */
  208.     rt_device_t hw_dev = RT_NULL;   /* 定时器设备句柄 */
  209.     rt_hwtimer_mode_t mode;         /* 定时器模式 */

  210.     /* 查找定时器设备 */
  211.     hw_dev = rt_device_find("timer4");
  212.     /* 以读写方式打开设备 */
  213.     ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
  214.     /* 设置超时回调函数 */
  215.     rt_device_set_rx_indicate(hw_dev, timeout_cb4);
  216.     /* 设置模式为周期性定时器 */
  217.     mode = HWTIMER_MODE_PERIOD;
  218.     ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
  219.     /* 设置定时器超时值为5s并启动定时器 */
  220.     timeout_s.sec = 1;      /* 秒 */
  221.     timeout_s.usec = 0;     /* 微秒 */
  222.     if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
  223.     {
  224.         rt_kprintf("set timeout value failed\n");
  225.         return RT_ERROR;
  226.     }
  227.     return ret;
  228. }
  229. MSH_CMD_EXPORT(hw4timer_sample, hw4timer_sample);

  230. static int hw5timer_sample(int argc, char *argv[])
  231. {
  232.     rt_err_t ret = RT_EOK;
  233.     rt_hwtimerval_t timeout_s;      /* 定时器超时值 */
  234.     rt_device_t hw_dev = RT_NULL;   /* 定时器设备句柄 */
  235.     rt_hwtimer_mode_t mode;         /* 定时器模式 */

  236.     /* 查找定时器设备 */
  237.     hw_dev = rt_device_find("timer5");
  238.     /* 以读写方式打开设备 */
  239.     ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
  240.     /* 设置超时回调函数 */
  241.     rt_device_set_rx_indicate(hw_dev, timeout_cb5);
  242.     /* 设置模式为周期性定时器 */
  243.     mode = HWTIMER_MODE_PERIOD;
  244.     ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
  245.     /* 设置定时器超时值为5s并启动定时器 */
  246.     timeout_s.sec = 1;      /* 秒 */
  247.     timeout_s.usec = 0;     /* 微秒 */
  248.     if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
  249.     {
  250.         rt_kprintf("set timeout value failed\n");
  251.         return RT_ERROR;
  252.     }
  253.     return ret;
  254. }
  255. MSH_CMD_EXPORT(hw5timer_sample, hw5timer_sample);

  256. static int hw9timer_sample(int argc, char *argv[])
  257. {
  258.     rt_err_t ret = RT_EOK;
  259.     rt_hwtimerval_t timeout_s;      /* 定时器超时值 */
  260.     rt_device_t hw_dev = RT_NULL;   /* 定时器设备句柄 */
  261.     rt_hwtimer_mode_t mode;         /* 定时器模式 */

  262.     /* 查找定时器设备 */
  263.     hw_dev = rt_device_find("timer9");
  264.     /* 以读写方式打开设备 */
  265.     ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
  266.     /* 设置超时回调函数 */
  267.     rt_device_set_rx_indicate(hw_dev, timeout_cb9);
  268.     /* 设置模式为周期性定时器 */
  269.     mode = HWTIMER_MODE_PERIOD;
  270.     ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
  271.     /* 设置定时器超时值为5s并启动定时器 */
  272.     timeout_s.sec = 1;      /* 秒 */
  273.     timeout_s.usec = 0;     /* 微秒 */
  274.     if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
  275.     {
  276.         rt_kprintf("set timeout value failed\n");
  277.         return RT_ERROR;
  278.     }
  279.     return ret;
  280. }
  281. MSH_CMD_EXPORT(hw9timer_sample, hw9timer_sample);

  282. static int hw10timer_sample(int argc, char *argv[])
  283. {
  284.     rt_err_t ret = RT_EOK;
  285.     rt_hwtimerval_t timeout_s;      /* 定时器超时值 */
  286.     rt_device_t hw_dev = RT_NULL;   /* 定时器设备句柄 */
  287.     rt_hwtimer_mode_t mode;         /* 定时器模式 */

  288.     /* 查找定时器设备 */
  289.     hw_dev = rt_device_find("timer10");
  290.     /* 以读写方式打开设备 */
  291.     ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
  292.     /* 设置超时回调函数 */
  293.     rt_device_set_rx_indicate(hw_dev, timeout_cb10);
  294.     /* 设置模式为周期性定时器 */
  295.     mode = HWTIMER_MODE_PERIOD;
  296.     ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
  297.     /* 设置定时器超时值为5s并启动定时器 */
  298.     timeout_s.sec = 1;      /* 秒 */
  299.     timeout_s.usec = 0;     /* 微秒 */
  300.     if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
  301.     {
  302.         rt_kprintf("set timeout value failed\n");
  303.         return RT_ERROR;
  304.     }
  305.     return ret;
  306. }
  307. MSH_CMD_EXPORT(hw10timer_sample, hw10timer_sample);

  308. static int hw11timer_sample(int argc, char *argv[])
  309. {
  310.     rt_err_t ret = RT_EOK;
  311.     rt_hwtimerval_t timeout_s;      /* 定时器超时值 */
  312.     rt_device_t hw_dev = RT_NULL;   /* 定时器设备句柄 */
  313.     rt_hwtimer_mode_t mode;         /* 定时器模式 */

  314.     /* 查找定时器设备 */
  315.     hw_dev = rt_device_find("timer11");
  316.     /* 以读写方式打开设备 */
  317.     ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
  318.     /* 设置超时回调函数 */
  319.     rt_device_set_rx_indicate(hw_dev, timeout_cb11);
  320.     /* 设置模式为周期性定时器 */
  321.     mode = HWTIMER_MODE_PERIOD;
  322.     ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
  323.     /* 设置定时器超时值为5s并启动定时器 */
  324.     timeout_s.sec = 1;      /* 秒 */
  325.     timeout_s.usec = 0;     /* 微秒 */
  326.     if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
  327.     {
  328.         rt_kprintf("set timeout value failed\n");
  329.         return RT_ERROR;
  330.     }
  331.     return ret;
  332. }
  333. MSH_CMD_EXPORT(hw11timer_sample, hw11timer_sample);

复制代码

注意,定时器1的定时会快一倍(这里是1s的间隔,但定时器1会500ms进一次中断),尚不清楚是rt-thread的库造成的还是硬件本身时钟相关的原因,后续在研究。
————————————————
版权声明:小盼你最萌哒
如有侵权请联系删除


收藏 评论0 发布时间:2023-3-14 12:59

举报

0个回答
关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版