My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

Timer.cpp 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #ifdef __PLAT_LINUX__
  23. #include "Timer.h"
  24. #include <stdio.h>
  25. Timer::Timer() {
  26. active = false;
  27. compare = 0;
  28. frequency = 0;
  29. overruns = 0;
  30. timerid = 0;
  31. cbfn = nullptr;
  32. period = 0;
  33. start_time = 0;
  34. avg_error = 0;
  35. }
  36. Timer::~Timer() {
  37. timer_delete(timerid);
  38. }
  39. void Timer::init(uint32_t sig_id, uint32_t sim_freq, callback_fn* fn) {
  40. struct sigaction sa;
  41. struct sigevent sev;
  42. frequency = sim_freq;
  43. cbfn = fn;
  44. sa.sa_flags = SA_SIGINFO;
  45. sa.sa_sigaction = Timer::handler;
  46. sigemptyset(&sa.sa_mask);
  47. if (sigaction(SIGRTMIN, &sa, NULL) == -1) {
  48. return; // todo: handle error
  49. }
  50. sigemptyset(&mask);
  51. sigaddset(&mask, SIGRTMIN);
  52. disable();
  53. sev.sigev_notify = SIGEV_SIGNAL;
  54. sev.sigev_signo = SIGRTMIN;
  55. sev.sigev_value.sival_ptr = (void*)this;
  56. if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) {
  57. return; // todo: handle error
  58. }
  59. }
  60. void Timer::start(uint32_t frequency) {
  61. setCompare(this->frequency / frequency);
  62. //printf("timer(%ld) started\n", getID());
  63. }
  64. void Timer::enable() {
  65. if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1) {
  66. return; // todo: handle error
  67. }
  68. active = true;
  69. //printf("timer(%ld) enabled\n", getID());
  70. }
  71. void Timer::disable() {
  72. if (sigprocmask(SIG_SETMASK, &mask, NULL) == -1) {
  73. return; // todo: handle error
  74. }
  75. active = false;
  76. }
  77. void Timer::setCompare(uint32_t compare) {
  78. uint32_t nsec_offset = 0;
  79. if (active) {
  80. nsec_offset = Clock::nanos() - this->start_time; // calculate how long the timer would have been running for
  81. nsec_offset = nsec_offset < 1000 ? nsec_offset : 0; // constrain, this shouldn't be needed but apparently Marlin enables interrupts on the stepper timer before initialising it, todo: investigate ?bug?
  82. }
  83. this->compare = compare;
  84. uint64_t ns = Clock::ticksToNanos(compare, frequency) - nsec_offset;
  85. struct itimerspec its;
  86. its.it_value.tv_sec = ns / 1000000000;
  87. its.it_value.tv_nsec = ns % 1000000000;
  88. its.it_interval.tv_sec = its.it_value.tv_sec;
  89. its.it_interval.tv_nsec = its.it_value.tv_nsec;
  90. if (timer_settime(timerid, 0, &its, NULL) == -1) {
  91. printf("timer(%ld) failed, compare: %d(%ld)\n", getID(), compare, its.it_value.tv_nsec);
  92. return; // todo: handle error
  93. }
  94. //printf("timer(%ld) started, compare: %d(%d)\n", getID(), compare, its.it_value.tv_nsec);
  95. this->period = its.it_value.tv_nsec;
  96. this->start_time = Clock::nanos();
  97. }
  98. uint32_t Timer::getCount() {
  99. return Clock::nanosToTicks(Clock::nanos() - this->start_time, frequency);
  100. }
  101. #endif // __PLAT_LINUX__