Skip to content

Commit 3ff4753

Browse files
hub-jbaalbertogg
authored andcommitted
ExtUI for Anycubic I3 Mega (MarlinFirmware#18655)
1 parent f9d23a4 commit 3ff4753

File tree

7 files changed

+1753
-4
lines changed

7 files changed

+1753
-4
lines changed

Marlin/src/gcode/feature/pause/M125.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void GcodeSuite::M125() {
7979

8080
if (pause_print(retract, park_point, 0, show_lcd)) {
8181
TERN_(POWER_LOSS_RECOVERY, if (recovery.enabled) recovery.save(true));
82-
if (!sd_printing || show_lcd) {
82+
if (ENABLED(EXTENSIBLE_UI) || !sd_printing || show_lcd) {
8383
wait_for_confirmation(false, 0);
8484
resume_print(0, 0, -retract, 0);
8585
}
Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
1+
/*
2+
anycubic_serial.cpp --- Support for Anycubic i3 Mega TFT serial connection
3+
Created by Christian Hopp on 09.12.17.
4+
5+
Original file:
6+
HardwareSerial.cpp - Hardware serial library for Wiring
7+
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
8+
9+
This library is free software; you can redistribute it and/or
10+
modify it under the terms of the GNU Lesser General Public
11+
License as published by the Free Software Foundation; either
12+
version 2.1 of the License, or (at your option) any later version.
13+
14+
This library is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
Lesser General Public License for more details.
18+
19+
You should have received a copy of the GNU Lesser General Public
20+
License along with this library; if not, write to the Free Software
21+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22+
23+
Modified 23 November 2006 by David A. Mellis
24+
Modified 28 September 2010 by Mark Sproul
25+
Modified 14 August 2012 by Alarus
26+
*/
27+
28+
#include "../../../../inc/MarlinConfig.h"
29+
30+
#if ENABLED(ANYCUBIC_TFT_MODEL)
31+
32+
#include "Arduino.h"
33+
34+
// this next line disables the entire HardwareSerial.cpp,
35+
// so I can support AtTiny series and other chips without a UART
36+
#ifdef UBRR3H
37+
38+
#include "anycubic_serial.h"
39+
40+
#include <stdlib.h>
41+
#include <stdio.h>
42+
#include <string.h>
43+
#include <inttypes.h>
44+
#include "wiring_private.h"
45+
46+
// Define constants and variables for buffering incoming serial data. We're
47+
// using a ring buffer (I think), in which head is the index of the location
48+
// to which to write the next incoming character and tail is the index of the
49+
// location from which to read.
50+
#if (RAMEND < 1000)
51+
#define SERIAL_BUFFER_SIZE 64
52+
#else
53+
#define SERIAL_BUFFER_SIZE 128
54+
#endif
55+
56+
struct ring_buffer {
57+
unsigned char buffer[SERIAL_BUFFER_SIZE];
58+
volatile unsigned int head;
59+
volatile unsigned int tail;
60+
};
61+
62+
#ifdef UBRR3H
63+
ring_buffer rx_buffer_ajg = { { 0 }, 0, 0 };
64+
ring_buffer tx_buffer_ajg = { { 0 }, 0, 0 };
65+
#endif
66+
67+
inline void store_char(unsigned char c, ring_buffer *buffer) {
68+
int i = (unsigned int)(buffer->head + 1) % SERIAL_BUFFER_SIZE;
69+
70+
// if we should be storing the received character into the location
71+
// just before the tail (meaning that the head would advance to the
72+
// current location of the tail), we're about to overflow the buffer
73+
// and so we don't write the character or advance the head.
74+
if (i != buffer->tail) {
75+
buffer->buffer[buffer->head] = c;
76+
buffer->head = i;
77+
}
78+
}
79+
80+
#if defined(USART3_RX_vect) && defined(UDR3)
81+
void serialEvent3() __attribute__((weak));
82+
void serialEvent3() {}
83+
#define serialEvent3_implemented
84+
ISR(USART3_RX_vect) {
85+
if (bit_is_clear(UCSR3A, UPE3)) {
86+
unsigned char c = UDR3;
87+
store_char(c, &rx_buffer_ajg);
88+
}
89+
else {
90+
unsigned char c = UDR3;
91+
}
92+
}
93+
#endif
94+
95+
#ifdef USART3_UDRE_vect
96+
97+
ISR(USART3_UDRE_vect) {
98+
if (tx_buffer_ajg.head == tx_buffer_ajg.tail) {
99+
// Buffer empty, so disable interrupts
100+
cbi(UCSR3B, UDRIE3);
101+
}
102+
else {
103+
// There is more data in the output buffer. Send the next byte
104+
unsigned char c = tx_buffer_ajg.buffer[tx_buffer_ajg.tail];
105+
tx_buffer_ajg.tail = (tx_buffer_ajg.tail + 1) % SERIAL_BUFFER_SIZE;
106+
107+
UDR3 = c;
108+
}
109+
}
110+
111+
#endif
112+
113+
// Constructors ////////////////////////////////////////////////////////////////
114+
115+
AnycubicSerialClass::AnycubicSerialClass(ring_buffer *rx_buffer, ring_buffer *tx_buffer,
116+
volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
117+
volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
118+
volatile uint8_t *ucsrc, volatile uint8_t *udr,
119+
uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x
120+
) {
121+
_rx_buffer = rx_buffer;
122+
_tx_buffer = tx_buffer;
123+
_ubrrh = ubrrh;
124+
_ubrrl = ubrrl;
125+
_ucsra = ucsra;
126+
_ucsrb = ucsrb;
127+
_ucsrc = ucsrc;
128+
_udr = udr;
129+
_rxen = rxen;
130+
_txen = txen;
131+
_rxcie = rxcie;
132+
_udrie = udrie;
133+
_u2x = u2x;
134+
}
135+
136+
// Public Methods //////////////////////////////////////////////////////////////
137+
138+
void AnycubicSerialClass::begin(unsigned long baud) {
139+
uint16_t baud_setting;
140+
bool use_u2x = true;
141+
142+
#if F_CPU == 16000000UL
143+
// hardcoded exception for compatibility with the bootloader shipped
144+
// with the Duemilanove and previous boards and the firmware on the 8U2
145+
// on the Uno and Mega 2560.
146+
if (baud == 57600) use_u2x = false;
147+
#endif
148+
149+
try_again:
150+
151+
if (use_u2x) {
152+
*_ucsra = 1 << _u2x;
153+
baud_setting = (F_CPU / 4 / baud - 1) / 2;
154+
} else {
155+
*_ucsra = 0;
156+
baud_setting = (F_CPU / 8 / baud - 1) / 2;
157+
}
158+
159+
if ((baud_setting > 4095) && use_u2x) {
160+
use_u2x = false;
161+
goto try_again;
162+
}
163+
164+
// assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
165+
*_ubrrh = baud_setting >> 8;
166+
*_ubrrl = baud_setting;
167+
168+
transmitting = false;
169+
170+
sbi(*_ucsrb, _rxen);
171+
sbi(*_ucsrb, _txen);
172+
sbi(*_ucsrb, _rxcie);
173+
cbi(*_ucsrb, _udrie);
174+
}
175+
176+
void AnycubicSerialClass::begin(unsigned long baud, byte config) {
177+
uint16_t baud_setting;
178+
uint8_t current_config;
179+
bool use_u2x = true;
180+
181+
#if F_CPU == 16000000UL
182+
// hardcoded exception for compatibility with the bootloader shipped
183+
// with the Duemilanove and previous boards and the firmware on the 8U2
184+
// on the Uno and Mega 2560.
185+
if (baud == 57600) use_u2x = false;
186+
#endif
187+
188+
try_again:
189+
190+
if (use_u2x) {
191+
*_ucsra = 1 << _u2x;
192+
baud_setting = (F_CPU / 4 / baud - 1) / 2;
193+
}
194+
else {
195+
*_ucsra = 0;
196+
baud_setting = (F_CPU / 8 / baud - 1) / 2;
197+
}
198+
199+
if ((baud_setting > 4095) && use_u2x) {
200+
use_u2x = false;
201+
goto try_again;
202+
}
203+
204+
// assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
205+
*_ubrrh = baud_setting >> 8;
206+
*_ubrrl = baud_setting;
207+
208+
//set the data bits, parity, and stop bits
209+
#ifdef __AVR_ATmega8__
210+
config |= 0x80; // select UCSRC register (shared with UBRRH)
211+
#endif
212+
*_ucsrc = config;
213+
214+
sbi(*_ucsrb, _rxen);
215+
sbi(*_ucsrb, _txen);
216+
sbi(*_ucsrb, _rxcie);
217+
cbi(*_ucsrb, _udrie);
218+
}
219+
220+
void AnycubicSerialClass::end() {
221+
// wait for transmission of outgoing data
222+
while (_tx_buffer->head != _tx_buffer->tail)
223+
;
224+
225+
cbi(*_ucsrb, _rxen);
226+
cbi(*_ucsrb, _txen);
227+
cbi(*_ucsrb, _rxcie);
228+
cbi(*_ucsrb, _udrie);
229+
230+
// clear any received data
231+
_rx_buffer->head = _rx_buffer->tail;
232+
}
233+
234+
int AnycubicSerialClass::available(void) {
235+
return (int)(SERIAL_BUFFER_SIZE + _rx_buffer->head - _rx_buffer->tail) % SERIAL_BUFFER_SIZE;
236+
}
237+
238+
int AnycubicSerialClass::peek(void) {
239+
if (_rx_buffer->head == _rx_buffer->tail) {
240+
return -1;
241+
} else {
242+
return _rx_buffer->buffer[_rx_buffer->tail];
243+
}
244+
}
245+
246+
int AnycubicSerialClass::read(void) {
247+
// if the head isn't ahead of the tail, we don't have any characters
248+
if (_rx_buffer->head == _rx_buffer->tail) {
249+
return -1;
250+
} else {
251+
unsigned char c = _rx_buffer->buffer[_rx_buffer->tail];
252+
_rx_buffer->tail = (unsigned int)(_rx_buffer->tail + 1) % SERIAL_BUFFER_SIZE;
253+
return c;
254+
}
255+
}
256+
257+
void AnycubicSerialClass::flush() {
258+
// UDR is kept full while the buffer is not empty, so TXC triggers when EMPTY && SENT
259+
while (transmitting && ! (*_ucsra & _BV(TXC0)));
260+
transmitting = false;
261+
}
262+
263+
size_t AnycubicSerialClass::write(uint8_t c) {
264+
int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE;
265+
266+
// If the output buffer is full, there's nothing for it other than to
267+
// wait for the interrupt handler to empty it a bit
268+
// ???: return 0 here instead?
269+
while (i == _tx_buffer->tail)
270+
;
271+
272+
_tx_buffer->buffer[_tx_buffer->head] = c;
273+
_tx_buffer->head = i;
274+
275+
sbi(*_ucsrb, _udrie);
276+
// clear the TXC bit -- "can be cleared by writing a one to its bit location"
277+
transmitting = true;
278+
sbi(*_ucsra, TXC0);
279+
280+
return 1;
281+
}
282+
283+
AnycubicSerialClass::operator bool() {
284+
return true;
285+
}
286+
287+
// Preinstantiate Objects //////////////////////////////////////////////////////
288+
289+
#ifdef UBRR3H
290+
AnycubicSerialClass AnycubicSerial(&rx_buffer_ajg, &tx_buffer_ajg, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UCSR3C, &UDR3, RXEN3, TXEN3, RXCIE3, UDRIE3, U2X3);
291+
#endif
292+
293+
#endif // UBRR3H
294+
#endif // ANYCUBIC_TFT_MODEL

0 commit comments

Comments
 (0)