Skip to content

Commit

Permalink
Add escc flag to the scons build args
Browse files Browse the repository at this point in the history
Add initial README for ESCC board

This commit introduces a new README for the ESCC board detailing the pinout configurations and connections between the Molex connector and IONIQ harness and radar components. The documentation provides a clear, structured guide to assist with proper wiring and setup.

Improving comments for clarity and explicitness

IDE is behaving now. Wd don't need this hack anymore

Add ESCC build option and refine ESCC specific code

Introduce --escc option in SCons to conditionally build ESCC firmware. Update build_project to support CPPDEFINES and modify main.c to handle ESCC-specific safety modes and behaviors more cleanly.

Remove unnecessary send_escc_msg calls in Hyundai safety code

The updates in the code remove redundant send_escc_msg calls in the Hyundai safety code. We don't really need to send the FCA or AEB data through the ESCC packet because we don't block it at all. So we don't really need to "package" it anymore on the ESCC message

Add send_escc_msg call in case blocks

The send_escc_msg function is now called within the individual case blocks for SCC11, SCC12, and FCA11, enhancing code readability. Before, it was called on any message that the radar sends, but this strategy was inefficient as it was piggybacking on their frequencies.

Refactor `escc_tx_hook` function in Hyundai safety code

The commented-out `escc_tx_hook` function in Hyundai ESCC safety code has been refactored and is now in use again, replacing the formerly used `alloutput_tx_hook`. Debugging functionality has been preserved and conditions for when the function is unused have also been handled. The resulting code is now cleaner and more efficient.

Add ESCC firmware and build files, and safety mode for Hyundai ESCC

This commit introduces ESCC firmware and the corresponding build files. We have also defined a new safety mode for Hyundai ESCC. The necessary scripts for flashing and recovery have been added, alongside the gitignore file for the ESCC board. Additionally, we have implemented new message processing and forwarding logic to handle ESCC specific messages in main and safety files.
  • Loading branch information
devtekve committed Feb 1, 2025
1 parent 4ca9633 commit 3cf59d6
Show file tree
Hide file tree
Showing 14 changed files with 481 additions and 12 deletions.
26 changes: 16 additions & 10 deletions SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ def to_c_uint32(x):
return "{" + 'U,'.join(map(str, nums)) + "U}"


def build_project(project_name, project, extra_flags):
def build_project(project_name, project, extra_flags, cppdefines=None):
cppdefines = cppdefines or []
print(f"{project_name} -> CPPDEFINES [{', '.join(cppdefines)}]")
linkerscript_fn = File(project["LINKER_SCRIPT"]).srcnode().relpath

flags = project["PROJECT_FLAGS"] + extra_flags + common_flags + [
Expand Down Expand Up @@ -101,6 +103,7 @@ def build_project(project_name, project, extra_flags):
'Objcopy': Builder(generator=objcopy, suffix='.bin', src_suffix='.elf')
},
tools=["default", "compilation_db"],
CPPDEFINES=cppdefines,
)

startup = env.Object(f"obj/startup_{project_name}", project["STARTUP_FILE"])
Expand Down Expand Up @@ -179,12 +182,15 @@ with open("board/obj/cert.h", "w") as f:
for cert in certs:
f.write("\n".join(cert) + "\n")

# panda fw
SConscript('board/SConscript')

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

# test files
if GetOption('extras'):
SConscript('tests/libpanda/SConscript')
if not GetOption('escc'): # if we are building ESCC, we don't build anything else
# panda fw
SConscript('board/SConscript')

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

# test files
if GetOption('extras'):
SConscript('tests/libpanda/SConscript')
else:
SConscript('board/escc/SConscript')
4 changes: 4 additions & 0 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ AddOption('--mutation',
action='store_true',
help='generate mutation-ready code')

AddOption('--escc',
action='store_true',
help='build escc firmware')

