Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 212 lines (175 sloc) 4.843 kb
29beb0a @timkrins Using GRBL instead now. Not yet working.
authored
1 /*
2 wiring_serial.c - serial functions.
3 Part of Arduino - http://www.arduino.cc/
4
5 Copyright (c) 2005-2006 David A. Mellis
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General
18 Public License along with this library; if not, write to the
19 Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 Boston, MA 02111-1307 USA
21
22 $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
23 */
24
25 //#include "wiring_private.h"
26 #include <math.h>
27 #include <avr/pgmspace.h>
28 #include <avr/interrupt.h>
29
30 // Define constants and variables for buffering incoming serial data. We're
31 // using a ring buffer (I think), in which rx_buffer_head is the index of the
32 // location to which to write the next incoming character and rx_buffer_tail
33 // is the index of the location from which to read.
34 #ifdef __AVR_ATmega328P__
35 #define RX_BUFFER_SIZE 256
36 #else
37 #define RX_BUFFER_SIZE 64
38 #endif
39
40 unsigned char rx_buffer[RX_BUFFER_SIZE];
41
42 int rx_buffer_head = 0;
43 int rx_buffer_tail = 0;
44
45 void beginSerial(long baud)
46 {
47 UBRR0H = ((F_CPU / 16 + baud / 2) / baud - 1) >> 8;
48 UBRR0L = ((F_CPU / 16 + baud / 2) / baud - 1);
49
50 /* baud doubler off - Only needed on Uno XXX */
51 UCSR0A &= ~(1 << U2X0);
52
53 // enable rx and tx
54 UCSR0B |= 1<<RXEN0;
55 UCSR0B |= 1<<TXEN0;
56
57 // enable interrupt on complete reception of a byte
58 UCSR0B |= 1<<RXCIE0;
59
60 // defaults to 8-bit, no parity, 1 stop bit
61 }
62
63 void serialWrite(unsigned char c)
64 {
65 while (!(UCSR0A & (1 << UDRE0)))
66 ;
67
68 UDR0 = c;
69 }
70
71 int serialAvailable()
72 {
73 return (RX_BUFFER_SIZE + rx_buffer_head - rx_buffer_tail) % RX_BUFFER_SIZE;
74 }
75
76 int serialRead()
77 {
78 // if the head isn't ahead of the tail, we don't have any characters
79 if (rx_buffer_head == rx_buffer_tail) {
80 return -1;
81 } else {
82 unsigned char c = rx_buffer[rx_buffer_tail];
83 rx_buffer_tail = (rx_buffer_tail + 1) % RX_BUFFER_SIZE;
84 return c;
85 }
86 }
87
88 void serialFlush()
89 {
90 // don't reverse this or there may be problems if the RX interrupt
91 // occurs after reading the value of rx_buffer_head but before writing
92 // the value to rx_buffer_tail; the previous value of rx_buffer_head
93 // may be written to rx_buffer_tail, making it appear as if the buffer
94 // were full, not empty.
95 rx_buffer_head = rx_buffer_tail;
96 }
97
98 SIGNAL(USART0_RX_vect)
99 {
100 unsigned char c = UDR0;
101 int i = (rx_buffer_head + 1) % RX_BUFFER_SIZE;
102
103 // if we should be storing the received character into the location
104 // just before the tail (meaning that the head would advance to the
105 // current location of the tail), we're about to overflow the buffer
106 // and so we don't write the character or advance the head.
107 if (i != rx_buffer_tail) {
108 rx_buffer[rx_buffer_head] = c;
109 rx_buffer_head = i;
110 }
111 }
112
113 // void printMode(int mode)
114 // {
115 // // do nothing, we only support serial printing, not lcd.
116 // }
117
118 void printByte(unsigned char c)
119 {
120 serialWrite(c);
121 }
122
123 // void printNewline()
124 // {
125 // printByte('\n');
126 // }
127 //
128 void printString(const char *s)
129 {
130 while (*s)
131 printByte(*s++);
132 }
133
134 // Print a string stored in PGM-memory
135 void printPgmString(const char *s)
136 {
137 char c;
138 while ((c = pgm_read_byte_near(s++)))
139 printByte(c);
140 }
141
142 void printIntegerInBase(unsigned long n, unsigned long base)
143 {
144 unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
145 unsigned long i = 0;
146
147 if (n == 0) {
148 printByte('0');
149 return;
150 }
151
152 while (n > 0) {
153 buf[i++] = n % base;
154 n /= base;
155 }
156
157 for (; i > 0; i--)
158 printByte(buf[i - 1] < 10 ?
159 '0' + buf[i - 1] :
160 'A' + buf[i - 1] - 10);
161 }
162
163 void printInteger(long n)
164 {
165 if (n < 0) {
166 printByte('-');
167 n = -n;
168 }
169
170 printIntegerInBase(n, 10);
171 }
172
173 void printFloat(double n)
174 {
175 double integer_part, fractional_part;
176 fractional_part = modf(n, &integer_part);
177 printInteger(integer_part);
178 printByte('.');
179 printInteger(round(fractional_part*1000));
180 }
181
182 // void printHex(unsigned long n)
183 // {
184 // printIntegerInBase(n, 16);
185 // }
186 //
187 // void printOctal(unsigned long n)
188 // {
189 // printIntegerInBase(n, 8);
190 // }
191 //
192 // void printBinary(unsigned long n)
193 // {
194 // printIntegerInBase(n, 2);
195 // }
196
197 /* Including print() adds approximately 1500 bytes to the binary size,
198 * so we replace it with the smaller and less-confusing printString(),
199 * printInteger(), etc.
200 void print(const char *format, ...)
201 {
202 char buf[256];
203 va_list ap;
204
205 va_start(ap, format);
206 vsnprintf(buf, 256, format, ap);
207 va_end(ap);
208
209 printString(buf);
210 }
211 */
Something went wrong with that request. Please try again.