My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TMC2660.cpp 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. /**
  2. * TMC26XStepper.cpp - - TMC26X Stepper library for Wiring/Arduino
  3. *
  4. * based on the stepper library by Tom Igoe, et. al.
  5. *
  6. * Copyright (c) 2011, Interactive Matter, Marcus Nowotny
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. //#include "Arduino.h"
  28. #ifdef STM32F7
  29. #include <stdbool.h>
  30. #include <SPI.h>
  31. #include "TMC2660.h"
  32. #include "../../HAL/HAL_STM32F7/HAL_STM32F7.h"
  33. #include "../../core/serial.h"
  34. #include "../../inc/MarlinConfig.h"
  35. #include "../../Marlin.h"
  36. #include "../../module/stepper_indirection.h"
  37. #include "../../module/printcounter.h"
  38. #include "../../libs/duration_t.h"
  39. #include "../../libs/hex_print_routines.h"
  40. //some default values used in initialization
  41. #define DEFAULT_MICROSTEPPING_VALUE 32
  42. //TMC26X register definitions
  43. #define DRIVER_CONTROL_REGISTER 0x0ul
  44. #define CHOPPER_CONFIG_REGISTER 0x80000ul
  45. #define COOL_STEP_REGISTER 0xA0000ul
  46. #define STALL_GUARD2_LOAD_MEASURE_REGISTER 0xC0000ul
  47. #define DRIVER_CONFIG_REGISTER 0xE0000ul
  48. #define REGISTER_BIT_PATTERN 0xFFFFFul
  49. //definitions for the driver control register
  50. #define MICROSTEPPING_PATTERN 0xFul
  51. #define STEP_INTERPOLATION 0x200ul
  52. #define DOUBLE_EDGE_STEP 0x100ul
  53. #define VSENSE 0x40ul
  54. #define READ_MICROSTEP_POSTION 0x0ul
  55. #define READ_STALL_GUARD_READING 0x10ul
  56. #define READ_STALL_GUARD_AND_COOL_STEP 0x20ul
  57. #define READ_SELECTION_PATTERN 0x30ul
  58. //definitions for the chopper config register
  59. #define CHOPPER_MODE_STANDARD 0x0ul
  60. #define CHOPPER_MODE_T_OFF_FAST_DECAY 0x4000ul
  61. #define T_OFF_PATTERN 0xful
  62. #define RANDOM_TOFF_TIME 0x2000ul
  63. #define BLANK_TIMING_PATTERN 0x18000ul
  64. #define BLANK_TIMING_SHIFT 15
  65. #define HYSTERESIS_DECREMENT_PATTERN 0x1800ul
  66. #define HYSTERESIS_DECREMENT_SHIFT 11
  67. #define HYSTERESIS_LOW_VALUE_PATTERN 0x780ul
  68. #define HYSTERESIS_LOW_SHIFT 7
  69. #define HYSTERESIS_START_VALUE_PATTERN 0x78ul
  70. #define HYSTERESIS_START_VALUE_SHIFT 4
  71. #define T_OFF_TIMING_PATERN 0xFul
  72. //definitions for cool step register
  73. #define MINIMUM_CURRENT_FOURTH 0x8000ul
  74. #define CURRENT_DOWN_STEP_SPEED_PATTERN 0x6000ul
  75. #define SE_MAX_PATTERN 0xF00ul
  76. #define SE_CURRENT_STEP_WIDTH_PATTERN 0x60ul
  77. #define SE_MIN_PATTERN 0xful
  78. //definitions for stall guard2 current register
  79. #define STALL_GUARD_FILTER_ENABLED 0x10000ul
  80. #define STALL_GUARD_TRESHHOLD_VALUE_PATTERN 0x17F00ul
  81. #define CURRENT_SCALING_PATTERN 0x1Ful
  82. #define STALL_GUARD_CONFIG_PATTERN 0x17F00ul
  83. #define STALL_GUARD_VALUE_PATTERN 0x7F00ul
  84. //definitions for the input from the TCM260
  85. #define STATUS_STALL_GUARD_STATUS 0x1ul
  86. #define STATUS_OVER_TEMPERATURE_SHUTDOWN 0x2ul
  87. #define STATUS_OVER_TEMPERATURE_WARNING 0x4ul
  88. #define STATUS_SHORT_TO_GROUND_A 0x8ul
  89. #define STATUS_SHORT_TO_GROUND_B 0x10ul
  90. #define STATUS_OPEN_LOAD_A 0x20ul
  91. #define STATUS_OPEN_LOAD_B 0x40ul
  92. #define STATUS_STAND_STILL 0x80ul
  93. #define READOUT_VALUE_PATTERN 0xFFC00ul
  94. #define CPU_32_BIT
  95. //default values
  96. #define INITIAL_MICROSTEPPING 0x3ul //32th microstepping
  97. SPIClass SPI_6(SPI6, SPI6_MOSI_PIN, SPI6_MISO_PIN, SPI6_SCK_PIN);
  98. #define STEPPER_SPI SPI_6
  99. //debuging output
  100. //#define TMC_DEBUG1
  101. unsigned char current_scaling = 0;
  102. /**
  103. * Constructor
  104. * number_of_steps - the steps per rotation
  105. * cs_pin - the SPI client select pin
  106. * dir_pin - the pin where the direction pin is connected
  107. * step_pin - the pin where the step pin is connected
  108. */
  109. TMC26XStepper::TMC26XStepper(int number_of_steps, int cs_pin, int dir_pin, int step_pin, unsigned int current, unsigned int resistor) {
  110. // We are not started yet
  111. started = false;
  112. // By default cool step is not enabled
  113. cool_step_enabled = false;
  114. // Save the pins for later use
  115. this->cs_pin = cs_pin;
  116. this->dir_pin = dir_pin;
  117. this->step_pin = step_pin;
  118. // Store the current sense resistor value for later use
  119. this->resistor = resistor;
  120. // Initizalize our status values
  121. this->steps_left = 0;
  122. this->direction = 0;
  123. // Initialize register values
  124. driver_control_register_value = DRIVER_CONTROL_REGISTER | INITIAL_MICROSTEPPING;
  125. chopper_config_register = CHOPPER_CONFIG_REGISTER;
  126. // Setting the default register values
  127. driver_control_register_value = DRIVER_CONTROL_REGISTER|INITIAL_MICROSTEPPING;
  128. microsteps = _BV(INITIAL_MICROSTEPPING);
  129. chopper_config_register = CHOPPER_CONFIG_REGISTER;
  130. cool_step_register_value = COOL_STEP_REGISTER;
  131. stall_guard2_current_register_value = STALL_GUARD2_LOAD_MEASURE_REGISTER;
  132. driver_configuration_register_value = DRIVER_CONFIG_REGISTER | READ_STALL_GUARD_READING;
  133. // Set the current
  134. setCurrent(current);
  135. // Set to a conservative start value
  136. setConstantOffTimeChopper(7, 54, 13,12,1);
  137. // Set a nice microstepping value
  138. setMicrosteps(DEFAULT_MICROSTEPPING_VALUE);
  139. // Save the number of steps
  140. this->number_of_steps = number_of_steps;
  141. }
  142. /**
  143. * start & configure the stepper driver
  144. * just must be called.
  145. */
  146. void TMC26XStepper::start() {
  147. #ifdef TMC_DEBUG1
  148. SERIAL_ECHOPGM("\n TMC26X stepper library \n");
  149. SERIAL_ECHOPAIR("\n CS pin: ", cs_pin);
  150. SERIAL_ECHOPAIR("\n DIR pin: ", dir_pin);
  151. SERIAL_ECHOPAIR("\n STEP pin: ", step_pin);
  152. SERIAL_PRINTF("\n current scaling: %d", current_scaling);
  153. SERIAL_PRINTF("\n Resistor: %d", resistor);
  154. //SERIAL_PRINTF("\n current: %d", current);
  155. SERIAL_ECHOPAIR("\n Microstepping: ", microsteps);
  156. #endif
  157. //set the pins as output & its initial value
  158. pinMode(step_pin, OUTPUT);
  159. pinMode(dir_pin, OUTPUT);
  160. pinMode(cs_pin, OUTPUT);
  161. //pinMode(STEPPER_ENABLE_PIN, OUTPUT);
  162. digitalWrite(step_pin, LOW);
  163. digitalWrite(dir_pin, LOW);
  164. digitalWrite(cs_pin, HIGH);
  165. STEPPER_SPI.begin();
  166. STEPPER_SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  167. //set the initial values
  168. send262(driver_control_register_value);
  169. send262(chopper_config_register);
  170. send262(cool_step_register_value);
  171. send262(stall_guard2_current_register_value);
  172. send262(driver_configuration_register_value);
  173. //save that we are in running mode
  174. started = true;
  175. }
  176. /**
  177. * Mark the driver as unstarted to be able to start it again
  178. */
  179. void TMC26XStepper::un_start() { started = false; }
  180. /**
  181. * Sets the speed in revs per minute
  182. */
  183. void TMC26XStepper::setSpeed(unsigned int whatSpeed) {
  184. this->speed = whatSpeed;
  185. this->step_delay = 60UL * sq(1000UL) / ((unsigned long)this->number_of_steps * (unsigned long)whatSpeed * (unsigned long)this->microsteps);
  186. #ifdef TMC_DEBUG0 // crashes
  187. //SERIAL_PRINTF("Step delay in micros: ");
  188. SERIAL_ECHOPAIR("\nStep delay in micros: ", this->step_delay);
  189. #endif
  190. // Update the next step time
  191. this->next_step_time = this->last_step_time + this->step_delay;
  192. }
  193. unsigned int TMC26XStepper::getSpeed(void) { return this->speed; }
  194. /**
  195. * Moves the motor steps_to_move steps.
  196. * Negative indicates the reverse direction.
  197. */
  198. char TMC26XStepper::step(int steps_to_move) {
  199. if (this->steps_left == 0) {
  200. this->steps_left = abs(steps_to_move); // how many steps to take
  201. // determine direction based on whether steps_to_move is + or -:
  202. if (steps_to_move > 0)
  203. this->direction = 1;
  204. else if (steps_to_move < 0)
  205. this->direction = 0;
  206. return 0;
  207. }
  208. return -1;
  209. }
  210. char TMC26XStepper::move(void) {
  211. // decrement the number of steps, moving one step each time:
  212. if (this->steps_left > 0) {
  213. unsigned long time = micros();
  214. // move only if the appropriate delay has passed:
  215. // rem if (time >= this->next_step_time) {
  216. if (abs(time - this->last_step_time) > this->step_delay) {
  217. // increment or decrement the step number,
  218. // depending on direction:
  219. if (this->direction == 1)
  220. digitalWrite(step_pin, HIGH);
  221. else {
  222. digitalWrite(dir_pin, HIGH);
  223. digitalWrite(step_pin, HIGH);
  224. }
  225. // get the timeStamp of when you stepped:
  226. this->last_step_time = time;
  227. this->next_step_time = time + this->step_delay;
  228. // decrement the steps left:
  229. steps_left--;
  230. //disable the step & dir pins
  231. digitalWrite(step_pin, LOW);
  232. digitalWrite(dir_pin, LOW);
  233. }
  234. return -1;
  235. }
  236. return 0;
  237. }
  238. char TMC26XStepper::isMoving(void) { return this->steps_left > 0; }
  239. unsigned int TMC26XStepper::getStepsLeft(void) { return this->steps_left; }
  240. char TMC26XStepper::stop(void) {
  241. //note to self if the motor is currently moving
  242. char state = isMoving();
  243. //stop the motor
  244. this->steps_left = 0;
  245. this->direction = 0;
  246. //return if it was moving
  247. return state;
  248. }
  249. void TMC26XStepper::setCurrent(unsigned int current) {
  250. unsigned char current_scaling = 0;
  251. //calculate the current scaling from the max current setting (in mA)
  252. double mASetting = (double)current,
  253. resistor_value = (double)this->resistor;
  254. // remove vsense flag
  255. this->driver_configuration_register_value &= ~(VSENSE);
  256. // Derived from I = (cs + 1) / 32 * (Vsense / Rsense)
  257. // leading to cs = 32 * R * I / V (with V = 0,31V oder 0,165V and I = 1000 * current)
  258. // with Rsense = 0,15
  259. // for vsense = 0,310V (VSENSE not set)
  260. // or vsense = 0,165V (VSENSE set)
  261. current_scaling = (byte)((resistor_value * mASetting * 32.0 / (0.31 * sq(1000.0))) - 0.5); //theoretically - 1.0 for better rounding it is 0.5
  262. // Check if the current scalingis too low
  263. if (current_scaling < 16) {
  264. // Set the csense bit to get a use half the sense voltage (to support lower motor currents)
  265. this->driver_configuration_register_value |= VSENSE;
  266. // and recalculate the current setting
  267. current_scaling = (byte)((resistor_value * mASetting * 32.0 / (0.165 * sq(1000.0))) - 0.5); //theoretically - 1.0 for better rounding it is 0.5
  268. #ifdef TMC_DEBUG0 // crashes
  269. //SERIAL_PRINTF("CS (Vsense=1): ");
  270. SERIAL_ECHOPAIR("\nCS (Vsense=1): ",current_scaling);
  271. } else {
  272. //SERIAL_PRINTF("CS: ");
  273. SERIAL_ECHOPAIR("\nCS: ", current_scaling);
  274. #endif
  275. }
  276. // do some sanity checks
  277. NOMORE(current_scaling, 31);
  278. // delete the old value
  279. stall_guard2_current_register_value &= ~(CURRENT_SCALING_PATTERN);
  280. // set the new current scaling
  281. stall_guard2_current_register_value |= current_scaling;
  282. // if started we directly send it to the motor
  283. if (started) {
  284. send262(driver_configuration_register_value);
  285. send262(stall_guard2_current_register_value);
  286. }
  287. }
  288. unsigned int TMC26XStepper::getCurrent(void) {
  289. // Calculate the current according to the datasheet to be on the safe side.
  290. // This is not the fastest but the most accurate and illustrative way.
  291. double result = (double)(stall_guard2_current_register_value & CURRENT_SCALING_PATTERN),
  292. resistor_value = (double)this->resistor,
  293. voltage = (driver_configuration_register_value & VSENSE) ? 0.165 : 0.31;
  294. result = (result + 1.0) / 32.0 * voltage / resistor_value * sq(1000.0);
  295. return (unsigned int)result;
  296. }
  297. void TMC26XStepper::setStallGuardThreshold(char stall_guard_threshold, char stall_guard_filter_enabled) {
  298. // We just have 5 bits
  299. LIMIT(stall_guard_threshold, -64, 63);
  300. // Add trim down to 7 bits
  301. stall_guard_threshold &= 0x7F;
  302. // Delete old stall guard settings
  303. stall_guard2_current_register_value &= ~(STALL_GUARD_CONFIG_PATTERN);
  304. if (stall_guard_filter_enabled)
  305. stall_guard2_current_register_value |= STALL_GUARD_FILTER_ENABLED;
  306. // Set the new stall guard threshold
  307. stall_guard2_current_register_value |= (((unsigned long)stall_guard_threshold << 8) & STALL_GUARD_CONFIG_PATTERN);
  308. // If started we directly send it to the motor
  309. if (started) send262(stall_guard2_current_register_value);
  310. }
  311. char TMC26XStepper::getStallGuardThreshold(void) {
  312. unsigned long stall_guard_threshold = stall_guard2_current_register_value & STALL_GUARD_VALUE_PATTERN;
  313. //shift it down to bit 0
  314. stall_guard_threshold >>= 8;
  315. //convert the value to an int to correctly handle the negative numbers
  316. char result = stall_guard_threshold;
  317. //check if it is negative and fill it up with leading 1 for proper negative number representation
  318. //rem if (result & _BV(6)) {
  319. if (TEST(result, 6)) result |= 0xC0;
  320. return result;
  321. }
  322. char TMC26XStepper::getStallGuardFilter(void) {
  323. if (stall_guard2_current_register_value & STALL_GUARD_FILTER_ENABLED)
  324. return -1;
  325. return 0;
  326. }
  327. /**
  328. * Set the number of microsteps per step.
  329. * 0,2,4,8,16,32,64,128,256 is supported
  330. * any value in between will be mapped to the next smaller value
  331. * 0 and 1 set the motor in full step mode
  332. */
  333. void TMC26XStepper::setMicrosteps(int number_of_steps) {
  334. long setting_pattern;
  335. //poor mans log
  336. if (number_of_steps >= 256) {
  337. setting_pattern = 0;
  338. microsteps = 256;
  339. }
  340. else if (number_of_steps >= 128) {
  341. setting_pattern = 1;
  342. microsteps = 128;
  343. }
  344. else if (number_of_steps >= 64) {
  345. setting_pattern = 2;
  346. microsteps = 64;
  347. }
  348. else if (number_of_steps >= 32) {
  349. setting_pattern = 3;
  350. microsteps = 32;
  351. }
  352. else if (number_of_steps >= 16) {
  353. setting_pattern = 4;
  354. microsteps = 16;
  355. }
  356. else if (number_of_steps >= 8) {
  357. setting_pattern = 5;
  358. microsteps = 8;
  359. }
  360. else if (number_of_steps >= 4) {
  361. setting_pattern = 6;
  362. microsteps = 4;
  363. }
  364. else if (number_of_steps >= 2) {
  365. setting_pattern = 7;
  366. microsteps = 2;
  367. //1 and 0 lead to full step
  368. }
  369. else if (number_of_steps <= 1) {
  370. setting_pattern = 8;
  371. microsteps = 1;
  372. }
  373. #ifdef TMC_DEBUG0 // crashes
  374. //SERIAL_PRINTF("Microstepping: ");
  375. SERIAL_ECHOPAIR("\n Microstepping: ", microsteps);
  376. #endif
  377. // Delete the old value
  378. this->driver_control_register_value &= 0xFFFF0UL;
  379. // Set the new value
  380. this->driver_control_register_value |= setting_pattern;
  381. // If started we directly send it to the motor
  382. if (started) send262(driver_control_register_value);
  383. // Recalculate the stepping delay by simply setting the speed again
  384. this->setSpeed(this->speed);
  385. }
  386. /**
  387. * returns the effective number of microsteps at the moment
  388. */
  389. int TMC26XStepper::getMicrosteps(void) { return microsteps }
  390. /**
  391. * constant_off_time: The off time setting controls the minimum chopper frequency.
  392. * For most applications an off time within the range of 5μs to 20μs will fit.
  393. * 2...15: off time setting
  394. *
  395. * blank_time: Selects the comparator blank time. This time needs to safely cover the switching event and the
  396. * duration of the ringing on the sense resistor. For
  397. * 0: min. setting 3: max. setting
  398. *
  399. * fast_decay_time_setting: Fast decay time setting. With CHM=1, these bits control the portion of fast decay for each chopper cycle.
  400. * 0: slow decay only
  401. * 1...15: duration of fast decay phase
  402. *
  403. * sine_wave_offset: Sine wave offset. With CHM=1, these bits control the sine wave offset.
  404. * A positive offset corrects for zero crossing error.
  405. * -3..-1: negative offset 0: no offset 1...12: positive offset
  406. *
  407. * use_current_comparator: Selects usage of the current comparator for termination of the fast decay cycle.
  408. * If current comparator is enabled, it terminates the fast decay cycle in case the current
  409. * reaches a higher negative value than the actual positive value.
  410. * 1: enable comparator termination of fast decay cycle
  411. * 0: end by time only
  412. */
  413. void TMC26XStepper::setConstantOffTimeChopper(char constant_off_time, char blank_time, char fast_decay_time_setting, char sine_wave_offset, unsigned char use_current_comparator) {
  414. // Perform some sanity checks
  415. LIMIT(constant_off_time, 2, 15);
  416. // Save the constant off time
  417. this->constant_off_time = constant_off_time;
  418. // Calculate the value acc to the clock cycles
  419. const char blank_value = blank_time >= 54 ? 3 :
  420. blank_time >= 36 ? 2 :
  421. blank_time >= 24 ? 1 : 0;
  422. LIMIT(fast_decay_time_setting, 0, 15);
  423. LIMIT(sine_wave_offset, -3, 12);
  424. // Shift the sine_wave_offset
  425. sine_wave_offset += 3;
  426. // Calculate the register setting
  427. // First of all delete all the values for this
  428. chopper_config_register &= ~(_BV(12) | BLANK_TIMING_PATTERN | HYSTERESIS_DECREMENT_PATTERN | HYSTERESIS_LOW_VALUE_PATTERN | HYSTERESIS_START_VALUE_PATTERN | T_OFF_TIMING_PATERN);
  429. // Set the constant off pattern
  430. chopper_config_register |= CHOPPER_MODE_T_OFF_FAST_DECAY;
  431. // Set the blank timing value
  432. chopper_config_register |= ((unsigned long)blank_value) << BLANK_TIMING_SHIFT;
  433. // Setting the constant off time
  434. chopper_config_register |= constant_off_time;
  435. // Set the fast decay time
  436. // Set msb
  437. chopper_config_register |= (((unsigned long)(fast_decay_time_setting & 0x8)) << HYSTERESIS_DECREMENT_SHIFT);
  438. // Other bits
  439. chopper_config_register |= (((unsigned long)(fast_decay_time_setting & 0x7)) << HYSTERESIS_START_VALUE_SHIFT);
  440. // Set the sine wave offset
  441. chopper_config_register |= (unsigned long)sine_wave_offset << HYSTERESIS_LOW_SHIFT;
  442. // Using the current comparator?
  443. if (!use_current_comparator)
  444. chopper_config_register |= _BV(12);
  445. // If started we directly send it to the motor
  446. if (started) {
  447. // rem send262(driver_control_register_value);
  448. send262(chopper_config_register);
  449. }
  450. }
  451. /**
  452. * constant_off_time: The off time setting controls the minimum chopper frequency.
  453. * For most applications an off time within the range of 5μs to 20μs will fit.
  454. * 2...15: off time setting
  455. *
  456. * blank_time: Selects the comparator blank time. This time needs to safely cover the switching event and the
  457. * duration of the ringing on the sense resistor. For
  458. * 0: min. setting 3: max. setting
  459. *
  460. * hysteresis_start: Hysteresis start setting. Please remark, that this value is an offset to the hysteresis end value HEND.
  461. * 1...8
  462. *
  463. * hysteresis_end: Hysteresis end setting. Sets the hysteresis end value after a number of decrements. Decrement interval time is controlled by HDEC.
  464. * The sum HSTRT+HEND must be <16. At a current setting CS of max. 30 (amplitude reduced to 240), the sum is not limited.
  465. * -3..-1: negative HEND 0: zero HEND 1...12: positive HEND
  466. *
  467. * hysteresis_decrement: Hysteresis decrement setting. This setting determines the slope of the hysteresis during on time and during fast decay time.
  468. * 0: fast decrement 3: very slow decrement
  469. */
  470. void TMC26XStepper::setSpreadCycleChopper(char constant_off_time, char blank_time, char hysteresis_start, char hysteresis_end, char hysteresis_decrement) {
  471. // Perform some sanity checks
  472. LIMIT(constant_off_time, 2, 15);
  473. // Save the constant off time
  474. this->constant_off_time = constant_off_time;
  475. // Calculate the value acc to the clock cycles
  476. const char blank_value = blank_time >= 54 ? 3 :
  477. blank_time >= 36 ? 2 :
  478. blank_time >= 24 ? 1 : 0;
  479. LIMIT(hysteresis_start, 1, 8);
  480. hysteresis_start--;
  481. LIMIT(hysteresis_start, -3, 12);
  482. // Shift the hysteresis_end
  483. hysteresis_end += 3;
  484. LIMIT(hysteresis_decrement, 0, 3);
  485. //first of all delete all the values for this
  486. chopper_config_register &= ~(CHOPPER_MODE_T_OFF_FAST_DECAY | BLANK_TIMING_PATTERN | HYSTERESIS_DECREMENT_PATTERN | HYSTERESIS_LOW_VALUE_PATTERN | HYSTERESIS_START_VALUE_PATTERN | T_OFF_TIMING_PATERN);
  487. //set the blank timing value
  488. chopper_config_register |= ((unsigned long)blank_value) << BLANK_TIMING_SHIFT;
  489. //setting the constant off time
  490. chopper_config_register |= constant_off_time;
  491. //set the hysteresis_start
  492. chopper_config_register |= ((unsigned long)hysteresis_start) << HYSTERESIS_START_VALUE_SHIFT;
  493. //set the hysteresis end
  494. chopper_config_register |= ((unsigned long)hysteresis_end) << HYSTERESIS_LOW_SHIFT;
  495. //set the hystereis decrement
  496. chopper_config_register |= ((unsigned long)blank_value) << BLANK_TIMING_SHIFT;
  497. //if started we directly send it to the motor
  498. if (started) {
  499. //rem send262(driver_control_register_value);
  500. send262(chopper_config_register);
  501. }
  502. }
  503. /**
  504. * In a constant off time chopper scheme both coil choppers run freely, i.e. are not synchronized.
  505. * The frequency of each chopper mainly depends on the coil current and the position dependant motor coil inductivity, thus it depends on the microstep position.
  506. * With some motors a slightly audible beat can occur between the chopper frequencies, especially when they are near to each other. This typically occurs at a
  507. * few microstep positions within each quarter wave. This effect normally is not audible when compared to mechanical noise generated by ball bearings, etc.
  508. * Further factors which can cause a similar effect are a poor layout of sense resistor GND connection.
  509. * Hint: A common factor, which can cause motor noise, is a bad PCB layout causing coupling of both sense resistor voltages
  510. * (please refer to sense resistor layout hint in chapter 8.1).
  511. * In order to minimize the effect of a beat between both chopper frequencies, an internal random generator is provided.
  512. * It modulates the slow decay time setting when switched on by the RNDTF bit. The RNDTF feature further spreads the chopper spectrum,
  513. * reducing electromagnetic emission on single frequencies.
  514. */
  515. void TMC26XStepper::setRandomOffTime(char value) {
  516. if (value)
  517. chopper_config_register |= RANDOM_TOFF_TIME;
  518. else
  519. chopper_config_register &= ~(RANDOM_TOFF_TIME);
  520. //if started we directly send it to the motor
  521. if (started) {
  522. //rem send262(driver_control_register_value);
  523. send262(chopper_config_register);
  524. }
  525. }
  526. void TMC26XStepper::setCoolStepConfiguration(
  527. unsigned int lower_SG_threshold,
  528. unsigned int SG_hysteresis,
  529. unsigned char current_decrement_step_size,
  530. unsigned char current_increment_step_size,
  531. unsigned char lower_current_limit)
  532. {
  533. // Sanitize the input values
  534. NOMORE(lower_SG_threshold, 480);
  535. // Divide by 32
  536. lower_SG_threshold >>= 5;
  537. NOMORE(SG_hysteresis, 480);
  538. // Divide by 32
  539. SG_hysteresis >>= 5;
  540. NOMORE(current_decrement_step_size, 3);
  541. NOMORE(current_increment_step_size, 3);
  542. NOMORE(lower_current_limit, 1);
  543. // Store the lower level in order to enable/disable the cool step
  544. this->cool_step_lower_threshold=lower_SG_threshold;
  545. // If cool step is not enabled we delete the lower value to keep it disabled
  546. if (!this->cool_step_enabled) lower_SG_threshold = 0;
  547. // The good news is that we can start with a complete new cool step register value
  548. // And simply set the values in the register
  549. cool_step_register_value = ((unsigned long)lower_SG_threshold)
  550. | (((unsigned long)SG_hysteresis) << 8)
  551. | (((unsigned long)current_decrement_step_size) << 5)
  552. | (((unsigned long)current_increment_step_size) << 13)
  553. | (((unsigned long)lower_current_limit) << 15)
  554. | COOL_STEP_REGISTER; // Register signature
  555. //SERIAL_PRINTFln(cool_step_register_value,HEX);
  556. if (started) send262(cool_step_register_value);
  557. }
  558. void TMC26XStepper::setCoolStepEnabled(boolean enabled) {
  559. // Simply delete the lower limit to disable the cool step
  560. cool_step_register_value &= ~SE_MIN_PATTERN;
  561. // And set it to the proper value if cool step is to be enabled
  562. if (enabled)
  563. cool_step_register_value |= this->cool_step_lower_threshold;
  564. // And save the enabled status
  565. this->cool_step_enabled = enabled;
  566. // Save the register value
  567. if (started) send262(cool_step_register_value);
  568. }
  569. boolean TMC26XStepper::isCoolStepEnabled(void) { return this->cool_step_enabled; }
  570. unsigned int TMC26XStepper::getCoolStepLowerSgThreshold() {
  571. // We return our internally stored value - in order to provide the correct setting even if cool step is not enabled
  572. return this->cool_step_lower_threshold<<5;
  573. }
  574. unsigned int TMC26XStepper::getCoolStepUpperSgThreshold() {
  575. return (unsigned char)((cool_step_register_value & SE_MAX_PATTERN) >> 8) << 5;
  576. }
  577. unsigned char TMC26XStepper::getCoolStepCurrentIncrementSize() {
  578. return (unsigned char)((cool_step_register_value & CURRENT_DOWN_STEP_SPEED_PATTERN) >> 13);
  579. }
  580. unsigned char TMC26XStepper::getCoolStepNumberOfSGReadings() {
  581. return (unsigned char)((cool_step_register_value & SE_CURRENT_STEP_WIDTH_PATTERN) >> 5);
  582. }
  583. unsigned char TMC26XStepper::getCoolStepLowerCurrentLimit() {
  584. return (unsigned char)((cool_step_register_value & MINIMUM_CURRENT_FOURTH) >> 15);
  585. }
  586. void TMC26XStepper::setEnabled(boolean enabled) {
  587. //delete the t_off in the chopper config to get sure
  588. chopper_config_register &= ~(T_OFF_PATTERN);
  589. if (enabled) {
  590. //and set the t_off time
  591. chopper_config_register |= this->constant_off_time;
  592. }
  593. //if not enabled we don't have to do anything since we already delete t_off from the register
  594. if (started) send262(chopper_config_register);
  595. }
  596. boolean TMC26XStepper::isEnabled() { return !!(chopper_config_register & T_OFF_PATTERN); }
  597. /**
  598. * reads a value from the TMC26X status register. The value is not obtained directly but can then
  599. * be read by the various status routines.
  600. *
  601. */
  602. void TMC26XStepper::readStatus(char read_value) {
  603. unsigned long old_driver_configuration_register_value = driver_configuration_register_value;
  604. //reset the readout configuration
  605. driver_configuration_register_value &= ~(READ_SELECTION_PATTERN);
  606. //this now equals TMC26X_READOUT_POSITION - so we just have to check the other two options
  607. if (read_value == TMC26X_READOUT_STALLGUARD)
  608. driver_configuration_register_value |= READ_STALL_GUARD_READING;
  609. else if (read_value == TMC26X_READOUT_CURRENT)
  610. driver_configuration_register_value |= READ_STALL_GUARD_AND_COOL_STEP;
  611. //all other cases are ignored to prevent funny values
  612. //check if the readout is configured for the value we are interested in
  613. if (driver_configuration_register_value != old_driver_configuration_register_value) {
  614. //because then we need to write the value twice - one time for configuring, second time to get the value, see below
  615. send262(driver_configuration_register_value);
  616. }
  617. //write the configuration to get the last status
  618. send262(driver_configuration_register_value);
  619. }
  620. int TMC26XStepper::getMotorPosition(void) {
  621. //we read it out even if we are not started yet - perhaps it is useful information for somebody
  622. readStatus(TMC26X_READOUT_POSITION);
  623. return getReadoutValue();
  624. }
  625. //reads the stall guard setting from last status
  626. //returns -1 if stallguard information is not present
  627. int TMC26XStepper::getCurrentStallGuardReading(void) {
  628. //if we don't yet started there cannot be a stall guard value
  629. if (!started) return -1;
  630. //not time optimal, but solution optiomal:
  631. //first read out the stall guard value
  632. readStatus(TMC26X_READOUT_STALLGUARD);
  633. return getReadoutValue();
  634. }
  635. unsigned char TMC26XStepper::getCurrentCSReading(void) {
  636. //if we don't yet started there cannot be a stall guard value
  637. if (!started) return 0;
  638. //not time optimal, but solution optiomal:
  639. //first read out the stall guard value
  640. readStatus(TMC26X_READOUT_CURRENT);
  641. return (getReadoutValue() & 0x1F);
  642. }
  643. unsigned int TMC26XStepper::getCurrentCurrent(void) {
  644. double result = (double)getCurrentCSReading(),
  645. resistor_value = (double)this->resistor,
  646. voltage = (driver_configuration_register_value & VSENSE)? 0.165 : 0.31;
  647. result = (result + 1.0) / 32.0 * voltage / resistor_value * sq(1000.0);
  648. return (unsigned int)result;
  649. }
  650. /**
  651. * Return true if the stallguard threshold has been reached
  652. */
  653. boolean TMC26XStepper::isStallGuardOverThreshold(void) {
  654. if (!this->started) return false;
  655. return (driver_status_result & STATUS_STALL_GUARD_STATUS);
  656. }
  657. /**
  658. * returns if there is any over temperature condition:
  659. * OVER_TEMPERATURE_PREWARING if pre warning level has been reached
  660. * OVER_TEMPERATURE_SHUTDOWN if the temperature is so hot that the driver is shut down
  661. * Any of those levels are not too good.
  662. */
  663. char TMC26XStepper::getOverTemperature(void) {
  664. if (!this->started) return 0;
  665. if (driver_status_result & STATUS_OVER_TEMPERATURE_SHUTDOWN)
  666. return TMC26X_OVERTEMPERATURE_SHUTDOWN;
  667. if (driver_status_result & STATUS_OVER_TEMPERATURE_WARNING)
  668. return TMC26X_OVERTEMPERATURE_PREWARING;
  669. return 0;
  670. }
  671. // Is motor channel A shorted to ground
  672. boolean TMC26XStepper::isShortToGroundA(void) {
  673. if (!this->started) return false;
  674. return (driver_status_result & STATUS_SHORT_TO_GROUND_A);
  675. }
  676. // Is motor channel B shorted to ground
  677. boolean TMC26XStepper::isShortToGroundB(void) {
  678. if (!this->started) return false;
  679. return (driver_status_result & STATUS_SHORT_TO_GROUND_B);
  680. }
  681. // Is motor channel A connected
  682. boolean TMC26XStepper::isOpenLoadA(void) {
  683. if (!this->started) return false;
  684. return (driver_status_result & STATUS_OPEN_LOAD_A);
  685. }
  686. // Is motor channel B connected
  687. boolean TMC26XStepper::isOpenLoadB(void) {
  688. if (!this->started) return false;
  689. return (driver_status_result & STATUS_OPEN_LOAD_B);
  690. }
  691. // Is chopper inactive since 2^20 clock cycles - defaults to ~0,08s
  692. boolean TMC26XStepper::isStandStill(void) {
  693. if (!this->started) return false;
  694. return (driver_status_result & STATUS_STAND_STILL);
  695. }
  696. //is chopper inactive since 2^20 clock cycles - defaults to ~0,08s
  697. boolean TMC26XStepper::isStallGuardReached(void) {
  698. if (!this->started) return false;
  699. return (driver_status_result & STATUS_STALL_GUARD_STATUS);
  700. }
  701. //reads the stall guard setting from last status
  702. //returns -1 if stallguard inforamtion is not present
  703. int TMC26XStepper::getReadoutValue(void) {
  704. return (int)(driver_status_result >> 10);
  705. }
  706. int TMC26XStepper::getResistor() { return this->resistor; }
  707. boolean TMC26XStepper::isCurrentScalingHalfed() {
  708. return !!(this->driver_configuration_register_value & VSENSE);
  709. }
  710. /**
  711. * version() returns the version of the library:
  712. */
  713. int TMC26XStepper::version(void) { return 1; }
  714. void TMC26XStepper::debugLastStatus() {
  715. #ifdef TMC_DEBUG1
  716. if (this->started) {
  717. if (this->getOverTemperature()&TMC26X_OVERTEMPERATURE_PREWARING)
  718. SERIAL_ECHOLNPGM("\n WARNING: Overtemperature Prewarning!");
  719. else if (this->getOverTemperature()&TMC26X_OVERTEMPERATURE_SHUTDOWN)
  720. SERIAL_ECHOLNPGM("\n ERROR: Overtemperature Shutdown!");
  721. if (this->isShortToGroundA())
  722. SERIAL_ECHOLNPGM("\n ERROR: SHORT to ground on channel A!");
  723. if (this->isShortToGroundB())
  724. SERIAL_ECHOLNPGM("\n ERROR: SHORT to ground on channel B!");
  725. if (this->isOpenLoadA())
  726. SERIAL_ECHOLNPGM("\n ERROR: Channel A seems to be unconnected!");
  727. if (this->isOpenLoadB())
  728. SERIAL_ECHOLNPGM("\n ERROR: Channel B seems to be unconnected!");
  729. if (this->isStallGuardReached())
  730. SERIAL_ECHOLNPGM("\n INFO: Stall Guard level reached!");
  731. if (this->isStandStill())
  732. SERIAL_ECHOLNPGM("\n INFO: Motor is standing still.");
  733. unsigned long readout_config = driver_configuration_register_value & READ_SELECTION_PATTERN;
  734. const int value = getReadoutValue();
  735. if (readout_config == READ_MICROSTEP_POSTION) {
  736. //SERIAL_PRINTF("Microstep postion phase A: ");
  737. SERIAL_ECHOPAIR("\n Microstep postion phase A: ", value);
  738. }
  739. else if (readout_config == READ_STALL_GUARD_READING) {
  740. //SERIAL_PRINTF("Stall Guard value:");
  741. SERIAL_ECHOPAIR("\n Stall Guard value:", value);
  742. }
  743. else if (readout_config == READ_STALL_GUARD_AND_COOL_STEP) {
  744. int stallGuard = value & 0xF, current = value & 0x1F0;
  745. //SERIAL_PRINTF("Approx Stall Guard: ");
  746. SERIAL_ECHOPAIR("\n Approx Stall Guard: ", stallGuard);
  747. //SERIAL_PRINTF("Current level");
  748. SERIAL_ECHOPAIR("\n Current level", current);
  749. }
  750. }
  751. #endif
  752. }
  753. /**
  754. * send register settings to the stepper driver via SPI
  755. * returns the current status
  756. */
  757. inline void TMC26XStepper::send262(unsigned long datagram) {
  758. unsigned long i_datagram;
  759. //preserver the previous spi mode
  760. //unsigned char oldMode = SPCR & SPI_MODE_MASK;
  761. //if the mode is not correct set it to mode 3
  762. //if (oldMode != SPI_MODE3) {
  763. // SPI.setDataMode(SPI_MODE3);
  764. //}
  765. //select the TMC driver
  766. digitalWrite(cs_pin,LOW);
  767. //ensure that only valid bist are set (0-19)
  768. //datagram &=REGISTER_BIT_PATTERN;
  769. #ifdef TMC_DEBUG1
  770. //SERIAL_PRINTF("Sending ");
  771. //SERIAL_PRINTF("Sending ", datagram,HEX);
  772. //SERIAL_ECHOPAIR("\n\nSending \n", print_hex_long(datagram));
  773. SERIAL_PRINTF("\n\nSending %x", datagram);
  774. #endif
  775. //write/read the values
  776. i_datagram = STEPPER_SPI.transfer((datagram >> 16) & 0xFF);
  777. i_datagram <<= 8;
  778. i_datagram |= STEPPER_SPI.transfer((datagram >> 8) & 0xFF);
  779. i_datagram <<= 8;
  780. i_datagram |= STEPPER_SPI.transfer((datagram) & 0xFF);
  781. i_datagram >>= 4;
  782. #ifdef TMC_DEBUG1
  783. //SERIAL_PRINTF("Received ");
  784. //SERIAL_PRINTF("Received ", i_datagram,HEX);
  785. //SERIAL_ECHOPAIR("\n\nReceived \n", i_datagram);
  786. SERIAL_PRINTF("\n\nReceived %x", i_datagram);
  787. debugLastStatus();
  788. #endif
  789. //deselect the TMC chip
  790. digitalWrite(cs_pin,HIGH);
  791. //restore the previous SPI mode if neccessary
  792. //if the mode is not correct set it to mode 3
  793. //if (oldMode != SPI_MODE3) {
  794. // SPI.setDataMode(oldMode);
  795. //}
  796. //store the datagram as status result
  797. driver_status_result = i_datagram;
  798. }
  799. #endif // STM32F7