Statistiques
| Branche: | Révision:

root / Version 0.9 / RS232_MUX.X / hardware_uart_OLD.h @ 10dcb6e9

Historique | Voir | Annoter | Télécharger (22,52 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 RS232_HARDWAREUART_H
31
#define        RS232_HARDWAREUART_H
32

    
33
#include <xc.h> // include processor files - each processor file is guarded. 
34
#include <stdbool.h>
35
#include <avr/interrupt.h>
36
#include <avr/eeprom.h>
37

    
38
///////////////////////////////////////////////////////////////
39
//Definitions
40

    
41
//Values registers
42

    
43
#define F5BIT_MODE           0x00
44
#define F6BIT_MODE           0x01
45
#define F7BIT_MODE           0x02
46
#define F8BIT_MODE           0x03
47
#define F9LBIT_MODE          0x06
48
#define F9HBIT_MODE          0x07
49

    
50
#define NO_PARITY           0x00
51
#define EVEN_PARITY         0x02
52
#define ODD_PARITY          0x03
53

    
54
#define ONE_STOPBIT         0x00
55
#define TWO_STOPBITS        0x01
56

    
57

    
58
//EEPROM addresses
59

    
60
#define ADDR_BAUD_2400           0x0
61
#define ADDR_BAUD_4800           0x1
62
#define ADDR_BAUD_9600           0x2
63
#define ADDR_BAUD_19200          0x3
64
#define ADDR_BAUD_38400          0x4
65
#define ADDR_BAUD_57600          0x5
66
#define ADDR_BAUD_115200         0x6
67

    
68
#define ADDR_PARITY_NONE         0x0
69
#define ADDR_PARITY_EVEN         0x1
70
#define ADDR_PARITY_ODD          0x2
71

    
72
#define ADDR_DATA_F5             0x0
73
#define ADDR_DATA_F6             0x1
74
#define ADDR_DATA_F7             0x2
75
#define ADDR_DATA_F8             0x3
76
#define ADDR_DATA_F9L            0x4
77
#define ADDR_DATA_F9H            0x5
78

    
79
#define ADDR_STOP_ONE            0x0
80
#define ADDR_STOP_TWO            0x1
81

    
82

    
83
#define DEFAULT_ADDR_BAUD        ADDR_BAUD_9600
84
#define DEFAULT_ADDR_PARITY      ADDR_PARITY_NONE
85
#define DEFAULT_ADDR_DATA        ADDR_DATA_F8
86
#define DEFAULT_ADDR_STOP        ADDR_STOP_ONE
87
#define DEFAULT_VALUE_DEBUG_LED  0x1F
88

    
89
#define EEPROM_DEBUG_LED_ADDR   0x8
90

    
91

    
92

    
93
//According to AVR32DA48 pins
94

    
95
#define USART0_PORT_PINS    0x01
96
#define USART0_DEBUG_LED    0x0C
97

    
98
#define USART1_PORT_PINS    0x01
99
#define USART1_DEBUG_LED    0x0C
100

    
101
#define USART2_PORT_PINS    0x01
102
#define USART2_DEBUG_LED    0x0C
103

    
104
#define USART3_PORT_PINS    0x01
105
#define USART3_DEBUG_LED    0x0C
106

    
107
#define USART4_PORT_PINS    0x01
108
#define USART4_DEBUG_LED    0x0C
109

    
110
//Port index for debug leds
111
#define USART0_INDEX    0
112
#define USART1_INDEX    1
113
#define USART2_INDEX    2
114
#define USART3_INDEX    3
115
#define USART4_INDEX    4
116

    
117

    
118
#ifdef        __cplusplus
119
extern "C" {
120
#endif /* __cplusplus */
121
    
122
    
123
    
124
///////////////////////////////////////////////////////////////
125
//Global Vars
126

    
127
typedef struct RXREAD RXREAD;
128
struct RXREAD
129
{
130
    uint8_t _rxReicv; // temp byte
131
    uint8_t _rxBuf[256]; //back end serial buffer
132
    uint16_t bytesAvailable; //show if bytes available...
133
};
134

    
135

    
136
typedef struct eepromPort eepromPort;
137
struct eepromPort
138
{
139
    uint8_t regA; // register A for speed and parity configuration
140
    uint8_t regB; // register B for Data structure and stop bits
141
};
142

    
143
typedef struct cfgPort cfgPort;
144
struct cfgPort
145
{
146
    uint16_t baud; // Port speed
147
    uint8_t parity; // Parity for frame structure
148
    uint8_t dataByte; // Data byte structure
149
    uint8_t stopBit; // Stop bits for frame structure
150
    uint8_t debugLed;
151
};
152

    
153
//EEPROM data structure (for reading registers)
154
eepromPort eepromVCOM[4], eepromDebugLeds;
155

    
156

    
157
//RX structure for reading buffers from serial ports
158
volatile RXREAD _RX0, _RX1, _RX2, _RX3, _RX4;
159
volatile bool _hasWritten[] = {false, false, false, false, false};
160
volatile bool _hasRead[] = {false, false, false, false, false};
161

    
162

    
163
///////////////////////////////////////////////////////////////
164
//Functions
165

    
166

    
167
/**
168
 <p><b>void getEEPROMCfg(void)</b></p>
169
 <p><b>Read eeprom values stored for all serial ports</b></p>
170
 */
171
void getEEPROMCfg(void)
172
{
173
    uint8_t i = 0, j = 0;
174
    while(j < 8)
175
    {
176
        //Read at first bytes
177
        eepromVCOM[i].regA = eeprom_read_byte(j);
178
        eepromVCOM[i].regB = eeprom_read_byte(j+1);
179
        //Then check if writing default values is necessary
180
        if(eepromVCOM[i].regA == 0xFF)
181
        {
182
            eepromVCOM[i].regA = (DEFAULT_ADDR_BAUD << 4) | (DEFAULT_ADDR_PARITY);
183
            eeprom_write_byte(j, (DEFAULT_ADDR_BAUD << 4) | (DEFAULT_ADDR_PARITY));
184
        }
185
        if(eepromVCOM[i].regB == 0xFF)
186
        {
187
            eepromVCOM[i].regB = (DEFAULT_ADDR_DATA << 4) | (DEFAULT_ADDR_STOP);
188
            eeprom_write_byte(j+1, (DEFAULT_ADDR_DATA << 4) | (DEFAULT_ADDR_STOP));
189
        }
190
        i++;
191
        j+=2;
192
    }
193
    
194
    //And finally, check debug leds value
195
    eepromDebugLeds.regA = eeprom_read_byte(EEPROM_DEBUG_LED_ADDR);
196
    if(eepromDebugLeds.regA == 0xFF)
197
    {
198
        eepromDebugLeds.regA = DEFAULT_VALUE_DEBUG_LED;
199
        eeprom_write_byte(EEPROM_DEBUG_LED_ADDR, DEFAULT_VALUE_DEBUG_LED);
200
    }
201
        
202
    
203
}
204

    
205
/**
206
 <p><b>uint8_t getDebugLedsConfiguration(void)</b></p>
207
 <p><b>Get leds configuration for debug (TX/RX) of each serial port</b></p>
208
 <p><b>             Return : uint8_t -> state of each leds [VCOM4 VCOM3 VCOM2 VCOM1 MCOM]</b></p>
209
 */
210
uint8_t getDebugLedsConfiguration(void)
211
{
212
    return eepromDebugLeds.regA;
213
}
214

    
215

    
216

    
217

    
218
/**
219
 <p><b>void readConfiguration(cfgPort *cfg, uint8_t n)</b></p>
220
 <p><b>Get configuration with eeprom read values</b></p>
221
 <p><b>             cfgPort -> Serial port configuration</b></p>
222
 <p><b>             n -> Serial port to configure</b></p>
223
 */
224
void readConfiguration(cfgPort *cfg, uint8_t n)
225
{
226
    //BAUD value register for these frequencies in this order :
227
    //2400, 4800, 9600, 19200, 38400, 57600, 115200
228
    uint16_t speedCfg[] = { 40000, 20000, 10000, 5000, 2500, 1667, 833 };
229
    uint8_t parityCfg[] = { NO_PARITY, EVEN_PARITY, ODD_PARITY };
230
    uint8_t dataCfg[] = { F5BIT_MODE, F6BIT_MODE, F7BIT_MODE, F8BIT_MODE, F9LBIT_MODE, F9HBIT_MODE };
231
    uint8_t stopCfg[] = { ONE_STOPBIT, TWO_STOPBITS };
232
    
233
    cfg->baud = speedCfg[(eepromVCOM[n].regA >> 4)];
234
    cfg->parity = parityCfg[(eepromVCOM[n].regA & 0xF)];
235
    cfg->dataByte = dataCfg[(eepromVCOM[n].regB >> 4)];
236
    cfg->stopBit = stopCfg[(eepromVCOM[n].regB & 0xF)];
237
}
238

    
239

    
240

    
241
/**
242
 <p><b>void debugLedsTest(void)</b></p>
243
 <p><b>Debug leds test sequence</b></p>
244
 */
245

    
246

    
247
void debugLedsTest(void)
248
{
249
    PORT_t *port;
250
    uint16_t addrPorts[] = {&PORTA, &PORTB, &PORTC, &PORTE, &PORTF };
251
    for(int i = 0; i < 5; i++)
252
    {
253
        port = addrPorts[i];
254
        port->OUT = 0x04;
255
        _delay_ms(125);
256
        port->OUT = 0x0C;
257
        _delay_ms(125);
258
        port->OUT = 0x00;
259
    }
260
}
261

    
262
/**
263
 <p><b>void senseDebugLeds(void)</b></p>
264
 <p><b>Call this to update leds status</b></p>
265
 */
266
void senseDebugLeds(void)
267
{
268
    ///////////////////////////
269
    //Check Master Port
270
    if(_hasWritten[USART0_INDEX])
271
    {
272
        PORTA.OUT |= 0x04;
273
        _hasWritten[USART0_INDEX] = false;
274
    }
275
    else
276
        PORTA.OUT &= 0xFB;
277
    
278
    if(_hasRead[USART0_INDEX])
279
    {
280
        PORTA.OUT |= 0x08;
281
        _hasRead[USART0_INDEX] = false;
282
    }
283
    else
284
        PORTA.OUT &= 0xF7;
285
    
286
    ///////////////////////////
287
    //Check VCOM1
288
    if(_hasWritten[USART3_INDEX])
289
    {
290
        PORTC.OUT |= 0x04;
291
        _hasWritten[USART3_INDEX] = false;
292
    }
293
    else
294
        PORTC.OUT &= 0xFB;
295
    
296
    if(_hasRead[USART3_INDEX])
297
    {
298
        PORTC.OUT |= 0x08;
299
        _hasRead[USART3_INDEX] = false;
300
    }
301
    else
302
        PORTC.OUT &= 0xF7;
303
    
304
    ///////////////////////////
305
    //Check VCOM2
306
    if(_hasWritten[USART1_INDEX])
307
    {
308
        PORTF.OUT |= 0x04;
309
        _hasWritten[USART1_INDEX] = false;
310
    }
311
    else
312
        PORTF.OUT &= 0xFB;
313
    
314
    if(_hasRead[USART1_INDEX])
315
    {
316
        PORTF.OUT |= 0x08;
317
        _hasRead[USART1_INDEX] = false;
318
    }
319
    else
320
        PORTF.OUT &= 0xF7;
321
    
322
    ///////////////////////////
323
    //Check VCOM3
324
    if(_hasWritten[USART4_INDEX])
325
    {
326
        PORTB.OUT |= 0x04;
327
        _hasWritten[USART4_INDEX] = false;
328
    }
329
    else
330
    {
331
        PORTB.OUT &= 0xFB;
332
    }
333
        
334
    
335
    if(_hasRead[USART4_INDEX])
336
    {
337
        PORTB.OUT |= 0x08;
338
        _hasRead[USART4_INDEX] = false;
339
    }
340
    else
341
        PORTB.OUT &= 0xF7;
342
    
343
    
344
    ///////////////////////////
345
    //Check VCOM4
346
    if(_hasWritten[USART2_INDEX])
347
    {
348
        PORTE.OUT |= 0x04;
349
        _hasWritten[USART2_INDEX] = false;
350
    }
351
    else
352
        PORTE.OUT &= 0xFB;
353
    
354
    if(_hasRead[USART2_INDEX])
355
    {
356
        PORTE.OUT |= 0x08;
357
        _hasRead[USART2_INDEX] = false;
358
    }
359
    else
360
        PORTE.OUT &= 0xF7;
361
}
362

    
363

    
364
/////////////////////////////////////////////////
365
//For Master port (USART0)
366

    
367
/**
368
 <p><b>void initPort0(uint32_t bauds)</b></p>
369
 <p><b>Init MASTER PORT</b></p>
370
 <p><b>Parameters : uint8_t bauds -> Set bauds speed</b></p>
371
 <p><b>             bool enableDebug -> Enable debug leds</b></p>
372
 <p><b>             uint8_t chSize -> Data bits</b></p>
373
 <p><b>             uint8_t stopBit -> How many stop bits</b></p>
374
 <p><b>             uint8_t parity -> Parity type (ODD, EVEN, DISABLED)</b></p>
375
 <p><b>Don't forget to call sei() function after called all initPortx() functions !</p></b>
376
 */
377
void initPort0(uint32_t bauds, bool enableDebug, uint8_t chSize, bool stopBit, uint8_t parity)
378
{
379
    if(enableDebug)
380
        PORTA.DIR = USART0_PORT_PINS | USART0_DEBUG_LED; //Set TX/RX IO and RX/TX debug leds for USART0
381
    else
382
        PORTA.DIR = USART0_PORT_PINS; //Set TX/RX IO
383
    
384
    USART0.BAUD = bauds; 
385
    USART0.CTRLA = 0x00; //disable RX complete interrupt
386
    USART0.CTRLB = 0xD0; //Enable TX and RX sending
387
    USART0.CTRLC = (parity << 4) | (stopBit << 3) | chSize;
388
}
389

    
390

    
391
/**
392
 <p><b>void txWrite0(uint8_t b)</b></p>
393
 <p><b>Write a byte throught MASTER PORT</b></p>
394
 <p><b>Parameters : uint8_t b -> Byte to write</b></p>
395
 */
396

    
397
void txWrite0(uint8_t b) //write one byte
398
{
399
    while((USART0.STATUS & 0x20) != 0x20);
400
    USART0.TXDATAL = b;
401
    _hasWritten[USART0_INDEX] = true;
402
    while((USART0.STATUS & 0x40) != 0x40); //check when we transmit all the bits
403
    USART0.STATUS = 0x40; //clear the flag for next transmission
404
}
405

    
406

    
407
/**
408
 <p><b>uint8_t rxRead0(void)</b></p>
409
 <p><b>Read a byte from MASTER PORT</b></p>
410
 <p><b>Return : uint8_t result -> Byte to read</b></p>
411
 */
412
uint8_t rxRead0(void) //Read one byte from buffer
413
{
414
    uint8_t result = _RX0._rxBuf[0]; //get byte in order (FIFO)
415
    
416
    for(uint8_t j = 0; j < 255; j++) //shift bytes
417
    {
418
        _RX0._rxBuf[j] = _RX0._rxBuf[j+1];
419
    }
420
    if(_RX0.bytesAvailable > 0)
421
        _RX0.bytesAvailable--; //remove 1 byte
422
    
423
    return result;
424
}
425

    
426

    
427
/**
428
 <p><b>uint16_t portAvailable0(void)</b></p>
429
 <p><b>Get number of bytes available into the MASTER PORT's buffer</b></p>
430
 <p><b>Return : uint16_t bytesAvailable -> Number of bytes</b></p>
431
 */
432
uint16_t portAvailable0(void)
433
{
434
    return _RX0.bytesAvailable;
435
}
436

    
437

    
438
ISR(USART0_RXC_vect)
439
{
440
    //_RX0._rxReicv = USART0.RXDATAL; //read buffer
441
    _RX0._rxBuf[_RX0.bytesAvailable] = USART0.RXDATAL; //Add one byte to the buffer
442
    //_RX0._rxReicv = 0x00; //clear value
443
    if(_RX0.bytesAvailable < 255) //ensure not to overflow buffer size...
444
        _RX0.bytesAvailable++; //a new byte is available into the buffer
445
        //PORTA.OUT ^= 0x3; //debug led
446
    _hasRead[USART0_INDEX] = true;
447
}
448

    
449

    
450

    
451

    
452

    
453
/////////////////////////////////////////////////
454
//For Virtual port 1 (USART3)
455

    
456

    
457
/**
458
 <p><b>void initPort1(uint32_t bauds)</b></p>
459
 <p><b>Init MASTER PORT</b></p>
460
 <p><b>Parameters : uint8_t bauds -> Set bauds speed</b></p>
461
 <p><b>             bool enableDebug -> Enable debug leds</b></p>
462
 <p><b>             uint8_t chSize -> Data bits</b></p>
463
 <p><b>             uint8_t stopBit -> How many stop bits</b></p>
464
 <p><b>             uint8_t parity -> Parity type (ODD, EVEN, DISABLED)</b></p>
465
 <p><b>Don't forget to call sei() function after called all initPortx() functions !</p></b>
466
 */
467
void initPort1(uint32_t bauds, bool enableDebug, uint8_t chSize, bool stopBit, uint8_t parity)
468
{
469
    
470
    if(enableDebug)
471
        PORTB.DIR = USART3_PORT_PINS | USART3_DEBUG_LED; //Set TX/RX IO and RX/TX debug leds for USART0
472
    else
473
        PORTB.DIR = USART3_PORT_PINS; //Set TX/RX IO
474
    
475
    USART3.BAUD = bauds; 
476
    USART3.CTRLA = 0x80; //enable RX complete interrupt
477
    USART3.CTRLB = 0xD0; //Enable TX and RX sending
478
    USART3.CTRLC = (parity << 4) | (stopBit << 3) | chSize; //set serial port cfg
479
}
480

    
481

    
482

    
483
/**
484
 <p><b>void txWrite1(uint8_t b)</b></p>
485
 <p><b>Write a byte throught VCOM1</b></p>
486
 <p><b>Parameters : uint8_t b -> Byte to write</b></p>
487
 */
488
void txWrite1(uint8_t b) //Write one byte
489
{
490
    USART3.TXDATAL = b;
491
    _hasWritten[USART3_INDEX] = true;
492
    while((USART3.STATUS & 0x40) != 0x40); //check when we transmit all the bits
493
    USART3.STATUS = 0x40; //clear the flag for next transmission
494
}
495

    
496

    
497
/**
498
 <p><b>uint8_t rxRead1(void)</b></p>
499
 <p><b>Read a byte from VCOM1</b></p>
500
 <p><b>Return : uint8_t result -> Byte to read</b></p>
501
 */
502
uint8_t rxRead1(void) //Read one byte from buffer
503
{
504
    uint8_t result = _RX1._rxBuf[0]; //get byte in order (FIFO)
505
    
506
    for(uint8_t j = 0; j < 255; j++) //shift bytes
507
    {
508
        _RX1._rxBuf[j] = _RX1._rxBuf[j+1];
509
    }
510
    if(_RX1.bytesAvailable > 0)
511
        _RX1.bytesAvailable--; //remove 1 byte
512
    
513
    return result;
514
}
515

    
516

    
517
/**
518
 <p><b>uint16_t portAvailable1(void)</b></p>
519
 <p><b>Get number of bytes available into the VCOM1 PORT's buffer</b></p>
520
 <p><b>Return : uint16_t bytesAvailable -> Number of bytes</b></p>
521
 */
522
uint16_t portAvailable1(void)
523
{
524
    return _RX1.bytesAvailable;
525
}
526

    
527

    
528

    
529
ISR(USART3_RXC_vect)
530
{
531
    //while((USART0.RXDATAH & 0x80) == 0x80);
532
    _RX1._rxReicv = USART3.RXDATAL; //read buffer
533
    _RX1._rxBuf[_RX1.bytesAvailable] = _RX1._rxReicv; //Add one byte to the buffer
534
    _RX1._rxReicv = 0x00; //clear value
535
    if(_RX1.bytesAvailable < 255) //ensure not to overflow buffer size...
536
        _RX1.bytesAvailable++; //a new byte is available into the buffer
537
        //PORTA.OUT ^= 0x3; //debug led
538
    
539
    _hasRead[USART3_INDEX] = true;
540
}
541

    
542

    
543
/////////////////////////////////////////////////
544
//For Virtual port 2 (USART1)
545

    
546

    
547
/**
548
 <p><b>void initPort2(uint32_t bauds)</b></p>
549
 <p><b>Init MASTER PORT</b></p>
550
 <p><b>Parameters : uint8_t bauds -> Set bauds speed</b></p>
551
 <p><b>             bool enableDebug -> Enable debug leds</b></p>
552
 <p><b>             uint8_t chSize -> Data bits</b></p>
553
 <p><b>             uint8_t stopBit -> How many stop bits</b></p>
554
 <p><b>             uint8_t parity -> Parity type (ODD, EVEN, DISABLED)</b></p>
555
 <p><b>Don't forget to call sei() function after called all initPortx() functions !</p></b>
556
 */
557
void initPort2(uint32_t bauds, bool enableDebug, uint8_t chSize, bool stopBit, uint8_t parity)
558
{
559
    
560
    if(enableDebug)
561
        PORTC.DIR = USART1_PORT_PINS | USART1_DEBUG_LED; //Set TX/RX IO and RX/TX debug leds for USART0
562
    else
563
        PORTC.DIR = USART1_PORT_PINS; //Set TX/RX IO
564
    
565
    
566
    USART1.BAUD = bauds; 
567
    USART1.CTRLA = 0x80; //enable RX complete interrupt
568
    USART1.CTRLB = 0xD0; //Enable TX and RX sending
569
    USART1.CTRLC = (parity << 4) | (stopBit << 3) | chSize; //set serial port cfg
570
}
571

    
572

    
573

    
574
/**
575
 <p><b>void txWrite2(uint8_t b)</b></p>
576
 <p><b>Write a byte throught VCOM2</b></p>
577
 <p><b>Parameters : uint8_t b -> Byte to write</b></p>
578
 */
579
void txWrite2(uint8_t b) //Virtual Port 2
580
{
581
    USART1.TXDATAL = b;
582
    _hasWritten[USART1_INDEX] = true;
583
    while((USART1.STATUS & 0x40) != 0x40); //check when we transmit all the bits
584
    USART1.STATUS = 0x40; //clear the flag for next transmission
585
}
586

    
587

    
588
/**
589
 <p><b>uint8_t rxRead2(void)</b></p>
590
 <p><b>Read a byte from VCOM2</b></p>
591
 <p><b>Return : uint8_t result -> Byte to read</b></p>
592
 */
593
uint8_t rxRead2(void) //Read one byte from buffer
594
{
595
    uint8_t result = _RX2._rxBuf[0]; //get byte in order (FIFO)
596
    
597
    for(uint8_t j = 0; j < 255; j++) //shift bytes
598
    {
599
        _RX2._rxBuf[j] = _RX2._rxBuf[j+1];
600
    }
601
    if(_RX2.bytesAvailable > 0)
602
        _RX2.bytesAvailable--; //remove 1 byte
603
    
604
    return result;
605
}
606

    
607

    
608
/**
609
 <p><b>uint16_t portAvailable2(void)</b></p>
610
 <p><b>Get number of bytes available into the VCOM2 PORT's buffer</b></p>
611
 <p><b>Return : uint16_t bytesAvailable -> Number of bytes</b></p>
612
 */
613
uint16_t portAvailable2(void)
614
{
615
    return _RX2.bytesAvailable;
616
}
617

    
618

    
619

    
620

    
621
ISR(USART1_RXC_vect)
622
{
623
    //while((USART0.RXDATAH & 0x80) == 0x80);
624
    _RX2._rxReicv = USART1.RXDATAL; //read buffer
625
    _RX2._rxBuf[_RX2.bytesAvailable] = _RX2._rxReicv; //Add one byte to the buffer
626
    _RX2._rxReicv = 0x00; //clear value
627
    if(_RX2.bytesAvailable < 255) //ensure not to overflow buffer size...
628
        _RX2.bytesAvailable++; //a new byte is available into the buffer
629
        //PORTA.OUT ^= 0x3; //debug led
630
    _hasRead[USART1_INDEX] = true;
631
}
632

    
633
/////////////////////////////////////////////////
634
//For Virtual port 3 (USART4)
635

    
636

    
637
/**
638
 <p><b>void initPort3(uint32_t bauds)</b></p>
639
 <p><b>Init MASTER PORT</b></p>
640
 <p><b>Parameters : uint8_t bauds -> Set bauds speed</b></p>
641
 <p><b>             bool enableDebug -> Enable debug leds</b></p>
642
 <p><b>             uint8_t chSize -> Data bits</b></p>
643
 <p><b>             uint8_t stopBit -> How many stop bits</b></p>
644
 <p><b>             uint8_t parity -> Parity type (ODD, EVEN, DISABLED)</b></p>
645
 <p><b>Don't forget to call sei() function after called all initPortx() functions !</p></b>
646
 */
647
void initPort3(uint32_t bauds, bool enableDebug, uint8_t chSize, bool stopBit, uint8_t parity)
648
{
649
    
650
    if(enableDebug)
651
        PORTE.DIR = USART4_PORT_PINS | USART4_DEBUG_LED; //Set TX/RX IO and RX/TX debug leds for USART0
652
    else
653
        PORTE.DIR = USART4_PORT_PINS; //Set TX/RX IO
654
    
655
    
656
    
657
    USART4.BAUD = bauds; 
658
    USART4.CTRLA = 0x80; //enable RX complete interrupt
659
    USART4.CTRLB = 0xD0; //Enable TX and RX sending
660
    USART4.CTRLC = (parity << 4) | (stopBit << 3) | chSize; //set serial port cfg
661
}
662

    
663

    
664

    
665
/**
666
 <p><b>void txWrite3(uint8_t b)</b></p>
667
 <p><b>Write a byte throught VCOM3</b></p>
668
 <p><b>Parameters : uint8_t b -> Byte to write</b></p>
669
 */
670
void txWrite3(uint8_t b) //Write one byte
671
{
672
    USART4.TXDATAL = b;
673
    _hasWritten[USART4_INDEX] = true;
674
    while((USART4.STATUS & 0x40) != 0x40); //check when we transmit all the bits
675
    USART4.STATUS = 0x40; //clear the flag for next transmission
676
}
677

    
678
/**
679
 <p><b>uint8_t rxRead3(void)</b></p>
680
 <p><b>Read a byte from VCOM3</b></p>
681
 <p><b>Return : uint8_t result -> Byte to read</b></p>
682
 */
683
uint8_t rxRead3(void) //Read one byte from buffer
684
{
685
    uint8_t result = _RX3._rxBuf[0]; //get byte in order (FIFO)
686
    
687
    for(uint8_t j = 0; j < 255; j++) //shift bytes
688
    {
689
        _RX3._rxBuf[j] = _RX3._rxBuf[j+1];
690
    }
691
    if(_RX3.bytesAvailable > 0)
692
        _RX3.bytesAvailable--; //remove 1 byte
693
    
694
    return result;
695
}
696

    
697

    
698
/**
699
 <p><b>uint16_t portAvailable3(void)</b></p>
700
 <p><b>Get number of bytes available into the VCOM3 PORT's buffer</b></p>
701
 <p><b>Return : uint16_t bytesAvailable -> Number of bytes</b></p>
702
 */
703
uint16_t portAvailable3(void)
704
{
705
    return _RX3.bytesAvailable;
706
}
707

    
708

    
709

    
710
ISR(USART4_RXC_vect)
711
{
712
    //while((USART0.RXDATAH & 0x80) == 0x80);
713
    _RX3._rxReicv = USART4.RXDATAL; //read buffer
714
    _RX3._rxBuf[_RX3.bytesAvailable] = _RX3._rxReicv; //Add one byte to the buffer
715
    _RX3._rxReicv = 0x00; //clear value
716
    if(_RX3.bytesAvailable < 255) //ensure not to overflow buffer size...
717
        _RX3.bytesAvailable++; //a new byte is available into the buffer
718
        //PORTA.OUT ^= 0x3; //debug led
719
    _hasRead[USART4_INDEX] = true;
720
}
721

    
722

    
723

    
724

    
725

    
726

    
727
/////////////////////////////////////////////////
728
//For Virtual port 4 (USART2)
729

    
730

    
731
/**
732
 <p><b>void initPort4(uint32_t bauds)</b></p>
733
 <p><b>Init MASTER PORT</b></p>
734
 <p><b>Parameters : uint8_t bauds -> Set bauds speed</b></p>
735
 <p><b>             bool enableDebug -> Enable debug leds</b></p>
736
 <p><b>             uint8_t chSize -> Data bits</b></p>
737
 <p><b>             uint8_t stopBit -> How many stop bits</b></p>
738
 <p><b>             uint8_t parity -> Parity type (ODD, EVEN, DISABLED)</b></p>
739
 <p><b>Don't forget to call sei() function after called all initPortx() functions !</p></b>
740
 */
741
void initPort4(uint32_t bauds, bool enableDebug, uint8_t chSize, bool stopBit, uint8_t parity)
742
{
743
    
744
    if(enableDebug)
745
        PORTF.DIR = USART2_PORT_PINS | USART2_DEBUG_LED; //Set TX/RX IO and RX/TX debug leds for USART0
746
    else
747
        PORTF.DIR = USART2_PORT_PINS; //Set TX/RX IO
748
    
749
    
750
    USART2.BAUD = bauds; 
751
    USART2.CTRLA = 0x80; //enable RX complete interrupt
752
    USART2.CTRLB = 0xD0; //Enable TX and RX sending
753
    USART2.CTRLC = (parity << 4) | (stopBit << 3) | chSize; //set serial port cfg
754
}
755

    
756

    
757

    
758
/**
759
 <p><b>void txWrite4(uint8_t b)</b></p>
760
 <p><b>Write a byte throught VCOM4</b></p>
761
 <p><b>Parameters : uint8_t b -> Byte to write</b></p>
762
 */
763
void txWrite4(uint8_t b) //Virtual Port 4
764
{
765
    USART2.TXDATAL = b;
766
    _hasWritten[USART2_INDEX] = true;
767
    while((USART2.STATUS & 0x40) != 0x40); //check when we transmit all the bits
768
    USART2.STATUS = 0x40; //clear the flag for next transmission
769
}
770

    
771
/**
772
 <p><b>uint8_t rxRead4(void)</b></p>
773
 <p><b>Read a byte from VCOM4</b></p>
774
 <p><b>Return : uint8_t result -> Byte to read</b></p>
775
 */
776
uint8_t rxRead4(void) //Read one byte from buffer
777
{
778
    uint8_t result = _RX4._rxBuf[0]; //get byte in order (FIFO)
779
    
780
    for(uint8_t j = 0; j < 255; j++) //shift bytes
781
    {
782
        _RX4._rxBuf[j] = _RX4._rxBuf[j+1];
783
    }
784
    if(_RX4.bytesAvailable > 0)
785
        _RX4.bytesAvailable--; //remove 1 byte
786
    
787
    return result;
788
}
789

    
790

    
791
/**
792
 <p><b>uint16_t portAvailable4(void)</b></p>
793
 <p><b>Get number of bytes available into the VCOM4 PORT's buffer</b></p>
794
 <p><b>Return : uint16_t bytesAvailable -> Number of bytes</b></p>
795
 */
796
uint16_t portAvailable4(void)
797
{
798
    return _RX4.bytesAvailable;
799
}
800

    
801

    
802

    
803
ISR(USART2_RXC_vect)
804
{
805
    //while((USART0.RXDATAH & 0x80) == 0x80);
806
    _RX4._rxReicv = USART2.RXDATAL; //read buffer
807
    _RX4._rxBuf[_RX4.bytesAvailable] = _RX4._rxReicv; //Add one byte to the buffer
808
    _RX4._rxReicv = 0x00; //clear value
809
    if(_RX4.bytesAvailable < 255) //ensure not to overflow buffer size...
810
        _RX4.bytesAvailable++; //a new byte is available into the buffer
811
        //PORTA.OUT ^= 0x3; //debug led
812
    _hasRead[USART2_INDEX] = true;
813
}
814

    
815
#ifdef        __cplusplus
816
}
817
#endif /* __cplusplus */
818

    
819
#endif        /* XC_HEADER_TEMPLATE_H */
820