From dbd56f6842b1acf5c0068605c928c2c1c3cb619c Mon Sep 17 00:00:00 2001 From: jianpingwu1 Date: Wed, 9 Jul 2025 20:55:37 +0800 Subject: [PATCH] update the file according to the review --- src/platform/GD32/bus_i2c_gd32.c | 59 ++++++++++++++++++++---- src/platform/GD32/sdio_gdf4xx.c | 2 - src/platform/GD32/timer_gd32f4xx.c | 4 ++ src/platform/GD32/usb_f4/usbd_msc_desc.c | 2 +- 4 files changed, 56 insertions(+), 11 deletions(-) diff --git a/src/platform/GD32/bus_i2c_gd32.c b/src/platform/GD32/bus_i2c_gd32.c index b66b0c4b1c..4f2e7a919d 100755 --- a/src/platform/GD32/bus_i2c_gd32.c +++ b/src/platform/GD32/bus_i2c_gd32.c @@ -293,6 +293,7 @@ static void i2c_er_handler(I2CDevice device) uint32_t I2Cx = (uint32_t )(i2cDevice[device].hardware->reg); i2cState_t *state = &i2cDevice[device].state; + timeUs_t timeoutStartUs = 0; // Read the I2C status register volatile uint32_t SR1Register = I2C_STAT0(I2Cx); @@ -306,13 +307,28 @@ static void i2c_er_handler(I2CDevice device) i2c_interrupt_disable(I2Cx, I2C_INT_BUF); // disable the RBNE/TBE interrupt - prevent the ISR tailchaining onto the ERR if (!(SR1Register & I2C_STAT0_LOSTARB) && !(I2C_CTL0(I2Cx) & I2C_CTL0_STOP)) { if (I2C_CTL0(I2Cx) & I2C_CTL0_START) { // We are currently trying to send a start, this is very bad as start, stop will hang the peripheral - while (I2C_CTL0(I2Cx) & I2C_CTL0_START) {; } // wait for any start to finish sending + timeoutStartUs = microsISR(); + while (I2C_CTL0(I2Cx) & I2C_CTL0_START) { // wait for any start to finish sending + if (cmpTimeUs(microsISR(), timeoutStartUs) >= I2C_TIMEOUT_US) { + break; + } + } i2c_stop_on_bus(I2Cx); // send stop to finalise bus transaction - while (I2C_CTL0(I2Cx) & I2C_CTL0_STOP) {; } // wait for stop to finish sending + timeoutStartUs = microsISR(); + while (I2C_CTL0(I2Cx) & I2C_CTL0_STOP) { // wait for stop to finish sending + if (cmpTimeUs(microsISR(), timeoutStartUs) >= I2C_TIMEOUT_US) { + break; + } + } i2cInit(device); // reset and configure the hardware } else { i2c_stop_on_bus(I2Cx); // stop to free up the bus - while (I2C_CTL0(I2Cx) & I2C_CTL0_STOP) {; } + timeoutStartUs = microsISR(); + while (I2C_CTL0(I2Cx) & I2C_CTL0_STOP) { // wait for stop to finish sending + if (cmpTimeUs(microsISR(), timeoutStartUs) >= I2C_TIMEOUT_US) { + break; + } + } i2c_interrupt_disable(I2Cx, I2C_INT_ERR); // Disable EV and ERR interrupts while bus inactive i2c_interrupt_disable(I2Cx, I2C_INT_EV); } @@ -329,6 +345,8 @@ void i2c_ev_handler(I2CDevice device) i2cEvState_t *ev_state = &i2c_ev_state[device]; i2cState_t *state = &i2cDevice[device].state; + timeUs_t timeoutStartUs = 0; + uint8_t SReg_1 = I2C_STAT0(I2Cx); // read the status register here if (SReg_1 & I2C_STAT0_SBSEND) { // we just sent a start @@ -349,7 +367,12 @@ void i2c_ev_handler(I2CDevice device) __DMB(); I2C_STAT1(I2Cx); // clear ADDR after ACK is turned off i2c_stop_on_bus(I2Cx); // program the stop - while(I2C_CTL0(I2Cx) & I2C_CTL0_STOP){;} + timeoutStartUs = microsISR(); + while (I2C_CTL0(I2Cx) & I2C_CTL0_STOP) { // wait for stop to finish sending + if (cmpTimeUs(microsISR(), timeoutStartUs) >= I2C_TIMEOUT_US) { + break; + } + } ev_state->final_stop = 1; i2c_interrupt_enable(I2Cx, I2C_INT_BUF); @@ -372,7 +395,12 @@ void i2c_ev_handler(I2CDevice device) i2c_ack_config(I2Cx, I2C_ACK_DISABLE); // turn off ACK state->read_p[ev_state->index++] = (uint8_t)I2C_DATA(I2Cx); // read data N-2 i2c_stop_on_bus(I2Cx); // program the Stop - while (I2C_CTL0(I2Cx) & I2C_CTL0_STOP) {; } + timeoutStartUs = microsISR(); + while (I2C_CTL0(I2Cx) & I2C_CTL0_STOP) { // wait for stop to finish sending + if (cmpTimeUs(microsISR(), timeoutStartUs) >= I2C_TIMEOUT_US) { + break; + } + } ev_state->final_stop = 1; // required to fix hardware state->read_p[ev_state->index++] = (uint8_t)I2C_DATA(I2Cx); // read data N - 1 @@ -380,7 +408,12 @@ void i2c_ev_handler(I2CDevice device) } else { if (ev_state->final_stop) { i2c_stop_on_bus(I2Cx); // program the Stop - while (I2C_CTL0(I2Cx) & I2C_CTL0_STOP) {; } + timeoutStartUs = microsISR(); + while (I2C_CTL0(I2Cx) & I2C_CTL0_STOP) { // wait for stop to finish sending + if (cmpTimeUs(microsISR(), timeoutStartUs) >= I2C_TIMEOUT_US) { + break; + } + } } else { i2c_start_on_bus(I2Cx); // program a rep start } @@ -393,7 +426,12 @@ void i2c_ev_handler(I2CDevice device) if (ev_state->subaddress_sent || (state->writing)) { if (ev_state->final_stop) { i2c_stop_on_bus(I2Cx); // program the Stop - while (I2C_CTL0(I2Cx) & I2C_CTL0_STOP) {; } + timeoutStartUs = microsISR(); + while (I2C_CTL0(I2Cx) & I2C_CTL0_STOP) { // wait for stop to finish sending + if (cmpTimeUs(microsISR(), timeoutStartUs) >= I2C_TIMEOUT_US) { + break; + } + } } else { i2c_start_on_bus(I2Cx); // program a rep start } @@ -405,7 +443,12 @@ void i2c_ev_handler(I2CDevice device) } } // we must wait for the start to clear, otherwise we get constant BTC - while (I2C_CTL0(I2Cx) & I2C_CTL0_START) {; } + timeUs_t timeoutStartUs = microsISR(); + while (I2C_CTL0(I2Cx) & I2C_CTL0_START) { // wait for any start to finish sending + if (cmpTimeUs(microsISR(), timeoutStartUs) >= I2C_TIMEOUT_US) { + break; + } + } } else if (SReg_1 & I2C_STAT0_RBNE) { // Byte received state->read_p[ev_state->index++] = (uint8_t)I2C_DATA(I2Cx); if (state->bytes == (ev_state->index + 3)) diff --git a/src/platform/GD32/sdio_gdf4xx.c b/src/platform/GD32/sdio_gdf4xx.c index f9bc7deeec..20ad150a34 100755 --- a/src/platform/GD32/sdio_gdf4xx.c +++ b/src/platform/GD32/sdio_gdf4xx.c @@ -1530,8 +1530,6 @@ void SDIO_DMA_ST6_IRQHandler(dmaChannelDescriptor_t *dma) { UNUSED(dma); - dma_channel_enum channel_flag_offset = channelx; - // Transfer Error Interrupt management if((DMA_INTF1(DMA1) & DMA_FLAG_ADD(DMA_INT_FLAG_TAE, (DMA_CH6-DMA_CH4))) != 0) { if((DMA_CHCTL(DMA1, DMA_CH6)& DMA_CHXCTL_TAEIE) != 0) { diff --git a/src/platform/GD32/timer_gd32f4xx.c b/src/platform/GD32/timer_gd32f4xx.c index 0d889259ff..ea9ba45caf 100755 --- a/src/platform/GD32/timer_gd32f4xx.c +++ b/src/platform/GD32/timer_gd32f4xx.c @@ -173,6 +173,10 @@ uint32_t timerPrescaler(const TIM_TypeDef *tim) void gd32_timer_input_capture_config(void* timer, uint16_t channel, uint8_t state) { + if (timer == NULL) { + return; + } + switch(channel) { case TIMER_CH_0: if(state) { diff --git a/src/platform/GD32/usb_f4/usbd_msc_desc.c b/src/platform/GD32/usb_f4/usbd_msc_desc.c index 5d99388130..8d5e5caadf 100755 --- a/src/platform/GD32/usb_f4/usbd_msc_desc.c +++ b/src/platform/GD32/usb_f4/usbd_msc_desc.c @@ -154,7 +154,7 @@ __ALIGN_BEGIN const usb_desc_config_set bf_other_speed_msc_config_desc __ALIGN_E .wTotalLength = USB_MSC_CONFIG_DESC_SIZE, .bNumInterfaces = 0x01U, .bConfigurationValue = 0x01U, - .iConfiguration = 0x00U, + .iConfiguration = 0x04U, .bmAttributes = 0xC0U, .bMaxPower = 0x32U },