Hardware Reference
In-Depth Information
If you want to be certain that the register address is established with a known value,
you should always issue a write request first. In the preceding diagram, the write request
immediately follows the start bit (S). Only the peripheral's register address byte is written
out prior to the repeating start bit (RS), which follows.
After the RS bit, the peripheral address is transmitted once more to re-engage
the DS1307, but this time as a read request. From that point on, the master reads
bytes sequentially from the DS13017 until a NAK is sent. The final stop bit (P) sent by
the master ends the exchange. This peripheral provides us with a good example of a
multimessage I/O.
This is demonstrated in lines 27 to 45 of the program ds1307get.c , in the upcoming
pages. The entire I/O is driven by the structures iomsgs[0] and iomsgs[1] . Structure
iomsgs[0] directs the driver to write to peripheral address 0x68 and writes 1 0x00 data
byte out to it. This establishes the RTC's internal register with a value of 0x00. The read
request is described in iomsgs[1] , which is a read from the same peripheral 0x68, for
8 bytes. (Only 7 bytes are strictly required for the date and time, but we read the
additional control byte anyway.)
The data structure is laid out in C terms in the file ds1307.h . An optional exercise for
you is to add a command-line option to ds1307set to stop the clock and turn it on again
using the ch bit (line 8 of ds1307.h ).
Source module i2c_common.c has the usual I2C open/initialization and close
routines in it.
Wiring
Like any I2C project for the Pi, you'll wire the SDA and SCL lines as follows:
Pre Rev 2.0
Rev 2.0+
GPIO
Line
GPIO
Line
P1
0
SDA0
2
SDA1
P1-03
1
SCL0
3
SCL1
P1-05
The DS1307 PCB (or chip) is powered from the +5 V supply. Prior to attaching
it to the Raspberry Pi, it is a good idea to power the DS1307 and measure the voltage
appearing on its SDA and SCL lines. Both should measure near ground potential. If you
see +5 V instead, stop and find out why.
Running the Examples
Since these programs use the I2C Linux drivers, make sure these kernel modules are
either already loaded, or load them manually now:
$ sudo modprobe i2c-bcm2708
$ sudo modprobe i2c-dev
 
 
Search WWH ::




Custom Search