eRTK
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
Macros | Enumerations | Functions
uart.h File Reference
#include <util/atomic.h>
#include "eRTK.h"
Include dependency graph for uart.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define UART0_BAUD   19200lu
 
#define UART1_BAUD   9600lu
 
#define UART2_BAUD   9600lu
 
#define UART3_BAUD   9600lu
 
#define ROLLOVER(x, max)   x = ++x >= max ? 0 : x
 
#define RX0_SIZE   16
 
#define TX0_SIZE   16
 
#define UBRR0_VAL   ((F_CPU+UART0_BAUD*8)/(UART0_BAUD*16)-1)
 
#define BAUD0_REAL   (F_CPU/(16*(UBRR0_VAL+1)))
 
#define BAUD0_ERROR   ((BAUD0_REAL*1000)/UART0_BAUD)
 
#define RX1_SIZE   16
 
#define TX1_SIZE   16
 
#define UBRR1_VAL   ((F_CPU+UART1_BAUD*8)/(UART1_BAUD*16)-1)
 
#define BAUD1_REAL   (F_CPU/(16*(UBRR1_VAL+1)))
 
#define BAUD1_ERROR   ((BAUD1_REAL*1000)/UART1_BAUD)
 
#define RX2_SIZE   16
 
#define TX2_SIZE   16
 
#define UBRR2_VAL   ((F_CPU+UART2_BAUD*8)/(UART2_BAUD*16)-1)
 
#define BAUD2_REAL   (F_CPU/(16*(UBRR2_VAL+1)))
 
#define BAUD2_ERROR   ((BAUD2_REAL*1000)/UART2_BAUD)
 
#define RX3_SIZE   16
 
#define TX3_SIZE   16
 
#define UBRR3_VAL   ((F_CPU+UART3_BAUD*8)/(UART3_BAUD*16)-1)
 
#define BAUD3_REAL   (F_CPU/(16*(UBRR3_VAL+1)))
 
#define BAUD3_ERROR   ((BAUD3_REAL*1000)/UART3_BAUD)
 

Enumerations

enum  tUART { UART0 =1, UART1 =2, UART2 =3, UART3 =4 }
 

Functions

tUART open (tUART port)
 
uint8_t read (tUART port, void *puffer, uint8_t nbytes, uint8_t timeout)
 
void write (tUART port, void *puffer, uint8_t nbytes)
 

Macro Definition Documentation

#define BAUD0_ERROR   ((BAUD0_REAL*1000)/UART0_BAUD)

Definition at line 28 of file uart.h.

#define BAUD0_REAL   (F_CPU/(16*(UBRR0_VAL+1)))

Definition at line 27 of file uart.h.

#define BAUD1_ERROR   ((BAUD1_REAL*1000)/UART1_BAUD)

Definition at line 40 of file uart.h.

#define BAUD1_REAL   (F_CPU/(16*(UBRR1_VAL+1)))

Definition at line 39 of file uart.h.

#define BAUD2_ERROR   ((BAUD2_REAL*1000)/UART2_BAUD)

Definition at line 51 of file uart.h.

#define BAUD2_REAL   (F_CPU/(16*(UBRR2_VAL+1)))

Definition at line 50 of file uart.h.

#define BAUD3_ERROR   ((BAUD3_REAL*1000)/UART3_BAUD)

Definition at line 62 of file uart.h.

#define BAUD3_REAL   (F_CPU/(16*(UBRR3_VAL+1)))

Definition at line 61 of file uart.h.

#define ROLLOVER (   x,
  max 
)    x = ++x >= max ? 0 : x

Definition at line 21 of file uart.h.

#define RX0_SIZE   16

Definition at line 24 of file uart.h.

#define RX1_SIZE   16

Definition at line 36 of file uart.h.

#define RX2_SIZE   16

Definition at line 47 of file uart.h.

#define RX3_SIZE   16

Definition at line 58 of file uart.h.

#define TX0_SIZE   16

Definition at line 25 of file uart.h.

