Statistiques
| Branche: | Révision:

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

Historique | Voir | Annoter | Télécharger (21,533 ko)

1 10dcb6e9 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.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
    _hasRead[USART0_INDEX] = true;
415
    return USART0.RXDATAL;
416
}
417
418
419
/**
420
 <p><b>uint16_t portAvailable0(void)</b></p>
421
 <p><b>Get number of bytes available into the MASTER PORT's buffer</b></p>
422
 <p><b>Return : uint16_t bytesAvailable -> Number of bytes</b></p>
423
 */
424
uint8_t portAvailable0(void)
425
{
426
    return ((USART0.STATUS & 0x80) >> 7);
427
}
428
429
430
/*ISR(USART0_RXC_vect)
431
{
432
    //_RX0._rxReicv = USART0.RXDATAL; //read buffer
433
    _RX0._rxBuf[_RX0.bytesAvailable] = USART0.RXDATAL; //Add one byte to the buffer
434
    //_RX0._rxReicv = 0x00; //clear value
435
    if(_RX0.bytesAvailable < 255) //ensure not to overflow buffer size...
436
        _RX0.bytesAvailable++; //a new byte is available into the buffer
437
        //PORTA.OUT ^= 0x3; //debug led
438
    _hasRead[USART0_INDEX] = true;
439
}*/
440
441
442
443
444
445
/////////////////////////////////////////////////
446
//For Virtual port 1 (USART3)
447
448
449
/**
450
 <p><b>void initPort1(uint32_t bauds)</b></p>
451
 <p><b>Init MASTER PORT</b></p>
452
 <p><b>Parameters : uint8_t bauds -> Set bauds speed</b></p>
453
 <p><b>             bool enableDebug -> Enable debug leds</b></p>
454
 <p><b>             uint8_t chSize -> Data bits</b></p>
455
 <p><b>             uint8_t stopBit -> How many stop bits</b></p>
456
 <p><b>             uint8_t parity -> Parity type (ODD, EVEN, DISABLED)</b></p>
457
 <p><b>Don't forget to call sei() function after called all initPortx() functions !</p></b>
458
 */
459
void initPort1(uint32_t bauds, bool enableDebug, uint8_t chSize, bool stopBit, uint8_t parity)
460
{
461
    
462
    if(enableDebug)
463
        PORTB.DIR = USART3_PORT_PINS | USART3_DEBUG_LED; //Set TX/RX IO and RX/TX debug leds for USART0
464
    else
465
        PORTB.DIR = USART3_PORT_PINS; //Set TX/RX IO
466
    
467
    USART3.BAUD = bauds; 
468
    USART3.CTRLA = 0x00; //disable RX complete interrupt
469
    USART3.CTRLB = 0xD0; //Enable TX and RX sending
470
    USART3.CTRLC = (parity << 4) | (stopBit << 3) | chSize; //set serial port cfg
471
}
472
473
474
475
/**
476
 <p><b>void txWrite1(uint8_t b)</b></p>
477
 <p><b>Write a byte throught VCOM1</b></p>
478
 <p><b>Parameters : uint8_t b -> Byte to write</b></p>
479
 */
480
void txWrite1(uint8_t b) //Write one byte
481
{
482
    USART3.TXDATAL = b;
483
    _hasWritten[USART3_INDEX] = true;
484
    while((USART3.STATUS & 0x40) != 0x40); //check when we transmit all the bits
485
    USART3.STATUS = 0x40; //clear the flag for next transmission
486
}
487
488
489
/**
490
 <p><b>uint8_t rxRead1(void)</b></p>
491
 <p><b>Read a byte from VCOM1</b></p>
492
 <p><b>Return : uint8_t result -> Byte to read</b></p>
493
 */
494
uint8_t rxRead1(void) //Read one byte from buffer
495
{
496
    _hasRead[USART3_INDEX] = true;
497
    return USART3.RXDATAL;
498
}
499
500
501
/**
502
 <p><b>uint16_t portAvailable1(void)</b></p>
503
 <p><b>Get number of bytes available into the VCOM1 PORT's buffer</b></p>
504
 <p><b>Return : uint16_t bytesAvailable -> Number of bytes</b></p>
505
 */
