|
|
@@ -4,34 +4,17 @@
|
|
|
#include "libs/logger.h"
|
|
|
#include "libs/utils.h"
|
|
|
|
|
|
-#define SHARK_UART_BAUDRATE 230400
|
|
|
+#define SHARK_UART_BAUDRATE 500000
|
|
|
|
|
|
-#ifdef DEBUG_PORT_UART1
|
|
|
#define SHARK_UART0_com USART1
|
|
|
-#define SHARK_UART0_tx_port GPIOA
|
|
|
-#define SHARK_UART0_tx_pin GPIO_PIN_2
|
|
|
-#define SHARK_UART0_rx_port GPIOA
|
|
|
-#define SHARK_UART0_rx_pin GPIO_PIN_3
|
|
|
-#define SHARK_UART0_irq USART1_IRQn
|
|
|
-#define SHARK_UART0_clk RCU_USART1
|
|
|
-#define SHARK_UART0_tx_gpio_clk RCU_GPIOA
|
|
|
-#define SHARK_UART0_rx_gpio_clk RCU_GPIOA
|
|
|
-#define SHARK_UART0_tx_dma DMA0
|
|
|
-#define SHARK_UART0_tx_dma_ch DMA_CH6
|
|
|
-#define SHARK_UART0_tx_dma_clk RCU_DMA0
|
|
|
-#define SHARK_UART0_rx_dma DMA0
|
|
|
-#define SHARK_UART0_rx_dma_ch DMA_CH5
|
|
|
-#define SHARK_UART0_rx_dma_clk RCU_DMA0
|
|
|
-#define SHARK_UART0_DMA_TX_IRQ DMA0_Channel6_IRQn
|
|
|
-#define UART_DMA_IRQHandler DMA0_Channel6_IRQHandler
|
|
|
-#else
|
|
|
-#define SHARK_UART0_com USART3
|
|
|
#define SHARK_UART0_tx_port GPIOB
|
|
|
-#define SHARK_UART0_tx_pin GPIO_PINS_10
|
|
|
+#define SHARK_UART0_tx_pin GPIO_PINS_6
|
|
|
#define SHARK_UART0_rx_port GPIOB
|
|
|
-#define SHARK_UART0_rx_pin GPIO_PINS_11
|
|
|
-#define SHARK_UART0_irq USART3_IRQn
|
|
|
-#define SHARK_UART0_clk CRM_USART3_PERIPH_CLOCK
|
|
|
+#define SHARK_UART0_rx_pin GPIO_PINS_7
|
|
|
+#define SHARK_UART0_IOMUX USART1_GMUX_0001
|
|
|
+
|
|
|
+#define SHARK_UART0_irq USART0_IRQn
|
|
|
+#define SHARK_UART0_clk CRM_USART1_PERIPH_CLOCK
|
|
|
#define SHARK_UART0_tx_gpio_clk CRM_GPIOB_PERIPH_CLOCK
|
|
|
#define SHARK_UART0_rx_gpio_clk CRM_GPIOB_PERIPH_CLOCK
|
|
|
#define SHARK_UART0_tx_dma DMA1
|
|
|
@@ -40,9 +23,13 @@
|
|
|
#define SHARK_UART0_rx_dma DMA1
|
|
|
#define SHARK_UART0_rx_dma_ch DMA1_CHANNEL3
|
|
|
#define SHARK_UART0_rx_dma_clk CRM_DMA1_PERIPH_CLOCK
|
|
|
-#define SHARK_UART0_DMA_TX_IRQ DMA0_Channel3_IRQn
|
|
|
-#define UART_DMA_IRQHandler DMA1_Channel3_IRQHandler
|
|
|
-#endif
|
|
|
+#define DMA_UART_TX_FLEX_CHANNEL FLEX_CHANNEL2
|
|
|
+#define DMA_UART_TX_FLEX DMA_FLEXIBLE_UART1_TX
|
|
|
+#define DMA_UART_RX_FLEX_CHANNEL FLEX_CHANNEL3
|
|
|
+#define DMA_UART_RX_FLEX DMA_FLEXIBLE_UART1_RX
|
|
|
+#define DMA_UART_TX_FDT_FLAG DMA1_FDT2_FLAG
|
|
|
+#define SHARK_UART_DMA_CHCNT_RX() (SHARK_UART0_rx_dma_ch->dtcnt)
|
|
|
+
|
|
|
|
|
|
// ================================================================================
|
|
|
#define ENABLE_RX_DMA 1
|
|
|
@@ -61,6 +48,12 @@ static shark_uart_t _shark_uart[1];
|
|
|
static usart_type *_uart_index(usart_type* com){
|
|
|
return SHARK_UART0;
|
|
|
}
|
|
|
+
|
|
|
+__weak void shark_uart_on_frame_received(u8 *data, u16 lenght){
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
static bool shark_uart_on_rx_frame(shark_uart_t *uart)
|
|
|
{
|
|
|
u16 crc0 = decode_u16(uart->rx_frame + uart->rx_length);
|
|
|
@@ -69,17 +62,13 @@ static bool shark_uart_on_rx_frame(shark_uart_t *uart)
|
|
|
if (crc0 != crc1) {
|
|
|
return false;
|
|
|
}
|
|
|
- //protocol_recv_frame(_uart_index(uart->uart_com), (char *)uart->rx_frame, uart->rx_length);
|
|
|
+ shark_uart_on_frame_received(uart->rx_frame, uart->rx_length);
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-static void shark_uart_rx(shark_uart_t *uart){
|
|
|
- while(1) {
|
|
|
- u8 data = 0;
|
|
|
- update_dma_w_pos(uart);
|
|
|
- if (circle_get_one_data(&uart->rx_queue, &data) != 1) {
|
|
|
- return;
|
|
|
- }
|
|
|
+static void shark_uart_rx_data(shark_uart_t *uart, u8 *buff, u16 size){
|
|
|
+ for (u8 *buff_end = buff + size; buff < buff_end; buff++) {
|
|
|
+ u8 data = *buff;
|
|
|
switch(data){
|
|
|
case CH_START:
|
|
|
uart->rx_length = 0;
|
|
|
@@ -128,9 +117,23 @@ static void shark_uart_rx(shark_uart_t *uart){
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+void shark_uart_dma_rx(shark_uart_t *uart)
|
|
|
+{
|
|
|
+ u16 index = uart->rx_index;
|
|
|
+
|
|
|
+ uart->rx_index = SHARK_UART_RX_MEM_SIZE - SHARK_UART_DMA_CHCNT_RX();
|
|
|
+
|
|
|
+ if (uart->rx_index < index) {
|
|
|
+ shark_uart_rx_data(uart, uart->rx_cache + index, SHARK_UART_RX_MEM_SIZE - index);
|
|
|
+ shark_uart_rx_data(uart, uart->rx_cache, uart->rx_index);
|
|
|
+ } else {
|
|
|
+ shark_uart_rx_data(uart, uart->rx_cache + index, uart->rx_index - index);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
#define DMA_CHCTL(dma, dma_ch) ((dma_channel_type *)dma_ch)->ctrl
|
|
|
#define DMA_CHMADDR(dma, dma_ch) ((dma_channel_type *)dma_ch)->maddr
|
|
|
-
|
|
|
#define DMA_CHXCTL_CHEN BIT(0) /*!< channel enable */
|
|
|
|
|
|
static void shark_uart_dma_tx(shark_uart_t *uart)
|
|
|
@@ -138,10 +141,10 @@ static void shark_uart_dma_tx(shark_uart_t *uart)
|
|
|
u32 value = DMA_CHCTL(SHARK_UART0_tx_dma, uart->tx_dma_ch);
|
|
|
|
|
|
if (value & DMA_CHXCTL_CHEN) {
|
|
|
- if (SET != dma_flag_get(DMA1_FDT1_FLAG)) {
|
|
|
+ if (SET != dma_flag_get(DMA_UART_TX_FDT_FLAG)) {
|
|
|
return;
|
|
|
}
|
|
|
- dma_flag_clear(DMA1_FDT1_FLAG);
|
|
|
+ dma_flag_clear(DMA_UART_TX_FDT_FLAG);
|
|
|
byte_queue_skip(&uart->tx_queue, uart->tx_length);
|
|
|
DMA_CHCTL(SHARK_UART0_tx_dma, uart->tx_dma_ch) = value & (~DMA_CHXCTL_CHEN);
|
|
|
}
|
|
|
@@ -154,8 +157,6 @@ static void shark_uart_dma_tx(shark_uart_t *uart)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
static void shark_uart_write(shark_uart_t *uart, const u8 *buff, u16 size)
|
|
|
{
|
|
|
while (size > 0) {
|
|
|
@@ -202,8 +203,8 @@ static void shark_uart_tx_dma_init(shark_uart_t *uart){
|
|
|
dma_init_struct.priority = DMA_PRIORITY_MEDIUM;
|
|
|
dma_init_struct.loop_mode_enable = FALSE;
|
|
|
dma_init(uart->tx_dma_ch, &dma_init_struct);
|
|
|
- /* config flexible dma for usart2 tx */
|
|
|
- //dma_flexible_config(DMA1, FLEX_CHANNEL1, DMA_FLEXIBLE_UART2_TX);
|
|
|
+ /* config flexible dma for usart1 tx */
|
|
|
+ dma_flexible_config(SHARK_UART0_tx_dma, DMA_UART_TX_FLEX_CHANNEL, DMA_UART_TX_FLEX);
|
|
|
|
|
|
usart_dma_transmitter_enable(uart->uart_com, TRUE);
|
|
|
}
|
|
|
@@ -226,9 +227,8 @@ static void shark_uart_rx_dma_init(shark_uart_t *uart){
|
|
|
dma_init_struct.priority = DMA_PRIORITY_MEDIUM;
|
|
|
dma_init_struct.loop_mode_enable = FALSE;
|
|
|
dma_init(uart->rx_dma_ch, &dma_init_struct);
|
|
|
- /* config flexible dma for usart2 rx */
|
|
|
- //dma_flexible_config(DMA1, FLEX_CHANNEL2, DMA_FLEXIBLE_UART2_RX);
|
|
|
-
|
|
|
+ /* config flexible dma for usart1 rx */
|
|
|
+ dma_flexible_config(SHARK_UART0_rx_dma, DMA_UART_RX_FLEX_CHANNEL, DMA_UART_RX_FLEX);
|
|
|
usart_dma_receiver_enable(uart->uart_com, TRUE);
|
|
|
}
|
|
|
#endif
|
|
|
@@ -249,6 +249,8 @@ static void shark_uart_pin_init(shark_uart_t *uart){
|
|
|
gpio_init_struct.gpio_pins = SHARK_UART0_rx_pin;
|
|
|
gpio_init_struct.gpio_pull = GPIO_PULL_UP;
|
|
|
gpio_init(SHARK_UART0_rx_port, &gpio_init_struct);
|
|
|
+ /* remap usart1 tx and rx pins */
|
|
|
+ gpio_pin_remap_config(SHARK_UART0_IOMUX, TRUE);
|
|
|
}
|
|
|
|
|
|
static void shark_uart_pin_deinit(shark_uart_t *uart){
|
|
|
@@ -282,7 +284,7 @@ static u32 shark_uart_task(void *args)
|
|
|
{
|
|
|
shark_uart_t *uart = (shark_uart_t *)args;
|
|
|
if(uart->uart_com != 0) {
|
|
|
- shark_uart_rx(uart);
|
|
|
+ shark_uart_dma_rx(uart);
|
|
|
shark_uart_dma_tx(uart);
|
|
|
}
|
|
|
return 0;
|
|
|
@@ -299,24 +301,6 @@ void shark_uart_flush(void){
|
|
|
}
|
|
|
|
|
|
|
|
|
-#if 0
|
|
|
-void DMA_Channel1_2_IRQHandler(void){
|
|
|
- shark_uart_t *uart = _shark_uart + SHARK_UART0;
|
|
|
- if (dma_interrupt_flag_get(uart->tx_dma_ch, DMA_INT_FLAG_FTF) != RESET){
|
|
|
- shark_uart_dma_tx(uart);
|
|
|
- dma_interrupt_flag_clear(uart->tx_dma_ch, DMA_INT_FLAG_FTF);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void DMA_Channel3_4_IRQHandler(void){
|
|
|
- shark_uart_t *uart = _shark_uart + SHARK_UART1;
|
|
|
- if (dma_interrupt_flag_get(uart->tx_dma_ch, DMA_INT_FLAG_FTF) != RESET){
|
|
|
- shark_uart_dma_tx(uart);
|
|
|
- dma_interrupt_flag_clear(uart->tx_dma_ch, DMA_INT_FLAG_FTF);
|
|
|
- }
|
|
|
-}
|
|
|
-#endif
|
|
|
-
|
|
|
static u8 *tx_cache_addr(uart_enum_t uart_no){
|
|
|
return shark_uart0_tx_cache;
|
|
|
}
|
|
|
@@ -343,14 +327,6 @@ void shark_uart_deinit(uart_enum_t uart_no){
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-bool shark_uart_timeout(void){
|
|
|
-#if UART_NUM==2
|
|
|
- return (_shark_uart[0].uart_no_data && _shark_uart[1].uart_no_data)?TRUE:FALSE;
|
|
|
-#else
|
|
|
- return (_shark_uart[0].uart_no_data)?TRUE:FALSE;
|
|
|
-#endif
|
|
|
-}
|
|
|
void shark_uart_init(uart_enum_t uart_no)
|
|
|
{
|
|
|
shark_uart_t *uart = _shark_uart + uart_no;
|
|
|
@@ -358,8 +334,8 @@ void shark_uart_init(uart_enum_t uart_no)
|
|
|
uart->rx_length = 0;
|
|
|
uart->tx_length = 0;
|
|
|
uart->uart_com = SHARK_UART0_com;
|
|
|
+ uart->rx_cache = rx_cache_addr(uart_no);
|
|
|
|
|
|
- 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 = SHARK_UART0_rx_dma_ch;
|
|
|
@@ -391,10 +367,6 @@ void USART3_IRQHandler(void){
|
|
|
#endif
|
|
|
|
|
|
|
|
|
-void UART_DMA_IRQHandler(void) {
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
static void shark_uart_write_byte_esc(shark_uart_t *uart, u8 value)
|
|
|
{
|
|
|
switch (value) {
|