diff --git a/CAR.ioc b/CAR.ioc index 24547a7..073b083 100644 --- a/CAR.ioc +++ b/CAR.ioc @@ -14,13 +14,13 @@ Mcu.IP3=SYS Mcu.IP4=TIM2 Mcu.IP5=TIM3 Mcu.IP6=TIM4 -Mcu.IP7=USART1 +Mcu.IP7=USART2 Mcu.IPNb=8 Mcu.Name=STM32F103C(8-B)Tx Mcu.Package=LQFP48 Mcu.Pin0=PD0-OSC_IN Mcu.Pin1=PD1-OSC_OUT -Mcu.Pin10=PA10 +Mcu.Pin10=PB11 Mcu.Pin11=PA12 Mcu.Pin12=PA13 Mcu.Pin13=PA14 @@ -37,12 +37,12 @@ Mcu.Pin22=VP_TIM2_VS_ClockSourceINT Mcu.Pin23=VP_TIM3_VS_ClockSourceINT Mcu.Pin24=VP_TIM4_VS_ClockSourceINT Mcu.Pin3=PA1 -Mcu.Pin4=PA7 -Mcu.Pin5=PB0 -Mcu.Pin6=PB1 -Mcu.Pin7=PB10 -Mcu.Pin8=PB11 -Mcu.Pin9=PA9 +Mcu.Pin4=PA2 +Mcu.Pin5=PA3 +Mcu.Pin6=PA7 +Mcu.Pin7=PB0 +Mcu.Pin8=PB1 +Mcu.Pin9=PB10 Mcu.PinsNb=25 Mcu.ThirdPartyNb=0 Mcu.UserConstants= @@ -51,7 +51,7 @@ MxCube.Version=6.12.0 MxDb.Version=DB.6.0.120 NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false 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 @@ -61,6 +61,7 @@ NVIC.PriorityGroup=NVIC_PRIORITYGROUP_2 NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false NVIC.SysTick_IRQn=true\:3\:0\:true\:false\:true\:false\:true\:false NVIC.TIM2_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true +NVIC.USART2_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false PA0-WKUP.GPIOParameters=PinState,GPIO_Label PA0-WKUP.GPIO_Label=FM_K2_POWERC @@ -72,11 +73,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 @@ -84,16 +83,18 @@ 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 +PA2.Mode=Asynchronous +PA2.Signal=USART2_TX +PA3.Mode=Asynchronous +PA3.Signal=USART2_RX 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 @@ -163,14 +164,14 @@ 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= ProjectManager.UnderRoot=false -ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_I2C2_Init-I2C2-false-HAL-true,4-MX_TIM3_Init-TIM3-false-HAL-true,5-MX_TIM4_Init-TIM4-false-HAL-true,6-MX_USART1_UART_Init-USART1-false-HAL-true,7-MX_TIM2_Init-TIM2-false-HAL-true +ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_I2C2_Init-I2C2-false-HAL-true,4-MX_TIM3_Init-TIM3-false-HAL-true,5-MX_TIM4_Init-TIM4-false-HAL-true,6-MX_TIM2_Init-TIM2-false-HAL-true,7-MX_USART2_UART_Init-USART2-false-HAL-true,8-MX_USART1_UART_Init-USART1-false-HAL-true,9-MX_TIM1_Init-TIM1-false-HAL-true RCC.ADCFreqValue=36000000 RCC.AHBFreq_Value=72000000 RCC.APB1CLKDivider=RCC_HCLK_DIV2 @@ -204,8 +205,9 @@ SH.S_TIM4_CH1.0=TIM4_CH1,PWM Generation1 CH1 SH.S_TIM4_CH1.ConfNb=1 SH.S_TIM4_CH2.0=TIM4_CH2,PWM Generation2 CH2 SH.S_TIM4_CH2.ConfNb=1 -TIM2.IPParameters=Prescaler,Period -TIM2.Period=1 - 1 +TIM2.AutoReloadPreload=TIM_AUTORELOAD_PRELOAD_ENABLE +TIM2.IPParameters=Prescaler,Period,AutoReloadPreload +TIM2.Period=10 - 1 TIM2.Prescaler=72 - 1 TIM3.Channel-PWM\ Generation2\ CH2=TIM_CHANNEL_2 TIM3.Channel-PWM\ Generation3\ CH3=TIM_CHANNEL_3 @@ -224,8 +226,8 @@ TIM4.Period=100 TIM4.Prescaler=720 - 1 TIM4.Pulse-PWM\ Generation1\ CH1=100 TIM4.Pulse-PWM\ Generation2\ CH2=100 -USART1.IPParameters=VirtualMode -USART1.VirtualMode=VM_ASYNC +USART2.IPParameters=VirtualMode +USART2.VirtualMode=VM_ASYNC VP_SYS_VS_Systick.Mode=SysTick VP_SYS_VS_Systick.Signal=SYS_VS_Systick VP_TIM2_VS_ClockSourceINT.Mode=Internal diff --git a/Core/Inc/hcsr04.h b/Core/Inc/hcsr04.h index 9c00c39..3c7b8c0 100644 --- a/Core/Inc/hcsr04.h +++ b/Core/Inc/hcsr04.h @@ -3,7 +3,13 @@ #include "main.h" -int16_t sonar_mm(void); -float 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 3c21e51..ae7fcc9 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/stm32f1xx_it.h b/Core/Inc/stm32f1xx_it.h index 3a79269..d44be6e 100644 --- a/Core/Inc/stm32f1xx_it.h +++ b/Core/Inc/stm32f1xx_it.h @@ -56,6 +56,7 @@ void DebugMon_Handler(void); void PendSV_Handler(void); void SysTick_Handler(void); void TIM2_IRQHandler(void); +void USART2_IRQHandler(void); void EXTI15_10_IRQHandler(void); /* USER CODE BEGIN EFP */ 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 b8f18ef..f0e2da3 100644 --- a/Core/Inc/usart.h +++ b/Core/Inc/usart.h @@ -32,13 +32,13 @@ extern "C" { /* USER CODE END Includes */ -extern UART_HandleTypeDef huart1; +extern UART_HandleTypeDef huart2; /* USER CODE BEGIN Private defines */ /* USER CODE END Private defines */ -void MX_USART1_UART_Init(void); +void MX_USART2_UART_Init(void); /* USER CODE BEGIN Prototypes */ 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 834455d..c971bd8 100644 --- a/Core/Src/hcsr04.c +++ b/Core/Src/hcsr04.c @@ -1,83 +1,60 @@ #include "hcsr04.h" +#include "stm32f1xx_hal_tim.h" #include "tim.h" -// time 的单位是 μs -uint64_t time; // 声明变量,用来计时 -uint64_t time_end; // 声明变量,存储回波信号时间 -uint8_t state = 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); -int16_t sonar_mm(void) // 测距并返回单位为毫米的距离结果 -{ - uint32_t distance, distance_mm = 0; - 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(); - while (state); - state = 1; + // 注册定时器中断溢出回调 + HAL_TIM_RegisterCallback(&htim2, HAL_TIM_PERIOD_ELAPSED_CB_ID, HCSR04_HAL_TIM_PeriodElapsedCallback); +} - // BUG 现在 time 的单位是 1μs - if (time_end / 100 < 38) - { // 判断是否小于38毫秒,大于38毫秒的就是超时,直接调到下面返回0 - distance = (time_end * 346) / 2; // 计算距离,25°C空气中的音速为346m/s - distance_mm = distance / 100; // 因为上面的time_end的单位是10微秒,所以要得出单位为毫米的距离结果,还得除以100 +// 启动测距,单位: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 distance_mm; // 返回测距结果 + return sonor_distance; } -float sonar(void) // 测距并返回单位为米的距离结果 -{ - uint32_t distance, distance_mm = 0; - float distance_m = 0; - 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); // 输出低电平 - - while (state); - state = 1; - - // BUG 现在 time 的单位是 1μs - if (time_end / 100 < 38) - { - distance = (time_end * 346) / 2; - distance_mm = distance / 100; - distance_m = distance_mm / 1000; +void HCSR04_HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim) { + if (htim == &htim2) { // 每10us自增一 + time++; } - return distance_m; } -void delay_us(uint16_t us) -{ - HAL_TIM_Base_Start_IT(&htim2); - time = 0; - while (time < us); - HAL_TIM_Base_Stop_IT(&htim2); -} - -void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) -{ - if (htim == &htim2) - ++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)) - { +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); time = 0; - } - else - { + } else { time_end = time; HAL_TIM_Base_Stop_IT(&htim2); - state = 0; + 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 4c0a127..eb464b1 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,29 +97,22 @@ 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(); /* USER CODE END 2 */ /* 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 086d351..404be4b 100644 --- a/Core/Src/stm32f1xx_it.c +++ b/Core/Src/stm32f1xx_it.c @@ -56,6 +56,7 @@ /* External variables --------------------------------------------------------*/ extern TIM_HandleTypeDef htim2; +extern UART_HandleTypeDef huart2; /* USER CODE BEGIN EV */ /* USER CODE END EV */ @@ -212,6 +213,20 @@ void TIM2_IRQHandler(void) /* USER CODE END TIM2_IRQn 1 */ } +/** + * @brief This function handles USART2 global interrupt. + */ +void USART2_IRQHandler(void) +{ + /* USER CODE BEGIN USART2_IRQn 0 */ + + /* USER CODE END USART2_IRQn 0 */ + HAL_UART_IRQHandler(&huart2); + /* USER CODE BEGIN USART2_IRQn 1 */ + + /* USER CODE END USART2_IRQn 1 */ +} + /** * @brief This function handles EXTI line[15:10] interrupts. */ @@ -220,7 +235,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 f66b0a8..b223538 100644 --- a/Core/Src/tim.c +++ b/Core/Src/tim.c @@ -45,9 +45,9 @@ void MX_TIM2_Init(void) htim2.Instance = TIM2; htim2.Init.Prescaler = 72 - 1; htim2.Init.CounterMode = TIM_COUNTERMODE_UP; - htim2.Init.Period = 1 - 1; + htim2.Init.Period = 10 - 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 94dd404..47b4c93 100644 --- a/Core/Src/usart.c +++ b/Core/Src/usart.c @@ -24,35 +24,35 @@ #include /* USER CODE END 0 */ -UART_HandleTypeDef huart1; +UART_HandleTypeDef huart2; -/* 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 */ } @@ -60,71 +60,63 @@ 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); - /* USER CODE BEGIN USART1_MspInit 1 */ + /* USART2 interrupt Init */ + HAL_NVIC_SetPriority(USART2_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(USART2_IRQn); + /* USER CODE BEGIN USART2_MspInit 1 */ - /* USER CODE END USART1_MspInit 1 */ + /* USER CODE END USART2_MspInit 1 */ } } 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); - /* USER CODE BEGIN USART1_MspDeInit 1 */ + /* USART2 interrupt Deinit */ + HAL_NVIC_DisableIRQ(USART2_IRQn); + /* USER CODE BEGIN USART2_MspDeInit 1 */ - /* USER CODE END USART1_MspDeInit 1 */ + /* USER CODE END USART2_MspDeInit 1 */ } } /* 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 aadec10..1ae0f85 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 @@ -104,16 +104,16 @@ 0 0 3 - - - - - - - - - - + + + + + + + + + + BIN\CMSIS_AGDI.dll @@ -130,7 +130,7 @@ 0 ARMDBGFLAGS - + 0 @@ -140,7 +140,7 @@ 0 DLGUARM - + 0 @@ -167,8 +167,8 @@ 0 0 ../Core/Src/main.c - - + + 1 @@ -183,8 +183,8 @@ 0 0 ..\Core\Src\motor.c - - + + @@ -216,19 +216,19 @@ 0 0 - - + + 0 0 0 - - - - - - - - + + + + + + + + 1 0 @@ -577,7 +577,7 @@ Drivers/CMSIS - 0 + 1 0 0 0 diff --git a/MDK-ARM/CAR.uvprojx b/MDK-ARM/CAR.uvprojx index b173827..97796f4 100644 --- a/MDK-ARM/CAR.uvprojx +++ b/MDK-ARM/CAR.uvprojx @@ -1,7 +1,10 @@ - - + + + 2.1 +
### uVision Project, (C) Keil Software
+ CAR @@ -16,28 +19,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 @@ -52,15 +55,15 @@ 1 1 1 - + 1 0 0 0 0 - - + + 0 0 0 @@ -69,8 +72,8 @@ 0 0 - - + + 0 0 0 @@ -79,15 +82,15 @@ 0 1 - - + + 0 0 0 0 1 - + 0 @@ -101,8 +104,8 @@ 0 0 3 - - + + 0 @@ -111,7 +114,7 @@ DCM.DLL -pCM3 SARMCM3.DLL - + TCM.DLL -pCM3 @@ -136,10 +139,10 @@ 1 BIN\UL2V8M.DLL "" () - - - - + + + + 0 @@ -172,7 +175,7 @@ 0 0 "Cortex-M3" - + 0 0 0 @@ -306,7 +309,7 @@ 0x0 - + 1 @@ -333,9 +336,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 @@ -351,10 +354,10 @@ 0 4 - - - - + + + + @@ -364,15 +367,15 @@ 0 1 0 - - - - - - - - - + + + + + + + + + @@ -538,18 +541,20 @@ + - + - + - + - + + @@ -558,5 +563,5 @@ -
+
diff --git a/MDK-ARM/startup_stm32f103xb.lst b/MDK-ARM/startup_stm32f103xb.lst index 8841181..7d1318c 100644 --- a/MDK-ARM/startup_stm32f103xb.lst +++ b/MDK-ARM/startup_stm32f103xb.lst @@ -461,11 +461,11 @@ ARM Macro Assembler Page 8 Command Line: --debug --xref --diag_suppress=9931 --cpu=Cortex-M3 --apcs=interw ork --depend=car\startup_stm32f103xb.d -ocar\startup_stm32f103xb.o -I.\RTE\_CAR - -IE:\Users\lenovo\AppData\Local\Arm\Packs\ARM\CMSIS\5.8.0\CMSIS\Core\Include - -IE:\Users\lenovo\AppData\Local\Arm\Packs\Keil\STM32F1xx_DFP\2.2.0\Device\Includ -e --predefine="__MICROLIB SETA 1" --predefine="__UVISION_VERSION SETA 536" --pr -edefine="_RTE_ SETA 1" --predefine="STM32F10X_MD SETA 1" --predefine="_RTE_ SET -A 1" --list=startup_stm32f103xb.lst startup_stm32f103xb.s + -IE:\Users\Administrator\AppData\Local\Arm\Packs\ARM\CMSIS\5.8.0\CMSIS\Core\In +clude -IE:\Users\Administrator\AppData\Local\Arm\Packs\Keil\STM32F1xx_DFP\2.2.0 +\Device\Include --predefine="__MICROLIB SETA 1" --predefine="__UVISION_VERSION +SETA 536" --predefine="_RTE_ SETA 1" --predefine="STM32F10X_MD SETA 1" --predef +ine="_RTE_ SETA 1" --list=startup_stm32f103xb.lst startup_stm32f103xb.s 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