Initial commit
ESP32-S3 firmware acting as BMC for another board, controlling an ATX power supply and providing access to UART. Co-authored with GLM-5
This commit is contained in:
129
main/main.c
Normal file
129
main/main.c
Normal file
@@ -0,0 +1,129 @@
|
||||
#include <stdio.h>
|
||||
#include "esp_log.h"
|
||||
#include "esp_event.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "config.h"
|
||||
#include "gpio_controller.h"
|
||||
#include "uart_handler.h"
|
||||
#include "ethernet_manager.h"
|
||||
#include "web_server.h"
|
||||
|
||||
static const char *TAG = TAG_BMC;
|
||||
|
||||
// Ethernet event callback
|
||||
static void eth_event_callback(eth_event_type_t event) {
|
||||
switch (event) {
|
||||
case ETH_EVENT_CONNECTED:
|
||||
ESP_LOGI(TAG, "Ethernet connected");
|
||||
gpio_set_status_led(true);
|
||||
break;
|
||||
|
||||
case ETH_EVENT_DISCONNECTED:
|
||||
ESP_LOGW(TAG, "Ethernet disconnected");
|
||||
gpio_set_status_led(false);
|
||||
break;
|
||||
|
||||
case ETH_EVENT_GOT_IP: {
|
||||
char ip[16];
|
||||
if (ethernet_get_ip(ip) == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Got IP: %s", ip);
|
||||
}
|
||||
} break;
|
||||
|
||||
case ETH_EVENT_LOST_IP:
|
||||
ESP_LOGW(TAG, "Lost IP address");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void app_main(void) {
|
||||
ESP_LOGI(TAG, "========================================");
|
||||
ESP_LOGI(TAG, "ESP32-S3 BMC Firmware Starting...");
|
||||
ESP_LOGI(TAG, "========================================");
|
||||
|
||||
// Initialize GPIO controller
|
||||
ESP_LOGI(TAG, "Initializing GPIO controller...");
|
||||
esp_err_t ret = gpio_controller_init();
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to initialize GPIO controller: %s", esp_err_to_name(ret));
|
||||
// Continue anyway - some GPIOs might still work
|
||||
}
|
||||
|
||||
// Set status LED to indicate initialization
|
||||
gpio_set_status_led(false);
|
||||
|
||||
// Initialize UART handler
|
||||
ESP_LOGI(TAG, "Initializing UART handler...");
|
||||
ret = uart_handler_init();
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to initialize UART handler: %s", esp_err_to_name(ret));
|
||||
// Continue anyway - serial console is optional
|
||||
}
|
||||
|
||||
// Initialize Ethernet manager
|
||||
ESP_LOGI(TAG, "Initializing Ethernet manager...");
|
||||
ethernet_register_event_callback(eth_event_callback);
|
||||
ret = ethernet_manager_init();
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to initialize Ethernet: %s", esp_err_to_name(ret));
|
||||
// Cannot continue without network
|
||||
return;
|
||||
}
|
||||
|
||||
// Wait for Ethernet connection (with timeout)
|
||||
ESP_LOGI(TAG, "Waiting for Ethernet connection...");
|
||||
int timeout = 30; // 30 seconds timeout
|
||||
while (!ethernet_is_connected() && timeout > 0) {
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
timeout--;
|
||||
ESP_LOGD(TAG, "Waiting for Ethernet... (%d seconds remaining)", timeout);
|
||||
}
|
||||
|
||||
if (!ethernet_is_connected()) {
|
||||
ESP_LOGW(TAG, "Ethernet not connected after timeout, starting server anyway...");
|
||||
}
|
||||
|
||||
// Start web server
|
||||
ESP_LOGI(TAG, "Starting web server...");
|
||||
ret = web_server_start();
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to start web server: %s", esp_err_to_name(ret));
|
||||
return;
|
||||
}
|
||||
|
||||
// Print network information
|
||||
char ip[16], netmask[16], gateway[16], mac[18];
|
||||
if (ethernet_get_network_info(ip, netmask, gateway) == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Network Configuration:");
|
||||
ESP_LOGI(TAG, " IP Address: %s", ip);
|
||||
ESP_LOGI(TAG, " Netmask: %s", netmask);
|
||||
ESP_LOGI(TAG, " Gateway: %s", gateway);
|
||||
}
|
||||
|
||||
if (ethernet_get_mac(mac) == ESP_OK) {
|
||||
ESP_LOGI(TAG, " MAC Address: %s", mac);
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "========================================");
|
||||
ESP_LOGI(TAG, "BMC Ready!");
|
||||
ESP_LOGI(TAG, "Web interface: http://%s/", ip);
|
||||
ESP_LOGI(TAG, "========================================");
|
||||
|
||||
// Main loop - monitor system health
|
||||
while (1) {
|
||||
// Update status LED based on connection state
|
||||
if (ethernet_is_connected()) {
|
||||
gpio_set_status_led(true);
|
||||
} else {
|
||||
// Blink LED if not connected
|
||||
gpio_set_status_led(false);
|
||||
vTaskDelay(pdMS_TO_TICKS(500));
|
||||
gpio_set_status_led(true);
|
||||
vTaskDelay(pdMS_TO_TICKS(500));
|
||||
gpio_set_status_led(false);
|
||||
}
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(5000));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user