Commit Graph

42 Commits

Author SHA1 Message Date
Nico edcfdba42d Fix HW Serial buffer size determination 2015-09-20 11:07:19 +02:00
Matthijs Kooijman 695d48cc79 Warn about increasing HardwareSerial buffers to > 256
Properly supporting bigger buffers need better atomicity code, which
isn't implemented yet. Until it is, best to add a warning. See #2405.
2015-06-18 16:36:07 +02:00
Nico 2e90af7ab6 RX and TX Serial buffer sizes can now be defined independently
Fixes #2597
2015-05-12 15:09:48 +02:00
Cristian Maglie 0f7a0ec4ed Merge branch 'master' into ide-1.5.x
Conflicts:
	hardware/arduino/cores/arduino/HardwareSerial.cpp
	hardware/arduino/cores/robot/Arduino.h
2014-10-21 17:47:59 +02:00
PaulStoffregen bcc5488cbc Add availableForWrite() to HardwareSerial 2014-07-18 07:01:26 -07:00
jantje f87e11534a Added support for different size of TX and RX buffer sizes.
Added support for buffer sizes bigger than 256 bytes.
Added possibility to overrule the default size.

Added support for different size of TX and RX buffer sizes.
The default values remain the same. You can however specify a different
value for TX and RX buffer

Added possibility to overrule the default size.
If you want to have different values
define SERIAL_TX_BUFFER_SIZE and SERIAL_RX_BUFFER_SIZE on the command
line


Added support for buffer sizes bigger than 256 bytes.
Because of the possibility to change the size of the buffer sizes longer
than 256 must be supported.
The type of the indexes is decided upon the size of the buffers. So
there is no increase in program/data size when the buffers are smaller
than 257
2014-03-24 21:40:12 +01:00
jantje 699e9c09ce This commit contains 2 changes:
Added support for different size of TX and RX buffer sizes.
Added support for buffer sizes bigger than 256 bytes.

Added support for different size of TX and RX buffer sizes.
The default values remain the same. If you want to have different values
define SERIAL_TX_BUFFER_SIZE and SERIAL_RX_BUFFER_SIZE on the command
line

Added support for buffer sizes bigger than 256 bytes.
The type of the indexes is decided upon the size of the buffers. So
there is no increase in program/data size when the buffers are smaller
than 257
2014-03-23 23:12:00 +01:00
Cristian Maglie cd51a0784c Added license for avr/HardwareSerial.
See #1847
2014-02-10 12:55:16 +01:00
Cristian Maglie 49fc2ab8ad Inlined HardwareSerial calls to RX ISR.
Moreover, declaring pointers-to-registers as const and using initializer
list in class constructor allows the compiler to further improve inlining
performance.

This change recovers about 50 bytes of program space on single-UART devices.

See #1711
2014-01-22 11:19:35 +01:00
Matthijs Kooijman 99f7ef7c67 Centrally decide which hardware UARTS are available
Before, this decision was made in few different places, based on
sometimes different register defines.

Now, HardwareSerial.h decides wich UARTS are available, defines
USE_HWSERIALn macros and HardwareSerial.cpp simply checks these macros
(together with some #ifs to decide which registers to use for UART 0).
For consistency, USBAPI.h also defines a HAVE_CDCSERIAL macro when
applicable.

For supported targets, this should change any behaviour. For unsupported
targets, the error messages might subtly change because some checks are
moved or changed.

Additionally, this moves the USBAPI.h include form HardareSerial.h into
Arduino.h and raises an error when both CDC serial and UART0 are
available (previously this would silently use UART0 instead of CDC, but
there is not currently any Atmel chip available for which this would
occur).
2014-01-22 09:38:34 +01:00
Matthijs Kooijman 560295c983 Improve HardwareSerial::flush()
The flush() method blocks until all characters in the serial buffer have
been written to the uart _and_ transmitted. This is checked by waiting
until the "TXC" (TX Complete) bit is set by the UART, signalling
completion. This bit is cleared by write() when adding a new byte to the
buffer and set by the hardware after tranmission ends, so it is always
guaranteed to be zero from the moment the first byte in a sequence is
queued until the moment the last byte is transmitted, and it is one from
the moment the last byte in the buffer is transmitted until the first
byte in the next sequence is queued.

However, the TXC bit is also zero from initialization to the moment the
first byte ever is queued (and then continues to be zero until the first
sequence of bytes completes transmission). Unfortunately we cannot
manually set the TXC bit during initialization, we can only clear it. To
make sure that flush() would not (indefinitely) block when it is called
_before_ anything was written to the serial device, the "transmitting"
variable was introduced.

This variable suggests that it is only true when something is
transmitting, which isn't currently the case (it remains true after
transmission is complete until flush() is called, for example).
Furthermore, there is no need to keep the status of transmission, the
only thing needed is to remember if anything has ever been written, so
the corner case described above can be detected.

This commit improves the code by:
 - Renaming the "transmitting" variable to _written (making it more
   clear and following the leading underscore naming convention).
 - Not resetting the value of _written at the end of flush(), there is
   no point to this.
 - Only checking the "_written" value once in flush(), since it can
   never be toggled off anyway.
 - Initializing the value of _written in both versions of _begin (though
   it probably gets initialized to 0 by default anyway, better to be
   explicit).
