xyControl  0.1
Quadrotor Flight Controller on AVR Basis
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups
Files | Macros | Functions
UART Library

UART Library enabling you to control all available UART Modules. More...

Files

file  serial.h
 UART Library Header File.
 
file  serial_device.h
 UART Library device-specific configuration.
 
file  serial.c
 UART Library Implementation.
 

Macros

#define USB   0
 First UART Name. More...
 
#define BLUETOOTH   1
 Second UART Name. More...
 
#define BAUD(baudRate, xtalCpu)   ((xtalCpu)/((baudRate)*16l)-1)
 Calculate Baudrate Register Value. More...
 
#define RX_BUFFER_SIZE   32
 If you define this, a '\r' (CR) will be put in front of a '\n' (LF) when sending a byte. More...
 
#define TX_BUFFER_SIZE   16
 TX Buffer Size in Bytes (Power of 2) More...
 
#define FLOWCONTROL
 Defining this enables incoming XON XOFF (sends XOFF if rx buff is full) More...
 
#define FLOWMARK   5
 Space remaining to trigger xoff/xon. More...
 
#define XON   0x11
 XON Value. More...
 
#define XOFF   0x13
 XOFF Value. More...
 

Functions

uint8_t serialAvailable (void)
 Get number of available UART modules. More...
 
void serialInit (uint8_t uart, uint16_t baud)
 Initialize the UART Hardware. More...
 
void serialClose (uint8_t uart)
 Stop the UART Hardware. More...
 
void setFlow (uint8_t uart, uint8_t on)
 Manually change the flow control. More...
 
uint8_t serialHasChar (uint8_t uart)
 Check if a byte was received. More...
 
uint8_t serialGet (uint8_t uart)
 Read a single byte. More...
 
uint8_t serialGetBlocking (uint8_t uart)
 Wait until a character is received. More...
 
uint8_t serialRxBufferFull (uint8_t uart)
 Check if the receive buffer is full. More...
 
uint8_t serialRxBufferEmpty (uint8_t uart)
 Check if the receive buffer is empty. More...
 
void serialWrite (uint8_t uart, uint8_t data)
 Send a byte. More...
 
void serialWriteString (uint8_t uart, const char *data)
 Send a string. More...
 
uint8_t serialTxBufferFull (uint8_t uart)
 Check if the transmit buffer is full. More...
 
uint8_t serialTxBufferEmpty (uint8_t uart)
 Check if the transmit buffer is empty. More...
 

Detailed Description

UART Library enabling you to control all available UART Modules.

With XON/XOFF Flow Control and buffered Receiving and Transmitting.

Macro Definition Documentation

#define BAUD (   baudRate,
  xtalCpu 
)    ((xtalCpu)/((baudRate)*16l)-1)

Calculate Baudrate Register Value.

Definition at line 49 of file serial.h.

Referenced by xyInit().

#define BLUETOOTH   1

Second UART Name.

Examples:
hardwareTest.c.

Definition at line 46 of file serial.h.

#define FLOWCONTROL

Defining this enables incoming XON XOFF (sends XOFF if rx buff is full)

Definition at line 63 of file serial.c.

#define FLOWMARK   5

Space remaining to trigger xoff/xon.

Definition at line 65 of file serial.c.

Referenced by serialGet().

#define RX_BUFFER_SIZE   32

If you define this, a '\r' (CR) will be put in front of a '\n' (LF) when sending a byte.

Binary Communication will then be impossible!RX Buffer Size in Bytes (Power of 2)

Definition at line 55 of file serial.c.

Referenced by serialGet(), and serialRxBufferFull().

#define TX_BUFFER_SIZE   16

TX Buffer Size in Bytes (Power of 2)

Definition at line 59 of file serial.c.

Referenced by serialTxBufferFull(), and serialWrite().

#define USB   0

First UART Name.

Examples:
hardwareTest.c.

Definition at line 45 of file serial.h.

#define XOFF   0x13

XOFF Value.

Definition at line 67 of file serial.c.

Referenced by setFlow().

#define XON   0x11

XON Value.

Definition at line 66 of file serial.c.

Referenced by serialGet(), and setFlow().

Function Documentation

uint8_t serialAvailable ( void  )

Get number of available UART modules.

Returns
number of modules

Definition at line 114 of file serial.c.

Referenced by uartinput(), uartMenuTask(), uartoutput(), and xyInit().

114  {
115  return UART_COUNT;
116 }
void serialClose ( uint8_t  uart)

Stop the UART Hardware.

Parameters
uartUART Module to stop

Definition at line 149 of file serial.c.

References serialTxBufferEmpty().

