diff --git a/CMakeLists.txt b/CMakeLists.txt index dc5036d..77d3200 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,7 +29,7 @@ set(TARGET_SRC Src/led.c Src/uart_ring_buffer.c Src/command.c - Src/i2c.c + Src/board_config.c Src/ultrasonic_analog.c ) diff --git a/Inc/board_config.h b/Inc/board_config.h index 47b3c6e..4089583 100644 --- a/Inc/board_config.h +++ b/Inc/board_config.h @@ -1,6 +1,10 @@ #ifndef BOARD_CONFIG_H #define BOARD_CONFIG_H +#define GD32E23XF4 0x10 +#define GD32E23XF6 0x20 +#define GD32E23XF8 0x40 + /* >>>>>>>>>>>>>>>>>>>>[RS485 PHY DEFINE]<<<<<<<<<<<<<<<<<<<< */ // #define RS485_MAX13487 // RS485 PHY : MAX13487 (AutoDir) @@ -18,6 +22,23 @@ /******************************************************************************/ +/* Dynamic USART Configuration Structure */ +typedef struct { + uint32_t rcu_usart; + uint32_t usart_periph; + IRQn_Type irq_type; + void (*irq_handler)(void); // 函数指针:指向中断处理函数 +} usart_config_t; + +extern usart_config_t g_usart_config; +extern uint8_t g_mcu_flash_size; + +/* USART中断处理函数声明 */ +void usart0_irq_handler(void); +void usart1_irq_handler(void); + +/******************************************************************************/ + #define RCU_GPIO_I2C RCU_GPIOF #define RCU_I2C RCU_I2C0 #define I2C_SCL_PORT GPIOF @@ -36,15 +57,15 @@ /******************************************************************************/ -#define RS485_RCU RCU_USART0 +#define RS485_RCU (g_usart_config.rcu_usart) +#define RS485_PHY (g_usart_config.usart_periph) +#define RS485_IRQ (g_usart_config.irq_type) #define RS485_GPIO_RCU RCU_GPIOA #define RS485_GPIO_PORT GPIOA +#define RS485_EN_PIN GPIO_PIN_1 #define RS485_TX_PIN GPIO_PIN_2 #define RS485_RX_PIN GPIO_PIN_3 -#define RS485_PHY USART0 #define RS485_BAUDRATE 115200U -#define RS485_EN_PIN GPIO_PIN_1 -#define RS485_IRQ USART0_IRQn /******************************************************************************/ @@ -58,11 +79,6 @@ /******************************************************************************/ -#define US_TX_DELAY_RCU RCU_TIMER15 -#define US_TX_DELAY_TIMER TIMER15 - -/******************************************************************************/ - #define US_RX_GPIO_RCU RCU_GPIOB #define US_RX_EXTI_RCU RCU_CFGCMP #define US_RX_GPIO_PORT GPIOA @@ -76,12 +92,11 @@ #define US_ECHO_RCU RCU_TIMER16 #define US_ECHO_TIMER TIMER16 #define US_ECHO_CH TIMER_CH_0 +#define US_ECHO_TIMER_IRQ TIMER16_IRQn /******************************************************************************/ -#define TIME_CORRECTION_US 230U -#define CAPTURE_VALUE_MAX 550U - -/******************************************************************************/ +void mcu_detect_and_config(void); +uint8_t get_flash_size(void); #endif //BOARD_CONFIG_H diff --git a/Inc/command.h b/Inc/command.h index d5b56f1..514347d 100644 --- a/Inc/command.h +++ b/Inc/command.h @@ -81,6 +81,8 @@ void command_process(void); */ void handle_command(const uint8_t *cmd, uint8_t len); +void ultrasonic_distance_raw_value_report(void); + /** @} */ // end of Command group #endif // COMMAND_H diff --git a/Inc/ultrasonic_analog.h b/Inc/ultrasonic_analog.h index ed6af7e..5aa0140 100644 --- a/Inc/ultrasonic_analog.h +++ b/Inc/ultrasonic_analog.h @@ -9,23 +9,13 @@ extern volatile bool g_ultrasonic_measure_done; /**************************************************************************************************/ #define ULTRASONIC_TX_CYCLES 5U /* 发送5个PWM周期驱动换能器 */ -#define ULTRASONIC_TX_TIME 498U // ms -#define ULTRASONIC_MAX_TOF_RELOAD 1000U //us +#define ULTRASONIC_TX_RINGDOWN_RELOAD 240U // 240us +#define ULTRASONIC_MAX_TOF_RELOAD 1000U // 1000us /**************************************************************************************************/ -void ultrasonic_tx_init(void); - void ultrasonic_pwm_out_cycles(void); -// void ultrasonic_transmit_delay(const uint16_t micro_second); - -// void ultrasonic_rece_exti_config(void); - -void ultrasonic_echo_timer_config(void); - void ultrasonic_config(void); -// uint16_t ultrasonic_calc_distance(void); - -#endif // UART_H +#endif // ULTRASONIC_ANALOG_H diff --git a/Src/board_config.c b/Src/board_config.c new file mode 100644 index 0000000..a68f73e --- /dev/null +++ b/Src/board_config.c @@ -0,0 +1,52 @@ +#include "gd32e23x.h" +#include "board_config.h" +#include "systick.h" + +/******************************************************************************/ + +#define FLASH_SIZE_ADDR (*(const uint8_t *)0x1FFFF7E0) // Flash base address + +/******************************************************************************/ + +/* 前向声明中断处理函数 */ +void usart0_irq_handler(void); +void usart1_irq_handler(void); + +usart_config_t g_usart_config = { + .rcu_usart = RCU_USART1, + .usart_periph = USART1, + .irq_type = USART1_IRQn, + .irq_handler = usart1_irq_handler // 初始化函数指针 +}; + +uint8_t g_mcu_flash_size = 0; + +void mcu_detect_and_config(void) { + g_mcu_flash_size = FLASH_SIZE_ADDR; + + switch (g_mcu_flash_size) { + case GD32E23XF4: + g_usart_config.rcu_usart = RCU_USART0; + g_usart_config.usart_periph = USART0; + g_usart_config.irq_type = USART0_IRQn; + g_usart_config.irq_handler = usart0_irq_handler; // 指向USART0处理函数 + break; + case GD32E23XF6: + g_usart_config.rcu_usart = RCU_USART1; + g_usart_config.usart_periph = USART1; + g_usart_config.irq_type = USART1_IRQn; + g_usart_config.irq_handler = usart1_irq_handler; // 指向USART1处理函数 + break; + default: // Default to GD32E23XF8 + g_usart_config.rcu_usart = RCU_USART1; + g_usart_config.usart_periph = USART1; + g_usart_config.irq_type = USART1_IRQn; + g_usart_config.irq_handler = usart1_irq_handler; // 指向USART1处理函数 + break; + } + +} + +uint8_t get_flash_size(void) { + return g_mcu_flash_size; +} diff --git a/Src/command.c b/Src/command.c index f7a3980..18b9721 100644 --- a/Src/command.c +++ b/Src/command.c @@ -18,6 +18,7 @@ #include "board_config.h" #include "gd32e23x_usart.h" #include "ultrasonic_analog.h" +#include "systick.h" /* ============================================================================ * 协议格式说明 @@ -29,7 +30,7 @@ * @details * Host -> Device 命令帧格式: * [0] HEADER = 0xD5 // 包头标识 - * [1] BOARD_TYPE = 0x03 // 板卡类型标识 + * [1] BOARD_TYPE = 0x04 // 板卡类型标识 * [2] LEN = 数据区字节数 // 有效载荷长度 * [3..(3+LEN-1)] 数据 // 命令数据,如 "M1", "M2S123" * [last] CRC = 校验码 // 从索引1到(last-1)的累加和低8位 @@ -154,13 +155,6 @@ static void send_response(uint8_t type, const uint8_t *payload, uint8_t len) // 等待发送完成 while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {} - // // 使用printf发送(通过重定向到串口) - // for (uint8_t i = 0; i < buf_len; i++) { - // printf("%c", buf[i]); - // } - - // // 刷新缓冲区 - // fflush(stdout); } /** @@ -253,20 +247,25 @@ void handle_command(const uint8_t *frame, uint8_t len) { if (cmd_index == cmd_len) { // 仅基础命令,如 M1, M2, M3 switch (base_cmd) { - case 1u: // M1: enable sensor report - // g_ultrasonic_measure_done = false; + case 1u: // M1: send ultrasonic driver single and respon raw data + // 启动超声波PWM发送 ultrasonic_pwm_out_cycles(); - gpio_bit_toggle(GPIOA, GPIO_PIN_0); - - while (g_ultrasonic_measure_done) - { - g_ultrasonic_measure_done = false; - send_response(RESP_TYPE_OK, s_report_status_err, sizeof(s_report_status_err)); - return; + uint16_t timeout_count = 0; + const uint16_t max_timeout = 150; + + while (!g_ultrasonic_measure_done && timeout_count < max_timeout) { + delay_10us(1); // 延时 10us + timeout_count++; } - - - + + // 根据等待结果发送不同的响应 + if (g_ultrasonic_measure_done) { + ultrasonic_distance_raw_value_report(); + } else { + static const uint8_t timeout_error_data[] = {0xFF, 0xFF }; + send_response(RESP_TYPE_OK, timeout_error_data, sizeof(timeout_error_data)); + } + return; case 2u: // M2: disable sensor report send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok)); @@ -310,7 +309,6 @@ void handle_command(const uint8_t *frame, uint8_t len) { { // case 100u: // // set_pwm(param_value); - // printf("Set PWM to %u\n", param_value); // return; default: @@ -418,3 +416,32 @@ void command_process(void) { } } } + +/** + * @brief 超声波测距原始值上报函数 + * @details 读取超声波测距的定时器计数值,计算距离并按协议格式发送响应包 + * 距离计算公式:距离(0.1mm) = 定时器计数值(us) * 17 + * 响应数据格式:4字节32位无符号整数,单位为0.1mm + * @note 定时器配置为1us计数,最大计时2000us,对应距离340mm + * @ingroup Command + */ +void ultrasonic_distance_raw_value_report(void) { + uint8_t raw_result[2]; + uint16_t raw_distance = 0; + + // 读取定时器计数值(微秒) + uint16_t timer_count_us = timer_counter_read(US_ECHO_TIMER); + + // 计算距离:定时器计数值 * 17 = 距离(0.1mm) + // 声速340m/s,往返距离,时间单位us + // 距离(mm) = (timer_count_us * 340) / (2 * 1000) = timer_count_us * 0.17 + // 距离(0.1mm) = timer_count_us * 1.7 ≈ timer_count_us * 17 / 10 + raw_distance = (uint16_t)timer_count_us * 17; + + // 将16位距离值分解为2个字节(大端序) + raw_result[0] = (uint8_t)(raw_distance >> 8); + raw_result[1] = (uint8_t)(raw_distance & 0xFF); + + // 发送响应包 + send_response(RESP_TYPE_OK, raw_result, sizeof(raw_result)); +} diff --git a/Src/gd32e23x_it.c b/Src/gd32e23x_it.c index 1712db6..dbe2d44 100644 --- a/Src/gd32e23x_it.c +++ b/Src/gd32e23x_it.c @@ -38,8 +38,7 @@ OF SUCH DAMAGE. #include "uart_ring_buffer.h" #include "led.h" #include "ultrasonic_analog.h" - -extern uint16_t g_capture_value; +#include "board_config.h" /*! \brief this function handles NMI exception @@ -104,39 +103,21 @@ void SysTick_Handler(void) { delay_decrement(); } -// /** -// * @brief This function handles TIMER15 interrupt request. -// * @param[in] none -// * @param[out] none -// * @retval None -// */ -// void TIMER15_IRQHandler(void) { -// if (timer_interrupt_flag_get(US_TX_DELAY_TIMER, TIMER_INT_FLAG_UP) == SET) -// { -// timer_interrupt_flag_clear(US_TX_DELAY_TIMER, TIMER_INT_FLAG_UP); -// exti_interrupt_enable(US_RX_GPIO_EXTI); // turn on hardware external input interrupt -// timer_counter_value_config(US_ECHO_TIMER, 0); -// timer_enable(US_ECHO_TIMER); // turn on timer to calculate the first ultrasonic echo time -// timer_disable(US_TX_DELAY_TIMER); -// } -// } - -void TIMER15_IRQHandler(void) { - if (timer_interrupt_flag_get(TIMER15, TIMER_INT_FLAG_CH1)) { - timer_interrupt_flag_clear(TIMER15, TIMER_INT_FLAG_CH1); - // g_ultrasonic_measure_done = true; // TODO 测距命令发送回报标识位,最终不应在这里 - gpio_bit_set(GPIOA, GPIO_PIN_0); // TODO waiting for delete - } - - if (timer_interrupt_flag_get(TIMER15, TIMER_INT_FLAG_UP)) { - timer_interrupt_flag_clear(TIMER15, TIMER_INT_FLAG_UP); - timer_disable(TIMER15); +void TIMER16_IRQHandler(void) +{ + // CH0比较中断 (ULTRASONIC_TX_RINGDOWN_RELOAD) + if(timer_interrupt_flag_get(TIMER16, TIMER_INT_FLAG_CH0) != RESET) { + timer_interrupt_flag_clear(TIMER16, TIMER_INT_FLAG_CH0); + exti_interrupt_enable(US_RX_GPIO_EXTI); } + // UP更新中断 (ULTRASONIC_MAX_TOF_RELOAD) + if(timer_interrupt_flag_get(TIMER16, TIMER_INT_FLAG_UP) != RESET) { + timer_interrupt_flag_clear(TIMER16, TIMER_INT_FLAG_UP); + timer_disable(US_ECHO_TIMER); + } } - - /** * @brief This function handles external lines 0 to 1 interrupt request * @param[in] none @@ -147,14 +128,12 @@ void EXTI0_1_IRQHandler(void) { if (exti_interrupt_flag_get(US_RX_GPIO_EXTI) == SET) { exti_interrupt_flag_clear(US_RX_GPIO_EXTI); - g_capture_value = timer_channel_capture_value_register_read(US_ECHO_TIMER, US_ECHO_CH); - timer_disable(US_ECHO_TIMER); + g_ultrasonic_measure_done = true; // 超声转换完成,置位flag后有命令处理部分回包 exti_interrupt_disable(US_RX_GPIO_EXTI); } } - /* PWM周期计数器 - 使用ULTRASONIC_TX_CYCLES宏 */ volatile uint8_t pwm_cycle_count = 0; @@ -167,15 +146,21 @@ void TIMER13_IRQHandler(void) if(pwm_cycle_count >= (ULTRASONIC_TX_CYCLES)) { timer_disable(TIMER13); pwm_cycle_count = 0; - g_ultrasonic_measure_done = true; // TODO 测距命令发送回报标识位,最终不应在这里 } } } void USART0_IRQHandler(void) { - if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) { - uint8_t data = usart_data_receive(USART0); - (void)uart_ring_buffer_put(data); // 缓冲满时丢弃,返回值可用于统计 + // 检查当前配置是否使用USART0,并且函数指针不为空 + if(g_usart_config.usart_periph == USART0 && g_usart_config.irq_handler != 0) { + g_usart_config.irq_handler(); // 通过函数指针调用对应的处理函数 + } +} + +void USART1_IRQHandler(void) { + // 检查当前配置是否使用USART1,并且函数指针不为空 + if(g_usart_config.usart_periph == USART1 && g_usart_config.irq_handler != 0) { + g_usart_config.irq_handler(); // 通过函数指针调用对应的处理函数 } } diff --git a/Src/main.c b/Src/main.c index fbc34bd..9785e69 100644 --- a/Src/main.c +++ b/Src/main.c @@ -38,11 +38,10 @@ OF SUCH DAMAGE. #include "led.h" #include "command.h" #include -#include "i2c.h" #include "board_config.h" #include "ultrasonic_analog.h" -volatile uint16_t g_capture_value; +// #define FLASH_SIZE_ADDR (*(const uint16_t *)0x1FFFF7E0) /*! \brief main function @@ -52,14 +51,19 @@ volatile uint16_t g_capture_value; */ int main(void) { + led_init(); + mcu_detect_and_config(); + setbuf(stdout, NULL); systick_config(); rs485_init(); - led_init(); ultrasonic_config(); + ultrasonic_pwm_out_cycles(); + + printf("Flash size: %d Kbytes\n", get_flash_size()); #ifdef DEBUG_VERBOSE char hello_world[] = {"Hello World!\r\n"}; @@ -75,14 +79,7 @@ int main(void) while(1){ - - // ultrasonic_pwm_out_cycles(); - - // delay_ms(10); command_process(); delay_ms(10); - // uint16_t ultrasonic_value = ultrasonic_calc_distance(); - // printf("ultrasonic value: %d", ultrasonic_value); - // delay_ms(10); } } diff --git a/Src/syscalls.c b/Src/syscalls.c index 46beccf..c1a1d8c 100644 --- a/Src/syscalls.c +++ b/Src/syscalls.c @@ -16,6 +16,7 @@ #include #include #include "gd32e23x_usart.h" +#include "board_config.h" #undef errno extern int errno; @@ -164,7 +165,7 @@ int _execve(char *name, char **argv, char **env) // USART0 printf重定向实现 int __io_putchar(int ch) { // 等待发送缓冲区空 - while (usart_flag_get(USART0, USART_FLAG_TBE) == RESET) {} - usart_data_transmit(USART0, (uint8_t)ch); + while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {} + usart_data_transmit(RS485_PHY, (uint8_t)ch); return ch; } diff --git a/Src/uart.c b/Src/uart.c index 6d699c9..013e5ea 100644 --- a/Src/uart.c +++ b/Src/uart.c @@ -3,7 +3,7 @@ #include "gd32e23x_rcu.h" #include "gd32e23x_gpio.h" #include "board_config.h" - +#include "uart_ring_buffer.h" void rs485_init(void) { @@ -37,7 +37,7 @@ void rs485_init(void) { usart_enable(RS485_PHY); - nvic_irq_enable(USART0_IRQn, 0); + nvic_irq_enable(RS485_IRQ, 0); usart_interrupt_enable(RS485_PHY, USART_INT_RBNE); // usart_interrupt_enable(RS485_PHY, USART_INT_IDLE); @@ -70,3 +70,37 @@ void rs485_init(void) { #endif // RS485_MAX13487 } + +/******************************************************************************/ +/* 具体的中断处理函数实现 */ +/******************************************************************************/ + +void usart0_irq_handler(void) { + // 处理USART0的接收中断 + if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) { + uint8_t data = usart_data_receive(USART0); + // 使用原有的环形缓冲区处理逻辑 + (void)uart_ring_buffer_put(data); // 缓冲满时丢弃,返回值可用于统计 + } + + // 处理USART0的空闲中断 + if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE)) { + usart_interrupt_flag_clear(USART0, USART_INT_FLAG_IDLE); + // 在这里添加空闲中断处理逻辑 + } +} + +void usart1_irq_handler(void) { + // 处理USART1的接收中断 + if(usart_interrupt_flag_get(USART1, USART_INT_FLAG_RBNE)) { + uint8_t data = usart_data_receive(USART1); + // 使用原有的环形缓冲区处理逻辑 + (void)uart_ring_buffer_put(data); // 缓冲满时丢弃,返回值可用于统计 + } + + // 处理USART1的空闲中断 + if(usart_interrupt_flag_get(USART1, USART_INT_FLAG_IDLE)) { + usart_interrupt_flag_clear(USART1, USART_INT_FLAG_IDLE); + // 在这里添加空闲中断处理逻辑 + } +} diff --git a/Src/ultrasonic_analog.c b/Src/ultrasonic_analog.c index 957346e..1cb0fd7 100644 --- a/Src/ultrasonic_analog.c +++ b/Src/ultrasonic_analog.c @@ -6,17 +6,9 @@ #include "systick.h" volatile bool g_ultrasonic_measure_done = false; -extern uint32_t g_capture_value; void ultrasonic_tx_init(void) { - // TODO - rcu_periph_clock_enable(RCU_GPIOA); - - gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_0); - gpio_output_options_set(GPIOA, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_0); - gpio_bit_reset(GPIOA, GPIO_PIN_0); - rcu_periph_clock_enable(US_TX_GPIO_RCU); gpio_mode_set(US_TX_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, US_TX_GPIO_PIN); @@ -60,42 +52,34 @@ void ultrasonic_tx_init(void) { timer_counter_value_config(US_TX_TIMER, 0); timer_interrupt_enable(US_TX_TIMER, TIMER_INT_UP); - nvic_irq_enable(TIMER13_IRQn, 2); + nvic_irq_enable(TIMER13_IRQn, 2U); } void ultrasonic_pwm_out_cycles(void) { - g_ultrasonic_measure_done = false; + // 停止发射定时器 timer_disable(TIMER13); - timer_channel_output_state_config(TIMER13, TIMER_CH_0, TIMER_CCX_ENABLE); + // 重置并启动回波计时器 + timer_disable(US_ECHO_TIMER); + + // 重要:清除所有中断标志 + // timer_interrupt_flag_clear(US_ECHO_TIMER, TIMER_INT_FLAG_CH0); + // timer_interrupt_flag_clear(US_ECHO_TIMER, TIMER_INT_FLAG_UP); + + // 重置计数器 + timer_counter_value_config(US_ECHO_TIMER, 0); + + // 启动发射PWM timer_enable(US_TX_TIMER); - timer_enable(TIMER15); + // 启动回波计时器 + timer_enable(US_ECHO_TIMER); } -// void ultrasonic_transmit_debounce_delay(const uint16_t micro_second) { -// rcu_periph_clock_enable(US_TX_DELAY_RCU); -// timer_deinit(US_TX_DELAY_TIMER); - -// timer_parameter_struct timer_initpara; -// timer_struct_para_init(&timer_initpara); -// timer_initpara.prescaler = 71; -// timer_initpara.alignedmode = TIMER_COUNTER_EDGE; -// timer_initpara.counterdirection = TIMER_COUNTER_UP; -// timer_initpara.period = micro_second - 1; -// timer_initpara.clockdivision = TIMER_CKDIV_DIV1; -// timer_initpara.repetitioncounter = 0; -// timer_init(US_TX_DELAY_TIMER, &timer_initpara); - -// timer_auto_reload_shadow_enable(US_TX_DELAY_TIMER); -// timer_interrupt_enable(US_TX_DELAY_TIMER, TIMER_INT_UP); -// nvic_irq_enable(TIMER15_IRQn, 1U); -// } - void ultrasonic_receive_exti_config(void) { rcu_periph_clock_enable(US_RX_GPIO_RCU); rcu_periph_clock_enable(US_RX_EXTI_RCU); @@ -106,8 +90,6 @@ void ultrasonic_receive_exti_config(void) { exti_init(US_RX_GPIO_EXTI, EXTI_INTERRUPT, EXTI_TRIG_FALLING); exti_flag_clear(US_RX_GPIO_EXTI); - - // exti_interrupt_enable(EXTI_0); } void ultrasonic_echo_timer_config(void) { @@ -116,61 +98,43 @@ void ultrasonic_echo_timer_config(void) { timer_parameter_struct timer_initpara; timer_struct_para_init(&timer_initpara); - timer_initpara.prescaler = 71; + timer_initpara.prescaler = 71; // 72MHz/72 = 1MHz (1us per count) timer_initpara.alignedmode = TIMER_COUNTER_EDGE; timer_initpara.counterdirection = TIMER_COUNTER_UP; - timer_initpara.period = ULTRASONIC_MAX_TOF_RELOAD - 1; + timer_initpara.period = (ULTRASONIC_MAX_TOF_RELOAD - 1); timer_initpara.clockdivision = TIMER_CKDIV_DIV1; - timer_init(TIMER15, &timer_initpara); + timer_init(US_ECHO_TIMER, &timer_initpara); + timer_auto_reload_shadow_disable(US_ECHO_TIMER); + + // 配置输出比较通道0 timer_oc_parameter_struct timer_oc_initpara; timer_channel_output_struct_para_init(&timer_oc_initpara); timer_oc_initpara.outputstate = TIMER_CCX_ENABLE; timer_oc_initpara.ocpolarity = TIMER_OC_POLARITY_HIGH; - timer_channel_output_config(TIMER15, TIMER_CH_1,&timer_oc_initpara); + timer_channel_output_config(US_ECHO_TIMER, TIMER_CH_0, &timer_oc_initpara); - timer_interrupt_enable(TIMER15, TIMER_INT_CH1 | TIMER_INT_UP); - nvic_irq_enable(TIMER15_IRQn, 0); + // 配置输出比较模式 + timer_channel_output_mode_config(US_ECHO_TIMER, TIMER_CH_0, TIMER_OC_MODE_TIMING); + + // 设置比较值 + timer_channel_output_pulse_value_config(US_ECHO_TIMER, TIMER_CH_0, (ULTRASONIC_TX_RINGDOWN_RELOAD - 1)); + // 清除中断标志 + timer_interrupt_flag_clear(US_ECHO_TIMER, TIMER_INT_FLAG_CH0); + timer_interrupt_flag_clear(US_ECHO_TIMER, TIMER_INT_FLAG_UP); - // timer_parameter_struct timer_initpara; - // timer_struct_para_init(&timer_initpara); - // timer_initpara.prescaler = 71; - // timer_initpara.alignedmode = TIMER_COUNTER_EDGE; - // timer_initpara.counterdirection = TIMER_COUNTER_UP; - // timer_initpara.period = ULTRASONIC_MAX_TOF_RELOAD; - // timer_initpara.clockdivision = TIMER_CKDIV_DIV1; - // timer_initpara.repetitioncounter = 0; - // timer_init(US_ECHO_TIMER, &timer_initpara); - - // timer_ic_parameter_struct timer_icinitpara; - // timer_channel_input_struct_para_init(&timer_icinitpara); - // timer_icinitpara.icpolarity = TIMER_IC_POLARITY_BOTH_EDGE; - // timer_icinitpara.icselection = TIMER_IC_SELECTION_INDIRECTTI; - // timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1; - // timer_icinitpara.icfilter = 0x03; - // timer_input_capture_config(US_ECHO_TIMER, US_ECHO_CH, &timer_icinitpara); - + // 使能中断 + timer_interrupt_enable(US_ECHO_TIMER, TIMER_INT_UP); // UP中断 + timer_interrupt_enable(US_ECHO_TIMER, TIMER_INT_CH0); // CH0中断 + + nvic_irq_enable(US_ECHO_TIMER_IRQ, 1U); + // timer_single_pulse_mode_config(US_ECHO_TIMER, TIMER_SP_MODE_SINGLE); } void ultrasonic_config(void) { ultrasonic_tx_init(); - // ultrasonic_transmit_debounce_delay(TIME_CORRECTION_US); - // ultrasonic_receive_exti_config(); + ultrasonic_receive_exti_config(); ultrasonic_echo_timer_config(); } - -// uint16_t ultrasonic_calc_distance(void) { -// // while (!ultrasonicMeasurementDone); -// // uint32_t us_value = timer_channel_capture_value_register_read(US_ECHO_TIMER, US_ECHO_CH); -// uint16_t distance = (TIME_CORRECTION_US + g_capture_value) * 17; -// /* -// * (TIME_CORRECTION_US + us_value) * 340 m/s -// * ----------------------------------------- -// * 1000 000 -// * ---------------------------------------------- -// * 2 -// */ -// return distance; -// } diff --git a/cmake/project_config.cmake b/cmake/project_config.cmake index d7127e0..e2ca5e9 100644 --- a/cmake/project_config.cmake +++ b/cmake/project_config.cmake @@ -1,5 +1,5 @@ # Project basic info -set(PROJECT_NAME "gd32e23x") +set(PROJECT_NAME "ultrasonic-analog") set(VERSION_MAJOR 1) set(VERSION_MINOR 0) set(VERSION_PATCH 0) @@ -8,7 +8,7 @@ string(TIMESTAMP BUILD_DATE "%Y-%m-%d") # 编译条件(如IIC类型等) # set(IIC_TYPE "AutoDetectDriveCurrent") -set(IIC_TYPE "HW-IIC") +set(IIC_TYPE "24V") # 其它自定义宏 add_definitions(-DIIC_TYPE=${IIC_TYPE})