1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
#![no_std]
#![feature(generic_associated_types)]
#![feature(type_alias_impl_trait)]
#![feature(min_type_alias_impl_trait)]
use core::convert::TryInto;
use core::future::Future;
use core::mem;
struct TMCHelpers {}
impl TMCHelpers {
pub fn field_get(data: u32, mask: u32, shift: u32) -> u32 {
(data & mask) >> shift
}
pub fn field_set(data: u32, mask: u32, shift: u32, value: u32) -> u32 {
(data & (!mask)) | ((value << shift) & mask)
}
pub fn to_signed_32(x: u32) -> i32 {
let m = x & 0xffffffff;
((m ^ 0x80000000) - 0x80000000).try_into().unwrap()
}
}
struct TMCL {}
impl TMCL {
// @staticmethod
// def validate_host_id(host_id):
// if(not(type(host_id) == int)):
// raise TypeError
// if(not(0 <= host_id < 256)):
// raise ValueError("Incorrect Host ID value. Must be between 0 and 255 inclusively.")
//
// @staticmethod
// def validate_module_id(module_id):
// if(not(type(module_id) == int)):
// raise TypeError
// if(not(0 <= module_id < 256)):
// raise ValueError("Incorrect Module ID value. Must be between 0 and 255 inclusively.")
fn calculate_checksum(data: [u8; 8]) -> u8 {
0
}
// @staticmethod
// def calculate_checksum(data):
// checksum = 0
// for d in data:
// checksum += d
// checksum %= 256
// return checksum
}
pub trait TMCLConnnection {
type SendFuture<'a>: Future<Output = ()>;
type ReceiveFuture<'a>: Future<Output = [u8; 8]>;
fn _send<'a>(&'a mut self, host_id: u16, module_id: u16, data: [u8; 8])
-> Self::SendFuture<'a>;
fn _recv<'a>(&'a mut self, host_id: u16, module_id: u16) -> Self::ReceiveFuture<'a>;
}
pub struct TMCLInterface<T: TMCLConnnection> {
_MODULE_ID: u16,
_HOST_ID: u16,
connection: T,
}
impl<T: TMCLConnnection> TMCLInterface<T> {
pub fn new(connection: T, host_id: Option<u16>, module_id: Option<u16>) -> Self {
let host_id = match host_id {
Some(host_id) => host_id,
None => 2,
};
let module_id = match module_id {
Some(module_id) => module_id,
None => 1,
};
TMCLInterface {
connection: connection,
_MODULE_ID: module_id,
_HOST_ID: host_id,
}
}
// """
// This class is a base class for sending TMCL commands over a communication
// interface.
// Each instance of this class represents one TMCL host. The bus connection for
// the TMCL communication is represented by a subclass inheriting this base
// class. An application with multiple busses should therefore use subclasses
// for all types of busses (e.g. one USB TMCL interface and one serial TMCL
// interface) and create exactly one instance of one of those subclasses per
// bus.
// A subclass is required to override the following functions:
// _send(self, hostID, module_id, data)
// _recv(self, hostID, module_id)
// A subclass may use the boolean _debug attribute to toggle printing further
// debug output.
// A subclass may read the _HOST_ID and _MODULE_ID parameters.
// """
//
// pub fn __init__(&mut self, hostID=2, defaultmodule_id=1, debug=False) {
// """
// Parameters:
// hostID:
// Type: int, optional, default value: 2
// The ID of the TMCL host. This ID is the same for each module
// when communicating with multiple modules.
// defaultmodule_id:
// Type: int, optional, default value: 1
// The default module ID to use when no ID is given to any of the
// tmcl_interface functions. When only communicating with one
// module a script can omit the module_id for all TMCL interface
// calls by declaring this default value once at the start.
// debug:
// Type: bool, optional, default: False
// A switch for enabling debug mode. Can be changed with
// enableDebug(). In debug mode all sent and received TMCL packets
// get dumped to stdout. The boolean _debug attribute holds the
// current state of debug mode - subclasses may read it to print
// further debug output.
// """
//
// TMCL.validate_host_id(hostID)
// TMCL.validate_module_id(defaultmodule_id)
//
// if not type(debug) == bool:
// raise TypeError
//
// self._HOST_ID = hostID
// self._MODULE_ID = defaultmodule_id
// self._debug = debug
//
// pub fn enableDebug(&mut self, enable) {
// """
// Set the debug mode, which dumps all TMCL datagrams written and read.
// """
// if type(enable) != bool:
// raise TypeError("Expected boolean value")
//
// self._debug = enable
// }
//
//
// pub fn send_request(&mut self, request, module_id=None) {
// """
// Send a TMCL_Request and read back a TMCL_Reply. This function blocks until
// the reply has been received.
// """
//
// if not module_id:
// module_id = self._MODULE_ID
//
// if self._debug:
// request.dump()
//
// self._send(self._HOST_ID, module_id, request.toBuffer())
// reply = TMCL_Reply.from_buffer(self._recv(self._HOST_ID, module_id))
//
// if self._debug:
// reply.dump()
//
// return reply
// }
// Send a TMCL datagram and read back a reply. This function blocks until
// the reply has been received.
async fn send(
&mut self,
opcode: TMCLCommand,
op_type: u8,
motor: u8,
value: i32,
module_id: Option<u16>,
) -> TMCLReply {
// If no module ID is given, use the default one
let module_id = match module_id {
None => self._MODULE_ID,
Some(module_id) => module_id,
};
let request = TMCLRequest::new(module_id as u8, opcode, op_type, motor, value, None);
self.connection
._send(self._HOST_ID, module_id, request.to_buffer())
.await;
TMCLReply::from_buffer(self.connection._recv(self._HOST_ID, module_id).await)
}
// Send the command for entering bootloader mode. This TMCL command does
// sresult in a reply.
// async fn sendBoot(&mut self, module_id: Option<u16>) {
// // # If no module ID is given, use the default one
// let module_id = match module_id {
// None => self._MODULE_ID,
// Some(module_id) => module_id,
// };
//
// let request = TMCLRequest::new(
// module_id as u8,
// TMCLCommand::BOOT,
// 0x81,
// 0x92,
// 0xA3B4C5D6,
// None,
// );
//
// // # Send the request
// self.connection
// ._send(self._HOST_ID, module_id, request.to_buffer())
// .await
// }
// pub fn getVersionString(&mut self, module_id=None) {
// """
// Request the ASCII version string.
// """
// reply = self.send(TMCLCommand.GET_FIRMWARE_VERSION, 0, 0, 0, module_id)
//
// return reply.versionString()
//
// }
// # General parameter access functions
async fn parameter(
&mut self,
p_command: TMCLCommand,
p_type: u8,
p_axis: u8,
p_value: i32,
module_id: Option<u16>,
signed: bool,
) -> i32 {
let mut value = self
.send(p_command, p_type, p_axis, p_value, module_id)
.await
.value as i32;
if signed {
value = TMCHelpers::to_signed_32(value as u32);
}
value
}
async fn set_parameter(
&mut self,
p_command: TMCLCommand,
p_type: u8,
p_axis: u8,
p_value: i32,
module_id: Option<u16>,
) -> TMCLReply {
self.send(p_command, p_type, p_axis, p_value, module_id)
.await
}
// Axis parameter access functions
async fn axis_parameter(
&mut self,
command_type: u8,
axis: u8,
module_id: Option<u16>,
signed: bool,
) -> i32 {
let mut value = self
.send(TMCLCommand::GAP, command_type, axis, 0, module_id)
.await
.value as i32;
if signed {
value = TMCHelpers::to_signed_32(value as u32);
}
value
}
async fn set_axis_parameter(
&mut self,
command_type: u8,
axis: u8,
value: i32,
module_id: Option<u16>,
) -> TMCLReply {
self.send(TMCLCommand::SAP, command_type, axis, value, module_id)
.await
}
async fn store_axis_parameter(
&mut self,
command_type: u8,
axis: u8,
module_id: Option<u16>,
) -> TMCLReply {
self.send(TMCLCommand::STAP, command_type, axis, 0, module_id)
.await
}
async fn set_and_store_axis_parameter(
&mut self,
command_type: u8,
axis: u8,
value: i32,
module_id: Option<u16>,
) {
self.send(TMCLCommand::SAP, command_type, axis, value, module_id)
.await;
self.send(TMCLCommand::STAP, command_type, axis, 0, module_id)
.await;
}
// # Global parameter access functions
async fn global_parameter(
&mut self,
command_type: u8,
bank: u8,
module_id: Option<u16>,
signed: bool,
) -> i32 {
let mut value = self
.send(TMCLCommand::GGP, command_type, bank, 0, module_id)
.await
.value as i32;
if signed {
value = TMCHelpers::to_signed_32(value as u32);
}
value
}
async fn set_global_parameter(
&mut self,
command_type: u8,
bank: u8,
value: i32,
module_id: Option<u16>,
) {
self.send(TMCLCommand::SGP, command_type, bank, value, module_id)
.await;
}
async fn store_global_parameter(&mut self, command_type: u8, bank: u8, module_id: Option<u16>) {
self.send(TMCLCommand::STGP, command_type, bank, 0, module_id)
.await;
}
async fn set_and_store_global_parameter(
&mut self,
command_type: u8,
bank: u8,
value: i32,
module_id: Option<u16>,
) {
self.send(TMCLCommand::SGP, command_type, bank, value, module_id)
.await;
self.send(TMCLCommand::STGP, command_type, bank, 0, module_id)
.await;
}
// # Register access functions
async fn write_mc(
&mut self,
register_address: u8,
value: i32,
module_id: Option<u16>,
) -> TMCLReply {
self.write_register(register_address, TMCLCommand::WRITE_MC, 0, value, module_id)
.await
}
async fn read_mc(&mut self, register_address: u8, module_id: Option<u16>, signed: bool) -> i32 {
self.read_register(register_address, TMCLCommand::READ_MC, 0, module_id, signed)
.await
}
async fn write_drv(
&mut self,
register_address: u8,
value: i32,
module_id: Option<u16>,
) -> TMCLReply {
// return self.writeRegister(registerAddress, TMCLCommand.WRITE_DRV, 1, value, module_id);
self.write_register(
register_address,
TMCLCommand::WRITE_DRV,
1,
value,
module_id,
)
.await
}
async fn read_drv(
&mut self,
register_address: u8,
module_id: Option<u16>,
signed: bool,
) -> i32 {
self.read_register(
register_address,
TMCLCommand::READ_DRV,
1,
module_id,
signed,
)
.await
}
async fn read_register(
&mut self,
register_address: u8,
command: TMCLCommand,
channel: u8,
module_id: Option<u16>,
signed: bool,
) -> i32 {
let mut value = self
.send(command, register_address, channel, 0, module_id)
.await
.value as i32;
if signed {
value = TMCHelpers::to_signed_32(value as u32);
}
value
}
//
async fn write_register(
&mut self,
register_address: u8,
command: TMCLCommand,
channel: u8,
value: i32,
module_id: Option<u16>,
) -> TMCLReply {
self.send(command, register_address, channel, value, module_id)
.await
}
// # Motion control functions
async fn rotate(&mut self, motor: u8, velocity: i32, module_id: Option<u16>) -> TMCLReply {
self.send(TMCLCommand::ROR, 0, motor, velocity, module_id)
.await
}
async fn stop(&mut self, motor: u8, module_id: Option<u16>) -> TMCLReply {
self.send(TMCLCommand::MST, 0, motor, 0, module_id).await
}
async fn mv(
&mut self,
move_type: u8,
motor: u8,
position: i32,
module_id: Option<u16>,
) -> TMCLReply {
self.send(TMCLCommand::MVP, move_type, motor, position, module_id)
.await
}
/*
Use the TMCL MVP command to perform an absolute movement.
Returns the value of the reply. Refer to the documentation of your
specific module for details on what is returned.
*/
async fn move_to(&mut self, motor: u8, position: i32, module_id: Option<u16>) -> u32 {
self.mv(0, motor, position, module_id).await.value
}
//
/*
Use the TMCL MVP command to perform a relative movement.
Returns the value of the reply. Refer to the documentation of your
specific module for details on what is returned.
*/
async fn move_by(&mut self, motor: u8, distance: i32, module_id: Option<u16>) -> u32 {
self.mv(1, motor, distance, module_id).await.value
}
//
// # IO pin functions
async fn analog_input(&mut self, x: u8, module_id: Option<u16>) -> u32 {
self.send(TMCLCommand::GIO, x, 1, 0, module_id).await.value
}
//
async fn digital_input(&mut self, x: u8, module_id: Option<u16>) -> u32 {
self.send(TMCLCommand::GIO, x, 0, 0, module_id).await.value
}
//
async fn digital_output(&mut self, x: u8, module_id: Option<u16>) -> u32 {
self.send(TMCLCommand::GIO, x, 2, 0, module_id).await.value
}
//
async fn set_digital_output(&mut self, x: u8, module_id: Option<u16>) -> u32 {
self.send(TMCLCommand::SIO, x, 2, 1, module_id).await.value
}
//
async fn clear_digital_output(&mut self, x: u8, module_id: Option<u16>) -> u32 {
self.send(TMCLCommand::SIO, x, 2, 0, module_id).await.value
}
//
// " testing new interface usage (ED) => "
// # axis parameter access functions
async fn axis_parameter_raw(
&mut self,
module_id: Option<u16>,
axis: u8,
command_type: u8,
) -> u32 {
self.send(TMCLCommand::GAP, command_type, axis, 0, module_id)
.await
.value
}
//
async fn set_axis_parameter_raw(
&mut self,
module_id: Option<u16>,
axis: u8,
command_type: u8,
value: i32,
) -> TMCLReply {
self.send(TMCLCommand::SAP, command_type, axis, value, module_id)
.await
}
//
// # global parameter access functions
async fn global_parameter_raw(
&mut self,
module_id: Option<u16>,
bank: u8,
command_type: u8,
) -> u32 {
self.send(TMCLCommand::GGP, command_type, bank, 0, module_id)
.await
.value
}
//
async fn set_global_parameter_raw(
&mut self,
module_id: Option<u16>,
bank: u8,
command_type: u8,
value: i32,
) -> TMCLReply {
self.send(TMCLCommand::SGP, command_type, bank, value, module_id)
.await
}
//
// # IO pin functions
async fn analog_input_raw(&mut self, module_id: Option<u16>, x: u8) -> u32 {
self.send(TMCLCommand::GIO, x, 1, 0, module_id).await.value
}
//
async fn digital_input_raw(&mut self, module_id: Option<u16>, x: u8) -> u32 {
self.send(TMCLCommand::GIO, x, 0, 0, module_id).await.value
}
//
async fn digital_output_raw(&mut self, module_id: Option<u16>, x: u8) -> u32 {
self.send(TMCLCommand::GIO, x, 2, 0, module_id).await.value
}
//
async fn set_digital_output_raw(&mut self, module_id: Option<u16>, x: u8) -> u32 {
self.send(TMCLCommand::SIO, x, 2, 1, module_id).await.value
}
//
async fn clear_digital_output_raw(&mut self, module_id: Option<u16>, x: u8) -> u32 {
self.send(TMCLCommand::SIO, x, 2, 0, module_id).await.value
}
//
// " <= testing new interface usage (ED) "
}
#[allow(non_camel_case_types)]
pub enum TMCLCommand {
ROR = 1,
ROL = 2,
MST = 3,
MVP = 4,
SAP = 5,
GAP = 6,
STAP = 7,
RSAP = 8,
SGP = 9,
GGP = 10,
STGP = 11,
RSGP = 12,
RFS = 13,
SIO = 14,
GIO = 15,
CALC = 19,
COMP = 20,
JC = 21,
JA = 22,
CSUB = 23,
RSUB = 24,
WAIT = 27,
STOP = 28,
SAC = 29,
SCO = 30,
GCO = 31,
CCO = 32,
CALCX = 33,
AAP = 34,
AGP = 35,
CLE = 36,
TMCL_UF0 = 64,
TMCL_UF1 = 65,
TMCL_UF2 = 66,
TMCL_UF3 = 67,
TMCL_UF4 = 68,
TMCL_UF5 = 69,
TMCL_UF6 = 70,
TMCL_UF7 = 71,
STOP_APPLICATION = 128,
RUN_APPLICATION = 129,
STEP_APPLICATION = 130,
RESET_APPLICATION = 131,
START_DOWNLOAD_MODE = 132,
QUIT_DOWNLOAD_MODE = 133,
READ_TMCL_MEMORY = 134,
GET_APPLICATION_STATUS = 135,
GET_FIRMWARE_VERSION = 136,
RESTORE_FACTORY_SETTINGS = 137,
ASSIGNMENT = 143,
WRITE_MC = 146,
WRITE_DRV = 147,
READ_MC = 148,
READ_DRV = 149,
BOOT_ERASE_ALL = 200,
BOOT_WRITE_BUFFER = 201,
BOOT_WRITE_PAGE = 202,
BOOT_GET_CHECKSUM = 203,
BOOT_READ_MEMORY = 204,
BOOT_START_APPL = 205,
BOOT_GET_INFO = 206,
BOOT_WRITE_LENGTH = 208,
BOOT = 242,
}
pub struct TMCLRequest {
module_address: u8,
command: u8,
command_type: u8,
motor_bank: u8,
value: i32,
checksum: u8,
}
impl TMCLRequest {
pub fn new(
address: u8,
command: TMCLCommand,
command_type: u8,
motor_bank: u8,
value: i32,
checksum: Option<u8>,
) -> Self {
let mut request = TMCLRequest {
module_address: address & 0xFF,
command: command as u8 & 0xFF,
command_type: command_type & 0xFF,
motor_bank: motor_bank & 0xFF,
value: value,
checksum: 0,
};
request.checksum = match checksum {
Some(checksum) => checksum,
None => TMCL::calculate_checksum(request.to_buffer()),
};
request
}
pub fn to_buffer(&self) -> [u8; 8] {
[
self.module_address,
self.command,
self.command_type,
self.motor_bank,
((self.value >> (8 * 3)) & 0xff) as u8,
((self.value >> (8 * 2)) & 0xff) as u8,
((self.value >> (8 * 1)) & 0xff) as u8,
((self.value >> (8 * 0)) & 0xff) as u8,
]
}
}
pub struct TMCLReply {
reply_address: u8,
module_address: u8,
status: u8,
command: u8,
value: u32,
checksum: u8,
special: bool,
}
impl TMCLReply {
pub fn new(
reply_address: u8,
module_address: u8,
status: u8,
command: TMCLCommand,
value: u32,
checksum: Option<u8>,
special: bool,
) -> Self {
TMCLReply {
reply_address: reply_address & 0xFF,
module_address: module_address & 0xFF,
status: status & 0xFF,
command: command as u8 & 0xFF,
value: value & 0xFFFFFFFF,
checksum: 0,
special: special,
}
}
pub fn from_buffer(data: [u8; 8]) -> Self {
let reply_address: u8 = data[0];
let module_address: u8 = data[1];
let status: u8 = data[2];
let command: TMCLCommand = unsafe { mem::transmute(data[3] as i8) };
let value: u32 = ((data[4] as u32) << (8 * 3))
+ ((data[5] as u32) << (8 * 2))
+ ((data[6] as u32) << (8 * 1))
+ ((data[7] as u32) << (8 * 0));
Self::new(
reply_address,
module_address,
status,
command,
value,
None,
false,
)
}
pub fn value(&self) -> u32 {
self.value
}
pub fn is_valid(self) -> bool {
self.status == 0
}
}
pub mod connections;
pub mod modules;