Skip to content

Commit

Permalink
Merge branch 'master' into cherokee_kl
Browse files Browse the repository at this point in the history
  • Loading branch information
jyoung8607 committed Feb 17, 2024
2 parents 9f8d407 + 997b075 commit 5139b6d
Show file tree
Hide file tree
Showing 261 changed files with 1,638 additions and 21,568 deletions.
2 changes: 0 additions & 2 deletions .dockerignore

This file was deleted.

47 changes: 9 additions & 38 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ concurrency:

env:
RUN: docker run -v ${{ github.workspace }}:/tmp/openpilot/panda -w /tmp/openpilot/panda --rm panda /bin/bash -c
PERSIST: docker run -v ${{ github.workspace }}:/tmp/openpilot/panda -w /tmp/openpilot/panda --name panda panda /bin/bash -c
BUILD: |
export DOCKER_BUILDKIT=1
docker build --pull --build-arg BUILDKIT_INLINE_CACHE=1 --cache-from ghcr.io/commaai/panda:latest -t panda -f Dockerfile .
Expand Down Expand Up @@ -47,7 +46,7 @@ jobs:
run: eval "$BUILD"
- name: Test python package installer
run: ${{ env.RUN }} "python setup.py install"
- name: Build panda + pedal images and bootstub
- name: Build panda images and bootstub
run: ${{ env.RUN }} "scons -j4"
- name: Build panda with SPI support
run: ${{ env.RUN }} "ENABLE_SPI=1 scons -j4"
Expand Down Expand Up @@ -90,48 +89,22 @@ jobs:
scons -j$(nproc) ${{ matrix.flags }} && \
tests/safety/test.sh"
safety_coverage:
name: safety coverage
runs-on: ubuntu-20.04
timeout-minutes: 20
steps:
- uses: actions/checkout@v2
- name: Build Docker image
run: eval "$BUILD"
- name: Run safety coverage test
timeout-minutes: 5
run: |
${{ env.RUN }} "cd .. && \
scons -c && \
scons -j$(nproc) opendbc/ cereal/ && \
cd panda/tests/safety && \
./test_coverage.sh"
misra:
name: misra c2012
name: MISRA C:2012
runs-on: ubuntu-20.04
timeout-minutes: 20
steps:
- uses: actions/checkout@v2
- name: Build Docker image
run: eval "$BUILD"
- name: Run Misra C 2012 analysis
- name: Build FW
run: ${{ env.RUN }} "scons -j$(nproc)"
- name: Run MISRA C:2012 analysis
timeout-minutes: 1
run: ${{ env.RUN }} "cd tests/misra && ./test_misra.sh"
- name: MISRA mutation tests
timeout-minutes: 4
run: ${{ env.PERSIST }} "cd tests/misra && ./test_misra.sh"
- name: Copy analysis outputs
run: docker cp panda:/tmp/misra /tmp
- uses: actions/upload-artifact@v2
if: always()
with:
name: cppcheck_output
path: /tmp/misra/cppcheck_*_output.txt
- uses: actions/upload-artifact@v2
if: always()
with:
name: misra_output
path: /tmp/misra/misra_*_output.txt
- run: docker rm panda
if: always()
run: ${{ env.RUN }} "cd tests/misra && ./test_mutation.py"

python_linter:
name: python linter
Expand All @@ -144,5 +117,3 @@ jobs:
- name: Run static analysis
timeout-minutes: 3
run: ${{ env.RUN }} "pre-commit run --all"


4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ repos:
- id: check-executables-have-shebangs
- id: check-shebang-scripts-are-executable
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.7.1
rev: v1.8.0
hooks:
- id: mypy
additional_dependencies: ['git+https://github.com/numpy/numpy-stubs', 'types-requests', 'types-atomicwrites',
'types-pycurl']
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.7
rev: v0.2.1
hooks:
- id: ruff
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ ENV PATH="/root/.pyenv/bin:/root/.pyenv/shims:${PATH}"

ENV PANDA_PATH=/tmp/openpilot/panda
ENV OPENPILOT_REF="5690386d8d731c9bebda536a5c71c890f6dfe98c"
ENV OPENDBC_REF="5b0c73977f1428700d0344d52874a90a4c5168fb"
ENV OPENDBC_REF="40d9c723d48496229fecc436046538a53af19c11"

