From 2d807260c50d691894012016a0c27aeb41d85b78 Mon Sep 17 00:00:00 2001 From: cbb2625 <2625274797@qq.com> Date: Thu, 9 Oct 2025 21:09:33 +0800 Subject: [PATCH] =?UTF-8?q?SPI=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Robot_V2/device/tle5012b.cpp | 46 +++++++++++++++++++----------------- Robot_V2/main.cpp | 15 ++++++------ 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/Robot_V2/device/tle5012b.cpp b/Robot_V2/device/tle5012b.cpp index d39d58b..9f8d0bc 100644 --- a/Robot_V2/device/tle5012b.cpp +++ b/Robot_V2/device/tle5012b.cpp @@ -1,4 +1,5 @@ #include "tle5012b.h" +#include "gd32c10x_gpio.h" #include "stdbool.h" #define SPI_TX_OFF() \ @@ -88,7 +89,7 @@ uint16_t spi_rw_half_word(uint32_t spi_periph, const uint16_t i_HalfWord) data = spi_i2s_data_receive(spi_periph); /* throw Dummy */ } - /* ---------- wait bsu idle ---------- */ + /* ---------- wait bus idle ---------- */ if (ok) { to = 200; @@ -110,12 +111,12 @@ uint16_t cdd_TLI5012_WriteReg(uint16_t i_Cmd, uint16_t i_Data) { uint16_t result = 0xFFFF; /* Default error code */ SPI_CS_ENABLE(); - result = spi_rw_half_word(SPI2, i_Cmd); - result = spi_rw_half_word(SPI2, i_Data); + SPI_TX_ON(); + result = spi_rw_half_word(SPI0, i_Cmd); + result = spi_rw_half_word(SPI0, i_Data); SPI_TX_OFF(); /* Configure MOSI as input to avoid conflicts */ - result = spi_rw_half_word(SPI2, DUMMY_BYTE); + // result = spi_rw_half_word(SPI0, result); SPI_CS_DISABLE(); - SPI_TX_ON(); /* Restore MOSI as output */ return result; } @@ -130,29 +131,30 @@ uint16_t cdd_TLI5012_ReadReg(uint16_t i_Cmd, uint16_t * const i_Data) { uint16_t result = 0xFFFF; /* Default error code */ SPI_CS_ENABLE(); - *i_Data = spi_rw_half_word(SPI2, i_Cmd); + SPI_TX_ON(); + *i_Data = spi_rw_half_word(SPI0, i_Cmd); SPI_TX_OFF(); /* Configure MOSI as input to avoid conflicts */ - *i_Data = spi_rw_half_word(SPI2, DUMMY_BYTE); - result = spi_rw_half_word(SPI2, DUMMY_BYTE); + // *i_Data = spi_rw_half_word(SPI0, DUMMY_BYTE); + result = spi_rw_half_word(SPI0, result); SPI_CS_DISABLE(); - SPI_TX_ON(); /* Restore MOSI as output */ return result; } void tle5012b_gpio_init(void) { - rcu_periph_clock_enable(RCU_SPI2); + rcu_periph_clock_enable(RCU_GPIOA); + rcu_periph_clock_enable(RCU_GPIOB); + rcu_periph_clock_enable(RCU_SPI0); + rcu_periph_clock_enable(RCU_AF); + /* JTAG-DP disabled and SW-DP enabled, so SPI0 can use PB3 and PB4 */ gpio_pin_remap_config(GPIO_SWJ_SWDPENABLE_REMAP, ENABLE); - /*pin configuration: PA15 CS */ - rcu_periph_clock_enable(RCU_GPIOA); + gpio_pin_remap_config(GPIO_SPI0_REMAP, ENABLE); + /* SPI0 GPIO config: NSS/PA15, SCK/PB3, MOSI/PB5 */ gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_15); - /*rcu_periph_clock_enable*/ - rcu_periph_clock_enable(RCU_GPIOB); - /* PB3 SCLK, PB5 MOSI, */ gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_3 | GPIO_PIN_5); - /* PB4 MISO, */ + /* SPI0 GPIO config: MISO/PB4 */ gpio_init(GPIOB, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_4); } @@ -166,7 +168,7 @@ void tle5012b_spi_init(void) spi_parameter_struct spi_init_str; spi_struct_para_init(&spi_init_str); - /* SPI2 parameter config */ + /* SPI0 parameter config */ spi_init_str.device_mode = SPI_MASTER; spi_init_str.trans_mode = SPI_TRANSMODE_FULLDUPLEX; spi_init_str.frame_size = SPI_FRAMESIZE_16BIT; @@ -174,11 +176,11 @@ void tle5012b_spi_init(void) spi_init_str.nss = SPI_NSS_SOFT; spi_init_str.endian = SPI_ENDIAN_MSB; - spi_init_str.prescale = SPI_PSC_16; - spi_init(SPI2, &spi_init_str); - spi_nss_output_enable(SPI2); - spi_nssp_mode_enable(SPI2); - spi_enable(SPI2); + spi_init_str.prescale = SPI_PSC_128; + spi_init(SPI0, &spi_init_str); + spi_nss_output_enable(SPI0); + spi_nssp_mode_enable(SPI0); + spi_enable(SPI0); } diff --git a/Robot_V2/main.cpp b/Robot_V2/main.cpp index ce5471a..2c96d15 100644 --- a/Robot_V2/main.cpp +++ b/Robot_V2/main.cpp @@ -3,6 +3,7 @@ #include "Filter.h" #include "Timer.h" #include "adc.h" +#include "device/tle5012b.h" #include "initial.h" #include "sys/initial.h" #include "systick.h" @@ -26,9 +27,7 @@ int main(void) { tle5012b_gpio_init(); // TLI5012B初始化 tle5012b_spi_init(); - // cdd_TLI5012_Init(); - - + cdd_TLI5012_Init(); // 初始化TLE5012B传感器 watchdog_init(1000); // 初始化CAN回环测试 @@ -38,7 +37,7 @@ int main(void) { fwdgt_counter_reload(); // can_loopback_test(); - // /* value print */ + /* 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", @@ -47,11 +46,13 @@ int main(void) { // 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"); + // printf(" the angle is %2.2f\r\n", tle5012b_read()); + + printf("\n----------------------------------------------------\n"); - delay_1ms(200); + delay_1ms(700); // TLI5012B测试 - // Test_TLI5012B(); + Test_TLI5012B(); Set_Pwm(0, 1500); }