Prusa-MMU-Private
PrusaMultiMaterialUpgradev3firmwareforMK3SMK4
speed_table.h
Go to the documentation of this file.
1 #pragma once
3 #include "../config/config.h"
4 #include "../hal/progmem.h"
5 #include "../hal/cpu.h"
6 #include "math.h"
7 
8 namespace modules {
9 
11 namespace speed_table {
12 
13 typedef uint16_t st_timer_t;
14 
16 static_assert(F_CPU / config::stepTimerFrequencyDivider == 2000000,
17  "speed tables not compatible for the requested frequency");
18 
20 extern const st_timer_t speed_table_fast[256][2] PROGMEM;
21 
23 extern const st_timer_t speed_table_slow[256][2] PROGMEM;
24 
26 static inline st_timer_t calc_timer(st_timer_t step_rate, uint8_t &step_loops) {
27  if (step_rate > config::maxStepFrequency)
28  step_rate = config::maxStepFrequency;
29  if (step_rate > 20000) { // If steprate > 20kHz >> step 4 times
30  step_rate = (step_rate >> 2) & 0x3fff;
31  step_loops = 4;
32  } else if (step_rate > 10000) { // If steprate > 10kHz >> step 2 times
33  step_rate = (step_rate >> 1) & 0x7fff;
34  step_loops = 2;
35  } else {
36  step_loops = 1;
37  }
38 
40  namespace pm = hal::progmem;
41 
42  st_timer_t timer; // calculated interval
43 
44  if (step_rate < (F_CPU / 500000))
45  step_rate = (F_CPU / 500000);
46  step_rate -= (F_CPU / 500000); // Correct for minimal speed
47  if (step_rate >= (8 * 256)) { // higher step rate
48  const uint16_t *table_address = &speed_table_fast[(uint8_t)(step_rate >> 8)][0];
49  uint8_t tmp_step_rate = (step_rate & 0x00ff);
50  uint16_t gain = pm::read_word(table_address + 1);
51  timer = mulU8X16toH16(tmp_step_rate, gain);
52  timer = pm::read_word(table_address) - timer;
53  } else { // lower step rates
54  const uint16_t *table_address = &speed_table_slow[0][0];
55  table_address += (step_rate >> 2) & 0xfffe;
56  timer = pm::read_word(table_address);
57  timer -= ((pm::read_word(table_address + 1) * (uint8_t)(step_rate & 0x0007)) >> 3);
58  }
59  if (timer < 100) {
60  // 20kHz this should never happen
61  timer = 100;
62  }
63  return timer;
64 }
65 
66 } // namespace speed_table
67 } // namespace modules
#define F_CPU
Main clock frequency.
Definition: cpu.h:11
static constexpr uint16_t maxStepFrequency
Max step frequency 40KHz.
Definition: config.h:62
static constexpr uint8_t stepTimerFrequencyDivider
Step timer frequency divider (F = F_CPU / divider)
Definition: config.h:71
AVR PROGMEM handling.
Definition: progmem.h:14
static uint16_t read_word(const uint16_t *addr)
read a 16bit word from PROGMEM
Definition: progmem.h:17
static uint16_t mulU8X16toH16(const uint8_t charIn1, const uint16_t intIn2)
(intIn1 * intIn2) >> 8
Definition: math.h:11
static st_timer_t calc_timer(st_timer_t step_rate, uint8_t &step_loops)
Calculate the next timer interval and steps according to current step rate.
Definition: speed_table.h:26
const st_timer_t speed_table_fast[256][2] PROGMEM
CPU timer frequency divider required for the speed tables.
Definition: speed_table.cpp:9
The modules namespace contains models of MMU's components.
Definition: command_base.h:8