|
19 | 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 | 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21 | 21 | # THE SOFTWARE.
|
22 |
| - |
23 | 22 | import logging
|
| 23 | +import subprocess |
24 | 24 |
|
25 | 25 | import smbus
|
26 | 26 |
|
@@ -65,6 +65,24 @@ def get_i2c_device(address, busnum=None, **kwargs):
|
65 | 65 | busnum = get_default_bus()
|
66 | 66 | return Device(address, busnum, **kwargs)
|
67 | 67 |
|
| 68 | +def require_repeated_start(): |
| 69 | + """Enable repeated start conditions for I2C register reads. This is the |
| 70 | + normal behavior for I2C, however on some platforms like the Raspberry Pi |
| 71 | + there are bugs which disable repeated starts unless explicitly enabled with |
| 72 | + this function. See this thread for more details: |
| 73 | + http://www.raspberrypi.org/forums/viewtopic.php?f=44&t=15840 |
| 74 | + """ |
| 75 | + plat = Platform.platform_detect() |
| 76 | + if plat == Platform.RASPBERRY_PI: |
| 77 | + # On the Raspberry Pi there is a bug where register reads don't send a |
| 78 | + # repeated start condition like the kernel smbus I2C driver functions |
| 79 | + # define. As a workaround this bit in the BCM2708 driver sysfs tree can |
| 80 | + # be changed to enable I2C repeated starts. |
| 81 | + subprocess.check_call('chmod 666 /sys/module/i2c_bcm2708/parameters/combined', shell=True) |
| 82 | + subprocess.check_call('echo -n 1 > /sys/module/i2c_bcm2708/parameters/combined', shell=True) |
| 83 | + # Other platforms are a no-op because they (presumably) have the correct |
| 84 | + # behavior and send repeated starts. |
| 85 | + |
68 | 86 |
|
69 | 87 | class Device(object):
|
70 | 88 | """Class for communicating with an I2C device using the smbus library.
|
|
0 commit comments