Baeseke
Posts: 2
Joined: Wed Sep 26, 2012 9:15 am

spi

Tue Oct 16, 2012 4:58 pm

Dear,

I'm trying to get an DS1390 SPI RTC (http://datasheets.maximintegrated.com/e ... DS1394.pdf) working on a raspberry pi.
I'm using debian wheezy with the http://www.open.com.au/mikem/bcm2835/index.html lib, currently i'm using this code:

Code: Select all

 int main(int argc, char **argv) {
    // If you call this, it will not actually access the GPIO
// Use for testing
//        bcm2835_set_debug(1);

      if (!bcm2835_init())
        return 1;

    bcm2835_spi_begin();
    bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_LSBFIRST);      // The default
    bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);                   // The default
    bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_512); // The default
    bcm2835_spi_chipSelect(BCM2835_SPI_CS0);                      // The default
    bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, HIGH);   // the default




	//sleep(5);
		unsigned char msec=0, sec=0x12, min=0x23, hr=0x05, dow=0x07, date=0x25, mon=0x08, yr=0x12;

    // Send a byte to the slave and simultaneously read a byte back from the slave
    // If you tie MISO to MOSI, you should read back what was sent

		
			uint8_t data = bcm2835_spi_transfer(0x8d);
	   		printf("Dummy: %02X\n", data);
			data = bcm2835_spi_transfer(0x18);
	   		printf("Dummy: %02X\n", data);

			data = bcm2835_spi_transfer(0x80);
			printf("Dummy: %02X\n", data);
			data = bcm2835_spi_transfer(msec);
			printf("Dummy: %02X\n", data);
			data = bcm2835_spi_transfer(sec);
			printf("Dummy: %02X\n", data);
			data = bcm2835_spi_transfer(min);
			printf("Dummy: %02X\n", data);
			data = bcm2835_spi_transfer(hr);
			printf("Dummy: %02X\n", data);
			data = bcm2835_spi_transfer(dow);
			printf("Dummy: %02X\n", data);
			data = bcm2835_spi_transfer(date);
			printf("Dummy: %02X\n", data);
			data = bcm2835_spi_transfer(mon);
			printf("Dummy: %02X\n", data);
			data = bcm2835_spi_transfer(yr);
			printf("Dummy: %02X\n", data);

		data = bcm2835_spi_transfer(0);
			printf("Dummy: %02X\n", data);
			data = bcm2835_spi_transfer(0);
			printf("msec: %02X\n", data);		
			data = bcm2835_spi_transfer(0);
			printf("sec: %02X\n", data);
			data = bcm2835_spi_transfer(0);
			printf("min: %02X\n", data);
			data = bcm2835_spi_transfer(0);
			printf("hr: %02X\n", data);
			data = bcm2835_spi_transfer(0);
			printf("dow: %02X\n", data);
			data = bcm2835_spi_transfer(0);
			printf("date: %02X\n", data);
			data = bcm2835_spi_transfer(0);
			printf("mon: %02X\n", data);
			data = bcm2835_spi_transfer(0);
			printf("yr: %02X\n", data);           
    bcm2835_spi_end();
    return 0;
}
Normally i would use a buffer and other stuff to shorten the code but for the moment the data I receive is always incorrect and i even looks like it's rotating with 4 byte.

Am I doing something wrong here?
Thanks in advance
Baeseke

User avatar
gordon@drogon.net
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: spi

Wed Oct 17, 2012 12:07 pm

Baeseke wrote: Am I doing something wrong here?
Thanks in advance
Baeseke
May I ask why you're not using the Kernel SPI driver? It's trivially easy to do stuff like this with it.

I can't think of any reason whatsoever to not use it these days. It's been stable for months now.

-Gordon
--
Gordons projects: https://projects.drogon.net/

Baeseke
Posts: 2
Joined: Wed Sep 26, 2012 9:15 am

Re: spi

Wed Oct 17, 2012 10:41 pm

Dear,

I've been testing with all the possible drivers i could find.
After changing some settings it's almost working.
The only problem I have right now is that the RTC doesn't count, the data is always the same, even when the status registers have the correct bits

User avatar
xranby
Posts: 540
Joined: Sat Mar 03, 2012 10:02 pm
Contact: Website

Re: spi DS1390 / DS1394

Tue Oct 03, 2017 10:27 am

Can you share the devicetree .dts configuration you made?

