-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwr_uart.c
197 lines (168 loc) · 4.95 KB
/
wr_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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "wr_uart.h"
#include "wr_console.h"
#include "inc/lm3s811.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_ints.h"
#include "driverlib/gpio.h"
#include "driverlib/uart.h"
#include "driverlib/interrupt.h"
//*****************************************************************************
//
// The UART interrupt handler.
//
//*****************************************************************************
void UART0IntHandler(void)
{
unsigned long ulStatus;
long tempChar; //current char from UART
//
// Get the interrrupt status.
//
ulStatus = UARTIntStatus(UART0_BASE, true);
//
// Clear the asserted interrupts.
//
UARTIntClear(UART0_BASE, ulStatus);
//
// Loop while there are characters in the receive FIFO.
//
while(UARTCharsAvail(UART0_BASE))
{
//
// Read the next character from the UART and write it back to the UART.
//
tempChar = UARTCharGetNonBlocking(UART0_BASE);
WRConsoleCharPut(UART0_BASE, tempChar);
}
}
void UART1IntHandler(void)
{
unsigned long ulStatus;
long tempChar; //current char from UART
//
// Get the interrrupt status.
//
ulStatus = UARTIntStatus(UART1_BASE, true);
//
// Clear the asserted interrupts.
//
UARTIntClear(UART1_BASE, ulStatus);
//
// Loop while there are characters in the receive FIFO.
//
while(UARTCharsAvail(UART1_BASE))
{
//
// Read the next character from the UART and write it back to the UART.
//
tempChar = UARTCharGetNonBlocking(UART1_BASE);
WRConsoleCharPut(UART1_BASE, tempChar);
}
}
//*****************************************************************************
//
// Send the string to the UART.
//
//*****************************************************************************
long timeOfOneByteSending(unsigned long ulBase)
{
// CPU rate is SysCtlClockGet (6 Mhz)
// in SysCtlDelay method one loop takes 3 cycle
// configuration of UART as 8-N-1
// so one byte contain 9 bit and each sending each bit take 6M / baud rate
// (1000 / (baud rate of connection)) * 9 for sending of one byte
// (cpu clock / 1000) cycles of cpu in 1ms
// (9000 / baud) * (cpu clock / 1000) * 3 - loop of SysTlcDelay
// But call a lot of function also take much time
// because I can't estimate that time so ignore it =(
long baud = 0;
if( ulBase == UART0_BASE )
baud = WR_UART0_BAUD;
if( ulBase == UART1_BASE )
baud = WR_UART1_BAUD;
return (3*9*SysCtlClockGet()) / baud;
}
void UARTStrSend(unsigned long ulBase, const char *pucBuffer)
{
//
// Loop while there are more characters to send.
//
long delay = timeOfOneByteSending(ulBase);
while(*pucBuffer)
{
//
// Write the next character to the UART.
//
UARTCharPutNonBlocking(ulBase, (unsigned char)*pucBuffer++);
//
// Delay
//
SysCtlDelay(delay);
}
}
char* WR_HELLO_STR = "I'm Walking Robot! What I have to do? : ";
void WRUART0Init(void)
{
//
// Enable the peripherals used by this example.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
//
// Enable processor interrupts.
//
IntMasterEnable();
//
// Set GPIO A0 and A1 as UART pins.
//
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
//
// Configure the UART for 115,200, 8-N-1 operation.
//
UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), WR_UART0_BAUD,
(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
UART_CONFIG_PAR_NONE));
//
// Enable the UART interrupt.
//
IntPrioritySet( INT_UART0, 7 );
IntEnable(INT_UART0);
UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT);
//
// Prompt for text to be entered.
//
UARTStrSend(UART0_BASE, WR_HELLO_STR);
}
void WRUART1Init(void)
{
//
// Enable the peripherals used by this example.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART1);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
//
// Enable processor interrupts.
//
IntMasterEnable();
//
// Set GPIO PD2 and PD3 as UART pins.
//
GPIOPinTypeUART(GPIO_PORTD_BASE, GPIO_PIN_2 | GPIO_PIN_3);
//
// Configure the UART for 9,600 8-N-1 operation.
//
UARTConfigSetExpClk(UART1_BASE, SysCtlClockGet(), WR_UART1_BAUD,
(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
UART_CONFIG_PAR_NONE));
//
// Enable the UART interrupt.
//
IntPrioritySet( INT_UART1, 7 );
IntEnable(INT_UART1);
UARTIntEnable(UART1_BASE, UART_INT_RX | UART_INT_RT);
//
// Prompt for text to be entered.
//
UARTStrSend(UART1_BASE, "WR_HELLO_STR");
}