A real embedded system needs a system-tick, and Arduino doesn’t have one … No, the yield() function is not a real system-tick, so I’ve swimmed a little bit into the code so that I get a real one. This is my progress.
In the file hooks.c we need to add a place holder for our callback. Add this code at the end of such file:
static void __empty2() {
// Empty
}
void tick_hook(void) __attribute__ ((weak, alias("__empty2")));
Then we need to declare our callback in Arduino.h. Look for the line
void yield(void);
and write after it:
void tick_hook( void );
It’s supposed that our callback is going to be called in every system tick, so look for timer 0 ISR function in wiring.c:
#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
ISR(TIM0_OVF_vect)
#else
ISR(TIMER0_OVF_vect)
#endif
{
and write at its end the call to our callback:
tick_hook();
Finally, we need to write the body of our callback. My first attempt was to determine the tick period, so in the main source code I wrote:
void tick_hook()
{
static bool ledState = false;
// should exist a toggle function? Hope so!
if( ledState ) {
digitalWrite(LED_BUILTIN, HIGH);
ledState = false;
}
else {
digitalWrite(LED_BUILTIN, LOW);
ledState = true;
}
}
The result was that our callback is called every one millisecond, as seen in the next image:

From here we can do whatever is required in our application using the system-tick. Just remember, any code inside an ISR should be as short and fast as possible.
Update: A complete example!
/*Copyright (C)
* 2018 - fjrg76 at hotmail dot com
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
//----------------------------------------------------------------------
// Class: HeartBeat
//----------------------------------------------------------------------
template<const size_t Ticks, const uint8_t Pin>
class HeartBeat
{
public:
HeartBeat();
void Toggle();
private:
bool pinState;
size_t ticks;
};
template<const size_t Ticks, const uint8_t Pin>
HeartBeat<Ticks, Pin>::HeartBeat() : pinState{ false }, ticks{ Ticks }
{
pinMode(Pin, OUTPUT);
}
template<const size_t Ticks, const uint8_t Pin>
void HeartBeat<Ticks, Pin>::Toggle()
{
--ticks;
if( ticks == 0 ) {
ticks = Ticks;
pinState = pinState ? false : true;
digitalWrite( Pin, pinState );
}
}
HeartBeat<500, LED_BUILTIN> heartBeat;
//----------------------------------------------------------------------
// Our callback
//----------------------------------------------------------------------
void tick_hook()
{
heartBeat.Toggle();
// the built-in led is toggled every 500 milliseconds
}
void setup()
{
// empty
}
void loop()
{
// empty
}
In the console write:
make make upload
Greetings!
[…] want to mess with such project and real time kernels, I wrote an entry in my other blog, here, in which I only talk about the system […]
Me gustaMe gusta
[…] (La versión original y en inglés de esta entrada la pueden consultar en mi blog alternativo: https://arduinoforadvanceddevelopers.wordpress.com/2018/03/12/system-tick-for-the-arduino-platform/) […]
Me gustaMe gusta
[…] with that, I create my own inside the very Arduino’s heart. You might take a look to this post in my other blog. (“Real time” means that this tick is interrupt driven. To some extent […]
Me gustaMe gusta
[…] (La versión original y en inglés de esta entrada la pueden consultar en mi blog alternativo.) […]
Me gustaMe gusta