1 | //-----------------------------------------------------------------------------
|
---|
2 |
|
---|
3 | #include "usart.h"
|
---|
4 |
|
---|
5 | #include <avr/interrupt.h>
|
---|
6 | //-----------------------------------------------------------------------------
|
---|
7 |
|
---|
8 | #ifdef USART_USE_RX_IRQ
|
---|
9 | U08 usart_rx_buffer[USART_RX_BUFFER_SIZE];
|
---|
10 | //U08 *usart_rx_buffer_ptr = &usart_rx_buffer[0];
|
---|
11 | U08 usart_received_chars;
|
---|
12 | volatile BOOL usart_rx_ready = false;
|
---|
13 | static U08 usart_rx_buffer_index = 0;
|
---|
14 | static U08 usart_receive_char;
|
---|
15 | static BOOL usart_receive_suspended = false;
|
---|
16 | volatile BOOL ISR_toggle_out = false;
|
---|
17 | #endif
|
---|
18 | //-----------------------------------------------------------------------------
|
---|
19 |
|
---|
20 | void usart_init(void)
|
---|
21 | {
|
---|
22 | USART_SET_BAUDRATE(USART_BAUDRATE);
|
---|
23 |
|
---|
24 | UCSRA = 0x00;
|
---|
25 |
|
---|
26 | UCSRB = 0x00; // Disable receiver and transmitter and interrupts
|
---|
27 |
|
---|
28 | #ifdef USART_USE_RX
|
---|
29 | UCSRB |= (1 << RXEN); // Turn on receiver
|
---|
30 | DDRD &= ~(1<<PD0); // PD0 is RXD
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | #ifdef USART_USE_RX_IRQ
|
---|
34 | UCSRB |= (1 << RXCIE); // Enable rx interrupt
|
---|
35 | #endif
|
---|
36 |
|
---|
37 | #ifdef USART_USE_TX
|
---|
38 | UCSRB |= (1 << TXEN); // Turn on transmitter
|
---|
39 | DDRD |= 1<<PD1; // PD1 is TXD
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | UCSRC = (1 << URSEL) | (1 << UCSZ1) | (1 << UCSZ0); // 8-Bit character length
|
---|
43 | }
|
---|
44 | //-----------------------------------------------------------------------------
|
---|
45 |
|
---|
46 | void usart_write_char(U08 data)
|
---|
47 | {
|
---|
48 | while (!(UCSRA & (1 << UDRE))) ; // Wait until tx register is empty
|
---|
49 |
|
---|
50 | UDR = data;
|
---|
51 | }
|
---|
52 | //-----------------------------------------------------------------------------
|
---|
53 |
|
---|
54 | void usart_write_crlf(void)
|
---|
55 | {
|
---|
56 | usart_write_char(USART_CHAR_CR);
|
---|
57 | usart_write_char(USART_CHAR_LF);
|
---|
58 | }
|
---|
59 | //-----------------------------------------------------------------------------
|
---|
60 |
|
---|
61 | void usart_write_str(pU08 str)
|
---|
62 | {
|
---|
63 | while (*str)
|
---|
64 | {
|
---|
65 | usart_write_char(*str++);
|
---|
66 | }
|
---|
67 | }
|
---|
68 | //-----------------------------------------------------------------------------
|
---|
69 |
|
---|
70 | void usart_write_flash_str(fpU08 str)
|
---|
71 | {
|
---|
72 | while (*str)
|
---|
73 | {
|
---|
74 | usart_write_char(*str++);
|
---|
75 | }
|
---|
76 | }
|
---|
77 | //-----------------------------------------------------------------------------
|
---|
78 |
|
---|
79 | void usart_writeln_flash_str(fpU08 str)
|
---|
80 | {
|
---|
81 | while (*str)
|
---|
82 | {
|
---|
83 | usart_write_char(*str++);
|
---|
84 | }
|
---|
85 |
|
---|
86 | usart_write_char(USART_CHAR_CR);
|
---|
87 | usart_write_char(USART_CHAR_LF);
|
---|
88 | }
|
---|
89 | //-----------------------------------------------------------------------------
|
---|
90 |
|
---|
91 | void usart_writeln_str(pU08 str)
|
---|
92 | {
|
---|
93 | usart_write_str(str);
|
---|
94 | usart_write_char(USART_CHAR_CR);
|
---|
95 | usart_write_char(USART_CHAR_LF);
|
---|
96 | }
|
---|
97 | //-----------------------------------------------------------------------------
|
---|
98 |
|
---|
99 | void usart_write_U08(U08 value,U08 digits)
|
---|
100 | {
|
---|
101 | usart_write_str(nc_U08_to_str(value,digits));
|
---|
102 | }
|
---|
103 | //-----------------------------------------------------------------------------
|
---|
104 |
|
---|
105 | void usart_write_S08(S08 value,U08 digits)
|
---|
106 | {
|
---|
107 | usart_write_str(nc_S08_to_str(value,digits));
|
---|
108 | }
|
---|
109 | //-----------------------------------------------------------------------------
|
---|
110 |
|
---|
111 | void usart_write_U08_hex(U08 value)
|
---|
112 | {
|
---|
113 | usart_write_str(nc_U08_to_hex(value));
|
---|
114 | }
|
---|
115 | //-----------------------------------------------------------------------------
|
---|
116 |
|
---|
117 | void usart_write_U08_bin(U08 value)
|
---|
118 | {
|
---|
119 | usart_write_str(nc_U08_to_bin(value));
|
---|
120 | }
|
---|
121 | //-----------------------------------------------------------------------------
|
---|
122 |
|
---|
123 | void usart_write_U16(U16 value,U08 digits)
|
---|
124 | {
|
---|
125 | usart_write_str(nc_U16_to_str(value,digits));
|
---|
126 | }
|
---|
127 | //-----------------------------------------------------------------------------
|
---|
128 |
|
---|
129 | void usart_write_S16(S16 value,U08 digits)
|
---|
130 | {
|
---|
131 | usart_write_str(nc_S16_to_str(value,digits));
|
---|
132 | }
|
---|
133 | //-----------------------------------------------------------------------------
|
---|
134 |
|
---|
135 | void usart_write_U16_hex(U16 value)
|
---|
136 | {
|
---|
137 | usart_write_str(nc_U16_to_hex(value));
|
---|
138 | }
|
---|
139 | //-----------------------------------------------------------------------------
|
---|
140 |
|
---|
141 | void usart_write_U32(U32 value,U08 digits)
|
---|
142 | {
|
---|
143 | usart_write_str(nc_U32_to_str(value,digits));
|
---|
144 | }
|
---|
145 | //-----------------------------------------------------------------------------
|
---|
146 |
|
---|
147 | void usart_write_S32(S32 value,U08 digits)
|
---|
148 | {
|
---|
149 | usart_write_str(nc_S32_to_str(value,digits));
|
---|
150 | }
|
---|
151 | //-----------------------------------------------------------------------------
|
---|
152 |
|
---|
153 | void usart_write_U32_hex(U32 value)
|
---|
154 | {
|
---|
155 | usart_write_str(nc_U32_to_hex(value));
|
---|
156 | }
|
---|
157 | //-----------------------------------------------------------------------------
|
---|
158 |
|
---|
159 | void usart_write_float(float value,U08 decimals,U08 digits)
|
---|
160 | {
|
---|
161 | usart_write_str(nc_float_to_str(value,decimals,digits));
|
---|
162 | }
|
---|
163 | //-----------------------------------------------------------------------------
|
---|
164 |
|
---|
165 | #ifdef USART_USE_RX_IRQ
|
---|
166 |
|
---|
167 | ISR(SIG_USART_RECV)
|
---|
168 | {
|
---|
169 | if (ISR_toggle_out) PORTB ^= (1<<PB3); // toggle Out2_spare when starting ISR
|
---|
170 |
|
---|
171 | usart_receive_char = UDR;
|
---|
172 |
|
---|
173 | if (usart_rx_ready) // Exit if ready flag is still set
|
---|
174 | {
|
---|
175 | return;
|
---|
176 | }
|
---|
177 |
|
---|
178 | // If CR received, then set ready flag
|
---|
179 | if (usart_receive_char == USART_CHAR_CR)
|
---|
180 | {
|
---|
181 | usart_rx_buffer[usart_rx_buffer_index] = 0; // Terminate input string
|
---|
182 | usart_received_chars = usart_rx_buffer_index;
|
---|
183 | usart_rx_buffer_index = 0;
|
---|
184 | usart_receive_suspended = false;
|
---|
185 | usart_rx_ready = TRUE;
|
---|
186 | return;
|
---|
187 | }
|
---|
188 |
|
---|
189 | // Ignore all characters till next CR
|
---|
190 | if (usart_receive_suspended)
|
---|
191 | {
|
---|
192 | return;
|
---|
193 | }
|
---|
194 |
|
---|
195 | // Check for underscore or comment
|
---|
196 | if (usart_receive_char == '_' || usart_receive_char == ';')
|
---|
197 | {
|
---|
198 | usart_receive_suspended = true;
|
---|
199 |
|
---|
200 | return;
|
---|
201 | }
|
---|
202 |
|
---|
203 | // If Backspace received, then delete last character
|
---|
204 | if (usart_receive_char == USART_CHAR_BS && usart_rx_buffer_index)
|
---|
205 | {
|
---|
206 | usart_rx_buffer_index--;
|
---|
207 |
|
---|
208 | return;
|
---|
209 | }
|
---|
210 |
|
---|
211 | // Ignore invalid characters
|
---|
212 | if (usart_receive_char < USART_CHAR_SPC)
|
---|
213 | {
|
---|
214 | return;
|
---|
215 | }
|
---|
216 |
|
---|
217 | #ifdef USART_USE_UPPERCASE
|
---|
218 | if (usart_receive_char >= 'a' && usart_receive_char <= 'z')
|
---|
219 | {
|
---|
220 | usart_receive_char -= 32;
|
---|
221 | }
|
---|
222 | #endif
|
---|
223 |
|
---|
224 | if (usart_rx_buffer_index < USART_RX_BUFFER_SIZE - 1) // Store character
|
---|
225 | {
|
---|
226 | usart_rx_buffer[usart_rx_buffer_index++] = usart_receive_char;
|
---|
227 | usart_writeln_str(usart_rx_buffer);
|
---|
228 | }
|
---|
229 | #endif
|
---|
230 | //usart_write_char(usart_rx_buffer[usart_rx_buffer_index]);
|
---|
231 | //usart_writeln_str(usart_rx_buffer);
|
---|
232 | //usart_write_char(usart_rx_buffer[1,2]);
|
---|
233 | //usart_write_char(usart_rx_buffer[usart_rx_buffer_index]);
|
---|
234 | //usart_writeln_flash_str(*usart_rx_buffer);
|
---|
235 | //usart_write_char(usart_rx_buffer);
|
---|
236 | }
|
---|
237 |
|
---|
238 | /*
|
---|
239 | #define uart_maxstrlen 64
|
---|
240 |
|
---|
241 | volatile U8 uart_str_complete=0;
|
---|
242 | volatile U8 uart_str_count=0;
|
---|
243 | volatile U8 uart_string[uart_maxstrlen+1]="";
|
---|
244 |
|
---|
245 | ISR(USART_RXC_vect)
|
---|
246 | {
|
---|
247 | unsigned char buffer = 64;
|
---|
248 | // Daten aus dem Puffer lesen
|
---|
249 | buffer = UDR;
|
---|
250 | UDR = buffer;
|
---|
251 | if ( uart_str_complete==0 ){ // wenn uart_string gerade in Verwendung, neues Zeichen verwerfen
|
---|
252 | // Daten werden erst in string geschrieben, wenn nicht String-Ende/max Zeichenlänge erreicht ist/string gerade verarbeitet wird
|
---|
253 | if (buffer!='\n' && buffer!='\r' && uart_str_count<uart_maxstrlen-1){
|
---|
254 | uart_string[uart_str_count]=buffer;
|
---|
255 | uart_str_count++;
|
---|
256 | } else {
|
---|
257 | uart_string[uart_str_count]='\0';
|
---|
258 | uart_str_count=0;
|
---|
259 | uart_str_complete=1;
|
---|
260 | }
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | */
|
---|