Skip to content

Commit f8c868a

Browse files
engdoreismarnovandermaas
authored andcommitted
[legacy, hal] Implement timer driver
Signed-off-by: Douglas Reis <[email protected]>
1 parent c3f7bea commit f8c868a

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

sw/legacy/demo/lcd_st7735/main.cc

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ extern "C" {
1111
#include "lowrisc_logo.h"
1212
#include "sonata_system.h"
1313
#include "st7735/lcd_st7735.h"
14-
#include "timer.h"
1514

1615
int coremark_main();
1716
}
1817

1918
#include "spi.hh"
2019
#include "pwm.hh"
2120
#include "gpio.hh"
21+
#include "timer.hh"
2222
#include "platform.hh"
2323

2424
#define SIMULATION 0
@@ -49,15 +49,10 @@ static uint32_t gpio_write(void *handle, bool cs, bool dc) {
4949
return 0;
5050
}
5151

52+
Timer timer(platform::Timer::Timer0, SYSCLK_FREQ);
5253
static void timer_delay(uint32_t ms) {
5354
#if !SIMULATION
54-
// Configure timer to trigger every 1 ms
55-
timer_enable(SYSCLK_FREQ / 1000);
56-
uint32_t timeout = get_elapsed_time() + ms;
57-
while (get_elapsed_time() < timeout) {
58-
asm volatile("wfi");
59-
}
60-
timer_disable();
55+
timer.delay(ms);
6156
#endif
6257
}
6358

@@ -89,14 +84,12 @@ static Buttons_t scan_buttons(uint32_t timeout) {
8984
}
9085

9186
int main(void) {
92-
timer_init();
93-
9487
// Init spi driver.
9588
Lcd spi(platform::Spi::SpiLcd);
9689

9790
// Turn on LCD backlight via PWM
98-
Pwm pwm(platform::Pwm::Pwm6);
99-
pwm.set(1, 155);
91+
Pwm pwm(platform::Pwm::PwmLcd);
92+
pwm.set(1, 255);
10093

10194
// Set the initial state of the LCD control pins.
10295
spi.chip_select(false);

sw/legacy/hal/timer.hh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright lowRISC contributors.
2+
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
#pragma once
6+
7+
#include "mmio/timer.hh"
8+
#include "platform.hh"
9+
10+
class Timer {
11+
public:
12+
const platform::Timer base_addr;
13+
const std::size_t clock_hz;
14+
15+
constexpr Timer(platform::Timer base_addr, std::size_t clock) : base_addr(base_addr), clock_hz(clock) {
16+
mmio::timer::Timer timer(base_addr);
17+
timer.mtimecmph.value.write(std::numeric_limits<uint32_t>::max()).commit();
18+
timer.mtimecmpl.value.write(std::numeric_limits<uint32_t>::max()).commit();
19+
}
20+
21+
uint64_t get() {
22+
mmio::timer::Timer timer(base_addr);
23+
return static_cast<uint64_t>(timer.mtimeh.fetch().value.get()) << 32 | timer.mtimel.fetch().value.get();
24+
}
25+
26+
void delay(uint64_t millis) {
27+
uint64_t timeout = (millis * clock_hz / 1000) + get();
28+
while (timeout > get());
29+
}
30+
};
31+
32+
class CountDown {
33+
Timer& timer;
34+
uint64_t timeout;
35+
36+
public:
37+
CountDown(Timer& timer, uint64_t millis) : timer(timer) { timeout = (millis * timer.clock_hz / 1000) + timer.get(); }
38+
39+
bool has_timeout() { return timeout < timer.get(); }
40+
41+
void wait() { while (!has_timeout()); }
42+
};

0 commit comments

Comments
 (0)