env = Environment(
COMPILATIONDB_USE_ABSPATH=True,
tools=["default", "compilation_db"],
Expand Down
1 change: 1 addition & 0 deletions board/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//#define DEBUG_FAULTS
//#define DEBUG_COMMS
//#define DEBUG_FAN
//#define DEBUG_CAN_LOOP

#define CAN_INIT_TIMEOUT_MS 500U
#define USBPACKET_MAX_SIZE 0x40U
Expand Down
1 change: 1 addition & 0 deletions board/escc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
obj/*
81 changes: 81 additions & 0 deletions board/escc/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
## Documentation
[see more](https://github.com/sunnypilot/sunnypilot/blob/5d754169a6285e88e755ff9a592a851cb92b0abc/docs/escc/ESCC%20IONIQ%20PHEV%20Guide.md)

### Detailed Connection List

#### Molex 5018762640 Pinout:

- Pin 1: GND
- Pin 2: IGN
- Pin 3: internal 120 Ohm Resistor, simply bridge to Pin 5
- Pin 4: C-CAN HIGH
- Pin 5: internal 120 Ohm Resistor, simply bridge to Pin 3
- Pin 6: C-CAN LOW
- Pin 8: L-CAN HIGH
- Pin 10: L-CAN LOW
- Pin 12: 12v
- Pin 14: 12v
- Pin 16: IGN
- Pin 18: L-CAN HIGH
- Pin 20: L-CAN LOW
- Pin 22: C-CAN HIGH
- Pin 24: C-CAN LOW
- Pin 26: GND

#### **IONIQ 2203663 (Car Harness, Female):**

- Pin 1: 12v -> Molex Pin 12
- Pin 4: GND -> Molex Pin 1
- Pin 7: C-CAN LOW -> Molex Pin 6
- Pin 8: C-CAN HIGH -> Molex Pin 4
- Pin 9: L-CAN LOW -> Molex Pin 10
- Pin 10: IGPM (IGN) -> Molex Pin 2
- Pin 18: L-CAN HIGH -> Molex Pin 8

#### **IONIQ 2203663 (Car Radar, Male):**

- Molex Pin: 14 -> Pin 1: 12v
- Molex Pin: 26 -> Pin 4: GND
- Molex Pin: 24 -> Pin 7: C-CAN LOW
- Molex Pin: 22 -> Pin 8: C-CAN HIGH
- Molex Pin: 20 -> Pin 9: L-CAN LOW
- Molex Pin: 16 -> Pin 10: IGN
- Molex Pin: 18 -> Pin 18: L-CAN HIGH


```
IONIQ 2203663 Molex 5018762640 IONIQ 2203663
Car Harness (Female) Car Radar (Male)
+-----+-----+ +-----+-----+ +-----+-----+
| 18 | 9 | | 2 | 1 | | 10 | 1 |
| 17 | 8 | | 4 | 3 | | 11 | 2 |
| 16 | 7 | | 6 | 5 | | 12 | 3 |
| 15 | 6 | | 8 | 7 | | 13 | 4 |
| 14 | 5 | NOTCH | 10 | 9 | | 14 | 5 | NOTCH
| 13 | 4 | | 12 | 11 | | 15 | 6 |
| 12 | 3 | | 14 | 13 | NOTCH | 16 | 7 |
| 11 | 2 | | 16 | 15 | | 17 | 8 |
| 10 | 1 | | 18 | 17 | | 18 | 9 |
+-----+-----+ | 20 | 19 | +-----+-----+
| 22 | 21 |
| 24 | 23 |
| 26 | 25 |
+-----+-----+
Connections:
Molex Pin 3 (120 Ohm) <--> Molex Pin 5 (120 Ohm)
Harness Pin 1 (12V) <--> Molex Pin 12 (12V)
Harness Pin 4 (GND) <--> Molex Pin 1 (GND)
Harness Pin 10 (IGN) <--> Molex Pin 2 (IGN)
Harness Pin 8 (C-CAN H) <--> Molex Pin 4 (C-CAN H)
Harness Pin 7 (C-CAN L) <--> Molex Pin 6 (C-CAN L)
Harness Pin 18 (L-CAN H) <--> Molex Pin 8 (L-CAN H)
Harness Pin 9 (L-CAN L) <--> Molex Pin 10 (L-CAN L)
Molex Pin 20 (L-CAN L) <--> Radar Pin 9 (L-CAN L)
Molex Pin 18 (L-CAN H) <--> Radar Pin 18 (L-CAN H)
Molex Pin 24 (C-CAN L) <--> Radar Pin 7 (C-CAN L)
Molex Pin 22 (C-CAN H) <--> Radar Pin 8 (C-CAN H)
Molex Pin 16 (IGN) <--> Radar Pin 10 (IGN)
Molex Pin 14 (12V) <--> Radar Pin 1 (12V)
Molex Pin 26 (GND) <--> Radar Pin 4 (GND)
```
34 changes: 34 additions & 0 deletions board/escc/SConscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os
import copy

Import('build_project')

build_projects = {}

build_projects["escc"] = {
"MAIN": "../main.c",
"BOOTSTUB": "../bootstub.c",
"STARTUP_FILE": File("../stm32f4/startup_stm32f413xx.s"),
"LINKER_SCRIPT": File("../stm32f4/stm32f4_flash.ld"),
"APP_START_ADDRESS": "0x8004000",
"PROJECT_FLAGS": [
"-mcpu=cortex-m4",
"-mhard-float",
"-DSTM32F4",
"-DSTM32F413xx",
"-mfpu=fpv4-sp-d16",
"-fsingle-precision-constant",
"-Os",
"-g",
],
}

for project_name, project in build_projects.items():
flags = [
"-DPANDA",
"-DESCC",
]
if ("ENABLE_SPI" in os.environ or "h7" in project_name) and not project_name.startswith('pedal'):
flags.append('-DENABLE_SPI')

build_project(project_name, project, flags, ["ESCC"])
18 changes: 18 additions & 0 deletions board/escc/flash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python3
import os
import subprocess

from panda import Panda

board_path = os.path.dirname(os.path.realpath(__file__))

if __name__ == "__main__":
print(board_path)
subprocess.check_call(f"scons -C {board_path}/../.. -j$(nproc) {board_path} --escc", shell=True)

serials = Panda.list()
print(f"found {len(serials)} panda(s) - {serials}")
for s in serials:
print("flashing", s)
with Panda(serial=s) as p:
p.flash("obj/escc.bin.signed")
8 changes: 8 additions & 0 deletions board/escc/flash_can.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env sh
set -e

cd ..
scons -u -j$(nproc) --escc
cd escc

../../tests/escc/enter_canloader.py obj/escc.bin.signed
11 changes: 11 additions & 0 deletions board/escc/recover.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env sh
set -e

DFU_UTIL="dfu-util"

cd ..
scons -u -j$(nproc) --escc
cd escc

$DFU_UTIL -d 0483:df11 -a 0 -s 0x08004000 -D obj/escc.bin.signed
$DFU_UTIL -d 0483:df11 -a 0 -s 0x08000000:leave -D obj/bootstub.escc.bin
94 changes: 93 additions & 1 deletion board/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,61 @@ bool is_car_safety_mode(uint16_t mode) {
(mode != SAFETY_ELM327);
}

#ifdef ESCC
// ***************************** ESCC code *****************************
bool is_car_safety_mode_escc(uint16_t mode) {
return is_car_safety_mode(mode) && (mode != SAFETY_HYUNDAI_ESCC);
}
#define is_car_safety_mode is_car_safety_mode_escc

#define CAN_ESCC_INPUT 0x2AC
#define CAN_ESCC_OUTPUT 0x2ABU

void send_escc_msg(const ESCC_Msg *msg, const int bus_number) {
#ifdef DEBUG

print("send_escc_msg\n");
print("fca_cmd_act: "); putui(msg->fca_cmd_act); print("\n");
print("aeb_cmd_act: "); putui(msg->aeb_cmd_act); print("\n");
print("cf_vsm_warn_fca11: "); putui(msg->cf_vsm_warn_fca11); print("\n");
print("cf_vsm_warn_scc12: "); putui(msg->cf_vsm_warn_scc12); print("\n");
print("cf_vsm_deccmdact_scc12: "); putui(msg->cf_vsm_deccmdact_scc12); print("\n");
print("cf_vsm_deccmdact_fca11: "); putui(msg->cf_vsm_deccmdact_fca11); print("\n");
print("cr_vsm_deccmd_scc12: "); putui(msg->cr_vsm_deccmd_scc12); print("\n");
print("cr_vsm_deccmd_fca11: "); putui(msg->cr_vsm_deccmd_fca11); print("\n");
print("obj_valid: "); putui(msg->obj_valid); print("\n");
print("acc_objstatus: "); putui(msg->acc_objstatus); print("\n");
print("acc_obj_lat_pos_1: "); putui(msg->acc_obj_lat_pos_1); print("\n");
print("acc_obj_lat_pos_2: "); putui(msg->acc_obj_lat_pos_2); print("\n");
print("acc_obj_dist_1: "); putui(msg->acc_obj_dist_1); print("\n");
print("acc_obj_dist_2: "); putui(msg->acc_obj_dist_2); print("\n");
print("acc_obj_rel_spd_1: "); putui(msg->acc_obj_rel_spd_1); print("\n");
print("acc_obj_rel_spd_2: "); putui(msg->acc_obj_rel_spd_2); print("\n");
#endif

uint8_t dat[8];
dat[0] = (msg->fca_cmd_act) | (msg->cf_vsm_warn_fca11 << 1) | (msg->aeb_cmd_act << 3) |
(msg->cf_vsm_warn_scc12 << 4) | (msg->cf_vsm_deccmdact_scc12 << 6) | (msg->cf_vsm_deccmdact_fca11 << 7);
dat[1] = (msg->cr_vsm_deccmd_scc12);
dat[2] = (msg->obj_valid) | (msg->acc_objstatus << 1);
dat[3] = (msg->acc_obj_lat_pos_1);
dat[4] = (msg->acc_obj_lat_pos_2) | (msg->acc_obj_dist_1 << 1);
dat[5] = (msg->acc_obj_dist_2) | (msg->acc_obj_rel_spd_1 << 4);
dat[6] = (msg->acc_obj_rel_spd_2);
dat[7] = (msg->cr_vsm_deccmd_fca11);

CANPacket_t to_send;
to_send.extended = CAN_ESCC_OUTPUT >= 0x800 ? 1 : 0;
to_send.addr = CAN_ESCC_OUTPUT;
to_send.bus = bus_number;
to_send.data_len_code = sizeof(dat);
memcpy(to_send.data, dat, sizeof(dat));

can_set_checksum(&to_send);
can_send(&to_send, bus_number, true);
}
#endif

// ***************************** main code *****************************

// cppcheck-suppress unusedFunction ; used in headers not included in cppcheck
Expand Down Expand Up @@ -186,7 +241,9 @@ static void tick_handler(void) {
print("rx:"); puth4(can_rx_q.r_ptr); print("-"); puth4(can_rx_q.w_ptr); print(" ");
print("tx1:"); puth4(can_tx1_q.r_ptr); print("-"); puth4(can_tx1_q.w_ptr); print(" ");
print("tx2:"); puth4(can_tx2_q.r_ptr); print("-"); puth4(can_tx2_q.w_ptr); print(" ");
print("tx3:"); puth4(can_tx3_q.r_ptr); print("-"); puth4(can_tx3_q.w_ptr); print("\n");
print("tx3:"); puth4(can_tx3_q.r_ptr); print("-"); puth4(can_tx3_q.w_ptr); print(" ");
print("SAFETY MODE:"); putui(current_safety_mode);
print("\n");
#endif

// set green LED to be controls allowed
Expand Down Expand Up @@ -235,6 +292,7 @@ static void tick_handler(void) {

mads_heartbeat_engaged_check();

#ifndef ESCC // Hearbet disabled when ESCC
if (!heartbeat_disabled) {
// if the heartbeat has been gone for a while, go to SILENT safety mode and enter power save
if (heartbeat_counter >= (check_started() ? HEARTBEAT_IGNITION_CNT_ON : HEARTBEAT_IGNITION_CNT_OFF)) {
Expand Down Expand Up @@ -273,6 +331,7 @@ static void tick_handler(void) {
fan_set_power(enabled ? 50U : 0U);
}
}
#endif

// check registers
check_registers();
Expand Down Expand Up @@ -335,8 +394,13 @@ int main(void) {
fan_init();
}

#ifdef ESCC
// init to SAFETY_HYUNDAI_ESCC and can all output
set_safety_mode(SAFETY_HYUNDAI_ESCC, 1U);
#else
// init to SILENT and can silent
set_safety_mode(SAFETY_SILENT, 0U);
#endif

// enable CAN TXs
current_board->enable_can_transceivers(true);
Expand Down Expand Up @@ -367,7 +431,35 @@ int main(void) {
enable_interrupts();

// LED should keep on blinking all the time
#if defined(ESCC) && defined(DEBUG_CAN_LOOP)
ESCC_Msg escc;
escc.fca_cmd_act = 1;
escc.aeb_cmd_act = 1;
escc.cf_vsm_warn_fca11 = 1;
escc.cf_vsm_warn_scc12 = 1;
escc.cf_vsm_deccmdact_scc12 = 1;
escc.cf_vsm_deccmdact_fca11 = 1;
escc.cr_vsm_deccmd_scc12 = 1;
escc.cr_vsm_deccmd_fca11 = 1;
escc.obj_valid = 1;
escc.acc_objstatus = 1;
escc.acc_obj_lat_pos_1 = 1;
escc.acc_obj_lat_pos_2 = 1;
escc.acc_obj_dist_1 = 1;
escc.acc_obj_dist_2 = 1;
escc.acc_obj_rel_spd_1 = 1;
escc.acc_obj_rel_spd_2 = 1;
int i = 0;
#endif

while (true) {

#if defined(ESCC) && defined(DEBUG_CAN_LOOP)
send_escc_msg(&escc, i++);
if(i > 3)
i = 0;
#endif

if (power_save_status == POWER_SAVE_STATUS_DISABLED) {
#ifdef DEBUG_FAULTS
if (fault_status == FAULT_STATUS_NONE) {
Expand Down
12 changes: 11 additions & 1 deletion board/safety.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
#include "safety/safety_elm327.h"
#include "safety/safety_body.h"

#ifdef ESCC
#include "safety/safety_hyundai_escc.h"
#endif

// CAN-FD only safety modes
#ifdef CANFD
#include "safety/safety_hyundai_canfd.h"
Expand Down Expand Up @@ -56,6 +60,10 @@
#define SAFETY_BODY 27U
#define SAFETY_HYUNDAI_CANFD 28U

#ifdef ESCC
#define SAFETY_HYUNDAI_ESCC 29U
#endif

uint32_t GET_BYTES(const CANPacket_t *msg, int start, int len) {
uint32_t ret = 0U;
for (int i = 0; i < len; i++) {
Expand Down Expand Up @@ -375,7 +383,9 @@ int set_safety_hooks(uint16_t mode, uint16_t param) {
{SAFETY_GM, &gm_hooks},
{SAFETY_HONDA_BOSCH, &honda_bosch_hooks},
{SAFETY_HYUNDAI, &hyundai_hooks},
{SAFETY_CHRYSLER, &chrysler_hooks},
#ifdef ESCC
{SAFETY_HYUNDAI_ESCC, &hyundai_escc_hooks},
#endif {SAFETY_CHRYSLER, &chrysler_hooks},
{SAFETY_SUBARU, &subaru_hooks},
{SAFETY_VOLKSWAGEN_MQB, &volkswagen_mqb_hooks},
{SAFETY_NISSAN, &nissan_hooks},
Expand Down
Loading

0 comments on commit 3cf59d6

Please sign in to comment.