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 34KB

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