Просмотр исходного кода

1. uart接收改为中断方式
2. 2个uart的接收buffer分开
3. 加入uart的log信息,定位ps100和电池不通信的问题

Signed-off-by: huhui <huhui@sharkgulf.com>

huhui 4 лет назад
Родитель
Сommit
b6cdc2832f

+ 2 - 2
Application/app/protocol.c

@@ -53,7 +53,7 @@ void protocol_notify_old_frame(uart_enum_t uart_no){
 	head.checksum = _check_sum((uint8_t *)&head, head.size);
 	shark_uart_write_bytes(current_uart, (uint8_t *)&head, head.size);
 }
-
+extern u32 uart_new_prot;
 void protocol_recv_frame(uart_enum_t uart_no, uint8_t *data, int len){
 	current_uart = uart_no;
 	if (len < sizeof(can_frame_t)){
@@ -65,7 +65,7 @@ void protocol_recv_frame(uart_enum_t uart_no, uint8_t *data, int len){
 		health_add_uart_error(0 ,0 ,1);
 		return;
 	}
-
+	uart_new_prot++;
 	len -= sizeof(can_frame_t);
 	if (can_frame->key >= CAN_KEY_IAP_ENTER){
 		process_iap_message(can_frame, len);

+ 2 - 2
Application/app/protocol_old.c

@@ -119,7 +119,7 @@ static int get_response_data(uint8_t *data, uint8_t operate, uint8_t result){
 	return head->size;
 }
 
-
+extern u32 uart_old_prot;
 static int protocol_old_process_binary(uart_enum_t uart_no, uint8_t *data, int len){
 	current_uart = uart_no;
 	comm_head_t *head = (comm_head_t *)data;
@@ -131,7 +131,7 @@ static int protocol_old_process_binary(uart_enum_t uart_no, uint8_t *data, int l
 	if (checksum != _checksum(data, len)){
 		return -1;
 	}
-
+	uart_old_prot ++;
 	data = head->data;
 	data += 6;
 	uint8_t operate = data[0];

+ 4 - 1
Application/app/sox/state.c

@@ -125,6 +125,7 @@ void bms_state_log(void){
 	state_debug("open dfet %d - %d - 0x%x - %d\n", open_dfet, open_dfet_failt, close_dfet_reson, close_dfet_no_hall);
 	state_debug("Reset Reson 0x%x\n", bsp_get_rst_reson());
 	state_debug("BackUp value 0x%x\n", bsp_get_backup());
+	state_debug("Debug: %d, %d\n", shark_uart_timeout(), io_state()->hall_detect);
 	log_no_hall_time();
 #if 0
 	state_debug("Charging: %d\n", _bms_state.charging);
@@ -157,6 +158,7 @@ static void _debug_timer_handler(shark_timer_t *t){
 #else
 	bms_state_log();
 	measure_log();
+	shark_uart_log();
 #endif
 	shark_timer_post(&_debug_timer, 2000);
 }
@@ -396,7 +398,8 @@ static void _process_deepsleep(s32 health){
 	if (shark_get_mseconds() < (_sleep_time + 3 * 1000)){
 		return;
 	}
-	
+	printf("SYSTEM: enter sleep\n");
+	shark_uart_flush();
 	nv_save_all_soc();
 	mcu_enter_deepsleep();
 	soc_update_for_deepsleep(mcu_get_sleeptime());//²¹³¥ÐÝÃߵŦºÄ

+ 98 - 7
Application/bsp/uart.c

@@ -3,6 +3,7 @@
 #include "bsp/gpio.h"
 #include "libs/shark_libs.h"
 #include "libs/shark_task.h"
+#include "libs/logger.h"
 
 #define SHARK_UART_BAUDRATE				38400
 
@@ -39,17 +40,33 @@
 #define SHARK_UART1_rx_dma_clk			RCU_DMA
 
 // ================================================================================
+#define ENABLE_RX_DMA 0
 
 static u8 shark_uart0_tx_cache[SHARK_UART_TX_MEM_SIZE];
 #if UART_NUM==2
 static u8 shark_uart1_tx_cache[SHARK_UART_TX_MEM_SIZE];
 #endif
-static u8 shark_uart_rx_cache[SHARK_UART_RX_MEM_SIZE];
+
+static u8 shark_uart0_rx_cache[SHARK_UART_RX_MEM_SIZE];
+#if UART_NUM==2
+static u8 shark_uart1_rx_cache[SHARK_UART_RX_MEM_SIZE];
+#endif
+
 static shark_uart_t _shark_uart[UART_NUM];
 static shark_task_t _uart_task;
 static u64 _rx_time;
+u32 uart_rx_bytes = 0;
+u32 uart_old_prot = 0;
+u32 uart_new_prot = 0;
+u32 uart_tx_bytes = 0;
+static u32 uart_run_times[2];
+static u32 uart_irq_times[2];
 ///static bool uart_no_data = false;
+#if ENABLE_RX_DMA==1
 #define update_dma_w_pos(uart) circle_update_write_position(&uart->rx_queue, SHARK_UART_RX_MEM_SIZE - DMA_CHCNT(uart->rx_dma_ch))
+#else
+#define update_dma_w_pos(uart){}
+#endif
 extern void protocol_recv_frame(uart_enum_t uart_no, char *data, int len);
 extern void protocol_old_recv_frame(uart_enum_t uart_no, uint8_t *data, int len);
 extern void health_add_uart_error(uint32_t c, uint32_t l, uint32_t d);
@@ -77,6 +94,7 @@ static bool shark_uart_on_rx_frame(shark_uart_t *uart)
 static void shark_uart_rx(shark_uart_t *uart){
 	while(1) {
 		u8 data;
+		uart_run_times[_uart_index(uart->uart_com)] ++;
 		update_dma_w_pos(uart);
 		if (circle_get_one_data(&uart->rx_queue, &data) != 1) {
 			if (shark_get_mseconds() >= (5 + _rx_time)) {
@@ -95,6 +113,7 @@ static void shark_uart_rx(shark_uart_t *uart){
 			}
 			break;
 		}
+		uart_rx_bytes ++;
 		_rx_time = shark_get_mseconds();
 		uart->rx_frame_old_prot[uart->rx_length_old_prot ++] = data;
 		if (uart->rx_length_old_prot == sizeof(uart->rx_frame_old_prot)){
@@ -181,6 +200,7 @@ static void shark_uart_dma_tx(shark_uart_t *uart)
 
 static void shark_uart_write(shark_uart_t *uart, const u8 *buff, u16 size)
 {
+	uart_tx_bytes += size;
 	while (size > 0) {
 		u16 length = byte_queue_write(&uart->tx_queue, buff, size);
 
@@ -226,7 +246,7 @@ static void shark_uart_tx_dma_init(shark_uart_t *uart){
 #endif
 }
 
-
+#if ENABLE_RX_DMA==1
 static void shark_uart_rx_dma_init(shark_uart_t *uart){
 	dma_parameter_struct dma_init_struct;
 	rcu_periph_clock_enable(_uart_index(uart->uart_com)== SHARK_UART0?SHARK_UART0_rx_dma_clk:SHARK_UART1_rx_dma_clk);
@@ -246,7 +266,7 @@ static void shark_uart_rx_dma_init(shark_uart_t *uart){
 	dma_channel_enable(uart->rx_dma_ch);
 	usart_dma_receive_config(uart->uart_com, USART_DENR_ENABLE);
 }
-
+#endif
 static void shark_uart_pin_init(shark_uart_t *uart){
 	if (_uart_index(uart->uart_com) == SHARK_UART0) {
 		rcu_periph_clock_enable(SHARK_UART0_clk);
@@ -293,7 +313,15 @@ static void shark_uart_device_init(shark_uart_t *uart){
 	usart_hardware_flow_cts_config(uart->uart_com, USART_CTS_DISABLE);
 	usart_receive_config(uart->uart_com, USART_RECEIVE_ENABLE);
 	usart_transmit_config(uart->uart_com, USART_TRANSMIT_ENABLE);
-	
+
+#if ENABLE_RX_DMA==0
+	usart_overrun_disable(uart->uart_com);//must add
+	usart_lin_mode_disable(uart->uart_com);
+	usart_receiver_timeout_disable(uart->uart_com);
+	usart_reception_error_dma_disable(uart->uart_com);
+
+	usart_interrupt_enable(uart->uart_com,USART_INT_RBNE);
+#endif
 }
 
 static u32 shark_uart_handler(void)
@@ -313,15 +341,35 @@ static u32 shark_uart_handler(void)
 	return 0;
 }
 
+void shark_uart_log(void){
+	shark_uart_t *uart = _shark_uart + SHARK_UART0;
+	sys_debug("Debug: %d, %d, %d, %d\n", uart_tx_bytes, uart_rx_bytes, uart_new_prot, uart_old_prot);
+	sys_debug("uart0: %d, %d, %d, %d, %d\n", uart_run_times[0], uart->rx_queue.r_pos, uart->rx_queue.w_pos, uart->rx_queue.buffer_len, uart_irq_times[0]);
+#if ENABLE_RX_DMA==1
+	sys_debug("dma0: %d\n", DMA_CHCNT(uart->rx_dma_ch));
+#endif
+#if UART_NUM==2
+	uart = _shark_uart + SHARK_UART1;
+	sys_debug("uart1: %d, %d, %d, %d, %d\n", uart_run_times[1], uart->rx_queue.r_pos, uart->rx_queue.w_pos, uart->rx_queue.buffer_len, uart_irq_times[1]);
+#if ENABLE_RX_DMA==1
+	sys_debug("dma1: %d\n", DMA_CHCNT(uart->rx_dma_ch));
+#endif
+#endif
+}
+
 void shark_uart_flush(void){
 	shark_uart_t *uart = _shark_uart + SHARK_UART0;
 	if (uart->uart_com != 0) {
-		shark_uart_dma_tx(uart);
+		while(!byte_queue_empty(&uart->tx_queue)) {
+			shark_uart_dma_tx(uart);
+		}
 	}
 #if UART_NUM==2	
 	uart = _shark_uart + SHARK_UART1;
 	if (uart->uart_com != 0) {
-		shark_uart_dma_tx(uart);
+		while(!byte_queue_empty(&uart->tx_queue)) {
+			shark_uart_dma_tx(uart);
+		}
 	}
 #endif	
 }
@@ -353,6 +401,15 @@ static u8 *tx_cache_addr(uart_enum_t uart_no){
 #endif
 }
 
+static u8 *rx_cache_addr(uart_enum_t uart_no){
+#if UART_NUM==2
+	return (uart_no == SHARK_UART0)?shark_uart0_rx_cache:shark_uart1_rx_cache;
+#else
+	return shark_uart0_rx_cache;
+#endif
+}
+
+
 void shark_uart_deinit(uart_enum_t uart_no){
 	shark_uart_t *uart = _shark_uart + uart_no;
 	if (uart->uart_com != 0) {
@@ -367,8 +424,14 @@ void shark_uart_deinit(uart_enum_t uart_no){
 	}
 	if (uart_no == SHARK_UART0) {
 		UART0_IR_EN(0);
+#if ENABLE_RX_DMA==0
+		nvic_irq_disable(USART0_IRQn);
+#endif
 	}else {
 		UART1_IR_EN(0);
+#if ENABLE_RX_DMA==0
+		nvic_irq_disable(USART1_IRQn);
+#endif
 	}
 }
 
@@ -388,7 +451,7 @@ void shark_uart_init(uart_enum_t uart_no)
 	uart->tx_length = 0;
 	uart->uart_com = (uart_no == SHARK_UART0)?SHARK_UART0_com:SHARK_UART1_com;
 
-	circle_buffer_init(&uart->rx_queue, shark_uart_rx_cache, SHARK_UART_TX_MEM_SIZE);
+	circle_buffer_init(&uart->rx_queue, rx_cache_addr(uart_no), SHARK_UART_RX_MEM_SIZE);
 	byte_queue_init(&uart->tx_queue,tx_cache_addr(uart_no), SHARK_UART_TX_MEM_SIZE);
 
 	uart->rx_dma_ch = (uart_no == SHARK_UART0)?SHARK_UART0_rx_dma_ch:SHARK_UART1_rx_dma_ch;
@@ -396,7 +459,9 @@ void shark_uart_init(uart_enum_t uart_no)
 
 	shark_uart_pin_init(uart);
 	shark_uart_device_init(uart);
+#if ENABLE_RX_DMA==1
 	shark_uart_rx_dma_init(uart);
+#endif
 	shark_uart_tx_dma_init(uart);
 	usart_enable(uart->uart_com);
 	
@@ -406,13 +471,39 @@ void shark_uart_init(uart_enum_t uart_no)
 	}
 	if (uart_no == SHARK_UART0) {
 		UART0_IR_EN(1);
+#if ENABLE_RX_DMA==0
+		nvic_irq_enable(USART0_IRQn, 3, 0);
+#endif
 	}else {
 		UART1_IR_EN(1);
+#if ENABLE_RX_DMA==0
+		nvic_irq_enable(USART1_IRQn, 3, 0);
+#endif
 	}
 	_rx_time = shark_get_mseconds();
 	uart->uart_no_data = false;
 }
 
+#if ENABLE_RX_DMA==0
+void USART0_IRQHandler(void){
+	if(usart_flag_get(USART0, USART_FLAG_RBNE) == SET){
+		shark_uart_t *uart = _shark_uart + SHARK_UART0;
+		uart_irq_times[0] ++;
+		u8 c = usart_data_receive(USART0);
+		circle_put_one_data(&uart->rx_queue, c);
+	}
+}
+
+void USART1_IRQHandler(void){
+	if(usart_flag_get(USART1, USART_FLAG_RBNE) == SET){
+		shark_uart_t *uart = _shark_uart + SHARK_UART1;
+		uart_irq_times[1] ++;
+		u8 c = usart_data_receive(USART1);
+		circle_put_one_data(&uart->rx_queue, c);
+	}
+}
+#endif
+
 static void shark_uart_write_byte_esc(shark_uart_t *uart, u8 value)
 {
 	switch (value) {

+ 7 - 8
Application/bsp/uart.h

@@ -11,11 +11,13 @@
 #define CH_ESC_END						0x06
 #define CH_ESC_ESC						0x07
 
-#define SHARK_UART_TX_MEM_SIZE			512
-#define SHARK_UART_RX_MEM_SIZE			512
+#define SHARK_UART_TX_MEM_SIZE			384
+#define SHARK_UART_RX_MEM_SIZE			384
+#define RX_FRAME_MAX_LEN 260
+#define RX_OLD_FRAME_MAX_LEN 256
 
 typedef enum {
-	SHARK_UART0,
+	SHARK_UART0 = 0,
 	SHARK_UART1,
 	SHARK_UART2,
 	SHARK_UART3,
@@ -23,8 +25,6 @@ typedef enum {
 	SHARK_UART_COUNT
 } uart_enum_t;
 
-#define RX_FRAME_MAX_LEN 260
-
 typedef struct {
 	byte_queue_t tx_queue;
 	c_buffer_t rx_queue;
@@ -35,7 +35,7 @@ typedef struct {
 	uint32_t uart_com;//uart device
 	uint8_t rx_frame[RX_FRAME_MAX_LEN];
 	uint16_t rx_length;
-	uint8_t rx_frame_old_prot[256];
+	uint8_t rx_frame_old_prot[RX_OLD_FRAME_MAX_LEN];
 	uint16_t rx_length_old_prot;
 	bool escape;
 	bool start;
@@ -51,6 +51,5 @@ void shark_uart_frame_end(uart_enum_t uart_no);
 void shark_uart_write_bytes(uart_enum_t uart_no, u8 *buff, u16 size);
 void shark_uart_flush(void);
 bool shark_uart_timeout(void);
-
-
+void shark_uart_log(void);