Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin' into 49-cangen-sim
Browse files Browse the repository at this point in the history
tszwinglitw committed Oct 20, 2024
2 parents eac7335 + fe86adc commit 5cb9db1
Showing 10 changed files with 246 additions and 234 deletions.
12 changes: 6 additions & 6 deletions ftdi_flash.cfg
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
adapter driver ftdi
ftdi vid_pid 0x0403 0x6010
ftdi_vid_pid 0x0403 0x6010

# Initial state: Assuming 0x0008 doesn't interfere with your setup
# and 0x000b sets the required initial states for your other signals.
ftdi layout_init 0x0008 0x000b
ftdi_layout_init 0x0008 0x000b

ftdi layout_signal LED_Tx -data 0x00F0 -oe 0x00F0
ftdi layout_signal LED_Rx -data 0x00F0 -oe 0x00F0
ftdi_layout_signal LED_Tx -data 0x00F0 -oe 0x00F0
ftdi_layout_signal LED_Rx -data 0x00F0 -oe 0x00F0

# Configure GPIOL0 (ADBUS4) as nSRST, assuming active low reset
# Setting `-data` for active low, `-oe` to enable output.
# If tri-state isn't supported, this configures the pin as push-pull.
ftdi layout_signal nSRST -data 0x0010 -oe 0x0010
ftdi_layout_signal nSRST -data 0x0010 -oe 0x0010

transport select jtag
transport select jtag
19 changes: 13 additions & 6 deletions general/include/LTC4015.h
Original file line number Diff line number Diff line change
@@ -39,12 +39,19 @@
//Enable Coulomb Counter Low Alert.(0xOD Bit 13)
//Enable Coulomb Counter High Alert.(0xOD Bit 12)

typedef int (*Read_Ptr)(uint16_t DevAddress, uint16_t MemAddress,
uint16_t MemAddSize, uint8_t *pData, uint16_t Size);

typedef int (*Write_Ptr)(uint16_t DevAddress, uint16_t MemAddress,
uint16_t MemAddSize, uint8_t *pData, uint16_t Size);

typedef struct {
I2C_HandleTypeDef *i2cHandle;
uint16_t chargeFaults; //Stores error faults from CHGSTATE register
uint16_t qcount;
uint16_t limitAlerts;

Read_Ptr read;
Write_Ptr write;
} LTC4015_T;

/**
@@ -54,7 +61,7 @@ typedef struct {
* @param hi2c
* @return HAL_StatusTypeDef
*/
HAL_StatusTypeDef LTC4015_Init(LTC4015_T *dev, I2C_HandleTypeDef *i2cHandle);
int LTC4015_Init(LTC4015_T *dev, Read_Ptr read, Write_Ptr write);

/**
* @brief Reads from the LTC4015EUHF#PBF load switch
@@ -63,7 +70,7 @@ HAL_StatusTypeDef LTC4015_Init(LTC4015_T *dev, I2C_HandleTypeDef *i2cHandle);
* @param dev
* @param i2c_handle
*/
HAL_StatusTypeDef LTC4015_read(LTC4015_T *dev, uint16_t reg, uint16_t *data);
int LTC4015_read(LTC4015_T *dev, uint16_t reg, uint16_t *data);

/**
* @brief Writes to the LTC4015EUHF#PBF load switch
@@ -73,7 +80,7 @@ HAL_StatusTypeDef LTC4015_read(LTC4015_T *dev, uint16_t reg, uint16_t *data);
* @param i2c_handle
* @return HAL_StatusTypeDef
*/
HAL_StatusTypeDef LTC4015_write(LTC4015_T *dev, uint16_t reg, uint16_t data);
int LTC4015_write(LTC4015_T *dev, uint16_t reg, uint16_t data);

/**
* @brief Checks coulombs of charger to see if within a given range, more info on page 38-40
@@ -85,8 +92,8 @@ HAL_StatusTypeDef LTC4015_write(LTC4015_T *dev, uint16_t reg, uint16_t data);
* @param lowAlert
* @return HAL_StatusTypeDef, QCOUNT, Faulted
*/
HAL_StatusTypeDef LTC4015_Qcounter(LTC4015_T *dev, uint16_t prescaler,
uint16_t highAlert, uint16_t lowAlert);
int LTC4015_Qcounter(LTC4015_T *dev, uint16_t prescaler, uint16_t highAlert,
uint16_t lowAlert);

#ifndef MAX_NUM_LTC4015_INSTANCES
#define MAX_NUM_LTC4015_INSTANCES 1
56 changes: 27 additions & 29 deletions general/src/LTC4015.c
Original file line number Diff line number Diff line change
@@ -8,64 +8,62 @@

#include "LTC4015.h"

HAL_StatusTypeDef LTC4015_Init(LTC4015_T *dev, I2C_HandleTypeDef *i2cHandle)
int LTC4015_Init(LTC4015_T *dev, Read_Ptr read, Write_Ptr write)
{
dev->i2cHandle = i2cHandle;
dev->read = read;
dev->write = write;

// Gets the value from the Charging state register
LtC4015_read(dev, LTC4015_CHGSTATE, dev->CHGdata);
//Gets the value from the Charging state register
LtC4015_read(dev, LTC4015_CHGSTATE, &dev->chargeFaults);
}

