diff --git a/demos/port/avr/atmega2560/osport.c b/demos/port/avr/atmega2560/osport.c index 73d679e..e0f0770 100644 --- a/demos/port/avr/atmega2560/osport.c +++ b/demos/port/avr/atmega2560/osport.c @@ -2,7 +2,7 @@ @file DemOS: osport.c @author Rajmund Szymanski - @date 10.03.2020 + @date 22.03.2023 @brief DemOS port file for ATtiny817 uC. ****************************************************************************** @@ -31,6 +31,7 @@ #include "os.h" #include +#include /* --------------------------------------------------------------------------------------------- */ @@ -39,18 +40,30 @@ cnt_t sys_counter = 0; /* --------------------------------------------------------------------------------------------- */ +__attribute__((weak)) +void sys_hook( void ) {} // user function - called from counter interrupt + +/* --------------------------------------------------------------------------------------------- */ + ISR( TIMER1_OVF_vect ) { +// TIFR1 = (1 << OCF1A); sys_counter++; + sys_hook(); } /* --------------------------------------------------------------------------------------------- */ -void sys_init( void ) -{ - OCR1AH = (F_CPU / 1000 / 64 - 1) >> 8; - OCR1AL = (F_CPU / 1000 / 64 - 1); +#define PRESCALER 64 + +static_assert(((F_CPU) / (PRESCALER) / 1000UL) * (PRESCALER) * 1000UL == (F_CPU), "prescaler too large"); +/* --------------------------------------------------------------------------------------------- */ + +static void port_init( void ) +{ + OCR1AH = (F_CPU / (PRESCALER) / 1000 - 1) / 256; + OCR1AL = (F_CPU / (PRESCALER) / 1000 - 1) % 256; TCCR1B = (1 << CS11) | (1 << CS10); TCCR1A = (1 << COM1A1) | (1 << COM1A0); TIMSK1 = (1 << OCIE1A); @@ -60,6 +73,15 @@ void sys_init( void ) /* --------------------------------------------------------------------------------------------- */ +void sys_init( void ) +{ + static OS_ONE(init); + + one_call(init, port_init); +} + +/* --------------------------------------------------------------------------------------------- */ + cnt_t sys_time( void ) { cnt_t cnt; @@ -70,3 +92,20 @@ cnt_t sys_time( void ) } /* --------------------------------------------------------------------------------------------- */ + +static cnt_t get_counter( void ) +{ + cnt_t result; + do result = sys_counter; while (result != sys_counter); + return result; +} + +/* --------------------------------------------------------------------------------------------- */ + +void sys_delay( cnt_t delay ) +{ + cnt_t start = get_counter(); + while (get_counter() - start + 1 <= delay); +} + +/* --------------------------------------------------------------------------------------------- */ diff --git a/demos/port/avr/atmega2560/osport.h b/demos/port/avr/atmega2560/osport.h index e6d3a83..3e7b66c 100644 --- a/demos/port/avr/atmega2560/osport.h +++ b/demos/port/avr/atmega2560/osport.h @@ -2,7 +2,7 @@ @file DemOS: osport.h @author Rajmund Szymanski - @date 16.03.2023 + @date 22.03.2023 @brief DemOS port definitions for ATtiny817 uC. ****************************************************************************** @@ -33,7 +33,6 @@ #define __DEMOSPORT_H #include -#include #ifdef __cplusplus extern "C" {