Skip to content

Instantly share code, notes, and snippets.

@TobleMiner
Created March 21, 2019 16:04
Show Gist options
  • Save TobleMiner/4b2cd838dce596f7da59088fff0857cb to your computer and use it in GitHub Desktop.
Save TobleMiner/4b2cd838dce596f7da59088fff0857cb to your computer and use it in GitHub Desktop.
Coulomb Counter I2C clock calibration
#!/usr/bin/env bash
set -e -o pipefail
BUS="1"
ADDRESS="0x42"
TIME=2
calibrate() {
local mode=$1
local time=$2
local hextime="$(printf '%016X' "$((time * 1000000000))")"
local reg=15
echo "$hextime" | fold -w 2 | while read byte; do
i2cset -y $BUS $ADDRESS $reg 0x$byte
reg=$((reg + 1))
done
# Set calibration register to calibrate $mode
i2cset -y $BUS $ADDRESS 14 $mode
sleep $time
# Set calibration register to finish calibration
i2cset -y $BUS $ADDRESS 14 0
sleep 1
# Check if calibration was successful (should be 0)
i2cget -y $BUS $ADDRESS 14
}
echo "Calibrating timer1 ..."
calibrate 1 10
echo "Calibrating WDT ..."
calibrate 2 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment