5 * Copyright (c) 2013 Gordon Henderson.
6 ***********************************************************************
7 * This file is part of wiringPi:
8 * https://projects.drogon.net/raspberry-pi/wiringpi/
10 * wiringPi is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * wiringPi is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
22 ***********************************************************************
50 static int dPin, cPin, sPin ;
54 * Shift a number in from the chip, LSB first. Note that the data is
55 * sampled on the trailing edge of the last clock, so it's valid immediately.
56 *********************************************************************************
59 static unsigned int dsShiftIn (void)
64 pinMode (dPin, INPUT) ; delayMicroseconds (1) ;
66 for (i = 0 ; i < 8 ; ++i)
68 value |= (digitalRead (dPin) << i) ;
69 digitalWrite (cPin, HIGH) ; delayMicroseconds (1) ;
70 digitalWrite (cPin, LOW) ; delayMicroseconds (1) ;
79 * A normal LSB-first shift-out, just slowed down a bit - the Pi is
80 * a bit faster than the chip can handle.
81 *********************************************************************************
84 static void dsShiftOut (unsigned int data)
88 pinMode (dPin, OUTPUT) ;
90 for (i = 0 ; i < 8 ; ++i)
92 digitalWrite (dPin, data & (1 << i)) ; delayMicroseconds (1) ;
93 digitalWrite (cPin, HIGH) ; delayMicroseconds (1) ;
94 digitalWrite (cPin, LOW) ; delayMicroseconds (1) ;
100 * ds1302regRead: ds1302regWrite:
101 * Read/Write a value to an RTC Register or RAM location on the chip
102 *********************************************************************************
105 static unsigned int ds1302regRead (const int reg)
109 digitalWrite (sPin, HIGH) ; delayMicroseconds (1) ;
111 data = dsShiftIn () ;
112 digitalWrite (sPin, LOW) ; delayMicroseconds (1) ;
117 static void ds1302regWrite (const int reg, const unsigned int data)
119 digitalWrite (sPin, HIGH) ; delayMicroseconds (1) ;
122 digitalWrite (sPin, LOW) ; delayMicroseconds (1) ;
127 * ds1302rtcWrite: ds1302rtcRead:
128 * Writes/Reads the data to/from the RTC register
129 *********************************************************************************
132 unsigned int ds1302rtcRead (const int reg)
134 return ds1302regRead (0x81 | ((reg & 0x1F) << 1)) ;
137 void ds1302rtcWrite (int reg, unsigned int data)
139 ds1302regWrite (0x80 | ((reg & 0x1F) << 1), data) ;
144 * ds1302ramWrite: ds1302ramRead:
145 * Writes/Reads the data to/from the RTC register
146 *********************************************************************************
149 unsigned int ds1302ramRead (const int addr)
151 return ds1302regRead (0xC1 | ((addr & 0x1F) << 1)) ;
154 void ds1302ramWrite (const int addr, const unsigned int data)
156 ds1302regWrite ( 0xC0 | ((addr & 0x1F) << 1), data) ;
161 * Read all 8 bytes of the clock in a single operation
162 *********************************************************************************
165 void ds1302clockRead (int clockData [8])
168 unsigned int regVal = 0x81 | ((RTC_BM & 0x1F) << 1) ;
170 digitalWrite (sPin, HIGH) ; delayMicroseconds (1) ;
172 dsShiftOut (regVal) ;
173 for (i = 0 ; i < 8 ; ++i)
174 clockData [i] = dsShiftIn () ;
176 digitalWrite (sPin, LOW) ; delayMicroseconds (1) ;
182 * Write all 8 bytes of the clock in a single operation
183 *********************************************************************************
186 void ds1302clockWrite (const int clockData [8])
189 unsigned int regVal = 0x80 | ((RTC_BM & 0x1F) << 1) ;
191 digitalWrite (sPin, HIGH) ; delayMicroseconds (1) ;
193 dsShiftOut (regVal) ;
194 for (i = 0 ; i < 8 ; ++i)
195 dsShiftOut (clockData [i]) ;
197 digitalWrite (sPin, LOW) ; delayMicroseconds (1) ;
202 * ds1302trickleCharge:
203 * Set the bits on the trickle charger.
204 * Probably best left alone...
205 *********************************************************************************
208 void ds1302trickleCharge (const int diodes, const int resistors)
210 if (diodes + resistors == 0)
211 ds1302rtcWrite (RTC_TC, 0x5C) ; // Disabled
213 ds1302rtcWrite (RTC_TC, 0xA0 | ((diodes & 3) << 2) | (resistors & 3)) ;
221 * Initialise the chip & remember the pins we're using
222 *********************************************************************************
225 void ds1302setup (const int clockPin, const int dataPin, const int csPin)
231 digitalWrite (dPin, LOW) ;
232 digitalWrite (cPin, LOW) ;
233 digitalWrite (sPin, LOW) ;
235 pinMode (dPin, OUTPUT) ;
236 pinMode (cPin, OUTPUT) ;
237 pinMode (sPin, OUTPUT) ;
239 ds1302rtcWrite (RTC_WP, 0) ; // Remove write-protect