My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Timer.h 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #pragma once
  23. #include <stdint.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <signal.h>
  27. #include <time.h>
  28. #include <stdio.h>
  29. #include "Clock.h"
  30. class Timer {
  31. public:
  32. Timer();
  33. virtual ~Timer();
  34. typedef void (callback_fn)();
  35. void init(uint32_t sig_id, uint32_t sim_freq, callback_fn* fn);
  36. void start(uint32_t frequency);
  37. void enable();
  38. bool enabled() {return active;}
  39. void disable();
  40. void setCompare(uint32_t compare);
  41. uint32_t getCount();
  42. uint32_t getCompare() {return compare;}
  43. uint32_t getOverruns() {return overruns;}
  44. uint32_t getAvgError() {return avg_error;}
  45. intptr_t getID() {
  46. return (*(intptr_t*)timerid);
  47. }
  48. static void handler(int sig, siginfo_t *si, void *uc){
  49. Timer* _this = (Timer*)si->si_value.sival_ptr;
  50. _this->avg_error += (Clock::nanos() - _this->start_time) - _this->period; //high_resolution_clock is also limited in precision, but best we have
  51. _this->avg_error /= 2; //very crude precision analysis (actually within +-500ns usually)
  52. _this->start_time = Clock::nanos(); // wrap
  53. _this->cbfn();
  54. _this->overruns += timer_getoverrun(_this->timerid); // even at 50Khz this doesn't stay zero, again demonstrating the limitations
  55. // using a realtime linux kernel would help somewhat
  56. }
  57. private:
  58. bool active;
  59. uint32_t compare;
  60. uint32_t frequency;
  61. uint32_t overruns;
  62. timer_t timerid;
  63. sigset_t mask;
  64. callback_fn* cbfn;
  65. uint64_t period;
  66. uint64_t avg_error;
  67. uint64_t start_time;
  68. };