# RTC selection
RTC="DS1307"
#RTC="DS3231"
# RTC common
DATE_FILE="/mnt/udisk/date"
I2C_ADDR="68"
RTC_REG="0x00"
RTC_BYTES="7"
# For DS1307
SRAM_REG="0x08"
SRAM_BYTES="7"
# For DS3231
STAMP_FILE="/etc/date_stamp"
I2C_GET="/usr/bin/i2cget -y 0 0x$I2C_ADDR"
I2C_SET="/usr/bin/i2cset -y 0 0x$I2C_ADDR"
I2C_DETECT="/usr/bin/i2cdetect -y 0 0x$I2C_ADDR 0x$I2C_ADDR"
DATE="/bin/date"
BOOT=1
# Read RTC and update date
read_rtc () {
[ "$RTC" == "DS1307" ] && STAMP="$($I2C_GET $SRAM_REG i $SRAM_BYTES)"
[ "$RTC" == "DS3231" ] && STAMP="$($DATE -r "$STAMP_FILE" +"0x%02S 0x%02M 0x%02H 0x%02u 0x%02d 0x%02m 0x%02y")"
$DATE -us "$($I2C_GET $RTC_REG i $RTC_BYTES | awk '{printf "20%02X%02X%02X%02X%02X.%02X", $7, $6, $5, $3, $2, $1}')" >/dev/null
[ $BOOT -eq 0 ] && (echo "Date daemon: RTC updated to $($DATE)" >/dev/kmsg)
[ $BOOT -eq 1 ] && BOOT=0 && (echo "Date daemon: RTC date is $($DATE)" >/dev/kmsg)
}
# Update RTC and STAMP area
write_rtc () {
$I2C_SET $RTC_REG $NEW i
[ "$RTC" == "DS1307" ] && $I2C_SET $SRAM_REG $NEW i
[ "$RTC" == "DS3231" ] && touch -r "$DATE_FILE" "$STAMP_FILE"
read_rtc
}
# Detect RTC
[ -z "$($I2C_DETECT | grep " $I2C_ADDR ")" ] && (echo "Date daemon: RTC not detected!" >/dev/kmsg) && (exit 1)
read_rtc
# Monitor "date" file, update if timestamp changed
while true; do
if [ -f "$DATE_FILE" ]; then
NEW="$($DATE -r "$DATE_FILE" +"0x%02S 0x%02M 0x%02H 0x%02u 0x%02d 0x%02m 0x%02y")"
[ "$NEW" != "$STAMP" ] && write_rtc
fi
sleep 5
done
#!/bin/sh
case "$1" in
start|restart|reload)
pidof date_daemon | xargs kill -9 2>/dev/null
/usr/bin/date_daemon &
;;
stop)
pidof date_daemon | xargs kill -9 2>/dev/null
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
[ 5642.284836] Date daemon: RTC date is Thu Apr 28 01:55:42 UTC 2022
(Inserted USB drive)
[ 5677.630590] Date daemon: RTC updated to Thu Apr 28 01:56:20 UTC 2022
System Normal start ...
Welcome to phoenix
[ 4.839277] Date daemon: RTC date is Thu Apr 28 02:01:35 UTC 2022
phoenix login:
...
Now uses the RTC SRAM to store the "date" file timestamp, instead the system flash.
...
Ouch, a DS3231? Saw DS1307 somewhere and spent quite a while working on the script
Luckly the date registers are the same, so the important part is the same.
git clone [url]https://github.com/Lichee-Pi/linux[/url] -b nano-5.2-flash
cd linux
touch .scmversion # Required to prevent build from appending a "+" sign to the kernel LOCALVERSION, or it'll make "licheepi-nano+"
export ARCH=arm CROSS_COMPILE=arm-linux-gnueabi-
make licheepi_nano_defconfig
make menuconfig
make -j8
Ouch, a DS3231? Saw DS1307 somewhere and spent quite a while working on the script
Luckly the date registers are the same, so the important part is the same.
Sure, using the eeprom is really easy to do, just use different addresses.
This example uses 0x50 (Internal EEPROM), but you could use any other, ex. from the RTC module EEPROM.
.....
Anyways, as you said, for the very few times it's going to be updated, the flash will do fine, or even better, as UBI filesystem provides wear leveling.
#!/bin/sh
# Variables
DBG=0
DATE_FILE="/mnt/udisk/date"
STAMP_FILE="/etc/date_stamp"
I2C_ADDR="68"
RTC_REG="0x00"
RTC_BYTES="7"
# SRAM_REG="0x08"
# SRAM_BYTES="6"
I2C_GET="/usr/bin/i2cget -y 0 0x$I2C_ADDR"
I2C_SET="/usr/bin/i2cset -y 0 0x$I2C_ADDR"
I2C_DETECT="/usr/bin/i2cdetect -y 0 0x$I2C_ADDR 0x$I2C_ADDR"
DATE="/bin/date"
# Detect RTC presence
detect_rtc () {
DETECTED="$($I2C_DETECT | grep " $I2C_ADDR ")"
if [ -z "$DETECTED" ]; then
echo "Date daemon: RTC not detected!" >/dev/kmsg
exit 1
else
echo "Date daemon: Found RTC at address 0x$I2C_ADDR" >/dev/kmsg
fi
}
# Read RTC SRAM area used for stamp.
#read_stamp () {
# STAMP="$($I2C_GET $SRAM_REG i $SRAM_BYTES)" #0xss 0xmm 0xhh 0xDD 0xMM 0xYY
# [ $DBG -eq 1 ] && echo "Date daemon: Read RTC stamp: $STAMP" >/dev/kmsg
#}
# Read file used for stamp.
read_stamp () {
STAMP=$(date -r "$STAMP_FILE" +"%s") #epoch
[ $DBG -eq 1 ] && echo "Date daemon: Read date_stamp date: $STAMP" >/dev/kmsg
}
# Write RTC SRAM area used for stamp
#write_stamp () {
# $I2C_SET $SRAM_REG $NEW_STAMP i
# [ $DBG -eq 1 ] && echo "Date daemon: Write RTC stamp: $NEW_STAMP" >/dev/kmsg
#}
# Write file used for stamp
write_stamp () {
touch -d @$NEW_STAMP "$STAMP_FILE"
[ $DBG -eq 1 ] && echo "Date daemon: Updated date_stamp with NEW_STAMP time" >/dev/kmsg
}
# Read RTC and update date; calls read_stamp
read_rtc () {
read_stamp
NOW="$($I2C_GET $RTC_REG i $RTC_BYTES)"
YYYY="$(printf "%04X" $(( 0x2000 + $(echo $NOW | awk '{print $7}') )))"
MM="$(printf "%02X" $(echo $NOW | awk '{print $6}'))"
DD="$(printf "%02X" $(echo $NOW | awk '{print $5}'))"
hh="$(printf "%02X" $(echo $NOW | awk '{print $3}'))"
mm="$(printf "%02X" $(echo $NOW | awk '{print $2}'))"
ss="$(printf "%02X" $(echo $NOW | awk '{print $1}'))"
$DATE -us "$YYYY$MM$DD$hh$mm.$ss" >/dev/null
echo "Date daemon: Date retrieved from RTC: $YYYY-$MM-$DD $hh:$mm:$ss" >/dev/kmsg
}
# Extract date and write to RTC; calls read_rtc which, in turn, calls read_stamp
write_rtc () {
#Extract fields
YY="$(echo $NEW_DATE | awk '{print $1}')"
MM="$(echo $NEW_DATE | awk '{print $2}')"
DD="$(echo $NEW_DATE | awk '{print $3}')"
#DAY="$(echo $NEW_DATE | awk '{print $4}')"
AA="$(echo $NEW_DATE | awk '{print $4}')"
hh="$(echo $NEW_DATE | awk '{print $5}')"
mm="$(echo $NEW_DATE | awk '{print $6}')"
ss="$(echo $NEW_DATE | awk '{print $7}')"
# As AA is obtained with %u (day of week NUMBER), this is no longer needed
#case "$DAY" in
# "Mon")
# AA="1"
# ;;
# "Tue")
# AA="2"
# ;;
# "Wed")
# AA="3"
# ;;
# "Thu")
# AA="4"
# ;;
# "Fri")
# AA="5"
# ;;
# "Sat")
# AA="6"
# ;;
# "Sun")
# AA="7"
# ;;
#esac
#Update RTC
$I2C_SET $RTC_REG 0x$ss 0x$mm 0x$hh 0x$AA 0x$DD 0x$MM 0x$YY i
write_stamp
read_rtc
echo "Date daemon: Updated RTC date: $DAY $YY-$MM-$DD $hh:$mm:$ss" >/dev/kmsg
}
# Script execution start
# Create date_stamp if missing
if [ ! -f "$STAMP_FILE" ]; then
touch "$STAMP_FILE"
fi
detect_rtc
read_rtc
# Monitor "date" file, update if timestamp changed
while true; do
if [ -f "$DATE_FILE" ]; then
# NEW_STAMP="$($DATE -r "$DATE_FILE" +"0x%S 0x%M 0x%H 0x%d 0x%m 0x%y")"
NEW_STAMP=$($DATE -r "$DATE_FILE" +"%s") #epoch
[ $DBG -eq 1 ] && echo "Date daemon: OLD STAMP: $STAMP" >/dev/kmsg
[ $DBG -eq 1 ] && echo "Date daemon: NEW STAMP: $NEW_STAMP" >/dev/kmsg
if [ "$NEW_STAMP" != "$STAMP" ]; then
# NEW_DATE="$($DATE -r "$DATE_FILE" +"%y %m %d %a %H %M %S")"
# Adding 20 secs to the date (time from PC to scope), Day of week with %u
NEW_DATE="$($DATE -d "@$((20 + $NEW_STAMP))" +"%y %m %d %u %H %M %S")"
write_rtc
fi
else
[ $DBG -eq 1 ] && echo "Date daemon: Date file \"$DATE_FILE\" not found" >/dev/kmsg
fi
sleep 5
done
#
# [ 2998.818192] usb 1-1: USB disconnect, device number 4
[ 2999.853749] FAT-fs (sda1): unable to read boot sector to mark fs as dirty
0:Got Signal with value umount
0:Got Signal with value umount
# [ 3011.622598] usb 1-1: new high-speed USB device number 5 using musb-hdrc
[ 3011.826582] usb-storage 1-1:1.0: USB Mass Storage device detected
[ 3011.885068] scsi host0: usb-storage 1-1:1.0
[ 3012.964435] scsi 0:0:0:0: Direct-Access Generic USB Flash Disk 7.76 PQ: 0 ANSI: 4
[ 3012.977692] sd 0:0:0:0: [sda] 15974400 512-byte logical blocks: (8.18 GB/7.62 GiB)
[ 3013.094479] sd 0:0:0:0: [sda] Write Protect is off
[ 3013.102202] sd 0:0:0:0: Attached scsi generic sg0 type 0
[ 3013.139709] sd 0:0:0:0: [sda] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
[ 3013.199958] scsi 0:0:0:1: Direct-Access Generic USB Flash Disk 7.76 PQ: 0 ANSI: 4
[ 3013.216202] sd 0:0:0:1: [sdb] Attached SCSI removable disk
[ 3013.344367] sd 0:0:0:1: Attached scsi generic sg1 type 0
[ 3013.466786] sda: sda1
[ 3013.535570] sd 0:0:0:0: [sda] Attached SCSI removable disk
[ 3014.402269] FAT-fs (sda1): Volume was not properly unmounted. Some data may be corrupt. Please run fsck.
0:Got Signal with value mount
SPI-NAND: W25N01GV is found size: 128MB.
*** Warning - bad CRC, using default environment
Welcome to phoenix
phoenix login: anolis_picture_width:21 picture!=NULL failed.
anolis_picture_width:21 picture!=NULL failed.
anolis_picture_width:21 picture!=NULL failed.
anolis_picture_width:21 picture!=NULL failed.
anolis_picture_width:21 picture!=NULL failed.
SPI-NAND: W25N01GV is found size: 128MB.
*** Warning - bad CRC, using default environment
Follow the platform-tools procedure described in the FAQ, then you can restore your backup.
I've opened a thread for that on the "official forum" (not very confident they take it into account, but nothing is lost for trying ).
....
I connected to the serial console, and have this messages with error:
...
U-Boot SPL 2018.01 (May 25 2021 - 08:44:03)
DRAM: 64 MiB
Trying to boot from sunxi SPI
U-Boot 2018.01 (May 25 2021 - 08:44:03 +0800) Allwinner Technology
CPU: Allwinner F Series (SUNIV)
Model: Lichee Pi Nano
DRAM: 64 MiB
show_dram_config:82ea5bc0, 82ea7ee8, 0, 82fc8000
SPI-NAND: W25N01GV is found size: 128MB.
*** Warning - bad CRC, using default environment
cfg_video_init:in:e59ff014
Setting up a 800x480 lcd console (overscan 0x0)
SPI-NAND: W25N01GV is found size: 128MB.
sunxi_lcdc_backlight_enable_user pwm 80
In: serial
Out: serial
Err: serial
Hit any key to stop autoboot: 0
SPI-NAND: W25N01GV is found size: 128MB.
SPI-NAND: 3145728 bytes @ 0xf00000 Read: OK
SPI-NAND: 20480 bytes @ 0xd00000 Read: OK
## Booting kernel from Legacy Image at 80500000 ...
Image Name: Linux-5.2.0-licheepi-nano
Image Type: ARM Linux Kernel Image (uncompressed)
Data Size: 3071416 Bytes = 2.9 MiB
Load Address: 80008000
Entry Point: 80008000
Verifying Checksum ... OK
## Flattened Device Tree blob at 80c00000
Booting using the fdt blob at 0x80c00000
Loading Kernel Image ... OK
Loading Device Tree to 816fa000, end 816ffee3 ... OK
add fb mem rsv ok, 83f44000, bc000
Starting kernel ...
Welcome to phoenix
phoenix login:
[ 3.966128] Date daemon: Found RTC at address 0x68
[ 4.586737] Date daemon: Date retrieved from RTC: 2022-04-25 10:03:26
anolis_picture_width:21 picture!=NULL failed.
anolis_picture_width:21 picture!=NULL failed.
anolis_picture_width:21 picture!=NULL failed.
anolis_picture_width:21 picture!=NULL failed.
anolis_picture_width:21 picture!=NULL failed.
util_thread_start_with_sched_priority:thread 0xa15a18 is running
[ 7.344970] spi_fpga_tn652: loading out-of-tree module taints kernel.
[ 8.719567] input: afg3050_kbd as /devices/platform/soc/1c27000.i2c/i2c-0/0-0037/input/input0 CNxxxxxxxxxx
[ 9.030971] Mass Storage Function, version: 2009/09/11
[ 9.036275] LUN: removable file: (no medium)
[ 9.093118] dual speed tmc: IN/ep1in, OUT/ep1out, INTep2in/
[ 9.099344] phy phy-1c13400.phy.0: Changing dr_mode to 1
=>
=>
=>
=> help
? - alias for 'help'
bootm - boot application image from memory
clrlogo - fill the boot logo area with black
env - environment handling commands
exit - exit script
false - do nothing, unsuccessfully
fdt - flattened device tree utility commands
gpio - query and control gpio pins
help - print command description/usage
printenv- print environment variables
reset - Perform RESET of the CPU
setenv - set environment variables
showvar - print local hushshell variables
spi_nand- SPI NAND flash sub-system
test - minimal test like /bin/sh
true - do nothing, successfully
version - print monitor, compiler and linker version
=>
=> spi_nand
spi_nand - SPI NAND flash sub-system
Usage:
spi_nand probe [[bus:]cs] [hz] [mode] - init flash device on given SPI bus
and chip select
spi_nand read addr offset len - read `len' bytes starting at
`offset' to memory at `addr', skipping bad blocks.
spi_nand write addr offset len - write `len' bytes from memory
at `addr' to flash at `offset', skipping bad blocks.
spi_nand erase[.spread] offset [+]len - erase `len' bytes from `offset'
`+len' round up `len' to block size
With '.spread', erase enough for given file size, otherwise,
'size' includes skipped bad blocks.
spi_nand update addr offset len - erase and write `len' bytes from memory
at `addr' to flash at `offset'
spi_nand bad - show bad blocks
spi_nand markbad offset - mark block include `offset' as bad block
=>
=> version
U-Boot 2018.01 (May 25 2021 - 08:44:03 +0800) Allwinner Technology
arm-linux-gcc.br_real (Buildroot 2017.02.5) 5.4.0
GNU ld (GNU Binutils) 2.26.1
=>
This indicates something weird with your data, shouldn't be there.Code: [Select]SPI-NAND: W25N01GV is found size: 128MB.
*** Warning - bad CRC, using default environment
Huh, you could also ask them for the kernel sources, otherwise they're violating the GPL.
...
Eventually, I will ask Hantek to consider open-sourcing the phoenix binary, so that this becomes a (partially) Open Source community project for an Open Source DSO. Personally I see this as a win-win situation, as Hantek would be able to dedicate their firmware engineers to other projects and the community would gain access to the first Open Source DSO.
....
See here.