Skip to content

Instantly share code, notes, and snippets.

@calebccff
Last active November 29, 2021 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calebccff/00b31cde1c438e3f02020b1fcd1f7945 to your computer and use it in GitHub Desktop.
Save calebccff/00b31cde1c438e3f02020b1fcd1f7945 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import argparse
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from scipy.stats import linregress
pmics = ["pmi8998", "pm660"]
fabs = ["gf", "tsmc", "smic", "mgna"]
combos = [("pmi8998", "gf"), ("pmi8998", "smic"), ("pm660", "gf"), ("pm660", "tsmc")]
def parse_args():
parser = argparse.ArgumentParser(description="Calculate coefficients for RRADC")
parser.add_argument("--pmic", help="which pmic", choices=pmics)
parser.add_argument("--fab", help="which fab", choices=fabs)
try:
args = parser.parse_args()
except argparse.ArgumentError:
parser.print_help()
return None
if (args.pmic == "pmi8998" and (args.fab == "tsmc" or args.fab == "mgna")) \
or (args.pmic == "pm660" and args.fab == "smic"):
print("fab and pmic combination not supported")
return None
return args
def get_coeffs(pmic, fab):
if pmic == "pmi8998":
if fab == "gf":
return (1303168, 3784)
if fab == "smic":
return (1338433, 3655)
elif pmic == "pm660":
if fab == "gf":
return (1309001, 3403)
if fab == "tsmc":
return (1314779, 3496)
max_reading = 1 << 10
div_factor = 5
raw_vals = [x*div_factor for x in range(0, int(max_reading/div_factor))]
def do_calc(pmic, fab):
(offset, slope) = get_coeffs(pmic, fab)
print("offset: %d" % offset)
print("slope: %d" % slope)
chg_temps = ([], [])
chg_temps_hot = ([], [])
#print("\nchg temp")
for val in raw_vals:
uv = val * 4
uv = uv * 5000000
uv = uv / (3 * max_reading)
uv = offset - uv;
uv = (uv * 1000) / slope
uv += 25000
chg_temps_hot[0].append(val)
chg_temps_hot[1].append(uv / 1000)
#print("%d, %d" % (val, uv / 1000))
#print("\nchg temp hot:")
for val in raw_vals:
uv = val * 5000000
uv = uv / (3 * max_reading)
uv = offset - uv;
uv = (uv * 1000) / slope + 25000
chg_temps[0].append(val)
chg_temps[1].append(uv / 1000)
#print("%d, %d" % (val, uv / 1000))
return chg_temps, chg_temps_hot
args = parse_args()
results = []
if not args.pmic or not args.fab:
for (pmic, fab) in combos:
if pmic != "pm660":
print("\npmic: %s, fab: %s" % (pmic, fab))
chg_temps, chg_temps_hot = do_calc(pmic, fab)
results.append({"pmic": pmic, "fab": fab, "chg_temps": chg_temps, "chg_temps_hot": chg_temps_hot})
else:
do_calc(args.pmic, args.fab)
fig, ax = plt.subplots()
for res in results:
label = "%s-%s" % (res["pmic"], res["fab"])
chg_temps = (res["chg_temps"][0], res["chg_temps"][1])
chg_temps_hot = (res["chg_temps_hot"][0], res["chg_temps_hot"][1])
ax.scatter(chg_temps[0], chg_temps[1], label="chg_temp: %s" % label)
ax.scatter(chg_temps_hot[0], chg_temps_hot[1], label="chg_temp_hot: %s" % label)
# print("\n%s" % label)
# print(linregress(chg_temps[0], chg_temps[1]))
plt.xlabel("raw value")
plt.ylabel("chg temp (C)")
# plt.plot([x[0] for x in results[0]["chg_temps"]], [x[1] for x in results[0]["chg_temps"]], label="chg temp")
# plt.plot([x[0] for x in results[0]["chg_temps_hot"]], [x[1] for x in results[0]["chg_temps_hot"]], label="chg temp hot")
plt.legend()
rect = patches.Rectangle((600, -35), 350, 155, linewidth=1, edgecolor='r', facecolor='none')
ax.add_patch(rect)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment