RISC-V: A Bare-metal Introduction using C++. Machine Mode Timer.

The Machine Level ISA Timer

Timekeeping in Modern C++

namespace driver {
struct default_timer_config {
static constexpr unsigned int MTIME_FREQ_HZ=32768;
};
template<class CONFIG=default_timer_config> class timer {
/** Duration of each timer tick */
using timer_ticks = std::chrono::duration<int,
std::ratio<1, CONFIG::MTIME_FREQ_HZ>>;
}
}
uint64_t value_from_mtime = ...;
auto value_in_ms =
std::chrono::duration_cast<std::chrono::microseconds>(
driver::timer::timer_ticks(value_from_mtime));
auto time_offset = std::chrono::microseconds(???);
uint64_t value_of_mtimecmp = std::chrono::duration_cast<timer_ticks>
(time_offset).count();

Reading/Writing MMIO Registers in C++

struct mtimer_address_spec {
static constexpr std::uintptr_t
MTIMECMP_ADDR = 0x2000000 + 0x4000;
static constexpr std::uintptr_t
MTIME_ADDR = 0x2000000 + 0xBFF8;
};
template<class ADDRESS_SPEC=mtimer_address_spec>
void set_raw_time_cmp(uint64_t clock_offset) {
// Single bus access
auto mtimecmp = reinterpret_cast<volatile std::uint64_t *>
(ADDRESS_SPEC::MTIMECMP_ADDR);
*mtimecmp = *mtimecmp + clock_offset;
}

Conclusion

64 Bit Registers Access on a 32 Bit Bus

--

--

--

Software Engineer, Software Architect, Embedded Systems, Distributed Systems, Digital Design, ソフトウェアエンジニア http://www.linkedin.com/in/phil-mulholland-884a8

Love podcasts or audiobooks? Learn on the go with our new app.

[C++] Understanding Singly Linked List

If-statements design: guard clauses may be all you need

What “Big O” Really Means

Jupyter Notebooks in VS Code

An internal view on Unicorn Orchestrator

4 things to do to build Internal. Control

Automating Mailchimp Emails on sign up using Javascript (Firebase Cloud Functions)

Software Development Processes: Less is More

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Phil Mulholland

Phil Mulholland

Software Engineer, Software Architect, Embedded Systems, Distributed Systems, Digital Design, ソフトウェアエンジニア http://www.linkedin.com/in/phil-mulholland-884a8

More from Medium

System Embedded

C++20 Concepts: part 3

[C++] namespace

Modern C++ in Advent of Code: Day18