System-tick in Arduino

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:

IMAG1056

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
}

 

Greetings!

Anuncios

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión /  Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )

w

Conectando a %s