From 8fa7ed32d82cc84633ab448efe9ad69e50807292 Mon Sep 17 00:00:00 2001 From: yelvlab Date: Tue, 24 Dec 2024 11:17:25 +0800 Subject: [PATCH] =?UTF-8?q?IIC=E5=8F=91=E9=80=81=E5=92=8C=E6=8E=A5?= =?UTF-8?q?=E6=94=B6=E5=87=BD=E6=95=B0=E7=9A=84printf=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E6=9D=A1=E4=BB=B6=E7=BC=96=E8=AF=91=EF=BC=8C?= =?UTF-8?q?=E6=9D=A1=E4=BB=B6=E5=9C=A8main.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/i2c.h | 1 + inc/main.h | 2 ++ src/i2c.c | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/inc/i2c.h b/inc/i2c.h index d285e4c..b1b104b 100644 --- a/inc/i2c.h +++ b/inc/i2c.h @@ -8,6 +8,7 @@ #include "gd32e23x_it.h" #include "gd32e23x.h" #include "systick.h" +#include "main.h" #include #include #include diff --git a/inc/main.h b/inc/main.h index dd037de..f322711 100644 --- a/inc/main.h +++ b/inc/main.h @@ -35,4 +35,6 @@ OF SUCH DAMAGE. #ifndef MAIN_H #define MAIN_H +#define DEBUG_VERBOES + #endif /* MAIN_H */ diff --git a/src/i2c.c b/src/i2c.c index c0958f0..2babf2f 100644 --- a/src/i2c.c +++ b/src/i2c.c @@ -158,7 +158,9 @@ uint8_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]) } else { timeout = 0; state = I2C_START; +#ifdef DEBUG_VERBOES: printf("i2c bus is busy in WRITE BYTE!\n"); +#endif } break; case I2C_SEND_ADDRESS: @@ -173,7 +175,9 @@ uint8_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]) } else { timeout = 0; state = I2C_START; +#ifdef DEBUG_VERBOES: printf("i2c master sends start signal timeout in WRITE BYTE!\n"); +#endif } break; case I2C_CLEAR_ADDRESS_FLAG: @@ -188,7 +192,9 @@ uint8_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]) } else { timeout = 0; state = I2C_START; +#ifdef DEBUG_VERBOES: printf("i2c master clears address flag timeout in WRITE BYTE!\n"); +#endif } break; case I2C_TRANSMIT_DATA: @@ -203,7 +209,9 @@ uint8_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]) } else { timeout = 0; state = I2C_START; +#ifdef DEBUG_VERBOES: printf("i2c master sends data timeout in WRITE BYTE!\n"); +#endif } /* wait until BTC bit is set */ @@ -217,7 +225,9 @@ uint8_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]) } else { timeout = 0; state = I2C_START; +#ifdef DEBUG_VERBOES: printf("i2c master sends MSB data timeout in WRITE BYTE!\n"); +#endif } /* wait until BTC bit is set */ @@ -232,7 +242,9 @@ uint8_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]) } else { timeout = 0; state = I2C_START; +#ifdef DEBUG_VERBOES: printf("i2c master sends LSB data timeout in WRITE BYTE!\n"); +#endif } /* wait until BTC bit is set */ @@ -245,7 +257,9 @@ uint8_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]) } else { timeout = 0; state = I2C_START; +#ifdef DEBUG_VERBOES: printf("i2c master sends data timeout in WRITE BYTE!\n"); +#endif } break; case I2C_STOP: @@ -262,14 +276,18 @@ uint8_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]) } else { timeout = 0; state = I2C_START; +#ifdef DEBUG_VERBOES: printf("i2c master sends stop signal timeout in WRITE BYTE!\n"); +#endif } break; default: state = I2C_START; i2c_timeout_flag = I2C_OK; timeout = 0; +#ifdef DEBUG_VERBOES: printf("i2c master sends start signal in WRITE BYTE.\n"); +#endif break; } } @@ -301,7 +319,9 @@ uint8_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) { // i2c_bus_reset(); timeout = 0; state = I2C_START; +#ifdef DEBUG_VERBOES: printf("i2c bus is busy in READ!\n"); +#endif } } /* send the start signal */ @@ -328,7 +348,9 @@ uint8_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) { timeout = 0; state = I2C_START; read_cycle = RESET; +#ifdef DEBUG_VERBOES: printf("i2c master sends start signal timeout in READ!\n"); +#endif } break; case I2C_CLEAR_ADDRESS_FLAG: @@ -348,7 +370,9 @@ uint8_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) { timeout = 0; state = I2C_START; read_cycle = RESET; +#ifdef DEBUG_VERBOES: printf("i2c master clears address flag timeout in READ!\n"); +#endif } break; case I2C_TRANSMIT_DATA: @@ -365,7 +389,9 @@ uint8_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) { timeout = 0; state = I2C_START; read_cycle = RESET; +#ifdef DEBUG_VERBOES: printf("i2c master wait data buffer is empty timeout in READ!\n"); +#endif } /* wait until BTC bit is set */ while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) { @@ -379,7 +405,9 @@ uint8_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) { timeout = 0; state = I2C_START; read_cycle = RESET; +#ifdef DEBUG_VERBOES: printf("i2c master sends register address timeout in READ!\n"); +#endif } } else { while (number_of_byte) { @@ -404,7 +432,9 @@ uint8_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) { timeout = 0; state = I2C_START; read_cycle = 0; +#ifdef DEBUG_VERBOES: printf("i2c master sends data timeout in READ!\n"); +#endif } } timeout = 0; @@ -424,7 +454,9 @@ uint8_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) { timeout = 0; state = I2C_START; read_cycle = 0; +#ifdef DEBUG_VERBOES: printf("i2c master sends stop signal timeout in READ!\n"); +#endif } break; default: @@ -432,7 +464,9 @@ uint8_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) { read_cycle = 0; i2c_timeout_flag = I2C_OK; timeout = 0; +#ifdef DEBUG_VERBOES: printf("i2c master sends start signal in READ.\n"); +#endif break; } }