/* serial.c - Low level functions for sending and recieving bytes via the serial port Part of Grbl The MIT License (MIT) GRBL(tm) - Embedded CNC g-code interpreter and motion-controller Copyright (c) 2009-2011 Simen Svale Skogsrud Copyright (c) 2011-2012 Sungeun K. Jeon Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include "serial.h" #include "config.h" #include "motion_control.h" #include "protocol.h" uint8_t serial_rx_buffer[RX_BUFFER_SIZE]; uint8_t serial_rx_buffer_head = 0; volatile uint8_t serial_rx_buffer_tail = 0; uint8_t serial_tx_buffer[RX_BUFFER_SIZE]; uint8_t serial_tx_buffer_head = 0; volatile uint8_t serial_tx_buffer_tail = 0; #ifdef ENABLE_XONXOFF volatile uint8_t flow_ctrl = XON_SENT; // Flow control state variable // Returns the number of bytes in the RX buffer. This replaces a typical byte counter to prevent // the interrupt and main programs from writing to the counter at the same time. static uint8_t get_serial_rx_buffer_count() { if (serial_rx_buffer_head == serial_rx_buffer_tail) { return(0); } if (serial_rx_buffer_head < serial_rx_buffer_tail) { return(serial_rx_buffer_tail-serial_rx_buffer_head); } return (RX_BUFFER_SIZE - (serial_rx_buffer_head-serial_rx_buffer_tail)); } #endif 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 #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 #endif UBRR0H = UBRR0_value >> 8; UBRR0L = UBRR0_value; // enable rx and tx UCSR0B |= 1<= serial_rx_buffer_FULL) && flow_ctrl == XON_SENT) { flow_ctrl = SEND_XOFF; UCSR0B |= (1 << UDRIE0); // Force TX } #endif } } } void serial_reset_read_buffer() { serial_rx_buffer_tail = serial_rx_buffer_head; #ifdef ENABLE_XONXOFF flow_ctrl = XON_SENT; #endif }