Statistiques
| Branche: | Révision:

root / Version 1.6 / RS232_MUX.X / hardware_uart_OLD.h @ master

Historique | Voir | Annoter | Télécharger (21,776 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
/*
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
//Values registers
52

    
53
#define F5BIT_MODE           0x00
54
#define F6BIT_MODE           0x01
55
#define F7BIT_MODE           0x02
56
#define F8BIT_MODE           0x03
57
#define F9LBIT_MODE          0x06
58
#define F9HBIT_MODE          0x07
59

    
60
#define NO_PARITY           0x00
61
#define EVEN_PARITY         0x02
62
#define ODD_PARITY          0x03
63

    
64
#define ONE_STOPBIT         0x00
65
#define TWO_STOPBITS        0x01
66

    
67

    
68
//EEPROM addresses
69

    
70
#define ADDR_BAUD_2400           0x0
71
#define ADDR_BAUD_4800           0x1
72
#define ADDR_BAUD_9600           0x2
73
#define ADDR_BAUD_19200          0x3
74
#define ADDR_BAUD_38400          0x4
75
#define ADDR_BAUD_57600          0x5
76
#define ADDR_BAUD_115200         0x6
77

    
78
#define ADDR_PARITY_NONE         0x0
79
#define ADDR_PARITY_EVEN         0x1
80
#define ADDR_PARITY_ODD          0x2
81

    
82
#define ADDR_DATA_F5             0x0
83
#define ADDR_DATA_F6             0x1
84
#define ADDR_DATA_F7             0x2
85
#define ADDR_DATA_F8             0x3
86
#define ADDR_DATA_F9L            0x4
87
#define ADDR_DATA_F9H            0x5
88

    
89
#define ADDR_STOP_ONE            0x0
90
#define ADDR_STOP_TWO            0x1
91

    
92

    
93
#define DEFAULT_ADDR_BAUD        ADDR_BAUD_9600
94
#define DEFAULT_ADDR_PARITY      ADDR_PARITY_NONE
95
#define DEFAULT_ADDR_DATA        ADDR_DATA_F8
96
#define DEFAULT_ADDR_STOP        ADDR_STOP_ONE
97
#define DEFAULT_VALUE_DEBUG_LED  0x1F
98

    
99
#define EEPROM_DEBUG_LED_ADDR   0x8
100

    
101

    
102

    
103
//According to AVR32DA48 pins
104

    
105
#define USART0_PORT_PINS    0x01
106
#define USART0_DEBUG_LED    0x0C
107

    
108
#define USART1_PORT_PINS    0x01
109
#define USART1_DEBUG_LED    0x0C
110

    
111
#define USART2_PORT_PINS    0x01
112
#define USART2_DEBUG_LED    0x0C
113

    
114
#define USART3_PORT_PINS    0x01
115
#define USART3_DEBUG_LED    0x0C
116

    
117
#define USART4_PORT_PINS    0x01
118
#define USART4_DEBUG_LED    0x0C
119

    
120
//Port index for debug leds
121
#define USART0_INDEX    0
122
#define USART1_INDEX    1
123
#define USART2_INDEX    2
124
#define USART3_INDEX    3
125
#define USART4_INDEX    4
126

    
127

    
128
#ifdef        __cplusplus
129
extern "C" {
130
#endif /* __cplusplus */
131
    
132
    
133
    
134
///////////////////////////////////////////////////////////////
135
//Global Vars
136

    
137
typedef struct RXREAD RXREAD;
138
struct RXREAD
139
{
140
    uint8_t _rxReicv; // temp byte
141
    uint8_t _rxBuf[256]; //back end serial buffer
142
    uint16_t bytesAvailable; //show if bytes available...
143
};
144

    
145

    
146
typedef struct eepromPort eepromPort;
147
struct eepromPort
148
{
149
    uint8_t regA; // register A for speed and parity configuration
150
    uint8_t regB; // register B for Data structure and stop bits
151
};
152

    
153
typedef struct cfgPort cfgPort;
154
struct cfgPort
155
{
156
    uint16_t baud; // Port speed
157
    uint8_t parity; // Parity for frame structure
158
    uint8_t dataByte; // Data byte structure
159
    uint8_t stopBit; // Stop bits for frame structure
160
    uint8_t debugLed;
161
};
162

    
163
//EEPROM data structure (for reading registers)
164
eepromPort eepromVCOM[4], eepromDebugLeds;
165

    
166

    
167
//RX structure for reading buffers from serial ports
168
volatile RXREAD _RX0, _RX1, _RX2, _RX3, _RX4;
169
volatile bool _hasWritten[] = {false, false, false, false, false};
170
volatile bool _hasRead[] = {false, false, false, false, false};
171

    
172

    
173
///////////////////////////////////////////////////////////////
174
//Functions
175

    
176

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

    
215
/**
216
 <p><b>uint8_t getDebugLedsConfiguration(void)</b></p>
217
 <p><b>Get leds configuration for debug (TX/RX) of each serial port</b></p>
218
 <p><b>             Return : uint8_t -> state of each leds [VCOM4 VCOM3 VCOM2 VCOM1 MCOM]</b></p>
219
 */
220
uint8_t getDebugLedsConfiguration(void)
221
{
222
    return eepromDebugLeds.regA;
223
}
224

    
225

    
226

    
227

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

    
249

    
250

    
251
/**
252
 <p><b>void debugLedsTest(void)</b></p>
253
 <p><b>Debug leds test sequence</b></p>
254
 */
255

    
256

    
257
void debugLedsTest(void)
258
{
259
    PORT_t *port;
260
    uint16_t addrPorts[] = {&PORTA, &PORTB, &PORTC, &PORTE, &PORTF };
261
    for(int i = 0; i < 5; i++)
262
    {
263
        port = addrPorts[i];
264
        port->OUT = 0x04;
265
        _delay_ms(125);
266
        port->OUT = 0x0C;
267
        _delay_ms(125);
268
        port->OUT = 0x00;
269
    }
270
}
271

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

    
373

    
374
/////////////////////////////////////////////////
375
//For Master port (USART0)
376

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

    
400

    
401
/**
402
 <p><b>void txWrite0(uint8_t b)</b></p>
403
 <p><b>Write a byte throught MASTER PORT</b></p>
404
 <p><b>Parameters : uint8_t b -> Byte to write</b></p>
405
 */
406

    
407
void txWrite0(uint8_t b) //write one byte
408
{
409
    while((USART0.STATUS & 0x20) != 0x20);
410
    USART0.TXDATAL = b;
411
    _hasWritten[USART0_INDEX] = true;
412
    while((USART0.STATUS & 0x40) != 0x40); //check when we transmit all the bits
413
    USART0.STATUS = 0x40; //clear the flag for next transmission
414
}
415

    
416

    
417
/**
418
 <p><b>uint8_t rxRead0(void)</b></p>
419
 <p><b>Read a byte from MASTER PORT</b></p>
420
 <p><b>Return : uint8_t result -> Byte to read</b></p>
421
 */
422
uint8_t rxRead0(void) //Read one byte from buffer
423
{    
424
    _hasRead[USART0_INDEX] = true;
425
    return USART0.RXDATAL;
426
}
427

    
428

    
429
/**
430
 <p><b>uint16_t portAvailable0(void)</b></p>
431
 <p><b>Get number of bytes available into the MASTER PORT's buffer</b></p>
432
 <p><b>Return : uint16_t bytesAvailable -> Number of bytes</b></p>
433
 */
434
uint8_t portAvailable0(void)
435
{
436
    return ((USART0.RXDATAH & 0x80) >> 7);
437
}
438

    
439

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

    
451

    
452

    
453

    
454

    
455
/////////////////////////////////////////////////
456
//For Virtual port 1 (USART3)
457

    
458

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

    
483

    
484

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

    
499

    
500
/**
501
 <p><b>uint8_t rxRead1(void)</b></p>
502
 <p><b>Read a byte from VCOM1</b></p>
503
 <p><b>Return : uint8_t result -> Byte to read</b></p>
504
 */
505
uint8_t rxRead1(void) //Read one byte from buffer
506
{
507
    _hasRead[USART3_INDEX] = true;
508
    return USART3.RXDATAL;
509
}
510

    
511

    
512
/**
513
 <p><b>uint16_t portAvailable1(void)</b></p>
514
 <p><b>Get number of bytes available into the VCOM1 PORT's buffer</b></p>
515
 <p><b>Return : uint16_t bytesAvailable -> Number of bytes</b></p>
516
 */
517
uint8_t portAvailable1(void)
518
{
519
    return ((USART3.STATUS & 0x80) >> 7);
520
}
521

    
522

    
523

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

    
538
/////////////////////////////////////////////////
539
//For Virtual port 2 (USART1)
540

    
541

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

    
567

    
568

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

    
583

    
584
/**
585
 <p><b>uint8_t rxRead2(void)</b></p>
586
 <p><b>Read a byte from VCOM2</b></p>
587
 <p><b>Return : uint8_t result -> Byte to read</b></p>
588
 */
589
uint8_t rxRead2(void) //Read one byte from buffer
590
{
591
    _hasRead[USART1_INDEX] = true;
592
    return USART1.RXDATAL;
593
}
594

    
595

    
596
/**
597
 <p><b>uint16_t portAvailable2(void)</b></p>
598
 <p><b>Get number of bytes available into the VCOM2 PORT's buffer</b></p>
599
 <p><b>Return : uint16_t bytesAvailable -> Number of bytes</b></p>
600
 */
601
uint8_t portAvailable2(void)
602
{
603
    return ((USART1.STATUS & 0x80) >> 7);
604
}
605

    
606

    
607

    
608

    
609
/*ISR(USART1_RXC_vect)
610
{
611
    //while((USART0.RXDATAH & 0x80) == 0x80);
612
    _RX2._rxReicv = USART1.RXDATAL; //read buffer
613
    _RX2._rxBuf[_RX2.bytesAvailable] = _RX2._rxReicv; //Add one byte to the buffer
614
    _RX2._rxReicv = 0x00; //clear value
615
    if(_RX2.bytesAvailable < 255) //ensure not to overflow buffer size...
616
        _RX2.bytesAvailable++; //a new byte is available into the buffer
617
        //PORTA.OUT ^= 0x3; //debug led
618
    _hasRead[USART1_INDEX] = true;
619
}
620
*/
621
/////////////////////////////////////////////////
622
//For Virtual port 3 (USART4)
623

    
624

    
625
/**
626
 <p><b>void initPort3(uint32_t bauds)</b></p>
627
 <p><b>Init MASTER PORT</b></p>
628
 <p><b>Parameters : uint8_t bauds -> Set bauds speed</b></p>
629
 <p><b>             bool enableDebug -> Enable debug leds</b></p>
630
 <p><b>             uint8_t chSize -> Data bits</b></p>
631
 <p><b>             uint8_t stopBit -> How many stop bits</b></p>
632
 <p><b>             uint8_t parity -> Parity type (ODD, EVEN, DISABLED)</b></p>
633
 <p><b>Don't forget to call sei() function after called all initPortx() functions !</p></b>
634
 */
635
void initPort3(uint32_t bauds, bool enableDebug, uint8_t chSize, bool stopBit, uint8_t parity)
636
{
637
    
638
    if(enableDebug)
639
        PORTE.DIR = USART4_PORT_PINS | USART4_DEBUG_LED; //Set TX/RX IO and RX/TX debug leds for USART0
640
    else
641
        PORTE.DIR = USART4_PORT_PINS; //Set TX/RX IO
642
    
643
    
644
    
645
    USART4.BAUD = bauds; 
646
    USART4.CTRLA = 0x00; //enable RX complete interrupt
647
    USART4.CTRLB = 0xD0; //Enable TX and RX sending
648
    USART4.CTRLC = (parity << 4) | (stopBit << 3) | chSize; //set serial port cfg
649
}
650

    
651

    
652

    
653
/**
654
 <p><b>void txWrite3(uint8_t b)</b></p>
655
 <p><b>Write a byte throught VCOM3</b></p>
656
 <p><b>Parameters : uint8_t b -> Byte to write</b></p>
657
 */
658
void txWrite3(uint8_t b) //Write one byte
659
{
660
    while((USART4.STATUS & 0x20) != 0x20);
661
    USART4.TXDATAL = b;
662
    _hasWritten[USART4_INDEX] = true;
663
    while((USART4.STATUS & 0x40) != 0x40); //check when we transmit all the bits
664
    USART4.STATUS = 0x40; //clear the flag for next transmission
665
}
666

    
667
/**
668
 <p><b>uint8_t rxRead3(void)</b></p>
669
 <p><b>Read a byte from VCOM3</b></p>
670
 <p><b>Return : uint8_t result -> Byte to read</b></p>
671
 */
672
uint8_t rxRead3(void) //Read one byte from buffer
673
{
674
    _hasRead[USART4_INDEX] = true;
675
    return USART4.RXDATAL;
676
}
677

    
678

    
679
/**
680
 <p><b>uint16_t portAvailable3(void)</b></p>
681
 <p><b>Get number of bytes available into the VCOM3 PORT's buffer</b></p>
682
 <p><b>Return : uint16_t bytesAvailable -> Number of bytes</b></p>
683
 */
684
uint8_t portAvailable3(void)
685
{
686
    return ((USART4.STATUS & 0x80) >> 7);
687
}
688

    
689

    
690
/*
691
ISR(USART4_RXC_vect)
692
{
693
    //while((USART0.RXDATAH & 0x80) == 0x80);
694
    _RX3._rxReicv = USART4.RXDATAL; //read buffer
695
    _RX3._rxBuf[_RX3.bytesAvailable] = _RX3._rxReicv; //Add one byte to the buffer
696
    _RX3._rxReicv = 0x00; //clear value
697
    if(_RX3.bytesAvailable < 255) //ensure not to overflow buffer size...
698
        _RX3.bytesAvailable++; //a new byte is available into the buffer
699
        //PORTA.OUT ^= 0x3; //debug led
700
    _hasRead[USART4_INDEX] = true;
701
}*/
702

    
703

    
704

    
705

    
706

    
707

    
708
/////////////////////////////////////////////////
709
//For Virtual port 4 (USART2)
710

    
711

    
712
/**
713
 <p><b>void initPort4(uint32_t bauds)</b></p>
714
 <p><b>Init MASTER PORT</b></p>
715
 <p><b>Parameters : uint8_t bauds -> Set bauds speed</b></p>
716
 <p><b>             bool enableDebug -> Enable debug leds</b></p>
717
 <p><b>             uint8_t chSize -> Data bits</b></p>
718
 <p><b>             uint8_t stopBit -> How many stop bits</b></p>
719
 <p><b>             uint8_t parity -> Parity type (ODD, EVEN, DISABLED)</b></p>
720
 <p><b>Don't forget to call sei() function after called all initPortx() functions !</p></b>
721
 */
722
void initPort4(uint32_t bauds, bool enableDebug, uint8_t chSize, bool stopBit, uint8_t parity)
723
{
724
    
725
    if(enableDebug)
726
        PORTF.DIR = USART2_PORT_PINS | USART2_DEBUG_LED; //Set TX/RX IO and RX/TX debug leds for USART0
727
    else
728
        PORTF.DIR = USART2_PORT_PINS; //Set TX/RX IO
729
    
730
    
731
    USART2.BAUD = bauds; 
732
    USART2.CTRLA = 0x00; //enable RX complete interrupt
733
    USART2.CTRLB = 0xD0; //Enable TX and RX sending
734
    USART2.CTRLC = (parity << 4) | (stopBit << 3) | chSize; //set serial port cfg
735
}
736

    
737

    
738

    
739
/**
740
 <p><b>void txWrite4(uint8_t b)</b></p>
741
 <p><b>Write a byte throught VCOM4</b></p>
742
 <p><b>Parameters : uint8_t b -> Byte to write</b></p>
743
 */
744
void txWrite4(uint8_t b) //Virtual Port 4
745
{
746
    while((USART2.STATUS & 0x20) != 0x20);
747
    USART2.TXDATAL = b;
748
    _hasWritten[USART2_INDEX] = true;
749
    while((USART2.STATUS & 0x40) != 0x40); //check when we transmit all the bits
750
    USART2.STATUS = 0x40; //clear the flag for next transmission
751
}
752

    
753
/**
754
 <p><b>uint8_t rxRead4(void)</b></p>
755
 <p><b>Read a byte from VCOM4</b></p>
756
 <p><b>Return : uint8_t result -> Byte to read</b></p>
757
 */
758
uint8_t rxRead4(void) //Read one byte from buffer
759
{
760
    _hasRead[USART2_INDEX] = true;
761
    return USART2.RXDATAL;
762
}
763

    
764

    
765
/**
766
 <p><b>uint16_t portAvailable4(void)</b></p>
767
 <p><b>Get number of bytes available into the VCOM4 PORT's buffer</b></p>
768
 <p><b>Return : uint16_t bytesAvailable -> Number of bytes</b></p>
769
 */
770
uint8_t portAvailable4(void)
771
{
772
    return ((USART2.STATUS & 0x80) >> 7);
773
}
774

    
775

    
776
/*
777
ISR(USART2_RXC_vect)
778
{
779
    //while((USART0.RXDATAH & 0x80) == 0x80);
780
    _RX4._rxReicv = USART2.RXDATAL; //read buffer
781
    _RX4._rxBuf[_RX4.bytesAvailable] = _RX4._rxReicv; //Add one byte to the buffer
782
    _RX4._rxReicv = 0x00; //clear value
783
    if(_RX4.bytesAvailable < 255) //ensure not to overflow buffer size...
784
        _RX4.bytesAvailable++; //a new byte is available into the buffer
785
        //PORTA.OUT ^= 0x3; //debug led
786
    _hasRead[USART2_INDEX] = true;
787
}*/
788

    
789
#ifdef        __cplusplus
790
}
791
#endif /* __cplusplus */
792

    
793
#endif        /* XC_HEADER_TEMPLATE_H */
794