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
|
//*****************************************************************************
//
// softuart_echo.c - Example for reading data from and writing data to the
// SoftUART.
//
// Copyright (c) 2012-2014 Texas Instruments Incorporated. All rights reserved.
// Software License Agreement
//
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 2.1.0.12573 of the DK-TM4C123G Firmware Package.
//
//*****************************************************************************
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/rom.h"
#include "driverlib/sysctl.h"
#include "driverlib/timer.h"
#include "grlib/grlib.h"
#include "drivers/cfal96x64x16.h"
#include "utils/softuart.h"
//*****************************************************************************
//
//! \addtogroup example_list
//! <h1>SoftUART Echo (softuart_echo)</h1>
//!
//! This example application utilizes the SoftUART to echo text. The SoftUART
//! is configured to use the same pins as the first UART (connected to the FTDI
//! virtual serial port on the evaluation board), at 115,200 baud, 8-n-1 mode.
//! All characters received on the SoftUART are transmitted back to the
//! SoftUART.
//
//*****************************************************************************
//*****************************************************************************
//
// The instance data for the SoftUART module.
//
//*****************************************************************************
static tSoftUART g_sUART;
//*****************************************************************************
//
// The buffer to be used for the SoftUART transmit buffer.
//
//*****************************************************************************
static uint8_t g_pui8TxBuffer[64];
//*****************************************************************************
//
// The buffer to be used for the SoftUART receive buffer.
//
//*****************************************************************************
static uint16_t g_pui16RxBuffer[64];
//*****************************************************************************
//
// The number of processor clocks in the time period of a single bit on the
// SoftUART interface.
//
//*****************************************************************************
static uint32_t g_ui32BitTime;
//*****************************************************************************
//
// A flag that is set in the SoftUART "interrupt" handler when there are
// characters in the receive buffer that need to be read.
//
//*****************************************************************************
static volatile uint32_t g_ui32Flag = 0;
//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif
//*****************************************************************************
//
// The interrupt handler for the SoftUART transmit timer interrupt.
//
//*****************************************************************************
void
Timer0AIntHandler(void)
{
//
// Clear the timer interrupt.
//
ROM_TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
//
// Call the SoftUART transmit timer tick function.
//
SoftUARTTxTimerTick(&g_sUART);
}
//*****************************************************************************
//
// The interrupt handler for the SoftUART receive timer interrupt.
//
//*****************************************************************************
void
Timer0BIntHandler(void)
{
//
// Clear the timer interrupt.
//
ROM_TimerIntClear(TIMER0_BASE, TIMER_TIMB_TIMEOUT);
//
// Call the SoftUART receive timer tick function, and see if the timer
// should be disabled.
//
if(SoftUARTRxTick(&g_sUART, false) == SOFTUART_RXTIMER_END)
{
//
// Disable the timer interrupt since the SoftUART doesn't need it any
// longer.
//
ROM_TimerDisable(TIMER0_BASE, TIMER_B);
}
}
//*****************************************************************************
//
// The interrupt handler for the SoftUART GPIO edge interrupt.
//
//*****************************************************************************
void
GPIOAIntHandler(void)
{
//
// Configure the SoftUART receive timer so that it will sample at the
// mid-bit time of this character.
//
ROM_TimerDisable(TIMER0_BASE, TIMER_B);
ROM_TimerLoadSet(TIMER0_BASE, TIMER_B, g_ui32BitTime);
ROM_TimerIntClear(TIMER0_BASE, TIMER_TIMB_TIMEOUT);
ROM_TimerEnable(TIMER0_BASE, TIMER_B);
//
// Call the SoftUART receive timer tick function.
//
SoftUARTRxTick(&g_sUART, true);
}
//*****************************************************************************
//
// The SoftUART "interrupt" handler.
//
//*****************************************************************************
void
SoftUARTIntHandler(void)
{
uint32_t ui32Status;
//
// Get the interrrupt status.
//
ui32Status = SoftUARTIntStatus(&g_sUART, true);
//
// Clear the asserted interrupts.
//
SoftUARTIntClear(&g_sUART, ui32Status);
//
// Set the flag indicating that there are characters to be read from the
// receive buffer. This is done instead of reading the characters here in
// order to minimize the amount of time spent in the "interrupt" handler
// (which is important at higher baud rates).
//
g_ui32Flag = 1;
}
//*****************************************************************************
//
// Send a string to the UART.
//
//*****************************************************************************
void
UARTSend(const uint8_t *pui8Buffer, uint32_t ui32Count)
{
//
// Loop while there are more characters to send.
//
while(ui32Count--)
{
//
// Write the next character to the UART.
//
SoftUARTCharPut(&g_sUART, *pui8Buffer++);
}
}
//*****************************************************************************
//
// This example demonstrates how to send a string of data to the UART.
//
//*****************************************************************************
int
main(void)
{
tRectangle sRect;
tContext sContext;
//
// Set the clocking to run at 50 MHz from the PLL.
//
ROM_SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_16MHZ);
//
// Initialize the display driver.
//
CFAL96x64x16Init();
//
// Initialize the graphics context.
//
GrContextInit(&sContext, &g_sCFAL96x64x16);
//
// Fill the top part of the screen with blue to create the banner.
//
sRect.i16XMin = 0;
sRect.i16YMin = 0;
sRect.i16XMax = GrContextDpyWidthGet(&sContext) - 1;
sRect.i16YMax = 9;
GrContextForegroundSet(&sContext, ClrDarkBlue);
GrRectFill(&sContext, &sRect);
//
// Change foreground for white text.
//
GrContextForegroundSet(&sContext, ClrWhite);
//
// Put the application name in the middle of the banner.
//
GrContextFontSet(&sContext, g_psFontFixed6x8);
GrStringDrawCentered(&sContext, "softuart-echo", -1,
GrContextDpyWidthGet(&sContext) / 2, 4, 0);
//
// Initialize the display and write some instructions.
//
GrStringDrawCentered(&sContext, "Connect a", -1,
GrContextDpyWidthGet(&sContext) / 2, 20, false);
GrStringDrawCentered(&sContext, "terminal", -1,
GrContextDpyWidthGet(&sContext) / 2, 30, false);
GrStringDrawCentered(&sContext, "to UART0.", -1,
GrContextDpyWidthGet(&sContext) / 2, 40, false);
GrStringDrawCentered(&sContext, "115000,N,8,1", -1,
GrContextDpyWidthGet(&sContext) / 2, 50, false);
//
// Enable the peripherals used by this example.
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
//
// Compute the bit time for 115,200 baud.
//
g_ui32BitTime = (ROM_SysCtlClockGet() / 115200) - 1;
//
// Configure the SoftUART for 8-N-1 operation.
//
SoftUARTInit(&g_sUART);
SoftUARTRxGPIOSet(&g_sUART, GPIO_PORTA_BASE, GPIO_PIN_0);
SoftUARTTxGPIOSet(&g_sUART, GPIO_PORTA_BASE, GPIO_PIN_1);
SoftUARTRxBufferSet(&g_sUART, g_pui16RxBuffer,
sizeof(g_pui16RxBuffer) / sizeof(g_pui16RxBuffer[0]));
SoftUARTTxBufferSet(&g_sUART, g_pui8TxBuffer, sizeof(g_pui8TxBuffer));
SoftUARTCallbackSet(&g_sUART, SoftUARTIntHandler);
SoftUARTConfigSet(&g_sUART,
(SOFTUART_CONFIG_WLEN_8 | SOFTUART_CONFIG_STOP_ONE |
SOFTUART_CONFIG_PAR_NONE));
//
// Configure the timer for the SoftUART transmitter.
//
ROM_TimerConfigure(TIMER0_BASE,
(TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PERIODIC |
TIMER_CFG_B_PERIODIC));
ROM_TimerLoadSet(TIMER0_BASE, TIMER_A, g_ui32BitTime);
ROM_TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT | TIMER_TIMB_TIMEOUT);
ROM_TimerEnable(TIMER0_BASE, TIMER_A);
//
// Set the priorities of the interrupts associated with the SoftUART. The
// receiver is higher priority than the transmitter, and the receiver edge
// interrupt is higher priority than the receiver timer interrupt.
//
ROM_IntPrioritySet(INT_GPIOA, 0x00);
ROM_IntPrioritySet(INT_TIMER0B, 0x40);
ROM_IntPrioritySet(INT_TIMER0A, 0x80);
//
// Enable the interrupts associated with the SoftUART.
//
ROM_IntEnable(INT_TIMER0A);
ROM_IntEnable(INT_TIMER0B);
ROM_IntEnable(INT_GPIOA);
//
// Prompt for text to be entered.
//
UARTSend((uint8_t *)"\033[2JEnter text: ", 16);
//
// Enable the SoftUART interrupt.
//
SoftUARTIntEnable(&g_sUART, SOFTUART_INT_RX | SOFTUART_INT_RT);
//
// Loop forever echoing data through the UART.
//
while(1)
{
//
// Wait until there are characters available in the receive buffer.
//
while(g_ui32Flag == 0)
{
}
g_ui32Flag = 0;
//
// Loop while there are characters in the receive buffer.
//
while(SoftUARTCharsAvail(&g_sUART))
{
//
// Read the next character from the UART and write it back to the
// UART.
//
SoftUARTCharPutNonBlocking(&g_sUART,
SoftUARTCharGetNonBlocking(&g_sUART));
}
}
}
|