/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : 萝莉3代航模遥控器 - 主程序 * @author : 小云 (Hermes Agent) * @date : 2026-06-25 ****************************************************************************** * 系统架构: * - 10ms 定时循环: 按键扫描 + ADC 轮询 + LED 更新 * - 主循环: 混控计算 + 菜单 UI + 协议收发 * - 中断: SysTick (时基), EXTI (NRF IRQ), UART (SBUS) * * 模块依赖: * oled.h -> OLED SSD1306 128x64 显示 * input.h -> 按键/ADC 输入采集 * menu.h -> 菜单系统与 UI 交互 * storage.h -> Flash 存储 (模型/配置) * pwm_out.h -> PWM/LED/WS2812 输出 * nrf24l01.h-> NRF24L01+ 2.4G 无线 * protocol.h-> 协议层 (RF/SBUS/USB HID) ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "adc.h" #include "i2c.h" #include "spi.h" #include "tim.h" #include "usart.h" #include "usb.h" #include "gpio.h" /* 应用层模块 */ #include "oled.h" #include "input.h" #include "menu.h" #include "storage.h" #include "pwm_out.h" #include "nrf24l01.h" #include "protocol.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ #define MAIN_LOOP_PERIOD_MS 10 /* 主循环周期 10ms */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ static uint32_t last_loop_tick = 0; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP */ static void System_Init(void); static void System_Loop10ms(void); /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_ADC1_Init(); MX_ADC2_Init(); MX_I2C2_Init(); MX_SPI1_Init(); MX_USART1_UART_Init(); MX_USART3_UART_Init(); MX_USB_PCD_Init(); MX_TIM3_Init(); MX_TIM4_Init(); /* USER CODE BEGIN 2 */ /* ---- 应用层初始化 ---- */ System_Init(); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ uint32_t now = HAL_GetTick(); /* 10ms 定时任务 */ if (now - last_loop_tick >= MAIN_LOOP_PERIOD_MS) { last_loop_tick = now; System_Loop10ms(); } /* 实时任务: 混控 + 菜单 + 协议 */ Mixer_Compute(g_channels); Menu_Run(); Protocol_Run(); } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC|RCC_PERIPHCLK_USB; PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6; PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5; if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ /** * @brief 系统初始化: 加载配置、初始化各模块、显示开机画面 * @retval None */ static void System_Init(void) { /* 1. 加载存储配置 */ Storage_Init(); Storage_Load(); /* 2. 初始化 OLED 并显示开机画面 */ OLED_Init(); OLED_DrawBitmap(0, 0, bitmap_bytes, 64, 64); /* 开机 Logo */ OLED_ShowString(70, 20, "Loli3", 16); OLED_ShowString(70, 40, "RC v3.0", 6); OLED_Display(); HAL_Delay(2000); /* 显示 2 秒 */ /* 3. 初始化输入模块 */ Input_Init(); /* 4. 初始化 PWM/LED */ PWM_Init(); LED_SetSystem(1); /* 系统 LED 常亮 */ /* 5. 初始化 NRF24L01 */ NRF24L01_Init(); if (NRF24L01_Check()) { /* NRF24L01 存在 */ NRF24L01_SetChannel(g_config.nrf_channel); LED_SetNRF(0); /* 慢闪 = 未连接 */ } else { /* NRF24L01 未检测到 */ LED_SetNRF(3); /* 灭 */ } /* 6. 初始化协议层 */ Protocol_Init(); /* 7. 初始化菜单 */ Menu_Init(); /* 8. 根据配置设置 PWM 模式 */ PWM_SetMode(0, g_config.pwm1_mode); PWM_SetMode(1, g_config.pwm2_mode); PWM_SetWS2812Mode(g_config.ws2812_effect); PWM_SetLEDBrightness(g_config.led_brightness); PWM_SetVibrateLevel(g_config.vibrate_level); } /** * @brief 10ms 定时任务: 按键扫描 + ADC 轮询 + LED 闪烁更新 * @retval None */ static void System_Loop10ms(void) { /* 按键 + ADC 扫描 */ Input_Scan(); /* LED 闪烁更新 */ LED_Update(); } /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* 错误时 LED 快闪 */ __disable_irq(); while (1) { /* 系统 LED 快闪指示错误 */ HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_6); for (volatile uint32_t i = 0; i < 500000; i++); } /* USER CODE END Error_Handler_Debug */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */