-
Notifications
You must be signed in to change notification settings - Fork 76
/
HardwareSerial.h
195 lines (157 loc) · 6.24 KB
/
HardwareSerial.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//************************************************************************
//* HardwareSerial.h
//*
//* Arduino Library for PIC32
//* Copyright (c) 2010, 2011 by Mark Sproul
//************************************************************************
//* this code is best viewed with tabs set to 4 spaces
//************************************************************************
//* Edit History
//************************************************************************
//* Oct 12, 2010 Got MPLAB X working on MacOSX 1.6 for the first time
//* May 25, 2011 <MLS> Added support for Uart2 on UNO32
//* Jun 24, 2011 <MLS> Adding USB support fore Serial.xxx
//* Jun 29, 2011 <MLS> USB support only compiles if enabled
//* Aug 26, 2011 <MLS> Microchip starter kits w/USB now default to USB serial
//* Sep 1, 2011 <MLS> Issue #111, #ifdefs around <plib.h>, it was being included twice
//* Sep 2, 2011 <MLS> Issue #111, changed include <plib.h> to include <p32xxxx.h>
//* Nov 1, 2011 <MLS> Issue #140, HardwareSerial not derived from Stream
//* Nov 1, 2011 <MLS> Also fixed some other compatibilty issues
//* Nov 12, 2001 <GeneApperson> Rewrite for board variant support
//************************************************************************
/*
HardwareSerial.h - Hardware serial library for Wiring
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef HardwareSerial_h
#define HardwareSerial_h
#define __LANGUAGE_C__
#include <inttypes.h>
#include <p32xxxx.h>
#include "p32_defs.h"
#ifdef __cplusplus
#include "Print.h"
#include "Stream.h"
#endif
//* ------------------------------------------------------------
//* General Declarations
//* ------------------------------------------------------------
// Define constants and variables for buffering incoming serial data. We're
// using a ring buffer, in which rx_buffer_head is the index of the
// location to which to write the next incoming character and rx_buffer_tail
// is the index of the location from which to read.
// The algorithms used to operate on the head and tail assume that the
// size is a power of 2. (e.g. 32, 64, 128, etc)
#define RX_BUFFER_SIZE 128
typedef struct {
unsigned char buffer[RX_BUFFER_SIZE];
int head;
int tail;
} ring_buffer;
#ifdef __cplusplus
//* ------------------------------------------------------------
//* Object Class Declarations
//* ------------------------------------------------------------
class HardwareSerial : public Stream
{
private:
p32_uart * uart; //uart register map
uint8_t irq; //base IRQ number for the UART
uint8_t vec; //interrupt vector for the UART
uint8_t ipl; //interrupt priority level
uint8_t spl; //interrupt sub-priority level
p32_regset * ifs; //interrupt flag register set
p32_regset * iec; //interrupt enable control register set
uint32_t bit_err; //err interrupt flag bit
uint32_t bit_rx; //rx interrupt flag bit
uint32_t bit_tx; //tx interrupt flag bit
ring_buffer rx_buffer; //queue used for UART rx data
public:
HardwareSerial(p32_uart * uartP, int irq, int vec, int ipl, int spl);
void doSerialInt(void);
void begin(unsigned long baudRate);
void end();
virtual int available(void);
virtual int peek();
virtual int read(void);
virtual void flush(void);
virtual void write(uint8_t);
using Print::write; // pull in write(str) and write(buf, size) from Print
};
#if defined(_USB) && defined(_USE_USB_FOR_SERIAL_)
//*******************************************************************************************
class USBSerial : public Stream
{
private:
ring_buffer *_rx_buffer;
public:
USBSerial (ring_buffer *rx_buffer);
void begin(unsigned long baudRate);
void end();
virtual int available(void);
virtual int peek();
virtual int read(void);
virtual void flush(void);
virtual void write(uint8_t);
virtual void write(const char *str);
virtual void write(const uint8_t *buffer, size_t size);
using Print::write; // pull in write(str) and write(buf, size) from Print
};
#endif // defined(_USB) && defined(_USE_USB_FOR_SERIAL_)
//* ------------------------------------------------------------
//* Declaare Serial Port Objects
//* ------------------------------------------------------------
//* NUM_SERIAL_PORTS is defined in Board_Defs.h
#if defined(_USB) && defined(_USE_USB_FOR_SERIAL_)
/* If we're using USB for serial, the USB serial port gets
** instantiated as Serial and hardware serial port 0 gets
** instantiated as Serial0.
*/
extern USBSerial Serial;
#if (NUM_SERIAL_PORTS > 0)
extern HardwareSerial Serial0;
#endif
#else
/* If we're not using USB for serial, then hardware serial port 0
** gets instantiated as Serial.
*/
#if (NUM_SERIAL_PORTS > 0)
extern HardwareSerial Serial;
#endif
#endif
#if (NUM_SERIAL_PORTS > 1)
extern HardwareSerial Serial1;
#endif
#if (NUM_SERIAL_PORTS > 2)
extern HardwareSerial Serial2;
#endif
#if (NUM_SERIAL_PORTS > 3)
extern HardwareSerial Serial3;
#endif
#if (NUM_SERIAL_PORTS > 4)
extern HardwareSerial Serial4;
#endif
#if (NUM_SERIAL_PORTS > 5)
extern HardwareSerial Serial5;
#endif
#if (NUM_SERIAL_PORTS > 6)
extern HardwareSerial Serial6;
#endif
#if (NUM_SERIAL_PORTS > 7)
extern HardwareSerial Serial7;
#endif
//* ------------------------------------------------------------
#endif // __cplusplus
#endif // HardwareSerial_h
/************************************************************************/