506
uint8_t portAvailable1(void)
507
{
508
    return ((USART3.STATUS & 0x80) >> 7);
509
}
510
511
512
513
/*ISR(USART3_RXC_vect)
514
{
515
    //while((USART0.RXDATAH & 0x80) == 0x80);
516
    _RX1._rxReicv = USART3.RXDATAL; //read buffer
517
    _RX1._rxBuf[_RX1.bytesAvailable] = _RX1._rxReicv; //Add one byte to the buffer
518
    _RX1._rxReicv = 0x00; //clear value
519
    if(_RX1.bytesAvailable < 255) //ensure not to overflow buffer size...
520
        _RX1.bytesAvailable++; //a new byte is available into the buffer
521
        //PORTA.OUT ^= 0x3; //debug led
522
    
523
    _hasRead[USART3_INDEX] = true;
524
}
525
*/
526
527
/////////////////////////////////////////////////
528
//For Virtual port 2 (USART1)
529
530
531
/**
532
 <p><b>void initPort2(uint32_t bauds)</b></p>
533
 <p><b>Init MASTER PORT</b></p>
534
 <p><b>Parameters : uint8_t bauds -> Set bauds speed</b></p>
535
 <p><b>             bool enableDebug -> Enable debug leds</b></p>
536
 <p><b>             uint8_t chSize -> Data bits</b></p>
537
 <p><b>             uint8_t stopBit -> How many stop bits</b></p>
538
 <p><b>             uint8_t parity -> Parity type (ODD, EVEN, DISABLED)</b></p>
539
 <p><b>Don't forget to call sei() function after called all initPortx() functions !</p></b>
540
 */
541
void initPort2(uint32_t bauds, bool enableDebug, uint8_t chSize, bool stopBit, uint8_t parity)
542
{
543
    
544
    if(enableDebug)
545
        PORTC.DIR = USART1_PORT_PINS | USART1_DEBUG_LED; //Set TX/RX IO and RX/TX debug leds for USART0
546
    else
547
        PORTC.DIR = USART1_PORT_PINS; //Set TX/RX IO
548
    
549
    
550
    USART1.BAUD = bauds; 
551
    USART1.CTRLA = 0x00; //enable RX complete interrupt
552
    USART1.CTRLB = 0xD0; //Enable TX and RX sending
553
    USART1.CTRLC = (parity << 4) | (stopBit << 3) | chSize; //set serial port cfg
554
}
555
556
557
558
/**
559
 <p><b>void txWrite2(uint8_t b)</b></p>
560
 <p><b>Write a byte throught VCOM2</b></p>
561
 <p><b>Parameters : uint8_t b -> Byte to write</b></p>
562
 */
563
void txWrite2(uint8_t b) //Virtual Port 2
564
{
565
    USART1.TXDATAL = b;
566
    _hasWritten[USART1_INDEX] = true;
567
    while((USART1.STATUS & 0x40) != 0x40); //check when we transmit all the bits
568
    USART1.STATUS = 0x40; //clear the flag for next transmission
569
}
570
571
572
/**
573
 <p><b>uint8_t rxRead2(void)</b></p>
574
 <p><b>Read a byte from VCOM2</b></p>
575
 <p><b>Return : uint8_t result -> Byte to read</b></p>
576
 */
577
uint8_t rxRead2(void) //Read one byte from buffer
578
{
579
    _hasRead[USART1_INDEX] = true;
580
    return USART1.RXDATAL;
581
}
582
583
584
/**
585
 <p><b>uint16_t portAvailable2(void)</b></p>
586
 <p><b>Get number of bytes available into the VCOM2 PORT's buffer</b></p>
587
 <p><b>Return : uint16_t bytesAvailable -> Number of bytes</b></p>
588
 */
