Skip to content

Commit a48606b

Browse files
committed
comment changes.
1 parent 0bfc3ff commit a48606b

2 files changed

Lines changed: 30 additions & 19 deletions

File tree

megaavr/cores/megatinycore/HardwareSerial.h

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,17 @@
6969
* Since the USE_ASM_* = 1 option is apparently working, we do not recommend disabling it, as it will waste flash and hurt performance.
7070
*
7171
* Flash versus RAM table
72-
* | | modern tinyAVR series parts | Other modern parts |
73-
* | Flash | 0-series | 1-series | 2-series | mega | All Dx | EA | EB |
74-
* |-------|----------|----------|----------|------|--------|------|------|
75-
* | 2048 | 128 | 128 | - | - | - | - | - |
76-
* | 4096 | 256 | 256 | 512 | - | - | - | - |
77-
* | 8192 | 512 | 512 | 1024 | 1024 | - | 1024 | 1024 |
78-
* | 16384 | 1024 | 2048 | 2048 | 2048 | 2048 | 2048 | 2048 |
79-
* | 32768 | - | 2048 | 3072 | 4096 | 4096 | 4096 | 3072 |
80-
* | 49152 | - | - | - | 6120 | - | - | - |
81-
* | 65536 | - | - | - | - | 8192 | 6120 | - |
82-
* | 128k | - | - | - | - | 16384 | - | - |
72+
* | | modern tinyAVR series parts | Other modern parts | |
73+
* | Flash | 0-series | 1-series | 2-series | mega | All Dx | EA | EB | SD |
74+
* |-------|----------|----------|----------|------|--------|------|------|------|
75+
* | 2048 | 128 | 128 | - | - | - | - | - | - |
76+
* | 4096 | 256 | 256 | 512 | - | - | - | - | - |
77+
* | 8192 | 512 | 512 | 1024 | 1024 | - | - | - | - |
78+
* | 16384 | 1024 | 2048 | 2048 | 2048 | 2048 | 2048 | 2048 | - |
79+
* | 32768 | - | 2048 | 3072 | 4096 | 4096 | 4096 | 3072 | 4096 |
80+
* | 49152 | - | - | - | 6120 | - | - | - | - |
81+
* | 65536 | - | - | - | - | 8192 | 6120 | - | 8182 |
82+
* | 128k | - | - | - | - | 16384 | - | - | - |
8383
* This ratio is remarkably consistent. No AVR part was ever made with less than 8:1 flash:ram,
8484
* nor more than 16:1, since the earliest recognizable AVRs. I am only aware of one exception. Was it some bizarro part
8585
* from the dark ages? Nope - it's the surprisingly popular ATmega2560!
@@ -97,8 +97,13 @@
9797
*
9898
* (the two numbers in final column are given because 0/1-serieas has 1 port, but tiny2 has 2, though if you only use one, you only
9999
* get one set of buffers)
100+
*
101+
* The rationale behind those numbers is simple: The ring buffer code has calculate a modulo (several hundred clock cycles, and it would have to do it in an ISR - yeah, not good.
102+
* But if you cheat by having a power of two size of the buffer, you can do the calculation in a low single digit number of clocks. So it's gotta be a power of 2.
103+
* And RX iis obviously more important than TX for the buffer size, because a TX underrun will not lose your data, just slow down transmission, while a RX overrun means lost data
104+
* But TX isn't unimportant, because of the importance of serial logging to Arduino development. It is very easily to, in an attempt to log enough information to debug the problem,
105+
* force the CPU to spend most of it's time waiting for a chance to put the next byte of log output into the buffer, which is full.
100106
*/
101-
102107
/* Buffer Sizing */
103108

104109
// The buffer sizes can be overridden in by defining SERIAL_TX_BUFFER either in variant file (as defines in pins_arduino.h) or boards.txt (By passing them as extra flags).
@@ -154,8 +159,13 @@
154159
#endif
155160

156161
/* Buffer sizing done */
157-
158-
162+
/* DANGER DANGER DANGER */
163+
/* The ASM implementations push the envelope, *hard*. The asm implementations themselves are only slightly better than what the compiler makes. That improvement does not justify the asm.
164+
* The magic is all from a couple of tricks which, if you look at them carefully, all turn out to rely on one specific trick. That trick, strictly speaking, is illegal; we are explicitly
165+
* told in the compiler documentation that you cannot use labels in one asm statement in another asm statement. This (as well as the more performant attachInterrupt and the Flash.h library
166+
* when not using optiboot, with full flash write enabled) rely on being able to do this, because we have 3 identical functions which must be present for each of the up to 6 serial ports
167+
* announced and released products - these fuinctions in total waste 202 bytes of flash per serial instance, with code that is identical except for a single constant./
168+
*/
159169
#if !defined(LTODISABLED)
160170
#if !defined(USE_ASM_TXC)
161171
#define USE_ASM_TXC 2 // A bit slower than 1 in exchange for halfduplex.

megaavr/cores/megatinycore/UART.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@
6868
hardware. Crucially the only thing different betweren the USARTs here isthe addressthey're working with.
6969
Much of the benefit comes from being able to get the benefits of functionsin terms of flash use without the
7070
penalties that come with using a true CALL instruction in an ISR (50-80 byte prologue + epiloge), and also
71-
being aware that the X register can't do displacement when planning what goes in which regiseser... which
72-
is not avr-gcc's strong suite, and often ends up displacing from the X with adiw/sbiw spam. savings for one
71+
being aware that the X-register can't do displacement when planning what pointer goes in which register
72+
(The mechanics of the ring buffer wound up juggling pointers; this is avoided in the hand optimized versions
73+
to the extent possible; knowledge of the USART behavior allows some further optimizations)
7374
copy of it is small. Savings for several is gets large fast! Performance is better, but not much.
7475
Biggest advantage is for 2-series with the dual UARTs, but potentially as little as 4k of flash.
7576
@@ -82,6 +83,7 @@
8283
:::);
8384
}
8485
86+
8587
*/
8688

8789
#if USE_ASM_TXC == 2
@@ -136,8 +138,8 @@
136138
"_txc_flush_rx:" "\n\t" // start of rx flush loop.
137139
"ld r24, Z" "\n\t" // Z + 0 = USARTn.RXDATAL rx data
138140
"ldd r24, Z + 4" "\n\t" // Z + 4 = USARTn.STATUS
139-
"sbrc r24, 7" "\n\t" // if RXC bit is set...
140-
"rjmp _txc_flush_rx" "\n\t" // .... skip this jump to remove more from the buffer.
141+
"sbrc r24, 7" "\n\t" // if RXC bit is cleared.
142+
"rjmp _txc_flush_rx" "\n\t" // .... skip this jump (which will remove more from the buffer).
141143
"ldd r24, Z + 5" "\n\t" // Z + 5 = USARTn.CTRLA read CTRLA
142144
"andi r24, 0xBF" "\n\t" // clear TXCIE
143145
"ori r24, 0x80" "\n\t" // set RXCIE
@@ -808,7 +810,6 @@
808810
(*_hwserial_module).CTRLA = ctrla;
809811
} else {
810812
// Enable "data register empty interrupt"
811-
812813
(*_hwserial_module).CTRLA |= USART_DREIE_bm;
813814
}
814815
return 1;

0 commit comments

Comments
 (0)