source: firmware/FSC/src/usart.c@ 13872

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