Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions public/prism/drivers/A4401_BOND/A4401_BOND.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,38 @@ def bond_max_hdr_dac(self, hdr: int, pin: int, mv: int):
answer = self.rpc.call_method('bond_max_hdr_dac', hdr, port, mv)
return self._rpc_validate(answer)

def bond_max_hdr_gpo(self, hdr: int, pin: int, state: bool):
""" MAX11311 Header <1-4> write GPO Port <0-10> GPO state
- get port from pin, and also check mode

:return: {'success': True, 'method': 'bond_max_hdr_gpo',
'result': {'state': <bool> }
"""
success, port, error = self._bond_check_hdr_pin(hdr, pin, "GPO")
if not success:
return error

with self._lock:
self.logger.info(f"bond_max_hdr_gpo {hdr} {port} {state}")
answer = self.rpc.call_method('bond_max_hdr_gpo', hdr, port, state)
return self._rpc_validate(answer)

def bond_max_hdr_gpi(self, hdr: int, pin: int):
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is not working yet. It always returns False in my testing.

""" MAX11311 Header <1-4> read GPI Port <0-10>
- get port from pin, and also check mode

:return: {'success': True, 'method': 'bond_max_hdr_gpi',
'result': {'state': <bool>}
"""
success, port, error = self._bond_check_hdr_pin(hdr, pin, "GPI")
if not success:
return error

with self._lock:
self.logger.info(f"bond_max_hdr_gpi {hdr} {port}")
answer = self.rpc.call_method('bond_max_hdr_gpi', hdr, port)
return self._rpc_validate(answer)

# DEBUG APIs

def bond_debug_batt_emu(self):
Expand Down
64 changes: 64 additions & 0 deletions public/prism/drivers/A4401_BOND/A4401_BOND_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,45 @@ def parse_args():
required=True,
default=2500)

bond_max_hdr_gpo = subp.add_parser('bond_max_hdr_gpo',
description="Write Header Pin GPO")
bond_max_hdr_gpo.add_argument('--header',
dest="_hdr",
action='store',
type=int,
help='header, 1-4',
required=True,
default=1)
bond_max_hdr_gpo.add_argument('--pin',
dest="_pin",
action='store',
type=int,
help='pin, 1-20, but not all pins have function',
required=True,
default=1)
bond_max_hdr_gpo.add_argument('--state',
dest="_state",
choices=('1', '0'),
help='True|False',
required=True)

bond_max_hdr_gpi = subp.add_parser('bond_max_hdr_gpi',
description="Read Header Pin GPO")
bond_max_hdr_gpi.add_argument('--header',
dest="_hdr",
action='store',
type=int,
help='header, 1-4',
required=True,
default=1)
bond_max_hdr_gpi.add_argument('--pin',
dest="_pin",
action='store',
type=int,
help='pin, 1-20, but not all pins have function',
required=True,
default=1)

debug_batt_emu = subp.add_parser('debug_batt_emu',
description="BOND debug battery emulator")

Expand Down Expand Up @@ -431,6 +470,25 @@ def bond_max_hdr_dac(args):
return response["success"]


def bond_max_hdr_gpi(args):
logging.info("bond_max_hdr_gpi: {}".format(args))

response = teensy.bond_max_hdr_gpi(args._hdr, args._pin)
logging.info("{}".format(response))
return response["success"]


def bond_max_hdr_gpo(args):
logging.info("bond_max_hdr_gpo: {}".format(args))

_state = True
if args._state == '0': _state = False

response = teensy.bond_max_hdr_gpo(args._hdr, args._pin, _state)
logging.info("{}".format(response))
return response["success"]


def vbat_set(args):
logging.info("vbat_set: {}".format(args))

Expand Down Expand Up @@ -574,6 +632,12 @@ def sequence(args):
elif args._cmd == 'bond_max_hdr_dac':
success = bond_max_hdr_dac(args)

elif args._cmd == 'bond_max_hdr_gpi':
success = bond_max_hdr_gpi(args)

elif args._cmd == 'bond_max_hdr_gpo':
success = bond_max_hdr_gpo(args)

elif args._cmd == 'vbat_set':
success = vbat_set(args)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Sistemi Corporation, copyright, all rights reserved, 2023
* Martin Guthrie
*
*
*/
#pragma once

Expand All @@ -9,3 +9,17 @@ typedef struct {
uint16_t d;
int delay;
} _init_regs_t;