HAL_StatusTypeDef LTC4015_read(LTC4015_T *dev, uint16_t reg, uint16_t *data){
return HAL_I2C_Mem_Read(dev -> i2c_handle, LTC4015_ADDR_68, reg,
I2C_MEMADD_SIZE_8BIT, data, 1, HAL_MAX_DELAY)

int LTC4015_read(LTC4015_T *dev, uint16_t reg, uint16_t *data)
{
return dev->read(LTC4015_ADDR_68, reg, 0x00000001U, data, 1);
}

HAL_StatusTypeDef LTC4015_write(LTC4015_T *dev, uint16_t reg, uint16_t data){
return HAL_I2C_Mem_Write(dev->i2c_handle, LTC4015_ADDR_68, reg,
I2C_MEMADD_SIZE_8BIT, data, 1, HAL_MAX_DELAY)
int LTC4015_write(LTC4015_T *dev, uint16_t reg, uint16_t data)
{
return dev->write(LTC4015_ADDR_68, reg, 0x00000001U, data, 1);
}

HAL_StatusTypeDef LTC4015_Qcounter(LTC4015_T *dev, uint16_t prescaler,
uint16_t highAlert, uint16_t lowAlert)
int LTC4015_Qcounter(LTC4015_T *dev, uint16_t prescaler, uint16_t highAlert,
uint16_t lowAlert)
{
// Increases integration time, at which QCOUNT is updated
//Increases integration time, at which QCOUNT is updated
LTC4015_write(dev, QCOUNT_PRESCALE_FACTOR, prescaler);

// Sets the High amount for the QCOUNT before error is flagged
//Sets the High amount for the QCOUNT before error is flagged
LTC4015_write(dev, QCOUNT_HI_ALERT_LIMIT, highAlert);
LTC4015_write(dev, EN_LIMIT_ALERTS,
0x1000); // Enable bit is in 12th bit postion
0x1000); //Enable bit is in 12th bit postion

// Sets the low amount for the QCOUNT before error is flagged
//Sets the low amount for the QCOUNT before error is flagged
LTC4015_write(dev, QCOUNT_LO_ALERT_LIMIT, lowAlert);
LTC4015_write(dev, EN_LIMIT_ALERTS,
0x2000); // Enable bit is in 13th bit postion
0x2000); //Enable bit is in 13th bit postion

LTC4015_write(dev, CONFIG_BITS,
0x0008); // enable QCOUNT, in bit postion 2
0x0008); //enable QCOUNT, in bit postion 2
LTC4015_read(dev, LIMIT_ALERTS, dev->limitAlerts);
LTC4015_read(dev, QCOUNT, dev->qcount);

// This all could be put into a while loop if you want to continually check
// for errors
//This all could be put into a while loop if you want to continually check for errors
LTC4015_write(dev, CONFIG_BITS,
(CONFIG_BITS ^
0x1000)); // should re-enable charging if was disabled
// Sees if the alerts are being flagged, and then will return the QCOUNT
0x1000)); //should re-enable charging if was disabled
//Sees if the alerts are being flagged, and then will return the QCOUNT
if (LIMIT_ALERTS | 0x1000 == 0x1000) {
LTC4015_write(
dev, EN_LIMIT_ALERTS,
(EN_LIMIT_ALERTS ^
0x1000)); // Should just reset the enable but touch nothing else
LTC4015_write(dev, CONFIG_BITS, 0x1000); // suspends charger
0x1000)); //Should just reset the enable but touch nothing else
LTC4015_write(dev, CONFIG_BITS, 0x1000); //suspends charger
return (QCOUNT,
highAlert) // Need away to tell its being flagged, but not
// really sure what to return
highAlert); //Need away to tell its being flagged, but not really sure what to return
} else if (LIMIT_ALERTS | 0x2000 == 0x2000) {
LTC4015_write(dev, EN_LIMIT_ALERTS, EN_LIMIT_ALERTS ^ 0x2000);
LTC4015_write(dev, CONFIG_BITS, 0x1000); // suspends charger
return (QCOUNT, lowAlert) // sames issue as above
LTC4015_write(dev, CONFIG_BITS, 0x1000); //suspends charger
return (QCOUNT, lowAlert); //sames issue as above
}
}
2 changes: 1 addition & 1 deletion ner_environment/build_system/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

from .build_system import main
from .build_system import app
from . import miniterm
329 changes: 161 additions & 168 deletions ner_environment/build_system/build_system.py

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions ner_environment/build_system/miniterm.py
Original file line number Diff line number Diff line change
@@ -42,17 +42,17 @@ def run_miniterm(device, baudrate=115200):
print(f"Failed to run pyserial-miniterm: {e}", file=sys.stderr)
sys.exit(1)

def main(args):
def main(ls=False, device=""):
# Detect the operating system and find USB devices
if not args.device:
if device == "":
# reverse so FTDI is the default if present, can always be overridden
devices = list(reversed(list_usb_devices()))

