PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
fullduplexserial.h
Go to the documentation of this file.
1 
28 #pragma once
29 
30 #include <PropWare/PropWare.h>
34 
35 namespace PropWare {
36 
37 void *get_full_duplex_serial_driver ();
38 
43  public ScanCapable {
44  public:
45  typedef enum {
46  INVERT_RX = BIT_0,
47  INVERT_TX = BIT_1,
48  OPEN_DRAIN_SOURCE_TX = BIT_2,
49  IGNORE_TX_ECHO_ON_RX = BIT_3
50  } Mode;
51 
52  static const size_t BUFFER_SIZE = 16;
53 
54  public:
67  FullDuplexSerial (const int rxPinNumber = _cfg_rxpin, const int txPinNumber = _cfg_txpin,
68  const uint32_t mode = 0, const int baudrate = _cfg_baudrate)
69  : m_transmitLock(locknew()),
70  m_stringLock(locknew()),
71  m_cogID(-1),
72  m_receivePinNumber(rxPinNumber),
73  m_transmitPinNumber(txPinNumber),
74  m_mode(mode),
75  m_bitTicks(CLKFREQ / baudrate),
76  m_bufferPointer((uint32_t) this->m_receiveBuffer) {
77  }
78 
83  if (-1 != this->m_cogID)
84  cogstop(this->m_cogID);
85  lockret(this->m_transmitLock);
86  lockret(this->m_stringLock);
87  }
88 
94  int start () {
95  return this->m_cogID = cognew(get_full_duplex_serial_driver(), (int32_t) (&this->m_receiveHead));
96  }
97 
101  void truncate () {
102  char c;
103  while (this->get_char_non_blocking(c));
104  }
105 
111  bool receive_ready () const {
112  return this->m_receiveHead != this->m_receiveTail;
113  }
114 
122  bool get_char_non_blocking (char &c) {
123  if (this->receive_ready()) {
124  c = this->m_receiveBuffer[this->m_receiveTail];
125  this->m_receiveTail = (this->m_receiveTail + 1) & 0xf;
126  return true;
127  } else
128  return false;
129  }
130 
139  bool get_char (char &c, const unsigned int timeout) {
140  const unsigned int startTime = CNT;
141  bool success;
142  while (!(success = this->get_char_non_blocking(c))
143  && ((CNT - startTime) < timeout));
144  return success;
145  }
146 
147  char get_char () {
148  char c;
149  while (!this->get_char_non_blocking(c));
150  return c;
151  }
152 
153  void put_char (const char c) {
154  // Send byte (may wait for room in buffer)
155  while (lockset(this->m_transmitLock));
156  while (this->m_transmitTail == ((this->m_transmitHead + 1) & 0xf));
157  this->m_transmitBuffer[this->m_transmitHead] = c;
158  this->m_transmitHead = (this->m_transmitHead + 1) & 0xf;
159  lockclr(this->m_transmitLock);
160  if (this->m_mode & IGNORE_TX_ECHO_ON_RX)
161  this->get_char();
162  }
163 
164  void puts (const char string[]) {
165  const unsigned int length = strlen(string);
166  while (lockset(this->m_stringLock));
167  for (unsigned int i = 0; i < length; i++)
168  this->put_char((string++)[0]);
169  lockclr(this->m_stringLock);
170  }
171 
172  protected:
173  const uint8_t m_transmitLock;
174  const uint8_t m_stringLock;
175  int32_t m_cogID;
176  char m_receiveBuffer[BUFFER_SIZE];
177  char m_transmitBuffer[BUFFER_SIZE];
178 
179  // These variables must appear in this order. The assembly code relies on the exact order
180  volatile uint32_t m_receiveHead;
181  volatile uint32_t m_receiveTail;
182  volatile uint32_t m_transmitHead;
183  volatile uint32_t m_transmitTail;
184  const int m_receivePinNumber;
185  const int m_transmitPinNumber;
186  const uint32_t m_mode;
187  const uint32_t m_bitTicks;
188  const uint32_t m_bufferPointer;
189 };
190 
191 }
PropWare::FullDuplexSerial::~FullDuplexSerial
~FullDuplexSerial()
Stop the driver cog and return the locks.
Definition: fullduplexserial.h:82
cognew
#define cognew(code, param)
Start a new Propeller PASM COG.
Definition: propeller.h:94
uartcommondata.h
PropWare::ScanCapable
Interface for all classes capable of printing.
Definition: scancapable.h:38
timeout
int timeout(int time)
Compares the time against the time elapsed since mark (deprecated).
Definition: timeout.c:19
PropWare::FullDuplexSerial::get_char
char get_char()
Read and return a single character. Whether the method is blocking or not depends entirely on the imp...
Definition: fullduplexserial.h:147
PropWare::FullDuplexSerial::truncate
void truncate()
Empty the receive buffer.
Definition: fullduplexserial.h:101
PropWare::FullDuplexSerial::puts
void puts(const char string[])
Send a null-terminated character array. Though this method could be created using put_char,...
Definition: fullduplexserial.h:164
PropWare::PrintCapable
Interface for all classes capable of printing.
Definition: printcapable.h:38
PropWare::FullDuplexSerial::get_char
bool get_char(char &c, const unsigned int timeout)
Wait for a byte to be received and return after a timeout.
Definition: fullduplexserial.h:139
lockset
#define lockset(lockid)
Set a lock.
Definition: propeller.h:163
PropWare.h
CLKFREQ
#define CLKFREQ
Returns the current clock frequency.
Definition: propeller.h:46
lockret
#define lockret(lockid)
Return lock to pool.
Definition: propeller.h:155
PropWare::FullDuplexSerial::start
int start()
Start the driver cog.
Definition: fullduplexserial.h:94
lockclr
#define lockclr(lockid)
Clear lock.
Definition: propeller.h:170
PropWare::FullDuplexSerial::FullDuplexSerial
FullDuplexSerial(const int rxPinNumber=_cfg_rxpin, const int txPinNumber=_cfg_txpin, const uint32_t mode=0, const int baudrate=_cfg_baudrate)
Definition: fullduplexserial.h:67
printcapable.h
cogstop
#define cogstop(a)
Stop a COG.
Definition: propeller.h:100
CNT
#define CNT
The system clock count.
Definition: propeller1.h:151
locknew
#define locknew()
Get a new lock from the pool of Propeller hardware locks.
Definition: propeller.h:147
PropWare::FullDuplexSerial::receive_ready
bool receive_ready() const
Find out if a byte is waiting in the receive buffer.
Definition: fullduplexserial.h:111
PropWare::FullDuplexSerial
Converted to C++ using spin2cpp and then modified to become a PrintCapable object in PropWare's arsen...
Definition: fullduplexserial.h:42
PropWare
Generic definitions and functions for the Parallax Propeller.
Definition: runnable.h:33
PropWare::FullDuplexSerial::get_char_non_blocking
bool get_char_non_blocking(char &c)
Check if byte received (never waits)
Definition: fullduplexserial.h:122
PropWare::FullDuplexSerial::put_char
void put_char(const char c)
Print a single character.
Definition: fullduplexserial.h:153
scancapable.h