├── INSTALL.sh ├── systemdscript ├── .gitignore ├── boards └── Allwinner-H3 │ ├── gpio-poweroff.dts │ └── README.txt ├── lifepo4wered-access.h ├── Dockerfile ├── initscript ├── Makefile ├── bindings ├── lifepo4wered.py └── lifepo4wered.js ├── lifepo4wered-access.c ├── lifepo4wered-data.h ├── lifepo4wered-cli.c ├── lifepo4wered-daemon.c ├── README.md ├── lifepo4wered-data.c └── LICENSE /INSTALL.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # Install script for compatibility with legacy instructions 3 | make user-install 4 | -------------------------------------------------------------------------------- /systemdscript: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=Daemon for LiFePO4wered power manager 3 | 4 | [Service] 5 | Type=notify 6 | ExecStart=DAEMON_DIRECTORY/lifepo4wered-daemon -f 7 | Restart=always 8 | RestartSec=10 9 | WatchdogSec=5 10 | 11 | [Install] 12 | WantedBy=sysinit.target 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | .deps 3 | *.pyc 4 | 5 | # Object files 6 | *.o 7 | *.ko 8 | *.obj 9 | *.elf 10 | 11 | # Precompiled Headers 12 | *.gch 13 | *.pch 14 | 15 | # Libraries 16 | *.lib 17 | *.a 18 | *.la 19 | *.lo 20 | 21 | # Shared objects (inc. Windows DLLs) 22 | *.dll 23 | *.so 24 | *.so.* 25 | *.dylib 26 | 27 | # Executables 28 | *.exe 29 | *.out 30 | *.app 31 | *.i*86 32 | *.x86_64 33 | *.hex 34 | -------------------------------------------------------------------------------- /boards/Allwinner-H3/gpio-poweroff.dts: -------------------------------------------------------------------------------- 1 | /dts-v1/; 2 | /plugin/; 3 | 4 | / { 5 | compatible = "allwinner,sun8i-h3"; 6 | 7 | fragment@0 { 8 | 9 | target = <&pio>; 10 | __overlay__ { 11 | poweroff_pins:poweroff_pins { 12 | allwinner,pins = "PA13"; 13 | allwinner,function = "gpio_out"; 14 | 15 | }; 16 | }; 17 | }; 18 | 19 | fragment@1 { 20 | 21 | target-path = "/"; 22 | __overlay__ { 23 | poweroff: poweroff { 24 | compatible = "gpio-poweroff"; 25 | gpios = <&pio 0 13 1>; 26 | }; 27 | }; 28 | }; 29 | }; 30 | -------------------------------------------------------------------------------- /lifepo4wered-access.h: -------------------------------------------------------------------------------- 1 | /* 2 | * LiFePO4wered/Pi access module 3 | * Copyright (C) 2015 Patrick Van Oosterwijck 4 | * Released under the GPL v2 5 | */ 6 | 7 | #ifndef LIFEPO4WERED_ACCESS_H 8 | #define LIFEPO4WERED_ACCESS_H 9 | 10 | #include 11 | #include 12 | 13 | 14 | /* Read LiFePO4wered/Pi data */ 15 | 16 | bool read_lifepo4wered_data(uint8_t reg, uint8_t count, uint8_t *data); 17 | 18 | /* Write LiFePO4wered/Pi chip data */ 19 | 20 | bool write_lifepo4wered_data(uint8_t reg, uint8_t count, uint8_t *data, 21 | bool unlock); 22 | 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # Build image 2 | FROM balenalib/raspberrypi4-64-alpine:3.12-build as build 3 | # Work in /tmp directory 4 | WORKDIR /tmp 5 | # Copy all files to the work directory 6 | COPY . ./ 7 | # Build the code 8 | RUN make all USE_SYSTEMD=0 USE_BALENA=1 PREFIX=/usr 9 | 10 | # Runtime image without build tools 11 | FROM balenalib/raspberrypi4-64-alpine:3.12-run 12 | # Install the files 13 | COPY --from=build /tmp/build/liblifepo4wered.so /usr/lib/liblifepo4wered.so 14 | COPY --from=build /tmp/build/lifepo4wered-cli /usr/bin/lifepo4wered-cli 15 | COPY --from=build /tmp/build/lifepo4wered-daemon /usr/sbin/lifepo4wered-daemon 16 | 17 | # Load the I2C device driver and run the deamon 18 | CMD lifepo4wered-daemon -f 19 | -------------------------------------------------------------------------------- /initscript: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | ### BEGIN INIT INFO 4 | # Provides: lifepo4wered-daemon 5 | # Required-Start: $remote_fs $syslog 6 | # Required-Stop: $remote_fs $syslog 7 | # Default-Start: 2 3 4 5 8 | # Default-Stop: 0 1 6 9 | # Short-Description: LiFePO4wered/Pi daemon 10 | # Description: LiFePO4wered/Pi system-side daemon 11 | ### END INIT INFO 12 | 13 | # Daemon name and location 14 | DIR=DAEMON_DIRECTORY 15 | DAEMON_NAME=lifepo4wered-daemon 16 | DAEMON=$DIR/$DAEMON_NAME 17 | 18 | # This next line determines what user the script runs as. 19 | # Root generally not recommended but necessary for system shutdown. 20 | DAEMON_USER=root 21 | 22 | . /lib/lsb/init-functions 23 | 24 | do_start () { 25 | log_daemon_msg "Starting system $DAEMON_NAME daemon" 26 | start-stop-daemon --start --user $DAEMON_USER --chuid $DAEMON_USER --exec $DAEMON --nicelevel 10 27 | log_end_msg $? 28 | } 29 | 30 | do_stop () { 31 | log_daemon_msg "Stopping system $DAEMON_NAME daemon" 32 | start-stop-daemon --stop --exec $DAEMON --retry 10 33 | log_end_msg $? 34 | } 35 | 36 | case "$1" in 37 | 38 | start|stop) 39 | do_${1} 40 | ;; 41 | 42 | restart|reload) 43 | do_stop 44 | do_start 45 | ;; 46 | 47 | status) 48 | status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $? 49 | ;; 50 | 51 | *) 52 | echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|status}" 53 | exit 1 54 | ;; 55 | 56 | esac 57 | exit 0 58 | -------------------------------------------------------------------------------- /boards/Allwinner-H3/README.txt: -------------------------------------------------------------------------------- 1 | NOTE: This was contributed by Mauricio José Flórez Moncayo of 2 | SMARTCHIP S.A.S. (www.techchip.com) and has not been tested by me. 3 | 4 | A common problem when using the the LiFePO4wered/Pi and related products with 5 | single board computers other than the Raspberry Pi is that most other boards 6 | do not bring down the TX line after the kernel is done shutting down. The 7 | LiFePO4wered/Pi uses the fact that the TX line goes low as an indication that 8 | the system has completed shutdown and it is safe to remove power. 9 | 10 | While current firmware (2018 and later) has a feature to cut power to the host 11 | a certain amount of time after shutdown was initiated (see PI_SHDN_TO), it 12 | is definitely more desirable to be able to detect shutdown and remove power 13 | based on that. 14 | 15 | The Linux kernel provides a feature to make this possible called 16 | "gpio-poweroff", and on most ARM based systems it can be enabled using a 17 | custom device tree overlay. 18 | 19 | Mauricio made such an overlay to enable gpio-poweroff on PA13 (hardware pin 8) 20 | of the Orange Pi Plus 2e, which will likely work on other Allwinner H3 based 21 | boards as well. It was only tested by him on that board though, and it may 22 | be necessary to change the pin definition on other H3 boards if they connect 23 | different chip pins to the GPIO header. 24 | 25 | This will only work with 4.1x and better kernels, not on legacy 3.x kernels. 26 | At the time of creation he tested it using the Armbian nightly with kernel 4.14 27 | and applied the custom overlay following the instructions found at this link: 28 | 29 | https://docs.armbian.com/Hardware_Allwinner_overlays/#using-custom-overlays 30 | 31 | One problem with enabling gpio-poweroff on the pin is that the user cannot use 32 | the UART on this pin at the same time, but that may not be a problem because 33 | H3 has 2 more UARTS on the 40 pins headers that can be activated at any time. 34 | 35 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | PREFIX ?= /usr/local 2 | CC ?= gcc 3 | LD ?= ld 4 | CFLAGS ?= -std=c99 -Wall -O2 5 | USE_SYSTEMD ?= 1 6 | USE_BALENA ?= 0 7 | OPTCFLAGS-10 = -DSYSTEMD 8 | OPTCFLAGS-01 = 9 | OPTCFLAGS-10 = -DSYSTEMD 10 | OPTCFLAGS-01 = -DBALENA 11 | OPTCFLAGS = $(OPTCFLAGS-$(USE_SYSTEMD)$(USE_BALENA)) 12 | OPTLDFLAGS-1 = -lsystemd 13 | OPTLDFLAGS-0 = 14 | OPTLDFLAGS = $(OPTLDFLAGS-$(USE_SYSTEMD)) 15 | 16 | all: build/lifepo4wered-cli build/lifepo4wered-daemon build/liblifepo4wered.so 17 | 18 | build/%.o: %.c 19 | @test -d build/ || mkdir -p build/ 20 | $(CC) -c $(OPTCFLAGS) $(CFLAGS) $< -o $@ 21 | build/liblifepo4wered.so: build/lifepo4wered-access.o build/lifepo4wered-data.o 22 | $(LD) -o $@ $^ -shared 23 | build/lifepo4wered-cli: build/lifepo4wered-access.o build/lifepo4wered-data.o build/lifepo4wered-cli.o 24 | $(CC) -o $@ $^ 25 | build/lifepo4wered-daemon: build/lifepo4wered-access.o build/lifepo4wered-data.o build/lifepo4wered-daemon.o 26 | $(CC) -o $@ $^ $(OPTLDFLAGS) 27 | help: 28 | @echo "Make goals:" 29 | @echo " all - build programs" 30 | @echo " install - install programs to $$DESTDIR$$PREFIX" 31 | @echo " clean - delete generated files" 32 | 33 | install-init-0: # sysvinit 34 | install -D -p initscript $(DESTDIR)/etc/init.d/lifepo4wered-daemon 35 | sed -i "s:DAEMON_DIRECTORY:$(PREFIX)/sbin:" $(DESTDIR)/etc/init.d/lifepo4wered-daemon 36 | install-init-1: install-init-0 # systemd and sysvinit 37 | install -D -p systemdscript $(DESTDIR)$(PREFIX)/lib/systemd/system/lifepo4wered-daemon.service 38 | sed -i "s:DAEMON_DIRECTORY:$(PREFIX)/sbin:" $(DESTDIR)$(PREFIX)/lib/systemd/system/lifepo4wered-daemon.service 39 | build/modules-load.conf: 40 | echo "i2c-dev" > build/modules-load.conf 41 | install-files: all build/modules-load.conf 42 | install -D -p build/liblifepo4wered.so $(DESTDIR)$(PREFIX)/lib/liblifepo4wered.so 43 | install -D -p build/lifepo4wered-cli $(DESTDIR)$(PREFIX)/bin/lifepo4wered-cli 44 | install -D -p build/lifepo4wered-daemon $(DESTDIR)$(PREFIX)/sbin/lifepo4wered-daemon 45 | install -D -p build/modules-load.conf $(DESTDIR)/lib/modules-load.d/lifepo4wered.conf 46 | 47 | install: install-files install-init-$(USE_SYSTEMD) 48 | 49 | enable-bus: 50 | ifneq (, $(shell command -v raspi-config 2> /dev/null)) 51 | raspi-config nonint do_i2c 0 52 | raspi-config nonint do_serial 0 53 | endif 54 | enable-init-0: # sysvinit 55 | update-rc.d lifepo4wered-daemon defaults 56 | service lifepo4wered-daemon restart 57 | enable-init-1: # systemd 58 | systemctl daemon-reload 59 | systemctl enable lifepo4wered-daemon.service 60 | systemctl restart lifepo4wered-daemon.service 61 | user-install: install enable-bus enable-init-$(USE_SYSTEMD) 62 | 63 | clean: 64 | rm -rf build 65 | -------------------------------------------------------------------------------- /bindings/lifepo4wered.py: -------------------------------------------------------------------------------- 1 | # LiFePO4wered access Python module 2 | # Copyright (c) 2017 Patrick Van Oosterwijck 3 | 4 | from ctypes import cdll 5 | 6 | 7 | # Variable definitions 8 | 9 | I2C_REG_VER = 0 10 | I2C_ADDRESS = 1 11 | LED_STATE = 2 12 | TOUCH_STATE = 3 13 | TOUCH_CAP_CYCLES = 4 14 | TOUCH_THRESHOLD = 5 15 | TOUCH_HYSTERESIS = 6 16 | DCO_RSEL = 7 17 | DCO_DCOMOD = 8 18 | VIN = 9 19 | VBAT = 10 20 | VOUT = 11 21 | IOUT = 12 22 | VBAT_MIN = 13 23 | VBAT_SHDN = 14 24 | VBAT_BOOT = 15 25 | VOUT_MAX = 16 26 | VIN_THRESHOLD = 17 27 | IOUT_SHDN_THRESHOLD = 18 28 | VOFFSET_ADC = 19 29 | VBAT_OFFSET = 20 30 | VOUT_OFFSET = 21 31 | VIN_OFFSET = 22 32 | IOUT_OFFSET = 23 33 | AUTO_BOOT = 24 34 | WAKE_TIME = 25 35 | SHDN_DELAY = 26 36 | AUTO_SHDN_TIME = 27 37 | PI_BOOT_TO = 28 38 | PI_SHDN_TO = 29 39 | RTC_TIME = 30 40 | RTC_WAKE_TIME = 31 41 | WATCHDOG_CFG = 32 42 | WATCHDOG_GRACE = 33 43 | WATCHDOG_TIMER = 34 44 | PI_RUNNING = 35 45 | CFG_WRITE = 36 46 | 47 | # Touch states and masks 48 | 49 | TOUCH_INACTIVE = 0x00 50 | TOUCH_START = 0x03 51 | TOUCH_STOP = 0x0C 52 | TOUCH_HELD = 0x0F 53 | TOUCH_ACTIVE_MASK = 0x03 54 | TOUCH_MASK = 0x0F 55 | 56 | # LED states when Pi on 57 | 58 | LED_STATE_OFF = 0x00 59 | LED_STATE_ON = 0x01 60 | LED_STATE_PULSING = 0x02 61 | LED_STATE_FLASHING = 0x03 62 | 63 | # Auto boot settings 64 | 65 | AUTO_BOOT_OFF = 0x00 66 | AUTO_BOOT_VBAT = 0x01 67 | AUTO_BOOT_VBAT_SMART = 0x02 68 | AUTO_BOOT_VIN = 0x03 69 | AUTO_BOOT_VIN_SMART = 0x04 70 | AUTO_BOOT_NO_VIN = 0x05 71 | AUTO_BOOT_NO_VIN_SMART = 0x06 72 | 73 | # Watchdog settings 74 | 75 | WATCHDOG_OFF = 0x00 76 | WATCHDOG_ALERT = 0x01 77 | WATCHDOG_SHDN = 0x02 78 | 79 | # Register access masks 80 | 81 | ACCESS_READ = 0x01 82 | ACCESS_WRITE = 0x02 83 | 84 | 85 | # Load shared object 86 | 87 | lib = cdll.LoadLibrary('/usr/local/lib/liblifepo4wered.so') 88 | 89 | # Determine if the specified variable can be accessed in the specified 90 | # manner (read, write or both) 91 | 92 | def access_lifepo4wered(var, access_mask): 93 | return lib.access_lifepo4wered(var, mask) 94 | 95 | # Read data from LiFePO4wered device 96 | 97 | def read_lifepo4wered(var): 98 | return lib.read_lifepo4wered(var) 99 | 100 | # Write data to LiFePO4wered device 101 | 102 | def write_lifepo4wered(var, value): 103 | return lib.write_lifepo4wered(var, value) 104 | 105 | -------------------------------------------------------------------------------- /bindings/lifepo4wered.js: -------------------------------------------------------------------------------- 1 | // LiFePO4wered access Node.js module 2 | // Copyright (c) 2018 Patrick Van Oosterwijck 3 | 4 | var ffi = require('ffi') 5 | 6 | // Load access functions from shared object 7 | 8 | var lib = ffi.Library('/usr/local/lib/liblifepo4wered.so', { 9 | 'access_lifepo4wered': [ 'int', [ 'int', 'int' ] ], 10 | 'read_lifepo4wered': [ 'int', [ 'int' ] ], 11 | 'write_lifepo4wered': [ 'int', [ 'int', 'int' ] ] 12 | }); 13 | 14 | // Export object 15 | 16 | module.exports = { 17 | 18 | // Variable definitions 19 | 20 | I2C_REG_VER : 0, 21 | I2C_ADDRESS : 1, 22 | LED_STATE : 2, 23 | TOUCH_STATE : 3, 24 | TOUCH_CAP_CYCLES : 4, 25 | TOUCH_THRESHOLD : 5, 26 | TOUCH_HYSTERESIS : 6, 27 | DCO_RSEL : 7, 28 | DCO_DCOMOD : 8, 29 | VIN : 9, 30 | VBAT : 10, 31 | VOUT : 11, 32 | IOUT : 12, 33 | VBAT_MIN : 13, 34 | VBAT_SHDN : 14, 35 | VBAT_BOOT : 15, 36 | VOUT_MAX : 16, 37 | VIN_THRESHOLD : 17, 38 | IOUT_SHDN_THRESHOLD : 18, 39 | VOFFSET_ADC : 19, 40 | VBAT_OFFSET : 20, 41 | VOUT_OFFSET : 21, 42 | VIN_OFFSET : 22, 43 | IOUT_OFFSET : 23, 44 | AUTO_BOOT : 24, 45 | WAKE_TIME : 25, 46 | SHDN_DELAY : 26, 47 | AUTO_SHDN_TIME : 27, 48 | PI_BOOT_TO : 28, 49 | PI_SHDN_TO : 29, 50 | RTC_TIME : 30, 51 | RTC_WAKE_TIME : 31, 52 | WATCHDOG_CFG : 32, 53 | WATCHDOG_GRACE : 33, 54 | WATCHDOG_TIMER : 34, 55 | PI_RUNNING : 35, 56 | CFG_WRITE : 36, 57 | 58 | // Touch states and masks 59 | 60 | TOUCH_INACTIVE : 0x00, 61 | TOUCH_START : 0x03, 62 | TOUCH_STOP : 0x0C, 63 | TOUCH_HELD : 0x0F, 64 | TOUCH_ACTIVE_MASK : 0x03, 65 | TOUCH_MASK : 0x0F, 66 | 67 | // LED states when Pi on 68 | 69 | LED_STATE_OFF : 0x00, 70 | LED_STATE_ON : 0x01, 71 | LED_STATE_PULSING : 0x02, 72 | LED_STATE_FLASHING : 0x03, 73 | 74 | // Auto boot settings 75 | 76 | AUTO_BOOT_OFF : 0x00, 77 | AUTO_BOOT_VBAT : 0x01, 78 | AUTO_BOOT_VBAT_SMART : 0x02, 79 | AUTO_BOOT_VIN : 0x03, 80 | AUTO_BOOT_VIN_SMART : 0x04, 81 | AUTO_BOOT_NO_VIN : 0x05, 82 | AUTO_BOOT_NO_VIN_SMART: 0x06, 83 | 84 | // Watchdog settings 85 | 86 | WATCHDOG_OFF : 0x00, 87 | WATCHDOG_ALERT : 0x01, 88 | WATCHDOG_SHDN : 0x02, 89 | 90 | // Register access masks 91 | 92 | ACCESS_READ : 0x01, 93 | ACCESS_WRITE : 0x02, 94 | 95 | // Export access functions 96 | 97 | access_lifepo4wered : lib.access_lifepo4wered, 98 | read_lifepo4wered : lib.read_lifepo4wered, 99 | write_lifepo4wered : lib.write_lifepo4wered 100 | 101 | }; 102 | 103 | -------------------------------------------------------------------------------- /lifepo4wered-access.c: -------------------------------------------------------------------------------- 1 | /* 2 | * LiFePO4wered/Pi access module 3 | * Copyright (C) 2015-2020 Patrick Van Oosterwijck 4 | * Released under the GPL v2 5 | */ 6 | 7 | #define _DEFAULT_SOURCE 8 | #include 9 | #include 10 | #include 11 | #include 12 | #ifndef I2C_FUNC_I2C 13 | #include 14 | #define TOBUFTYPE(x) (x) 15 | #else 16 | #define TOBUFTYPE(x) ((char *)(x)) 17 | #endif 18 | #include 19 | #include 20 | #include 21 | #include "lifepo4wered-access.h" 22 | 23 | 24 | /* LiFePO4wered/Pi access constants */ 25 | 26 | #define I2C_BUS 1 27 | #define I2C_ADDRESS 0x43 28 | #define I2C_WR_UNLOCK 0xC9 29 | 30 | 31 | /* Open access to the specified I2C bus */ 32 | 33 | static bool open_i2c_bus(int bus, int *file) { 34 | /* Create the name of the device file */ 35 | char filename[20]; 36 | snprintf(filename, 19, "/dev/i2c-%d", bus); 37 | /* Open the device file */ 38 | *file = open(filename, O_RDWR); 39 | if (*file < 0) return false; 40 | /* Lock access */ 41 | if (flock(*file, LOCK_EX|LOCK_NB) != 0) { 42 | close (*file); 43 | return false; 44 | } 45 | /* Success */ 46 | return true; 47 | } 48 | 49 | /* Close access to the specified I2C bus */ 50 | 51 | static bool close_i2c_bus(int file) { 52 | flock(file, LOCK_UN); 53 | close(file); 54 | return file >= 0; 55 | } 56 | 57 | /* Read LiFePO4wered/Pi data */ 58 | 59 | bool read_lifepo4wered_data(uint8_t reg, uint8_t count, uint8_t *data) { 60 | /* Open the I2C bus */ 61 | int file; 62 | if (!open_i2c_bus(I2C_BUS, &file)) 63 | return false; 64 | 65 | /* Declare I2C message structures */ 66 | struct i2c_msg dread[2]; 67 | struct i2c_rdwr_ioctl_data msgread = { 68 | dread, 69 | 2 70 | }; 71 | /* Write register message */ 72 | dread[0].addr = I2C_ADDRESS; 73 | dread[0].flags = 0; 74 | dread[0].len = 1; 75 | dread[0].buf = TOBUFTYPE(®); 76 | /* Read data message */ 77 | dread[1].addr = I2C_ADDRESS; 78 | dread[1].flags = I2C_M_RD; 79 | dread[1].len = count; 80 | dread[1].buf = TOBUFTYPE(data); 81 | 82 | /* Execute the command to send the register */ 83 | bool result = ioctl(file, I2C_RDWR, &msgread) >= 0; 84 | 85 | /* Close the I2C bus */ 86 | close_i2c_bus(file); 87 | 88 | /* Return the result */ 89 | return result; 90 | } 91 | 92 | /* Write LiFePO4wered/Pi chip data */ 93 | 94 | bool write_lifepo4wered_data(uint8_t reg, uint8_t count, uint8_t *data, 95 | bool unlock) { 96 | /* Open the I2C bus */ 97 | int file; 98 | if (!open_i2c_bus(I2C_BUS, &file)) 99 | return false; 100 | 101 | /* Declare I2C message structures */ 102 | struct i2c_msg dwrite; 103 | struct i2c_rdwr_ioctl_data msgwrite = { 104 | &dwrite, 105 | 1 106 | }; 107 | /* Message payload */ 108 | uint8_t payload[255]; 109 | uint8_t header_len = unlock ? 2 : 1; 110 | payload[0] = reg; 111 | payload[1] = (I2C_ADDRESS << 1) ^ I2C_WR_UNLOCK ^ reg; 112 | memcpy(&payload[header_len], data, count); 113 | /* Write data message */ 114 | dwrite.addr = I2C_ADDRESS; 115 | dwrite.flags = 0; 116 | dwrite.len = header_len + count; 117 | dwrite.buf = TOBUFTYPE(payload); 118 | 119 | /* Execute the command */ 120 | bool result = ioctl(file, I2C_RDWR, &msgwrite) >= 0; 121 | 122 | /* Close the I2C bus */ 123 | close_i2c_bus(file); 124 | 125 | /* Return the result */ 126 | return result; 127 | } 128 | -------------------------------------------------------------------------------- /lifepo4wered-data.h: -------------------------------------------------------------------------------- 1 | /* 2 | * LiFePO4wered/Pi data module 3 | * Copyright (C) 2015-2018 Patrick Van Oosterwijck 4 | * Released under the GPL v2 5 | */ 6 | 7 | #ifndef LIFEPO4WERED_DATA_H 8 | #define LIFEPO4WERED_DATA_H 9 | 10 | #include 11 | #include 12 | 13 | 14 | /* Generate enumeration and corresponding strings of available 15 | * LiFePO4wered/Pi variables */ 16 | 17 | #define FOREACH_LIFEPO4WERED_VAR(LIFEPO4WERED_VAR) \ 18 | LIFEPO4WERED_VAR(I2C_REG_VER) \ 19 | LIFEPO4WERED_VAR(I2C_ADDRESS) \ 20 | LIFEPO4WERED_VAR(LED_STATE) \ 21 | LIFEPO4WERED_VAR(TOUCH_STATE) \ 22 | LIFEPO4WERED_VAR(TOUCH_CAP_CYCLES) \ 23 | LIFEPO4WERED_VAR(TOUCH_THRESHOLD) \ 24 | LIFEPO4WERED_VAR(TOUCH_HYSTERESIS) \ 25 | LIFEPO4WERED_VAR(DCO_RSEL) \ 26 | LIFEPO4WERED_VAR(DCO_DCOMOD) \ 27 | LIFEPO4WERED_VAR(VIN) \ 28 | LIFEPO4WERED_VAR(VBAT) \ 29 | LIFEPO4WERED_VAR(VOUT) \ 30 | LIFEPO4WERED_VAR(IOUT) \ 31 | LIFEPO4WERED_VAR(VBAT_MIN) \ 32 | LIFEPO4WERED_VAR(VBAT_SHDN) \ 33 | LIFEPO4WERED_VAR(VBAT_BOOT) \ 34 | LIFEPO4WERED_VAR(VOUT_MAX) \ 35 | LIFEPO4WERED_VAR(VIN_THRESHOLD) \ 36 | LIFEPO4WERED_VAR(IOUT_SHDN_THRESHOLD) \ 37 | LIFEPO4WERED_VAR(VOFFSET_ADC) \ 38 | LIFEPO4WERED_VAR(VBAT_OFFSET) \ 39 | LIFEPO4WERED_VAR(VOUT_OFFSET) \ 40 | LIFEPO4WERED_VAR(VIN_OFFSET) \ 41 | LIFEPO4WERED_VAR(IOUT_OFFSET) \ 42 | LIFEPO4WERED_VAR(AUTO_BOOT) \ 43 | LIFEPO4WERED_VAR(WAKE_TIME) \ 44 | LIFEPO4WERED_VAR(SHDN_DELAY) \ 45 | LIFEPO4WERED_VAR(AUTO_SHDN_TIME) \ 46 | LIFEPO4WERED_VAR(PI_BOOT_TO) \ 47 | LIFEPO4WERED_VAR(PI_SHDN_TO) \ 48 | LIFEPO4WERED_VAR(RTC_TIME) \ 49 | LIFEPO4WERED_VAR(RTC_WAKE_TIME) \ 50 | LIFEPO4WERED_VAR(WATCHDOG_CFG) \ 51 | LIFEPO4WERED_VAR(WATCHDOG_GRACE) \ 52 | LIFEPO4WERED_VAR(WATCHDOG_TIMER) \ 53 | LIFEPO4WERED_VAR(PI_RUNNING) \ 54 | LIFEPO4WERED_VAR(CFG_WRITE) \ 55 | 56 | #define GENERATE_ENUM(ENUM) ENUM, 57 | 58 | enum eLiFePO4weredVar { 59 | FOREACH_LIFEPO4WERED_VAR(GENERATE_ENUM) 60 | LFP_VAR_COUNT, 61 | LFP_VAR_INVALID = LFP_VAR_COUNT 62 | }; 63 | 64 | extern const char *lifepo4wered_var_name[LFP_VAR_COUNT]; 65 | 66 | /* Touch states and masks */ 67 | 68 | #define TOUCH_INACTIVE 0x00 69 | #define TOUCH_START 0x03 70 | #define TOUCH_STOP 0x0C 71 | #define TOUCH_HELD 0x0F 72 | #define TOUCH_ACTIVE_MASK 0x03 73 | #define TOUCH_MASK 0x0F 74 | 75 | /* LED states when Pi on */ 76 | 77 | #define LED_STATE_OFF 0x00 78 | #define LED_STATE_ON 0x01 79 | #define LED_STATE_PULSING 0x02 80 | #define LED_STATE_FLASHING 0x03 81 | 82 | /* Auto boot settings */ 83 | 84 | #define AUTO_BOOT_OFF 0x00 85 | #define AUTO_BOOT_VBAT 0x01 86 | #define AUTO_BOOT_VBAT_SMART 0x02 87 | #define AUTO_BOOT_VIN 0x03 88 | #define AUTO_BOOT_VIN_SMART 0x04 89 | #define AUTO_BOOT_NO_VIN 0x05 90 | #define AUTO_BOOT_NO_VIN_SMART 0x06 91 | 92 | /* Watchdog settings */ 93 | 94 | #define WATCHDOG_OFF 0x00 95 | #define WATCHDOG_ALERT 0x01 96 | #define WATCHDOG_SHDN 0x02 97 | 98 | /* Register access masks */ 99 | 100 | #define ACCESS_READ 0x01 101 | #define ACCESS_WRITE 0x02 102 | 103 | 104 | /* Determine if the specified variable can be accessed in the specified 105 | * manner (read, write or both) */ 106 | 107 | bool access_lifepo4wered(enum eLiFePO4weredVar var, uint8_t access_mask); 108 | 109 | /* Read data from LiFePO4wered/Pi */ 110 | 111 | int32_t read_lifepo4wered(enum eLiFePO4weredVar); 112 | 113 | /* Write data to LiFePO4wered/Pi */ 114 | 115 | int32_t write_lifepo4wered(enum eLiFePO4weredVar, int32_t value); 116 | 117 | 118 | #endif 119 | -------------------------------------------------------------------------------- /lifepo4wered-cli.c: -------------------------------------------------------------------------------- 1 | /* 2 | * LiFePO4wered/Pi variable read/write command line tool 3 | * Copyright (C) 2015-2020 Patrick Van Oosterwijck 4 | * Released under the GPL v2 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include "lifepo4wered-data.h" 12 | 13 | 14 | /* Read or write operation */ 15 | 16 | enum eOperation { 17 | OP_INVALID, 18 | OP_READ, 19 | OP_WRITE 20 | }; 21 | 22 | /* Decimal or hexadecimal data */ 23 | 24 | enum eDataFormat { 25 | DF_INVALID, 26 | DF_DEC, 27 | DF_HEX, 28 | DF_DATA 29 | }; 30 | 31 | /* Variable not specified */ 32 | 33 | #define LFP_VAR_UNSPECIFIED (LFP_VAR_INVALID + 1) 34 | 35 | /* Print help */ 36 | 37 | void print_help(char *name, char *error, uint8_t access_mask) { 38 | if (error) { 39 | fprintf(stderr, "ERROR: %s\n\n", error); 40 | } 41 | if (access_mask & ACCESS_READ && access_mask & ACCESS_WRITE) { 42 | printf("Usage: %s [value]\n\n", name); 43 | printf("Available operations:\n"); 44 | printf("READ or GET: get variable and print it in decimal\n"); 45 | printf("READHEX, GETHEX or HEX: get variable and print it in hexadecimal\n"); 46 | printf("WRITE, SET or PUT: set the variable to the provided value\n\n"); 47 | printf("Available variables:\n"); 48 | } else if (access_mask & ACCESS_READ) { 49 | printf("Available variables for READ:\n"); 50 | } else if (access_mask & ACCESS_WRITE) { 51 | printf("Available variables for WRITE:\n"); 52 | } 53 | for (int i=0; i 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include "lifepo4wered-data.h" 20 | 21 | #ifdef SYSTEMD 22 | #include 23 | #endif 24 | 25 | /* Time difference (s) for the system time to be updated from the RTC */ 26 | 27 | #define RTC_SET_DIFF 10 28 | 29 | /* Delay (ns) between attempts to check for RTC second rollover */ 30 | 31 | #define RTC_CHECK_DELAY 50000000 32 | 33 | /* Running flag */ 34 | 35 | volatile sig_atomic_t running; 36 | 37 | /* Running in foreground flag */ 38 | bool foreground = false; 39 | 40 | #define log_info(args...) do { \ 41 | if (foreground) {\ 42 | fprintf(stdout, args); \ 43 | fprintf(stdout, "\n"); \ 44 | fflush(stdout); \ 45 | } else \ 46 | syslog(LOG_INFO, args); \ 47 | } while (0) 48 | 49 | /* TERM signal handler */ 50 | 51 | void term_handler(int signum) 52 | { 53 | running = 0; 54 | } 55 | 56 | /* Set up TERM signal handler */ 57 | 58 | void set_term_handler(void) { 59 | struct sigaction action; 60 | 61 | action.sa_handler = term_handler; 62 | sigemptyset (&action.sa_mask); 63 | action.sa_flags = 0; 64 | sigaction(SIGTERM, &action, NULL); 65 | } 66 | 67 | /* Shut down the system */ 68 | 69 | void shut_down(void) { 70 | log_info("Triggering system shutdown"); 71 | #ifdef BALENA 72 | char *params[4] = {"sh", "-c", "curl -X POST " \ 73 | "\"$BALENA_SUPERVISOR_ADDRESS/v1/shutdown?" \ 74 | "apikey=$BALENA_SUPERVISOR_API_KEY\"", NULL}; 75 | execv("/bin/sh", params); 76 | #elif defined SYSTEMD 77 | char *params[3] = {"systemctl", "poweroff", NULL}; 78 | execv("/bin/systemctl", params); 79 | #else 80 | char *params[3] = {"init", "0", NULL}; 81 | execv("/sbin/init", params); 82 | #endif 83 | } 84 | 85 | /* If the LiFePO4wered module has RTC functionality and the current 86 | * system time is off more than the limit of time difference, set 87 | * the system time from the RTC */ 88 | 89 | void system_time_from_rtc(void) { 90 | /* Make sure the connected LiFePO4wered module has RTC functionality */ 91 | if (!access_lifepo4wered(RTC_TIME, ACCESS_READ)) 92 | return; 93 | /* Is the time different enough? */ 94 | if (abs(read_lifepo4wered(RTC_TIME) - (int32_t)time(NULL)) >= RTC_SET_DIFF) { 95 | /* Wait until the RTC time changes */ 96 | struct timespec new_ts = {0}; 97 | struct timespec ts; 98 | ts.tv_sec = 0; 99 | ts.tv_nsec = RTC_CHECK_DELAY; 100 | int32_t start_time = read_lifepo4wered(RTC_TIME); 101 | do { 102 | nanosleep(&ts, NULL); 103 | new_ts.tv_sec = read_lifepo4wered(RTC_TIME); 104 | } while(new_ts.tv_sec == start_time); 105 | /* Set the system time to the RTC time */ 106 | clock_settime(CLOCK_REALTIME, &new_ts); 107 | /* Log message */ 108 | log_info("System time restored from RTC: %li", new_ts.tv_sec); 109 | } 110 | } 111 | 112 | /* If the LiFePO4wered module has RTC functionality, save the current 113 | * system time to the RTC */ 114 | 115 | void system_time_to_rtc(void) { 116 | /* Make sure the connected LiFePO4wered module has RTC functionality */ 117 | if (!access_lifepo4wered(RTC_TIME, ACCESS_WRITE)) 118 | return; 119 | /* Wait until the system time changes */ 120 | struct timespec ts; 121 | ts.tv_sec = 0; 122 | ts.tv_nsec = RTC_CHECK_DELAY; 123 | time_t now_time, start_time = time(NULL); 124 | do { 125 | nanosleep(&ts, NULL); 126 | now_time = time(NULL); 127 | } while(now_time == start_time); 128 | /* Save the system time to the RTC */ 129 | write_lifepo4wered(RTC_TIME, (int32_t)now_time); 130 | /* Log message */ 131 | log_info("System time saved to RTC: %d", (int32_t)now_time); 132 | } 133 | 134 | /* Main program */ 135 | 136 | int main(int argc, char *argv[]) { 137 | bool trigger_shutdown = false; 138 | 139 | #ifdef SYSTEMD 140 | sd_notify(0, "STATUS=Startup"); 141 | #endif 142 | /* Run in foreground if -f flag is passed */ 143 | if (argc == 2 && strcmp(argv[1], "-f") == 0) 144 | foreground = true; 145 | /* Otherwise fork and detach to run as daemon */ 146 | else if (daemon(0, 0)) 147 | return 1; 148 | 149 | /* Open the syslog if we need to */ 150 | if (!foreground) 151 | openlog("LiFePO4wered", LOG_PID|LOG_CONS, LOG_DAEMON); 152 | 153 | log_info("LiFePO4wered daemon started"); 154 | 155 | /* Set handler for TERM signal */ 156 | set_term_handler(); 157 | 158 | /* Set LiFePO4wered/Pi running flag */ 159 | write_lifepo4wered(PI_RUNNING, 1); 160 | running = 1; 161 | 162 | /* If available and necessary, restore the system time from the RTC */ 163 | system_time_from_rtc(); 164 | 165 | #ifdef SYSTEMD 166 | sd_notify(0, "READY=1"); 167 | sd_notify(0, "STATUS=Active"); 168 | #endif 169 | 170 | /* Sleep while the Pi is on, until this daemon gets a signal 171 | * to terminate (which might be because the LiFePO4wered/Pi 172 | * running flag is reset) */ 173 | while (running) { 174 | /* Start shutdown if the LiFePO4wered/Pi running flag is reset */ 175 | if (read_lifepo4wered(PI_RUNNING) == 0) { 176 | log_info("Signal from LiFePO4wered module to shut down"); 177 | trigger_shutdown = true; 178 | running = 0; 179 | } 180 | 181 | /* Sleep most of the time */ 182 | 183 | #ifdef SYSTEMD 184 | sd_notify(0, "WATCHDOG=1"); 185 | #endif 186 | sleep(1); 187 | } 188 | 189 | #ifdef SYSTEMD 190 | sd_notify(0, "STOPPING=1"); 191 | sd_notify(0, "STATUS=Shutdown"); 192 | #endif 193 | 194 | /* If available, save the system time to the RTC */ 195 | system_time_to_rtc(); 196 | 197 | /* Do we need to trigger system shutdown? 198 | * (The LiFePO4wered/Pi triggered it) */ 199 | if (trigger_shutdown) { 200 | /* Then trigger a system shutdown */ 201 | shut_down(); 202 | } else { 203 | /* Otherwise tell the LiFePO4wered/Pi we're shutting down */ 204 | write_lifepo4wered(PI_RUNNING, 0); 205 | log_info("Signaling LiFePO4wered module that system is shutting down"); 206 | } 207 | 208 | /* Close the syslog */ 209 | closelog(); 210 | 211 | return 1; 212 | } 213 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LiFePO4wered-Pi 2 | Access library, command line tool and daemon for the LiFePO4wered/Pi+ and legacy LiFePO4wered/Pi and LiFePO4wered/Pi3 modules 3 | 4 | ## Installation 5 | 6 | Starting from a fresh Raspbian image, first install the `build-essential`, 7 | `git` and the `systemd support library` packages: 8 | 9 | ``` 10 | sudo apt-get -y install build-essential git libsystemd-dev 11 | ``` 12 | 13 | In a directory where you keep source code, clone the LiFePO4wered-Pi repository 14 | from GitHub: 15 | 16 | ``` 17 | git clone https://github.com/xorbit/LiFePO4wered-Pi.git 18 | ``` 19 | 20 | Get in to the source code directory: 21 | 22 | ``` 23 | cd LiFePO4wered-Pi/ 24 | ``` 25 | 26 | Build the code: 27 | 28 | ``` 29 | make all 30 | ``` 31 | 32 | And install it: 33 | 34 | ``` 35 | sudo make user-install 36 | ``` 37 | 38 | That's it! You may need to restart for some configuration changes (such as enabling the I2C device) to take effect. 39 | 40 | ## Legacy build scripts 41 | 42 | The build and install commands were changed from what they originally were. 43 | To prevent confusion for people not reading this but following installation 44 | instructions in older manuals, `build.py` and `INSTALL.sh` are provided that 45 | just call the `make` commands above for legacy compatibility. 46 | 47 | ## Daemon 48 | 49 | The installation command installs a background program 50 | (lifepo4wered-daemon), along with scripts to start it. You can also start 51 | the daemon manually, it will by default run in the background, but you can force it to run in the foreground by adding the `-f` argument. 52 | 53 | The daemon supports startup via `systemd`, including its notification 54 | and keepalive features. See `man systemd.service` for details. 55 | 56 | If you do not want to include `systemd` support in the daemon, you can build 57 | the code with: 58 | 59 | ``` 60 | make all USE_SYSTEMD=0 61 | ``` 62 | 63 | ## CLI 64 | 65 | The `lifepo4wered-cli` tool provides convenient access to the LiFePO4wered 66 | device's I2C registers. Run it without parameters to get help information: 67 | 68 | ``` 69 | lifepo4wered-cli 70 | ``` 71 | 72 | To get a full dump of all register values, try: 73 | 74 | ``` 75 | lifepo4wered-cli get 76 | ``` 77 | 78 | To get the current battery voltage in millivolts, try: 79 | 80 | ``` 81 | lifepo4wered-cli get vbat 82 | ``` 83 | 84 | To set the wake up time to an hour, run: 85 | 86 | ``` 87 | lifepo4wered-cli set wake_time 60 88 | ``` 89 | 90 | To set the auto-boot flag to make the Pi run whenever there is power to do so, but still be able to turn the Pi off with the button or from software, run: 91 | 92 | ``` 93 | lifepo4wered-cli set auto_boot 2 94 | ``` 95 | 96 | To make this change permanent by saving it to flash, run: 97 | 98 | ``` 99 | lifepo4wered-cli set cfg_write 0x46 100 | ``` 101 | 102 | The `0x46` value is a magic key to allow config flash writes. 103 | 104 | Adjusting some of the register values can cause problems such as not being able 105 | to turn on the system using the touch button. To prevent permanently bricking 106 | the LiFePO4wered device, always test your changes thoroughly before writing them 107 | to flash. If you made a change that makes your LiFePO4wered device not work 108 | correctly, and it is not written to flash, you can undo it by unplugging the 109 | LiFePO4wered device and removing the LiFePO4 cell from the battery holder for 110 | a couple of minutes. The LiFePO4wered device should revert to its previous 111 | last saved state when you put the battery back. 112 | 113 | Check out the product brief for the 114 | [LiFePO4wered/Pi+](https://lifepo4wered.com/files/LiFePO4wered-Pi+-Product-Brief.pdf) or legacy [LiFePO4wered/Pi](http://lifepo4wered.com/files/LiFePO4wered-Pi-Product-Brief.pdf) or [LiFePO4wered/Pi3](http://lifepo4wered.com/files/LiFePO4wered-Pi3-Product-Brief.pdf) devices for a complete list of registers and valid values and options available in each product. Alternatively, running `lifepo4wered-cli get` returns a dump with all valid registers for the connected device. 115 | 116 | ## Permissions 117 | 118 | The user running the `lifepo4wered-cli` tool needs to have sufficient 119 | privileges to access the I2C bus. On Raspbian, the `pi` user by default can 120 | access the bus because it is in the `i2c` group. If you run as a different 121 | user, you either need to add this user to the `i2c` group or run the tool with 122 | `sudo`. On other distributions, a different group name may be used. You can 123 | check the owner and group of the I2C device with: 124 | 125 | ``` 126 | ls -l /dev/i2c-1 127 | ``` 128 | 129 | The command line tool returns the following negative values to indicate error 130 | conditions: 131 | 132 | | Return value | Condition | 133 | | -- | -- | 134 | | -1 | Could not access the LiFePO4wered device to perform the specified operation. Usually this condition is caused by insufficient privileges when trying to access the I2C bus. Trying to run the command as root or with `sudo` to fix the problem. When writing settings, this value is also returned if a register is not writable. | 135 | | -2 | The I2C bus could be accessed and the operation is valid, but communication with the LiFePO4wered device failed. After trying several times (20 by default), the I2C bus transaction could not be completed successfully. This happens if the LiFePO4wered device is not physically present or if something (possibly another HAT) is preventing the I2C bus from operating correctly. | 136 | 137 | ## Balena 138 | 139 | The included `Dockerfile` can be used to compile the daemon as a [Balena](https://www.balena.io/) compatible service. Typically this would be used in a multicontainer setup where the LiFePO4wered service would be separate from your application container(s). The `Dockerfile` uses the `USE_BALENA=1` `make` parameter to alter the shutdown command to send a shutdown request to the Balena supervisor container and runs the daemon code in foreground mode using the `-f` flag. 140 | 141 | The included `Dockerfile` uses a 64-bit Raspberry Pi 4 base image, if you are using a different Pi or 32-bit Balena base OS, you need to alter the base image in both ("build" and "run") `FROM` lines. 142 | 143 | A `docker-compose.yml` file such as the one below should be created to include the LiFePO4wered service in your application, make it privileged (to access the I2C bus and set system time), and give it access to the supervisor API: 144 | 145 | ```yaml 146 | version: '2' 147 | services: 148 | your-own-application-service: 149 | build: ./balena-node-hello-world 150 | ports: 151 | - "80:80" 152 | lifepo4wered-pi: 153 | build: ./LiFePO4wered-Pi 154 | privileged: true 155 | labels: 156 | io.balena.features.supervisor-api: '1' 157 | ``` 158 | 159 | If your application needs access to the LiFePO4wered device, the easiest approach may be to include the `lifepo4wered-cli` or `liblifepo4wered.so` with language bindings in your application's container, building it using the multistage approach demonstrated in our `Dockerfile`. Make sure you then also give your own application access to the I2C bus. 160 | 161 | -------------------------------------------------------------------------------- /lifepo4wered-data.c: -------------------------------------------------------------------------------- 1 | /* 2 | * LiFePO4wered/Pi data module 3 | * Copyright (C) 2015-2020 Patrick Van Oosterwijck 4 | * Released under the GPL v2 5 | */ 6 | 7 | #define _DEFAULT_SOURCE 8 | #include 9 | #include 10 | #include "lifepo4wered-data.h" 11 | #include "lifepo4wered-access.h" 12 | 13 | 14 | /* Number of I2C register versions defined */ 15 | 16 | #define I2C_REG_VER_COUNT 7 17 | 18 | /* Minimum I2C register version that requires write unlock */ 19 | 20 | #define I2C_WRUNLOCK_REG_VER 5 21 | 22 | /* Constant to use when a register is not availabled in a particular 23 | * register version */ 24 | 25 | #define R_NA 0xFF 26 | 27 | /* I2C access retries */ 28 | 29 | #define I2C_RETRIES 20 30 | 31 | /* I2C identical reads requirement */ 32 | 33 | #define I2C_IDENTICAL_READS 3 34 | 35 | /* I2C retry delay in us */ 36 | 37 | #define I2C_RETRY_DELAY 500 38 | 39 | /* Generate strings for variable names */ 40 | 41 | #define GENERATE_STRING(STRING) #STRING, 42 | 43 | const char *lifepo4wered_var_name[LFP_VAR_COUNT] = { 44 | FOREACH_LIFEPO4WERED_VAR(GENERATE_STRING) 45 | }; 46 | 47 | 48 | /* Structure to define variable scale factors */ 49 | 50 | struct sVarScale { 51 | int32_t mul; 52 | int32_t div; 53 | }; 54 | 55 | /* Structure to define variable read and write behavior */ 56 | 57 | struct sVarDef { 58 | uint8_t reg[I2C_REG_VER_COUNT]; 59 | uint8_t read_bytes; 60 | uint8_t write_bytes; 61 | uint8_t sign_extend; 62 | }; 63 | 64 | /* This table defines scaling for all I2C registers, for different 65 | * scaling variants */ 66 | 67 | static const struct sVarScale var_scale[LFP_VAR_COUNT][3] = { 68 | /* I2C_REG_VER */ { { 1, 1 }, { 1, 1 }, { 1, 1 } }, 69 | /* I2C_ADDRESS */ { { 1, 1 }, { 1, 1 }, { 1, 1 } }, 70 | /* LED_STATE */ { { 1, 1 }, { 1, 1 }, { 1, 1 } }, 71 | /* TOUCH_STATE */ { { 1, 1 }, { 1, 1 }, { 1, 1 } }, 72 | /* TOUCH_CAP_CYCLES */ { { 1, 1 }, { 1, 1 }, { 1, 1 } }, 73 | /* TOUCH_THRESHOLD */ { { 1, 1 }, { 1, 1 }, { 1, 1 } }, 74 | /* TOUCH_HYSTERESIS */ { { 1, 1 }, { 1, 1 }, { 1, 1 } }, 75 | /* DCO_RSEL */ { { 1, 1 }, { 1, 1 }, { 1, 1 } }, 76 | /* DCO_DCOMOD */ { { 1, 1 }, { 1, 1 }, { 1, 1 } }, 77 | /* VIN */ { { 966667, 102300 }, { 120833, 102300 }, { 317154, 102300 } }, 78 | /* VBAT */ { { 5000, 1023 }, { 625, 1023 }, { 625, 1023 } }, 79 | /* VOUT */ { { 554878, 102300 }, { 69360, 102300 }, { 65705, 102300 } }, 80 | /* IOUT */ { { 581395, 102300 }, { 72674, 102300 }, { 72674, 102300 } }, 81 | /* VBAT_MIN */ { { 5000, 1023 }, { 625, 1023 }, { 625, 1023 } }, 82 | /* VBAT_SHDN */ { { 5000, 1023 }, { 625, 1023 }, { 625, 1023 } }, 83 | /* VBAT_BOOT */ { { 5000, 1023 }, { 625, 1023 }, { 625, 1023 } }, 84 | /* VOUT_MAX */ { { 554878, 102300 }, { 69360, 102300 }, { 65705, 102300 } }, 85 | /* VIN_THRESHOLD */ { { 966667, 102300 }, { 120833, 102300 }, { 317154, 102300 } }, 86 | /* IOUT_SHDN_THRESH */ { { 581395, 102300 }, { 72674, 102300 }, { 72674, 102300 } }, 87 | /* VOFFSET_ADC */ { { 5000, 1023 }, { 5000, 1023 }, { 5000, 1023 } }, 88 | /* VBAT_OFFSET */ { { 5000, 1023 }, { 5000, 1023 }, { 5000, 1023 } }, 89 | /* VOUT_OFFSET */ { { 554878, 102300 }, { 554878, 102300 }, { 525641, 102300 } }, 90 | /* VIN_OFFSET */ { { 966667, 102300 }, { 966667, 102300 }, { 253723, 10230 } }, 91 | /* IOUT_OFFSET */ { { 581395, 102300 }, { 581395, 102300 }, { 581395, 102300 } }, 92 | /* AUTO_BOOT */ { { 1, 1 }, { 1, 1 }, { 1, 1 } }, 93 | /* WAKE_TIME */ { { 1, 1 }, { 1, 1 }, { 1, 1 } }, 94 | /* SHDN_DELAY */ { { 1, 1 }, { 1, 1 }, { 1, 1 } }, 95 | /* AUTO_SHDN_TIME */ { { 1, 1 }, { 1, 1 }, { 1, 1 } }, 96 | /* PI_BOOT_TO */ { { 10, 1 }, { 10, 1 }, { 10, 1 } }, 97 | /* PI_SHDN_TO */ { { 10, 1 }, { 10, 1 }, { 10, 1 } }, 98 | /* RTC_TIME */ { { 1, 1 }, { 1, 1 }, { 1, 1 } }, 99 | /* RTC_WAKE_TIME */ { { 1, 1 }, { 1, 1 }, { 1, 1 } }, 100 | /* WATCHDOG_CFG */ { { 1, 1 }, { 1, 1 }, { 1, 1 } }, 101 | /* WATCHDOG_GRACE */ { { 10, 1 }, { 10, 1 }, { 10, 1 } }, 102 | /* WATCHDOG_TIMER */ { { 10, 1 }, { 10, 1 }, { 10, 1 } }, 103 | /* PI_RUNNING */ { { 1, 1 }, { 1, 1 }, { 1, 1 } }, 104 | /* CFG_WRITE */ { { 1, 1 }, { 1, 1 }, { 1, 1 } }, 105 | }; 106 | 107 | /* This table covers I2C register scale variants used by each register 108 | * version */ 109 | 110 | static const uint8_t var_scale_variant[I2C_REG_VER_COUNT] = { 111 | 0, 0, 0, 0, 1, 1, 2 112 | }; 113 | 114 | /* This table covers definitions of all I2C registers */ 115 | 116 | static const struct sVarDef var_table[LFP_VAR_COUNT] = { 117 | /* I2C_REG_VER */ { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 1, 0, 0 }, 118 | /* I2C_ADDRESS */ { { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 }, 1, 1, 0 }, 119 | /* LED_STATE */ { { 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02 }, 1, 1, 0 }, 120 | /* TOUCH_STATE */ { { 0x19, 0x1B, 0x1D, 0x23, 0x22, 0x28, 0x3A }, 1, 0, 0 }, 121 | /* TOUCH_CAP_CYCLES */ { { 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03 }, 1, 1, 0 }, 122 | /* TOUCH_THRESHOLD */ { { 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04 }, 1, 1, 0 }, 123 | /* TOUCH_HYSTERESIS */ { { 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05 }, 1, 1, 0 }, 124 | /* DCO_RSEL */ { { 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06 }, 1, 1, 0 }, 125 | /* DCO_DCOMOD */ { { 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07 }, 1, 1, 0 }, 126 | /* VIN */ { { R_NA, R_NA, R_NA, 0x21, R_NA, 0x26, 0x36 }, 2, 0, 1 }, 127 | /* VBAT */ { { 0x15, 0x17, 0x19, 0x1D, 0x1E, 0x22, 0x32 }, 2, 0, 1 }, 128 | /* VOUT */ { { 0x17, 0x19, 0x1B, 0x1F, 0x20, 0x24, 0x34 }, 2, 0, 1 }, 129 | /* IOUT */ { { R_NA, R_NA, R_NA, R_NA, R_NA, R_NA, 0x38 }, 2, 0, 1 }, 130 | /* VBAT_MIN */ { { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, 2, 2, 1 }, 131 | /* VBAT_SHDN */ { { 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A }, 2, 2, 1 }, 132 | /* VBAT_BOOT */ { { 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C }, 2, 2, 1 }, 133 | /* VOUT_MAX */ { { 0x0E, 0x0E, 0x0E, 0x0E, 0x0E, 0x0E, 0x0E }, 2, 2, 1 }, 134 | /* VIN_THRESHOLD */ { { R_NA, R_NA, R_NA, 0x10, R_NA, 0x10, 0x10 }, 2, 2, 1 }, 135 | /* IOUT_SHDN_THRESH */ { { R_NA, R_NA, R_NA, R_NA, R_NA, R_NA, 0x1A }, 2, 2, 1 }, 136 | /* VOFFSET_ADC */ { { R_NA, R_NA, 0x10, 0x12, 0x10, 0x12, R_NA }, 2, 2, 1 }, 137 | /* VBAT_OFFSET */ { { R_NA, R_NA, R_NA, R_NA, R_NA, R_NA, 0x12 }, 2, 2, 1 }, 138 | /* VOUT_OFFSET */ { { R_NA, R_NA, R_NA, R_NA, R_NA, R_NA, 0x14 }, 2, 2, 1 }, 139 | /* VIN_OFFSET */ { { R_NA, R_NA, R_NA, R_NA, R_NA, R_NA, 0x16 }, 2, 2, 1 }, 140 | /* IOUT_OFFSET */ { { R_NA, R_NA, R_NA, R_NA, R_NA, R_NA, 0x18 }, 2, 2, 1 }, 141 | /* AUTO_BOOT */ { { 0x10, 0x12, 0x14, 0x18, 0x14, 0x18, 0x20 }, 1, 1, 0 }, 142 | /* WAKE_TIME */ { { 0x12, 0x14, 0x16, 0x1A, 0x1A, 0x1E, 0x26 }, 2, 2, 0 }, 143 | /* SHDN_DELAY */ { { R_NA, 0x10, 0x12, 0x14, 0x12, 0x14, 0x1C }, 2, 2, 0 }, 144 | /* AUTO_SHDN_TIME */ { { R_NA, R_NA, R_NA, 0x16, R_NA, 0x16, 0x1E }, 2, 2, 0 }, 145 | /* PI_BOOT_TO */ { { R_NA, R_NA, R_NA, R_NA, 0x15, 0x19, 0x21 }, 1, 1, 0 }, 146 | /* PI_SHDN_TO */ { { R_NA, R_NA, R_NA, R_NA, 0x16, 0x1A, 0x22 }, 1, 1, 0 }, 147 | /* RTC_TIME */ { { R_NA, R_NA, R_NA, R_NA, R_NA, R_NA, 0x28 }, 4, 4, 0 }, 148 | /* RTC_WAKE_TIME */ { { R_NA, R_NA, R_NA, R_NA, R_NA, R_NA, 0x2C }, 4, 4, 0 }, 149 | /* WATCHDOG_CFG */ { { R_NA, R_NA, R_NA, R_NA, 0x17, 0x1B, 0x23 }, 1, 1, 0 }, 150 | /* WATCHDOG_GRACE */ { { R_NA, R_NA, R_NA, R_NA, 0x18, 0x1C, 0x24 }, 1, 1, 0 }, 151 | /* WATCHDOG_TIMER */ { { R_NA, R_NA, R_NA, R_NA, 0x1C, 0x20, 0x30 }, 1, 1, 0 }, 152 | /* PI_RUNNING */ { { 0x14, 0x16, 0x18, 0x1C, 0x1D, 0x21, 0x31 }, 1, 1, 0 }, 153 | /* CFG_WRITE */ { { 0x11, 0x13, 0x15, 0x19, 0x19, 0x1D, 0x25 }, 1, 1, 0 }, 154 | }; 155 | 156 | /* I2C register version detected */ 157 | 158 | static int32_t i2c_reg_ver = 0; 159 | 160 | 161 | /* Determine if the specified variable can be accessed in the specified 162 | * manner (read, write or both) and return a pointer to the variable 163 | * definition (internal function) */ 164 | 165 | static bool can_access_lifepo4wered(enum eLiFePO4weredVar var, 166 | uint8_t access_mask, const struct sVarDef **vd) { 167 | /* Check if we have a I2C register version */ 168 | if (i2c_reg_ver <= 0) { 169 | /* If not, read it */ 170 | i2c_reg_ver = read_lifepo4wered(I2C_REG_VER); 171 | } 172 | /* Are the variable and I2C register version in defined range? */ 173 | if (var > I2C_REG_VER && var < LFP_VAR_COUNT && 174 | i2c_reg_ver > 0 && i2c_reg_ver <= I2C_REG_VER_COUNT) { 175 | /* Get a pointer to the variable definition */ 176 | const struct sVarDef *var_def = &var_table[var]; 177 | /* Save it to the provided pointer, if one is provided */ 178 | if (vd) { 179 | *vd = var_def; 180 | } 181 | /* Is this variable defined for the register version? */ 182 | if (var_def->reg[i2c_reg_ver - 1] != R_NA) { 183 | /* Then check the access */ 184 | return ((access_mask & ACCESS_READ) && var_def->read_bytes) || 185 | ((access_mask & ACCESS_WRITE) && var_def->write_bytes); 186 | } 187 | } 188 | /* Access not available */ 189 | return false; 190 | } 191 | 192 | /* Determine if the specified variable can be accessed in the specified 193 | * manner (read, write or both, external function) */ 194 | 195 | bool access_lifepo4wered(enum eLiFePO4weredVar var, uint8_t access_mask) { 196 | if (var == I2C_REG_VER && (access_mask & ACCESS_READ)) { 197 | return true; 198 | } else { 199 | return can_access_lifepo4wered(var, access_mask, NULL); 200 | } 201 | } 202 | 203 | /* Read data from LiFePO4wered/Pi 204 | * Because the MSP430G micro I2C peripheral relies heavily on software 205 | * support, it seems not possible to make reads work 100% reliable at 206 | * 100kHz, because other interrupts that are running may cause too much 207 | * latency and the first bit comes out wrong. We fix that by requiring 208 | * a number of identical reads. This also takes care of rejecting 209 | * multi-byte values that change in the middle of a read, so shadow 210 | * buffering reads on the micro may not be needed anymore. */ 211 | 212 | int32_t read_lifepo4wered(enum eLiFePO4weredVar var) { 213 | const struct sVarDef *var_def; 214 | if (var == I2C_REG_VER || 215 | can_access_lifepo4wered(var, ACCESS_READ, &var_def)) { 216 | uint8_t match_tries = 0; 217 | union { 218 | uint8_t b[4]; 219 | int16_t h[2]; 220 | int32_t i; 221 | } data, match_data; 222 | data.i = 0; 223 | match_data.i = 0; 224 | uint8_t reg = var == I2C_REG_VER ? 225 | I2C_REG_VER : var_def->reg[i2c_reg_ver - 1]; 226 | uint8_t read_bytes = var == I2C_REG_VER ? 1 : var_def->read_bytes; 227 | uint8_t sign_extend = var == I2C_REG_VER ? 0 : var_def->sign_extend; 228 | for (uint8_t retries = 0; retries < I2C_RETRIES; retries++) { 229 | usleep(500); 230 | if (read_lifepo4wered_data(reg, read_bytes, data.b)) { 231 | if (!match_tries || data.i == match_data.i) { 232 | if (match_tries >= I2C_IDENTICAL_READS - 1) { 233 | if (var == I2C_REG_VER) { 234 | return le32toh(data.i); 235 | } 236 | const struct sVarScale *scale = 237 | &var_scale[var][var_scale_variant[i2c_reg_ver - 1]]; 238 | if (sign_extend) { 239 | data.i = data.h[0]; 240 | } 241 | return (le32toh(data.i) * scale->mul + scale->div / 2) 242 | / scale->div; 243 | } 244 | match_tries++; 245 | } else { 246 | match_tries = 0; 247 | } 248 | match_data.i = data.i; 249 | } 250 | } 251 | return -2; 252 | } 253 | return -1; 254 | } 255 | 256 | /* Write data to LiFePO4wered/Pi */ 257 | 258 | int32_t write_lifepo4wered(enum eLiFePO4weredVar var, int32_t value) { 259 | const struct sVarDef *var_def; 260 | if (can_access_lifepo4wered(var, ACCESS_WRITE, &var_def) && i2c_reg_ver) { 261 | union { 262 | uint8_t b[4]; 263 | int32_t i; 264 | } data; 265 | const struct sVarScale *scale = 266 | &var_scale[var][var_scale_variant[i2c_reg_ver - 1]]; 267 | data.i = htole32((value * scale->div + scale->mul / 2) / scale->mul); 268 | for (uint8_t retries = 0; retries < I2C_RETRIES; retries++) { 269 | if (write_lifepo4wered_data(var_def->reg[i2c_reg_ver - 1], 270 | var_def->write_bytes, data.b, 271 | i2c_reg_ver >= I2C_WRUNLOCK_REG_VER)) { 272 | return read_lifepo4wered(var); 273 | } 274 | } 275 | return -2; 276 | } 277 | return -1; 278 | } 279 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | 341 | --------------------------------------------------------------------------------