From 33d3feede4da2ab29f8f9c74ee4f984684c59a9d Mon Sep 17 00:00:00 2001 From: robinson <18656133516@qq.com> Date: Thu, 16 Apr 2026 22:55:16 +0800 Subject: [PATCH] [add] added new hal level led/can --- .settings/bundles-lock.store.json | 4 + .settings/bundles.store.json | 4 + .vscode/settings.json | 1 + CMakeLists.txt | 30 +- Core/Src/Backup/can.c.bak | 385 --------- Core/Src/Backup/freertos.c.bak | 338 -------- Core/Src/Backup/gpio.c.bak | 68 -- Core/Src/Backup/i2c.c.bak | 122 --- Core/Src/Backup/main.c.bak | 249 ------ Core/Src/Backup/stm32f4xx_hal_msp.c.bak | 84 -- .../Backup/stm32f4xx_hal_timebase_tim.c.bak | 128 --- Core/Src/Backup/stm32f4xx_it.c.bak | 193 ----- Core/Src/Backup/syscalls.c.bak | 176 ----- Core/Src/Backup/sysmem.c.bak | 79 -- Core/Src/Backup/system_stm32f4xx.c.bak | 747 ------------------ Core/Src/Backup/usart.c.bak | 114 --- Core/Src/freertos.c | 215 +---- Core/Src/main.c | 19 - app/app.c | 13 + bsp/stm32f4/can/bsp_can.c | 26 +- bsp/stm32f4/can/bsp_can.h | 10 +- bsp/stm32f4/led/bsp_led.c | 13 + bsp/stm32f4/led/bsp_led.h | 13 + hal/stm32f4/can/hal_can_stm32f4.c | 60 ++ hal/stm32f4/can/hal_can_stm32f4.h | 14 + hal/stm32f4/led/hal_led_stm32f4.c | 73 ++ interfaces/can/can_if.c | 36 + interfaces/can/can_if.h | 28 + interfaces/led/led_if.c | 36 + interfaces/led/led_if.h | 20 + modules/device/led/led.c | 49 ++ modules/device/led/led.h | 14 + modules/device/motor/mf4010v2.c | 53 +- modules/device/motor/mf4010v2.h | 20 +- modules/sensor/imu/j901p.c | 9 - 35 files changed, 443 insertions(+), 3000 deletions(-) delete mode 100644 Core/Src/Backup/can.c.bak delete mode 100644 Core/Src/Backup/freertos.c.bak delete mode 100644 Core/Src/Backup/gpio.c.bak delete mode 100644 Core/Src/Backup/i2c.c.bak delete mode 100644 Core/Src/Backup/main.c.bak delete mode 100644 Core/Src/Backup/stm32f4xx_hal_msp.c.bak delete mode 100644 Core/Src/Backup/stm32f4xx_hal_timebase_tim.c.bak delete mode 100644 Core/Src/Backup/stm32f4xx_it.c.bak delete mode 100644 Core/Src/Backup/syscalls.c.bak delete mode 100644 Core/Src/Backup/sysmem.c.bak delete mode 100644 Core/Src/Backup/system_stm32f4xx.c.bak delete mode 100644 Core/Src/Backup/usart.c.bak create mode 100644 app/app.c create mode 100644 bsp/stm32f4/led/bsp_led.c create mode 100644 bsp/stm32f4/led/bsp_led.h create mode 100644 hal/stm32f4/can/hal_can_stm32f4.c create mode 100644 hal/stm32f4/can/hal_can_stm32f4.h create mode 100644 hal/stm32f4/led/hal_led_stm32f4.c create mode 100644 interfaces/can/can_if.c create mode 100644 interfaces/can/can_if.h create mode 100644 interfaces/led/led_if.c create mode 100644 interfaces/led/led_if.h create mode 100644 modules/device/led/led.c create mode 100644 modules/device/led/led.h diff --git a/.settings/bundles-lock.store.json b/.settings/bundles-lock.store.json index 1c4d1d5..374c636 100644 --- a/.settings/bundles-lock.store.json +++ b/.settings/bundles-lock.store.json @@ -82,6 +82,10 @@ "version": "1.0.1+st.2", "platform": "all", "selected_by": [ + { + "name": "gnu-tools-for-stm32-14_3_1-description", + "version": "1.0.1+st.2" + }, { "name": "gnu-tools-for-stm32-14_3_1-description", "version": ">=0.0.1" diff --git a/.settings/bundles.store.json b/.settings/bundles.store.json index 0bc523f..2c12504 100644 --- a/.settings/bundles.store.json +++ b/.settings/bundles.store.json @@ -15,6 +15,10 @@ { "name": "st-arm-clangd", "version": "21.1.0+st.2" + }, + { + "name": "gnu-tools-for-stm32-14_3_1-description", + "version": "1.0.1+st.2" } ] } diff --git a/.vscode/settings.json b/.vscode/settings.json index 8627b90..f66e8b8 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -8,6 +8,7 @@ ], "stm32cube-ide-clangd.path": "cube", "stm32cube-ide-clangd.arguments": [ + "--compile-commands-dir=${workspaceFolder}/build/Debug", "starm-clangd", "--query-driver=${env:CUBE_BUNDLE_PATH}/gnu-tools-for-stm32/14.3.1+st.2/bin/arm-none-eabi-gcc*", "--query-driver=${env:CUBE_BUNDLE_PATH}/gnu-tools-for-stm32/14.3.1+st.2/bin/arm-none-eabi-g++*" diff --git a/CMakeLists.txt b/CMakeLists.txt index 19cfabe..e689f80 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,19 +48,33 @@ target_link_directories(${CMAKE_PROJECT_NAME} PRIVATE # Add sources to executable target_sources(${CMAKE_PROJECT_NAME} PRIVATE # Add user sources here - Modules/Sensors/j901p.c - Modules/Sensors/j901p_hal.c - Modules/Devices/mf4010v2.c - Bsp/can/bsp_can.c + modules/sensor/imu/j901p.c + modules/sensor/imu/j901p_hal.c + modules/device/motor/mf4010v2.c + modules/device/led/led.c + bsp/stm32f4/can/bsp_can.c + bsp/stm32f4/led/bsp_led.c + hal/stm32f4/led/hal_led_stm32f4.c + hal/stm32f4/can/hal_can_stm32f4.c + app/app.c + interfaces/led/led_if.c + interfaces/can/can_if.c + ) # Add include paths target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE # Add user defined include paths - ${CMAKE_CURRENT_SOURCE_DIR}/Modules/Sensors - ${CMAKE_CURRENT_SOURCE_DIR}/Modules/Devices - ${CMAKE_CURRENT_SOURCE_DIR}/Bsp/can - ${CMAKE_CURRENT_SOURCE_DIR}/Core/Inc + ${CMAKE_CURRENT_SOURCE_DIR}/modules/device/led + ${CMAKE_CURRENT_SOURCE_DIR}/modules/device/motor + ${CMAKE_CURRENT_SOURCE_DIR}/modules/sensor/imu + ${CMAKE_CURRENT_SOURCE_DIR}/bsp/stm32f4/can + ${CMAKE_CURRENT_SOURCE_DIR}/bsp/stm32f4/led + ${CMAKE_CURRENT_SOURCE_DIR}/app + ${CMAKE_CURRENT_SOURCE_DIR}/hal/stm32f4/led + ${CMAKE_CURRENT_SOURCE_DIR}/hal/stm32f4/can + ${CMAKE_CURRENT_SOURCE_DIR}/interfaces/led + ${CMAKE_CURRENT_SOURCE_DIR}/interfaces/can ) # Add project symbols (macros) diff --git a/Core/Src/Backup/can.c.bak b/Core/Src/Backup/can.c.bak deleted file mode 100644 index c7f5761..0000000 --- a/Core/Src/Backup/can.c.bak +++ /dev/null @@ -1,385 +0,0 @@ -/* USER CODE BEGIN Header */ -/** - ****************************************************************************** - * @file can.c - * @brief This file provides code for the configuration - * of the CAN instances. - ****************************************************************************** - * @attention - * - * Copyright (c) 2026 STMicroelectronics. - * All rights reserved. - * - * This software is licensed under terms that can be found in the LICENSE file - * in the root directory of this software component. - * If no LICENSE file comes with this software, it is provided AS-IS. - * - ****************************************************************************** - */ -/* USER CODE END Header */ -/* Includes ------------------------------------------------------------------*/ -#include "can.h" - -/* USER CODE BEGIN 0 */ -#include -#include -#include "cmsis_os.h" - -/* USER CODE END 0 */ - -CAN_HandleTypeDef hcan2; - -/* CAN2 init function */ -void MX_CAN2_Init(void) -{ - - /* USER CODE BEGIN CAN2_Init 0 */ - - /* USER CODE END CAN2_Init 0 */ - - /* USER CODE BEGIN CAN2_Init 1 */ - - /* USER CODE END CAN2_Init 1 */ - hcan2.Instance = CAN2; - hcan2.Init.Prescaler = 6; - hcan2.Init.Mode = CAN_MODE_NORMAL; - hcan2.Init.SyncJumpWidth = CAN_SJW_1TQ; - hcan2.Init.TimeSeg1 = CAN_BS1_2TQ; - hcan2.Init.TimeSeg2 = CAN_BS2_4TQ; - hcan2.Init.TimeTriggeredMode = DISABLE; - hcan2.Init.AutoBusOff = DISABLE; - hcan2.Init.AutoWakeUp = DISABLE; - hcan2.Init.AutoRetransmission = ENABLE; - hcan2.Init.ReceiveFifoLocked = DISABLE; - hcan2.Init.TransmitFifoPriority = DISABLE; - if (HAL_CAN_Init(&hcan2) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN CAN2_Init 2 */ - CAN_ConfigFilter(&hcan2, 0, 0x0000, 0x0000); - /* USER CODE END CAN2_Init 2 */ - -} - -void HAL_CAN_MspInit(CAN_HandleTypeDef* canHandle) -{ - - GPIO_InitTypeDef GPIO_InitStruct = {0}; - if(canHandle->Instance==CAN2) - { - /* USER CODE BEGIN CAN2_MspInit 0 */ - - /* USER CODE END CAN2_MspInit 0 */ - /* CAN2 clock enable */ - __HAL_RCC_CAN2_CLK_ENABLE(); - __HAL_RCC_CAN1_CLK_ENABLE(); - - __HAL_RCC_GPIOB_CLK_ENABLE(); - /**CAN2 GPIO Configuration - PB12 ------> CAN2_RX - PB13 ------> CAN2_TX - */ - GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; - GPIO_InitStruct.Alternate = GPIO_AF9_CAN2; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - - /* USER CODE BEGIN CAN2_MspInit 1 */ - - /* USER CODE END CAN2_MspInit 1 */ - } -} - -void HAL_CAN_MspDeInit(CAN_HandleTypeDef* canHandle) -{ - - if(canHandle->Instance==CAN2) - { - /* USER CODE BEGIN CAN2_MspDeInit 0 */ - - /* USER CODE END CAN2_MspDeInit 0 */ - /* Peripheral clock disable */ - __HAL_RCC_CAN2_CLK_DISABLE(); - __HAL_RCC_CAN1_CLK_DISABLE(); - - /**CAN2 GPIO Configuration - PB12 ------> CAN2_RX - PB13 ------> CAN2_TX - */ - HAL_GPIO_DeInit(GPIOB, GPIO_PIN_12|GPIO_PIN_13); - - /* USER CODE BEGIN CAN2_MspDeInit 1 */ - - /* USER CODE END CAN2_MspDeInit 1 */ - } -} - -/* USER CODE BEGIN 1 */ - -/** - * @brief 启动 CAN 外设 - */ -HAL_StatusTypeDef CAN_Start(CAN_HandleTypeDef* hcan) -{ - return HAL_CAN_Start(hcan); -} - -/** - * @brief 配置 CAN 滤波器 - * @param hcan: CAN 句柄指针 - * @param filter_id: 滤波器编号 (0-13) - * @param id: 要接收的 ID - * @param mask: 掩码 - */ -void CAN_ConfigFilter(CAN_HandleTypeDef* hcan, uint8_t filter_id, uint16_t id, uint16_t mask) -{ - CAN_FilterTypeDef sFilterConfig; - - sFilterConfig.FilterBank = filter_id; - sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK; - sFilterConfig.FilterScale = CAN_FILTERSCALE_16BIT; - sFilterConfig.FilterIdHigh = id; - sFilterConfig.FilterIdLow = 0x0000; - sFilterConfig.FilterMaskIdHigh = mask; - sFilterConfig.FilterMaskIdLow = 0x0000; - sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0; - sFilterConfig.FilterActivation = ENABLE; - - if (HAL_CAN_ConfigFilter(hcan, &sFilterConfig) != HAL_OK) - { - Error_Handler(); - } -} - - - -/** - * @brief 发送 CAN 帧 - * @param hcan: CAN 句柄指针 - * @param id: 标准 ID (0x000-0x7FF) - * @param data: 数据指针 - * @param len: 数据长度 (0-8) - * @param timeout: 超时时间 (ms) - * @retval HAL 状态 - */ -HAL_StatusTypeDef CAN_SendFrame(CAN_HandleTypeDef* hcan, uint16_t id, const uint8_t* data, uint8_t len, uint32_t timeout) -{ - CAN_TxHeaderTypeDef TxHeader; - uint32_t TxMailbox; - - if (data == NULL || len > 8) - { - return HAL_ERROR; - } - - TxHeader.StdId = 0x0000 | id; - TxHeader.ExtId = 0x0000; - TxHeader.IDE = CAN_ID_STD; - TxHeader.RTR = CAN_RTR_DATA; - TxHeader.DLC = len; - TxHeader.TransmitGlobalTime = DISABLE; - printf("Sending CAN frame: ID=0x%03X, Data=", id); - for (uint8_t i = 0; i < len; i++) - { - printf("%02X ", data[i]); - } - printf("\r\n"); - // 等待邮箱可用 - if (HAL_CAN_GetTxMailboxesFreeLevel(hcan) == 0) - { - // 等待直到有邮箱可用 - while (HAL_CAN_GetTxMailboxesFreeLevel(hcan) == 0) - { - if (timeout > 0) - { - osDelay(1); - timeout--; - if (timeout == 0) - { - return HAL_TIMEOUT; - } - } - } - } - // 添加发送消息 - printf("Adding CAN message to mailbox...\r\n"); - if (HAL_CAN_AddTxMessage(hcan, &TxHeader, data, &TxMailbox) != HAL_OK) - { - return HAL_ERROR; - } - printf("Message added to mailbox %lu, waiting for transmission...\r\n", TxMailbox); - // 等待发送完成 - while (HAL_CAN_IsTxMessagePending(hcan, TxMailbox)) - { - if (timeout > 0) - { - osDelay(1); - timeout--; - if (timeout == 0) - { - // 超时,打印诊断信息 - printf("TX timeout! ESR=0x%08lX TSR=0x%08lX\r\n", - hcan->Instance->ESR, hcan->Instance->TSR); - printf(" TEC=%lu REC=%lu LEC=%lu\r\n", - (hcan->Instance->ESR >> 16) & 0xFF, - (hcan->Instance->ESR >> 24) & 0xFF, - (hcan->Instance->ESR >> 4) & 0x07); - printf(" TME=0x%lX BOFF=%lu EPVF=%lu EWGF=%lu\r\n", - (hcan->Instance->TSR >> 26) & 0x7, - (hcan->Instance->ESR >> 2) & 1, - (hcan->Instance->ESR >> 1) & 1, - hcan->Instance->ESR & 1); - return HAL_TIMEOUT; - } - } - } - - return HAL_OK; -} - -/** - * @brief 接收 CAN 帧 - * @param hcan: CAN 句柄指针 - * @param id: 接收到的 ID (输出) - * @param data: 数据缓冲区 (输出) - * @param len: 数据长度 (输出) - * @param timeout: 超时时间 (ms) - * @retval HAL 状态 - */ -HAL_StatusTypeDef CAN_ReceiveFrame(CAN_HandleTypeDef* hcan, uint16_t* id, uint8_t* data, uint8_t* len, uint32_t timeout) -{ - CAN_RxHeaderTypeDef RxHeader; - uint32_t start_tick; - - if (id == NULL || data == NULL || len == NULL) - { - return HAL_ERROR; - } - - start_tick = HAL_GetTick(); - - // 等待消息到达 - while (HAL_CAN_GetRxFifoFillLevel(hcan, CAN_RX_FIFO0) == 0) - { - if (timeout > 0) - { - if ((HAL_GetTick() - start_tick) >= timeout) - { - return HAL_TIMEOUT; - } - } - // 短暂延时避免死循环 - osDelay(1); - } - - // 接收消息 - if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, data) != HAL_OK) - { - return HAL_ERROR; - } - - *id = RxHeader.StdId; - *len = RxHeader.DLC; - - return HAL_OK; -} - -/** - * @brief 检查是否有接收到的消息 - * @param hcan: CAN 句柄指针 - * @retval 1: 有消息,0: 无消息 - */ -uint8_t CAN_HasMessage(CAN_HandleTypeDef* hcan) -{ - return HAL_CAN_GetRxFifoFillLevel(hcan, CAN_RX_FIFO0) > 0 ? 1 : 0; -} - -/** - * @brief 获取 CAN 错误码 - * @param hcan: CAN 句柄指针 - * @retval CAN 错误码 - */ -uint32_t CAN_GetErrorCode(CAN_HandleTypeDef* hcan) -{ - return HAL_CAN_GetError(hcan); -} - -/** - * @brief 获取 CAN 状态 - * @param hcan: CAN 句柄指针 - * @retval CAN 状态 - */ -uint32_t CAN_GetState(CAN_HandleTypeDef* hcan) -{ - return HAL_CAN_GetState(hcan); -} - -/** - * @brief 打印 CAN 详细调试信息 - * @param hcan: CAN 句柄指针 - * @param huart: 用于输出的串口句柄 - */ -void CAN_PrintDebugInfo(CAN_HandleTypeDef* hcan, UART_HandleTypeDef *huart) -{ - char buf[80]; - int len; - - len = snprintf(buf, sizeof(buf), "=== CAN Debug Info ===\r\n"); - HAL_UART_Transmit(huart, (uint8_t*)buf, len, 50); - - len = snprintf(buf, sizeof(buf), "State: %d, Error: 0x%08lX\r\n", - (int)HAL_CAN_GetState(hcan), HAL_CAN_GetError(hcan)); - HAL_UART_Transmit(huart, (uint8_t*)buf, len, 50); - - uint32_t esr = hcan->Instance->ESR; - len = snprintf(buf, sizeof(buf), "ESR: 0x%08lX\r\n", esr); - HAL_UART_Transmit(huart, (uint8_t*)buf, len, 50); - - uint32_t lec = (esr >> 4) & 0x07; - const char* lec_str[] = {"No Error", "Stuff Error", "Form Error", - "Ack Error", "Bit1 Error", "Bit0 Error", - "CRC Error", "No Change"}; - len = snprintf(buf, sizeof(buf), " LEC: %ld (%s)\r\n", lec, lec_str[lec]); - HAL_UART_Transmit(huart, (uint8_t*)buf, len, 50); - - len = snprintf(buf, sizeof(buf), " TEC: %ld, REC: %ld\r\n", - (esr >> 16) & 0xFF, (esr >> 24) & 0xFF); - HAL_UART_Transmit(huart, (uint8_t*)buf, len, 50); - - len = snprintf(buf, sizeof(buf), " BOFF: %ld, EPVF: %ld, EWGF: %ld\r\n", - (esr >> 2) & 1, (esr >> 1) & 1, esr & 1); - HAL_UART_Transmit(huart, (uint8_t*)buf, len, 50); - - uint32_t tsr = hcan->Instance->TSR; - len = snprintf(buf, sizeof(buf), "TSR: 0x%08lX\r\n", tsr); - HAL_UART_Transmit(huart, (uint8_t*)buf, len, 50); - - len = snprintf(buf, sizeof(buf), " TME: 0x%lX, LOW: 0x%lX, CODE: %ld\r\n", - (tsr >> 26) & 0x7, (tsr >> 29) & 0x7, (tsr >> 24) & 0x3); - HAL_UART_Transmit(huart, (uint8_t*)buf, len, 50); - - len = snprintf(buf, sizeof(buf), " Free mailboxes: %ld\r\n", - HAL_CAN_GetTxMailboxesFreeLevel(hcan)); - HAL_UART_Transmit(huart, (uint8_t*)buf, len, 50); - - len = snprintf(buf, sizeof(buf), "RFR0: 0x%08lX, FIFO0: %ld\r\n", - hcan->Instance->RF0R, HAL_CAN_GetRxFifoFillLevel(hcan, CAN_RX_FIFO0)); - HAL_UART_Transmit(huart, (uint8_t*)buf, len, 50); - - len = snprintf(buf, sizeof(buf), "RFR1: 0x%08lX, FIFO1: %ld\r\n", - hcan->Instance->RF1R, HAL_CAN_GetRxFifoFillLevel(hcan, CAN_RX_FIFO1)); - HAL_UART_Transmit(huart, (uint8_t*)buf, len, 50); - - len = snprintf(buf, sizeof(buf), "BTR: 0x%08lX (BRP=%ld, BS1=%ld, BS2=%ld, SJW=%ld)\r\n", - hcan->Instance->BTR, - (hcan->Instance->BTR >> 0) & 0x1FF, - (hcan->Instance->BTR >> 16) & 0x0F, - (hcan->Instance->BTR >> 20) & 0x07, - (hcan->Instance->BTR >> 24) & 0x03); - HAL_UART_Transmit(huart, (uint8_t*)buf, len, 50); -} - -/* USER CODE END 1 */ diff --git a/Core/Src/Backup/freertos.c.bak b/Core/Src/Backup/freertos.c.bak deleted file mode 100644 index 569352d..0000000 --- a/Core/Src/Backup/freertos.c.bak +++ /dev/null @@ -1,338 +0,0 @@ -/* USER CODE BEGIN Header */ -/** - ****************************************************************************** - * @file freertos.c - * @brief FreeRTOS configuration file - ****************************************************************************** - * @attention - * - * Copyright (c) 2026 STMicroelectronics. - * All rights reserved. - * - * This software is licensed under terms that can be found in the LICENSE file - * in the root directory of this software component. - * If no LICENSE file comes with this software, it is provided AS-IS. - * - ****************************************************************************** - */ -/* USER CODE END Header */ - -/* Includes ------------------------------------------------------------------*/ -#include "FreeRTOS.h" -#include "task.h" -#include "main.h" -#include "cmsis_os.h" - -/* Private includes ----------------------------------------------------------*/ -/* USER CODE BEGIN Includes */ -#include -#include -#include -#include "usart.h" -#include "can.h" -#include "j901p.h" -#include "j901p_hal.h" -#include "mf4010v2.h" -#include "mf4010v2_hal.h" -/* USER CODE END Includes */ - -/* Private typedef -----------------------------------------------------------*/ -/* USER CODE BEGIN PTD */ - -/* USER CODE END PTD */ - -/* Private define ------------------------------------------------------------*/ -/* USER CODE BEGIN PD */ - -/* USER CODE END PD */ - -/* Private macro -------------------------------------------------------------*/ -/* USER CODE BEGIN PM */ - -/* USER CODE END PM */ - -/* Private variables ---------------------------------------------------------*/ -/* USER CODE BEGIN Variables */ -volatile uint8_t s_cDataUpdate; -void CopeSensorData(uint32_t uiReg, uint32_t uiRegNum); - -// 传感器数据结构(用于任务间通信) -typedef struct { - int16_t acc[3]; // 加速度计原始数据 - int16_t gyro[3]; // 陀螺仪原始数据 - int16_t angle[3]; // 角度数据 - uint8_t valid; // 数据有效标志 -} SensorData_TypeDef; - -SensorData_TypeDef sensorData = {{0}, {0}, {0}, 0}; - -// 电机实例 -mf4010v2_t motor1; // z轴电机 -mf4010v2_t motor2; // xy轴电机 -/* USER CODE END Variables */ -/* Definitions for monitorTask */ -osThreadId_t monitorTaskHandle; -const osThreadAttr_t monitorTask_attributes = { - .name = "monitorTask", - .stack_size = 256 * 4, - .priority = (osPriority_t) osPriorityLow, -}; -/* Definitions for controlTask */ -osThreadId_t controlTaskHandle; -const osThreadAttr_t controlTask_attributes = { - .name = "controlTask", - .stack_size = 256 * 4, - .priority = (osPriority_t) osPriorityNormal, -}; -/* Definitions for sensorTask */ -osThreadId_t sensorTaskHandle; -const osThreadAttr_t sensorTask_attributes = { - .name = "sensorTask", - .stack_size = 256 * 4, - .priority = (osPriority_t) osPriorityHigh, -}; - -/* Private function prototypes -----------------------------------------------*/ -/* USER CODE BEGIN FunctionPrototypes */ - -/* USER CODE END FunctionPrototypes */ - -void StartMonitorTask(void *argument); -void StartControlTask(void *argument); -void StartSensorTask(void *argument); - -void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */ - -/** - * @brief FreeRTOS initialization - * @param None - * @retval None - */ -void MX_FREERTOS_Init(void) { - /* USER CODE BEGIN Init */ - - /* USER CODE END Init */ - - /* USER CODE BEGIN RTOS_MUTEX */ - /* add mutexes, ... */ - /* USER CODE END RTOS_MUTEX */ - - /* USER CODE BEGIN RTOS_SEMAPHORES */ - /* add semaphores, ... */ - /* USER CODE END RTOS_SEMAPHORES */ - - /* USER CODE BEGIN RTOS_TIMERS */ - /* start timers, add new ones, ... */ - /* USER CODE END RTOS_TIMERS */ - - /* USER CODE BEGIN RTOS_QUEUES */ - /* add queues, ... */ - /* USER CODE END RTOS_QUEUES */ - - /* Create the thread(s) */ - /* creation of monitorTask */ - monitorTaskHandle = osThreadNew(StartMonitorTask, NULL, &monitorTask_attributes); - - /* creation of controlTask */ - controlTaskHandle = osThreadNew(StartControlTask, NULL, &controlTask_attributes); - - /* creation of sensorTask */ - sensorTaskHandle = osThreadNew(StartSensorTask, NULL, &sensorTask_attributes); - - /* USER CODE BEGIN RTOS_THREADS */ - /* add threads, ... */ - /* USER CODE END RTOS_THREADS */ - - /* USER CODE BEGIN RTOS_EVENTS */ - /* add events, ... */ - /* USER CODE END RTOS_EVENTS */ - -} - -/* USER CODE BEGIN Header_StartMonitorTask */ -/** - * @brief Function implementing the monitorTask thread. - * @param argument: Not used - * @retval None - */ -/* USER CODE END Header_StartMonitorTask */ -void StartMonitorTask(void *argument) -{ - /* USER CODE BEGIN StartMonitorTask */ - argument = argument; - // 等待传感器数据初始化 - osDelay(500); - - const uint8_t monitor_start[] = "Monitor task started\r\n"; - HAL_UART_Transmit(&huart1, monitor_start, sizeof(monitor_start)-1, 100); - - for(;;) - { - // 等待有效数据 - if(sensorData.valid) - { - // 输出加速度 - const uint8_t acc_msg[] = "ACC: "; - HAL_UART_Transmit(&huart1, acc_msg, sizeof(acc_msg)-1, 50); - - for(int i=0; i<3; i++) - { - int32_t acc_scaled = (sensorData.acc[i] * 16 * 1000) / 32768; - int32_t acc_int = acc_scaled / 1000; - int32_t acc_frac = acc_scaled % 1000; - if(acc_frac < 0) acc_frac = -acc_frac; - - char acc_str[20]; - int acc_len = snprintf(acc_str, sizeof(acc_str), "%ld.%03ld ", acc_int, acc_frac); - HAL_UART_Transmit(&huart1, (uint8_t*)acc_str, acc_len, 50); - } - HAL_UART_Transmit(&huart1, (uint8_t*)"\r\n", 2, 50); - - // 输出角速度 - const uint8_t gyro_msg[] = "GYRO: "; - HAL_UART_Transmit(&huart1, gyro_msg, sizeof(gyro_msg)-1, 50); - - for(int i=0; i<3; i++) - { - int32_t gyro_scaled = (sensorData.gyro[i] * 2000 * 1000) / 32768; - int32_t gyro_int = gyro_scaled / 1000; - int32_t gyro_frac = gyro_scaled % 1000; - if(gyro_frac < 0) gyro_frac = -gyro_frac; - - char gyro_str[20]; - int gyro_len = snprintf(gyro_str, sizeof(gyro_str), "%ld.%03ld ", gyro_int, gyro_frac); - HAL_UART_Transmit(&huart1, (uint8_t*)gyro_str, gyro_len, 50); - } - HAL_UART_Transmit(&huart1, (uint8_t*)"\r\n", 2, 50); - - // 输出角度 - const uint8_t angle_msg[] = "ANGLE: "; - HAL_UART_Transmit(&huart1, angle_msg, sizeof(angle_msg)-1, 50); - - for(int i=0; i<3; i++) - { - int32_t angle_scaled = (sensorData.angle[i] * 180 * 1000) / 32768; - int32_t angle_int = angle_scaled / 1000; - int32_t angle_frac = angle_scaled % 1000; - if(angle_frac < 0) angle_frac = -angle_frac; - - char angle_str[20]; - int angle_len = snprintf(angle_str, sizeof(angle_str), "%ld.%03ld ", angle_int, angle_frac); - HAL_UART_Transmit(&huart1, (uint8_t*)angle_str, angle_len, 50); - } - HAL_UART_Transmit(&huart1, (uint8_t*)"\r\n", 2, 50); - - sensorData.valid = 0; // 清除标志,等待下次更新 - } - - osDelay(200); // 200ms 输出间隔 - } - /* USER CODE END StartMonitorTask */ -} - -/* USER CODE BEGIN Header_StartControlTask */ -/** -* @brief Function implementing the controlTask thread. -* @param argument: Not used -* @retval None -*/ -/* USER CODE END Header_StartControlTask */ -void StartControlTask(void *argument) -{ - /* USER CODE BEGIN StartControlTask */ - argument = argument; - - osDelay(500); - - // 初始化电机 - const uint8_t motor_init[] = "Initializing motor...\r\n"; - HAL_UART_Transmit(&huart1, motor_init, sizeof(motor_init)-1, 100); - size_t len; - mf4010_status_t status; - status = mf4010v2_init(&motor1, 0x141); - if(status != MF4010_OK) { - char err_code[32]; - len = snprintf(err_code, sizeof(err_code), "Motor1 init: %d\r\n", status); - HAL_UART_Transmit(&huart1, (uint8_t*)err_code, len, 50); - } - status = mf4010v2_init(&motor2, 0x142); - if(status != MF4010_OK) { - char err_code[32]; - len = snprintf(err_code, sizeof(err_code), "Motor2 init: %d\r\n", status); - HAL_UART_Transmit(&huart1, (uint8_t*)err_code, len, 50); - } - - - osDelay(200); - - // 测试发送电机启动命令 - const uint8_t cmd[] = COMMAND_MOTOR_CLOSE; - status = mf4010v2_send_command(&motor2, cmd, 1000); - if (status != MF4010_OK) { - char err_code[32]; - len = snprintf(err_code, sizeof(err_code), "Motor2 command: %d\r\n", status); - HAL_UART_Transmit(&huart1, (uint8_t*)err_code, len, 50); - } - status = mf4010v2_send_command(&motor1, cmd, 1000); - if (status != MF4010_OK) { - char err_code[32]; - len = snprintf(err_code, sizeof(err_code), "Motor1 command: %d\r\n", status); - HAL_UART_Transmit(&huart1, (uint8_t*)err_code, len, 50); - } - for (;;) { - osDelay(1); - } - - /* USER CODE END StartControlTask */ -} - -/* USER CODE BEGIN Header_StartSensorTask */ -/** -* @brief Function implementing the sensorTask thread. -* @param argument: Not used -* @retval None -*/ -/* USER CODE END Header_StartSensorTask */ -void StartSensorTask(void *argument) -{ - /* USER CODE BEGIN StartSensorTask */ - argument = argument; - // 初始化传感器 - WitRegisterCallBack(CopeSensorData); - WitDelayMsRegister(J901P_Delay); - J901P_HAL_Init(0x50); - - WitReadReg(AX, 12); - osDelay(100); - - for(;;) - { - if(s_cDataUpdate) - { - WitReadReg(AX, 12); - osDelay(50); - - // 复制数据到共享结构 - for(int i=0; i<3; i++) { - sensorData.acc[i] = sReg[AX+i]; - sensorData.gyro[i] = sReg[GX+i]; - sensorData.angle[i] = sReg[Roll+i]; - } - sensorData.valid = 1; // 标记数据有效 - - s_cDataUpdate = 0; - osDelay(200); - } - else - { - WitReadReg(AX, 12); - osDelay(100); - } - } - /* USER CODE END StartSensorTask */ -} - -/* Private application code --------------------------------------------------*/ -/* USER CODE BEGIN Application */ -/* USER CODE END Application */ - diff --git a/Core/Src/Backup/gpio.c.bak b/Core/Src/Backup/gpio.c.bak deleted file mode 100644 index f7b4ca4..0000000 --- a/Core/Src/Backup/gpio.c.bak +++ /dev/null @@ -1,68 +0,0 @@ -/* USER CODE BEGIN Header */ -/** - ****************************************************************************** - * @file gpio.c - * @brief This file provides code for the configuration - * of all used GPIO pins. - ****************************************************************************** - * @attention - * - * Copyright (c) 2026 STMicroelectronics. - * All rights reserved. - * - * This software is licensed under terms that can be found in the LICENSE file - * in the root directory of this software component. - * If no LICENSE file comes with this software, it is provided AS-IS. - * - ****************************************************************************** - */ -/* USER CODE END Header */ - -/* Includes ------------------------------------------------------------------*/ -#include "gpio.h" - -/* USER CODE BEGIN 0 */ - -/* USER CODE END 0 */ - -/*----------------------------------------------------------------------------*/ -/* Configure GPIO */ -/*----------------------------------------------------------------------------*/ -/* USER CODE BEGIN 1 */ - -/* USER CODE END 1 */ - -/** Configure pins as - * Analog - * Input - * Output - * EVENT_OUT - * EXTI - PD0 ------> CAN1_RX - PD1 ------> CAN1_TX -*/ -void MX_GPIO_Init(void) -{ - - GPIO_InitTypeDef GPIO_InitStruct = {0}; - - /* GPIO Ports Clock Enable */ - __HAL_RCC_GPIOF_CLK_ENABLE(); - __HAL_RCC_GPIOH_CLK_ENABLE(); - __HAL_RCC_GPIOB_CLK_ENABLE(); - __HAL_RCC_GPIOA_CLK_ENABLE(); - __HAL_RCC_GPIOD_CLK_ENABLE(); - - /*Configure GPIO pins : PD0 PD1 */ - GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; - GPIO_InitStruct.Alternate = GPIO_AF9_CAN1; - HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); - -} - -/* USER CODE BEGIN 2 */ - -/* USER CODE END 2 */ diff --git a/Core/Src/Backup/i2c.c.bak b/Core/Src/Backup/i2c.c.bak deleted file mode 100644 index 8e34a38..0000000 --- a/Core/Src/Backup/i2c.c.bak +++ /dev/null @@ -1,122 +0,0 @@ -/* USER CODE BEGIN Header */ -/** - ****************************************************************************** - * @file i2c.c - * @brief This file provides code for the configuration - * of the I2C instances. - ****************************************************************************** - * @attention - * - * Copyright (c) 2026 STMicroelectronics. - * All rights reserved. - * - * This software is licensed under terms that can be found in the LICENSE file - * in the root directory of this software component. - * If no LICENSE file comes with this software, it is provided AS-IS. - * - ****************************************************************************** - */ -/* USER CODE END Header */ -/* Includes ------------------------------------------------------------------*/ -#include "i2c.h" - -/* USER CODE BEGIN 0 */ - -/* USER CODE END 0 */ - -I2C_HandleTypeDef hi2c2; - -/* I2C2 init function */ -void MX_I2C2_Init(void) -{ - - /* USER CODE BEGIN I2C2_Init 0 */ - - /* USER CODE END I2C2_Init 0 */ - - /* USER CODE BEGIN I2C2_Init 1 */ - - /* USER CODE END I2C2_Init 1 */ - hi2c2.Instance = I2C2; - hi2c2.Init.ClockSpeed = 400000; - hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2; - hi2c2.Init.OwnAddress1 = 0; - hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; - hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; - hi2c2.Init.OwnAddress2 = 0; - hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; - hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; - if (HAL_I2C_Init(&hi2c2) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN I2C2_Init 2 */ - - /* USER CODE END I2C2_Init 2 */ - -} - -void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle) -{ - - GPIO_InitTypeDef GPIO_InitStruct = {0}; - if(i2cHandle->Instance==I2C2) - { - /* USER CODE BEGIN I2C2_MspInit 0 */ - - /* USER CODE END I2C2_MspInit 0 */ - - __HAL_RCC_GPIOF_CLK_ENABLE(); - /**I2C2 GPIO Configuration - PF0 ------> I2C2_SDA - PF1 ------> I2C2_SCL - */ - GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1; - GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; - GPIO_InitStruct.Alternate = GPIO_AF4_I2C2; - HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); - - /* I2C2 clock enable */ - __HAL_RCC_I2C2_CLK_ENABLE(); - - /* I2C2 interrupt Init */ - HAL_NVIC_SetPriority(I2C2_EV_IRQn, 5, 0); - HAL_NVIC_EnableIRQ(I2C2_EV_IRQn); - /* USER CODE BEGIN I2C2_MspInit 1 */ - - /* USER CODE END I2C2_MspInit 1 */ - } -} - -void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle) -{ - - if(i2cHandle->Instance==I2C2) - { - /* USER CODE BEGIN I2C2_MspDeInit 0 */ - - /* USER CODE END I2C2_MspDeInit 0 */ - /* Peripheral clock disable */ - __HAL_RCC_I2C2_CLK_DISABLE(); - - /**I2C2 GPIO Configuration - PF0 ------> I2C2_SDA - PF1 ------> I2C2_SCL - */ - HAL_GPIO_DeInit(GPIOF, GPIO_PIN_0); - - HAL_GPIO_DeInit(GPIOF, GPIO_PIN_1); - - /* I2C2 interrupt Deinit */ - HAL_NVIC_DisableIRQ(I2C2_EV_IRQn); - /* USER CODE BEGIN I2C2_MspDeInit 1 */ - - /* USER CODE END I2C2_MspDeInit 1 */ - } -} - -/* USER CODE BEGIN 1 */ - -/* USER CODE END 1 */ diff --git a/Core/Src/Backup/main.c.bak b/Core/Src/Backup/main.c.bak deleted file mode 100644 index a45a98f..0000000 --- a/Core/Src/Backup/main.c.bak +++ /dev/null @@ -1,249 +0,0 @@ -/* USER CODE BEGIN Header */ -/** - ****************************************************************************** - * @file : main.c - * @brief : Main program body - ****************************************************************************** - * @attention - * - * Copyright (c) 2026 STMicroelectronics. - * All rights reserved. - * - * This software is licensed under terms that can be found in the LICENSE file - * in the root directory of this software component. - * If no LICENSE file comes with this software, it is provided AS-IS. - * - ****************************************************************************** - */ -/* USER CODE END Header */ -/* Includes ------------------------------------------------------------------*/ -#include "main.h" -#include "cmsis_os.h" -#include "can.h" -#include "i2c.h" -#include "usart.h" -#include "gpio.h" - -/* Private includes ----------------------------------------------------------*/ -/* USER CODE BEGIN Includes */ -#include "j901p.h" -#include "j901p_hal.h" -/* USER CODE END Includes */ - -/* Private typedef -----------------------------------------------------------*/ -/* USER CODE BEGIN PTD */ - -/* USER CODE END PTD */ - -/* Private define ------------------------------------------------------------*/ -/* USER CODE BEGIN PD */ - -/* USER CODE END PD */ - -/* Private macro -------------------------------------------------------------*/ -/* USER CODE BEGIN PM */ - -/* USER CODE END PM */ - -/* Private variables ---------------------------------------------------------*/ - -/* USER CODE BEGIN PV */ -volatile uint8_t s_cDataUpdate = 0; -/* USER CODE END PV */ - -/* Private function prototypes -----------------------------------------------*/ -void SystemClock_Config(void); -void MX_FREERTOS_Init(void); -/* USER CODE BEGIN PFP */ -int __io_putchar(int ch) -{ - HAL_UART_Transmit(&huart1, (uint8_t*)&ch, 1, 50); - return ch; -} -/* USER CODE END PFP */ - -/* Private user code ---------------------------------------------------------*/ -/* USER CODE BEGIN 0 */ - -void CopeSensorData(uint32_t uiReg, uint32_t uiRegNum); - -void CopeSensorData(uint32_t uiReg, uint32_t uiRegNum) -{ - for(uint32_t i = 0; i < uiRegNum; i++) - { - switch(uiReg + i) - { - case AZ: s_cDataUpdate |= 0x01; break; - case GZ: s_cDataUpdate |= 0x02; break; - case Yaw: s_cDataUpdate |= 0x04; break; - default: break; - } - } -} - -/* 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_I2C2_Init(); - MX_USART1_UART_Init(); - MX_CAN1_Init(); - MX_CAN2_Init(); - /* USER CODE BEGIN 2 */ - - const uint8_t start_msg[] = "System starting...\r\n"; - HAL_UART_Transmit(&huart1, start_msg, sizeof(start_msg) - 1, 100); - - /* USER CODE END 2 */ - - /* Init scheduler */ - osKernelInitialize(); - - /* Call init function for freertos objects (in cmsis_os2.c) */ - MX_FREERTOS_Init(); - - /* Start scheduler */ - osKernelStart(); - - /* We should never get here as control is now taken by the scheduler */ - - /* Infinite loop */ - /* USER CODE BEGIN WHILE */ - while (1) - { - /* USER CODE END WHILE */ - - /* USER CODE BEGIN 3 */ - } - /* USER CODE END 3 */ -} - -/** - * @brief System Clock Configuration - * @retval None - */ -void SystemClock_Config(void) -{ - RCC_OscInitTypeDef RCC_OscInitStruct = {0}; - RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; - - /** Configure the main internal regulator output voltage - */ - __HAL_RCC_PWR_CLK_ENABLE(); - __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); - - /** 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.PLL.PLLState = RCC_PLL_ON; - RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; - RCC_OscInitStruct.PLL.PLLM = 4; - RCC_OscInitStruct.PLL.PLLN = 168; - RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; - RCC_OscInitStruct.PLL.PLLQ = 4; - 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_DIV4; - RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; - - if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) - { - Error_Handler(); - } -} - -/* USER CODE BEGIN 4 */ - -/* USER CODE END 4 */ - -/** - * @brief Period elapsed callback in non blocking mode - * @note This function is called when TIM1 interrupt took place, inside - * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment - * a global variable "uwTick" used as application time base. - * @param htim : TIM handle - * @retval None - */ -void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) -{ - /* USER CODE BEGIN Callback 0 */ - - /* USER CODE END Callback 0 */ - if (htim->Instance == TIM1) - { - HAL_IncTick(); - } - /* USER CODE BEGIN Callback 1 */ - - /* USER CODE END Callback 1 */ -} - -/** - * @brief This function is executed in case of error occurrence. - * @retval None - */ -void Error_Handler(void) -{ - /* USER CODE BEGIN Error_Handler_Debug */ - /* User can add his own implementation to report the HAL error return state */ - __disable_irq(); - while (1) - { - } - /* 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 */ diff --git a/Core/Src/Backup/stm32f4xx_hal_msp.c.bak b/Core/Src/Backup/stm32f4xx_hal_msp.c.bak deleted file mode 100644 index a4e2b79..0000000 --- a/Core/Src/Backup/stm32f4xx_hal_msp.c.bak +++ /dev/null @@ -1,84 +0,0 @@ -/* USER CODE BEGIN Header */ -/** - ****************************************************************************** - * @file stm32f4xx_hal_msp.c - * @brief This file provides code for the MSP Initialization - * and de-Initialization codes. - ****************************************************************************** - * @attention - * - * Copyright (c) 2026 STMicroelectronics. - * All rights reserved. - * - * This software is licensed under terms that can be found in the LICENSE file - * in the root directory of this software component. - * If no LICENSE file comes with this software, it is provided AS-IS. - * - ****************************************************************************** - */ -/* USER CODE END Header */ - -/* Includes ------------------------------------------------------------------*/ -#include "main.h" -/* USER CODE BEGIN Includes */ - -/* USER CODE END Includes */ - -/* Private typedef -----------------------------------------------------------*/ -/* USER CODE BEGIN TD */ - -/* USER CODE END TD */ - -/* Private define ------------------------------------------------------------*/ -/* USER CODE BEGIN Define */ - -/* USER CODE END Define */ - -/* Private macro -------------------------------------------------------------*/ -/* USER CODE BEGIN Macro */ - -/* USER CODE END Macro */ - -/* Private variables ---------------------------------------------------------*/ -/* USER CODE BEGIN PV */ - -/* USER CODE END PV */ - -/* Private function prototypes -----------------------------------------------*/ -/* USER CODE BEGIN PFP */ - -/* USER CODE END PFP */ - -/* External functions --------------------------------------------------------*/ -/* USER CODE BEGIN ExternalFunctions */ - -/* USER CODE END ExternalFunctions */ - -/* USER CODE BEGIN 0 */ - -/* USER CODE END 0 */ -/** - * Initializes the Global MSP. - */ -void HAL_MspInit(void) -{ - - /* USER CODE BEGIN MspInit 0 */ - - /* USER CODE END MspInit 0 */ - - __HAL_RCC_SYSCFG_CLK_ENABLE(); - __HAL_RCC_PWR_CLK_ENABLE(); - - /* System interrupt init*/ - /* PendSV_IRQn interrupt configuration */ - HAL_NVIC_SetPriority(PendSV_IRQn, 15, 0); - - /* USER CODE BEGIN MspInit 1 */ - - /* USER CODE END MspInit 1 */ -} - -/* USER CODE BEGIN 1 */ - -/* USER CODE END 1 */ diff --git a/Core/Src/Backup/stm32f4xx_hal_timebase_tim.c.bak b/Core/Src/Backup/stm32f4xx_hal_timebase_tim.c.bak deleted file mode 100644 index 9b4d703..0000000 --- a/Core/Src/Backup/stm32f4xx_hal_timebase_tim.c.bak +++ /dev/null @@ -1,128 +0,0 @@ -/* USER CODE BEGIN Header */ -/** - ****************************************************************************** - * @file stm32f4xx_hal_timebase_tim.c - * @brief HAL time base based on the hardware TIM. - ****************************************************************************** - * @attention - * - * Copyright (c) 2026 STMicroelectronics. - * All rights reserved. - * - * This software is licensed under terms that can be found in the LICENSE file - * in the root directory of this software component. - * If no LICENSE file comes with this software, it is provided AS-IS. - * - ****************************************************************************** - */ -/* USER CODE END Header */ - -/* Includes ------------------------------------------------------------------*/ -#include "stm32f4xx_hal.h" -#include "stm32f4xx_hal_tim.h" - -/* Private typedef -----------------------------------------------------------*/ -/* Private define ------------------------------------------------------------*/ -/* Private macro -------------------------------------------------------------*/ -/* Private variables ---------------------------------------------------------*/ -TIM_HandleTypeDef htim1; -/* Private function prototypes -----------------------------------------------*/ -/* Private functions ---------------------------------------------------------*/ - -/** - * @brief This function configures the TIM1 as a time base source. - * The time source is configured to have 1ms time base with a dedicated - * Tick interrupt priority. - * @note This function is called automatically at the beginning of program after - * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig(). - * @param TickPriority: Tick interrupt priority. - * @retval HAL status - */ -HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) -{ - RCC_ClkInitTypeDef clkconfig; - uint32_t uwTimclock = 0U; - - uint32_t uwPrescalerValue = 0U; - uint32_t pFLatency; - - HAL_StatusTypeDef status; - - /* Enable TIM1 clock */ - __HAL_RCC_TIM1_CLK_ENABLE(); - - /* Get clock configuration */ - HAL_RCC_GetClockConfig(&clkconfig, &pFLatency); - - /* Compute TIM1 clock */ - uwTimclock = 2*HAL_RCC_GetPCLK2Freq(); - - /* Compute the prescaler value to have TIM1 counter clock equal to 1MHz */ - uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U); - - /* Initialize TIM1 */ - htim1.Instance = TIM1; - - /* Initialize TIMx peripheral as follow: - * Period = [(TIM1CLK/1000) - 1]. to have a (1/1000) s time base. - * Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock. - * ClockDivision = 0 - * Counter direction = Up - */ - htim1.Init.Period = (1000000U / 1000U) - 1U; - htim1.Init.Prescaler = uwPrescalerValue; - htim1.Init.ClockDivision = 0; - htim1.Init.CounterMode = TIM_COUNTERMODE_UP; - htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; - - status = HAL_TIM_Base_Init(&htim1); - if (status == HAL_OK) - { - /* Start the TIM time Base generation in interrupt mode */ - status = HAL_TIM_Base_Start_IT(&htim1); - if (status == HAL_OK) - { - /* Enable the TIM1 global Interrupt */ - HAL_NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn); - /* Configure the SysTick IRQ priority */ - if (TickPriority < (1UL << __NVIC_PRIO_BITS)) - { - /* Configure the TIM IRQ priority */ - HAL_NVIC_SetPriority(TIM1_UP_TIM10_IRQn, TickPriority, 0U); - uwTickPrio = TickPriority; - } - else - { - status = HAL_ERROR; - } - } - } - - /* Return function status */ - return status; -} - -/** - * @brief Suspend Tick increment. - * @note Disable the tick increment by disabling TIM1 update interrupt. - * @param None - * @retval None - */ -void HAL_SuspendTick(void) -{ - /* Disable TIM1 update Interrupt */ - __HAL_TIM_DISABLE_IT(&htim1, TIM_IT_UPDATE); -} - -/** - * @brief Resume Tick increment. - * @note Enable the tick increment by Enabling TIM1 update interrupt. - * @param None - * @retval None - */ -void HAL_ResumeTick(void) -{ - /* Enable TIM1 Update interrupt */ - __HAL_TIM_ENABLE_IT(&htim1, TIM_IT_UPDATE); -} - diff --git a/Core/Src/Backup/stm32f4xx_it.c.bak b/Core/Src/Backup/stm32f4xx_it.c.bak deleted file mode 100644 index 119c5d2..0000000 --- a/Core/Src/Backup/stm32f4xx_it.c.bak +++ /dev/null @@ -1,193 +0,0 @@ -/* USER CODE BEGIN Header */ -/** - ****************************************************************************** - * @file stm32f4xx_it.c - * @brief Interrupt Service Routines. - ****************************************************************************** - * @attention - * - * Copyright (c) 2026 STMicroelectronics. - * All rights reserved. - * - * This software is licensed under terms that can be found in the LICENSE file - * in the root directory of this software component. - * If no LICENSE file comes with this software, it is provided AS-IS. - * - ****************************************************************************** - */ -/* USER CODE END Header */ - -/* Includes ------------------------------------------------------------------*/ -#include "main.h" -#include "stm32f4xx_it.h" -/* Private includes ----------------------------------------------------------*/ -/* USER CODE BEGIN Includes */ -/* USER CODE END Includes */ - -/* Private typedef -----------------------------------------------------------*/ -/* USER CODE BEGIN TD */ - -/* USER CODE END TD */ - -/* Private define ------------------------------------------------------------*/ -/* USER CODE BEGIN PD */ - -/* USER CODE END PD */ - -/* Private macro -------------------------------------------------------------*/ -/* USER CODE BEGIN PM */ - -/* USER CODE END PM */ - -/* Private variables ---------------------------------------------------------*/ -/* USER CODE BEGIN PV */ - -/* USER CODE END PV */ - -/* Private function prototypes -----------------------------------------------*/ -/* USER CODE BEGIN PFP */ - -/* USER CODE END PFP */ - -/* Private user code ---------------------------------------------------------*/ -/* USER CODE BEGIN 0 */ - -/* USER CODE END 0 */ - -/* External variables --------------------------------------------------------*/ -extern I2C_HandleTypeDef hi2c2; -extern TIM_HandleTypeDef htim1; - -/* USER CODE BEGIN EV */ - -/* USER CODE END EV */ - -/******************************************************************************/ -/* Cortex-M4 Processor Interruption and Exception Handlers */ -/******************************************************************************/ -/** - * @brief This function handles Non maskable interrupt. - */ -void NMI_Handler(void) -{ - /* USER CODE BEGIN NonMaskableInt_IRQn 0 */ - - /* USER CODE END NonMaskableInt_IRQn 0 */ - /* USER CODE BEGIN NonMaskableInt_IRQn 1 */ - while (1) - { - } - /* USER CODE END NonMaskableInt_IRQn 1 */ -} - -/** - * @brief This function handles Hard fault interrupt. - */ -void HardFault_Handler(void) -{ - /* USER CODE BEGIN HardFault_IRQn 0 */ - - /* USER CODE END HardFault_IRQn 0 */ - while (1) - { - /* USER CODE BEGIN W1_HardFault_IRQn 0 */ - /* USER CODE END W1_HardFault_IRQn 0 */ - } -} - -/** - * @brief This function handles Memory management fault. - */ -void MemManage_Handler(void) -{ - /* USER CODE BEGIN MemoryManagement_IRQn 0 */ - - /* USER CODE END MemoryManagement_IRQn 0 */ - while (1) - { - /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */ - /* USER CODE END W1_MemoryManagement_IRQn 0 */ - } -} - -/** - * @brief This function handles Pre-fetch fault, memory access fault. - */ -void BusFault_Handler(void) -{ - /* USER CODE BEGIN BusFault_IRQn 0 */ - - /* USER CODE END BusFault_IRQn 0 */ - while (1) - { - /* USER CODE BEGIN W1_BusFault_IRQn 0 */ - /* USER CODE END W1_BusFault_IRQn 0 */ - } -} - -/** - * @brief This function handles Undefined instruction or illegal state. - */ -void UsageFault_Handler(void) -{ - /* USER CODE BEGIN UsageFault_IRQn 0 */ - - /* USER CODE END UsageFault_IRQn 0 */ - while (1) - { - /* USER CODE BEGIN W1_UsageFault_IRQn 0 */ - /* USER CODE END W1_UsageFault_IRQn 0 */ - } -} - -/** - * @brief This function handles Debug monitor. - */ -void DebugMon_Handler(void) -{ - /* USER CODE BEGIN DebugMonitor_IRQn 0 */ - - /* USER CODE END DebugMonitor_IRQn 0 */ - /* USER CODE BEGIN DebugMonitor_IRQn 1 */ - - /* USER CODE END DebugMonitor_IRQn 1 */ -} - -/******************************************************************************/ -/* STM32F4xx Peripheral Interrupt Handlers */ -/* Add here the Interrupt Handlers for the used peripherals. */ -/* For the available peripheral interrupt handler names, */ -/* please refer to the startup file (startup_stm32f4xx.s). */ -/******************************************************************************/ - -/** - * @brief This function handles TIM1 update interrupt and TIM10 global interrupt. - */ -void TIM1_UP_TIM10_IRQHandler(void) -{ - /* USER CODE BEGIN TIM1_UP_TIM10_IRQn 0 */ - - /* USER CODE END TIM1_UP_TIM10_IRQn 0 */ - HAL_TIM_IRQHandler(&htim1); - /* USER CODE BEGIN TIM1_UP_TIM10_IRQn 1 */ - - /* USER CODE END TIM1_UP_TIM10_IRQn 1 */ -} - -/** - * @brief This function handles I2C2 event interrupt. - */ -void I2C2_EV_IRQHandler(void) -{ - /* USER CODE BEGIN I2C2_EV_IRQn 0 */ - - /* USER CODE END I2C2_EV_IRQn 0 */ - HAL_I2C_EV_IRQHandler(&hi2c2); - /* USER CODE BEGIN I2C2_EV_IRQn 1 */ - - /* USER CODE END I2C2_EV_IRQn 1 */ -} - -/* USER CODE BEGIN 1 */ - -/* USER CODE END 1 */ diff --git a/Core/Src/Backup/syscalls.c.bak b/Core/Src/Backup/syscalls.c.bak deleted file mode 100644 index e33a849..0000000 --- a/Core/Src/Backup/syscalls.c.bak +++ /dev/null @@ -1,176 +0,0 @@ -/** - ****************************************************************************** - * @file syscalls.c - * @author Auto-generated by STM32CubeMX - * @brief Minimal System calls file - * - * For more information about which c-functions - * need which of these lowlevel functions - * please consult the Newlib libc-manual - ****************************************************************************** - * @attention - * - * Copyright (c) 2020-2024 STMicroelectronics. - * All rights reserved. - * - * This software is licensed under terms that can be found in the LICENSE file - * in the root directory of this software component. - * If no LICENSE file comes with this software, it is provided AS-IS. - * - ****************************************************************************** - */ - -/* Includes */ -#include -#include -#include -#include -#include -#include -#include -#include - - -/* Variables */ -extern int __io_putchar(int ch) __attribute__((weak)); -extern int __io_getchar(void) __attribute__((weak)); - - -char *__env[1] = { 0 }; -char **environ = __env; - - -/* Functions */ -void initialise_monitor_handles() -{ -} - -int _getpid(void) -{ - return 1; -} - -int _kill(int pid, int sig) -{ - (void)pid; - (void)sig; - errno = EINVAL; - return -1; -} - -void _exit (int status) -{ - _kill(status, -1); - while (1) {} /* Make sure we hang here */ -} - -__attribute__((weak)) int _read(int file, char *ptr, int len) -{ - (void)file; - int DataIdx; - - for (DataIdx = 0; DataIdx < len; DataIdx++) - { - *ptr++ = __io_getchar(); - } - - return len; -} - -__attribute__((weak)) int _write(int file, char *ptr, int len) -{ - (void)file; - int DataIdx; - - for (DataIdx = 0; DataIdx < len; DataIdx++) - { - __io_putchar(*ptr++); - } - return len; -} - -int _close(int file) -{ - (void)file; - return -1; -} - - -int _fstat(int file, struct stat *st) -{ - (void)file; - st->st_mode = S_IFCHR; - return 0; -} - -int _isatty(int file) -{ - (void)file; - return 1; -} - -int _lseek(int file, int ptr, int dir) -{ - (void)file; - (void)ptr; - (void)dir; - return 0; -} - -int _open(char *path, int flags, ...) -{ - (void)path; - (void)flags; - /* Pretend like we always fail */ - return -1; -} - -int _wait(int *status) -{ - (void)status; - errno = ECHILD; - return -1; -} - -int _unlink(char *name) -{ - (void)name; - errno = ENOENT; - return -1; -} - -int _times(struct tms *buf) -{ - (void)buf; - return -1; -} - -int _stat(char *file, struct stat *st) -{ - (void)file; - st->st_mode = S_IFCHR; - return 0; -} - -int _link(char *old, char *new) -{ - (void)old; - (void)new; - errno = EMLINK; - return -1; -} - -int _fork(void) -{ - errno = EAGAIN; - return -1; -} - -int _execve(char *name, char **argv, char **env) -{ - (void)name; - (void)argv; - (void)env; - errno = ENOMEM; - return -1; -} diff --git a/Core/Src/Backup/sysmem.c.bak b/Core/Src/Backup/sysmem.c.bak deleted file mode 100644 index 246470e..0000000 --- a/Core/Src/Backup/sysmem.c.bak +++ /dev/null @@ -1,79 +0,0 @@ -/** - ****************************************************************************** - * @file sysmem.c - * @author Generated by STM32CubeMX - * @brief System Memory calls file - * - * For more information about which C functions - * need which of these lowlevel functions - * please consult the newlib libc manual - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 STMicroelectronics. - * All rights reserved. - * - * This software is licensed under terms that can be found in the LICENSE file - * in the root directory of this software component. - * If no LICENSE file comes with this software, it is provided AS-IS. - * - ****************************************************************************** - */ - -/* Includes */ -#include -#include - -/** - * Pointer to the current high watermark of the heap usage - */ -static uint8_t *__sbrk_heap_end = NULL; - -/** - * @brief _sbrk() allocates memory to the newlib heap and is used by malloc - * and others from the C library - * - * @verbatim - * ############################################################################ - * # .data # .bss # newlib heap # MSP stack # - * # # # # Reserved by _Min_Stack_Size # - * ############################################################################ - * ^-- RAM start ^-- _end _estack, RAM end --^ - * @endverbatim - * - * This implementation starts allocating at the '_end' linker symbol - * The '_Min_Stack_Size' linker symbol reserves a memory for the MSP stack - * The implementation considers '_estack' linker symbol to be RAM end - * NOTE: If the MSP stack, at any point during execution, grows larger than the - * reserved size, please increase the '_Min_Stack_Size'. - * - * @param incr Memory size - * @return Pointer to allocated memory - */ -void *_sbrk(ptrdiff_t incr) -{ - extern uint8_t _end; /* Symbol defined in the linker script */ - extern uint8_t _estack; /* Symbol defined in the linker script */ - extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */ - const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size; - const uint8_t *max_heap = (uint8_t *)stack_limit; - uint8_t *prev_heap_end; - - /* Initialize heap end at first call */ - if (NULL == __sbrk_heap_end) - { - __sbrk_heap_end = &_end; - } - - /* Protect heap from growing into the reserved MSP stack */ - if (__sbrk_heap_end + incr > max_heap) - { - errno = ENOMEM; - return (void *)-1; - } - - prev_heap_end = __sbrk_heap_end; - __sbrk_heap_end += incr; - - return (void *)prev_heap_end; -} diff --git a/Core/Src/Backup/system_stm32f4xx.c.bak b/Core/Src/Backup/system_stm32f4xx.c.bak deleted file mode 100644 index 7a61e9c..0000000 --- a/Core/Src/Backup/system_stm32f4xx.c.bak +++ /dev/null @@ -1,747 +0,0 @@ -/** - ****************************************************************************** - * @file system_stm32f4xx.c - * @author MCD Application Team - * @brief CMSIS Cortex-M4 Device Peripheral Access Layer System Source File. - * - * This file provides two functions and one global variable to be called from - * user application: - * - SystemInit(): This function is called at startup just after reset and - * before branch to main program. This call is made inside - * the "startup_stm32f4xx.s" file. - * - * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used - * by the user application to setup the SysTick - * timer or configure other parameters. - * - * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must - * be called whenever the core clock is changed - * during program execution. - * - * - ****************************************************************************** - * @attention - * - * Copyright (c) 2017 STMicroelectronics. - * All rights reserved. - * - * This software is licensed under terms that can be found in the LICENSE file - * in the root directory of this software component. - * If no LICENSE file comes with this software, it is provided AS-IS. - * - ****************************************************************************** - */ - -/** @addtogroup CMSIS - * @{ - */ - -/** @addtogroup stm32f4xx_system - * @{ - */ - -/** @addtogroup STM32F4xx_System_Private_Includes - * @{ - */ - - -#include "stm32f4xx.h" - -#if !defined (HSE_VALUE) - #define HSE_VALUE ((uint32_t)25000000) /*!< Default value of the External oscillator in Hz */ -#endif /* HSE_VALUE */ - -#if !defined (HSI_VALUE) - #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/ -#endif /* HSI_VALUE */ - -/** - * @} - */ - -/** @addtogroup STM32F4xx_System_Private_TypesDefinitions - * @{ - */ - -/** - * @} - */ - -/** @addtogroup STM32F4xx_System_Private_Defines - * @{ - */ - -/************************* Miscellaneous Configuration ************************/ -/*!< Uncomment the following line if you need to use external SRAM or SDRAM as data memory */ -#if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx)\ - || defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\ - || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) -/* #define DATA_IN_ExtSRAM */ -#endif /* STM32F40xxx || STM32F41xxx || STM32F42xxx || STM32F43xxx || STM32F469xx || STM32F479xx ||\ - STM32F412Zx || STM32F412Vx */ - -#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\ - || defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) -/* #define DATA_IN_ExtSDRAM */ -#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F446xx || STM32F469xx ||\ - STM32F479xx */ - -/* Note: Following vector table addresses must be defined in line with linker - configuration. */ -/*!< Uncomment the following line if you need to relocate the vector table - anywhere in Flash or Sram, else the vector table is kept at the automatic - remap of boot address selected */ -/* #define USER_VECT_TAB_ADDRESS */ - -#if defined(USER_VECT_TAB_ADDRESS) -/*!< Uncomment the following line if you need to relocate your vector Table - in Sram else user remap will be done in Flash. */ -/* #define VECT_TAB_SRAM */ -#if defined(VECT_TAB_SRAM) -#define VECT_TAB_BASE_ADDRESS SRAM_BASE /*!< Vector Table base address field. - This value must be a multiple of 0x200. */ -#else -#define VECT_TAB_BASE_ADDRESS FLASH_BASE /*!< Vector Table base address field. - This value must be a multiple of 0x200. */ -#endif /* VECT_TAB_SRAM */ -#if !defined(VECT_TAB_OFFSET) -#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table offset field. - This value must be a multiple of 0x200. */ -#endif /* VECT_TAB_OFFSET */ -#endif /* USER_VECT_TAB_ADDRESS */ -/******************************************************************************/ - -/** - * @} - */ - -/** @addtogroup STM32F4xx_System_Private_Macros - * @{ - */ - -/** - * @} - */ - -/** @addtogroup STM32F4xx_System_Private_Variables - * @{ - */ - /* This variable is updated in three ways: - 1) by calling CMSIS function SystemCoreClockUpdate() - 2) by calling HAL API function HAL_RCC_GetHCLKFreq() - 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency - Note: If you use this function to configure the system clock; then there - is no need to call the 2 first functions listed above, since SystemCoreClock - variable is updated automatically. - */ -uint32_t SystemCoreClock = 16000000; -const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9}; -const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4}; -/** - * @} - */ - -/** @addtogroup STM32F4xx_System_Private_FunctionPrototypes - * @{ - */ - -#if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM) - static void SystemInit_ExtMemCtl(void); -#endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */ - -/** - * @} - */ - -/** @addtogroup STM32F4xx_System_Private_Functions - * @{ - */ - -/** - * @brief Setup the microcontroller system - * Initialize the FPU setting, vector table location and External memory - * configuration. - * @param None - * @retval None - */ -void SystemInit(void) -{ - /* FPU settings ------------------------------------------------------------*/ - #if (__FPU_PRESENT == 1) && (__FPU_USED == 1) - SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */ - #endif - -#if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM) - SystemInit_ExtMemCtl(); -#endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */ - - /* Configure the Vector Table location -------------------------------------*/ -#if defined(USER_VECT_TAB_ADDRESS) - SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */ -#endif /* USER_VECT_TAB_ADDRESS */ -} - -/** - * @brief Update SystemCoreClock variable according to Clock Register Values. - * The SystemCoreClock variable contains the core clock (HCLK), it can - * be used by the user application to setup the SysTick timer or configure - * other parameters. - * - * @note Each time the core clock (HCLK) changes, this function must be called - * to update SystemCoreClock variable value. Otherwise, any configuration - * based on this variable will be incorrect. - * - * @note - The system frequency computed by this function is not the real - * frequency in the chip. It is calculated based on the predefined - * constant and the selected clock source: - * - * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*) - * - * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**) - * - * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**) - * or HSI_VALUE(*) multiplied/divided by the PLL factors. - * - * (*) HSI_VALUE is a constant defined in stm32f4xx_hal_conf.h file (default value - * 16 MHz) but the real value may vary depending on the variations - * in voltage and temperature. - * - * (**) HSE_VALUE is a constant defined in stm32f4xx_hal_conf.h file (its value - * depends on the application requirements), user has to ensure that HSE_VALUE - * is same as the real frequency of the crystal used. Otherwise, this function - * may have wrong result. - * - * - The result of this function could be not correct when using fractional - * value for HSE crystal. - * - * @param None - * @retval None - */ -void SystemCoreClockUpdate(void) -{ - uint32_t tmp, pllvco, pllp, pllsource, pllm; - - /* Get SYSCLK source -------------------------------------------------------*/ - tmp = RCC->CFGR & RCC_CFGR_SWS; - - switch (tmp) - { - case 0x00: /* HSI used as system clock source */ - SystemCoreClock = HSI_VALUE; - break; - case 0x04: /* HSE used as system clock source */ - SystemCoreClock = HSE_VALUE; - break; - case 0x08: /* PLL used as system clock source */ - - /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N - SYSCLK = PLL_VCO / PLL_P - */ - pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22; - pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM; - - if (pllsource != 0) - { - /* HSE used as PLL clock source */ - pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6); - } - else - { - /* HSI used as PLL clock source */ - pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6); - } - - pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2; - SystemCoreClock = pllvco/pllp; - break; - default: - SystemCoreClock = HSI_VALUE; - break; - } - /* Compute HCLK frequency --------------------------------------------------*/ - /* Get HCLK prescaler */ - tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)]; - /* HCLK frequency */ - SystemCoreClock >>= tmp; -} - -#if defined (DATA_IN_ExtSRAM) && defined (DATA_IN_ExtSDRAM) -#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\ - || defined(STM32F469xx) || defined(STM32F479xx) -/** - * @brief Setup the external memory controller. - * Called in startup_stm32f4xx.s before jump to main. - * This function configures the external memories (SRAM/SDRAM) - * This SRAM/SDRAM will be used as program data memory (including heap and stack). - * @param None - * @retval None - */ -void SystemInit_ExtMemCtl(void) -{ - __IO uint32_t tmp = 0x00; - - register uint32_t tmpreg = 0, timeout = 0xFFFF; - register __IO uint32_t index; - - /* Enable GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH and GPIOI interface clock */ - RCC->AHB1ENR |= 0x000001F8; - - /* Delay after an RCC peripheral clock enabling */ - tmp = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOCEN); - - /* Connect PDx pins to FMC Alternate function */ - GPIOD->AFR[0] = 0x00CCC0CC; - GPIOD->AFR[1] = 0xCCCCCCCC; - /* Configure PDx pins in Alternate function mode */ - GPIOD->MODER = 0xAAAA0A8A; - /* Configure PDx pins speed to 100 MHz */ - GPIOD->OSPEEDR = 0xFFFF0FCF; - /* Configure PDx pins Output type to push-pull */ - GPIOD->OTYPER = 0x00000000; - /* No pull-up, pull-down for PDx pins */ - GPIOD->PUPDR = 0x00000000; - - /* Connect PEx pins to FMC Alternate function */ - GPIOE->AFR[0] = 0xC00CC0CC; - GPIOE->AFR[1] = 0xCCCCCCCC; - /* Configure PEx pins in Alternate function mode */ - GPIOE->MODER = 0xAAAA828A; - /* Configure PEx pins speed to 100 MHz */ - GPIOE->OSPEEDR = 0xFFFFC3CF; - /* Configure PEx pins Output type to push-pull */ - GPIOE->OTYPER = 0x00000000; - /* No pull-up, pull-down for PEx pins */ - GPIOE->PUPDR = 0x00000000; - - /* Connect PFx pins to FMC Alternate function */ - GPIOF->AFR[0] = 0xCCCCCCCC; - GPIOF->AFR[1] = 0xCCCCCCCC; - /* Configure PFx pins in Alternate function mode */ - GPIOF->MODER = 0xAA800AAA; - /* Configure PFx pins speed to 50 MHz */ - GPIOF->OSPEEDR = 0xAA800AAA; - /* Configure PFx pins Output type to push-pull */ - GPIOF->OTYPER = 0x00000000; - /* No pull-up, pull-down for PFx pins */ - GPIOF->PUPDR = 0x00000000; - - /* Connect PGx pins to FMC Alternate function */ - GPIOG->AFR[0] = 0xCCCCCCCC; - GPIOG->AFR[1] = 0xCCCCCCCC; - /* Configure PGx pins in Alternate function mode */ - GPIOG->MODER = 0xAAAAAAAA; - /* Configure PGx pins speed to 50 MHz */ - GPIOG->OSPEEDR = 0xAAAAAAAA; - /* Configure PGx pins Output type to push-pull */ - GPIOG->OTYPER = 0x00000000; - /* No pull-up, pull-down for PGx pins */ - GPIOG->PUPDR = 0x00000000; - - /* Connect PHx pins to FMC Alternate function */ - GPIOH->AFR[0] = 0x00C0CC00; - GPIOH->AFR[1] = 0xCCCCCCCC; - /* Configure PHx pins in Alternate function mode */ - GPIOH->MODER = 0xAAAA08A0; - /* Configure PHx pins speed to 50 MHz */ - GPIOH->OSPEEDR = 0xAAAA08A0; - /* Configure PHx pins Output type to push-pull */ - GPIOH->OTYPER = 0x00000000; - /* No pull-up, pull-down for PHx pins */ - GPIOH->PUPDR = 0x00000000; - - /* Connect PIx pins to FMC Alternate function */ - GPIOI->AFR[0] = 0xCCCCCCCC; - GPIOI->AFR[1] = 0x00000CC0; - /* Configure PIx pins in Alternate function mode */ - GPIOI->MODER = 0x0028AAAA; - /* Configure PIx pins speed to 50 MHz */ - GPIOI->OSPEEDR = 0x0028AAAA; - /* Configure PIx pins Output type to push-pull */ - GPIOI->OTYPER = 0x00000000; - /* No pull-up, pull-down for PIx pins */ - GPIOI->PUPDR = 0x00000000; - -/*-- FMC Configuration -------------------------------------------------------*/ - /* Enable the FMC interface clock */ - RCC->AHB3ENR |= 0x00000001; - /* Delay after an RCC peripheral clock enabling */ - tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN); - - FMC_Bank5_6->SDCR[0] = 0x000019E4; - FMC_Bank5_6->SDTR[0] = 0x01115351; - - /* SDRAM initialization sequence */ - /* Clock enable command */ - FMC_Bank5_6->SDCMR = 0x00000011; - tmpreg = FMC_Bank5_6->SDSR & 0x00000020; - while((tmpreg != 0) && (timeout-- > 0)) - { - tmpreg = FMC_Bank5_6->SDSR & 0x00000020; - } - - /* Delay */ - for (index = 0; index<1000; index++); - - /* PALL command */ - FMC_Bank5_6->SDCMR = 0x00000012; - tmpreg = FMC_Bank5_6->SDSR & 0x00000020; - timeout = 0xFFFF; - while((tmpreg != 0) && (timeout-- > 0)) - { - tmpreg = FMC_Bank5_6->SDSR & 0x00000020; - } - - /* Auto refresh command */ - FMC_Bank5_6->SDCMR = 0x00000073; - tmpreg = FMC_Bank5_6->SDSR & 0x00000020; - timeout = 0xFFFF; - while((tmpreg != 0) && (timeout-- > 0)) - { - tmpreg = FMC_Bank5_6->SDSR & 0x00000020; - } - - /* MRD register program */ - FMC_Bank5_6->SDCMR = 0x00046014; - tmpreg = FMC_Bank5_6->SDSR & 0x00000020; - timeout = 0xFFFF; - while((tmpreg != 0) && (timeout-- > 0)) - { - tmpreg = FMC_Bank5_6->SDSR & 0x00000020; - } - - /* Set refresh count */ - tmpreg = FMC_Bank5_6->SDRTR; - FMC_Bank5_6->SDRTR = (tmpreg | (0x0000027C<<1)); - - /* Disable write protection */ - tmpreg = FMC_Bank5_6->SDCR[0]; - FMC_Bank5_6->SDCR[0] = (tmpreg & 0xFFFFFDFF); - -#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) - /* Configure and enable Bank1_SRAM2 */ - FMC_Bank1->BTCR[2] = 0x00001011; - FMC_Bank1->BTCR[3] = 0x00000201; - FMC_Bank1E->BWTR[2] = 0x0fffffff; -#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */ -#if defined(STM32F469xx) || defined(STM32F479xx) - /* Configure and enable Bank1_SRAM2 */ - FMC_Bank1->BTCR[2] = 0x00001091; - FMC_Bank1->BTCR[3] = 0x00110212; - FMC_Bank1E->BWTR[2] = 0x0fffffff; -#endif /* STM32F469xx || STM32F479xx */ - - (void)(tmp); -} -#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */ -#elif defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM) -/** - * @brief Setup the external memory controller. - * Called in startup_stm32f4xx.s before jump to main. - * This function configures the external memories (SRAM/SDRAM) - * This SRAM/SDRAM will be used as program data memory (including heap and stack). - * @param None - * @retval None - */ -void SystemInit_ExtMemCtl(void) -{ - __IO uint32_t tmp = 0x00; -#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\ - || defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) -#if defined (DATA_IN_ExtSDRAM) - register uint32_t tmpreg = 0, timeout = 0xFFFF; - register __IO uint32_t index; - -#if defined(STM32F446xx) - /* Enable GPIOA, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG interface - clock */ - RCC->AHB1ENR |= 0x0000007D; -#else - /* Enable GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH and GPIOI interface - clock */ - RCC->AHB1ENR |= 0x000001F8; -#endif /* STM32F446xx */ - /* Delay after an RCC peripheral clock enabling */ - tmp = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOCEN); - -#if defined(STM32F446xx) - /* Connect PAx pins to FMC Alternate function */ - GPIOA->AFR[0] |= 0xC0000000; - GPIOA->AFR[1] |= 0x00000000; - /* Configure PDx pins in Alternate function mode */ - GPIOA->MODER |= 0x00008000; - /* Configure PDx pins speed to 50 MHz */ - GPIOA->OSPEEDR |= 0x00008000; - /* Configure PDx pins Output type to push-pull */ - GPIOA->OTYPER |= 0x00000000; - /* No pull-up, pull-down for PDx pins */ - GPIOA->PUPDR |= 0x00000000; - - /* Connect PCx pins to FMC Alternate function */ - GPIOC->AFR[0] |= 0x00CC0000; - GPIOC->AFR[1] |= 0x00000000; - /* Configure PDx pins in Alternate function mode */ - GPIOC->MODER |= 0x00000A00; - /* Configure PDx pins speed to 50 MHz */ - GPIOC->OSPEEDR |= 0x00000A00; - /* Configure PDx pins Output type to push-pull */ - GPIOC->OTYPER |= 0x00000000; - /* No pull-up, pull-down for PDx pins */ - GPIOC->PUPDR |= 0x00000000; -#endif /* STM32F446xx */ - - /* Connect PDx pins to FMC Alternate function */ - GPIOD->AFR[0] = 0x000000CC; - GPIOD->AFR[1] = 0xCC000CCC; - /* Configure PDx pins in Alternate function mode */ - GPIOD->MODER = 0xA02A000A; - /* Configure PDx pins speed to 50 MHz */ - GPIOD->OSPEEDR = 0xA02A000A; - /* Configure PDx pins Output type to push-pull */ - GPIOD->OTYPER = 0x00000000; - /* No pull-up, pull-down for PDx pins */ - GPIOD->PUPDR = 0x00000000; - - /* Connect PEx pins to FMC Alternate function */ - GPIOE->AFR[0] = 0xC00000CC; - GPIOE->AFR[1] = 0xCCCCCCCC; - /* Configure PEx pins in Alternate function mode */ - GPIOE->MODER = 0xAAAA800A; - /* Configure PEx pins speed to 50 MHz */ - GPIOE->OSPEEDR = 0xAAAA800A; - /* Configure PEx pins Output type to push-pull */ - GPIOE->OTYPER = 0x00000000; - /* No pull-up, pull-down for PEx pins */ - GPIOE->PUPDR = 0x00000000; - - /* Connect PFx pins to FMC Alternate function */ - GPIOF->AFR[0] = 0xCCCCCCCC; - GPIOF->AFR[1] = 0xCCCCCCCC; - /* Configure PFx pins in Alternate function mode */ - GPIOF->MODER = 0xAA800AAA; - /* Configure PFx pins speed to 50 MHz */ - GPIOF->OSPEEDR = 0xAA800AAA; - /* Configure PFx pins Output type to push-pull */ - GPIOF->OTYPER = 0x00000000; - /* No pull-up, pull-down for PFx pins */ - GPIOF->PUPDR = 0x00000000; - - /* Connect PGx pins to FMC Alternate function */ - GPIOG->AFR[0] = 0xCCCCCCCC; - GPIOG->AFR[1] = 0xCCCCCCCC; - /* Configure PGx pins in Alternate function mode */ - GPIOG->MODER = 0xAAAAAAAA; - /* Configure PGx pins speed to 50 MHz */ - GPIOG->OSPEEDR = 0xAAAAAAAA; - /* Configure PGx pins Output type to push-pull */ - GPIOG->OTYPER = 0x00000000; - /* No pull-up, pull-down for PGx pins */ - GPIOG->PUPDR = 0x00000000; - -#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\ - || defined(STM32F469xx) || defined(STM32F479xx) - /* Connect PHx pins to FMC Alternate function */ - GPIOH->AFR[0] = 0x00C0CC00; - GPIOH->AFR[1] = 0xCCCCCCCC; - /* Configure PHx pins in Alternate function mode */ - GPIOH->MODER = 0xAAAA08A0; - /* Configure PHx pins speed to 50 MHz */ - GPIOH->OSPEEDR = 0xAAAA08A0; - /* Configure PHx pins Output type to push-pull */ - GPIOH->OTYPER = 0x00000000; - /* No pull-up, pull-down for PHx pins */ - GPIOH->PUPDR = 0x00000000; - - /* Connect PIx pins to FMC Alternate function */ - GPIOI->AFR[0] = 0xCCCCCCCC; - GPIOI->AFR[1] = 0x00000CC0; - /* Configure PIx pins in Alternate function mode */ - GPIOI->MODER = 0x0028AAAA; - /* Configure PIx pins speed to 50 MHz */ - GPIOI->OSPEEDR = 0x0028AAAA; - /* Configure PIx pins Output type to push-pull */ - GPIOI->OTYPER = 0x00000000; - /* No pull-up, pull-down for PIx pins */ - GPIOI->PUPDR = 0x00000000; -#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx */ - -/*-- FMC Configuration -------------------------------------------------------*/ - /* Enable the FMC interface clock */ - RCC->AHB3ENR |= 0x00000001; - /* Delay after an RCC peripheral clock enabling */ - tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN); - - /* Configure and enable SDRAM bank1 */ -#if defined(STM32F446xx) - FMC_Bank5_6->SDCR[0] = 0x00001954; -#else - FMC_Bank5_6->SDCR[0] = 0x000019E4; -#endif /* STM32F446xx */ - FMC_Bank5_6->SDTR[0] = 0x01115351; - - /* SDRAM initialization sequence */ - /* Clock enable command */ - FMC_Bank5_6->SDCMR = 0x00000011; - tmpreg = FMC_Bank5_6->SDSR & 0x00000020; - while((tmpreg != 0) && (timeout-- > 0)) - { - tmpreg = FMC_Bank5_6->SDSR & 0x00000020; - } - - /* Delay */ - for (index = 0; index<1000; index++); - - /* PALL command */ - FMC_Bank5_6->SDCMR = 0x00000012; - tmpreg = FMC_Bank5_6->SDSR & 0x00000020; - timeout = 0xFFFF; - while((tmpreg != 0) && (timeout-- > 0)) - { - tmpreg = FMC_Bank5_6->SDSR & 0x00000020; - } - - /* Auto refresh command */ -#if defined(STM32F446xx) - FMC_Bank5_6->SDCMR = 0x000000F3; -#else - FMC_Bank5_6->SDCMR = 0x00000073; -#endif /* STM32F446xx */ - tmpreg = FMC_Bank5_6->SDSR & 0x00000020; - timeout = 0xFFFF; - while((tmpreg != 0) && (timeout-- > 0)) - { - tmpreg = FMC_Bank5_6->SDSR & 0x00000020; - } - - /* MRD register program */ -#if defined(STM32F446xx) - FMC_Bank5_6->SDCMR = 0x00044014; -#else - FMC_Bank5_6->SDCMR = 0x00046014; -#endif /* STM32F446xx */ - tmpreg = FMC_Bank5_6->SDSR & 0x00000020; - timeout = 0xFFFF; - while((tmpreg != 0) && (timeout-- > 0)) - { - tmpreg = FMC_Bank5_6->SDSR & 0x00000020; - } - - /* Set refresh count */ - tmpreg = FMC_Bank5_6->SDRTR; -#if defined(STM32F446xx) - FMC_Bank5_6->SDRTR = (tmpreg | (0x0000050C<<1)); -#else - FMC_Bank5_6->SDRTR = (tmpreg | (0x0000027C<<1)); -#endif /* STM32F446xx */ - - /* Disable write protection */ - tmpreg = FMC_Bank5_6->SDCR[0]; - FMC_Bank5_6->SDCR[0] = (tmpreg & 0xFFFFFDFF); -#endif /* DATA_IN_ExtSDRAM */ -#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || STM32F446xx || STM32F469xx || STM32F479xx */ - -#if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx)\ - || defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)\ - || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) - -#if defined(DATA_IN_ExtSRAM) -/*-- GPIOs Configuration -----------------------------------------------------*/ - /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */ - RCC->AHB1ENR |= 0x00000078; - /* Delay after an RCC peripheral clock enabling */ - tmp = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIODEN); - - /* Connect PDx pins to FMC Alternate function */ - GPIOD->AFR[0] = 0x00CCC0CC; - GPIOD->AFR[1] = 0xCCCCCCCC; - /* Configure PDx pins in Alternate function mode */ - GPIOD->MODER = 0xAAAA0A8A; - /* Configure PDx pins speed to 100 MHz */ - GPIOD->OSPEEDR = 0xFFFF0FCF; - /* Configure PDx pins Output type to push-pull */ - GPIOD->OTYPER = 0x00000000; - /* No pull-up, pull-down for PDx pins */ - GPIOD->PUPDR = 0x00000000; - - /* Connect PEx pins to FMC Alternate function */ - GPIOE->AFR[0] = 0xC00CC0CC; - GPIOE->AFR[1] = 0xCCCCCCCC; - /* Configure PEx pins in Alternate function mode */ - GPIOE->MODER = 0xAAAA828A; - /* Configure PEx pins speed to 100 MHz */ - GPIOE->OSPEEDR = 0xFFFFC3CF; - /* Configure PEx pins Output type to push-pull */ - GPIOE->OTYPER = 0x00000000; - /* No pull-up, pull-down for PEx pins */ - GPIOE->PUPDR = 0x00000000; - - /* Connect PFx pins to FMC Alternate function */ - GPIOF->AFR[0] = 0x00CCCCCC; - GPIOF->AFR[1] = 0xCCCC0000; - /* Configure PFx pins in Alternate function mode */ - GPIOF->MODER = 0xAA000AAA; - /* Configure PFx pins speed to 100 MHz */ - GPIOF->OSPEEDR = 0xFF000FFF; - /* Configure PFx pins Output type to push-pull */ - GPIOF->OTYPER = 0x00000000; - /* No pull-up, pull-down for PFx pins */ - GPIOF->PUPDR = 0x00000000; - - /* Connect PGx pins to FMC Alternate function */ - GPIOG->AFR[0] = 0x00CCCCCC; - GPIOG->AFR[1] = 0x000000C0; - /* Configure PGx pins in Alternate function mode */ - GPIOG->MODER = 0x00085AAA; - /* Configure PGx pins speed to 100 MHz */ - GPIOG->OSPEEDR = 0x000CAFFF; - /* Configure PGx pins Output type to push-pull */ - GPIOG->OTYPER = 0x00000000; - /* No pull-up, pull-down for PGx pins */ - GPIOG->PUPDR = 0x00000000; - -/*-- FMC/FSMC Configuration --------------------------------------------------*/ - /* Enable the FMC/FSMC interface clock */ - RCC->AHB3ENR |= 0x00000001; - -#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) - /* Delay after an RCC peripheral clock enabling */ - tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN); - /* Configure and enable Bank1_SRAM2 */ - FMC_Bank1->BTCR[2] = 0x00001011; - FMC_Bank1->BTCR[3] = 0x00000201; - FMC_Bank1E->BWTR[2] = 0x0fffffff; -#endif /* STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */ -#if defined(STM32F469xx) || defined(STM32F479xx) - /* Delay after an RCC peripheral clock enabling */ - tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FMCEN); - /* Configure and enable Bank1_SRAM2 */ - FMC_Bank1->BTCR[2] = 0x00001091; - FMC_Bank1->BTCR[3] = 0x00110212; - FMC_Bank1E->BWTR[2] = 0x0fffffff; -#endif /* STM32F469xx || STM32F479xx */ -#if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx)|| defined(STM32F417xx)\ - || defined(STM32F412Zx) || defined(STM32F412Vx) - /* Delay after an RCC peripheral clock enabling */ - tmp = READ_BIT(RCC->AHB3ENR, RCC_AHB3ENR_FSMCEN); - /* Configure and enable Bank1_SRAM2 */ - FSMC_Bank1->BTCR[2] = 0x00001011; - FSMC_Bank1->BTCR[3] = 0x00000201; - FSMC_Bank1E->BWTR[2] = 0x0FFFFFFF; -#endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F412Zx || STM32F412Vx */ - -#endif /* DATA_IN_ExtSRAM */ -#endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx ||\ - STM32F429xx || STM32F439xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Vx */ - (void)(tmp); -} -#endif /* DATA_IN_ExtSRAM && DATA_IN_ExtSDRAM */ -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ diff --git a/Core/Src/Backup/usart.c.bak b/Core/Src/Backup/usart.c.bak deleted file mode 100644 index 727b716..0000000 --- a/Core/Src/Backup/usart.c.bak +++ /dev/null @@ -1,114 +0,0 @@ -/* USER CODE BEGIN Header */ -/** - ****************************************************************************** - * @file usart.c - * @brief This file provides code for the configuration - * of the USART instances. - ****************************************************************************** - * @attention - * - * Copyright (c) 2026 STMicroelectronics. - * All rights reserved. - * - * This software is licensed under terms that can be found in the LICENSE file - * in the root directory of this software component. - * If no LICENSE file comes with this software, it is provided AS-IS. - * - ****************************************************************************** - */ -/* USER CODE END Header */ -/* Includes ------------------------------------------------------------------*/ -#include "usart.h" - -/* USER CODE BEGIN 0 */ - -/* USER CODE END 0 */ - -UART_HandleTypeDef huart1; - -/* USART1 init function */ - -void MX_USART1_UART_Init(void) -{ - - /* USER CODE BEGIN USART1_Init 0 */ - - /* USER CODE END USART1_Init 0 */ - - /* USER CODE BEGIN USART1_Init 1 */ - - /* USER CODE END USART1_Init 1 */ - huart1.Instance = USART1; - huart1.Init.BaudRate = 115200; - huart1.Init.WordLength = UART_WORDLENGTH_8B; - huart1.Init.StopBits = UART_STOPBITS_1; - huart1.Init.Parity = UART_PARITY_NONE; - huart1.Init.Mode = UART_MODE_TX_RX; - huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; - huart1.Init.OverSampling = UART_OVERSAMPLING_16; - if (HAL_UART_Init(&huart1) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN USART1_Init 2 */ - - /* USER CODE END USART1_Init 2 */ - -} - -void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) -{ - - GPIO_InitTypeDef GPIO_InitStruct = {0}; - if(uartHandle->Instance==USART1) - { - /* USER CODE BEGIN USART1_MspInit 0 */ - - /* USER CODE END USART1_MspInit 0 */ - /* USART1 clock enable */ - __HAL_RCC_USART1_CLK_ENABLE(); - - __HAL_RCC_GPIOA_CLK_ENABLE(); - /**USART1 GPIO Configuration - PA9 ------> USART1_TX - PA10 ------> USART1_RX - */ - GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; - GPIO_InitStruct.Alternate = GPIO_AF7_USART1; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - - /* USER CODE BEGIN USART1_MspInit 1 */ - - /* USER CODE END USART1_MspInit 1 */ - } -} - -void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) -{ - - if(uartHandle->Instance==USART1) - { - /* USER CODE BEGIN USART1_MspDeInit 0 */ - - /* USER CODE END USART1_MspDeInit 0 */ - /* Peripheral clock disable */ - __HAL_RCC_USART1_CLK_DISABLE(); - - /**USART1 GPIO Configuration - PA9 ------> USART1_TX - PA10 ------> USART1_RX - */ - HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10); - - /* USER CODE BEGIN USART1_MspDeInit 1 */ - - /* USER CODE END USART1_MspDeInit 1 */ - } -} - -/* USER CODE BEGIN 1 */ - -/* USER CODE END 1 */ diff --git a/Core/Src/freertos.c b/Core/Src/freertos.c index 0ef80f0..efe3fec 100644 --- a/Core/Src/freertos.c +++ b/Core/Src/freertos.c @@ -33,7 +33,6 @@ #include "j901p.h" #include "j901p_hal.h" #include "mf4010v2.h" -#include "mf4010v2_hal.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -53,22 +52,7 @@ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN Variables */ -extern volatile uint8_t s_cDataUpdate; -void CopeSensorData(uint32_t uiReg, uint32_t uiRegNum); -// 传感器数据结构(用于任务间通信) -typedef struct { - int16_t acc[3]; // 加速度计原始数据 - int16_t gyro[3]; // 陀螺仪原始数据 - int16_t angle[3]; // 角度数据 - uint8_t valid; // 数据有效标志 -} SensorData_TypeDef; - -SensorData_TypeDef sensorData = {{0}, {0}, {0}, 0}; - -// 电机实例 -mf4010v2_t motor1; // z轴电机 -mf4010v2_t motor2; // xy轴电机 /* USER CODE END Variables */ /* Definitions for monitorTask */ osThreadId_t monitorTaskHandle; @@ -131,13 +115,13 @@ void MX_FREERTOS_Init(void) { /* Create the thread(s) */ /* creation of monitorTask */ - // monitorTaskHandle = osThreadNew(StartMonitorTask, NULL, &monitorTask_attributes); + monitorTaskHandle = osThreadNew(StartMonitorTask, NULL, &monitorTask_attributes); /* creation of controlTask */ controlTaskHandle = osThreadNew(StartControlTask, NULL, &controlTask_attributes); /* creation of sensorTask */ - // sensorTaskHandle = osThreadNew(StartSensorTask, NULL, &sensorTask_attributes); + sensorTaskHandle = osThreadNew(StartSensorTask, NULL, &sensorTask_attributes); /* USER CODE BEGIN RTOS_THREADS */ /* add threads, ... */ @@ -159,74 +143,7 @@ void MX_FREERTOS_Init(void) { void StartMonitorTask(void *argument) { /* USER CODE BEGIN StartMonitorTask */ - argument = argument; - // 等待传感器数据初始化 - osDelay(500); - const uint8_t monitor_start[] = "Monitor task started\r\n"; - HAL_UART_Transmit(&huart1, monitor_start, sizeof(monitor_start)-1, 100); - - for(;;) - { - // 等待有效数据 - if(sensorData.valid) - { - // 输出加速度 - const uint8_t acc_msg[] = "ACC: "; - HAL_UART_Transmit(&huart1, acc_msg, sizeof(acc_msg)-1, 50); - - for(int i=0; i<3; i++) - { - int32_t acc_scaled = (sensorData.acc[i] * 16 * 1000) / 32768; - int32_t acc_int = acc_scaled / 1000; - int32_t acc_frac = acc_scaled % 1000; - if(acc_frac < 0) acc_frac = -acc_frac; - - char acc_str[20]; - int acc_len = snprintf(acc_str, sizeof(acc_str), "%ld.%03ld ", acc_int, acc_frac); - HAL_UART_Transmit(&huart1, (uint8_t*)acc_str, acc_len, 50); - } - HAL_UART_Transmit(&huart1, (uint8_t*)"\r\n", 2, 50); - - // 输出角速度 - const uint8_t gyro_msg[] = "GYRO: "; - HAL_UART_Transmit(&huart1, gyro_msg, sizeof(gyro_msg)-1, 50); - - for(int i=0; i<3; i++) - { - int32_t gyro_scaled = (sensorData.gyro[i] * 2000 * 1000) / 32768; - int32_t gyro_int = gyro_scaled / 1000; - int32_t gyro_frac = gyro_scaled % 1000; - if(gyro_frac < 0) gyro_frac = -gyro_frac; - - char gyro_str[20]; - int gyro_len = snprintf(gyro_str, sizeof(gyro_str), "%ld.%03ld ", gyro_int, gyro_frac); - HAL_UART_Transmit(&huart1, (uint8_t*)gyro_str, gyro_len, 50); - } - HAL_UART_Transmit(&huart1, (uint8_t*)"\r\n", 2, 50); - - // 输出角度 - const uint8_t angle_msg[] = "ANGLE: "; - HAL_UART_Transmit(&huart1, angle_msg, sizeof(angle_msg)-1, 50); - - for(int i=0; i<3; i++) - { - int32_t angle_scaled = (sensorData.angle[i] * 180 * 1000) / 32768; - int32_t angle_int = angle_scaled / 1000; - int32_t angle_frac = angle_scaled % 1000; - if(angle_frac < 0) angle_frac = -angle_frac; - - char angle_str[20]; - int angle_len = snprintf(angle_str, sizeof(angle_str), "%ld.%03ld ", angle_int, angle_frac); - HAL_UART_Transmit(&huart1, (uint8_t*)angle_str, angle_len, 50); - } - HAL_UART_Transmit(&huart1, (uint8_t*)"\r\n", 2, 50); - - sensorData.valid = 0; // 清除标志,等待下次更新 - } - - osDelay(200); // 200ms 输出间隔 - } /* USER CODE END StartMonitorTask */ } @@ -246,99 +163,7 @@ void StartMonitorTask(void *argument) void StartControlTask(void *argument) { /* USER CODE BEGIN StartControlTask */ - argument = argument; - - osDelay(500); - - /* ========================================================================== - * 步骤 1: 打印 CAN 初始状态和调试信息 - * ========================================================================== */ - const uint8_t init_msg[] = "\r\n========== CAN/Motor Initialization ==========\r\n"; - HAL_UART_Transmit(&huart1, init_msg, sizeof(init_msg)-1, 100); - - /* 打印 CAN 详细调试信息 */ - CAN_PrintDebugInfo(&hcan2, &huart1); - - /* ========================================================================== - * 步骤 2: 初始化电机结构体 - * ========================================================================== */ - const uint8_t motor_init_msg[] = "\r\nInitializing motor (ID=0x141)...\r\n"; - HAL_UART_Transmit(&huart1, motor_init_msg, sizeof(motor_init_msg)-1, 100); - - mf4010_status_t status; - char log_buf[128]; - int log_len; - - status = mf4010v2_init(&motor2, 0x141); - if(status != MF4010_OK) { - log_len = snprintf(log_buf, sizeof(log_buf), "Error: Motor init failed with status %d\r\n", status); - HAL_UART_Transmit(&huart1, (uint8_t*)log_buf, log_len, 100); - } else { - const uint8_t ok_msg[] = "Motor structure initialized OK\r\n"; - HAL_UART_Transmit(&huart1, ok_msg, sizeof(ok_msg)-1, 100); - } - - osDelay(100); - - /* ========================================================================== - * 步骤 3: 发送电机测试命令(关闭电机) - * ========================================================================== */ - const uint8_t cmd_msg[] = "\r\nSending MOTOR_CLOSE command...\r\n"; - HAL_UART_Transmit(&huart1, cmd_msg, sizeof(cmd_msg)-1, 100); - - const uint8_t cmd[] = COMMAND_MOTOR_CLOSE; - status = mf4010v2_send_command(&motor2, cmd, 1000); - - if (status != MF4010_OK) { - log_len = snprintf(log_buf, sizeof(log_buf), - "Error: Motor command failed with status %d\r\n" - " Error count: %d\r\n" - " Success count: %d\r\n" - " Flags: 0x%02X\r\n", - status, - motor2.error_count, - motor2.success_count, - motor2.flags); - HAL_UART_Transmit(&huart1, (uint8_t*)log_buf, log_len, 100); - - /* 打印 CAN 错误状态 */ - const uint8_t can_err_msg[] = "\r\nCAN error status:\r\n"; - HAL_UART_Transmit(&huart1, can_err_msg, sizeof(can_err_msg)-1, 100); - CAN_PrintDebugInfo(&hcan2, &huart1); - } else { - const uint8_t success_msg[] = "Motor command sent successfully!\r\n"; - HAL_UART_Transmit(&huart1, success_msg, sizeof(success_msg)-1, 100); - - log_len = snprintf(log_buf, sizeof(log_buf), - " Response[0]: 0x%02X\r\n" - " Error count: %d\r\n" - " Success count: %d\r\n", - motor2.last_response[0], - motor2.error_count, - motor2.success_count); - HAL_UART_Transmit(&huart1, (uint8_t*)log_buf, log_len, 100); - } - - /* ========================================================================== - * 主循环 - * ========================================================================== */ - const uint8_t main_loop_msg[] = "\r\nEntering main loop...\r\n"; - HAL_UART_Transmit(&huart1, main_loop_msg, sizeof(main_loop_msg)-1, 100); - - for (;;) { - osDelay(1000); // 1 秒延时 - - /* 可选:周期性打印状态 */ - #if 0 - log_len = snprintf(log_buf, sizeof(log_buf), - "Motor status: err=%d, ok=%d, flags=0x%02X\r\n", - motor2.error_count, - motor2.success_count, - motor2.flags); - HAL_UART_Transmit(&huart1, (uint8_t*)log_buf, log_len, 100); - #endif - } - + /* USER CODE END StartControlTask */ } @@ -352,39 +177,7 @@ void StartControlTask(void *argument) void StartSensorTask(void *argument) { /* USER CODE BEGIN StartSensorTask */ - argument = argument; - // 初始化传感器 - WitRegisterCallBack(CopeSensorData); - WitDelayMsRegister(J901P_Delay); - J901P_HAL_Init(0x50); - - WitReadReg(AX, 12); - osDelay(100); - - for(;;) - { - if(s_cDataUpdate) - { - WitReadReg(AX, 12); - osDelay(50); - - // 复制数据到共享结构 - for(int i=0; i<3; i++) { - sensorData.acc[i] = sReg[AX+i]; - sensorData.gyro[i] = sReg[GX+i]; - sensorData.angle[i] = sReg[Roll+i]; - } - sensorData.valid = 1; // 标记数据有效 - - s_cDataUpdate = 0; - osDelay(200); - } - else - { - WitReadReg(AX, 12); - osDelay(100); - } - } + /* USER CODE END StartSensorTask */ } diff --git a/Core/Src/main.c b/Core/Src/main.c index 501ef6c..5a9cc23 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -26,8 +26,6 @@ /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ -#include "j901p.h" -#include "j901p_hal.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -48,7 +46,6 @@ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ -volatile uint8_t s_cDataUpdate = 0; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ @@ -65,21 +62,7 @@ int __io_putchar(int ch) /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ -void CopeSensorData(uint32_t uiReg, uint32_t uiRegNum); -void CopeSensorData(uint32_t uiReg, uint32_t uiRegNum) -{ - for(uint32_t i = 0; i < uiRegNum; i++) - { - switch(uiReg + i) - { - case AZ: s_cDataUpdate |= 0x01; break; - case GZ: s_cDataUpdate |= 0x02; break; - case Yaw: s_cDataUpdate |= 0x04; break; - default: break; - } - } -} /* USER CODE END 0 */ @@ -117,8 +100,6 @@ int main(void) MX_CAN2_Init(); /* USER CODE BEGIN 2 */ - const uint8_t start_msg[] = "System starting...\r\n"; - HAL_UART_Transmit(&huart1, start_msg, sizeof(start_msg) - 1, 100); /* USER CODE END 2 */ diff --git a/app/app.c b/app/app.c new file mode 100644 index 0000000..01c17f8 --- /dev/null +++ b/app/app.c @@ -0,0 +1,13 @@ +#include "led.h" + +static led_t led0; + +void app_init(void) +{ + led_init(&led0, 0); +} + +void task_1_app_run(void) +{ + led_toggle(&led0); +} \ No newline at end of file diff --git a/bsp/stm32f4/can/bsp_can.c b/bsp/stm32f4/can/bsp_can.c index 828613a..b201f2b 100644 --- a/bsp/stm32f4/can/bsp_can.c +++ b/bsp/stm32f4/can/bsp_can.c @@ -1,8 +1,26 @@ #include "bsp_can.h" +#include "can.h" -void can_init(void) { - +void bsp_can_init(void) +{ + // CAN GPIO 和时钟已在 MX_CAN2_Init 中配置 + // 这里可以添加额外的初始化 } -int can_send(uint32_t id, uint8_t* data, uint8_t length) { - + +int bsp_can_send(uint32_t id, uint8_t* data, uint8_t length) +{ + if (!data || length > 8) + return -1; + + HAL_StatusTypeDef ret = CAN_SendFrame(&hcan2, id, data, length, 100); + return (ret == HAL_OK) ? 0 : -1; +} + +int bsp_can_recv(uint32_t *id, uint8_t* data, uint8_t *length) +{ + if (!id || !data || !length) + return -1; + + HAL_StatusTypeDef ret = CAN_ReceiveFrame(&hcan2, (uint16_t*)id, data, length, 0); + return (ret == HAL_OK) ? 0 : -1; } \ No newline at end of file diff --git a/bsp/stm32f4/can/bsp_can.h b/bsp/stm32f4/can/bsp_can.h index 77fd90a..5b1834e 100644 --- a/bsp/stm32f4/can/bsp_can.h +++ b/bsp/stm32f4/can/bsp_can.h @@ -2,22 +2,22 @@ #define __BSP_CAN__H__ #include +#include "main.h" #ifdef __cplusplus extern "C" { #endif + typedef struct { uint32_t id; // CAN ID uint8_t data[8]; // CAN 数据 uint8_t length; // 数据长度 (0-8) } can_message_t; -typedef struct { +void bsp_can_init(void); +int bsp_can_send(uint32_t id, uint8_t* data, uint8_t length); +int bsp_can_recv(uint32_t *id, uint8_t* data, uint8_t *length); -} can_config_t; - -void can_init(void); -int can_send(uint32_t id, uint8_t* data, uint8_t length); #ifdef __cplusplus } #endif diff --git a/bsp/stm32f4/led/bsp_led.c b/bsp/stm32f4/led/bsp_led.c new file mode 100644 index 0000000..4b39fea --- /dev/null +++ b/bsp/stm32f4/led/bsp_led.c @@ -0,0 +1,13 @@ +#include "bsp_led.h" + +static bsp_led_t leds[] = { + {GPIOA, GPIO_PIN_5}, // LED0 + {GPIOB, GPIO_PIN_0}, // LED1 +}; + +bsp_led_t* bsp_led_get(int id) +{ + if (id < 0 || id >= sizeof(leds)/sizeof(leds[0])) + return 0; + return &leds[id]; +} \ No newline at end of file diff --git a/bsp/stm32f4/led/bsp_led.h b/bsp/stm32f4/led/bsp_led.h new file mode 100644 index 0000000..5dbb025 --- /dev/null +++ b/bsp/stm32f4/led/bsp_led.h @@ -0,0 +1,13 @@ +#ifndef BSP_LED_H +#define BSP_LED_H + +#include "stm32f4xx_hal.h" + +typedef struct { + GPIO_TypeDef *port; + uint16_t pin; +} bsp_led_t; + +bsp_led_t* bsp_led_get(int id); + +#endif \ No newline at end of file diff --git a/hal/stm32f4/can/hal_can_stm32f4.c b/hal/stm32f4/can/hal_can_stm32f4.c new file mode 100644 index 0000000..bcbc353 --- /dev/null +++ b/hal/stm32f4/can/hal_can_stm32f4.c @@ -0,0 +1,60 @@ +#include "can_if.h" +#include "bsp_can.h" +#include "can.h" + +static int hal_can_init(void) +{ + // 配置滤波器:接收所有帧 + CAN_ConfigFilterAll(&hcan2, 0); + + // 启动 CAN + if (CAN_Start(&hcan2) != HAL_OK) + return -1; + + return 0; +} + +static int hal_can_send(const can_message_t *msg) +{ + if (!msg || msg->length > 8) + return -1; + + HAL_StatusTypeDef ret = CAN_SendFrame(&hcan2, msg->id, msg->data, msg->length, 100); + return (ret == HAL_OK) ? 0 : -1; +} + +static int hal_can_recv(can_message_t *msg) +{ + if (!msg) + return -1; + + uint16_t id; + uint8_t len; + + HAL_StatusTypeDef ret = CAN_ReceiveFrame(&hcan2, &id, msg->data, &len, 0); + if (ret != HAL_OK) + return -1; + + msg->id = id; + msg->length = len; + return 0; +} + +static int hal_can_filter_config(uint8_t filter_id, uint32_t id, uint32_t mask) +{ + CAN_ConfigFilter(&hcan2, filter_id, id, mask); + return 0; +} + +// 注册接口 +static const can_ops_t ops = { + .init = hal_can_init, + .send = hal_can_send, + .recv = hal_can_recv, + .filter_config = hal_can_filter_config +}; + +void hal_can_init_all(void) +{ + can_register_ops(&ops); +} diff --git a/hal/stm32f4/can/hal_can_stm32f4.h b/hal/stm32f4/can/hal_can_stm32f4.h new file mode 100644 index 0000000..0fa1ced --- /dev/null +++ b/hal/stm32f4/can/hal_can_stm32f4.h @@ -0,0 +1,14 @@ +#ifndef HAL_CAN_STM32F4_H +#define HAL_CAN_STM32F4_H + +#ifdef __cplusplus +extern "C" { +#endif + +void hal_can_init_all(void); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/hal/stm32f4/led/hal_led_stm32f4.c b/hal/stm32f4/led/hal_led_stm32f4.c new file mode 100644 index 0000000..0a8212f --- /dev/null +++ b/hal/stm32f4/led/hal_led_stm32f4.c @@ -0,0 +1,73 @@ +#include "led_if.h" +#include "bsp_led.h" + +static void hal_led_enable_gpio_clock(GPIO_TypeDef *port) +{ + if (port == GPIOA) __HAL_RCC_GPIOA_CLK_ENABLE(); + else if (port == GPIOB) __HAL_RCC_GPIOB_CLK_ENABLE(); + else if (port == GPIOC) __HAL_RCC_GPIOC_CLK_ENABLE(); + else if (port == GPIOD) __HAL_RCC_GPIOD_CLK_ENABLE(); + else if (port == GPIOE) __HAL_RCC_GPIOE_CLK_ENABLE(); + else if (port == GPIOF) __HAL_RCC_GPIOF_CLK_ENABLE(); + else if (port == GPIOG) __HAL_RCC_GPIOG_CLK_ENABLE(); +} + +static int hal_led_init(int id) +{ + bsp_led_t *led = bsp_led_get(id); + if (!led) return -1; + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + + hal_led_enable_gpio_clock(led->port); + + GPIO_InitStruct.Pin = led->pin; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + + HAL_GPIO_Init(led->port, &GPIO_InitStruct); + + return 0; +} + +static int hal_led_write(int id, int state) +{ + bsp_led_t *led = bsp_led_get(id); + if (!led) return -1; + + HAL_GPIO_WritePin(led->port, led->pin, + state ? GPIO_PIN_SET : GPIO_PIN_RESET); + + return 0; +} + +static int hal_led_read(int id) +{ + bsp_led_t *led = bsp_led_get(id); + if (!led) return -1; + + return HAL_GPIO_ReadPin(led->port, led->pin); +} + +static int hal_led_toggle(int id) +{ + bsp_led_t *led = bsp_led_get(id); + if (!led) return -1; + + HAL_GPIO_TogglePin(led->port, led->pin); + return 0; +} + +// 注册接口 +static const led_ops_t ops = { + .init = hal_led_init, + .write = hal_led_write, + .read = hal_led_read, + .toggle = hal_led_toggle +}; + +void hal_led_init_all(void) +{ + led_register_ops(&ops); +} \ No newline at end of file diff --git a/interfaces/can/can_if.c b/interfaces/can/can_if.c new file mode 100644 index 0000000..fc72b66 --- /dev/null +++ b/interfaces/can/can_if.c @@ -0,0 +1,36 @@ +#include "can_if.h" + +static const can_ops_t *g_can_ops = 0; + +void can_register_ops(const can_ops_t *ops) +{ + g_can_ops = ops; +} + +int can_if_init(void) +{ + if (!g_can_ops || !g_can_ops->init) + return -1; + return g_can_ops->init(); +} + +int can_if_send(const can_message_t *msg) +{ + if (!g_can_ops || !g_can_ops->send) + return -1; + return g_can_ops->send(msg); +} + +int can_if_recv(can_message_t *msg) +{ + if (!g_can_ops || !g_can_ops->recv) + return -1; + return g_can_ops->recv(msg); +} + +int can_if_filter_config(uint8_t filter_id, uint32_t id, uint32_t mask) +{ + if (!g_can_ops || !g_can_ops->filter_config) + return -1; + return g_can_ops->filter_config(filter_id, id, mask); +} diff --git a/interfaces/can/can_if.h b/interfaces/can/can_if.h new file mode 100644 index 0000000..2931b0a --- /dev/null +++ b/interfaces/can/can_if.h @@ -0,0 +1,28 @@ +#ifndef CAN_IF_H +#define CAN_IF_H + +#include + +typedef struct { + uint32_t id; // CAN ID + uint8_t data[8]; // CAN 数据 + uint8_t length; // 数据长度 (0-8) +} can_message_t; + +typedef struct { + int (*init)(void); + int (*send)(const can_message_t *msg); + int (*recv)(can_message_t *msg); + int (*filter_config)(uint8_t filter_id, uint32_t id, uint32_t mask); +} can_ops_t; + +// 注册接口(由 HAL 调用) +void can_register_ops(const can_ops_t *ops); + +// 对上层暴露统一 API +int can_if_init(void); +int can_if_send(const can_message_t *msg); +int can_if_recv(can_message_t *msg); +int can_if_filter_config(uint8_t filter_id, uint32_t id, uint32_t mask); + +#endif diff --git a/interfaces/led/led_if.c b/interfaces/led/led_if.c new file mode 100644 index 0000000..59bde07 --- /dev/null +++ b/interfaces/led/led_if.c @@ -0,0 +1,36 @@ +#include "led_if.h" + +static const led_ops_t *g_led_ops = 0; + +void led_register_ops(const led_ops_t *ops) +{ + g_led_ops = ops; +} + +int led_if_init(int id) +{ + if (!g_led_ops || !g_led_ops->init) + return -1; + return g_led_ops->init(id); +} + +int led_if_write(int id, int state) +{ + if (!g_led_ops || !g_led_ops->write) + return -1; + return g_led_ops->write(id, state); +} + +int led_if_read(int id) +{ + if (!g_led_ops || !g_led_ops->read) + return -1; + return g_led_ops->read(id); +} + +int led_if_toggle(int id) +{ + if (!g_led_ops || !g_led_ops->toggle) + return -1; + return g_led_ops->toggle(id); +} \ No newline at end of file diff --git a/interfaces/led/led_if.h b/interfaces/led/led_if.h new file mode 100644 index 0000000..f0ab6c5 --- /dev/null +++ b/interfaces/led/led_if.h @@ -0,0 +1,20 @@ +#ifndef LED_IF_H +#define LED_IF_H + +typedef struct { + int (*init)(int id); + int (*write)(int id, int state); + int (*read)(int id); + int (*toggle)(int id); +} led_ops_t; + +// 注册接口(由HAL调用) +void led_register_ops(const led_ops_t *ops); + +// 对上层暴露统一API +int led_if_init(int id); +int led_if_write(int id, int state); +int led_if_read(int id); +int led_if_toggle(int id); + +#endif \ No newline at end of file diff --git a/modules/device/led/led.c b/modules/device/led/led.c new file mode 100644 index 0000000..d31422e --- /dev/null +++ b/modules/device/led/led.c @@ -0,0 +1,49 @@ +#include "led.h" +#include "led_if.h" + +int led_init(led_t *led, int id) +{ + if (!led) return -1; + + led->id = id; + led->state = 0; + + int ret = led_if_init(id); + if (ret == 0) { + // 同步硬件状态 + int hw_state = led_if_read(id); + led->state = (hw_state > 0) ? 1 : 0; + } + + return ret; +} + +int led_on(led_t *led) +{ + if (!led) return -1; + + int ret = led_if_write(led->id, 1); + if (ret == 0) led->state = 1; + + return ret; +} + +int led_off(led_t *led) +{ + if (!led) return -1; + + int ret = led_if_write(led->id, 0); + if (ret == 0) led->state = 0; + + return ret; +} + +int led_toggle(led_t *led) +{ + if (!led) return -1; + + int ret = led_if_toggle(led->id); + if (ret == 0) led->state ^= 1; + + return ret; +} \ No newline at end of file diff --git a/modules/device/led/led.h b/modules/device/led/led.h new file mode 100644 index 0000000..0e083d0 --- /dev/null +++ b/modules/device/led/led.h @@ -0,0 +1,14 @@ +#ifndef LED_H +#define LED_H + +typedef struct { + int id; + int state; +} led_t; + +int led_init(led_t *led, int id); +int led_on(led_t *led); +int led_off(led_t *led); +int led_toggle(led_t *led); + +#endif \ No newline at end of file diff --git a/modules/device/motor/mf4010v2.c b/modules/device/motor/mf4010v2.c index 6646dda..4ddb8b0 100644 --- a/modules/device/motor/mf4010v2.c +++ b/modules/device/motor/mf4010v2.c @@ -37,8 +37,6 @@ mf4010_status_t mf4010v2_init(mf4010v2_t* motor, uint16_t id) motor->error_count = 0; motor->success_count = 0; motor->flags = MF4010_FLAG_NONE; - motor->ops.init = mf4010v2_init; - motor->ops.send_command = mf4010v2_send_command; return MF4010_OK; } @@ -47,55 +45,6 @@ mf4010_status_t mf4010v2_init(mf4010v2_t* motor, uint16_t id) */ mf4010_status_t mf4010v2_send_command(mf4010v2_t* motor, const uint8_t command[8], uint32_t timeout_ms) { - // 参数验证 - if(motor == NULL || command == NULL) { - return MF4010_ERR_INVALID_PARAM; - } - // 验证命令格式(首字节应该是命令字) - if((command[0] & 0x80) == 0 && command[0] < 0x30) { - printf("Warning: Invalid command format 0x%02X\r\n", command[0]); - // 不返回错误,继续执行 - } - - // 发送 CAN 消息 - if (can_send(motor->id, command, 8) != HAL_OK) - { - motor->error_count++; - motor->flags &= ~MF4010_FLAG_CONNECTED; - return MF4010_ERR_TIMEOUT; - } - - // 接收响应 - uint16_t rx_id; - uint8_t len; - if (MF4010V2_HAL_Receive(&rx_id, motor->last_response, &len, timeout_ms) != HAL_OK) - { - motor->error_count++; - motor->flags &= ~MF4010_FLAG_CONNECTED; - return MF4010_ERR_NO_RESPONSE; - } - - // 验证响应 ID - if (rx_id != motor->id) - { - return MF4010_ERR_CRC; - } - - motor->last_comm_time = get_system_tick(); - motor->success_count++; - motor->flags |= MF4010_FLAG_CONNECTED; - - // 解析错误状态 - if (motor->last_response[0] & 0x80) - { - uint8_t error_code = motor->last_response[0] & 0x7F; - printf("Motor error from 0x%03X: 0x%02X\r\n", motor->id, error_code); - motor->error_count++; - motor->flags |= MF4010_FLAG_FAULT; - return MF4010_ERR_CRC; - } - - motor->flags &= ~MF4010_FLAG_FAULT; - return MF4010_OK; + } diff --git a/modules/device/motor/mf4010v2.h b/modules/device/motor/mf4010v2.h index d51da9e..933ee9a 100644 --- a/modules/device/motor/mf4010v2.h +++ b/modules/device/motor/mf4010v2.h @@ -60,23 +60,25 @@ typedef enum { /** * @brief 电机结构体(优化版) */ -struct __mf4010v2_ops_t; -typedef struct { - struct __mf4010v2_ops_t ops; /**< 操作函数指针 */ +struct __mf4010v2_t; +typedef struct __mf4010v2_t mf4010v2_t; // 前向声明 + typedef struct { + mf4010_status_t (*init)(); + mf4010_status_t (*send_command)(); +}mf4010v2_ops_t; + +struct __mf4010v2_t { + mf4010v2_ops_t ops; /**< 操作函数指针 */ uint16_t id; /**< CAN ID (0x000-0x7FF) */ uint8_t last_response[8]; /**< 最后一次响应数据 */ uint32_t last_comm_time; /**< 最后通信时间戳 (ms) */ uint16_t error_count; /**< 通信错误计数 */ uint16_t success_count; /**< 成功通信计数 */ uint8_t flags; /**< 状态标志 (@ref mf4010_flag_t) */ -} mf4010v2_t; - -struct __mf4010v2_ops_t { - mf4010_status_t (*init)(mf4010v2_t* self, uint16_t id); - mf4010_status_t (*send_command)(mf4010v2_t* self, const uint8_t command[8], uint32_t timeout_ms); }; -typedef struct __mf4010v2_ops_t mf4010v2_ops_t; + + /* ============================================================================ diff --git a/modules/sensor/imu/j901p.c b/modules/sensor/imu/j901p.c index 250162f..b456d94 100644 --- a/modules/sensor/imu/j901p.c +++ b/modules/sensor/imu/j901p.c @@ -393,7 +393,6 @@ int32_t WitStartAccCali(void) if(WitWriteReg(KEY, KEY_UNLOCK) != WIT_HAL_OK) return WIT_HAL_ERROR;// unlock reg if(s_uiProtoclo == WIT_PROTOCOL_MODBUS) p_WitDelaymsFunc(20); else if(s_uiProtoclo == WIT_PROTOCOL_NORMAL) p_WitDelaymsFunc(1); - else ; if(WitWriteReg(CALSW, CALGYROACC) != WIT_HAL_OK) return WIT_HAL_ERROR; return WIT_HAL_OK; } @@ -402,7 +401,6 @@ int32_t WitStopAccCali(void) if(WitWriteReg(CALSW, NORMAL) != WIT_HAL_OK) return WIT_HAL_ERROR; if(s_uiProtoclo == WIT_PROTOCOL_MODBUS) p_WitDelaymsFunc(20); else if(s_uiProtoclo == WIT_PROTOCOL_NORMAL) p_WitDelaymsFunc(1); - else ; if(WitWriteReg(SAVE, SAVE_PARAM) != WIT_HAL_OK) return WIT_HAL_ERROR; return WIT_HAL_OK; } @@ -412,7 +410,6 @@ int32_t WitStartMagCali(void) if(WitWriteReg(KEY, KEY_UNLOCK) != WIT_HAL_OK) return WIT_HAL_ERROR; if(s_uiProtoclo == WIT_PROTOCOL_MODBUS) p_WitDelaymsFunc(20); else if(s_uiProtoclo == WIT_PROTOCOL_NORMAL) p_WitDelaymsFunc(1); - else ; if(WitWriteReg(CALSW, CALMAGMM) != WIT_HAL_OK) return WIT_HAL_ERROR; return WIT_HAL_OK; } @@ -421,7 +418,6 @@ int32_t WitStopMagCali(void) if(WitWriteReg(KEY, KEY_UNLOCK) != WIT_HAL_OK) return WIT_HAL_ERROR; if(s_uiProtoclo == WIT_PROTOCOL_MODBUS) p_WitDelaymsFunc(20); else if(s_uiProtoclo == WIT_PROTOCOL_NORMAL) p_WitDelaymsFunc(1); - else ; if(WitWriteReg(CALSW, NORMAL) != WIT_HAL_OK) return WIT_HAL_ERROR; return WIT_HAL_OK; } @@ -435,7 +431,6 @@ int32_t WitSetUartBaud(int32_t uiBaudIndex) if(WitWriteReg(KEY, KEY_UNLOCK) != WIT_HAL_OK) return WIT_HAL_ERROR; if(s_uiProtoclo == WIT_PROTOCOL_MODBUS) p_WitDelaymsFunc(20); else if(s_uiProtoclo == WIT_PROTOCOL_NORMAL) p_WitDelaymsFunc(1); - else ; if(WitWriteReg(BAUD, uiBaudIndex) != WIT_HAL_OK) return WIT_HAL_ERROR; return WIT_HAL_OK; } @@ -449,7 +444,6 @@ int32_t WitSetCanBaud(int32_t uiBaudIndex) if(WitWriteReg(KEY, KEY_UNLOCK) != WIT_HAL_OK) return WIT_HAL_ERROR; if(s_uiProtoclo == WIT_PROTOCOL_MODBUS) p_WitDelaymsFunc(20); else if(s_uiProtoclo == WIT_PROTOCOL_NORMAL) p_WitDelaymsFunc(1); - else ; if(WitWriteReg(BAUD, uiBaudIndex) != WIT_HAL_OK) return WIT_HAL_ERROR; return WIT_HAL_OK; } @@ -463,7 +457,6 @@ int32_t WitSetBandwidth(int32_t uiBaudWidth) if(WitWriteReg(KEY, KEY_UNLOCK) != WIT_HAL_OK) return WIT_HAL_ERROR; if(s_uiProtoclo == WIT_PROTOCOL_MODBUS) p_WitDelaymsFunc(20); else if(s_uiProtoclo == WIT_PROTOCOL_NORMAL) p_WitDelaymsFunc(1); - else ; if(WitWriteReg(BANDWIDTH, uiBaudWidth) != WIT_HAL_OK) return WIT_HAL_ERROR; return WIT_HAL_OK; } @@ -478,7 +471,6 @@ int32_t WitSetOutputRate(int32_t uiRate) if(WitWriteReg(KEY, KEY_UNLOCK) != WIT_HAL_OK) return WIT_HAL_ERROR; if(s_uiProtoclo == WIT_PROTOCOL_MODBUS) p_WitDelaymsFunc(20); else if(s_uiProtoclo == WIT_PROTOCOL_NORMAL) p_WitDelaymsFunc(1); - else ; if(WitWriteReg(RRATE, uiRate) != WIT_HAL_OK) return WIT_HAL_ERROR; return WIT_HAL_OK; } @@ -493,7 +485,6 @@ int32_t WitSetContent(int32_t uiRsw) if(WitWriteReg(KEY, KEY_UNLOCK) != WIT_HAL_OK) return WIT_HAL_ERROR; if(s_uiProtoclo == WIT_PROTOCOL_MODBUS) p_WitDelaymsFunc(20); else if(s_uiProtoclo == WIT_PROTOCOL_NORMAL) p_WitDelaymsFunc(1); - else ; if(WitWriteReg(RSW, uiRsw) != WIT_HAL_OK) return WIT_HAL_ERROR; return WIT_HAL_OK; }