121 lines
3.6 KiB
C
121 lines
3.6 KiB
C
/**
|
|
* @file app_main.c
|
|
* @brief AliceRC application initialization and task management
|
|
*/
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "freertos/queue.h"
|
|
#include "esp_log.h"
|
|
#include "esp_system.h"
|
|
#include "driver/gpio.h"
|
|
#include "driver/adc.h"
|
|
#include "nvs_flash.h"
|
|
#include "app_main.h"
|
|
|
|
/* Queue handles for inter-task communication */
|
|
QueueHandle_t adc_data_queue = NULL;
|
|
QueueHandle_t rf_command_queue = NULL;
|
|
QueueHandle_t ui_event_queue = NULL;
|
|
|
|
static const char *TAG = "app_main";
|
|
|
|
void app_init_gpio(void)
|
|
{
|
|
/* Configure button GPIOs as inputs with pull-ups */
|
|
gpio_config_t btn_conf = {
|
|
.pin_bit_mask = (1ULL<<BTN_UP_GPIO) | (1ULL<<BTN_DOWN_GPIO) |
|
|
(1ULL<<BTN_SEL_GPIO) | (1ULL<<BTN_BACK_GPIO),
|
|
.mode = GPIO_MODE_INPUT,
|
|
.pull_up_en = GPIO_PULLUP_ENABLE,
|
|
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
|
.intr_type = GPIO_INTR_DISABLE
|
|
};
|
|
gpio_config(&btn_conf);
|
|
|
|
/* Configure status LED */
|
|
gpio_config_t led_conf = {
|
|
.pin_bit_mask = (1ULL << STATUS_LED_GPIO),
|
|
.mode = GPIO_MODE_OUTPUT,
|
|
.pull_up_en = GPIO_PULLUP_DISABLE,
|
|
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
|
.intr_type = GPIO_INTR_DISABLE
|
|
};
|
|
gpio_config(&led_conf);
|
|
gpio_set_level(STATUS_LED_GPIO, 0);
|
|
|
|
/* Configure PWR_EN (ESP32S3 only) */
|
|
#if CONFIG_IDF_TARGET_ESP32S3
|
|
gpio_config_t pwr_conf = {
|
|
.pin_bit_mask = (1ULL << PWR_EN_GPIO),
|
|
.mode = GPIO_MODE_OUTPUT,
|
|
.pull_up_en = GPIO_PULLUP_DISABLE,
|
|
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
|
.intr_type = GPIO_INTR_DISABLE
|
|
};
|
|
gpio_config(&pwr_conf);
|
|
gpio_set_level(PWR_EN_GPIO, 1); /* Enable peripheral power */
|
|
#endif
|
|
|
|
ESP_LOGI(TAG, "GPIO initialized");
|
|
}
|
|
|
|
void app_init_adc(void)
|
|
{
|
|
/* Initialize ADC1 for battery voltage */
|
|
adc1_config_width(ADC_WIDTH_BIT_12);
|
|
adc1_config_channel_atten(ADC1_CHANNEL_0, ADC_ATTEN_DB_12); /* GPIO 8 */
|
|
|
|
/* Initialize ADC2 for joystick channels (ESP32S3) */
|
|
#if CONFIG_IDF_TARGET_ESP32S3
|
|
/* ADC2 channels: 4,5,6,7,15,16,17,9 */
|
|
adc2_config_channel_atten(ADC2_CHANNEL_0, ADC_ATTEN_DB_12); /* GPIO 4 */
|
|
adc2_config_channel_atten(ADC2_CHANNEL_1, ADC_ATTEN_DB_12); /* GPIO 5 */
|
|
adc2_config_channel_atten(ADC2_CHANNEL_2, ADC_ATTEN_DB_12); /* GPIO 6 */
|
|
adc2_config_channel_atten(ADC2_CHANNEL_3, ADC_ATTEN_DB_12); /* GPIO 7 */
|
|
#endif
|
|
|
|
ESP_LOGI(TAG, "ADC initialized");
|
|
}
|
|
|
|
void app_init_nvs(void)
|
|
{
|
|
esp_err_t ret = nvs_flash_init();
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
ret = nvs_flash_init();
|
|
}
|
|
ESP_ERROR_CHECK(ret);
|
|
ESP_LOGI(TAG, "NVS initialized");
|
|
}
|
|
|
|
void app_create_tasks(void)
|
|
{
|
|
/* Create queues */
|
|
adc_data_queue = xQueueCreate(2, sizeof(adc_data_t));
|
|
ui_event_queue = xQueueCreate(10, sizeof(ui_event_t));
|
|
|
|
/* Create tasks */
|
|
xTaskCreatePinnedToCore(adc_task, "adc_task", TASK_STACK_ADC, NULL,
|
|
TASK_PRIO_ADC, NULL, tskNO_AFFINITY);
|
|
xTaskCreatePinnedToCore(ui_task, "ui_task", TASK_STACK_UI, NULL,
|
|
TASK_PRIO_UI, NULL, tskNO_AFFINITY);
|
|
xTaskCreatePinnedToCore(rf_task, "rf_task", TASK_STACK_RF, NULL,
|
|
TASK_PRIO_RF, NULL, tskNO_AFFINITY);
|
|
|
|
ESP_LOGI(TAG, "Tasks created");
|
|
}
|
|
|
|
void app_init(void)
|
|
{
|
|
ESP_LOGI(TAG, "Initializing AliceRC...");
|
|
|
|
app_init_nvs();
|
|
app_init_gpio();
|
|
app_init_adc();
|
|
app_create_tasks();
|
|
|
|
ESP_LOGI(TAG, "AliceRC initialized successfully");
|
|
}
|