2022-01-22 12:00:14 +08:00
|
|
|
extern int _app_start[0xc000]; // Only first 3 sectors of size 0x4000 are used
|
|
|
|
|
|
|
|
// Prototypes
|
2022-05-03 04:36:19 +08:00
|
|
|
void set_safety_mode(uint16_t mode, uint16_t param);
|
2022-01-22 12:00:14 +08:00
|
|
|
bool is_car_safety_mode(uint16_t mode);
|
|
|
|
|
|
|
|
int get_health_pkt(void *dat) {
|
|
|
|
COMPILE_TIME_ASSERT(sizeof(struct health_t) <= USBPACKET_MAX_SIZE);
|
|
|
|
struct health_t * health = (struct health_t*)dat;
|
|
|
|
|
|
|
|
health->uptime_pkt = uptime_cnt;
|
2023-05-04 19:22:01 +08:00
|
|
|
health->voltage_pkt = adc_get_mV(ADCCHAN_VIN) * VIN_READOUT_DIVIDER;
|
2022-01-22 12:00:14 +08:00
|
|
|
health->current_pkt = current_board->read_current();
|
|
|
|
|
2023-05-04 19:22:01 +08:00
|
|
|
// Use the GPIO pin to determine ignition or use a CAN based logic
|
2022-01-22 12:00:14 +08:00
|
|
|
health->ignition_line_pkt = (uint8_t)(current_board->check_ignition());
|
|
|
|
health->ignition_can_pkt = (uint8_t)(ignition_can);
|
|
|
|
|
|
|
|
health->controls_allowed_pkt = controls_allowed;
|
|
|
|
health->gas_interceptor_detected_pkt = gas_interceptor_detected;
|
2022-09-16 04:08:46 +08:00
|
|
|
health->safety_tx_blocked_pkt = safety_tx_blocked;
|
|
|
|
health->safety_rx_invalid_pkt = safety_rx_invalid;
|
|
|
|
health->tx_buffer_overflow_pkt = tx_buffer_overflow;
|
|
|
|
health->rx_buffer_overflow_pkt = rx_buffer_overflow;
|
2022-01-22 12:00:14 +08:00
|
|
|
health->gmlan_send_errs_pkt = gmlan_send_errs;
|
2023-05-08 19:30:23 +08:00
|
|
|
health->car_harness_status_pkt = harness.status;
|
2022-01-22 12:00:14 +08:00
|
|
|
health->safety_mode_pkt = (uint8_t)(current_safety_mode);
|
|
|
|
health->safety_param_pkt = current_safety_param;
|
2022-03-25 05:31:31 +08:00
|
|
|
health->alternative_experience_pkt = alternative_experience;
|
2022-01-22 12:00:14 +08:00
|
|
|
health->power_save_enabled_pkt = (uint8_t)(power_save_status == POWER_SAVE_STATUS_ENABLED);
|
|
|
|
health->heartbeat_lost_pkt = (uint8_t)(heartbeat_lost);
|
2022-10-04 07:05:16 +08:00
|
|
|
health->safety_rx_checks_invalid = safety_rx_checks_invalid;
|
2022-01-22 12:00:14 +08:00
|
|
|
|
2023-04-06 13:05:14 +08:00
|
|
|
health->spi_checksum_error_count = spi_checksum_error_count;
|
|
|
|
|
2022-01-22 12:00:14 +08:00
|
|
|
health->fault_status_pkt = fault_status;
|
|
|
|
health->faults_pkt = faults;
|
|
|
|
|
2022-04-11 17:59:47 +08:00
|
|
|
health->interrupt_load = interrupt_load;
|
|
|
|
|
2022-08-18 11:43:49 +08:00
|
|
|
health->fan_power = fan_state.power;
|
2023-04-21 04:30:56 +08:00
|
|
|
health->fan_stall_count = fan_state.total_stall_count;
|
2022-08-18 11:43:49 +08:00
|
|
|
|
2023-05-08 19:30:23 +08:00
|
|
|
health->sbu1_voltage_mV = harness.sbu1_voltage_mV;
|
|
|
|
health->sbu2_voltage_mV = harness.sbu2_voltage_mV;
|
|
|
|
|
2022-01-22 12:00:14 +08:00
|
|
|
return sizeof(*health);
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_rtc_pkt(void *dat) {
|
|
|
|
timestamp_t t = rtc_get_time();
|
|
|
|
(void)memcpy(dat, &t, sizeof(t));
|
|
|
|
return sizeof(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
// send on serial, first byte to select the ring
|
2022-08-03 19:11:52 +08:00
|
|
|
void comms_endpoint2_write(uint8_t *data, uint32_t len) {
|
|
|
|
uart_ring *ur = get_ring_by_number(data[0]);
|
|
|
|
if ((len != 0U) && (ur != NULL)) {
|
2022-11-18 00:59:11 +08:00
|
|
|
if ((data[0] < 2U) || (data[0] >= 4U) || safety_tx_lin_hook(data[0] - 2U, &data[1], len - 1U)) {
|
2022-08-03 19:11:52 +08:00
|
|
|
for (uint32_t i = 1; i < len; i++) {
|
|
|
|
while (!putc(ur, data[i])) {
|
2022-01-22 12:00:14 +08:00
|
|
|
// wait
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-03 19:11:52 +08:00
|
|
|
int comms_control_handler(ControlPacket_t *req, uint8_t *resp) {
|
2022-01-22 12:00:14 +08:00
|
|
|
unsigned int resp_len = 0;
|
|
|
|
uart_ring *ur = NULL;
|
|
|
|
timestamp_t t;
|
2023-02-14 19:16:15 +08:00
|
|
|
uint32_t time;
|
2022-11-11 13:34:43 +08:00
|
|
|
|
|
|
|
#ifdef DEBUG_COMMS
|
2022-11-30 14:55:10 +08:00
|
|
|
print("raw control request: "); hexdump(req, sizeof(ControlPacket_t)); print("\n");
|
|
|
|
print("- request "); puth(req->request); print("\n");
|
|
|
|
print("- param1 "); puth(req->param1); print("\n");
|
|
|
|
print("- param2 "); puth(req->param2); print("\n");
|
2022-11-11 13:34:43 +08:00
|
|
|
#endif
|
|
|
|
|
2022-08-03 19:11:52 +08:00
|
|
|
switch (req->request) {
|
2022-01-22 12:00:14 +08:00
|
|
|
// **** 0xa0: get rtc time
|
|
|
|
case 0xa0:
|
|
|
|
resp_len = get_rtc_pkt(resp);
|
|
|
|
break;
|
|
|
|
// **** 0xa1: set rtc year
|
|
|
|
case 0xa1:
|
|
|
|
t = rtc_get_time();
|
2022-08-03 19:11:52 +08:00
|
|
|
t.year = req->param1;
|
2022-01-22 12:00:14 +08:00
|
|
|
rtc_set_time(t);
|
|
|
|
break;
|
|
|
|
// **** 0xa2: set rtc month
|
|
|
|
case 0xa2:
|
|
|
|
t = rtc_get_time();
|
2022-08-03 19:11:52 +08:00
|
|
|
t.month = req->param1;
|
2022-01-22 12:00:14 +08:00
|
|
|
rtc_set_time(t);
|
|
|
|
break;
|
|
|
|
// **** 0xa3: set rtc day
|
|
|
|
case 0xa3:
|
|
|
|
t = rtc_get_time();
|
2022-08-03 19:11:52 +08:00
|
|
|
t.day = req->param1;
|
2022-01-22 12:00:14 +08:00
|
|
|
rtc_set_time(t);
|
|
|
|
break;
|
|
|
|
// **** 0xa4: set rtc weekday
|
|
|
|
case 0xa4:
|
|
|
|
t = rtc_get_time();
|
2022-08-03 19:11:52 +08:00
|
|
|
t.weekday = req->param1;
|
2022-01-22 12:00:14 +08:00
|
|
|
rtc_set_time(t);
|
|
|
|
break;
|
|
|
|
// **** 0xa5: set rtc hour
|
|
|
|
case 0xa5:
|
|
|
|
t = rtc_get_time();
|
2022-08-03 19:11:52 +08:00
|
|
|
t.hour = req->param1;
|
2022-01-22 12:00:14 +08:00
|
|
|
rtc_set_time(t);
|
|
|
|
break;
|
|
|
|
// **** 0xa6: set rtc minute
|
|
|
|
case 0xa6:
|
|
|
|
t = rtc_get_time();
|
2022-08-03 19:11:52 +08:00
|
|
|
t.minute = req->param1;
|
2022-01-22 12:00:14 +08:00
|
|
|
rtc_set_time(t);
|
|
|
|
break;
|
|
|
|
// **** 0xa7: set rtc second
|
|
|
|
case 0xa7:
|
|
|
|
t = rtc_get_time();
|
2022-08-03 19:11:52 +08:00
|
|
|
t.second = req->param1;
|
2022-01-22 12:00:14 +08:00
|
|
|
rtc_set_time(t);
|
|
|
|
break;
|
2023-02-14 19:16:15 +08:00
|
|
|
// **** 0xa8: get microsecond timer
|
|
|
|
case 0xa8:
|
|
|
|
time = microsecond_timer_get();
|
|
|
|
resp[0] = (time & 0x000000FFU);
|
|
|
|
resp[1] = ((time & 0x0000FF00U) >> 8U);
|
|
|
|
resp[2] = ((time & 0x00FF0000U) >> 16U);
|
|
|
|
resp[3] = ((time & 0xFF000000U) >> 24U);
|
|
|
|
resp_len = 4U;
|
|
|
|
break;
|
2022-01-22 12:00:14 +08:00
|
|
|
// **** 0xb0: set IR power
|
|
|
|
case 0xb0:
|
2022-08-03 19:11:52 +08:00
|
|
|
current_board->set_ir_power(req->param1);
|
2022-01-22 12:00:14 +08:00
|
|
|
break;
|
|
|
|
// **** 0xb1: set fan power
|
|
|
|
case 0xb1:
|
2022-08-18 11:43:49 +08:00
|
|
|
fan_set_power(req->param1);
|
2022-01-22 12:00:14 +08:00
|
|
|
break;
|
|
|
|
// **** 0xb2: get fan rpm
|
|
|
|
case 0xb2:
|
2022-08-18 11:43:49 +08:00
|
|
|
resp[0] = (fan_state.rpm & 0x00FFU);
|
|
|
|
resp[1] = ((fan_state.rpm & 0xFF00U) >> 8U);
|
2022-01-22 12:00:14 +08:00
|
|
|
resp_len = 2;
|
|
|
|
break;
|
|
|
|
// **** 0xb3: set phone power
|
|
|
|
case 0xb3:
|
2022-08-03 19:11:52 +08:00
|
|
|
current_board->set_phone_power(req->param1 > 0U);
|
2022-01-22 12:00:14 +08:00
|
|
|
break;
|
2023-01-14 02:59:58 +08:00
|
|
|
// **** 0xc0: reset communications
|
|
|
|
case 0xc0:
|
|
|
|
comms_can_reset();
|
|
|
|
break;
|
2022-01-22 12:00:14 +08:00
|
|
|
// **** 0xc1: get hardware type
|
|
|
|
case 0xc1:
|
|
|
|
resp[0] = hw_type;
|
|
|
|
resp_len = 1;
|
|
|
|
break;
|
2023-01-16 12:13:26 +08:00
|
|
|
// **** 0xc2: CAN health stats
|
2022-09-16 04:04:10 +08:00
|
|
|
case 0xc2:
|
|
|
|
COMPILE_TIME_ASSERT(sizeof(can_health_t) <= USBPACKET_MAX_SIZE);
|
|
|
|
if (req->param1 < 3U) {
|
2022-09-17 11:39:18 +08:00
|
|
|
can_health[req->param1].can_speed = (bus_config[req->param1].can_speed / 10U);
|
|
|
|
can_health[req->param1].can_data_speed = (bus_config[req->param1].can_data_speed / 10U);
|
|
|
|
can_health[req->param1].canfd_enabled = bus_config[req->param1].canfd_enabled;
|
|
|
|
can_health[req->param1].brs_enabled = bus_config[req->param1].brs_enabled;
|
2022-10-13 06:28:20 +08:00
|
|
|
can_health[req->param1].canfd_non_iso = bus_config[req->param1].canfd_non_iso;
|
2022-09-16 04:04:10 +08:00
|
|
|
resp_len = sizeof(can_health[req->param1]);
|
|
|
|
(void)memcpy(resp, &can_health[req->param1], resp_len);
|
|
|
|
}
|
|
|
|
break;
|
2023-01-16 12:13:26 +08:00
|
|
|
// **** 0xc3: fetch MCU UID
|
|
|
|
case 0xc3:
|
|
|
|
(void)memcpy(resp, ((uint8_t *)UID_BASE), 12);
|
|
|
|
resp_len = 12;
|
|
|
|
break;
|
|
|
|
// **** 0xd0: fetch serial (aka the provisioned dongle ID)
|
2022-01-22 12:00:14 +08:00
|
|
|
case 0xd0:
|
|
|
|
// addresses are OTP
|
2022-08-03 19:11:52 +08:00
|
|
|
if (req->param1 == 1U) {
|
2022-01-22 12:00:14 +08:00
|
|
|
(void)memcpy(resp, (uint8_t *)DEVICE_SERIAL_NUMBER_ADDRESS, 0x10);
|
|
|
|
resp_len = 0x10;
|
|
|
|
} else {
|
|
|
|
get_provision_chunk(resp);
|
|
|
|
resp_len = PROVISION_CHUNK_LEN;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// **** 0xd1: enter bootloader mode
|
|
|
|
case 0xd1:
|
|
|
|
// this allows reflashing of the bootstub
|
2022-08-03 19:11:52 +08:00
|
|
|
switch (req->param1) {
|
2022-01-22 12:00:14 +08:00
|
|
|
case 0:
|
|
|
|
// only allow bootloader entry on debug builds
|
|
|
|
#ifdef ALLOW_DEBUG
|
2022-11-30 14:55:10 +08:00
|
|
|
print("-> entering bootloader\n");
|
2022-01-22 12:00:14 +08:00
|
|
|
enter_bootloader_mode = ENTER_BOOTLOADER_MAGIC;
|
|
|
|
NVIC_SystemReset();
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case 1:
|
2022-11-30 14:55:10 +08:00
|
|
|
print("-> entering softloader\n");
|
2022-01-22 12:00:14 +08:00
|
|
|
enter_bootloader_mode = ENTER_SOFTLOADER_MAGIC;
|
|
|
|
NVIC_SystemReset();
|
|
|
|
break;
|
|
|
|
default:
|
2022-11-30 14:55:10 +08:00
|
|
|
print("Bootloader mode invalid\n");
|
2022-01-22 12:00:14 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// **** 0xd2: get health packet
|
|
|
|
case 0xd2:
|
|
|
|
resp_len = get_health_pkt(resp);
|
|
|
|
break;
|
|
|
|
// **** 0xd3: get first 64 bytes of signature
|
|
|
|
case 0xd3:
|
|
|
|
{
|
|
|
|
resp_len = 64;
|
|
|
|
char * code = (char*)_app_start;
|
|
|
|
int code_len = _app_start[0];
|
|
|
|
(void)memcpy(resp, &code[code_len], resp_len);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// **** 0xd4: get second 64 bytes of signature
|
|
|
|
case 0xd4:
|
|
|
|
{
|
|
|
|
resp_len = 64;
|
|
|
|
char * code = (char*)_app_start;
|
|
|
|
int code_len = _app_start[0];
|
|
|
|
(void)memcpy(resp, &code[code_len + 64], resp_len);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// **** 0xd6: get version
|
|
|
|
case 0xd6:
|
|
|
|
COMPILE_TIME_ASSERT(sizeof(gitversion) <= USBPACKET_MAX_SIZE);
|
|
|
|
(void)memcpy(resp, gitversion, sizeof(gitversion));
|
|
|
|
resp_len = sizeof(gitversion) - 1U;
|
|
|
|
break;
|
|
|
|
// **** 0xd8: reset ST
|
|
|
|
case 0xd8:
|
|
|
|
NVIC_SystemReset();
|
|
|
|
break;
|
|
|
|
// **** 0xd9: set ESP power
|
|
|
|
case 0xd9:
|
2022-08-03 19:11:52 +08:00
|
|
|
if (req->param1 == 1U) {
|
2022-01-22 12:00:14 +08:00
|
|
|
current_board->set_gps_mode(GPS_ENABLED);
|
2022-08-03 19:11:52 +08:00
|
|
|
} else if (req->param1 == 2U) {
|
2022-01-22 12:00:14 +08:00
|
|
|
current_board->set_gps_mode(GPS_BOOTMODE);
|
|
|
|
} else {
|
|
|
|
current_board->set_gps_mode(GPS_DISABLED);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// **** 0xda: reset ESP, with optional boot mode
|
|
|
|
case 0xda:
|
|
|
|
current_board->set_gps_mode(GPS_DISABLED);
|
|
|
|
delay(1000000);
|
2022-08-03 19:11:52 +08:00
|
|
|
if (req->param1 == 1U) {
|
2022-01-22 12:00:14 +08:00
|
|
|
current_board->set_gps_mode(GPS_BOOTMODE);
|
|
|
|
} else {
|
|
|
|
current_board->set_gps_mode(GPS_ENABLED);
|
|
|
|
}
|
|
|
|
delay(1000000);
|
|
|
|
current_board->set_gps_mode(GPS_ENABLED);
|
|
|
|
break;
|
|
|
|
// **** 0xdb: set GMLAN (white/grey) or OBD CAN (black) multiplexing mode
|
|
|
|
case 0xdb:
|
|
|
|
if(current_board->has_obd){
|
2022-08-03 19:11:52 +08:00
|
|
|
if (req->param1 == 1U) {
|
2022-01-22 12:00:14 +08:00
|
|
|
// Enable OBD CAN
|
|
|
|
current_board->set_can_mode(CAN_MODE_OBD_CAN2);
|
|
|
|
} else {
|
|
|
|
// Disable OBD CAN
|
|
|
|
current_board->set_can_mode(CAN_MODE_NORMAL);
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-03 19:11:52 +08:00
|
|
|
if (req->param1 == 1U) {
|
2022-01-22 12:00:14 +08:00
|
|
|
// GMLAN ON
|
2022-08-03 19:11:52 +08:00
|
|
|
if (req->param2 == 1U) {
|
2022-01-22 12:00:14 +08:00
|
|
|
can_set_gmlan(1);
|
2022-08-03 19:11:52 +08:00
|
|
|
} else if (req->param2 == 2U) {
|
2022-01-22 12:00:14 +08:00
|
|
|
can_set_gmlan(2);
|
|
|
|
} else {
|
2022-11-30 14:55:10 +08:00
|
|
|
print("Invalid bus num for GMLAN CAN set\n");
|
2022-01-22 12:00:14 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
can_set_gmlan(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// **** 0xdc: set safety mode
|
|
|
|
case 0xdc:
|
2022-08-03 19:11:52 +08:00
|
|
|
set_safety_mode(req->param1, (uint16_t)req->param2);
|
2022-01-22 12:00:14 +08:00
|
|
|
break;
|
|
|
|
// **** 0xdd: get healthpacket and CANPacket versions
|
|
|
|
case 0xdd:
|
|
|
|
resp[0] = HEALTH_PACKET_VERSION;
|
|
|
|
resp[1] = CAN_PACKET_VERSION;
|
2022-09-16 04:04:10 +08:00
|
|
|
resp[2] = CAN_HEALTH_PACKET_VERSION;
|
|
|
|
resp_len = 3;
|
2022-01-22 12:00:14 +08:00
|
|
|
break;
|
|
|
|
// **** 0xde: set can bitrate
|
|
|
|
case 0xde:
|
2022-11-30 07:56:43 +08:00
|
|
|
if ((req->param1 < PANDA_BUS_CNT) && is_speed_valid(req->param2, speeds, sizeof(speeds)/sizeof(speeds[0]))) {
|
2022-08-03 19:11:52 +08:00
|
|
|
bus_config[req->param1].can_speed = req->param2;
|
|
|
|
bool ret = can_init(CAN_NUM_FROM_BUS_NUM(req->param1));
|
2022-01-22 12:00:14 +08:00
|
|
|
UNUSED(ret);
|
|
|
|
}
|
|
|
|
break;
|
2022-03-25 05:31:31 +08:00
|
|
|
// **** 0xdf: set alternative experience
|
2022-01-22 12:00:14 +08:00
|
|
|
case 0xdf:
|
|
|
|
// you can only set this if you are in a non car safety mode
|
|
|
|
if (!is_car_safety_mode(current_safety_mode)) {
|
2022-08-03 19:11:52 +08:00
|
|
|
alternative_experience = req->param1;
|
2022-01-22 12:00:14 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
// **** 0xe0: uart read
|
|
|
|
case 0xe0:
|
2022-08-03 19:11:52 +08:00
|
|
|
ur = get_ring_by_number(req->param1);
|
2022-01-22 12:00:14 +08:00
|
|
|
if (!ur) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Remove this again and fix boardd code to hande the message bursts instead of single chars
|
|
|
|
if (ur == &uart_ring_gps) {
|
|
|
|
dma_pointer_handler(ur, DMA2_Stream5->NDTR);
|
|
|
|
}
|
|
|
|
|
|
|
|
// read
|
2022-08-03 19:11:52 +08:00
|
|
|
while ((resp_len < MIN(req->length, USBPACKET_MAX_SIZE)) &&
|
2022-01-22 12:00:14 +08:00
|
|
|
getc(ur, (char*)&resp[resp_len])) {
|
|
|
|
++resp_len;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// **** 0xe1: uart set baud rate
|
|
|
|
case 0xe1:
|
2022-08-03 19:11:52 +08:00
|
|
|
ur = get_ring_by_number(req->param1);
|
2022-01-22 12:00:14 +08:00
|
|
|
if (!ur) {
|
|
|
|
break;
|
|
|
|
}
|
2022-08-03 19:11:52 +08:00
|
|
|
uart_set_baud(ur->uart, req->param2);
|
2022-01-22 12:00:14 +08:00
|
|
|
break;
|
|
|
|
// **** 0xe2: uart set parity
|
|
|
|
case 0xe2:
|
2022-08-03 19:11:52 +08:00
|
|
|
ur = get_ring_by_number(req->param1);
|
2022-01-22 12:00:14 +08:00
|
|
|
if (!ur) {
|
|
|
|
break;
|
|
|
|
}
|
2022-08-03 19:11:52 +08:00
|
|
|
switch (req->param2) {
|
2022-01-22 12:00:14 +08:00
|
|
|
case 0:
|
|
|
|
// disable parity, 8-bit
|
|
|
|
ur->uart->CR1 &= ~(USART_CR1_PCE | USART_CR1_M);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
// even parity, 9-bit
|
|
|
|
ur->uart->CR1 &= ~USART_CR1_PS;
|
|
|
|
ur->uart->CR1 |= USART_CR1_PCE | USART_CR1_M;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
// odd parity, 9-bit
|
|
|
|
ur->uart->CR1 |= USART_CR1_PS;
|
|
|
|
ur->uart->CR1 |= USART_CR1_PCE | USART_CR1_M;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// **** 0xe4: uart set baud rate extended
|
|
|
|
case 0xe4:
|
2022-08-03 19:11:52 +08:00
|
|
|
ur = get_ring_by_number(req->param1);
|
2022-01-22 12:00:14 +08:00
|
|
|
if (!ur) {
|
|
|
|
break;
|
|
|
|
}
|
2022-08-03 19:11:52 +08:00
|
|
|
uart_set_baud(ur->uart, (int)req->param2*300);
|
2022-01-22 12:00:14 +08:00
|
|
|
break;
|
|
|
|
// **** 0xe5: set CAN loopback (for testing)
|
|
|
|
case 0xe5:
|
2022-08-03 19:11:52 +08:00
|
|
|
can_loopback = (req->param1 > 0U);
|
2022-01-22 12:00:14 +08:00
|
|
|
can_init_all();
|
|
|
|
break;
|
|
|
|
// **** 0xe7: set power save state
|
|
|
|
case 0xe7:
|
2022-08-03 19:11:52 +08:00
|
|
|
set_power_save_state(req->param1);
|
2022-01-22 12:00:14 +08:00
|
|
|
break;
|
|
|
|
// **** 0xf0: k-line/l-line wake-up pulse for KWP2000 fast initialization
|
|
|
|
case 0xf0:
|
|
|
|
if(current_board->has_lin) {
|
2022-08-03 19:11:52 +08:00
|
|
|
bool k = (req->param1 == 0U) || (req->param1 == 2U);
|
|
|
|
bool l = (req->param1 == 1U) || (req->param1 == 2U);
|
2022-01-22 12:00:14 +08:00
|
|
|
if (bitbang_wakeup(k, l)) {
|
|
|
|
resp_len = -1; // do not clear NAK yet (wait for bit banging to finish)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// **** 0xf1: Clear CAN ring buffer.
|
|
|
|
case 0xf1:
|
2022-08-03 19:11:52 +08:00
|
|
|
if (req->param1 == 0xFFFFU) {
|
2022-11-30 14:55:10 +08:00
|
|
|
print("Clearing CAN Rx queue\n");
|
2022-01-22 12:00:14 +08:00
|
|
|
can_clear(&can_rx_q);
|
2022-11-30 07:56:43 +08:00
|
|
|
} else if (req->param1 < PANDA_BUS_CNT) {
|
2022-11-30 14:55:10 +08:00
|
|
|
print("Clearing CAN Tx queue\n");
|
2022-08-03 19:11:52 +08:00
|
|
|
can_clear(can_queues[req->param1]);
|
2022-01-22 12:00:14 +08:00
|
|
|
} else {
|
2022-11-30 14:55:10 +08:00
|
|
|
print("Clearing CAN CAN ring buffer failed: wrong bus number\n");
|
2022-01-22 12:00:14 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
// **** 0xf2: Clear UART ring buffer.
|
|
|
|
case 0xf2:
|
|
|
|
{
|
2022-08-03 19:11:52 +08:00
|
|
|
uart_ring * rb = get_ring_by_number(req->param1);
|
2022-01-22 12:00:14 +08:00
|
|
|
if (rb != NULL) {
|
2022-11-30 14:55:10 +08:00
|
|
|
print("Clearing UART queue.\n");
|
2022-01-22 12:00:14 +08:00
|
|
|
clear_uart_buff(rb);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// **** 0xf3: Heartbeat. Resets heartbeat counter.
|
|
|
|
case 0xf3:
|
|
|
|
{
|
|
|
|
heartbeat_counter = 0U;
|
|
|
|
heartbeat_lost = false;
|
|
|
|
heartbeat_disabled = false;
|
2022-08-03 19:11:52 +08:00
|
|
|
heartbeat_engaged = (req->param1 == 1U);
|
2022-01-22 12:00:14 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// **** 0xf4: k-line/l-line 5 baud initialization
|
|
|
|
case 0xf4:
|
|
|
|
if(current_board->has_lin) {
|
2022-08-03 19:11:52 +08:00
|
|
|
bool k = (req->param1 == 0U) || (req->param1 == 2U);
|
|
|
|
bool l = (req->param1 == 1U) || (req->param1 == 2U);
|
|
|
|
uint8_t five_baud_addr = (req->param2 & 0xFFU);
|
2022-01-22 12:00:14 +08:00
|
|
|
if (bitbang_five_baud_addr(k, l, five_baud_addr)) {
|
|
|
|
resp_len = -1; // do not clear NAK yet (wait for bit banging to finish)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// **** 0xf6: set siren enabled
|
|
|
|
case 0xf6:
|
2022-08-03 19:11:52 +08:00
|
|
|
siren_enabled = (req->param1 != 0U);
|
2022-01-22 12:00:14 +08:00
|
|
|
break;
|
2023-01-11 07:09:51 +08:00
|
|
|
// **** 0xf7: set green led enabled
|
|
|
|
case 0xf7:
|
|
|
|
green_led_enabled = (req->param1 != 0U);
|
|
|
|
break;
|
2022-01-22 12:00:14 +08:00
|
|
|
// **** 0xf8: disable heartbeat checks
|
|
|
|
case 0xf8:
|
2022-08-18 13:42:18 +08:00
|
|
|
if (!is_car_safety_mode(current_safety_mode)) {
|
|
|
|
heartbeat_disabled = true;
|
|
|
|
}
|
2022-01-22 12:00:14 +08:00
|
|
|
break;
|
2022-10-13 06:28:20 +08:00
|
|
|
// **** 0xf9: set CAN FD data bitrate
|
2022-01-22 12:00:14 +08:00
|
|
|
case 0xf9:
|
2022-11-30 07:56:43 +08:00
|
|
|
if ((req->param1 < PANDA_CAN_CNT) &&
|
2022-09-17 14:51:55 +08:00
|
|
|
current_board->has_canfd &&
|
|
|
|
is_speed_valid(req->param2, data_speeds, sizeof(data_speeds)/sizeof(data_speeds[0]))) {
|
2022-08-03 19:11:52 +08:00
|
|
|
bus_config[req->param1].can_data_speed = req->param2;
|
|
|
|
bus_config[req->param1].canfd_enabled = (req->param2 >= bus_config[req->param1].can_speed);
|
|
|
|
bus_config[req->param1].brs_enabled = (req->param2 > bus_config[req->param1].can_speed);
|
|
|
|
bool ret = can_init(CAN_NUM_FROM_BUS_NUM(req->param1));
|
2022-01-22 12:00:14 +08:00
|
|
|
UNUSED(ret);
|
|
|
|
}
|
|
|
|
break;
|
2022-08-02 08:25:48 +08:00
|
|
|
// **** 0xfb: allow highest power saving mode (stop) to be entered
|
2022-03-08 06:24:04 +08:00
|
|
|
case 0xfb:
|
2022-08-02 08:25:48 +08:00
|
|
|
deepsleep_allowed = true;
|
2022-03-08 06:24:04 +08:00
|
|
|
break;
|
2022-10-13 06:28:20 +08:00
|
|
|
// **** 0xfc: set CAN FD non-ISO mode
|
|
|
|
case 0xfc:
|
2022-11-30 07:56:43 +08:00
|
|
|
if ((req->param1 < PANDA_CAN_CNT) && current_board->has_canfd) {
|
2022-10-13 06:28:20 +08:00
|
|
|
bus_config[req->param1].canfd_non_iso = (req->param2 != 0U);
|
|
|
|
bool ret = can_init(CAN_NUM_FROM_BUS_NUM(req->param1));
|
|
|
|
UNUSED(ret);
|
|
|
|
}
|
|
|
|
break;
|
2023-06-13 23:00:56 +08:00
|
|
|
// *** 0xfd: read logs
|
|
|
|
case 0xfd:
|
|
|
|
if (req->param1 == 1U) {
|
|
|
|
logging_find_read_index();
|
|
|
|
}
|
|
|
|
resp_len = logging_read(resp);
|
|
|
|
break;
|
2022-01-22 12:00:14 +08:00
|
|
|
default:
|
2022-11-30 14:55:10 +08:00
|
|
|
print("NO HANDLER ");
|
2022-08-03 19:11:52 +08:00
|
|
|
puth(req->request);
|
2022-11-30 14:55:10 +08:00
|
|
|
print("\n");
|
2022-01-22 12:00:14 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return resp_len;
|
|
|
|
}
|