porttimer.c 2.6 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 "FreeRTOS.h"
  27. #include "mbport.h"
  28. #include "timers.h"
  29. #include "tim.h"
  30. // 使用软定时器
  31. // #define USE_SOFT_TIMER
  32. /* ----------------------- static functions ---------------------------------*/
  33. static TimerHandle_t xTimerId;
  34. static uint32_t ulTimeout;
  35. void prvvTIMERExpiredISR(eMBRTUSlaveObj * pObj);
  36. /* ----------------------- Start implementation -----------------------------*/
  37. BOOL xMBPortTimersInit(USHORT usTim1Timerout50us, TIM_HandleTypeDef def) {
  38. #ifdef USE_SOFT_TIMER
  39. ulTimeout = 50 * usTim1Timerout50us * osKernelGetTickFreq() / 1000000 + 1;
  40. xTimerId = osTimerNew(prvvTIMERExpiredISR, osTimerOnce, NULL, NULL);
  41. return xTimerId != NULL;
  42. #else
  43. return TRUE;
  44. #endif
  45. }
  46. void vMBPortTimersEnable(eMBRTUSlaveObj * pObj) {
  47. #ifdef USE_SOFT_TIMER
  48. osTimerStart(xTimerId, ulTimeout);
  49. #else
  50. __HAL_TIM_CLEAR_IT(&pObj->pTimHandle,TIM_IT_UPDATE);
  51. __HAL_TIM_ENABLE_IT(&pObj->pTimHandle,TIM_IT_UPDATE);
  52. __HAL_TIM_SET_COUNTER(&pObj->pTimHandle,0);
  53. __HAL_TIM_ENABLE(&pObj->pTimHandle);
  54. #endif
  55. }
  56. void vMBPortTimersDisable(eMBRTUSlaveObj * pObj) {
  57. #ifdef USE_SOFT_TIMER
  58. osTimerStop(xTimerId);
  59. #else
  60. __HAL_TIM_DISABLE(&pObj->pTimHandle);
  61. __HAL_TIM_SET_COUNTER(&pObj->pTimHandle, 0);
  62. __HAL_TIM_DISABLE_IT(&pObj->pTimHandle, TIM_IT_UPDATE);
  63. __HAL_TIM_CLEAR_IT(&pObj->pTimHandle, TIM_IT_UPDATE);
  64. #endif
  65. }
  66. void prvvTIMERExpiredISR(eMBRTUSlaveObj *obj) {
  67. (void) pxMBPortCBTimerExpired(obj);
  68. }