Statistiques
| Branche: | Révision:

root / Version 1.9 / RS232_MUX.X / hardware_uart.h @ 350cdd5d

Historique | Voir | Annoter | Télécharger (16,363 ko)

1 350cdd5d Enzo Niro
/* 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.5 
27
 */
28
29
30
/*
31
 TODO list:
32
 * Put TXDATAH in write when F9BIT mode enabled ?
33
 
34
 
35
 
36
 */
37
38
39
40
#ifndef RS232_HARDWAREUART_H
41
#define        RS232_HARDWAREUART_H
42
43
#include <xc.h> // include processor files - each processor file is guarded. 
44
#include <stdbool.h>
45
#include <avr/interrupt.h>
46
#include <avr/eeprom.h>
47
48
///////////////////////////////////////////////////////////////
49
//Definitions
50
51
52
53
54
55
//According to AVR32DA48 pins
56
57
#define USART0_PORT_PINS    0x01
58
#define USART0_DEBUG_LED    0x0C
59
60
#define USART1_PORT_PINS    0x01
61
#define USART1_DEBUG_LED    0x0C
62
63
#define USART2_PORT_PINS    0x01
64
#define USART2_DEBUG_LED    0x0C
65
66
#define USART3_PORT_PINS    0x01
67
#define USART3_DEBUG_LED    0x0C
68
69
#define USART4_PORT_PINS    0x01
70
#define USART4_DEBUG_LED    0x0C
71
72
//Port index for debug leds
73
#define USART0_INDEX    0
74
#define USART1_INDEX    1
75
#define USART2_INDEX    2
76
#define USART3_INDEX    3
77
#define USART4_INDEX    4
78
79
80
#ifdef        __cplusplus
81
extern "C" {
82
#endif /* __cplusplus */
83
    
84
    
85
    
86
    
87
//////////////////////////////////////////////
88
//prototypes
89
90
    
91
92
93
void initPort0(uint32_t bauds, uint8_t chSize, bool stopBit, uint8_t parity);
94
void txWrite0(uint8_t b); //write one byte
95
uint8_t rxRead0(void); //Read one byte from buffer
96
uint8_t portAvailable0(void);
97
98
void initPort1(uint32_t bauds, uint8_t chSize, bool stopBit, uint8_t parity);
99
void txWrite1(uint8_t b); //write one byte
100
uint8_t rxRead1(void); //Read one byte from buffer
101
uint8_t portAvailable1(void);
102
103
void initPort2(uint32_t bauds, uint8_t chSize, bool stopBit, uint8_t parity);
104
void txWrite2(uint8_t b); //write one byte
105
uint8_t rxRead2(void); //Read one byte from buffer
106
uint8_t portAvailable2(void);
107
108
void initPort3(uint32_t bauds, uint8_t chSize, bool stopBit, uint8_t parity);
109
void txWrite3(uint8_t b); //write one byte
110
uint8_t rxRead3(void); //Read one byte from buffer
111
uint8_t portAvailable3(void);
112
113
void initPort4(uint32_t bauds, uint8_t chSize, bool stopBit, uint8_t parity);
114
void txWrite4(uint8_t b); //write one byte
115
uint8_t rxRead4(void); //Read one byte from buffer
116
uint8_t portAvailable4(void);
117
118
    
119
//////////////////////////////////////////////
120
    
121
    
122
    
123
///////////////////////////////////////////////////////////////
124
//Global Vars
125
126
typedef struct RXREAD RXREAD;
127
struct RXREAD
128
{
129
    uint8_t _rxReicv; // temp byte
130
    uint8_t _rxBuf[256]; //back end serial buffer
131
    uint8_t bytesAvailable; //show if bytes available...
132
    uint8_t _tempBytes;
133
};
134
135
136
137
138
139
//RX structure for reading buffers from serial ports
140
volatile RXREAD _RX0, _RX1, _RX2, _RX3, _RX4;
141
142
143
144
///////////////////////////////////////////////////////////////
145
//Functions
146
147
148
/////////////////////////////////////////////////
149
//For Master port (USART0)
150
151
/**
152
 <p><b>void initPort0(uint32_t bauds)</b></p>
153
 <p><b>Init MASTER PORT</b></p>
154
 <p><b>Parameters : uint8_t bauds -> Set bauds speed</b></p>
155
 <p><b>             bool enableDebug -> Enable debug leds</b></p>
156
 <p><b>             uint8_t chSize -> Data bits</b></p>
157
 <p><b>             uint8_t stopBit -> How many stop bits</b></p>
158
 <p><b>             uint8_t parity -> Parity type (ODD, EVEN, DISABLED)</b></p>
159
 <p><b>Don't forget to call sei() function after called all initPortx() functions !</p></b>
160
 */
161
void initPort0(uint32_t bauds, uint8_t chSize, bool stopBit, uint8_t parity)
162
{
163
    /*if(enableDebug)
164
        PORTA.DIR = USART0_PORT_PINS | USART0_DEBUG_LED; //Set TX/RX IO and RX/TX debug leds for USART0
165
    else
166
        PORTA.DIR = USART0_PORT_PINS; //Set TX/RX IO
167
    */
168
    USART0.BAUD = bauds; 
169
    USART0.CTRLA = 0x80; //enable RX complete interrupt
170
    USART0.CTRLB = 0xD0; //Enable TX and RX sending
171
    USART0.CTRLC = (parity << 4) | (stopBit << 3) | chSize;
172
}
173
174
175
/**
176
 <p><b>void txWrite0(uint8_t b)</b></p>
177
 <p><b>Write a byte throught MASTER PORT</b></p>
178
 <p><b>Parameters : uint8_t b -> Byte to write</b></p>
179
 */
180
181
void txWrite0(uint8_t b) //write one byte
182
{
183
    while((USART0.STATUS & 0x20) != 0x20);
184
    USART0.TXDATAL = b;
185
    while((USART0.STATUS & 0x40) != 0x40); //check when we transmit all the bits
186
    USART0.STATUS = 0x40; //clear the flag for next transmission
187
}
188
189
190
/**
191
 <p><b>uint8_t rxRead0(void)</b></p>
192
 <p><b>Read a byte from MASTER PORT</b></p>
193
 <p><b>Return : uint8_t result -> Byte to read</b></p>
194
 */
195
uint8_t rxRead0(void) //Read one byte from buffer
196
{    
197
    if(_RX0.bytesAvailable > 0)
198
        _RX0.bytesAvailable--;
199
    
200
    return _RX0._rxBuf[_RX0.bytesAvailable];
201
    //_hasRead[USART0_INDEX] = true;
202
    //return USART0.RXDATAL;
203
}
204
205
206
/**
207
 <p><b>uint16_t portAvailable0(void)</b></p>
208
 <p><b>Get number of bytes available into the MASTER PORT's buffer</b></p>
209
 <p><b>Return : uint16_t bytesAvailable -> Number of bytes</b></p>
210
 */
211
uint8_t portAvailable0(void)
212
{
213
    return _RX0.bytesAvailable;
214
    //return ((USART0.RXDATAH & 0x80) >> 7);
215
}
216
217
218
ISR(USART0_RXC_vect)
219
{
220
    //USART0.CTRLA = 0x80;
221
    //_RX0._rxReicv = USART0.RXDATAL; //read buffer
222
    _RX0._rxBuf[_RX0.bytesAvailable] = USART0.RXDATAL; //Add one byte to the buffer
223
    //_RX0._rxReicv = 0x00; //clear value
224
    if(_RX0.bytesAvailable < 255) //ensure not to overflow buffer size...
225
        _RX0.bytesAvailable++; //a new byte is available into the buffer
226
        //PORTA.OUT ^= 0x3; //debug led
227
}
228
229
/*ISR(USART0_DRE_vect)
230
{
231
    _RX0.bytesAvailable = _RX0._tempBytes;
232
    USART0.CTRLA = 0x80; //Clear DRE interrupt
233
    _hasRead[USART0_INDEX] = true;
234
}*/
235
236
237
238
239
/////////////////////////////////////////////////
240
//For Virtual port 1 (USART3)
241
242
243
/**
244
 <p><b>void initPort1(uint32_t bauds)</b></p>
245
 <p><b>Init MASTER PORT</b></p>
246
 <p><b>Parameters : uint8_t bauds -> Set bauds speed</b></p>
247
 <p><b>             bool enableDebug -> Enable debug leds</b></p>
248
 <p><b>             uint8_t chSize -> Data bits</b></p>
249
 <p><b>             uint8_t stopBit -> How many stop bits</b></p>
250
 <p><b>             uint8_t parity -> Parity type (ODD, EVEN, DISABLED)</b></p>
251
 <p><b>Don't forget to call sei() function after called all initPortx() functions !</p></b>
252
 */
253
void initPort1(uint32_t bauds, uint8_t chSize, bool stopBit, uint8_t parity)
254
{
255
    
256
    /*if(enableDebug)
257
        PORTB.DIR = USART3_PORT_PINS | USART3_DEBUG_LED; //Set TX/RX IO and RX/TX debug leds for USART0
258
    else
259
        PORTB.DIR = USART3_PORT_PINS; //Set TX/RX IO
260
    */
261
    PORTB.DIR |= USART3_PORT_PINS; //Set TX/RX IO
262
    USART3.BAUD = bauds; 
263
    USART3.CTRLA = 0x80; //enable RX complete interrupt
264
    USART3.CTRLB = 0xD0; //Enable TX and RX sending
265
    USART3.CTRLC = (parity << 4) | (stopBit << 3) | chSize; //set serial port cfg
266
}
267
268
269
270
/**
271
 <p><b>void txWrite1(uint8_t b)</b></p>
272
 <p><b>Write a byte throught VCOM1</b></p>
273
 <p><b>Parameters : uint8_t b -> Byte to write</b></p>
274
 */
275
void txWrite1(uint8_t b) //Write one byte
276
{
277
    while((USART3.STATUS & 0x20) != 0x20);
278
    USART3.TXDATAL = b;
279
    while((USART3.STATUS & 0x40) != 0x40); //check when we transmit all the bits
280
    USART3.STATUS = 0x40; //clear the flag for next transmission
281
}
282
283
284
/**
285
 <p><b>uint8_t rxRead1(void)</b></p>
286
 <p><b>Read a byte from VCOM1</b></p>
287
 <p><b>Return : uint8_t result -> Byte to read</b></p>
288
 */
289
uint8_t rxRead1(void) //Read one byte from buffer
290
{
291
    
292
    if(_RX1.bytesAvailable > 0)
293
        _RX1.bytesAvailable--;
294
    
295
    return _RX1._rxBuf[_RX1.bytesAvailable];
296
    
297
    
298
    /*_hasRead[USART3_INDEX] = true;
299
    return USART3.RXDATAL;*/
300
}
301
302
303
/**
304
 <p><b>uint16_t portAvailable1(void)</b></p>
305
 <p><b>Get number of bytes available into the VCOM1 PORT's buffer</b></p>
306
 <p><b>Return : uint16_t bytesAvailable -> Number of bytes</b></p>
307
 */
308
uint8_t portAvailable1(void)
309
{
310
    return _RX1.bytesAvailable;
311
    //return ((USART3.STATUS & 0x80) >> 7);
312
}
313
314
315
316
ISR(USART3_RXC_vect)
317
{
318
    //while((USART0.RXDATAH & 0x80) == 0x80);
319
    _RX1._rxBuf[_RX1.bytesAvailable] = USART3.RXDATAL; //Add one byte to the buffer
320
    if(_RX1.bytesAvailable < 255) //ensure not to overflow buffer size...
321
        _RX1.bytesAvailable++; //a new byte is available into the buffer
322
        //PORTA.OUT ^= 0x3; //debug led
323
}
324
325
326
/////////////////////////////////////////////////
327
//For Virtual port 2 (USART1)
328
329
330
/**
331
 <p><b>void initPort2(uint32_t bauds)</b></p>
332
 <p><b>Init MASTER PORT</b></p>
333
 <p><b>Parameters : uint8_t bauds -> Set bauds speed</b></p>
334
 <p><b>             bool enableDebug -> Enable debug leds</b></p>
335
 <p><b>             uint8_t chSize -> Data bits</b></p>
336
 <p><b>             uint8_t stopBit -> How many stop bits</b></p>
337
 <p><b>             uint8_t parity -> Parity type (ODD, EVEN, DISABLED)</b></p>
338
 <p><b>Don't forget to call sei() function after called all initPortx() functions !</p></b>
339
 */
340
void initPort2(uint32_t bauds, uint8_t chSize, bool stopBit, uint8_t parity)
341
{
342
    
343
    /*if(enableDebug)
344
        PORTC.DIR = USART1_PORT_PINS | USART1_DEBUG_LED; //Set TX/RX IO and RX/TX debug leds for USART0
345
    else
346
        PORTC.DIR = USART1_PORT_PINS; //Set TX/RX IO
347
    */
348
    PORTC.DIR |= USART1_PORT_PINS; //Set TX/RX IO
349
    USART1.BAUD = bauds; 
350
    USART1.CTRLA = 0x80; //enable RX complete interrupt
351
    USART1.CTRLB = 0xD0; //Enable TX and RX sending
352
    USART1.CTRLC = (parity << 4) | (stopBit << 3) | chSize; //set serial port cfg
353
}
354
355
356
357
/**
358
 <p><b>void txWrite2(uint8_t b)</b></p>
359
 <p><b>Write a byte throught VCOM2</b></p>
360
 <p><b>Parameters : uint8_t b -> Byte to write</b></p>
361
 */
362
void txWrite2(uint8_t b) //Virtual Port 2
363
{
364
    while((USART1.STATUS & 0x20) != 0x20);
365
    USART1.TXDATAL = b;
366
    while((USART1.STATUS & 0x40) != 0x40); //check when we transmit all the bits
367
    USART1.STATUS = 0x40; //clear the flag for next transmission
368
}
369
370
371
/**
372
 <p><b>uint8_t rxRead2(void)</b></p>
373
 <p><b>Read a byte from VCOM2</b></p>
374
 <p><b>Return : uint8_t result -> Byte to read</b></p>
375
 */
376
uint8_t rxRead2(void) //Read one byte from buffer
377
{
378
    
379
    if(_RX2.bytesAvailable > 0)
380
        _RX2.bytesAvailable--;
381
    
382
    return _RX2._rxBuf[_RX2.bytesAvailable];
383
    
384
    
385
    /*_hasRead[USART1_INDEX] = true;
386
    return USART1.RXDATAL;*/
387
}
388
389
390
/**
391
 <p><b>uint16_t portAvailable2(void)</b></p>
392
 <p><b>Get number of bytes available into the VCOM2 PORT's buffer</b></p>
393
 <p><b>Return : uint16_t bytesAvailable -> Number of bytes</b></p>
394
 */
395
uint8_t portAvailable2(void)
396
{
397
    return _RX2.bytesAvailable;
398
}
399
400
401
402
403
ISR(USART1_RXC_vect)
404
{
405
    _RX2._rxBuf[_RX2.bytesAvailable] = USART1.RXDATAL; //Add one byte to the buffer
406
    if(_RX2.bytesAvailable < 255) //ensure not to overflow buffer size...
407
        _RX2.bytesAvailable++; //a new byte is available into the buffer
408
        //PORTA.OUT ^= 0x3; //debug led
409
}
410
411
/////////////////////////////////////////////////
412
//For Virtual port 3 (USART4)
413
414
415
/**
416
 <p><b>void initPort3(uint32_t bauds)</b></p>
417
 <p><b>Init MASTER PORT</b></p>
418
 <p><b>Parameters : uint8_t bauds -> Set bauds speed</b></p>
419
 <p><b>             bool enableDebug -> Enable debug leds</b></p>
420
 <p><b>             uint8_t chSize -> Data bits</b></p>
421
 <p><b>             uint8_t stopBit -> How many stop bits</b></p>
422
 <p><b>             uint8_t parity -> Parity type (ODD, EVEN, DISABLED)</b></p>
423
 <p><b>Don't forget to call sei() function after called all initPortx() functions !</p></b>
424
 */
425
void initPort3(uint32_t bauds, uint8_t chSize, bool stopBit, uint8_t parity)
426
{
427
    
428
    /*if(enableDebug)
429
        PORTE.DIR = USART4_PORT_PINS | USART4_DEBUG_LED; //Set TX/RX IO and RX/TX debug leds for USART0
430
    else
431
        PORTE.DIR = USART4_PORT_PINS; //Set TX/RX IO
432
    */
433
    PORTE.DIR |= USART4_PORT_PINS; //Set TX/RX IO
434
    USART4.BAUD = bauds; 
435
    USART4.CTRLA = 0x80; //enable RX complete interrupt
436
    USART4.CTRLB = 0xD0; //Enable TX and RX sending
437
    USART4.CTRLC = (parity << 4) | (stopBit << 3) | chSize; //set serial port cfg
438
}
439
440
441
442
/**
443
 <p><b>void txWrite3(uint8_t b)</b></p>
444
 <p><b>Write a byte throught VCOM3</b></p>
445
 <p><b>Parameters : uint8_t b -> Byte to write</b></p>
446
 */
447
void txWrite3(uint8_t b) //Write one byte
448
{
449
    while((USART4.STATUS & 0x20) != 0x20);
450
    USART4.TXDATAL = b;
451
    while((USART4.STATUS & 0x40) != 0x40); //check when we transmit all the bits
452
    USART4.STATUS = 0x40; //clear the flag for next transmission
453
}
454
455
/**
456
 <p><b>uint8_t rxRead3(void)</b></p>
457
 <p><b>Read a byte from VCOM3</b></p>
458
 <p><b>Return : uint8_t result -> Byte to read</b></p>
459
 */
460
uint8_t rxRead3(void) //Read one byte from buffer
461
{
462
    
463
    if(_RX3.bytesAvailable > 0)
464
        _RX3.bytesAvailable--;
465
    
466
    return _RX3._rxBuf[_RX3.bytesAvailable];
467
    
468
    
469
    /*_hasRead[USART4_INDEX] = true;
470
    return USART4.RXDATAL;*/
471
}
472
473
474
/**
475
 <p><b>uint16_t portAvailable3(void)</b></p>
476
 <p><b>Get number of bytes available into the VCOM3 PORT's buffer</b></p>
477
 <p><b>Return : uint16_t bytesAvailable -> Number of bytes</b></p>
478
 */
479
uint8_t portAvailable3(void)
480
{
481
    return _RX3.bytesAvailable;
482
}
483
484
485
486
ISR(USART4_RXC_vect)
487
{
488
    _RX3._rxBuf[_RX3.bytesAvailable] = USART4.RXDATAL; //Add one byte to the buffer
489
    if(_RX3.bytesAvailable < 255) //ensure not to overflow buffer size...
490
        _RX3.bytesAvailable++; //a new byte is available into the buffer
491
        //PORTA.OUT ^= 0x3; //debug led
492
}
493
494
495
496
497
498
499
/////////////////////////////////////////////////
500
//For Virtual port 4 (USART2)
501
502
503
/**
504
 <p><b>void initPort4(uint32_t bauds)</b></p>
505
 <p><b>Init MASTER PORT</b></p>
506
 <p><b>Parameters : uint8_t bauds -> Set bauds speed</b></p>
507
 <p><b>             bool enableDebug -> Enable debug leds</b></p>
508
 <p><b>             uint8_t chSize -> Data bits</b></p>
509
 <p><b>             uint8_t stopBit -> How many stop bits</b></p>
510
 <p><b>             uint8_t parity -> Parity type (ODD, EVEN, DISABLED)</b></p>
511
 <p><b>Don't forget to call sei() function after called all initPortx() functions !</p></b>
512
 */
513
void initPort4(uint32_t bauds, uint8_t chSize, bool stopBit, uint8_t parity)
514
{
515
    
516
    /*if(enableDebug)
517
        PORTF.DIR = USART2_PORT_PINS | USART2_DEBUG_LED; //Set TX/RX IO and RX/TX debug leds for USART0
518
    else
519
        PORTF.DIR = USART2_PORT_PINS; //Set TX/RX IO
520
    */
521
    PORTF.DIR |= USART2_PORT_PINS; //Set TX/RX IO
522
    USART2.BAUD = bauds; 
523
    USART2.CTRLA = 0x80; //enable RX complete interrupt
524
    USART2.CTRLB = 0xD0; //Enable TX and RX sending
525
    USART2.CTRLC = (parity << 4) | (stopBit << 3) | chSize; //set serial port cfg
526
}
527
528
529
530
/**
531
 <p><b>void txWrite4(uint8_t b)</b></p>
532
 <p><b>Write a byte throught VCOM4</b></p>
533
 <p><b>Parameters : uint8_t b -> Byte to write</b></p>
534
 */
535
void txWrite4(uint8_t b) //Virtual Port 4
536
{
537
    while((USART2.STATUS & 0x20) != 0x20);
538
    USART2.TXDATAL = b;
539
    while((USART2.STATUS & 0x40) != 0x40); //check when we transmit all the bits
540
    USART2.STATUS = 0x40; //clear the flag for next transmission
541
}
542
543
/**
544
 <p><b>uint8_t rxRead4(void)</b></p>
545
 <p><b>Read a byte from VCOM4</b></p>
546
 <p><b>Return : uint8_t result -> Byte to read</b></p>
547
 */
548
uint8_t rxRead4(void) //Read one byte from buffer
549
{
550
    
551
    if(_RX4.bytesAvailable > 0)
552
        _RX4.bytesAvailable--;
553
    
554
    return _RX4._rxBuf[_RX4.bytesAvailable];
555
    
556
    /*_hasRead[USART2_INDEX] = true;
557
    return USART2.RXDATAL;*/
558
}
559
560
561
/**
562
 <p><b>uint16_t portAvailable4(void)</b></p>
563
 <p><b>Get number of bytes available into the VCOM4 PORT's buffer</b></p>
564
 <p><b>Return : uint16_t bytesAvailable -> Number of bytes</b></p>
565
 */
566
uint8_t portAvailable4(void)
567
{
568
    return _RX4.bytesAvailable;
569
}
570
571
572
573
ISR(USART2_RXC_vect)
574
{
575
    _RX4._rxBuf[_RX4.bytesAvailable] = USART2.RXDATAL; //Add one byte to the buffer
576
    if(_RX4.bytesAvailable < 255) //ensure not to overflow buffer size...
577
        _RX4.bytesAvailable++; //a new byte is available into the buffer
578
        //PORTA.OUT ^= 0x3; //debug led
579
}
580
581
#ifdef        __cplusplus
582
}
583
#endif /* __cplusplus */
584
585
#endif        /* XC_HEADER_TEMPLATE_H */