PropWare
C++ objects and CMake build system for Parallax Propeller
Home
Download
Getting Started
Documentation
Reference
About
Related Links
Namespace List
Class List
File List
Examples
Build System
File List
File Members
simple
fdserial.c
1
9
#include <stdlib.h>
10
#include "fdserial.h"
11
12
/*
13
* start initializes and starts native assembly driver in a cog.
14
* @param rxpin is pin number for receive input
15
* @param txpin is pin number for transmit output
16
* @param mode is interface mode. see header FDSERIAL_MODE_...
17
* @param baudrate is frequency of bits ... 115200, 57600, etc...
18
* @returns non-zero on success
19
*/
20
fdserial
*fdserial_open(
int
rxpin,
int
txpin,
int
mode,
int
baudrate)
21
{
22
extern
int
binary_pst_dat_start[];
23
24
fdserial_st
*fdptr;
25
26
/* can't use array instead of malloc because it would go out of scope. */
27
char
* bufptr = (
char
*) malloc(2*(FDSERIAL_BUFF_MASK+1));
28
fdserial
* term = (
fdserial
*) malloc(
sizeof
(
fdserial
));
29
memset(term, 0,
sizeof
(
fdserial
));
30
31
fdptr = (
void
*) malloc(
sizeof
(
fdserial_st
));
32
term->
devst
= fdptr;
33
memset((
char
*)fdptr, 0,
sizeof
(
fdserial_st
));
34
35
if
(rxpin == 31 && txpin == 30) {
36
simpleterm_close();
37
}
38
39
/* required for terminal to work */
40
term->
txChar
= fdserial_txChar;
41
term->
rxChar
= fdserial_rxChar;
42
43
fdptr->
rx_pin
= rxpin;
/* recieve pin */
44
fdptr->
tx_pin
= txpin;
/* transmit pin */
45
fdptr->
mode
= mode;
/* interface mode */
46
47
/* baud from clkfreq (cpu clock typically 80000000 for 5M*pll16x) */
48
fdptr->
ticks
= CLKFREQ/baudrate;
49
50
fdptr->
buffptr
= bufptr;
/* receive and transmit buffer */
51
52
/* now start the kernel */
53
#if defined(__PROPELLER_USE_XMM__)
54
{
unsigned
int
buffer[2048];
55
memcpy(buffer, binary_pst_dat_start, 2048);
56
term->
cogid
[0] = cognew(buffer, (
void
*)fdptr) + 1;
57
}
58
#else
59
term->
cogid
[0] = setStopCOGID(cognew((
void
*)binary_pst_dat_start, (
void
*)fdptr));
60
#endif
61
waitcnt(CLKFREQ/2+CNT);
// give cog chance to load
62
return
term;
63
}
64
65
/*
66
* stop stops the cog running the native assembly driver
67
*/
68
void
fdserial_close(
fdserial
*term)
69
{
70
int
id
= term->
cogid
[0];
71
fdserial_st
* fdp = (
fdserial_st
*) term->
devst
;
72
73
while
(fdserial_rxCheck(term) >= 0)
74
;
// clear out queue by receiving all available
75
fdserial_txFlush(term);
76
77
if
(
id
> 0) cogstop(getStopCOGID(
id
));
78
79
free((
void
*)fdp->
buffptr
);
80
free((
void
*)fdp);
81
free(term);
82
term = 0;
83
}
84
85
/*
86
* checks if anything is in the tx queue
87
*/
88
int
fdserial_txEmpty(
fdserial
*term)
89
{
90
volatile
fdserial_st
* fdp = (
fdserial_st
*) term->
devst
;
91
return
fdp->
tx_tail
== fdp->
tx_head
;
92
}
93
94
/*
95
* Gets a byte from the receive queue if available
96
* Function does not block. We move rxtail after getting char.
97
* @returns receive byte 0 to 0xff or -1 if none available
98
*/
99
int
fdserial_rxCheck(
fdserial
*term)
100
{
101
int
rc = -1;
102
volatile
fdserial_st
* fdp = (
fdserial_st
*) term->
devst
;
103
volatile
char
* rxbuf = (
volatile
char
*) fdp->
buffptr
;
// rx buff starts at offset 0
104
105
if
(fdp->
rx_tail
!= fdp->
rx_head
)
106
{
107
rc = rxbuf[fdp->
rx_tail
];
108
fdp->
rx_tail
= (fdp->
rx_tail
+1) & FDSERIAL_BUFF_MASK;
109
}
110
return
rc;
111
}
112
113
/*
114
* Wait for a byte from the receive queue. blocks until something is ready.
115
* @returns received byte
116
*/
117
int
fdserial_rxChar(
fdserial
*term)
118
{
119
int
rc = fdserial_rxCheck(term);
120
while
(rc < 0)
121
rc = fdserial_rxCheck(term);
122
return
rc;
123
}
124
125
/*
126
* tx sends a byte on the transmit queue.
127
* @param txbyte is byte to send.
128
*/
129
int
fdserial_txChar(
fdserial
*term,
int
txbyte)
130
{
131
int
rc = -1;
132
volatile
fdserial_st
* fdp = (
fdserial_st
*) term->
devst
;
133
volatile
char
* txbuf = (
volatile
char
*) fdp->
buffptr
+ FDSERIAL_BUFF_MASK+1;
134
135
while
(fdp->
tx_tail
== ((fdp->
tx_head
+1) & FDSERIAL_BUFF_MASK))
136
;
// wait for queue to be empty
137
txbuf[fdp->
tx_head
] = txbyte;
138
fdp->
tx_head
= (fdp->
tx_head
+1) & FDSERIAL_BUFF_MASK;
139
if
(fdp->
mode
& FDSERIAL_MODE_IGNORE_TX_ECHO)
140
rc = fdserial_rxChar(term);
// why not rxcheck or timeout ... this blocks for char
141
return
rc;
142
}
143
144
/*
145
+--------------------------------------------------------------------
146
| TERMS OF USE: MIT License
147
+--------------------------------------------------------------------
148
Permission is hereby granted, free of charge, to any person obtaining
149
a copy of this software and associated documentation files
150
(the "Software"), to deal in the Software without restriction,
151
including without limitation the rights to use, copy, modify, merge,
152
publish, distribute, sublicense, and/or sell copies of the Software,
153
and to permit persons to whom the Software is furnished to do so,
154
subject to the following conditions:
155
156
The above copyright notice and this permission notice shall be
157
included in all copies or substantial portions of the Software.
158
159
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
160
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
162
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
163
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
164
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
165
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
166
+--------------------------------------------------------------------
167
*/
fdserial_struct::mode
int mode
Definition:
fdserial.h:93
fdserial_struct::tx_tail
int tx_tail
Definition:
fdserial.h:90
text_struct::rxChar
int(* rxChar)(struct text_struct *p)
Definition:
simpletext.h:73
text_struct::txChar
int(* txChar)(struct text_struct *p, int ch)
Definition:
simpletext.h:75
text_struct::cogid
int cogid[TERM_COG_LEN]
Definition:
simpletext.h:77
text_struct::devst
volatile void * devst
Definition:
simpletext.h:79
fdserial_struct::ticks
int ticks
Definition:
fdserial.h:94
fdserial_struct::tx_head
int tx_head
Definition:
fdserial.h:89
fdserial_struct::rx_pin
int rx_pin
Definition:
fdserial.h:91
fdserial_struct
Defines fdserial interface structure of 9 contiguous longs + buffers.
Definition:
fdserial.h:85
fdserial_struct::rx_head
int rx_head
Definition:
fdserial.h:87
fdserial_struct::buffptr
char * buffptr
Definition:
fdserial.h:95
text_struct
Structure that contains data used by simple text device libraries.
Definition:
simpletext.h:70
fdserial_struct::rx_tail
int rx_tail
Definition:
fdserial.h:88
fdserial_struct::tx_pin
int tx_pin
Definition:
fdserial.h:92
Generated on Sat Aug 27 2016 00:58:09 for PropWare by
1.8.12