From 11b1ebdc2b87fa1b20b98358c8b98c8bae8cfafe Mon Sep 17 00:00:00 2001 From: JasonChen <2805998671@qq.com> Date: Sat, 13 Jul 2024 15:22:46 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20=E8=B6=85=E5=A3=B0=E6=B3=A2?= =?UTF-8?q?=E6=B5=8B=E8=B7=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CAR.ioc | 18 +-- Core/Inc/hcsr04.h | 9 +- Core/Inc/main.h | 10 +- Core/Inc/stm32f1xx_hal_conf.h | 2 +- Core/Inc/syscalls.h | 138 ++++++++++++++++++ Core/Inc/usart.h | 2 +- Core/Src/gpio.c | 12 +- Core/Src/hcsr04.c | 86 ++++++----- Core/Src/main.c | 24 ++-- Core/Src/stm32f1xx_it.c | 2 +- Core/Src/syscalls.c | 1 + Core/Src/tim.c | 2 +- Core/Src/usart.c | 87 +++++------ MDK-ARM/CAR.uvoptx | 14 +- MDK-ARM/CAR.uvprojx | 117 +++++++-------- Makefile | 4 +- STM32Make.make | 263 ++++++++++++++++++++++++++++++++++ 17 files changed, 590 insertions(+), 201 deletions(-) create mode 100644 Core/Inc/syscalls.h create mode 100644 STM32Make.make diff --git a/CAR.ioc b/CAR.ioc index 59da6c9..f36bbd9 100644 --- a/CAR.ioc +++ b/CAR.ioc @@ -67,7 +67,7 @@ MxDb.Version=DB.6.0.120 NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false NVIC.DMA1_Channel6_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false -NVIC.EXTI15_10_IRQn=true\:1\:0\:true\:false\:true\:true\:true\:true +NVIC.EXTI15_10_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true NVIC.ForceEnableDMAVector=false NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false NVIC.MemoryManagement_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false @@ -90,11 +90,9 @@ PA1.GPIO_Label=FAN_GS PA1.Locked=true PA1.PinState=GPIO_PIN_SET PA1.Signal=GPIO_Output -PA10.Mode=Asynchronous -PA10.Signal=USART1_RX -PA12.GPIOParameters=GPIO_Label,GPIO_ModeDefaultEXTI -PA12.GPIO_Label=SDA_C_EXTI12 -PA12.GPIO_ModeDefaultEXTI=GPIO_MODE_IT_RISING_FALLING +PA12.GPIOParameters=GPIO_PuPd,GPIO_Label +PA12.GPIO_Label=Echo +PA12.GPIO_PuPd=GPIO_PULLDOWN PA12.Locked=true PA12.Signal=GPXTI12 PA13.Mode=Serial_Wire @@ -102,7 +100,7 @@ PA13.Signal=SYS_JTMS-SWDIO PA14.Mode=Serial_Wire PA14.Signal=SYS_JTCK-SWCLK PA15.GPIOParameters=GPIO_Speed,GPIO_Label -PA15.GPIO_Label=SCL_C +PA15.GPIO_Label=Trig PA15.GPIO_Speed=GPIO_SPEED_FREQ_HIGH PA15.Locked=true PA15.Signal=GPIO_Output @@ -114,8 +112,6 @@ PA7.GPIOParameters=GPIO_Label PA7.GPIO_Label=LED_B_TIM3CH2 PA7.Locked=true PA7.Signal=S_TIM3_CH2 -PA9.Mode=Asynchronous -PA9.Signal=USART1_TX PB0.GPIOParameters=GPIO_Label PB0.GPIO_Label=LED_G_TIM3CH3 PB0.Locked=true @@ -185,9 +181,9 @@ ProjectManager.ProjectBuild=false ProjectManager.ProjectFileName=CAR.ioc ProjectManager.ProjectName=CAR ProjectManager.ProjectStructure= -ProjectManager.RegisterCallBack= +ProjectManager.RegisterCallBack=TIM ProjectManager.StackSize=0x400 -ProjectManager.TargetToolchain=MDK-ARM V5.32 +ProjectManager.TargetToolchain=Makefile ProjectManager.ToolChainLocation= ProjectManager.UAScriptAfterPath= ProjectManager.UAScriptBeforePath= diff --git a/Core/Inc/hcsr04.h b/Core/Inc/hcsr04.h index e890867..3c7b8c0 100644 --- a/Core/Inc/hcsr04.h +++ b/Core/Inc/hcsr04.h @@ -3,6 +3,13 @@ #include "main.h" -uint16_t sonar(void); +extern uint32_t sonor_distance; // 测量出的距离,单位mm +extern uint8_t sonor_state; // 测距状态变量,0:未测量,1:正在测量,2:已测量 + +void HC_SR04_Init(void); +uint32_t sonar_mm(void); + +// 测距完毕回调,执行完自定义任务后将sonor_state置0 + void Sonar_CP_Callback(); #endif diff --git a/Core/Inc/main.h b/Core/Inc/main.h index 7289bbe..495fe9c 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -71,11 +71,11 @@ void Error_Handler(void); #define MPU6050_SCL_GPIO_Port GPIOB #define MPU6050_SDA_Pin GPIO_PIN_11 #define MPU6050_SDA_GPIO_Port GPIOB -#define SDA_C_EXTI12_Pin GPIO_PIN_12 -#define SDA_C_EXTI12_GPIO_Port GPIOA -#define SDA_C_EXTI12_EXTI_IRQn EXTI15_10_IRQn -#define SCL_C_Pin GPIO_PIN_15 -#define SCL_C_GPIO_Port GPIOA +#define Echo_Pin GPIO_PIN_12 +#define Echo_GPIO_Port GPIOA +#define Echo_EXTI_IRQn EXTI15_10_IRQn +#define Trig_Pin GPIO_PIN_15 +#define Trig_GPIO_Port GPIOA #define R_FORWARD_Pin GPIO_PIN_4 #define R_FORWARD_GPIO_Port GPIOB #define R_BACKWARD_Pin GPIO_PIN_5 diff --git a/Core/Inc/stm32f1xx_hal_conf.h b/Core/Inc/stm32f1xx_hal_conf.h index 0afbce5..ff0bae5 100644 --- a/Core/Inc/stm32f1xx_hal_conf.h +++ b/Core/Inc/stm32f1xx_hal_conf.h @@ -152,7 +152,7 @@ #define USE_HAL_IRDA_REGISTER_CALLBACKS 0U /* IRDA register callback disabled */ #define USE_HAL_SRAM_REGISTER_CALLBACKS 0U /* SRAM register callback disabled */ #define USE_HAL_SPI_REGISTER_CALLBACKS 0U /* SPI register callback disabled */ -#define USE_HAL_TIM_REGISTER_CALLBACKS 0U /* TIM register callback disabled */ +#define USE_HAL_TIM_REGISTER_CALLBACKS 1U /* TIM register callback enabled */ #define USE_HAL_UART_REGISTER_CALLBACKS 0U /* UART register callback disabled */ #define USE_HAL_USART_REGISTER_CALLBACKS 0U /* USART register callback disabled */ #define USE_HAL_WWDG_REGISTER_CALLBACKS 0U /* WWDG register callback disabled */ diff --git a/Core/Inc/syscalls.h b/Core/Inc/syscalls.h new file mode 100644 index 0000000..879c28d --- /dev/null +++ b/Core/Inc/syscalls.h @@ -0,0 +1,138 @@ +/** + ****************************************************************************** + * @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; + + 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; +} + +// 条件编译 +#ifdef __GNUC__ +#define PUTCHAR_PROTOTYPE int __io_putchar(int ch) +#define GETCHAR_PROTOTYPE int __io_getchar(void) +#else +#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) +#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 + * @return int + * + * @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是串口的句柄 + return ch; +} + +/** + * @brief fgets 重定向 + * 重定向 C 标准库 scanf 函数到串口 huart2 + * 注意以 空格 为结束 + * @param f + * @return int + * + * @usage scanf("%c", &RecData); + */ +int fgetc(FILE *f) { + uint8_t ch; + HAL_UART_Receive(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY); // huart2是串口的句柄 + return ch; +} + +#endif /* __GNUC__ */ + +void delay_us(uint16_t us) { + uint32_t delay = (HAL_RCC_GetHCLKFreq() / 4000000 * us); + while (delay--); +} +#endif diff --git a/Core/Inc/usart.h b/Core/Inc/usart.h index c54411d..37e914b 100644 --- a/Core/Inc/usart.h +++ b/Core/Inc/usart.h @@ -32,7 +32,7 @@ extern "C" { /* USER CODE END Includes */ -extern UART_HandleTypeDef huart1; +extern UART_HandleTypeDef huart2; extern UART_HandleTypeDef huart2; diff --git a/Core/Src/gpio.c b/Core/Src/gpio.c index c7267c7..ccab9ed 100644 --- a/Core/Src/gpio.c +++ b/Core/Src/gpio.c @@ -53,7 +53,7 @@ void MX_GPIO_Init(void) HAL_GPIO_WritePin(GPIOA, FM_K2_POWERC_Pin|FAN_GS_Pin, GPIO_PIN_SET); /*Configure GPIO pin Output Level */ - HAL_GPIO_WritePin(SCL_C_GPIO_Port, SCL_C_Pin, GPIO_PIN_RESET); + HAL_GPIO_WritePin(Trig_GPIO_Port, Trig_Pin, GPIO_PIN_RESET); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOB, R_FORWARD_Pin|R_BACKWARD_Pin|L_BACKWARD_Pin|L_FORWARD_Pin, GPIO_PIN_RESET); @@ -66,17 +66,17 @@ void MX_GPIO_Init(void) HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /*Configure GPIO pin : PtPin */ - GPIO_InitStruct.Pin = SDA_C_EXTI12_Pin; + GPIO_InitStruct.Pin = Echo_Pin; GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING; - GPIO_InitStruct.Pull = GPIO_NOPULL; - HAL_GPIO_Init(SDA_C_EXTI12_GPIO_Port, &GPIO_InitStruct); + GPIO_InitStruct.Pull = GPIO_PULLDOWN; + HAL_GPIO_Init(Echo_GPIO_Port, &GPIO_InitStruct); /*Configure GPIO pin : PtPin */ - GPIO_InitStruct.Pin = SCL_C_Pin; + GPIO_InitStruct.Pin = Trig_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; - HAL_GPIO_Init(SCL_C_GPIO_Port, &GPIO_InitStruct); + HAL_GPIO_Init(Trig_GPIO_Port, &GPIO_InitStruct); /*Configure GPIO pins : PBPin PBPin PBPin PBPin */ GPIO_InitStruct.Pin = R_FORWARD_Pin|R_BACKWARD_Pin|L_BACKWARD_Pin|L_FORWARD_Pin; diff --git a/Core/Src/hcsr04.c b/Core/Src/hcsr04.c index 5a29526..c971bd8 100644 --- a/Core/Src/hcsr04.c +++ b/Core/Src/hcsr04.c @@ -1,66 +1,60 @@ #include "hcsr04.h" +#include "stm32f1xx_hal_tim.h" #include "tim.h" -// time 的单位是 10μs -static volatile uint64_t time; // 声明变量,用来计时 -static uint64_t time_end; // 声明变量,存储回波信号时间 -static uint8_t state; -static int16_t delay_time = -1; // 用于延时 +uint64_t time; // 声明变量,用来计时 +uint64_t time_end; // 声明变量,存储回波信号时间 +uint8_t sonor_state = 1; // 测距状态变量,0:未测量,1:正在测量,2:已测量 +uint32_t sonor_distance; // 测量出的距离,单位mm -// HACK 临时的 delay 解决方法 -void delay_us(uint16_t us); +void HCSR04_HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim); -uint16_t sonar(void) // 测距并返回单位为毫米的距离结果 -{ - // 超时则返回5m距离 - uint32_t distance = 5000; - HAL_GPIO_WritePin(SCL_C_GPIO_Port, SCL_C_Pin, GPIO_PIN_SET); // 输出高电平 - delay_us(15); // 延时15微秒 - HAL_GPIO_WritePin(SCL_C_GPIO_Port, SCL_C_Pin, GPIO_PIN_RESET); // 输出低电平 +// 初始化超声波测距 +void HC_SR04_Init(void) { + // 禁用JTAG + __HAL_RCC_AFIO_CLK_ENABLE(); + __HAL_AFIO_REMAP_SWJ_NOJTAG(); - // HACK 考虑RTOS优化 - HAL_Delay(15); - - if (state) - distance = time_end * 346 / 200; // 计算距离,25°C空气中的音速为346m/s - return distance; + // 注册定时器中断溢出回调 + HAL_TIM_RegisterCallback(&htim2, HAL_TIM_PERIOD_ELAPSED_CB_ID, HCSR04_HAL_TIM_PeriodElapsedCallback); } -void delay_us(uint16_t us) -{ - delay_time = us; - time = 0; - HAL_TIM_Base_Start_IT(&htim2); - while (time < us) - ; - delay_time = -1; +// 启动测距,单位:mm +uint32_t sonar_mm(void) { + sonor_state = 1; + HAL_GPIO_WritePin(Trig_GPIO_Port, Trig_Pin, GPIO_PIN_SET); // 输出高电平 + delay_us(15); // 延时15微秒 + HAL_GPIO_WritePin(Trig_GPIO_Port, Trig_Pin, GPIO_PIN_RESET); // 输出低电平 + if (time_end / 100 < 38) { // 判断是否小于38毫秒,大于38毫秒的就是超时,直接调到下面返回0 + sonor_distance = (time_end * 346) / 2; // 计算距离,25°C空气中的音速为346m/s + sonor_distance = sonor_distance / 100; // 因为上面的time_end的单位是10微秒,所以要得出单位为毫米的距离结果,还得除以100 + } + return sonor_distance; } -void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) -{ - if (htim == &htim2) - { - ++time; - if (delay_us > 0 && time == delay_time) - HAL_TIM_Base_Stop_IT(&htim2); +void HCSR04_HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim) { + if (htim == &htim2) { // 每10us自增一 + time++; } } -void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) -{ - if (GPIO_Pin == SDA_C_EXTI12_Pin) - { - if (HAL_GPIO_ReadPin(SDA_C_EXTI12_GPIO_Port, SDA_C_EXTI12_Pin)) - { - time = 0; +void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { + if (GPIO_Pin == Echo_Pin) { + if (HAL_GPIO_ReadPin(Echo_GPIO_Port, Echo_Pin)) { HAL_TIM_Base_Start_IT(&htim2); - } - else - { + time = 0; + } else { time_end = time; HAL_TIM_Base_Stop_IT(&htim2); - state = 1; + sonor_state = 2; + Sonar_CP_Callback(); } } } + +// 测距完毕回调,执行完自定义任务后将sonor_state置0 +__attribute__((weak)) void Sonar_CP_Callback() { + printf("distance = %d (mm)\n", sonor_distance); + sonor_state = 0; +} diff --git a/Core/Src/main.c b/Core/Src/main.c index 7252aa9..750cce2 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -26,8 +26,10 @@ /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ +#include "hcsr04.h" #include "led.h" #include "motor.h" +#include "syscalls.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -59,6 +61,9 @@ void SystemClock_Config(void); /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ +// void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { +// printf("main.c\n"); +// } /* USER CODE END 0 */ @@ -92,9 +97,10 @@ int main(void) { MX_I2C2_Init(); MX_TIM3_Init(); MX_TIM4_Init(); - MX_USART1_UART_Init(); MX_TIM2_Init(); + MX_USART2_UART_Init(); /* USER CODE BEGIN 2 */ + HC_SR04_Init(); /* Initialize all configured peripherals */ MX_GPIO_Init(); @@ -111,20 +117,12 @@ int main(void) { /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { - int Distance_mm = sonar_mm(); - int Distance_m = Distance_mm / 1000; - int Distance_m_p = Distance_mm % 1000; - printf("Distance_mm = %d\n", Distance_mm); - printf("Distance_m = %d\n", Distance_m); - printf("Distance_m_p = %d\n", Distance_m_p); + printf("test\n"); - if (Distance_mm < 100) { - LED_Start(LED_R); - MOTOR_Start(); - } else { - LED_Stop(LED_R); - MOTOR_Stop(); + if (sonor_state == 0) { + sonar_mm(); } + HAL_Delay(300); // 延时300毫秒 /* USER CODE END WHILE */ diff --git a/Core/Src/stm32f1xx_it.c b/Core/Src/stm32f1xx_it.c index 3c0d409..cd1dc7d 100644 --- a/Core/Src/stm32f1xx_it.c +++ b/Core/Src/stm32f1xx_it.c @@ -265,7 +265,7 @@ void EXTI15_10_IRQHandler(void) /* USER CODE BEGIN EXTI15_10_IRQn 0 */ /* USER CODE END EXTI15_10_IRQn 0 */ - HAL_GPIO_EXTI_IRQHandler(SDA_C_EXTI12_Pin); + HAL_GPIO_EXTI_IRQHandler(Echo_Pin); /* USER CODE BEGIN EXTI15_10_IRQn 1 */ /* USER CODE END EXTI15_10_IRQn 1 */ diff --git a/Core/Src/syscalls.c b/Core/Src/syscalls.c index a60e42f..e33a849 100644 --- a/Core/Src/syscalls.c +++ b/Core/Src/syscalls.c @@ -28,6 +28,7 @@ #include #include #include +#include /* Variables */ diff --git a/Core/Src/tim.c b/Core/Src/tim.c index ec3e6eb..f908f29 100644 --- a/Core/Src/tim.c +++ b/Core/Src/tim.c @@ -47,7 +47,7 @@ void MX_TIM2_Init(void) htim2.Init.CounterMode = TIM_COUNTERMODE_UP; htim2.Init.Period = 72 - 1; htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; - htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; + htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; if (HAL_TIM_Base_Init(&htim2) != HAL_OK) { Error_Handler(); diff --git a/Core/Src/usart.c b/Core/Src/usart.c index a3f577c..4c4c945 100644 --- a/Core/Src/usart.c +++ b/Core/Src/usart.c @@ -28,33 +28,33 @@ UART_HandleTypeDef huart1; UART_HandleTypeDef huart2; DMA_HandleTypeDef hdma_usart2_rx; -/* USART1 init function */ +/* USART2 init function */ -void MX_USART1_UART_Init(void) +void MX_USART2_UART_Init(void) { - /* USER CODE BEGIN USART1_Init 0 */ + /* USER CODE BEGIN USART2_Init 0 */ - /* USER CODE END USART1_Init 0 */ + /* USER CODE END USART2_Init 0 */ - /* USER CODE BEGIN USART1_Init 1 */ + /* USER CODE BEGIN USART2_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) + /* USER CODE END USART2_Init 1 */ + huart2.Instance = USART2; + huart2.Init.BaudRate = 115200; + huart2.Init.WordLength = UART_WORDLENGTH_8B; + huart2.Init.StopBits = UART_STOPBITS_1; + huart2.Init.Parity = UART_PARITY_NONE; + huart2.Init.Mode = UART_MODE_TX_RX; + huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; + huart2.Init.OverSampling = UART_OVERSAMPLING_16; + if (HAL_UART_Init(&huart2) != HAL_OK) { Error_Handler(); } - /* USER CODE BEGIN USART1_Init 2 */ + /* USER CODE BEGIN USART2_Init 2 */ - /* USER CODE END USART1_Init 2 */ + /* USER CODE END USART2_Init 2 */ } /* USART2 init function */ @@ -91,25 +91,25 @@ void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) { GPIO_InitTypeDef GPIO_InitStruct = {0}; - if(uartHandle->Instance==USART1) + if(uartHandle->Instance==USART2) { - /* USER CODE BEGIN USART1_MspInit 0 */ + /* USER CODE BEGIN USART2_MspInit 0 */ - /* USER CODE END USART1_MspInit 0 */ - /* USART1 clock enable */ - __HAL_RCC_USART1_CLK_ENABLE(); + /* USER CODE END USART2_MspInit 0 */ + /* USART2 clock enable */ + __HAL_RCC_USART2_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); - /**USART1 GPIO Configuration - PA9 ------> USART1_TX - PA10 ------> USART1_RX + /**USART2 GPIO Configuration + PA2 ------> USART2_TX + PA3 ------> USART2_RX */ - GPIO_InitStruct.Pin = GPIO_PIN_9; + GPIO_InitStruct.Pin = GPIO_PIN_2; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - GPIO_InitStruct.Pin = GPIO_PIN_10; + GPIO_InitStruct.Pin = GPIO_PIN_3; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); @@ -119,7 +119,7 @@ void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) HAL_NVIC_EnableIRQ(USART1_IRQn); /* USER CODE BEGIN USART1_MspInit 1 */ - /* USER CODE END USART1_MspInit 1 */ + /* USER CODE END USART2_MspInit 1 */ } else if(uartHandle->Instance==USART2) { @@ -173,25 +173,25 @@ void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) { - if(uartHandle->Instance==USART1) + if(uartHandle->Instance==USART2) { - /* USER CODE BEGIN USART1_MspDeInit 0 */ + /* USER CODE BEGIN USART2_MspDeInit 0 */ - /* USER CODE END USART1_MspDeInit 0 */ + /* USER CODE END USART2_MspDeInit 0 */ /* Peripheral clock disable */ - __HAL_RCC_USART1_CLK_DISABLE(); + __HAL_RCC_USART2_CLK_DISABLE(); - /**USART1 GPIO Configuration - PA9 ------> USART1_TX - PA10 ------> USART1_RX + /**USART2 GPIO Configuration + PA2 ------> USART2_TX + PA3 ------> USART2_RX */ - HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10); + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3); /* USART1 interrupt Deinit */ HAL_NVIC_DisableIRQ(USART1_IRQn); /* USER CODE BEGIN USART1_MspDeInit 1 */ - /* USER CODE END USART1_MspDeInit 1 */ + /* USER CODE END USART2_MspDeInit 1 */ } else if(uartHandle->Instance==USART2) { @@ -219,18 +219,5 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) } /* USER CODE BEGIN 1 */ -int fputc(int c, FILE* f) -{ - static uint8_t ch; - ch = c; - HAL_UART_Transmit(&huart1, &ch, 1, 1000); - return c; -} -int fgetc(FILE* f) -{ - static uint8_t ch; - HAL_UART_Receive(&huart1, &ch, 1, 0xffff); - return ch; -} /* USER CODE END 1 */ diff --git a/MDK-ARM/CAR.uvoptx b/MDK-ARM/CAR.uvoptx index 6631c6d..bfd2666 100644 --- a/MDK-ARM/CAR.uvoptx +++ b/MDK-ARM/CAR.uvoptx @@ -1,4 +1,4 @@ - + 1.0 @@ -45,7 +45,7 @@ 79 66 8 - + 1 @@ -154,7 +154,7 @@ 0 ARMDBGFLAGS - + 0 @@ -164,7 +164,7 @@ 0 DLGUARM - + 0 @@ -292,8 +292,8 @@ 0 0 - - + + 0 @@ -681,7 +681,7 @@ Drivers/CMSIS - 0 + 1 0 0 0 diff --git a/MDK-ARM/CAR.uvprojx b/MDK-ARM/CAR.uvprojx index fe75818..8f084c3 100644 --- a/MDK-ARM/CAR.uvprojx +++ b/MDK-ARM/CAR.uvprojx @@ -1,7 +1,10 @@ - - + + + 2.1 +
### uVision Project, (C) Keil Software
+ CAR @@ -15,28 +18,28 @@ Keil.STM32F1xx_DFP.2.2.0 http://www.keil.com/pack/ IRAM(0x20000000-0x20004FFF) IROM(0x8000000-0x800FFFF) CLOCK(8000000) CPUTYPE("Cortex-M3") TZ - - - + + + 0 - - - - - - - - - - + + + + + + + + + + $$Device:STM32F103C8$SVD\STM32F103xx.svd 0 0 - - - - - + + + + + 0 0 @@ -51,15 +54,15 @@ 1 1 1 - + 1 0 0 0 0 - - + + 0 0 0 @@ -68,8 +71,8 @@ 0 0 - - + + 0 0 0 @@ -78,15 +81,15 @@ 0 1 - - + + 0 0 0 0 1 - + 0 @@ -100,8 +103,8 @@ 0 0 3 - - + + 0 @@ -110,7 +113,7 @@ DCM.DLL -pCM3 SARMCM3.DLL - + TCM.DLL -pCM3 @@ -135,10 +138,10 @@ 1 BIN\UL2V8M.DLL "" () - - - - + + + + 0 @@ -171,7 +174,7 @@ 0 0 "Cortex-M3" - + 0 0 0 @@ -303,7 +306,7 @@ 0x0 - + 1 @@ -329,9 +332,9 @@ 0 0 - + USE_HAL_DRIVER,STM32F103xB - + ../Core/Inc;../Drivers/STM32F1xx_HAL_Driver/Inc;../Drivers/STM32F1xx_HAL_Driver/Inc/Legacy;../Drivers/CMSIS/Device/ST/STM32F1xx/Include;../Drivers/CMSIS/Include @@ -347,10 +350,10 @@ 0 0 - - - - + + + + @@ -360,15 +363,15 @@ 0 1 0 - - - - - - - - - + + + + + + + + + @@ -599,18 +602,20 @@ + - + - + - + - + + diff --git a/Makefile b/Makefile index fc2fb0f..7a96bf0 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ ########################################################################################################################## -# File automatically-generated by tool: [projectgenerator] version: [4.4.0-B60] date: [Thu Jul 11 23:29:43 CST 2024] +# File automatically-generated by tool: [projectgenerator] version: [4.4.0-B60] date: [Sat Jul 13 14:09:19 CST 2024] ########################################################################################################################## # ------------------------------------------------ @@ -60,10 +60,10 @@ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c \ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_uart.c \ Core/Src/system_stm32f1xx.c \ Core/Src/sysmem.c \ -Core/Src/syscalls.c \ Core/Src/hcsr04.c \ Core/Src/motor.c \ Core/Src/led.c \ +Core/Src/syscalls.c # ASM sources ASM_SOURCES = \ diff --git a/STM32Make.make b/STM32Make.make new file mode 100644 index 0000000..7a3b0e7 --- /dev/null +++ b/STM32Make.make @@ -0,0 +1,263 @@ +########################################################################################################################## +# File automatically-generated by STM32forVSCode +########################################################################################################################## + +# ------------------------------------------------ +# Generic Makefile (based on gcc) +# +# ChangeLog : +# 2017-02-10 - Several enhancements + project update mode +# 2015-07-22 - first version +# ------------------------------------------------ + +###################################### +# target +###################################### +TARGET = CAR + + +###################################### +# building variables +###################################### +# debug build? +DEBUG = 1 +# optimization +OPT = -Og + + +####################################### +# paths +####################################### +# Build path +BUILD_DIR = build + +###################################### +# source +###################################### +# C sources +C_SOURCES = \ +Core/Src/gpio.c \ +Core/Src/hcsr04.c \ +Core/Src/i2c.c \ +Core/Src/led.c \ +Core/Src/main.c \ +Core/Src/motor.c \ +Core/Src/stm32f1xx_hal_msp.c \ +Core/Src/stm32f1xx_it.c \ +Core/Src/syscalls.c \ +Core/Src/sysmem.c \ +Core/Src/system_stm32f1xx.c \ +Core/Src/tim.c \ +Core/Src/usart.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_exti.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_i2c.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pwr.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c \ +Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_uart.c + + +CPP_SOURCES = \ + + +# ASM sources +ASM_SOURCES = \ +startup_stm32f103xb.s + + + +####################################### +# binaries +####################################### +PREFIX = arm-none-eabi- +POSTFIX = " +# The gcc compiler bin path can be either defined in make command via GCC_PATH variable (> make GCC_PATH=xxx) +# either it can be added to the PATH environment variable. +GCC_PATH="E:/PROGRAM FILES/GNU ARM EMBEDDED TOOLCHAIN/10 2021.10/BIN +ifdef GCC_PATH +CXX = $(GCC_PATH)/$(PREFIX)g++$(POSTFIX) +CC = $(GCC_PATH)/$(PREFIX)gcc$(POSTFIX) +AS = $(GCC_PATH)/$(PREFIX)gcc$(POSTFIX) -x assembler-with-cpp +CP = $(GCC_PATH)/$(PREFIX)objcopy$(POSTFIX) +SZ = $(GCC_PATH)/$(PREFIX)size$(POSTFIX) +else +CXX = $(PREFIX)g++ +CC = $(PREFIX)gcc +AS = $(PREFIX)gcc -x assembler-with-cpp +CP = $(PREFIX)objcopy +SZ = $(PREFIX)size +endif +HEX = $(CP) -O ihex +BIN = $(CP) -O binary -S + +####################################### +# CFLAGS +####################################### +# cpu +CPU = -mcpu=cortex-m3 + +# fpu +FPU = + +# float-abi +FLOAT-ABI = + +# mcu +MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI) + +# macros for gcc +# AS defines +AS_DEFS = + +# C defines +C_DEFS = \ +-DSTM32F103xB \ +-DUSE_HAL_DRIVER + + +# CXX defines +CXX_DEFS = \ +-DSTM32F103xB \ +-DUSE_HAL_DRIVER + + +# AS includes +AS_INCLUDES = \ + +# C includes +C_INCLUDES = \ +-ICore/Inc \ +-IDrivers/CMSIS/Device/ST/STM32F1xx/Include \ +-IDrivers/CMSIS/Include \ +-IDrivers/STM32F1xx_HAL_Driver/Inc \ +-IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy + + + +# compile gcc flags +ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections + +CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections + +CXXFLAGS = $(MCU) $(CXX_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections -feliminate-unused-debug-types + +ifeq ($(DEBUG), 1) +CFLAGS += -g -gdwarf -ggdb +CXXFLAGS += -g -gdwarf -ggdb +endif + +# Add additional flags +CFLAGS += -Wall -fdata-sections -ffunction-sections +ASFLAGS += -Wall -fdata-sections -ffunction-sections +CXXFLAGS += + +# Generate dependency information +CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)" +CXXFLAGS += -MMD -MP -MF"$(@:%.o=%.d)" + +####################################### +# LDFLAGS +####################################### +# link script +LDSCRIPT = STM32F103C8Tx_FLASH.ld + +# libraries +LIBS = -lc -lm -lnosys +LIBDIR = \ + + +# Additional LD Flags from config file +ADDITIONALLDFLAGS = -Wl,--print-memory-usage -specs=nano.specs + +LDFLAGS = $(MCU) $(ADDITIONALLDFLAGS) -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections + +# default action: build all +all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin + + +####################################### +# build the application +####################################### +# list of cpp program objects +OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(CPP_SOURCES:.cpp=.o))) +vpath %.cpp $(sort $(dir $(CPP_SOURCES))) + +# list of C objects +OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o))) +vpath %.c $(sort $(dir $(C_SOURCES))) + +# list of ASM program objects +UPPER_CASE_ASM_SOURCES = $(filter %.S,$(ASM_SOURCES)) +LOWER_CASE_ASM_SOURCES = $(filter %.s,$(ASM_SOURCES)) + +OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(UPPER_CASE_ASM_SOURCES:.S=.o))) +OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(LOWER_CASE_ASM_SOURCES:.s=.o))) +vpath %.s $(sort $(dir $(ASM_SOURCES))) + +$(BUILD_DIR)/%.o: %.cpp STM32Make.make | $(BUILD_DIR) + $(CXX) -c $(CXXFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.cpp=.lst)) $< -o $@ + +$(BUILD_DIR)/%.o: %.cxx STM32Make.make | $(BUILD_DIR) + $(CXX) -c $(CXXFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.cxx=.lst)) $< -o $@ + +$(BUILD_DIR)/%.o: %.c STM32Make.make | $(BUILD_DIR) + $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@ + +$(BUILD_DIR)/%.o: %.s STM32Make.make | $(BUILD_DIR) + $(AS) -c $(CFLAGS) $< -o $@ + +$(BUILD_DIR)/%.o: %.S STM32Make.make | $(BUILD_DIR) + $(AS) -c $(CFLAGS) $< -o $@ + +$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) STM32Make.make + $(CC) $(OBJECTS) $(LDFLAGS) -o $@ + $(SZ) $@ + +$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR) + $(HEX) $< $@ + +$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR) + $(BIN) $< $@ + +$(BUILD_DIR): + mkdir $@ + +####################################### +# flash +####################################### +flash: $(BUILD_DIR)/$(TARGET).elf + "E:/PROGRAM FILES/OPENOCD-20231002-0.12.0/BIN/OPENOCD.EXE" -f ./openocd.cfg -c "program $(BUILD_DIR)/$(TARGET).elf verify reset exit" + +####################################### +# erase +####################################### +erase: $(BUILD_DIR)/$(TARGET).elf + "E:/PROGRAM FILES/OPENOCD-20231002-0.12.0/BIN/OPENOCD.EXE" -f ./openocd.cfg -c "init; reset halt; stm32f1x mass_erase 0; exit" + +####################################### +# clean up +####################################### +clean: + cmd /c rd /s /q $(BUILD_DIR) + +####################################### +# custom makefile rules +####################################### + + + +####################################### +# dependencies +####################################### +-include $(wildcard $(BUILD_DIR)/*.d) + +# *** EOF *** \ No newline at end of file