forked from plerup/espsoftwareserial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoftwareSerial.cpp
More file actions
173 lines (150 loc) · 5.18 KB
/
SoftwareSerial.cpp
File metadata and controls
173 lines (150 loc) · 5.18 KB
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
/*
SoftwareSerial.cpp - Implementation of the Arduino software serial for ESP8266.
Copyright (c) 2015 Peter Lerup. All rights 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
*/
#include <Arduino.h>
// The Arduino standard GPIO routines are not enough,
// must use some from the Espressif SDK as well
extern "C" {
#include "gpio.h"
}
#include <SoftwareSerial.h>
SoftwareSerial::SoftwareSerial(int receivePin, int transmitPin, bool invertedLogic, unsigned int buffSize) {
m_rxValid = m_txValid = false;
m_buffer = NULL;
m_inverseLogic =invertedLogic;
if (isValidGPIOpin(receivePin)) {
m_rxPin = receivePin;
m_buffSize = buffSize;
m_buffer = (uint8_t*)malloc(m_buffSize);
if (m_buffer != NULL) {
m_rxValid = true;
m_inPos = m_outPos = 0;
pinMode(m_rxPin, INPUT);
// Use SDK interrupt management as Arduino attachInterrupt doesn't take any parameter
ETS_GPIO_INTR_ATTACH(handle_interrupt, this);
GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, BIT(m_rxPin));
if (m_inverseLogic) {
gpio_pin_intr_state_set(GPIO_ID_PIN(m_rxPin), GPIO_PIN_INTR_POSEDGE);
} else {
gpio_pin_intr_state_set(GPIO_ID_PIN(m_rxPin), GPIO_PIN_INTR_NEGEDGE);
}
}
}
if (isValidGPIOpin(transmitPin)) {
m_txValid = true;
m_txPin = transmitPin;
pinMode(m_txPin, OUTPUT);
}
// Default speed
begin(9600);
}
SoftwareSerial::~SoftwareSerial() {
// No available SDK API to detach an interrupt handler,
// just disable the pin interrupt for now
gpio_pin_intr_state_set(GPIO_ID_PIN(m_rxPin), GPIO_PIN_INTR_DISABLE);
if (m_buffer)
free(m_buffer);
}
bool SoftwareSerial::isValidGPIOpin(int pin) {
// Some GPIO pins are reserved by the system
return (pin >= 0 && pin <= 5) || (pin >= 12 && pin <= 15);
}
void SoftwareSerial::begin(long speed) {
m_bitTime = round(1000000.0/speed);
if (m_bitTime < 5 || m_bitTime > 500) {
// Invalid speed
m_rxValid = m_txValid = false;
}
}
int SoftwareSerial::read() {
if (!m_rxValid || (m_inPos == m_outPos)) return -1;
uint8_t ch = m_buffer[m_outPos];
m_outPos = (m_outPos+1) % m_buffSize;
return ch;
}
int SoftwareSerial::available() {
return m_rxValid && ((m_inPos-m_outPos) > 0);
}
// Use micros loop to get as exect timing as possible
#define WAIT { while (micros()-start < wait); wait += m_bitTime; }
size_t SoftwareSerial::write(uint8_t b) {
if (!m_txValid) return 0;
// Disable interrupt in order to get a clean transmit
cli();
uint16_t wait = m_bitTime;
digitalWrite(m_txPin, HIGH);
unsigned long start = micros();
// Start bit;
digitalWrite(m_txPin, LOW);
WAIT;
for (int i = 0; i < 8; i++) {
digitalWrite(m_txPin, (b & 1) ? HIGH : LOW);
WAIT;
b >>= 1;
}
// Stop bit
digitalWrite(m_txPin, HIGH);
WAIT;
sei();
return 1;
}
void SoftwareSerial::flush() {
m_inPos = m_outPos = 0;
}
int SoftwareSerial::peek() {
if (!m_rxValid || (m_inPos == m_outPos)) return -1;
return m_buffer[m_outPos];
}
void SoftwareSerial::rxRead() {
uint16_t wait = m_bitTime;
unsigned long start = micros();
// Skip half start bit unless this is less than normal interrupt delay time
if (m_bitTime > 10) {
wait = m_bitTime/2;
WAIT;
}
uint8_t rec = 0;
for (int i = 0; i < 8; i++) {
WAIT;
rec >>= 1;
if (digitalRead(m_rxPin))
rec |= 0x80;
}
if (m_inverseLogic) rec=~rec;
// Stop bit
WAIT;
// Store the received value in the buffer unless we have an overflow
int next = (m_inPos+1) % m_buffSize;
if (next != m_inPos) {
m_buffer[m_inPos] = rec;
m_inPos = next;
}
}
void SoftwareSerial::handle_interrupt(SoftwareSerial *swSerObj) {
if (!swSerObj) return;
// Check if this interrupt was was coming from the the rx pin of this object
int pin = swSerObj->m_rxPin;
uint32_t gpioStatus = GPIO_REG_READ(GPIO_STATUS_ADDRESS);
if (!(gpioStatus & BIT(pin))) return;
// Clear the interrupt
GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, gpioStatus);
// Seems like the interrupt is delivered on all flanks in spite
// of GPIO_PIN_INTR_NEGEDGE. Hence ignore unless we have a start bit
if (digitalRead(pin)==!swSerObj->m_inverseLogic) return;
// Disable GPIO interrupts when sampling the incoming byte
ETS_GPIO_INTR_DISABLE();
swSerObj->rxRead();
ETS_GPIO_INTR_ENABLE();
}