User manual

TMCM-1640 TMCL Firmware V2.00 Manual (Rev. 2.00 / 2012-JUL-31) 32
www.trinamic.com
4.6.18 CSUB (call subroutine) and RSUB (return from subroutine)
For implementing subroutines there are two commands:
- CSUB calls a subroutine in the TMCL program memory. It is intended for standalone operation, only.
The host address and the reply are required to take the instruction to the TMCL program memory
while the TMCL program downloads. This command cannot be used in direct mode.
- RSUB is used for returning from a subroutine to the next command behind the CSUB command.
4.6.18.1 CSUB (call subroutine)
Internal function: The actual TMCL program counter value is saved to an internal stack, afterwards
overwritten with the passed value. The number of entries in the internal stack is limited to 8. This also
limits nesting of subroutine calls to 8. The command will be ignored if there is no more stack space left.
Related commands: RSUB, JA
Mnemonic: CSUB <Label>
Binary representation:
COMMAND
TYPE
MOT/BANK
VALUE
23
don’t care
don’t care
<subroutine address>
Binary format of the CSUB SubW command assuming that the label SubW is at address 100:
Byte Index
0
1
2
3
4
5
6
7
Function
Target-
address
Instruction
Number
Type
Motor/
Bank
Operand
Byte3
Operand
Byte2
Operand
Byte1
Operand
Byte0
Value (hex)
$01
$17
$00
$00
$00
$00
$00
$64
4.6.18.2 RSUB (return from subroutine)
Internal function: The TMCL program counter is set to the last value of the stack. The command will be
ignored if the stack is empty.
Related command: CSUB
Mnemonic: RSUB
Binary representation:
COMMAND
TYPE
MOT/BANK
VALUE
24
don’t care
don’t care
don’t care
Binary format of RSUB:
Byte Index
0
1
2
3
4
5
6
7
Function
Target-
address
Instruction
Number
Type
Motor/
Bank
Operand
Byte3
Operand
Byte2
Operand
Byte1
Operand
Byte0
Value (hex)
$01
$18
$00
$00
$00
$00
$00
$00
Example: Call a subroutine
Loop: MVP ABS, 0, 10000
CSUB SubW //Save program counter and jump to label SubW (see below)
MVP ABS, 0, 0
JA Loop
SubW: WAIT POS, 0, 0
WAIT TICKS, 0, 50
RSUB //Continue with the command following the CSUB command (in this
example: MVP ABS).