Beefy Boxes and Bandwidth Generously Provided by pair Networks
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??


in reply to I2C help (from python)

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.

Replies are listed 'Best First'.
Re^2: I2C help (from python)
by BrowserUk (Patriarch) on Nov 30, 2015 at 18:33 UTC
    In C, the original was probably a macro: #define CLEAR_BIT(port, n) port.DEVICE_REG_DATA &= ~(0x1<<n)

    Great observation! I withdraw my "stupidly complex" remark above.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Thanks

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1148939]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-03-28 19:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found