# Reading and writing TMC2300 registers

Firmware has implemented functionality to read and write individual Trinamic TMC2300 registers over internal UART single wire interface. It shares transmit and receive line like an RS485 based interface. Data transmission is secured using a cyclic redundancy check and each TMC2300 driver is addressed individually as follows:

<table border="1" id="bkmrk-axis-address-x-0-y-1" style="border-collapse: collapse; width: 48.0248%; height: 145px;"><tbody><tr style="height: 29px;"><td class="align-center" style="width: 53.2003%; height: 29px;">**Axis**</td><td class="align-center" style="width: 74.2446%; height: 29px;">**Address**</td></tr><tr style="height: 29px;"><td class="align-center" style="width: 53.2003%; height: 29px;">X</td><td class="align-center" style="width: 74.2446%; height: 29px;">0</td></tr><tr style="height: 29px;"><td class="align-center" style="width: 53.2003%; height: 29px;">Y</td><td class="align-center" style="width: 74.2446%; height: 29px;">1</td></tr><tr style="height: 29px;"><td class="align-center" style="width: 53.2003%; height: 29px;">Z</td><td class="align-center" style="width: 74.2446%; height: 29px;">2</td></tr><tr style="height: 29px;"><td class="align-center" style="width: 53.2003%; height: 29px;">A</td><td class="align-center" style="width: 74.2446%; height: 29px;">3</td></tr><tr><td class="align-center" style="width: 53.2003%;">Broadcast to all axes</td><td class="align-center" style="width: 74.2446%;">**9**</td></tr></tbody></table>

