Tutorial: How to use RTC modules (DS3231) with Red Pitaya

Applications, development tools, FPGA, C, WEB
Post Reply
radiationman
Posts: 9
Joined: Tue Mar 16, 2021 4:53 pm

Tutorial: How to use RTC modules (DS3231) with Red Pitaya

Post by radiationman » Wed Mar 17, 2021 2:28 am

The Red Pitaya does not come with a real time clock; it sets its time via connection to the internet. After many months of browsing this forum, and learning some basic bash scripting, I've found a way to set the Red Pitaya's system time with a normal RTC module such as the DS3231. I am a complete noob with programming in general, and I know this is not the most efficient or sexy way of keeping time on the Red Pitaya. However, I hope that this post will help other novices like me when it comes to implementing such a crucial aspect of data acquisition: keeping time.

Here is the RTC I'm using, though this is bound to work for other models.
https://www.amazon.com/WayinTop-DS3231- ... 628&sr=8-5

1. Connect the Vcc, GND, SDA, and SCL pins from the module to the respective GPIO pins on the Red Pitaya. Power on and connect to the Red Pitaya via SSH.

3. Determine if the RTC is connected, and the Red Pitaya can see it.

Code: Select all

$ i2cdetect -y -r 0
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: UU -- -- -- -- -- -- 57 -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
The output from i2cdetect should show the 0x68 address occupied by the RTC. If you see "UU" instead of "68," then you shouldn't be reading this tutorial for guidance.

4. Set the time of the RTC module to Tues March 16, 2021 08:05:00 UTC or something. According to the data sheet for the DS3231, registers 0x00-0x06 hold the seconds, minutes, hours, etc.
https://datasheets.maximintegrated.com/en/ds/DS3231.pdf Use i2cset to tell each register where it should be.

Code: Select all

$ i2cset -y 0 0x68 0x01 0    //start at 0 seconds
$ i2cset -y 0 0x68 0x02 5    // start at 5 minutes
....
5. Once you think you have the time set right (you can check an individual register using i2cget instead of i2cset), the RTC will keep the time until you change it again, or it drifts for some other reason.

We next need to set the Red Pitaya's system time from the RTC's time. For my application, I need the time to be accurate no matter the state of the Red Pitaya (losing power, resets, etc). So I use a bash script to set the Red Pitaya's time at boot. Create a timedate.sh file on the Red Pitaya, defining seconds, minutes, hours, day, month, and year. You can just copy and save this someone on your board. The name "timedate".sh doesn't matter.

Code: Select all

#!/bin/bash

# read in sec, min, etc from ds3231 and store in variable

sec=$(i2cget -y 0 0x68 0x00)
min=$(i2cget -y 0 0x68 0x01)
hour=$(i2cget -y 0 0x68 0x02)

day=$(i2cget -y 0 0x68 0x04)
month=$(i2cget -y 0 0x68 0x05)
year=$(i2cget -y 0 0x68 0x06)

# output of i2cget is of form 0x00, save on last two characters

sec=${sec: -2}
min=${min: -2}
hour=${hour: -2}

day=${day: -2}
month=${month: -2}
year=${year: -2}

# store all variables into strings to set time with

datee="20${year}${month}${day}"
timee="${hour}:${min}:${sec}"

# set system time 

date +%Y%m%d -s "$datee"
date +%T -s "$timee"
Please notice the use of the -2 operation. The output of i2cget will be of form 0x00. But we only want the last 2 digits of this output. The last line stores the output of i2c get as 2 digits corresponding to seconds.

Run this script from the command line using

Code: Select all

$ bash /path/to/script/timedate.sh
6. Automatically run the script at boot by creating a .service in systemd.

Code: Select all

[Unit]
Description=Time and date script

[Service]
ExecStart=/bin/bash /path/to/script/timedate.sh

Restart=always

[Install]
WantedBy=multi-user.target
Save this in your systemd directory

Code: Select all

/etc/systemd/system/myservice.service
Now you just have to enable the service. It will run the script at boot.

Code: Select all

$ systemctl enable myservice.service //service will start at next boot
$ systemctl disable myservice.service //will disable service on next boot

$ systemctl start myservice.service //starts service now, no reboot needed
$ systemctl stop myservice.service

There you have it. The Red Pitaya now does not rely on an internet connection to keep time. Hopefully this is helpful to the community until official RTC support comes out. ;)

radman

Post Reply
jadalnie klasyczne ekskluzywne meble wypoczynkowe do salonu ekskluzywne meble tapicerowane ekskluzywne meble do sypialni ekskluzywne meble włoskie

Who is online

Users browsing this forum: Google [Bot] and 14 guests