Page 1 of 1

spi

Posted: Tue Oct 16, 2012 4:58 pm
by Baeseke
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

Re: spi

Posted: Wed Oct 17, 2012 12:07 pm
by gordon@drogon.net
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

Re: spi

Posted: Wed Oct 17, 2012 10:41 pm
by Baeseke
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

Re: spi DS1390 / DS1394

Posted: Tue Oct 03, 2017 10:27 am
by xranby
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

Re: spi

Posted: Tue Oct 03, 2017 10:51 am
by PeterO
Did we even have device tree support five years ago (that's how old this thread is!).
PeterO

Re: spi DS1390 / DS1394

Posted: Tue Oct 03, 2017 2:56 pm
by xranby
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

Re: spi

Posted: Mon Jun 22, 2020 12:34 pm
by xranby
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 169 times
correct clock crystal.jpg.jpg
correct clock crystal.jpg.jpg (59.19 KiB) Viewed 169 times
if the clock crystal is rotated and not working.

Code: Select all

$ sudo hwclock
hwclock: Timed out waiting for time change.

Re: spi

Posted: Wed Jun 24, 2020 2:02 am
by LdB
necro on a necro ... 8 years and counting