-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUART_program.c
402 lines (349 loc) · 7.46 KB
/
UART_program.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include "Macros.h"
#include "STD_TYPES.h"
#include "TM4C123.h"
#include "UART_interface.h"
void (*UART0_callback) (void);
void (*UART1_callback) (void);
void (*UART2_callback) (void);
void UART0_vidInit(UARTConfig_t * UARTConfig)
{
UART0->CTL = 0; /* UART5 module disbable */
UART0->IBRD = UARTConfig->u16Integer; /* for 9600 baud rate, integer = 104 */
UART0->FBRD = UARTConfig->u8Fraction; /* for 9600 baud rate, fractional = 11*/
/*select system clock*/
if (UARTConfig->u8ClockSource == UART_CLOCKSOURCE_RC)
{
UART0->CC |= 0;
}
else
{
UART0->CC = 0x5;
}
UART0->DR &= 0;
if (UARTConfig->u8FIFOEnabled)
{
SET_BIT(UART0->LCRH,4);
}
else
{
CLEAR_BIT(UART0->LCRH,4);
}
/*Data length*/
switch(UARTConfig->u8WordLength)
{
case UART_WORDSIZE_5:
UART0->LCRH = 0x00;
break;
case UART_WORDSIZE_6:
UART0->LCRH = 0x20;
break;
case UART_WORDSIZE_7:
UART0->LCRH = 0x40;
break;
case UART_WORDSIZE_8:
UART0->LCRH = 0x60; /* data lenght 8-bit, not parity bit, no FIFO */
break;
}
if (UARTConfig->u8HighSpeedEnabled == UART_HIGHSPEED_FALSE)
{
CLEAR_BIT(UART0->CTL,5);
}
else
{
SET_BIT(UART0->CTL,5);
}
/*Select Rx or TX or bot to be enabled*/
switch(UARTConfig->u8RxTx)
{
case UART_RXTX_BOTH:
SET_BIT(UART0->CTL,8);
SET_BIT(UART0->CTL,9);
break;
case UART_RXTX_RX_ONLY:
SET_BIT(UART0->CTL,9);
break;
case UART_RXTX_TX_ONLY:
SET_BIT(UART0->CTL,8);
break;
}
/*Enabled UART*/
SET_BIT(UART0->CTL,0);
if (UARTConfig->u8InterruptEnabled)
{
/*Receive interrupt*/
SET_BIT(UART0->IM,4);
}
else
{
CLEAR_BIT(UART0->IM,4);
}
/*Function to be executed when interrupt occurs*/
UART0_vidPutISRFunction(UARTConfig->ptrF);
}
void UART0_vidPutISRFunction(void(*ptrF)(void))
{
UART0_callback = ptrF;
}
void UART0_vidSendByte(unsigned char data)
{
while((UART0->FR & (1<<5)) != 0); /* wait until Tx buffer not full */
UART0->DR = data; /* before giving it another byte */
}
char UART0_Receiver(void)
{
char data;
while((UART0->FR & (1<<4)) != 0); /* wait until Rx buffer is not full */
data = (char) UART0->DR ; /* before giving it another byte */
return (unsigned char) data;
}
char UART0_u8GetReceivedByte(void)
{
return (u8) UART0->DR;
}
void UART0_vidSendString(char *str)
{
while(*str)
{
UART0_vidSendByte(*(str++));
}
}
void UART0_Handler()
{
UART0_callback();
UART0->ICR = 0x0010;
}
void UART1_vidInit(UARTConfig_t * UARTConfig)
{
UART1->CTL = 0; /* UART5 module disbable */
UART1->IBRD = UARTConfig->u16Integer; /* for 9600 baud rate, integer = 104 */
UART1->FBRD = UARTConfig->u8Fraction; /* for 9600 baud rate, fractional = 11*/
/*select system clock*/
if (UARTConfig->u8ClockSource == UART_CLOCKSOURCE_RC)
{
UART1->CC |= 0;
}
else
{
UART1->CC = 0x5;
}
UART1->DR &= 0;
if (UARTConfig->u8FIFOEnabled)
{
SET_BIT(UART1->LCRH,4);
}
else
{
CLEAR_BIT(UART1->LCRH,4);
}
/*Data length*/
switch(UARTConfig->u8WordLength)
{
case UART_WORDSIZE_5:
UART1->LCRH = 0x00;
break;
case UART_WORDSIZE_6:
UART1->LCRH = 0x20;
break;
case UART_WORDSIZE_7:
UART1->LCRH = 0x40;
break;
case UART_WORDSIZE_8:
UART1->LCRH = 0x60; /* data lenght 8-bit, not parity bit, no FIFO */
break;
}
if (UARTConfig->u8HighSpeedEnabled == UART_HIGHSPEED_FALSE)
{
CLEAR_BIT(UART1->CTL,5);
}
else
{
SET_BIT(UART1->CTL,5);
}
/*Select Rx or TX or bot to be enabled*/
switch(UARTConfig->u8RxTx)
{
case UART_RXTX_BOTH:
SET_BIT(UART1->CTL,8);
SET_BIT(UART1->CTL,9);
break;
case UART_RXTX_RX_ONLY:
SET_BIT(UART1->CTL,9);
break;
case UART_RXTX_TX_ONLY:
SET_BIT(UART1->CTL,8);
break;
}
/*Enabled UART*/
SET_BIT(UART1->CTL,0);
if (UARTConfig->u8InterruptEnabled)
{
/*Receive interrupt*/
SET_BIT(UART1->IM,4);
}
else
{
CLEAR_BIT(UART1->IM,4);
}
}
void UART1_vidPutISRFunction(void(*ptrF)(void))
{
UART1_callback = ptrF;
}
void UART1_vidSendByte(unsigned char data)
{
while((UART1->FR & (1<<5)) != 0); /* wait until Tx buffer not full */
UART1->DR = data; /* before giving it another byte */
}
char UART1_Receiver(void)
{
char data;
while((UART1->FR & (1<<4)) != 0); /* wait until Rx buffer is not full */
data = (char) UART1->DR ; /* before giving it another byte */
return (unsigned char) data;
}
char UART1_u8GetReceivedByte(void)
{
return (u8) UART1->DR;
}
void UART1_vidSendString(char *str)
{
while(*str)
{
UART1_vidSendByte(*(str++));
}
}
void UART1_Handler()
{
UART1_callback();
UART1->ICR = 0x0010;
}
void UART2_vidInit(UARTConfig_t * UARTConfig)
{
UART2->CTL = 0; /* UART5 module disbable */
UART2->IBRD = UARTConfig->u16Integer; /* for 9600 baud rate, integer = 104 */
UART2->FBRD = UARTConfig->u8Fraction; /* for 9600 baud rate, fractional = 11*/
/*select system clock*/
if (UARTConfig->u8ClockSource == UART_CLOCKSOURCE_RC)
{
UART2->CC |= 0;
}
else
{
UART2->CC = 0x5;
}
UART2->DR &= 0;
if (UARTConfig->u8FIFOEnabled)
{
SET_BIT(UART2->LCRH,4);
}
else
{
CLEAR_BIT(UART2->LCRH,4);
}
/*Data length*/
switch(UARTConfig->u8WordLength)
{
case UART_WORDSIZE_5:
UART2->LCRH = 0x00;
break;
case UART_WORDSIZE_6:
UART2->LCRH = 0x20;
break;
case UART_WORDSIZE_7:
UART2->LCRH = 0x40;
break;
case UART_WORDSIZE_8:
UART2->LCRH = 0x60; /* data lenght 8-bit, not parity bit, no FIFO */
break;
}
if (UARTConfig->u8HighSpeedEnabled == UART_HIGHSPEED_FALSE)
{
CLEAR_BIT(UART2->CTL,5);
}
else
{
SET_BIT(UART2->CTL,5);
}
/*Select Rx or TX or bot to be enabled*/
switch(UARTConfig->u8RxTx)
{
case UART_RXTX_BOTH:
SET_BIT(UART2->CTL,8);
SET_BIT(UART2->CTL,9);
break;
case UART_RXTX_RX_ONLY:
SET_BIT(UART2->CTL,9);
break;
case UART_RXTX_TX_ONLY:
SET_BIT(UART2->CTL,8);
break;
}
/*Enabled UART*/
SET_BIT(UART2->CTL,0);
if (UARTConfig->u8InterruptEnabled)
{
/*Receive interrupt*/
SET_BIT(UART2->IM,4);
}
else
{
CLEAR_BIT(UART2->IM,4);
}
}
void UART2_vidPutISRFunction(void(*ptrF)(void))
{
UART2_callback = ptrF;
}
void UART2_vidSendByte(unsigned char data)
{
while((UART2->FR & (1<<5)) != 0); /* wait until Tx buffer not full */
UART2->DR = data; /* before giving it another byte */
}
char UART2_Receiver(void)
{
char data;
while((UART2->FR & (1<<4)) != 0); /* wait until Rx buffer is not full */
data = (char) UART2->DR ; /* before giving it another byte */
return (unsigned char) data;
}
char UART2_u8GetReceivedByte(void)
{
return (u8) UART2->DR;
}
void UART2_vidSendString(char *str)
{
while(*str)
{
UART2_vidSendByte(*(str++));
}
}
void UART2_Handler()
{
UART2_callback();
UART2->ICR = 0x0010;
}
void UART_vidSendNumber(void (*ptrF) (u8),u16 u16Number)
{
if(u16Number < 10) {
ptrF('0');
ptrF(u16Number+'0');
}
else {
if (u16Number < 100) {
ptrF(u16Number/10+'0');
ptrF(u16Number%10+'0');
}
else if (u16Number < 1000) {
ptrF((u16Number/100)+'0');
ptrF((u16Number%100)/10+'0');
ptrF((u16Number%100)%10+'0');
}
else if (u16Number < 10000)
{
ptrF((u16Number/1000)+'0');
ptrF((u16Number%1000)/100+'0');
ptrF((u16Number%100)/10+'0');
ptrF((u16Number%100)%10+'0');
}
}
}