Add global timer

This commit is contained in:
Jeremy Soller 2020-02-03 15:16:17 -07:00
parent 3692bed6b2
commit ce22dd5208
No known key found for this signature in database
GPG Key ID: E988B49EE78A7FB1
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;
}

View File

@ -3,6 +3,7 @@
#include <stdio.h>
#include <arch/delay.h>
#include <arch/time.h>
#include <board/battery.h>
#include <board/gpio.h>
#include <board/gctrl.h>
@ -23,10 +24,6 @@ void external_0(void) __interrupt(0) {
TRACE("external_0\n");
}
void timer_0(void) __interrupt(1) {
TRACE("timer_0\n");
}
void external_1(void) __interrupt(2) {
TRACE("external_1\n");
}
@ -44,6 +41,8 @@ void timer_2(void) __interrupt(5) {
}
void init(void) {
time_init();
gpio_init();
gctrl_init();
pwm_init();

View File

@ -1,6 +1,7 @@
#include <mcs51/8051.h>
#include <arch/delay.h>
#include <arch/time.h>
#include <board/battery.h>
#include <board/gpio.h>
#include <board/power.h>
@ -342,33 +343,17 @@ void power_event(void) {
gpio_set(&LED_ACIN, false);
} else if (s4_new) {
// Suspended, flashing green light
static int8_t suspend_timer = 0;
if (suspend_timer <= 0) {
static uint32_t last_time = 0;
uint32_t time = time_get();
if (
(time < last_time) // overflow
||
(time >= (last_time + 1000)) // timeout
) {
gpio_set(&LED_PWR, !gpio_get(&LED_PWR));
// Suspend timer fires every 1 s
suspend_timer = 100;
last_time = time;
}
gpio_set(&LED_ACIN, false);
// If timer 1 is finished
if (TF1) {
// Stop timer 1 running
TR1 = 0;
// Clear timer 1 finished flag
TF1 = 0;
// Decrement suspend timer
suspend_timer -= 1;
}
// If timer 1 is not running
if (!TR1) {
// Start timer for 10 ms
// 65536-(10000 * 69 + 89)/90 = 0xE20C
TMOD = (TMOD & 0x0F) | 0x10;
TH1 = 0xE2;
TL1 = 0x0C;
TR1 = 1;
}
} else if (!ac_new) {
// AC plugged in, orange light
gpio_set(&LED_PWR, false);

View File

@ -3,6 +3,7 @@
#include <stdio.h>
#include <arch/delay.h>
#include <arch/time.h>
#include <board/battery.h>
#include <board/gpio.h>
#include <board/gctrl.h>
@ -24,10 +25,6 @@ void external_0(void) __interrupt(0) {
TRACE("external_0\n");
}
void timer_0(void) __interrupt(1) {
TRACE("timer_0\n");
}
void external_1(void) __interrupt(2) {
TRACE("external_1\n");
}
@ -45,6 +42,8 @@ void timer_2(void) __interrupt(5) {
}
void init(void) {
time_init();
gpio_init();
gctrl_init();
pwm_init();

View File

@ -1,6 +1,7 @@
#include <mcs51/8051.h>
#include <arch/delay.h>
#include <arch/time.h>
#include <board/battery.h>
#include <board/gpio.h>
#include <board/power.h>
@ -342,33 +343,17 @@ void power_event(void) {
gpio_set(&LED_ACIN, false);
} else if (s4_new) {
// Suspended, flashing green light
static int8_t suspend_timer = 0;
if (suspend_timer <= 0) {
static uint32_t last_time = 0;
uint32_t time = time_get();
if (
(time < last_time) // overflow
||
(time >= (last_time + 1000)) // timeout
) {
gpio_set(&LED_PWR, !gpio_get(&LED_PWR));
// Suspend timer fires every 1 s
suspend_timer = 100;
last_time = time;
}
gpio_set(&LED_ACIN, false);
// If timer 1 is finished
if (TF1) {
// Stop timer 1 running
TR1 = 0;
// Clear timer 1 finished flag
TF1 = 0;
// Decrement suspend timer
suspend_timer -= 1;
}
// If timer 1 is not running
if (!TR1) {
// Start timer for 10 ms
// 65536-(10000 * 69 + 89)/90 = 0xE20C
TMOD = (TMOD & 0x0F) | 0x10;
TH1 = 0xE2;
TL1 = 0x0C;
TR1 = 1;
}
} else if (!ac_new) {
// AC plugged in, orange light
gpio_set(&LED_PWR, false);

View File

@ -3,6 +3,7 @@
#include <stdio.h>
#include <arch/delay.h>
#include <arch/time.h>
#include <board/battery.h>
#include <board/gpio.h>
#include <board/gctrl.h>
@ -24,10 +25,6 @@ void external_0(void) __interrupt(0) {
TRACE("external_0\n");
}
void timer_0(void) __interrupt(1) {
TRACE("timer_0\n");
}
void external_1(void) __interrupt(2) {
TRACE("external_1\n");
}
@ -45,6 +42,8 @@ void timer_2(void) __interrupt(5) {
}
void init(void) {
time_init();
gpio_init();
gctrl_init();
pwm_init();

View File

@ -1,6 +1,7 @@
#include <mcs51/8051.h>
#include <arch/delay.h>
#include <arch/time.h>
#include <board/battery.h>
#include <board/gpio.h>
#include <board/power.h>
@ -342,33 +343,17 @@ void power_event(void) {
gpio_set(&LED_ACIN, false);
} else if (s4_new) {
// Suspended, flashing green light
static int8_t suspend_timer = 0;
if (suspend_timer <= 0) {
static uint32_t last_time = 0;
uint32_t time = time_get();
if (
(time < last_time) // overflow
||
(time >= (last_time + 1000)) // timeout
) {
gpio_set(&LED_PWR, !gpio_get(&LED_PWR));
// Suspend timer fires every 1 s
suspend_timer = 100;
last_time = time;
}
gpio_set(&LED_ACIN, false);
// If timer 1 is finished
if (TF1) {
// Stop timer 1 running
TR1 = 0;
// Clear timer 1 finished flag
TF1 = 0;
// Decrement suspend timer
suspend_timer -= 1;
}
// If timer 1 is not running
if (!TR1) {
// Start timer for 10 ms
// 65536-(10000 * 69 + 89)/90 = 0xE20C
TMOD = (TMOD & 0x0F) | 0x10;
TH1 = 0xE2;
TL1 = 0x0C;
TR1 = 1;
}
} else if (!ac_new) {
// AC plugged in, orange light
gpio_set(&LED_PWR, false);