Trickle charge support was added first in 2015 for the linux kernel driver thus you may need a fairly recent 4.4 linux kernel if you require such support.
https://github.com/torvalds/linux/commi ... c-ds1390.c
Xerxes Rånby @xranby I once had two, then I gave one away. Now both are in use every day!
twitter.com/xranby

User avatar
PeterO
Posts: 5828
Joined: Sun Jul 22, 2012 4:14 pm

Re: spi

Tue Oct 03, 2017 10:51 am

Did we even have device tree support five years ago (that's how old this thread is!).
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
xranby
Posts: 540
Joined: Sat Mar 03, 2012 10:02 pm
Contact: Website

Re: spi DS1390 / DS1394

Tue Oct 03, 2017 2:56 pm

I advice you to look at using the kernel module and a device tree overlay to add the ds1390 to your system as a system realtime clock.

I could not find any examples online how to configure a device tree for the ds1390 thus I have created something here that you can look at and use as a reference. I have composed a devicetree overlay that can detect a ds1394 on the spi bus (same driver as the ds1390), you need to change the trickle resistor and use of internal diode to match your setup.

ds1390-rpi.dts

Code: Select all

/dts-v1/;
/plugin/;

/ {
    compatible = "brcm,bcm2835";

    fragment@0 {
        target = <&spi0>;
        __overlay__ {

            /* disable spi-dev for spi0.0 */
            spidev@1 {
                status = "disabled";
            };
            spidev@0 {
                status = "disabled";
            };
            /* enable ds1394 RTC clock */
            ds1390: rtc@0 {
                compatible = "rtc-ds1390";
                reg = <0>;
                trickle-diode-disable = <0>;
                trickle-resistor-ohms = <250>;
                spi-max-frequency = <4000000>;
                status = "okay";
            };
        };
    };
};

to ease compilation you create a

Makefile

Code: Select all

DTC=dtc

all: ds1390-rpi.dtb

ds1390-rpi.dtb: ds1390-rpi.dts
        $(DTC) -@ -I dts -O dtb -o ds1390-rpi.dtbo ds1390-rpi.dts

install-ds1390-rpi.dtbo: ds1390-rpi.dtbo
        cp ds1390-rpi.dtbo /boot/overlays/

install: install-ds1390-rpi.dtbo

clean:
        rm *.dtbo
to install you run

Code: Select all

 make
sudo make install
You also need to add the following line to /boot/config.txt

Code: Select all

dtoverlay=ds1390-rpi
when you boot up you can check the "dmesg" command and notice that the ds1390 or ds1394 has been registered as a system rtc clock. you can use sudo hwclock -r to read the time stored in the chip sudo hwclock -s to set the system time from the clock chip and sudo hwclock -w to write system time to the clock chip
Xerxes Rånby @xranby I once had two, then I gave one away. Now both are in use every day!
twitter.com/xranby

User avatar
xranby
Posts: 540
Joined: Sat Mar 03, 2012 10:02 pm
Contact: Website

Re: spi

Mon Jun 22, 2020 12:34 pm

Baeseke wrote:
Wed Oct 17, 2012 10:41 pm
The only problem I have right now is that the RTC doesn't count, the data is always the same, even when the status registers have the correct bits
If you have never performed any write to the rtc0 then you can get the following error from hwclock

Code: Select all

$ sudo hwclock
hwclock:  ioctl(RTC_RD_TIME) to /dev/rtc0 to read the time failed: Invalid argument
this is fixed by setting-up and setting the time of the rtc0 once using

Code: Select all

hwclock -w
this will initialize the rtc0 to start trickle charge/or start use the battery, after that hwclock will work.

Double check that the clock crystal is correctly rotated,
the pick and place machine had rotated all clock crystals 180 degree for one batch of add-on boards I received using ds1394.
incorrect clock crystal orientation for ds1394.jpg
incorrect clock crystal orientation for ds1394.jpg (84.57 KiB) Viewed 203 times
correct clock crystal.jpg.jpg
correct clock crystal.jpg.jpg (59.19 KiB) Viewed 203 times
if the clock crystal is rotated and not working.

Code: Select all

$ sudo hwclock
hwclock: Timed out waiting for time change.
Xerxes Rånby @xranby I once had two, then I gave one away. Now both are in use every day!
twitter.com/xranby

LdB
Posts: 1549
Joined: Wed Dec 07, 2016 2:29 pm

Re: spi

Wed Jun 24, 2020 2:02 am

necro on a necro ... 8 years and counting

Return to “C/C++”