589
uint8_t portAvailable2(void)
590
{
591
    return ((USART1.STATUS & 0x80) >> 7);
592
}
593
594
595
596
597
/*ISR(USART1_RXC_vect)
598
{
599
    //while((USART0.RXDATAH & 0x80) == 0x80);
600
    _RX2._rxReicv = USART1.RXDATAL; //read buffer
601
    _RX2._rxBuf[_RX2.bytesAvailable] = _RX2._rxReicv; //Add one byte to the buffer
602
    _RX2._rxReicv = 0x00; //clear value
603
    if(_RX2.bytesAvailable < 255) //ensure not to overflow buffer size...
604
        _RX2.bytesAvailable++; //a new byte is available into the buffer
605
        //PORTA.OUT ^= 0x3; //debug led
606
    _hasRead[USART1_INDEX] = true;
607
}
608
*/
609
/////////////////////////////////////////////////
610
//For Virtual port 3 (USART4)
611
612
613
/**
614
 <p><b>void initPort3(uint32_t bauds)</b></p>
615
 <p><b>Init MASTER PORT</b></p>
616
 <p><b>Parameters : uint8_t bauds -> Set bauds speed</b></p>
617
 <p><b>             bool enableDebug -> Enable debug leds</b></p>
618
 <p><b>             uint8_t chSize -> Data bits</b></p>
619
 <p><b>             uint8_t stopBit -> How many stop bits</b></p>
620
 <p><b>             uint8_t parity -> Parity type (ODD, EVEN, DISABLED)</b></p>
621
 <p><b>Don't forget to call sei() function after called all initPortx() functions !</p></b>
622
 */
623
void initPort3(uint32_t bauds, bool enableDebug, uint8_t chSize, bool stopBit, uint8_t parity)
624
{
625
    
626
    if(enableDebug)
627
        PORTE.DIR = USART4_PORT_PINS | USART4_DEBUG_LED; //Set TX/RX IO and RX/TX debug leds for USART0
628
    else
629
        PORTE.DIR = USART4_PORT_PINS; //Set TX/RX IO
630
    
631
    
632
    
633
    USART4.BAUD = bauds; 
634
    USART4.CTRLA = 0x00; //enable RX complete interrupt
635
    USART4.CTRLB = 0xD0; //Enable TX and RX sending
636
    USART4.CTRLC = (parity << 4) | (stopBit << 3) | chSize; //set serial port cfg
637
}
638
639
640
641
/**
642
 <p><b>void txWrite3(uint8_t b)</b></p>
643
 <p><b>Write a byte throught VCOM3</b></p>
644
 <p><b>Parameters : uint8_t b -> Byte to write</b></p>
645
 */
646
void txWrite3(uint8_t b) //Write one byte
647
{
648
    USART4.TXDATAL = b;
649
    _hasWritten[USART4_INDEX] = true;
650
    while((USART4.STATUS & 0x40) != 0x40); //check when we transmit all the bits
651
    USART4.STATUS = 0x40; //clear the flag for next transmission
652
}
653
654
/**
655
 <p><b>uint8_t rxRead3(void)</b></p>
656
 <p><b>Read a byte from VCOM3</b></p>
657
 <p><b>Return : uint8_t result -> Byte to read</b></p>
658
 */
659
uint8_t rxRead3(void) //Read one byte from buffer
660
{
661
    _hasRead[USART4_INDEX] = true;
662
    return USART4.RXDATAL;
663
}
664
665
666
/**
667
 <p><b>uint16_t portAvailable3(void)</b></p>
668
 <p><b>Get number of bytes available into the VCOM3 PORT's buffer</b></p>
669
 <p><b>Return : uint16_t bytesAvailable -> Number of bytes</b></p>
670
 */