if not devices:
print("No USB devices found.", file=sys.stderr)
sys.exit(1)

if args.list:
if ls:
print("Devices present:")
for i, device in enumerate(devices):
print(device)
@@ -63,11 +63,11 @@ def main(args):
# Default to the first device if available
selected_device = devices[0]
else:
selected_device = args.device
selected_device = device
print(f"Selected USB device: {selected_device}")

# Run pyserial-miniterm with the selected device
run_miniterm(selected_device)

if __name__ == '__main__':
main()
main()
40 changes: 23 additions & 17 deletions ner_environment/ner_setup.py
Original file line number Diff line number Diff line change
@@ -9,6 +9,18 @@
except ImportError:
distro = None

def is_wsl(v: str = platform.uname().release) -> int:
"""
detects if Python is running in WSL
"""

if v.endswith("-Microsoft"):
return 1
elif v.endswith("microsoft-standard-WSL2"):
return 2

return 0

def run_command(command, check=True, shell=False):
try:
result = subprocess.run(command, check=check, shell=shell, text=True, capture_output=True)
@@ -19,22 +31,20 @@ def run_command(command, check=True, shell=False):
print(f"Command failed: {e.cmd}\nReturn code: {e.returncode}\nOutput: {e.output}\nError: {e.stderr}", file=sys.stderr)
sys.exit(1)

def check_docker_and_rust():
print("This script requires Docker and Rust to be installed.")
answer = input("Do you have Docker and Rust installed? (yes/no): ").strip().lower()
def check_docker():
print("This script requires Docker to be installed.")
answer = input("Do you have Docker installed? (yes/no): ").strip().lower()
if 'y' not in answer:
sys.exit(1)

os_type = platform.system()
if os_type == 'Windows':
print("Windows OS detected. If on windows, you must be using bash (included with git), instead of cmd or powershell")
answer = input("Are you using bash? (yes/no): ").strip().lower()
if 'y' not in answer:
sys.exit(1)
print("Windows OS detected. If on windows, you must be using WSL. Revist the docs and reinstall. Sorry")
sys.exit(1)

def docker_pull(image_url):
os_type = platform.system()
if os_type=='Windows' or os_type == 'Darwin':
if is_wsl() or os_type == 'Darwin':
print("Please open the Docker Desktop application before proceeding")
answer =input("Is the Docker Desktop app running? (yes/no)")
print("Pulling Docker image...")
@@ -70,11 +80,7 @@ def install_precommit(venv_python):
def install_openocd():
os_type = platform.system()
try:
if os_type == "Windows":
print("Please install OpenOCD manually on Windows. You can download it from https://gnutoolchains.com/arm-eabi/openocd/")
print("You will need to unzip the downloaded file and run the exe file.")
return
elif os_type == "Darwin":
if os_type == "Darwin":
run_command(["brew", "install", "openocd"])

elif os_type == "Linux":
@@ -113,8 +119,8 @@ def main():
print("Everyone's system is different, and if you already have something installed, or know exactly")
print("what you are doing, feel free to manually go about this as needed.")

# Step 0: Check for Docker and Rust
check_docker_and_rust()
# Step 0: Check for Docker
check_docker()

# Step 1: pull image
answer = input("Would you like to pull the docker image? (yes/no)")
@@ -137,7 +143,7 @@ def main():
answer = input("Would you like to install all python packages in the venv? (yes/no)")
if 'y' in answer:
# Use the venv's Python
venv_python = os.path.join(venv_path, 'Scripts', 'python') if os_type == "Windows" else os.path.join(venv_path, 'bin', 'python')
venv_python = os.path.join(venv_path, 'bin', 'python')

# Step 4: run setup.py (installs requirements and entry points)
run_setup(venv_python)
@@ -151,7 +157,7 @@ def main():
install_openocd()

# Step 6: Install usbip if on Linux
if os_type == "Linux":
if os_type == "Linux" and is_wsl() == 0:
answer = input("Would you like to install usbip? (yes/no)")
if 'y' in answer:
install_usbip()
1 change: 1 addition & 0 deletions ner_environment/requirements.txt
Original file line number Diff line number Diff line change
@@ -2,3 +2,4 @@ distro
pre-commit
clang-format
pyserial
typer
4 changes: 2 additions & 2 deletions ner_environment/setup.py
Original file line number Diff line number Diff line change
@@ -7,12 +7,12 @@ def parse_requirements(filename):

setup(
name='ner-enviroment',
version='0.1',
version='0.2',
packages=find_packages(include=['build_system', 'build_system.*']),
install_requires=parse_requirements('requirements.txt'),
entry_points={
'console_scripts': [
'ner=build_system:main',
'ner=build_system:app',
'clang_restage=build_system.clang_restage:main'
],
},
7 changes: 7 additions & 0 deletions openocd.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
$_TARGETNAME configure -event gdb-detach {
reset run
shutdown
}

init
reset halt

0 comments on commit 5cb9db1

Please sign in to comment.