--- name: automotive-ecu-systems description: > Automotive Ecu Systems expertise. Covers 8 topics: Bcm Body Control, Bms Battery Management, Domain Controller Integration, Ivi Infotainment Systems, Pdu Power Distribution. tags: [automotive, automotive-ecu-systems] --- # Automotive Ecu Systems ## Bcm Body Control # BCM (Body Control Module) - Comfort and Convenience Systems ## Overview The Body Control Unit (BCM) manages exterior/interior lighting, HVAC integration, door locks, windows, wipers, keyless entry, anti-theft systems, and power distribution across body electronics. This skill covers production-ready BCM development with LIN bus mastering. ## Core Responsibilities ### 1. Exterior/Interior Lighting Control ```c /* bcm_lighting_control.c - Comprehensive lighting management */ #include "bcm_lighting_control.h" #include #include #define PWM_FREQUENCY_HZ 1000 #define DIM_STEP_PERCENT 5 #define AUTO_HEADLIGHT_THRESHOLD_LUX 100 typedef enum { LIGHT_MODE_OFF = 0, LIGHT_MODE_PARKING, LIGHT_MODE_DAYTIME_RUNNING, LIGHT_MODE_LOW_BEAM, LIGHT_MODE_HIGH_BEAM, LIGHT_MODE_AUTO } HeadlightMode_t; typedef struct { bool left_turn_signal_active; bool right_turn_signal_active; bool hazard_active; uint8_t turn_signal_phase; /* 0-100% for flashing */ uint32_t last_toggle_ms; } TurnSignalState_t; typedef struct { HeadlightMode_t mode; uint8_t brightness_percent; bool high_beam_assist_active; bool adaptive_lighting_active; } HeadlightState_t; static TurnSignalState_t g_turn_signals = {0}; static HeadlightState_t g_headlights = {0}; void BCM_Lighting_Init(void) { /* Initialize PWM channels for LED control */ PWM_Init(PWM_CHANNEL_LEFT_HEADLIGHT, PWM_FREQUENCY_HZ); PWM_Init(PWM_CHANNEL_RIGHT_HEADLIGHT, PWM_FREQUENCY_HZ); PWM_Init(PWM_CHANNEL_DOME_LIGHT, PWM_FREQUENCY_HZ); PWM_Init(PWM_CHANNEL_AMBIENT_LIGHT, PWM_FREQUENCY_HZ); /* Set initial state */ g_headlights.mode = LIGHT_MODE_OFF; g_headlights.brightness_percent = 100; /* Load saved settings from EEPROM */ NvM_ReadBlock(NVM_BLOCK_LIGHTING_SETTINGS, &g_headlights); } void BCM_TurnSignals_Update(void) { uint32_t current_time = GetSystemTime_ms(); /* Flash at 1 Hz (500ms on, 500ms off) */ if ((current_time - g_turn_signals.last_toggle_ms) > 500) { g_turn_signals.turn_signal_phase = (g_turn_signals.turn_signal_phase == 0) ? 100 : 0; g_turn_signals.last_toggle_ms = current_time; } /* Left turn signal */ if (g_turn_signals.left_turn_signal_active || g_turn_signals.hazard_active) { PWM_SetDutyCycle(PWM_CHANNEL_LEFT_TURN_FRONT, g_turn_signals.turn_signal_phase); PWM_SetDutyCycle(PWM_CHANNEL_LEFT_TURN_REAR, g_turn_signals.turn_signal_phase); } else { PWM_SetDutyCycle(PWM_CHANNEL_LEFT_TURN_FRONT, 0); PWM_SetDutyCycle(PWM_CHANNEL_LEFT_TURN_REAR, 0); } /* Right turn signal */ if (g_turn_signals.right_turn_signal_active || g_turn_signals.hazard_active) { PWM_SetDutyCycle(PWM_CHANNEL_RIGHT_TURN_FRONT, g_turn_signals.turn_signal_phase); PWM_SetDutyCycle(PWM_CHANNEL_RIGHT_TURN_REAR, g_turn_signals.turn_signal_phase); } else { PWM_SetDutyCycle(PWM_CHANNEL_RIGHT_TURN_FRONT, 0); PWM_SetDutyCycle(PWM_CHANNEL_RIGHT_TURN_REAR, 0); } } void BCM_Headlights_Update(void) { uint16_t ambient_light_lux = BCM_LightSensor_Read(); switch (g_headlights.mode) { case LIGHT_MODE_OFF: PWM_SetDutyCycle(PWM_CHANNEL_LEFT_HEADLIGHT, 0); PWM_SetDutyCycle(PWM_CHANNEL_RIGHT_HEADLIGHT, 0); break; case LIGHT_MODE_PARKING: /* 20% brightness for parking lights */ PWM_SetDutyCycle(PWM_CHANNEL_LEFT_HEADLIGHT, 20); PWM_SetDutyCycle(PWM_CHANNEL_RIGHT_HEADLIGHT, 20); break; case LIGHT_MODE_DAYTIME_RUNNING: /* 50% brightness for DRL */ PWM_SetDutyCycle(PWM_CHANNEL_LEFT_HEADLIGHT, 50); PWM_SetDutyCycle(PWM_CHANNEL_RIGHT_HEADLIGHT, 50); break; case LIGHT_MODE_LOW_BEAM: PWM_SetDutyCycle(PWM_CHANNEL_LEFT_HEADLIGHT, g_headlights.brightness_percent); PWM_SetDutyCycle(PWM_CHANNEL_RIGHT_HEADLIGHT, g_headlights.brightness_percent); break; case LIGHT_MODE_HIGH_BEAM: /* Full brightness for high beam */ PWM_SetDutyCycle(PWM_CHANNEL_LEFT_HEADLIGHT, 100); PWM_SetDutyCycle(PWM_CHANNEL_RIGHT_HEADLIGHT, 100); PWM_SetDutyCycle(PWM_CHANNEL_HIGH_BEAM, 100); break; case LIGHT_MODE_AUTO: /* Automatic headlight control based on ambient light */ if (ambient_light_lux < AUTO_HEADLIGHT_THRESHOLD_LUX) { /* Dark: enable low beams */ g_headlights.mode = LIGHT_MODE_LOW_BEAM; } else { /* Bright: enable DRL only */ g_headlights.mode = LIGHT_MODE_DAYTIME_RUNNING; } break; } /* High beam assist: automatically switch to low beam when oncoming traffic detected */ if (g_headlights.high_beam_assist_active && g_headlights.mode == LIGHT_MODE_HIGH_BEAM) { bool oncoming_detected = ADAS_Camera_DetectOncomingVehicle(); if (oncoming_detected) { g_headlights.mode = LIGHT_MODE_LOW_BEAM; } } } /* Interior dome light with fade-in/fade-out */ void BCM_DomeLight_SetState(bool on, bool fade) { static uint8_t current_brightness = 0; uint8_t target_brightness = on ? 100 : 0; if (fade) { /* Fade gradually */ while (current_brightness != target_brightness) { if (current_brightness < target_brightness) { current_brightness += DIM_STEP_PERCENT; } else { current_brightness -= DIM_STEP_PERCENT; } PWM_SetDutyCycle(PWM_CHANNEL_DOME_LIGHT, current_brightness); OsTask_Sleep(50); /* 50ms steps for smooth fade */ } } else { /* Immediate switch */ current_brightness = target_brightness; PWM_SetDutyCycle(PWM_CHANNEL_DOME_LIGHT, current_brightness); } } ``` ### 2. Door Lock/Unlock and Keyless Entry ```c /* bcm_door_control.c - Central locking and keyless entry */ #include "bcm_door_control.h" #define KEYFOB_UNLOCK_TIMEOUT_MS 3000 #define AUTO_LOCK_SPEED_THRESHOLD_KPH 10 #define PASSIVE_ENTRY_RANGE_M 2.0 typedef enum { DOOR_FL = 0, DOOR_FR, DOOR_RL, DOOR_RR, DOOR_TRUNK, DOOR_COUNT } DoorID_t; typedef struct { bool locked; bool open; uint32_t last_lock_timestamp_ms; } DoorState_t; typedef struct { uint32_t keyfob_id; int8_t rssi_dbm; float distance_m; /* Estimated from RSSI */ bool authenticated; } KeyFobState_t; static DoorState_t g_doors[DOOR_COUNT] = {0}; static KeyFobState_t g_active_keyfob = {0}; void BCM_DoorControl_Init(void) { /* Initialize door lock actuators */ for (int i = 0; i < DOOR_COUNT; i++) { GPIO_ConfigOutput(DOOR_LOCK_PINS[i]); g_doors[i].locked = true; } /* Initialize door open sensors (switches) */ for (int i = 0; i < DOOR_COUNT; i++) { GPIO_ConfigInput(DOOR_SWITCH_PINS[i], GPIO_PULL_UP); } /* Initialize BLE for passive keyless entry */ BLE_Init(); BLE_StartAdvertising("VehicleKey"); } void BCM_DoorControl_LockAll(void) { for (int i = 0; i < DOOR_COUNT; i++) { if (!g_doors[i].locked) { /* Activate lock actuator (pulse for 500ms) */ GPIO_Set(DOOR_LOCK_PINS[i], true); OsTask_Sleep(500); GPIO_Set(DOOR_LOCK_PINS[i], false); g_doors[i].locked = true; g_doors[i].last_lock_timestamp_ms = GetSystemTime_ms(); } } /* Chirp horn once to confirm lock */ BCM_Horn_Chirp(1); /* Flash turn signals once */ BCM_TurnSignals_Flash(1); } void BCM_DoorControl_UnlockAll(void) { /* Unlock driver door first (common in luxury vehicles) */ BCM_DoorControl_UnlockSingle(DOOR_FL); /* Wait 2 seconds, then unlock all if button pressed again */ uint32_t start_time = GetSystemTime_ms(); while ((GetSystemTime_ms() - start_time) < 2000) { if (KeyFob_ButtonPressed(KEYFOB_BUTTON_UNLOCK)) { /* Second press: unlock all doors */ for (int i = 0; i < DOOR_COUNT; i++) { BCM_DoorControl_UnlockSingle((DoorID_t)i); } break; } OsTask_Sleep(10); } /* Flash turn signals twice */ BCM_TurnSignals_Flash(2); } void BCM_DoorControl_UnlockSingle(DoorID_t door) { if (g_doors[door].locked) { /* Activate unlock actuator */ GPIO_Set(DOOR_UNLOCK_PINS[door], true); OsTask_Sleep(500); GPIO_Set(DOOR_UNLOCK_PINS[door], false); g_doors[door].locked = false; } } /* Passive keyless entry: unlock when approaching with authenticated key */ void BCM_PassiveEntry_Update(void) { /* Scan for BLE key fobs */ if (BLE_ScanForDevice(g_active_keyfob.keyfob_id)) { g_active_keyfob.rssi_dbm = BLE_GetRSSI(); /* Estimate distance from RSSI (simplified model) */ g_active_keyfob.distance_m = pow(10, (-59 - g_active_keyfob.rssi_dbm) / (10 * 2.0)); /* Authenticate key fob */ if (!g_active_keyfob.authenticated) { uint8_t challenge[16]; uint8_t response[16]; BCM_Crypto_GenerateChallenge(challenge); BLE_SendChallenge(challenge); if (BLE_ReceiveResponse(response) && BCM_Crypto_VerifyResponse(challenge, response)) { g_active_keyfob.authenticated = true; } } /* Unlock if authenticated and within range */ if (g_active_keyfob.authenticated && g_active_keyfob.distance_m < PASSIVE_ENTRY_RANGE_M) { /* Check if door handle touched (capacitive sensor) */ if (GPIO_Read(DOOR_HANDLE_SENSOR_FL)) { BCM_DoorControl_UnlockSingle(DOOR_FL); } } } } /* Auto-lock when driving */ void BCM_AutoLock_Update(void) { uint16_t vehicle_speed = VCU_GetVehicleSpeed_kph(); if (vehicle_speed > AUTO_LOCK_SPEED_THRESHOLD_KPH) { /* Vehicle is moving: auto-lock all doors */ bool any_unlocked = false; for (int i = 0; i < DOOR_COUNT; i++) { if (!g_doors[i].locked) { any_unlocked = true; break; } } if (any_unlocked) { BCM_DoorControl_LockAll(); } } } ``` ### 3. Window Control with Anti-Pinch ```c /* bcm_window_control.c - Power window management with anti-pinch */ #include "bcm_window_control.h" #define WINDOW_FL 0 #define WINDOW_FR 1 #define WINDOW_RL 2 #define WINDOW_RR 3 #define WINDOW_COUNT 4 #define ANTI_PINCH_FORCE_THRESHOLD_N 100 #define WINDOW_POSITION_SAMPLES 10 typedef enum { WINDOW_STATE_STOPPED = 0, WINDOW_STATE_MOVING_UP, WINDOW_STATE_MOVING_DOWN, WINDOW_STATE_PINCH_DETECTED } WindowState_t; typedef struct { WindowState_t state; uint8_t position_percent; /* 0=closed, 100=fully open */ uint16_t motor_current_ma; bool one_touch_up_active; bool one_touch_down_active; } WindowControl_t; static WindowControl_t g_windows[WINDOW_COUNT] = {0}; void BCM_WindowControl_Init(void) { /* Initialize window motor drivers (H-bridge) */ for (int i = 0; i < WINDOW_COUNT; i++) { GPIO_ConfigOutput(WINDOW_MOTOR_UP_PINS[i]); GPIO_ConfigOutput(WINDOW_MOTOR_DOWN_PINS[i]); } /* Initialize window position sensors (Hall effect) */ for (int i = 0; i < WINDOW_COUNT; i++) { ADC_ConfigChannel(WINDOW_POSITION_ADC_CHANNELS[i]); } /* Initialize current sensing for anti-pinch */ for (int i = 0; i < WINDOW_COUNT; i++) { ADC_ConfigChannel(WINDOW_CURRENT_ADC_CHANNELS[i]); } } void BCM_Window_MoveUp(uint8_t window_id) { if (window_id >= WINDOW_COUNT) return; WindowControl_t* window = &g_windows[window_id]; if (window->position_percent == 0) { return; /* Already fully closed */ } /* Activate motor upward */ GPIO_Set(WINDOW_MOTOR_UP_PINS[window_id], true); GPIO_Set(WINDOW_MOTOR_DOWN_PINS[window_id], false); window->state = WINDOW_STATE_MOVING_UP; } void BCM_Window_MoveDown(uint8_t window_id) { if (window_id >= WINDOW_COUNT) return; WindowControl_t* window = &g_windows[window_id]; if (window->position_percent == 100) { return; /* Already fully open */ } /* Activate motor downward */ GPIO_Set(WINDOW_MOTOR_UP_PINS[window_id], false); GPIO_Set(WINDOW_MOTOR_DOWN_PINS[window_id], true); window->state = WINDOW_STATE_MOVING_DOWN; } void BCM_Window_Stop(uint8_t window_id) { if (window_id >= WINDOW_COUNT) return; /* Stop motor */ GPIO_Set(WINDOW_MOTOR_UP_PINS[window_id], false); GPIO_Set(WINDOW_MOTOR_DOWN_PINS[window_id], false); g_windows[window_id].state = WINDOW_STATE_STOPPED; } /* Anti-pinch detection: monitor motor current during closing */ void BCM_Window_AntiPinchUpdate(uint8_t window_id) { WindowControl_t* window = &g_windows[window_id]; if (window->state != WINDOW_STATE_MOVING_UP) { return; /* Only check during closing */ } /* Read motor current */ uint16_t adc_value = ADC_Read(WINDOW_CURRENT_ADC_CHANNELS[window_id]); window->motor_current_ma = (adc_value * 5000) / 4096; /* 12-bit ADC, 0-5A range */ /* Detect excessive current (indicates obstruction) */ if (window->motor_current_ma > ANTI_PINCH_FORCE_THRESHOLD_N) { /* Pinch detected: reverse window */ window->state = WINDOW_STATE_PINCH_DETECTED; BCM_Window_Stop(window_id); OsTask_Sleep(100); /* Move down slightly to release obstruction */ GPIO_Set(WINDOW_MOTOR_DOWN_PINS[window_id], true); OsTask_Sleep(500); GPIO_Set(WINDOW_MOTOR_DOWN_PINS[window_id], false); window->state = WINDOW_STATE_STOPPED; /* Log event */ DTC_SetFault(DTC_WINDOW_ANTI_PINCH_TRIGGERED + window_id); } } /* One-touch up/down */ void BCM_Window_OneTouchUp(uint8_t window_id) { g_windows[window_id].one_touch_up_active = true; while (g_windows[window_id].position_percent > 0 && g_windows[window_id].state != WINDOW_STATE_PINCH_DETECTED) { BCM_Window_MoveUp(window_id); BCM_Window_UpdatePosition(window_id); BCM_Window_AntiPinchUpdate(window_id); OsTask_Sleep(10); } BCM_Window_Stop(window_id); g_windows[window_id].one_touch_up_active = false; } ``` ### 4. LIN Bus Mastering (Door Modules) ```c /* bcm_lin_master.c - LIN bus control for door modules */ #include "bcm_lin_master.h" #define LIN_BAUDRATE 19200 #define LIN_BREAK_DURATION_US 750 #define LIN_FRAME_TIMEOUT_MS 50 typedef struct { uint8_t frame_id; uint8_t data[8]; uint8_t length; uint8_t checksum; } LINFrame_t; /* Door module addresses */ #define LIN_DOOR_FL_ID 0x01 #define LIN_DOOR_FR_ID 0x02 #define LIN_DOOR_RL_ID 0x03 #define LIN_DOOR_RR_ID 0x04 void BCM_LIN_Init(void) { /* Configure UART for LIN */ UART_Init(LIN_UART_PORT, LIN_BAUDRATE); UART_SetMode(LIN_UART_PORT, UART_MODE_LIN); } void BCM_LIN_SendBreak(void) { /* Generate LIN break field (dominant for 750µs) */ GPIO_Set(LIN_TX_PIN, false); usleep(LIN_BREAK_DURATION_US); GPIO_Set(LIN_TX_PIN, true); } bool BCM_LIN_SendFrame(const LINFrame_t* frame) { /* Send break + sync byte + frame ID */ BCM_LIN_SendBreak(); UART_WriteByte(LIN_UART_PORT, 0x55); /* Sync byte */ UART_WriteByte(LIN_UART_PORT, frame->frame_id); /* Send data */ for (int i = 0; i < frame->length; i++) { UART_WriteByte(LIN_UART_PORT, frame->data[i]); } /* Send checksum */ UART_WriteByte(LIN_UART_PORT, frame->checksum); return true; } /* Command door module to lock/unlock */ void BCM_LIN_DoorLockCommand(uint8_t door_module_id, bool lock) { LINFrame_t frame; frame.frame_id = door_module_id; frame.length = 2; frame.data[0] = lock ? 0x01 : 0x02; /* 0x01=lock, 0x02=unlock */ frame.data[1] = 0x00; frame.checksum = BCM_LIN_CalculateChecksum(&frame); BCM_LIN_SendFrame(&frame); } /* Read door status from LIN module */ bool BCM_LIN_ReadDoorStatus(uint8_t door_module_id, bool* door_open, bool* window_position) { LINFrame_t request; request.frame_id = door_module_id | 0x40; /* Read request */ request.length = 0; request.checksum = BCM_LIN_CalculateChecksum(&request); BCM_LIN_SendFrame(&request); /* Wait for response */ LINFrame_t response; if (BCM_LIN_ReceiveFrame(&response, LIN_FRAME_TIMEOUT_MS)) { *door_open = (response.data[0] & 0x01) != 0; *window_position = response.data[1]; return true; } return false; } ``` ## BCM CAN Database (DBC) ``` VERSION "" NS_ : BS_: BU_: BCM VCU IVI /* BCM Lighting Status */ BO_ 512 BCM_LightingStatus: 8 BCM SG_ BCM_HeadlightMode : 0|8@1+ (0,0) [0|5] "" IVI SG_ BCM_LeftTurnSignal : 8|1@1+ (0,0) [0|1] "" VCU,IVI SG_ BCM_RightTurnSignal : 9|1@1+ (0,0) [0|1] "" VCU,IVI SG_ BCM_HazardActive : 10|1@1+ (0,0) [0|1] "" VCU,IVI SG_ BCM_HighBeamActive : 11|1@1+ (0,0) [0|1] "" VCU,IVI SG_ BCM_BrakeLight : 12|1@1+ (0,0) [0|1] "" VCU /* BCM Door Status */ BO_ 513 BCM_DoorStatus: 8 BCM SG_ BCM_DoorLocked_FL : 0|1@1+ (0,0) [0|1] "" IVI SG_ BCM_DoorLocked_FR : 1|1@1+ (0,0) [0|1] "" IVI SG_ BCM_DoorLocked_RL : 2|1@1+ (0,0) [0|1] "" IVI SG_ BCM_DoorLocked_RR : 3|1@1+ (0,0) [0|1] "" IVI SG_ BCM_DoorOpen_FL : 8|1@1+ (0,0) [0|1] "" VCU,IVI SG_ BCM_DoorOpen_FR : 9|1@1+ (0,0) [0|1] "" VCU,IVI SG_ BCM_DoorOpen_RL : 10|1@1+ (0,0) [0|1] "" VCU,IVI SG_ BCM_DoorOpen_RR : 11|1@1+ (0,0) [0|1] "" VCU,IVI SG_ BCM_TrunkOpen : 12|1@1+ (0,0) [0|1] "" VCU,IVI VAL_ 512 BCM_HeadlightMode 0 "Off" 1 "Parking" 2 "DRL" 3 "LowBeam" 4 "HighBeam" 5 "Auto"; ``` ## References - SAE J1850: Class B Data Communication Network Interface - ISO 17987: Local Interconnect Network (LIN) Protocol - IEC 60529: IP Rating (Ingress Protection) - ECE R48: Installation of lighting devices ## Common Issues - LIN bus communication errors due to incorrect timing - Anti-pinch false triggers from motor current spikes - Keyless entry authentication failures - Door lock actuators jamming in cold weather - PWM flicker at low brightness levels --- ## Bms Battery Management # BMS (Battery Management System) for EVs/HEVs ## Overview The Battery Management System (BMS) monitors cell voltages, estimates SOC/SOH, performs cell balancing, manages thermal systems, controls contactors, and ensures ISO 26262 ASIL-D safety compliance for high-voltage battery packs. ## Core Responsibilities ### 1. Cell Voltage Monitoring ```c /* bms_cell_monitoring.c - Multi-cell voltage acquisition */ #include "bms_cell_monitoring.h" #define MAX_CELLS_PER_MODULE 12 #define MAX_MODULES 10 #define TOTAL_CELLS (MAX_CELLS_PER_MODULE * MAX_MODULES) #define CELL_OVERVOLTAGE_MV 4200 #define CELL_UNDERVOLTAGE_MV 2500 typedef struct { uint16_t voltage_mv; int16_t temperature_c_x10; /* 0.1°C resolution */ bool balancing_active; } CellData_t; typedef struct { CellData_t cells[MAX_CELLS_PER_MODULE]; uint8_t module_id; int16_t module_temperature_c_x10; bool communication_ok; } ModuleData_t; static ModuleData_t g_modules[MAX_MODULES]; /* LTC6811 Battery Monitor IC interface */ void BMS_CellMonitoring_Init(void) { /* Initialize SPI for LTC6811 daisy chain */ SPI_Init(SPI_BMS, 1000000); /* 1 MHz */ /* Wake up all LTC6811 ICs */ BMS_LTC6811_Wakeup(); /* Configure cell measurement mode */ uint8_t config[6] = { 0xF8, /* GPIO pull-downs off, REFON=1 */ 0x00, /* Discharge switches off */ 0x00, 0x00, 0x00, 0x00 }; BMS_LTC6811_WriteConfig(config); } void BMS_CellMonitoring_Update(void) { /* Start cell voltage conversion (all cells, all modules) */ BMS_LTC6811_StartCellConversion(ADC_MODE_NORMAL, ADC_FILTER_7KHZ); /* Wait for conversion (2.3ms for normal mode) */ OsTask_Sleep(3); /* Read cell voltages from all modules */ for (uint8_t module = 0; module < MAX_MODULES; module++) { uint16_t cell_voltages[MAX_CELLS_PER_MODULE]; if (BMS_LTC6811_ReadCellVoltages(module, cell_voltages)) { for (uint8_t cell = 0; cell < MAX_CELLS_PER_MODULE; cell++) { g_modules[module].cells[cell].voltage_mv = cell_voltages[cell] / 10; /* 100µV resolution */ /* Check overvoltage/undervoltage */ if (cell_voltages[cell] > CELL_OVERVOLTAGE_MV) { BMS_Fault_SetOvervoltage(module, cell); } if (cell_voltages[cell] < CELL_UNDERVOLTAGE_MV) { BMS_Fault_SetUndervoltage(module, cell); } } g_modules[module].communication_ok = true; } else { g_modules[module].communication_ok = false; BMS_Fault_SetCommunicationLoss(module); } } /* Read temperatures via GPIO (NTC thermistors) */ BMS_LTC6811_ReadGPIO(); } uint16_t BMS_GetMinCellVoltage_mV(void) { uint16_t min_voltage = 0xFFFF; for (uint8_t mod = 0; mod < MAX_MODULES; mod++) { for (uint8_t cell = 0; cell < MAX_CELLS_PER_MODULE; cell++) { if (g_modules[mod].cells[cell].voltage_mv < min_voltage) { min_voltage = g_modules[mod].cells[cell].voltage_mv; } } } return min_voltage; } uint16_t BMS_GetMaxCellVoltage_mV(void) { uint16_t max_voltage = 0; for (uint8_t mod = 0; mod < MAX_MODULES; mod++) { for (uint8_t cell = 0; cell < MAX_CELLS_PER_MODULE; cell++) { if (g_modules[mod].cells[cell].voltage_mv > max_voltage) { max_voltage = g_modules[mod].cells[cell].voltage_mv; } } } return max_voltage; } ``` ### 2. SOC/SOH Estimation (Coulomb Counting + Kalman Filter) ```c /* bms_soc_estimation.c - State of Charge / State of Health algorithms */ #include "bms_soc_estimation.h" #include #define BATTERY_CAPACITY_AH 75.0 #define COULOMB_EFFICIENCY 0.98 /* Charge efficiency */ typedef struct { float soc_percent; /* 0-100% */ float soh_percent; /* 0-100%, degrades over time */ float coulomb_count_ah; /* Accumulated amp-hours */ float ocv_voltage_v; /* Open circuit voltage */ uint32_t cycle_count; /* Full charge/discharge cycles */ float remaining_capacity_ah; } SOCState_t; static SOCState_t g_soc_state = { .soc_percent = 50.0, .soh_percent = 100.0, .coulomb_count_ah = BATTERY_CAPACITY_AH / 2.0, .remaining_capacity_ah = BATTERY_CAPACITY_AH }; /* OCV-SOC lookup table (Open Circuit Voltage to SOC mapping) */ static const struct { float voltage_v; float soc_percent; } OCV_SOC_TABLE[] = { {3.27, 0.0}, {3.61, 10.0}, {3.69, 20.0}, {3.71, 30.0}, {3.73, 40.0}, {3.77, 50.0}, {3.83, 60.0}, {3.92, 70.0}, {4.01, 80.0}, {4.08, 90.0}, {4.20, 100.0} }; void BMS_SOC_Init(void) { /* Load last known SOC from EEPROM */ NvM_ReadBlock(NVM_BLOCK_SOC_STATE, &g_soc_state); /* Initialize Kalman filter for SOC estimation */ BMS_KalmanFilter_Init(); } void BMS_SOC_Update(float current_a, uint32_t delta_time_ms) { /* Coulomb counting: integrate current over time */ float delta_time_h = delta_time_ms / 3600000.0; float delta_ah = current_a * delta_time_h; /* Positive current = discharge, negative = charge */ if (current_a > 0) { g_soc_state.coulomb_count_ah -= delta_ah; } else { g_soc_state.coulomb_count_ah -= delta_ah * COULOMB_EFFICIENCY; } /* Clamp to capacity limits */ if (g_soc_state.coulomb_count_ah < 0) { g_soc_state.coulomb_count_ah = 0; } if (g_soc_state.coulomb_count_ah > g_soc_state.remaining_capacity_ah) { g_soc_state.coulomb_count_ah = g_soc_state.remaining_capacity_ah; } /* Calculate SOC from coulomb count */ g_soc_state.soc_percent = (g_soc_state.coulomb_count_ah / g_soc_state.remaining_capacity_ah) * 100.0; /* Kalman filter fusion with OCV-based SOC (when current near zero) */ if (fabs(current_a) < 1.0) { /* Low current: use OCV */ float pack_voltage_v = BMS_GetPackVoltage() / 1000.0; float avg_cell_voltage_v = pack_voltage_v / TOTAL_CELLS; float ocv_soc = BMS_SOC_LookupOCV(avg_cell_voltage_v); /* Kalman filter update */ g_soc_state.soc_percent = BMS_KalmanFilter_Update( g_soc_state.soc_percent, ocv_soc); } /* Persist SOC every 1% change */ static float last_saved_soc = 0; if (fabs(g_soc_state.soc_percent - last_saved_soc) > 1.0) { NvM_WriteBlock(NVM_BLOCK_SOC_STATE, &g_soc_state); last_saved_soc = g_soc_state.soc_percent; } } float BMS_SOC_LookupOCV(float voltage_v) { /* Linear interpolation in OCV-SOC table */ for (int i = 0; i < (sizeof(OCV_SOC_TABLE) / sizeof(OCV_SOC_TABLE[0])) - 1; i++) { if (voltage_v >= OCV_SOC_TABLE[i].voltage_v && voltage_v <= OCV_SOC_TABLE[i+1].voltage_v) { float v_range = OCV_SOC_TABLE[i+1].voltage_v - OCV_SOC_TABLE[i].voltage_v; float soc_range = OCV_SOC_TABLE[i+1].soc_percent - OCV_SOC_TABLE[i].soc_percent; float v_delta = voltage_v - OCV_SOC_TABLE[i].voltage_v; return OCV_SOC_TABLE[i].soc_percent + (v_delta / v_range) * soc_range; } } return 50.0; /* Default fallback */ } /* SOH estimation based on capacity fade */ void BMS_SOH_Update(void) { /* Detect full charge cycle: SOC goes 100% -> 0% -> 100% */ static bool charging = false; static bool discharged = false; if (g_soc_state.soc_percent > 99.0 && !charging) { charging = true; if (discharged) { /* Full cycle completed */ g_soc_state.cycle_count++; /* Estimate capacity fade: 80% at 1000 cycles (linear model) */ g_soc_state.remaining_capacity_ah = BATTERY_CAPACITY_AH * (1.0 - (g_soc_state.cycle_count / 5000.0)); g_soc_state.soh_percent = (g_soc_state.remaining_capacity_ah / BATTERY_CAPACITY_AH) * 100.0; discharged = false; } } if (g_soc_state.soc_percent < 5.0) { discharged = true; charging = false; } } ``` ### 3. Cell Balancing (Active/Passive) ```c /* bms_cell_balancing.c - Cell voltage equalization */ #include "bms_cell_balancing.h" #define BALANCE_THRESHOLD_MV 10 /* Start balancing if cell delta > 10mV */ #define BALANCE_TARGET_MV 5 /* Stop when delta < 5mV */ #define MAX_BALANCE_CURRENT_MA 200 void BMS_CellBalancing_Update(void) { uint16_t min_voltage = BMS_GetMinCellVoltage_mV(); uint16_t max_voltage = BMS_GetMaxCellVoltage_mV(); if ((max_voltage - min_voltage) < BALANCE_THRESHOLD_MV) { /* Cells well balanced: disable all balancing */ BMS_CellBalancing_DisableAll(); return; } /* Passive balancing: discharge high cells through resistors */ for (uint8_t mod = 0; mod < MAX_MODULES; mod++) { uint16_t balance_mask = 0; for (uint8_t cell = 0; cell < MAX_CELLS_PER_MODULE; cell++) { uint16_t cell_voltage = g_modules[mod].cells[cell].voltage_mv; /* Balance if above minimum + threshold */ if (cell_voltage > (min_voltage + BALANCE_TARGET_MV)) { balance_mask |= (1 << cell); g_modules[mod].cells[cell].balancing_active = true; } else { g_modules[mod].cells[cell].balancing_active = false; } } /* Write balance control register to LTC6811 */ BMS_LTC6811_SetBalancing(mod, balance_mask); } } ``` ### 4. Contactor Control (Safety-Critical) ```c /* bms_contactor_control.c - High-voltage contactor sequencing */ #include "bms_contactor_control.h" #define PRECHARGE_TIMEOUT_MS 5000 #define PRECHARGE_THRESHOLD_PERCENT 95 typedef enum { CONTACTOR_STATE_OPEN = 0, CONTACTOR_STATE_PRECHARGING, CONTACTOR_STATE_CLOSED, CONTACTOR_STATE_FAULT } ContactorState_t; static ContactorState_t g_contactor_state = CONTACTOR_STATE_OPEN; void BMS_Contactor_Close(void) { /* Safety checks before closing */ if (!BMS_Safety_PreCloseCheck()) { g_contactor_state = CONTACTOR_STATE_FAULT; return; } /* Step 1: Close negative contactor */ GPIO_Set(GPIO_CONTACTOR_NEGATIVE, true); OsTask_Sleep(50); /* Step 2: Precharge positive side through resistor */ g_contactor_state = CONTACTOR_STATE_PRECHARGING; GPIO_Set(GPIO_PRECHARGE_RELAY, true); uint32_t start_time = GetSystemTime_ms(); uint16_t pack_voltage_v = BMS_GetPackVoltage(); uint16_t link_voltage_v = ADC_ReadHVLinkVoltage(); /* Wait for DC link to charge to 95% of pack voltage */ while ((GetSystemTime_ms() - start_time) < PRECHARGE_TIMEOUT_MS) { link_voltage_v = ADC_ReadHVLinkVoltage(); if (link_voltage_v > (pack_voltage_v * PRECHARGE_THRESHOLD_PERCENT / 100)) { break; /* Precharge complete */ } OsTask_Sleep(10); } if (link_voltage_v < (pack_voltage_v * PRECHARGE_THRESHOLD_PERCENT / 100)) { /* Precharge timeout: fault */ GPIO_Set(GPIO_PRECHARGE_RELAY, false); GPIO_Set(GPIO_CONTACTOR_NEGATIVE, false); g_contactor_state = CONTACTOR_STATE_FAULT; DTC_SetFault(DTC_PRECHARGE_TIMEOUT); return; } /* Step 3: Close positive contactor */ GPIO_Set(GPIO_CONTACTOR_POSITIVE, true); OsTask_Sleep(50); /* Step 4: Open precharge relay */ GPIO_Set(GPIO_PRECHARGE_RELAY, false); g_contactor_state = CONTACTOR_STATE_CLOSED; } void BMS_Contactor_Open(void) { /* Open positive first, then negative */ GPIO_Set(GPIO_CONTACTOR_POSITIVE, false); OsTask_Sleep(50); GPIO_Set(GPIO_CONTACTOR_NEGATIVE, false); g_contactor_state = CONTACTOR_STATE_OPEN; } ``` ## BMS CAN Database (DBC) ``` VERSION "" NS_ : BS_: BU_: BMS VCU MCU /* BMS Battery Status */ BO_ 768 BMS_BatteryStatus: 8 BMS SG_ BMS_PackVoltage_V : 0|16@1+ (0.1,0) [0|600] "V" VCU,MCU SG_ BMS_PackCurrent_A : 16|16@1- (0.1,-320) [-320|320] "A" VCU,MCU SG_ BMS_SOC_percent : 32|8@1+ (0.5,0) [0|100] "%" VCU,MCU SG_ BMS_SOH_percent : 40|8@1+ (0.5,0) [0|100] "%" VCU SG_ BMS_MaxCellTemp_C : 48|8@1+ (1,-40) [-40|100] "C" VCU,MCU SG_ BMS_ContactorState : 56|8@1+ (0,0) [0|3] "" VCU,MCU VAL_ 768 BMS_ContactorState 0 "Open" 1 "Precharging" 2 "Closed" 3 "Fault"; ``` ## ISO 26262 ASIL-D Safety Mechanisms - Dual voltage measurement paths with cross-checking - Watchdog timer for contactor control - Cell voltage plausibility checks - Safe state transition on fault detection ## References - ISO 26262: Functional Safety for Road Vehicles - UL 2580: Batteries for Use in Electric Vehicles - IEC 62133: Safety Requirements for Portable Sealed Secondary Cells - SAE J2464: EV Battery Systems Crashworthiness ## Common Issues - SOC drift from coulomb counting errors - Cell balancing inefficiency at low SOC - Precharge relay welding from inrush current - Temperature sensor failures affecting thermal management --- ## Domain Controller Integration # Domain Controller Architecture - Next-Gen Vehicle E/E Architecture ## Overview Domain Controller architecture centralizes ECU functions into fewer, more powerful computing platforms: Chassis Domain, Powertrain Domain, Body/Comfort Domain, ADAS Domain, with cross-domain communication via service-oriented architecture (SOA) and resource sharing. ## Domain Controller Concepts ### 1. Chassis Domain Controller ```c /* chassis_domain_controller.c - Integrated chassis functions */ #include "chassis_domain.h" /* Consolidated functions: ESC, ABS, TCS, EPS, ADAS braking */ typedef struct { /* Electronic Stability Control */ bool esc_active; float yaw_rate_deg_s; float lateral_acceleration_g; /* Anti-lock Braking System */ uint8_t wheel_speeds_kph[4]; bool abs_active[4]; /* Traction Control System */ bool tcs_active; uint16_t tcs_torque_reduction_nm; /* Electric Power Steering */ float steering_angle_deg; float steering_torque_nm; /* ADAS Braking Interface */ bool adas_brake_request; float adas_decel_mps2; } ChassisDomain_t; static ChassisDomain_t g_chassis = {0}; void ChassisDomain_Main_10ms(void) { /* Read sensors from CAN/FlexRay */ ChassisDomain_ReadSensors(); /* ESC control loop */ ChassisDomain_ESC_Update(); /* ABS control per wheel */ for (int i = 0; i < 4; i++) { ChassisDomain_ABS_UpdateWheel(i); } /* TCS integration with powertrain domain */ if (g_chassis.tcs_active) { /* Send torque reduction request to powertrain domain */ SOMEIP_SendRequest(POWERTRAIN_DOMAIN_SERVICE_ID, METHOD_REDUCE_TORQUE, &g_chassis.tcs_torque_reduction_nm, sizeof(uint16_t)); } /* ADAS brake arbitration */ if (g_chassis.adas_brake_request) { ChassisDomain_ADAS_BrakeControl(); } /* Publish chassis status to Ethernet backbone */ ChassisDomain_PublishStatus(); } void ChassisDomain_ESC_Update(void) { /* Read IMU (gyroscope + accelerometer) */ float yaw_rate = IMU_GetYawRate(); float lat_accel = IMU_GetLateralAcceleration(); /* Calculate desired yaw rate from steering angle */ float desired_yaw = (g_chassis.steering_angle_deg * VCU_GetVehicleSpeed_kph()) / 15.0; /* ESC intervention if yaw error exceeds threshold */ float yaw_error = desired_yaw - yaw_rate; if (fabs(yaw_error) > 5.0) { g_chassis.esc_active = true; /* Apply differential braking to correct yaw */ if (yaw_error > 0) { /* Understeer: brake inside rear wheel */ ChassisDomain_ApplyBrake(WHEEL_RL, 30); } else { /* Oversteer: brake outside front wheel */ ChassisDomain_ApplyBrake(WHEEL_FL, 30); } /* Reduce engine torque */ SOMEIP_SendRequest(POWERTRAIN_DOMAIN_SERVICE_ID, METHOD_REDUCE_TORQUE, &(uint16_t){100}, 2); } else { g_chassis.esc_active = false; } } ``` ### 2. Powertrain Domain Controller ```c /* powertrain_domain_controller.c - EV powertrain integration */ #include "powertrain_domain.h" /* Consolidated: VCU, BMS, MCU functions */ typedef struct { /* Motor control */ int16_t motor_torque_cmd_nm; uint16_t motor_speed_rpm; float motor_temperature_c; /* Battery management */ float battery_soc_percent; uint16_t battery_voltage_v; float battery_current_a; /* Thermal management */ bool cooling_pump_active; uint8_t radiator_fan_speed_percent; } PowertrainDomain_t; static PowertrainDomain_t g_powertrain = {0}; void PowertrainDomain_Main_10ms(void) { /* Motor control */ PowertrainDomain_MotorControl(); /* Battery management */ PowertrainDomain_BMS_Update(); /* Thermal management */ PowertrainDomain_ThermalControl(); /* Service-oriented communication */ PowertrainDomain_HandleSOARequests(); } void PowertrainDomain_HandleSOARequests(void) { /* Handle SOME/IP service requests from other domains */ SOMEIP_Request_t req; if (SOMEIP_ReceiveRequest(&req)) { switch (req.method_id) { case METHOD_REDUCE_TORQUE: { uint16_t reduction_nm = *(uint16_t*)req.payload; g_powertrain.motor_torque_cmd_nm -= reduction_nm; /* Send response */ SOMEIP_SendResponse(&req, RESPONSE_OK, NULL, 0); break; } case METHOD_GET_SOC: { uint8_t soc = (uint8_t)g_powertrain.battery_soc_percent; SOMEIP_SendResponse(&req, RESPONSE_OK, &soc, 1); break; } case METHOD_SET_CHARGING_LIMIT: { uint8_t limit_percent = *(uint8_t*)req.payload; PowertrainDomain_SetChargingLimit(limit_percent); SOMEIP_SendResponse(&req, RESPONSE_OK, NULL, 0); break; } } } } ``` ### 3. Body/Comfort Domain Controller ```c /* body_domain_controller.c - Comfort and convenience functions */ #include "body_domain.h" /* Consolidated: BCM, HVAC, seats, ambient lighting */ typedef struct { /* Climate control */ float cabin_temperature_c; uint8_t hvac_fan_speed; bool ac_compressor_active; /* Lighting */ HeadlightMode_t headlight_mode; uint8_t ambient_light_brightness; /* Seats */ uint8_t driver_seat_heating_level; uint8_t passenger_seat_heating_level; } BodyDomain_t; static BodyDomain_t g_body = {0}; void BodyDomain_Main_50ms(void) { /* Climate control */ BodyDomain_HVAC_Update(); /* Lighting control */ BodyDomain_Lighting_Update(); /* Seat control */ BodyDomain_Seats_Update(); /* User preference synchronization (cloud) */ BodyDomain_SyncUserPreferences(); } void BodyDomain_SyncUserPreferences(void) { /* Load user profile from cloud (via TCU) */ UserProfile_t profile; if (Cloud_GetUserProfile(g_authenticated_user_id, &profile)) { /* Apply preferences */ g_body.driver_seat_heating_level = profile.seat_heat_pref; g_body.ambient_light_brightness = profile.ambient_light_pref; g_body.headlight_mode = profile.headlight_mode_pref; /* Adjust seat position (via LIN to seat ECU) */ LIN_SendSeatPosition(profile.seat_position); } } ``` ### 4. ADAS Domain Controller ```c /* adas_domain_controller.c - Perception, planning, control */ #include "adas_domain.h" /* Consolidated: camera, radar, lidar fusion, path planning */ typedef struct { /* Perception */ Object_t detected_objects[32]; uint8_t object_count; /* Localization */ float ego_position_x; float ego_position_y; float ego_heading_deg; /* Path planning */ Trajectory_t planned_path; /* Control */ float target_acceleration_mps2; float target_steering_angle_deg; } ADASDomain_t; static ADASDomain_t g_adas = {0}; void ADASDomain_Main_20ms(void) { /* Sensor fusion */ ADASDomain_SensorFusion(); /* Object detection and tracking */ ADASDomain_ObjectTracking(); /* Path planning */ ADASDomain_PathPlanning(); /* Send control commands to chassis/powertrain domains */ ADASDomain_SendControlCommands(); } void ADASDomain_SendControlCommands(void) { /* Request steering via chassis domain */ SOMEIP_SendRequest(CHASSIS_DOMAIN_SERVICE_ID, METHOD_SET_STEERING_ANGLE, &g_adas.target_steering_angle_deg, sizeof(float)); /* Request acceleration via powertrain domain */ if (g_adas.target_acceleration_mps2 > 0) { /* Acceleration */ int16_t torque_nm = (int16_t)(g_adas.target_acceleration_mps2 * 50); SOMEIP_SendRequest(POWERTRAIN_DOMAIN_SERVICE_ID, METHOD_SET_TORQUE, &torque_nm, sizeof(int16_t)); } else { /* Braking */ float decel_mps2 = -g_adas.target_acceleration_mps2; SOMEIP_SendRequest(CHASSIS_DOMAIN_SERVICE_ID, METHOD_APPLY_BRAKE, &decel_mps2, sizeof(float)); } } ``` ## Service-Oriented Architecture (SOME/IP) ### SOME/IP Service Definition (ARXML) ```xml ADAS_Services ADAS_Control_Service 0x1234 1 0 SetSteeringAngle 0x0001 REQUEST-RESPONSE ApplyBrake 0x0002 REQUEST-RESPONSE ObjectDetected 0x8001 ``` ### Cross-Domain Communication Example ```c /* domain_communication.c - SOME/IP client/server example */ #include "someip.h" /* Client: ADAS domain requests torque from powertrain domain */ void ADAS_RequestTorque(int16_t torque_nm) { SOMEIP_Message_t msg; msg.service_id = POWERTRAIN_SERVICE_ID; msg.method_id = METHOD_SET_TORQUE; msg.client_id = ADAS_DOMAIN_CLIENT_ID; msg.session_id = GetNextSessionID(); msg.payload_length = sizeof(int16_t); memcpy(msg.payload, &torque_nm, sizeof(int16_t)); SOMEIP_Send(&msg); /* Wait for response */ SOMEIP_Message_t response; if (SOMEIP_WaitForResponse(&response, 100)) { if (response.return_code == SOMEIP_RETURN_OK) { /* Request acknowledged */ } } } /* Server: Powertrain domain handles torque request */ void Powertrain_SOMEIP_Handler(const SOMEIP_Message_t* request) { if (request->method_id == METHOD_SET_TORQUE) { int16_t requested_torque = *(int16_t*)request->payload; /* Apply safety limits */ if (requested_torque > MAX_TORQUE_NM) { requested_torque = MAX_TORQUE_NM; } /* Set motor torque */ VCU_SetMotorTorque(requested_torque); /* Send response */ SOMEIP_Message_t response; response.service_id = request->service_id; response.method_id = request->method_id; response.client_id = request->client_id; response.session_id = request->session_id; response.return_code = SOMEIP_RETURN_OK; response.payload_length = 0; SOMEIP_Send(&response); } } ``` ## Resource Sharing and Timing ### Hypervisor-Based Domain Isolation ```c /* hypervisor_config.h - QNX Hypervisor partition configuration */ /* Chassis Domain - Guest VM #1 */ #define CHASSIS_DOMAIN_RAM_BASE 0x80000000 #define CHASSIS_DOMAIN_RAM_SIZE 512MB #define CHASSIS_DOMAIN_CPU_MASK 0x03 /* CPU 0-1 */ #define CHASSIS_DOMAIN_PRIORITY CRITICAL /* Powertrain Domain - Guest VM #2 */ #define POWERTRAIN_DOMAIN_RAM_BASE 0xA0000000 #define POWERTRAIN_DOMAIN_RAM_SIZE 512MB #define POWERTRAIN_DOMAIN_CPU_MASK 0x0C /* CPU 2-3 */ #define POWERTRAIN_DOMAIN_PRIORITY CRITICAL /* Body Domain - Guest VM #3 */ #define BODY_DOMAIN_RAM_BASE 0xC0000000 #define BODY_DOMAIN_RAM_SIZE 256MB #define BODY_DOMAIN_CPU_MASK 0x10 /* CPU 4 */ #define BODY_DOMAIN_PRIORITY NORMAL /* ADAS Domain - Guest VM #4 (highest compute) */ #define ADAS_DOMAIN_RAM_BASE 0xD0000000 #define ADAS_DOMAIN_RAM_SIZE 2GB #define ADAS_DOMAIN_CPU_MASK 0xE0 /* CPU 5-7 */ #define ADAS_DOMAIN_PRIORITY HIGH ``` ## Benefits of Domain Controller Architecture - **Reduced wiring harness complexity**: Fewer ECUs = less copper - **Centralized computing**: More powerful processors, better performance - **OTA update efficiency**: Update entire domain instead of individual ECUs - **Cost reduction**: Consolidation reduces hardware costs - **Scalability**: Easier to add features without new ECUs ## References - AUTOSAR Adaptive Platform R22-11 - SOME/IP Protocol Specification v1.3 - QNX Hypervisor for Automotive - ISO 21434: Road Vehicles - Cybersecurity Engineering - ASAM OpenX: Service-Oriented Communication ## Common Issues - Inter-domain latency exceeding real-time requirements - Resource contention between domains on shared CPU cores - SOME/IP service discovery failures - Hypervisor overhead impacting deterministic timing - Cross-domain debugging complexity --- ## Ivi Infotainment Systems # IVI (In-Vehicle Infotainment) Systems ## Overview The In-Vehicle Infotainment (IVI) system manages navigation, multimedia, connectivity (CarPlay/Android Auto), voice assistant, HMI frameworks (Qt/Flutter), and runs on Android Automotive OS, QNX, or Linux platforms. ## Platform Architectures ### 1. Android Automotive OS (AAOS) ```java // VehicleHalService.java - Android Automotive HAL integration package com.example.ivi; import android.car.Car; import android.car.VehiclePropertyIds; import android.car.hardware.CarPropertyValue; import android.car.hardware.property.CarPropertyManager; public class VehicleHalService { private CarPropertyManager mCarPropertyManager; public void init(Context context) { Car car = Car.createCar(context); mCarPropertyManager = (CarPropertyManager) car.getCarManager(Car.PROPERTY_SERVICE); // Subscribe to vehicle speed updates mCarPropertyManager.registerCallback( new CarPropertyManager.CarPropertyEventCallback() { @Override public void onChangeEvent(CarPropertyValue value) { if (value.getPropertyId() == VehiclePropertyIds.PERF_VEHICLE_SPEED) { float speedMs = (Float) value.getValue(); updateSpeedUI(speedMs * 3.6f); // Convert to km/h } } @Override public void onErrorEvent(int propId, int zone) { Log.e("VehicleHal", "Property error: " + propId); } }, VehiclePropertyIds.PERF_VEHICLE_SPEED, CarPropertyManager.SENSOR_RATE_NORMAL); } public void setHvacTemperature(float tempCelsius) { mCarPropertyManager.setFloatProperty( VehiclePropertyIds.HVAC_TEMPERATURE_SET, VehicleAreaType.VEHICLE_AREA_TYPE_SEAT, tempCelsius); } } ``` ### 2. QNX-Based IVI ```c /* qnx_ivi_service.c - QNX CAR platform integration */ #include #include void IVI_QNX_Init(void) { /* Initialize QNX CAR framework */ car_control_t *control = car_control_create(); /* Register for CAN message callbacks */ car_control_set_can_callback(control, IVI_CAN_MessageHandler); /* Initialize Screen Graphics Subsystem */ screen_context_t screen_ctx; screen_create_context(&screen_ctx, SCREEN_APPLICATION_CONTEXT); /* Create display window */ screen_window_t window; screen_create_window(&window, screen_ctx); screen_set_window_property_iv(window, SCREEN_PROPERTY_SIZE, (int[]){1920, 1080}); } void IVI_CAN_MessageHandler(car_can_message_t *msg) { if (msg->id == 0x100) { /* VCU Motor Command */ uint16_t torque = (msg->data[0] << 8) | msg->data[1]; IVI_UpdatePowerMeter(torque); } } ``` ### 3. Navigation Integration (HERE/TomTom) ```kotlin // NavigationService.kt - HERE SDK integration package com.example.ivi.navigation import com.here.sdk.core.GeoCoordinates import com.here.sdk.routing.CalculateRouteCallback import com.here.sdk.routing.Route import com.here.sdk.routing.RoutingEngine class NavigationService { private lateinit var routingEngine: RoutingEngine fun initialize() { routingEngine = RoutingEngine() } fun calculateRoute( origin: GeoCoordinates, destination: GeoCoordinates, callback: (Route?) -> Unit ) { val waypoints = listOf( Waypoint(origin), Waypoint(destination) ) val carOptions = CarOptions().apply { routeOptions.alternatives = 3 avoidanceOptions.avoidTollRoads = false optimizationMode = OptimizationMode.FASTEST } routingEngine.calculateRoute(waypoints, carOptions) { routingError, routes -> if (routingError == null && routes?.isNotEmpty() == true) { callback(routes[0]) } else { callback(null) } } } } ``` ### 4. CarPlay/Android Auto Integration ```java // AndroidAutoService.java - Android Auto projection package com.example.ivi.projection; import android.content.Intent; import com.google.android.apps.auto.sdk.CarActivity; public class AndroidAutoService extends CarActivity { @Override public void onCreate() { super.onCreate(); // Start Android Auto projection Intent intent = new Intent("com.google.android.gms.car.PROJECTION_SERVICE"); startService(intent); } @Override public void onCarConnectionStateChanged(int state) { if (state == CarConnection.STATE_CONNECTED) { // Phone connected: mirror Android Auto UI enableProjectionMode(); } } } ``` ### 5. Voice Assistant Integration (Alexa/Google Assistant) ```python # voice_assistant.py - Voice command handler import speech_recognition as sr import pyttsx3 class VoiceAssistant: def __init__(self): self.recognizer = sr.Recognizer() self.tts = pyttsx3.init() def listen_for_command(self): with sr.Microphone() as source: print("Listening...") audio = self.recognizer.listen(source) try: command = self.recognizer.recognize_google(audio) self.process_command(command) except sr.UnknownValueError: self.speak("Sorry, I didn't understand that.") def process_command(self, command): if "navigate to" in command.lower(): destination = command.lower().replace("navigate to", "").strip() self.navigate(destination) elif "set temperature" in command.lower(): temp = int(command.split()[-1]) self.set_hvac_temperature(temp) def speak(self, text): self.tts.say(text) self.tts.runAndWait() ``` ## HMI Framework (Qt QML) ```qml /* DashboardView.qml - Main instrument cluster */ import QtQuick 2.15 import QtQuick.Controls 2.15 Rectangle { width: 1920 height: 720 // Speedometer Item { id: speedometer x: 100 y: 100 Canvas { id: speedArc width: 400 height: 400 onPaint: { var ctx = getContext("2d"); ctx.clearRect(0, 0, width, height); // Draw arc for speed (0-240 km/h) ctx.beginPath(); ctx.arc(200, 200, 150, 0.75 * Math.PI, (0.75 + 1.5 * (vehicleSpeed / 240)) * Math.PI); ctx.lineWidth = 20; ctx.strokeStyle = "#00FF00"; ctx.stroke(); } Connections { target: vehicleData onSpeedChanged: speedArc.requestPaint() } } Text { text: vehicleData.speed + " km/h" font.pixelSize: 48 color: "white" anchors.centerIn: parent } } // Battery SOC gauge Rectangle { x: 600 y: 100 width: 300 height: 50 color: "transparent" border.color: "white" Rectangle { width: parent.width * (vehicleData.batterySOC / 100) height: parent.height color: vehicleData.batterySOC > 20 ? "#00FF00" : "#FF0000" } Text { text: vehicleData.batterySOC + "%" color: "white" anchors.centerIn: parent } } } ``` ## IVI CAN Interface (DBC) ``` VERSION "" NS_ : BS_: BU_: IVI VCU BCM BMS /* IVI User Commands */ BO_ 1024 IVI_UserCommand: 8 IVI SG_ IVI_DriveMode : 0|8@1+ (0,0) [0|3] "" VCU SG_ IVI_ACTempSet : 8|8@1+ (0.5,10) [10|35] "C" BCM SG_ IVI_ACFanSpeed : 16|8@1+ (0,0) [0|7] "" BCM VAL_ 1024 IVI_DriveMode 0 "Eco" 1 "Normal" 2 "Sport" 3 "Custom"; ``` ## References - Android Automotive OS Developer Guide - QNX CAR Platform API Reference - HERE SDK for Automotive - CarPlay App Programming Guide - W3C Automotive Web Platform Specification ## Common Issues - High CPU usage from rendering complex 3D gauges - CarPlay/Android Auto disconnection during navigation - Voice recognition accuracy in noisy cabin - Slow boot time (> 10 seconds) --- ## Pdu Power Distribution # PDU (Power Distribution Unit) - High/Low Voltage Power Management ## Overview The Power Distribution Unit (PDU) manages high-voltage DC/DC converters, low-voltage power distribution, fuse/relay control, load shedding, power budgeting, battery voltage monitoring, and wake-up source management. ## Core Responsibilities ### 1. High-Voltage DC/DC Converter ```c /* pdu_hv_dcdc.c - High-voltage to 12V DC/DC conversion */ #include "pdu_hv_dcdc.h" #define HV_INPUT_MIN_V 200 #define HV_INPUT_MAX_V 450 #define LV_OUTPUT_TARGET_V 14.0 #define MAX_OUTPUT_CURRENT_A 150 typedef struct { uint16_t hv_input_voltage_v; float lv_output_voltage_v; float output_current_a; float efficiency_percent; bool enabled; bool fault_active; } DCDC_State_t; static DCDC_State_t g_dcdc = {0}; void PDU_DCDC_Init(void) { /* Configure PWM for DC/DC converter control */ PWM_Init(PWM_CHANNEL_DCDC, 100000); /* 100 kHz switching */ /* Set initial duty cycle to 0 */ PWM_SetDutyCycle(PWM_CHANNEL_DCDC, 0); g_dcdc.enabled = false; } void PDU_DCDC_Enable(void) { /* Safety checks */ g_dcdc.hv_input_voltage_v = ADC_ReadHVInput(); if (g_dcdc.hv_input_voltage_v < HV_INPUT_MIN_V || g_dcdc.hv_input_voltage_v > HV_INPUT_MAX_V) { g_dcdc.fault_active = true; return; } /* Enable DC/DC converter */ GPIO_Set(GPIO_DCDC_ENABLE, true); g_dcdc.enabled = true; /* Start voltage regulation loop */ PDU_DCDC_RegulationLoop(); } void PDU_DCDC_RegulationLoop(void) { /* PI controller for output voltage regulation */ static float integral = 0; const float Kp = 0.5; const float Ki = 0.1; while (g_dcdc.enabled) { /* Read output voltage and current */ g_dcdc.lv_output_voltage_v = ADC_ReadLVOutput(); g_dcdc.output_current_a = ADC_ReadOutputCurrent(); /* Calculate error */ float error = LV_OUTPUT_TARGET_V - g_dcdc.lv_output_voltage_v; /* PI control */ integral += error * 0.01; /* 10ms loop time */ float duty_cycle = (Kp * error) + (Ki * integral); /* Clamp duty cycle */ if (duty_cycle > 95) duty_cycle = 95; if (duty_cycle < 5) duty_cycle = 5; PWM_SetDutyCycle(PWM_CHANNEL_DCDC, duty_cycle); /* Overcurrent protection */ if (g_dcdc.output_current_a > MAX_OUTPUT_CURRENT_A) { PDU_DCDC_Disable(); g_dcdc.fault_active = true; DTC_SetFault(DTC_DCDC_OVERCURRENT); break; } OsTask_Sleep(10); } } void PDU_DCDC_Disable(void) { PWM_SetDutyCycle(PWM_CHANNEL_DCDC, 0); GPIO_Set(GPIO_DCDC_ENABLE, false); g_dcdc.enabled = false; } ``` ### 2. Low-Voltage Power Distribution (Fuse/Relay Control) ```c /* pdu_lv_distribution.c - 12V power distribution and load management */ #include "pdu_lv_distribution.h" #define MAX_POWER_CHANNELS 16 typedef enum { LOAD_PRIORITY_CRITICAL = 0, /* Safety: always on */ LOAD_PRIORITY_HIGH, /* Powertrain */ LOAD_PRIORITY_MEDIUM, /* Comfort */ LOAD_PRIORITY_LOW /* Infotainment */ } LoadPriority_t; typedef struct { const char* name; uint8_t relay_pin; uint8_t current_sense_adc; float max_current_a; LoadPriority_t priority; bool enabled; float measured_current_a; } PowerChannel_t; static PowerChannel_t g_power_channels[MAX_POWER_CHANNELS] = { {"BCM", GPIO_RELAY_BCM, ADC_CH_BCM_CURRENT, 15.0, LOAD_PRIORITY_CRITICAL, true, 0}, {"VCU", GPIO_RELAY_VCU, ADC_CH_VCU_CURRENT, 10.0, LOAD_PRIORITY_CRITICAL, true, 0}, {"BMS", GPIO_RELAY_BMS, ADC_CH_BMS_CURRENT, 8.0, LOAD_PRIORITY_CRITICAL, true, 0}, {"MCU", GPIO_RELAY_MCU, ADC_CH_MCU_CURRENT, 12.0, LOAD_PRIORITY_HIGH, true, 0}, {"IVI", GPIO_RELAY_IVI, ADC_CH_IVI_CURRENT, 20.0, LOAD_PRIORITY_LOW, true, 0}, {"HVAC", GPIO_RELAY_HVAC, ADC_CH_HVAC_CURRENT, 25.0, LOAD_PRIORITY_MEDIUM, true, 0}, {"Headlights", GPIO_RELAY_LIGHTS, ADC_CH_LIGHTS_CURRENT, 10.0, LOAD_PRIORITY_HIGH, false, 0}, {"USB_Ports", GPIO_RELAY_USB, ADC_CH_USB_CURRENT, 5.0, LOAD_PRIORITY_LOW, false, 0} }; void PDU_LV_Init(void) { /* Initialize all relay control pins */ for (int i = 0; i < MAX_POWER_CHANNELS; i++) { GPIO_ConfigOutput(g_power_channels[i].relay_pin); /* Enable critical and high priority loads by default */ if (g_power_channels[i].priority <= LOAD_PRIORITY_HIGH) { PDU_LV_EnableChannel(i); } } } void PDU_LV_EnableChannel(uint8_t channel_id) { if (channel_id >= MAX_POWER_CHANNELS) return; GPIO_Set(g_power_channels[channel_id].relay_pin, true); g_power_channels[channel_id].enabled = true; } void PDU_LV_DisableChannel(uint8_t channel_id) { if (channel_id >= MAX_POWER_CHANNELS) return; GPIO_Set(g_power_channels[channel_id].relay_pin, false); g_power_channels[channel_id].enabled = false; } /* Monitor current and detect overcurrent faults */ void PDU_LV_MonitorCurrents(void) { for (int i = 0; i < MAX_POWER_CHANNELS; i++) { if (!g_power_channels[i].enabled) continue; /* Read current sensor (Hall effect sensor, 185mV/A) */ uint16_t adc_value = ADC_Read(g_power_channels[i].current_sense_adc); float voltage_mv = (adc_value * 5000.0) / 4096.0; g_power_channels[i].measured_current_a = (voltage_mv - 2500.0) / 185.0; /* Check for overcurrent */ if (g_power_channels[i].measured_current_a > g_power_channels[i].max_current_a) { /* Overcurrent detected: disable channel */ PDU_LV_DisableChannel(i); DTC_SetFault(DTC_OVERCURRENT_BASE + i); /* Log event */ Log("Overcurrent on %s: %.2f A (max %.2f A)", g_power_channels[i].name, g_power_channels[i].measured_current_a, g_power_channels[i].max_current_a); } } } ``` ### 3. Load Shedding (Power Budget Management) ```c /* pdu_load_shedding.c - Intelligent load management under power constraints */ #include "pdu_load_shedding.h" #define BATTERY_CRITICAL_VOLTAGE_V 11.0 #define BATTERY_LOW_VOLTAGE_V 11.5 void PDU_LoadShedding_Update(void) { float battery_voltage = ADC_ReadBatteryVoltage(); float total_current = 0; /* Calculate total current draw */ for (int i = 0; i < MAX_POWER_CHANNELS; i++) { if (g_power_channels[i].enabled) { total_current += g_power_channels[i].measured_current_a; } } /* Check if battery voltage is low */ if (battery_voltage < BATTERY_CRITICAL_VOLTAGE_V) { /* Critical: shed all non-critical loads */ for (int i = 0; i < MAX_POWER_CHANNELS; i++) { if (g_power_channels[i].priority > LOAD_PRIORITY_CRITICAL) { PDU_LV_DisableChannel(i); } } Log("Critical battery voltage: %.2f V - load shedding active", battery_voltage); } else if (battery_voltage < BATTERY_LOW_VOLTAGE_V) { /* Low: shed low-priority loads */ for (int i = 0; i < MAX_POWER_CHANNELS; i++) { if (g_power_channels[i].priority >= LOAD_PRIORITY_LOW) { PDU_LV_DisableChannel(i); } } Log("Low battery voltage: %.2f V - reducing load", battery_voltage); } /* Check DC/DC converter output current limit */ if (total_current > (MAX_OUTPUT_CURRENT_A * 0.9)) { /* Approaching limit: shed lowest priority loads */ for (int i = MAX_POWER_CHANNELS - 1; i >= 0; i--) { if (g_power_channels[i].priority == LOAD_PRIORITY_LOW && g_power_channels[i].enabled) { PDU_LV_DisableChannel(i); /* Recalculate total current */ total_current -= g_power_channels[i].measured_current_a; if (total_current < (MAX_OUTPUT_CURRENT_A * 0.85)) { break; /* Sufficient headroom */ } } } } } ``` ### 4. Wake-Up Source Management ```c /* pdu_wakeup_sources.c - Network wake-up coordination */ #include "pdu_wakeup_sources.h" #define WAKEUP_CAN_TIMEOUT_MS 100 #define SLEEP_DELAY_MS 5000 typedef enum { WAKEUP_SOURCE_CAN = 0, WAKEUP_SOURCE_LIN, WAKEUP_SOURCE_IGNITION, WAKEUP_SOURCE_DOOR, WAKEUP_SOURCE_TIMER, WAKEUP_SOURCE_COUNT } WakeupSource_t; static bool g_wakeup_pending[WAKEUP_SOURCE_COUNT] = {false}; void PDU_Wakeup_OnCANActivity(void) { g_wakeup_pending[WAKEUP_SOURCE_CAN] = true; /* Power up CAN transceivers */ GPIO_Set(GPIO_CAN_POWERTRAIN_ENABLE, true); GPIO_Set(GPIO_CAN_CHASSIS_ENABLE, true); /* Notify ECUs of wake-up */ CAN_SendWakeupNotification(); } void PDU_Sleep_Prepare(void) { /* Wait for all ECUs to enter sleep */ uint32_t start_time = GetSystemTime_ms(); while ((GetSystemTime_ms() - start_time) < SLEEP_DELAY_MS) { /* Check for wake-up requests */ for (int i = 0; i < WAKEUP_SOURCE_COUNT; i++) { if (g_wakeup_pending[i]) { /* Wake-up requested: abort sleep */ return; } } OsTask_Sleep(10); } /* Enter sleep mode */ PDU_EnterSleep(); } void PDU_EnterSleep(void) { /* Disable non-critical power channels */ for (int i = 0; i < MAX_POWER_CHANNELS; i++) { if (g_power_channels[i].priority > LOAD_PRIORITY_CRITICAL) { PDU_LV_DisableChannel(i); } } /* Configure wake-up sources */ CAN_ConfigureWakeup(CAN_WAKEUP_ENABLED); GPIO_ConfigureWakeup(GPIO_IGNITION, GPIO_WAKEUP_RISING_EDGE); /* Enter low-power mode */ Mcu_SetMode(MCU_MODE_SLEEP); } ``` ## PDU CAN Database (DBC) ``` VERSION "" NS_ : BS_: BU_: PDU VCU BMS BCM /* PDU Power Status */ BO_ 896 PDU_PowerStatus: 8 PDU SG_ PDU_BatteryVoltage_V : 0|16@1+ (0.01,0) [0|16] "V" VCU,BMS SG_ PDU_DCDCOutputCurrent_A : 16|16@1+ (0.1,0) [0|200] "A" VCU SG_ PDU_LoadSheddingActive : 32|1@1+ (0,0) [0|1] "" VCU,BCM SG_ PDU_PowerChannelStatus : 40|16@1+ (0,0) [0|65535] "" VCU /* Each bit in PowerChannelStatus represents one load (0=off, 1=on) */ ``` ## References - LTC3780: High-Voltage Buck-Boost DC/DC Controller - ISO 16750: Road vehicles - Environmental conditions and testing for electrical and electronic equipment - SAE J1455: Recommended Environmental Practices for Electronic Equipment Design - IEC 61000-4-2: EMC Immunity to Electrostatic Discharge ## Common Issues - DC/DC converter instability at high loads - Relay contact welding from inrush current - False overcurrent detection from current sensor noise - Wake-up failures due to CAN transceiver not powered --- ## Tcu Telematics Connectivity # TCU (Telematics Control Unit) - Connectivity and Remote Services ## Overview The Telematics Control Unit (TCU) provides 4G/5G cellular connectivity, GNSS positioning, remote diagnostics, OTA updates, eCall/bCall emergency services, and fleet management integration. This skill covers production-ready TCU development with modem integration. ## Core Responsibilities ### 1. 4G/5G Modem Integration ```c /* tcu_modem_manager.c - Cellular modem control (Quectel/Sierra Wireless) */ #include "tcu_modem_manager.h" #include #include #define MODEM_UART_PORT "/dev/ttyUSB2" #define MODEM_BAUD_RATE 115200 #define AT_COMMAND_TIMEOUT_MS 5000 #define MAX_AT_RESPONSE_LENGTH 512 typedef enum { MODEM_STATE_OFF = 0, MODEM_STATE_INITIALIZING, MODEM_STATE_REGISTERING, MODEM_STATE_CONNECTED, MODEM_STATE_ERROR } ModemState_t; typedef struct { int uart_fd; ModemState_t state; char imei[16]; char iccid[21]; int signal_strength_dbm; char network_operator[32]; char ip_address[16]; bool data_session_active; } ModemContext_t; static ModemContext_t g_modem = {0}; /* AT command send/receive */ bool TCU_Modem_SendATCommand(const char* cmd, char* response, uint16_t response_size) { /* Send AT command */ char cmd_buffer[128]; snprintf(cmd_buffer, sizeof(cmd_buffer), "%s\r\n", cmd); int bytes_written = write(g_modem.uart_fd, cmd_buffer, strlen(cmd_buffer)); if (bytes_written < 0) { return false; } /* Wait for response */ uint32_t start_time = GetSystemTime_ms(); int total_bytes = 0; while ((GetSystemTime_ms() - start_time) < AT_COMMAND_TIMEOUT_MS) { int bytes_available = 0; ioctl(g_modem.uart_fd, FIONREAD, &bytes_available); if (bytes_available > 0) { int bytes_read = read(g_modem.uart_fd, &response[total_bytes], response_size - total_bytes - 1); if (bytes_read > 0) { total_bytes += bytes_read; response[total_bytes] = '\0'; /* Check for "OK" or "ERROR" */ if (strstr(response, "OK\r\n") != NULL) { return true; } if (strstr(response, "ERROR\r\n") != NULL) { return false; } } } usleep(10000); /* 10ms polling interval */ } return false; /* Timeout */ } void TCU_Modem_Init(void) { /* Open UART port */ g_modem.uart_fd = open(MODEM_UART_PORT, O_RDWR | O_NOCTTY); if (g_modem.uart_fd < 0) { g_modem.state = MODEM_STATE_ERROR; return; } /* Configure UART: 115200 8N1 */ struct termios tty; tcgetattr(g_modem.uart_fd, &tty); cfsetospeed(&tty, B115200); cfsetispeed(&tty, B115200); tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; tty.c_cflag &= ~PARENB; tty.c_cflag &= ~CSTOPB; tcsetattr(g_modem.uart_fd, TCSANOW, &tty); g_modem.state = MODEM_STATE_INITIALIZING; char response[MAX_AT_RESPONSE_LENGTH]; /* Basic AT command check */ if (!TCU_Modem_SendATCommand("AT", response, sizeof(response))) { g_modem.state = MODEM_STATE_ERROR; return; } /* Disable echo */ TCU_Modem_SendATCommand("ATE0", response, sizeof(response)); /* Get IMEI */ if (TCU_Modem_SendATCommand("AT+GSN", response, sizeof(response))) { sscanf(response, "%15s", g_modem.imei); } /* Get ICCID (SIM card ID) */ if (TCU_Modem_SendATCommand("AT+CCID", response, sizeof(response))) { sscanf(response, "+CCID: %20s", g_modem.iccid); } /* Check SIM status */ if (!TCU_Modem_SendATCommand("AT+CPIN?", response, sizeof(response))) { g_modem.state = MODEM_STATE_ERROR; return; } /* Start network registration */ TCU_Modem_StartNetworkRegistration(); } void TCU_Modem_StartNetworkRegistration(void) { char response[MAX_AT_RESPONSE_LENGTH]; /* Set network mode: LTE only for 4G, NR+LTE for 5G */ TCU_Modem_SendATCommand("AT+QCFG=\"nwscanmode\",3", response, sizeof(response)); /* Enable network registration URC */ TCU_Modem_SendATCommand("AT+CREG=2", response, sizeof(response)); /* Check registration status */ if (TCU_Modem_SendATCommand("AT+CREG?", response, sizeof(response))) { int n, stat; if (sscanf(response, "+CREG: %d,%d", &n, &stat) == 2) { if (stat == 1 || stat == 5) { /* Registered (home or roaming) */ g_modem.state = MODEM_STATE_REGISTERED; TCU_Modem_GetNetworkInfo(); } else { g_modem.state = MODEM_STATE_REGISTERING; } } } } void TCU_Modem_GetNetworkInfo(void) { char response[MAX_AT_RESPONSE_LENGTH]; /* Get signal strength */ if (TCU_Modem_SendATCommand("AT+CSQ", response, sizeof(response))) { int rssi, ber; if (sscanf(response, "+CSQ: %d,%d", &rssi, &ber) == 2) { /* Convert RSSI to dBm: dBm = -113 + 2*rssi */ g_modem.signal_strength_dbm = -113 + (2 * rssi); } } /* Get operator name */ if (TCU_Modem_SendATCommand("AT+COPS?", response, sizeof(response))) { char operator_name[32]; if (sscanf(response, "+COPS: 0,0,\"%31[^\"]\"", operator_name) == 1) { strncpy(g_modem.network_operator, operator_name, sizeof(g_modem.network_operator)); } } } bool TCU_Modem_StartDataSession(const char* apn) { char response[MAX_AT_RESPONSE_LENGTH]; char cmd[128]; /* Configure PDP context */ snprintf(cmd, sizeof(cmd), "AT+QICSGP=1,1,\"%s\",\"\",\"\",1", apn); if (!TCU_Modem_SendATCommand(cmd, response, sizeof(response))) { return false; } /* Activate PDP context */ if (!TCU_Modem_SendATCommand("AT+QIACT=1", response, sizeof(response))) { return false; } /* Get IP address */ if (TCU_Modem_SendATCommand("AT+QIACT?", response, sizeof(response))) { char ip_addr[16]; if (sscanf(response, "+QIACT: 1,1,1,\"%15[^\"]\"", ip_addr) == 1) { strncpy(g_modem.ip_address, ip_addr, sizeof(g_modem.ip_address)); g_modem.data_session_active = true; g_modem.state = MODEM_STATE_CONNECTED; return true; } } return false; } /* HTTP client for cloud connectivity */ bool TCU_Modem_HTTPPost(const char* url, const char* json_payload, char* response) { char cmd[256]; char at_response[MAX_AT_RESPONSE_LENGTH]; /* Configure HTTP context */ snprintf(cmd, sizeof(cmd), "AT+QHTTPCFG=\"contextid\",1"); TCU_Modem_SendATCommand(cmd, at_response, sizeof(at_response)); /* Set URL */ snprintf(cmd, sizeof(cmd), "AT+QHTTPURL=%zu,80", strlen(url)); TCU_Modem_SendATCommand(cmd, at_response, sizeof(at_response)); /* Send URL */ write(g_modem.uart_fd, url, strlen(url)); usleep(100000); /* POST data */ snprintf(cmd, sizeof(cmd), "AT+QHTTPPOST=%zu,80,80", strlen(json_payload)); TCU_Modem_SendATCommand(cmd, at_response, sizeof(at_response)); /* Send payload */ write(g_modem.uart_fd, json_payload, strlen(json_payload)); /* Wait for response */ sleep(2); /* Read response */ TCU_Modem_SendATCommand("AT+QHTTPREAD=80", response, MAX_AT_RESPONSE_LENGTH); return true; } ``` ### 2. GNSS/GPS Position Tracking ```c /* tcu_gnss_manager.c - GPS/GLONASS/BeiDou positioning */ #include "tcu_gnss_manager.h" #include #define EARTH_RADIUS_KM 6371.0 typedef struct { double latitude; double longitude; float altitude_m; float speed_kph; float heading_deg; uint8_t satellites_used; float hdop; /* Horizontal dilution of precision */ bool fix_valid; uint32_t timestamp_ms; } GNSSPosition_t; static GNSSPosition_t g_gnss_position = {0}; void TCU_GNSS_Init(void) { char response[MAX_AT_RESPONSE_LENGTH]; /* Enable GNSS */ TCU_Modem_SendATCommand("AT+QGPS=1", response, sizeof(response)); /* Configure GNSS to use GPS+GLONASS+BeiDou */ TCU_Modem_SendATCommand("AT+QGPSCFG=\"gnssconfig\",7", response, sizeof(response)); } bool TCU_GNSS_GetPosition(GNSSPosition_t* position) { char response[MAX_AT_RESPONSE_LENGTH]; /* Query GNSS position */ if (!TCU_Modem_SendATCommand("AT+QGPSLOC=2", response, sizeof(response))) { return false; } /* Parse NMEA-like response: +QGPSLOC: