root / Version 0.6 / RS232_MUX.X / hardware_uart.h @ c9e951f9
Historique | Voir | Annoter | Télécharger (13,11 ko)
1 |
/* Microchip Technology Inc. and its subsidiaries. You may use this software
|
---|---|
2 |
* and any derivatives exclusively with Microchip products.
|
3 |
*
|
4 |
* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
|
5 |
* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
|
6 |
* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
|
7 |
* PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION
|
8 |
* WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.
|
9 |
*
|
10 |
* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
|
11 |
* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
|
12 |
* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
|
13 |
* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
|
14 |
* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS
|
15 |
* IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF
|
16 |
* ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
|
17 |
*
|
18 |
* MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE
|
19 |
* TERMS.
|
20 |
*/
|
21 |
|
22 |
/*
|
23 |
* File: hardware_uart.h
|
24 |
* Author: Enzo Niro
|
25 |
* Comments: Library made for RS232 MUX board
|
26 |
* Revision history: V1.0
|
27 |
*/
|
28 |
|
29 |
|
30 |
#ifndef XC_HEADER_TEMPLATE_H
|
31 |
#define XC_HEADER_TEMPLATE_H
|
32 |
|
33 |
#include <xc.h> // include processor files - each processor file is guarded. |
34 |
#include <stdbool.h> |
35 |
|
36 |
|
37 |
|
38 |
//According to AVR32DA48 pins
|
39 |
|
40 |
#define USART0_PORT_PINS 0x01 |
41 |
#define USART0_DEBUG_LED 0x0C |
42 |
|
43 |
#define USART1_PORT_PINS 0x01 |
44 |
#define USART1_DEBUG_LED 0x0C |
45 |
|
46 |
#define USART2_PORT_PINS 0x01 |
47 |
#define USART2_DEBUG_LED 0x0C |
48 |
|
49 |
#define USART3_PORT_PINS 0x01 |
50 |
#define USART3_DEBUG_LED 0x0C |
51 |
|
52 |
#define USART4_PORT_PINS 0x01 |
53 |
#define USART4_DEBUG_LED 0x0C |
54 |
|
55 |
|
56 |
|
57 |
#ifdef __cplusplus
|
58 |
extern "C" { |
59 |
#endif /* __cplusplus */ |
60 |
|
61 |
|
62 |
|
63 |
///////////////////////////////////////////////////////////////
|
64 |
//Global Vars
|
65 |
|
66 |
typedef struct RXREAD RXREAD; |
67 |
struct RXREAD
|
68 |
{ |
69 |
uint8_t _rxReicv; // temp byte
|
70 |
uint8_t _rxBuf[256]; //back end serial buffer |
71 |
uint16_t bytesAvailable; //show if bytes available...
|
72 |
}; |
73 |
|
74 |
|
75 |
RXREAD RX0, RX1, RX2, RX3, RX4; |
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
/**
|
82 |
<p><b>void debugLedsTest(void)</b></p>
|
83 |
<p><b>Debug leds test sequence</b></p>
|
84 |
*/
|
85 |
|
86 |
|
87 |
void debugLedsTest(void) |
88 |
{ |
89 |
PORT_t *port; |
90 |
uint16_t addrPorts[] = {&PORTA, &PORTB, &PORTC, &PORTE, &PORTF }; |
91 |
for(int i = 0; i < 5; i++) |
92 |
{ |
93 |
port = addrPorts[i]; |
94 |
port->OUT = 0x04;
|
95 |
_delay_ms(250);
|
96 |
port->OUT = 0x0C;
|
97 |
_delay_ms(250);
|
98 |
port->OUT = 0x00;
|
99 |
} |
100 |
} |
101 |
|
102 |
|
103 |
|
104 |
/////////////////////////////////////////////////
|
105 |
//For Master port (USART0)
|
106 |
|
107 |
/**
|
108 |
<p><b>void initPort0(uint32_t bauds)</b></p>
|
109 |
<p><b>Init MASTER PORT</b></p>
|
110 |
<p><b>Parameters : uint8_t bauds -> Set bauds speed</b></p>
|
111 |
<p><b> bool enableDebug -> Enable debug leds</b></p>
|
112 |
<p><b>Don't forget to call sei() function after called all initPortx() functions !</p></b>
|
113 |
*/
|
114 |
void initPort0(uint32_t bauds, bool enableDebug) |
115 |
{ |
116 |
if(enableDebug)
|
117 |
PORTA.DIR = USART0_PORT_PINS | USART0_DEBUG_LED; //Set TX/RX IO and RX/TX debug leds for USART0
|
118 |
else
|
119 |
PORTA.DIR = USART0_PORT_PINS; //Set TX/RX IO
|
120 |
|
121 |
USART0.BAUD = bauds; |
122 |
USART0.CTRLA = 0x80; //enable RX complete interrupt |
123 |
USART0.CTRLB = 0xD0; //Enable TX and RX sending |
124 |
} |
125 |
|
126 |
|
127 |
/**
|
128 |
<p><b>void txWrite0(uint8_t b)</b></p>
|
129 |
<p><b>Write a byte throught MASTER PORT</b></p>
|
130 |
<p><b>Parameters : uint8_t b -> Byte to write</b></p>
|
131 |
*/
|
132 |
|
133 |
void txWrite0(uint8_t b) //write one byte |
134 |
{ |
135 |
USART0.TXDATAL = b; |
136 |
} |
137 |
|
138 |
|
139 |
/**
|
140 |
<p><b>uint8_t rxRead0(void)</b></p>
|
141 |
<p><b>Read a byte from MASTER PORT</b></p>
|
142 |
<p><b>Return : uint8_t result -> Byte to read</b></p>
|
143 |
*/
|
144 |
uint8_t rxRead0(void) //Read one byte from buffer |
145 |
{ |
146 |
uint8_t result = RX0._rxBuf[0]; //get byte in order (FIFO) |
147 |
|
148 |
for(uint8_t j = 0; j < 255; j++) //shift bytes |
149 |
{ |
150 |
RX0._rxBuf[j] = RX0._rxBuf[j+1];
|
151 |
} |
152 |
if(RX0.bytesAvailable > 0) |
153 |
RX0.bytesAvailable--; //remove 1 byte
|
154 |
|
155 |
return result;
|
156 |
} |
157 |
|
158 |
|
159 |
/**
|
160 |
<p><b>uint16_t portAvailable0(void)</b></p>
|
161 |
<p><b>Get number of bytes available into the MASTER PORT's buffer</b></p>
|
162 |
<p><b>Return : uint16_t bytesAvailable -> Number of bytes</b></p>
|
163 |
*/
|
164 |
uint16_t portAvailable0(void)
|
165 |
{ |
166 |
return RX0.bytesAvailable;
|
167 |
} |
168 |
|
169 |
|
170 |
ISR(USART0_RXC_vect) |
171 |
{ |
172 |
//while((USART0.RXDATAH & 0x80) == 0x80);
|
173 |
RX0._rxReicv = USART0.RXDATAL; //read buffer
|
174 |
RX0._rxBuf[RX0.bytesAvailable] = RX0._rxReicv; //Add one byte to the buffer
|
175 |
RX0._rxReicv = 0x00; //clear value |
176 |
if(RX0.bytesAvailable < 255) //ensure not to overflow buffer size... |
177 |
RX0.bytesAvailable++; //a new byte is available into the buffer
|
178 |
//PORTA.OUT ^= 0x3; //debug led
|
179 |
} |
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
/////////////////////////////////////////////////
|
186 |
//For Virtual port 1 (USART3)
|
187 |
|
188 |
|
189 |
/**
|
190 |
<p><b>void initPort1(uint32_t bauds)</b></p>
|
191 |
<p><b>Init MASTER PORT</b></p>
|
192 |
<p><b>Parameters : uint8_t bauds -> Set bauds speed</b></p>
|
193 |
<p><b> bool enableDebug -> Enable debug leds</b></p>
|
194 |
<p><b>Don't forget to call sei() function after called all initPortx() functions !</p></b>
|
195 |
*/
|
196 |
void initPort1(uint32_t bauds, bool enableDebug) |
197 |
{ |
198 |
|
199 |
if(enableDebug)
|
200 |
PORTB.DIR = USART3_PORT_PINS | USART3_DEBUG_LED; //Set TX/RX IO and RX/TX debug leds for USART0
|
201 |
else
|
202 |
PORTB.DIR = USART3_PORT_PINS; //Set TX/RX IO
|
203 |
|
204 |
USART3.BAUD = bauds; |
205 |
USART3.CTRLA = 0x80; //enable RX complete interrupt |
206 |
USART3.CTRLB = 0xD0; //Enable TX and RX sending |
207 |
} |
208 |
|
209 |
|
210 |
|
211 |
/**
|
212 |
<p><b>void txWrite1(uint8_t b)</b></p>
|
213 |
<p><b>Write a byte throught VCOM1</b></p>
|
214 |
<p><b>Parameters : uint8_t b -> Byte to write</b></p>
|
215 |
*/
|
216 |
void txWrite1(uint8_t b) //Write one byte |
217 |
{ |
218 |
USART3.TXDATAL = b; |
219 |
} |
220 |
|
221 |
|
222 |
/**
|
223 |
<p><b>uint8_t rxRead1(void)</b></p>
|
224 |
<p><b>Read a byte from VCOM1</b></p>
|
225 |
<p><b>Return : uint8_t result -> Byte to read</b></p>
|
226 |
*/
|
227 |
uint8_t rxRead1(void) //Read one byte from buffer |
228 |
{ |
229 |
uint8_t result = RX1._rxBuf[0]; //get byte in order (FIFO) |
230 |
|
231 |
for(uint8_t j = 0; j < 255; j++) //shift bytes |
232 |
{ |
233 |
RX1._rxBuf[j] = RX1._rxBuf[j+1];
|
234 |
} |
235 |
if(RX1.bytesAvailable > 0) |
236 |
RX1.bytesAvailable--; //remove 1 byte
|
237 |
|
238 |
return result;
|
239 |
} |
240 |
|
241 |
|
242 |
/**
|
243 |
<p><b>uint16_t portAvailable1(void)</b></p>
|
244 |
<p><b>Get number of bytes available into the VCOM1 PORT's buffer</b></p>
|
245 |
<p><b>Return : uint16_t bytesAvailable -> Number of bytes</b></p>
|
246 |
*/
|
247 |
uint16_t portAvailable1(void)
|
248 |
{ |
249 |
return RX1.bytesAvailable;
|
250 |
} |
251 |
|
252 |
|
253 |
|
254 |
ISR(USART3_RXC_vect) |
255 |
{ |
256 |
//while((USART0.RXDATAH & 0x80) == 0x80);
|
257 |
RX1._rxReicv = USART3.RXDATAL; //read buffer
|
258 |
RX1._rxBuf[RX1.bytesAvailable] = RX1._rxReicv; //Add one byte to the buffer
|
259 |
RX1._rxReicv = 0x00; //clear value |
260 |
if(RX1.bytesAvailable < 255) //ensure not to overflow buffer size... |
261 |
RX1.bytesAvailable++; //a new byte is available into the buffer
|
262 |
//PORTA.OUT ^= 0x3; //debug led
|
263 |
} |
264 |
|
265 |
|
266 |
/////////////////////////////////////////////////
|
267 |
//For Virtual port 2 (USART1)
|
268 |
|
269 |
|
270 |
/**
|
271 |
<p><b>void initPort2(uint32_t bauds)</b></p>
|
272 |
<p><b>Init MASTER PORT</b></p>
|
273 |
<p><b>Parameters : uint8_t bauds -> Set bauds speed</b></p>
|
274 |
<p><b> bool enableDebug -> Enable debug leds</b></p>
|
275 |
<p><b>Don't forget to call sei() function after called all initPortx() functions !</p></b>
|
276 |
*/
|
277 |
void initPort2(uint32_t bauds, bool enableDebug) |
278 |
{ |
279 |
|
280 |
if(enableDebug)
|
281 |
PORTC.DIR = USART1_PORT_PINS | USART1_DEBUG_LED; //Set TX/RX IO and RX/TX debug leds for USART0
|
282 |
else
|
283 |
PORTC.DIR = USART1_PORT_PINS; //Set TX/RX IO
|
284 |
|
285 |
|
286 |
USART1.BAUD = bauds; |
287 |
USART1.CTRLA = 0x80; //enable RX complete interrupt |
288 |
USART1.CTRLB = 0xD0; //Enable TX and RX sending |
289 |
} |
290 |
|
291 |
|
292 |
|
293 |
/**
|
294 |
<p><b>void txWrite2(uint8_t b)</b></p>
|
295 |
<p><b>Write a byte throught VCOM2</b></p>
|
296 |
<p><b>Parameters : uint8_t b -> Byte to write</b></p>
|
297 |
*/
|
298 |
void txWrite2(uint8_t b) //Virtual Port 2 |
299 |
{ |
300 |
USART1.TXDATAL = b; |
301 |
} |
302 |
|
303 |
|
304 |
/**
|
305 |
<p><b>uint8_t rxRead2(void)</b></p>
|
306 |
<p><b>Read a byte from VCOM2</b></p>
|
307 |
<p><b>Return : uint8_t result -> Byte to read</b></p>
|
308 |
*/
|
309 |
uint8_t rxRead2(void) //Read one byte from buffer |
310 |
{ |
311 |
uint8_t result = RX2._rxBuf[0]; //get byte in order (FIFO) |
312 |
|
313 |
for(uint8_t j = 0; j < 255; j++) //shift bytes |
314 |
{ |
315 |
RX2._rxBuf[j] = RX2._rxBuf[j+1];
|
316 |
} |
317 |
if(RX2.bytesAvailable > 0) |
318 |
RX2.bytesAvailable--; //remove 1 byte
|
319 |
|
320 |
return result;
|
321 |
} |
322 |
|
323 |
|
324 |
/**
|
325 |
<p><b>uint16_t portAvailable2(void)</b></p>
|
326 |
<p><b>Get number of bytes available into the VCOM2 PORT's buffer</b></p>
|
327 |
<p><b>Return : uint16_t bytesAvailable -> Number of bytes</b></p>
|
328 |
*/
|
329 |
uint16_t portAvailable2(void)
|
330 |
{ |
331 |
return RX2.bytesAvailable;
|
332 |
} |
333 |
|
334 |
|
335 |
|
336 |
|
337 |
ISR(USART1_RXC_vect) |
338 |
{ |
339 |
//while((USART0.RXDATAH & 0x80) == 0x80);
|
340 |
RX2._rxReicv = USART1.RXDATAL; //read buffer
|
341 |
RX2._rxBuf[RX2.bytesAvailable] = RX2._rxReicv; //Add one byte to the buffer
|
342 |
RX2._rxReicv = 0x00; //clear value |
343 |
if(RX2.bytesAvailable < 255) //ensure not to overflow buffer size... |
344 |
RX2.bytesAvailable++; //a new byte is available into the buffer
|
345 |
//PORTA.OUT ^= 0x3; //debug led
|
346 |
} |
347 |
|
348 |
/////////////////////////////////////////////////
|
349 |
//For Virtual port 3 (USART4)
|
350 |
|
351 |
|
352 |
/**
|
353 |
<p><b>void initPort3(uint32_t bauds)</b></p>
|
354 |
<p><b>Init MASTER PORT</b></p>
|
355 |
<p><b>Parameters : uint8_t bauds -> Set bauds speed</b></p>
|
356 |
<p><b> bool enableDebug -> Enable debug leds</b></p>
|
357 |
<p><b>Don't forget to call sei() function after called all initPortx() functions !</p></b>
|
358 |
*/
|
359 |
void initPort3(uint32_t bauds, bool enableDebug) |
360 |
{ |
361 |
|
362 |
if(enableDebug)
|
363 |
PORTE.DIR = USART4_PORT_PINS | USART4_DEBUG_LED; //Set TX/RX IO and RX/TX debug leds for USART0
|
364 |
else
|
365 |
PORTE.DIR = USART4_PORT_PINS; //Set TX/RX IO
|
366 |
|
367 |
|
368 |
|
369 |
USART4.BAUD = bauds; |
370 |
USART4.CTRLA = 0x80; //enable RX complete interrupt |
371 |
USART4.CTRLB = 0xD0; //Enable TX and RX sending |
372 |
} |
373 |
|
374 |
|
375 |
|
376 |
/**
|
377 |
<p><b>void txWrite3(uint8_t b)</b></p>
|
378 |
<p><b>Write a byte throught VCOM3</b></p>
|
379 |
<p><b>Parameters : uint8_t b -> Byte to write</b></p>
|
380 |
*/
|
381 |
void txWrite3(uint8_t b) //Write one byte |
382 |
{ |
383 |
USART4.TXDATAL = b; |
384 |
} |
385 |
|
386 |
/**
|
387 |
<p><b>uint8_t rxRead3(void)</b></p>
|
388 |
<p><b>Read a byte from VCOM3</b></p>
|
389 |
<p><b>Return : uint8_t result -> Byte to read</b></p>
|
390 |
*/
|
391 |
uint8_t rxRead3(void) //Read one byte from buffer |
392 |
{ |
393 |
uint8_t result = RX3._rxBuf[0]; //get byte in order (FIFO) |
394 |
|
395 |
for(uint8_t j = 0; j < 255; j++) //shift bytes |
396 |
{ |
397 |
RX3._rxBuf[j] = RX3._rxBuf[j+1];
|
398 |
} |
399 |
if(RX3.bytesAvailable > 0) |
400 |
RX3.bytesAvailable--; //remove 1 byte
|
401 |
|
402 |
return result;
|
403 |
} |
404 |
|
405 |
|
406 |
/**
|
407 |
<p><b>uint16_t portAvailable3(void)</b></p>
|
408 |
<p><b>Get number of bytes available into the VCOM3 PORT's buffer</b></p>
|
409 |
<p><b>Return : uint16_t bytesAvailable -> Number of bytes</b></p>
|
410 |
*/
|
411 |
uint16_t portAvailable3(void)
|
412 |
{ |
413 |
return RX3.bytesAvailable;
|
414 |
} |
415 |
|
416 |
|
417 |
|
418 |
ISR(USART4_RXC_vect) |
419 |
{ |
420 |
//while((USART0.RXDATAH & 0x80) == 0x80);
|
421 |
RX3._rxReicv = USART4.RXDATAL; //read buffer
|
422 |
RX3._rxBuf[RX3.bytesAvailable] = RX3._rxReicv; //Add one byte to the buffer
|
423 |
RX3._rxReicv = 0x00; //clear value |
424 |
if(RX3.bytesAvailable < 255) //ensure not to overflow buffer size... |
425 |
RX3.bytesAvailable++; //a new byte is available into the buffer
|
426 |
//PORTA.OUT ^= 0x3; //debug led
|
427 |
} |
428 |
|
429 |
/////////////////////////////////////////////////
|
430 |
//For Virtual port 4 (USART2)
|
431 |
|
432 |
|
433 |
/**
|
434 |
<p><b>void initPort4(uint32_t bauds)</b></p>
|
435 |
<p><b>Init MASTER PORT</b></p>
|
436 |
<p><b>Parameters : uint8_t bauds -> Set bauds speed</b></p>
|
437 |
<p><b> bool enableDebug -> Enable debug leds</b></p>
|
438 |
<p><b>Don't forget to call sei() function after called all initPortx() functions !</p></b>
|
439 |
*/
|
440 |
void initPort4(uint32_t bauds, bool enableDebug) |
441 |
{ |
442 |
|
443 |
if(enableDebug)
|
444 |
PORTF.DIR = USART2_PORT_PINS | USART2_DEBUG_LED; //Set TX/RX IO and RX/TX debug leds for USART0
|
445 |
else
|
446 |
PORTF.DIR = USART2_PORT_PINS; //Set TX/RX IO
|
447 |
|
448 |
|
449 |
USART2.BAUD = bauds; |
450 |
USART2.CTRLA = 0x80; //enable RX complete interrupt |
451 |
USART2.CTRLB = 0xD0; //Enable TX and RX sending |
452 |
} |
453 |
|
454 |
|
455 |
|
456 |
/**
|
457 |
<p><b>void txWrite4(uint8_t b)</b></p>
|
458 |
<p><b>Write a byte throught VCOM4</b></p>
|
459 |
<p><b>Parameters : uint8_t b -> Byte to write</b></p>
|
460 |
*/
|
461 |
void txWrite4(uint8_t b) //Virtual Port 4 |
462 |
{ |
463 |
USART2.TXDATAL = b; |
464 |
} |
465 |
|
466 |
/**
|
467 |
<p><b>uint8_t rxRead4(void)</b></p>
|
468 |
<p><b>Read a byte from VCOM4</b></p>
|
469 |
<p><b>Return : uint8_t result -> Byte to read</b></p>
|
470 |
*/
|
471 |
uint8_t rxRead4(void) //Read one byte from buffer |
472 |
{ |
473 |
uint8_t result = RX4._rxBuf[0]; //get byte in order (FIFO) |
474 |
|
475 |
for(uint8_t j = 0; j < 255; j++) //shift bytes |
476 |
{ |
477 |
RX4._rxBuf[j] = RX4._rxBuf[j+1];
|
478 |
} |
479 |
if(RX4.bytesAvailable > 0) |
480 |
RX4.bytesAvailable--; //remove 1 byte
|
481 |
|
482 |
return result;
|
483 |
} |
484 |
|
485 |
|
486 |
/**
|
487 |
<p><b>uint16_t portAvailable4(void)</b></p>
|
488 |
<p><b>Get number of bytes available into the VCOM4 PORT's buffer</b></p>
|
489 |
<p><b>Return : uint16_t bytesAvailable -> Number of bytes</b></p>
|
490 |
*/
|
491 |
uint16_t portAvailable4(void)
|
492 |
{ |
493 |
return RX4.bytesAvailable;
|
494 |
} |
495 |
|
496 |
|
497 |
|
498 |
ISR(USART2_RXC_vect) |
499 |
{ |
500 |
//while((USART0.RXDATAH & 0x80) == 0x80);
|
501 |
RX4._rxReicv = USART2.RXDATAL; //read buffer
|
502 |
RX4._rxBuf[RX4.bytesAvailable] = RX4._rxReicv; //Add one byte to the buffer
|
503 |
RX4._rxReicv = 0x00; //clear value |
504 |
if(RX4.bytesAvailable < 255) //ensure not to overflow buffer size... |
505 |
RX4.bytesAvailable++; //a new byte is available into the buffer
|
506 |
//PORTA.OUT ^= 0x3; //debug led
|
507 |
} |
508 |
|
509 |
#ifdef __cplusplus
|
510 |
} |
511 |
#endif /* __cplusplus */ |
512 |
|
513 |
#endif /* XC_HEADER_TEMPLATE_H */ |
514 |
|