Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple UARTS always consume RAM #1425

Closed
xxxajk opened this issue May 18, 2013 · 4 comments
Closed

Multiple UARTS always consume RAM #1425

xxxajk opened this issue May 18, 2013 · 4 comments
Labels
Component: Core Related to the code for the standard Arduino API Component: HardwareSerial The hardware serial functionality of the core libraries
Milestone

Comments

@xxxajk
Copy link
Contributor

xxxajk commented May 18, 2013

If I only use Serial, but not Serial[123] buffers and classes are created for all four.

I have patched iomxx0_1.h to allow me to remove this extra bloat. I verified that it works just fine on my Mega1280.

I do this by defining DISABLE_SERIAL[0-3] on the compiler command line I can disable UARTS I am not using. If I am only using one UART, I can save 510 bytes of ram, which is a huge amount for a micro-controller.

If there is any way of eliminating the bloat without modifying the headers, that would be awesome, however I don't see any way to do this.

@xxxajk
Copy link
Contributor Author

xxxajk commented Jun 3, 2013

Here is the patch, it has been used by many people, and works wonderfully. I think that this should be expanded to other chips as well. Please note that this patch does nothing to currently written software, and it needs to be activated manually. Therefore it will not affect anything out in the wild, unless you specifically ask for the port(s) to be disabled.

Each disabled ports gives you ~170 bytes of additional on chip ram.
Disable 3, and that is 510 bytes, almost enough for an entire storage sector.
Many people would nearly kill to get an additional byte some times, let alone 510 :-)

Here is the patch, please let me know if you rather me make a pull request instead.

--- hardware/tools/avr/lib/avr/include/avr/__iomxx0_1.h 2013-05-17 19:41:47.000000000 -0400
+++ hardware/tools/avr/lib/avr/include/avr/iomxx0_1.h   2013-05-17 20:00:32.000000000 -0400
@@ -969,6 +969,8 @@

 /* Reserved [0xC3] */

+#ifndef DISABLE_SERIAL0
+
 /* Combine UBRR0L and UBRR0H */
 #define UBRR0   _SFR_MEM16(0xC4)

@@ -976,6 +978,7 @@
 #define UBRR0H  _SFR_MEM8(0xC5)

 #define UDR0    _SFR_MEM8(0XC6)
+#endif

 /* Reserved [0xC7] */

@@ -1011,6 +1014,7 @@

 /* Reserved [0xCB] */

+#ifndef DISABLE_SERIAL1
 /* Combine UBRR1L and UBRR1H */
 #define UBRR1   _SFR_MEM16(0xCC)

@@ -1018,7 +1022,7 @@
 #define UBRR1H  _SFR_MEM8(0xCD)

 #define UDR1    _SFR_MEM8(0XCE)
-
+#endif
 /* Reserved [0xCF] */

 #if defined(__ATmegaxx0__)
@@ -1054,7 +1058,7 @@
 # define UCPOL2 0

 /* Reserved [0xD3] */
-
+#ifndef DISABLE_SERIAL2
 /* Combine UBRR2L and UBRR2H */
 # define UBRR2  _SFR_MEM16(0xD4)

@@ -1062,6 +1066,7 @@
 # define UBRR2H _SFR_MEM8(0xD5)

 # define UDR2   _SFR_MEM8(0XD6)
+#endif

 #endif /* __ATmegaxx0__ */

@@ -1285,6 +1290,7 @@

 /* Reserved [0x133] */

+#ifndef DISABLE_SERIAL3
 /* Combine UBRR3L and UBRR3H */
 # define UBRR3  _SFR_MEM16(0x134)

@@ -1292,6 +1298,7 @@
 # define UBRR3H _SFR_MEM8(0x135)

 # define UDR3   _SFR_MEM8(0X136)
+#endif

 #endif /* __ATmegaxx0__ */

@@ -1398,6 +1405,7 @@
 #define SPI_STC_vect                   _VECTOR(24)
 #define SIG_SPI                                _VECTOR(24)

