-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial.c
171 lines (131 loc) · 4.81 KB
/
serial.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
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
/*
serial.c - An embedded CNC Controller with rs274/ngc (g-code) support
Serial driver for Cypress PSoC 5 (CY8CKIT-059)
Part of grblHAL
Copyright (c) 2017-2024 Terje Io
grblHAL 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.
grblHAL 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 grblHAL. If not, see <http://www.gnu.org/licenses/>.
*/
#include "project.h"
#include "serial.h"
#include "grbl/hal.h"
#include "grbl/protocol.h"
static void uart_rx_interrupt_handler (void);
static stream_rx_buffer_t rxbuffer = {0};
static enqueue_realtime_command_ptr enqueue_realtime_command = protocol_enqueue_realtime_command;
static int16_t serialGetC (void)
{
int16_t data;
uint32_t bptr = rxbuffer.tail;
if(bptr == rxbuffer.head) // If buffer empty
return SERIAL_NO_DATA; // return no data available
// UARTIntDisable(UARTCH, UART_INT_RX|UART_INT_RT);
data = rxbuffer.data[bptr++]; // Get next character, increment tmp pointer
rxbuffer.tail = bptr & (RX_BUFFER_SIZE - 1); // and update pointer
// UARTIntEnable(UARTCH, UART_INT_RX|UART_INT_RT);
// if (rts_state && BUFCOUNT(rx_head, rx_tail, RX_BUFFER_SIZE) < RX_BUFFER_LWM) // Clear RTS if
// GPIOPinWrite(RTS_PORT, RTS_PIN, rts_state = 0); // buffer count is below low water mark
return data;
}
static bool serialPutC (const char c)
{
UART_PutChar(c);
return true;
}
static bool serialSuspendInput (bool suspend)
{
return stream_rx_suspend(&rxbuffer, suspend);
}
static inline uint16_t serialRxCount(void)
{
uint16_t head = rxbuffer.head, tail = rxbuffer.tail;
return BUFCOUNT(head, tail, RX_BUFFER_SIZE);
}
static uint16_t serialRxFree(void)
{
return RX_BUFFER_SIZE - serialRxCount();
}
static void serialRxFlush(void)
{
rxbuffer.tail = rxbuffer.head;
UART_ClearRxBuffer(); // clear FIFO too
// GPIOPinWrite(RTS_PORT, RTS_PIN, rts_state = 0);
}
static void serialRxCancel(void)
{
rxbuffer.data[rxbuffer.head] = CMD_RESET;
rxbuffer.tail = rxbuffer.head;
rxbuffer.head = (rxbuffer.tail + 1) & (RX_BUFFER_SIZE - 1);
UART_ClearRxBuffer(); // clear FIFO too
// GPIOPinWrite(RTS_PORT, RTS_PIN, rts_state = 0);
}
static void serialWriteS(const char *data)
{
char c, *ptr = (char *)data;
while((c = *ptr++) != '\0')
serialPutC(c);
}
static void serialWrite(const char *data, uint16_t length)
{
char *ptr = (char *)data;
while(length--)
serialPutC(*ptr++);
}
static bool serialEnqueueRtCommand (char c)
{
return enqueue_realtime_command(c);
}
static enqueue_realtime_command_ptr serialSetRtHandler (enqueue_realtime_command_ptr handler)
{
enqueue_realtime_command_ptr prev = enqueue_realtime_command;
if(handler)
enqueue_realtime_command = handler;
return prev;
}
const io_stream_t *serialInit (void)
{
static const io_stream_t stream = {
.type = StreamType_Serial,
.is_connected = stream_connected,
.read = serialGetC,
.write = serialWriteS,
.write_char = serialPutC,
.write_all = serialWriteS,
.write_n = serialWrite,
.enqueue_rt_command = serialEnqueueRtCommand,
.get_rx_buffer_free = serialRxFree,
.get_rx_buffer_count = serialRxCount,
.reset_read_buffer = serialRxFlush,
.cancel_read_buffer = serialRxCancel,
.suspend_read = serialSuspendInput,
.set_enqueue_rt_handler = serialSetRtHandler
};
UART_Start();
UART_RX_Interrupt_StartEx(uart_rx_interrupt_handler);
return &stream;
}
static void uart_rx_interrupt_handler (void)
{
uint8_t data;
while((data = UART_GetChar())) { // UART_GetChar() returns 0 if no data
if(!enqueue_realtime_command((char)data)) {
uint32_t bptr = (rxbuffer.head + 1) & (RX_BUFFER_SIZE - 1); // Get next head pointer
if(bptr == rxbuffer.tail) // If buffer full
rxbuffer.overflow = 1; // flag overflow,
else {
rxbuffer.data[rxbuffer.head] = (char)data; // else add data to buffer
rxbuffer.head = bptr; // and update pointer
}
}
} // loop until FIFO empty
// if (!rts_state && BUFCOUNT(rx_head, rx_tail, RX_BUFFER_SIZE) >= RX_BUFFER_HWM)
// GPIOPinWrite(RTS_PORT, RTS_PIN, rts_state = RTS_PIN);
}