Skip to content

Commit

Permalink
atmega2560 port
Browse files Browse the repository at this point in the history
  • Loading branch information
rajszym committed Oct 31, 2023
1 parent 881e328 commit 2325e16
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
49 changes: 44 additions & 5 deletions demos/port/avr/atmega2560/osport.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
******************************************************************************
Expand Down Expand Up @@ -31,6 +31,7 @@

#include "os.h"
#include <avr/interrupt.h>
#include <assert.h>

/* --------------------------------------------------------------------------------------------- */

Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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);
}

/* --------------------------------------------------------------------------------------------- */
3 changes: 1 addition & 2 deletions demos/port/avr/atmega2560/osport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
******************************************************************************
Expand Down Expand Up @@ -33,7 +33,6 @@
#define __DEMOSPORT_H

#include <stdint.h>
#include <avr/io.h>

#ifdef __cplusplus
extern "C" {
Expand Down

0 comments on commit 2325e16

Please sign in to comment.