timer.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. {
  61. return (osKernelGetTickCount() + time);
  62. }
  63. /*******************************************************************************/
  64. bool timerIsExpired( uint32_t timer )
  65. {
  66. uint32_t uDiff;
  67. int32_t sDiff;
  68. uDiff = (timer - osKernelGetTickCount()); /* Calculate the diff between the timers */
  69. sDiff = uDiff; /* Convert the diff to a signed var */
  70. /* Check if the given timer has expired already */
  71. if( sDiff < 0 )
  72. {
  73. return true;
  74. }
  75. return false;
  76. }
  77. /*******************************************************************************/
  78. void timerDelay( uint16_t tOut )
  79. {
  80. uint32_t t;
  81. /* Calculate the timer and wait blocking until is running */
  82. t = timerCalculateTimer( tOut );
  83. while( timerIsRunning(t) );
  84. }
  85. /*******************************************************************************/
  86. void timerStopwatchStart( void )
  87. {
  88. timerStopwatchTick = osKernelGetTickCount();
  89. }
  90. /*******************************************************************************/
  91. uint32_t timerStopwatchMeasure( void )
  92. {
  93. return (uint32_t)(osKernelGetTickCount() - timerStopwatchTick);
  94. }