PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
fdserial_utils.c
1 
8 #include <propeller.h>
9 #include "fdserial.h"
10 
11 /*
12  * rxflush empties the receive queue
13  */
15 {
16  while(fdserial_rxCheck(term) >= 0)
17  ; // clear out queue by receiving all available
18 }
19 
20 /*
21  * Check if a byte is available in the buffer.
22  * Function does not block.
23  * @returns non-zero if a byte is available.
24  */
26 {
27  volatile fdserial_st* fdp = (fdserial_st*) term->devst;
28  return (fdp->rx_tail != fdp->rx_head);
29 }
30 
31 /*
32  * Get a byte from the receive queue if available within timeout period.
33  * Function blocks if no recieve for ms timeout.
34  * @param ms is number of milliseconds to wait for a char
35  * @returns receive byte 0 to 0xff or -1 if none available
36  */
37 int fdserial_rxTime(fdserial *term, int ms)
38 {
39  int rc = -1;
40  int t1 = 0;
41  int t0 = CNT;
42  do {
43  rc = fdserial_rxCheck(term);
44  t1 = CNT;
45  if((t1 - t0)/(CLKFREQ/1000) > ms)
46  break;
47  } while(rc < 0);
48  return rc;
49 }
50 
52 {
53  while(!fdserial_txEmpty(term));
54 }
55 
56 /*
57  * Get a byte from the receive buffer without changing the pointers.
58  * The function does not block.
59  * returns non-zero if a valid byte is available.
60  */
62 {
63  int rc = 0;
64  volatile fdserial_st* fdp = (fdserial_st*) term->devst;
65  volatile char* rxbuf = (volatile char*) fdp->buffptr; // rx buff starts at offset 0
66 
67  if(fdp->rx_tail != fdp->rx_head) {
68  rc = rxbuf[fdp->rx_tail];
69  }
70  return rc;
71 }
72 
73 /*
74  * Get number of bytes available in the receive buffer.
75  * Queue overflows can not be detected.
76  * The function does not block.
77  * returns less than 1 if no bytes are available.
78  */
80 {
81  int rc = 0;
82  volatile fdserial_st* fdp = (fdserial_st*) term->devst;
83  volatile char* rxbuf = (volatile char*) fdp->buffptr; // rx buff starts at offset 0
84 
85  if(fdp->rx_tail == fdp->rx_head) {
86  rc = 0;
87  }
88  else {
89  if(fdp->rx_head > fdp->rx_tail) {
90  rc = fdp->rx_head - fdp->rx_tail;
91  }
92  else {
93  // [.....H.........T....]
94  rc = FDSERIAL_BUFF_MASK+1;
95  rc -= fdp->rx_tail; // buffer size - tail mark
96  rc += fdp->rx_head; // plus head mark
97  }
98  }
99  return rc;
100 }
101 
102 /*
103 +--------------------------------------------------------------------
104 | TERMS OF USE: MIT License
105 +--------------------------------------------------------------------
106 Permission is hereby granted, free of charge, to any person obtaining
107 a copy of this software and associated documentation files
108 (the "Software"), to deal in the Software without restriction,
109 including without limitation the rights to use, copy, modify, merge,
110 publish, distribute, sublicense, and/or sell copies of the Software,
111 and to permit persons to whom the Software is furnished to do so,
112 subject to the following conditions:
113 
114 The above copyright notice and this permission notice shall be
115 included in all copies or substantial portions of the Software.
116 
117 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
118 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
119 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
120 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
121 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
122 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
123 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
124 +--------------------------------------------------------------------
125 */
126 
FDSERIAL_BUFF_MASK
#define FDSERIAL_BUFF_MASK
Definition: fdserial.h:56
fdserial_rxReady
int fdserial_rxReady(fdserial *term)
Check if a byte is ready in the receive buffer.
Definition: fdserial_utils.c:25
fdserial_rxPeek
int fdserial_rxPeek(fdserial *term)
Get a byte from the receive buffer without changing the pointers. The function does not block.
Definition: fdserial_utils.c:61
fdserial_struct::rx_head
int rx_head
Definition: fdserial.h:99
fdserial_rxTime
int fdserial_rxTime(fdserial *term, int ms)
Gets a byte from the receive buffer if available, or wait for up to timeout ms to receive a byte.
Definition: fdserial_utils.c:37
text_struct::devst
volatile void * devst
Definition: simpletext.h:90
fdserial_rxCheck
int fdserial_rxCheck(fdserial *term)
Gets a byte from the receive buffer if available, but does not wait if there's nothing in the buffer.
Definition: fdserial.c:146
fdserial_struct::rx_tail
int rx_tail
Definition: fdserial.h:100
CLKFREQ
#define CLKFREQ
Returns the current clock frequency.
Definition: propeller.h:46
fdserial_rxCount
int fdserial_rxCount(fdserial *term)
Get number of bytes available in the receive buffer without receiving any. NOTE: This function is not...
Definition: fdserial_utils.c:79
fdserial_struct
Defines fdserial interface structure of 9 contiguous longs + buffers.
Definition: fdserial.h:97
fdserial_rxFlush
void fdserial_rxFlush(fdserial *term)
Empties the receive buffer.
Definition: fdserial_utils.c:14
fdserial_txFlush
void fdserial_txFlush(fdserial *term)
Remove any bytes that might be waiting in the transmit buffer.
Definition: fdserial_utils.c:51
text_struct
Structure that contains data used by simple text device libraries.
Definition: simpletext.h:81
CNT
#define CNT
The system clock count.
Definition: propeller1.h:151
ms
int ms
Propeller system clock ticks in 1 millisecond (ms).
Definition: timeTicks.c:20
fdserial_txEmpty
int fdserial_txEmpty(fdserial *term)
Check if the transmit buffer is empty.
Definition: fdserial.c:135
fdserial_struct::buffptr
char * buffptr
Definition: fdserial.h:107
fdserial.h
This library supports creating and managing one or more full duplex serial connections with periphera...