createSpeedLookupTable.py 1.6 KB

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