修复SPI通道错误的问题,增加看门狗功能

This commit is contained in:
cbb
2025-10-02 23:50:12 +08:00
parent 20fbb30067
commit d755715363
9 changed files with 152 additions and 113 deletions
+18
View File
@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "windows-gcc-x86",
"includePath": [
"${workspaceFolder}/**"
],
"compilerPath": "C:/mingw/bin/gcc.exe",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "windows-gcc-x86",
"compilerArgs": [
""
]
}
],
"version": 4
}
@@ -27,6 +27,8 @@
#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:MISC@1.0.0 */
@@ -41,6 +43,8 @@
#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 */
+3 -1
View File
@@ -15,7 +15,7 @@ project:
files:
- file: device/Can.cpp
- file: device/Can.h
- file: device/tle5012b.c
- file: device/tle5012b.cpp
- file: device/tle5012b.h
- file: device/ADC.cpp
- file: device/adc.h
@@ -43,6 +43,8 @@ project:
- component: Device:GD32C10x_libopt
- component: Device:GD32C10x_StdPeripherals:DBG
- component: Device:EVAL:GD32C10x
- component: Device:GD32C10x_StdPeripherals:FWDGT
- component: Device:GD32C10x_StdPeripherals:WWDGT
output:
type:
- elf
+2 -2
View File
@@ -247,10 +247,10 @@ void can_loopback_test(void) {
if((transmit_message.tx_sfid == receive_message.rx_sfid) && (transmit_message.tx_ff == receive_message.rx_ff)
&& (transmit_message.tx_dlen == receive_message.rx_dlen) ) {
/* loopback test is success */
std::printf("CAN LO0PBACK SUCCESS");
std::printf("CAN LO0PBACK SUCCESS\r\n");
} else {
/* loopback test is failed */
std::printf("CAN LO0PBACK FAIL");
std::printf("CAN LO0PBACK FAIL\r\n");
}
}
@@ -4,11 +4,11 @@
#define SPI_TX_OFF() \
do { \
gpio_init(GPIOB, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_5); \
} while(0);
} while(0)
#define SPI_TX_ON() \
do { \
gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5); \
} while(0);
} while(0)
#define SPI_CS_ENABLE() gpio_bit_reset(GPIOA, GPIO_PIN_15)
#define SPI_CS_DISABLE() gpio_bit_set(GPIOA, GPIO_PIN_15)
@@ -20,42 +20,39 @@
*/
/* SPI operation types */
#define WRITE 0 /**< SPI Write Operation */
#define READ 1 /**< SPI Read Operation */
static constexpr uint16_t WRITE = 0; /**< SPI Write Operation */
static constexpr uint16_t READ = 1; /**< SPI Read Operation */
/* SPI communication constants */
#define DUMMY_BYTE 0xFFFF /**< Dummy byte for SPI read operations */
static constexpr uint16_t DUMMY_BYTE = 0xFFFF; /**< Dummy byte for SPI read operations */
#define WR_REG 0x0000 /**< Write command - MSB is 0 */
#define RD_REG 0x8000 /**< Read command - MSB is 1 */
static constexpr uint16_t WR_REG = 0x0000; /**< Write command - MSB is 0 */
static constexpr uint16_t RD_REG = 0x8000; /**< Read command - MSB is 1 */
#define LockValue_LADDR 0x0000 /**< Lock value for addresses 0x00-0x04 */
#define LockValue_HADDR 0x5000 /**< Lock value for addresses 0x05-0x11 */
static constexpr uint16_t LockValue_LADDR = 0x0000; /**< Lock value for addresses 0x00-0x04 */
static constexpr uint16_t LockValue_HADDR = 0x5000; /**< Lock value for addresses 0x05-0x11 */
/* Update buffer access command */
#define UPD_CMD 0x0400 /**< Update command - bit 10 is 1, access update buffer */
static constexpr uint16_t UPD_CMD = 0x0400; /**< Update command - bit 10 is 1, access update buffer */
/* Read command values */
#define READ_STAT_VALUE 0x8001 /**< Read status register */
#define READ_ACSTAT_VALUE 0x8011 /**< Read activation status register */
#define READ_ANGLE_VALUE 0x8021 /**< Read angle value register */
#define READ_SPEED_VALUE 0x8031 /**< Read speed value register */
static constexpr uint16_t READ_STAT_VALUE = 0x8001; /**< Read status register */
static constexpr uint16_t READ_ACSTAT_VALUE = 0x8011; /**< Read activation status register */
static constexpr uint16_t READ_ANGLE_VALUE = 0x8021; /**< Read angle value register */
static constexpr uint16_t READ_SPEED_VALUE = 0x8031; /**< Read speed value register */
/* Write command values and their corresponding data */
#define WRITE_MOD1_VALUE 0x5061 /**< Write to MOD1 register (0_1010_0_000110_0001) */
#define MOD1_VALUE 0x0001 /**< MOD1 register configuration value */
static constexpr uint16_t WRITE_MOD1_VALUE = 0x5061; /**< Write to MOD1 register (0_1010_0_000110_0001) */
static constexpr uint16_t MOD1_VALUE = 0x0001; /**< MOD1 register configuration value */
#define WRITE_MOD2_VALUE 0x5081 /**< Write to MOD2 register (0_1010_0_001000_0001) */
#define MOD2_VALUE 0x0809 /**< MOD2 register configuration value */
static constexpr uint16_t WRITE_MOD2_VALUE = 0x5081; /**< Write to MOD2 register (0_1010_0_001000_0001) */
static constexpr uint16_t MOD2_VALUE = 0x0809; /**< MOD2 register configuration value */
#define WRITE_MOD3_VALUE 0x5091 /**< Write to MOD3 register (0_1010_0_001001_0001) */
#define MOD3_VALUE 0x0000 /**< MOD3 register configuration value */
#define WRITE_MOD4_VALUE 0x50E1 /**< Write to MOD4 register (0_1010_0_001110_0001) */
#define MOD4_VALUE \
0x0080 /**< MOD4 register value: 12-bit 4096, absolute count disable 0x0080; \
enable 0x0000 */
static constexpr uint16_t WRITE_MOD3_VALUE = 0x5091; /**< Write to MOD3 register (0_1010_0_001001_0001) */
static constexpr uint16_t MOD3_VALUE = 0x0000; /**< MOD3 register configuration value */
static constexpr uint16_t WRITE_MOD4_VALUE = 0x50E1; /**< Write to MOD4 register (0_1010_0_001110_0001) */
static constexpr uint16_t MOD4_VALUE = 0x0080; /**< MOD4 register value: 12-bit 4096, absolute count disable 0x0080; enable 0x0000 */
/**
* @brief Send or read half word through SPI
@@ -113,10 +110,10 @@ 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(SPI0, i_Cmd);
result = spi_rw_half_word(SPI0, i_Data);
result = spi_rw_half_word(SPI2, i_Cmd);
result = spi_rw_half_word(SPI2, i_Data);
SPI_TX_OFF(); /* Configure MOSI as input to avoid conflicts */
result = spi_rw_half_word(SPI0, DUMMY_BYTE);
result = spi_rw_half_word(SPI2, DUMMY_BYTE);
SPI_CS_DISABLE();
SPI_TX_ON(); /* Restore MOSI as output */
return result;
@@ -133,10 +130,10 @@ 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(SPI0, i_Cmd);
*i_Data = spi_rw_half_word(SPI2, i_Cmd);
SPI_TX_OFF(); /* Configure MOSI as input to avoid conflicts */
*i_Data = spi_rw_half_word(SPI0, DUMMY_BYTE);
result = spi_rw_half_word(SPI0, DUMMY_BYTE);
*i_Data = spi_rw_half_word(SPI2, DUMMY_BYTE);
result = spi_rw_half_word(SPI2, DUMMY_BYTE);
SPI_CS_DISABLE();
SPI_TX_ON(); /* Restore MOSI as output */
return result;
@@ -169,7 +166,7 @@ void tle5012b_spi_init(void)
spi_parameter_struct spi_init_str;
spi_struct_para_init(&spi_init_str);
/* SPI0 parameter config */
/* SPI2 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;
@@ -226,82 +223,82 @@ void cdd_TLI5012_Init(void)
u16Safe = cdd_TLI5012_ReadReg(u16Command, &u16Data);
printf("\r\n [TLI5012Init] read MOD1: 0x%04X, SafeWord: 0x%04X \r\n", u16Data, u16Safe);
/* Write MOD1 register with configuration value */
u16Command = WRITE_MOD1_VALUE;
u16Data = MOD1_VALUE;
u16Safe = cdd_TLI5012_WriteReg(u16Command, u16Data);
printf("\r\n [TLI5012Init] [MOD1] write u16Data: 0x%04X, SafeWord: 0x%04X \r\n", u16Data, u16Safe);
// /* Write MOD1 register with configuration value */
// u16Command = WRITE_MOD1_VALUE;
// u16Data = MOD1_VALUE;
// u16Safe = cdd_TLI5012_WriteReg(u16Command, u16Data);
// printf("\r\n [TLI5012Init] [MOD1] write u16Data: 0x%04X, SafeWord: 0x%04X \r\n", u16Data, u16Safe);
/* Verify MOD1 register write operation */
u16Command = RD_REG | WRITE_MOD1_VALUE;
u16Safe = cdd_TLI5012_ReadReg(u16Command, &u16Data);
printf("\r\n [TLI5012Init] read MOD1: 0x%04X, SafeWord: 0x%04X \r\n", u16Data, u16Safe);
// /* Verify MOD1 register write operation */
// u16Command = RD_REG | WRITE_MOD1_VALUE;
// u16Safe = cdd_TLI5012_ReadReg(u16Command, &u16Data);
// printf("\r\n [TLI5012Init] read MOD1: 0x%04X, SafeWord: 0x%04X \r\n", u16Data, u16Safe);
/* ---------- Step 4: Configure MOD2 register ---------- */
/* Read current MOD2 register value */
u16Command = RD_REG | WRITE_MOD2_VALUE;
u16Safe = cdd_TLI5012_ReadReg(u16Command, &u16Data);
printf("\r\n [TLI5012Init] read MOD2: 0x%04X, SafeWord: 0x%04X \r\n", u16Data, u16Safe);
// /* ---------- Step 4: Configure MOD2 register ---------- */
// /* Read current MOD2 register value */
// u16Command = RD_REG | WRITE_MOD2_VALUE;
// u16Safe = cdd_TLI5012_ReadReg(u16Command, &u16Data);
// printf("\r\n [TLI5012Init] read MOD2: 0x%04X, SafeWord: 0x%04X \r\n", u16Data, u16Safe);
/* Write MOD2 register with configuration value */
u16Command = WRITE_MOD2_VALUE;
u16Data = MOD2_VALUE;
u16Safe = cdd_TLI5012_WriteReg(u16Command, u16Data);
printf("\r\n [TLI5012Init] [MOD2] write u16Data: 0x%04X, SafeWord: 0x%04X \r\n", u16Data, u16Safe);
// /* Write MOD2 register with configuration value */
// u16Command = WRITE_MOD2_VALUE;
// u16Data = MOD2_VALUE;
// u16Safe = cdd_TLI5012_WriteReg(u16Command, u16Data);
// printf("\r\n [TLI5012Init] [MOD2] write u16Data: 0x%04X, SafeWord: 0x%04X \r\n", u16Data, u16Safe);
/* Verify MOD2 register write operation */
u16Command = RD_REG | WRITE_MOD2_VALUE;
u16Safe = cdd_TLI5012_ReadReg(u16Command, &u16Data);
printf("\r\n [TLI5012Init] read MOD2: 0x%04X, SafeWord: 0x%04X \r\n", u16Data, u16Safe);
// /* Verify MOD2 register write operation */
// u16Command = RD_REG | WRITE_MOD2_VALUE;
// u16Safe = cdd_TLI5012_ReadReg(u16Command, &u16Data);
// printf("\r\n [TLI5012Init] read MOD2: 0x%04X, SafeWord: 0x%04X \r\n", u16Data, u16Safe);
/* ---------- Step 5: Configure MOD3 register ---------- */
/* Read current MOD3 register value */
u16Command = RD_REG | WRITE_MOD3_VALUE;
u16Safe = cdd_TLI5012_ReadReg(u16Command, &u16Data);
printf("\r\n [TLI5012Init] read MOD3: 0x%04X, SafeWord: 0x%04X \r\n", u16Data, u16Safe);
// /* ---------- Step 5: Configure MOD3 register ---------- */
// /* Read current MOD3 register value */
// u16Command = RD_REG | WRITE_MOD3_VALUE;
// u16Safe = cdd_TLI5012_ReadReg(u16Command, &u16Data);
// printf("\r\n [TLI5012Init] read MOD3: 0x%04X, SafeWord: 0x%04X \r\n", u16Data, u16Safe);
/* Write MOD3 register with configuration value */
u16Command = WRITE_MOD3_VALUE;
u16Data = MOD3_VALUE;
u16Safe = cdd_TLI5012_WriteReg(u16Command, u16Data);
printf("\r\n [TLI5012Init] [MOD3] write u16Data: 0x%04X, SafeWord: 0x%04X \r\n", u16Data, u16Safe);
// /* Write MOD3 register with configuration value */
// u16Command = WRITE_MOD3_VALUE;
// u16Data = MOD3_VALUE;
// u16Safe = cdd_TLI5012_WriteReg(u16Command, u16Data);
// printf("\r\n [TLI5012Init] [MOD3] write u16Data: 0x%04X, SafeWord: 0x%04X \r\n", u16Data, u16Safe);
/* Verify MOD3 register write operation */
u16Command = RD_REG | WRITE_MOD3_VALUE;
u16Safe = cdd_TLI5012_ReadReg(u16Command, &u16Data);
printf("\r\n [TLI5012Init] read MOD3: 0x%04X, SafeWord: 0x%04X \r\n", u16Data, u16Safe);
// /* Verify MOD3 register write operation */
// u16Command = RD_REG | WRITE_MOD3_VALUE;
// u16Safe = cdd_TLI5012_ReadReg(u16Command, &u16Data);
// printf("\r\n [TLI5012Init] read MOD3: 0x%04X, SafeWord: 0x%04X \r\n", u16Data, u16Safe);
/* Optional: Configure MOD3 register with SSC_OD = 1 (commented out) */
// /* Optional: Configure MOD3 register with SSC_OD = 1 (commented out) */
u16Command = 0x5091;
u16Data |= 0x0004; // Set SSC_OD bit
u16Safe = cdd_TLI5012_WriteReg(u16Command, u16Data);
printf("\r\n [TLI5012Init] [MOD3] write u16Data: 0x%04X, SafeWord: 0x%04X \r\n", u16Data, u16Safe);
// u16Command = 0x5091;
// u16Data |= 0x0004; // Set SSC_OD bit
// u16Safe = cdd_TLI5012_WriteReg(u16Command, u16Data);
// printf("\r\n [TLI5012Init] [MOD3] write u16Data: 0x%04X, SafeWord: 0x%04X \r\n", u16Data, u16Safe);
/* ---------- Step 6: Configure MOD4 register ---------- */
/* Read current MOD4 register value */
u16Command = RD_REG | WRITE_MOD4_VALUE;
u16Safe = cdd_TLI5012_ReadReg(u16Command, &u16Data);
printf("\r\n [TLI5012Init] read MOD4: 0x%04X, SafeWord: 0x%04X \r\n", u16Data, u16Safe);
// /* ---------- Step 6: Configure MOD4 register ---------- */
// /* Read current MOD4 register value */
// u16Command = RD_REG | WRITE_MOD4_VALUE;
// u16Safe = cdd_TLI5012_ReadReg(u16Command, &u16Data);
// printf("\r\n [TLI5012Init] read MOD4: 0x%04X, SafeWord: 0x%04X \r\n", u16Data, u16Safe);
/* Write MOD4 register with configuration value */
u16Command = WRITE_MOD4_VALUE;
u16Data = MOD4_VALUE;
u16Safe = cdd_TLI5012_WriteReg(u16Command, u16Data);
printf("\r\n [TLI5012Init] [MOD4] write u16Data: 0x%04X, SafeWord: 0x%04X \r\n", u16Data, u16Safe);
// /* Write MOD4 register with configuration value */
// u16Command = WRITE_MOD4_VALUE;
// u16Data = MOD4_VALUE;
// u16Safe = cdd_TLI5012_WriteReg(u16Command, u16Data);
// printf("\r\n [TLI5012Init] [MOD4] write u16Data: 0x%04X, SafeWord: 0x%04X \r\n", u16Data, u16Safe);
/* Verify MOD4 register write operation */
u16Command = RD_REG | WRITE_MOD4_VALUE;
u16Safe = cdd_TLI5012_ReadReg(u16Command, &u16Data);
printf("\r\n [TLI5012Init] read MOD4: 0x%04X, SafeWord: 0x%04X \r\n", u16Data, u16Safe);
// /* Verify MOD4 register write operation */
// u16Command = RD_REG | WRITE_MOD4_VALUE;
// u16Safe = cdd_TLI5012_ReadReg(u16Command, &u16Data);
// printf("\r\n [TLI5012Init] read MOD4: 0x%04X, SafeWord: 0x%04X \r\n", u16Data, u16Safe);
}
void Test_TLI5012B(void)
{
uint16_t u16Command = 0x8021;
uint16_t u16Command = READ_ANGLE_VALUE;
uint16_t u16Data = 0x0000;
uint16_t u16Safe = 0x0000;
float angle = 0;
@@ -309,7 +306,7 @@ void Test_TLI5012B(void)
u16Safe = cdd_TLI5012_ReadReg(u16Command, &u16Data);
u16Data = u16Data & 0x7FFF;
printf ( "\r\n AVAL = 0x%04X, SafeWord = 0x%04X \r\n", u16Data, u16Safe);
// angle = 360*( ((int16_t)(u16Data << 1)) >> 1)/32768;
// angle = 360*( ((int16_t)(u16Data << 1)) >> 1)/32768;
angle = ( 360*( ( (int16_t)(u16Data << 1) ) >> 1 ) ) >> 15;
printf ( "\r\n angle = %f, AVAL = %d \r\n", angle, (((int16_t)(u16Data << 1)) >> 1));
}
+1 -2
View File
@@ -4,8 +4,7 @@
extern "C" {
#endif
#include "gd32c10x_eval.h"
#include <stdio.h>
#include "stdio.h"
void tle5012b_gpio_init(void);
void tle5012b_spi_init(void);
+21 -17
View File
@@ -4,9 +4,11 @@
#include "Timer.h"
#include "adc.h"
#include "initial.h"
#include "sys/initial.h"
#include "systick.h"
#include "core_cm4.h"
#include "tle5012b.h"
#include <cinttypes>
#include <cstdio>
@@ -26,29 +28,31 @@ int main(void) {
tle5012b_spi_init();
// cdd_TLI5012_Init();
watchdog_init(1000);
// 初始化CAN回环测试
// can_loopback_init();
// can_loopback_test();
// TLI5012B测试
// Test_TLI5012B();
extern u16 adc_value[7];
while (1) {
/* 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());
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");
/* clear the FWDGT reset flag */
fwdgt_counter_reload();
// can_loopback_test();
// /* 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());
// 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);
// TLI5012B测试
// Test_TLI5012B();
Set_Pwm(0, 1500);
}
}
+15 -1
View File
@@ -95,7 +95,6 @@ void GPIO_config(void) {
rcu_periph_clock_enable(RCU_GPIOB);
// 配置PB2为输入引脚,用于读取电机状态
gpio_init(GPIOB, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
}
/* retarget the C library printf function to the USART */
@@ -104,6 +103,21 @@ u8 USART0_init() {
return 1;
}
/*!
\brief configure the watchdog peripheral
\param[in] none
\param[out] none
\retval none
*/
u8 watchdog_init(u32 time) {
u8 status = 0;
/* configure FWDGT counter clock: 40KHz(IRC40K) / 64 = 0.625 KHz */
/* time = reload_value / clock_frequency */
status = fwdgt_config(time * 0.625, FWDGT_PSC_DIV64);
fwdgt_enable();
return status;
}
int fputc(int ch, FILE *f) throw() {
usart_data_transmit(EVAL_COM0, (uint8_t)ch);
while (RESET == usart_flag_get(EVAL_COM0, USART_FLAG_TBE))
+1
View File
@@ -18,6 +18,7 @@ int fputc(int ch, FILE *f); // 声明fputc函数以重载标准库实现
u8 tle5210b_init(void);
u8 USART0_init(void);
u8 watchdog_init(u32 time);
#ifdef __cplusplus
}