You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.9KB

  1. // Copyright 2011 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  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. // -----------------------------------------------------------------------------
  17. //
  18. // Millisecond clock. The RTC is not used here because we need a 32 bits
  19. // timestamp.
  20. #ifndef AVRLIBX_SYSTEM_TIME_H_
  21. #define AVRLIBX_SYSTEM_TIME_H_
  22. #include <avr/delay.h>
  23. #include <avr/io.h>
  24. #include "avrlibx/avrlibx.h"
  25. #include "avrlibx/system/timer.h"
  26. #define ConstantDelay(x) _delay_ms((x))
  27. namespace avrlibx {
  28. uint32_t milliseconds();
  29. void Delay(uint32_t delay);
  30. extern volatile LongWord milliseconds_count;
  31. // Must be called every millisecond.
  32. inline void Tick() {
  33. ++milliseconds_count.words[0];
  34. if (milliseconds_count.words[0] == 0) {
  35. ++milliseconds_count.words[1];
  36. }
  37. }
  38. #ifdef TCF0
  39. #define TICK_ISR ISR(TCF0_OVF_vect) { Tick(); }
  40. typedef Timer<PortF, 0> TickTimer;
  41. #else
  42. #define TICK_ISR ISR(TCE0_OVF_vect) { Tick(); }
  43. typedef Timer<PortE, 0> TickTimer;
  44. #endif // TCF0
  45. inline void SetupTickTimer(uint8_t priority = 1) {
  46. TickTimer timer;
  47. timer.set_prescaler(TIMER_PRESCALER_CLK);
  48. timer.set_period(F_CPU / 1000);
  49. timer.EnableOverflowInterrupt(priority);
  50. timer.set_mode(TIMER_MODE_NORMAL);
  51. }
  52. inline void DelayRTC(uint16_t delay) {
  53. RTC.CNT = 0;
  54. while (RTC.CNT < delay);
  55. }
  56. } // namespace avrlibx
  57. #endif // AVRLIBX_SYSTEM_TIME_H_