Skip to content

Commit 49d1112

Browse files
committed
Update of NeoHWSerial (https://github.com/SlashDevin/NeoHWSerial). Changes: - add required files for Arduino IDE >=v1.5.6 - renamed attachInterrupt()/detachInterrupt() to attachRxInterrupt()/attachTxInterrupt() - add attachTxInterrupt()/detachTxInterrupt() for user function on Tx-empty interrupt. Useful e.g. for sync break handling in LIN bus - call user Rx-ISR function with UART status byte. Useful e.g. for sync break handling in LIN bus - enable optional default action in user function via return value
1 parent a3aa029 commit 49d1112

14 files changed

+427
-814
lines changed

1.0.5/NeoHWSerial.cpp

-493
This file was deleted.

1.0.5/NeoHWSerial.h

-125
This file was deleted.

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 gicking
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
1-
The **NeoHWSerial** class is a drop-in replacement for the Arduino built-in class `HardwareSerial`. It adds the capability to handle received characters with a user-defined function *during* the RX interrupt. This can improve performance significantly by eliminating all processing associated with storing and retrieving characters in the ring buffer **and** all polling code that constantly checked for new characters: `available` and `read` are unnecessary.
1+
The **NeoHWSerial** class is a drop-in replacement for the Arduino built-in class `HardwareSerial`. It adds the capability to handle received characters with a user-defined function *during* the RX interrupt. This can improve performance significantly by eliminating all processing associated with storing and retrieving characters in the ring buffer **and** all polling code that constantly checked for new characters: `available` and `read` are unnecessary.
2+
3+
Note: This is a minor update of [NeoHWSerial by SlashDevin](https://github.com/SlashDevin/NeoHWSerial) for AVR. Changes:
4+
- updated to Arduino IDE >=v1.5.6 library format (see [here](https://arduino.github.io/arduino-cli/latest/library-specification/))
5+
- pass UART status byte to user receive function. Required e.g. for break detection in [LIN bus](https://en.wikipedia.org/wiki/Local_Interconnect_Network)
6+
- support optional storing to ring buffer after return from user routine via return value
7+
28

39
### Installation
410

511
1. Download the NeoHWSerial master zip file.
612
2. Extract all files into a tempory working directory.
7-
3. Create a `NeoHWSerial` subdirectory in your `Arduino/Libraries` directory.
8-
4. Copy all files from the version number subdirectory that corresponds to your IDE version into the `NeoHWSerial` subdirectory you created in step 1. For example, if you are using Arduino IDE version 1.0.5, copy all files from the extracted `NeoHWSerial-master/1.0.5` subdirectory into your `Arduino/Libraries/NeoHWSerial` subdirectory.
13+
3. Create a `NeoHWSerial` subdirectory in your `Arduino/libraries` directory.
914

1015
### Usage
1116

1217
To handle all received characters with your function, you must register it with the specific `NeoSerial[n]` instance:
1318

1419
#include <NeoHWSerial.h>
15-
20+
1621
volatile uint32_t newlines = 0UL;
17-
18-
static void handleRxChar( uint8_t c )
22+
23+
static bool handleRxChar( uint8_t c, uint8_t status )
1924
{
2025
if (c == '\n')
2126
newlines++;
27+
return false; // don't store c in ring buffer
2228
}
23-
29+
2430
void setup()
2531
{
2632
NeoSerial1.attachInterrupt( handleRxChar );
@@ -29,14 +35,14 @@ To handle all received characters with your function, you must register it with
2935

3036
Remember that the registered function is called from an interrupt context, and it should return as quickly as possible. Taking too much time in the function will cause many unpredictable behaviors, including loss of received data. See the similar warnings for the built-in [`attachInterrupt`](https://www.arduino.cc/en/Reference/AttachInterrupt) for digital pins.
3137

32-
The registered function will be called from the ISR whenever a character is received. The received character **will not** be stored in the `rx_buffer`, and it **will not** be returned from `read()`. Any characters that were received and buffered before `attachInterrupt` was called remain in `rx_buffer`, and could be retrieved by calling `read()`.
38+
The registered function will be called from the ISR whenever a character is received. The received character is stored in the `rx_buffer` after return from the user function, if the user function returns a _true_ value. Characters that were received and buffered before `attachInterrupt` was called remain in `rx_buffer`, and could be retrieved by calling `read()`.
3339

3440
If `attachInterrupt` is never called, or it is passed a `NULL` function, the normal buffering occurs, and all received characters must be obtained by calling `read()`.
3541

3642
The original `HardwareSerial` files were modified to include two new methods, `attachInterrupt` and `detachInterrupt`, and one new data member, the private `_isr`:
3743

3844
```
39-
typedef void (* isr_t)( uint8_t );
45+
typedef void (* isr_t)( uint8_t, uint8_t );
4046
void attachInterrupt( isr_t fn );
4147
void detachInterrupt() { attachInterrupt( (isr_t) NULL ); };
4248
@@ -48,9 +54,9 @@ The original `HardwareSerial` files were modified to include two new methods, `a
4854

4955
To avoid name collisions, all `HardwareSerial` instances are prefixed with "Neo" in this replacement library. All parts of your sketch, including other libraries, must use
5056

51-
* `NeoSerial` instead of `Serial`,
57+
* `NeoSerial` instead of `Serial`,
5258
* `NeoSerial1` instead of `Serial1`,
53-
* `NeoSerial2` instead of `Serial2`, and
59+
* `NeoSerial2` instead of `Serial2`, and
5460
* `NeoSerial3` instead of `Serial3`.
5561

5662
If there are any references to the original `HardwareSerial` instances, you will get one or more linker errors:

1.0.5/examples/echo/echo.ino renamed to examples/echo/echo.ino

+17-12
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
// interrupt with NeoHWSerial.
66
//
77

8-
static volatile uint16_t count = 0;
8+
static volatile uint16_t ISR_count = 0;
99

10-
static void char_received( uint8_t c )
10+
static bool char_received( uint8_t c, uint8_t status )
1111
{
1212
// This is a little naughty, as it will try to block
1313
// in this ISR if the tx_buffer is full. For this example,
1414
// we are only sending as many characters as we have received,
1515
// and they arrive at the same rate we are sending them.
1616
NeoSerial.write( c );
17-
count++;
17+
ISR_count++;
18+
return false; // do not store received data to ring buffer
1819
}
1920

2021
void setup()
@@ -28,17 +29,21 @@ void loop()
2829
{
2930
delay( 1000 );
3031

31-
uint8_t oldSREG = SREG;
32+
// get number of calls to char_received()
3233
noInterrupts();
33-
uint16_t old_count = count;
34-
count = 0;
35-
SREG = oldSREG;
36-
37-
if (old_count) {
34+
uint16_t echo_count = ISR_count;
35+
ISR_count = 0;
36+
interrupts();
37+
38+
if (echo_count)
39+
{
3840
NeoSerial.print( '\n' );
39-
NeoSerial.print( old_count );
41+
NeoSerial.print( echo_count );
4042
NeoSerial.println( F(" characters echoed") );
41-
} else
43+
}
44+
else
45+
{
4246
NeoSerial.print( '.' );
47+
}
4348
NeoSerial.flush();
44-
}
49+
}

keywords.txt

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
###############################################
2+
# Syntax coloring for NeoHWSerial library
3+
###############################################
4+
5+
###################################
6+
# Classes and Datatypes (KEYWORD1)
7+
###################################
8+
9+
NeoSerial KEYWORD1
10+
NeoSerial0 KEYWORD1
11+
NeoSerial1 KEYWORD1
12+
NeoSerial2 KEYWORD1
13+
NeoSerial3 KEYWORD1
14+
15+
###################################
16+
# Methods and Functions (KEYWORD2)
17+
###################################
18+
19+
begin KEYWORD2
20+
end KEYWORD2
21+
available KEYWORD2
22+
peek KEYWORD2
23+
read KEYWORD2
24+
availableForWrite KEYWORD2
25+
flush KEYWORD2
26+
write KEYWORD2
27+
attachInterrupt KEYWORD2
28+
detachInterrupt KEYWORD2
29+
serialEventRun KEYWORD2
30+
31+
###################################
32+
# Constants (LITERAL1)
33+
###################################
34+
35+
HAVE_HWSERIAL0 LITERAL1
36+
HAVE_HWSERIAL1 LITERAL1
37+
HAVE_HWSERIAL2 LITERAL1
38+
HAVE_HWSERIAL3 LITERAL1
39+
SERIAL_TX_BUFFER_SIZE LITERAL1
40+
SERIAL_5N1 LITERAL1
41+
SERIAL_6N1 LITERAL1
42+
SERIAL_7N1 LITERAL1
43+
SERIAL_8N1 LITERAL1
44+
SERIAL_5N2 LITERAL1
45+
SERIAL_6N2 LITERAL1
46+
SERIAL_7N2 LITERAL1
47+
SERIAL_8N2 LITERAL1
48+
SERIAL_5E1 LITERAL1
49+
SERIAL_6E1 LITERAL1
50+
SERIAL_7E1 LITERAL1
51+
SERIAL_8E1 LITERAL1
52+
SERIAL_5E2 LITERAL1
53+
SERIAL_6E2 LITERAL1
54+
SERIAL_7E2 LITERAL1
55+
SERIAL_8E2 LITERAL1
56+
SERIAL_5O1 LITERAL1
57+
SERIAL_6O1 LITERAL1
58+
SERIAL_7O1 LITERAL1
59+
SERIAL_8O1 LITERAL1
60+
SERIAL_5O2 LITERAL1
61+
SERIAL_6O2 LITERAL1
62+
SERIAL_7O2 LITERAL1
63+
SERIAL_8O2 LITERAL1
64+
65+
##################### END #####################

library.properties

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=NeoHWSerial
2+
version=1.6.6
3+
author=Georg Icking-Konert
4+
maintainer=Georg Icking-Konert
5+
sentence=NeoHWSerial for AVR
6+
paragraph=Adaptation of NeoHWSerial by SlashDevin for AVR
7+
category=Communication
8+
url=https://github.com/gicking/NeoHWSerial
9+
architectures=avr

0 commit comments

Comments
 (0)