149  {
150  if (uart >= UART_COUNT)
151  return;
152 
153  uint8_t sreg = SREG;
154  sei();
155  while (!serialTxBufferEmpty(uart));
156  while (*serialRegisters[uart][SERIALB] & (1 << serialBits[uart][SERIALUDRIE])); // Wait while Transmit Interrupt is on
157  cli();
158  *serialRegisters[uart][SERIALB] = 0;
159  *serialRegisters[uart][SERIALC] = 0;
160  SREG = sreg;
161 }
uint8_t serialGet ( uint8_t  uart)

Read a single byte.

Parameters
uartUART Module to read from
Returns
Received byte or 0
Examples:
hardwareTest.c.

Definition at line 218 of file serial.c.

References FLOWMARK, RX_BUFFER_SIZE, and XON.

Referenced by serialGetBlocking(), uartinput(), and uartMenuTask().

218  {
219  if (uart >= UART_COUNT)
220  return 0;
221 
222  uint8_t c;
223 
224 #ifdef FLOWCONTROL
225  rxBufferElements[uart]--;
226  if ((flow[uart] == 0) && (rxBufferElements[uart] <= FLOWMARK)) {
227  while (sendThisNext[uart] != 0);
228  sendThisNext[uart] = XON;
229  flow[uart] = 1;
230  if (shouldStartTransmission[uart]) {
231  shouldStartTransmission[uart] = 0;
232  *serialRegisters[uart][SERIALB] |= (1 << serialBits[uart][SERIALUDRIE]); // Enable Interrupt
233  *serialRegisters[uart][SERIALA] |= (1 << serialBits[uart][SERIALUDRE]); // Trigger Interrupt
234  }
235  }
236 #endif
237 
238  if (rxRead[uart] != rxWrite[uart]) {
239  c = rxBuffer[uart][rxRead[uart]];
240  rxBuffer[uart][rxRead[uart]] = 0;
241  if (rxRead[uart] < (RX_BUFFER_SIZE - 1)) {
242  rxRead[uart]++;
243  } else {
244  rxRead[uart] = 0;
245  }
246  return c;
247  } else {
248  return 0;
249  }
250 }
uint8_t serialGetBlocking ( uint8_t  uart)

Wait until a character is received.

Parameters
uartUART Module to read from
Returns
Received byte

Definition at line 210 of file serial.c.

References serialGet(), and serialHasChar().

210  {
211  if (uart >= UART_COUNT)
212  return 0;
213 
214  while(!serialHasChar(uart));
215  return serialGet(uart);
216 }
uint8_t serialHasChar ( uint8_t  uart)

Check if a byte was received.

Parameters
uartUART Module to check
Returns
1 if a byte was received, 0 if not
Examples:
hardwareTest.c.

Definition at line 199 of file serial.c.

Referenced by serialGetBlocking(), uartinput(), and uartMenuTask().

199  {
200  if (uart >= UART_COUNT)
201  return 0;
202 
203  if (rxRead[uart] != rxWrite[uart]) { // True if char available
204  return 1;
205  } else {
206  return 0;
207  }
208 }
void serialInit ( uint8_t  uart,
uint16_t  baud 
)

Initialize the UART Hardware.

Parameters
uartUART Module to initialize
baudBaudrate. Use the BAUD() macro!

Definition at line 118 of file serial.c.

Referenced by xyInit().

118  {
119  if (uart >= UART_COUNT)
120  return;
121 
122  // Initialize state variables
123  rxRead[uart] = 0;
124  rxWrite[uart] = 0;
125  txRead[uart] = 0;
126  txWrite[uart] = 0;
127  shouldStartTransmission[uart] = 1;
128 #ifdef FLOWCONTROL
129  sendThisNext[uart] = 0;
130  flow[uart] = 1;
131  rxBufferElements[uart] = 0;
132 #endif
133 
134  // Default Configuration: 8N1
135  *serialRegisters[uart][SERIALC] = (1 << serialBits[uart][SERIALUCSZ0]) | (1 << serialBits[uart][SERIALUCSZ1]);
136 
137  // Set baudrate
138 #if SERIALBAUDBIT == 8
139  *serialRegisters[uart][SERIALUBRRH] = (baud >> 8);
140  *serialRegisters[uart][SERIALUBRRL] = baud;
141 #else
142  *serialBaudRegisters[uart] = baud;
143 #endif
144 
145  *serialRegisters[uart][SERIALB] = (1 << serialBits[uart][SERIALRXCIE]); // Enable Interrupts
146  *serialRegisters[uart][SERIALB] |= (1 << serialBits[uart][SERIALRXEN]) | (1 << serialBits[uart][SERIALTXEN]); // Enable Receiver/Transmitter
147 }
uint8_t serialRxBufferEmpty ( uint8_t  uart)

Check if the receive buffer is empty.

Parameters
uartUART Module to check
Returns
1 if buffer is empty, 0 if not.

Definition at line 259 of file serial.c.

259  {
260  if (uart >= UART_COUNT)
261  return 0;
262 
263  if (rxRead[uart] != rxWrite[uart]) {
264  return 0;
265  } else {
266  return 1;
267  }
268 }
uint8_t serialRxBufferFull ( uint8_t  uart)

