generated from hulk/gd32e23x_template
	完成LED、RS485、IIC初始化部分代码。
This commit is contained in:
		| @@ -25,6 +25,8 @@ set(TARGET_C_SRC | |||||||
|         ${CMAKE_SOURCE_DIR}/src/gd32e23x_it.c |         ${CMAKE_SOURCE_DIR}/src/gd32e23x_it.c | ||||||
|         ${CMAKE_SOURCE_DIR}/src/systick.c |         ${CMAKE_SOURCE_DIR}/src/systick.c | ||||||
|         ${CMAKE_SOURCE_DIR}/src/peripheral.c |         ${CMAKE_SOURCE_DIR}/src/peripheral.c | ||||||
|  |         ${CMAKE_SOURCE_DIR}/src/LDC1612.c | ||||||
|  |         ${CMAKE_SOURCE_DIR}/src/RS485.c | ||||||
| ) | ) | ||||||
|  |  | ||||||
| add_executable(xlsw_3dp_LDC1612 ${TARGET_C_SRC}) | add_executable(xlsw_3dp_LDC1612 ${TARGET_C_SRC}) | ||||||
|   | |||||||
							
								
								
									
										42
									
								
								inc/LDC1612.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								inc/LDC1612.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | |||||||
|  | // | ||||||
|  | // Created by dell on 24-12-3. | ||||||
|  | // | ||||||
|  |  | ||||||
|  | #ifndef LDC1612_H | ||||||
|  | #define LDC1612_H | ||||||
|  |  | ||||||
|  | #include "gd32e23x_it.h" | ||||||
|  | #include "gd32e23x.h" | ||||||
|  | #include "systick.h" | ||||||
|  | #include <stdbool.h> | ||||||
|  | #include <string.h> | ||||||
|  | #include <stdio.h> | ||||||
|  | #include <stdlib.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 | ||||||
|  |  | ||||||
|  | #define I2C_SPEED 100000 | ||||||
|  | #define IR_I2C    I2C0 | ||||||
|  | #define RCU_IR_GPIO RCU_GPIOF | ||||||
|  | #define RCU_I2C RCU_I2C0 | ||||||
|  | #define I2C_SCL_PORT GPIOF | ||||||
|  | #define I2C_SCL_PIN GPIO_PIN_1 | ||||||
|  | #define I2C_SDA_PORT GPIOF | ||||||
|  | #define I2C_SDA_PIN GPIO_PIN_0 | ||||||
|  | #define I2C_GPIO_AF GPIO_AF_1 | ||||||
|  |  | ||||||
|  | #define I2C_TIME_OUT    (uint16_t)(5000) | ||||||
|  |  | ||||||
|  | #define SLAVE_ADDR         (0x5A << 1) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | void led_config(void); | ||||||
|  | void LDC1612_I2CConfig(void); | ||||||
|  |  | ||||||
|  |  | ||||||
|  | #endif //LDC1612_H | ||||||
							
								
								
									
										31
									
								
								inc/RS485.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								inc/RS485.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,31 @@ | |||||||
|  | // | ||||||
|  | // Created by dell on 24-12-3. | ||||||
|  | // | ||||||
|  |  | ||||||
|  | #ifndef RS485_H | ||||||
|  | #define RS485_H | ||||||
|  |  | ||||||
|  | #include "gd32e23x_it.h" | ||||||
|  | #include "gd32e23x.h" | ||||||
|  | #include "systick.h" | ||||||
|  | #include <stdbool.h> | ||||||
|  | #include <string.h> | ||||||
|  | #include <stdio.h> | ||||||
|  | #include <stdlib.h> | ||||||
|  |  | ||||||
|  | #define RS485_RCU            RCU_USART0 | ||||||
|  | #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 RX_BUFFER_SIZE 64 | ||||||
|  |  | ||||||
|  | void RS485_config(void); | ||||||
|  | void process_command(char *cmd); | ||||||
|  |  | ||||||
|  | #endif //RS485_H | ||||||
							
								
								
									
										60
									
								
								src/LDC1612.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								src/LDC1612.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,60 @@ | |||||||
