Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

SPI interface with RTC DS3234


  • Please log in to reply
23 replies to this topic

#1 Novice Alex

Novice Alex

    Advanced Member

  • Members
  • PipPipPip
  • 59 posts

Posted 14 July 2011 - 07:03 AM

Hi Guys,

Need helps on how to program and interface with this RTC DS3234 found in Sparkfun.

I have tried the connection wiring base on this reference link. The only things is that I don't connect the SQW pin on the breakout board. I suppose it is not use for RTC.

Also I have tried to start a new VB.net program to interface using the SPI but it always return 255 for each call.

Here is the datasheet for DS3234.

Please help.

#2 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 14 July 2011 - 08:01 AM

SQW stands for Square Wave. I myself used an I²C realtime clock module, details @ http://wiki.netduino...k-Breakout.ashx On that one the SQW pin isn't connected as well. From your DS3234 Breakout, which pins are connected to the netduino, and to which pin on the netduino? I think it should be: Gnd -> Netduino Gnd VCC -> Netduino +3.3V CLK -> Netduino Digital pin 13 MISO -> Netduino Digital pin 12 MOSI -> Netduino Digital pin 11 SS -> Netduino any GPIO pin (except for digital pin 4)
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#3 Novice Alex

Novice Alex

    Advanced Member

  • Members
  • PipPipPip
  • 59 posts

Posted 14 July 2011 - 08:28 AM

SQW stands for Square Wave.

I myself used an I²C realtime clock module, details @ http://wiki.netduino...k-Breakout.ashx
On that one the SQW pin isn't connected as well.

From your DS3234 Breakout, which pins are connected to the netduino, and to which pin on the netduino? I think it should be:

Gnd -> Netduino Gnd
VCC -> Netduino +3.3V
CLK -> Netduino Digital pin 13
MISO -> Netduino Digital pin 12
MOSI -> Netduino Digital pin 11
SS -> Netduino any GPIO pin (except for digital pin 4)


Yes, I am having the same connection. One thing I need to highlight is that I did not provide the battery during my testing.
I will download the sample code from your link and test it out.

(the code is for I2C, do you have any for SPI?)
Thanks.

#4 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 14 July 2011 - 08:37 AM

Yes, I am having the same connection. One thing I need to highlight is that I did not provide the battery during my testing.
I will download the sample code from your link and test it out.

(the code is for I2C, do you have any for SPI?)
Thanks.

My sample code can't be used for SPI, you have to figure that part out yourself :D I only have the I²C-module since it was cheaper.

I think the battery is still required; the battery maintains the clock input and the 3.3V helps shifting the output. When I only get 255's from SPI I always toggle the bools in the SPI configuration on a trial/error base until it works :rolleyes:
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#5 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 14 July 2011 - 08:46 AM

When I only get 255's from SPI I always toggle the bools in the SPI configuration on a trial/error base until it works

:o :o :o
*** HORROR!!! HORROR!!! HORROR!!! ***
Biggest fault of Netduino? It runs by electricity.

#6 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 14 July 2011 - 08:52 AM

:o :o :o
*** HORROR!!! HORROR!!! HORROR!!! ***

:D really I've done that, with success :) The chip select bool I can read from the datasheet (should be False for the DS3234), but the clock states, no clue how to read that to be honest. Still a newb in many things here ;)
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#7 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 14 July 2011 - 09:13 AM

Stefan, read my signature...
Biggest fault of Netduino? It runs by electricity.

#8 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 14 July 2011 - 09:17 AM

Stefan, read my signature...

It's so true :D
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#9 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 14 July 2011 - 11:30 AM

:o :o :o
*** HORROR!!! HORROR!!! HORROR!!! ***

One feels NetduBot could be in for a hard life - poor mite.

Regards,
Mark

To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#10 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 14 July 2011 - 11:35 AM

