ASCII Serial Com
Serial communication library between computers, microcontrollers, FPGAs, etc. Uses only ASCII. Not the most efficient protocol, but meant to be easy to read
Loading...
Searching...
No Matches
Classes | Macros | Typedefs | Functions | Variables
millisec_timer.h File Reference

Portable millisecond timer. More...

#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <libopencm3/cm3/systick.h>
Include dependency graph for millisec_timer.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  __millisec_timer
 portable millisecond timer More...
 

Macros

#define MILLISEC_TIMER_SYSTICK_IT    void sys_tick_handler(void) { MILLISEC_TIMER_NOW++; }
 Millisecond timer SysTick interrupt.
 
#define MILLISEC_TIMER_AVR_TIMER0_ISR    ISR(TIMER0_COMPA_vect) { MILLISEC_TIMER_NOW++; }
 Millisecond timer SysTick interrupt.
 
#define millisec_timer_avr_timer0_setup_16MHz()
 Setup the systick timer.
 

Typedefs

typedef uint32_t millisec_timer_unit_t
 
typedef struct __millisec_timer millisec_timer
 portable millisecond timer
 

Functions

void millisec_timer_set_rel (millisec_timer *timer, const millisec_timer_unit_t now, const millisec_timer_unit_t rel)
 Set timer to expire in the future.
 
bool millisec_timer_is_expired (millisec_timer *timer, const millisec_timer_unit_t now)
 Check if timer has expired & if so, disable it.
 
bool millisec_timer_is_expired_repeat (millisec_timer *timer, const millisec_timer_unit_t now)
 Check if timer has expired & if so, re-enable for the same interval.
 
void millisec_timer_systick_setup (uint32_t ahb_frequency)
 Setup the systick timer.
 

Variables

static uint32_t MILLISEC_TIMER_NOW = 0
 A counter that increments every millisecond.
 

Detailed Description

Portable millisecond timer.

On microcontrollers, MILLISEC_TIMER_NOW will be declared and can be setup to increment every millisecond.

For Cortex-M, put:

1) #include <libopencm3/cm3/nvic.h> at the top of your main file

2) MILLISEC_TIMER_SYSTICK_IT; outside of any function to define the systick interrupt

3) millisec_timer_systick_setup(); with rcc_ahb_frequency as its argument,in the setup portion of your main function. It's probably best to put before other things.

For AVR, put:

1) #include <avr/interrupt.h> #include <avr/io.h> #include <util/atomic.h> at the top of your main file

2) MILLISEC_TIMER_AVR_TIMER0_ISR; outside of any function to define Timer0 ISR.

3) millisec_timer_avr_timer0_setup_16MHz(); in the setup portion of your main function to setup the timer for 16 MHz clock speed.

Definition in file millisec_timer.h.

Macro Definition Documentation

◆ MILLISEC_TIMER_AVR_TIMER0_ISR

#define MILLISEC_TIMER_AVR_TIMER0_ISR    ISR(TIMER0_COMPA_vect) { MILLISEC_TIMER_NOW++; }

Millisecond timer SysTick interrupt.

Implements an interrupt handler that increments MILLISEC_TIMER_NOW

Definition at line 128 of file millisec_timer.h.

◆ millisec_timer_avr_timer0_setup_16MHz

#define millisec_timer_avr_timer0_setup_16MHz ( )
Value:
TCCR0B |= 0x3; \
TCCR0A |= 2; \
OCR0A = 250; \
TIMSK0 |= 1 << OCIE0A

Setup the systick timer.

Configures Timer0 to raise the TIMER0_COMPA interrupt every millisecond.

It's a good idea to use ATOMIC blocks around MILLISEC_TIMER_NOW access.

Definition at line 138 of file millisec_timer.h.

◆ MILLISEC_TIMER_SYSTICK_IT

#define MILLISEC_TIMER_SYSTICK_IT    void sys_tick_handler(void) { MILLISEC_TIMER_NOW++; }

Millisecond timer SysTick interrupt.

Implements an interrupt handler that increments MILLISEC_TIMER_NOW

Definition at line 111 of file millisec_timer.h.

Typedef Documentation

◆ millisec_timer

portable millisecond timer

It's designed to be easy to use and handle wraparound properly (which happens every 49.7 days for uint32_t).

◆ millisec_timer_unit_t

typedef uint32_t millisec_timer_unit_t

Definition at line 47 of file millisec_timer.h.

Function Documentation

◆ millisec_timer_is_expired()

bool millisec_timer_is_expired ( millisec_timer * timer,
const millisec_timer_unit_t now )

Check if timer has expired & if so, disable it.

If timer is enabled and it's expired, return true and disable the timer. If not enabled or the time hasn't expired, return false.

Definition at line 16 of file millisec_timer.c.

◆ millisec_timer_is_expired_repeat()

bool millisec_timer_is_expired_repeat ( millisec_timer * timer,
const millisec_timer_unit_t now )

Check if timer has expired & if so, re-enable for the same interval.

If timer is enabled and it's set time has expired: 1) set the new expiration time to be the old expire time + (old expire time

  • old set time) 2) set the new set time to be the old expire time 3) return true. If not enabled or the time hasn't expired, return false.

Definition at line 30 of file millisec_timer.c.

◆ millisec_timer_set_rel()

void millisec_timer_set_rel ( millisec_timer * timer,
const millisec_timer_unit_t now,
const millisec_timer_unit_t rel )

Set timer to expire in the future.

Sets and enables timer to expire rel ms in the future.

This sets the set_time to now and the expire_time to set_time + rel

Definition at line 8 of file millisec_timer.c.

◆ millisec_timer_systick_setup()

void millisec_timer_systick_setup ( uint32_t ahb_frequency)

Setup the systick timer.

Configures the systick timer to raise the interrupt every millisecond.

Definition at line 50 of file millisec_timer.c.

Variable Documentation

◆ MILLISEC_TIMER_NOW

uint32_t MILLISEC_TIMER_NOW = 0
static

A counter that increments every millisecond.

This value will increment every millisecond, if everything is setup correctly.

Definition at line 101 of file millisec_timer.h.