What does this common SPI function do? - arduino

I see this function in Arduino scripts using SPI: ISR (SPI_STC_vect). I am familiar with C but this doesn't seem to follow - it looks like a function though there is no return type definition, and the argument doesn't make sense. I am new to SPI and cannot find much/any documentation online. Other functions which run during an interrupt are called using attachInterrupt, however this doesn't seem to be called anywhere.

ISR() is a macro that introduces an interrupt handler.
SPI_STC_vect is a Serial Transfer Complete interrupt vector.
See here for complete documentation, including the list of available interrupt vectors.

Related

Using microcomputer pin alternate functions

I hope this isn't too general a question, but how does one use an alternate pin function when programming a microcomputer. Specifically, does one have to "tell" the microcomputer that you're using the pin for a function other than its default. I realize that it's done, in effect, when one sets up SPI or I2C, for example, but is it language specific, so that in C/C++ one has to specifically identify the function one is going to use in some fashion. So, for GPIO (or whatever the default function is) one doesn't have to do anything, but if one wants to use a pin's alternate function 3 (let's say it's U2-RXD one has to communicate that explicitly to the microcontroller? Pardon the "stupid" question.
I'll take "it depends" as the definitive answer and defer to the datasheet for follow-up.
Thanks.

Difference between Serial.print() and Stream.print()

I would like to know the difference between Serial functions and Stream functions in Ardiuno coding. Both behave in the same way but wat is the difference then?
Stream is the base class that the Serial function inherits.
Copy/paste from the reference:
Stream defines the reading functions in Arduino. When using any core functionality that uses a read() or similar method, you can safely assume it calls on the Stream class. For functions like print(), Stream inherits from the Print class.
References:
https://www.arduino.cc/reference/en/language/functions/communication/stream/
What is the difference between Serial and Stream on the Arduino, and how is Serial.write implemented?

Which functions don't work when using a noInterrupts() / interrupts() block?

I have some code in an Arduino library that is time sensitive, and want to protect it between noInterrupts() and interrupts(). The documentation states:
Some functions will not work while interrupts are disabled, and incoming communication may be ignored.
Is there a list of what (standard) functions won't work? In particular, I need save off the time with a call to millis(). Is the number behind millis() still getting updated, or should I move it out of the noInterrupts() / interrupts() block?
It would appear from this answer that millis() in particular would be disabled by disabling interrupts, as that call relies on a on an interrupt attached to a timer that fires at about 1KHz. I've pored over the official documentation though, and can find no exhaustive list of what can be affected. I'm sure many are dismayed by this obvious lack in the official documentation.
Looking further, the timer (Timer/Counter 0 in the ATmega documentation) that controls millis() still counts in the background whether interrupts are enabled or not -- the question is, if your code spans the time when the interrupt would have fired, you could miss a tick. See reference below.
Bottom line is if you need interrupts, keep your noInterrupts() sections brief. And keep your code that is attached to interrupts briefer. ;) Whether you're coding in sketches or bare-metal, it's always important to keep interrupts fast in-and-out.
This external reference is also interesting, shows the math and code behind the millis().

How to bypass Serial port libraries in Arduino

I want to write my own code for managing an Arduino serial port (9bit serial) so I need to bypass the preprogrammed arduino ISR vector for Serial1 TX. I can do that by modifying HardwareSerial1.cpp but I dislike the fact that my code becomes non portable and a potential victim of library updates.
Do you know how to program a different ISR vector without messing into library code? I tried this in my own code to no avail:
//bypass arduino library for serial 1 and use this ISR instead
ISR(UART1_UDRE_vect)
{
9bitSerial.interrupt();
}
The following error is reported, which seems to confirm the approach doesn't work:
ISR.cpp:11:25: error: expected unqualified-id before numeric constant
ISR.cpp:in expansion of macro 'UART1_UDRE_vect'
Any clues?
The safest way, IMHO, is to copy the entire HardwareSerial class to a differently-named class. Then proceed to make your modifications. This is exactly what I did for NeoHWSerial and NeoICSerial: they are copied and renamed from HardwareSerial and AltSoftSerial.
However, all uses of Serialx (instances of HardwareSerial) in a build must be replaced with instances of your class. You won't be able to mix instances of HardwareSerial with your class (duplicate ISR errors). But that's no big deal if your class only adds behavior. Other instances that don't use 9 bits should not be affected. If you don't mind, I'd be interested in folding those changes into NeoHWSerial. I've been occasionally looking at 9-bit serial for NeoSWSerial, too.

how is the connection between signal and slot made in QT?

I have been a Qt programmer for quite some time now and i understand most of the general features of Qt. I am still confused about how the connect statement connects a signals to a slot at run time. Basically i would like to understand what happens at compile time and what happens at run time..
compile time: meta object compiler will generate code to implement a signal in an additional cpp file (one for each class containing Q_OBJECT).
run time: signal is mapped to a slot, slot gets executed? this is the part i am not clear about...SIGNAL and SLOTS are macros that expand to string representation of the signal/slot names...how does this and the meta object help in mapping calls to slots at run time? details would be appreciated...
EDIT:
this link will give you a better idea..(only if you are interested in the gory details...)
http://dev.libqxt.org/libqxt/wiki/Meta_Object_Format
couple this with the documentation of QMetaObject and things should become clear...
There are various ways you can connect a signal to a method (signal/slot).
However, they all revolve around getting the correct method number.
After you have the correct method number and the object to call it on, you simply call a virtualized function (qt_metacall) in QObject which finds the correct method to call from the number given. You can find this function in the files that the MOC generates. Also, in that generated file, you can find a line which creates a static QMetaObject of your class. This object registers the names of your slots and signals to the method numbers.
These might provide some interesting stuff to read:
http://doc.qt.io/qt-5/qmetaobject.html
http://doc.qt.io/qt-5/metaobjects.html
http://doc.qt.io/qt-5/signalsandslots.html
You can also learn a lot by running thought the slot activations with a debugger.
Basically signals and slots work similar to messages in Objective-C.
The macros cause the preprocessor to replace them with some code which "registers" and "looks up" the functions/methods that are to be effectively executed when a slot is called.
This allows for more flexibility, because the code that is emitting a signal or calling a slot does not need to know much about the other code modules which use them. Each slot and signal generate a signature that is looked up at runtime and then called.
If you are familiar with C/C++, you can compare this to dynamic libraries. Symbols are looked up at runtime and their address is then used to let the CPU "jump" to them to execute.
Also, these links might help you:
Qt question: How do signals and slots work?
http://doc.qt.io/qt-5/signalsandslots.html

Resources