generated from hulk/gd32e23x_template
	有问题
This commit is contained in:
		
							
								
								
									
										10
									
								
								inc/rs485.h
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								inc/rs485.h
									
									
									
									
									
								
							| @@ -24,7 +24,7 @@ | ||||
|  | ||||
| #define RS485_EN_PIN         GPIO_PIN_1 | ||||
|  | ||||
| #define RX_BUFFER_SIZE 64 | ||||
| #define RX_BUFFER_SIZE 32 | ||||
|  | ||||
| typedef enum { | ||||
|     VALIDATION_SUCCESS = 0, | ||||
| @@ -32,11 +32,17 @@ typedef enum { | ||||
| } validation_result_t; | ||||
|  | ||||
| void rs485_config(void); | ||||
|  | ||||
| void process_command(uint8_t *cmd, size_t length); | ||||
|  | ||||
| uint8_t calculate_crc(uint8_t data[], size_t data_length); | ||||
| validation_result_t validate_package_crc(uint8_t *data, size_t data_length); | ||||
| validation_result_t validate_package_header(uint8_t *data); | ||||
|  | ||||
| validation_result_t validate_package_type(uint8_t *data); | ||||
|  | ||||
| void eddy_current_value_report(void); | ||||
|  | ||||
| void tempture_value_report(void); | ||||
|  | ||||
| #endif //RS485_H | ||||
| #endif //RS485_H | ||||
|   | ||||
| @@ -121,7 +121,7 @@ void USART0_IRQHandler(void) { | ||||
|     static uint8_t rx_index = 0; | ||||
|     static uint8_t rx_buffer[RX_BUFFER_SIZE]; | ||||
|  | ||||
|     if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) { | ||||
|     if (usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE) != RESET) { | ||||
|         usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE); | ||||
|         uint8_t received_data = (uint8_t) usart_data_receive(USART0); | ||||
|  | ||||
| @@ -129,25 +129,23 @@ void USART0_IRQHandler(void) { | ||||
|         if (rx_index < RX_BUFFER_SIZE - 1) { | ||||
|             rx_buffer[rx_index++] = received_data; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|         // printf("len: %d, data: %c\r\n", rx_index, received_data); | ||||
|  | ||||
|         // 检查是否接收到换行符,表示指令结束 | ||||
|         // if (received_data == '\n') { | ||||
|             if (calculate_crc(rx_buffer, rx_index - 1) == rx_buffer[rx_index - 1]) { | ||||
|                 // printf("CRC check success"); | ||||
|                 // rx_buffer[rx_index] = '\0'; // 添加字符串结束符 | ||||
|                 process_command(rx_buffer, rx_index); // 处理指令 | ||||
|                 rx_index = 0; // 重置缓冲区索引 | ||||
|                 // printf("%c", 0xff); | ||||
|             } else { | ||||
|                 // printf("CRC check failed"); | ||||
|                 // printf("%c%c%c%c", 0xB5, 0xF2, 0x00, 0xF2); | ||||
|                 // fflush(stdout); | ||||
|                 rx_index = 0; // 重置缓冲区索引 | ||||
|                 return; | ||||
|             } | ||||
|  | ||||
|     if (usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE) != RESET) { | ||||
|         usart_interrupt_flag_clear(USART0, USART_INT_FLAG_IDLE); | ||||
|         // printf("%c%c%c%c%c%c", 0xB5, 0xF0, 0x02, 0x6F, 0x6B, 0xCC); | ||||
|         // printf("%d", rx_index); | ||||
|         // printf("%c", rx_buffer[0]); | ||||
|         // uint8_t crc = calculate_crc(rx_buffer, rx_index - 2); | ||||
|         // printf("%d", crc); | ||||
|         // if (calculate_crc(rx_buffer, rx_index - 1) == rx_buffer[rx_index - 1]) | ||||
|         //     // printf("CRC check success"); | ||||
|         //     // rx_buffer[rx_index] = '\0'; // 添加字符串结束符 | ||||
|         process_command(rx_buffer, rx_index); // 处理指令 | ||||
|         // } else { | ||||
|         //     // printf("CRC check failed"); | ||||
|         //     printf("%c%c%c%c", 0xB5, 0xF2, 0x00, 0xF2); | ||||
|         //     // fflush(stdout); | ||||
|         // } | ||||
|     } | ||||
| } | ||||
|   | ||||
							
								
								
									
										34
									
								
								src/i2c.c
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								src/i2c.c
									
									
									
									
									
								
							| @@ -158,7 +158,7 @@ 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: | ||||
| #ifdef DEBUG_VERBOES | ||||
|                     printf("i2c bus is busy in WRITE BYTE!\n"); | ||||
| #endif | ||||
|                 } | ||||
| @@ -175,7 +175,7 @@ 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: | ||||
| #ifdef DEBUG_VERBOES | ||||
|                     printf("i2c master sends start signal timeout in WRITE BYTE!\n"); | ||||
| #endif | ||||
|                 } | ||||
| @@ -192,7 +192,7 @@ 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: | ||||
| #ifdef DEBUG_VERBOES | ||||
|                     printf("i2c master clears address flag timeout in WRITE BYTE!\n"); | ||||
| #endif | ||||
|                 } | ||||
| @@ -209,7 +209,7 @@ 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: | ||||
| #ifdef DEBUG_VERBOES | ||||
|                     printf("i2c master sends data timeout in WRITE BYTE!\n"); | ||||
| #endif | ||||
|                 } | ||||
| @@ -225,7 +225,7 @@ 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: | ||||
| #ifdef DEBUG_VERBOES | ||||
|                     printf("i2c master sends MSB data timeout in WRITE BYTE!\n"); | ||||
| #endif | ||||
|                 } | ||||
| @@ -242,7 +242,7 @@ 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: | ||||
| #ifdef DEBUG_VERBOES | ||||
|                     printf("i2c master sends LSB data timeout in WRITE BYTE!\n"); | ||||
| #endif | ||||
|                 } | ||||
| @@ -257,7 +257,7 @@ 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: | ||||
| #ifdef DEBUG_VERBOES | ||||
|                     printf("i2c master sends data timeout in WRITE BYTE!\n"); | ||||
| #endif | ||||
|                 } | ||||
| @@ -276,7 +276,7 @@ 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: | ||||
| #ifdef DEBUG_VERBOES | ||||
|                     printf("i2c master sends stop signal timeout in WRITE BYTE!\n"); | ||||
| #endif | ||||
|                 } | ||||
| @@ -285,7 +285,7 @@ uint8_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]) | ||||
|                 state = I2C_START; | ||||
|                 i2c_timeout_flag = I2C_OK; | ||||
|                 timeout = 0; | ||||
| #ifdef DEBUG_VERBOES: | ||||
| #ifdef DEBUG_VERBOES | ||||
|                 printf("i2c master sends start signal in WRITE BYTE.\n"); | ||||
| #endif | ||||
|                 break; | ||||
| @@ -319,7 +319,7 @@ 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: | ||||
| #ifdef DEBUG_VERBOES | ||||
|                         printf("i2c bus is busy in READ!\n"); | ||||
| #endif | ||||
|                     } | ||||
| @@ -348,7 +348,7 @@ 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: | ||||
| #ifdef DEBUG_VERBOES | ||||
|                     printf("i2c master sends start signal timeout in READ!\n"); | ||||
| #endif | ||||
|                 } | ||||
| @@ -370,7 +370,7 @@ 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: | ||||
| #ifdef DEBUG_VERBOES | ||||
|                     printf("i2c master clears address flag timeout in READ!\n"); | ||||
| #endif | ||||
|                 } | ||||
| @@ -389,7 +389,7 @@ 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: | ||||
| #ifdef DEBUG_VERBOES | ||||
|                         printf("i2c master wait data buffer is empty timeout in READ!\n"); | ||||
| #endif | ||||
|                     } | ||||
| @@ -405,7 +405,7 @@ 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: | ||||
| #ifdef DEBUG_VERBOES | ||||
|                         printf("i2c master sends register address timeout in READ!\n"); | ||||
| #endif | ||||
|                     } | ||||
| @@ -432,7 +432,7 @@ 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: | ||||
| #ifdef DEBUG_VERBOES | ||||
|                             printf("i2c master sends data timeout in READ!\n"); | ||||
| #endif | ||||
|                         } | ||||
| @@ -454,7 +454,7 @@ 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: | ||||
| #ifdef DEBUG_VERBOES | ||||
|                     printf("i2c master sends stop signal timeout in READ!\n"); | ||||
| #endif | ||||
|                 } | ||||
| @@ -464,7 +464,7 @@ 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: | ||||
| #ifdef DEBUG_VERBOES | ||||
|                 printf("i2c master sends start signal in READ.\n"); | ||||
| #endif | ||||
|                 break; | ||||
|   | ||||
| @@ -36,7 +36,7 @@ int main(void) { | ||||
|     i2c_gpio_config(); | ||||
|     i2c_config(); | ||||
|  | ||||
|     ldc1612_iic_get_sensor_infomation(); | ||||
|     // ldc1612_iic_get_sensor_infomation(); | ||||
|     /* configure LDC1612 */ | ||||
|     ldc1612_single_ch0_config(); | ||||
|  | ||||
| @@ -46,6 +46,7 @@ int main(void) { | ||||
|         g_temperature_uint32 = 0; | ||||
|         g_eddy_current_value_uint32 = ldc1612_get_raw_channel_result(CHANNEL_0); | ||||
|         g_temperature_uint32 = tmp112a_get_raw_channel_result(); | ||||
|         // printf("1"); | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
							
								
								
									
										65
									
								
								src/rs485.c
									
									
									
									
									
								
							
							
						
						
									
										65
									
								
								src/rs485.c
									
									
									
									
									
								
							| @@ -33,52 +33,69 @@ void rs485_config(void) { | ||||
|  | ||||
|     usart_enable(RS485_PHY); | ||||
|  | ||||
|     nvic_irq_enable(USART0_IRQn, 0); | ||||
|     usart_interrupt_enable(RS485_PHY, USART_INT_RBNE); | ||||
|     usart_interrupt_enable(RS485_PHY, USART_INT_IDLE); | ||||
|  | ||||
|     nvic_irq_enable(USART0_IRQn, 0); | ||||
| } | ||||
|  | ||||
| void process_command(uint8_t *cmd, size_t length) { | ||||
|     char combined_str[3]; | ||||
|  | ||||
|     if (cmd[0] == 0xD5) { | ||||
|         if (cmd[1] == 0x03) { | ||||
|             if (cmd[2] == 0x02) { | ||||
|                 sprintf(combined_str, "%c%c", cmd[3], cmd[4]); | ||||
|                 if (strcmp(combined_str, "M1") == 0) { | ||||
|     // printf("%d", length); | ||||
|  | ||||
|                     eddy_current_value_report(); | ||||
|                 } else if (strcmp(combined_str, "M2") == 0) { | ||||
|                     tempture_value_report(); | ||||
|                 } else { | ||||
|                     printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C); | ||||
|                     return; | ||||
|                 } | ||||
|             } else { | ||||
|                 printf("%c%c%c%c", 0xB5, 0xF1, 0x00, 0xF1); | ||||
|             } | ||||
|         } else { | ||||
|             printf("%c%c%c%c", 0xB5, 0xF5, 0x00, 0xF5); | ||||
|         } | ||||
|     // validate_package_crc(cmd, length); | ||||
|     // if (calculate_crc(cmd, length - 1) != cmd[length - 1]) { | ||||
|     //     printf("%c%c%c%c", 0xB5, 0xF3, 0x00, 0xF3); | ||||
|     // } | ||||
|  | ||||
|     // if (cmd[0] == 0xD5) { | ||||
|     //     if (cmd[1] == 0x03) { | ||||
|     //         if (cmd[2] == 0x02) { | ||||
|     sprintf(combined_str, "%c%c", cmd[3], cmd[4]); | ||||
|     if (strcmp(combined_str, "M1") == 0) { | ||||
|         eddy_current_value_report(); | ||||
|     } else if (strcmp(combined_str, "M2") == 0) { | ||||
|         tempture_value_report(); | ||||
|     } else { | ||||
|         printf("%c%c%c%c", 0xB5, 0xF3, 0x00, 0xF3); | ||||
|         printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C); | ||||
|         // return; | ||||
|     } | ||||
|     //         } else { | ||||
|     //             printf("%c%c%c%c", 0xB5, 0xF1, 0x00, 0xF1); | ||||
|     //         } | ||||
|     //     } else { | ||||
|     //         printf("%c%c%c%c", 0xB5, 0xF5, 0x00, 0xF5); | ||||
|     //     } | ||||
|     // } | ||||
| } | ||||
|  | ||||
| uint8_t calculate_crc(uint8_t data[], size_t data_length) { | ||||
|     uint8_t crc = 0; | ||||
|  | ||||
|     for (size_t i = 1; i < data_length; i++) { | ||||
|     for (size_t i = 1; i < data_length - 1; i++) { | ||||
|         crc += data[i]; | ||||
|     } | ||||
|  | ||||
|     return (uint8_t)(crc & 0xFF); | ||||
|     return (uint8_t) (crc & 0xFF); | ||||
| } | ||||
|  | ||||
| validation_result_t validate_package_crc(uint8_t *data, size_t data_length) { | ||||
|     uint8_t crc = calculate_crc(data, data_length - 2); | ||||
|  | ||||
|     if (crc == data[data_length]) { | ||||
|         return VALIDATION_SUCCESS; | ||||
|     } else { | ||||
|         printf("%c%c%c%c", 0xB5, 0xF3, 0x01, 0xF3); | ||||
|         return VALIDATION_CRC_ERROR; | ||||
|     } | ||||
| } | ||||
|  | ||||
| validation_result_t validate_package_header(uint8_t *data) { | ||||
|     if (data[0] == 0xD5) { | ||||
|         return VALIDATION_SUCCESS; | ||||
|     } else { | ||||
|         printf("%c%c%c%c", 0xB5, 0xF3, 0x00, 0xF3); | ||||
|         printf("%c%c%c%c", 0xB5, 0xF3, 0x02, 0xF3); | ||||
|         return VALIDATION_CRC_ERROR; | ||||
|     } | ||||
| } | ||||
| @@ -87,7 +104,7 @@ validation_result_t validate_package_type(uint8_t *data) { | ||||
|     if (data[1] == 0x03) { | ||||
|         return VALIDATION_SUCCESS; | ||||
|     } else { | ||||
|         printf("%c%c%c%c", 0xB5, 0xF3, 0x00, 0xF3); | ||||
|         printf("%c%c%c%c", 0xB5, 0xF3, 0x03, 0xF3); | ||||
|         return VALIDATION_CRC_ERROR; | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user