createSpeedLookupTable.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3. from __future__ import division
  4. """ Generate the stepper delay lookup table for Marlin firmware. """
  5. import argparse
  6. __author__ = "Ben Gamari <bgamari@gmail.com>"
  7. __copyright__ = "Copyright 2012, Ben Gamari"
  8. __license__ = "GPL"
  9. parser = argparse.ArgumentParser(description=__doc__)
  10. parser.add_argument('-f', '--cpu-freq', type=int, default=16, help='CPU clockrate in MHz (default=16)')
  11. parser.add_argument('-d', '--divider', type=int, default=8, help='Timer/counter pre-scale divider (default=8)')
  12. args = parser.parse_args()
  13. cpu_freq = args.cpu_freq * 1000000
  14. timer_freq = cpu_freq / args.divider
  15. print("#pragma once")
  16. print()
  17. print("#if F_CPU == %d" % cpu_freq)
  18. print()
  19. print(" const struct { uint16_t base; uint8_t gain; } speed_lookuptable_fast[256] PROGMEM = {")
  20. a = [0 for i in range(8)] + [ int(0.5 + float(timer_freq) / (i*256)) for i in range(8, 256) ]
  21. b = [0 for i in range(8)] + [ a[i] - a[i+1] for i in range(8, 255) ]
  22. b.append(b[-1])
  23. for i in range(32):
  24. print(" ", end='')
  25. for j in range(8):
  26. print("{ %5d, %5d }," % (a[8*i+j], b[8*i+j]), end='')
  27. if j < 7: print(" ", end='')
  28. if i == 0: print(" // dummy first row")
  29. else: print()
  30. print(" };")
  31. print()
  32. print(" const uint16_t speed_lookuptable_slow[256][2] PROGMEM = {")
  33. a = [ int(0.5 + float(timer_freq) / ((i*8)+(args.cpu_freq*2))) for i in range(256) ]
  34. b = [ a[i] - a[i+1] for i in range(255) ]
  35. b.append(b[-1])
  36. for i in range(32):
  37. print(" ", end='')
  38. for j in range(8):
  39. print("{ %5d, %5d }," % (a[8*i+j], b[8*i+j]), end='')
  40. if j < 7: print(" ", end='')
  41. print()
  42. print(" };")
  43. print()
  44. print("#endif")