2014-01-22 09:37:54 +01:00
Matthijs Kooijman 80d6af6273 Move interrupt handlers into HardwareSerial class
The actual interrupt vectors are of course defined as before, but they
let new methods in the HardwareSerial class do the actual work. This
greatly reduces code duplication and prepares for one of my next commits
which requires the tx interrupt handler to be called from another
context as well.

The actual content of the interrupts handlers was pretty much identical,
so that remains unchanged (except that store_char was now only needed
once, so it was inlined).

Now all access to the buffers are inside the HardwareSerial class, the
buffer variables can be made private.

One would expect a program size reduction from this change (at least
with multiple UARTs), but due to the fact that the interrupt handlers
now only have indirect access to a few registers (which previously were
just hardcoded in the handlers) and because there is some extra function
call overhead, the code size on the uno actually increases by around
70 bytes. On the mega, which has four UARTs, the code size decreases by
around 70 bytes.
2014-01-16 16:59:06 +01:00
Matthijs Kooijman 3babfc2a85 Use constants for register bit positions in HardwareSerial
Previously, the constants to use for the bit positions of the various
UARTs were passed to the HardwareSerial constructor. However, this
meant that whenever these values were used, the had to be indirectly
loaded, resulting in extra code overhead. Additionally, since there is
no instruction to shift a value by a variable amount, the 1 << x
expressions (inside _BV and sbi() / cbi()) would be compiled as a loop
instead of being evaluated at compiletime.

Now, the HardwareSerial class always uses the constants for the bit
positions of UART 0 (and some code is present to make sure these
constants exist, even for targets that only have a single unnumbered
UART or start at UART1).

This was already done for the TXC0 constant, for some reason. For the
actual register addresses, this approach does not work, since these are
of course different between the different UARTs on a single chip.

Of course, always using the UART 0 constants is only correct when the
constants are actually identical for the different UARTs. It has been
verified that this is currently the case for all targets supported by
avr-gcc 4.7.2, and the code contains compile-time checks to verify this
for the current target, in case a new target is added for which this
does not hold. This verification was done using:

for i in TXC RXEN TXEN RXCIE UDRIE U2X UPE; do echo $i; grep --no-filename -r "#define $i[0-9]\? " /usr/lib/avr/include/avr/io* | sed "s/#define $i[0-9]\?\s*\(\S\)\+\s*\(\/\*.*\*\/\)\?$/\1/" | sort | uniq ; done

This command shows that the above constants are identical for all uarts
on all platforms, except for TXC, which is sometimes 6 and sometimes 0.
Further investigation shows that it is always 6, except in io90scr100.h,
but that file defines TXC0 with value 6 for the UART and uses TXC with
value 0 for some USB-related register.

This commit reduces program size on the uno by around 120 bytes.
2014-01-16 16:36:06 +01:00
Cristian Maglie 0a0cda4f04 Slightly reduce code utilization by inlining HardwareSerail begin(baud) and operator bool() 2014-01-16 13:50:59 +01:00
Matthijs Kooijman d1da7ef303 Make private members of HardwareSerial protected
This allows users to create subclasses.

Closes: #947
2013-07-26 12:41:35 +02:00
Matthijs Kooijman 714874dd8c Move buffers into HardwareSerial
This removes the need for doing an extra pointer dereference on every
access to the buffers, shrinking the code by around 100 bytes.

The members for these buffers must be public for now, since the
interrupt handlers also need to access them. These can later be made
private again.

Furthermore, the struct ring_buffer was removed. This allows the all
head and tail pointers to be put into the HardwareSerial struct before
the actual buffers, so the pointers all end up in the first 32 bytes of
the struct that can be accessed using a single instruction (ldd).

References: #947
2013-07-26 12:39:56 +02:00
David A. Mellis c4337d812b Moving TXCO definition into HardwareSerial.cpp from HardwareSerial.h.
Otherwise, you get an error when compiling for processors with no serial port because the header file is always compiled.

See, for an example of the problem: https://github.com/damellis/attiny/issues/8
2012-12-08 10:44:48 -05:00
David A. Mellis 4a01b84cab Moving TXCO definition into HardwareSerial.cpp from HardwareSerial.h.
Otherwise, you get an error when compiling for processors with no serial port because the header file is always compiled.

