1""" 2I2C test for the CC3200 based boards. 3A MPU-9150 sensor must be connected to the I2C bus. 4""" 5 6from machine import I2C 7import os 8import time 9 10mch = os.uname().machine 11if "LaunchPad" in mch: 12 i2c_pins = ("GP11", "GP10") 13elif "WiPy" in mch: 14 i2c_pins = ("GP15", "GP10") 15else: 16 raise Exception("Board not supported!") 17 18i2c = I2C(0, I2C.MASTER, baudrate=400000) 19# try initing without the peripheral id 20i2c = I2C() 21print(i2c) 22i2c = I2C(mode=I2C.MASTER, baudrate=50000, pins=i2c_pins) 23print(i2c) 24 25i2c = I2C(0, I2C.MASTER, baudrate=100000) 26print(i2c) 27i2c = I2C(0, mode=I2C.MASTER, baudrate=400000) 28print(i2c) 29i2c = I2C(0, mode=I2C.MASTER, baudrate=400000, pins=i2c_pins) 30print(i2c) 31 32addr = i2c.scan()[0] 33print(addr) 34 35reg = bytearray(1) 36reg2 = bytearray(2) 37reg2_r = bytearray(2) 38 39# reset the sensor 40reg[0] |= 0x80 41print(1 == i2c.writeto_mem(addr, 107, reg)) 42time.sleep_ms(100) # wait for the sensor to reset... 43 44print(1 == i2c.readfrom_mem_into(addr, 107, reg)) # read the power management register 1 45print(0x40 == reg[0]) 46 47# now just read one byte 48data = i2c.readfrom_mem(addr, 117, 1) # read the "who am I?" register 49print(0x68 == data[0]) 50print(len(data) == 1) 51print(1 == i2c.readfrom_mem_into(addr, 117, reg)) # read the "who am I?" register again 52print(0x68 == reg[0]) 53 54# now try reading two bytes 55data = i2c.readfrom_mem(addr, 116, 2) # read the "who am I?" register 56print(0x68 == data[1]) 57print(data == b"\x00\x68") 58print(len(data) == 2) 59print(2 == i2c.readfrom_mem_into(addr, 116, reg2)) # read the "who am I?" register again 60print(0x68 == reg2[1]) 61print(reg2 == b"\x00\x68") 62 63print(1 == i2c.readfrom_mem_into(addr, 107, reg)) # read the power management register 1 64print(0x40 == reg[0]) 65# clear the sleep bit 66reg[0] = 0 67print(1 == i2c.writeto_mem(addr, 107, reg)) 68# read it back 69i2c.readfrom_mem_into(addr, 107, reg) 70print(0 == reg[0]) 71 72# set the sleep bit 73reg[0] = 0x40 74print(1 == i2c.writeto_mem(addr, 107, reg)) 75# read it back 76i2c.readfrom_mem_into(addr, 107, reg) 77print(0x40 == reg[0]) 78 79# reset the sensor 80reg[0] |= 0x80 81print(1 == i2c.writeto_mem(addr, 107, reg)) 82time.sleep_ms(100) # wait for the sensor to reset... 83 84# now read and write two register at a time 85print(2 == i2c.readfrom_mem_into(addr, 107, reg2)) 86print(0x40 == reg2[0]) 87print(0x00 == reg2[1]) 88# clear the sleep bit 89reg2[0] = 0 90# set some other bits 91reg2[1] |= 0x03 92print(2 == i2c.writeto_mem(addr, 107, reg2)) 93# read it back 94i2c.readfrom_mem_into(addr, 107, reg2_r) 95print(reg2 == reg2_r) 96 97# reset the sensor 98reg[0] = 0x80 99print(1 == i2c.writeto_mem(addr, 107, reg)) 100time.sleep_ms(100) # wait for the sensor to reset... 101 102# try some raw read and writes 103reg[0] = 117 # register address 104print(1 == i2c.writeto(addr, reg, stop=False)) # just write the register address 105# now read 106print(1 == i2c.readfrom_into(addr, reg)) 107print(reg[0] == 0x68) 108reg[0] = 117 # register address 109print(1 == i2c.writeto(addr, reg, stop=False)) # just write the register address 110# now read 111print(0x68 == i2c.readfrom(addr, 1)[0]) 112 113i2c.readfrom_mem_into(addr, 107, reg2) 114print(0x40 == reg2[0]) 115print(0x00 == reg2[1]) 116 117reg2[0] = 107 # register address 118reg2[1] = 0 119print(2 == i2c.writeto(addr, reg2, stop=True)) # write the register address and the data 120i2c.readfrom_mem_into(addr, 107, reg) # check it back 121print(reg[0] == 0) 122 123# check for memory leaks... 124for i in range(0, 1000): 125 i2c = I2C(0, I2C.MASTER, baudrate=100000) 126 127# test deinit 128i2c = I2C(0, I2C.MASTER, baudrate=100000) 129i2c.deinit() 130print(i2c) 131 132# next ones should raise 133try: 134 i2c.scan() 135except Exception: 136 print("Exception") 137 138try: 139 i2c.readfrom(addr, 1) 140except Exception: 141 print("Exception") 142 143try: 144 i2c.readfrom_into(addr, reg) 145except Exception: 146 print("Exception") 147 148try: 149 i2c.readfrom_mem_into(addr, 107, reg) 150except Exception: 151 print("Exception") 152 153try: 154 i2c.writeto(addr, reg, stop=False) 155except Exception: 156 print("Exception") 157 158try: 159 i2c.writeto_mem(addr, 107, reg) 160except Exception: 161 print("Exception") 162 163try: 164 i2c.readfrom_mem(addr, 116, 2) 165except Exception: 166 print("Exception") 167 168try: 169 I2C(1, I2C.MASTER, baudrate=100000) 170except Exception: 171 print("Exception") 172 173# reinitialization must work 174i2c.init(baudrate=400000) 175print(i2c) 176