Check if the receive buffer is full.

Parameters
uartUART Module to check
Returns
1 if buffer is full, 0 if not

Definition at line 252 of file serial.c.

References RX_BUFFER_SIZE.

252  {
253  if (uart >= UART_COUNT)
254  return 0;
255 
256  return (((rxWrite[uart] + 1) == rxRead[uart]) || ((rxRead[uart] == 0) && ((rxWrite[uart] + 1) == RX_BUFFER_SIZE)));
257 }
uint8_t serialTxBufferEmpty ( uint8_t  uart)

Check if the transmit buffer is empty.

Parameters
uartUART Module to check
Returns
1 if buffer is empty, 0 if not.

Definition at line 318 of file serial.c.

Referenced by serialClose().

318  {
319  if (uart >= UART_COUNT)
320  return 0;
321 
322  if (txRead[uart] != txWrite[uart]) {
323  return 0;
324  } else {
325  return 1;
326  }
327 }
uint8_t serialTxBufferFull ( uint8_t  uart)

Check if the transmit buffer is full.

Parameters
uartUART Module to check
Returns
1 if buffer is full, 0 if not

Definition at line 311 of file serial.c.

References TX_BUFFER_SIZE.

Referenced by serialWrite().

311  {
312  if (uart >= UART_COUNT)
313  return 0;
314 
315  return (((txWrite[uart] + 1) == txRead[uart]) || ((txRead[uart] == 0) && ((txWrite[uart] + 1) == TX_BUFFER_SIZE)));
316 }
void serialWrite ( uint8_t  uart,
uint8_t  data 
)

Send a byte.

Parameters
uartUART Module to write to
dataByte to send
Examples:
hardwareTest.c.

Definition at line 274 of file serial.c.

References serialTxBufferFull(), and TX_BUFFER_SIZE.

Referenced by serialWriteString(), and uartoutput().

274  {
275  if (uart >= UART_COUNT)
276  return;
277 
278 #ifdef SERIALINJECTCR
279  if (data == '\n') {
280  serialWrite(uart, '\r');
281  }
282 #endif
283  while (serialTxBufferFull(uart));
284 
285  txBuffer[uart][txWrite[uart]] = data;
286  if (txWrite[uart] < (TX_BUFFER_SIZE - 1)) {
287  txWrite[uart]++;
288  } else {
289  txWrite[uart] = 0;
290  }
291  if (shouldStartTransmission[uart]) {
292  shouldStartTransmission[uart] = 0;
293  *serialRegisters[uart][SERIALB] |= (1 << serialBits[uart][SERIALUDRIE]); // Enable Interrupt
294  *serialRegisters[uart][SERIALA] |= (1 << serialBits[uart][SERIALUDRE]); // Trigger Interrupt
295  }
296 }
void serialWriteString ( uint8_t  uart,
const char *  data 
)

Send a string.

Parameters
uartUART Module to write to
dataNull-Terminated String

Definition at line 298 of file serial.c.

References serialWrite().

298  {
299  if (uart >= UART_COUNT)
300  return;
301 
302  if (data == 0) {
303  serialWriteString(uart, "NULL");
304  } else {
305  while (*data != '\0') {
306  serialWrite(uart, *data++);
307  }
308  }
309 }
void setFlow ( uint8_t  uart,
uint8_t  on 
)

Manually change the flow control.

Flow Control has to be compiled into the library!

Parameters
uartUART Module to operate on
on1 of on, 0 if off

Definition at line 164 of file serial.c.

References XOFF, and XON.

164  {
165  if (uart >= UART_COUNT)
166  return;
167 
168  if (flow[uart] != on) {
169  if (on == 1) {
170  // Send XON
171  while (sendThisNext[uart] != 0);
172  sendThisNext[uart] = XON;
173  flow[uart] = 1;
174  if (shouldStartTransmission[uart]) {
175  shouldStartTransmission[uart] = 0;
176  *serialRegisters[uart][SERIALB] |= (1 << serialBits[uart][SERIALUDRIE]);
177  *serialRegisters[uart][SERIALA] |= (1 << serialBits[uart][SERIALUDRE]); // Trigger Interrupt
178  }
179  } else {
180  // Send XOFF
181  sendThisNext[uart] = XOFF;
182  flow[uart] = 0;
183  if (shouldStartTransmission[uart]) {
184  shouldStartTransmission[uart] = 0;
185  *serialRegisters[uart][SERIALB] |= (1 << serialBits[uart][SERIALUDRIE]);
186  *serialRegisters[uart][SERIALA] |= (1 << serialBits[uart][SERIALUDRE]); // Trigger Interrupt
187  }
188  }
189  // Wait till it's transmitted
190  while (*serialRegisters[uart][SERIALB] & (1 << serialBits[uart][SERIALUDRIE]));
191  }
192 }