-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuart.c
More file actions
209 lines (168 loc) · 4.65 KB
/
uart.c
File metadata and controls
209 lines (168 loc) · 4.65 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* UART Interface
*
* Supports serial IO using AT91 UART
* Character reception is interrupt supported:
* ReceiveLine(char* line, int timeout)
* RecvData( char* pData, int Size )
*
* Character transmission via:
* SendLine(char* line)
*
* Call the following to initialise the UART
* UartInit(AT91UartGetchar, AT91UartPutchar);
* AT91InitInterrupt(TimerBeat, UartRxrdy);
* AT91UartInit();
*/
#include <string.h>
#include "config.h"
#include "uart.h"
#include "timer.h"
#include <intrinsic.h>
/* Timeout for reading new data - ms */
#define RD_TIMEOUT 10
/* The receive buffer */
static char rbuf[RXBUF_SIZE];
/* Pointer to next available vacant byte in the buffer */
static volatile int rptr = 0;
/* The serviced routines for reception and transmission via the UART */
static int(*getchar_function)();
static void(*putchar_function)(int);
/*
* Connect UART receive and transmit functions
*/
void UartInit(int(*getchar_func)(), void(*putchar_func)(int)) {
getchar_function = getchar_func;
putchar_function = putchar_func;
}
/*
* UartRxrdy()
*
* Interrupt service for UART Rx
* Character has been received - read and store in buffer
* The buffer index, rptr, points at the next available vacant
* space in the buffer, rbuff[]
*
*/
void UartRxrdy() {
unsigned char value;
value = (*getchar_function)(); /* Read character from UART */
if (rptr >= RXBUF_SIZE) /* Is there enough space? */
return;
rbuf[rptr++] = value; /* Store character and update pointer */
}
/*
* ReceiveLine()
*
* API function to extract all available characters from the serial receive
* buffer within a timebound.
* Returns zero if no characters are available within the timeound.
* If at least one character is available within the timebound it returns
* with the number of characters read and a pointer to the string.
* If at least one character is available, any new characters must be
* available within RD_TIMEOUT ms or they will be ignored.
*
* Reads characters from the serial receive buffer, rbuf[]. The index, rptr,
* points just beyond the last character available. So, the buffer contains
* rbuf[0]..rbuf[rptr-1] characters.
*
* Parameters:
* line: pointer to a string
* timeout: time limit in ms
* Return value: number of characters read
*
* Note - this function is insecure since interrupts are not disabled
* while the buffer is flushed.
*
* WDHenderson, September 2008
*
*/
int ReceiveLine(char* line, int timeout) {
int n;
int elapsed = 0;
while (rptr == 0)
{
Sleep(RD_TIMEOUT);
elapsed += RD_TIMEOUT;
if (timeout && elapsed > timeout)
return 0;
}
for (;;)
{
n = rptr;
Sleep(RD_TIMEOUT);
elapsed += RD_TIMEOUT;
if (n == rptr)
break;
if (timeout && elapsed > timeout)
return 0;
}
rptr = 0;
memcpy(line, rbuf, n);
rbuf[n] = 0;
return n;
}
/*
* RecvData()
*
* Reads precisely size bytes from the serial buffer
* Returns zero of fewer than size bytes are available. Otherwise
* returns the number of bytes read and a pointer to the string
*
* Reads characters from the serial receive buffer, rbuf[]. The index, rptr,
* points just beyond the last character available. So, the buffer contains
* rbuf[0]..rbuf[rptr-1] characters.
*
* Parameters:
* size: number of bytes to be read
* pData: pointer to the start of the character buffer
*
* Documentation and clarification - WDH, September 2008
*
*/
int RecvData( char* pData, int Size ) {
/* Terminate if no data. */
if ( rptr < Size ) { /* Test number of chars available */
return 0; /* Too few chars available */
}
/* NOTE: Disable interrupts here to protect the buffer */
__disable_interrupt();
rptr = rptr - Size; /* Adjust buffer index to remove size chars */
memcpy( pData, &rbuf[0], Size ); /* Copy receive buffer to parameter buffer */
memcpy( rbuf, &rbuf[Size], rptr ); /* Shift remaining chars to front of buffer */
/* NOTE: Reenable interrupts here */
__enable_interrupt();
return Size;
}
/*
* SendLine()
*
* API function to transmit a, null terminated, string
*
* Parameter:
* line: pointer to, null terminated, string
*
*/
void SendLine(char* line) {
for ( ; *line; line++)
{
(*putchar_function)(*line);
}
}
/*
* SendData()
*
* API function to transmit a character string of specified size
*
* Parameters:
* pData: pointer to string
* Size: number of characters to transmit
*
*/
void SendData( char* pData, int Size ) {
int i;
for (i = 0; i < Size; ++i)
{
(*putchar_function)(pData[i]);
}
}