Commit Graph

3580 Commits

Author SHA1 Message Date
Matt Robinson cd9657ffd0 Clean up unused var from HardwareSerial_private.h 2014-01-28 20:39:15 +00:00
Cristian Maglie 9ebfe233b8 Merge branch 'patch-1' of github.com:bobh66/Arduino into bobh66-patch-1 2014-01-28 19:01:26 +01:00
Cristian Maglie bcfa1a7479 Updated revision log 2014-01-28 18:33:08 +01:00
Cristian Maglie 5b48b1e4fe Merge branch 'serial-patch-2' into ide-1.5.x 2014-01-27 22:48:17 +01:00
Cristian Maglie 48c1223762 Update to jssc 2.8.0
Fixes #1811
2014-01-27 16:06:55 +01:00
Cristian Maglie f876733491 Updated manpage 2014-01-25 23:29:35 +01:00
Cristian Maglie a013ab2dda Added some string to translate 2014-01-23 17:24:13 +01:00
Cristian Maglie 895d394565 Added command line option "--preferences-file" to manually set the path of preferences. 2014-01-23 17:20:58 +01:00
Cristian Maglie dd02dcffab Merge branch 'master' into ide-1.5.x
Conflicts:
	app/src/processing/app/debug/Compiler.java
2014-01-23 13:17:46 +01:00
Matthijs Kooijman 9ad14b2f0c In HardwareSerial::write, bypass the queue when it's empty
This helps improve the effective datarate on high (>500kbit/s) bitrates,
by skipping the interrupt and associated overhead. At 1 Mbit/s the
implementation previously got up to about 600-700 kbit/s, but now it
actually gets up to the 1Mbit/s (values are rough estimates, though).
2014-01-22 12:06:02 +01:00
Cristian Maglie 275c0a02b1 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 0e97bcb2df Put each HardwareSerial instance in its own .cpp file
By putting the ISRs and HardwareSerial instance for each instance in a
separate compilation unit, the compile will only consider them for
linking when the instance is actually used. The ISR is always referenced
by the compiler runtime and the Serialx_available() function is always
referenced by SerialEventRun(), but both references are weak and thus do
not cause the compilation to be included in the link by themselves.

The effect of this is that when multiple HardwareSerial ports are
available, but not all are used, buffers are only allocated and ISRs are
only included for the serial ports that are used. On the mega, this
lowers memory usage from 653 bytes to just 182 when only using the first
serial port.

On boards with just a single port, there is no change, since the code
and memory was already left out when no serial port was used at all.

This fixes #1425 and fixes #1259.
2014-01-22 09:39:19 +01:00
Matthijs Kooijman 8e43c1a0cd 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 0be4e8cd3c Disable the UDRE interrupt sooner in HardwareSerial
Before, the interrupt was disabled when it was triggered and it turned
out there was no data to send. However, the interrupt can be disabled
already when the last byte is written to the UART, since write() will
always re-enable the interrupt when it adds new data to the buffer.

Closes: #1008
2014-01-22 09:38:25 +01:00
Matthijs Kooijman ccd8880a37 Fix lockup when writing to HardwareSerial with interrupts disabled
When interrupts are disabled, writing to HardwareSerial could cause a
lockup. When the tx buffer is full, a busy-wait loop is used to wait for
the interrupt handler to free up a byte in the buffer. However, when
interrupts are disabled, this will of course never happen and the
Arduino will lock up. This often caused lockups when doing (big) debug
printing from an interrupt handler.

Additionally, calling flush() with interrupts disabled while
transmission was in progress would also cause a lockup.

When interrupts are disabled, the code now actively checks the UDRE
(UART Data Register Empty) and calls the interrupt handler to free up
room if the bit is set.

This can lead to delays in interrupt handlers when the serial buffer is
full, but a delay is of course always preferred to a lockup.

