Commit Graph

184 Commits

Author SHA1 Message Date
Matthijs Kooijman 14c703096a In SoftwareSerial::recv, only calculate the new tail once
This shortens the generated code a bit more.
2015-01-26 17:03:25 +01:00
Matthijs Kooijman 42e23fb9bc Mark SoftwareSerial::recv and handle_interrupt as always_inline
Since those functions are only called once now, it makes sense to inline
them. This saves a few bytes of program space, but also saves a few
cycles in the critical RX path.
2015-01-26 17:03:25 +01:00
Matthijs Kooijman 8910658611 In SoftwareSerial, use ISR_ALIASOF to prevent duplication
Previously, up to four separate but identical ISR routines were defined,
for PCINT0, PCINT1, PCINT2 and PCINT3. Each of these would generate
their own function, with a lot of push-popping because another function
was called.

Now, the ISR_ALIASOF macro from avr-libc is used to declare just the
PCINT0 version and make all other ISRs point to that one, saving a lot
of program space, as well as some speed because of improved inlining.

On an Arduino Uno with gcc 4.3, this saves 168 bytes. With gcc 4.8, this
saves 150 bytes.
2015-01-26 17:03:25 +01:00
Matthijs Kooijman 8fd7324bcc Optimize SoftwareSerial::recv
Similar to SoftwareSerial::write, this rewrites the loop to only touch
the MSB and then shift those bits up, allowing the compiler to generate
more efficient code. Unlike the write function however, it is not needed
to put all instance variables used into local variables, for some reason
the compiler already does this (and doing it manually even makes the
code bigger).

On the Arduino Uno using gcc 4.3 this saves 26 bytes. Using gcc 4.8 this
saves 30 bytes.

