def get_rtc_time(self): """ Low-level I2C driver prototype for reading time from RTC. """ read = I2CTransaction() # opeartions 0-4 are just low-level boilerplate read.s()\ .a(self.I2C_WRITE_ADDRESS)\ .w(self.CLOCK_BASE_ADDRESS)\ .rs()\ .a(self.I2C_READ_ADDRESS) # operations 5-11 will clock data out from RTC module read.ra(6).rn() read.p() result = dev.i2c_run(read) # uncomment to debug read operations # print('Date read:\n%s' % result.description) bcd_date = [ result.get_data(5) & 0b01111111, # seconds result.get_data(6) & 0b01111111, # minutes result.get_data(7) & 0b00111111, # hours, 24hrs clock result.get_data(8) & 0b00000111, # day of week result.get_data(9) & 0b00111111, # day of month result.get_data(10) & 0b00011111, # month without LP bit result.get_data(11), # year is encoded as 8-bit ] date = [self._from_bcd(bcd) for bcd in bcd_date] return date
def control(self, byte): t = I2CTransaction() t.s()\ .a(self.I2C_WRITE_ADDRESS)\ .w(self.CONTROL_ADDRESS)\ .w(byte)\ .p() result = self.dev.i2c_run(t) return result.get_data(3)
def wait(self, timeout=0.1): """ Poll device until it's ready to accept new commands. According to 24LC32 datasheet, polling must be done by addressing the device with R/W bit set to 0 - this is write address. :param float timeout: How long to wait for the device :return: True if device is ready, False otherwise """ t = I2CTransaction() t.s().a(self.chip_write_address).p() deadline = time() + timeout while time() < deadline: r = self.device.i2c_run(t) if r.ack(1): return True return False
def set_rtc_time(self, hour, minute, second, weekday, day, month, year): """ Low-level I2C driver prototype for setting time. """ time = [ self.START_BIT | (self._to_bcd(second) & 0b01111111), self._to_bcd(minute), # no masking needed self._to_bcd(hour) & 0b00111111, # mask 12/24 bit self._to_bcd(weekday) & 0b00000111, # mask control bits, self._to_bcd(day), # no masking needed self._to_bcd(month) & 0b00011111, # don't write LP bit self._to_bcd(year) # don't mask anything ] write = I2CTransaction() write.s()\ .a(self.I2C_WRITE_ADDRESS)\ .w(self.CLOCK_BASE_ADDRESS) for byte in time: write.w(byte) write.p() result = self.dev.i2c_run(write)
def set_rtc_alarm(self, hour, minute, second, weekday, day, month): """ Low-level I2C driver prototype for setting alarm time. """ time = [ self._to_bcd(second) & 0b01111111, self._to_bcd(minute) & 0b01111111, # no masking needed self._to_bcd(hour) & 0b00111111, # mask 12/24 bit self.ALARM_POL | self.ALARM_MATCH | self._to_bcd( weekday), # combine polarization and match mode with day self._to_bcd(day), self._to_bcd(month), ] write = I2CTransaction() write.s()\ .a(self.I2C_WRITE_ADDRESS)\ .w(self.ALARM_BASE_ADDRESS) for byte in time: write.w(byte) write.p() result = self.dev.i2c_run(write)
from iob.devices import ArduinoLeonardo from iob import I2CTransaction dev = ArduinoLeonardo('/dev/ttyACM0') dev.connect() dev.i2c_enable = True print("I2C reset on bus error = %s" % dev.i2c_rst_on_error) print("I2C initial status = %s" % dev.i2c_status) print('\nWriting 7 bytes:') write = I2CTransaction() write.s().a(0b11011110).w(0x20) for byte in [1, 2, 3, 4, 5, 6, 7]: write.w(byte) write.p() write_result = dev.i2c_run(write) print(write_result.description) print('\nReading 7 bytes:') read = I2CTransaction() read.s()\ .a(0b11011110)\ .w(0x20)\ .rs()\ .a(0b11011111) # example of chained API read.ra(6).rn() # read 6 bytes with ACK and 7th byte with NACK read.p() # stop bus read_result = dev.i2c_run(read)