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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.