Created
August 27, 2017 15:08
-
-
Save hdo/3551f22579794e4454afee62a5a44d2b to your computer and use it in GitHub Desktop.
Enable GRBL second serial port for receiving commands on Arduino Mega Board
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// original see: github.com/gnea/grbl-Mega | |
#include "grbl.h" | |
... | |
void serial_init() | |
{ | |
// Set baud rate | |
#if BAUD_RATE < 57600 | |
uint16_t UBRR0_value = ((F_CPU / (8L * BAUD_RATE)) - 1)/2 ; | |
UCSR0A &= ~(1 << U2X0); // baud doubler off - Only needed on Uno XXX | |
// by HDO | |
UCSR1A &= ~(1 << U2X0); // baud doubler off - Only needed on Uno XXX | |
#else | |
uint16_t UBRR0_value = ((F_CPU / (4L * BAUD_RATE)) - 1)/2; | |
UCSR0A |= (1 << U2X0); // baud doubler on for high baud rates, i.e. 115200 | |
// by HDO | |
UCSR1A |= (1 << U2X0); // baud doubler on for high baud rates, i.e. 115200 | |
#endif | |
UBRR0H = UBRR0_value >> 8; | |
UBRR0L = UBRR0_value; | |
// enable rx, tx, and interrupt on complete reception of a byte | |
UCSR0B |= (1<<RXEN0 | 1<<TXEN0 | 1<<RXCIE0); | |
// defaults to 8-bit, no parity, 1 stop bit | |
// HDO: add second serial rx, use same values als UART0 | |
UBRR1H = UBRR0_value >> 8; | |
UBRR1L = UBRR0_value; | |
// enable rx, tx, and interrupt on complete reception of a byte | |
UCSR1B |= (1<<RXEN1 | 1<<TXEN1 | 1<<RXCIE1); | |
} | |
... | |
// add by HDO | |
// Data Register Empty Interrupt handler | |
ISR(USART1_UDRE_vect) | |
{ | |
// currently not supported | |
} | |
// add by HDO | |
ISR(USART1_RX_vect) | |
{ | |
uint8_t data = UDR1; | |
uint8_t next_head; | |
// Pick off realtime command characters directly from the serial stream. These characters are | |
// not passed into the main buffer, but these set system state flag bits for realtime execution. | |
switch (data) { | |
case CMD_RESET: mc_reset(); break; // Call motion control reset routine. | |
case CMD_STATUS_REPORT: system_set_exec_state_flag(EXEC_STATUS_REPORT); break; // Set as true | |
case CMD_CYCLE_START: system_set_exec_state_flag(EXEC_CYCLE_START); break; // Set as true | |
case CMD_FEED_HOLD: system_set_exec_state_flag(EXEC_FEED_HOLD); break; // Set as true | |
default : | |
if (data > 0x7F) { // Real-time control characters are extended ACSII only. | |
switch(data) { | |
case CMD_SAFETY_DOOR: system_set_exec_state_flag(EXEC_SAFETY_DOOR); break; // Set as true | |
case CMD_JOG_CANCEL: | |
if (sys.state & STATE_JOG) { // Block all other states from invoking motion cancel. | |
system_set_exec_state_flag(EXEC_MOTION_CANCEL); | |
} | |
break; | |
} | |
// Throw away any unfound extended-ASCII character by not passing it to the serial buffer. | |
} else { // Write character to buffer | |
next_head = serial_rx_buffer_head + 1; | |
if (next_head == RX_RING_BUFFER) { next_head = 0; } | |
// Write data to buffer unless it is full. | |
if (next_head != serial_rx_buffer_tail) { | |
serial_rx_buffer[serial_rx_buffer_head] = data; | |
serial_rx_buffer_head = next_head; | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
HI
did you design this so that you could use two USART's in grbl at the same time or to use USART1 to replace USART0?
My problem is that i'm trying to use a board from a craft cutter, it has an ATmega2561 on it but they have used to USART1 for the serial to usb connection