Perl Monk, Perl Meditation | |
PerlMonks |
Re: I2C help (from python)by RonW (Parson) |
on Nov 30, 2015 at 18:24 UTC ( [id://1148939]=note: print w/replies, xml ) | Need Help?? |
So it's really this line self.DEVICE_REG_DATA &= ~(0x1<<0) that's causing my trouble. Welcome to the world of "embedded systems programming". The others have provided good answers to your question. FWIW, I offer some additional information. That line looks like a too literal translation from C to Python. In C, the original was probably a macro: #define CLEAR_BIT(port, n) port.DEVICE_REG_DATA &= ~(0x1<<n) and would be used like: CLEAR_BIT(widget, 2); where widget is a structured variable mapped to a physical device port. So, the expression, port.DEVICE_REG_DATA &= ~(0x1<<n) (in C), will read the current value in the device port's data register, clear bit n, then write the new value back to the device port's data register. In Perl, if you needed real read-modify-write access to a physical register, you would need an XS module to give you a Perl callable API to low level functions that do the read-modify-write operation for your Perl code. This is because while Perl has read-modify-write operators, they were included partly for convenience and partly because expressions like $num_cookies = $num_cookies + $batch_size are more work to maintain than $num_cookies += $batch_size (because the former has more opportunities for making mistakes). Even if Perl internally uses read-modify-write operations, mapping the IV (or UV) field of a Perl scalar is not practical as the the physical registers (at least on the typical $0.50 processors used for embedded control systems) are not necessarily the same size as the IV/UV field, and other fields in the scalar might get in the way.
In Section
Seekers of Perl Wisdom
|
|