Kilolib
Kilobot Library
debug.h
Go to the documentation of this file.
1 #ifndef __DEBUG_H__
2 #define __DEBUG_H__
3 
44 #ifdef DEBUG
45 
46 #include <stdio.h>
47 #include <avr/io.h>
48 #include <avr/interrupt.h>
49 
50 #ifdef NONBLOCKING
51 #include "ringbuffer.h"
52 RB_create(debug_buffer, char, 128);
53 
54 static int debug_putchar(char c, FILE *stream) {
55  if (RB_full(debug_buffer)) {
56  return -1;
57  } else {
58  RB_back(debug_buffer) = c;
59  RB_pushback(debug_buffer);
60  UCSR0B |= (1<<UDRIE0);
61  return 0;
62  }
63 }
64 
65 ISR(USART_UDRE_vect) {
66  if (RB_empty(debug_buffer)) {
67  UCSR0B &= ~(1<<UDRIE0);
68  } else {
69  UDR0 = RB_front(debug_buffer);
70  RB_popfront(debug_buffer);
71  }
72 }
73 
74 #define debug_init_extra() {\
75  RB_init(debug_buffer);\
76 }
77 
78 #else
79 static int debug_putchar(char c, FILE *stream) {
80  UDR0 = c;
81  while(!(UCSR0A & (1<<UDRE0)));
82  return 0;
83 }
84 
85 // static int debug_getchar(FILE *stream) {
86 // while(!(UCSR0A & (1<<RXC0)));
87 // return UDR0;
88 // }
89 
90 #define debug_init_extra() {}
91 
92 #endif
93 
94 
95 
96 void debug_init() {
97  static FILE debug_stdout = FDEV_SETUP_STREAM(debug_putchar, NULL, _FDEV_SETUP_WRITE);
98  cli();
99  DDRD |= (1<<1); // Set UART TxD pin as output
100 #ifndef BAUD
101 #define BAUD 38400
102 #endif
103 #include <util/setbaud.h>
104  UBRR0 = UBRR_VALUE;
105 #if USE_2X
106  UCSR0A |= (1<<U2X0);
107 #else
108  UCSR0A &= ~(1<<U2X0);
109 #endif
110  UCSR0C |= (1<<UCSZ01)|(1<<UCSZ00); // No parity, 8 bits comm, 1 stop bit
111  UCSR0B |= (1<<TXEN0); // Enable transmission
112  debug_init_extra();
113  stdout = &debug_stdout;
114  sei();
115 }
116 
117 
118 #else
119 
120 #define debug_init()
121 #define printf(...)
122 
123 #endif//DEBUG
124 
125 #endif//__DEBUG_H__
#define debug_init()
debug initialization
Definition: debug.h:120