pic24_uart.c

00001 /*
00002  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
00003  * All rights reserved.
00004  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
00005  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
00006  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
00007  *
00008  * Permission to use, copy, modify, and distribute this software and its
00009  * documentation for any purpose, without fee, and without written agreement is
00010  * hereby granted, provided that the above copyright notice, the following
00011  * two paragraphs and the authors appear in all copies of this software.
00012  *
00013  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
00014  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
00015  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
00016  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00017  *
00018  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
00019  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
00020  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
00021  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
00022  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
00023  *
00024  * Please maintain this header in its entirety when copying/modifying
00025  * these files.
00026  *
00027  *
00028  */
00029 
00030 
00031 
00032 
00033 #include "pic24_all.h"
00034 
00035 // Only include if this UART exists.
00036 #if (NUM_UART_MODS >= 1)
00037 
00038 
00039 // Documentation for this file. If the \file tag is not present,
00040 // this file will not be documented.
00041 // Note: place this comment below the #if NUM_UART_MODS so Doxygen
00042 // will only see it once.
00055 /*********************************
00056  * Function private to this file *
00057  *********************************/
00058 
00059 
00060 
00061 
00062 
00063 
00064 /*********************************************************
00065  * Public functions intended to be called by other files *
00066  *********************************************************/
00071 void checkRxErrorUART1(void) {
00072   uint8 u8_c;
00073 //check for errors, reset if detected.
00074   if (U1STAbits.PERR) {
00075     u8_c = U1RXREG; //clear error
00076     reportError("UART1 parity error\n");
00077   }
00078   if (U1STAbits.FERR) {
00079     u8_c = U1RXREG; //clear error
00080     reportError("UART1 framing error\n");
00081   }
00082   if (U1STAbits.OERR) {
00083     U1STAbits.OERR = 0; //clear error
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; //wrap if needed
00117   while (u16_tmp == u16_txFifo1Tail)
00118     doHeartbeat();
00119 
00120   au8_txFifo1[u16_tmp] = u8_c; //write to buffer
00121   u16_txFifo1Head = u16_tmp;    //update head
00122   _U1TXIE = 1;                 //enable interrupt
00123 }
00124 
00125 void _ISR _U1TXInterrupt (void) {
00126   if (u16_txFifo1Head == u16_txFifo1Tail) {
00127     //empty TX buffer, disable the interrupt, do not clear the flag
00128     _U1TXIE = 0;
00129   } else {
00130     //at least one free spot in the TX buffer!
00131     u16_txFifo1Tail++;     //increment tail pointer
00132     if (u16_txFifo1Tail == UART1_TX_FIFO_SIZE)
00133       u16_txFifo1Tail = 0; //wrap if needed
00134     _U1TXIF = 0;   //clear the interrupt flag
00135     //transfer character from software buffer to transmit buffer
00136     U1TXREG =  au8_txFifo1[u16_txFifo1Tail];
00137   }
00138 }
00139 
00140 
00141 #else
00142 
00146 void outChar1(uint8 u8_c) {
00147   //wait for transmit buffer to be empty
00148   while (IS_TRANSMIT_BUFFER_FULL_UART1())
00149     doHeartbeat();
00150   U1TXREG = u8_c;
00151 }
00152 #endif
00153 
00154 #ifdef UART1_RX_INTERRUPT
00155 //Interrupt driven RX
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; //wrap
00184   return au8_rxFifo1[u16_rxFifo1Tail];  //return the character
00185 }
00186 
00187 void _ISR _U1RXInterrupt (void) {
00188   int8 u8_c;
00189 
00190   _U1RXIF = 0;          //clear the UART RX interrupt bit
00191   checkRxErrorUART1();
00192   u8_c = U1RXREG;       //read character
00193   u16_rxFifo1Head++;     //increment head pointer
00194   if (u16_rxFifo1Head == UART1_RX_FIFO_SIZE)
00195     u16_rxFifo1Head = 0; //wrap if needed
00196   if (u16_rxFifo1Head == u16_rxFifo1Tail) {
00197     //FIFO overrun!, report error
00198     reportError("UART1 RX Interrupt FIFO overrun!");
00199   }
00200   au8_rxFifo1[u16_rxFifo1Head] = u8_c;   //place in buffer
00201 }
00202 
00203 #else
00204 
00207 uint8 isCharReady1(void) {
00208   return(IS_CHAR_READY_UART1());
00209 }
00210 
00215 uint8 inChar1(void) {
00216   //do heartbeat while waiting for character.
00217   // Use a do-while to insure error checks
00218   // are always run.
00219   while (!IS_CHAR_READY_UART1())
00220     doHeartbeat();
00221   checkRxErrorUART1();
00222   return U1RXREG;  //read the receive register
00223 }
00224 #endif
00225 
00226 
00235 void configUART1(uint32 u32_baudRate) {
00236   /*************************  UART config ********************/
00237   // NOTE: the following pin mappings are (for simplicity)
00238   // identical for UARTS 1-4. See comments in the #warning
00239   // statements below for more information.
00240 #if defined(EXPLORER16_100P)
00241 //nothing to do, pins mapped to fixed ports
00242 #elif (1 == 1)             //change pin mappings for your device
00243 #ifdef DANGEROUS_WEB
00244   // Configure for the Dangerous Prototypes web platform.
00245   // See http://dangerousprototypes.com/2009/12/11/prototype-web-platform/.
00246   CONFIG_RP14_AS_DIG_PIN();                //RX RP pin must be digital
00247   CONFIG_U1RX_TO_RP(14);                   //U1RX <- RP14
00248   CONFIG_RP15_AS_DIG_PIN();                //TX RP pin must be digital
00249   CONFIG_U1TX_TO_RP(15);                   //U1TX -> RP15
00250 #else
00251   // Default configuration
00252   CONFIG_RP10_AS_DIG_PIN();                //RX RP pin must be digital
00253   CONFIG_U1RX_TO_RP(10);                 //U1RX <- RP10
00254   CONFIG_RP11_AS_DIG_PIN();                //TX RP pin must be digital
00255   CONFIG_U1TX_TO_RP(11);                 //U1TX -> RP11
00256 #endif
00257   DISABLE_U1TX_ANALOG();                 //turn off any analog functionality on pin (may be needed if pin is hardmapped and RPx does not exist)
00258   DISABLE_U1RX_ANALOG();                 //turn off any analog functionality on pin (may be needed if pin is hardmapped and RPx does not e
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   //UART macros defined in "pic24_uart.h"
00274   CONFIG_BAUDRATE_UART1(u32_baudRate);   //baud rate
00275   CONFIG_PDSEL_UART1(UXMODE_PDSEL_8DATA_NOPARITY);        // 8-bit data, no parity
00276   CONFIG_STOPBITS_UART1(1);            // 1 Stop bit
00277 #ifdef UART1_RX_INTERRUPT
00278   _U1RXIF = 0;              //clear the flag
00279   _U1RXIP = UART1_RX_INTERRUPT_PRIORITY;  //choose a priority
00280   _U1RXIE = 1;              //enable the interrupt
00281 #endif
00282 #ifdef UART1_TX_INTERRUPT
00283   //do not clear the U1TXIF flag!
00284   _U1TXIP = UART1_TX_INTERRUPT_PRIORITY;  //choose a priority
00285   //do not enable the interrupt until we try to write to the UART
00286 #endif
00287   ENABLE_UART1();                        //enable the UART
00288 }
00289 
00290 #endif // #if (NUM_UARTS >= 1)
00291 
00292 
00293 
00294 
00295 /*
00296  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
00297  * All rights reserved.
00298  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
00299  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
00300  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
00301  *
00302  * Permission to use, copy, modify, and distribute this software and its
00303  * documentation for any purpose, without fee, and without written agreement is
00304  * hereby granted, provided that the above copyright notice, the following
00305  * two paragraphs and the authors appear in all copies of this software.
00306  *
00307  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
00308  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
00309  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
00310  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00311  *
00312  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
00313  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
00314  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
00315  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
00316  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
00317  *
00318  * Please maintain this header in its entirety when copying/modifying
00319  * these files.
00320  *
00321  *
00322  */
00323 
00324 
00325 
00326 
00327 #include "pic24_all.h"
00328 
00329 // Only include if this UART exists.
00330 #if (NUM_UART_MODS >= 2)
00331 
00332 
00333 // Documentation for this file. If the \file tag is not present,
00334 // this file will not be documented.
00335 // Note: place this comment below the #if NUM_UART_MODS so Doxygen
00336 // will only see it once.
00349 /*********************************
00350  * Function private to this file *
00351  *********************************/
00352 
00353 
00354 
00355 
00356 
00357 
00358 /*********************************************************
00359  * Public functions intended to be called by other files *
00360  *********************************************************/
00365 void checkRxErrorUART2(void) {
00366   uint8 u8_c;
00367 //check for errors, reset if detected.
00368   if (U2STAbits.PERR) {
00369     u8_c = U2RXREG; //clear error
00370     reportError("UART2 parity error\n");
00371   }
00372   if (U2STAbits.FERR) {
00373     u8_c = U2RXREG; //clear error
00374     reportError("UART2 framing error\n");
00375   }
00376   if (U2STAbits.OERR) {
00377     U2STAbits.OERR = 0; //clear error
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; //wrap if needed
00411   while (u16_tmp == u16_txFifo2Tail)
00412     doHeartbeat();
00413 
00414   au8_txFifo2[u16_tmp] = u8_c; //write to buffer
00415   u16_txFifo2Head = u16_tmp;    //update head
00416   _U2TXIE = 1;                 //enable interrupt
00417 }
00418 
00419 void _ISR _U2TXInterrupt (void) {
00420   if (u16_txFifo2Head == u16_txFifo2Tail) {
00421     //empty TX buffer, disable the interrupt, do not clear the flag
00422     _U2TXIE = 0;
00423   } else {
00424     //at least one free spot in the TX buffer!
00425     u16_txFifo2Tail++;     //increment tail pointer
00426     if (u16_txFifo2Tail == UART2_TX_FIFO_SIZE)
00427       u16_txFifo2Tail = 0; //wrap if needed
00428     _U2TXIF = 0;   //clear the interrupt flag
00429     //transfer character from software buffer to transmit buffer
00430     U2TXREG =  au8_txFifo2[u16_txFifo2Tail];
00431   }
00432 }
00433 
00434 
00435 #else
00436 
00440 void outChar2(uint8 u8_c) {
00441   //wait for transmit buffer to be empty
00442   while (IS_TRANSMIT_BUFFER_FULL_UART2())
00443     doHeartbeat();
00444   U2TXREG = u8_c;
00445 }
00446 #endif
00447 
00448 #ifdef UART2_RX_INTERRUPT
00449 //Interrupt driven RX
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; //wrap
00478   return au8_rxFifo2[u16_rxFifo2Tail];  //return the character
00479 }
00480 
00481 void _ISR _U2RXInterrupt (void) {
00482   int8 u8_c;
00483 
00484   _U2RXIF = 0;          //clear the UART RX interrupt bit
00485   checkRxErrorUART2();
00486   u8_c = U2RXREG;       //read character
00487   u16_rxFifo2Head++;     //increment head pointer
00488   if (u16_rxFifo2Head == UART2_RX_FIFO_SIZE)
00489     u16_rxFifo2Head = 0; //wrap if needed
00490   if (u16_rxFifo2Head == u16_rxFifo2Tail) {
00491     //FIFO overrun!, report error
00492     reportError("UART2 RX Interrupt FIFO overrun!");
00493   }
00494   au8_rxFifo2[u16_rxFifo2Head] = u8_c;   //place in buffer
00495 }
00496 
00497 #else
00498 
00501 uint8 isCharReady2(void) {
00502   return(IS_CHAR_READY_UART2());
00503 }
00504 
00509 uint8 inChar2(void) {
00510   //do heartbeat while waiting for character.
00511   // Use a do-while to insure error checks
00512   // are always run.
00513   while (!IS_CHAR_READY_UART2())
00514     doHeartbeat();
00515   checkRxErrorUART2();
00516   return U2RXREG;  //read the receive register
00517 }
00518 #endif
00519 
00520 
00529 void configUART2(uint32 u32_baudRate) {
00530   /*************************  UART config ********************/
00531   // NOTE: the following pin mappings are (for simplicity)
00532   // identical for UARTS 1-4. See comments in the #warning
00533   // statements below for more information.
00534 #if defined(EXPLORER16_100P)
00535 //nothing to do, pins mapped to fixed ports
00536 #elif (2 == 1)             //change pin mappings for your device
00537   CONFIG_RP10_AS_DIG_PIN();                //RX RP pin must be digital
00538   CONFIG_U2RX_TO_RP(10);                 //U2RX <- RP10
00539   CONFIG_RP11_AS_DIG_PIN();                //TX RP pin must be digital
00540   CONFIG_U2TX_TO_RP(11);                 //U2TX -> RP11
00541   DISABLE_U2TX_ANALOG();                 //turn off any analog functionality on pin (may be needed if pin is hardmapped and RPx does not exist)
00542   DISABLE_U2RX_ANALOG();                 //turn off any analog functionality on pin (may be needed if pin is hardmapped and RPx does not e
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   //UART macros defined in "pic24_uart.h"
00558   CONFIG_BAUDRATE_UART2(u32_baudRate);   //baud rate
00559   CONFIG_PDSEL_UART2(UXMODE_PDSEL_8DATA_NOPARITY);        // 8-bit data, no parity
00560   CONFIG_STOPBITS_UART2(1);            // 1 Stop bit
00561 #ifdef UART2_RX_INTERRUPT
00562   _U2RXIF = 0;              //clear the flag
00563   _U2RXIP = UART2_RX_INTERRUPT_PRIORITY;  //choose a priority
00564   _U2RXIE = 1;              //enable the interrupt
00565 #endif
00566 #ifdef UART2_TX_INTERRUPT
00567   //do not clear the U2TXIF flag!
00568   _U2TXIP = UART2_TX_INTERRUPT_PRIORITY;  //choose a priority
00569   //do not enable the interrupt until we try to write to the UART
00570 #endif
00571   ENABLE_UART2();                        //enable the UART
00572 }
00573 
00574 #endif // #if (NUM_UARTS >= 2)
00575 
00576 
00577 
00578 
00579 /*
00580  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
00581  * All rights reserved.
00582  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
00583  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
00584  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
00585  *
00586  * Permission to use, copy, modify, and distribute this software and its
00587  * documentation for any purpose, without fee, and without written agreement is
00588  * hereby granted, provided that the above copyright notice, the following
00589  * two paragraphs and the authors appear in all copies of this software.
00590  *
00591  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
00592  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
00593  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
00594  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00595  *
00596  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
00597  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
00598  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
00599  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
00600  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
00601  *
00602  * Please maintain this header in its entirety when copying/modifying
00603  * these files.
00604  *
00605  *
00606  */
00607 
00608 
00609 
00610 
00611 #include "pic24_all.h"
00612 
00613 // Only include if this UART exists.
00614 #if (NUM_UART_MODS >= 3)
00615 
00616 
00617 // Documentation for this file. If the \file tag is not present,
00618 // this file will not be documented.
00619 // Note: place this comment below the #if NUM_UART_MODS so Doxygen
00620 // will only see it once.
00633 /*********************************
00634  * Function private to this file *
00635  *********************************/
00636 
00637 
00638 
00639 
00640 
00641 
00642 /*********************************************************
00643  * Public functions intended to be called by other files *
00644  *********************************************************/
00649 void checkRxErrorUART3(void) {
00650   uint8 u8_c;
00651 //check for errors, reset if detected.
00652   if (U3STAbits.PERR) {
00653     u8_c = U3RXREG; //clear error
00654     reportError("UART3 parity error\n");
00655   }
00656   if (U3STAbits.FERR) {
00657     u8_c = U3RXREG; //clear error
00658     reportError("UART3 framing error\n");
00659   }
00660   if (U3STAbits.OERR) {
00661     U3STAbits.OERR = 0; //clear error
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; //wrap if needed
00695   while (u16_tmp == u16_txFifo3Tail)
00696     doHeartbeat();
00697 
00698   au8_txFifo3[u16_tmp] = u8_c; //write to buffer
00699   u16_txFifo3Head = u16_tmp;    //update head
00700   _U3TXIE = 1;                 //enable interrupt
00701 }
00702 
00703 void _ISR _U3TXInterrupt (void) {
00704   if (u16_txFifo3Head == u16_txFifo3Tail) {
00705     //empty TX buffer, disable the interrupt, do not clear the flag
00706     _U3TXIE = 0;
00707   } else {
00708     //at least one free spot in the TX buffer!
00709     u16_txFifo3Tail++;     //increment tail pointer
00710     if (u16_txFifo3Tail == UART3_TX_FIFO_SIZE)
00711       u16_txFifo3Tail = 0; //wrap if needed
00712     _U3TXIF = 0;   //clear the interrupt flag
00713     //transfer character from software buffer to transmit buffer
00714     U3TXREG =  au8_txFifo3[u16_txFifo3Tail];
00715   }
00716 }
00717 
00718 
00719 #else
00720 
00724 void outChar3(uint8 u8_c) {
00725   //wait for transmit buffer to be empty
00726   while (IS_TRANSMIT_BUFFER_FULL_UART3())
00727     doHeartbeat();
00728   U3TXREG = u8_c;
00729 }
00730 #endif
00731 
00732 #ifdef UART3_RX_INTERRUPT
00733 //Interrupt driven RX
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; //wrap
00762   return au8_rxFifo3[u16_rxFifo3Tail];  //return the character
00763 }
00764 
00765 void _ISR _U3RXInterrupt (void) {
00766   int8 u8_c;
00767 
00768   _U3RXIF = 0;          //clear the UART RX interrupt bit
00769   checkRxErrorUART3();
00770   u8_c = U3RXREG;       //read character
00771   u16_rxFifo3Head++;     //increment head pointer
00772   if (u16_rxFifo3Head == UART3_RX_FIFO_SIZE)
00773     u16_rxFifo3Head = 0; //wrap if needed
00774   if (u16_rxFifo3Head == u16_rxFifo3Tail) {
00775     //FIFO overrun!, report error
00776     reportError("UART3 RX Interrupt FIFO overrun!");
00777   }
00778   au8_rxFifo3[u16_rxFifo3Head] = u8_c;   //place in buffer
00779 }
00780 
00781 #else
00782 
00785 uint8 isCharReady3(void) {
00786   return(IS_CHAR_READY_UART3());
00787 }
00788 
00793 uint8 inChar3(void) {
00794   //do heartbeat while waiting for character.
00795   // Use a do-while to insure error checks
00796   // are always run.
00797   while (!IS_CHAR_READY_UART3())
00798     doHeartbeat();
00799   checkRxErrorUART3();
00800   return U3RXREG;  //read the receive register
00801 }
00802 #endif
00803 
00804 
00813 void configUART3(uint32 u32_baudRate) {
00814   /*************************  UART config ********************/
00815   // NOTE: the following pin mappings are (for simplicity)
00816   // identical for UARTS 1-4. See comments in the #warning
00817   // statements below for more information.
00818 #if defined(EXPLORER16_100P)
00819 //nothing to do, pins mapped to fixed ports
00820 #elif (3 == 1)             //change pin mappings for your device
00821   CONFIG_RP10_AS_DIG_PIN();                //RX RP pin must be digital
00822   CONFIG_U3RX_TO_RP(10);                 //U3RX <- RP10
00823   CONFIG_RP11_AS_DIG_PIN();                //TX RP pin must be digital
00824   CONFIG_U3TX_TO_RP(11);                 //U3TX -> RP11
00825   DISABLE_U3TX_ANALOG();                 //turn off any analog functionality on pin (may be needed if pin is hardmapped and RPx does not exist)
00826   DISABLE_U3RX_ANALOG();                 //turn off any analog functionality on pin (may be needed if pin is hardmapped and RPx does not e
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   //UART macros defined in "pic24_uart.h"
00842   CONFIG_BAUDRATE_UART3(u32_baudRate);   //baud rate
00843   CONFIG_PDSEL_UART3(UXMODE_PDSEL_8DATA_NOPARITY);        // 8-bit data, no parity
00844   CONFIG_STOPBITS_UART3(1);            // 1 Stop bit
00845 #ifdef UART3_RX_INTERRUPT
00846   _U3RXIF = 0;              //clear the flag
00847   _U3RXIP = UART3_RX_INTERRUPT_PRIORITY;  //choose a priority
00848   _U3RXIE = 1;              //enable the interrupt
00849 #endif
00850 #ifdef UART3_TX_INTERRUPT
00851   //do not clear the U3TXIF flag!
00852   _U3TXIP = UART3_TX_INTERRUPT_PRIORITY;  //choose a priority
00853   //do not enable the interrupt until we try to write to the UART
00854 #endif
00855   ENABLE_UART3();                        //enable the UART
00856 }
00857 
00858 #endif // #if (NUM_UARTS >= 3)
00859 
00860 
00861 
00862 
00863 /*
00864  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
00865  * All rights reserved.
00866  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
00867  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
00868  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
00869  *
00870  * Permission to use, copy, modify, and distribute this software and its
00871  * documentation for any purpose, without fee, and without written agreement is
00872  * hereby granted, provided that the above copyright notice, the following
00873  * two paragraphs and the authors appear in all copies of this software.
00874  *
00875  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
00876  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
00877  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
00878  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00879  *
00880  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
00881  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
00882  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
00883  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
00884  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
00885  *
00886  * Please maintain this header in its entirety when copying/modifying
00887  * these files.
00888  *
00889  *
00890  */
00891 
00892 
00893 
00894 
00895 #include "pic24_all.h"
00896 
00897 // Only include if this UART exists.
00898 #if (NUM_UART_MODS >= 4)
00899 
00900 
00901 // Documentation for this file. If the \file tag is not present,
00902 // this file will not be documented.
00903 // Note: place this comment below the #if NUM_UART_MODS so Doxygen
00904 // will only see it once.
00917 /*********************************
00918  * Function private to this file *
00919  *********************************/
00920 
00921 
00922 
00923 
00924 
00925 
00926 /*********************************************************
00927  * Public functions intended to be called by other files *
00928  *********************************************************/
00933 void checkRxErrorUART4(void) {
00934   uint8 u8_c;
00935 //check for errors, reset if detected.
00936   if (U4STAbits.PERR) {
00937     u8_c = U4RXREG; //clear error
00938     reportError("UART4 parity error\n");
00939   }
00940   if (U4STAbits.FERR) {
00941     u8_c = U4RXREG; //clear error
00942     reportError("UART4 framing error\n");
00943   }
00944   if (U4STAbits.OERR) {
00945     U4STAbits.OERR = 0; //clear error
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; //wrap if needed
00979   while (u16_tmp == u16_txFifo4Tail)
00980     doHeartbeat();
00981 
00982   au8_txFifo4[u16_tmp] = u8_c; //write to buffer
00983   u16_txFifo4Head = u16_tmp;    //update head
00984   _U4TXIE = 1;                 //enable interrupt
00985 }
00986 
00987 void _ISR _U4TXInterrupt (void) {
00988   if (u16_txFifo4Head == u16_txFifo4Tail) {
00989     //empty TX buffer, disable the interrupt, do not clear the flag
00990     _U4TXIE = 0;
00991   } else {
00992     //at least one free spot in the TX buffer!
00993     u16_txFifo4Tail++;     //increment tail pointer
00994     if (u16_txFifo4Tail == UART4_TX_FIFO_SIZE)
00995       u16_txFifo4Tail = 0; //wrap if needed
00996     _U4TXIF = 0;   //clear the interrupt flag
00997     //transfer character from software buffer to transmit buffer
00998     U4TXREG =  au8_txFifo4[u16_txFifo4Tail];
00999   }
01000 }
01001 
01002 
01003 #else
01004 
01008 void outChar4(uint8 u8_c) {
01009   //wait for transmit buffer to be empty
01010   while (IS_TRANSMIT_BUFFER_FULL_UART4())
01011     doHeartbeat();
01012   U4TXREG = u8_c;
01013 }
01014 #endif
01015 
01016 #ifdef UART4_RX_INTERRUPT
01017 //Interrupt driven RX
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; //wrap
01046   return au8_rxFifo4[u16_rxFifo4Tail];  //return the character
01047 }
01048 
01049 void _ISR _U4RXInterrupt (void) {
01050   int8 u8_c;
01051 
01052   _U4RXIF = 0;          //clear the UART RX interrupt bit
01053   checkRxErrorUART4();
01054   u8_c = U4RXREG;       //read character
01055   u16_rxFifo4Head++;     //increment head pointer
01056   if (u16_rxFifo4Head == UART4_RX_FIFO_SIZE)
01057     u16_rxFifo4Head = 0; //wrap if needed
01058   if (u16_rxFifo4Head == u16_rxFifo4Tail) {
01059     //FIFO overrun!, report error
01060     reportError("UART4 RX Interrupt FIFO overrun!");
01061   }
01062   au8_rxFifo4[u16_rxFifo4Head] = u8_c;   //place in buffer
01063 }
01064 
01065 #else
01066 
01069 uint8 isCharReady4(void) {
01070   return(IS_CHAR_READY_UART4());
01071 }
01072 
01077 uint8 inChar4(void) {
01078   //do heartbeat while waiting for character.
01079   // Use a do-while to insure error checks
01080   // are always run.
01081   while (!IS_CHAR_READY_UART4())
01082     doHeartbeat();
01083   checkRxErrorUART4();
01084   return U4RXREG;  //read the receive register
01085 }
01086 #endif
01087 
01088 
01097 void configUART4(uint32 u32_baudRate) {
01098   /*************************  UART config ********************/
01099   // NOTE: the following pin mappings are (for simplicity)
01100   // identical for UARTS 1-4. See comments in the #warning
01101   // statements below for more information.
01102 #if defined(EXPLORER16_100P)
01103 //nothing to do, pins mapped to fixed ports
01104 #elif (4 == 1)             //change pin mappings for your device
01105   CONFIG_RP10_AS_DIG_PIN();                //RX RP pin must be digital
01106   CONFIG_U4RX_TO_RP(10);                 //U4RX <- RP10
01107   CONFIG_RP11_AS_DIG_PIN();                //TX RP pin must be digital
01108   CONFIG_U4TX_TO_RP(11);                 //U4TX -> RP11
01109   DISABLE_U4TX_ANALOG();                 //turn off any analog functionality on pin (may be needed if pin is hardmapped and RPx does not exist)
01110   DISABLE_U4RX_ANALOG();                 //turn off any analog functionality on pin (may be needed if pin is hardmapped and RPx does not e
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   //UART macros defined in "pic24_uart.h"
01126   CONFIG_BAUDRATE_UART4(u32_baudRate);   //baud rate
01127   CONFIG_PDSEL_UART4(UXMODE_PDSEL_8DATA_NOPARITY);        // 8-bit data, no parity
01128   CONFIG_STOPBITS_UART4(1);            // 1 Stop bit
01129 #ifdef UART4_RX_INTERRUPT
01130   _U4RXIF = 0;              //clear the flag
01131   _U4RXIP = UART4_RX_INTERRUPT_PRIORITY;  //choose a priority
01132   _U4RXIE = 1;              //enable the interrupt
01133 #endif
01134 #ifdef UART4_TX_INTERRUPT
01135   //do not clear the U4TXIF flag!
01136   _U4TXIP = UART4_TX_INTERRUPT_PRIORITY;  //choose a priority
01137   //do not enable the interrupt until we try to write to the UART
01138 #endif
01139   ENABLE_UART4();                        //enable the UART
01140 }
01141 
01142 #endif // #if (NUM_UARTS >= 4)
01143 
01144 
01145 
01146 

Generated on Mon Oct 18 07:40:46 2010 for Python-on-a-chip by  doxygen 1.5.9