generated from hulk/gd32e23x_template
	err
This commit is contained in:
		| @@ -91,9 +91,12 @@ set(TARGET_CFLAGS_HARDWARE "-mcpu=cortex-m23 -mfloat-abi=soft -mthumb -mthumb-in | |||||||
|  |  | ||||||
| # Conditional flags | # Conditional flags | ||||||
| # DEBUG | # DEBUG | ||||||
| set(CMAKE_C_FLAGS_DEBUG     "-DDEBUG=0 -O0 -g") | #set(CMAKE_C_FLAGS_DEBUG     "-DDEBUG=0 -O0 -g") | ||||||
| set(CMAKE_CXX_FLAGS_DEBUG   "-DDEBUG=0 -O0 -g") | #set(CMAKE_CXX_FLAGS_DEBUG   "-DDEBUG=0 -O0 -g") | ||||||
| set(CMAKE_ASM_FLAGS_DEBUG   "-DDEBUG=0 -O0 -g") | #set(CMAKE_ASM_FLAGS_DEBUG   "-DDEBUG=0 -O0 -g") | ||||||
|  | set(CMAKE_C_FLAGS_DEBUG     "-DDEBUG=0 -Os -g") | ||||||
|  | set(CMAKE_CXX_FLAGS_DEBUG   "-DDEBUG=0 -Os -g") | ||||||
|  | set(CMAKE_ASM_FLAGS_DEBUG   "-DDEBUG=0 -Os -g") | ||||||
|  |  | ||||||
| # RELEASE | # RELEASE | ||||||
| set(CMAKE_C_FLAGS_RELEASE        "-DNDEBUG -O3") #  -flto | set(CMAKE_C_FLAGS_RELEASE        "-DNDEBUG -O3") #  -flto | ||||||
|   | |||||||
| @@ -22,7 +22,19 @@ | |||||||
|  |  | ||||||
| #define I2C_TIME_OUT    (uint16_t)(10000) | #define I2C_TIME_OUT    (uint16_t)(10000) | ||||||
| #define LDC_I2C       I2C0 | #define LDC_I2C       I2C0 | ||||||
| #define I2C_SPEED 100000 | #define I2C_SPEED 400000 | ||||||
|  |  | ||||||
|  | typedef enum { | ||||||
|  |     I2C_START = 0, | ||||||
|  |     I2C_SEND_ADDRESS, | ||||||
|  |     I2C_CLEAR_ADDRESS_FLAG, | ||||||
|  |     I2C_TRANSMIT_DATA, | ||||||
|  |     I2C_STOP | ||||||
|  | } i2c_process_enum; | ||||||
|  |  | ||||||
|  | #define I2C_OK          1 | ||||||
|  | #define I2C_FAIL        0 | ||||||
|  | #define I2C_END         1 | ||||||
|  |  | ||||||
| #define RCU_IR_GPIO RCU_GPIOF | #define RCU_IR_GPIO RCU_GPIOF | ||||||
| #define RCU_I2C RCU_I2C0 | #define RCU_I2C RCU_I2C0 | ||||||
| @@ -32,7 +44,7 @@ | |||||||
| #define I2C_SDA_PIN GPIO_PIN_0 | #define I2C_SDA_PIN GPIO_PIN_0 | ||||||
| #define I2C_GPIO_AF GPIO_AF_1 | #define I2C_GPIO_AF GPIO_AF_1 | ||||||
|  |  | ||||||
| #define LDC1612_ADDR         (0x2A << 1) | #define LDC1612_ADDR         (0x2B << 1) | ||||||
|  |  | ||||||
| // LDC1612 寄存器地址 | // LDC1612 寄存器地址 | ||||||
| #define RCOUNT0_ADDR          0x08 | #define RCOUNT0_ADDR          0x08 | ||||||
| @@ -49,6 +61,10 @@ void LDC1612_Init(void); | |||||||
| int LDC1612_read_reg(uint8_t device_address, uint8_t reg_address, uint8_t *data, uint16_t length); | int LDC1612_read_reg(uint8_t device_address, uint8_t reg_address, uint8_t *data, uint16_t length); | ||||||
| int i2c_write_reg(uint8_t device_address, uint8_t *data); | int i2c_write_reg(uint8_t device_address, uint8_t *data); | ||||||
|  |  | ||||||
|  | uint8_t eeprom_byte_write_timeout(uint8_t device_address, uint8_t *data); | ||||||
|  |  | ||||||
| void I2C_Scan(void); | void I2C_Scan(void); | ||||||
|  |  | ||||||
|  | int LDC_getID(void); | ||||||
|  |  | ||||||
| #endif //LDC1612_H | #endif //LDC1612_H | ||||||
|   | |||||||
| @@ -45,9 +45,9 @@ | |||||||
|  |  | ||||||
| /* select a system clock by uncommenting the following line */ | /* select a system clock by uncommenting the following line */ | ||||||
| //#define __SYSTEM_CLOCK_8M_HXTAL              (__HXTAL) | //#define __SYSTEM_CLOCK_8M_HXTAL              (__HXTAL) | ||||||
| //#define __SYSTEM_CLOCK_8M_IRC8M              (__IRC8M) | #define __SYSTEM_CLOCK_8M_IRC8M              (__IRC8M) | ||||||
| // #define __SYSTEM_CLOCK_72M_PLL_HXTAL         (uint32_t)(72000000) | // #define __SYSTEM_CLOCK_72M_PLL_HXTAL         (uint32_t)(72000000) | ||||||
| #define __SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2    (uint32_t)(72000000) | // #define __SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2    (uint32_t)(72000000) | ||||||
|  |  | ||||||
| #define RCU_MODIFY(__delay)     do{                                     \ | #define RCU_MODIFY(__delay)     do{                                     \ | ||||||
|                                     volatile uint32_t i;                \ |                                     volatile uint32_t i;                \ | ||||||
|   | |||||||
							
								
								
									
										289
									
								
								src/LDC1612.c
									
									
									
									
									
								
							
							
						
						
									
										289
									
								
								src/LDC1612.c
									
									
									
									
									
								
							| @@ -17,7 +17,7 @@ void led_config(void) | |||||||
|  |  | ||||||
|     timer_parameter_struct timer_initpara; |     timer_parameter_struct timer_initpara; | ||||||
|     timer_struct_para_init(&timer_initpara); |     timer_struct_para_init(&timer_initpara); | ||||||
|     timer_initpara.prescaler = 7199; |     timer_initpara.prescaler = 799; | ||||||
|     timer_initpara.alignedmode = TIMER_COUNTER_EDGE; |     timer_initpara.alignedmode = TIMER_COUNTER_EDGE; | ||||||
|     timer_initpara.counterdirection = TIMER_COUNTER_UP; |     timer_initpara.counterdirection = TIMER_COUNTER_UP; | ||||||
|     timer_initpara.period = 999; |     timer_initpara.period = 999; | ||||||
| @@ -29,7 +29,7 @@ void led_config(void) | |||||||
|  |  | ||||||
|     timer_enable(LED_TIMER); |     timer_enable(LED_TIMER); | ||||||
|  |  | ||||||
|     nvic_irq_enable(LED_IRQ, 0); |     nvic_irq_enable(LED_IRQ, 2); | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -70,10 +70,11 @@ int i2c_write_reg(uint8_t device_address, uint8_t *data) | |||||||
| { | { | ||||||
|     uint32_t timeout = 0; |     uint32_t timeout = 0; | ||||||
|     printf("debug tag- 0\r\n"); |     printf("debug tag- 0\r\n"); | ||||||
|     // 生成起始条件 |     // 确保 I2C 总线空闲 | ||||||
|     while (i2c_flag_get(LDC_I2C, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) |     while (i2c_flag_get(LDC_I2C, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) | ||||||
|         timeout++; |         timeout++; | ||||||
|     if (timeout >= I2C_TIME_OUT) { |     if (timeout >= I2C_TIME_OUT) { | ||||||
|  |         printf("debug tag- 1-\r\n"); | ||||||
|         return -1; // 超时返回错误 |         return -1; // 超时返回错误 | ||||||
|     } |     } | ||||||
|     i2c_start_on_bus(LDC_I2C); |     i2c_start_on_bus(LDC_I2C); | ||||||
| @@ -91,7 +92,7 @@ int i2c_write_reg(uint8_t device_address, uint8_t *data) | |||||||
|     printf("debug tag- 2\r\n"); |     printf("debug tag- 2\r\n"); | ||||||
|  |  | ||||||
|     // 等待地址发送完成 |     // 等待地址发送完成 | ||||||
|     while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) && (timeout < I2C_TIME_OUT)) |     while (!i2c_flag_get(LDC_I2C, I2C_FLAG_ADDSEND) && (timeout < I2C_TIME_OUT)) | ||||||
|         timeout++; |         timeout++; | ||||||
|     if (timeout >= I2C_TIME_OUT) { |     if (timeout >= I2C_TIME_OUT) { | ||||||
|         printf("debug tag- 3-\r\n"); |         printf("debug tag- 3-\r\n"); | ||||||
| @@ -101,12 +102,6 @@ int i2c_write_reg(uint8_t device_address, uint8_t *data) | |||||||
|     timeout = 0; |     timeout = 0; | ||||||
|     printf("debug tag- 3\r\n"); |     printf("debug tag- 3\r\n"); | ||||||
|  |  | ||||||
|     // 发送寄存器地址 |  | ||||||
|     while (!i2c_flag_get(LDC_I2C, I2C_FLAG_TBE) && (timeout < I2C_TIME_OUT)) |  | ||||||
|         timeout++; |  | ||||||
|     if (timeout >= I2C_TIME_OUT) { |  | ||||||
|         return -1; // 超时返回错误 |  | ||||||
|     } |  | ||||||
|     // 发送寄存器地址和数据 |     // 发送寄存器地址和数据 | ||||||
|     for (int i = 0; i < 3; i++) { |     for (int i = 0; i < 3; i++) { | ||||||
|         while (!i2c_flag_get(LDC_I2C, I2C_FLAG_TBE) && (timeout < I2C_TIME_OUT)) |         while (!i2c_flag_get(LDC_I2C, I2C_FLAG_TBE) && (timeout < I2C_TIME_OUT)) | ||||||
| @@ -129,16 +124,154 @@ int i2c_write_reg(uint8_t device_address, uint8_t *data) | |||||||
|     i2c_stop_on_bus(LDC_I2C); |     i2c_stop_on_bus(LDC_I2C); | ||||||
|     timeout = 0; |     timeout = 0; | ||||||
|  |  | ||||||
|     while (i2c_flag_get(LDC_I2C, I2C_FLAG_STPDET) && (timeout < I2C_TIME_OUT)) |     // 确保停止条件已发送完成 | ||||||
|  |     while (i2c_flag_get(LDC_I2C, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) | ||||||
|         timeout++; |         timeout++; | ||||||
|     if (timeout >= I2C_TIME_OUT) { |     if (timeout >= I2C_TIME_OUT) { | ||||||
|         return -1; // 超时返回错误 |         return -1; // 超时返回错误 | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     printf("test info"); |     printf("test info\r\n"); | ||||||
|     return 0; |     return 0; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | uint8_t eeprom_byte_write_timeout(uint8_t device_address, uint8_t *data) | ||||||
|  | { | ||||||
|  |     uint8_t state = I2C_START; | ||||||
|  |     uint16_t timeout = 0; | ||||||
|  |     uint8_t i2c_timeout_flag = 0; | ||||||
|  |  | ||||||
|  |     /* enable acknowledge */ | ||||||
|  |     i2c_ack_config(LDC_I2C, I2C_ACK_ENABLE); | ||||||
|  |     while(!(i2c_timeout_flag)) { | ||||||
|  |         switch(state) { | ||||||
|  |         case I2C_START: | ||||||
|  |             /* i2c master sends start signal only when the bus is idle */ | ||||||
|  |             while(i2c_flag_get(LDC_I2C, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) { | ||||||
|  |                 timeout++; | ||||||
|  |             } | ||||||
|  |             if(timeout < I2C_TIME_OUT) { | ||||||
|  |                 i2c_start_on_bus(LDC_I2C); | ||||||
|  |                 timeout = 0; | ||||||
|  |                 state = I2C_SEND_ADDRESS; | ||||||
|  |             } else { | ||||||
|  |                 timeout = 0; | ||||||
|  |                 state   = I2C_START; | ||||||
|  |                 printf("i2c bus is busy in WRITE BYTE!\n"); | ||||||
|  |             } | ||||||
|  |             break; | ||||||
|  |         case I2C_SEND_ADDRESS: | ||||||
|  |             /* i2c master sends START signal successfully */ | ||||||
|  |             while((!i2c_flag_get(LDC_I2C, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) { | ||||||
|  |                 timeout++; | ||||||
|  |             } | ||||||
|  |             if(timeout < I2C_TIME_OUT) { | ||||||
|  |                 i2c_master_addressing(LDC_I2C, device_address, I2C_TRANSMITTER); | ||||||
|  |                 timeout = 0; | ||||||
|  |                 state = I2C_CLEAR_ADDRESS_FLAG; | ||||||
|  |             } else { | ||||||
|  |                 timeout = 0; | ||||||
|  |                 state = I2C_START; | ||||||
|  |                 printf("i2c master sends start signal timeout in WRITE BYTE!\n"); | ||||||
|  |             } | ||||||
|  |             break; | ||||||
|  |         case I2C_CLEAR_ADDRESS_FLAG: | ||||||
|  |             /* address flag set means i2c slave sends ACK */ | ||||||
|  |             while((!i2c_flag_get(LDC_I2C, I2C_FLAG_ADDSEND)) && (timeout < I2C_TIME_OUT)) { | ||||||
|  |                 timeout++; | ||||||
|  |             } | ||||||
|  |             if(timeout < I2C_TIME_OUT) { | ||||||
|  |                 i2c_flag_clear(LDC_I2C, I2C_FLAG_ADDSEND); | ||||||
|  |                 timeout = 0; | ||||||
|  |                 state = I2C_TRANSMIT_DATA; | ||||||
|  |             } else { | ||||||
|  |                 timeout = 0; | ||||||
|  |                 state = I2C_START; | ||||||
|  |                 printf("i2c master clears address flag timeout in WRITE BYTE!\n"); | ||||||
|  |             } | ||||||
|  |             break; | ||||||
|  |         case I2C_TRANSMIT_DATA: | ||||||
|  |             /* wait until the transmit data buffer is empty */ | ||||||
|  |             while((!i2c_flag_get(LDC_I2C, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) { | ||||||
|  |                 timeout++; | ||||||
|  |             } | ||||||
|  |             if(timeout < I2C_TIME_OUT) { | ||||||
|  |                 /* send the EEPROM's internal address to write to : only one byte address */ | ||||||
|  |                 i2c_data_transmit(LDC_I2C, data[0]); | ||||||
|  |                 timeout = 0; | ||||||
|  |             } else { | ||||||
|  |                 timeout = 0; | ||||||
|  |                 state = I2C_START; | ||||||
|  |                 printf("i2c master sends data timeout in WRITE BYTE!\n"); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             /* wait until BTC bit is set */ | ||||||
|  |             while((!i2c_flag_get(LDC_I2C, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) { | ||||||
|  |                 timeout++; | ||||||
|  |             } | ||||||
|  |             if(timeout < I2C_TIME_OUT) { | ||||||
|  |                 /* send the EEPROM's internal address to write to : only one byte address */ | ||||||
|  |                 i2c_data_transmit(LDC_I2C, data[1]); | ||||||
|  |                 timeout = 0; | ||||||
|  |             } else { | ||||||
|  |                 timeout = 0; | ||||||
|  |                 state = I2C_START; | ||||||
|  |                 printf("i2c master sends data timeout in WRITE 1 BYTE!\n"); | ||||||
|  |             } | ||||||
|  |             /* wait until BTC bit is set */ | ||||||
|  |             while((!i2c_flag_get(LDC_I2C, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) { | ||||||
|  |                 timeout++; | ||||||
|  |             } | ||||||
|  |             if(timeout < I2C_TIME_OUT) { | ||||||
|  |                 /* send the EEPROM's internal address to write to : only one byte address */ | ||||||
|  |                 i2c_data_transmit(LDC_I2C, data[2]); | ||||||
|  |                 timeout = 0; | ||||||
|  |             } else { | ||||||
|  |                 timeout = 0; | ||||||
|  |                 state = I2C_START; | ||||||
|  |                 printf("i2c master sends data timeout in WRITE 2 BYTE!\n"); | ||||||
|  |             } | ||||||
|  |             /* wait until BTC bit is set */ | ||||||
|  |             while((!i2c_flag_get(LDC_I2C, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) { | ||||||
|  |                 timeout++; | ||||||
|  |             } | ||||||
|  |             if(timeout < I2C_TIME_OUT) { | ||||||
|  |                 state = I2C_STOP; | ||||||
|  |                 timeout = 0; | ||||||
|  |             } else { | ||||||
|  |                 timeout = 0; | ||||||
|  |                 state = I2C_START; | ||||||
|  |                 printf("i2c master sends data timeout in WRITE BYTE!\n"); | ||||||
|  |             } | ||||||
|  |             break; | ||||||
|  |         case I2C_STOP: | ||||||
|  |             /* send a stop condition to I2C bus */ | ||||||
|  |             i2c_stop_on_bus(LDC_I2C); | ||||||
|  |             /* i2c master sends STOP signal successfully */ | ||||||
|  |             while((I2C_CTL0(LDC_I2C) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) { | ||||||
|  |                 timeout++; | ||||||
|  |             } | ||||||
|  |             if(timeout < I2C_TIME_OUT) { | ||||||
|  |                 timeout = 0; | ||||||
|  |                 state = I2C_END; | ||||||
|  |                 i2c_timeout_flag = I2C_OK; | ||||||
|  |             } else { | ||||||
|  |                 timeout = 0; | ||||||
|  |                 state = I2C_START; | ||||||
|  |                 printf("i2c master sends stop signal timeout in WRITE BYTE!\n"); | ||||||
|  |             } | ||||||
|  |             break; | ||||||
|  |         default: | ||||||
|  |             state = I2C_START; | ||||||
|  |             i2c_timeout_flag = I2C_OK; | ||||||
|  |             timeout = 0; | ||||||
|  |             printf("i2c master sends start signal in WRITE BYTE.\n"); | ||||||
|  |             break; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     return I2C_END; | ||||||
|  | } | ||||||
|  |  | ||||||
| void LDC1612_Init(void) { | void LDC1612_Init(void) { | ||||||
|     uint8_t RCOUNT0_ALL[3]={RCOUNT0_ADDR,0x05,0x36};//csdn |     uint8_t RCOUNT0_ALL[3]={RCOUNT0_ADDR,0x05,0x36};//csdn | ||||||
|     uint8_t SETTLECOUNT0_ALL[3]={SETTLECOUNT0_ADDR,0x00,0x0a}; |     uint8_t SETTLECOUNT0_ALL[3]={SETTLECOUNT0_ADDR,0x00,0x0a}; | ||||||
| @@ -148,18 +281,25 @@ void LDC1612_Init(void) { | |||||||
|     uint8_t DRIVE_CURRENT0_ALL[3]={DRIVE_CURRENT0_ADDR,0x90,0x00}; |     uint8_t DRIVE_CURRENT0_ALL[3]={DRIVE_CURRENT0_ADDR,0x90,0x00}; | ||||||
|     uint8_t CONFIG_ALL[3]={CONFIG_ADDR,0x14,0x01};//csdn |     uint8_t CONFIG_ALL[3]={CONFIG_ADDR,0x14,0x01};//csdn | ||||||
|  |  | ||||||
|     LDC1612_I2CConfig(); |     // LDC1612_I2CConfig(); | ||||||
|  |  | ||||||
|     int ret = i2c_write_reg(LDC1612_ADDR, RCOUNT0_ALL); |     // int ret = i2c_write_reg(LDC1612_ADDR, RCOUNT0_ALL); | ||||||
|  |     // | ||||||
|     printf("ret:%d\r\n",ret); |     // printf("ret:%d\r\n",ret); | ||||||
|     // i2c_write_reg(LDC1612_ADDR, RCOUNT0_ALL); |     i2c_write_reg(LDC1612_ADDR, RCOUNT0_ALL); | ||||||
| //     i2c_write_reg(LDC1612_ADDR, SETTLECOUNT0_ALL); |     // eeprom_byte_write_timeout(LDC1612_ADDR, SETTLECOUNT0_ALL); | ||||||
| //     i2c_write_reg(LDC1612_ADDR, CLOCK_DIVIDERS0_ALL); |     delay_ms(100); | ||||||
| //     i2c_write_reg(LDC1612_ADDR, ERROR_CONFIG_ALL); |     i2c_write_reg(LDC1612_ADDR, SETTLECOUNT0_ALL); | ||||||
| //     i2c_write_reg(LDC1612_ADDR, MUX_CONFIG_ALL); |     delay_ms(100); | ||||||
| //     i2c_write_reg(LDC1612_ADDR, DRIVE_CURRENT0_ALL); |     // i2c_write_reg(LDC1612_ADDR, CLOCK_DIVIDERS0_ALL); | ||||||
| //     i2c_write_reg(LDC1612_ADDR, CONFIG_ALL); |     // delay_ms(100); | ||||||
|  |     // i2c_write_reg(LDC1612_ADDR, ERROR_CONFIG_ALL); | ||||||
|  |     // delay_ms(100); | ||||||
|  |     // i2c_write_reg(LDC1612_ADDR, MUX_CONFIG_ALL); | ||||||
|  |     // delay_ms(100); | ||||||
|  |     // i2c_write_reg(LDC1612_ADDR, DRIVE_CURRENT0_ALL); | ||||||
|  |     // delay_ms(100); | ||||||
|  |     // i2c_write_reg(LDC1612_ADDR, CONFIG_ALL); | ||||||
| } | } | ||||||
|  |  | ||||||
| /** | /** | ||||||
| @@ -313,3 +453,106 @@ void I2C_Scan(void) { | |||||||
|         printf("Total %d I2C devices found.\r\n", found_devices); |         printf("Total %d I2C devices found.\r\n", found_devices); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | int LDC_getID(void) { | ||||||
|  |     uint8_t data[2] = {0}; | ||||||
|  |     uint16_t timeout = 0; | ||||||
|  |  | ||||||
|  |     i2c_ack_config(I2C0, I2C_ACK_ENABLE); | ||||||
|  |  | ||||||
|  |     while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) //判断IIC总线是否忙,发送起始信号 | ||||||
|  |         timeout++; | ||||||
|  |     if (timeout < I2C_TIME_OUT) { | ||||||
|  |         i2c_start_on_bus(I2C0); | ||||||
|  |         timeout = 0; | ||||||
|  |     } else { | ||||||
|  |         printf("err\r\n"); | ||||||
|  |         return -4101; // 超时返回错误 | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND) && (timeout < I2C_TIME_OUT)) //判断起始位是否发送,设置sensor地址并设置为写 | ||||||
|  |         timeout++; | ||||||
|  |     if (timeout < I2C_TIME_OUT) { | ||||||
|  |         i2c_master_addressing(I2C0, (0x2B << 1), I2C_TRANSMITTER); | ||||||
|  |         timeout = 0; | ||||||
|  |     } else { | ||||||
|  |         return -4102; // 超时返回错误 | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) && (timeout < I2C_TIME_OUT)) | ||||||
|  |         timeout++; | ||||||
|  |     if (timeout < I2C_TIME_OUT) { | ||||||
|  |         i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND); | ||||||
|  |         timeout = 0; | ||||||
|  |     } else { | ||||||
|  |         return -4103; // 超时返回错误 | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     while (!i2c_flag_get(I2C0, I2C_FLAG_TBE) && (timeout < I2C_TIME_OUT)) //判断地址是否发送完成,然后发送寄存器地址 | ||||||
|  |         timeout++; | ||||||
|  |     if (timeout < I2C_TIME_OUT) { | ||||||
|  |         i2c_data_transmit(I2C0, 0x7f); | ||||||
|  |         timeout = 0; | ||||||
|  |         // i2c_start_on_bus(IR_I2C); | ||||||
|  |     } else { | ||||||
|  |         return -4104; // 超时返回错误 | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     while (i2c_flag_get(I2C0, I2C_FLAG_BTC) && (timeout < I2C_TIME_OUT)) //判断发送缓冲器是否为空,为空后(发送完毕)重新发送开始信号 | ||||||
|  |         timeout++; | ||||||
|  |     if (timeout < I2C_TIME_OUT) { | ||||||
|  |         i2c_start_on_bus(I2C0); | ||||||
|  |         timeout = 0; | ||||||
|  |     } else { | ||||||
|  |         return -4105; // 超时返回错误 | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND) && (timeout < I2C_TIME_OUT)) { | ||||||
|  |         timeout++; | ||||||
|  |     } | ||||||
|  |     if (timeout < I2C_TIME_OUT) { | ||||||
|  |         i2c_master_addressing(I2C0, (0x2B << 1), I2C_RECEIVER); | ||||||
|  |         timeout = 0; | ||||||
|  |     } else { | ||||||
|  |         return -4106; // 超时返回错误 | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) && (timeout < I2C_TIME_OUT)) | ||||||
|  |         timeout++; | ||||||
|  |     if (timeout < I2C_TIME_OUT) { | ||||||
|  |         i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND); | ||||||
|  |         timeout = 0; | ||||||
|  |     } else { | ||||||
|  |         return -4107; // 超时返回错误 | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     // 读取第一个字节的数据 | ||||||
|  |     while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE) && (timeout < I2C_TIME_OUT)) { | ||||||
|  |         timeout++; | ||||||
|  |     } | ||||||
|  |     if (timeout < I2C_TIME_OUT) { | ||||||
|  |         data[0] = i2c_data_receive(I2C0); | ||||||
|  |         timeout = 0; | ||||||
|  |     } else { | ||||||
|  |         return -4108; // 超时返回错误 | ||||||
|  |     } | ||||||
|  |     i2c_ack_config(I2C0, I2C_ACK_DISABLE); | ||||||
|  |  | ||||||
|  |     // 读取第二个字节的数据 | ||||||
|  |     while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE) && (timeout < I2C_TIME_OUT)) { | ||||||
|  |         timeout++; | ||||||
|  |     } | ||||||
|  |     if (timeout < I2C_TIME_OUT) { | ||||||
|  |         data[1] = i2c_data_receive(I2C0); | ||||||
|  |         timeout = 0; | ||||||
|  |     } else { | ||||||
|  |         return -4109; // 超时返回错误 | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     i2c_stop_on_bus(I2C0); | ||||||
|  |  | ||||||
|  |     printf("device id:%x %x\r\n",data[0],data[1]); | ||||||
|  |  | ||||||
|  |     return 0; | ||||||
|  | } | ||||||
| @@ -4,12 +4,6 @@ | |||||||
|  |  | ||||||
| #include "RS485.h" | #include "RS485.h" | ||||||
|  |  | ||||||
| #define MAX_CMD_SIZE 16 |  | ||||||
| #define BUFSIZE 8 |  | ||||||
|  |  | ||||||
| extern uint16_t g_distance_uint16; |  | ||||||
| extern uint16_t g_temperature_uint16; |  | ||||||
|  |  | ||||||
| void RS485_config(void) { | void RS485_config(void) { | ||||||
|     rcu_periph_clock_enable(RS485_GPIO_RCU); |     rcu_periph_clock_enable(RS485_GPIO_RCU); | ||||||
|     rcu_periph_clock_enable(RS485_RCU); |     rcu_periph_clock_enable(RS485_RCU); | ||||||
| @@ -40,10 +34,8 @@ void RS485_config(void) { | |||||||
| void process_command(char *cmd) { | void process_command(char *cmd) { | ||||||
|     if (strncmp(cmd, "M1", 2) == 0) { |     if (strncmp(cmd, "M1", 2) == 0) { | ||||||
|         printf("M1 -=-=- OK!\r\n"); |         printf("M1 -=-=- OK!\r\n"); | ||||||
|         printf("Distance: %d\r\n", g_distance_uint16); |  | ||||||
|     } else if (strncmp(cmd, "M2", 2) == 0) { |     } else if (strncmp(cmd, "M2", 2) == 0) { | ||||||
|         printf("M2 -=-=- OK!\r\n"); |         printf("M2 -=-=- OK!\r\n"); | ||||||
|         printf("Temperature: %d\r\n", g_temperature_uint16); |  | ||||||
|         // } else if (strncmp(cmd, "M3", 2) == 0) { |         // } else if (strncmp(cmd, "M3", 2) == 0) { | ||||||
|         //     char *param_str = cmd + 2; // Skip "M3" |         //     char *param_str = cmd + 2; // Skip "M3" | ||||||
|         //     int param = atoi(param_str + 1); // Skip "S" and convert to integer |         //     int param = atoi(param_str + 1); // Skip "S" and convert to integer | ||||||
|   | |||||||
							
								
								
									
										17
									
								
								src/main.c
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								src/main.c
									
									
									
									
									
								
							| @@ -35,19 +35,24 @@ int main(void) | |||||||
|  |  | ||||||
|     LDC1612_I2CConfig(); |     LDC1612_I2CConfig(); | ||||||
|     // LDC1612_Init(); |     // LDC1612_Init(); | ||||||
|     I2C_Scan(); |     // I2C_Scan(); | ||||||
|  | int ret = 0; | ||||||
|  |     ret = LDC_getID(); | ||||||
|  |     printf("ret:%d\r\n",ret); | ||||||
|  |  | ||||||
|  |  | ||||||
|  |     uint8_t data[2] = {0}; | ||||||
|     uint8_t data[8] = {0}; |  | ||||||
|     uint32_t value = 0; |     uint32_t value = 0; | ||||||
|  |  | ||||||
|     while(1){ |     while(1){ | ||||||
|  |         // LDC1612_read_reg(LDC1612_ADDR, 0x7f, data, 2); | ||||||
|         // LDC1612_read_reg(LDC1612_ADDR, 0x00, data, 2); |         // printf("msb:%x %x\r\n",data[0],data[1]); | ||||||
|  |         // LDC1612_read_reg(LDC1612_ADDR, 0x01, data, 2); | ||||||
|     // printf("msb:%x %x\r\n",data[0],data[1]); |     // printf("msb:%x %x\r\n",data[0],data[1]); | ||||||
|     //     printf("hello world!\r\n"); |     //     printf("hello world!\r\n"); | ||||||
|     delay_ms(5000); |         // LDC1612_Init(); | ||||||
|  |         // LDC1612_read_reg(LDC1612_ADDR, 0x01, data, 2); | ||||||
|  |     delay_ms(1000); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user