String bond_max_hdr_init(int hdr, // 1-4
int *adcs, int adc_len,
int *dacs, int dac_len,
int *gpos, int gpo_len,
int *gpis, int gpi_len,
int gpo_mv, int gpi_mv);
String bond_batt_emu_cal(void);
String bond_max_hdr_adc_cal(int hdr);
String bond_max_hdr_adc(int hdr, int port);
String bond_max_hdr_dac(int hdr, int port, int mv);
String bond_max_hdr_gpi(int hdr, int port);
String bond_max_hdr_gpo(int hdr, int port, bool state);
int init_max_hdr_bist(void);
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Sistemi Corporation, copyright, all rights reserved, 2023
* Martin Guthrie
*
*
*/
#include "bond_max_hdr.h"
#include "src/oled/bond_oled.h"
Expand Down Expand Up @@ -35,10 +35,10 @@ static _init_regs_t init_hdr_regs[] = {

uint8_t _get_max_cs_from_hdr(int hdr) {
switch (hdr) {
case 1: return SPI_CS_HRD1_Pin;
case 2: return SPI_CS_HDR2_Pin;
case 3: return SPI_CS_HDR3_Pin;
case 4: return SPI_CS_HDR4_Pin;
case 1: return SPI_CS_HRD1_Pin;
case 2: return SPI_CS_HDR2_Pin;
case 3: return SPI_CS_HDR3_Pin;
case 4: return SPI_CS_HDR4_Pin;
}
return 0;
}
Expand Down Expand Up @@ -75,7 +75,7 @@ MAX11300RegAddress_t _reg_port_config_port(int port) {
case 9: return port_cfg_p9;
case 10: return port_cfg_p10;
case 11: return port_cfg_p11;
}
}
return port_cfg_p0; // squelch build warning
}

Expand All @@ -93,8 +93,8 @@ MAX11300::MAX11300_Ports _get_port_from_int(int port) {
case 9: return MAX11300::PIXI_PORT9;
case 10: return MAX11300::PIXI_PORT10;
case 11: return MAX11300::PIXI_PORT11;
}
return MAX11300::PIXI_PORT0; // squelch build warning
}
return MAX11300::PIXI_PORT0; // squelch build warning
}

