generated from hulk/gd32e23x_template
	Compare commits
	
		
			5 Commits
		
	
	
		
			444804efe4
			...
			研发稳定版本
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| b94a430ba2 | |||
| 8cd24132a6 | |||
| e2e17d870c | |||
| d359ab5320 | |||
| 0ebe9c5d16 | 
@@ -8,6 +8,8 @@ set(VERSION_MINOR 2)
 | 
				
			|||||||
set(VERSION_PATCH 0)
 | 
					set(VERSION_PATCH 0)
 | 
				
			||||||
set(VERSION "V${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
 | 
					set(VERSION "V${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
 | 
				
			||||||
string(TIMESTAMP CURRENT_DATE "%Y-%m-%d")
 | 
					string(TIMESTAMP CURRENT_DATE "%Y-%m-%d")
 | 
				
			||||||
 | 
					set(IIC_TYPE "SW-IIC")
 | 
				
			||||||
 | 
					#set(IIC_TYPE "HW-IIC")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
enable_language(C)
 | 
					enable_language(C)
 | 
				
			||||||
enable_language(CXX)
 | 
					enable_language(CXX)
 | 
				
			||||||
@@ -30,6 +32,7 @@ set(TARGET_C_SRC
 | 
				
			|||||||
        ${CMAKE_SOURCE_DIR}/src/led.c
 | 
					        ${CMAKE_SOURCE_DIR}/src/led.c
 | 
				
			||||||
        ${CMAKE_SOURCE_DIR}/src/i2c.c
 | 
					        ${CMAKE_SOURCE_DIR}/src/i2c.c
 | 
				
			||||||
        ${CMAKE_SOURCE_DIR}/src/soft_i2c.c
 | 
					        ${CMAKE_SOURCE_DIR}/src/soft_i2c.c
 | 
				
			||||||
 | 
					        ${CMAKE_SOURCE_DIR}/src/fwdgt.c
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
add_executable(xlsw_3dp_LDC1612 ${TARGET_C_SRC})
 | 
					add_executable(xlsw_3dp_LDC1612 ${TARGET_C_SRC})
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,4 +6,4 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
## 电涡流传感器模块通信协议
 | 
					## 电涡流传感器模块通信协议
 | 
				
			||||||
 | 
					
 | 
				
			||||||
通信协议:[LDC1612通信协议]{./CommunicationProtocol.md}
 | 
					通信协议:[LDC1612通信协议](CommunicationProtocol.md)
 | 
				
			||||||
@@ -41,12 +41,12 @@ function(print_size_of_target TARGET)
 | 
				
			|||||||
            )
 | 
					            )
 | 
				
			||||||
endfunction()
 | 
					endfunction()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function(_generate_file TARGET PREFIX VERSION DATE OUTPUT_EXTENSION OBJCOPY_BFD_OUTPUT)
 | 
					function(_generate_file TARGET PREFIX VERSION DATE IIC_TYPE OUTPUT_EXTENSION OBJCOPY_BFD_OUTPUT)
 | 
				
			||||||
    get_target_property(TARGET_OUTPUT_NAME ${TARGET} OUTPUT_NAME)
 | 
					    get_target_property(TARGET_OUTPUT_NAME ${TARGET} OUTPUT_NAME)
 | 
				
			||||||
    if (TARGET_OUTPUT_NAME)
 | 
					    if (TARGET_OUTPUT_NAME)
 | 
				
			||||||
        set(OUTPUT_FILE_NAME "${PREFIX}_${VERSION}_${DATE}.${OUTPUT_EXTENSION}")
 | 
					        set(OUTPUT_FILE_NAME "${PREFIX}_${VERSION}_${DATE}_${IIC_TYPE}.${OUTPUT_EXTENSION}")
 | 
				
			||||||
    else()
 | 
					    else()
 | 
				
			||||||
        set(OUTPUT_FILE_NAME "${TARGET}_${VERSION}_${DATE}.${OUTPUT_EXTENSION}")
 | 
					        set(OUTPUT_FILE_NAME "${TARGET}_${VERSION}_${DATE}_${IIC_TYPE}.${OUTPUT_EXTENSION}")
 | 
				
			||||||
    endif()
 | 
					    endif()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    get_target_property(RUNTIME_OUTPUT_DIRECTORY ${TARGET} RUNTIME_OUTPUT_DIRECTORY)
 | 
					    get_target_property(RUNTIME_OUTPUT_DIRECTORY ${TARGET} RUNTIME_OUTPUT_DIRECTORY)
 | 
				
			||||||
@@ -66,11 +66,11 @@ function(_generate_file TARGET PREFIX VERSION DATE OUTPUT_EXTENSION OBJCOPY_BFD_
 | 
				
			|||||||
endfunction()
 | 
					endfunction()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function(generate_binary_file TARGET PREFIX)
 | 
					function(generate_binary_file TARGET PREFIX)
 | 
				
			||||||
    _generate_file(${TARGET} "${PREFIX}" "${VERSION}" "${CURRENT_DATE}" "bin" "binary")
 | 
					    _generate_file(${TARGET} "${PREFIX}" "${VERSION}" "${CURRENT_DATE}" "${IIC_TYPE}" "bin" "binary")
 | 
				
			||||||
endfunction()
 | 
					endfunction()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function(generate_hex_file TARGET PREFIX)
 | 
					function(generate_hex_file TARGET PREFIX)
 | 
				
			||||||
    _generate_file(${TARGET} "${PREFIX}" "${VERSION}" "${CURRENT_DATE}" "hex" "ihex")
 | 
					    _generate_file(${TARGET} "${PREFIX}" "${VERSION}" "${CURRENT_DATE}" "${IIC_TYPE}" "hex" "ihex")
 | 
				
			||||||
endfunction()
 | 
					endfunction()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
set(CMAKE_EXECUTABLE_SUFFIX_C   .elf)
 | 
					set(CMAKE_EXECUTABLE_SUFFIX_C   .elf)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -11,14 +11,13 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/******************************************************************************/
 | 
					/******************************************************************************/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define I2C_SPEED       20000
 | 
					#define RCU_GPIO_I2C         RCU_GPIOF
 | 
				
			||||||
#define RCU_GPIO_I2C    RCU_GPIOF
 | 
					#define RCU_I2C              RCU_I2C0
 | 
				
			||||||
#define RCU_I2C         RCU_I2C0
 | 
					#define I2C_SCL_PORT         GPIOF
 | 
				
			||||||
#define I2C_SCL_PORT    GPIOF
 | 
					#define I2C_SCL_PIN          GPIO_PIN_1
 | 
				
			||||||
#define I2C_SCL_PIN     GPIO_PIN_1
 | 
					#define I2C_SDA_PORT         GPIOF
 | 
				
			||||||
#define I2C_SDA_PORT    GPIOF
 | 
					#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
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
/******************************************************************************/
 | 
					/******************************************************************************/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -33,4 +32,13 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/******************************************************************************/
 | 
					/******************************************************************************/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define LED_PORT             GPIOA
 | 
				
			||||||
 | 
					#define LED_PIN              GPIO_PIN_7
 | 
				
			||||||
 | 
					#define LED_RCU              RCU_GPIOA
 | 
				
			||||||
 | 
					#define LED_BLINK_TIMER_RCU  RCU_TIMER16
 | 
				
			||||||
 | 
					#define LED_BLINK_TIMER      TIMER16
 | 
				
			||||||
 | 
					#define LED_BLINK_IRQ        TIMER16_IRQn
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/******************************************************************************/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif //BOARD_CONFIG_H
 | 
					#endif //BOARD_CONFIG_H
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										15
									
								
								inc/fwdgt.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								inc/fwdgt.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,15 @@
 | 
				
			|||||||
 | 
					//
 | 
				
			||||||
 | 
					// Created by yelv1 on 24-12-29.
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifndef FWDGT_H
 | 
				
			||||||
 | 
					#define FWDGT_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "gd32e23x.h"
 | 
				
			||||||
 | 
					#include "board_config.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void watchdog_init(void);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void fwdgt_reset_mcu(void);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif //FWDGT_H
 | 
				
			||||||
@@ -19,13 +19,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/******************************************************************************/
 | 
					/******************************************************************************/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define I2C_SCL_HIGH()      gpio_bit_set(I2C_SCL_PORT, I2C_SCL_PIN)
 | 
					#define I2C_SPEED       20000
 | 
				
			||||||
#define I2C_SCL_LOW()       gpio_bit_reset(I2C_SCL_PORT, I2C_SCL_PIN)
 | 
					 | 
				
			||||||
#define I2C_SDA_HIGH()      gpio_bit_set(I2C_SDA_PORT, I2C_SDA_PIN)
 | 
					 | 
				
			||||||
#define I2C_SDA_LOW()       gpio_bit_reset(I2C_SDA_PORT, I2C_SDA_PIN)
 | 
					 | 
				
			||||||
#define I2C_SDA_READ()      gpio_input_bit_get(I2C_SDA_PORT, I2C_SDA_PIN)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/******************************************************************************/
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define I2C_TIME_OUT    (uint16_t)(5000)
 | 
					#define I2C_TIME_OUT    (uint16_t)(5000)
 | 
				
			||||||
#define I2C_OK          1
 | 
					#define I2C_OK          1
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										19
									
								
								inc/led.h
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								inc/led.h
									
									
									
									
									
								
							@@ -6,24 +6,7 @@
 | 
				
			|||||||
#define LED_H
 | 
					#define LED_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "gd32e23x_it.h"
 | 
					#include "gd32e23x_it.h"
 | 
				
			||||||
#include "gd32e23x.h"
 | 
					#include "board_config.h"
 | 
				
			||||||
#include "systick.h"
 | 
					 | 
				
			||||||
#include <stdbool.h>
 | 
					 | 
				
			||||||
#include <string.h>
 | 
					 | 
				
			||||||
#include <stdio.h>
 | 
					 | 
				
			||||||
#include <stdlib.h>
 | 
					 | 
				
			||||||
#include <math.h>
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/******************************************************************************/
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#define LED_PORT            GPIOA
 | 
					 | 
				
			||||||
#define LED_PIN             GPIO_PIN_7
 | 
					 | 
				
			||||||
#define LED_RCU             RCU_GPIOA
 | 
					 | 
				
			||||||
#define LED_TIMER_RCU       RCU_TIMER16
 | 
					 | 
				
			||||||
#define LED_TIMER           TIMER16
 | 
					 | 
				
			||||||
#define LED_IRQ             TIMER16_IRQn
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/******************************************************************************/
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
void led_config(void);
 | 
					void led_config(void);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										18
									
								
								inc/main.h
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								inc/main.h
									
									
									
									
									
								
							@@ -35,10 +35,20 @@ OF SUCH DAMAGE.
 | 
				
			|||||||
#ifndef MAIN_H
 | 
					#ifndef MAIN_H
 | 
				
			||||||
#define MAIN_H
 | 
					#define MAIN_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// #define DEBUG_VERBOES
 | 
					#include <stdio.h>
 | 
				
			||||||
 | 
					#include "gd32e23x.h"
 | 
				
			||||||
 | 
					#include "systick.h"
 | 
				
			||||||
 | 
					#include "gd32e23x_libopt.h"
 | 
				
			||||||
 | 
					#include "rs485.h"
 | 
				
			||||||
 | 
					#include "led.h"
 | 
				
			||||||
 | 
					#include "ldc1612.h"
 | 
				
			||||||
 | 
					#include "fwdgt.h"
 | 
				
			||||||
 | 
					#include "board_config.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void watchdog_init(void);
 | 
					#ifdef SOFTWARE_IIC
 | 
				
			||||||
 | 
					#include "soft_i2c.h"
 | 
				
			||||||
void reset_mcu(void);
 | 
					#else
 | 
				
			||||||
 | 
					#include "i2c.h"
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif /* MAIN_H */
 | 
					#endif /* MAIN_H */
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										34
									
								
								inc/rs485.h
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								inc/rs485.h
									
									
									
									
									
								
							@@ -8,33 +8,19 @@
 | 
				
			|||||||
#include "gd32e23x_it.h"
 | 
					#include "gd32e23x_it.h"
 | 
				
			||||||
#include "gd32e23x.h"
 | 
					#include "gd32e23x.h"
 | 
				
			||||||
#include "systick.h"
 | 
					#include "systick.h"
 | 
				
			||||||
#include <stdbool.h>
 | 
					 | 
				
			||||||
#include <string.h>
 | 
					 | 
				
			||||||
#include <stdio.h>
 | 
					#include <stdio.h>
 | 
				
			||||||
#include <stdlib.h>
 | 
					 | 
				
			||||||
#include <sys/types.h>
 | 
					 | 
				
			||||||
#include "i2c.h"
 | 
					 | 
				
			||||||
#include "ldc1612.h"
 | 
					#include "ldc1612.h"
 | 
				
			||||||
#include "tmp112.h"
 | 
					#include "tmp112.h"
 | 
				
			||||||
 | 
					#include "fwdgt.h"
 | 
				
			||||||
 | 
					#include "board_config.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/******************************************************************************/
 | 
					/******************************************************************************/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define RS485_RCU            RCU_USART0
 | 
					#define RX_BUFFER_SIZE            32
 | 
				
			||||||
#define RS485_GPIO_RCU       RCU_GPIOA
 | 
					 | 
				
			||||||
#define RS485_GPIO_PORT      GPIOA
 | 
					 | 
				
			||||||
#define RS485_TX_PIN         GPIO_PIN_2
 | 
					 | 
				
			||||||
#define RS485_RX_PIN         GPIO_PIN_3
 | 
					 | 
				
			||||||
#define RS485_PHY            USART0
 | 
					 | 
				
			||||||
#define RS485_BAUDRATE       115200U
 | 
					 | 
				
			||||||
#define RS485_EN_PIN         GPIO_PIN_1
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
/******************************************************************************/
 | 
					#define PROTOCOL_PACKAGE_HEADER   0xD5
 | 
				
			||||||
 | 
					#define PROTOCOL_BOARD_TYPE       0x03
 | 
				
			||||||
#define RX_BUFFER_SIZE           32
 | 
					#define PROTOCOL_PACKAGE_LENGTH   0x02
 | 
				
			||||||
 | 
					 | 
				
			||||||
#define LDC1612_PACKAGE_HEADER   0xD5
 | 
					 | 
				
			||||||
#define LDC1612_BOARD_TYPE       0x03
 | 
					 | 
				
			||||||
#define LDC1612_PACKAGE_LENGTH   0x02
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
/******************************************************************************/
 | 
					/******************************************************************************/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -50,13 +36,21 @@ typedef enum
 | 
				
			|||||||
/******************************************************************************/
 | 
					/******************************************************************************/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void rs485_config(void);
 | 
					void rs485_config(void);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void process_command(uint8_t* cmd, size_t length);
 | 
					void process_command(uint8_t* cmd, size_t length);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
uint8_t calculate_crc(uint8_t data[], uint8_t data_length);
 | 
					uint8_t calculate_crc(uint8_t data[], uint8_t data_length);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
validation_result_t validate_package_crc(uint8_t* data, uint8_t data_length);
 | 
					validation_result_t validate_package_crc(uint8_t* data, uint8_t data_length);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
validation_result_t validate_package_header(uint8_t* data);
 | 
					validation_result_t validate_package_header(uint8_t* data);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
validation_result_t validate_package_type(uint8_t* data);
 | 
					validation_result_t validate_package_type(uint8_t* data);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
validation_result_t validate_data_length(uint8_t* data);
 | 
					validation_result_t validate_data_length(uint8_t* data);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void eddy_current_value_report(void);
 | 
					void eddy_current_value_report(void);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void tempture_value_report(void);
 | 
					void tempture_value_report(void);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif //RS485_H
 | 
					#endif //RS485_H
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,12 +7,6 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#include "gd32e23x_it.h"
 | 
					#include "gd32e23x_it.h"
 | 
				
			||||||
#include "gd32e23x.h"
 | 
					#include "gd32e23x.h"
 | 
				
			||||||
#include "systick.h"
 | 
					 | 
				
			||||||
#include <stdbool.h>
 | 
					 | 
				
			||||||
#include <string.h>
 | 
					 | 
				
			||||||
#include <stdio.h>
 | 
					 | 
				
			||||||
#include <stdlib.h>
 | 
					 | 
				
			||||||
#include <math.h>
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "board_config.h"
 | 
					#include "board_config.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -28,6 +22,8 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/******************************************************************************/
 | 
					/******************************************************************************/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define TMP112A_TEMP_REG     0x00
 | 
				
			||||||
 | 
					
 | 
				
			||||||
uint32_t tmp112a_get_raw_channel_result(void);
 | 
					uint32_t tmp112a_get_raw_channel_result(void);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif //TMP112_H
 | 
					#endif //TMP112_H
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										28
									
								
								src/fwdgt.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								src/fwdgt.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
				
			|||||||
 | 
					//
 | 
				
			||||||
 | 
					// Created by yelv1 on 24-12-29.
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "fwdgt.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void watchdog_init(void) {
 | 
				
			||||||
 | 
					    /* Enable the LSI clock */
 | 
				
			||||||
 | 
					    rcu_osci_on(RCU_IRC40K);
 | 
				
			||||||
 | 
					    rcu_osci_stab_wait(RCU_IRC40K);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* Configure FWDGT counter clock: 40KHz(IRC40K) / 64 = 0.625 KHz */
 | 
				
			||||||
 | 
					    fwdgt_config(625, FWDGT_PSC_DIV64); // Set timeout to 1 seconds (625 / 0.625 KHz)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* Enable FWDGT */
 | 
				
			||||||
 | 
					    fwdgt_enable();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void fwdgt_reset_mcu(void) {
 | 
				
			||||||
 | 
					    /* Enable the write access to the FWDGT_CTL register */
 | 
				
			||||||
 | 
					    FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* Configure FWDGT to trigger a system reset */
 | 
				
			||||||
 | 
					    fwdgt_config(50, FWDGT_PSC_DIV4);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* Reload the counter to trigger the reset */
 | 
				
			||||||
 | 
					    fwdgt_counter_reload();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -103,15 +103,15 @@ void SysTick_Handler(void) {
 | 
				
			|||||||
  * @retval None
 | 
					  * @retval None
 | 
				
			||||||
  */
 | 
					  */
 | 
				
			||||||
void TIMER16_IRQHandler(void) {
 | 
					void TIMER16_IRQHandler(void) {
 | 
				
			||||||
    if (timer_interrupt_flag_get(LED_TIMER, TIMER_INT_FLAG_UP) == SET) {
 | 
					    if (timer_interrupt_flag_get(LED_BLINK_TIMER, TIMER_INT_FLAG_UP) == SET) {
 | 
				
			||||||
        timer_interrupt_flag_clear(LED_TIMER, TIMER_INT_FLAG_UP);
 | 
					        timer_interrupt_flag_clear(LED_BLINK_TIMER, TIMER_INT_FLAG_UP);
 | 
				
			||||||
        static uint8_t led_status = 0;
 | 
					        static uint8_t led_status = 0;
 | 
				
			||||||
        if (led_status) {
 | 
					        if (led_status) {
 | 
				
			||||||
            gpio_bit_write(LED_PORT, LED_PIN, RESET);
 | 
					            gpio_bit_write(LED_PORT, LED_PIN, RESET);
 | 
				
			||||||
            timer_autoreload_value_config(LED_TIMER, 19200);
 | 
					            timer_autoreload_value_config(LED_BLINK_TIMER, 19200);
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            gpio_bit_write(LED_PORT, LED_PIN, SET);
 | 
					            gpio_bit_write(LED_PORT, LED_PIN, SET);
 | 
				
			||||||
            timer_autoreload_value_config(LED_TIMER, 800);
 | 
					            timer_autoreload_value_config(LED_BLINK_TIMER, 800);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        led_status = !led_status;
 | 
					        led_status = !led_status;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										14
									
								
								src/led.c
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								src/led.c
									
									
									
									
									
								
							@@ -11,8 +11,8 @@ void led_config(void) {
 | 
				
			|||||||
    gpio_output_options_set(LED_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, LED_PIN);
 | 
					    gpio_output_options_set(LED_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, LED_PIN);
 | 
				
			||||||
    gpio_bit_write(LED_PORT, LED_PIN, SET);
 | 
					    gpio_bit_write(LED_PORT, LED_PIN, SET);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    rcu_periph_clock_enable(LED_TIMER_RCU);
 | 
					    rcu_periph_clock_enable(LED_BLINK_TIMER_RCU);
 | 
				
			||||||
    timer_deinit(LED_TIMER);
 | 
					    timer_deinit(LED_BLINK_TIMER);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    timer_parameter_struct timer_initpara;
 | 
					    timer_parameter_struct timer_initpara;
 | 
				
			||||||
    timer_struct_para_init(&timer_initpara);
 | 
					    timer_struct_para_init(&timer_initpara);
 | 
				
			||||||
@@ -21,12 +21,12 @@ void led_config(void) {
 | 
				
			|||||||
    timer_initpara.counterdirection = TIMER_COUNTER_UP;
 | 
					    timer_initpara.counterdirection = TIMER_COUNTER_UP;
 | 
				
			||||||
    timer_initpara.period = 9999;
 | 
					    timer_initpara.period = 9999;
 | 
				
			||||||
    timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
 | 
					    timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
 | 
				
			||||||
    timer_init(LED_TIMER, &timer_initpara);
 | 
					    timer_init(LED_BLINK_TIMER, &timer_initpara);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    timer_auto_reload_shadow_enable(LED_TIMER);
 | 
					    timer_auto_reload_shadow_enable(LED_BLINK_TIMER);
 | 
				
			||||||
    timer_interrupt_enable(LED_TIMER, TIMER_INT_UP);
 | 
					    timer_interrupt_enable(LED_BLINK_TIMER, TIMER_INT_UP);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    timer_enable(LED_TIMER);
 | 
					    timer_enable(LED_BLINK_TIMER);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    nvic_irq_enable(LED_IRQ, 2);
 | 
					    nvic_irq_enable(LED_BLINK_IRQ, 2);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										38
									
								
								src/main.c
									
									
									
									
									
								
							
							
						
						
									
										38
									
								
								src/main.c
									
									
									
									
									
								
							@@ -5,44 +5,6 @@
 | 
				
			|||||||
    \version 2024-02-22, V2.1.0, firmware for GD32E23x
 | 
					    \version 2024-02-22, V2.1.0, firmware for GD32E23x
 | 
				
			||||||
*/
 | 
					*/
 | 
				
			||||||
#include "main.h"
 | 
					#include "main.h"
 | 
				
			||||||
#include <stdio.h>
 | 
					 | 
				
			||||||
#include "gd32e23x.h"
 | 
					 | 
				
			||||||
#include "systick.h"
 | 
					 | 
				
			||||||
#include "gd32e23x_libopt.h"
 | 
					 | 
				
			||||||
#include "rs485.h"
 | 
					 | 
				
			||||||
#include "led.h"
 | 
					 | 
				
			||||||
#include "i2c.h"
 | 
					 | 
				
			||||||
#include "ldc1612.h"
 | 
					 | 
				
			||||||
#include "board_config.h"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#ifdef SOFTWARE_IIC
 | 
					 | 
				
			||||||
#include "soft_i2c.h"
 | 
					 | 
				
			||||||
#else
 | 
					 | 
				
			||||||
#include "i2c.h"
 | 
					 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
void watchdog_init(void) {
 | 
					 | 
				
			||||||
    /* Enable the LSI clock */
 | 
					 | 
				
			||||||
    rcu_osci_on(RCU_IRC40K);
 | 
					 | 
				
			||||||
    rcu_osci_stab_wait(RCU_IRC40K);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /* Configure FWDGT counter clock: 40KHz(IRC40K) / 64 = 0.625 KHz */
 | 
					 | 
				
			||||||
    fwdgt_config(625, FWDGT_PSC_DIV64); // Set timeout to 1 seconds (625 / 0.625 KHz)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /* Enable FWDGT */
 | 
					 | 
				
			||||||
    fwdgt_enable();
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
void reset_mcu(void) {
 | 
					 | 
				
			||||||
    /* Enable the write access to the FWDGT_CTL register */
 | 
					 | 
				
			||||||
    FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /* Configure FWDGT to trigger a system reset */
 | 
					 | 
				
			||||||
    fwdgt_config(50, FWDGT_PSC_DIV4);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /* Reload the counter to trigger the reset */
 | 
					 | 
				
			||||||
    fwdgt_counter_reload();
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*!
 | 
					/*!
 | 
				
			||||||
    \brief      main function
 | 
					    \brief      main function
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -56,7 +56,7 @@ void process_command(uint8_t *cmd, size_t length) {
 | 
				
			|||||||
            } else if (strcmp(combined_str, "M3") == 0)
 | 
					            } else if (strcmp(combined_str, "M3") == 0)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                printf("%c%c%c%c%c%c", 0xB5, 0xF1, 0x02, 0x6F, 0x6B, 0xCC);
 | 
					                printf("%c%c%c%c%c%c", 0xB5, 0xF1, 0x02, 0x6F, 0x6B, 0xCC);
 | 
				
			||||||
                reset_mcu();
 | 
					                fwdgt_reset_mcu();
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
                printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C);
 | 
					                printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C);
 | 
				
			||||||
                return;
 | 
					                return;
 | 
				
			||||||
@@ -98,7 +98,7 @@ validation_result_t validate_package_crc(uint8_t *data, uint8_t data_length) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
validation_result_t validate_package_header(uint8_t *data) {
 | 
					validation_result_t validate_package_header(uint8_t *data) {
 | 
				
			||||||
    if (data[0] == LDC1612_PACKAGE_HEADER) {
 | 
					    if (data[0] == PROTOCOL_PACKAGE_HEADER) {
 | 
				
			||||||
        return VALIDATION_SUCCESS;
 | 
					        return VALIDATION_SUCCESS;
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
        return VALIDATION_HEADER_ERROR;
 | 
					        return VALIDATION_HEADER_ERROR;
 | 
				
			||||||
@@ -106,7 +106,7 @@ validation_result_t validate_package_header(uint8_t *data) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
validation_result_t validate_package_type(uint8_t *data) {
 | 
					validation_result_t validate_package_type(uint8_t *data) {
 | 
				
			||||||
    if (data[1] == LDC1612_BOARD_TYPE) {
 | 
					    if (data[1] == PROTOCOL_BOARD_TYPE) {
 | 
				
			||||||
        return VALIDATION_SUCCESS;
 | 
					        return VALIDATION_SUCCESS;
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
        return VALIDATION_TYPE_ERROR;
 | 
					        return VALIDATION_TYPE_ERROR;
 | 
				
			||||||
@@ -114,7 +114,7 @@ validation_result_t validate_package_type(uint8_t *data) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
validation_result_t validate_data_length(uint8_t *data) {
 | 
					validation_result_t validate_data_length(uint8_t *data) {
 | 
				
			||||||
    if (data[2] == LDC1612_PACKAGE_LENGTH) {
 | 
					    if (data[2] == PROTOCOL_PACKAGE_LENGTH) {
 | 
				
			||||||
        return VALIDATION_SUCCESS;
 | 
					        return VALIDATION_SUCCESS;
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
        return VALIDATION_LENGTH_ERROR;
 | 
					        return VALIDATION_LENGTH_ERROR;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,7 +13,7 @@
 | 
				
			|||||||
void soft_i2c_delay(void) {
 | 
					void soft_i2c_delay(void) {
 | 
				
			||||||
    delay_us(20); // Adjust delay as needed
 | 
					    delay_us(20); // Adjust delay as needed
 | 
				
			||||||
    /* delay to freq
 | 
					    /* delay to freq
 | 
				
			||||||
     *  20KHz: delay_us(20);
 | 
					     *  15KHz: delay_us(20);
 | 
				
			||||||
     *  65KHz: delay_us(1);
 | 
					     *  65KHz: delay_us(1);
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -44,15 +44,12 @@ void soft_i2c_config(void) {
 | 
				
			|||||||
    \retval     none
 | 
					    \retval     none
 | 
				
			||||||
*/
 | 
					*/
 | 
				
			||||||
void soft_i2c_start(void) {
 | 
					void soft_i2c_start(void) {
 | 
				
			||||||
    // sda_out();
 | 
					 | 
				
			||||||
    I2C_SDA_HIGH();
 | 
					    I2C_SDA_HIGH();
 | 
				
			||||||
    I2C_SCL_HIGH();
 | 
					    I2C_SCL_HIGH();
 | 
				
			||||||
    soft_i2c_delay();
 | 
					    soft_i2c_delay();
 | 
				
			||||||
    I2C_SDA_LOW();
 | 
					    I2C_SDA_LOW();
 | 
				
			||||||
    soft_i2c_delay();
 | 
					    soft_i2c_delay();
 | 
				
			||||||
    I2C_SCL_LOW();
 | 
					    I2C_SCL_LOW();
 | 
				
			||||||
    // soft_i2c_delay();
 | 
					 | 
				
			||||||
    // 从全高开始,SCL为高期间,SDA下降沿表示start信号,再拉低SCL
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*!
 | 
					/*!
 | 
				
			||||||
@@ -69,7 +66,6 @@ void soft_i2c_stop(void) {
 | 
				
			|||||||
    I2C_SCL_HIGH();
 | 
					    I2C_SCL_HIGH();
 | 
				
			||||||
    soft_i2c_delay();
 | 
					    soft_i2c_delay();
 | 
				
			||||||
    I2C_SDA_HIGH();
 | 
					    I2C_SDA_HIGH();
 | 
				
			||||||
    // 从全低开始,SCL为高期间,SDA上升沿表示stop
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*!
 | 
					/*!
 | 
				
			||||||
@@ -87,7 +83,6 @@ void soft_i2c_send_ack(void) {
 | 
				
			|||||||
    I2C_SCL_LOW();
 | 
					    I2C_SCL_LOW();
 | 
				
			||||||
    soft_i2c_delay();
 | 
					    soft_i2c_delay();
 | 
				
			||||||
    I2C_SDA_HIGH();
 | 
					    I2C_SDA_HIGH();
 | 
				
			||||||
    // SCL产生一个正常的时钟周期,其间SDA始终为低电平,表示ACK
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*!
 | 
					/*!
 | 
				
			||||||
@@ -97,7 +92,6 @@ void soft_i2c_send_ack(void) {
 | 
				
			|||||||
    \retval     none
 | 
					    \retval     none
 | 
				
			||||||
*/
 | 
					*/
 | 
				
			||||||
void soft_i2c_send_nack(void) {
 | 
					void soft_i2c_send_nack(void) {
 | 
				
			||||||
    // sda_out();
 | 
					 | 
				
			||||||
    I2C_SDA_HIGH();
 | 
					    I2C_SDA_HIGH();
 | 
				
			||||||
    soft_i2c_delay();
 | 
					    soft_i2c_delay();
 | 
				
			||||||
    I2C_SCL_HIGH();
 | 
					    I2C_SCL_HIGH();
 | 
				
			||||||
@@ -105,7 +99,6 @@ void soft_i2c_send_nack(void) {
 | 
				
			|||||||
    I2C_SCL_LOW();
 | 
					    I2C_SCL_LOW();
 | 
				
			||||||
    soft_i2c_delay();
 | 
					    soft_i2c_delay();
 | 
				
			||||||
    I2C_SDA_HIGH();
 | 
					    I2C_SDA_HIGH();
 | 
				
			||||||
    // SCL产生一个正常的时钟周期,其间SDA始终为高电平,表示NACK
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*!
 | 
					/*!
 | 
				
			||||||
@@ -115,13 +108,11 @@ void soft_i2c_send_nack(void) {
 | 
				
			|||||||
    \retval     0: ACK received, 1: ACK not received
 | 
					    \retval     0: ACK received, 1: ACK not received
 | 
				
			||||||
*/
 | 
					*/
 | 
				
			||||||
uint8_t soft_i2c_wait_ack(void) {
 | 
					uint8_t soft_i2c_wait_ack(void) {
 | 
				
			||||||
    // sda_in();
 | 
					 | 
				
			||||||
    I2C_SDA_HIGH();
 | 
					    I2C_SDA_HIGH();
 | 
				
			||||||
    soft_i2c_delay();
 | 
					    soft_i2c_delay();
 | 
				
			||||||
    I2C_SCL_HIGH();
 | 
					    I2C_SCL_HIGH();
 | 
				
			||||||
    soft_i2c_delay();
 | 
					    soft_i2c_delay();
 | 
				
			||||||
    uint8_t ack = !I2C_SDA_READ();
 | 
					    uint8_t ack = !I2C_SDA_READ();
 | 
				
			||||||
    //ACK信号是第九个时钟期间,SDA被从机在SCL高期间,拉低并保持低电平。此处判断SDA是否被拉低,被拉低则返回0,取反为1,表示收到ACK
 | 
					 | 
				
			||||||
    I2C_SCL_LOW();
 | 
					    I2C_SCL_LOW();
 | 
				
			||||||
    return ack;
 | 
					    return ack;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -135,19 +126,18 @@ uint8_t soft_i2c_wait_ack(void) {
 | 
				
			|||||||
void soft_i2c_send_byte(uint8_t byte) {
 | 
					void soft_i2c_send_byte(uint8_t byte) {
 | 
				
			||||||
    // sda_out();
 | 
					    // sda_out();
 | 
				
			||||||
    for (int i = 0; i < 8; i++) {
 | 
					    for (int i = 0; i < 8; i++) {
 | 
				
			||||||
        if (byte & 0x80) { //通过&操作获取第一位是1还是0
 | 
					        if (byte & 0x80) {
 | 
				
			||||||
            I2C_SDA_HIGH(); //SCL低电平期间,SDA高电平表示1
 | 
					            I2C_SDA_HIGH();
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            I2C_SDA_LOW(); //SCL低电平期间,SDA低电平表示0
 | 
					            I2C_SDA_LOW();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        byte <<= 1; //左移一位,把原本第二位的数据移到第一位,再判断高低电平
 | 
					        byte <<= 1;
 | 
				
			||||||
        soft_i2c_delay();
 | 
					        soft_i2c_delay();
 | 
				
			||||||
        I2C_SCL_HIGH(); //SCL拉高电平,SDA电平状态保持不变
 | 
					        I2C_SCL_HIGH();
 | 
				
			||||||
 | 
					        soft_i2c_delay();
 | 
				
			||||||
 | 
					        I2C_SCL_LOW();
 | 
				
			||||||
        soft_i2c_delay();
 | 
					        soft_i2c_delay();
 | 
				
			||||||
        I2C_SCL_LOW(); //SCL拉低电平
 | 
					 | 
				
			||||||
        delay_us(5);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    // i2c_wait_ack();
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*!
 | 
					/*!
 | 
				
			||||||
@@ -157,9 +147,8 @@ void soft_i2c_send_byte(uint8_t byte) {
 | 
				
			|||||||
    \retval     received byte
 | 
					    \retval     received byte
 | 
				
			||||||
*/
 | 
					*/
 | 
				
			||||||
uint8_t soft_i2c_receive_byte(uint8_t ack) {
 | 
					uint8_t soft_i2c_receive_byte(uint8_t ack) {
 | 
				
			||||||
    // sda_in();
 | 
					 | 
				
			||||||
    uint8_t byte = 0;
 | 
					    uint8_t byte = 0;
 | 
				
			||||||
    I2C_SDA_HIGH(); //从高开始
 | 
					    I2C_SDA_HIGH();
 | 
				
			||||||
    for (int i = 0; i < 8; i++) {
 | 
					    for (int i = 0; i < 8; i++) {
 | 
				
			||||||
        byte <<= 1;
 | 
					        byte <<= 1;
 | 
				
			||||||
        I2C_SCL_HIGH();
 | 
					        I2C_SCL_HIGH();
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -11,9 +11,9 @@ uint32_t tmp112a_get_raw_channel_result(void) {
 | 
				
			|||||||
    // ldc1612_iic_read_16bits(CONVERTION_RESULT_REG_START + channel, value);
 | 
					    // ldc1612_iic_read_16bits(CONVERTION_RESULT_REG_START + channel, value);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef SOFTWARE_IIC
 | 
					#ifdef SOFTWARE_IIC
 | 
				
			||||||
    soft_i2c_read_16bits(TMP112A_ADDR, 0x00, value);
 | 
					    soft_i2c_read_16bits(TMP112A_ADDR, TMP112A_TEMP_REG, value);
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
    i2c_read_16bits(TMP112A_ADDR, 0x00, value);
 | 
					    i2c_read_16bits(TMP112A_ADDR, TMP112A_TEMP_REG, value);
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    raw_value = ((uint16_t) (value[0] << 4) | (value[1]>>4));
 | 
					    raw_value = ((uint16_t) (value[0] << 4) | (value[1]>>4));
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user