view pircharge/abb.c @ 343:917e05f03ad2

fluid-mnf/serial.c: <assert.h> no longer needed
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 13 Mar 2020 05:40:23 +0000
parents 84a4f6ef2d28
children
line wrap: on
line source

#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <rvinterf/pktmux.h>
#include <rvinterf/limits.h>
#include <rvinterf/localsock.h>
#include <rvinterf/localtypes.h>
#include <rvinterf/etm.h>
#include <rvinterf/exitcodes.h>

extern u_char rvi_msg[];
extern int rvi_msg_len;

extern int adccal_a, adccal_b;

struct abbtab {
	char	*name;
	int	regno;
	int	special;
} abbtab[] = {
	{"VBAT",    15,    2},
	{"ICHG",    17,    1},
	{"CHGREG",  25,    0},
	{"BCICTL1", 28,    0},
	{"BCICTL2", 29,    0},
	{"BCICONF", 32+13, 0},
	{0,         0,     0}
};

int abbr_in_progress, abbr_index;

void
issue_abbr()
{
	u_char cmdpkt[5];
	int i, c;

	cmdpkt[0] = RVT_TM_HEADER;
	cmdpkt[1] = ETM_CORE;
	cmdpkt[2] = TMCORE_OPC_CODEC_RD;
	cmdpkt[3] = abbtab[abbr_index].regno;
	c = 0;
	for (i = 1; i <= 3; i++)
		c ^= cmdpkt[i];
	cmdpkt[i] = c;
	send_pkt_to_target(cmdpkt, 5);
}

void
adc_end_process()
{
	if (abbr_in_progress)
		return;
	abbr_in_progress = 1;
	abbr_index = 0;
	issue_abbr();
}

void
etm_packet_rx()
{
	int i, c;
	unsigned val;

	if (rvi_msg_len != 9) {
		printf("Received TM packet of wrong length\n");
		return;
	}
	c = 0;
	for (i = 2; i < rvi_msg_len; i++)
		c ^= rvi_msg[i];
	if (c) {
		printf("Received TM packet with bad checksum\n");
		return;
	}
	if (rvi_msg[2] != ETM_CORE) {
		printf("Received TM packet that isn't ETM_CORE\n");
		return;
	}
	if (rvi_msg[4] != TMCORE_OPC_CODEC_RD) {
		printf("Received ETM_CORE packet that isn't abbr response\n");
		return;
	}
	if (rvi_msg[3]) {
		printf("abbr response has error code %02X\n", rvi_msg[3]);
		return;
	}
	if (!abbr_in_progress) {
		printf("abbr response with no abbr in progress\n");
		return;
	}
	if (rvi_msg[5] != abbtab[abbr_index].regno) {
		printf("abbr response for the wrong register\n");
		abbr_in_progress = 0;
		return;
	}
	val = rvi_msg[6] | rvi_msg[7] << 8;
	printf("%s=%03X", abbtab[abbr_index].name, val);
	switch (abbtab[abbr_index].special) {
	case 2:
		val = (val * adccal_a) >> 10;
		val += adccal_b;
		/* FALL THRU */
	case 1:
		printf(" (%u)", val);
	}
	putchar('\n');
	abbr_index++;
	if (abbtab[abbr_index].name)
		issue_abbr();
	else
		abbr_in_progress = 0;
}