Skip to content

Commit 2325e16

Browse files
committed
atmega2560 port
1 parent 881e328 commit 2325e16

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

demos/port/avr/atmega2560/osport.c

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
@file DemOS: osport.c
44
@author Rajmund Szymanski
5-
@date 10.03.2020
5+
@date 22.03.2023
66
@brief DemOS port file for ATtiny817 uC.
77
88
******************************************************************************
@@ -31,6 +31,7 @@
3131

3232
#include "os.h"
3333
#include <avr/interrupt.h>
34+
#include <assert.h>
3435

3536
/* --------------------------------------------------------------------------------------------- */
3637

@@ -39,18 +40,30 @@ cnt_t sys_counter = 0;
3940

4041
/* --------------------------------------------------------------------------------------------- */
4142

43+
__attribute__((weak))
44+
void sys_hook( void ) {} // user function - called from counter interrupt
45+
46+
/* --------------------------------------------------------------------------------------------- */
47+
4248
ISR( TIMER1_OVF_vect )
4349
{
50+
// TIFR1 = (1 << OCF1A);
4451
sys_counter++;
52+
sys_hook();
4553
}
4654

4755
/* --------------------------------------------------------------------------------------------- */
4856

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");
5360

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;
5467
TCCR1B = (1 << CS11) | (1 << CS10);
5568
TCCR1A = (1 << COM1A1) | (1 << COM1A0);
5669
TIMSK1 = (1 << OCIE1A);
@@ -60,6 +73,15 @@ void sys_init( void )
6073

6174
/* --------------------------------------------------------------------------------------------- */
6275

76+
void sys_init( void )
77+
{
78+
static OS_ONE(init);
79+
80+
one_call(init, port_init);
81+
}
82+
83+
/* --------------------------------------------------------------------------------------------- */
84+
6385
cnt_t sys_time( void )
6486
{
6587
cnt_t cnt;
@@ -70,3 +92,20 @@ cnt_t sys_time( void )
7092
}
7193

7294
/* --------------------------------------------------------------------------------------------- */
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+
/* --------------------------------------------------------------------------------------------- */

demos/port/avr/atmega2560/osport.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
@file DemOS: osport.h
44
@author Rajmund Szymanski
5-
@date 16.03.2023
5+
@date 22.03.2023
66
@brief DemOS port definitions for ATtiny817 uC.
77
88
******************************************************************************
@@ -33,7 +33,6 @@
3333
#define __DEMOSPORT_H
3434

3535
#include <stdint.h>
36-
#include <avr/io.h>
3736

3837
#ifdef __cplusplus
3938
extern "C" {

0 commit comments

Comments
 (0)