Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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 reply to Re: I2C help (from python) by RonW
in thread I2C help (from python) by packetstormer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2024-04-23 13:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found