I have some questions about this function which is in SoftwareSerial.cpp and can be found here on github:
inline void DebugPulse(uint8_t pin, uint8_t count)
{
volatile uint8_t *pport = portOutputRegister(digitalPinToPort(pin));
uint8_t val = *pport;
while (count--)
{
*pport = val | digitalPinToBitMask(pin);
*pport = val;
}
}
What is an inline?
Won't the while loop continue indefinitely? If not, why? I realize that each time it loops, the value of count will be decremented by 1, but then it seems to me that the loop should run until the limit has been reached as to the lowest value permitted for the variable count at which point, it should throw a runtime error (if those were possible in arduino's that is) .
'inline' is a hint to the compiler that the function is not used often so it can be inserted in the few places it is used rather than being called like a function.
The 'while' will stop when count reaches zero (== false). If count starts at 5, for example, the loop will run for count = 5, 4, 3, 2, and 1. Since the decrement (--) is after the value is tested, inside the loop I think you will see the values of 4, 3, 2, 1, and 0 for count.
Runs if != 0. Any non-zero value converts to te boolean 'true' and zero converts to the boolean 'false'. Any "while (integerExpression)" is equivalent to "while ((integerExpression) != 0)"
The compiler expands this function directly to the caller instead of calling it.
This reduces function call overhead and increases execution speed, but increases the size of ROM consumed may increase.
In C, only 0 represents False and the others evaluate to True.
Also, 1 byte variable is subtracting 1 from 0x00 gives 0xFF.
(0b00000000 - 1 = 0b11111111)
This is 255 when interpreted as an unsigned integer.
If you continue more subtracting 1 from here, finaly it will become 0 and you will exit While.
OK I didn't know that ... so if the while condition is less than zero it would also run? so if the function was called with say ... pin 4 and count = -1 ... then the loop would run indefinitely?
Depends on the compiler. A smart compiler might see that a function is only called once and make it inline without the hint. Another compiler might ignore the 'inline' hint and decide to make the function callable.
Depends on the size of 'count'. If its an 8-bit integer it would count from -1 down to -128 and then roll over to 127, and then count down to 0. If its a 16-bit 'int' it would count down to -32768 and then roll over to 32767, and then count down to 0. No matter how big, if you count down long enough you will reach 0.
In terms of using this function for debugging ... the comments on this function say that you can use it for example to look at the signal on a scope ... but wouldn't even a large number for count cause this function to run rather quickly? I suppose you'd have to set up your scope to snap shot it.
The pulses would be going by at a few megahertz. If you set your scope to trigger you can capture and count the pulses to see where in the code you are.
The fastest pulse that a 16MHz Arduino can produce is 8MHz.
What the code produces is always slower than that.
This is a signal that can be sufficiently observed with an oscilloscope with some performance.
Also, it is desirable that debug signals don't affect the main program as much as possible.
Therefore, it uses the maximum performance of the processor and minimizes the CPU cost.
Could you give me a scenario where this function would be useful? I've got zero experience debugging serial communications and have never found myself needing to do so in spite of using serial communications for decades. But I am curious...
Im thinking about setting up the scope just to see this in action.
Probably, Not a serial communication debug for users, developer debug of how well the library is working.
This may have been prepared by the developer to test the library.
In software serial communication, only the code controls the timing of the signal.
Using some techniques for achieve operate high baud rate.
I think use this signal to debug for optimize code.