jcrona / tamalib

A hardware agnostic Tamagotchi P1 emulation library
GNU General Public License v2.0
165 stars 17 forks source link

tamalib MCU frequency #9

Open user-e opened 2 years ago

user-e commented 2 years ago

Not an issue, just a question.

It was pretty easy to run tamalib and Tamagothi P1 on Raspberry Pi Pico Clone RP2040 with a SSD1306 display, but i got some problem to understand how to set the correctly run frequency to get the time and speed run correctly.

bool_t tamalib_init(const u12_t *program, breakpoint_t *breakpoints, u32_t freq);

In tamalib_init does the freq value mean the MCU frequency?

If yes, does the sleep_until function used to add a delay and simulate the 32768Hz?

1-D6-B7-DD9-9-D00-45-C5-93-A4-AEE7-C7-E41608

jcrona commented 2 years ago

Hi, sorry for the delay !

The frequency in tamalib_init can be whatever you want, actually. It just needs to match the granularity of your internal logical time base, and thus the granularity of the timestamps used by hal_get_timestamp() and hal_sleep_until(), allowing TamaLIB to know how to deal with them. That means that if your time base is a counter incremented at 32768 Hz, you need to call tamalib_init with 32768 and use that counter for all timestamp related operations.

Note that since the emulation is not cycle-accurate, you can have a logical time base with a granularity higher than the one of your hardware. For instance, you can have a counter with a granularity of 1µs (1000000 Hz), incremented by an HW timer running at 1000 Hz (thus doing += 1000; each tick).

user-e commented 2 years ago

Many thanks @jcrona !

short version:


static timestamp_t hal_get_timestamp_us()
{
    return (timestamp_t) to_us_since_boot (get_absolute_time());
}

static void hal_sleep_until(timestamp_t ts)
{
    while((int32_t) (ts - hal_get_timestamp_us()) > 0);
}

tamalib_register_hal(&my_hal);
tamalib_init(g_program,NULL, 1000000);
tamalib_mainloop();

I'm having a perfect timing now :) I will (really sloooowling) made the same work as OpenTama but with RP2040 MCU!