Skip to content

Commit

Permalink
cleaned up serial completing support for non blocking tx and refactor…
Browse files Browse the repository at this point in the history
…ing formatting functions into a new module 'print'
  • Loading branch information
simen committed Jun 3, 2011
1 parent 8793b55 commit c0b4b83
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 118 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion doc/resources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion doc/structure.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ Supporting files:

'nuts_bolts.h' : A tiny collection of useful constants and macros used everywhere

'serial' : Low level serial communications
'serial' : Low level serial communications

'print' : Functions to print strings of different formats (using serial)
6 changes: 2 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "config.h"
#include "planner.h"
#include "stepper.h"
#include "spindle_control.h"
Expand All @@ -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();
Expand Down
1 change: 0 additions & 1 deletion motion_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include "nuts_bolts.h"
#include "stepper.h"
#include "planner.h"
#include "serial.h"


void mc_dwell(uint32_t milliseconds)
Expand Down
1 change: 0 additions & 1 deletion planner.c
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down
58 changes: 58 additions & 0 deletions print.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <math.h>
#include <avr/pgmspace.h>
#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));
}

14 changes: 14 additions & 0 deletions print.h
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "protocol.h"
#include "gcode.h"
#include "serial.h"
#include "print.h"
#include "settings.h"
#include "config.h"
#include <math.h>
Expand Down Expand Up @@ -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"));
}
Expand All @@ -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
Expand Down
114 changes: 24 additions & 90 deletions serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
*/

#include <math.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>

// 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
Expand All @@ -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);
Expand All @@ -60,56 +59,50 @@ void beginSerial(long baud)

// enable interrupt on complete reception of a byte
UCSR0B |= 1<<RXCIE0;

// defaults to 8-bit, no parity, 1 stop bit
}

void serialWrite(uint8_t data) {
void serial_write(uint8_t data) {
// Calculate next head
uint8_t next_head = (tx_buffer_head + 1) % TX_BUFFER_SIZE;

// wait until there's a space in the buffer
while (next_head == tx_buffer_tail) ;
// Wait until there's a space in the buffer
while (next_head == tx_buffer_tail) { sleep_mode(); };

// Store data and advance head
tx_buffer[tx_buffer_head] = data;
tx_buffer_head = next_head;

// enable the Data Register Empty Interrupt
UCSR0B |= (1 << UDRIE0);
// Enable Data Register Empty Interrupt
UCSR0B |= (1 << UDRIE0);
}

// interrupt called on Data Register Empty
// Data Register Empty Interrupt handler
SIGNAL(USART_UDRE_vect) {
// temporary tx_buffer_tail
// (to optimize for volatile, there are no interrupts inside an interrupt routine)

// temporary tx_buffer_tail (to optimize for volatile)
uint8_t tail = tx_buffer_tail;

// get a byte from the buffer
uint8_t data = tx_buffer[tail];
// send the byte
UDR0 = data;
// Send a byte from the buffer
UDR0 = tx_buffer[tail];

// update tail position
// Update tail position
tail ++;
tail %= TX_BUFFER_SIZE;
tx_buffer_tail = tail;

// if the buffer is empty, disable the interrupt
if (tail == tx_buffer_head) {
UCSR0B &= ~(1 << UDRIE0);
// Turn off Data Register Empty Interrupt if this concludes the transfer
if (tail == tx_buffer_head) {
UCSR0B &= ~(1 << UDRIE0);
}

tx_buffer_tail = tail;
}

// Returns true if there is any data in the read buffer
int serialAnyAvailable()
{
return (rx_buffer_head != rx_buffer_tail);
}

uint8_t serialRead()
uint8_t serial_read()
{
if (!serialAnyAvailable()) {
return -1;
if (rx_buffer_head != rx_buffer_tail) {
return 0xff;
} else {
uint8_t data = rx_buffer[rx_buffer_tail];
rx_buffer_tail = (rx_buffer_tail + 1) % RX_BUFFER_SIZE;
Expand All @@ -132,62 +125,3 @@ SIGNAL(USART_RX_vect)
}
}

void printByte(unsigned char c)
{
serialWrite((uint8_t) c);
}

void printString(const char *s)
{
while (*s)
printByte(*s++);
}

// Print a string stored in PGM-memory
void printPgmString(const char *s)
{
char c;
while ((c = pgm_read_byte_near(s++)))
printByte(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) {
printByte('0');
return;
}

while (n > 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));
}

18 changes: 3 additions & 15 deletions serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "nuts_bolts.h"
#include "settings.h"
#include "eeprom.h"
#include "serial.h"
#include "print.h"
#include <avr/pgmspace.h>
#include "protocol.h"
#include "config.h"
Expand Down
1 change: 0 additions & 1 deletion stepper.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include "nuts_bolts.h"
#include <avr/interrupt.h>
#include "planner.h"
#include "serial.h"
#include "limits.h"

// Some useful constants
Expand Down

0 comments on commit c0b4b83

Please sign in to comment.