Browse Source

implement writing of the time to the devices

Helmut Pozimski 5 years ago
parent
commit
807f84959d
1 changed files with 48 additions and 5 deletions
  1. 48 5
      rtc-ds13307.c

+ 48 - 5
rtc-ds13307.c

@@ -29,9 +29,6 @@
 
 #define DS1307_MAX_ADDR 0x3F
 
-#define DS1337_HOUR 0x02
-#define DS1337_DATE 0x03
-
 #define DS1337_CTL 0x0E
 #define DS1337_STAT 0x0F
 
@@ -64,13 +61,25 @@ static int ds13307_read_bytes(struct i2c_client *client,
 	}
 }
 
+/* Writes a single byte to a specified address on the device */
+static int ds13307_write_single_byte(struct i2c_client *client, u8 *addr, u8 *byte) {
+	int r;
+	u8 buf[2];
+	buf[0] = (*addr);
+	buf[1] = (*byte);
+	r = i2c_master_send(client, buf, 2);
+	if (r != 2) {
+		return -EIO;
+	}
+	return 0;
+}
 
 static int ds13307_read_time(struct device *dev, struct rtc_time *time) {
 	struct i2c_client *client;
 	int r;
 	int h12 = 0;
 	u8 buf[7], stopbit;
-	unsigned char addr = COMMON_SEC;
+	u8 addr = COMMON_SEC;
 	client = to_i2c_client(dev);
 	r = ds13307_read_bytes(client, &addr, buf, 7);
 	if (model_detected == DEVICE_DS1337) {
@@ -78,7 +87,7 @@ static int ds13307_read_time(struct device *dev, struct rtc_time *time) {
 		r = ds13307_read_bytes(client, &addr, &stopbit, 1);
 		stopbit = stopbit & COMMON_HIGH_BIT;
 	} else {
-		stopbit = buf[COMMON_SEC] & 0x40;
+		stopbit = buf[COMMON_SEC] & COMMON_HIGH_BIT;
 	}
 	if (r) {
 		return -EIO;
@@ -111,8 +120,42 @@ static int ds13307_read_time(struct device *dev, struct rtc_time *time) {
 	return 0;
 }
 
+static int ds13307_set_time(struct device *dev, struct rtc_time *time) {
+	struct i2c_client *client;
+	int r = 0, i;
+	u8 buf[7], addrs[7];
+	client = to_i2c_client(dev);
+	buf[0] = bin2bcd(time->tm_sec);
+	buf[1] = bin2bcd(time->tm_min);
+	buf[2] = bin2bcd(time->tm_hour);
+	buf[3] = bin2bcd(time->tm_mday);
+	if (model_detected == DEVICE_DS1337) {
+		buf[4] = bin2bcd(time->tm_mon + 1) | COMMON_HIGH_BIT;
+	} else {
+		buf[4] = bin2bcd(time->tm_mon + 1);
+	}
+	buf[5] = bin2bcd(time->tm_year % 100);
+	buf[6] = bin2bcd(time->tm_wday);
+	addrs[0] = COMMON_SEC;
+	addrs[1] = COMMON_MIN;
+	addrs[2] = COMMON_HOUR;
+	addrs[3] = COMMON_DATE;
+	addrs[4] = COMMON_MONTH;
+	addrs[5] = COMMON_YEAR;
+	addrs[6] = COMMON_DAY;
+	for (i=0; i<6; i++) {
+		r = ds13307_write_single_byte(client, &addrs[i], &buf[i]);
+		if (r) {
+			return -EIO;
+		}
+	}
+	return 0;
+	
+}
+
 static const struct rtc_class_ops ds13307_rtc_ops = {
 	.read_time = ds13307_read_time,
+	.set_time = ds13307_set_time
 };
 
 /* Performs the device detection to distinguish between the