PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
serial_rxtx.c
1 /*
2  * @file serial.c
3  * Defines the serial receive and transmit routines.
4  *
5  * Copyright (c) 2013, Parallax Inc.
6  * Written by Steve Denson
7  */
8 #include <propeller.h>
9 #include "serial.h"
10 
11 __attribute__((fcache)) static int _inbyte(int bitcycles, int cycle1, int rxmask, int value)
12 {
13  int waitcycles;
14  int j = 8;
15 
16  cycle1 += cycle1 >> 2;
17 
18  /* wait for a start bit */
19  waitpeq(0, rxmask);
20  waitcycles = cycle1 + CNT;
21 
22  /* index initialized above */
23  while(j-- > 0) {
24  /* C code is too big for fcache in xmm memory models.
25  // waitcycles = waitcnt2(waitcycles, bitcycles); */
26  __asm__ volatile("waitcnt %[_waitcycles], %[_bitcycles]"
27  : [_waitcycles] "+r" (waitcycles)
28  : [_bitcycles] "r" (bitcycles));
29 
30  /* value = ( (0 != (INA & rxmask)) << 7) | (value >> 1); */
31  __asm__ volatile("shr %[_value],# 1\n\t"
32  "test %[_mask],ina wz \n\t"
33  "muxnz %[_value], #1<<7"
34  : [_value] "+r" (value)
35  : [_mask] "r" (rxmask));
36  }
37  return value; /* fcached 0x40 or 64 bytes */
38 }
39 
40 int serial_rxChar(serial *device)
41 {
42  Serial_t *sp = (Serial_t*) device->devst;
43  int value = 0;
44 
45  /* set input */
46  unsigned int rxmask = 1 << sp->rx_pin;
47 
48  if(sp->tx_pin < SERIAL_MIN_PIN && sp->tx_pin > SERIAL_MAX_PIN)
49  return 0; /* don't receive on pins out of range */
50 
51  DIRA &= ~rxmask;
52 
53  value = _inbyte(sp->ticks, sp->ticks, rxmask, 0);
54  /* wait for the line to go high (as it will when the stop bit arrives) */
55  waitpeq(rxmask, rxmask);
56  return value & 0xff;
57 }
58 
59 __attribute__((fcache)) static void _outbyte(int bitcycles, int txmask, int value)
60 {
61  int j = 10;
62  int waitcycles;
63 
64  waitcycles = CNT + bitcycles;
65  while(j-- > 0) {
66  /* C code is too big and not fast enough for all memory models.
67  // waitcycles = waitcnt2(waitcycles, bitcycles); */
68  __asm__ volatile("waitcnt %[_waitcycles], %[_bitcycles]"
69  : [_waitcycles] "+r" (waitcycles)
70  : [_bitcycles] "r" (bitcycles));
71 
72  /* if (value & 1) OUTA |= txmask else OUTA &= ~txmask; value = value >> 1; */
73  __asm__ volatile("shr %[_value],#1 wc \n\t"
74  "muxc outa, %[_mask]"
75  : [_value] "+r" (value)
76  : [_mask] "r" (txmask));
77  }
78 }
79 
80 
81 int serial_txChar(serial *device, int value)
82 {
83  Serial_t *sp = (Serial_t*) device->devst;
84  int txmask = (1 << sp->tx_pin);
85 
86  if(sp->tx_pin < SERIAL_MIN_PIN && sp->tx_pin > SERIAL_MAX_PIN)
87  return 0; /* don't transmit on pins out of range */
88 
89  DIRA |= txmask;
90 
91  _outbyte(sp->ticks, txmask, (value | 0x100) << 1);
92 
93  return value;
94 }
95 
96 /*
97 +--------------------------------------------------------------------
98 | TERMS OF USE: MIT License
99 +--------------------------------------------------------------------
100 Permission is hereby granted, free of charge, to any person obtaining
101 a copy of this software and associated documentation files
102 (the "Software"), to deal in the Software without restriction,
103 including without limitation the rights to use, copy, modify, merge,
104 publish, distribute, sublicense, and/or sell copies of the Software,
105 and to permit persons to whom the Software is furnished to do so,
106 subject to the following conditions:
107 
108 The above copyright notice and this permission notice shall be
109 included in all copies or substantial portions of the Software.
110 
111 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
112 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
113 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
114 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
115 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
116 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
117 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
118 +--------------------------------------------------------------------
119 */
serial_info::tx_pin
int tx_pin
Definition: serial.h:58
waitpeq
#define waitpeq(state, mask)
Wait until INA equal state & mask.
Definition: propeller.h:190
serial_txChar
int serial_txChar(serial *device, int txbyte)
Send a byte.
Definition: serial_rxtx.c:81
text_struct::devst
volatile void * devst
Definition: simpletext.h:90
serial.h
This library supports creating and managing one or more half duplex serial connections with periphera...
serial_info::ticks
int ticks
Definition: serial.h:61
serial_info
Defines serial interface structure of 9 contiguous int variables.
Definition: serial.h:55
serial_info::rx_pin
int rx_pin
Definition: serial.h:57
SERIAL_MAX_PIN
#define SERIAL_MAX_PIN
Max serial pin value.
Definition: serial.h:50
text_struct
Structure that contains data used by simple text device libraries.
Definition: simpletext.h:81
serial_rxChar
int serial_rxChar(serial *device)
Receive a byte. Waits until next byte is received.
Definition: serial_rxtx.c:40
CNT
#define CNT
The system clock count.
Definition: propeller1.h:151
SERIAL_MIN_PIN
#define SERIAL_MIN_PIN
Min serial pin value.
Definition: serial.h:45
DIRA
#define DIRA
Use to set pins to input (0) or output (1).
Definition: propeller1.h:161