porttimer.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * FreeModbus Libary: CMSIS-RTOS2 Port
  3. * Copyright (C) 2013 Armink <armink.ztl@gmail.com>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * File: $Id: porttimer.c,v 1.60 2013/08/13 15:07:05 Armink $
  20. * porttimer.c,v 1.60 2022/07/17 quanghona <lyhonquang@gmail.com> CMSIS-RTOS2 port $
  21. */
  22. /* ----------------------- Platform includes --------------------------------*/
  23. #include "port.h"
  24. #include "cmsis_os2.h"
  25. /* ----------------------- Modbus includes ----------------------------------*/
  26. #include "mb.h"
  27. #include "mbport.h"
  28. #include "FreeRTOS.h"
  29. #include "timers.h"
  30. #include "tim.h"
  31. // 使用软定时器
  32. // #define USE_SOFT_TIMER
  33. /* ----------------------- static functions ---------------------------------*/
  34. static TimerHandle_t xTimerId;
  35. static uint32_t ulTimeout;
  36. void prvvTIMERExpiredISR();
  37. /* ----------------------- Start implementation -----------------------------*/
  38. BOOL xMBPortTimersInit(USHORT usTim1Timerout50us) {
  39. #ifdef USE_SOFT_TIMER
  40. ulTimeout = 50 * usTim1Timerout50us * osKernelGetTickFreq() / 1000000 + 1;
  41. xTimerId = osTimerNew(prvvTIMERExpiredISR, osTimerOnce, NULL, NULL);
  42. return xTimerId != NULL;
  43. #else
  44. return TRUE;
  45. #endif
  46. }
  47. void vMBPortTimersEnable() {
  48. #ifdef USE_SOFT_TIMER
  49. osTimerStart(xTimerId, ulTimeout);
  50. #else
  51. __HAL_TIM_CLEAR_IT(&htim2,TIM_IT_UPDATE);
  52. __HAL_TIM_ENABLE_IT(&htim2,TIM_IT_UPDATE);
  53. __HAL_TIM_SET_COUNTER(&htim2,0);
  54. __HAL_TIM_ENABLE(&htim2);
  55. #endif
  56. }
  57. void vMBPortTimersDisable() {
  58. #ifdef USE_SOFT_TIMER
  59. osTimerStop(xTimerId);
  60. #else
  61. __HAL_TIM_DISABLE(&htim2);
  62. __HAL_TIM_SET_COUNTER(&htim2, 0);
  63. __HAL_TIM_DISABLE_IT(&htim2, TIM_IT_UPDATE);
  64. __HAL_TIM_CLEAR_IT(&htim2, TIM_IT_UPDATE);
  65. #endif
  66. }
  67. void prvvTIMERExpiredISR() {
  68. (void) pxMBPortCBTimerExpired();
  69. }