PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
serial_open.c
1 /*
2  * @file serial_open.c
3  * Opens a serial module.
4  *
5  * Copyright (c) 2013, Parallax Inc.
6  * Written by Steve Denson
7  */
8 #include <stdlib.h>
9 #include <propeller.h>
10 #include "serial.h"
11 
12 extern HUBDATA terminal *dport_ptr;
13 
14 serial *serial_open(int rxpin, int txpin, int mode, int baudrate)
15 {
16  Serial_t *serptr;
17 
18  /* can't use array instead of malloc because it would go out of scope. */
19  text_t* text = (text_t*) malloc(sizeof(text_t));
20 
21  /* set pins first for boards that can misbehave intentionally like the Quickstart */
22  if(txpin >= SERIAL_MIN_PIN && txpin <= SERIAL_MAX_PIN) {
23  OUTA |= (1<<txpin);
24  DIRA |= (1<<txpin);
25  }
26  if(rxpin >= SERIAL_MIN_PIN && rxpin <= SERIAL_MAX_PIN) {
27  DIRA &= ~(1<<rxpin);
28  }
29 
30  //memset(text, 0, sizeof(text_t));
31  serptr = (Serial_t*) malloc(sizeof(Serial_t));
32  text->devst = serptr;
33  memset((char*)serptr, 0, sizeof(Serial_t));
34 
35  text->txChar = serial_txChar; /* required for terminal to work */
36  text->rxChar = serial_rxChar; /* required for terminal to work */
37  //
38  //if((mode & ECHO_RX_TO_TX) || (rxpin == 31 && txpin == 30))
39  if((mode & ECHO_RX_TO_TX))
40  {
41  text->terminalEcho = 1;
42  }
43  else
44  {
45  text->terminalEcho = 0;
46  }
47  //
48  /*
49  memcpy(&text->ec, "\r\n", 3);
50  memcpy(&text->ecs, "\r\0", 3);
51  */
52  //
53  if(rxpin == 31 && txpin == 30)
54  {
55  //memcpy(&text->ec, "\r\n", 3);
56  //memcpy(&text->ecs, "\r\0", 3);
57  text->ecA = '\r';
58  text->ecB = '\n';
59  text->ecsA = '\r';
60  text->ecsB = 0;
61  }
62  else
63  {
64  //memcpy(&text->ec, "\0\0", 3);
65  //memcpy(&text->ecs, "\0\0", 3);
66  text->ecA = 0;
67  text->ecB = 0;
68  text->ecsA = 0;
69  text->ecsB = 0;
70  }
71  //
72  serptr->rx_pin = rxpin; /* recieve pin */
73  serptr->tx_pin = txpin; /* transmit pin*/
74  serptr->mode = mode;
75  serptr->baud = baudrate;
76  serptr->ticks = CLKFREQ/baudrate; /* baud from clkfreq (cpu clock typically 80000000 for 5M*pll16x) */
77 
78  return text;
79 }
80 
81 /*
82 +--------------------------------------------------------------------
83 | TERMS OF USE: MIT License
84 +--------------------------------------------------------------------
85 Permission is hereby granted, free of charge, to any person obtaining
86 a copy of this software and associated documentation files
87 (the "Software"), to deal in the Software without restriction,
88 including without limitation the rights to use, copy, modify, merge,
89 publish, distribute, sublicense, and/or sell copies of the Software,
90 and to permit persons to whom the Software is furnished to do so,
91 subject to the following conditions:
92 
93 The above copyright notice and this permission notice shall be
94 included in all copies or substantial portions of the Software.
95 
96 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
97 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
98 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
99 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
100 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
101 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
102 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
103 +--------------------------------------------------------------------
104 */
OUTA
#define OUTA
Use to set output pin states when corresponding DIRA bits are 1.
Definition: propeller1.h:157
text_struct::rxChar
int(* rxChar)(struct text_struct *p)
Definition: simpletext.h:84
text_struct::txChar
int(* txChar)(struct text_struct *p, int ch)
Definition: simpletext.h:86
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
Defines serial interface structure of 9 contiguous int variables.
Definition: serial.h:55
ECHO_RX_TO_TX
#define ECHO_RX_TO_TX
Definition: simpletext.h:64
SERIAL_MAX_PIN
#define SERIAL_MAX_PIN
Max serial pin value.
Definition: serial.h:50
CLKFREQ
#define CLKFREQ
Returns the current clock frequency.
Definition: propeller.h:46
HUBDATA
#define HUBDATA
HUBDATA tells compiler to put data into HUB RAM section. This is mostly useful in XMM modes where dat...
Definition: propeller.h:32
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
SERIAL_MIN_PIN
#define SERIAL_MIN_PIN
Min serial pin value.
Definition: serial.h:45
text_struct::terminalEcho
volatile int terminalEcho
Definition: simpletext.h:92
serial_open
serial * serial_open(int rxpin, int txpin, int mode, int baudrate)
Open a simple (half duplex) serial connection.
Definition: serial_open.c:14
DIRA
#define DIRA
Use to set pins to input (0) or output (1).
Definition: propeller1.h:161
text_struct::ecA
volatile char ecA
Definition: simpletext.h:97