diff --git a/Makefile b/Makefile index e9c2c99..6011ae0 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,7 @@ DEVICE = atmega328p CLOCK = 16000000 PROGRAMMER = -c avrisp2 -P usb OBJECTS = main.o motion_control.o gcode.o spindle_control.o serial.o protocol.o stepper.o \ - eeprom.o settings.o planner.o nuts_bolts.o limits.o + eeprom.o settings.o planner.o nuts_bolts.o limits.o print.o # FUSES = -U hfuse:w:0xd9:m -U lfuse:w:0x24:m FUSES = -U hfuse:w:0xd2:m -U lfuse:w:0xff:m # update that line with this when programmer is back up: diff --git a/doc/resources.txt b/doc/resources.txt index 2754464..293a6f7 100644 --- a/doc/resources.txt +++ b/doc/resources.txt @@ -3,7 +3,7 @@ Allocation of AVR peripherals in Grbl See config.h for pin allocation. -The UART is handled by 'wiring_serial' and used primarily for streaming gcode +The UART is handled by 'serial' and used primarily for streaming gcode 16 bit Timer 1 and the TIMER1_COMPA interrupt is used by the 'stepper' module to handle step events diff --git a/doc/structure.txt b/doc/structure.txt index 51e8dee..4992f80 100644 --- a/doc/structure.txt +++ b/doc/structure.txt @@ -36,4 +36,6 @@ Supporting files: 'nuts_bolts.h' : A tiny collection of useful constants and macros used everywhere -'serial' : Low level serial communications \ No newline at end of file +'serial' : Low level serial communications + +'print' : Functions to print strings of different formats (using serial) \ No newline at end of file diff --git a/main.c b/main.c index 1951171..29d37da 100644 --- a/main.c +++ b/main.c @@ -22,6 +22,7 @@ #include #include #include +#include "config.h" #include "planner.h" #include "stepper.h" #include "spindle_control.h" @@ -33,12 +34,9 @@ #include "settings.h" #include "serial.h" -// #ifndef __AVR_ATmega328P__ -// # error "As of version 0.6 Grbl only supports atmega328p. If you want to run Grbl on an 168 check out 0.51 ('git co v0_51')" -// #endif - int main(void) { + serial_init(BAUD_RATE); protocol_init(); settings_init(); plan_init(); diff --git a/motion_control.c b/motion_control.c index 170499e..ef4a6b3 100644 --- a/motion_control.c +++ b/motion_control.c @@ -27,7 +27,6 @@ #include "nuts_bolts.h" #include "stepper.h" #include "planner.h" -#include "serial.h" void mc_dwell(uint32_t milliseconds) diff --git a/planner.c b/planner.c index d7504cb..28f4e2c 100644 --- a/planner.c +++ b/planner.c @@ -29,7 +29,6 @@ #include "stepper.h" #include "settings.h" #include "config.h" -#include "serial.h" // The number of linear motions that can be in the plan at any give time #ifdef __AVR_ATmega328P__ diff --git a/print.c b/print.c new file mode 100644 index 0000000..ffd3f33 --- /dev/null +++ b/print.c @@ -0,0 +1,58 @@ +#include +#include +#include "serial.h" + +void printString(const char *s) +{ + while (*s) + serial_write(*s++); +} + +// Print a string stored in PGM-memory +void printPgmString(const char *s) +{ + char c; + while ((c = pgm_read_byte_near(s++))) + serial_write(c); +} + +void printIntegerInBase(unsigned long n, unsigned long base) +{ + unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars. + unsigned long i = 0; + + if (n == 0) { + serial_write('0'); + return; + } + + while (n > 0) { + buf[i++] = n % base; + n /= base; + } + + for (; i > 0; i--) + serial_write(buf[i - 1] < 10 ? + '0' + buf[i - 1] : + 'A' + buf[i - 1] - 10); +} + +void printInteger(long n) +{ + if (n < 0) { + serial_write('-'); + n = -n; + } + + printIntegerInBase(n, 10); +} + +void printFloat(double n) +{ + double integer_part, fractional_part; + fractional_part = modf(n, &integer_part); + printInteger(integer_part); + serial_write('.'); + printInteger(round(fractional_part*1000)); +} + diff --git a/print.h b/print.h new file mode 100644 index 0000000..62b76ea --- /dev/null +++ b/print.h @@ -0,0 +1,14 @@ +#ifndef print_h +#define print_h + +void printNewline(void); +void printString(const char *s); +void printPgmString(const char *s); +void printInteger(long n); +void printHex(unsigned long n); +void printOctal(unsigned long n); +void printBinary(unsigned long n); +void printIntegerInBase(unsigned long n, unsigned long base); +void printFloat(double n); + +#endif \ No newline at end of file diff --git a/protocol.c b/protocol.c index c64280e..a504d3a 100644 --- a/protocol.c +++ b/protocol.c @@ -22,6 +22,7 @@ #include "protocol.h" #include "gcode.h" #include "serial.h" +#include "print.h" #include "settings.h" #include "config.h" #include @@ -55,7 +56,6 @@ static void status_message(int status_code) { void protocol_init() { - beginSerial(BAUD_RATE); printPgmString(PSTR("\r\nGrbl " GRBL_VERSION)); printPgmString(PSTR("\r\n")); } @@ -72,7 +72,7 @@ uint8_t protocol_execute_line(char *line) { void protocol_process() { char c; - while((c = serialRead()) != -1) + while((c = serial_read()) != 0xff) { if((char_counter > 0) && ((c == '\n') || (c == '\r'))) { // Line is complete. Then execute! line[char_counter] = 0; // treminate string diff --git a/serial.c b/serial.c index bcaa66f..cc78802 100644 --- a/serial.c +++ b/serial.c @@ -21,9 +21,8 @@ */ -#include -#include #include +#include // Define constants and variables for buffering incoming serial data. We're // using a ring buffer (I think), in which rx_buffer_head is the index of the @@ -46,7 +45,7 @@ uint8_t tx_buffer_head = 0; volatile uint8_t tx_buffer_tail = 0; -void beginSerial(long baud) +void serial_init(long baud) { UBRR0H = ((F_CPU / 16 + baud / 2) / baud - 1) >> 8; UBRR0L = ((F_CPU / 16 + baud / 2) / baud - 1); @@ -60,56 +59,50 @@ void beginSerial(long baud) // enable interrupt on complete reception of a byte UCSR0B |= 1< 0) { - buf[i++] = n % base; - n /= base; - } - - for (; i > 0; i--) - printByte(buf[i - 1] < 10 ? - '0' + buf[i - 1] : - 'A' + buf[i - 1] - 10); -} - -void printInteger(long n) -{ - if (n < 0) { - printByte('-'); - n = -n; - } - - printIntegerInBase(n, 10); -} - -void printFloat(double n) -{ - double integer_part, fractional_part; - fractional_part = modf(n, &integer_part); - printInteger(integer_part); - printByte('.'); - printInteger(round(fractional_part*1000)); -} - diff --git a/serial.h b/serial.h index e6f3055..1f7e420 100644 --- a/serial.h +++ b/serial.h @@ -25,20 +25,8 @@ #ifndef serial_h #define serial_h -void beginSerial(long); -void serialWrite(uint8_t); -int serialAnyAvailable(void); -uint8_t serialRead(void); -void printMode(int); -void printByte(unsigned char c); -void printNewline(void); -void printString(const char *s); -void printPgmString(const char *s); -void printInteger(long n); -void printHex(unsigned long n); -void printOctal(unsigned long n); -void printBinary(unsigned long n); -void printIntegerInBase(unsigned long n, unsigned long base); -void printFloat(double n); +void serial_init(long); +void serial_write(uint8_t); +uint8_t serial_read(void); #endif diff --git a/settings.c b/settings.c index 7782627..3497304 100644 --- a/settings.c +++ b/settings.c @@ -23,7 +23,7 @@ #include "nuts_bolts.h" #include "settings.h" #include "eeprom.h" -#include "serial.h" +#include "print.h" #include #include "protocol.h" #include "config.h" diff --git a/stepper.c b/stepper.c index a145475..164db81 100644 --- a/stepper.c +++ b/stepper.c @@ -30,7 +30,6 @@ #include "nuts_bolts.h" #include #include "planner.h" -#include "serial.h" #include "limits.h" // Some useful constants