Other OS

Module rtc-ds1307

3 2032
Hi,

I use the Busware COC on Banana Pro. This Module has a RTC on board. Unfortunately it seems that driver is not in Kernel. Is there any solution getting it to run?

it is shown on IC2 correctly on Adress 68
  1. root@lemaker ~ > i2cdetect -y 2
  2.      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
  3. 00:          -- -- -- -- -- -- -- -- -- -- -- -- --
  4. 10: -- -- -- -- -- -- -- -- 18 -- -- -- -- -- -- --
  5. 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  6. 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  7. 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  8. 50: 50 51 52 53 54 55 56 57 -- -- -- -- -- -- -- --
  9. 60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
  10. 70: -- -- -- -- -- -- -- --
Copy the Code
  1. root@lemaker ~ > cat /sys/class/i2c-adapter/i2c-2/2-0068/modalias
  2. i2c:ds1307
Copy the Code
  1. root@lemaker / > modprobe rtc-ds1307
  2. FATAL: Module rtc-ds1307 not found.
Copy the Code
RTC-DS1307 you can try with the following code to read and write:
  1. #!/usr/bin/env python

  2. # RTC_DS1307.py Python Driver Code
  3. # only works in 24 hour mode

  4. from datetime import datetime

  5. import smbus


  6. def _bcd_to_int(bcd):
  7.     """Decode a 2x4bit BCD to a integer.
  8.     """
  9.     out = 0
  10.     for d in (bcd >> 4, bcd):
  11.         for p in (1, 2, 4 ,8):
  12.             if d & 1:
  13.                 out += p
  14.             d >>= 1
  15.         out *= 10
  16.     return out / 10


  17. def _int_to_bcd(n):
  18.     """Encode a one or two digits number to the BCD.
  19.     """
  20.     bcd = 0
  21.     for i in (n // 10, n % 10):
  22.         for p in (8, 4, 2, 1):
  23.             if i >= p:
  24.                 bcd += 1
  25.                 i -= p
  26.             bcd <<= 1
  27.     return bcd >> 1


  28. class RTC_DS1307():
  29.     _REG_SECONDS = 0x00
  30.     _REG_MINUTES = 0x01
  31.     _REG_HOURS = 0x02
  32.     _REG_DAY = 0x03
  33.     _REG_DATE = 0x04
  34.     _REG_MONTH = 0x05
  35.     _REG_YEAR = 0x06
  36.     _REG_CONTROL = 0x07


  37.     def __init__(self, twi=2, addr=0x68):
  38.         self._bus = smbus.SMBus(twi)
  39.         self._addr = addr



  40.     def _write(self, register, data):
  41.         #print "addr =0x%x register = 0x%x data = 0x%x %i " % (self._addr, register, data,_bcd_to_int(data))
  42.         self._bus.write_byte_data(self._addr, register, data)


  43.     def _read(self, data):

  44.         returndata = self._bus.read_byte_data(self._addr, data)
  45.         #print "addr = 0x%x data = 0x%x %i returndata = 0x%x %i " % (self._addr, data, data, returndata, _bcd_to_int(returndata))
  46.         return returndata

  47.     def _read_seconds(self):
  48.         return _bcd_to_int(self._read(self._REG_SECONDS))


  49.     def _read_minutes(self):
  50.         return _bcd_to_int(self._read(self._REG_MINUTES))


  51.     def _read_hours(self):
  52.         d = self._read(self._REG_HOURS)
  53.         if (d == 0x64):
  54.                 d = 0x40
  55.         return _bcd_to_int(d & 0x3F)


  56.     def _read_day(self):
  57.         return _bcd_to_int(self._read(self._REG_DAY))


  58.     def _read_date(self):
  59.         return _bcd_to_int(self._read(self._REG_DATE))


  60.     def _read_month(self):
  61.         return _bcd_to_int(self._read(self._REG_MONTH))


  62.     def _read_year(self):
  63.         return _bcd_to_int(self._read(self._REG_YEAR))


  64.     def read_all(self):
  65.         """Return a tuple such as (year, month, date, day, hours, minutes,
  66.         seconds).
  67.         """
  68.         return (self._read_year(), self._read_month(), self._read_date(),
  69.                 self._read_day(), self._read_hours(), self._read_minutes(),
  70.                 self._read_seconds())


  71.     def read_str(self):
  72.         """Return a string such as 'YY-DD-MMTHH-MM-SS'.
  73.         """
  74.         return '%02d-%02d-%02dT%02d:%02d:%02d' % (self._read_year(),
  75.                 self._read_month(), self._read_date(), self._read_hours(),
  76.                 self._read_minutes(), self._read_seconds())


  77.     def read_datetime(self, century=21, tzinfo=None):
  78.         """Return the datetime.datetime object.
  79.         """
  80.         return datetime((century - 1) * 100 + self._read_year(),
  81.                 self._read_month(), self._read_date(), self._read_hours(),
  82.                 self._read_minutes(), self._read_seconds(), 0, tzinfo=tzinfo)


  83.     def write_all(self, seconds=None, minutes=None, hours=None, day=None,
  84.             date=None, month=None, year=None, save_as_24h=True):
  85.         """Direct write un-none value.
  86.         Range: seconds [0,59], minutes [0,59], hours [0,23],
  87.                day [0,7], date [1-31], month [1-12], year [0-99].
  88.         """
  89.         if seconds is not None:
  90.             if seconds < 0 or seconds > 59:
  91.                 raise ValueError('Seconds is out of range [0,59].')
  92.             self._write(self._REG_SECONDS, _int_to_bcd(seconds))

  93.         if minutes is not None:
  94.             if minutes < 0 or minutes > 59:
  95.                 raise ValueError('Minutes is out of range [0,59].')
  96.             self._write(self._REG_MINUTES, _int_to_bcd(minutes))

  97.         if hours is not None:
  98.             if hours < 0 or hours > 23:
  99.                 raise ValueError('Hours is out of range [0,23].')
  100.             self._write(self._REG_HOURS, _int_to_bcd(hours))  # not | 0x40 as in the orignal code

  101.         if year is not None:
  102.             if year < 0 or year > 99:
  103.                 raise ValueError('Years is out of range [0,99].')
  104.             self._write(self._REG_YEAR, _int_to_bcd(year))

  105.         if month is not None:
  106.             if month < 1 or month > 12:
  107.                 raise ValueError('Month is out of range [1,12].')
  108.             self._write(self._REG_MONTH, _int_to_bcd(month))

  109.         if date is not None:
  110.             if date < 1 or date > 31:
  111.                 raise ValueError('Date is out of range [1,31].')
  112.             self._write(self._REG_DATE, _int_to_bcd(date))

  113.         if day is not None:
  114.             if day < 1 or day > 7:
  115.                 raise ValueError('Day is out of range [1,7].')
  116.             self._write(self._REG_DAY, _int_to_bcd(day))


  117.     def write_datetime(self, dt):
  118.         """Write from a datetime.datetime object.
  119.         """
  120.         self.write_all(dt.second, dt.minute, dt.hour,
  121.                 dt.isoweekday(), dt.day, dt.month, dt.year % 100)


  122.     def write_now(self):
  123.         """Equal to DS1307.write_datetime(datetime.datetime.now()).
  124.         """
  125.         self.write_datetime(datetime.now())

Copy the Code

hnsncxy  
Thanks for your sharing

works for me, too! thank you.

You have to log in before you can reply Login | Sign Up

Points Rules