-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_uart.c
More file actions
106 lines (85 loc) · 2.8 KB
/
app_uart.c
File metadata and controls
106 lines (85 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "tl_common.h"
#include "drivers.h"
#include "app_uart.h"
#define UART_DATA_LEN 12 //data max ? (UART_DATA_LEN+4) must 16 byte aligned
typedef struct{
unsigned int dma_len; // dma len must be 4 byte
unsigned char data[UART_DATA_LEN];
}uart_data_t;
uart_data_t rec_buff = {0, {0, } };
uart_data_t trans_buff = {0, {0,} };
void app_uart_init(void)
{
//note: dma addr must be set first before any other uart initialization! (confirmed by sihui)
uart_recbuff_init((unsigned char *)&rec_buff, sizeof(rec_buff));
uart_gpio_set(UART_TX_PB1, UART_RX_PA0);// uart tx/rx pin set
uart_reset(); //will reset uart digital registers from 0x90 ~ 0x9f, so uart setting must set after this reset
//baud rate: 115200
#if (CLOCK_SYS_CLOCK_HZ == 16000000)
uart_init(9, 13, PARITY_NONE, STOP_BIT_ONE);
#elif (CLOCK_SYS_CLOCK_HZ == 24000000)
uart_init(12, 15, PARITY_NONE, STOP_BIT_ONE);
#endif
uart_dma_enable(1, 1); //uart data in hardware buffer moved by dma, so we need enable them first
irq_set_mask(FLD_IRQ_DMA_EN);
dma_chn_irq_enable(FLD_DMA_CHN_UART_RX | FLD_DMA_CHN_UART_TX, 1); //uart Rx/Tx dma irq enable
uart_irq_enable(0, 0); //uart Rx/Tx irq no need, disable them
irq_enable();
}
void at_print(char * str)
{
while(*str)
{
trans_buff.data[trans_buff.dma_len] = *str++;
trans_buff.dma_len += 1;
if(trans_buff.dma_len == 12)
{
uart_dma_send((unsigned char*)&trans_buff);
trans_buff.dma_len = 0;
WaitMs(2);
}
}
if(trans_buff.dma_len)
{
uart_dma_send((unsigned char*)&trans_buff);
trans_buff.dma_len = 0;
WaitMs(2);
}
}
void at_send(char * data, u32 len)
{
while(len > UART_DATA_LEN)
{
memcpy(trans_buff.data, data, UART_DATA_LEN);
data += UART_DATA_LEN;
len -= UART_DATA_LEN;
trans_buff.dma_len = UART_DATA_LEN;
uart_dma_send((unsigned char*)&trans_buff);
trans_buff.dma_len = 0;
WaitMs(2);
}
if(len > 0)
{
memcpy(trans_buff.data, data, len);
trans_buff.dma_len = len;
uart_dma_send((unsigned char*)&trans_buff);
trans_buff.dma_len = 0;
WaitMs(2);
}
}
void app_uart_irq_proc(void)
{
unsigned char uart_dma_irqsrc;
//1. UART irq
uart_dma_irqsrc = dma_chn_irq_status_get();///in function,interrupt flag have already been cleared,so need not to clear DMA interrupt flag here
if(uart_dma_irqsrc & FLD_DMA_CHN_UART_RX)
{
//Received uart data in rec_buff, user can copy data here
rec_buff.dma_len = 0;
dma_chn_irq_status_clr(FLD_DMA_CHN_UART_RX);
}
if(uart_dma_irqsrc & FLD_DMA_CHN_UART_TX)
{
dma_chn_irq_status_clr(FLD_DMA_CHN_UART_TX);
}
}