source: firmware/FSC/usb_src/usart.c@ 18066

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