timer.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* Copyright 2017 Fred Sundvik
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "timer.h"
  17. #include <stdatomic.h>
  18. static atomic_uint_least32_t current_time = 0;
  19. void timer_init(void) {
  20. current_time = 0;
  21. }
  22. void timer_clear(void) {
  23. current_time = 0;
  24. }
  25. uint16_t timer_read(void) {
  26. return current_time & 0xFFFF;
  27. }
  28. uint32_t timer_read32(void) {
  29. return current_time;
  30. }
  31. uint16_t timer_elapsed(uint16_t last) {
  32. return TIMER_DIFF_16(timer_read(), last);
  33. }
  34. uint32_t timer_elapsed32(uint32_t last) {
  35. return TIMER_DIFF_32(timer_read32(), last);
  36. }
  37. void set_time(uint32_t t) {
  38. current_time = t;
  39. }
  40. void advance_time(uint32_t ms) {
  41. current_time += ms;
  42. }
  43. void wait_ms(uint32_t ms) {
  44. advance_time(ms);
  45. }