Commit Graph

578 Commits

Author SHA1 Message Date
Martino Facchin b38d7f07f0 implement PUSB modules as linked list 2015-07-16 13:12:14 +02:00
Martino Facchin 353ef39c23 remove useless variables 2015-07-16 13:12:14 +02:00
Martino Facchin a1ba49e7c8 make CDC function non removable 2015-07-16 13:12:14 +02:00
Martino Facchin afd126f96a add weak setupUSB() hook
override this function to insert additional USB endpoints
2015-07-16 13:12:14 +02:00
Martino Facchin 15edf1c6c8 remove useless variable 2015-07-16 13:12:14 +02:00
Martino Facchin d3815e7e36 add PluggableUSB module 2015-07-16 13:12:14 +02:00
Martino Facchin 8518b4a545 move _initEndpoints from PROGMEM to RAM 2015-07-16 13:12:14 +02:00
Martino Facchin f71f63a644 Use generic composite device descriptor 2015-07-16 13:12:13 +02:00
Martino Facchin f1973e73cb Move EP defines to header 2015-07-16 13:12:13 +02:00
Martino Facchin ea2a421570 Remove HID core library 2015-07-16 13:12:13 +02:00
Matthijs Kooijman 72a5ef3c78 Check for __cplusplus >= 201103L as well as __GXX_EXPERIMENTAL_CXX0X__
Gcc 4.8 defines __cplusplus as 201103L, so we can check for that now. It
still also defines __GXX_EXPERIMENTAL_CXX0X__, but this could help on
other compilers, or if gcc ever decides to stop defining the
experimental macro.
2015-07-16 13:06:10 +02:00
Federico Fissore 37c29845f6 Removed a handful of SVN expansion keywords. See #277 2015-07-03 10:38:07 +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
Martino Facchin 17b840d27f Add a Stream::find(char) method 2015-06-01 14:06:32 +00:00
Cristian Maglie 6c763b7df3 Fixed unused argument warning 2015-05-30 15:19:44 +02:00
Cristian Maglie 64dd45c3ce Fixed unused variables warnings and indentation 2015-05-29 20:02:50 +02:00
chromhelm b7855ab7e1 Saving some bytes in HardwareSerial::write 2015-05-29 18:12:24 +02:00
Martino Facchin ef4eb0cf6a pulseIn: be consistent with standard API
return 0 if timeout has been reached
2015-05-29 15:01:38 +02:00
Martino Facchin 9e7c7c4ddc pulseInASM: rework comment style 2015-05-29 15:01:38 +02:00
Martino Facchin 8dfa9dfb9e pulseIn: add alternative implementation based on micros()
pulseInLong is suitable for long pulses in interrupt context
2015-05-29 15:01:38 +02:00
Martino Facchin 6940c1d644 pulseIn: modify function to use ASM implementation 2015-05-29 15:01:37 +02:00
Martino Facchin 2bac15c63d pulseIn: add ASM implementation
this assembly code was generated by avr-gcc 4.8.3
2015-05-29 15:01:37 +02:00
Martino Facchin 1a63b36583 Revert "Temporary fix for pulseIn() regression."
This reverts commit 8ddc5198f6ef8b2816a61b7b0fcce99f927d11a0.
To be substituted by ASM generated code
2015-05-29 15:01:37 +02:00
Martino Facchin 550f8b61ac Merge pull request #3202 from Chris--A/randomSeed
Modified randomSeed, now uses unsigned long.
2015-05-28 10:54:26 +02:00
Martino Facchin b8787565db Merge pull request #3211 from Chris--A/println-optimize
Println optimize
2015-05-28 10:24:12 +02:00
Cristian Maglie 39c65393b8 Merge pull request #3244 from Lauszus/INT2
Check if INT2 is actually defined before issuing warning in detachInterrupt
2015-05-28 09:51:25 +02:00
Martino Facchin cd7eae098c Merge pull request #3207 from chromhelm/master
Fixed bug with HWSerial flushing
2015-05-28 09:44:46 +02:00
Kristian Sloth Lauszus 53d77c30ce Check if INT2 is actually defined before issuing warning in detachInterrupt
This was caused after: https://github.com/arduino/Arduino/pull/3221
2015-05-28 03:42:32 +02:00
Victor Aprea 1bc9ee33f8 Add missing case to detachinterrupt 2015-05-24 20:35:23 -04:00
Cristian Maglie 335d7b37ef Indent fix. 2015-05-22 17:23:46 +02:00
Cano 04d34da55f zero delay fix, ADC prescaler fix 2015-05-22 17:23:46 +02:00
Cano d3ba34d3a1 delayMicroseconds(): added support for 1Mhz, 12Mhz and 24Mhz
1Mhz is a default clock speed on Atmega328, many users run it on the
internal 1Mhz clock to save battery power. Up until now
delayMicroseconds() function wasn't taking this frequencies into an
account.
2015-05-22 17:23:45 +02:00
Chris--A 7562af1752 Optimize printLn calls. 2015-05-22 23:00:07 +10:00
chromhelm 3e7e138484 Fixed bug with flushing 2015-05-21 21:42:40 +02:00
Chris--A 9dc724b9d9 Modified randomSeed, now uses unsigned long. 2015-05-21 16:52:42 +10: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 ad7ede5647 Fixed indentation in Stream.cpp, no code change 2015-04-21 18:10:32 +02:00
Cristian Maglie 9e2fdcc8f4 Fixed wrong bracket placement (see #3011) 2015-04-21 18:07:42 +02:00
Kristian Sloth Lauszus 82eb5692c6 Fixed: warning: comparison between signed and unsigned integer expressions 2015-04-21 00:08:11 +02:00
Kristian Sloth Lauszus 2398ef5260 Fixed: warning: suggest explicit braces to avoid ambiguous 'else' 2015-04-21 00:08:02 +02:00
Martino Facchin 6cbf4db4b6 Merge branch 'jipp-master' 2015-04-14 10:13:58 +02:00
Jan d27eb7e1c1 Fix for issue #292
Replaced direct register manipulation with calls to bitWrite(). Fixed TIMSK5 usage on Leonardo (as well as some other preprocessor statements).
2015-04-13 20:36:28 +02:00
jipp 7e5b5bfcb5 Update Tone.cpp
Tone.cpp stops compile when TCCR0A/TCCR0B defined and WGM01 not defined #2923
2015-04-13 16:18:18 +02:00
Jan 331f5c832e Update Tone.cpp
Rebased the bugfix from the original Google Code issue #292 to work with Arduino 1.6.x

Description of original fix provided by Pete62:
The later 8 bit AVR's use two registers (TCCRxA, TCCRxB) whereas the ATmega8 only uses a single register (TCCR2) to house the control bits for Timer 2.  Bits were inadvertently being cleared.
2015-04-11 00:15:58 +02:00
Martino Facchin deff3bf628 Stream: set findMulti function as protected
let's not expose it until the API is not accepted
widely by the dev mailing list
2015-03-24 17:13:14 +01:00
Jim Leonard (Xuth) 8504a809dd Fix findUntil in Stream library
PR #2696 without timeout-related changes
2015-03-24 12:31:50 +01:00
Cristian Maglie 61c82a1937 Temporary fix for pulseIn() regression.
Fixes #2538
2015-01-18 17:34:40 +01:00
Cristian Maglie 17db7a7843 Arduino custom type boolean is now mapped to bool type
Fixes #2151
Fixes #2147
2015-01-07 14:56:19 +01: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
Cristian Maglie 02f5b99958 Revert "Match return value to type in available()"
This reverts commit f40e4713542fa862d5b99b256a642e001a796988.
Added an hint for the buffer sizes.

See #2057
Fixes #2367
2014-10-21 17:18:26 +02:00
Cristian Maglie d62d063082 Fixed missing NOT_AN_INTERRUPT constant in digitalPinToInterrupt()
Fixes #2379
2014-10-21 16:55:37 +02:00
George Chailazopoulos 62045646a1 Add #include "Print.h" in Server.h 2014-09-26 22:58:19 +03:00
Cristian Maglie 60e6ec22f1 Fixed trivial include error introduced in previous commit 2e3e539b... oops... 2014-09-12 16:58:05 +02:00
Cristian Maglie b70269a303 Added license to USBAPI.h
Close #1847
2014-09-12 12:27:39 +02:00
Cristian Maglie 3155eeeae9 Merge pull request #1937 from matthijskooijman/stringindex
String index fixes and cleanups
2014-09-12 11:56:12 +02:00
Matthijs Kooijman fde95cf5b5 Fix off-by-one in String::substring
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-10 13:42:06 +02:00
Cristian Maglie 0b094167de Merge pull request #2279 from cmaglie/fix-leo-upload-osx10.9.4
Fix for upload problems on Arduino Leonardo (and derivatives) on OSX 10.9
2014-09-10 12:58:10 +02:00
Matthijs Kooijman 35a84769d4 Simplify String::remove(unsigned int)
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-10 12:33:25 +02:00
Matthijs Kooijman b2729a5156 Fix bounds check in String::remove()
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-10 12:33:25 +02:00
Matthijs Kooijman dfb0dee773 Remove unneeded check in String::remove(unsigned int)
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.
2014-09-10 12:33:24 +02:00
Cristian Maglie f02d637a89 Fixed license for new.cpp and small cosmetic changes 2014-09-06 12:44:53 +02:00
Cristian Maglie a4496b927b Correct implementation of gcc specific internal functions
The following empty stubs has been replaced by the gcc
flag -fno-threadsafe-static:

  int __cxa_guard_acquire(__guard *);
  void __cxa_guard_release (__guard *);
  void __cxa_guard_abort (__guard *);

The following empty stubs has been moved into their specific
module abi.cpp:

  void __cxa_pure_virtual(void) __attribute ((noreturn));
  void __cxa_deleted_virtual(void) __attribute ((noreturn));

Fix #107
2014-09-06 12:42:25 +02:00
Cristian Maglie df778925a8 Printable.h doesn't need new.h but only stdlib.h for size_t.
Probalby new.h may be removed completely.
2014-09-06 12:37:57 +02:00
Cristian Maglie 1d0f3510f6 Fix for upload problems on Arduino Leonardo (and derivatives) with OSX 10.9.4 2014-09-01 12:16:41 +02:00
Cristian Maglie 5d87f9e8e2 Merge branch 'master' into ide-1.5.x 2014-08-08 15:44:41 +02:00
Christopher Andrews 28acfd7d9d Added replacement stub for cstdlib atexit() funciton.
This is an empty stub to simply allow use of complex types with a
non global static lifetime. For more complex handling the function
'atexit' can be redefined in user code.

For more information see:

https://github.com/arduino/Arduino/pull/2229
https://github.com/arduino/Arduino/issues/1919
2014-08-08 15:38:57 +02:00
Cristian Maglie e057236dd6 Merge remote-tracking branch 'arduino/master' into ide-1.5.x 2014-07-24 14:46:05 +02:00
Cristian Maglie b06b62d2f7 Merge pull request #2206 from amulya349/master
Fix of a bug #2199
2014-07-24 14:40:52 +02:00
Amulya Kumar Sahoo 710bd1fa2e Fix of a bug 2014-07-23 01:03:44 +05:30
Cristian Maglie 38e9fa1a5f Merge pull request #2190 from ribbons/compiler-warnings
Fix two compiler warnings generated by updated toolchain
2014-07-19 09:35:58 +02:00
PaulStoffregen bcc5488cbc Add availableForWrite() to HardwareSerial 2014-07-18 07:01:26 -07:00
Matt Robinson cd68a1c52c Cast empty string to char* to fix compiler warning
Stream::find(char *target) passes an empty terminator string to
Stream::findUntil(char *target, char *terminator) which caused a compiler
warning with the updated toolchain, so cast it to a char*.
2014-07-17 16:51:35 +01:00
Cristian Maglie 08f1010230 Merge branch 'ide-1.5.x-timer-variant' of github.com:matthijskooijman/Arduino into matthijskooijman-ide-1.5.x-timer-variant 2014-07-01 18:29:07 +02:00
Cristian Maglie b4fc466b47 initVariant() is now declared as external "C" function.
See #2080 for more details.
2014-06-27 14:26:32 +02:00
Cristian Maglie 1decf99e10 Merge branch 'ide-1.5.7' into ide-1.5.x 2014-06-27 13:44:18 +02:00
Matthijs Kooijman 0d40e5cace Support Atmega8 (and similar that define PE instead of UPE) again
These chips were previously supported, but since parity error checking
was added, this support has broken. Most chips define UPE0 (etc.) for
the parity error bit. Some chips don't have numbered UARTS so only
define UPE and even fewer define PE instead of UPE. This adds support
for those chips again.

Closes: #2137
2014-06-25 17:20:07 +02:00
Matthijs Kooijman 4fdf87c0e8 Allow variants to define an initVariant() function that is called at startup.
See #2080 and #2139.
2014-06-25 16:56:19 +02:00
Cristian Maglie 8cf34fd737 Removed patch for old malloc bug (no more useful with updated toolchain) 2014-06-25 10:14:35 +02:00
Cristian Maglie 7a31770a9a Allow variants to define an initVariant() function that is called at startup.
See #2080
2014-06-19 19:49:47 +02:00
Cristian Maglie 55000f4b72 Merge branch 'master' into ide-1.5.x 2014-06-19 17:01:53 +02:00
Cristian Maglie f599d51996 Fixed regression in USBAPI and CDC (xaljox)
See:
9ac7e30252 (commitcomment-6718676)
2014-06-19 16:52:48 +02:00
Cristian Maglie fca64de387 Merge branch 'master' into HEAD 2014-06-13 10:29:12 +02:00
Embedded Micro 12219b37d5 Update USBAPI.h
Fixes bug where Serial.read() would always return 0 as the first byte.
2014-06-10 08:48:23 -07:00
Cristian Maglie 2d72650feb Merge pull request #2104 from amulya349/master
Fix of a bug in Stream.cpp
2014-06-02 10:37:37 +02:00
Cristian Maglie 8bef5cdf03 Merge branch 'master' into HEAD 2014-05-30 10:54:14 +02:00
Cristian Maglie 29b0b63d1c USB CDC available() method returns correct number of bytes in buffer.
See #1953
2014-05-30 10:46:46 +02:00
Amulya Kumar Sahoo 43a8a0f433 Fix of a bug
Stream::find(char *target) passes NULL as “terminator” to Stream::findUntil(char *target, char *terminator), which immediately dereferences it by passing it on to strlen():
 
bool Stream::find(char *target)
{
  return findUntil(target, NULL);
}
 
// as find but search ends if the terminator string is found
bool Stream::findUntil(char *target, char *terminator)
{
  return findUntil(target, strlen(target), terminator, strlen(terminator));
}
2014-05-30 11:44:50 +05:30
Cristian Maglie 2fd1c5b428 Merge branch 'usbcdc-improved' into ide-1.5.x 2014-05-26 13:38:07 +02:00
Paul Brook 5962f155f7 Improve CDC read code
Read CDC data from USB FIFO on demand instead of in ISR.
Remove superfluous ring buffer.

Signed-off-by: Paul Brook <paul@nowt.org>
2014-05-24 00:34:56 +02:00
Paul Brook b57b2ae3c1 Fix race condition in USB CDC transmit
If the Start of Frame interrupt triggers just after the call
to USB_SendSpace in USB_Send then we can get data loss.
When the first bank is full and the second partially full,
the SOF handler will release the second bank via USB_Flush.
Data is then lost due to overflow as USB_Send continues writing data
to the now-closed bank.

Fix this by re-checking the FIFO status inside LockEP, immediately before
doing the data write.

Signed-off-by: Paul Brook <paul@nowt.org>
2014-05-24 00:34:56 +02:00
Justin Rajewski 872c88bb5f improved USB write speeds 2014-05-24 00:34:56 +02:00
Cristian Maglie f3e78a4a29 Backported Print class from ide-1.5.x branch
Close #1951
2014-05-24 00:13:07 +02:00
Cristian Maglie 1ee86b6bcb Merge branch 'ide-1.5.x-hwserial-cleanup' of github.com:matthijskooijman/Arduino into matthijskooijman-ide-1.5.x-hwserial-cleanup 2014-05-23 21:16:42 +02:00
Cristian Maglie 2d46a8ef88 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
Matthijs Kooijman 3a48c9240d Support TIMER1C
Some devices, such as the atmega2560 or the atmega256rfr2 have a timer1c
output. It seems this output is not connected to anything on the Arduino
Mega, but this allows using it on third party hardware nonetheless.
2014-05-16 18:57:04 +02:00
Zachary J. Fields c16768565b Match return value to type in available() 2014-05-07 17:39:08 -07:00
Matthijs Kooijman e66944a4a1 Remove unneeded register and ISR names in HardwareSerialx.cpp
Before, HardwareSerial1+.cpp were a copy of HardwareSerial1.cpp with all
0's replaced by the corresponding number. This would mean that e.g.
the Serial1 object would use the UBRRL register instead of UBRR1L when
it was defined, or the USART_RX_vect instead of USART1_RX_vect.

In practice, this would neve actually cause problems, since:
 - No avr chip currently has both the non-numbered registers as well as
   numbered registers.
 - HardwareSerial.h would only define HAVE_HWSERIALx when the
   corresponding numbered register is defined (except for
   HAVE_HWSERIAL0, which is also defined when the unnumbered registers
   are present).

Furthermore, before both the UARTx_xx_vect and USART_x_xx_vect was used.
Looking at the include files, only UART1_xx_vect is actually used (by
iom161.h), the others use USARTx_xx_vect. For this reason,
HardwareSerial1.cpp keeps the preprocessor conditional to select either
UART or USART and the other files use USART unconditionally.

While we're here, also fix the compiler error message when no valid ISR
name was found (it previously said "for the first UART" in all cases).
2014-05-06 10:26:46 +02:00
Matthijs Kooijman 8984e083cf Fix comment typo 2014-05-06 10:19:08 +02:00
Matthijs Kooijman 6d4273609b Fix EXTERNAL_NUM_INTERRUPTS for atmega128rfa1 and atmega256rfr2 2014-05-02 12:50:07 +02:00
Cristian Maglie b9f95980c7 Fixed other trivial warnings in AVR USB core.
See #1877
2014-04-20 23:08:55 +02:00
Cristian Maglie 15d74b2530 Removed other unused variables in CDC.cpp and HID.cpp
See #1877
2014-04-20 20:56:51 +02:00
Cristian Maglie 40d87e96d7 Removed 'USB_MANUFACTURER' constant redefinition for known VIDs.
See #1877
2014-04-20 20:49:04 +02:00
Cristian Maglie 945cf41bc3 Merge branch 'ide-1.5.x-warnings' of github.com:matthijskooijman/Arduino into ide-1.5.x 2014-04-20 19:36:29 +02:00
Cristian Maglie c3cfe6b368 Merge commit '1ad74' into ide-1.5.x 2014-04-01 17:19:54 +02:00
Cristian Maglie 9769bac51b Use correct type for index calculation in HardwareSerial 2014-04-01 17:18:02 +02:00
jantje 098307de60 I forgot a file 2014-04-01 16:14:16 +02:00
Matt Jenkins 908c526c4c Import WString from 1.5.6 2014-04-01 14:46:13 +01:00
Matt Jenkins 3fb1c595d1 Fixed string constructor overloading bug 2014-04-01 14:02:17 +01:00
Matthijs Kooijman 0099416ea4 Fix typo in SerialEvent3 handling
In commit 0e97bcb (Put each HardwareSerial instance in its own .cpp
file), the serial event handling was changed. This was probably a
copy-paste typo.

The effect of this bug was that SerialEvent3 would not run, unless
SerialEvent2 was defined, but also that if SerialEvent2 is defined but
SerialEvent3 is not, this could cause a reset (call to NULL pointer).

This closes #1967, thanks to Peter Olson for finding the bug and fix.
2014-03-27 19:20:54 +01: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
Matthijs Kooijman 53c0f1412d Don't store peeked characters in a char variable
peekNextDigit() returns an int, so it can return -1 in addition to all
256 possible bytes. By putting the result in a signe char, all bytes
over 128 will be interpreted as "no bytes available". Furthermore, it
seems that on SAM "char" is unsigned by default, causing the
"if (c < 0)" line a bit further down to always be false.

Using an int is more appropriate.

A different fix for this issue was suggested in #1399. This fix helps
towards #1728.
2014-02-19 16:09:30 +01:00
Matthijs Kooijman a2408d154e Instead of #defining true and false, include stdbool.h
In C++, true and false are language keywords, so there is no need to
define them as macros. Including stdbool.h in C++ effectively changes
nothing. In C, true, false and also the bool type are not available, but
including stdbool.h will make them available.

Using stdbool.h means that we get true, false and the bool type in
whatever way the compiler thinks is best, which seems like a good idea
to me.

This also fixes the following compiler warnings if a .c file includes
both stdbool.h and Arduino.h:

	warning: "true" redefined [enabled by default]
	 #define true 0x1

	warning: "false" redefined [enabled by default]
	#define false 0x0

This fixes #1570 and helps toward fixing #1728.

This only changed the AVR core, the SAM core already doesn't define true
and false (but doesn't include stdbool.h either).
2014-02-19 16:09:29 +01:00
Matthijs Kooijman f76c327aae Use a union in IPAddress for uint8_t[] <-> uint32_t conversion
Previously, pointer casting was used, but this resulted in strict-aliasing warnings:

IPAddress.h: In member function ‘IPAddress::operator uint32_t() const’:
IPAddress.h:46:61: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
     operator uint32_t() const { return *((uint32_t*)_address); };
                                                             ^
IPAddress.h: In member function ‘bool IPAddress::operator==(const IPAddress&) const’:
IPAddress.h:47:81: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
     bool operator==(const IPAddress& addr) const { return (*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); };
                                                                                 ^
IPAddress.h:47:114: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
     bool operator==(const IPAddress& addr) const { return (*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); };

Converting between unrelated types like this is commonly done using a union,
which do not break the strict-aliasing rules. Using that union, inside
IPAddress there is now an attribute _address.bytes for the raw byte
arra, or _address.dword for the uint32_t version.

Since we now have easy access to the uint32_t version, this also removes
two memcpy invocations that can just become assignments.

This patch does not change the generated code in any way, the compiler
already optimized away the memcpy calls and the previous casts mean
exactly the same.

This is a different implementation of a part of #1399 and it helps
toward fixing #1728.
2014-02-19 16:09:29 +01:00
Matthijs Kooijman 45b5aa3ebb In HardwareSerial::_rx_complete_irq, don't use int for buffer index
This was already fixed for HardwareSerial.cpp in #1863, but there was
one more case hidden in HardwareSerial_private.h.

The index attributes have been uint8_t for a while, so there is no point
in using int for local variables. This should allow the compiler to
generate slightly more efficient code, but (at least on gcc 4.8.2) it
also confuses the register allocator, causing this change to increase
code size by 2 bytes instead due to extra push/pop instructions (but
this will probably change in the future if the compiler improves).
2014-02-18 17:14:42 +01:00
Matthijs Kooijman 02a5ae97d4 In HardwareSerial, don't use int for buffer indices
The index attributes have been uint8_t for a while, so there is no point
in using int for local variables. This should allow the compiler to
generate slightly more efficient code, but (at least on gcc 4.8.2) it
also confuses the register allocator, causing this change to increase
code size by 2 bytes instead due to extra push/pop instructions (but
this will probably change in the future if the compiler improves).
2014-02-14 10:25:34 +01:00
Cristian Maglie 823d958418 Added license for Client, IPAddressm and Server (master branch)
See #1847 and #1117
2014-02-13 17:49:14 +01:00
Cristian Maglie 291f5493ec Added license for Arduino.h, binary.h and main.cpp (master branch)
See #1847 and #1117
2014-02-13 17:48:47 +01:00
Cristian Maglie dfde3ec99f Added license for Arduino.h, binary.h and main.cpp
See #1847
2014-02-10 12:55:16 +01:00
Cristian Maglie 4c3a3761b8 Added license for Client, IPAddressm and Server
See #1847
2014-02-10 12:55:16 +01:00
Cristian Maglie cd51a0784c Added license for avr/HardwareSerial.
See #1847
2014-02-10 12:55:16 +01:00
Matt Robinson 4c8a8a2d5b Reorder HardwareSerial init to fix compiler warn
Switch the tx and rx buffer head/tail entries in the HardwareSerial
initialisation list so that they match the order the fields are defined
in. This fixes a compiler warning (repeated for each of the
HardwareSerial source files the header is used in).
2014-01-29 20:10:32 +00:00
Matt Robinson 166a6c28ed Clean up unused var from HardwareSerial_private.h 2014-01-28 20:39:15 +00:00
Matthijs Kooijman c3cd35f197 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 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 1848db3d66 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 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 f1cd85da7a 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 dbe23685c2 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 fa8df58c93 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 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 bd194db4e3 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
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
Matthijs Kooijman 494929495e 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 fc45ef0846 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 d43fd2014c Remove unused variable 2014-01-16 13:52:40 +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 4bc8aa15e3 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
Matthijs Kooijman 46b0ada9a9 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
Federico Fissore 1c99bf0e83 Removed = char from #define. See https://github.com/arduino/Arduino/issues/1792#issuecomment-31650586 2014-01-06 18:20:37 +01:00
Federico Fissore 64a9681935 Added new EULER constant. Fixes #1792 2014-01-06 09:48:34 +01:00
Cristian Maglie 1eee97d980 Improved portability of String class (maniacbug) 2014-01-01 17:22:40 +01:00
Cristian Maglie bf37051397 Merge branch 'cast' of github.com:Lauszus/Arduino into Lauszus-cast
Conflicts:
	hardware/arduino/avr/cores/arduino/Print.cpp
2013-12-31 20:11:08 +01:00
Matthijs Kooijman 1978e82e4e Use PGM_P instead of prog_char
On later versions of avr-libc, prog_char is deprecated. In 0acebeeff48
the one occurence of prog_char was replaced by "char PROGMEM", which is
not entirely correct (PROGMEM is supposed to be an attribute on a
variable, not on a type, even though this is how things work in older
libc versions). However, in 1130fede3a2 a few new occurences of
prog_char are introduced, which break compilation on newer libc versions
again.

This commit changes all these pointer types to use the PGM_P macro from
<avr/pgmspace.h>. This macro is just "const char *" in newer libc
versions and "const prog_char *" in older versions, so it should always
work.

References #795
2013-12-31 20:01:40 +01:00
Kristian Lauszus ad9f78d727 Use reinterpret_cast to cast __FlashStringHelper to const char* 2013-12-27 20:01:03 +01:00
Matthijs Kooijman 94cf4c2830 Remove unneeded casts in Print::write(const String&)
Now that Print::write(const char*) is also available, these casts are no
longer needed.
2013-12-24 13:22:43 +01:00
Matthijs Kooijman 0fd4002607 Add uint8_t* versions of methods in Stream
The new functions just call their char* equivalents, but this allows
reading bytes into a buffer of uint8_t as well as chars.
2013-12-24 13:22:42 +01:00
Matthijs Kooijman 7ded037878 Add Print::write(const char *, size_t)
The new function just calls Print::write(const uint8_t *, size_t), but
this allows writing out a buffer of chars (without having to learn about
casts).
2013-12-24 13:22:42 +01:00
Federico Fissore dce9146485 Print.print optimization. Closes #1760 2013-12-23 11:50:39 +01:00