Mark, you're totally right! Knowing that from Stefan, I guess the brand new creature he's building could be named as "Catduino". That's 'cos the nine lives the robot will have! :lol: (Stefan, you *KNOW* I am kidding, true?...Don't be as my wife: touchy, touchy and touchy again...) Cheers
Biggest fault of Netduino? It runs by electricity.

#11 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 14 July 2011 - 11:54 AM

Mark, you're totally right!
Knowing that from Stefan, I guess the brand new creature he's building could be named as "Catduino". That's 'cos the nine lives the robot will have! :lol:

(Stefan, you *KNOW* I am kidding, true?...Don't be as my wife: touchy, touchy and touchy again...)
Cheers

Oh yea I know :D And I know trial/error is more or less a code design approach (and even then not the best), but I love it since it can be very educational! My Netdubot, if you looked closely, is built in modules, so if a module gets broken, it's easely replaceable. I know myself, some day that will be of much use :D


But this is getting very very offtopic :)
Alex, how's the DS3234 going?
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#12 Novice Alex

Novice Alex

    Advanced Member

  • Members
  • PipPipPip
  • 59 posts

Posted 14 July 2011 - 03:07 PM

No good. Still not able to get it working. Probably getting the battery tomorrow and see if it really the battery. But another thing is the address. Just wondering how to specify the start address for write and read for SPI programming.

#13 Novice Alex

Novice Alex

    Advanced Member

  • Members
  • PipPipPip
  • 59 posts

Posted 15 July 2011 - 03:24 PM

No good. Still not able to get it working.
Probably getting the battery tomorrow and see if it really the battery.

But another thing is the address. Just wondering how to specify the start address for write and read for SPI programming.


managed to come across this sample codes on using this RTC.
I will need to get the correct battery and test it out again.

#14 Rusko

Rusko

    New Member

  • Members
  • Pip
  • 9 posts

Posted 30 September 2011 - 07:23 PM

I'm facing the same problems... any news? Thanks!
--
Rusko

#15 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 01 October 2011 - 04:14 AM

I'm facing the same problems... any news?
Thanks!

Could you post the code?
Biggest fault of Netduino? It runs by electricity.

#16 Rusko

Rusko

    New Member

  • Members
  • Pip
  • 9 posts

Posted 01 October 2011 - 06:44 AM

Sure!

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using System.Text;

namespace RTC
{
	public class Program
	{
    	public static void Main()
    	{
        	string res;
        	byte[] WriteBuffer = new byte[2];
        	byte[] ReadBuffer = new byte[2];
        	int Retry = 0;

        	SPI.Configuration RTC1 = new SPI.Configuration(
            	Pins.GPIO_PIN_D10,
            	false,
            	0,
            	0,
            	true,
            	true,
            	1000,
            	SPI_Devices.SPI1);

        	SPI SPIbus = new SPI(RTC1);
        	SPIbus.Config = RTC1;

        	// Init Config: 60 = disable Oscillator and Battery SQ wave @1hz, temp compensation, Alarms disabled
        	WriteAndCheck(SPIbus, 0x0E, 0x60);

        	// Write Today Date & Time
        	WriteAndCheck(SPIbus, 0x06, 0x11); // 2011 (year)
        	WriteAndCheck(SPIbus, 0x05, 0x10); //   10 (month:October)
        	WriteAndCheck(SPIbus, 0x04, 0x01); //	1 (day)
        	WriteAndCheck(SPIbus, 0x03, 0x06); //	6 (Saturday ?)
        	WriteAndCheck(SPIbus, 0x02, 0x08); //	8 (hours)
        	WriteAndCheck(SPIbus, 0x01, 0x24); //   24 (minutes)
        	WriteAndCheck(SPIbus, 0x00, 0x00); //   00 (seconds) 

        	do
        	{
            	res = "";
            	Retry = -7; // not counting the first read :-)
            	for (byte d = 0; d < 7; d++)
            	{
                	// read addresses 0x00 to 0x06 until I get someting good (< 0xF0)
                	do
                	{
                    	WriteBuffer[0] = d;
                    	WriteBuffer[1] = 0;
                    	SPIbus.WriteRead(WriteBuffer, ReadBuffer);
                    	Thread.Sleep(10);
                    	Retry++;
                	} while (ReadBuffer[1] > 0xF0);

                	res = ReadBuffer[1].ToString("x") + ":" + res; 
            	}
            	// Dump
            	Debug.Print("20" + res + " - Retry = " + Retry.ToString());
            	Thread.Sleep(500);
        	} while (true);
    	}


    	public static void WriteAndCheck(SPI device, byte Addr, byte Val)
    	{
        	byte[] writeBuffer = new byte[2];
        	byte[] readBuffer = new byte[2];

        	// Write at the given address until I read back the same value
        	do
        	{
            	writeBuffer[0] = (byte)(Addr + 0x80);
            	writeBuffer[1] = Val;
            	device.Write(writeBuffer);
            	Thread.Sleep(10);

            	writeBuffer[0] = Addr;
            	writeBuffer[1] = 0;
            	device.WriteRead(writeBuffer, readBuffer);
            	Thread.Sleep(10);
        	} while (readBuffer[1] != Val);
    	}
	}
}

Now it's "almost" working... sometimes it took many retry to correctly read the date, sometimes not... why?!?! Many thanks!

EDIT: I forgot to say I'm using FW 4.2 RC1...
--
Rusko

#17 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 01 October 2011 - 07:54 AM

Unless there is some issue on the 4.2 firmware, your code looks okay...and it does not make any sense adding sleeps somewhere or even retrying the same operation many times... I assume you do not have a scope, because it would be really useful in this situation. Did you tried to play with the SPI flags (edge/idle)? I would also make an attempt to set the SPI clock as low as possible. Anyway, the read/write *MUST* work correctly without sleeps and/or retries. The SPI driver is already slow by itself: adding new delays would be a total nightmare. Let me know. Cheers
Biggest fault of Netduino? It runs by electricity.

#18 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 01 October 2011 - 09:09 AM

        public static void WriteAndCheck(SPI device, byte Addr, byte Val)
        {
            byte[] writeBuffer = new byte[2];
            byte[] readBuffer = new byte[2];

            // Write at the given address until I read back the same value
            do
            {
                writeBuffer[0] = (byte)(Addr + 0x80);
                writeBuffer[1] = Val;
                device.Write(writeBuffer);
                Thread.Sleep(10);

                writeBuffer[0] = Addr;
                writeBuffer[1] = 0;
                device.WriteRead(writeBuffer, readBuffer);
                Thread.Sleep(10);
            } while (readBuffer[1] != Val);
        }
}

