Page 1 of 1

Set system time using a .sh script and RTC.

Posted: Thu Oct 22, 2020 7:32 pm
by earlforeal
I was able to connect a DS3221 RTC to the red pitaya's I2C (SDA/SCL) and can use terminal commands like

Code: Select all

$i2cdetect
$i2cdump 
$i2cget
Using i2cdump for a specific address(es) will read the seconds, minutes, hours, date, year (00h-06h) from the RTC.

Code: Select all

$ i2cdump -y -r 0x0-6 0 0x68
     0  1  2  3  4  5  6    
00: 51 51 21 01 14 01 17

//to read seconds only
$ i2cdump -y -r 0x0 0 0x68
$ 0x51

//read hour only
$ i2cdump -y -r 0x2 0 0x68
$ 0x21
 
I would like to set the RP's system time using the RTC, and was wondering if using a myscript.sh would work? For example (pseudo code)

Code: Select all

#!/bin/bash

//create variable that is current RP system time and will be amended with correct RTC time
old = $(date)

// create string variable using RTC time
new = $(i2cdump -y -r 0x0-6 0 0x68)

//change 'old' to 'new'
date -s '$new'

I am new to programming in general and have tried my best to articulate the problem, and ideas or advice would be greatly appreciated.