Add global timer

This commit is contained in:
Jeremy Soller
2020-02-03 15:16:17 -07:00
parent 3692bed6b2
commit ce22dd5208
11 changed files with 100 additions and 116 deletions

View File

@ -1,12 +1,24 @@
// Uses timer 1 to implement delays
#include <8051.h>
#include <arch/delay.h>
#include <arch/timer.h>
void delay_ticks(uint16_t ticks) {
timer_mode_1(-ticks);
timer_wait();
timer_stop();
uint16_t value = -ticks;
// Stop the timer
TR1 = 0;
TF1 = 0;
// Start timer in mode 1
TMOD = (TMOD & 0x0F) | 0x10;
TH1 = (unsigned char)(value >> 8);
TL1 = (unsigned char)value;
TR1 = 1;
// Wait until complete
while (TF1 == 0) {}
}
// This loops through delays of one ms in order to avoid overflow

View File

@ -0,0 +1,7 @@
#ifndef _ARCH_TIME_H
#define _ARCH_TIME_H
void time_init(void);
uint32_t time_get(void);
#endif // _ARCH_TIME_H

View File

@ -1,8 +0,0 @@
#ifndef _ARCH_TIMER_H
#define _ARCH_TIMER_H
void timer_mode_1(int value);
void timer_wait(void);
void timer_stop(void);
#endif // _ARCH_TIMER_H

41
src/arch/8051/time.c Normal file
View File

@ -0,0 +1,41 @@
// Uses timer 0 to keep track of global time
#include <8051.h>
#include <stdint.h>
#include <arch/time.h>
static volatile uint32_t time_overflows = 0;
void timer_0(void) __interrupt(1) {
time_overflows++;
// Restart timer
TR0 = 0;
TF0 = 0;
TH0 = 0xFD;
TL0 = 0x01;
TR0 = 1;
}
void time_init(void) __critical {
// Stop the timer
TR0 = 0;
TF0 = 0;
time_overflows = 0;
// Enable timer interrupts
ET0 = 1;
// Start timer in mode 1
// (65536 - 64769) / (9.2 MHz / 12) = ~1 ms interval
TMOD = (TMOD & 0xF0) | 0x01;
TH0 = 0xFD;
TL0 = 0x01;
TR0 = 1;
}
uint32_t time_get(void) __critical {
return time_overflows;
}

View File

@ -1,20 +0,0 @@
#include <8051.h>
#include <arch/timer.h>
void timer_mode_1(int value) {
timer_stop();
TMOD = (TMOD & 0xF0) | 0x01;
TH0 = (unsigned char)(value >> 8);
TL0 = (unsigned char)value;
TR0 = 1;
}
void timer_wait(void) {
while (TF0 == 0) {}
}
void timer_stop(void) {
TR0 = 0;
TF0 = 0;
}