671
uint8_t portAvailable3(void)
672
{
673
    return ((USART4.STATUS & 0x80) >> 7);
674
}
675
676
677
/*
678
ISR(USART4_RXC_vect)
679
{
680
    //while((USART0.RXDATAH & 0x80) == 0x80);
681
    _RX3._rxReicv = USART4.RXDATAL; //read buffer
682
    _RX3._rxBuf[_RX3.bytesAvailable] = _RX3._rxReicv; //Add one byte to the buffer
683
    _RX3._rxReicv = 0x00; //clear value
684
    if(_RX3.bytesAvailable < 255) //ensure not to overflow buffer size...
685
        _RX3.bytesAvailable++; //a new byte is available into the buffer
686
        //PORTA.OUT ^= 0x3; //debug led
687
    _hasRead[USART4_INDEX] = true;
688
}*/
689
690
691
692
693
694
695
/////////////////////////////////////////////////
696
//For Virtual port 4 (USART2)
697
698
699
/**
700
 <p><b>void initPort4(uint32_t bauds)</b></p>
701
 <p><b>Init MASTER PORT</b></p>
702
 <p><b>Parameters : uint8_t bauds -> Set bauds speed</b></p>
703
 <p><b>             bool enableDebug -> Enable debug leds</b></p>
704
 <p><b>             uint8_t chSize -> Data bits</b></p>
705
 <p><b>             uint8_t stopBit -> How many stop bits</b></p>
706
 <p><b>             uint8_t parity -> Parity type (ODD, EVEN, DISABLED)</b></p>
707
 <p><b>Don't forget to call sei() function after called all initPortx() functions !</p></b>
708
 */
709
void initPort4(uint32_t bauds, bool enableDebug, uint8_t chSize, bool stopBit, uint8_t parity)
710
{
711
    
712
    if(enableDebug)
713
        PORTF.DIR = USART2_PORT_PINS | USART2_DEBUG_LED; //Set TX/RX IO and RX/TX debug leds for USART0
714
    else
715
        PORTF.DIR = USART2_PORT_PINS; //Set TX/RX IO
716
    
717
    
718
    USART2.BAUD = bauds; 
719
    USART2.CTRLA = 0x00; //enable RX complete interrupt
720
    USART2.CTRLB = 0xD0; //Enable TX and RX sending
721
    USART2.CTRLC = (parity << 4) | (stopBit << 3) | chSize; //set serial port cfg
722
}
723
724
725
726
/**
727
 <p><b>void txWrite4(uint8_t b)</b></p>
728
 <p><b>Write a byte throught VCOM4</b></p>
729
 <p><b>Parameters : uint8_t b -> Byte to write</b></p>
730
 */
731
void txWrite4(uint8_t b) //Virtual Port 4
732
{
733
    USART2.TXDATAL = b;
734
    _hasWritten[USART2_INDEX] = true;
735
    while((USART2.STATUS & 0x40) != 0x40); //check when we transmit all the bits
736
    USART2.STATUS = 0x40; //clear the flag for next transmission
737
}
738
739
/**
740
 <p><b>uint8_t rxRead4(void)</b></p>
741
 <p><b>Read a byte from VCOM4</b></p>
742
 <p><b>Return : uint8_t result -> Byte to read</b></p>
743
 */
744
uint8_t rxRead4(void) //Read one byte from buffer
745
{
746
    _hasRead[USART2_INDEX] = true;
747
    return USART2.RXDATAL;
748
}
749
750
751
/**
752
 <p><b>uint16_t portAvailable4(void)</b></p>
753
 <p><b>Get number of bytes available into the VCOM4 PORT's buffer</b></p>
754
 <p><b>Return : uint16_t bytesAvailable -> Number of bytes</b></p>
755
 */
756
uint8_t portAvailable4(void)
757
{
758
    return ((USART2.STATUS & 0x80) >> 7);
759
}
760
761
762
/*
763
ISR(USART2_RXC_vect)
764
{
765
    //while((USART0.RXDATAH & 0x80) == 0x80);
766
    _RX4._rxReicv = USART2.RXDATAL; //read buffer
767
    _RX4._rxBuf[_RX4.bytesAvailable] = _RX4._rxReicv; //Add one byte to the buffer
768
    _RX4._rxReicv = 0x00; //clear value
769
    if(_RX4.bytesAvailable < 255) //ensure not to overflow buffer size...
770
        _RX4.bytesAvailable++; //a new byte is available into the buffer
771
        //PORTA.OUT ^= 0x3; //debug led
772
    _hasRead[USART2_INDEX] = true;
773
}*/
774
775
#ifdef        __cplusplus
776
}
777
#endif /* __cplusplus */
778
779
#endif        /* XC_HEADER_TEMPLATE_H */