timer.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /******************************************************************************
  2. * @attention
  3. *
  4. * <h2><center>&copy; COPYRIGHT 2016 STMicroelectronics</center></h2>
  5. *
  6. * Licensed under ST MYLIBERTY SOFTWARE LICENSE AGREEMENT (the "License");
  7. * You may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at:
  9. *
  10. * http://www.st.com/myliberty
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
  15. * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. ******************************************************************************/
  21. /*
  22. * PROJECT: ST25R391x firmware
  23. * $Revision: $
  24. * LANGUAGE: ANSI C
  25. */
  26. /*! \file timer.c
  27. *
  28. * \brief SW Timer implementation
  29. *
  30. * \author Gustavo Patricio
  31. *
  32. * This module makes use of a System Tick in millisconds and provides
  33. * an abstraction for SW timers
  34. *
  35. */
  36. /*
  37. ******************************************************************************
  38. * INCLUDES
  39. ******************************************************************************
  40. */
  41. #include "timer.h"
  42. /*
  43. ******************************************************************************
  44. * LOCAL DEFINES
  45. ******************************************************************************
  46. */
  47. /*
  48. ******************************************************************************
  49. * LOCAL VARIABLES
  50. ******************************************************************************
  51. */
  52. static uint32_t timerStopwatchTick;
  53. /*
  54. ******************************************************************************
  55. * GLOBAL FUNCTIONS
  56. ******************************************************************************
  57. */
  58. /*******************************************************************************/
  59. uint32_t timerCalculateTimer(uint16_t time) {
  60. return (HAL_GetTick() + time);
  61. }
  62. /*******************************************************************************/
  63. bool timerIsExpired(uint32_t timer) {
  64. uint32_t uDiff;
  65. int32_t sDiff;
  66. uDiff = (timer - HAL_GetTick()); /* Calculate the diff between the timers */
  67. sDiff = uDiff; /* Convert the diff to a signed var */
  68. /* Check if the given timer has expired already */
  69. if(sDiff < 0) {
  70. return true;
  71. }
  72. return false;
  73. }
  74. /*******************************************************************************/
  75. void timerDelay(uint16_t tOut) {
  76. uint32_t t;
  77. /* Calculate the timer and wait blocking until is running */
  78. t = timerCalculateTimer(tOut);
  79. while(timerIsRunning(t))
  80. ;
  81. }
  82. /*******************************************************************************/
  83. void timerStopwatchStart(void) {
  84. timerStopwatchTick = HAL_GetTick();
  85. }
  86. /*******************************************************************************/
  87. uint32_t timerStopwatchMeasure(void) {
  88. return (uint32_t)(HAL_GetTick() - timerStopwatchTick);
  89. }