speed_lookuptable.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #ifndef SPEED_LOOKUPTABLE_H
  2. #define SPEED_LOOKUPTABLE_H
  3. #include "Marlin.h"
  4. extern const uint16_t speed_lookuptable_fast[256][2] PROGMEM;
  5. extern const uint16_t speed_lookuptable_slow[256][2] PROGMEM;
  6. #ifndef _NO_ASM
  7. // return ((x * y) >> 8) with rounding when shifting right
  8. FORCE_INLINE uint16_t MUL8x16R8(uint8_t x, uint16_t y) {
  9. uint16_t out;
  10. __asm__ (
  11. // %0 out
  12. // %1 x
  13. // %2 y
  14. // uint8_t: %An or %n
  15. // uint16_t: %Bn %An
  16. // __uint24: %Cn %Bn %An
  17. // uint32_t: %Dn %Cn %Bn %An
  18. //
  19. //
  20. // B2 A2 *
  21. // A1
  22. //---------
  23. // B0 A0 RR
  24. "mul %B2, %A1" "\n\t"
  25. "movw %0, r0" "\n\t"
  26. "mul %A2, %A1" "\n\t"
  27. "lsl r0" "\n\t" //push MSB to carry for rounding
  28. "adc %A0, r1" "\n\t" //add with carry (for rounding)
  29. "clr r1" "\n\t" //make r1 __zero_reg__ again
  30. "adc %B0, r1" "\n\t" //propagate carry of addition (add 0 with carry)
  31. : "=&r" (out)
  32. : "r" (x), "r" (y)
  33. : "r0", "r1" //clobbers: Technically these are either scratch registers or always 0 registers, but I'm making sure the compiler knows just in case.
  34. );
  35. return out;
  36. }
  37. // intRes = longIn1 * longIn2 >> 24
  38. // uses:
  39. // r26 to store 0
  40. // r27 to store the byte 1 of the 48bit result
  41. #define MultiU24X24toH16(intRes, longIn1, longIn2) \
  42. asm volatile ( \
  43. "clr r26 \n\t" \
  44. "mul %A1, %B2 \n\t" \
  45. "mov r27, r1 \n\t" \
  46. "mul %B1, %C2 \n\t" \
  47. "movw %A0, r0 \n\t" \
  48. "mul %C1, %C2 \n\t" \
  49. "add %B0, r0 \n\t" \
  50. "mul %C1, %B2 \n\t" \
  51. "add %A0, r0 \n\t" \
  52. "adc %B0, r1 \n\t" \
  53. "mul %A1, %C2 \n\t" \
  54. "add r27, r0 \n\t" \
  55. "adc %A0, r1 \n\t" \
  56. "adc %B0, r26 \n\t" \
  57. "mul %B1, %B2 \n\t" \
  58. "add r27, r0 \n\t" \
  59. "adc %A0, r1 \n\t" \
  60. "adc %B0, r26 \n\t" \
  61. "mul %C1, %A2 \n\t" \
  62. "add r27, r0 \n\t" \
  63. "adc %A0, r1 \n\t" \
  64. "adc %B0, r26 \n\t" \
  65. "mul %B1, %A2 \n\t" \
  66. "add r27, r1 \n\t" \
  67. "adc %A0, r26 \n\t" \
  68. "adc %B0, r26 \n\t" \
  69. "lsr r27 \n\t" \
  70. "adc %A0, r26 \n\t" \
  71. "adc %B0, r26 \n\t" \
  72. "clr r1 \n\t" \
  73. : \
  74. "=&r" (intRes) \
  75. : \
  76. "d" (longIn1), \
  77. "d" (longIn2) \
  78. : \
  79. "r26" , "r27" \
  80. )
  81. #else //_NO_ASM
  82. static inline void MultiU16X8toH16(uint16_t& intRes, uint8_t& charIn1, uint16_t& intIn2)
  83. {
  84. intRes = ((uint32_t)charIn1 * (uint32_t)intIn2) >> 8;
  85. }
  86. static inline void MultiU24X24toH16(uint16_t& intRes, uint32_t& longIn1, uint32_t& longIn2)
  87. {
  88. intRes = ((uint64_t)longIn1 * (uint64_t)longIn2) >> 24;
  89. }
  90. #endif //_NO_ASM
  91. FORCE_INLINE unsigned short calc_timer(uint16_t step_rate, uint8_t& step_loops) {
  92. uint16_t timer;
  93. if(step_rate > MAX_STEP_FREQUENCY) step_rate = MAX_STEP_FREQUENCY;
  94. if(step_rate > 20000) { // If steprate > 20kHz >> step 4 times
  95. step_rate = (step_rate >> 2)&0x3fff;
  96. step_loops = 4;
  97. }
  98. else if(step_rate > 10000) { // If steprate > 10kHz >> step 2 times
  99. step_rate = (step_rate >> 1)&0x7fff;
  100. step_loops = 2;
  101. }
  102. else {
  103. step_loops = 1;
  104. }
  105. if(step_rate < (F_CPU/500000)) step_rate = (F_CPU/500000);
  106. step_rate -= (F_CPU/500000); // Correct for minimal speed
  107. if(step_rate >= (8*256)){ // higher step rate
  108. unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate>>8)][0];
  109. unsigned char tmp_step_rate = (step_rate & 0x00ff);
  110. uint16_t gain = (uint16_t)pgm_read_word_near(table_address+2);
  111. timer = (unsigned short)pgm_read_word_near(table_address) - MUL8x16R8(tmp_step_rate, gain);
  112. }
  113. else { // lower step rates
  114. unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
  115. table_address += ((step_rate)>>1) & 0xfffc;
  116. timer = (unsigned short)pgm_read_word_near(table_address);
  117. timer -= (((unsigned short)pgm_read_word_near(table_address+2) * (unsigned char)(step_rate & 0x0007))>>3);
  118. }
  119. if(timer < 100) { timer = 100; }//(20kHz this should never happen)////MSG_STEPPER_TOO_HIGH c=0 r=0
  120. return timer;
  121. }
  122. #endif