#define TX1_SIZE   16

Definition at line 37 of file uart.h.

#define TX2_SIZE   16

Definition at line 48 of file uart.h.

#define TX3_SIZE   16

Definition at line 59 of file uart.h.

#define UART0_BAUD   19200lu

Definition at line 14 of file uart.h.

#define UART1_BAUD   9600lu

Definition at line 15 of file uart.h.

#define UART2_BAUD   9600lu

Definition at line 16 of file uart.h.

#define UART3_BAUD   9600lu

Definition at line 17 of file uart.h.

#define UBRR0_VAL   ((F_CPU+UART0_BAUD*8)/(UART0_BAUD*16)-1)

Definition at line 26 of file uart.h.

#define UBRR1_VAL   ((F_CPU+UART1_BAUD*8)/(UART1_BAUD*16)-1)

Definition at line 38 of file uart.h.

#define UBRR2_VAL   ((F_CPU+UART2_BAUD*8)/(UART2_BAUD*16)-1)

Definition at line 49 of file uart.h.

#define UBRR3_VAL   ((F_CPU+UART3_BAUD*8)/(UART3_BAUD*16)-1)

Definition at line 60 of file uart.h.

Enumeration Type Documentation

enum tUART
Enumerator
UART0 
UART1 
UART2 
UART3 

Definition at line 68 of file uart.h.

68  {
69 #ifdef UART0_BAUD
70  UART0=1,
71 #endif
72 #ifdef UART1_BAUD
73  UART1=2,
74 #endif
75 #ifdef UART2_BAUD
76  UART2=3,
77 #endif
78 #ifdef UART3_BAUD
79  UART3=4,
80 #endif
81  } tUART;
tUART
Definition: uart.h:68
Definition: uart.h:76
Definition: uart.h:73
Definition: uart.h:79
Definition: uart.h:70

Function Documentation

tUART open ( tUART  port)

Definition at line 51 of file uart.c.

51  {
52  eRTK_get_sema( port );
53  switch( port ) {
54 #ifdef UART0_BAUD
55  case UART0: {
56  UBRR0H = ( UBRR0_VAL>>8 );
57  UBRR0L = UBRR0_VAL; //set baud rate
58  UCSR0A = 0; //no U2X, MPCM
59  UCSR0C = ( 1<<UCSZ01 )^( 1<<UCSZ00 )^( 1<<USBS0 )^( 0<<UPM01 )^( 0<<UPM00 ); // 8 Bit, no parity
60  UCSR0B = ( 1<<RXEN0 )^( 1<<TXEN0 )^( 1<<RXCIE0 ); // enable RX, TX, RX interrupt
61  rx_in0=rx_out0=0; // set rx buffer empty
62  break;
63  }
64 #endif
65 #ifdef UART1_BAUD
66  case UART1: {
67  UBRR1H = ( UBRR1_VAL>>8 );
68  UBRR1L = UBRR1_VAL; //set baud rate
69  UCSR1A = 0; //no U2X, MPCM
70  UCSR1C = ( 1<<UCSZ11 )^( 1<<UCSZ10 )^( 1<<USBS1 )^( 0<<UPM11 )^( 0<<UPM10 ); // 8 Bit, no parity
71  UCSR1B = ( 1<<RXEN1 )^( 1<<TXEN1 )^( 1<<RXCIE1 ); // enable RX, TX, RX interrupt
72  rx_in1=rx_out1=0; // set rx buffer empty
73  break;
74  }
75 #endif
76 #ifdef UART2_BAUD
77  case UART2: {
78  UBRR2H = ( UBRR2_VAL>>8 );
79  UBRR2L = UBRR2_VAL; //set baud rate
80  UCSR2A = 0; //no U2X, MPCM
81  UCSR2C = ( 1<<UCSZ21 )^( 1<<UCSZ20 )^( 1<<USBS2 )^( 0<<UPM21 )^( 0<<UPM20 ); // 8 Bit, no parity
82  UCSR2B = ( 1<<RXEN2 )^( 1<<TXEN2 )^( 1<<RXCIE2 ); // enable RX, TX, RX interrupt
83  rx_in2=rx_out2=0; // set rx buffer empty
84  break;
85  }
86 #endif
87 #ifdef UART3_BAUD
88  case UART3: {
89  UBRR3H = ( UBRR3_VAL>>8 );
90  UBRR3L = UBRR3_VAL; //set baud rate
91  UCSR3A = 0; //no U2X, MPCM
92  UCSR3C = ( 1<<UCSZ31 )^( 1<<UCSZ30 )^( 1<<USBS3 )^( 0<<UPM31 )^( 0<<UPM30 ); // 8 Bit, no parity
93  UCSR3B = ( 1<<RXEN3 )^( 1<<TXEN3 )^( 1<<RXCIE3 ); // enable RX, TX, RX interrupt
94  rx_in3=rx_out3=0; // set rx buffer empty
95  break;
96  }
97 #endif
98  default: deadbeef( SYS_UNKNOWN );
99  }
100  return port;
101  }
Definition: uart.h:76
#define UBRR2_VAL
Definition: uart.h:49
void eRTK_get_sema(uint8_t semaid)
Definition: eRTK.c:309
#define UBRR1_VAL
Definition: uart.h:38
Definition: uart.h:73
Definition: uart.h:79
#define UBRR3_VAL
Definition: uart.h:60
void deadbeef(tsys reason)
Definition: uart.h:70
#define UBRR0_VAL
Definition: uart.h:26