|  | // | ||||||
|  | // Created by dell on 24-12-3. | ||||||
|  | // | ||||||
|  |  | ||||||
|  | #include "LDC1612.h" | ||||||
|  |  | ||||||
|  |  | ||||||
|  | void led_config(void) | ||||||
|  | { | ||||||
|  |     rcu_periph_clock_enable(LED_RCU); | ||||||
|  |  | ||||||
|  |     gpio_mode_set(LED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED_PIN); | ||||||
|  |     gpio_output_options_set(LED_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, LED_PIN); | ||||||
|  |     gpio_bit_write(LED_PORT, LED_PIN, SET); | ||||||
|  |  | ||||||
|  |     rcu_periph_clock_enable(LED_TIMER_RCU); | ||||||
|  |     timer_deinit(LED_TIMER); | ||||||
|  |  | ||||||
|  |     timer_parameter_struct timer_initpara; | ||||||
|  |     timer_struct_para_init(&timer_initpara); | ||||||
|  |     timer_initpara.prescaler = 7199; | ||||||
|  |     timer_initpara.alignedmode = TIMER_COUNTER_EDGE; | ||||||
|  |     timer_initpara.counterdirection = TIMER_COUNTER_UP; | ||||||
|  |     timer_initpara.period = 999; | ||||||
|  |     timer_initpara.clockdivision = TIMER_CKDIV_DIV1; | ||||||
|  |     timer_init(LED_TIMER, &timer_initpara); | ||||||
|  |  | ||||||
|  |     timer_auto_reload_shadow_enable(LED_TIMER); | ||||||
|  |     timer_interrupt_enable(LED_TIMER, TIMER_INT_UP); | ||||||
|  |  | ||||||
|  |     timer_enable(LED_TIMER); | ||||||
|  |  | ||||||
|  |     nvic_irq_enable(LED_IRQ, 0); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |   * @brief  This function configure the I2C peripheral & GPIO | ||||||
|  |   * @param[in]  none | ||||||
|  |   * @param[out] none | ||||||
|  |   * @retval None | ||||||
|  |   */ | ||||||
|  | void LDC1612_I2CConfig(void) { | ||||||
|  |     rcu_periph_clock_enable(RCU_IR_GPIO); | ||||||
|  |     rcu_periph_clock_enable(RCU_I2C); | ||||||
|  |  | ||||||
|  |     gpio_af_set(I2C_SCL_PORT, I2C_GPIO_AF, I2C_SCL_PIN); | ||||||
|  |     gpio_af_set(I2C_SDA_PORT, I2C_GPIO_AF, I2C_SDA_PIN); | ||||||
|  |  | ||||||
|  |     gpio_mode_set(I2C_SCL_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, I2C_SCL_PIN); | ||||||
|  |     gpio_output_options_set(I2C_SCL_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, I2C_SCL_PIN); | ||||||
|  |  | ||||||
|  |     gpio_mode_set(I2C_SDA_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, I2C_SDA_PIN); | ||||||
|  |     gpio_output_options_set(I2C_SDA_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, I2C_SDA_PIN); | ||||||
|  |  | ||||||
|  |     i2c_clock_config(IR_I2C, I2C_SPEED, I2C_DTCY_2); | ||||||
|  |  | ||||||
|  |     i2c_mode_addr_config(IR_I2C, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, SLAVE_ADDR); | ||||||
|  |     i2c_enable(IR_I2C); | ||||||
|  |     i2c_ack_config(IR_I2C, I2C_ACK_ENABLE); | ||||||
|  | } | ||||||
							
								
								
									
										59
									
								
								src/RS485.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								src/RS485.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,59 @@ | |||||||
|  | // | ||||||
|  | // Created by dell on 24-12-3. | ||||||
|  | // | ||||||
|  |  | ||||||
|  | #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) { | ||||||
|  |     rcu_periph_clock_enable(RS485_GPIO_RCU); | ||||||
|  |     rcu_periph_clock_enable(RS485_RCU); | ||||||
|  |  | ||||||
|  |     gpio_af_set(RS485_GPIO_PORT, GPIO_AF_1, GPIO_PIN_2 | GPIO_PIN_3); | ||||||
|  |  | ||||||
|  |     /* configure USART Tx&Rx as alternate function push-pull */ | ||||||
|  |     gpio_mode_set(RS485_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, RS485_TX_PIN | RS485_RX_PIN); | ||||||
|  |     gpio_output_options_set(RS485_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, RS485_TX_PIN | RS485_RX_PIN); | ||||||
|  |  | ||||||
|  |     /* configure RS485 EN Pin */ | ||||||
|  |     gpio_mode_set(RS485_GPIO_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, RS485_EN_PIN); | ||||||
|  |     gpio_output_options_set(RS485_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RS485_EN_PIN); | ||||||
|  |     gpio_bit_write(RS485_GPIO_PORT, RS485_EN_PIN, SET); | ||||||
|  |  | ||||||
|  |     /* USART configure */ | ||||||
|  |     usart_deinit(RS485_PHY); | ||||||
|  |     usart_baudrate_set(RS485_PHY, RS485_BAUDRATE); | ||||||
|  |     usart_receive_config(RS485_PHY, USART_RECEIVE_ENABLE); | ||||||
|  |     usart_transmit_config(RS485_PHY, USART_TRANSMIT_ENABLE); | ||||||
|  |  | ||||||
|  |     usart_enable(RS485_PHY); | ||||||
|  |  | ||||||
|  |     nvic_irq_enable(USART0_IRQn, 0); | ||||||
|  |     usart_interrupt_enable(RS485_PHY, USART_INT_RBNE); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void process_command(char *cmd) { | ||||||
|  |     if (strncmp(cmd, "M1", 2) == 0) { | ||||||
|  |         printf("M1 -=-=- OK!\r\n"); | ||||||
|  |         printf("Distance: %d\r\n", g_distance_uint16); | ||||||
|  |     } else if (strncmp(cmd, "M2", 2) == 0) { | ||||||
|  |         printf("M2 -=-=- OK!\r\n"); | ||||||
|  |         printf("Temperature: %d\r\n", g_temperature_uint16); | ||||||
|  |         // } else if (strncmp(cmd, "M3", 2) == 0) { | ||||||
|  |         //     char *param_str = cmd + 2; // Skip "M3" | ||||||
|  |         //     int param = atoi(param_str + 1); // Skip "S" and convert to integer | ||||||
|  |         //     if (param >= 0 && param <= 100) { | ||||||
|  |         //         printf("M3 with parameter %d -=-=- OK!\r\n", param); | ||||||
|  |         //     } else { | ||||||
|  |         //         printf("Invalid parameter for M3 command!\r\n"); | ||||||
|  |         //     } | ||||||
|  |     } else { | ||||||
|  |         printf("Invalid Command!\r\n"); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
| @@ -35,6 +35,7 @@ OF SUCH DAMAGE. | |||||||
| #include "gd32e23x_it.h" | #include "gd32e23x_it.h" | ||||||
| #include "main.h" | #include "main.h" | ||||||
| #include "systick.h" | #include "systick.h" | ||||||
|  | #include "LDC1612.h" | ||||||
|  |  | ||||||
| /*! | /*! | ||||||
|     \brief      this function handles NMI exception |     \brief      this function handles NMI exception | ||||||
| @@ -116,4 +117,27 @@ void TIMER13_IRQHandler(void) | |||||||
|         } |         } | ||||||
|         led_status = !led_status; |         led_status = !led_status; | ||||||
|     } |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |   * @brief  This function handles TIMER5 interrupt request. | ||||||
|  |   * @param[in]  none | ||||||
|  |   * @param[out] none | ||||||
|  |   * @retval None | ||||||
|  |   */ | ||||||
|  | void TIMER16_IRQHandler(void) { | ||||||
|  |     if (timer_interrupt_flag_get(LED_TIMER, TIMER_INT_FLAG_UP) == SET) | ||||||
|  |     { | ||||||
|  |         timer_interrupt_flag_clear(LED_TIMER, TIMER_INT_FLAG_UP); | ||||||
|  |         static uint8_t led_status = 0; | ||||||
|  |         if (led_status) | ||||||
|  |         { | ||||||
|  |             gpio_bit_write(LED_PORT, LED_PIN, RESET); | ||||||
|  |             timer_autoreload_value_config(LED_TIMER, 19200); | ||||||
|  |         } else { | ||||||
|  |             gpio_bit_write(LED_PORT, LED_PIN, SET); | ||||||
|  |             timer_autoreload_value_config(LED_TIMER, 800); | ||||||
|  |         } | ||||||
|  |         led_status = !led_status; | ||||||
|  |     } | ||||||
| } | } | ||||||
							
								
								
									
										11
									
								
								src/main.c
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								src/main.c
									
									
									
									
									
								
							| @@ -12,6 +12,9 @@ | |||||||
|  |  | ||||||
| #include "peripheral.h" | #include "peripheral.h" | ||||||
|  |  | ||||||
|  | #include "RS485.h" | ||||||
|  | #include "LDC1612.h" | ||||||
|  |  | ||||||
| /*! | /*! | ||||||
|     \brief      main function |     \brief      main function | ||||||
|     \param[in]  none |     \param[in]  none | ||||||
| @@ -22,11 +25,13 @@ int main(void) | |||||||
| { | { | ||||||
|     /* configure systick */ |     /* configure systick */ | ||||||
|     systick_config(); |     systick_config(); | ||||||
|     usart_config(); |     RS485_config(); | ||||||
|     led_blink_config(); |     led_config(); | ||||||
|  |  | ||||||
|     delay_ms(5000); |     delay_ms(5000); | ||||||
|     printf("system start!\r\n"); |     printf("\r\n"); | ||||||
|  |     printf("XLSW-3DP-LDC1612! V0.0.1\r\n"); | ||||||
|  |     printf("\r\n"); | ||||||
|  |  | ||||||
|     while(1){ |     while(1){ | ||||||
|     printf("hello world!\r\n"); |     printf("hello world!\r\n"); | ||||||
|   | |||||||
| @@ -40,7 +40,7 @@ void led_blink_config(void) | |||||||
| { | { | ||||||
|     rcu_periph_clock_enable(RCU_GPIOB); |     rcu_periph_clock_enable(RCU_GPIOB); | ||||||
|  |  | ||||||
|     gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_1); |     gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_1); | ||||||
|     gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_1); |     gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_1); | ||||||
|     gpio_bit_write(GPIOB, GPIO_PIN_1, SET); |     gpio_bit_write(GPIOB, GPIO_PIN_1, SET); | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user