This repository has been archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uartstdio.c
1733 lines (1577 loc) · 54.9 KB
/
uartstdio.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//*****************************************************************************
//
// uartstdio.c - Utility driver to provide simple UART console functions.
//
// Copyright (c) 2007-2012 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 9453 of the Stellaris Firmware Development Package.
//
//*****************************************************************************
#include <stdarg.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_uart.h"
#include "driverlib/debug.h"
#include "driverlib/interrupt.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "uartstdio.h"
//*****************************************************************************
//
//! \addtogroup uartstdio_api
//! @{
//
//*****************************************************************************
//*****************************************************************************
//
// If buffered mode is defined, set aside RX and TX buffers and read/write
// pointers to control them.
//
//*****************************************************************************
#ifdef UART_BUFFERED
//*****************************************************************************
//
// This global controls whether or not we are echoing characters back to the
// transmitter. By default, echo is enabled but if using this module as a
// convenient method of implementing a buffered serial interface over which
// you will be running an application protocol, you are likely to want to
// disable echo by calling UARTEchoSet(false).
//
//*****************************************************************************
static tBoolean g_bDisableEcho;
//*****************************************************************************
//
// Output ring buffer. Buffer is full if g_ulUARTTxReadIndex is one ahead of
// g_ulUARTTxWriteIndex. Buffer is empty if the two indices are the same.
//
//*****************************************************************************
static unsigned char g_pcUARTTxBuffer[UART_TX_BUFFER_SIZE];
static volatile unsigned long g_ulUARTTxWriteIndex = 0;
static volatile unsigned long g_ulUARTTxReadIndex = 0;
//*****************************************************************************
//
// Input ring buffer. Buffer is full if g_ulUARTTxReadIndex is one ahead of
// g_ulUARTTxWriteIndex. Buffer is empty if the two indices are the same.
//
//*****************************************************************************
static unsigned char g_pcUARTRxBuffer[UART_RX_BUFFER_SIZE];
static volatile unsigned long g_ulUARTRxWriteIndex = 0;
static volatile unsigned long g_ulUARTRxReadIndex = 0;
//*****************************************************************************
//
// Macros to determine number of free and used bytes in the transmit buffer.
//
//*****************************************************************************
#define TX_BUFFER_USED (GetBufferCount(&g_ulUARTTxReadIndex, \
&g_ulUARTTxWriteIndex, \
UART_TX_BUFFER_SIZE))
#define TX_BUFFER_FREE (UART_TX_BUFFER_SIZE - TX_BUFFER_USED)
#define TX_BUFFER_EMPTY (IsBufferEmpty(&g_ulUARTTxReadIndex, \
&g_ulUARTTxWriteIndex))
#define TX_BUFFER_FULL (IsBufferFull(&g_ulUARTTxReadIndex, \
&g_ulUARTTxWriteIndex, \
UART_TX_BUFFER_SIZE))
#define ADVANCE_TX_BUFFER_INDEX(Index) \
(Index) = ((Index) + 1) % UART_TX_BUFFER_SIZE
//*****************************************************************************
//
// Macros to determine number of free and used bytes in the receive buffer.
//
//*****************************************************************************
#define RX_BUFFER_USED (GetBufferCount(&g_ulUARTRxReadIndex, \
&g_ulUARTRxWriteIndex, \
UART_RX_BUFFER_SIZE))
#define RX_BUFFER_FREE (UART_RX_BUFFER_SIZE - RX_BUFFER_USED)
#define RX_BUFFER_EMPTY (IsBufferEmpty(&g_ulUARTRxReadIndex, \
&g_ulUARTRxWriteIndex))
#define RX_BUFFER_FULL (IsBufferFull(&g_ulUARTRxReadIndex, \
&g_ulUARTRxWriteIndex, \
UART_RX_BUFFER_SIZE))
#define ADVANCE_RX_BUFFER_INDEX(Index) \
(Index) = ((Index) + 1) % UART_RX_BUFFER_SIZE
#endif
//*****************************************************************************
//
// The base address of the chosen UART.
//
//*****************************************************************************
static unsigned long g_ulBase = 0;
//*****************************************************************************
//
// A mapping from an integer between 0 and 15 to its ASCII character
// equivalent.
//
//*****************************************************************************
static const char * const g_pcHex = "0123456789abcdef";
//*****************************************************************************
//
// The list of possible base addresses for the console UART.
//
//*****************************************************************************
static const unsigned long g_ulUARTBase[3] =
{
UART0_BASE, UART1_BASE, UART2_BASE
};
#ifdef UART_BUFFERED
//*****************************************************************************
//
// The list of possible interrupts for the console UART.
//
//*****************************************************************************
static const unsigned long g_ulUARTInt[3] =
{
INT_UART0, INT_UART1, INT_UART2
};
//*****************************************************************************
//
// The port number in use.
//
//*****************************************************************************
static unsigned long g_ulPortNum;
#endif
//*****************************************************************************
//
// The list of UART peripherals.
//
//*****************************************************************************
static const unsigned long g_ulUARTPeriph[3] =
{
SYSCTL_PERIPH_UART0, SYSCTL_PERIPH_UART1, SYSCTL_PERIPH_UART2
};
//*****************************************************************************
//
//! Determines whether the ring buffer whose pointers and size are provided
//! is full or not.
//!
//! \param pulRead points to the read index for the buffer.
//! \param pulWrite points to the write index for the buffer.
//! \param ulSize is the size of the buffer in bytes.
//!
//! This function is used to determine whether or not a given ring buffer is
//! full. The structure of the code is specifically to ensure that we do not
//! see warnings from the compiler related to the order of volatile accesses
//! being undefined.
//!
//! \return Returns \b true if the buffer is full or \b false otherwise.
//
//*****************************************************************************
#ifdef UART_BUFFERED
static tBoolean
IsBufferFull(volatile unsigned long *pulRead,
volatile unsigned long *pulWrite, unsigned long ulSize)
{
unsigned long ulWrite;
unsigned long ulRead;
ulWrite = *pulWrite;
ulRead = *pulRead;
return((((ulWrite + 1) % ulSize) == ulRead) ? true : false);
}
#endif
//*****************************************************************************
//
//! Determines whether the ring buffer whose pointers and size are provided
//! is empty or not.
//!
//! \param pulRead points to the read index for the buffer.
//! \param pulWrite points to the write index for the buffer.
//!
//! This function is used to determine whether or not a given ring buffer is
//! empty. The structure of the code is specifically to ensure that we do not
//! see warnings from the compiler related to the order of volatile accesses
//! being undefined.
//!
//! \return Returns \b true if the buffer is empty or \b false otherwise.
//
//*****************************************************************************
#ifdef UART_BUFFERED
static tBoolean
IsBufferEmpty(volatile unsigned long *pulRead,
volatile unsigned long *pulWrite)
{
unsigned long ulWrite;
unsigned long ulRead;
ulWrite = *pulWrite;
ulRead = *pulRead;
return((ulWrite == ulRead) ? true : false);
}
#endif
//*****************************************************************************
//
//! Determines the number of bytes of data contained in a ring buffer.
//!
//! \param pulRead points to the read index for the buffer.
//! \param pulWrite points to the write index for the buffer.
//! \param ulSize is the size of the buffer in bytes.
//!
//! This function is used to determine how many bytes of data a given ring
//! buffer currently contains. The structure of the code is specifically to
//! ensure that we do not see warnings from the compiler related to the order
//! of volatile accesses being undefined.
//!
//! \return Returns the number of bytes of data currently in the buffer.
//
//*****************************************************************************
#ifdef UART_BUFFERED
static unsigned long
GetBufferCount(volatile unsigned long *pulRead,
volatile unsigned long *pulWrite, unsigned long ulSize)
{
unsigned long ulWrite;
unsigned long ulRead;
ulWrite = *pulWrite;
ulRead = *pulRead;
return((ulWrite >= ulRead) ? (ulWrite - ulRead) :
(ulSize - (ulRead - ulWrite)));
}
#endif
//*****************************************************************************
//
// Take as many bytes from the transmit buffer as we have space for and move
// them into the UART transmit FIFO.
//
//*****************************************************************************
#ifdef UART_BUFFERED
static void
UARTPrimeTransmit(unsigned long ulBase)
{
//
// Do we have any data to transmit?
//
if(!TX_BUFFER_EMPTY)
{
//
// Disable the UART interrupt. If we don't do this there is a race
// condition which can cause the read index to be corrupted.
//
MAP_IntDisable(g_ulUARTInt[g_ulPortNum]);
//
// Yes - take some characters out of the transmit buffer and feed
// them to the UART transmit FIFO.
//
while(MAP_UARTSpaceAvail(ulBase) && !TX_BUFFER_EMPTY)
{
MAP_UARTCharPutNonBlocking(ulBase,
g_pcUARTTxBuffer[g_ulUARTTxReadIndex]);
ADVANCE_TX_BUFFER_INDEX(g_ulUARTTxReadIndex);
}
//
// Reenable the UART interrupt.
//
MAP_IntEnable(g_ulUARTInt[g_ulPortNum]);
}
}
#endif
//*****************************************************************************
//
//! Configures the UART console.
//!
//! \param ulPortNum is the number of UART port to use for the serial console
//! (0-2)
//! \param ulBaud is the bit rate that the UART is to be configured to use.
//! \param ulSrcClock is the frequency of the source clock for the UART module.
//!
//! This function will configure the specified serial port to be used as a
//! serial console. The serial parameters are set to the baud rate
//! specified by the \e ulBaud parameter and use 8 bit, no parity, and 1 stop
//! bit.
//!
//! This function must be called prior to using any of the other UART console
//! functions: UARTprintf() or UARTgets(). This function assumes that the
//! caller has previously configured the relevant UART pins for operation as a
//! UART rather than as GPIOs.
//!
//! \return None.
//
//*****************************************************************************
void
UARTStdioConfig(unsigned long ulPortNum, unsigned long ulBaud,
unsigned long ulSrcClock)
{
//
// Check the arguments.
//
ASSERT((ulPortNum == 0) || (ulPortNum == 1) ||
(ulPortNum == 2));
#ifdef UART_BUFFERED
//
// In buffered mode, we only allow a single instance to be opened.
//
ASSERT(g_ulBase == 0);
#endif
//
// Check to make sure the UART peripheral is present.
//
if(!MAP_SysCtlPeripheralPresent(g_ulUARTPeriph[ulPortNum]))
{
return;
}
//
// Select the base address of the UART.
//
g_ulBase = g_ulUARTBase[ulPortNum];
//
// Enable the UART peripheral for use.
//
MAP_SysCtlPeripheralEnable(g_ulUARTPeriph[ulPortNum]);
//
// Configure the UART for 115200, n, 8, 1
//
MAP_UARTConfigSetExpClk(g_ulBase, ulSrcClock, ulBaud,
(UART_CONFIG_PAR_NONE | UART_CONFIG_STOP_ONE |
UART_CONFIG_WLEN_8));
#ifdef UART_BUFFERED
//
// Set the UART to interrupt whenever the TX FIFO is almost empty or
// when any character is received.
//
MAP_UARTFIFOLevelSet(g_ulBase, UART_FIFO_TX1_8, UART_FIFO_RX1_8);
//
// Flush both the buffers.
//
UARTFlushRx();
UARTFlushTx(true);
//
// Remember which interrupt we are dealing with.
//
g_ulPortNum = ulPortNum;
//
// We are configured for buffered output so enable the master interrupt
// for this UART and the receive interrupts. We don't actually enable the
// transmit interrupt in the UART itself until some data has been placed
// in the transmit buffer.
//
MAP_UARTIntDisable(g_ulBase, 0xFFFFFFFF);
MAP_UARTIntEnable(g_ulBase, UART_INT_RX | UART_INT_RT);
MAP_IntEnable(g_ulUARTInt[ulPortNum]);
#endif
//
// Enable the UART operation.
//
MAP_UARTEnable(g_ulBase);
}
//*****************************************************************************
//
//! Initializes the UART console.
//!
//! \param ulPortNum is the number of UART port to use for the serial console
//! (0-2)
//!
//! This function will initialize the specified serial port to be used as a
//! serial console. The serial parameters will be set to 115200, 8-N-1.
//! An application wishing to use a different baud rate may call
//! UARTStdioInitExpClk() instead of this function.
//!
//! This function or UARTStdioInitExpClk() must be called prior to using any
//! of the other UART console functions: UARTprintf() or UARTgets(). In order
//! for this function to work correctly, SysCtlClockSet() must be called prior
//! to calling this function.
//!
//! It is assumed that the caller has previously configured the relevant UART
//! pins for operation as a UART rather than as GPIOs.
//!
//! \return None.
//
//*****************************************************************************
void
UARTStdioInit(unsigned long ulPortNum)
{
//
// Pass this call on to the version of the function allowing the baud rate
// to be specified.
//
UARTStdioConfig(ulPortNum, 115200, MAP_SysCtlClockGet());
}
//*****************************************************************************
//
//! Initializes the UART console and allows the baud rate to be selected.
//!
//! \param ulPortNum is the number of UART port to use for the serial console
//! (0-2)
//! \param ulBaud is the bit rate that the UART is to be configured to use.
//!
//! This function will initialize the specified serial port to be used as a
//! serial console. The serial parameters will be set to 8-N-1 and the bit
//! rate set according to the value of the \e ulBaud parameter.
//!
//! This function or UARTStdioInit() must be called prior to using any of the
//! other UART console functions: UARTprintf() or UARTgets(). In order for
//! this function to work correctly, SysCtlClockSet() must be called prior to
//! calling this function. An application wishing to use 115,200 baud may call
//! UARTStdioInit() instead of this function but should not call both
//! functions.
//!
//! It is assumed that the caller has previously configured the relevant UART
//! pins for operation as a UART rather than as GPIOs.
//!
//! \return None.
//
//*****************************************************************************
void
UARTStdioInitExpClk(unsigned long ulPortNum, unsigned long ulBaud)
{
UARTStdioConfig(ulPortNum, ulBaud, MAP_SysCtlClockGet());
}
//*****************************************************************************
//
//! Writes a string of characters to the UART output.
//!
//! \param pcBuf points to a buffer containing the string to transmit.
//! \param ulLen is the length of the string to transmit.
//!
//! This function will transmit the string to the UART output. The number of
//! characters transmitted is determined by the \e ulLen parameter. This
//! function does no interpretation or translation of any characters. Since
//! the output is sent to a UART, any LF (/n) characters encountered will be
//! replaced with a CRLF pair.
//!
//! Besides using the \e ulLen parameter to stop transmitting the string, if a
//! null character (0) is encountered, then no more characters will be
//! transmitted and the function will return.
//!
//! In non-buffered mode, this function is blocking and will not return until
//! all the characters have been written to the output FIFO. In buffered mode,
//! the characters are written to the UART transmit buffer and the call returns
//! immediately. If insufficient space remains in the transmit buffer,
//! additional characters are discarded.
//!
//! \return Returns the count of characters written.
//
//*****************************************************************************
int
UARTwrite(const char *pcBuf, unsigned long ulLen)
{
#ifdef UART_BUFFERED
unsigned int uIdx;
//
// Check for valid arguments.
//
ASSERT(pcBuf != 0);
ASSERT(g_ulBase != 0);
//
// Send the characters
//
for(uIdx = 0; uIdx < ulLen; uIdx++)
{
//
// If the character to the UART is \n, then add a \r before it so that
// \n is translated to \n\r in the output.
//
if(pcBuf[uIdx] == '\n')
{
if(!TX_BUFFER_FULL)
{
g_pcUARTTxBuffer[g_ulUARTTxWriteIndex] = '\r';
ADVANCE_TX_BUFFER_INDEX(g_ulUARTTxWriteIndex);
}
else
{
//
// Buffer is full - discard remaining characters and return.
//
break;
}
}
//
// Send the character to the UART output.
//
if(!TX_BUFFER_FULL)
{
g_pcUARTTxBuffer[g_ulUARTTxWriteIndex] = pcBuf[uIdx];
ADVANCE_TX_BUFFER_INDEX(g_ulUARTTxWriteIndex);
}
else
{
//
// Buffer is full - discard remaining characters and return.
//
break;
}
}
//
// If we have anything in the buffer, make sure that the UART is set
// up to transmit it.
//
if(!TX_BUFFER_EMPTY)
{
UARTPrimeTransmit(g_ulBase);
MAP_UARTIntEnable(g_ulBase, UART_INT_TX);
}
//
// Return the number of characters written.
//
return(uIdx);
#else
unsigned int uIdx;
//
// Check for valid UART base address, and valid arguments.
//
ASSERT(g_ulBase != 0);
ASSERT(pcBuf != 0);
//
// Send the characters
//
for(uIdx = 0; uIdx < ulLen; uIdx++)
{
//
// If the character to the UART is \n, then add a \r before it so that
// \n is translated to \n\r in the output.
//
if(pcBuf[uIdx] == '\n')
{
MAP_UARTCharPut(g_ulBase, '\r');
}
//
// Send the character to the UART output.
//
MAP_UARTCharPut(g_ulBase, pcBuf[uIdx]);
}
//
// Return the number of characters written.
//
return(uIdx);
#endif
}
//*****************************************************************************
//
//! A simple UART based get string function, with some line processing.
//!
//! \param pcBuf points to a buffer for the incoming string from the UART.
//! \param ulLen is the length of the buffer for storage of the string,
//! including the trailing 0.
//!
//! This function will receive a string from the UART input and store the
//! characters in the buffer pointed to by \e pcBuf. The characters will
//! continue to be stored until a termination character is received. The
//! termination characters are CR, LF, or ESC. A CRLF pair is treated as a
//! single termination character. The termination characters are not stored in
//! the string. The string will be terminated with a 0 and the function will
//! return.
//!
//! In both buffered and unbuffered modes, this function will block until
//! a termination character is received. If non-blocking operation is required
//! in buffered mode, a call to UARTPeek() may be made to determine whether
//! a termination character already exists in the receive buffer prior to
//! calling UARTgets().
//!
//! Since the string will be null terminated, the user must ensure that the
//! buffer is sized to allow for the additional null character.
//!
//! \return Returns the count of characters that were stored, not including
//! the trailing 0.
//
//*****************************************************************************
int
UARTgets(char *pcBuf, unsigned long ulLen)
{
#ifdef UART_BUFFERED
unsigned long ulCount = 0;
char cChar;
//
// Check the arguments.
//
ASSERT(pcBuf != 0);
ASSERT(ulLen != 0);
ASSERT(g_ulBase != 0);
//
// Adjust the length back by 1 to leave space for the trailing
// null terminator.
//
ulLen--;
//
// Process characters until a newline is received.
//
while(1)
{
//
// Read the next character from the receive buffer.
//
if(!RX_BUFFER_EMPTY)
{
cChar = g_pcUARTRxBuffer[g_ulUARTRxReadIndex];
ADVANCE_RX_BUFFER_INDEX(g_ulUARTRxReadIndex);
//
// See if a newline or escape character was received.
//
if((cChar == '\r') || (cChar == '\n') || (cChar == 0x1b))
{
//
// Stop processing the input and end the line.
//
break;
}
//
// Process the received character as long as we are not at the end
// of the buffer. If the end of the buffer has been reached then
// all additional characters are ignored until a newline is
// received.
//
if(ulCount < ulLen)
{
//
// Store the character in the caller supplied buffer.
//
pcBuf[ulCount] = cChar;
//
// Increment the count of characters received.
//
ulCount++;
}
}
}
//
// Add a null termination to the string.
//
pcBuf[ulCount] = 0;
//
// Return the count of chars in the buffer, not counting the trailing 0.
//
return(ulCount);
#else
unsigned long ulCount = 0;
char cChar;
static char bLastWasCR = 0;
//
// Check the arguments.
//
ASSERT(pcBuf != 0);
ASSERT(ulLen != 0);
ASSERT(g_ulBase != 0);
//
// Adjust the length back by 1 to leave space for the trailing
// null terminator.
//
ulLen--;
//
// Process characters until a newline is received.
//
while(1)
{
//
// Read the next character from the console.
//
cChar = MAP_UARTCharGet(g_ulBase);
//
// See if the backspace key was pressed.
//
if(cChar == '\b')
{
//
// If there are any characters already in the buffer, then delete
// the last.
//
if(ulCount)
{
//
// Rub out the previous character.
//
UARTwrite("\b \b", 3);
//
// Decrement the number of characters in the buffer.
//
ulCount--;
}
//
// Skip ahead to read the next character.
//
continue;
}
//
// If this character is LF and last was CR, then just gobble up the
// character because the EOL processing was taken care of with the CR.
//
if((cChar == '\n') && bLastWasCR)
{
bLastWasCR = 0;
continue;
}
//
// See if a newline or escape character was received.
//
if((cChar == '\r') || (cChar == '\n') || (cChar == 0x1b))
{
//
// If the character is a CR, then it may be followed by a LF which
// should be paired with the CR. So remember that a CR was
// received.
//
if(cChar == '\r')
{
bLastWasCR = 1;
}
//
// Stop processing the input and end the line.
//
break;
}
//
// Process the received character as long as we are not at the end of
// the buffer. If the end of the buffer has been reached then all
// additional characters are ignored until a newline is received.
//
if(ulCount < ulLen)
{
//
// Store the character in the caller supplied buffer.
//
pcBuf[ulCount] = cChar;
//
// Increment the count of characters received.
//
ulCount++;
//
// Reflect the character back to the user.
//
MAP_UARTCharPut(g_ulBase, cChar);
}
}
//
// Add a null termination to the string.
//
pcBuf[ulCount] = 0;
//
// Send a CRLF pair to the terminal to end the line.
//
UARTwrite("\r\n", 2);
//
// Return the count of chars in the buffer, not counting the trailing 0.
//
return(ulCount);
#endif
}
//*****************************************************************************
//
//! Read a single character from the UART, blocking if necessary.
//!
//! This function will receive a single character from the UART and store it at
//! the supplied address.
//!
//! In both buffered and unbuffered modes, this function will block until a
//! character is received. If non-blocking operation is required in buffered
//! mode, a call to UARTRxAvail() may be made to determine whether any
//! characters are currently available for reading.
//!
//! \return Returns the character read.
//
//*****************************************************************************
unsigned char
UARTgetc(void)
{
#ifdef UART_BUFFERED
unsigned char cChar;
//
// Wait for a character to be received.
//
while(RX_BUFFER_EMPTY)
{
//
// Block waiting for a character to be received (if the buffer is
// currently empty).
//
}
//
// Read a character from the buffer.
//
cChar = g_pcUARTRxBuffer[g_ulUARTRxReadIndex];
ADVANCE_RX_BUFFER_INDEX(g_ulUARTRxReadIndex);
//
// Return the character to the caller.
//
return(cChar);
#else
//
// Block until a character is received by the UART then return it to
// the caller.
//
return(MAP_UARTCharGet(g_ulBase));
#endif
}
//*****************************************************************************
//
//! A simple UART based printf function supporting \%c, \%d, \%p, \%s, \%u,
//! \%x, and \%X.
//!
//! \param pcString is the format string.
//! \param ... are the optional arguments, which depend on the contents of the
//! format string.
//!
//! This function is very similar to the C library <tt>fprintf()</tt> function.
//! All of its output will be sent to the UART. Only the following formatting
//! characters are supported:
//!
//! - \%c to print a character
//! - \%d or \%i to print a decimal value
//! - \%s to print a string
//! - \%u to print an unsigned decimal value
//! - \%x to print a hexadecimal value using lower case letters
//! - \%X to print a hexadecimal value using lower case letters (not upper case
//! letters as would typically be used)
//! - \%p to print a pointer as a hexadecimal value
//! - \%\% to print out a \% character
//!
//! For \%s, \%d, \%i, \%u, \%p, \%x, and \%X, an optional number may reside
//! between the \% and the format character, which specifies the minimum number
//! of characters to use for that value; if preceded by a 0 then the extra
//! characters will be filled with zeros instead of spaces. For example,
//! ``\%8d'' will use eight characters to print the decimal value with spaces
//! added to reach eight; ``\%08d'' will use eight characters as well but will
//! add zeroes instead of spaces.
//!
//! The type of the arguments after \e pcString must match the requirements of
//! the format string. For example, if an integer was passed where a string
//! was expected, an error of some kind will most likely occur.
//!
//! \return None.
//
//*****************************************************************************
void
UARTprintf(const char *pcString, ...)
{
unsigned long ulIdx, ulValue, ulPos, ulCount, ulBase, ulNeg;
char *pcStr, pcBuf[16], cFill;
va_list vaArgP;
//
// Check the arguments.
//
ASSERT(pcString != 0);
//
// Start the varargs processing.
//
va_start(vaArgP, pcString);
//
// Loop while there are more characters in the string.
//
while(*pcString)
{
//
// Find the first non-% character, or the end of the string.
//
for(ulIdx = 0; (pcString[ulIdx] != '%') && (pcString[ulIdx] != '\0');
ulIdx++)
{
}
//
// Write this portion of the string.
//
UARTwrite(pcString, ulIdx);
//
// Skip the portion of the string that was written.
//
pcString += ulIdx;
//
// See if the next character is a %.
//
if(*pcString == '%')
{
//
// Skip the %.
//
pcString++;
//
// Set the digit count to zero, and the fill character to space
// (i.e. to the defaults).
//
ulCount = 0;
cFill = ' ';
//
// It may be necessary to get back here to process more characters.
// Goto's aren't pretty, but effective. I feel extremely dirty for
// using not one but two of the beasts.
//
again:
//
// Determine how to handle the next character.
//
switch(*pcString++)
{
//
// Handle the digit characters.
//
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':