<p class="callout info">Official TMC2300 [documentation](https://www.trinamic.com/products/integrated-circuits/details/tmc2300-la/)</p>

<p class="callout warning">All numbers in g-code arguments are decimal base numbers</p>

#### Write registers

Write register command forwards g-command arguments to TMC2300 configuration interface. While it is not recommended because of stability issues, write command can be sent even if motors are turning (G0, G1, ... commands)

<p class="callout warning">7'th bit of TMC2300 register has to be set to 1, thus for example **0x10** IHOLD\_IRUN becomes **0x90** </p>

<table border="1" id="bkmrk-g100-p%3Cxx%3E-l%3Cxx%3E-n%3Cx" style="border-collapse: collapse; width: 100%; height: 58px;"><tbody><tr style="height: 29px;"><td class="align-center" style="width: 14.2857%; height: 29px;">**G100**</td><td class="align-center" style="width: 14.2857%; height: 29px;">**P**&lt;xx&gt;</td><td class="align-center" style="width: 14.2857%; height: 29px;">**L**&lt;xx&gt;</td><td class="align-center" style="width: 14.2857%; height: 29px;">**N**&lt;xx&gt;</td><td class="align-center" style="width: 14.2857%; height: 29px;">**S**&lt;xx&gt;</td><td class="align-center" style="width: 14.2857%; height: 29px;">**F**&lt;xx&gt;</td><td class="align-center" style="width: 14.2857%; height: 29px;">**R**&lt;xx&gt;</td></tr><tr style="height: 29px;"><td style="width: 14.2857%; height: 29px;">G-command</td><td style="width: 14.2857%; height: 29px;">TMC2300 address</td><td style="width: 14.2857%; height: 29px;">TMC2300 register (mask with 0x80)</td><td style="width: 14.2857%; height: 29px;">DATA3</td><td style="width: 14.2857%; height: 29px;">DATA2</td><td style="width: 14.2857%; height: 29px;">DATA1</td><td style="width: 14.2857%; height: 29px;">DATA0</td></tr></tbody></table>

[![tmc2300_write2.png](https://wiki.kurokesu.com/uploads/images/gallery/2020-09/scaled-1680-/tmc2300_write2.png)](https://wiki.kurokesu.com/uploads/images/gallery/2020-09/tmc2300_write2.png)

<p class="callout info">Required checksum is calculated in firmware</p>

#### Read registers

Read registers command has two arguments TMC2300 address and register address. It responds to terminal immediately.

<table border="1" id="bkmrk-g101-p%3Cxx%3E-r%3Cxx%3E-g-c" style="border-collapse: collapse; width: 100%;"><tbody><tr><td class="align-center" style="width: 33.3333%;">**G101**</td><td class="align-center" style="width: 33.3333%;">**P**&lt;xx&gt;</td><td class="align-center" style="width: 33.3333%;">**R**&lt;xx&gt;</td></tr><tr><td style="width: 33.3333%;">G-command</td><td style="width: 33.3333%;">TMC2300 address</td><td style="width: 33.3333%;">TMC2300 register</td></tr></tbody></table>

Response is decimal coded 12 bytes. Sample reply looks like this: `[TMC:5,0,16,112,5,255,16,0,0,0,0,157]`

<table border="1" id="bkmrk-reply-byte-meaning-0" style="border-collapse: collapse; width: 53.8257%; height: 377px;"><tbody><tr style="height: 29px;"><td class="align-center" style="width: 21.6799%; height: 29px;">**Reply byte**</td><td class="align-center" style="width: 69.3563%; height: 29px;">**Meaning**</td></tr><tr style="height: 29px;"><td style="width: 21.6799%; height: 29px;">0</td><td style="width: 69.3563%; height: 29px;">Echo transmit byte0</td></tr><tr style="height: 29px;"><td style="width: 21.6799%; height: 29px;">1</td><td style="width: 69.3563%; height: 29px;">Echo transmit byte0</td></tr><tr style="height: 29px;"><td style="width: 21.6799%; height: 29px;">2</td><td style="width: 69.3563%; height: 29px;">Echo transmit byte0</td></tr><tr style="height: 29px;"><td style="width: 21.6799%; height: 29px;">3</td><td style="width: 69.3563%; height: 29px;">Echo transmit byte0</td></tr><tr style="height: 29px;"><td style="width: 21.6799%; height: 29px;">4</td><td style="width: 69.3563%; height: 29px;">Reply from TMC2300 sync (0x05)</td></tr><tr style="height: 29px;"><td style="width: 21.6799%; height: 29px;">5</td><td style="width: 69.3563%; height: 29px;">Address</td></tr><tr style="height: 29px;"><td style="width: 21.6799%; height: 29px;">6</td><td style="width: 69.3563%; height: 29px;">Register</td></tr><tr style="height: 29px;"><td style="width: 21.6799%; height: 29px;">7</td><td style="width: 69.3563%; height: 29px;">Data byte3</td></tr><tr style="height: 29px;"><td style="width: 21.6799%; height: 29px;">8</td><td style="width: 69.3563%; height: 29px;">Data byte2</td></tr><tr style="height: 29px;"><td style="width: 21.6799%; height: 29px;">9</td><td style="width: 69.3563%; height: 29px;">Data byte1</td></tr><tr style="height: 29px;"><td style="width: 21.6799%; height: 29px;">10</td><td style="width: 69.3563%; height: 29px;">Data byte0</td></tr><tr style="height: 29px;"><td style="width: 21.6799%; height: 29px;">11</td><td style="width: 69.3563%; height: 29px;">Checksum</td></tr></tbody></table>

[![tmc2300_read2.png](https://wiki.kurokesu.com/uploads/images/gallery/2020-09/scaled-1680-/tmc2300_read2.png)](https://wiki.kurokesu.com/uploads/images/gallery/2020-09/tmc2300_read2.png)

#### Examples

##### Set motor X current

Each motor driver can drive different motor. Power and other settings can be set individually. For example IHOLD\_IRUN (0x10) register controls motor idle, run and timing parameters.

`G100 P0 L144 N0 S0 F10 R5`

<table border="1" id="bkmrk-g100-write-to-tmc230" style="border-collapse: collapse; width: 62.9634%;"><tbody><tr><td style="width: 13.8272%;">G100</td><td style="width: 60.127%;">Write to TMC2300 g-command</td></tr><tr><td style="width: 13.8272%;">P0</td><td style="width: 60.127%;">Driver address = 0 (X axis)

</td></tr><tr><td style="width: 13.8272%;">L144</td><td style="width: 60.127%;">TMC2300 register IHOLD\_IRUN (0x10)

**0x10** -&gt;(set 7'th bit to 1)-&gt; **0x90** = **144** (dec)

</td></tr><tr><td style="width: 13.8272%;">N0</td><td style="width: 60.127%;">Data3 = 0 (not used)</td></tr><tr><td style="width: 13.8272%;">S0</td><td style="width: 60.127%;">Data2 = 0 (see **IHOLDDELAY** in table below)</td></tr><tr><td style="width: 13.8272%;">F10

</td><td style="width: 60.127%;">Data1 = 10 (see **IRUN** in table below)</td></tr><tr><td style="width: 13.8272%;">R5</td><td style="width: 60.127%;">Data0 = 5 (see **IHOLD** in table below)</td></tr></tbody></table>

[![tmc2300_pwr_registers2.png](https://wiki.kurokesu.com/uploads/images/gallery/2020-09/scaled-1680-/tmc2300_pwr_registers2.png)](https://wiki.kurokesu.com/uploads/images/gallery/2020-09/tmc2300_pwr_registers2.png)

##### Read input register

Each TMC2300 driver has input status registers GSTAT (0x01) indicating over temperature, short circuit, and more.

`G101 P0 R1`

Response looks like:

`[TMC:5,0,1,193,5,255,1,0,0,0,<strong>1</strong>,154]`

Response contains only 3 bits of useful information. See explanation in table below.

[![tmc2300_status.png](https://wiki.kurokesu.com/uploads/images/gallery/2020-09/scaled-1680-/tmc2300_status.png)](https://wiki.kurokesu.com/uploads/images/gallery/2020-09/tmc2300_status.png)