String bond_max_hdr_init(int hdr, // 1-4
Expand All @@ -104,7 +104,7 @@ String bond_max_hdr_init(int hdr, // 1-4
int *gpis, int gpi_len,
int gpo_mv, int gpi_mv) {
DynamicJsonDocument doc = _helper(__func__); // always first line of RPC API

MAX11300 *max = _get_max_from_hdr(hdr);
if (max == NULL) {
doc["result"]["error"] = "1 <= hdr <= 4, invalid parameter";
Expand Down Expand Up @@ -134,7 +134,7 @@ String bond_max_hdr_init(int hdr, // 1-4
d = 0x0666; // TODO: this comes from ??
for (int j = 0; j < dac_len; j++) {
init_regs[i].r = _reg_dac_data_port(dacs[j]); init_regs[i].d = d; i++;
}
}
init_regs[i].r = dac_preset_data_1; init_regs[i].d = 0x0; i++;
init_regs[i].r = dac_preset_data_2; init_regs[i].d = 0x0; i++;
// Set reg PORT_CONFIG -----------------------------------------------------
Expand All @@ -158,7 +158,7 @@ String bond_max_hdr_init(int hdr, // 1-4
d = 0x5100; // TODO: where this come from?
for (int j = 0; j < dac_len; j++) {
init_regs[i].r = _reg_port_config_port(dacs[j]); init_regs[i].d = d; i++;
}
}
// IRQ modes
init_regs[i].r = gpi_irqmode_P5_P0; init_regs[i].d = 0x0; i++;
init_regs[i].r = gpi_irqmode_P10_P6; init_regs[i].d = 0x0; i++;
Expand All @@ -167,7 +167,7 @@ String bond_max_hdr_init(int hdr, // 1-4
d = 0x7100; // TODO: where this come from?
for (int j = 0; j < adc_len; j++) {
init_regs[i].r = _reg_port_config_port(adcs[j]); init_regs[i].d = d; i++;
}
}
init_regs[i].r = device_control; init_regs[i].d = 0xc1 & 0x40ff; i++;
init_regs[i].r = device_control; init_regs[i].d = 0xc1; i++;
init_regs[i].r = interrupt_mask; init_regs[i].d = 0xffff; i++;
Expand All @@ -179,7 +179,7 @@ String bond_max_hdr_init(int hdr, // 1-4
doc["result"]["len_gpi"] = gpi_len;
doc["result"]["gpo_mv"] = gpo_mv;
doc["result"]["gpi_mv"] = gpi_mv;
doc["result"]["i"] = i; // !! check i < MAX_INIT_SETTINGS !!
doc["result"]["i"] = i; // !! check i < MAX_INIT_SETTINGS !!
return _response(doc);
}

Expand Down Expand Up @@ -211,7 +211,7 @@ String bond_batt_emu_cal(void) {

oled_print(OLED_LINE_RPC, __func__, !doc["success"]);
return _response(doc); // always the last line of RPC API
}
}

/* Read Header <1-4> ADC Port 11 cal voltage
* - all MAX11311's Port 11 is connected to 2500mV voltage reference
Expand All @@ -224,14 +224,14 @@ String bond_max_hdr_adc_cal(int hdr) {
doc["result"]["error"] = "1 <= hdr <= 4, invalid parameter";
doc["success"] = false;
return _response(doc);
}
}

uint16_t data = 0;
MAX11300::CmdResult result = max->single_ended_adc_read(MAX11300::PIXI_PORT11, &data);
if (result != MAX11300::Success) {
doc["result"]["error"] = "single_ended_adc_read error";
doc["success"] = false;
return _response(doc);
return _response(doc);
}

doc["result"]["mV"] = (data * 10000 + 2048) / 4096 ; // raw * 10000mV / 4096
Expand All @@ -251,14 +251,14 @@ String bond_max_hdr_adc(int hdr, int port) {
doc["result"]["error"] = "1 <= hdr <= 4, invalid parameter";
doc["success"] = false;
return _response(doc);
}
}

uint16_t data = 0;
MAX11300::CmdResult result = max->single_ended_adc_read(_get_port_from_int(port), &data);
if (result != MAX11300::Success) {
doc["result"]["error"] = "single_ended_adc_read error";
doc["success"] = false;
return _response(doc);
return _response(doc);
}

doc["result"]["mV"] = ((uint32_t)data * 10000 + 2048) / 4096; // raw * 10000mV / 4096
Expand All @@ -278,11 +278,11 @@ String bond_max_hdr_dac(int hdr, int port, int mv) {
doc["result"]["error"] = "1 <= hdr <= 4, invalid parameter";
doc["success"] = false;
return _response(doc);
}
}
if (mv < 0 || mv > 10000) {
doc["result"]["error"] = "0 <= mv <= 10000, invalid parameter";
doc["success"] = false;
return _response(doc);
return _response(doc);
}

float _data = mv * 4096 / 10000;
Expand All @@ -296,6 +296,53 @@ String bond_max_hdr_dac(int hdr, int port, int mv) {
return _response(doc); // always the last line of RPC API
}

String bond_max_hdr_gpo(int hdr, int port, bool state) {
DynamicJsonDocument doc = _helper(__func__); // always first line of RPC API

MAX11300 *max = _get_max_from_hdr(hdr);
if (max == NULL) {
doc["result"]["error"] = "1 <= hdr <= 4, invalid parameter";
doc["success"] = false;
return _response(doc);
}

MAX11300::CmdResult result = max->gpio_write(_get_port_from_int(port), (state ? 1 : 0));
if (result != MAX11300::Success) {
doc["result"]["error"] = "gpio_write error";
doc["success"] = false;
return _response(doc);
}

doc["result"]["state"] = state;

oled_print(OLED_LINE_RPC, __func__, !doc["success"]);
return _response(doc); // always the last line of RPC API
}

String bond_max_hdr_gpi(int hdr, int port) {
DynamicJsonDocument doc = _helper(__func__); // always first line of RPC API

MAX11300 *max = _get_max_from_hdr(hdr);
if (max == NULL) {
doc["result"]["error"] = "1 <= hdr <= 4, invalid parameter";
doc["success"] = false;
return _response(doc);
}

uint8_t state = 0;
MAX11300::CmdResult result = max->gpio_read(_get_port_from_int(port), state);
if (result != MAX11300::Success) {
doc["result"]["error"] = "gpio_read error";
doc["success"] = false;
return _response(doc);
}

doc["result"]["state"] = (state != 0x0);

oled_print(OLED_LINE_RPC, __func__, !doc["success"]);
return _response(doc); // always the last line of RPC API
}

int init_max_hdr_bist(void) {
/* Each MAX11311 on a header has port 11 mapped as an ADC which is
Connected to BOND's Vref (2.5V). On power up, setup() will init
Expand Down
Loading