timer.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #include <furi.h>
  43. /*
  44. ******************************************************************************
  45. * LOCAL DEFINES
  46. ******************************************************************************
  47. */
  48. /*
  49. ******************************************************************************
  50. * LOCAL VARIABLES
  51. ******************************************************************************
  52. */
  53. static uint32_t timerStopwatchTick;
  54. /*
  55. ******************************************************************************
  56. * GLOBAL FUNCTIONS
  57. ******************************************************************************
  58. */
  59. /*******************************************************************************/
  60. uint32_t timerCalculateTimer(uint16_t time) {
  61. return (furi_get_tick() + time);
  62. }
  63. /*******************************************************************************/
  64. bool timerIsExpired(uint32_t timer) {
  65. uint32_t uDiff;
  66. int32_t sDiff;
  67. uDiff = (timer - furi_get_tick()); /* Calculate the diff between the timers */
  68. sDiff = uDiff; /* Convert the diff to a signed var */
  69. /* Check if the given timer has expired already */
  70. if(sDiff < 0) {
  71. return true;
  72. }
  73. return false;
  74. }
  75. /*******************************************************************************/
  76. void timerDelay(uint16_t tOut) {
  77. uint32_t t;
  78. /* Calculate the timer and wait blocking until is running */
  79. t = timerCalculateTimer(tOut);
  80. while(timerIsRunning(t))
  81. ;
  82. }
  83. /*******************************************************************************/
  84. void timerStopwatchStart(void) {
  85. timerStopwatchTick = furi_get_tick();
  86. }
  87. /*******************************************************************************/
  88. uint32_t timerStopwatchMeasure(void) {
  89. return (uint32_t)(furi_get_tick() - timerStopwatchTick);
  90. }