Here is the call graph for this function:

Here is the caller graph for this function:

uint8_t read ( tUART  port,
void *  puffer,
uint8_t  nbytes,
uint8_t  timeout 
)

Definition at line 249 of file uart.c.

249  {
250  register uint8_t anz=0;
251  switch( port ) {
252 #ifdef UART0_BAUD
253  case UART0: {
254  if( puffer==NULL ) { //clear buffer
255  rx_in0=rx_out0;
256  return 0;
257  }
258  UCSR0B|=( 1<<RXCIE0 ); //enable RX interrupt
259  while( anz<nbytes ) { //bis genug im puffer steht
260  if( rx_in0!=rx_out0 ) { //puffer auslesen wenn zeichen vorhanden sind
261  *( uint8_t * )puffer++=rx_buf0[rx_out0];
262  ROLLOVER( rx_out0, RX0_SIZE );
263  ++anz;
264  }
265  else { //auf neue zeichen warten
266  if( timeout ) { //wenn ein timeout angegeben wurde
267  ATOMIC_BLOCK( ATOMIC_RESTORESTATE ) {
268  rxtid0=eRTK_GetTid();
269  nrx0=nbytes-anz;
270  eRTK_wefet( timeout );
271  rxtid0=0;
272  }
273  timeout=0;
274  }
275  else break;
276  }
277  }
278  break;
279  }
280 #endif
281 #ifdef UART1_BAUD
282  case UART1: {
283  if( puffer==NULL ) { //clear buffer
284  rx_in1=rx_out1;
285  return 0;
286  }
287  UCSR1B|=( 1<<RXCIE1 ); //enable RX interrupt
288  while( anz<nbytes ) { //bis genug im puffer steht
289  if( rx_in1!=rx_out1 ) { //puffer auslesen wenn zeichen vorhanden sind
290  *( uint8_t * )puffer++=rx_buf1[rx_out1];
291  ROLLOVER( rx_out1, RX1_SIZE );
292  ++anz;
293  }
294  else { //auf neue zeichen warten
295  if( timeout ) { //wenn ein timeout angegeben wurde
296  ATOMIC_BLOCK( ATOMIC_RESTORESTATE ) {
297  rxtid1=eRTK_GetTid();
298  nrx1=nbytes-anz;
299  eRTK_wefet( timeout );
300  rxtid1=0;
301  }
302  timeout=0;
303  }
304  else break;
305  }
306  }
307  break;
308  }
309 #endif
310 #ifdef UART2_BAUD
311  case UART2: {
312  if( puffer==NULL ) { //clear buffer
313  rx_in2=rx_out2;
314  return 0;
315  }
316  UCSR2B|=( 1<<RXCIE2 ); //enable RX interrupt
317  while( anz<nbytes ) { //bis genug im puffer steht
318  if( rx_in2!=rx_out2 ) { //puffer auslesen wenn zeichen vorhanden sind
319  *( uint8_t * )puffer++=rx_buf2[rx_out2];
320  ROLLOVER( rx_out2, RX2_SIZE );
321  ++anz;
322  }
323  else { //auf neue zeichen warten
324  if( timeout ) { //wenn ein timeout angegeben wurde
325  ATOMIC_BLOCK( ATOMIC_RESTORESTATE ) {
326  rxtid2=eRTK_GetTid();
327  nrx2=nbytes-anz;
328  eRTK_wefet( timeout );
329  rxtid2=0;
330  }
331  timeout=0;
332  }
333  else break;
334  }
335  }
336  break;
337  }
338 #endif
339 #ifdef UART3_BAUD
340  case UART3: {
341  if( puffer==NULL ) { //clear buffer
342  rx_in3=rx_out3;
343  return 0;
344  }
345  UCSR3B|=( 1<<RXCIE3 ); //enable RX interrupt
346  while( anz<nbytes ) { //bis genug im puffer steht
347  if( rx_in3!=rx_out3 ) { //puffer auslesen wenn zeichen vorhanden sind
348  *( uint8_t * )puffer++=rx_buf3[rx_out3];
349  ROLLOVER( rx_out3, RX3_SIZE );
350  ++anz;
351  }
352  else { //auf neue zeichen warten
353  if( timeout ) { //wenn ein timeout angegeben wurde
354  ATOMIC_BLOCK( ATOMIC_RESTORESTATE ) {
355  rxtid3=eRTK_GetTid();
356  nrx3=nbytes-anz;
357  eRTK_wefet( timeout );
358  rxtid3=0;
359  }
360  timeout=0;
361  }
362  else break;
363  }
364  }
365  break;
366  }
367 #endif
368  default: deadbeef( SYS_UNKNOWN );
369  }
370  return anz;
371  }
#define RX0_SIZE
Definition: uart.h:24
#define RX1_SIZE
Definition: uart.h:36
Definition: uart.h:76
uint8_t eRTK_GetTid(void)
Definition: eRTK.c:233
void eRTK_wefet(uint8_t timeout)
Definition: eRTK.c:269
Definition: uart.h:73
Definition: uart.h:79
#define ROLLOVER(x, max)
Definition: uart.h:21
void deadbeef(tsys reason)
#define RX3_SIZE
Definition: uart.h:58
#define RX2_SIZE
Definition: uart.h:47
Definition: uart.h:70

