-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuart.c
173 lines (151 loc) · 4.15 KB
/
uart.c
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "uart.h"
uint8_t uartMemory[ADI_UART_BIDIR_MEMORY_SIZE];
uint32_t uart_hErrors = 0;
ADI_UART_HANDLE uartDevice = NULL;
uint8_t rx_buffer[RX_BUFFER_SIZE];
uint8_t rx_buffer_b = 0;
uint8_t rx_buffer_e = 0;
uint8_t rx_buffer_size = 0;
uint8_t rx_buffer_overflow = 0;
void uartCallback(void* pAppHandle, uint32_t nEvent, void* pArg)
{
switch (nEvent)
{
case ADI_UART_EVENT_TX_BUFFER_PROCESSED:
break;
case ADI_UART_EVENT_RX_BUFFER_PROCESSED:
adi_pwr_ExitLowPowerMode(NULL);
break;
case ADI_UART_EVENT_NO_RX_BUFFER_EVENT:
adi_uart_SubmitRxBuffer(uartDevice, &rx_buffer[rx_buffer_e], 1, 0u);
rx_buffer_e = (rx_buffer_e+1)%RX_BUFFER_SIZE;
rx_buffer_size++;
if(rx_buffer_e == rx_buffer_b)
{
rx_buffer_overflow = 1;
}
break;
default:
break;
}
}
ADI_UART_RESULT uartSetup(uint32_t baudrate)
{
ADI_UART_RESULT result;
if((result = adi_uart_Open(0, ADI_UART_DIR_BIDIRECTION, uartMemory, ADI_UART_BIDIR_MEMORY_SIZE, &uartDevice)) != ADI_UART_SUCCESS)
{
return result;
}
if((result = adi_uart_RegisterCallback(uartDevice, uartCallback, NULL)) != ADI_UART_SUCCESS)
{
return result;
}
if((result = adi_uart_SetConfiguration(uartDevice, ADI_UART_NO_PARITY, ADI_UART_ONE_STOPBIT, ADI_UART_WORDLEN_8BITS)) != ADI_UART_SUCCESS)
{
return result;
}
if((result = adi_uart_EnableFifo(uartDevice, true)) != ADI_UART_SUCCESS)
{
return result;
}
if((result = adi_uart_SetRxFifoTriggerLevel(uartDevice, ADI_UART_RX_FIFO_TRIG_LEVEL_1BYTE)) != ADI_UART_SUCCESS)
{
return result;
}
switch(baudrate)
{
case 9600:
result = adi_uart_ConfigBaudRate(uartDevice, 22, 3, 1734, 3);
break;
case 19200:
result = adi_uart_ConfigBaudRate(uartDevice, 11, 3, 1735, 3);
break;
case 38400:
result = adi_uart_ConfigBaudRate(uartDevice, 17, 1, 501, 3);
break;
case 57600:
result = adi_uart_ConfigBaudRate(uartDevice, 7, 2, 31, 3);
break;
case 115200:
result = adi_uart_ConfigBaudRate(uartDevice, 7, 2, 31, 2);
break;
case 230400:
result = adi_uart_ConfigBaudRate(uartDevice, 7, 2, 31, 1);
break;
case 460800:
result = adi_uart_ConfigBaudRate(uartDevice, 7, 2, 31, 0);
break;
default:
return ADI_UART_INVALID_PARAMETER;
}
if(result)
{
return result;
}
return ADI_UART_SUCCESS;
}
uint8_t uartRead()
{
adi_uart_RegisterCallback(uartDevice, NULL, NULL);
if(rx_buffer_size>0)
{
uint8_t temp = rx_buffer[rx_buffer_b];
rx_buffer_b = (rx_buffer_b+1)%RX_BUFFER_SIZE;
rx_buffer_size--;
adi_uart_RegisterCallback(uartDevice, uartCallback, NULL);
return temp;
}
adi_uart_RegisterCallback(uartDevice, uartCallback, NULL);
return -1;
}
int uartReadBuffer(uint8_t* buf, uint32_t len)
{
adi_uart_RegisterCallback(uartDevice, NULL, NULL);
if(rx_buffer_size>=len)
{
if(rx_buffer_b < rx_buffer_e && rx_buffer_b+len>= RX_BUFFER_SIZE)
{
memcpy(buf, &rx_buffer[rx_buffer_b], RX_BUFFER_SIZE-rx_buffer_b);
memcpy(&buf[RX_BUFFER_SIZE-rx_buffer_b], &rx_buffer[0], len-(RX_BUFFER_SIZE-rx_buffer_b));
rx_buffer_b = (rx_buffer_b+len)%RX_BUFFER_SIZE;
rx_buffer_size-=len;
}
else
{
memcpy(buf, &rx_buffer[rx_buffer_b], len);
rx_buffer_b = (rx_buffer_b+len)%RX_BUFFER_SIZE;
rx_buffer_size-=len;
}
adi_uart_RegisterCallback(uartDevice, uartCallback, NULL);
return 0;
}
adi_uart_RegisterCallback(uartDevice, uartCallback, NULL);
return 1;
}
uint32_t uart_available()
{
return rx_buffer_size;
}
ADI_UART_RESULT uartWrite(uint8_t byte)
{
return adi_uart_Write(uartDevice, &byte, 1, true, &uart_hErrors);
}
ADI_UART_RESULT uartWriteBuffer(uint8_t* buffer, uint32_t len)
{
return adi_uart_Write(uartDevice, buffer, len, true, &uart_hErrors);
}
ADI_UART_RESULT async_uartWrite(uint8_t byte)
{
return adi_uart_SubmitTxBuffer(uartDevice, &byte, 1, true);
}
ADI_UART_RESULT async_uartWriteBuffer(uint8_t* buffer, uint32_t len)
{
return adi_uart_SubmitTxBuffer(uartDevice, buffer, len, true);
}
void uartFlush()
{
uint8_t rx_buffer_b = 0;
uint8_t rx_buffer_e = 0;
uint8_t rx_buffer_size = 0;
uint8_t rx_buffer_overflow = 0;
}