TS-7250-V3 TS-BAT10: Difference between revisions

From embeddedTS Manuals
(Created page with "{{Infobox |title = TS-BAT10 |image = 300px |titlestyle = |headerstyle = background:#ccf; |labelstyle = width:33% |datastyle = |da...")
 
No edit summary
Line 11: Line 11:


The TS-BAT10 is a 2000mAH 5V battery backup.
The TS-BAT10 is a 2000mAH 5V battery backup.
Refer to the [[TS-BAT10]] manual for hardware documentation.
This example will charge the battery when it is low and has power, or shutdown when it does not have power and 3.3V is low.
<source lang=c lines>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <unistd.h>
#include "pc104.h"
#define TSBAT10_BASE 0x110
typedef enum
{
SW1_OPEN = 1 << 7,
OVER33V = 1 << 6,
VIN_OK = 1 << 5,
BATTERY_EN = 1 << 4,
BAT1CHARGING = 1 << 3,
BAT1CHARGED = 1 << 2,
BAT2CHARGING = 1 << 1,
BAT2CHARGED = 1 << 0
} StatusFlags;
typedef enum
{
RESERVED = 1 << 7,
BAT2SLOWCHARGE = 1 << 6,
BAT1SLOWCHARGE = 1 << 5,
BAT2CHARGEEN = 1 << 4,
BAT1CHARGEEN = 1 << 3,
BAT2TIMERDISABLE = 1 << 2,
BAT1TIMERDISABLE = 1 << 1,
SOFTDISABLE = 1 << 0
} ConfigurationFlags;
int main(int argc, char **argv)
{
uint8_t conf;
pc104_init();
conf = BAT1CHARGEEN | BAT2CHARGEEN;
while(1) {
uint8_t status;
status = pc104_io_8_read(TSBAT10_BASE);
if(status & BATTERY_EN) {
printf("Battery disabled by JP4\n");
goto SLEEP;
}
if(!(status & VIN_OK)) {
/* If we dont have 5v, and 3.3V is low, shut down*/
if(!(status & OVER33V)) {
printf("TS-BAT10 is low & has no VIN.  Shutting down!\n");
system("shutdown -h now");
} else {
printf("VIN not ok, but 3.3V not low yet\n");
}
} else {
if(!(status & OVER33V)) {
/* If battery is not > 3.3V, charge */
printf("Battery is low, VIN present.  Charging.\n");
pc104_io_8_write(TSBAT10_BASE, conf);
} else {
printf("VIN OK, but battery is charged\n");
}
}
SLEEP:
sleep(5);
}
return 0;
}
</source>
Compile and install this with:
<source lang=bash>
gcc tsbat10.c pc104.c -o tsbat10
cp tsbat10 /usr/local/bin/
</source>
Under systemd create a service file at /etc/systemd/system/tsbat10.service
<source lang=ini>
[Unit]
Description=TS-BAT10 daemon
[Service]
Type=simple
ExecStart=/usr/local/bin/tsbat10
[Install]
WantedBy=multi-user.target
</source>
Enable and start this service with:
<source lang=bash>
systemctl enable --now tsbat10.service
</source>

Revision as of 11:32, 19 March 2020

TS-BAT10
TS-BAT10.jpg
Product Page
8-bit IO

The TS-BAT10 is a 2000mAH 5V battery backup.

Refer to the TS-BAT10 manual for hardware documentation.

This example will charge the battery when it is low and has power, or shutdown when it does not have power and 3.3V is low.

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <unistd.h>

#include "pc104.h"

#define TSBAT10_BASE 0x110
 
typedef enum
{
	SW1_OPEN = 1 << 7,
	OVER33V = 1 << 6,
	VIN_OK = 1 << 5,
	BATTERY_EN = 1 << 4,
	BAT1CHARGING = 1 << 3,
	BAT1CHARGED = 1 << 2,
	BAT2CHARGING = 1 << 1,
	BAT2CHARGED = 1 << 0
} StatusFlags;
 
typedef enum
{
	RESERVED = 1 << 7,
	BAT2SLOWCHARGE = 1 << 6,
	BAT1SLOWCHARGE = 1 << 5,
	BAT2CHARGEEN = 1 << 4,
	BAT1CHARGEEN = 1 << 3,
	BAT2TIMERDISABLE = 1 << 2,
	BAT1TIMERDISABLE = 1 << 1,
	SOFTDISABLE = 1 << 0
} ConfigurationFlags;

int main(int argc, char **argv)
{
	uint8_t conf;
	pc104_init();

	conf = BAT1CHARGEEN | BAT2CHARGEEN;

	while(1) {
		uint8_t status;
		status = pc104_io_8_read(TSBAT10_BASE);

		if(status & BATTERY_EN) {
			printf("Battery disabled by JP4\n");
			goto SLEEP;
		}

		if(!(status & VIN_OK)) {
			/* If we dont have 5v, and 3.3V is low, shut down*/
			if(!(status & OVER33V)) {
				printf("TS-BAT10 is low & has no VIN.  Shutting down!\n");
				system("shutdown -h now");
			} else { 
				printf("VIN not ok, but 3.3V not low yet\n");
			}
		} else {
			if(!(status & OVER33V)) {
				/* If battery is not > 3.3V, charge */
				printf("Battery is low, VIN present.  Charging.\n");
				pc104_io_8_write(TSBAT10_BASE, conf);
			} else {
				printf("VIN OK, but battery is charged\n");
			}
		}
SLEEP:		
		sleep(5);
	}

	return 0;
}

Compile and install this with:

gcc tsbat10.c pc104.c -o tsbat10
cp tsbat10 /usr/local/bin/

Under systemd create a service file at /etc/systemd/system/tsbat10.service

[Unit]
Description=TS-BAT10 daemon

[Service]
Type=simple
ExecStart=/usr/local/bin/tsbat10

[Install]
WantedBy=multi-user.target

Enable and start this service with:

systemctl enable --now tsbat10.service