|
|
@@ -4,37 +4,29 @@
|
|
|
#include "libs/logger.h"
|
|
|
#include "libs/utils.h"
|
|
|
|
|
|
-#define SHARK_UART_BAUDRATE 500000
|
|
|
+#define SHARK_UART_BAUDRATE 921600
|
|
|
|
|
|
-#define SHARK_UART0_com UART3
|
|
|
+#define SHARK_UART0_com USART2
|
|
|
#define SHARK_UART0_tx_port GPIOB
|
|
|
#define SHARK_UART0_tx_pin GPIO_PIN_10
|
|
|
#define SHARK_UART0_rx_port GPIOB
|
|
|
#define SHARK_UART0_rx_pin GPIO_PIN_11
|
|
|
-#define SHARK_UART0_irq UART3_IRQn
|
|
|
-#define SHARK_UART0_clk RCU_UART3
|
|
|
+#define SHARK_UART0_irq USART2_IRQn
|
|
|
+#define SHARK_UART0_clk RCU_USART2
|
|
|
#define SHARK_UART0_tx_gpio_clk RCU_GPIOB
|
|
|
#define SHARK_UART0_rx_gpio_clk RCU_GPIOB
|
|
|
-#define SHARK_UART0_tx_dma DMA1
|
|
|
-#define SHARK_UART0_tx_dma_ch DMA_CH4
|
|
|
-#define SHARK_UART0_tx_dma_clk RCU_DMA1
|
|
|
-#define SHARK_UART0_rx_dma DMA1
|
|
|
+#define SHARK_UART0_tx_dma DMA0
|
|
|
+#define SHARK_UART0_tx_dma_ch DMA_CH1
|
|
|
+#define SHARK_UART0_tx_dma_clk RCU_DMA0
|
|
|
+#define SHARK_UART0_rx_dma DMA0
|
|
|
#define SHARK_UART0_rx_dma_ch DMA_CH2
|
|
|
-#define SHARK_UART0_rx_dma_clk RCU_DMA1
|
|
|
+#define SHARK_UART0_rx_dma_clk RCU_DMA0
|
|
|
|
|
|
|
|
|
// ================================================================================
|
|
|
#define ENABLE_RX_DMA 1
|
|
|
-#define UART_NUM 1
|
|
|
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_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[1];
|
|
|
///static bool uart_no_data = false;
|
|
|
@@ -46,7 +38,7 @@ static shark_uart_t _shark_uart[1];
|
|
|
|
|
|
// ================================================================================
|
|
|
static uart_enum_t _uart_index(uint32_t com){
|
|
|
- return com == SHARK_UART0_com?SHARK_UART0:SHARK_UART1;
|
|
|
+ return SHARK_UART0;
|
|
|
}
|
|
|
static bool shark_uart_on_rx_frame(shark_uart_t *uart)
|
|
|
{
|
|
|
@@ -154,6 +146,7 @@ static void shark_uart_write(shark_uart_t *uart, const u8 *buff, u16 size)
|
|
|
shark_uart_dma_tx(uart);
|
|
|
buff += length;
|
|
|
size -= length;
|
|
|
+ co_task_schedule();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -163,9 +156,19 @@ static void shark_uart_write_byte(shark_uart_t *uart, u8 value)
|
|
|
}
|
|
|
|
|
|
|
|
|
+void shark_uart_write_log(char *buffer){
|
|
|
+ int len = strlen(buffer);
|
|
|
+ shark_uart_t *uart = (_shark_uart+SHARK_UART0);
|
|
|
+ if (len > byte_queue_get_free(&uart->tx_queue)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ byte_queue_write(&uart->tx_queue, (const u8 *)buffer, len);
|
|
|
+ shark_uart_dma_tx(uart);
|
|
|
+}
|
|
|
+
|
|
|
static void shark_uart_tx_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_tx_dma_clk:SHARK_UART0_tx_dma_clk);
|
|
|
+ rcu_periph_clock_enable(SHARK_UART0_tx_dma_clk);
|
|
|
dma_deinit(SHARK_UART0_tx_dma, uart->tx_dma_ch);
|
|
|
dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
|
|
|
dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
|
|
|
@@ -191,7 +194,7 @@ static void shark_uart_tx_dma_init(shark_uart_t *uart){
|
|
|
#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_UART0_rx_dma_clk);
|
|
|
+ rcu_periph_clock_enable(SHARK_UART0_rx_dma_clk);
|
|
|
dma_deinit(SHARK_UART0_rx_dma, uart->rx_dma_ch);
|
|
|
dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
|
|
|
dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
|
|
|
@@ -213,7 +216,8 @@ static void shark_uart_pin_init(shark_uart_t *uart){
|
|
|
rcu_periph_clock_enable(SHARK_UART0_clk);
|
|
|
rcu_periph_clock_enable(SHARK_UART0_rx_gpio_clk);
|
|
|
rcu_periph_clock_enable(SHARK_UART0_tx_gpio_clk);
|
|
|
- gpio_init(SHARK_UART0_tx_port, GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,SHARK_UART0_tx_pin);
|
|
|
+ rcu_periph_clock_enable(RCU_AF);
|
|
|
+ gpio_init(SHARK_UART0_tx_port, GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,SHARK_UART0_tx_pin);
|
|
|
gpio_init(SHARK_UART0_rx_port, GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,SHARK_UART0_rx_pin);
|
|
|
}
|
|
|
|
|
|
@@ -263,15 +267,7 @@ void shark_uart_flush(void){
|
|
|
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) {
|
|
|
- while(!byte_queue_empty(&uart->tx_queue)) {
|
|
|
- shark_uart_dma_tx(uart);
|
|
|
- }
|
|
|
- }
|
|
|
-#endif
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -294,19 +290,11 @@ void DMA_Channel3_4_IRQHandler(void){
|
|
|
#endif
|
|
|
|
|
|
static u8 *tx_cache_addr(uart_enum_t uart_no){
|
|
|
-#if UART_NUM==2
|
|
|
- return (uart_no == SHARK_UART0)?shark_uart0_tx_cache:shark_uart1_tx_cache;
|
|
|
-#else
|
|
|
return shark_uart0_tx_cache;
|
|
|
-#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
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -315,22 +303,16 @@ void shark_uart_deinit(uart_enum_t uart_no){
|
|
|
if (uart->uart_com != 0) {
|
|
|
usart_disable(uart->uart_com);
|
|
|
usart_deinit(uart->uart_com);
|
|
|
- rcu_periph_clock_disable(uart_no == SHARK_UART0?SHARK_UART0_clk:SHARK_UART0_clk);
|
|
|
+ rcu_periph_clock_disable(SHARK_UART0_clk);
|
|
|
dma_channel_disable(SHARK_UART0_rx_dma, uart->rx_dma_ch);
|
|
|
dma_channel_disable(SHARK_UART0_tx_dma, uart->tx_dma_ch);
|
|
|
- rcu_periph_clock_disable(uart_no == SHARK_UART0?SHARK_UART0_tx_dma_clk:SHARK_UART0_tx_dma_clk);
|
|
|
- rcu_periph_clock_disable(uart_no == SHARK_UART0?SHARK_UART0_rx_dma_clk:SHARK_UART0_rx_dma_clk);
|
|
|
+ rcu_periph_clock_disable(SHARK_UART0_tx_dma_clk);
|
|
|
+ rcu_periph_clock_disable(SHARK_UART0_rx_dma_clk);
|
|
|
shark_uart_pin_deinit(uart);
|
|
|
}
|
|
|
- if (uart_no == SHARK_UART0) {
|
|
|
#if ENABLE_RX_DMA==0
|
|
|
- nvic_irq_disable(USART0_IRQn);
|
|
|
+ nvic_irq_disable(SHARK_UART0_irq);
|
|
|
#endif
|
|
|
- }else {
|
|
|
-#if ENABLE_RX_DMA==0
|
|
|
- nvic_irq_disable(USART1_IRQn);
|
|
|
-#endif
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -347,13 +329,13 @@ void shark_uart_init(uart_enum_t uart_no)
|
|
|
uart->escape = false;
|
|
|
uart->rx_length = 0;
|
|
|
uart->tx_length = 0;
|
|
|
- uart->uart_com = (uart_no == SHARK_UART0)?SHARK_UART0_com:SHARK_UART0_com;
|
|
|
+ uart->uart_com = SHARK_UART0_com;
|
|
|
|
|
|
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_UART0_rx_dma_ch;
|
|
|
- uart->tx_dma_ch = (uart_no == SHARK_UART0)?SHARK_UART0_tx_dma_ch:SHARK_UART0_tx_dma_ch;
|
|
|
+ uart->rx_dma_ch = SHARK_UART0_rx_dma_ch;
|
|
|
+ uart->tx_dma_ch = SHARK_UART0_tx_dma_ch;
|
|
|
|
|
|
shark_uart_pin_init(uart);
|
|
|
shark_uart_device_init(uart);
|
|
|
@@ -364,15 +346,9 @@ void shark_uart_init(uart_enum_t uart_no)
|
|
|
usart_enable(uart->uart_com);
|
|
|
|
|
|
co_task_create(shark_uart_task, uart, 256);
|
|
|
- if (uart_no == SHARK_UART0) {
|
|
|
#if ENABLE_RX_DMA==0
|
|
|
- nvic_irq_enable(UART3_IRQn, 3, 0);
|
|
|
+ nvic_irq_enable(SHARK_UART0_irq, 3, 0);
|
|
|
#endif
|
|
|
- }else {
|
|
|
-#if ENABLE_RX_DMA==0
|
|
|
- nvic_irq_enable(USART1_IRQn, 3, 0);
|
|
|
-#endif
|
|
|
- }
|
|
|
uart->uart_no_data = false;
|
|
|
}
|
|
|
|
|
|
@@ -461,9 +437,8 @@ void shark_uart_write_bytes(uart_enum_t uart_no, u8 *buff, u16 size){
|
|
|
shark_uart_write(_shark_uart + uart_no, buff, size);
|
|
|
}
|
|
|
|
|
|
-#if LOG_UART==1
|
|
|
int fputc(int c, FILE *fp){
|
|
|
shark_uart_write_byte(_shark_uart+SHARK_UART0, (u8)c);
|
|
|
return 1;
|
|
|
}
|
|
|
-#endif
|
|
|
+
|