Note that this removes the else clause in the code, making the C code
unbalanced, which looks like it breaks timing balance. However, looking
at the code generated by the compiler, it turns out that the old code
was actually unbalanced, while the new code is properly balanced.
2015-01-26 17:03:25 +01:00
Matthijs Kooijman 518cbca8ee Further optimize SoftwareSerial::write
This change restructures the loop, to help the compiler generate shorter
code (because now only the LSB of the data byte is checked and
subsequent bytes are shifted down one by one, it can use th "skip if bit
set" instruction).

Furthermore, it puts most attributes in local variables, which causes
the compiler to put them into registers. This makes the timing-critical
part of the code smaller, making it easier to provide accurate timings.

On an Arduino uno using gcc 4.3, this saves 58 bytes. On gcc 4.8, this
saves 14 bytes.
2015-01-26 17:03:25 +01:00
Matthijs Kooijman dd21593b53 Mark SoftwareSerial::tx_pin_write as "always_inline"
Somehow gcc 4.8 doesn't inline this function, even though it is always
called with constant arguments and can be reduced to just a few
instructions when inlined. Adding the always_inline attribute makes gcc
inline it, saving 46 bytes on the Arduino uno.

gcc 4.3 already inlined this function, so there are no space
savings there.
2015-01-26 17:03:25 +01:00
Matthijs Kooijman ba3f733493 Simplify SoftwareSerial::write
Before, there was nearly identical code for the inverted and regular
cases. However, simply inverting the byte in the inverted case allows
using the regular code twice, reducing the generated code size by 100
bytes (on an Arduino Uno and gcc 4.3, on gcc 4.8 the reduction is 50
bytes).
2015-01-26 17:03:25 +01:00
Matthijs Kooijman 0001ea1d3d Use stopListening() in SoftwareSerial::end()
stopListening also disabled the interrupt, if needed, so calling that
function makes more sense. Since stopListening only disables the
interrupt when the current SoftwareSerial is the active object, and that
can only be the case when _rx_delay_stopbit is non-zero, there is no
need to separately check _rx_delay_stopbit anymore.
2015-01-26 17:03:25 +01:00
Matthijs Kooijman a7aaa32b49 Fix race condition in SoftwareSerial::overflow()
If an interrupt causing overflow would occur between reading
_buffer_overflow and clearing it, this overflow condition would be
immediately cleared and never be returned by overflow().

By only clearing the overflow flag if an overflow actually occurred,
this problem goes away (worst case overflow() returns false even though
an overflow _just_ occurred, but then the next call to overflow() will
return true).
2015-01-26 17:03:25 +01:00
Matthijs Kooijman d6ad699c84 Toggle SoftwareSerial interrupts when starting / stopping to listen
This prevents interrupts from triggering when the SoftwareSerial
instance is not even listening.

Additionally, this removes the need to disable interrupts in
SoftwareSerial::listen, since no interrupts are active while it touches
the variables.
2015-01-26 17:03:25 +01:00
Matthijs Kooijman 09f0092949 Add SoftwareSerial::stopListening()
This allows one to explicitly stop a SoftwareSerial instance from
listening, without having to make another one listening.
2015-01-26 17:03:25 +01:00
Matthijs Kooijman 35075c337a Add SoftwareSerial::setRxIntMsk()
This moves the interrupt mask enabling / disabling code into a separate
method, so we can call it from multiple spots next.
2015-01-26 17:03:25 +01:00
Matthijs Kooijman 25b7c2e448 Let SoftwareSerial::end also check against _rx_delay_stopbit
The current check is still always false when the old check was, but
additionally it will not disable the interrupts when they were never
enabled (which shouldn't matter much, but this is more consistent).
2015-01-26 17:03:24 +01:00
Matthijs Kooijman b3b1f8e9d0 Let SoftwareSerial::listen() fail on invalid rx baud rates
In this case, SoftwareSerial::begin will not have enabled the
interrupts, so better not allow the SoftwareSerial instance to enter the
listening state either.
2015-01-26 17:03:24 +01:00
Matthijs Kooijman cb86359ee2 Clear SoftwareSerial rx delay if no interrupt register is found
Before enabling interupts, begin would see if the given receive pin
actually has an associated PCINT register. If not, the interrupts would
not be enabled.

Now, the same check is done, but when no register is available, the rx
parameters are not loaded at all (which in turn prevents the interrupt
from being enabled). This allows all code to use the same "is rx
enabled" (which will be added next).
2015-01-26 17:03:24 +01:00
Cristian Maglie 4c096dc6a9 Fix atomicity issues in SPI::beginTransaction and SPI::endTransaction (Andrew Kroll)
Previously, it could happen that SPI::beginTransaction was
interrupted by an ISR, while it is changing the SPI_AVR_EIMSK
register or interruptSave variable (it seems that there is
a small window after changing SPI_AVR_EIMSK where an interrupt
might still occur). If this happens, interruptSave is overwritten
with an invalid value, permanently disabling the pin interrupts.

To prevent this, disable interrupts globally while changing
these values.
2014-11-25 15:56:11 +01:00
Cristian Maglie 4db19b12c0 [avr] Made SPI.usingInterrupt() synchronized (Andrew Kroll) 2014-11-25 15:56:11 +01:00
Cristian Maglie b0eac9bd05 [avr] Added SPI.notUsingInterrupt() (Andrew Kroll) 2014-11-25 15:56:11 +01:00
Cristian Maglie 3e155441b5 [avr] Made SPI.begin() and SPI.end() synchronized (Andrew Kroll) 2014-11-25 15:49:17 +01:00
Cristian Maglie cd9ea1a371 [avr] Improved SPI speed on 16bit transfer.
From https://github.com/arduino/Arduino/pull/2376#issuecomment-59671152

Quoting Andrew Kroll:

   [..this commit..] introduces a small delay that can prevent the wait
   loop form iterating when running at the maximum speed. This gives
   you a little more speed, even if it seems counter-intuitive. At
   lower speeds, it is unnoticed. Watch the output on an oscilloscope
   when running full SPI speed, and you should see closer back-to-back
   writes.

Quoting Paul Stoffregen:

   I did quite a bit of experimenting with the NOP addition. The one
   that's in my copy gives about a 10% speedup on AVR.
2014-11-25 15:49:17 +01:00
Cristian Maglie 8b78659ca7 [avr] SPI: removed redundant include 2014-11-14 00:23:11 +01:00
Cristian Maglie 396dcd66c8 [avr] Small comments and headers fixes in SPI 2014-11-14 00:23:11 +01:00
PaulStoffregen 3791e4f260 SPI Transactions for AVR 2014-08-01 05:38:27 -07:00
Fede85 518c5a0fad missing paragraph field in library.properties 2014-07-18 20:08:01 +02:00
Fede85 4fa3f300b7 modified sentences in library.properties files 2014-07-18 19:41:34 +02:00
Cristian Maglie d767faae30 Merge pull request #1912 from Lauszus/issues440
Enable user to change the I2C clock frequency by calling setClock in the Wire library
2014-07-02 15:37:30 +02:00
Cristian Maglie 41975bf4f9 Merge remote-tracking branch 'arduino/master' into ide-1.5.x
Conflicts:
	build/shared/examples/01.Basics/Blink/Blink.ino
	build/shared/examples/09.USB/Keyboard/KeyboardReprogram/KeyboardReprogram.ino
	build/shared/examples/10.StarterKit/p02_SpaceshipInterface/p02_SpaceshipInterface.ino
	hardware/arduino/cores/arduino/HardwareSerial.cpp
2014-05-23 21:04:47 +02:00
Kristian Lauszus 955046c5ea Enable user to change the I2C clock frequency by calling setClock in the Wire library 2014-03-06 17:23:49 +01:00
Cristian Maglie 15c793a0e5 Revert "SPI library to new format" 2013-11-21 15:05:36 +01:00
Cristian Maglie 11630643ca Revert "EEPROM library to the new format"
This reverts commits:
3223d4fdca32ec03de4a3a2a0c22f2d40de5f374
77f8dd63ab102ab5d2929ac4edd5c00ae9d70493
2013-11-21 11:22:44 +01:00
Cristian Maglie c0630cf9bf Revert "SoftwareSerial library to the new format"
This reverts commit 38c3bbbd3c83eda057d4857635fbd78a4785c3a4.
2013-11-15 12:54:59 +01:00
Cristian Maglie 9a403664a6 Revert "Wire library to the 1.5 format"
This reverts commit a31857688bdc270ed65307755ff3b73ef4867982.
2013-11-15 12:54:59 +01:00
Fede85 9104bfc6f8 Wire library to the 1.5 format 2013-09-10 18:50:42 +02:00
Fede85 b0b96fa999 SpacebrewYun library to the 1.5 format 2013-09-06 18:25:03 +02:00
Fede85 1d6f34093a Temboo library to the 1.5 format 2013-09-06 18:15:14 +02:00
Fede85 dbdbe70b97 Bridge library to the 1.5 format 2013-09-06 15:38:07 +02:00
Federico Fissore 863cea6b4f Spacebrew keywords 2013-08-28 10:12:33 +02:00
Federico Fissore 5ee0aef579 spacebrew update 2013-08-28 10:12:33 +02:00
Federico Fissore 64e817bde6 removed .DS_Store folder 2013-08-28 10:12:33 +02:00
Cristian Maglie b868595a1e Merge branch 'ide-1.5.x' into dev-ide-1.5.x-discovery
Conflicts:
	app/src/processing/app/Preferences.java
	app/src/processing/app/debug/Uploader.java
2013-08-23 15:59:24 +02:00
Federico Fissore 5375c26cfb TemperatureWebPanel: widening refresh interval 2013-07-25 14:31:26 +02:00
Fede85 245c755b64 WiFi library to the new format 2013-07-19 16:20:34 +02:00
Federico Fissore fbf0c0de3b Bridge: Bridge.begin should wait more before giving up, as other processes may consume linux cpu power 2013-07-19 15:18:55 +02:00
Federico Fissore 531d41053b SpacebrewYun: added new example 2013-07-18 14:07:39 +02:00
Angelo Scialabba f7f29b4be2 Bridge: CRC16 being used as CRC 2013-07-15 16:41:17 +02:00
Federico Fissore 5774e611e4 updated temboo examples (added ControlBySMS) 2013-07-15 15:22:50 +02:00
Cristian Maglie 8330b196a4 Merge branch 'ide-1.5.x' into dev-ide-1.5.x-discovery
Conflicts:
	app/src/cc/arduino/packages/uploaders/SerialUploader.java
	app/src/processing/app/Editor.java
	app/src/processing/app/Sketch.java
	app/src/processing/app/debug/Uploader.java
2013-07-14 12:01:03 +02:00
Cristian Maglie 451d5b55a1 Merge branch 'master' into ide-1.5.x 2013-07-14 11:51:50 +02:00
Federico Fissore 2095d1e7cd File: implemented File.openNextFile() and File.rewindDirectory() 2013-07-11 18:14:49 +02:00
Federico Fissore 09a5884054 FileIO.h: removed wrong function declaration 2013-07-11 13:22:52 +02:00