forked from grblHAL/RP2040
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmc_uart.c
91 lines (66 loc) · 2.39 KB
/
tmc_uart.c
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
/*
tmc_uart.c - driver code for RP2040 ARM processors
Part of grblHAL
Copyright (c) 2020-2022 Terje Io
Grbl is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Grbl is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
*/
#include "driver.h"
#if TRINAMIC_UART_ENABLE
#include <string.h>
#include "serial.h"
#include "grbl/protocol.h"
static io_stream_t tmc_uart;
TMC_uart_write_datagram_t *tmc_uart_read (trinamic_motor_t driver, TMC_uart_read_datagram_t *dgr)
{
static TMC_uart_write_datagram_t wdgr = {0};
volatile uint32_t dly = 50, ms = hal.get_elapsed_ticks();
// tmc_uart.reset_write_buffer();
tmc_uart.write_n((char *)dgr->data, sizeof(TMC_uart_read_datagram_t));
while(tmc_uart.get_tx_buffer_count());
while(--dly)
protocol_execute_noop(0);
tmc_uart.disable_rx(false);
tmc_uart.reset_read_buffer();
// Wait for response with 2ms timeout
while(tmc_uart.get_rx_buffer_count() < 8) {
if(hal.get_elapsed_ticks() - ms >= 3)
break;
}
if((tmc_uart.get_rx_buffer_count()) >= 8) {
wdgr.data[0] = tmc_uart.read();
wdgr.data[1] = tmc_uart.read();
wdgr.data[2] = tmc_uart.read();
wdgr.data[3] = tmc_uart.read();
wdgr.data[4] = tmc_uart.read();
wdgr.data[5] = tmc_uart.read();
wdgr.data[6] = tmc_uart.read();
wdgr.data[7] = tmc_uart.read();
} else
wdgr.msg.addr.value = 0xFF;
tmc_uart.disable_rx(true);
dly = 8000;
while(--dly)
protocol_execute_noop(0);
return &wdgr;
}
void tmc_uart_write (trinamic_motor_t driver, TMC_uart_write_datagram_t *dgr)
{
tmc_uart.write_n((char *)dgr->data, sizeof(TMC_uart_write_datagram_t));
while(tmc_uart.get_tx_buffer_count());
}
void tmc_uart_init (void)
{
memcpy(&tmc_uart, serial2Init(230400), sizeof(io_stream_t));
tmc_uart.disable_rx(true);
tmc_uart.set_enqueue_rt_handler(stream_buffer_all);
}
#endif