Skip to content

Commit c6d860e

Browse files
committed
Adding new C++ Serial lib from Nick/Wiring, but modified to use avrlib's uart.c and uart.h as low-level functions.
1 parent 25c9111 commit c6d860e

File tree

5 files changed

+311
-0
lines changed

5 files changed

+311
-0
lines changed

targets/arduino/HardwareSerial.cpp

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
HarwareSerial.cpp - Hardware serial library for Wiring
3+
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
extern "C" {
21+
#include <stdio.h>
22+
#include <string.h>
23+
#include <inttypes.h>
24+
#include "Serial.h"
25+
}
26+
27+
#include "HardwareSerial.h"
28+
29+
// Constructors ////////////////////////////////////////////////////////////////
30+
31+
HardwareSerial::HardwareSerial(uint8_t uart)
32+
{
33+
if(uart == 0){
34+
_uart = 0;
35+
}else{
36+
_uart = 1;
37+
}
38+
}
39+
40+
// Public Methods //////////////////////////////////////////////////////////////
41+
42+
void HardwareSerial::begin(long speed)
43+
{
44+
uart_init(_uart, speed);
45+
}
46+
47+
uint8_t HardwareSerial::available(void)
48+
{
49+
return uart_available(_uart);
50+
}
51+
52+
int HardwareSerial::read(void)
53+
{
54+
return uart_read(_uart);
55+
}
56+
57+
void HardwareSerial::print(char c)
58+
{
59+
uart_write(_uart, &c, 1);
60+
}
61+
62+
void HardwareSerial::print(char c[])
63+
{
64+
uart_write(_uart, c, strlen(c));
65+
}
66+
67+
void HardwareSerial::print(uint8_t b)
68+
{
69+
char c = b;
70+
uart_write(_uart, &c, 1);
71+
}
72+
73+
void HardwareSerial::print(int n)
74+
{
75+
print((long) n);
76+
}
77+
78+
void HardwareSerial::print(long n)
79+
{
80+
if (n < 0) {
81+
print('-');
82+
n = -n;
83+
}
84+
printNumber(n, 10);
85+
}
86+
87+
void HardwareSerial::print(unsigned long n)
88+
{
89+
printNumber(n, 10);
90+
}
91+
92+
void HardwareSerial::print(long n, int base)
93+
{
94+
if (base == 0)
95+
print((char) n);
96+
else if (base == 10)
97+
print(n);
98+
else
99+
printNumber(n, base);
100+
}
101+
102+
void HardwareSerial::println(void)
103+
{
104+
print('\n');
105+
}
106+
107+
void HardwareSerial::println(char c)
108+
{
109+
print(c);
110+
println();
111+
}
112+
113+
void HardwareSerial::println(char c[])
114+
{
115+
uart_write(_uart, c, strlen(c));
116+
println();
117+
}
118+
119+
void HardwareSerial::println(uint8_t b)
120+
{
121+
print(b);
122+
println();
123+
}
124+
125+
void HardwareSerial::println(int n)
126+
{
127+
println((long) n);
128+
}
129+
130+
void HardwareSerial::println(long n)
131+
{
132+
print(n);
133+
println();
134+
}
135+
136+
void HardwareSerial::println(unsigned long n)
137+
{
138+
print(n);
139+
println();
140+
}
141+
142+
void HardwareSerial::println(long n, int base)
143+
{
144+
print(n, base);
145+
println();
146+
}
147+
148+
// Private Methods /////////////////////////////////////////////////////////////
149+
150+
void HardwareSerial::printNumber(unsigned long n, uint8_t base)
151+
{
152+
uint8_t buf[8 * sizeof(long)]; // Assumes 8-bit chars.
153+
int i = 0;
154+
if (n == 0) {
155+
print('0');
156+
return;
157+
}
158+
while (n > 0) {
159+
buf[i++] = n % base;
160+
n /= base;
161+
}
162+
for (i--; i >= 0; i--){
163+
print((char)(buf[i] < 10 ? '0' + buf[i] : 'A' + buf[i] - 10));
164+
}
165+
}
166+
167+
// Preinstantiate Objects //////////////////////////////////////////////////////
168+
169+
HardwareSerial Serial = HardwareSerial(0);
170+
//HardwareSerial Serial1 = HardwareSerial(1);
171+

targets/arduino/HardwareSerial.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
HardwareSerial.h - Hardware serial library for Wiring
3+
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef HardwareSerial_h
21+
#define HardwareSerial_h
22+
23+
#include <inttypes.h>
24+
25+
#define DEC 10
26+
#define HEX 16
27+
#define OCT 8
28+
#define BIN 2
29+
#define BYTE 0
30+
31+
class HardwareSerial
32+
{
33+
private:
34+
uint8_t _uart;
35+
void printNumber(unsigned long, uint8_t);
36+
public:
37+
HardwareSerial(uint8_t);
38+
void begin(long);
39+
uint8_t available(void);
40+
int read(void);
41+
void print(char);
42+
void print(char[]);
43+
void print(uint8_t);
44+
void print(int);
45+
void print(long);
46+
void print(unsigned long);
47+
void print(long, int);
48+
void println(void);
49+
void println(char);
50+
void println(char[]);
51+
void println(uint8_t);
52+
void println(int);
53+
void println(long);
54+
void println(unsigned long);
55+
void println(long, int);
56+
};
57+
58+
extern HardwareSerial Serial;
59+
//extern HardwareSerial Serial1;
60+
61+
#endif
62+

targets/arduino/Serial.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Serial.c - Serial library for Wiring
3+
Based on Hernando Barragan's original C implementation
4+
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#include "Serial.h"
22+
#include "uart.h"
23+
24+
void uart_init(uint8_t uart, long baudrate)
25+
{
26+
uartInit();
27+
uartSetBaudRate(baudrate);
28+
}
29+
30+
int uart_read(uint8_t uart)
31+
{
32+
return uartGetByte();
33+
}
34+
35+
uint8_t uart_available(uint8_t uart)
36+
{
37+
return uartGetRxBuffer()->datalength;
38+
}
39+
40+
void uart_write(uint8_t uart, char *buf, uint8_t len)
41+
{
42+
int i;
43+
for (i = 0; i < len; i++)
44+
uartSendByte(buf[i]);
45+
}

targets/arduino/Serial.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Serial.h - Serial library for Wiring
3+
Based on Hernando Barragan's original C implementation
4+
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#ifndef Serial_h
22+
#define Serial_h
23+
24+
#include <inttypes.h>
25+
26+
void uart_init(uint8_t, long);
27+
int uart_read(uint8_t);
28+
uint8_t uart_available(uint8_t);
29+
void uart_write(uint8_t, char*, uint8_t);
30+
31+
#endif
32+

targets/arduino/WProgram.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <avr/io.h>
22

33
#include "wiring.h"
4+
#include "HardwareSerial.h"
45

56
#include <avr/interrupt.h>
67
#include <avr/signal.h>

0 commit comments

Comments
 (0)