Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

* porting uart changes to g4 firmware

  • Loading branch information...
commit a7c022be82636b6bcc9d3b778197532df96e0691 1 parent 6c865be
@phooky phooky authored
View
1  v2/src/Extruder/boards/ecv22/UART.cc
@@ -67,7 +67,6 @@ UART::UART() : enabled(false) {
rxPin.setDirection(false);
rxPin.setValue(true);
-
listen();
}
View
23 v2/src/Extruder/boards/ecv34/UART.cc
@@ -39,16 +39,15 @@
UART UART::uart;
+volatile uint8_t loopback_bytes = 0;
// Unlike the old implementation, we go half-duplex: we don't listen while sending, and vice versa.
inline void speak() {
TX_ENABLE_PIN.setValue(true);
- RX_ENABLE_PIN.setValue(true);
}
inline void listen() {
TX_ENABLE_PIN.setValue(false);
- RX_ENABLE_PIN.setValue(false);
}
UART::UART() : enabled(false) {
@@ -61,13 +60,21 @@ UART::UART() : enabled(false) {
/* defaults to 8-bit, no parity, 1 stop bit */
TX_ENABLE_PIN.setDirection(true);
RX_ENABLE_PIN.setDirection(true);
- listen();
+ RX_ENABLE_PIN.setValue(false); // always in listen mode
+
+ // pulup on RX
+ Pin rxPin(PortD,0);
+ rxPin.setDirection(false);
+ rxPin.setValue(true);
+
+ listen();
}
// Reset the UART to a listening state. This is important for
// RS485-based comms.
void UART::reset() {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
+ loopback_bytes = 0;
listen();
}
}
@@ -78,6 +85,7 @@ void UART::beginSend() {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
uint8_t send_byte = out.getNextByteToSend();
speak();
+ loopback_bytes = 1;
UDR0 = send_byte;
}
}
@@ -91,15 +99,22 @@ void UART::enable(bool enabled_in) {
}
}
+volatile uint8_t in_byte;
// Send and receive interrupts
ISR(USART_RX_vect)
{
- UART::getHostUART().in.processByte( UDR0 );
+ in_byte = UDR0;
+ if (loopback_bytes > 0) {
+ loopback_bytes--;
+ } else {
+ UART::getHostUART().in.processByte( in_byte );
+ }
}
ISR(USART_TX_vect)
{
if (UART::getHostUART().out.isSending()) {
+ loopback_bytes++;
UDR0 = UART::getHostUART().out.getNextByteToSend();
} else {
listen();
View
32 v2/src/Motherboard/boards/mb24/UART.cc
@@ -65,21 +65,15 @@ UART UART::uart[2] = {
UART(1)
};
-volatile bool listening = true;
+volatile uint8_t loopback_bytes = 0;
// Unlike the old implementation, we go half-duplex: we don't listen while sending.
inline void listen() {
TX_ENABLE_PIN.setValue(false);
- RX_ENABLE_PIN.setValue(false);
- // Turn on the receiver
- UCSR1B |= _BV(RXEN1);
}
inline void speak() {
TX_ENABLE_PIN.setValue(true);
- RX_ENABLE_PIN.setValue(true);
- // Turn off the receiver
- UCSR1B &= ~_BV(RXEN1);
}
UART::UART(uint8_t index) : index_(index), enabled_(false) {
@@ -92,6 +86,7 @@ UART::UART(uint8_t index) : index_(index), enabled_(false) {
// Tx enable: PD4, active high
TX_ENABLE_PIN.setDirection(true);
RX_ENABLE_PIN.setDirection(true);
+ RX_ENABLE_PIN.setValue(false); // Active low
listen();
}
}
@@ -106,6 +101,7 @@ void UART::beginSend() {
SEND_BYTE(0,send_byte);
} else if (index_ == 1) {
speak();
+ loopback_bytes = 1;
SEND_BYTE(1,send_byte);
}
}
@@ -125,19 +121,28 @@ void UART::enable(bool enabled) {
// RS485-based comms.
void UART::reset() {
if (index_ == 1) {
+ loopback_bytes = 0;
listen();
}
}
// Send and receive interrupts
-#define UART_RX_ISR(uart_) \
-ISR(USART##uart_##_RX_vect) \
-{ \
- UART::uart[uart_].in.processByte( UDR##uart_ ); \
+ISR(USART0_RX_vect)
+{
+ UART::uart[0].in.processByte( UDR0 );
}
-UART_RX_ISR(0);
-UART_RX_ISR(1);
+volatile uint8_t byte_in;
+
+ISR(USART1_RX_vect)
+{
+ byte_in = UDR1;
+ if (loopback_bytes > 0) {
+ loopback_bytes--;
+ } else {
+ UART::uart[1].in.processByte( byte_in );
+ }
+}
ISR(USART0_TX_vect)
{
@@ -149,6 +154,7 @@ ISR(USART0_TX_vect)
ISR(USART1_TX_vect)
{
if (UART::uart[1].out.isSending()) {
+ loopback_bytes++;
UDR1 = UART::uart[1].out.getNextByteToSend();
} else {
listen();
Please sign in to comment.
Something went wrong with that request. Please try again.