🛠 Ajouter une RTC au CM4 (PCF8523 sur I2C1 via dt-blob)
Objectives
The CM4 module has neither a battery-backed RTC nor an embedded real-time clock (contrary to what one might think: the datasheet lists none — it is the carrier board that provides one, with the IO Board having an RTC connector with a battery holder). This tutorial adds a PCF8523 RTC on I2C1 using the dt-blob workflow from the Peripheral Software Guide — the official method for configuring pins on a custom carrier board from the earliest stages of boot — then wires the system time to the RTC.
Hardware
- 1 × CM4 on IO Board (or custom carrier board with PCF8523 RTC on I2C1)
- 1 × PCF8523 RTC module (3.3 V) + CR1220/CR2032 coin cell battery depending on the module
- Wiring to SDA1=GPIO44, SCL1=GPIO45 (on the IO Board's HAT footprint), 3.3 V and GND
sudo apt install -y device-tree-compiler i2c-tools
Complete code
# 1) Exposer I2C1 sur GPIO44/45 via dt-blob (méthode Peripheral Software Guide)
cat > minimal-cm-dt-blob.dts <<'EOF'
/dts-v1/; /plugin/;
/ {
pin-config {
pin@p44 { function = "i2c1"; termination = "pull_up"; }; // SDA1
pin@p45 { function = "i2c1"; termination = "pull_up"; }; // SCL1
};
};
EOF
sudo dtc -I dts -O dtb -o /boot/firmware/dt-blob.bin minimal-cm-dt-blob.dts
# 2) Activer le driver RTC et purger la fausse horloge
echo 'dtoverlay=i2c-rtc,pcf8523' | sudo tee -a /boot/firmware/config.txt
sudo apt remove -y fake-hwclock || true
sudo systemctl disable -q fake-hwclock.service 2>/dev/null || true
sudo reboot
# 3) Après reboot : vérifier bus, RTC et heure
sudo i2cdetect -y 1 # 68 = PCF8523 présent
sudo hwclock -s # RTC -> heure système
date && timedatectl | head -3
sudo hwclock -r -f /dev/rtc0 # lit l'heure de la RTC
Expected verification
i2cdetect -y 1 displays address 0x68; /dev/rtc0 exists; after a 5-minute power cut, date returns the correct time without NTP. dmesg | grep -i pcf8523 confirms the driver probe.
Common errors
fake-hwclockstill active: it rewrites the system time at boot with its fixed date and masks the proper functioning of the RTC.- Wrong bus: the PCF8523 on the IO Board is on I2C1 (GPIO44/45), not on I2C0/SDA0-GPIO0; forgetting the dt-blob or having
i2c_bcm2835not enabled leavesi2cdetectempty. - Misspelled overlay (
pcf8523and notds1307for a PCF8523): the driver does not probe the correct chip. - Missing battery or reversed contact: the time resets to 2000 on every cold boot.
Other tutorials for this board
- Premier démarrage d'un CM4 Lite depuis la microSD de l'IO Board
- GPIO du CM4 : comprendre les banks 1,8 V / 3,3 V avant de câbler
- Basculer le Wi-Fi du CM4 sur l'antenne externe U.FL (dtparam=ant2)
- Bootloader EEPROM du CM4 : maîtriser BOOT_ORDER via usbboot
- Son sur CM4 : DAC I2S/PCM (PCM5102) sans prise jack
- Horodatage industriel : PTP IEEE 1588 sur le PHY Ethernet du CM4
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash