2
2
3
3
@file DemOS: osport.c
4
4
@author Rajmund Szymanski
5
- @date 10 .03.2020
5
+ @date 22 .03.2023
6
6
@brief DemOS port file for ATtiny817 uC.
7
7
8
8
******************************************************************************
31
31
32
32
#include "os.h"
33
33
#include <avr/interrupt.h>
34
+ #include <assert.h>
34
35
35
36
/* --------------------------------------------------------------------------------------------- */
36
37
@@ -39,18 +40,30 @@ cnt_t sys_counter = 0;
39
40
40
41
/* --------------------------------------------------------------------------------------------- */
41
42
43
+ __attribute__((weak ))
44
+ void sys_hook ( void ) {} // user function - called from counter interrupt
45
+
46
+ /* --------------------------------------------------------------------------------------------- */
47
+
42
48
ISR ( TIMER1_OVF_vect )
43
49
{
50
+ // TIFR1 = (1 << OCF1A);
44
51
sys_counter ++ ;
52
+ sys_hook ();
45
53
}
46
54
47
55
/* --------------------------------------------------------------------------------------------- */
48
56
49
- void sys_init ( void )
50
- {
51
- OCR1AH = (F_CPU / 1000 / 64 - 1 ) >> 8 ;
52
- OCR1AL = (F_CPU / 1000 / 64 - 1 );
57
+ #define PRESCALER 64
58
+
59
+ static_assert (((F_CPU ) / (PRESCALER ) / 1000UL ) * (PRESCALER ) * 1000UL == (F_CPU ), "prescaler too large" );
53
60
61
+ /* --------------------------------------------------------------------------------------------- */
62
+
63
+ static void port_init ( void )
64
+ {
65
+ OCR1AH = (F_CPU / (PRESCALER ) / 1000 - 1 ) / 256 ;
66
+ OCR1AL = (F_CPU / (PRESCALER ) / 1000 - 1 ) % 256 ;
54
67
TCCR1B = (1 << CS11 ) | (1 << CS10 );
55
68
TCCR1A = (1 << COM1A1 ) | (1 << COM1A0 );
56
69
TIMSK1 = (1 << OCIE1A );
@@ -60,6 +73,15 @@ void sys_init( void )
60
73
61
74
/* --------------------------------------------------------------------------------------------- */
62
75
76
+ void sys_init ( void )
77
+ {
78
+ static OS_ONE (init );
79
+
80
+ one_call (init , port_init );
81
+ }
82
+
83
+ /* --------------------------------------------------------------------------------------------- */
84
+
63
85
cnt_t sys_time ( void )
64
86
{
65
87
cnt_t cnt ;
@@ -70,3 +92,20 @@ cnt_t sys_time( void )
70
92
}
71
93
72
94
/* --------------------------------------------------------------------------------------------- */
95
+
96
+ static cnt_t get_counter ( void )
97
+ {
98
+ cnt_t result ;
99
+ do result = sys_counter ; while (result != sys_counter );
100
+ return result ;
101
+ }
102
+
103
+ /* --------------------------------------------------------------------------------------------- */
104
+
105
+ void sys_delay ( cnt_t delay )
106
+ {
107
+ cnt_t start = get_counter ();
108
+ while (get_counter () - start + 1 <= delay );
109
+ }
110
+
111
+ /* --------------------------------------------------------------------------------------------- */
0 commit comments