COPY requirements.txt /tmp/
RUN pyenv install 3.11.4 && \
Expand Down
13 changes: 3 additions & 10 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pipeline {
["build", "scons -j4"],
["flash", "cd tests/ && ./reflash_internal_panda.py"],
["flash jungle", "cd board/jungle && ./flash.py"],
["test", "cd tests/hitl && HW_TYPES=6 pytest --durations=0 [2-7]*.py -k 'not test_send_recv'"],
["test", "cd tests/hitl && HW_TYPES=6 pytest -n0 --durations=0 [2-7]*.py -k 'not test_send_recv'"],
])
}
}
Expand All @@ -94,7 +94,7 @@ pipeline {
["build", "scons -j4"],
["flash", "cd tests/ && ./reflash_internal_panda.py"],
["flash jungle", "cd board/jungle && ./flash.py"],
["test", "cd tests/hitl && HW_TYPES=9 pytest --durations=0 2*.py [5-9]*.py"],
["test", "cd tests/hitl && HW_TYPES=9 pytest -n0 --durations=0 2*.py [5-9]*.py"],
])
}
}
Expand Down Expand Up @@ -126,19 +126,12 @@ pipeline {
stage('bootkick tests') {
steps {
script {
docker_run("test", 10, "pytest ./tests/som/test_bootkick.py")
docker_run("test", 10, "pytest -n0 ./tests/som/test_bootkick.py")
}
}
}

/*
stage('pedal tests') {
steps {
script {
docker_run("test pedal", 1, "PEDAL_JUNGLE=058010800f51363038363036 python ./tests/pedal/test_pedal.py")
}
}
}
stage('HITL tests') {
steps {
script {
Expand Down
101 changes: 42 additions & 59 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,68 @@
![panda tests](https://github.com/commaai/panda/workflows/tests/badge.svg)
![panda drivers](https://github.com/commaai/panda/workflows/drivers/badge.svg)

[panda](http://github.com/commaai/panda) is the nicest universal car interface ever.
panda speaks CAN and CAN FD, and it runs on [STM32F413](https://www.st.com/resource/en/reference_manual/rm0430-stm32f413423-advanced-armbased-32bit-mcus-stmicroelectronics.pdf) and [STM32H725](https://www.st.com/resource/en/reference_manual/rm0468-stm32h723733-stm32h725735-and-stm32h730-value-line-advanced-armbased-32bit-mcus-stmicroelectronics.pdf).

<a href="https://comma.ai/shop/products/panda-obd-ii-dongle"><img src="https://github.com/commaai/panda/blob/master/panda.png?raw=true"></a>
## Directory structure

```
.
├── board # Code that runs on the STM32
├── drivers # Drivers (not needed for use with Python)
├── python # Python userspace library for interfacing with the panda
├── tests # Tests and helper programs for panda
```

## Safety Model

When a panda powers up, by default it's in `SAFETY_SILENT` mode. While in `SAFETY_SILENT` mode, the CAN buses are forced to be silent. In order to send messages, you have to select a safety mode. Some of safety modes (for example `SAFETY_ALLOUTPUT`) are disabled in release firmwares. In order to use them, compile and flash your own build.

Safety modes optionally support `controls_allowed`, which allows or blocks a subset of messages based on a customizable state in the board.

## Code Rigor

The panda firmware is written for its use in conjuction with [openpilot](https://github.com/commaai/openpilot). The panda firmware, through its safety model, provides and enforces the
[openpilot safety](https://github.com/commaai/openpilot/blob/master/docs/SAFETY.md). Due to its critical function, it's important that the application code rigor within the `board` folder is held to high standards.

These are the [CI regression tests](https://github.com/commaai/panda/actions) we have in place:
* A generic static code analysis is performed by [cppcheck](https://github.com/danmar/cppcheck/).
* In addition, [cppcheck](https://github.com/danmar/cppcheck/) has a specific addon to check for [MISRA C:2012](https://www.misra.org.uk/MISRAHome/MISRAC2012/tabid/196/Default.aspx) violations. See [current coverage](https://github.com/commaai/panda/blob/master/tests/misra/coverage_table).
* Compiler options are relatively strict: the flags `-Wall -Wextra -Wstrict-prototypes -Werror` are enforced.
* The [safety logic](https://github.com/commaai/panda/tree/master/board/safety) is tested and verified by [unit tests](https://github.com/commaai/panda/tree/master/tests/safety) for each supported car variant.
to ensure that the behavior remains unchanged.
* A hardware-in-the-loop test verifies panda's functionalities on all active panda variants, including:
* additional safety model checks
* compiling and flashing the bootstub and app code
* receiving, sending, and forwarding CAN messages on all buses
* CAN loopback and latency tests through USB and SPI

panda speaks CAN, CAN FD, LIN, and GMLAN. panda supports [STM32F205](https://www.st.com/resource/en/reference_manual/rm0033-stm32f205xx-stm32f207xx-stm32f215xx-and-stm32f217xx-advanced-armbased-32bit-mcus-stmicroelectronics.pdf), [STM32F413](https://www.st.com/resource/en/reference_manual/rm0430-stm32f413423-advanced-armbased-32bit-mcus-stmicroelectronics.pdf), and [STM32H725](https://www.st.com/resource/en/reference_manual/rm0468-stm32h723733-stm32h725735-and-stm32h730-value-line-advanced-armbased-32bit-mcus-stmicroelectronics.pdf).
The above tests are themselves tested by:
* a [mutation test](tests/misra/test_mutation.py) on the MISRA coverage
* 100% line coverage enforced on the safety unit tests

In addition, we run the [ruff linter](https://github.com/astral-sh/ruff) and [mypy](https://mypy-lang.org/) on panda's Python library.

## Usage

Setup dependencies:
```bash
# Ubuntu
sudo apt-get install dfu-util gcc-arm-none-eabi python3-pip libffi-dev git
```
```bash

# macOS
brew install --cask gcc-arm-embedded
brew install python3 dfu-util gcc@13
```

Clone panda repository:
Clone panda repository and install:
``` bash
git clone https://github.com/commaai/panda.git
cd panda
```

Install requirements:
```bash
# install dependencies
pip install -r requirements.txt
```

Install library:
``` bash
# install panda
python setup.py install
```

Expand Down Expand Up @@ -68,53 +98,6 @@ As a universal car interface, it should support every reasonable software interf
- [Python library](https://github.com/commaai/panda/tree/master/python)
- [C++ library](https://github.com/commaai/openpilot/tree/master/selfdrive/boardd)
- [socketcan in kernel](https://github.com/commaai/panda/tree/master/drivers/linux) (alpha)
- [Windows J2534](https://github.com/commaai/panda/tree/master/drivers/windows)

## Directory structure

```
.
├── board # Code that runs on the STM32
├── drivers # Drivers (not needed for use with python)
├── python # Python userspace library for interfacing with the panda
├── tests # Tests and helper programs for panda
```

## Programming

See [`board/README.md`](board/README.md)

## Debugging

To print out the serial console from the STM32, run `tests/debug_console.py`

## Safety Model

When a panda powers up, by default it's in `SAFETY_SILENT` mode. While in `SAFETY_SILENT` mode, the buses are also forced to be silent. In order to send messages, you have to select a safety mode. Currently, setting safety modes is only supported over USB. Some of safety modes (for example `SAFETY_ALLOUTPUT`) are disabled in release firmwares. In order to use them, compile and flash your own build.

Safety modes optionally supports `controls_allowed`, which allows or blocks a subset of messages based on a customizable state in the board.

## Code Rigor

The panda firmware is written for its use in conjuction with [openpilot](https://github.com/commaai/openpilot). The panda firmware, through its safety model, provides and enforces the
[openpilot safety](https://github.com/commaai/openpilot/blob/master/docs/SAFETY.md). Due to its critical function, it's important that the application code rigor within the `board` folder is held to high standards.

These are the [CI regression tests](https://github.com/commaai/panda/actions) we have in place:
* A generic static code analysis is performed by [cppcheck](https://github.com/danmar/cppcheck/).
* In addition, [cppcheck](https://github.com/danmar/cppcheck/) has a specific addon to check for [MISRA C:2012](https://www.misra.org.uk/MISRAHome/MISRAC2012/tabid/196/Default.aspx) violations. See [current coverage](https://github.com/commaai/panda/blob/master/tests/misra/coverage_table).
* Compiler options are relatively strict: the flags `-Wall -Wextra -Wstrict-prototypes -Werror` are enforced.
* The [safety logic](https://github.com/commaai/panda/tree/master/board/safety) is tested and verified by [unit tests](https://github.com/commaai/panda/tree/master/tests/safety) for each supported car variant.
* A recorded drive for each supported car variant is [replayed through the safety logic](https://github.com/commaai/panda/tree/master/tests/safety_replay)
to ensure that the behavior remains unchanged.
* An internal Hardware-in-the-loop test, which currently only runs on pull requests opened by comma.ai's organization members, verifies the following functionalities:
* compiling the code and flashing it through USB.
* receiving, sending, and forwarding CAN messages on all buses, over USB.

In addition, we run the [ruff linter](https://github.com/astral-sh/ruff) on all python files within the panda repo.

## Hardware

Check out the hardware [guide](https://github.com/commaai/panda/blob/master/docs/guide.pdf)

## Licensing

Expand Down
16 changes: 7 additions & 9 deletions SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ BUILDER = "DEV"

common_flags = []

panda_root = Dir('.').abspath
panda_root = Dir('.')

if os.getenv("RELEASE"):
BUILD_TYPE = "RELEASE"
Expand All @@ -16,7 +16,7 @@ if os.getenv("RELEASE"):
assert os.path.exists(cert_fn), 'Certificate file not found. Please specify absolute path'
else:
BUILD_TYPE = "DEBUG"
cert_fn = File("./certs/debug").srcnode().abspath
cert_fn = File("./certs/debug").srcnode().relpath
common_flags += ["-DALLOW_DEBUG"]

if os.getenv("DEBUG"):
Expand All @@ -35,7 +35,7 @@ def get_version(builder, build_type):
def get_key_header(name):
from Crypto.PublicKey import RSA

public_fn = File(f'./certs/{name}.pub').srcnode().abspath
public_fn = File(f'./certs/{name}.pub').srcnode().get_path()
with open(public_fn) as f:
rsa = RSA.importKey(f.read())
assert(rsa.size_in_bits() == 1024)
Expand Down Expand Up @@ -63,7 +63,7 @@ def to_c_uint32(x):


def build_project(project_name, project, extra_flags):
linkerscript_fn = File(project["LINKER_SCRIPT"]).srcnode().abspath
linkerscript_fn = File(project["LINKER_SCRIPT"]).srcnode().relpath

flags = project["PROJECT_FLAGS"] + extra_flags + common_flags + [
"-Wall",
Expand Down Expand Up @@ -101,7 +101,8 @@ def build_project(project_name, project, extra_flags):
ASCOM="$AS $ASFLAGS -o $TARGET -c $SOURCES",
BUILDERS={
'Objcopy': Builder(generator=objcopy, suffix='.bin', src_suffix='.elf')
}
},
tools=["default", "compilation_db"],
)

startup = env.Object(f"obj/startup_{project_name}", project["STARTUP_FILE"])
Expand All @@ -123,7 +124,7 @@ def build_project(project_name, project, extra_flags):
main_bin = env.Objcopy(f"obj/{project_name}.bin", main_elf)

# Sign main
sign_py = File(f"{panda_root}/crypto/sign.py").srcnode().abspath
sign_py = File(f"{panda_root}/crypto/sign.py").srcnode().relpath
env.Command(f"obj/{project_name}.bin.signed", main_bin, f"SETLEN=1 {sign_py} $SOURCE $TARGET {cert_fn}")


Expand Down Expand Up @@ -179,9 +180,6 @@ with open("board/obj/cert.h", "w") as f:
# panda fw
SConscript('board/SConscript')

# pedal fw
SConscript('board/pedal/SConscript')

# panda jungle fw
SConscript('board/jungle/SConscript')

Expand Down
12 changes: 12 additions & 0 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,17 @@ AddOption('--coverage',
action='store_true',
help='build with test coverage options')

AddOption('--compile_db',
action='store_true',
help='build clang compilation database')

env = Environment(
COMPILATIONDB_USE_ABSPATH=True,
tools=["default", "compilation_db"],
)

if GetOption('compile_db'):
env.CompilationDatabase("compile_commands.json")

# panda fw & test files
SConscript('SConscript')
10 changes: 5 additions & 5 deletions board/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
Programming
----

**Panda**
## Programming

```
./flash.py # flash application
./recover.py # flash bootstub
```

## Debugging

To print out the serial console from the STM32, run `tests/debug_console.py`

Troubleshooting
----

Expand All @@ -16,5 +17,4 @@ If panda is blinking fast with green LED, use `flash.py`.

Otherwise if LED is off and panda can't be seen with `lsusb` command, use [panda paw](https://comma.ai/shop/products/panda-paw) to go into DFU mode.


If your device has an internal panda and none of the above works, try running `../tests/reflash_internal_panda.py`.
Loading

0 comments on commit 5139b6d

Please sign in to comment.