Sorry for jumping in here - just interested in why you are writing out two bytes to the SPI interface when you read the contents of a register? Should the buffers be 1 byte long at this point? That's the way I interpret the timing diagram at the bottom of page 18 of the data sheet.

Regards,
Mark

To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#19 Rusko

Rusko

    New Member

  • Members
  • Pip
  • 9 posts

Posted 01 October 2011 - 09:30 AM

I have found the problem... bad contact on battery holder! Now is working without delay and retry! Many thanks to everybody!

Sorry for jumping in here - just interested in why you are writing out two bytes to the SPI interface when you read the contents of a register? Should the buffers be 1 byte long at this point? That's the way I interpret the timing diagram at the bottom of page 18 of the data sheet.

AFAIK you cannot read without a write. I should write one byte (the address) and read one byte (a value)... I do both operations in one single shot: write address (and dummy read) and read value (with dummy write). Hope this is clear (and correct!).

BTW I'm writing a class to get/set a DateTime from the DS3234, I will post the code here as soon as I get something a little stable.
--
Rusko

#20 tlmiii01

tlmiii01

    Member

  • Members
  • PipPip
  • 12 posts

Posted 01 October 2011 - 03:03 PM

I have found the problem... bad contact on battery holder! Now is working without delay and retry! Many thanks to everybody!


AFAIK you cannot read without a write. I should write one byte (the address) and read one byte (a value)... I do both operations in one single shot: write address (and dummy read) and read value (with dummy write). Hope this is clear (and correct!).

BTW I'm writing a class to get/set a DateTime from the DS3234, I will post the code here as soon as I get something a little stable.


Take a look at this overloaded version of WriteRead...

public void WriteRead (
         byte[] writeBuffer,
         int writeOffset,
         int writeCount,
         byte[] readBuffer,
         int readOffset,
         int readCount,
         int startReadOffset
)

The key is startReadOffset, which allows you to tell the SPI device to wait until a number of bytes are written before starting to read. So, to read the time from DS3234, you could do this:

byte writeBuffer = new byte[] {0x00};
byte readBuffer[] = new byte[3];

device.WriteRead( writeBuffer, 0, 1, readBuffer 0, 3, 1 );

Hope this helps!

-Thomas




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.