aboutsummaryrefslogtreecommitdiff
AgeCommit message (Collapse)Author
2015-01-26Fix SoftwareSerial timingsMatthijs Kooijman
Instead of using a lookup table with (wrong) timings, this calculates the timings in SoftwareSerial::begin. This is probably a bit slower, but since it typically happens once, this shouldn't be a problem. Additionally, since the lookup tables can be removed, this is also a lot smaller, as well as supporting arbitrary CPU speeds and baudrates, instead of the limited set that was defined before. Furthermore, this switches to use the _delay_loop_2 function from avr-libc instead of a handcoded delay function. The avr-libc function only takes two instructions, as opposed to four instructions for the old one. The compiler also inlines the avr-libc function, which makes the timings more reliable. The calculated timings directly rely on the instructions generated by the compiler, since a significant amount of time is spent processing (compared to the delays, especially at higher speeds). This means that if the code is changed, or a different compiler is used, the calculations might need changing (though a few cycles more or less shouldn't cause immediate breakage). The timings in the code have been calculated from the assembly generated by gcc 4.8.2 and gcc 4.3.2. The RX baudrates supported by SoftwareSerial are still not unlimited. At 16Mhz, using gcc 4.8.2, everything up to 115200 works. At 8Mhz, it works up to 57600. Using gcc 4.3.2, it also works up to 57600 at 16Mhz and up to 38400 at 8Mhz. Note that at these highest speeds, communication works, but is still quite sensitive to other interrupts (like the millis() interrupts) when bytes are sent back-to-back, so there still are corrupted bytes in RX. TX works up to 115200 for all combinations of compiler and clock rates. This fixes #2019
2015-01-26Disable the RX PCINT inside SoftwareSerial::recvMatthijs Kooijman
Before, the interrupt would remain enabled during reception, which would re-set the PCINT flag because of the level changes inside the received byte. Because interrupts are globally disabled, this would not immediately trigger an interrupt, but the flag would be remembered to trigger another PCINT interrupt immediately after the first one is processed. Typically this was not a problem, because the second interrupt would see the stop bit, or an idle line, and decide that the interrupt triggered for someone else. However, at high baud rates, this could cause the next interrupt for the real start bit to be delayed so much that the byte got corrupted. By clearing the interrupt mask bit for just the RX pin (as opposed to the PCINT mask bit for the entire port), any PCINT events on other bits can still set the PCINT flag and be processed as normal. In this case, it's likely that there will be corruption, but that's inevitable when (other) interrupts happen during SoftwareSerial reception.
2015-01-26Optimize SoftwareSerial::setRxIntMsk()Matthijs Kooijman
This precalculates the mask register and value, making setRxIntMask considerably less complicated. Right now, this is not a big deal, but simplifying it allows using it inside the ISR next.
2015-01-26In SoftwareSerial::recv, only calculate the new tail onceMatthijs Kooijman
This shortens the generated code a bit more.
2015-01-26Mark SoftwareSerial::recv and handle_interrupt as always_inlineMatthijs Kooijman
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-26In SoftwareSerial, use ISR_ALIASOF to prevent duplicationMatthijs Kooijman
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-26Optimize SoftwareSerial::recvMatthijs Kooijman
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-26Further optimize SoftwareSerial::writeMatthijs Kooijman
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-26Mark SoftwareSerial::tx_pin_write as "always_inline"Matthijs Kooijman
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-26Simplify SoftwareSerial::writeMatthijs Kooijman
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-26Use stopListening() in SoftwareSerial::end()Matthijs Kooijman
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-26Fix race condition in SoftwareSerial::overflow()Matthijs Kooijman
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-26Toggle SoftwareSerial interrupts when starting / stopping to listenMatthijs Kooijman
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-26Add SoftwareSerial::stopListening()Matthijs Kooijman
This allows one to explicitly stop a SoftwareSerial instance from listening, without having to make another one listening.
2015-01-26Add SoftwareSerial::setRxIntMsk()Matthijs Kooijman
This moves the interrupt mask enabling / disabling code into a separate method, so we can call it from multiple spots next.
2015-01-26Let SoftwareSerial::end also check against _rx_delay_stopbitMatthijs Kooijman
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-26Let SoftwareSerial::listen() fail on invalid rx baud ratesMatthijs Kooijman
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-26Clear SoftwareSerial rx delay if no interrupt register is foundMatthijs Kooijman
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-19Merge branch 'sam-usart-mode-fix' into ide-1.5.xCristian Maglie
2015-01-18Temporary fix for pulseIn() regression.Cristian Maglie
Fixes #2538
2015-01-07Arduino custom type boolean is now mapped to bool typeCristian Maglie
Fixes #2151 Fixes #2147
2014-12-02Merge branch 'master' into ide-1.5.xCristian Maglie
Conflicts: hardware/arduino/cores/arduino/wiring.c libraries/Ethernet/src/utility/util.h libraries/Ethernet/src/utility/w5100.cpp libraries/Ethernet/src/utility/w5100.h libraries/Ethernet/util.h libraries/Ethernet/utility/util.h libraries/SPI/SPI.cpp libraries/SPI/SPI.h
2014-12-02Backported 'yield()' hook from 1.5.xCristian Maglie
2014-11-25Fix atomicity issues in SPI::beginTransaction and SPI::endTransaction ↵Cristian Maglie
(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[avr] Made SPI.usingInterrupt() synchronized (Andrew Kroll)Cristian Maglie
2014-11-25[avr] Added SPI.notUsingInterrupt() (Andrew Kroll)Cristian Maglie
2014-11-25[avr] Made SPI.begin() and SPI.end() synchronized (Andrew Kroll)Cristian Maglie
2014-11-25[avr] Improved SPI speed on 16bit transfer.Cristian Maglie
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-18Upped version to 1.6.0Cristian Maglie
2014-11-14[avr] SPI: removed redundant includeCristian Maglie
2014-11-14[avr] Small comments and headers fixes in SPICristian Maglie
2014-10-30Reduce avrdude verbosityMatthijs Kooijman
Previously, when verbose uploads were enabled, avrdude was run with four -v options, causing it to dump all raw bytes exchanged with the bootloader. This floods the console so much that meaningful output mostly disappears. Most users probably want to enable verbose mode just to see what avrdude command is ran. Furthermore, users that benefit from the raw bytes dumped are perfectly capable of either running avrdude manually, or modifying platform.txt. Given that, running avrdude with just one -v should be plenty. This fixes #891.
2014-10-21Merge branch 'master' into ide-1.5.xCristian Maglie
Conflicts: hardware/arduino/cores/arduino/HardwareSerial.cpp hardware/arduino/cores/robot/Arduino.h
2014-10-21Revert "Match return value to type in available()"Cristian Maglie
This reverts commit f40e4713542fa862d5b99b256a642e001a796988. Added an hint for the buffer sizes. See #2057 Fixes #2367
2014-10-21Fixed missing NOT_AN_INTERRUPT constant in digitalPinToInterrupt()Cristian Maglie
Fixes #2379
2014-10-01Merge branch 'master' into ide-1.5.x. Updated revision log.Cristian Maglie
Conflicts: build/shared/examples/09.USB/Keyboard/KeyboardLogout/KeyboardLogout.ino hardware/arduino/boards.txt
2014-10-01Merge pull request #2330 from spiritinlife/ide-1.5.xCristian Maglie
Add #include "Print.h" in Server.h Ide 1.5.x
2014-09-26Add #include "Print.h" in Server.hGeorge Chailazopoulos
2014-09-21set correct max size for mega2560 (to address issue #2277)Arnav Gupta
Signed-off-by: Arnav Gupta <championswimmer@gmail.com>
2014-09-20set correct max size for mega2560Arnav Gupta
Signed-off-by: Arnav Gupta <championswimmer@gmail.com>
2014-09-16Merge branch 'master' into ide-1.5.xCristian Maglie
Conflicts: app/src/processing/app/Base.java build/build.xml todo.txt
2014-09-12Fixed trivial include error introduced in previous commit 2e3e539b... oops...Cristian Maglie
2014-09-12Merge pull request #2299 from cmaglie/ide-1.5.x-spi-updatesCristian Maglie
Missing SPI Transaction API methods on Due (SAM3X)
2014-09-12Added license to USBAPI.hCristian Maglie
Close #1847
2014-09-12Merge pull request #1937 from matthijskooijman/stringindexCristian Maglie
String index fixes and cleanups
2014-09-10Fix off-by-one in String::substringMatthijs Kooijman
When checking the `left` argument, it previously allowed having left == len. However, this means the substring starts one past the last character in the string and should return the empty string. In practice, this already worked correctly, because buffer[len] contains the trailing nul, so it would (re)assign the empty string to `out`. However, fixing this check makes it a bit more logical, and prevents a fairly unlikely out-of-buffer write (to address 0x0) when calling substring on an invalidated String: String bar = (char*)NULL; bar.substring(0, 0);
2014-09-10Merge pull request #2279 from cmaglie/fix-leo-upload-osx10.9.4Cristian Maglie
Fix for upload problems on Arduino Leonardo (and derivatives) on OSX 10.9
2014-09-10Simplify String::remove(unsigned int)Matthijs Kooijman
Previously, this method calculated the length of the string from the given index onwards. However, the other remove() method called already contains code for this calculation, which is used when the count passed in is too big. This means we can just pass in a very big count that is guaranteed to point past the end of the string, shrinking the remove method by a few bytes.
2014-09-10Fix bounds check in String::remove()Matthijs Kooijman
Previously, if you passed in a very big index and/or count, the `index + count` could overflow, making the count be used as-is instead of being truncated (causing the string to be updated wrongly and potentially writing to arbitrary memory locations). We can rewrite the comparison to use `len - index` instead. Since we know that index < len, we are sure this subtraction does not overflow, regardless of what values of index and count we pass in. As an added bonus, the `len - index` value already needed be calculated inside the if, so this saves a few instructions in the generated code. To illustrate this problem, consider this code: String foo = "foo"; Serial.println(foo.length()); // Prints 3 foo.remove(1, 65535); // Should remove all but first character Serial.println(foo.length()); // Prints 4 without this patch Not shown in this is example is that some arbitrary memory is written as well.
2014-09-10Remove unneeded check in String::remove(unsigned int)Matthijs Kooijman
This check already happens in the remove(unsigned int, unsigned int) method that is caled, so there is no need to also check this here.