avrSerial  1.0
Multi-UART Library for many AVR devices
 All Files Functions Groups
Files | Macros | Functions
UART Library

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

Files

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

Macros

#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...
 
#define BAUD(baudRate, xtalCpu)   ((xtalCpu)/((baudRate)*16l)-1)
 Calculate Baudrate Register 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 serialGetBlocking (uint8_t uart)
 Wait until a character is received. More...
 
uint8_t serialGet (uint8_t uart)
 Read a single byte. 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.

Examples:
test.c.

Definition at line 45 of file serial.h.

#define FLOWCONTROL

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

Definition at line 62 of file serial.c.

#define FLOWMARK   5

Space remaining to trigger xoff/xon.

Definition at line 64 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 54 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 58 of file serial.c.

Referenced by serialTxBufferFull(), and serialWrite().

#define XOFF   0x13

XOFF Value.

Definition at line 66 of file serial.c.

Referenced by setFlow().

#define XON   0x11

XON Value.

Definition at line 65 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
Examples:
test.c.

Definition at line 113 of file serial.c.

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

Stop the UART Hardware.

Parameters
uartUART Module to stop

Definition at line 148 of file serial.c.

References serialTxBufferEmpty().

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

Read a single byte.

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

Definition at line 217 of file serial.c.

References FLOWMARK, RX_BUFFER_SIZE, and XON.

Referenced by serialGetBlocking().

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

Wait until a character is received.

Parameters
uartUART Module to read from
Returns
Received byte

Definition at line 209 of file serial.c.

References serialGet(), and serialHasChar().

209  {
210  if (uart >= UART_COUNT)
211  return 0;
212 
213  while(!serialHasChar(uart));
214  return serialGet(uart);
215 }
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:
test.c.

Definition at line 198 of file serial.c.

Referenced by serialGetBlocking().

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

Initialize the UART Hardware.

Parameters
uartUART Module to initialize
baudBaudrate. Use the BAUD() macro!
Examples:
test.c.

Definition at line 117 of file serial.c.

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

258  {
259  if (uart >= UART_COUNT)
260  return 0;
261 
262  if (rxRead[uart] != rxWrite[uart]) {
263  return 0;
264  } else {
265  return 1;
266  }
267 }
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 251 of file serial.c.

References RX_BUFFER_SIZE.

251  {
252  if (uart >= UART_COUNT)
253  return 0;
254 
255  return (((rxWrite[uart] + 1) == rxRead[uart]) || ((rxRead[uart] == 0) && ((rxWrite[uart] + 1) == RX_BUFFER_SIZE)));
256 }
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 317 of file serial.c.

Referenced by serialClose().

317  {
318  if (uart >= UART_COUNT)
319  return 0;
320 
321  if (txRead[uart] != txWrite[uart]) {
322  return 0;
323  } else {
324  return 1;
325  }
326 }
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 310 of file serial.c.

References TX_BUFFER_SIZE.

Referenced by serialWrite().

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

Send a byte.

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

Definition at line 273 of file serial.c.

References serialTxBufferFull(), and TX_BUFFER_SIZE.

Referenced by serialWriteString().

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

Send a string.

Parameters
uartUART Module to write to
dataNull-Terminated String
Examples:
test.c.

Definition at line 297 of file serial.c.

References serialWrite().

297  {
298  if (uart >= UART_COUNT)
299  return;
300 
301  if (data == 0) {
302  serialWriteString(uart, "NULL");
303  } else {
304  while (*data != '\0') {
305  serialWrite(uart, *data++);
306  }
307  }
308 }
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 163 of file serial.c.

References XOFF, and XON.

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