Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hkg: Add ESCC (Enhanced Smart Cruise Control) support #70

Open
wants to merge 3 commits into
base: master-new
Choose a base branch
from
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
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/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
67 changes: 66 additions & 1 deletion board/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,69 @@ void set_safety_mode(uint16_t mode, uint16_t param) {
can_init_all();
}

// cppcheck-suppress misra-c2012-5.5
bool is_car_safety_mode(uint16_t mode) {
return (mode != SAFETY_SILENT) &&
(mode != SAFETY_NOOUTPUT) &&
(mode != SAFETY_ALLOUTPUT) &&
(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 +242,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 +293,7 @@ static void tick_handler(void) {

mads_heartbeat_engaged_check();

#ifndef ESCC // Hearbet disabled when ESCC
devtekve marked this conversation as resolved.
Show resolved Hide resolved
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 +332,7 @@ static void tick_handler(void) {
fan_set_power(enabled ? 50U : 0U);
}
}
#endif

// check registers
check_registers();
Expand Down Expand Up @@ -335,8 +395,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
11 changes: 11 additions & 0 deletions 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,6 +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},
#ifdef ESCC
{SAFETY_HYUNDAI_ESCC, &hyundai_escc_hooks},
#endif
{SAFETY_CHRYSLER, &chrysler_hooks},
{SAFETY_SUBARU, &subaru_hooks},
{SAFETY_VOLKSWAGEN_MQB, &volkswagen_mqb_hooks},
Expand Down
Loading
Loading