Closes: #672
References: #1147
2014-01-22 09:38:16 +01:00
Matthijs Kooijman 4fb15f29ed Fix HardwareSerial::flush() when interrupts are kept disabled for a while
It turns out there is an additional corner case. The analysis in the
previous commit wrt to flush() assumes that the data register is always
kept filled by the interrupt handler, so the TXC bit won't get set until
all the queued bytes have been transmitted. But, when interrupts are
disabled for a longer period (for example when an interrupt handler for
another device is running for longer than 1-2 byte times), it could
happen that the UART stops transmitting while there are still more bytes
queued (but these are in the buffer, not in the UDR register, so the
UART can't know about them).

In this case, the TXC bit would get set, but the transmission is not
complete yet. We can easily detect this case by looking at the head and
tail pointers, but it seems easier to instead look at the UDRIE bit
(the TX interrupt is enabled if and only if there are bytes in the
queue). To fix this corner case, this commit:
 - Checks the UDRIE bit and only if it is unset, looks at the TXC bit.
 - Moves the clearing of TXC from write() to the tx interrupt handler.
   This (still) causes the TXC bit to be cleared whenever a byte is
   queued when the buffer is empty (in this case the tx interrupt will
   trigger directly after write() is called). It also causes the TXC bit
   to be cleared whenever transmission is resumed after it halted
   because interrupts have been disabled for too long.

As a side effect, another race condition is prevented. This could occur
at very high bitrates, where the transmission would be completed before
the code got time to clear the TXC0 register, making the clear happen
_after_ the transmission was already complete. With the new code, the
clearing of TXC happens directly after writing to the UDR register,
while interrupts are disabled, and we can be certain the data
transmission needs more time than one instruction to complete. This
fixes #1463 and replaces #1456.
2014-01-22 09:38:04 +01:00
Matthijs Kooijman 3d346518e0 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 722675bd1f Use bit_is_clear in HardwareSerial::flush()
This is slightly more clear than the previous explicit comparison.
2014-01-22 09:37:44 +01:00
Cristian Maglie a432e26861 Merge pull request #1814 from HeMan/master
Update for newer avr-gcc. (solves https://github.com/arduino/Arduino/issues/1807)
2014-01-22 00:34:37 -08:00
Cristian Maglie bd5a493c9f Merge pull request #1816 from HeMan/ide-1.5.x
Compile with -x assembler-with-cpp instead of -assembler-with-cpp.
2014-01-22 00:33:41 -08:00
Jimmy Hedman fb324358ee Compile with -x assembler-with-cpp instead of -assembler-with-cpp.
- Newer avr-gcc doesn't use -assembler-with-cpp, but
  uses -x assembler-with-cpp. This works with older compilers as well.
2014-01-21 21:57:35 +01:00
Jimmy Hedman 2fb3770757 Compile with -x assembler-with-cpp instead of -assembler-with-cpp.
- Newer avr-gcc doesn't use -assembler-with-cpp, but
  uses -x assembler-with-cpp. This works with older compilers as well.
2014-01-21 20:55:18 +01:00
Fede85 90fb863c01 better Client.stop() handling in WebClientRepeating and WifiWebClientRepeating examples 2014-01-21 20:38:52 +01:00
dpslwk 8364134ada Wire library, move hard references IRQn to defines in variant.h 2014-01-17 20:44:19 +00:00
Matthijs Kooijman 03fac844a8 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 e40cf5b7b8 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
Matthijs Kooijman 6ac8185c08 Define a _NOP() macro
Recent avr-libc releases define one, but this allows using it also on
older avr-libc releases.
2014-01-16 16:29:41 +01:00
Matthijs Kooijman 6cce4787bf Simplify HardwareSerial::begin()
This simplifies the baud rate calculation, removing the need for a goto
and shortening the code a bit. Other than that, this code should not use
any different settings than before.

Code was suggested by Rob Tillaart on github.

Closes: #1262
2014-01-16 16:04:33 +01:00
Matthijs Kooijman db5da3691e Remove unused variable 2014-01-16 13:52:40 +01:00
Cristian Maglie 1fab8c85e6 Slightly reduce code utilization by inlining HardwareSerail begin(baud) and operator bool() 2014-01-16 13:50:59 +01:00
Matthijs Kooijman f35ec75dce Remove duplicate code from HardwareSerial::begin() methods.
There are two begin methods, one which accepts just a baud rate and
uses the default bit settings and one which accepts both a baudrate and
a bit config. Previously, both of these contained a complete
implementation, but now the former just calls the latter, explicitely
passing the default 8N1 configuration.

Technically, this causes a small change: Before the UCSRC register was
untouched when calling begin(baud), now it is explicitely initialized
with 8N1. However, since this is the default configuration for at least
the Uno and the Mega (didn't check any others), probably for all avrs,
this shouldn't effectively change anything. Given that the Arduino
documentation also documents this as the default when none is passed,
explicitly setting it is probably a good idea in any case.
2014-01-16 13:20:11 +01:00
Cristian Maglie c2e9860cff Merge remote-tracking branch 'matthijs/ide-1.5.x-ipaddress-const' into ide-1.5.x 2014-01-16 13:17:44 +01:00
Matthijs Kooijman dde1a7541f Make some operators in IPAddress const
These functions do not modify the IPAddress object, but were not marked
as const. This meant that you could not do:

void set_ip(const IPAddress& ip) {
	uint32_t copy = ip;
}

Since calling operator uint32_t() on ip would discard the constness of
the reference.
2014-01-15 16:20:48 +01:00
Cristian Maglie 63e33be342 Merge tag '1.5.5-r2' into ide-1.5.x
Conflicts:
	build/shared/revisions.txt
	build/windows/dist/drivers/arduino.cat
2014-01-10 19:12:33 +01:00
Cristian Maglie 3d46d58b9f Signed drivers for Windows 8.1
Merge tag '1.0.5-r2' into HEAD

Conflicts:
	build/shared/revisions.txt
	build/windows/dist/drivers/arduino.cat
2014-01-10 19:10:27 +01:00
Cristian Maglie 2491c16d77 Merge branch 'lib-1.5-rev2' into HEAD 2014-01-09 14:56:23 +01:00
Cristian Maglie 8595d1444c Merge pull request #1790 from cmaglie/platform-paths
Fixed "runtime.hardware.path" and "runtime.platform.path" values
2014-01-09 04:30:00 -08:00
Cristian Maglie 711fe3d91a Merge tag '1.0.5-r2' into HEAD 2014-01-08 16:59:18 +01:00
Cristian Maglie e9c72c9278 Signed drivers for Windows 8.1 2014-01-08 16:58:32 +01:00
Cristian Maglie c2498aac5f Updated revisions log 2014-01-08 13:47:40 +01:00
Cristian Maglie 572592a178 Merge pull request #1794 from arduino/ide-1.5.x-euler
Added new EULER constant
2014-01-07 06:46:18 -08:00
Cristian Maglie 5925485e8f Updated drivers signature for Windows 2014-01-07 11:27:46 +01:00
Cristian Maglie 49166548ba Removed old Arduino Due drivers for Windows (leftovers) 2014-01-07 11:24:38 +01:00
Cristian Maglie e50b0ec030 Merge branch 'master' into ide-1.5.x
Conflicts:
	build/windows/dist/drivers/arduino.cat
2014-01-07 11:19:08 +01:00
Cristian Maglie 144b5e8815 Merge tag '1.0.5-r2' 2014-01-07 11:15:33 +01:00
Cristian Maglie 5c1b5c09e2 Updated drivers signature for Windows 2014-01-07 11:10:19 +01:00
Federico Fissore dfe77f388d Removed = char from #define. See https://github.com/arduino/Arduino/issues/1792#issuecomment-31650586 2014-01-06 18:20:37 +01:00
Federico Fissore 9fce7f1839 Added new EULER constant. Fixes #1792 2014-01-06 09:48:34 +01:00
Cristian Maglie 4dc21cee6c Fixed "runtime.hardware.path" and "runtime.platform.path" values
"runtime.hardware.path" now contains the path to the hardware folder
of the currently selected board and "runtime.platform.path" the path
to the specific platform.

This should fix #1176 and #1761.
2014-01-05 12:42:27 +01:00
Cristian Maglie 618f537691 Ethernet library refactoring
- removed arch folder
- merged socket.c and w5100.c for SAM and AVR, this is preparatory
  to make library vanilla
2014-01-05 08:50:52 +01:00