My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

L6470.h 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. ////////////////////////////////////////////////////////////
  2. //ORIGINAL CODE 12/12/2011- Mike Hord, SparkFun Electronics
  3. //LIBRARY Created by Adam Meyer of bildr Aug 18th 2012
  4. //Released as MIT license
  5. ////////////////////////////////////////////////////////////
  6. #ifndef L6470_h
  7. #define L6470_h
  8. #include <Arduino.h>
  9. #include <SPI.h>
  10. #define SLAVE_SELECT_PIN 38 // Wire this to the CSN pin
  11. // #define RESET 6 // Wire this to the STBY line
  12. #define BUSYN 7 // Wire this to the BSYN line
  13. // constant definitions for overcurrent thresholds. Write these values to
  14. // register dSPIN_OCD_TH to set the level at which an overcurrent even occurs.
  15. #define OCD_TH_375mA 0x00
  16. #define OCD_TH_750mA 0x01
  17. #define OCD_TH_1125mA 0x02
  18. #define OCD_TH_1500mA 0x03
  19. #define OCD_TH_1875mA 0x04
  20. #define OCD_TH_2250mA 0x05
  21. #define OCD_TH_2625mA 0x06
  22. #define OCD_TH_3000mA 0x07
  23. #define OCD_TH_3375mA 0x08
  24. #define OCD_TH_3750mA 0x09
  25. #define OCD_TH_4125mA 0x0A
  26. #define OCD_TH_4500mA 0x0B
  27. #define OCD_TH_4875mA 0x0C
  28. #define OCD_TH_5250mA 0x0D
  29. #define OCD_TH_5625mA 0x0E
  30. #define OCD_TH_6000mA 0x0F
  31. // STEP_MODE option values.
  32. // First comes the "microsteps per step" options...
  33. #define STEP_MODE_STEP_SEL 0x07 // Mask for these bits only.
  34. #define STEP_SEL_1 0x00
  35. #define STEP_SEL_1_2 0x01
  36. #define STEP_SEL_1_4 0x02
  37. #define STEP_SEL_1_8 0x03
  38. #define STEP_SEL_1_16 0x04
  39. #define STEP_SEL_1_32 0x05
  40. #define STEP_SEL_1_64 0x06
  41. #define STEP_SEL_1_128 0x07
  42. // ...next, define the SYNC_EN bit. When set, the BUSYN pin will instead
  43. // output a clock related to the full-step frequency as defined by the
  44. // SYNC_SEL bits below.
  45. #define STEP_MODE_SYNC_EN 0x80 // Mask for this bit
  46. #define SYNC_EN 0x80
  47. // ...last, define the SYNC_SEL modes. The clock output is defined by
  48. // the full-step frequency and the value in these bits- see the datasheet
  49. // for a matrix describing that relationship (page 46).
  50. #define STEP_MODE_SYNC_SEL 0x70
  51. #define SYNC_SEL_1_2 0x00
  52. #define SYNC_SEL_1 0x10
  53. #define SYNC_SEL_2 0x20
  54. #define SYNC_SEL_4 0x30
  55. #define SYNC_SEL_8 0x40
  56. #define SYNC_SEL_16 0x50
  57. #define SYNC_SEL_32 0x60
  58. #define SYNC_SEL_64 0x70
  59. // Bit names for the ALARM_EN register.
  60. // Each of these bits defines one potential alarm condition.
  61. // When one of these conditions occurs and the respective bit in ALARM_EN is set,
  62. // the FLAG pin will go low. The register must be queried to determine which event
  63. // caused the alarm.
  64. #define ALARM_EN_OVERCURRENT 0x01
  65. #define ALARM_EN_THERMAL_SHUTDOWN 0x02
  66. #define ALARM_EN_THERMAL_WARNING 0x04
  67. #define ALARM_EN_UNDER_VOLTAGE 0x08
  68. #define ALARM_EN_STALL_DET_A 0x10
  69. #define ALARM_EN_STALL_DET_B 0x20
  70. #define ALARM_EN_SW_TURN_ON 0x40
  71. #define ALARM_EN_WRONG_NPERF_CMD 0x80
  72. // CONFIG register renames.
  73. // Oscillator options.
  74. // The dSPIN needs to know what the clock frequency is because it uses that for some
  75. // calculations during operation.
  76. #define CONFIG_OSC_SEL 0x000F // Mask for this bit field.
  77. #define CONFIG_INT_16MHZ 0x0000 // Internal 16MHz, no output
  78. #define CONFIG_INT_16MHZ_OSCOUT_2MHZ 0x0008 // Default; internal 16MHz, 2MHz output
  79. #define CONFIG_INT_16MHZ_OSCOUT_4MHZ 0x0009 // Internal 16MHz, 4MHz output
  80. #define CONFIG_INT_16MHZ_OSCOUT_8MHZ 0x000A // Internal 16MHz, 8MHz output
  81. #define CONFIG_INT_16MHZ_OSCOUT_16MHZ 0x000B // Internal 16MHz, 16MHz output
  82. #define CONFIG_EXT_8MHZ_XTAL_DRIVE 0x0004 // External 8MHz crystal
  83. #define CONFIG_EXT_16MHZ_XTAL_DRIVE 0x0005 // External 16MHz crystal
  84. #define CONFIG_EXT_24MHZ_XTAL_DRIVE 0x0006 // External 24MHz crystal
  85. #define CONFIG_EXT_32MHZ_XTAL_DRIVE 0x0007 // External 32MHz crystal
  86. #define CONFIG_EXT_8MHZ_OSCOUT_INVERT 0x000C // External 8MHz crystal, output inverted
  87. #define CONFIG_EXT_16MHZ_OSCOUT_INVERT 0x000D // External 16MHz crystal, output inverted
  88. #define CONFIG_EXT_24MHZ_OSCOUT_INVERT 0x000E // External 24MHz crystal, output inverted
  89. #define CONFIG_EXT_32MHZ_OSCOUT_INVERT 0x000F // External 32MHz crystal, output inverted
  90. // Configure the functionality of the external switch input
  91. #define CONFIG_SW_MODE 0x0010 // Mask for this bit.
  92. #define CONFIG_SW_HARD_STOP 0x0000 // Default; hard stop motor on switch.
  93. #define CONFIG_SW_USER 0x0010 // Tie to the GoUntil and ReleaseSW
  94. // commands to provide jog function.
  95. // See page 25 of datasheet.
  96. // Configure the motor voltage compensation mode (see page 34 of datasheet)
  97. #define CONFIG_EN_VSCOMP 0x0020 // Mask for this bit.
  98. #define CONFIG_VS_COMP_DISABLE 0x0000 // Disable motor voltage compensation.
  99. #define CONFIG_VS_COMP_ENABLE 0x0020 // Enable motor voltage compensation.
  100. // Configure overcurrent detection event handling
  101. #define CONFIG_OC_SD 0x0080 // Mask for this bit.
  102. #define CONFIG_OC_SD_DISABLE 0x0000 // Bridges do NOT shutdown on OC detect
  103. #define CONFIG_OC_SD_ENABLE 0x0080 // Bridges shutdown on OC detect
  104. // Configure the slew rate of the power bridge output
  105. #define CONFIG_POW_SR 0x0300 // Mask for this bit field.
  106. #define CONFIG_SR_180V_us 0x0000 // 180V/us
  107. #define CONFIG_SR_290V_us 0x0200 // 290V/us
  108. #define CONFIG_SR_530V_us 0x0300 // 530V/us
  109. // Integer divisors for PWM sinewave generation
  110. // See page 32 of the datasheet for more information on this.
  111. #define CONFIG_F_PWM_DEC 0x1C00 // mask for this bit field
  112. #define CONFIG_PWM_MUL_0_625 (0x00)<<10
  113. #define CONFIG_PWM_MUL_0_75 (0x01)<<10
  114. #define CONFIG_PWM_MUL_0_875 (0x02)<<10
  115. #define CONFIG_PWM_MUL_1 (0x03)<<10
  116. #define CONFIG_PWM_MUL_1_25 (0x04)<<10
  117. #define CONFIG_PWM_MUL_1_5 (0x05)<<10
  118. #define CONFIG_PWM_MUL_1_75 (0x06)<<10
  119. #define CONFIG_PWM_MUL_2 (0x07)<<10
  120. // Multiplier for the PWM sinewave frequency
  121. #define CONFIG_F_PWM_INT 0xE000 // mask for this bit field.
  122. #define CONFIG_PWM_DIV_1 (0x00)<<13
  123. #define CONFIG_PWM_DIV_2 (0x01)<<13
  124. #define CONFIG_PWM_DIV_3 (0x02)<<13
  125. #define CONFIG_PWM_DIV_4 (0x03)<<13
  126. #define CONFIG_PWM_DIV_5 (0x04)<<13
  127. #define CONFIG_PWM_DIV_6 (0x05)<<13
  128. #define CONFIG_PWM_DIV_7 (0x06)<<13
  129. // Status register bit renames- read-only bits conferring information about the
  130. // device to the user.
  131. #define STATUS_HIZ 0x0001 // high when bridges are in HiZ mode
  132. #define STATUS_BUSY 0x0002 // mirrors BUSY pin
  133. #define STATUS_SW_F 0x0004 // low when switch open, high when closed
  134. #define STATUS_SW_EVN 0x0008 // active high, set on switch falling edge,
  135. // cleared by reading STATUS
  136. #define STATUS_DIR 0x0010 // Indicates current motor direction.
  137. // High is FWD, Low is REV.
  138. #define STATUS_NOTPERF_CMD 0x0080 // Last command not performed.
  139. #define STATUS_WRONG_CMD 0x0100 // Last command not valid.
  140. #define STATUS_UVLO 0x0200 // Undervoltage lockout is active
  141. #define STATUS_TH_WRN 0x0400 // Thermal warning
  142. #define STATUS_TH_SD 0x0800 // Thermal shutdown
  143. #define STATUS_OCD 0x1000 // Overcurrent detected
  144. #define STATUS_STEP_LOSS_A 0x2000 // Stall detected on A bridge
  145. #define STATUS_STEP_LOSS_B 0x4000 // Stall detected on B bridge
  146. #define STATUS_SCK_MOD 0x8000 // Step clock mode is active
  147. // Status register motor status field
  148. #define STATUS_MOT_STATUS 0x0060 // field mask
  149. #define STATUS_MOT_STATUS_STOPPED (0x0000)<<13 // Motor stopped
  150. #define STATUS_MOT_STATUS_ACCELERATION (0x0001)<<13 // Motor accelerating
  151. #define STATUS_MOT_STATUS_DECELERATION (0x0002)<<13 // Motor decelerating
  152. #define STATUS_MOT_STATUS_CONST_SPD (0x0003)<<13 // Motor at constant speed
  153. // Register address redefines.
  154. // See the Param_Handler() function for more info about these.
  155. #define ABS_POS 0x01
  156. #define EL_POS 0x02
  157. #define MARK 0x03
  158. #define SPEED 0x04
  159. #define ACC 0x05
  160. #define DEC 0x06
  161. #define MAX_SPEED 0x07
  162. #define MIN_SPEED 0x08
  163. #define FS_SPD 0x15
  164. #define KVAL_HOLD 0x09
  165. #define KVAL_RUN 0x0A
  166. #define KVAL_ACC 0x0B
  167. #define KVAL_DEC 0x0C
  168. #define INT_SPD 0x0D
  169. #define ST_SLP 0x0E
  170. #define FN_SLP_ACC 0x0F
  171. #define FN_SLP_DEC 0x10
  172. #define K_THERM 0x11
  173. #define ADC_OUT 0x12
  174. #define OCD_TH 0x13
  175. #define STALL_TH 0x14
  176. #define STEP_MODE 0x16
  177. #define ALARM_EN 0x17
  178. #define CONFIG 0x18
  179. #define STATUS 0x19
  180. //dSPIN commands
  181. #define NOP 0x00
  182. #define SET_PARAM 0x00
  183. #define GET_PARAM 0x20
  184. #define RUN 0x50
  185. #define STEP_CLOCK 0x58
  186. #define MOVE 0x40
  187. #define GOTO 0x60
  188. #define GOTO_DIR 0x68
  189. #define GO_UNTIL 0x82
  190. #define RELEASE_SW 0x92
  191. #define GO_HOME 0x70
  192. #define GO_MARK 0x78
  193. #define RESET_POS 0xD8
  194. #define RESET_DEVICE 0xC0
  195. #define SOFT_STOP 0xB0
  196. #define HARD_STOP 0xB8
  197. #define SOFT_HIZ 0xA0
  198. #define HARD_HIZ 0xA8
  199. #define GET_STATUS 0xD0
  200. /* dSPIN direction options */
  201. #define FWD 0x01
  202. #define REV 0x00
  203. /* dSPIN action options */
  204. #define ACTION_RESET 0x00
  205. #define ACTION_COPY 0x01
  206. class L6470{
  207. public:
  208. L6470(int SSPin);
  209. void init(int k_value);
  210. void setMicroSteps(int microSteps);
  211. void setCurrent(int current);
  212. void setMaxSpeed(int speed);
  213. void setMinSpeed(int speed);
  214. void setAcc(float acceleration);
  215. void setDec(float deceleration);
  216. void setOverCurrent(unsigned int ma_current);
  217. void setThresholdSpeed(float threshold);
  218. void setStallCurrent(float ma_current);
  219. unsigned long ParamHandler(byte param, unsigned long value);
  220. void SetLowSpeedOpt(boolean enable);
  221. void run(byte dir, float spd);
  222. void Step_Clock(byte dir);
  223. void goHome();
  224. void setAsHome();
  225. void goMark();
  226. void move(long n_step);
  227. void goTo(long pos);
  228. void goTo_DIR(byte dir, long pos);
  229. void goUntil(byte act, byte dir, unsigned long spd);
  230. boolean isBusy();
  231. void releaseSW(byte act, byte dir);
  232. float getSpeed();
  233. long getPos();
  234. void setMark();
  235. void setMark(long value);
  236. void resetPos();
  237. void resetDev();
  238. void softStop();
  239. void hardStop();
  240. void softFree();
  241. void free();
  242. int getStatus();
  243. void SetParam(byte param, unsigned long value);
  244. private:
  245. long convert(unsigned long val);
  246. unsigned long GetParam(byte param);
  247. unsigned long AccCalc(float stepsPerSecPerSec);
  248. unsigned long DecCalc(float stepsPerSecPerSec);
  249. unsigned long MaxSpdCalc(float stepsPerSec);
  250. unsigned long MinSpdCalc(float stepsPerSec);
  251. unsigned long FSCalc(float stepsPerSec);
  252. unsigned long IntSpdCalc(float stepsPerSec);
  253. unsigned long SpdCalc(float stepsPerSec);
  254. unsigned long Param(unsigned long value, byte bit_len);
  255. byte Xfer(byte data);
  256. int _SSPin;
  257. };
  258. #endif