Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow extern variables to volatile #7052

Open
ikskuh opened this issue Nov 10, 2020 · 1 comment
Open

Allow extern variables to volatile #7052

ikskuh opened this issue Nov 10, 2020 · 1 comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@ikskuh
Copy link
Contributor

ikskuh commented Nov 10, 2020

One use case for external volatile variables is mapping them to their hardware address via the linker script instead of using pointers. This makes code less cluttered:

const LED0 = 3;
extern volatile var PORTB: u8;

fn turnOnLed() void {
    PORTB |= (1<<LED0);
}

PORTB is defined in the linker script to the correct hardware address

Note that this use case needs to be considered with #3206.

Current state would be this:

const LED0 = 3;
const PORTB = @intToPtr(*volatile u8, 0x????);

fn turnOnLed() void {
    PORTB.* |= (1<<LED0);
}

Another proposal that might be relevant is #4284 which might still be required to use

@alexnask alexnask added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Nov 10, 2020
@andrewrk andrewrk added this to the 0.8.0 milestone Nov 10, 2020
@andrewrk
Copy link
Member

Here's how to do this with status quo zig:

const LED0 = 3;
extern var PORTB_linker: u8; // relocate this in the linker script
const PORTB: *volatile u8 = &PORTB_linker;

fn turnOnLed() void {
    PORTB.* |= (1<<LED0);
}

@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 May 19, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 Nov 23, 2021
@andrewrk andrewrk modified the milestones: 0.10.0, 0.11.0 Apr 16, 2022
@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Apr 9, 2023
@andrewrk andrewrk modified the milestones: 0.13.0, 0.12.0 Jul 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

3 participants