-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Assignment from struct literal to variable not working #7055
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
Comments
This is a bug with packed structs, #3133 is the tracking issue to fix them. |
@auwi-nordic The problem here is the use of The temporary workaround is to initialize the fields to zero rather than undefined, I'll try to send a patch later today to fix the problem. Keep in mind that the generated code may not do what you expect it to do, the compiler is currently trying to be smart and chops the loads/stores in small ones (mostly |
It is probably also a good idea to always assign 0 to the unused fields, since the hardware may expect that and/or then you clearly define what you write to the register (even if the value doesn't matter). |
Prevents silent memory corruption. Closes ziglang#7055
@LemonBoy Thanks, initializing to 0 works. Thanks for the heads up regarding loads/stores.. that could be useful to be aware of @FireFox317 You're absolutely right, defaulting to 0 is better I also realized that it might be best to do something like this: pub const PinCnfConfig = struct {
DIR: PinCnfDir = .Input,
INPUT: PinCnfInput = .Disconnect,
PULL: PinCnfPull = .Disabled,
DRIVE: PinCnfDrive = .S0S1,
SENSE: PinCnfSense = .Disabled,
};
pub const PinCnf = packed struct {
DIR: PinCnfDir = 0,
INPUT: PinCnfInput = 0,
PULL: PinCnfPull = 0,
_RESERVED1: u4 = 0,
DRIVE: PinCnfDrive = 0,
_RESERVED3: u5 = 0,
SENSE: PinCnfSense = 0,
_RESERVED4: u14 = 0,
pub fn set(self: *volatile PinCnf, config: PinCnfConfig) void {
self.DIR = config.DIR;
self.INPUT = config.INPUT;
self.PULL = config.PULL;
self.DRIVE = config.DRIVE;
self.SENSE = config.SENSE;
}
pub fn reset(self: *volatile PinCnf) void {
self.set(.{});
}
}; I'm thinking it might be best to put all hardware register accesses behind abstract functions. That way you can write models of the hardware and run at least parts of your firmware code locally before testing on real hardware |
Prevents silent memory corruption. Closes #7055
Prevents silent memory corruption. Closes #7055
In the 0.7.0 release it seems like assigning struct literals to variables does not work when not declaring the variable. Or I've misunderstood something about the language (since it seems like a pretty basic use-case)
Here's an example:
And the result in Case 1 seems similar to not doing any assignment to cnf at all:
The text was updated successfully, but these errors were encountered: