PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
uart.h
Go to the documentation of this file.
1 
26 #pragma once
27 
28 #include <PropWare/PropWare.h>
30 
31 namespace PropWare {
32 
33 #ifdef __PROPELLER_COG__
34 #define virtual
35 #endif
36 
86 class UART {
87  public:
92  };
93 
95 #define UART_ERRORS_LIMIT 16
96 
97 #define UART_ERRORS_BASE 64
98 
112  } ErrorCode;
113 
114  public:
115  static const uint8_t DEFAULT_DATA_WIDTH = 8;
116  static const Parity DEFAULT_PARITY = Parity::NO_PARITY;
117  static const uint8_t DEFAULT_STOP_BIT_WIDTH = 1;
118 
119  static const int MAX_BAUD = 4413793;
120 
121  public:
122  virtual ErrorCode set_data_width (const uint8_t dataWidth) {
123  if (1 > dataWidth || dataWidth > 16)
125 
126  this->m_dataWidth = dataWidth;
127 
128  this->m_dataMask = 0;
129  for (uint8_t i = 0; i < this->m_dataWidth; ++i)
130  this->m_dataMask |= 1 << i;
131 
132  this->set_parity_mask();
133  this->set_total_bits();
134 
135  return UART::NO_ERROR;
136  }
137 
138  uint8_t get_data_width () const {
139  return this->m_dataWidth;
140  }
141 
142  virtual void set_parity (const UART::Parity parity) {
143  this->m_parity = parity;
144  this->set_parity_mask();
145  this->set_stop_bit_mask();
146  this->set_total_bits();
147  }
148 
149  UART::Parity get_parity () const {
150  return this->m_parity;
151  }
152 
153  ErrorCode set_stop_bit_width (const uint8_t stopBitWidth) {
154  // Error checking
155  if (0 == stopBitWidth || stopBitWidth > 14)
157 
158  this->m_stopBitWidth = stopBitWidth;
159 
160  this->set_stop_bit_mask();
161 
162  this->set_total_bits();
163 
164  return NO_ERROR;
165  }
166 
167  uint8_t get_stop_bit_width () const {
168  return this->m_stopBitWidth;
169  }
170 
171  void set_baud_rate (const int32_t baudRate) {
172  this->m_bitCycles = CLKFREQ / baudRate;
173  }
174 
175  int32_t get_baud_rate () const {
176  return CLKFREQ / this->m_bitCycles;
177  }
178 
179  protected:
184  UART () {
185  this->set_data_width(UART::DEFAULT_DATA_WIDTH);
186  this->set_parity(UART::DEFAULT_PARITY);
187  this->set_stop_bit_width(UART::DEFAULT_STOP_BIT_WIDTH);
188  this->set_baud_rate(_cfg_baudrate);
189  }
190 
195  void set_stop_bit_mask () {
196  // Create the mask to the far right
197  this->m_stopBitMask = 1;
198  for (uint8_t i = 0; i < this->m_stopBitWidth - 1; ++i)
199  this->m_stopBitMask |= this->m_stopBitMask << 1;
200 
201  // Shift the mask into position (taking into account the current
202  // parity settings)
203  this->m_stopBitMask <<= this->m_dataWidth;
204  if (Parity::NO_PARITY != this->m_parity)
205  this->m_stopBitMask <<= 1;
206  }
207 
212  void set_parity_mask () {
213  this->m_parityMask = (uint16_t) (1 << this->m_dataWidth);
214  }
215 
222  void set_total_bits () {
223  // Total bits = start + data + parity + stop bits
224  this->m_totalBits = (uint8_t) (1 + this->m_dataWidth
225  + this->m_stopBitWidth);
226  if (Parity::NO_PARITY != this->m_parity)
227  ++this->m_totalBits;
228  }
229 
230  protected:
231  uint8_t m_dataWidth;
232  uint16_t m_dataMask;
233  UART::Parity m_parity;
234  uint16_t m_parityMask;
235  uint8_t m_stopBitWidth;
236  uint32_t m_stopBitMask;
237  uint32_t m_bitCycles;
238  uint8_t m_totalBits;
239 };
240 
241 #ifdef __PROPELLER_COG__
242 #undef virtual
243 #endif
244 
245 }
PropWare::UART::Parity::ODD_PARITY
@ ODD_PARITY
PropWare::UART::BAUD_TOO_HIGH
@ BAUD_TOO_HIGH
Definition: uart.h:105
PropWare::UART::Parity::NO_PARITY
@ NO_PARITY
PropWare::UART::INVALID_STOP_BIT_WIDTH
@ INVALID_STOP_BIT_WIDTH
Definition: uart.h:108
PropWare::UART::BEG_ERROR
@ BEG_ERROR
Definition: uart.h:104
PropWare::UART
Abstract base class for all unbuffered UART devices.
Definition: uart.h:86
PropWare::UART::TIMEOUT_ERROR
@ TIMEOUT_ERROR
Definition: uart.h:110
PropWare::UART::NULL_POINTER
@ NULL_POINTER
Definition: uart.h:109
uartcommondata.h
PropWare::UART::PARITY_ERROR
@ PARITY_ERROR
Definition: uart.h:106
PropWare.h
CLKFREQ
#define CLKFREQ
Returns the current clock frequency.
Definition: propeller.h:46
PropWare::UART::Parity::EVEN_PARITY
@ EVEN_PARITY
UART_ERRORS_BASE
#define UART_ERRORS_BASE
Definition: uart.h:97
PropWare::UART::END_ERROR
@ END_ERROR
Definition: uart.h:111
PropWare::UART::ErrorCode
ErrorCode
Definition: uart.h:102
PropWare
Generic definitions and functions for the Parallax Propeller.
Definition: runnable.h:33
PropWare::UART::INVALID_DATA_WIDTH
@ INVALID_DATA_WIDTH
Definition: uart.h:107
PropWare::UART::NO_ERROR
@ NO_ERROR
Definition: uart.h:103
PropWare::UART::Parity
Parity
Definition: uart.h:88