speed_lookuptable.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. // intRes = intIn1 * intIn2 >> 16
  8. // uses:
  9. // r26 to store 0
  10. // r27 to store the byte 1 of the 24 bit result
  11. #define MultiU16X8toH16(intRes, charIn1, intIn2) \
  12. asm volatile ( \
  13. "clr r26 \n\t" \
  14. "mul %A1, %B2 \n\t" \
  15. "movw %A0, r0 \n\t" \
  16. "mul %A1, %A2 \n\t" \
  17. "add %A0, r1 \n\t" \
  18. "adc %B0, r26 \n\t" \
  19. "lsr r0 \n\t" \
  20. "adc %A0, r26 \n\t" \
  21. "adc %B0, r26 \n\t" \
  22. "clr r1 \n\t" \
  23. : \
  24. "=&r" (intRes) \
  25. : \
  26. "d" (charIn1), \
  27. "d" (intIn2) \
  28. : \
  29. "r26" \
  30. )
  31. // intRes = longIn1 * longIn2 >> 24
  32. // uses:
  33. // r26 to store 0
  34. // r27 to store the byte 1 of the 48bit result
  35. #define MultiU24X24toH16(intRes, longIn1, longIn2) \
  36. asm volatile ( \
  37. "clr r26 \n\t" \
  38. "mul %A1, %B2 \n\t" \
  39. "mov r27, r1 \n\t" \
  40. "mul %B1, %C2 \n\t" \
  41. "movw %A0, r0 \n\t" \
  42. "mul %C1, %C2 \n\t" \
  43. "add %B0, r0 \n\t" \
  44. "mul %C1, %B2 \n\t" \
  45. "add %A0, r0 \n\t" \
  46. "adc %B0, r1 \n\t" \
  47. "mul %A1, %C2 \n\t" \
  48. "add r27, r0 \n\t" \
  49. "adc %A0, r1 \n\t" \
  50. "adc %B0, r26 \n\t" \
  51. "mul %B1, %B2 \n\t" \
  52. "add r27, r0 \n\t" \
  53. "adc %A0, r1 \n\t" \
  54. "adc %B0, r26 \n\t" \
  55. "mul %C1, %A2 \n\t" \
  56. "add r27, r0 \n\t" \
  57. "adc %A0, r1 \n\t" \
  58. "adc %B0, r26 \n\t" \
  59. "mul %B1, %A2 \n\t" \
  60. "add r27, r1 \n\t" \
  61. "adc %A0, r26 \n\t" \
  62. "adc %B0, r26 \n\t" \
  63. "lsr r27 \n\t" \
  64. "adc %A0, r26 \n\t" \
  65. "adc %B0, r26 \n\t" \
  66. "clr r1 \n\t" \
  67. : \
  68. "=&r" (intRes) \
  69. : \
  70. "d" (longIn1), \
  71. "d" (longIn2) \
  72. : \
  73. "r26" , "r27" \
  74. )
  75. #else //_NO_ASM
  76. static inline void MultiU16X8toH16(uint16_t& intRes, uint8_t& charIn1, uint16_t& intIn2)
  77. {
  78. intRes = ((uint32_t)charIn1 * (uint32_t)intIn2) >> 16;
  79. }
  80. static inline void MultiU24X24toH16(uint16_t& intRes, uint32_t& longIn1, uint32_t& longIn2)
  81. {
  82. intRes = ((uint64_t)longIn1 * (uint64_t)longIn2) >> 24;
  83. }
  84. #endif //_NO_ASM
  85. FORCE_INLINE unsigned short calc_timer(uint16_t step_rate, uint8_t& step_loops) {
  86. uint16_t timer;
  87. if(step_rate > MAX_STEP_FREQUENCY) step_rate = MAX_STEP_FREQUENCY;
  88. if(step_rate > 20000) { // If steprate > 20kHz >> step 4 times
  89. step_rate = (step_rate >> 2)&0x3fff;
  90. step_loops = 4;
  91. }
  92. else if(step_rate > 10000) { // If steprate > 10kHz >> step 2 times
  93. step_rate = (step_rate >> 1)&0x7fff;
  94. step_loops = 2;
  95. }
  96. else {
  97. step_loops = 1;
  98. }
  99. if(step_rate < (F_CPU/500000)) step_rate = (F_CPU/500000);
  100. step_rate -= (F_CPU/500000); // Correct for minimal speed
  101. if(step_rate >= (8*256)){ // higher step rate
  102. unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate>>8)][0];
  103. unsigned char tmp_step_rate = (step_rate & 0x00ff);
  104. uint16_t gain = (uint16_t)pgm_read_word_near(table_address+2);
  105. MultiU16X8toH16(timer, tmp_step_rate, gain);
  106. timer = (unsigned short)pgm_read_word_near(table_address) - timer;
  107. }
  108. else { // lower step rates
  109. unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
  110. table_address += ((step_rate)>>1) & 0xfffc;
  111. timer = (unsigned short)pgm_read_word_near(table_address);
  112. timer -= (((unsigned short)pgm_read_word_near(table_address+2) * (unsigned char)(step_rate & 0x0007))>>3);
  113. }
  114. if(timer < 100) { timer = 100; }//(20kHz this should never happen)////MSG_STEPPER_TOO_HIGH c=0 r=0
  115. return timer;
  116. }
  117. #endif