I’ve spent a few days on this already and everything I find doesn’t exactly fit what I’m asking. But I apologize ahead of time if this has been answered and I didn’t see it or didn’t quite understand the answer.
Here’s my scenario, I’m learning Zig by building a HAL (Hardware Abstraction Layer) for an ARM Processor. I’m trying to simplify the process of setting up the processor for the end user by deriving some of the needed calculations with comptime.
Here’s the issue I’m running into…
What I’d LIKE to do is, in my main.zig do something like
xosc.setFrequency(12000000,130000000);
With the two values being the crystal oscillator frequency and the final PLL frequency for the system.
Now, There are various places in the code (PLL, PWM, Clocks, etc) that use calculations that are based on the frequency the system is running at.
So what I want is when I use that function call above it would set a COMPTIME constant for the frequency the system is running at in a known place like the XOSC.zig file. I do not want to hardkey it as a constant because the user might change it based on their needs. I also want to prevent them from having to redeclare it or pass it again in every file for every peripheral.
I’d like to use comptime to derive the values they’d need to get the peripherals running with their system frequency and then they enable the peripheral if they want.
I’ve seen all kinds of reasoning about Zig not having compile time mutable things, but really I guess what I’m trying to do is have a dynamic constant (oxymoron?).
Please let me know if I can explain that better. But if anyone can point me to a way to achieve that, it would be much appreciated.
Thanks.