+#ifdef UBRR0H
 /* USART0, Rx Complete */
 #define USART0_RX_vect                 _VECTOR(25)
 #define SIG_USART0_RECV                        _VECTOR(25)
@@ -1409,6 +1417,7 @@
 /* USART0, Tx Complete */
 #define USART0_TX_vect                 _VECTOR(27)
 #define SIG_USART0_TRANS               _VECTOR(27)
+#endif

 /* Analog Comparator */
 #define ANALOG_COMP_vect               _VECTOR(28)
@@ -1442,6 +1451,7 @@
 #define TIMER3_OVF_vect                        _VECTOR(35)
 #define SIG_OVERFLOW3                  _VECTOR(35)

+#ifdef UBRR1H
 /* USART1, Rx Complete */
 #define USART1_RX_vect                 _VECTOR(36)
 #define SIG_USART1_RECV                        _VECTOR(36)
@@ -1453,6 +1463,7 @@
 /* USART1, Tx Complete */
 #define USART1_TX_vect                 _VECTOR(38)
 #define SIG_USART1_TRANS               _VECTOR(38)
+#endif

 /* 2-wire Serial Interface */
 #define TWI_vect                       _VECTOR(39)
@@ -1513,7 +1524,7 @@
 # define _VECTORS_SIZE 204

 #else
-
+#ifdef UBRR2H
 /* USART2, Rx Complete */
 #define USART2_RX_vect                 _VECTOR(51)
 #define SIG_USART2_RECV                        _VECTOR(51)
@@ -1525,7 +1536,9 @@
 /* USART2, Tx Complete */
 #define USART2_TX_vect                 _VECTOR(53)
 #define SIG_USART2_TRANS               _VECTOR(53)
+#endif

+#ifdef UBRR3H
 /* USART3, Rx Complete */
 #define USART3_RX_vect                 _VECTOR(54)
 #define SIG_USART3_RECV                        _VECTOR(54)
@@ -1537,6 +1550,7 @@
 /* USART3, Tx Complete */
 #define USART3_TX_vect                 _VECTOR(56)
 #define SIG_USART3_TRANS               _VECTOR(56)
+#endif

 # define _VECTORS_SIZE 228

@matthijskooijman
Copy link
Collaborator

The patch you submitted probably works, but requires modifying the avr-libc header files, which really is the wrong place to fix this. As promised, I've looked into this issue and just coded up a patch which fixes this in HardwareSerial.h/cpp, which is the more applicable place. I'll include that in my upcoming serial fixes pullrequest.

@xxxajk
Copy link
Contributor Author

xxxajk commented Dec 1, 2013

Sweet! I look forward to this!

On Sun, Dec 1, 2013 at 12:57 PM, Matthijs Kooijman <notifications@github.com

wrote:

The patch you submitted probably works, but requires modifying the
avr-libc header files, which really is the wrong place to fix this. As
promised, I've looked into this issue and just coded up a patch which fixes
this in HardwareSerial.h/cpp, which is the more applicable place. I'll
include that in my upcoming serial fixes pullrequest.


Reply to this email directly or view it on GitHubhttps://github.com//issues/1425#issuecomment-29578761
.

Visit my github for awesome Arduino code @ https://github.com/xxxajk

matthijskooijman added a commit to matthijskooijman/Arduino that referenced this issue Dec 18, 2013
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 arduino#1425 and fixes arduino#1259.
cmaglie pushed a commit to cmaglie/Arduino that referenced this issue Jan 22, 2014
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 arduino#1425 and fixes arduino#1259.
@cmaglie
Copy link
Member

cmaglie commented Jan 28, 2014

Fixed using Matthijs' patch. Will be released on 1.5.6.

@cmaglie cmaglie closed this as completed Jan 28, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Core Related to the code for the standard Arduino API Component: HardwareSerial The hardware serial functionality of the core libraries
Projects
None yet
Development

No branches or pull requests

3 participants