From 20fbb3006702665258afd997db45b58ec3fcae42 Mon Sep 17 00:00:00 2001 From: cbb <2625274797@qq.com> Date: Thu, 2 Oct 2025 02:52:07 +0800 Subject: [PATCH] =?UTF-8?q?ADC=E5=B7=B2=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Robot_V2.csolution.yml | 9 + Robot_V2/RTE/Device/GD32C103CB/gd32c10x_can.c | 5 +- .../RTE/Device/GD32C103CB/gd32c10x_eval.c | 2 +- Robot_V2/RTE/Device/GD32C103CB/gd32c10x_it.c | 139 ++++++++++ .../RTE/Device/GD32C103CB/startup_gd32c10x.s | 6 +- .../RTE/Device/GD32C103CB/system_gd32c10x.c | 10 +- .../RTE/_Debug_GD32C103CB/RTE_Components.h | 18 -- Robot_V2/Robot_V2.cproject.yml | 14 +- Robot_V2/device/ADC.cpp | 10 +- Robot_V2/device/Can.cpp | 243 +++++++++++------- Robot_V2/device/Can.h | 31 ++- Robot_V2/device/Timer.cpp | 87 ++++--- Robot_V2/device/Timer.h | 2 +- Robot_V2/device/adc.h | 1 - Robot_V2/main.cpp | 95 ++----- Robot_V2/sys/initial.cpp | 3 +- Robot_V2/sys/initial.h | 1 + Robot_V2/sys/{systick.c => systick.cpp} | 4 +- Robot_V2/sys/systick.h | 2 + 19 files changed, 424 insertions(+), 258 deletions(-) create mode 100644 Robot_V2/RTE/Device/GD32C103CB/gd32c10x_it.c rename Robot_V2/sys/{systick.c => systick.cpp} (98%) diff --git a/Robot_V2.csolution.yml b/Robot_V2.csolution.yml index df91090..955d4c3 100644 --- a/Robot_V2.csolution.yml +++ b/Robot_V2.csolution.yml @@ -27,6 +27,15 @@ solution: target-types: - type: GD32C103CB device: GD32C103CB + target-set: + - set: + images: + - project-context: Robot_V2.Debug + debugger: + name: ST-Link@pyOCD + port: 3333 + protocol: swd + clock: 10000000 # List of different build configurations. build-types: diff --git a/Robot_V2/RTE/Device/GD32C103CB/gd32c10x_can.c b/Robot_V2/RTE/Device/GD32C103CB/gd32c10x_can.c index 6b70808..39b4ac4 100644 --- a/Robot_V2/RTE/Device/GD32C103CB/gd32c10x_can.c +++ b/Robot_V2/RTE/Device/GD32C103CB/gd32c10x_can.c @@ -33,6 +33,7 @@ OF SUCH DAMAGE. */ #include "gd32c10x_can.h" +#include "stdio.h" #include #define CAN_ERROR_HANDLE(s) do{}while(1) @@ -975,10 +976,12 @@ void can_message_receive(uint32_t can_periph, uint8_t fifo_number, can_receive_m receive_message->rx_data[5] = (uint8_t)(GET_RFIFOMDATA1_DB5(CAN_RFIFOMDATA1(can_periph, fifo_number))); receive_message->rx_data[6] = (uint8_t)(GET_RFIFOMDATA1_DB6(CAN_RFIFOMDATA1(can_periph, fifo_number))); receive_message->rx_data[7] = (uint8_t)(GET_RFIFOMDATA1_DB7(CAN_RFIFOMDATA1(can_periph, fifo_number))); - } else { + + } else { CAN_ERROR_HANDLE("CAN FD function disabled \r\n"); } } else { + /* check FD function has been enabled */ /* get receive data length */ canfd_recv_cnt = (uint8_t)(GET_RFIFOMP_DLENC(CAN_RFIFOMP(can_periph, fifo_number))); diff --git a/Robot_V2/RTE/Device/GD32C103CB/gd32c10x_eval.c b/Robot_V2/RTE/Device/GD32C103CB/gd32c10x_eval.c index 079f63e..0bc0f94 100644 --- a/Robot_V2/RTE/Device/GD32C103CB/gd32c10x_eval.c +++ b/Robot_V2/RTE/Device/GD32C103CB/gd32c10x_eval.c @@ -215,7 +215,7 @@ void gd_eval_com_init(uint32_t com) /* USART configure */ usart_deinit(com); - usart_baudrate_set(com, 115200U); + usart_baudrate_set(com, 3000000U); usart_receive_config(com, USART_RECEIVE_ENABLE); usart_transmit_config(com, USART_TRANSMIT_ENABLE); usart_enable(com); diff --git a/Robot_V2/RTE/Device/GD32C103CB/gd32c10x_it.c b/Robot_V2/RTE/Device/GD32C103CB/gd32c10x_it.c new file mode 100644 index 0000000..bd96b46 --- /dev/null +++ b/Robot_V2/RTE/Device/GD32C103CB/gd32c10x_it.c @@ -0,0 +1,139 @@ +/*! + \file gd32c10x_it.c + \brief interrupt service routines + + \version 2025-09-30, V1.0.0, firmware for GD32C10x +*/ + +/* + Copyright (c) 2025, GigaDevice Semiconductor Inc. + + Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + 3. Neither the name of the copyright holder nor the names of its contributors + may be used to endorse or promote products derived from this software without + specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, +INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY +OF SUCH DAMAGE. +*/ + +#include "gd32c10x.h" +#include "systick.h" + +/*! + \brief this function handles NMI exception + \param[in] none + \param[out] none + \retval none +*/ +void NMI_Handler(void) +{ +} + +/*! + \brief this function handles HardFault exception + \param[in] none + \param[out] none + \retval none +*/ +void HardFault_Handler(void) +{ + /* if Hard Fault exception occurs, go to infinite loop */ + while (1){ + } +} + +/*! + \brief this function handles MemManage exception + \param[in] none + \param[out] none + \retval none +*/ +void MemManage_Handler(void) +{ + /* if Memory Manage exception occurs, go to infinite loop */ + while (1){ + } +} + +/*! + \brief this function handles BusFault exception + \param[in] none + \param[out] none + \retval none +*/ +void BusFault_Handler(void) +{ + /* if Bus Fault exception occurs, go to infinite loop */ + while (1){ + } +} + +/*! + \brief this function handles UsageFault exception + \param[in] none + \param[out] none + \retval none +*/ +void UsageFault_Handler(void) +{ + /* if Usage Fault exception occurs, go to infinite loop */ + while (1){ + } +} + +/*! + \brief this function handles SVC exception + \param[in] none + \param[out] none + \retval none +*/ +void SVC_Handler(void) +{ +} + +/*! + \brief this function handles DebugMon exception + \param[in] none + \param[out] none + \retval none +*/ +void DebugMon_Handler(void) +{ +} + +/*! + \brief this function handles PendSV exception + \param[in] none + \param[out] none + \retval none +*/ +void PendSV_Handler(void) +{ +} + +/*! + \brief this function handles SysTick exception + \param[in] none + \param[out] none + \retval none +*/ +void SysTick_Handler(void) +{ + delay_decrement(); +} \ No newline at end of file diff --git a/Robot_V2/RTE/Device/GD32C103CB/startup_gd32c10x.s b/Robot_V2/RTE/Device/GD32C103CB/startup_gd32c10x.s index a676bf3..0d7f9aa 100644 --- a/Robot_V2/RTE/Device/GD32C103CB/startup_gd32c10x.s +++ b/Robot_V2/RTE/Device/GD32C103CB/startup_gd32c10x.s @@ -212,7 +212,9 @@ PendSV_Handler\ SysTick_Handler\ PROC EXPORT SysTick_Handler [WEAK] - B . + IMPORT delay_decrement + LDR R0, =delay_decrement + BX R0 ENDP Default_Handler PROC @@ -245,7 +247,7 @@ Default_Handler PROC EXPORT TIMER0_UP_TIMER9_IRQHandler [WEAK] EXPORT TIMER0_TRG_CMT_TIMER10_IRQHandler [WEAK] EXPORT TIMER0_Channel_IRQHandler [WEAK] - EXPORT TIMER1_IRQHandler [WEAK] + EXPORT TIMER1_IRQHandler [WEAK] EXPORT TIMER2_IRQHandler [WEAK] EXPORT TIMER3_IRQHandler [WEAK] EXPORT I2C0_EV_IRQHandler [WEAK] diff --git a/Robot_V2/RTE/Device/GD32C103CB/system_gd32c10x.c b/Robot_V2/RTE/Device/GD32C103CB/system_gd32c10x.c index 11a3a71..225ed31 100644 --- a/Robot_V2/RTE/Device/GD32C103CB/system_gd32c10x.c +++ b/Robot_V2/RTE/Device/GD32C103CB/system_gd32c10x.c @@ -46,15 +46,15 @@ /* select a system clock by uncommenting the following line */ /* use IRC8M */ -//#define __SYSTEM_CLOCK_IRC8M (uint32_t)(__IRC8M) -//#define __SYSTEM_CLOCK_48M_PLL_IRC8M (uint32_t)(48000000) +// #define __SYSTEM_CLOCK_IRC8M (uint32_t)(__IRC8M) +// #define __SYSTEM_CLOCK_48M_PLL_IRC8M (uint32_t)(48000000) //#define __SYSTEM_CLOCK_72M_PLL_IRC8M (uint32_t)(72000000) //#define __SYSTEM_CLOCK_108M_PLL_IRC8M (uint32_t)(108000000) -//#define __SYSTEM_CLOCK_120M_PLL_IRC8M (uint32_t)(120000000) +// #define __SYSTEM_CLOCK_120M_PLL_IRC8M (uint32_t)(120000000) /* use HXTAL(CK_HXTAL = 8M) */ -//#define __SYSTEM_CLOCK_HXTAL (uint32_t)(__HXTAL) -//#define __SYSTEM_CLOCK_48M_PLL_HXTAL (uint32_t)(48000000) +// #define __SYSTEM_CLOCK_HXTAL (uint32_t)(__HXTAL) +// #define __SYSTEM_CLOCK_48M_PLL_HXTAL (uint32_t)(48000000) //#define __SYSTEM_CLOCK_72M_PLL_HXTAL (uint32_t)(72000000) //#define __SYSTEM_CLOCK_108M_PLL_HXTAL (uint32_t)(108000000) #define __SYSTEM_CLOCK_120M_PLL_HXTAL (uint32_t)(120000000) diff --git a/Robot_V2/RTE/_Debug_GD32C103CB/RTE_Components.h b/Robot_V2/RTE/_Debug_GD32C103CB/RTE_Components.h index a4c5036..6ef9920 100644 --- a/Robot_V2/RTE/_Debug_GD32C103CB/RTE_Components.h +++ b/Robot_V2/RTE/_Debug_GD32C103CB/RTE_Components.h @@ -17,48 +17,30 @@ /* GigaDevice::Device:GD32C10x_StdPeripherals:ADC@1.0.0 */ #define RTE_DEVICE_STDPERIPHERALS_ADC -/* GigaDevice::Device:GD32C10x_StdPeripherals:BKP@1.0.0 */ -#define RTE_DEVICE_STDPERIPHERALS_BKP /* GigaDevice::Device:GD32C10x_StdPeripherals:CAN@1.0.0 */ #define RTE_DEVICE_STDPERIPHERALS_CAN -/* GigaDevice::Device:GD32C10x_StdPeripherals:CRC@1.0.0 */ -#define RTE_DEVICE_STDPERIPHERALS_CRC -/* GigaDevice::Device:GD32C10x_StdPeripherals:CTC@1.0.0 */ -#define RTE_DEVICE_STDPERIPHERALS_CTC -/* GigaDevice::Device:GD32C10x_StdPeripherals:DAC@1.0.0 */ -#define RTE_DEVICE_STDPERIPHERALS_DAC /* GigaDevice::Device:GD32C10x_StdPeripherals:DBG@1.0.0 */ #define RTE_DEVICE_STDPERIPHERALS_DBG /* GigaDevice::Device:GD32C10x_StdPeripherals:DMA@1.0.0 */ #define RTE_DEVICE_STDPERIPHERALS_DMA -/* GigaDevice::Device:GD32C10x_StdPeripherals:EXMC@1.0.0 */ -#define RTE_DEVICE_STDPERIPHERALS_EXMC /* GigaDevice::Device:GD32C10x_StdPeripherals:EXTI@1.0.0 */ #define RTE_DEVICE_STDPERIPHERALS_EXTI /* GigaDevice::Device:GD32C10x_StdPeripherals:FMC@1.0.0 */ #define RTE_DEVICE_STDPERIPHERALS_FMC -/* GigaDevice::Device:GD32C10x_StdPeripherals:FWDGT@1.0.0 */ -#define RTE_DEVICE_STDPERIPHERALS_FWDGT /* GigaDevice::Device:GD32C10x_StdPeripherals:GPIO@1.0.0 */ #define RTE_DEVICE_STDPERIPHERALS_GPIO -/* GigaDevice::Device:GD32C10x_StdPeripherals:I2C@1.0.0 */ -#define RTE_DEVICE_STDPERIPHERALS_I2C /* GigaDevice::Device:GD32C10x_StdPeripherals:MISC@1.0.0 */ #define RTE_DEVICE_STDPERIPHERALS_MISC /* GigaDevice::Device:GD32C10x_StdPeripherals:PMU@1.0.0 */ #define RTE_DEVICE_STDPERIPHERALS_PMU /* GigaDevice::Device:GD32C10x_StdPeripherals:RCU@1.0.0 */ #define RTE_DEVICE_STDPERIPHERALS_RCU -/* GigaDevice::Device:GD32C10x_StdPeripherals:RTC@1.0.0 */ -#define RTE_DEVICE_STDPERIPHERALS_RTC /* GigaDevice::Device:GD32C10x_StdPeripherals:SPI_I2S@1.0.0 */ #define RTE_DEVICE_STDPERIPHERALS_SPI /* GigaDevice::Device:GD32C10x_StdPeripherals:TIMER@1.0.0 */ #define RTE_DEVICE_STDPERIPHERALS_TIMER /* GigaDevice::Device:GD32C10x_StdPeripherals:USART@1.0.0 */ #define RTE_DEVICE_STDPERIPHERALS_USART -/* GigaDevice::Device:GD32C10x_StdPeripherals:WWDGT@1.0.0 */ -#define RTE_DEVICE_STDPERIPHERALS_WWDGT #endif /* RTE_COMPONENTS_H */ diff --git a/Robot_V2/Robot_V2.cproject.yml b/Robot_V2/Robot_V2.cproject.yml index ba9d361..3d7a525 100644 --- a/Robot_V2/Robot_V2.cproject.yml +++ b/Robot_V2/Robot_V2.cproject.yml @@ -7,7 +7,7 @@ project: - file: main.cpp - group: sys files: - - file: sys/systick.c + - file: sys/systick.cpp - file: sys/systick.h - file: sys/initial.cpp - file: sys/initial.h @@ -29,30 +29,20 @@ project: - component: ARM::CMSIS:CORE - component: Device:Startup - component: Device:GD32C10x_StdPeripherals:ADC - - component: Device:GD32C10x_StdPeripherals:BKP - component: Device:GD32C10x_StdPeripherals:CAN - - component: Device:GD32C10x_StdPeripherals:CRC - - component: Device:GD32C10x_StdPeripherals:CTC - - component: Device:GD32C10x_StdPeripherals:DAC - - component: Device:GD32C10x_StdPeripherals:DBG - component: Device:GD32C10x_StdPeripherals:DMA - component: Device:GD32C10x_StdPeripherals:EXTI - component: Device:GD32C10x_StdPeripherals:FMC - - component: Device:GD32C10x_StdPeripherals:FWDGT - component: Device:GD32C10x_StdPeripherals:GPIO - - component: Device:GD32C10x_StdPeripherals:I2C - component: Device:GD32C10x_StdPeripherals:MISC - component: Device:GD32C10x_StdPeripherals:PMU - component: Device:GD32C10x_StdPeripherals:RCU - - component: Device:GD32C10x_StdPeripherals:RTC - component: Device:GD32C10x_StdPeripherals:SPI_I2S - component: Device:GD32C10x_StdPeripherals:TIMER - component: Device:GD32C10x_StdPeripherals:USART - - component: Device:GD32C10x_StdPeripherals:WWDGT - component: Device:GD32C10x_libopt + - component: Device:GD32C10x_StdPeripherals:DBG - component: Device:EVAL:GD32C10x - - component: Device:GD32C10x_StdPeripherals:EXMC - - component: CMSIS:DSP output: type: - elf diff --git a/Robot_V2/device/ADC.cpp b/Robot_V2/device/ADC.cpp index e719798..69ef5b3 100644 --- a/Robot_V2/device/ADC.cpp +++ b/Robot_V2/device/ADC.cpp @@ -1,4 +1,5 @@ #include "adc.h" +#include #include #include "math.h" @@ -11,10 +12,11 @@ constexpr float MAX_TEMP = 150.0f; constexpr float MIN_TEMP = -40.0f; constexpr float MAX_CURRENT = 50.0f; constexpr float MIN_CURRENT = -0.1f; -constexpr float MAX_VCC = 40.0f; -constexpr float MIN_VCC = 0.0f; +constexpr float MAX_VCC = 65.0f; +constexpr float MIN_VCC = -0.1f; -extern const u16 adc_value[7]; + +u16 adc_value[7] = {0}; // 显式初始化数组元素为0 /** * @brief 获取MCU温度 @@ -103,7 +105,7 @@ float get_vbus_voltage() { vcc_latest = adc_value[6] * ADC_SCALE * VCC_FACTOR; // 检查电压读数是否在有效范围内 - if (vcc_latest > MAX_VCC || vcc_latest < MAX_VCC) { + if (vcc_latest > MAX_VCC || vcc_latest < MIN_VCC) { return ERROR_ADC_VAL; } } diff --git a/Robot_V2/device/Can.cpp b/Robot_V2/device/Can.cpp index 46e33c3..0fafe36 100644 --- a/Robot_V2/device/Can.cpp +++ b/Robot_V2/device/Can.cpp @@ -1,7 +1,10 @@ #include "Can.h" #include "gd32c10x_can.h" #include "gd32c10x_eval.h" +#include +#include #include +#include #define DEV_CAN_LOOPBACK_ID 0x0000 #define DEV_CAN0_ID 0xaabb @@ -12,10 +15,14 @@ u8 can_gpio_init() { rcu_periph_clock_enable(RCU_CAN0); rcu_periph_clock_enable(RCU_GPIOA); rcu_periph_clock_enable(RCU_AF); - /* configure CAN0 GPIO */ gpio_init(GPIOA, GPIO_MODE_IPU, GPIO_OSPEED_MAX, GPIO_PIN_11); gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_MAX, GPIO_PIN_12); + + /* enable the EN GPIO clock */ + rcu_periph_clock_enable(RCU_GPIOB); + gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13); + gpio_bit_reset(GPIOB, GPIO_PIN_13); return 0; } @@ -27,6 +34,7 @@ u8 can_gpio_init() { */ void can_config(void) { can_parameter_struct can_parameter; + can_filter_parameter_struct can_filter; can_struct_para_init(CAN_INIT_STRUCT, &can_parameter); /* initialize CAN register */ @@ -44,24 +52,31 @@ void can_config(void) { can_parameter.resync_jump_width = CAN_BT_SJW_1TQ; can_parameter.time_segment_1 = CAN_BT_BS1_7TQ; can_parameter.time_segment_2 = CAN_BT_BS2_2TQ; - can_parameter.prescaler = 6; + can_parameter.prescaler = 12; /* initialize CAN */ can_init(CAN0, &can_parameter); - /* initialize filter */ - can1_filter_start_bank(14); - can_filter_mask_mode_init(DEV_CAN0_ID, DEV_CAN0_MASK, CAN_EXTENDED_FIFO0, - 0); - + /* initialize filter */ + /* configure filter mode */ + can_filter.filter_mode = CAN_FILTERMODE_MASK; + can_filter.filter_bits = CAN_FILTERBITS_32BIT; + /* configure filter ID */ + can_filter.filter_list_high = (uint16_t)(DEV_CAN_LOOPBACK_ID >> 13U); + can_filter.filter_list_low = ((uint16_t)(DEV_CAN_LOOPBACK_ID)) << 3U | (1U << 2); + /* configure filter mask */ + can_filter.filter_mask_high = 0x0000; + can_filter.filter_mask_low = 0x0000; + /* select receiver fifo */ + can_filter.filter_fifo_number = CAN_FIFO0; + can_filter.filter_enable = ENABLE; + can_filter_init(&can_filter); + /* configure CAN0 NVIC */ nvic_irq_enable(CAN0_RX0_IRQn, 0, 0); /* enable can receive FIFO0 not empty interrupt */ can_interrupt_enable(CAN0, CAN_INTEN_RFNEIE0); - /* enable the EN GPIO clock */ - rcu_periph_clock_enable(RCU_GPIOB); - gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13); - gpio_bit_reset(GPIOB, GPIO_PIN_13); + } /*! @@ -74,9 +89,13 @@ void canfd_config(void) { can_parameter_struct can_parameter; can_fdframe_struct can_fd_parameter; can_fd_tdc_struct can_fd_tdc_parameter; - can_struct_para_init(CAN_INIT_STRUCT, &can_parameter); - /* initialize CAN register */ - can_deinit(CAN0); + can_filter_parameter_struct can_filter; + + /* enable CAN clock */ + rcu_periph_clock_enable(RCU_CAN0); + + /* initialize CAN register */ + can_deinit(CAN0); /* initialize CAN parameters */ can_parameter.time_triggered = DISABLE; @@ -90,7 +109,7 @@ void canfd_config(void) { can_parameter.resync_jump_width = CAN_BT_SJW_1TQ; can_parameter.time_segment_1 = CAN_BT_BS1_7TQ; can_parameter.time_segment_2 = CAN_BT_BS2_2TQ; - can_parameter.prescaler = 6; + can_parameter.prescaler = 12; /* initialize CAN */ can_init(CAN0, &can_parameter); @@ -110,23 +129,17 @@ void canfd_config(void) { can_fd_parameter.data_time_segment_2 = CAN_BT_BS2_1TQ; /* CAN-FD data segement prescaler should be the same as non-data segement * prescaler */ - can_fd_parameter.data_prescaler = 6; + can_fd_parameter.data_prescaler = 12; can_fd_init(CAN0, &can_fd_parameter); - /* initialize filter */ - can1_filter_start_bank(14); - can_filter_mask_mode_init(DEV_CAN0_ID, DEV_CAN0_MASK, CAN_EXTENDED_FIFO0, - 0); + /* initialize filter */ + can1_filter_start_bank(14); + can_filter_mask_mode_init(DEV_CAN0_ID, DEV_CAN0_MASK, CAN_EXTENDED_FIFO0, 0); - /* configure CAN0 NVIC */ - nvic_irq_enable(CAN0_RX0_IRQn, 0, 0); - /* enable can receive FIFO0 not empty interrupt */ - can_interrupt_enable(CAN0, CAN_INTEN_RFNEIE0); - - /* enable the EN GPIO clock */ - rcu_periph_clock_enable(RCU_GPIOB); - gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13); - gpio_bit_reset(GPIOB, GPIO_PIN_13); + /* configure CAN0 NVIC */ + nvic_irq_enable(CAN0_RX0_IRQn, 0, 0); + /* enable can receive FIFO0 not empty interrupt */ + can_interrupt_enable(CAN0, CAN_INTEN_RFNEIE0); } /*! @@ -137,27 +150,54 @@ void canfd_config(void) { */ void can_loopback_init(void) { can_parameter_struct can_parameter; + can_fdframe_struct can_fd_parameter; + can_fd_tdc_struct can_fd_tdc_parameter; + can_filter_parameter_struct can_filter; - /* enable CAN clock */ - rcu_periph_clock_enable(RCU_CAN0); + /* enable CAN clock */ + rcu_periph_clock_enable(RCU_CAN0); - /* initialize CAN register */ - can_deinit(CAN0); + /* initialize CAN register */ + can_deinit(CAN0); - /* initialize CAN */ + /* initialize CAN parameters */ can_parameter.time_triggered = DISABLE; can_parameter.auto_bus_off_recovery = DISABLE; can_parameter.auto_wake_up = DISABLE; - can_parameter.auto_retrans = DISABLE; - can_parameter.rec_fifo_overwrite = DISABLE; - can_parameter.trans_fifo_order = DISABLE; + can_parameter.auto_retrans = ENABLE; + can_parameter.rec_fifo_overwrite = ENABLE; + can_parameter.trans_fifo_order = ENABLE; can_parameter.working_mode = CAN_LOOPBACK_MODE; /* baudrate 1Mbps, sample piont at 80% */ can_parameter.resync_jump_width = CAN_BT_SJW_1TQ; can_parameter.time_segment_1 = CAN_BT_BS1_7TQ; can_parameter.time_segment_2 = CAN_BT_BS2_2TQ; - can_parameter.prescaler = 6; + can_parameter.prescaler = 12; + /* initialize CAN */ can_init(CAN0, &can_parameter); + + can_struct_para_init(CAN_FD_FRAME_STRUCT, &can_fd_parameter); + can_fd_parameter.fd_frame = ENABLE; + can_fd_parameter.excp_event_detect = ENABLE; + can_fd_parameter.delay_compensation = ENABLE; + can_fd_tdc_parameter.tdc_filter = 0x04; + can_fd_tdc_parameter.tdc_mode = CAN_TDCMOD_CALC_AND_OFFSET; + can_fd_tdc_parameter.tdc_offset = 0x04; + can_fd_parameter.p_delay_compensation = &can_fd_tdc_parameter; + can_fd_parameter.iso_bosch = CAN_FDMOD_ISO; + can_fd_parameter.esi_mode = CAN_ESIMOD_HARDWARE; + /* FD mode data baudrate 2Mbps, sample piont at 80% */ + can_fd_parameter.data_resync_jump_width = CAN_BT_SJW_1TQ; + can_fd_parameter.data_time_segment_1 = CAN_BT_BS1_3TQ; + can_fd_parameter.data_time_segment_2 = CAN_BT_BS2_1TQ; + /* CAN-FD data segement prescaler should be the same as non-data segement + * prescaler */ + can_fd_parameter.data_prescaler = 12; + can_fd_init(CAN0, &can_fd_parameter); + + /* initialize filter */ + can1_filter_start_bank(14); + can_filter_mask_mode_init(DEV_CAN_LOOPBACK_ID, DEV_CAN0_MASK, CAN_EXTENDED_FIFO0, 0); } /*! @@ -167,51 +207,51 @@ void can_loopback_init(void) { \retval ErrStatus */ void can_loopback_test(void) { - can_trasnmit_message_struct transmit_message; - can_receive_message_struct receive_message; - uint32_t timeout = 0xFFFF; - uint8_t transmit_mailbox = 0; + can_trasnmit_message_struct transmit_message; + can_receive_message_struct receive_message; + uint32_t timeout = 0xFFFF; + uint8_t transmit_mailbox = 0; - can_struct_para_init(CAN_TX_MESSAGE_STRUCT, &transmit_message); - /* initialize transmit message */ - transmit_message.tx_sfid = DEV_CAN_LOOPBACK_ID; - transmit_message.tx_ft = CAN_FT_DATA; - transmit_message.tx_ff = CAN_FF_STANDARD; - transmit_message.tx_dlen = 2; - transmit_message.tx_data[0] = 0xAB; - transmit_message.tx_data[1] = 0xCD; - /* transmit message */ - transmit_mailbox = can_message_transmit(CAN0, &transmit_message); - /* waiting for transmit completed */ - while ((CAN_TRANSMIT_OK != can_transmit_states(CAN0, transmit_mailbox)) && - (0 != timeout)) { - timeout--; - } - timeout = 0xFFFF; - /* waiting for receive completed */ - while ((can_receive_message_length_get(CAN0, CAN_FIFO0) < 1) && - (0 != timeout)) { - timeout--; - } - can_struct_para_init(CAN_RX_MESSAGE_STRUCT, &receive_message); - can_message_receive(CAN0, CAN_FIFO0, &receive_message); + can_struct_para_init(CAN_FD_FRAME_STRUCT, &transmit_message); + transmit_message.tx_sfid = DEV_CAN_LOOPBACK_ID; + transmit_message.tx_efid = 0x00; + transmit_message.tx_ft = CAN_FT_DATA; + transmit_message.tx_ff = CAN_FF_EXTENDED; + transmit_message.tx_dlen = 12; + transmit_message.fd_flag = 1; + transmit_message.fd_brs = 1; + transmit_message.fd_esi = 0; - /* check the receive message */ - if ((DEV_CAN_LOOPBACK_ID == receive_message.rx_sfid) && - (CAN_FF_STANDARD == receive_message.rx_ff) && - (2 == receive_message.rx_dlen) && - (0xCDAB == - (receive_message.rx_data[1] << 8 | receive_message.rx_data[0]))) { - /* loopback test is success */ - printf("loopback success\r"); - for (u8 i = 0U; i < receive_message.rx_dlen; i++) { - printf(" %02x", receive_message.rx_data[i]); - } - } else { - /* loopback test is failed */ - printf("loopback failed\r"); + int i = 0; + while (i++> 8) & 0xFF); -} - -// 添加辅助函数用于将u16类型数据打包到u8数组中 -void pack_u16_to_bytes(u8* buffer, u16 offset, u16 value) { - buffer[offset] = (u8)(value & 0xFF); - buffer[offset + 1] = (u8)((value >> 8) & 0xFF); -} - -// 添加辅助函数用于将float类型数据打包到u8数组中 -void pack_float_to_bytes(u8* buffer, u16 offset, float_union_t& float_union) { - buffer[offset] = float_union.b[0]; - buffer[offset + 1] = float_union.b[1]; - buffer[offset + 2] = float_union.b[2]; - buffer[offset + 3] = float_union.b[3]; -} - /** * @brief CAN发送函数 * @retval 0: 发送失败 @@ -412,7 +431,31 @@ void pack_float_to_bytes(u8* buffer, u16 offset, float_union_t& float_union) { u8 CAN_transmiter::CAN_all_Send(){ u32 success_flag = 0; + constexpr u32 RT_Vel_Ang_FEED = 0x101; + constexpr u32 SET_Vel_Ang_FEED = 0x110; + constexpr u32 Current_FEED = 0x117; + constexpr u32 Status_FEED = 0x118; + constexpr u32 Limit_Vel_Vol_FEED = 0x119; + //打包RT_Vel_Ang_FEED + pack_float_to_bytes(transmit_data, 0, rt_angle_union); + pack_float_to_bytes(transmit_data, 4, rt_velocity_union); + //打包SET_Vel_Ang_FEED + pack_float_to_bytes(transmit_data, 0, target_angle_union); + pack_float_to_bytes(transmit_data, 4, target_velocity_union); + //打包Current_FEED + pack_float_to_bytes(transmit_data, 0, u_current_union); + pack_float_to_bytes(transmit_data, 4, w_current_union); + pack_float_to_bytes(transmit_data, 8, v_current_union); + //打包Status_FEED + transmit_data[0] = Status_code; + transmit_data[1] = controller_Mode; + pack_short_to_bytes(transmit_data, 2, motor_temp); + pack_short_to_bytes(transmit_data, 4, mcu_temp); + pack_short_to_bytes(transmit_data, 6, Power_Voltage); + //打包Limit_Vel_Vol_FEED + pack_float_to_bytes(transmit_data, 0, limit_velocity_union); + pack_short_to_bytes(transmit_data, 4, limit_voltage); return success_flag; } diff --git a/Robot_V2/device/Can.h b/Robot_V2/device/Can.h index 7fa2835..0e7f6d9 100644 --- a/Robot_V2/device/Can.h +++ b/Robot_V2/device/Can.h @@ -11,17 +11,38 @@ extern "C" { #include "stdint.h" #include -u8 can_gpio_init(void); -void can_config(void); -void canfd_config(void); -void can_loopback_init(void); -void can_loopback_test(void); // 定义一个联合体类型用于float和byte数组之间的转换 typedef union { float f; u8 b[4]; } float_union_t; +// 添加辅助函数用于将short类型数据打包到u8数组中 +static inline void pack_short_to_bytes(u8* buffer, u16 offset, short value) { + buffer[offset] = (u8)(value & 0xFF); + buffer[offset + 1] = (u8)((value >> 8) & 0xFF); +} + +// 添加辅助函数用于将u16类型数据打包到u8数组中 +static inline void pack_u16_to_bytes(u8* buffer, u16 offset, u16 value) { + buffer[offset] = (u8)(value & 0xFF); + buffer[offset + 1] = (u8)((value >> 8) & 0xFF); +} + +// 添加辅助函数用于将float类型数据打包到u8数组中 +static inline void pack_float_to_bytes(u8* buffer, u16 offset, float_union_t& float_union) { + buffer[offset] = float_union.b[0]; + buffer[offset + 1] = float_union.b[1]; + buffer[offset + 2] = float_union.b[2]; + buffer[offset + 3] = float_union.b[3]; +} + +u8 can_gpio_init(void); +void can_config(void); +void canfd_config(void); +void can_loopback_init(void); +void can_loopback_test(void); + /*! \brief CAN通信类 \note 封装CAN发送和接收功能 diff --git a/Robot_V2/device/Timer.cpp b/Robot_V2/device/Timer.cpp index d242eda..b76e091 100644 --- a/Robot_V2/device/Timer.cpp +++ b/Robot_V2/device/Timer.cpp @@ -1,18 +1,22 @@ #include "Timer.h" +#include "adc.h" #include "gd32c10x.h" #include "gd32c10x_gpio.h" #include "gd32c10x_timer.h" +#include "initial.h" #include #include #include -constexpr int TIMER1_PRESCALER = 11999; -constexpr int TIMER1_PERIOD = 9999; +constexpr int TIMER1_PRESCALER = 1199; +constexpr int TIMER1_PERIOD = 9; + constexpr int TIMER0_PRESCALER = 5999; constexpr int TIMER0_PERIOD = 15999; constexpr int TIMER0_CH0_PULSE = 4000; constexpr int TIMER0_CH1_PULSE = 6000; constexpr int TIMER0_CH2_PULSE = 8000; +extern u16 adc_value[7]; /** * @brief 设置引脚中断优先级 @@ -22,7 +26,7 @@ constexpr int TIMER0_CH2_PULSE = 8000; */ void nvic_config(void) { /* 设置NVIC优先级分组,1位抢占优先级,3位响应优先级 */ - nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3); + nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2); /* 使能TIMER1中断,设置抢占优先级为1,子优先级为1 */ nvic_irq_enable(TIMER1_IRQn, 1, 1); } @@ -40,7 +44,7 @@ void timer1_config(void) { 1s). ---------------------------------------------------------------------------- */ - timer_parameter_struct timer_initpara; + timer_parameter_struct timer_struct; /* 使能TIMER1时钟 */ rcu_periph_clock_enable(RCU_TIMER1); @@ -48,16 +52,16 @@ void timer1_config(void) { /* 复位TIMER1外设到默认状态 */ timer_deinit(TIMER1); /* 初始化TIMER初始化参数结构体 */ - timer_struct_para_init(&timer_initpara); + timer_struct_para_init(&timer_struct); /* TIMER1配置参数设置 */ - timer_initpara.prescaler = + timer_struct.prescaler = TIMER1_PRESCALER; /* 预分频值:12000分频(从0开始计数) */ - timer_initpara.alignedmode = TIMER_COUNTER_EDGE; /* 边沿对齐模式 */ - timer_initpara.counterdirection = TIMER_COUNTER_UP; /* 向上计数模式 */ - timer_initpara.period = + timer_struct.alignedmode = TIMER_COUNTER_EDGE; /* 边沿对齐模式 */ + timer_struct.counterdirection = TIMER_COUNTER_UP; /* 向上计数模式 */ + timer_struct.period = TIMER1_PERIOD; /* 自动重装载值:10000次计数(从0开始计数) */ - timer_initpara.clockdivision = TIMER_CKDIV_DIV1; /* 时钟分频值:1分频 */ - timer_init(TIMER1, &timer_initpara); + timer_struct.clockdivision = TIMER_CKDIV_DIV1; /* 时钟分频值:1分频 */ + timer_init(TIMER1, &timer_struct); /* 清除更新中断标志位 */ timer_interrupt_flag_clear(TIMER1, TIMER_INT_FLAG_UP); @@ -73,10 +77,10 @@ void timer1_config(void) { * @return void */ void User_timer_init(void) { - /* 配置TIMER1中断 */ - nvic_config(); /* 配置TIMER1 */ timer1_config(); + /* 配置TIMER1中断 */ + nvic_config(); } /** @@ -84,35 +88,41 @@ void User_timer_init(void) { * @return void */ void TIMER1_IRQHandler(void) { - u32 cnt = timer_counter_read(TIMER1); + // 检查更新中断标志并处理 + if (timer_interrupt_flag_get(TIMER1, TIMER_INT_UP) != RESET) { + // 清除更新中断标志 + timer_interrupt_flag_clear(TIMER1, TIMER_INT_UP); + + // 使用静态变量来跟踪定时器事件 + static u8 ms_counter = 0; + static u8 tens_counter = 0; + static u8 hundreds_counter = 0; + static u16 seconds_counter = 0; - // 根据计数器值执行不同的定时任务 - // 每100us执行一次 - // (此处可以添加需要每100us执行的代码) + // 增加计数器 + ms_counter++; + if (ms_counter >= 10) { + ms_counter = 0; + tens_counter++; + // 每1ms执行的代码可以放在这里 + if (tens_counter >= 10) { + tens_counter = 0; + hundreds_counter++; - // 每1ms执行一次 (100us * 10) - if (cnt % 10 == 0) { - // 添加需要每1ms执行的代码 - } + // 每10ms执行的代码可以放在这里 - // 每10ms执行一次 (100us * 100) - if (cnt % 100 == 0) { - // 添加需要每10ms执行的代码 - } + if (hundreds_counter >= 10) { + hundreds_counter = 0; + seconds_counter++; + // 每100ms执行的代码可以放在这里 - // 每100ms执行一次 (100us * 1000) - if (cnt % 1000 == 0) { - // 添加需要每100ms执行的代码 - } - - // 每1s执行一次 (100us * 10000) - if (cnt % 10000 == 0) { - // 添加需要每1s执行的代码 - } - - if (SET == timer_interrupt_flag_get(TIMER1, TIMER_INT_FLAG_UP)) { - /* 清除更新中断标志位 */ - timer_interrupt_flag_clear(TIMER1, TIMER_INT_FLAG_UP); + if (seconds_counter >= 10) { + seconds_counter = 0; + // 每1s执行的代码可以放在这里 + } + } + } + } } } @@ -133,7 +143,6 @@ void timer0_gpio_config(void) { gpio_pin_remap_config(GPIO_TIMER0_FULL_REMAP, ENABLE); } - /** * @brief TIMER0 配置函数 */ diff --git a/Robot_V2/device/Timer.h b/Robot_V2/device/Timer.h index faf8227..7aa2416 100644 --- a/Robot_V2/device/Timer.h +++ b/Robot_V2/device/Timer.h @@ -10,7 +10,7 @@ extern "C" { void User_timer_init(void); void Timer0_init(void); void Set_Pwm(u16 channel,u32 pulse); - +void TIMER1_IRQHandler(void); #ifdef __cplusplus } #endif diff --git a/Robot_V2/device/adc.h b/Robot_V2/device/adc.h index 477e4eb..c5b669b 100644 --- a/Robot_V2/device/adc.h +++ b/Robot_V2/device/adc.h @@ -7,7 +7,6 @@ extern "C" { #include "stdint.h" -const u16 adc_value[7] = {0}; // 显式初始化数组元素为0 float get_mcu_temp(void); float get_drv_temp(void); float get_vbus_voltage(void); diff --git a/Robot_V2/main.cpp b/Robot_V2/main.cpp index 5b5a6f3..59e2e89 100644 --- a/Robot_V2/main.cpp +++ b/Robot_V2/main.cpp @@ -5,89 +5,50 @@ #include "adc.h" #include "initial.h" #include "systick.h" +#include "core_cm4.h" #include "tle5012b.h" +#include + // 全局变量定义 CurrentKalman kalman(0.0f, 0.0026f, 4.8f, 0.0001f, 1e-6f, 0.02f); CircularBuffer buffer; -int main(void) { - systick_config(); - adc_config(); - dma_config(); - // 调试串口初始化 - USART0_init(); +CAN_transmiter CAN0_transmiter; - User_timer_init(); - // CAN初始化 - // CAN_transmiter CAN0_transmiter; - // 初始化CAN硬件 - can_gpio_init(); - can_config(); +int main(void) { + systick_config(); // 系统时钟初始化 + USART0_init(); // 调试串口初始化 + User_timer_init(); // Timer初始化 + adc_config(); // ADC初始化 + dma_config(); // DMA采样ADC初始化 + + tle5012b_gpio_init(); // TLI5012B初始化 + tle5012b_spi_init(); + // cdd_TLI5012_Init(); // 初始化CAN回环测试 - can_loopback_init(); - can_loopback_test(); - - // TLI5012B初始化 - tle5012b_gpio_init(); - tle5012b_spi_init(); - cdd_TLI5012_Init(); - - can_trasnmit_message_struct g_transmit_message; - can_receive_message_struct g_receive_message; - FlagStatus can0_receive_flag; - - // CAN发送初始化 - can_struct_para_init(CAN_TX_MESSAGE_STRUCT, &g_transmit_message); - g_transmit_message.tx_sfid = 0x00; - g_transmit_message.tx_efid = 0x00; - g_transmit_message.tx_ft = CAN_FT_DATA; - g_transmit_message.tx_ff = CAN_FF_EXTENDED; - g_transmit_message.tx_dlen = 8; - g_transmit_message.fd_flag = 0; - g_transmit_message.fd_brs = 1; - g_transmit_message.fd_esi = 0; - - // CAN接收初始化 - can_struct_para_init(CAN_RX_MESSAGE_STRUCT, &g_receive_message); - - /* print out the clock frequency of system, AHB, APB1 and APB2 */ - printf("\r\nCK_SYS is %d", rcu_clock_freq_get(CK_SYS)); - printf("\r\nCK_AHB is %d", rcu_clock_freq_get(CK_AHB)); - printf("\r\nCK_APB1 is %d", rcu_clock_freq_get(CK_APB1)); - printf("\r\nCK_APB2 is %d", rcu_clock_freq_get(CK_APB2)); + // can_loopback_init(); + // can_loopback_test(); // TLI5012B测试 - Test_TLI5012B(); + // Test_TLI5012B(); + extern u16 adc_value[7]; while (1) { - /* delay a time in milliseconds */ - delay_1ms(10); /* value print */ printf(" the temperature data is %2.2f degrees Celsius\r\n", get_mcu_temp()); printf(" the drv_temperature data is %2.2f degrees Celsius\r\n", get_drv_temp()); - // g_transmit_message.tx_efid = 0x201; - // g_transmit_message.tx_data[0] = 0xaa; - // g_transmit_message.tx_data[1] = 0xbb; - // g_transmit_message.tx_data[2] = 0xcc; - // g_transmit_message.tx_data[3] = 0xdd; - // g_transmit_message.tx_data[4] = 0xee; - // g_transmit_message.tx_data[5] = 0xff; - // g_transmit_message.tx_data[6] = 0x00; - // g_transmit_message.tx_data[7] = 0x11; - // g_transmit_message.tx_data[8] = 0x22; - // g_transmit_message.tx_data[9] = 0x33; - // g_transmit_message.tx_data[10] = 0x44; - // g_transmit_message.tx_data[11] = 0x55; - printf("\r\n can0 transmit data: "); - for (u8 i = 0U; i < g_transmit_message.tx_dlen; i++) { - printf(" %02x", g_transmit_message.tx_data[i]); - } - /* transmit message */ - can_message_transmit(CAN0, &g_transmit_message); + printf(" the U current data is %2.2f mA\r\n",get_current('U')); + printf(" the V current data is %2.2f mA\r\n",get_current('V')); + printf(" the W current data is %2.2f mA\r\n",get_current('W')); + printf(" the VBUS is %2.2f V\r\n",get_vbus_voltage()); + printf("\n----------------------------------------------------\n"); + + delay_1ms(200); - // Set_Pwm(0, 1500); + + Set_Pwm(0, 1500); } -} +} \ No newline at end of file diff --git a/Robot_V2/sys/initial.cpp b/Robot_V2/sys/initial.cpp index d738672..59c1213 100644 --- a/Robot_V2/sys/initial.cpp +++ b/Robot_V2/sys/initial.cpp @@ -1,6 +1,8 @@ #include "initial.h" #include "systick.h" +#include +extern u16 adc_value[7]; /*! \brief configure the DMA peripheral @@ -78,7 +80,6 @@ void adc_config(void) { /* 使能ADC接口 */ adc_enable(ADC0); - delay_1ms(1); /* ADC校准 */ adc_calibration_enable(ADC0); diff --git a/Robot_V2/sys/initial.h b/Robot_V2/sys/initial.h index aa692be..45ffe2a 100644 --- a/Robot_V2/sys/initial.h +++ b/Robot_V2/sys/initial.h @@ -13,6 +13,7 @@ void rcu_config(void); void adc_config(void); void dma_config(void); +static volatile uint32_t timer_delay_count = 0; // volatile 很重要! int fputc(int ch, FILE *f); // 声明fputc函数以重载标准库实现 u8 tle5210b_init(void); diff --git a/Robot_V2/sys/systick.c b/Robot_V2/sys/systick.cpp similarity index 98% rename from Robot_V2/sys/systick.c rename to Robot_V2/sys/systick.cpp index 5e257af..2b794da 100644 --- a/Robot_V2/sys/systick.c +++ b/Robot_V2/sys/systick.cpp @@ -34,6 +34,8 @@ OF SUCH DAMAGE. #include "gd32c10x.h" #include "systick.h" +#include "initial.h" +#include volatile static uint32_t delay; @@ -80,4 +82,4 @@ void delay_decrement(void) if (0U != delay){ delay--; } -} +} \ No newline at end of file diff --git a/Robot_V2/sys/systick.h b/Robot_V2/sys/systick.h index b19da66..3441d4c 100644 --- a/Robot_V2/sys/systick.h +++ b/Robot_V2/sys/systick.h @@ -47,6 +47,8 @@ void delay_1ms(uint32_t count); /* delay decrement */ void delay_decrement(void); +void timer_delay_ms(uint32_t ms); + #ifdef __cplusplus } #endif