/** * @file board_init.c * @brief Custom ECU board initialization */ #include "board.h" #include "kernel.h" #include "gpio_driver.h" #include "uart_driver.h" #include "can_driver.h" #include "spi_driver.h" #include "i2c_driver.h" #include "adc_driver.h" #include "pwm_driver.h" #include static bool board_initialized = false; /* Board-specific configuration */ typedef struct { uint32_t can_baudrate; uint32_t uart_baudrate; uint8_t node_id; bool can_fd_enabled; } BoardConfig_t; static const BoardConfig_t board_config = { .can_baudrate = 500000, .uart_baudrate = 115200, .node_id = 0x01, .can_fd_enabled = true }; /* ============================================================================ * Board Initialization * ============================================================================ */ void board_init(void) { if (board_initialized) { return; } /* Initialize clock */ board_clock_init(); /* Initialize GPIO */ board_gpio_init(); /* Initialize UART */ board_uart_init(); /* Initialize CAN */ board_can_init(); /* Initialize SPI */ board_spi_init(); /* Initialize I2C */ board_i2c_init(); /* Initialize ADC */ board_adc_init(); /* Initialize PWM */ board_pwm_init(); /* Initialize watchdog */ board_watchdog_init(); board_initialized = true; } /* ============================================================================ * Clock Initialization * ============================================================================ */ void board_clock_init(void) { /* Custom clock initialization */ /* This would be implemented for specific hardware */ } /* ============================================================================ * GPIO Initialization * ============================================================================ */ void board_gpio_init(void) { /* Configure status LEDs */ GpioPinConfig_t led_config = { .mode = GPIO_MODE_OUTPUT, .output_type = GPIO_OUTPUT_PUSH_PULL, .pull = GPIO_PULL_NONE, .speed = GPIO_SPEED_HIGH }; /* Configure each LED */ for (int i = 0; i < LED_COUNT; i++) { led_config.port = 0; led_config.pin = i; gpio_init(&led_config); } /* Turn off all LEDs */ for (int i = 0; i < LED_COUNT; i++) { board_led_off(i); } } /* ============================================================================ * UART Initialization * ============================================================================ */ void board_uart_init(void) { /* Configure debug UART */ UartConfig_t uart_config = { .baudrate = board_config.uart_baudrate, .data_bits = UART_DATA_BITS_8, .stop_bits = UART_STOP_BITS_1, .parity = UART_PARITY_NONE, .flow_control = UART_FLOW_CONTROL_NONE, .enable_rx = true, .enable_tx = true, .use_dma = false }; uart_init(0, &uart_config); } /* ============================================================================ * CAN Initialization * ============================================================================ */ void board_can_init(void) { /* Configure main CAN interface */ CanConfig_t can_config = { .nominal_baudrate = board_config.can_baudrate, .data_baudrate = board_config.can_baudrate, .frame_type = CAN_FRAME_CLASSIC, .enable_fd = board_config.can_fd_enabled, .enable_automatic_retransmission = true, .filter_count = 0 }; can_init(0, &can_config); } /* ============================================================================ * SPI Initialization * ============================================================================ */ void board_spi_init(void) { /* Configure SPI for external devices */ SpiConfig_t spi_config = { .mode = SPI_MODE_0, .clock_speed = SPI_CLOCK_8MHZ, .data_order = SPI_DATA_ORDER_MSB_FIRST, .data_size = 8, .use_dma = true, .enable_hardware_cs = false, .cs_polarity = SPI_CS_ACTIVE_LOW, .cs_port = 0, .cs_pin = 10 }; spi_init(0, &spi_config); } /* ============================================================================ * I2C Initialization * ============================================================================ */ void board_i2c_init(void) { /* Configure I2C for sensors */ I2cConfig_t i2c_config = { .speed = I2C_SPEED_FAST, .addressing_mode = I2C_ADDRESSING_7BIT, .own_address = 0x50, .enable_general_call = false, .enable_clock_stretching = true, .use_dma = false }; i2c_init(0, &i2c_config); } /* ============================================================================ * ADC Initialization * ============================================================================ */ void board_adc_init(void) { /* Configure ADC for analog inputs */ AdcConfig_t adc_config = { .resolution = ADC_RESOLUTION_12BIT, .mode = ADC_MODE_SCAN, .trigger_source = ADC_TRIGGER_TIMER, .reference = ADC_REFERENCE_VDD, .enable_dma = true, .conversion_frequency = 1000, .channel_count = 8, .channels = { {.channel = 0, .sampling_time = ADC_SAMPLING_28_5_CYCLES}, {.channel = 1, .sampling_time = ADC_SAMPLING_28_5_CYCLES}, {.channel = 2, .sampling_time = ADC_SAMPLING_28_5_CYCLES}, {.channel = 3, .sampling_time = ADC_SAMPLING_28_5_CYCLES}, {.channel = 4, .sampling_time = ADC_SAMPLING_28_5_CYCLES}, {.channel = 5, .sampling_time = ADC_SAMPLING_28_5_CYCLES}, {.channel = 6, .sampling_time = ADC_SAMPLING_28_5_CYCLES}, {.channel = 7, .sampling_time = ADC_SAMPLING_28_5_CYCLES} } }; adc_init(0, &adc_config); } /* ============================================================================ * PWM Initialization * ============================================================================ */ void board_pwm_init(void) { /* Configure PWM outputs */ PwmConfig_t pwm_config = { .frequency_hz = 1000, .alignment = PWM_ALIGNMENT_EDGE, .period_ticks = 1000, .prescaler = 100, .channel_count = 4, .channels = { {.channel = 0, .duty_cycle = 0}, {.channel = 1, .duty_cycle = 0}, {.channel = 2, .duty_cycle = 0}, {.channel = 3, .duty_cycle = 0} }, .enable_fault_protection = true, .fault_action = PWM_FAULT_DISABLE }; pwm_init(0, &pwm_config); } /* ============================================================================ * Watchdog Initialization * ============================================================================ */ void board_watchdog_init(void) { /* Custom watchdog initialization */ } void board_watchdog_service(void) { /* Custom watchdog service */ } /* ============================================================================ * LED Functions * ============================================================================ */ void board_led_on(uint8_t led) { if (led < LED_COUNT) { gpio_write(0, led, true); } } void board_led_off(uint8_t led) { if (led < LED_COUNT) { gpio_write(0, led, false); } } void board_led_toggle(uint8_t led) { if (led < LED_COUNT) { gpio_toggle(0, led); } } /* ============================================================================ * Button Functions * ============================================================================ */ bool board_button_read(uint8_t button) { /* Custom button implementation */ return false; } /* ============================================================================ * Delay Functions * ============================================================================ */ void board_delay_ms(uint32_t ms) { kernel_delay(ms); } void board_delay_us(uint32_t us) { /* Custom microsecond delay */ } /* ============================================================================ * Sensor Functions * ============================================================================ */ float board_get_temperature(void) { /* Custom temperature sensor reading */ return 25.0f; } float board_get_voltage(void) { /* Custom voltage reading */ return 3.3f; }