Skip to content

Commit 4417f0a

Browse files
committed
Updated for latest stream handling change.
1 parent 1456dd3 commit 4417f0a

File tree

3 files changed

+71
-63
lines changed

3 files changed

+71
-63
lines changed

driver.c

+2-10
Original file line numberDiff line numberDiff line change
@@ -671,10 +671,8 @@ bool driver_init (void)
671671
SYSTICK_TIMER_CCR0 = 1;
672672
SYSTICK_TIMER_CCTL0 |= CCIE;
673673

674-
serialInit();
675-
676674
hal.info = "MSP430F5529";
677-
hal.driver_version = "210626";
675+
hal.driver_version = "210716";
678676
hal.driver_setup = driver_setup;
679677
hal.f_step_timer = 24000000;
680678
hal.rx_buffer_size = RX_BUFFER_SIZE;
@@ -707,13 +705,7 @@ bool driver_init (void)
707705

708706
hal.control.get_state = systemGetState;
709707

710-
hal.stream.read = serialGetC;
711-
hal.stream.write = serialWriteS;
712-
hal.stream.write_all = serialWriteS;
713-
hal.stream.write_char = serialPutC;
714-
hal.stream.get_rx_buffer_free = serialRxFree;
715-
hal.stream.reset_read_buffer = serialRxFlush;
716-
hal.stream.cancel_read_buffer = serialRxCancel;
708+
memcpy(&hal.stream, serialInit(), sizeof(io_stream_t));
717709

718710
#ifdef EEPROM_ENABLE
719711
i2c_init();

serial.c

+64-40
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//
22
// serial.c - serial (UART) port library
33
//
4-
// v1.0 / 2019-06-03 / Io Engineering / Terje
4+
// v1.1 / 2021-07-15 / Io Engineering / Terje
55
//
66
//
77

88
/*
99
10-
Copyright (c) 2015-2019, Terje Io
10+
Copyright (c) 2015-2021, Terje Io
1111
All rights reserved.
1212
1313
Redistribution and use in source and binary forms, with or without modification,
@@ -43,11 +43,11 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4343

4444
#include "serial.h"
4545
#include "grbl/hal.h"
46-
47-
#define BUFCOUNT(head, tail, size) ((head >= tail) ? (head - tail) : (size - tail + head))
46+
#include "grbl/protocol.h"
4847

4948
static char txbuf[TX_BUFFER_SIZE];
5049
static char rxbuf[RX_BUFFER_SIZE];
50+
static enqueue_realtime_command_ptr enqueue_realtime_command = protocol_enqueue_realtime_command;
5151

5252
const char eol[] = "\r\n";
5353
static volatile uint16_t tx_head = 0, tx_tail = 0, rx_head = 0, rx_tail = 0, rx_overflow = 0;
@@ -56,63 +56,45 @@ static volatile uint16_t tx_head = 0, tx_tail = 0, rx_head = 0, rx_tail = 0, rx_
5656
static volatile unsigned int rx_off = XONOK;
5757
#endif
5858

59-
inline void setUCA1BR (uint16_t prescaler)
59+
static inline void setUCA1BR (uint16_t prescaler)
6060
{
6161
UCA1BR0 = prescaler & 0xFF; // LSB
6262
UCA1BR1 = prescaler >> 8; // MSB
6363
}
64-
65-
void serialInit (void)
66-
{
67-
UCA1CTL1 = UCSWRST;
68-
UCA1CTL1 |= UCSSEL_2; // Use SMCLK
69-
setUCA1BR(217); // Set baudrate to 115200 @ 25MHz SMCLK
70-
UCA1MCTL = 0; // Modulation UCBRSx=0, UCBRFx=0
71-
UCA1CTL0 = 0; // 8 bit, 1 stop bit, no parity
72-
SERIAL_SEL |= SERIAL_RXD|SERIAL_TXD;
73-
UCA1CTL1 &= ~UCSWRST; // Initialize USCI state machine
74-
UCA1IE |= UCRXIE; // Enable USCI_A0 RX interrupt
75-
76-
#ifndef XONXOFF
77-
SERIAL_RTS_PORT_DIR |= SERIAL_RTS_BIT; // Enable RTS pin
78-
SERIAL_RTS_PORT_OUT &= ~SERIAL_RTS_BIT; // and drive it low
79-
#endif
80-
81-
}
82-
83-
uint16_t serialTxCount (void)
64+
/*
65+
static uint16_t serialTxCount (void)
8466
{
8567
uint16_t tail = tx_tail;
8668
return BUFCOUNT(tx_head, tail, TX_BUFFER_SIZE);
8769
}
8870
89-
uint16_t serialRxCount (void)
71+
static uint16_t serialRxCount (void)
9072
{
9173
uint16_t tail = rx_tail, head = rx_head;
9274
return BUFCOUNT(head, tail, RX_BUFFER_SIZE);
9375
}
94-
95-
uint16_t serialRxFree (void)
76+
*/
77+
static uint16_t serialRxFree (void)
9678
{
9779
unsigned int tail = rx_tail, head = rx_head;
9880
return RX_BUFFER_SIZE - BUFCOUNT(head, tail, RX_BUFFER_SIZE);
9981
}
10082

101-
void serialRxFlush (void)
83+
static void serialRxFlush (void)
10284
{
10385
rx_head = rx_tail = 0;
10486
SERIAL_RTS_PORT_OUT &= ~SERIAL_RTS_BIT;
10587
}
10688

107-
void serialRxCancel (void)
89+
static void serialRxCancel (void)
10890
{
10991
rxbuf[rx_head] = ASCII_CAN;
11092
rx_tail = rx_head;
11193
rx_head = (rx_tail + 1) & (RX_BUFFER_SIZE - 1);
11294
SERIAL_RTS_PORT_OUT &= ~SERIAL_RTS_BIT;;
11395
}
11496

115-
bool serialPutC(const char data)
97+
static bool serialPutC (const char data)
11698
{
11799
unsigned int next_head = tx_head;
118100

@@ -134,8 +116,8 @@ bool serialPutC(const char data)
134116
return true;
135117
}
136118

137-
int16_t serialGetC (void) {
138-
119+
static int16_t serialGetC (void)
120+
{
139121
if(rx_tail == rx_head)
140122
return -1;
141123

@@ -164,27 +146,69 @@ int16_t serialGetC (void) {
164146
return (int16_t)data;
165147
}
166148

167-
void serialWriteS(const char *data) {
149+
static void serialWriteS (const char *data)
150+
{
168151

169152
char c, *ptr = (char *)data;
170153

171154
while((c = *ptr++) != '\0')
172155
serialPutC(c);
173-
174156
}
175-
176-
void serialWriteLn(const char *data) {
157+
/*
158+
static void serialWriteLn (const char *data)
159+
{
177160
serialWriteS(data);
178161
serialWriteS(eol);
179162
}
180163
181-
void serialWrite(const char *data, uint16_t length) {
182-
164+
static void serialWrite (const char *data, uint16_t length)
165+
{
183166
char *ptr = (char *)data;
184167
185168
while(length--)
186169
serialPutC(*ptr++);
170+
}
171+
*/
172+
static enqueue_realtime_command_ptr serialSetRtHandler (enqueue_realtime_command_ptr handler)
173+
{
174+
enqueue_realtime_command_ptr prev = enqueue_realtime_command;
175+
176+
if(handler)
177+
enqueue_realtime_command = handler;
178+
179+
return prev;
180+
}
181+
182+
const io_stream_t *serialInit (void)
183+
{
184+
static const io_stream_t stream = {
185+
.type = StreamType_Serial,
186+
.connected = true,
187+
.read = serialGetC,
188+
.write = serialWriteS,
189+
.write_char = serialPutC,
190+
.write_all = serialWriteS,
191+
.get_rx_buffer_free = serialRxFree,
192+
.reset_read_buffer = serialRxFlush,
193+
.cancel_read_buffer = serialRxCancel,
194+
.set_enqueue_rt_handler = serialSetRtHandler
195+
};
196+
197+
UCA1CTL1 = UCSWRST;
198+
UCA1CTL1 |= UCSSEL_2; // Use SMCLK
199+
setUCA1BR(217); // Set baudrate to 115200 @ 25MHz SMCLK
200+
UCA1MCTL = 0; // Modulation UCBRSx=0, UCBRFx=0
201+
UCA1CTL0 = 0; // 8 bit, 1 stop bit, no parity
202+
SERIAL_SEL |= SERIAL_RXD|SERIAL_TXD;
203+
UCA1CTL1 &= ~UCSWRST; // Initialize USCI state machine
204+
UCA1IE |= UCRXIE; // Enable USCI_A0 RX interrupt
205+
206+
#ifndef XONXOFF
207+
SERIAL_RTS_PORT_DIR |= SERIAL_RTS_BIT; // Enable RTS pin
208+
SERIAL_RTS_PORT_OUT &= ~SERIAL_RTS_BIT; // and drive it low
209+
#endif
187210

211+
return &stream;
188212
}
189213

190214
#pragma vector=USCI_A1_VECTOR
@@ -204,7 +228,7 @@ __interrupt void USCI1RX_ISR(void)
204228
next_head = UCA1RXBUF; // and do dummy read to clear interrupt
205229
} else {
206230
char data = UCA1RXBUF;
207-
if(!hal.stream.enqueue_realtime_command(data)) {
231+
if(!enqueue_realtime_command(data)) {
208232
rxbuf[rx_head] = data; // Add data to buffer
209233
rx_head = next_head; // and update pointer
210234
}

serial.h

+5-13
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//
22
// serial.h - serial (UART) port library
33
//
4-
// v1.0 / 2019-06-03 / Io Engineering / Terje
4+
// v1.1 / 2021-07-15 / Io Engineering / Terje
55
//
66
//
77

88
/*
99
10-
Copyright (c) 2015-2019, Terje Io
10+
Copyright (c) 2015-2021, Terje Io
1111
All rights reserved.
1212
1313
Redistribution and use in source and binary forms, with or without modification,
@@ -41,6 +41,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4141
#include <stdint.h>
4242

4343
#include "portmacros.h"
44+
#include "grbl/stream.h"
4445

4546
#define XONOK (ASCII_XON|0x80)
4647
#define XOFFOK (ASCII_XOFF|0x80)
@@ -60,15 +61,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6061

6162
/* UART */
6263

63-
void serialInit (void);
64-
uint16_t serialTxCount (void);
65-
uint16_t serialRxCount (void);
66-
uint16_t serialRxFree (void);
67-
void serialRxFlush (void);
68-
void serialRxCancel (void);
69-
int16_t serialGetC (void);
70-
bool serialPutC (const char data);
71-
void serialWriteS (const char *data);
72-
void serialWriteLn (const char *data);
73-
void serialWrite (const char *data, uint16_t length);
64+
const io_stream_t *serialInit (void);
7465

66+
/*EOF*/

0 commit comments

Comments
 (0)