Browse Source

clock

pull/1/head
Rafael Zurita 6 years ago
parent
commit
7d3d5e0912
  1. 62
      system/clkhandler.c
  2. 37
      system/clkinit.c
  3. 39
      system/intr.c

62
system/clkhandler.c

@ -4,42 +4,62 @@
#include <xinu.h>
#include <avr/io.h>
#include <avr/interrupt.h>
volatile unsigned int avr_ticks=0;
/*-----------------------------------------------------------------------
* clkhandler - high level clock interrupt handler
*-----------------------------------------------------------------------
*/
void clkhandler()
/* void clkhandler() */
ISR(TIMER0_COMPA_vect)
{
struct timer_csreg *tptr;
/* Increment 1000ms counter */
/* Every ms: What TO DO */
count1000++;
/* every second */
/* if avr_ticks == 1000 then 1 second */
/* After 1 sec, increment clktime */
avr_ticks ++;
if (avr_ticks > 100) {
avr_ticks=0;
if(count1000 >= 1000) {
clktime++;
count1000 = 0;
}
/* check if sleep queue is empty */
/* Increment 1000ms counter */
if(!isempty(sleepq)) {
/* sleepq nonempty, decrement the key of */
/* topmost process on sleepq */
count1000++;
if((--queuetab[firstid(sleepq)].qkey) == 0) {
/* After 1 sec, increment clktime */
wakeup();
if(count1000 >= 1000) {
clktime++;
count1000 = 0;
}
}
/* Decrement the preemption counter */
/* Reschedule if necessary */
/* check if sleep queue is empty */
if(!isempty(sleepq)) {
/* sleepq nonempty, decrement the key of */
/* topmost process on sleepq */
if((--preempt) == 0) {
preempt = QUANTUM;
resched();
if((--queuetab[firstid(sleepq)].qkey) == 0) {
wakeup();
}
}
/* Decrement the preemption counter */
/* Reschedule if necessary */
if((--preempt) == 0) {
preempt = QUANTUM;
resched();
}
}
}

37
system/clkinit.c

@ -8,9 +8,11 @@
#include <avr/interrupt.h>
uint32 clktime; /* Seconds since boot */
uint32 count1000; /* ms since last clock tick */
// RAFA uint32 count1000; /* ms since last clock tick */
unsigned long count1000; /* ms since last clock tick */
qid16 sleepq; /* Queue of sleeping processes */
uint32 preempt; /* Preemption counter */
// RAFA uint32 preempt; /* Preemption counter */
unsigned long preempt; /* Preemption counter */
/*------------------------------------------------------------------------
* clkinit - Initialize the clock and sleep queue at startup
@ -30,39 +32,16 @@ void clkinit(void)
clktime = 0; /* Start counting seconds */
count1000 = 0;
/* AVR atmega328p timer/clock init: interrupt every 1ms */
/*
* AVR atmega328p timer/clock init: interrupt every 1ms
* The AVR TIMER interrupt rutine is in clkhandler.c
*/
TCCR0B |= (1<<CS01) | (1<<CS00); //clock select is divided by 64.
TCCR0A |= (1<<WGM01); //sets mode to CTC
// OCR0A = 0x7C; //sets TOP to 124 so the timer will overflow every 1 ms.
OCR0A = 0xF9; //sets TOP to 124 so the timer will overflow every 1 ms.
TIMSK0 |= (1<<OCIE0A); //Output Compare Match A Interrupt Enable
return;
}
unsigned int avr_ticks=0;
ISR(TIMER0_COMPA_vect)
{
cli();
/* Every ms: What TO DO */
// clkhandler();
avr_ticks ++;
if (avr_ticks > 100) {
avr_ticks=0;
resched();
}
/* every second */
/* if avr_ticks == 1000 then 1 second */
sei();
}

39
system/intr.c

@ -1,45 +1,34 @@
/* Xinu for AVR
*
* Original license applies
* Modifications for STM32 by Robin Krens
* Please see LICENSE and AUTHORS
*
* $LOG$
* 2019/11/11 - ROBIN KRENS
* Initial version
*
* $DESCRIPTION$
*
* */
/* intr.S - enable, disable, restore, halt, pause, (ARM) */
/* avr specific */
#include <xinu.h>
#include <avr/interrupt.h>
/* RAFA: the arguments and returns do not have any meaning. It were there
* for STM32.
*
* For AVR: remove them some day
/*
* This code is based on AVR-Xinu by Michael Minor
*/
#include <avr/io.h>
/*------------------------------------------------------------------------
* disable - Disable interrupts and return the previous state
*------------------------------------------------------------------------
*/
intmask disable() {
cli();
return 0;
intmask disable(void)
{
int x = SREG;
asm("cli"); /*and disable interrupts*/
return x;
}
/*------------------------------------------------------------------------
* restore - Restore interrupts to value given by mask argument
Cortex M3 hardware handles a lot, rewrite
*------------------------------------------------------------------------
*/
inline void restore(intmask c) {
sei();
void restore(uint8 x)
{
SREG = x; /*restore the status register, possibly reenabling interrupts*/
}
/*------------------------------------------------------------------------

Loading…
Cancel
Save