ruuvi.drivers.c  ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
ruuvi_interface_timer.h File Reference

Interface functions to timer. More...

#include "ruuvi_driver_enabled_modules.h"
#include "ruuvi_driver_error.h"
#include <stdbool.h>

Go to the source code of this file.

Typedefs

typedef void * ri_timer_id_t
 Pointer to timer data. More...
 
typedef void(* ruuvi_timer_timeout_handler_t) (void *const p_context)
 Function to be called when timer times out. More...
 

Enumerations

enum  ri_timer_mode_t { RI_TIMER_MODE_SINGLE_SHOT , RI_TIMER_MODE_REPEATED }
 Enable implementation selected by application. More...
 

Functions

rd_status_t ri_timer_init (void)
 
rd_status_t ri_timer_uninit (void)
 
bool ri_timer_is_init (void)
 Check if timer is initialized. More...
 
rd_status_t ri_timer_create (ri_timer_id_t *p_timer_id, ri_timer_mode_t mode, ruuvi_timer_timeout_handler_t timeout_handler)
 
rd_status_t ri_timer_start (ri_timer_id_t timer_id, uint32_t ms, void *const context)
 Start given timer at a mode defined in ri_timer_create. More...
 
rd_status_t ri_timer_stop (ri_timer_id_t timer_id)
 

Detailed Description

Interface functions to timer.

Author
Otso Jousimaa otso@.nosp@m.ojou.nosp@m.sima..nosp@m.net
Date
2020-07-14

Timer abstraction. Allows creating single-shot and repeated timers which call a function at interval. The timer will run in interrupt context, if you need to use peripherals or spend a lot of time in timer function you should use ri_scheduler to run the functions without blocking timing-critical tasks.

Typical usage:

static ri_timer_id_t m_timer;
static void do_lots_of_things_in_scheduler (void * p_event_data, uint16_t event_size)
{
write_to_i2c_device((uint8_t*) event_data, event_size);
}
static void timer_isr(void * const p_context)
{
// Slow I2C write in scheduler
i2c_tx_t* tx = (i2c_tx_t*) p_context;
uint16_t event_size = (p_context[0] << 8) + p_context[1];
uint8_t* p_event_data = &(p_context[2]);
ri_scheduler_event_put (p_event_data, event_size, &do_lots_of_things_in_scheduler);
// Fast GPIO operation can be done here.
blink_led_right_away();
}
// Write 10 NULLs over I2C in a scheduler 10 seconds from now.
rd_status_t start_my_timer(void)
{
rd_status_t err_code = RD_SUCCESS;
static i2c_tx_t tx =
{
.write_len = 10;
.data = {0}
}
err_code |= ri_timer_init();
// Note: Address of timer
err_code |= ri_timer_create (&m_timer, RI_TIMER_MODE_SINGLE_SHOT, &timer_isr);
// Note: Value of timer, statically allocated data as context.
err_code |= ri_timer_start (m_timer, (10 * 1000U), &tx);
return err_code
}
uint32_t rd_status_t
bitfield for representing errors
#define RD_SUCCESS
Internal Error.
rd_status_t ri_scheduler_event_put(const void *const p_event_data, const uint16_t event_size, const ruuvi_scheduler_event_handler_t handler)
Schedule given task to be executed on next call to ri_scheduler_execute.
rd_status_t ri_timer_start(ri_timer_id_t timer_id, uint32_t ms, void *const context)
Start given timer at a mode defined in ri_timer_create.
rd_status_t ri_timer_create(ri_timer_id_t *p_timer_id, ri_timer_mode_t mode, ruuvi_timer_timeout_handler_t timeout_handler)
rd_status_t ri_timer_init(void)
void * ri_timer_id_t
Pointer to timer data.
@ RI_TIMER_MODE_SINGLE_SHOT

Definition in file ruuvi_interface_timer.h.