pic24_uart.c
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "pic24_all.h"
00034
00035
00036 #if (NUM_UART_MODS >= 1)
00037
00038
00039
00040
00041
00042
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00071 void checkRxErrorUART1(void) {
00072 uint8 u8_c;
00073
00074 if (U1STAbits.PERR) {
00075 u8_c = U1RXREG;
00076 reportError("UART1 parity error\n");
00077 }
00078 if (U1STAbits.FERR) {
00079 u8_c = U1RXREG;
00080 reportError("UART1 framing error\n");
00081 }
00082 if (U1STAbits.OERR) {
00083 U1STAbits.OERR = 0;
00084 reportError("UART1 overrun error\n");
00085 }
00086 }
00087
00088
00089
00090
00091 #ifdef UART1_TX_INTERRUPT
00092
00095 #ifndef UART1_TX_FIFO_SIZE
00096 #define UART1_TX_FIFO_SIZE 32 //choose a size
00097 #endif
00098
00099 #ifndef UART1_TX_INTERRUPT_PRIORITY
00100 #define UART1_TX_INTERRUPT_PRIORITY 1
00101 #endif
00102
00103 volatile uint8 au8_txFifo1[UART1_TX_FIFO_SIZE];
00104 volatile uint16 u16_txFifo1Head = 0;
00105 volatile uint16 u16_txFifo1Tail = 0;
00106
00111 void outChar1(uint8 u8_c) {
00112 uint16 u16_tmp;
00113
00114 u16_tmp = u16_txFifo1Head;
00115 u16_tmp++;
00116 if (u16_tmp == UART1_TX_FIFO_SIZE) u16_tmp = 0;
00117 while (u16_tmp == u16_txFifo1Tail)
00118 doHeartbeat();
00119
00120 au8_txFifo1[u16_tmp] = u8_c;
00121 u16_txFifo1Head = u16_tmp;
00122 _U1TXIE = 1;
00123 }
00124
00125 void _ISR _U1TXInterrupt (void) {
00126 if (u16_txFifo1Head == u16_txFifo1Tail) {
00127
00128 _U1TXIE = 0;
00129 } else {
00130
00131 u16_txFifo1Tail++;
00132 if (u16_txFifo1Tail == UART1_TX_FIFO_SIZE)
00133 u16_txFifo1Tail = 0;
00134 _U1TXIF = 0;
00135
00136 U1TXREG = au8_txFifo1[u16_txFifo1Tail];
00137 }
00138 }
00139
00140
00141 #else
00142
00146 void outChar1(uint8 u8_c) {
00147
00148 while (IS_TRANSMIT_BUFFER_FULL_UART1())
00149 doHeartbeat();
00150 U1TXREG = u8_c;
00151 }
00152 #endif
00153
00154 #ifdef UART1_RX_INTERRUPT
00155
00156 #ifndef UART1_RX_FIFO_SIZE
00157 #define UART1_RX_FIFO_SIZE 32 //choose a size
00158 #endif
00159
00160 #ifndef UART1_RX_INTERRUPT_PRIORITY
00161 #define UART1_RX_INTERRUPT_PRIORITY 1
00162 #endif
00163
00164 volatile uint8 au8_rxFifo1[UART1_RX_FIFO_SIZE];
00165 volatile uint16 u16_rxFifo1Head = 0;
00166 volatile uint16 u16_rxFifo1Tail = 0;
00167
00171 uint8 isCharReady1(void) {
00172 return(u16_rxFifo1Head != u16_rxFifo1Tail);
00173 }
00174
00179 uint8 inChar1(void) {
00180 while (u16_rxFifo1Head == u16_rxFifo1Tail)
00181 doHeartbeat();
00182 u16_rxFifo1Tail++;
00183 if (u16_rxFifo1Tail == UART1_RX_FIFO_SIZE) u16_rxFifo1Tail=0;
00184 return au8_rxFifo1[u16_rxFifo1Tail];
00185 }
00186
00187 void _ISR _U1RXInterrupt (void) {
00188 int8 u8_c;
00189
00190 _U1RXIF = 0;
00191 checkRxErrorUART1();
00192 u8_c = U1RXREG;
00193 u16_rxFifo1Head++;
00194 if (u16_rxFifo1Head == UART1_RX_FIFO_SIZE)
00195 u16_rxFifo1Head = 0;
00196 if (u16_rxFifo1Head == u16_rxFifo1Tail) {
00197
00198 reportError("UART1 RX Interrupt FIFO overrun!");
00199 }
00200 au8_rxFifo1[u16_rxFifo1Head] = u8_c;
00201 }
00202
00203 #else
00204
00207 uint8 isCharReady1(void) {
00208 return(IS_CHAR_READY_UART1());
00209 }
00210
00215 uint8 inChar1(void) {
00216
00217
00218
00219 while (!IS_CHAR_READY_UART1())
00220 doHeartbeat();
00221 checkRxErrorUART1();
00222 return U1RXREG;
00223 }
00224 #endif
00225
00226
00235 void configUART1(uint32 u32_baudRate) {
00236
00237
00238
00239
00240 #if defined(EXPLORER16_100P)
00241
00242 #elif (1 == 1) //change pin mappings for your device
00243 #ifdef DANGEROUS_WEB
00244
00245
00246 CONFIG_RP14_AS_DIG_PIN();
00247 CONFIG_U1RX_TO_RP(14);
00248 CONFIG_RP15_AS_DIG_PIN();
00249 CONFIG_U1TX_TO_RP(15);
00250 #else
00251
00252 CONFIG_RP10_AS_DIG_PIN();
00253 CONFIG_U1RX_TO_RP(10);
00254 CONFIG_RP11_AS_DIG_PIN();
00255 CONFIG_U1TX_TO_RP(11);
00256 #endif
00257 DISABLE_U1TX_ANALOG();
00258 DISABLE_U1RX_ANALOG();
00259 #else
00260 #warning UART1 pin mappings not defined!!! For simplicity,
00261 #warning pin mappings are identical for UARTS 1-4. If your device has more than
00262 #warning one UART, ****** CHANGE THE MAPPING ****** since
00263 #warning multiple UARTs can not share the same pins.
00264 #warning In particular:
00265 #warning 1. Change the statement #if (1 == 1) to #if 1
00266 #warning 2. Change the pin numbers in the next four lines
00267 #warning to something valid for your device.
00268 #warning If your device does not have remappable I/O,
00269 #warning (typical for >44 pin packages), skip this step --
00270 #warning the UART I/O pins are already assigned to something
00271 #warning valid.
00272 #endif
00273
00274 CONFIG_BAUDRATE_UART1(u32_baudRate);
00275 CONFIG_PDSEL_UART1(UXMODE_PDSEL_8DATA_NOPARITY);
00276 CONFIG_STOPBITS_UART1(1);
00277 #ifdef UART1_RX_INTERRUPT
00278 _U1RXIF = 0;
00279 _U1RXIP = UART1_RX_INTERRUPT_PRIORITY;
00280 _U1RXIE = 1;
00281 #endif
00282 #ifdef UART1_TX_INTERRUPT
00283
00284 _U1TXIP = UART1_TX_INTERRUPT_PRIORITY;
00285
00286 #endif
00287 ENABLE_UART1();
00288 }
00289
00290 #endif // #if (NUM_UARTS >= 1)
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327 #include "pic24_all.h"
00328
00329
00330 #if (NUM_UART_MODS >= 2)
00331
00332
00333
00334
00335
00336
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00365 void checkRxErrorUART2(void) {
00366 uint8 u8_c;
00367
00368 if (U2STAbits.PERR) {
00369 u8_c = U2RXREG;
00370 reportError("UART2 parity error\n");
00371 }
00372 if (U2STAbits.FERR) {
00373 u8_c = U2RXREG;
00374 reportError("UART2 framing error\n");
00375 }
00376 if (U2STAbits.OERR) {
00377 U2STAbits.OERR = 0;
00378 reportError("UART2 overrun error\n");
00379 }
00380 }
00381
00382
00383
00384
00385 #ifdef UART2_TX_INTERRUPT
00386
00389 #ifndef UART2_TX_FIFO_SIZE
00390 #define UART2_TX_FIFO_SIZE 32 //choose a size
00391 #endif
00392
00393 #ifndef UART2_TX_INTERRUPT_PRIORITY
00394 #define UART2_TX_INTERRUPT_PRIORITY 1
00395 #endif
00396
00397 volatile uint8 au8_txFifo2[UART2_TX_FIFO_SIZE];
00398 volatile uint16 u16_txFifo2Head = 0;
00399 volatile uint16 u16_txFifo2Tail = 0;
00400
00405 void outChar2(uint8 u8_c) {
00406 uint16 u16_tmp;
00407
00408 u16_tmp = u16_txFifo2Head;
00409 u16_tmp++;
00410 if (u16_tmp == UART2_TX_FIFO_SIZE) u16_tmp = 0;
00411 while (u16_tmp == u16_txFifo2Tail)
00412 doHeartbeat();
00413
00414 au8_txFifo2[u16_tmp] = u8_c;
00415 u16_txFifo2Head = u16_tmp;
00416 _U2TXIE = 1;
00417 }
00418
00419 void _ISR _U2TXInterrupt (void) {
00420 if (u16_txFifo2Head == u16_txFifo2Tail) {
00421
00422 _U2TXIE = 0;
00423 } else {
00424
00425 u16_txFifo2Tail++;
00426 if (u16_txFifo2Tail == UART2_TX_FIFO_SIZE)
00427 u16_txFifo2Tail = 0;
00428 _U2TXIF = 0;
00429
00430 U2TXREG = au8_txFifo2[u16_txFifo2Tail];
00431 }
00432 }
00433
00434
00435 #else
00436
00440 void outChar2(uint8 u8_c) {
00441
00442 while (IS_TRANSMIT_BUFFER_FULL_UART2())
00443 doHeartbeat();
00444 U2TXREG = u8_c;
00445 }
00446 #endif
00447
00448 #ifdef UART2_RX_INTERRUPT
00449
00450 #ifndef UART2_RX_FIFO_SIZE
00451 #define UART2_RX_FIFO_SIZE 32 //choose a size
00452 #endif
00453
00454 #ifndef UART2_RX_INTERRUPT_PRIORITY
00455 #define UART2_RX_INTERRUPT_PRIORITY 1
00456 #endif
00457
00458 volatile uint8 au8_rxFifo2[UART2_RX_FIFO_SIZE];
00459 volatile uint16 u16_rxFifo2Head = 0;
00460 volatile uint16 u16_rxFifo2Tail = 0;
00461
00465 uint8 isCharReady2(void) {
00466 return(u16_rxFifo2Head != u16_rxFifo2Tail);
00467 }
00468
00473 uint8 inChar2(void) {
00474 while (u16_rxFifo2Head == u16_rxFifo2Tail)
00475 doHeartbeat();
00476 u16_rxFifo2Tail++;
00477 if (u16_rxFifo2Tail == UART2_RX_FIFO_SIZE) u16_rxFifo2Tail=0;
00478 return au8_rxFifo2[u16_rxFifo2Tail];
00479 }
00480
00481 void _ISR _U2RXInterrupt (void) {
00482 int8 u8_c;
00483
00484 _U2RXIF = 0;
00485 checkRxErrorUART2();
00486 u8_c = U2RXREG;
00487 u16_rxFifo2Head++;
00488 if (u16_rxFifo2Head == UART2_RX_FIFO_SIZE)
00489 u16_rxFifo2Head = 0;
00490 if (u16_rxFifo2Head == u16_rxFifo2Tail) {
00491
00492 reportError("UART2 RX Interrupt FIFO overrun!");
00493 }
00494 au8_rxFifo2[u16_rxFifo2Head] = u8_c;
00495 }
00496
00497 #else
00498
00501 uint8 isCharReady2(void) {
00502 return(IS_CHAR_READY_UART2());
00503 }
00504
00509 uint8 inChar2(void) {
00510
00511
00512
00513 while (!IS_CHAR_READY_UART2())
00514 doHeartbeat();
00515 checkRxErrorUART2();
00516 return U2RXREG;
00517 }
00518 #endif
00519
00520
00529 void configUART2(uint32 u32_baudRate) {
00530
00531
00532
00533
00534 #if defined(EXPLORER16_100P)
00535
00536 #elif (2 == 1) //change pin mappings for your device
00537 CONFIG_RP10_AS_DIG_PIN();
00538 CONFIG_U2RX_TO_RP(10);
00539 CONFIG_RP11_AS_DIG_PIN();
00540 CONFIG_U2TX_TO_RP(11);
00541 DISABLE_U2TX_ANALOG();
00542 DISABLE_U2RX_ANALOG();
00543 #else
00544 #warning UART2 pin mappings not defined!!! For simplicity,
00545 #warning pin mappings are identical for UARTS 1-4. If your device has more than
00546 #warning one UART, ****** CHANGE THE MAPPING ****** since
00547 #warning multiple UARTs can not share the same pins.
00548 #warning In particular:
00549 #warning 1. Change the statement #if (2 == 1) to #if 1
00550 #warning 2. Change the pin numbers in the next four lines
00551 #warning to something valid for your device.
00552 #warning If your device does not have remappable I/O,
00553 #warning (typical for >44 pin packages), skip this step --
00554 #warning the UART I/O pins are already assigned to something
00555 #warning valid.
00556 #endif
00557
00558 CONFIG_BAUDRATE_UART2(u32_baudRate);
00559 CONFIG_PDSEL_UART2(UXMODE_PDSEL_8DATA_NOPARITY);
00560 CONFIG_STOPBITS_UART2(1);
00561 #ifdef UART2_RX_INTERRUPT
00562 _U2RXIF = 0;
00563 _U2RXIP = UART2_RX_INTERRUPT_PRIORITY;
00564 _U2RXIE = 1;
00565 #endif
00566 #ifdef UART2_TX_INTERRUPT
00567
00568 _U2TXIP = UART2_TX_INTERRUPT_PRIORITY;
00569
00570 #endif
00571 ENABLE_UART2();
00572 }
00573
00574 #endif // #if (NUM_UARTS >= 2)
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611 #include "pic24_all.h"
00612
00613
00614 #if (NUM_UART_MODS >= 3)
00615
00616
00617
00618
00619
00620
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00649 void checkRxErrorUART3(void) {
00650 uint8 u8_c;
00651
00652 if (U3STAbits.PERR) {
00653 u8_c = U3RXREG;
00654 reportError("UART3 parity error\n");
00655 }
00656 if (U3STAbits.FERR) {
00657 u8_c = U3RXREG;
00658 reportError("UART3 framing error\n");
00659 }
00660 if (U3STAbits.OERR) {
00661 U3STAbits.OERR = 0;
00662 reportError("UART3 overrun error\n");
00663 }
00664 }
00665
00666
00667
00668
00669 #ifdef UART3_TX_INTERRUPT
00670
00673 #ifndef UART3_TX_FIFO_SIZE
00674 #define UART3_TX_FIFO_SIZE 32 //choose a size
00675 #endif
00676
00677 #ifndef UART3_TX_INTERRUPT_PRIORITY
00678 #define UART3_TX_INTERRUPT_PRIORITY 1
00679 #endif
00680
00681 volatile uint8 au8_txFifo3[UART3_TX_FIFO_SIZE];
00682 volatile uint16 u16_txFifo3Head = 0;
00683 volatile uint16 u16_txFifo3Tail = 0;
00684
00689 void outChar3(uint8 u8_c) {
00690 uint16 u16_tmp;
00691
00692 u16_tmp = u16_txFifo3Head;
00693 u16_tmp++;
00694 if (u16_tmp == UART3_TX_FIFO_SIZE) u16_tmp = 0;
00695 while (u16_tmp == u16_txFifo3Tail)
00696 doHeartbeat();
00697
00698 au8_txFifo3[u16_tmp] = u8_c;
00699 u16_txFifo3Head = u16_tmp;
00700 _U3TXIE = 1;
00701 }
00702
00703 void _ISR _U3TXInterrupt (void) {
00704 if (u16_txFifo3Head == u16_txFifo3Tail) {
00705
00706 _U3TXIE = 0;
00707 } else {
00708
00709 u16_txFifo3Tail++;
00710 if (u16_txFifo3Tail == UART3_TX_FIFO_SIZE)
00711 u16_txFifo3Tail = 0;
00712 _U3TXIF = 0;
00713
00714 U3TXREG = au8_txFifo3[u16_txFifo3Tail];
00715 }
00716 }
00717
00718
00719 #else
00720
00724 void outChar3(uint8 u8_c) {
00725
00726 while (IS_TRANSMIT_BUFFER_FULL_UART3())
00727 doHeartbeat();
00728 U3TXREG = u8_c;
00729 }
00730 #endif
00731
00732 #ifdef UART3_RX_INTERRUPT
00733
00734 #ifndef UART3_RX_FIFO_SIZE
00735 #define UART3_RX_FIFO_SIZE 32 //choose a size
00736 #endif
00737
00738 #ifndef UART3_RX_INTERRUPT_PRIORITY
00739 #define UART3_RX_INTERRUPT_PRIORITY 1
00740 #endif
00741
00742 volatile uint8 au8_rxFifo3[UART3_RX_FIFO_SIZE];
00743 volatile uint16 u16_rxFifo3Head = 0;
00744 volatile uint16 u16_rxFifo3Tail = 0;
00745
00749 uint8 isCharReady3(void) {
00750 return(u16_rxFifo3Head != u16_rxFifo3Tail);
00751 }
00752
00757 uint8 inChar3(void) {
00758 while (u16_rxFifo3Head == u16_rxFifo3Tail)
00759 doHeartbeat();
00760 u16_rxFifo3Tail++;
00761 if (u16_rxFifo3Tail == UART3_RX_FIFO_SIZE) u16_rxFifo3Tail=0;
00762 return au8_rxFifo3[u16_rxFifo3Tail];
00763 }
00764
00765 void _ISR _U3RXInterrupt (void) {
00766 int8 u8_c;
00767
00768 _U3RXIF = 0;
00769 checkRxErrorUART3();
00770 u8_c = U3RXREG;
00771 u16_rxFifo3Head++;
00772 if (u16_rxFifo3Head == UART3_RX_FIFO_SIZE)
00773 u16_rxFifo3Head = 0;
00774 if (u16_rxFifo3Head == u16_rxFifo3Tail) {
00775
00776 reportError("UART3 RX Interrupt FIFO overrun!");
00777 }
00778 au8_rxFifo3[u16_rxFifo3Head] = u8_c;
00779 }
00780
00781 #else
00782
00785 uint8 isCharReady3(void) {
00786 return(IS_CHAR_READY_UART3());
00787 }
00788
00793 uint8 inChar3(void) {
00794
00795
00796
00797 while (!IS_CHAR_READY_UART3())
00798 doHeartbeat();
00799 checkRxErrorUART3();
00800 return U3RXREG;
00801 }
00802 #endif
00803
00804
00813 void configUART3(uint32 u32_baudRate) {
00814
00815
00816
00817
00818 #if defined(EXPLORER16_100P)
00819
00820 #elif (3 == 1) //change pin mappings for your device
00821 CONFIG_RP10_AS_DIG_PIN();
00822 CONFIG_U3RX_TO_RP(10);
00823 CONFIG_RP11_AS_DIG_PIN();
00824 CONFIG_U3TX_TO_RP(11);
00825 DISABLE_U3TX_ANALOG();
00826 DISABLE_U3RX_ANALOG();
00827 #else
00828 #warning UART3 pin mappings not defined!!! For simplicity,
00829 #warning pin mappings are identical for UARTS 1-4. If your device has more than
00830 #warning one UART, ****** CHANGE THE MAPPING ****** since
00831 #warning multiple UARTs can not share the same pins.
00832 #warning In particular:
00833 #warning 1. Change the statement #if (3 == 1) to #if 1
00834 #warning 2. Change the pin numbers in the next four lines
00835 #warning to something valid for your device.
00836 #warning If your device does not have remappable I/O,
00837 #warning (typical for >44 pin packages), skip this step --
00838 #warning the UART I/O pins are already assigned to something
00839 #warning valid.
00840 #endif
00841
00842 CONFIG_BAUDRATE_UART3(u32_baudRate);
00843 CONFIG_PDSEL_UART3(UXMODE_PDSEL_8DATA_NOPARITY);
00844 CONFIG_STOPBITS_UART3(1);
00845 #ifdef UART3_RX_INTERRUPT
00846 _U3RXIF = 0;
00847 _U3RXIP = UART3_RX_INTERRUPT_PRIORITY;
00848 _U3RXIE = 1;
00849 #endif
00850 #ifdef UART3_TX_INTERRUPT
00851
00852 _U3TXIP = UART3_TX_INTERRUPT_PRIORITY;
00853
00854 #endif
00855 ENABLE_UART3();
00856 }
00857
00858 #endif // #if (NUM_UARTS >= 3)
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895 #include "pic24_all.h"
00896
00897
00898 #if (NUM_UART_MODS >= 4)
00899
00900
00901
00902
00903
00904
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00933 void checkRxErrorUART4(void) {
00934 uint8 u8_c;
00935
00936 if (U4STAbits.PERR) {
00937 u8_c = U4RXREG;
00938 reportError("UART4 parity error\n");
00939 }
00940 if (U4STAbits.FERR) {
00941 u8_c = U4RXREG;
00942 reportError("UART4 framing error\n");
00943 }
00944 if (U4STAbits.OERR) {
00945 U4STAbits.OERR = 0;
00946 reportError("UART4 overrun error\n");
00947 }
00948 }
00949
00950
00951
00952
00953 #ifdef UART4_TX_INTERRUPT
00954
00957 #ifndef UART4_TX_FIFO_SIZE
00958 #define UART4_TX_FIFO_SIZE 32 //choose a size
00959 #endif
00960
00961 #ifndef UART4_TX_INTERRUPT_PRIORITY
00962 #define UART4_TX_INTERRUPT_PRIORITY 1
00963 #endif
00964
00965 volatile uint8 au8_txFifo4[UART4_TX_FIFO_SIZE];
00966 volatile uint16 u16_txFifo4Head = 0;
00967 volatile uint16 u16_txFifo4Tail = 0;
00968
00973 void outChar4(uint8 u8_c) {
00974 uint16 u16_tmp;
00975
00976 u16_tmp = u16_txFifo4Head;
00977 u16_tmp++;
00978 if (u16_tmp == UART4_TX_FIFO_SIZE) u16_tmp = 0;
00979 while (u16_tmp == u16_txFifo4Tail)
00980 doHeartbeat();
00981
00982 au8_txFifo4[u16_tmp] = u8_c;
00983 u16_txFifo4Head = u16_tmp;
00984 _U4TXIE = 1;
00985 }
00986
00987 void _ISR _U4TXInterrupt (void) {
00988 if (u16_txFifo4Head == u16_txFifo4Tail) {
00989
00990 _U4TXIE = 0;
00991 } else {
00992
00993 u16_txFifo4Tail++;
00994 if (u16_txFifo4Tail == UART4_TX_FIFO_SIZE)
00995 u16_txFifo4Tail = 0;
00996 _U4TXIF = 0;
00997
00998 U4TXREG = au8_txFifo4[u16_txFifo4Tail];
00999 }
01000 }
01001
01002
01003 #else
01004
01008 void outChar4(uint8 u8_c) {
01009
01010 while (IS_TRANSMIT_BUFFER_FULL_UART4())
01011 doHeartbeat();
01012 U4TXREG = u8_c;
01013 }
01014 #endif
01015
01016 #ifdef UART4_RX_INTERRUPT
01017
01018 #ifndef UART4_RX_FIFO_SIZE
01019 #define UART4_RX_FIFO_SIZE 32 //choose a size
01020 #endif
01021
01022 #ifndef UART4_RX_INTERRUPT_PRIORITY
01023 #define UART4_RX_INTERRUPT_PRIORITY 1
01024 #endif
01025
01026 volatile uint8 au8_rxFifo4[UART4_RX_FIFO_SIZE];
01027 volatile uint16 u16_rxFifo4Head = 0;
01028 volatile uint16 u16_rxFifo4Tail = 0;
01029
01033 uint8 isCharReady4(void) {
01034 return(u16_rxFifo4Head != u16_rxFifo4Tail);
01035 }
01036
01041 uint8 inChar4(void) {
01042 while (u16_rxFifo4Head == u16_rxFifo4Tail)
01043 doHeartbeat();
01044 u16_rxFifo4Tail++;
01045 if (u16_rxFifo4Tail == UART4_RX_FIFO_SIZE) u16_rxFifo4Tail=0;
01046 return au8_rxFifo4[u16_rxFifo4Tail];
01047 }
01048
01049 void _ISR _U4RXInterrupt (void) {
01050 int8 u8_c;
01051
01052 _U4RXIF = 0;
01053 checkRxErrorUART4();
01054 u8_c = U4RXREG;
01055 u16_rxFifo4Head++;
01056 if (u16_rxFifo4Head == UART4_RX_FIFO_SIZE)
01057 u16_rxFifo4Head = 0;
01058 if (u16_rxFifo4Head == u16_rxFifo4Tail) {
01059
01060 reportError("UART4 RX Interrupt FIFO overrun!");
01061 }
01062 au8_rxFifo4[u16_rxFifo4Head] = u8_c;
01063 }
01064
01065 #else
01066
01069 uint8 isCharReady4(void) {
01070 return(IS_CHAR_READY_UART4());
01071 }
01072
01077 uint8 inChar4(void) {
01078
01079
01080
01081 while (!IS_CHAR_READY_UART4())
01082 doHeartbeat();
01083 checkRxErrorUART4();
01084 return U4RXREG;
01085 }
01086 #endif
01087
01088
01097 void configUART4(uint32 u32_baudRate) {
01098
01099
01100
01101
01102 #if defined(EXPLORER16_100P)
01103
01104 #elif (4 == 1) //change pin mappings for your device
01105 CONFIG_RP10_AS_DIG_PIN();
01106 CONFIG_U4RX_TO_RP(10);
01107 CONFIG_RP11_AS_DIG_PIN();
01108 CONFIG_U4TX_TO_RP(11);
01109 DISABLE_U4TX_ANALOG();
01110 DISABLE_U4RX_ANALOG();
01111 #else
01112 #warning UART4 pin mappings not defined!!! For simplicity,
01113 #warning pin mappings are identical for UARTS 1-4. If your device has more than
01114 #warning one UART, ****** CHANGE THE MAPPING ****** since
01115 #warning multiple UARTs can not share the same pins.
01116 #warning In particular:
01117 #warning 1. Change the statement #if (4 == 1) to #if 1
01118 #warning 2. Change the pin numbers in the next four lines
01119 #warning to something valid for your device.
01120 #warning If your device does not have remappable I/O,
01121 #warning (typical for >44 pin packages), skip this step --
01122 #warning the UART I/O pins are already assigned to something
01123 #warning valid.
01124 #endif
01125
01126 CONFIG_BAUDRATE_UART4(u32_baudRate);
01127 CONFIG_PDSEL_UART4(UXMODE_PDSEL_8DATA_NOPARITY);
01128 CONFIG_STOPBITS_UART4(1);
01129 #ifdef UART4_RX_INTERRUPT
01130 _U4RXIF = 0;
01131 _U4RXIP = UART4_RX_INTERRUPT_PRIORITY;
01132 _U4RXIE = 1;
01133 #endif
01134 #ifdef UART4_TX_INTERRUPT
01135
01136 _U4TXIP = UART4_TX_INTERRUPT_PRIORITY;
01137
01138 #endif
01139 ENABLE_UART4();
01140 }
01141
01142 #endif // #if (NUM_UARTS >= 4)
01143
01144
01145
01146