See, for an example of the problem: https://github.com/damellis/attiny/issues/8
2012-11-29 13:48:01 -05:00
David A. Mellis 769aab0115 HardwareSerial: change byte to uint8_t (since byte definition isn't present). 2012-11-02 09:24:51 -04:00
David A. Mellis 5ca747e312 Changing setting of the UMSELn bits (for UART mode) and serial config values.
Before, the UMSELn1 bit was being to set to 1, putting the UART into a reserved mode. Now, we only set the high (0x80) bit to 1 for the ATmega8, which is needed to access UCSRnC (whose i/o address is shared with UBRRH).

Also, no longer bitwise-or the new config with the existing register value, because we're actually configuring all the settings in the register. (We're not using UCPOL, but it's supposed to be 0 in asynchronous mode.)
2012-08-30 08:47:35 -04:00
David A. Mellis 1650169f5d Renaming serial config constants to, e.g., SERIAL_8N1. 2012-08-30 08:21:12 -04:00
David A. Mellis 6542625bc2 Merge pull request #109 from Alarus/master
Serial.begin() parameter to set data bits, parity, stop bits.
2012-08-30 05:08:28 -07:00
David A. Mellis 70b6f11d63 Fixing Serial.flush() breakage on Leonardo (WestFW).
http://code.google.com/p/arduino/issues/detail?id=1020
2012-08-30 07:44:25 -04:00
David A. Mellis 912092b03f Fixing ATmega8 breakage from flush() change. (WestFW)
http://code.google.com/p/arduino/issues/detail?id=1019
2012-08-29 20:52:30 -04:00
David A. Mellis c29b408a9d Adding overloads so Serial.write(0) works.
http://code.google.com/p/arduino/issues/detail?id=1006
2012-08-29 16:32:05 -04:00
David A. Mellis 00ab72619e Serial.flush() waits for last character to be transmitted (michele.mazzucchi)
http://code.google.com/p/arduino/issues/detail?id=871
2012-08-28 08:02:54 -04:00
Alarus 1cda182f33 Update hardware/arduino/cores/arduino/HardwareSerial.h
New Serial.begin(baud, config);
2012-08-14 19:52:00 +06:00
Alarus 03a4b50b8e Update hardware/arduino/cores/arduino/HardwareSerial.h
Adding advanced begin (); with the ability to specify the length of bits, parity, stop bits.
2012-08-12 20:23:00 +06:00
Alarus b9bbc71dca Update hardware/arduino/cores/arduino/HardwareSerial.h
Adding advanced begin (); with the ability to specify the length of bits, parity, stop bits.
2012-08-12 20:18:50 +06:00
Zach Eveland a984b581a8 added Boolean operators to HardwareSerial and CDC to test whether the port is ready to send data.
Mostly useful for Leonardo - simple way to test whether the port is actually opened by an application and ready to receive data.  For Serial objects attached to real UARTs always returns true.
2012-04-01 12:54:35 -04:00
Zach Eveland 3524f2ff0b Revert "Merge branch 'master' of github.com:arduino/Arduino into diskloader_reboot"
This reverts commit df9835efafd13685251749bc210c0b96a18a96a5, reversing
changes made to ec45af8bfa9222a807c075dd1db4b5aa798bba03.

Conflicts:

	hardware/arduino/variants/mega/pins_arduino.h
	libraries/Ethernet/examples/PachubeClientString/PachubeClientString.ino
2011-10-27 11:45:13 -04:00
David A. Mellis 5515442dbb Small optimization in HardwareSerial.
begin(long) -> begin(unsigned long)
2011-02-25 18:36:46 -05:00
David A. Mellis 8b09b6be80 Making HardwareSerial tests register-based, not CPU-based.
Mark Sproul's patches, via Christian Maglie.
http://code.google.com/p/arduino/issues/detail?id=315
2010-10-17 13:36:02 -04:00
David A. Mellis f9a3b5cfa9 1280 -> 1280/2560. 2010-09-08 22:33:33 +00:00
David A. Mellis 2b1c9aea35 changing available() to return an int (because the Ethernet Client class or another stream might need more than 255 bytes). 2010-08-02 22:23:48 +00:00
David A. Mellis d3cb43c1c9 Adding a peek() function to Stream and HardwareSerial (Serial). 2010-07-04 23:31:55 +00:00
David A. Mellis 90eb0aecd8 Adding a basic Stream interface and modifying HardwareSerial to inherit from it. 2010-05-28 22:41:03 +00:00
David A. Mellis fe28586154 Adding Serial.end(). 2009-12-23 00:00:17 +00:00
David A. Mellis 35f5f6e99f Improving third-party hardware support:
- moving back to multple cores per platform
- using target instead of platform
- moving per-board and per-programmer preferences out of Preferences.java and into a new Target class
- adding a new "target" preference
- support for platform:value values in board preferences for bootloader path and core
- XXX: need to support platform:value syntax for board upload.using preferences.
2009-11-21 23:23:43 +00:00
David A. Mellis 584dece7b0 Moving things around. 2009-11-07 17:05:21 +00:00
David A. Mellis 2fc32fc341 Clearing the U2Xn bit in Serial.begin() (because it is set on the Pro / Pro Mini 328). Fixing some indentation. 2009-07-12 01:58:15 +00:00
David A. Mellis db605dd18b First integration of the Arduino code in Processing 5503: PreProcessor and Compiler have been integrated with changes to the Sketch.
Compilation still has problems (Thread error on success, and can't handle non-pde files in a sketch).
Modified the Mac OS X make.sh to copy the hardware, avr tools, and example over.
Removing some of the antlr stuff.  
Disabling the Commander (command-line execution) for now.
Added Library, LibraryManager, and Target.
Added support for prefixed preferences (e.g. for boards and programmers).
2009-06-01 08:32:11 +00:00