Here is the call graph for this function:

Here is the caller graph for this function:

void write ( tUART  port,
void *  puffer,
uint8_t  nbytes 
)

Definition at line 373 of file uart.c.

373  {
374  switch( port ) {
375 #ifdef UART0_BAUD
376  case( UART0 ): {
377  while( nbytes-- ) {
378  while( 1 ) { //notfalls warte bis der sendepuffer ein zeichen fassen kann
379  register uint8_t i=tx_in0;
380  ROLLOVER( i, TX0_SIZE );
381  if( i!=tx_out0 ) break; //noch platz im sendepuffer
382  ATOMIC_BLOCK( ATOMIC_RESTORESTATE ) {
383  UCSR0B|=( 1<<UDRIE0 ); //sonst sende interrupt wieder einschalten
384  txtid0=eRTK_GetTid(); //damit wir nicht aktiv warten
385  eRTK_SetSuspended( txtid0 );
386  eRTK_scheduler();
387  }
388  }
389  tx_buff0[tx_in0] = *( uint8_t * )puffer++;
390  ROLLOVER( tx_in0, TX0_SIZE );
391  UCSR0B|=( 1<<UDRIE0 ); //sende interrupt einschalten
392  }
393  break;
394  }
395 #endif
396 #ifdef UART1_BAUD
397  case( UART1 ): {
398  while( nbytes-- ) {
399  while( 1 ) { //notfalls warte bis der sendepuffer ein zeichen fassen kann
400  register uint8_t i=tx_in1;
401  ROLLOVER( i, TX1_SIZE );
402  if( i!=tx_out1 ) break; //noch platz im sendepuffer
403  ATOMIC_BLOCK( ATOMIC_RESTORESTATE ) {
404  UCSR1B|=( 1<<UDRIE1 ); //sonst sende interrupt wieder einschalten
405  txtid1=eRTK_GetTid(); //damit wir nicht aktiv warten
406  eRTK_SetSuspended( txtid1 );
407  eRTK_scheduler();
408  }
409  }
410  tx_buff1[tx_in1] = *( uint8_t * )puffer++;
411  ROLLOVER( tx_in1, TX1_SIZE );
412  UCSR1B|=( 1<<UDRIE1 ); //sende interrupt einschalten
413  }
414  break;
415  }
416 #endif
417 #ifdef UART2_BAUD
418  case( UART2 ): {
419  while( nbytes-- ) {
420  while( 1 ) { //notfalls warte bis der sendepuffer ein zeichen fassen kann
421  register uint8_t i=tx_in2;
422  ROLLOVER( i, TX2_SIZE );
423  if( i!=tx_out2 ) break; //noch platz im sendepuffer
424  ATOMIC_BLOCK( ATOMIC_RESTORESTATE ) {
425  UCSR2B|=( 1<<UDRIE2 ); //sonst sende interrupt wieder einschalten
426  txtid2=eRTK_GetTid(); //damit wir nicht aktiv warten
427  eRTK_SetSuspended( txtid2 );
428  eRTK_scheduler();
429  }
430  }
431  tx_buff2[tx_in2] = *( uint8_t * )puffer++;
432  ROLLOVER( tx_in2, TX2_SIZE );
433  UCSR2B|=( 1<<UDRIE2 ); //sende interrupt einschalten
434  }
435  break;
436  }
437 #endif
438 #ifdef UART3_BAUD
439  case( UART3 ): {
440  while( nbytes-- ) {
441  while( 1 ) { //notfalls warte bis der sendepuffer ein zeichen fassen kann
442  register uint8_t i=tx_in3;
443  ROLLOVER( i, TX3_SIZE );
444  if( i!=tx_out3 ) break; //noch platz im sendepuffer
445  ATOMIC_BLOCK( ATOMIC_RESTORESTATE ) {
446  UCSR3B|=( 1<<UDRIE3 ); //sonst sende interrupt wieder einschalten
447  txtid3=eRTK_GetTid(); //damit wir nicht aktiv warten
448  eRTK_SetSuspended( txtid3 );
449  eRTK_scheduler();
450  }
451  }
452  tx_buff3[tx_in3] = *( uint8_t * )puffer++;
453  ROLLOVER( tx_in3, TX3_SIZE );
454  UCSR3B|=( 1<<UDRIE3 ); //sende interrupt einschalten
455  }
456  break;
457  }
458 #endif
459  default: deadbeef( SYS_UNKNOWN );
460  }
461  }
Definition: uart.h:76
uint8_t eRTK_GetTid(void)
Definition: eRTK.c:233
#define TX3_SIZE
Definition: uart.h:59
Definition: uart.h:73
#define TX1_SIZE
Definition: uart.h:37
Definition: uart.h:79
#define TX2_SIZE
Definition: uart.h:48
#define ROLLOVER(x, max)
Definition: uart.h:21
void deadbeef(tsys reason)
void eRTK_SetSuspended(uint8_t tid)
Definition: eRTK.c:255
Definition: uart.h:70
#define TX0_SIZE
Definition: uart.h:25

Here is the call graph for this function:

Here is the caller graph for this function: