]> cloudbase.mooo.com Git - z180-stamp.git/blame - avr/pcf8583.c
Merge branch 'chan-fatfs' into fatfs-integration
[z180-stamp.git] / avr / pcf8583.c
CommitLineData
35edb766
L
1/*
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
61b0cfe9
L
7/*
8 * Date & Time support for Philips PCF8583 RTC
9 */
10
61b0cfe9
L
11#include "common.h"
12#include <stdlib.h>
e63b2f75 13#include "time.h"
61b0cfe9
L
14#include "rtc.h"
15#include "i2c.h"
e63b2f75
L
16#include "command.h"
17#include "debug.h"
61b0cfe9 18
f14850db
L
19#define DEBUG_RTC 0
20
21#define debug_rtc(fmt, args...) \
22 debug_cond(DEBUG_RTC, fmt, ##args)
23
61b0cfe9
L
24#define REG_CS 0x00 /* control/status */
25#define REG_CSEC 0x01 /* hundredth of a second */
26#define REG_SEC 0x02 /* seconds */
27#define REG_MIN 0x03 /* minutes */
28#define REG_HOUR 0x04 /* hours */
29#define REG_YRDATE 0x05 /* year/date */
30#define REG_WDMON 0x06 /* weekdays/months */
31#define NR_OF_REGS 7
32
33
34/* ------------------------------------------------------------------------- */
35
36static uint_fast8_t bcd2bin(uint8_t val)
37{
38 return (val >> 4) * 10 + (val & 0x0f);
39}
40
41static uint8_t bin2bcd (uint_fast8_t val)
42{
43 div_t d = div(val, 10);
e63b2f75 44
61b0cfe9
L
45 return (d.quot << 4) | d.rem;
46}
47
48
e63b2f75 49int rtc_get (struct tm *tmp)
61b0cfe9 50{
61b0cfe9 51 uint8_t rtcbuf[NR_OF_REGS];
e63b2f75
L
52 int16_t year;
53
61b0cfe9
L
54 i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, rtcbuf, NR_OF_REGS);
55 i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0x10, 1, (uint8_t *) &year, 2);
56
85046f8c 57 debug_rtc("Get RTC year: %u, wdays/month: %02x, year/date: %02x, "
61b0cfe9
L
58 "hour: %02x, min: %02x, sec: %02x, (stat: %02x)\n", year,
59 rtcbuf[6], rtcbuf[5], rtcbuf[4], rtcbuf[3], rtcbuf[2], rtcbuf[0]);
60
61 tmp->tm_sec = bcd2bin (rtcbuf[REG_SEC] & 0x7F);
62 tmp->tm_min = bcd2bin (rtcbuf[REG_MIN] & 0x7F);
63 tmp->tm_hour = bcd2bin (rtcbuf[REG_HOUR] & 0x3F);
64 tmp->tm_mday = bcd2bin (rtcbuf[REG_YRDATE] & 0x3F);
e63b2f75 65 tmp->tm_mon = bcd2bin (rtcbuf[REG_WDMON] & 0x1F) - 1;
85046f8c 66 while (year%4 != (rtcbuf[REG_YRDATE]>>6)) {
61b0cfe9 67 year++;
85046f8c 68 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0x10, 1, (uint8_t *) &year, 2);
61b0cfe9 69 }
85046f8c 70 tmp->tm_year = year - 1900;
61b0cfe9
L
71 tmp->tm_wday = rtcbuf[REG_WDMON] >> 5;
72 tmp->tm_yday = 0;
73 tmp->tm_isdst= 0;
74
f14850db 75 debug_rtc( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
61b0cfe9
L
76 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
77 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
78
85046f8c 79 return rtcbuf[REG_CS] == 0x04 ? 0 : 1;
61b0cfe9
L
80}
81
e63b2f75 82int rtc_set (struct tm *tmp)
61b0cfe9
L
83{
84 uint8_t rtcbuf[NR_OF_REGS];
85046f8c 85 int16_t year = tmp->tm_year + 1900;
61b0cfe9 86
f14850db 87 debug_rtc("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
61b0cfe9
L
88 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
89 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
90
91 rtcbuf[REG_CS] = 0x84;
92 rtcbuf[REG_CSEC] = 0x00;
e63b2f75 93 rtcbuf[REG_WDMON ] = (bin2bcd(tmp->tm_mon) + 1) | ((tmp->tm_wday) << 5);
85046f8c 94 rtcbuf[REG_YRDATE] = ((year % 4) << 6) | bin2bcd(tmp->tm_mday);
61b0cfe9
L
95 rtcbuf[REG_HOUR ] = bin2bcd(tmp->tm_hour);
96 rtcbuf[REG_MIN ] = bin2bcd(tmp->tm_min);
97 rtcbuf[REG_SEC ] = bin2bcd(tmp->tm_sec);
98
99 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, rtcbuf, NR_OF_REGS);
85046f8c 100 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0x10, 1, (uint8_t *) &year, 2);
61b0cfe9
L
101 rtcbuf[REG_CS] = 0x04;
102 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, rtcbuf, 1);
103
61b0cfe9
L
104 return 0;
105}