From 1d0343d3499ddd9630359ea821e4a2eba7f0dcc7 Mon Sep 17 00:00:00 2001 From: JasonChen <2805998671@qq.com> Date: Sun, 14 Jul 2024 16:07:46 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9E=20fix:=20=E4=BF=AE=E5=A4=8Dsyscall?= =?UTF-8?q?.h=E7=BC=96=E8=AF=91=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/Inc/syscalls.h | 73 ++----------------- Core/Src/main.c | 173 ++++++++++++++++++++------------------------ 2 files changed, 87 insertions(+), 159 deletions(-) diff --git a/Core/Inc/syscalls.h b/Core/Inc/syscalls.h index 879c28d..81c09c0 100644 --- a/Core/Inc/syscalls.h +++ b/Core/Inc/syscalls.h @@ -1,46 +1,18 @@ -/** - ****************************************************************************** - * @file syscalls.c - * @author Suroy Wrote with Auto-generated by STM32CubeIDE - * @url https://suroy.cn - * @brief STM32CubeIDE 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-2022 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 */ #ifndef __SYSCALLS_H #define __SYSCALLS_H + #include #include #include #include #include -#include -#include #include extern UART_HandleTypeDef huart2; -/* Variables */ extern int __io_putchar(int ch) __attribute__((weak)); extern int __io_getchar(void) __attribute__((weak)); -/* Functions */ - __attribute__((weak)) int _read(int file, char *ptr, int len) { (void)file; int DataIdx; @@ -71,36 +43,8 @@ __attribute__((weak)) int _write(int file, char *ptr, int len) { #define GETCHAR_PROTOTYPE int fgetc(FILE *f) #endif /* __GNUC__ */ -/** - * 函数功能: 重定向 c库函数 printf到 DEBUG_USARTx - * 输入参数: 无 - * 返 回 值: 无 - * 说 明:无 - */ -PUTCHAR_PROTOTYPE { - HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xFFFF); // 阻塞式无限等待 - return ch; -} - -/** - * 函数功能: 重定向 c库函数 getchar,scanf到 DEBUG_USARTx - * 输入参数: 无 - * 返 回 值: 无 - * 说 明:无 - */ -GETCHAR_PROTOTYPE { - uint8_t ch = 0; - HAL_UART_Receive(&huart2, &ch, 1, 0xFFFF); - - return ch; -} - -/* 非GCC模式才允许编译使用即 Keil、IAR 等 */ -#ifndef __GNUC__ - /** * @brief 重定向 C 标准库 printf 函数到串口 huart2 - * 适用于 Keil、IAR 等IDE;不适用 GCC * @author Suroy * @param ch * @param f @@ -108,14 +52,13 @@ GETCHAR_PROTOTYPE { * * @usage printf("USART1_Target:\r\n"); */ -int fputc(int ch, FILE *f) { - // 采用轮询方式发送1字节数据,超时时间为无限等待 - HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY); // huart2是串口的句柄 +PUTCHAR_PROTOTYPE { + HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY); // 阻塞式无限等待 return ch; } /** - * @brief fgets 重定向 + * @brief 重定向 c库函数 getchar,scanf到 DEBUG_USARTx * 重定向 C 标准库 scanf 函数到串口 huart2 * 注意以 空格 为结束 * @param f @@ -123,14 +66,12 @@ int fputc(int ch, FILE *f) { * * @usage scanf("%c", &RecData); */ -int fgetc(FILE *f) { - uint8_t ch; - HAL_UART_Receive(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY); // huart2是串口的句柄 +GETCHAR_PROTOTYPE { + uint8_t ch = 0; + HAL_UART_Receive(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY); return ch; } -#endif /* __GNUC__ */ - void delay_us(uint16_t us) { uint32_t delay = (HAL_RCC_GetHCLKFreq() / 4000000 * us); while (delay--); diff --git a/Core/Src/main.c b/Core/Src/main.c index f78c47e..6934fe1 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -18,11 +18,12 @@ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" + #include "dma.h" +#include "gpio.h" #include "i2c.h" #include "tim.h" #include "usart.h" -#include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ @@ -68,57 +69,49 @@ void SystemClock_Config(void); /* USER CODE END 0 */ /** - * @brief The application entry point. - * @retval int - */ -int main(void) -{ + * @brief The application entry point. + * @retval int + */ +int main(void) { + /* USER CODE BEGIN 1 */ - /* USER CODE BEGIN 1 */ + /* USER CODE END 1 */ - /* USER CODE END 1 */ + /* MCU Configuration--------------------------------------------------------*/ - /* MCU Configuration--------------------------------------------------------*/ + /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ + HAL_Init(); - /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ - HAL_Init(); + /* USER CODE BEGIN Init */ - /* USER CODE BEGIN Init */ + /* USER CODE END Init */ - /* USER CODE END Init */ + /* Configure the system clock */ + SystemClock_Config(); - /* Configure the system clock */ - SystemClock_Config(); + /* USER CODE BEGIN SysInit */ - /* USER CODE BEGIN SysInit */ + /* USER CODE END SysInit */ - /* USER CODE END SysInit */ + /* Initialize all configured peripherals */ + MX_GPIO_Init(); + MX_DMA_Init(); + MX_I2C2_Init(); + MX_TIM2_Init(); + MX_TIM3_Init(); + MX_TIM4_Init(); + MX_USART2_UART_Init(); + MX_USART1_UART_Init(); - /* Initialize all configured peripherals */ - MX_GPIO_Init(); - MX_DMA_Init(); - MX_I2C2_Init(); - MX_TIM2_Init(); - MX_TIM3_Init(); - MX_TIM4_Init(); - MX_USART2_UART_Init(); - MX_USART1_UART_Init(); - /* USER CODE BEGIN 2 */ + /* USER CODE BEGIN 2 */ HC_SR04_Init(); - /* Initialize all configured peripherals */ - MX_GPIO_Init(); - MX_I2C2_Init(); - MX_TIM2_Init(); - MX_TIM3_Init(); - MX_TIM4_Init(); - MX_USART2_UART_Init(); - /* USER CODE BEGIN 2 */ - - /* USER CODE END 2 */ + /* USER CODE BEGIN 2 */ - /* Infinite loop */ - /* USER CODE BEGIN WHILE */ + /* USER CODE END 2 */ + + /* Infinite loop */ + /* USER CODE BEGIN WHILE */ while (1) { printf("test\n"); @@ -128,50 +121,46 @@ int main(void) HAL_Delay(300); // 延时300毫秒 - /* USER CODE END WHILE */ + /* USER CODE END WHILE */ - /* USER CODE BEGIN 3 */ + /* USER CODE BEGIN 3 */ } - /* USER CODE END 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}; + * @brief System Clock Configuration + * @retval None + */ +void SystemClock_Config(void) { + RCC_OscInitTypeDef RCC_OscInitStruct = {0}; + RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; - /** Initializes the RCC Oscillators according to the specified parameters - * in the RCC_OscInitTypeDef structure. - */ - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; - RCC_OscInitStruct.HSEState = RCC_HSE_ON; - RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; - RCC_OscInitStruct.HSIState = RCC_HSI_ON; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; - RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; - RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) - { - Error_Handler(); - } + /** Initializes the RCC Oscillators according to the specified parameters + * in the RCC_OscInitTypeDef structure. + */ + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; + RCC_OscInitStruct.HSEState = RCC_HSE_ON; + RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; + RCC_OscInitStruct.HSIState = RCC_HSI_ON; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; + RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { + Error_Handler(); + } - /** Initializes the CPU, AHB and APB buses clocks - */ - RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK - |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; - RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; - RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; - RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; - RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; + /** Initializes the CPU, AHB and APB buses clocks + */ + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; - if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) - { - Error_Handler(); - } + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { + Error_Handler(); + } } /* USER CODE BEGIN 4 */ @@ -179,32 +168,30 @@ void SystemClock_Config(void) /* USER CODE END 4 */ /** - * @brief This function is executed in case of error occurrence. - * @retval None - */ -void Error_Handler(void) -{ - /* USER CODE BEGIN Error_Handler_Debug */ + * @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 */ + /* USER CODE END Error_Handler_Debug */ } -#ifdef USE_FULL_ASSERT +#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 */ + * @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 */ + /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */