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 00037 // Documentation for this file. If the \file tag isn't present, 00038 // this file won't be documented. 00043 /*** I N C L U D E S *************************************************/ 00044 #include "esos_pic24_rs232.h" 00045 00046 /*** G L O B A L S *************************************************/ 00047 00048 /*** T H E C O D E *************************************************/ 00049 #define __ESOS_HW_SIGNAL_START_TX() _U1TXIE = 1 00050 #define __ESOS_HW_SIGNAL_STOP_TX() _U1TXIE = 0 00051 00052 /********************************************************* 00053 * Public functions intended to be called by other files * 00054 *********************************************************/ 00055 inline void __esos_hw_signal_start_tx(void) { 00056 __ESOS_HW_SIGNAL_START_TX(); 00057 } 00058 00059 inline void __esos_hw_signal_stop_tx(void) { 00060 __ESOS_HW_SIGNAL_STOP_TX(); 00061 } 00062 00063 00064 00065 /* ########################################################################### */ 00066 void _ISRFAST _U1TXInterrupt (void) { 00067 if (__st_TxBuffer.u16_Head == __st_TxBuffer.u16_Tail) { 00068 //empty TX buffer, disable the interrupt, do not clear the flag 00069 __ESOS_HW_SIGNAL_STOP_TX(); 00070 } else { 00071 //at least one free spot in the TX buffer! 00072 __st_TxBuffer.u16_Tail++; //increment tail pointer 00073 if (__st_TxBuffer.u16_Tail == ESOS_SERIAL_IN_EP_SIZE) 00074 __st_TxBuffer.u16_Tail = 0; //wrap if needed 00075 _U1TXIF = 0; //clear the interrupt flag 00076 //transfer character from software buffer to transmit buffer 00077 U1TXREG = __st_TxBuffer.pau8_Data[__st_TxBuffer.u16_Tail]; 00078 } 00079 } 00080 00081 void _ISRFAST _U1RXInterrupt (void) { 00082 int8 u8_c; 00083 00084 _U1RXIF = 0; //clear the UART RX interrupt bit 00085 00086 // This fcn is found in pic24_uart.c which we've replaced with 00087 // our own ESOS versions. 00088 u8_c = U1RXREG; //read character 00089 __st_RxBuffer.u16_Head++; //increment head pointer 00090 if (__st_RxBuffer.u16_Head == ESOS_SERIAL_OUT_EP_SIZE) 00091 __st_RxBuffer.u16_Head = 0; //wrap if needed 00092 00093 __st_RxBuffer.pau8_Data[__st_RxBuffer.u16_Head] = u8_c; //place in buffer 00094 } 00095 00104 void configUART1(uint32 u32_baudRate) { 00105 /************************* UART config ********************/ 00106 //Pin mapping macros in pic24_ports.h 00107 00108 CONFIG_RP10_AS_DIG_PIN(); //RX RP pin must be digital. What about TX pin? 00109 CONFIG_U1RX_TO_RP(10); //U1RX <- RP10 00110 CONFIG_U1TX_TO_RP(11); //U1TX -> RP11 00111 00112 //UART macros defined in "pic24_uart.h" 00113 CONFIG_BAUDRATE_UART1(u32_baudRate); //baud rate 00114 CONFIG_PDSEL_UART1(UXMODE_PDSEL_8DATA_NOPARITY); // 8-bit data, no parity 00115 CONFIG_STOPBITS_UART1(1); // 1 Stop bit 00116 00117 // ESOS comm system uses UART1_RX IRQs 00118 _U1RXIF = 0; //clear the flag 00119 _U1RXIP = __ESOS_UART1_RX_INTERRUPT_PRIORITY; //choose a priority 00120 _U1RXIE = 1; //enable the interrupt 00121 00122 // ESOS comm system uses UART1_RX IRQs 00123 // do not clear the U1TXIF flag! 00124 _U1RXIP = __ESOS_UART1_TX_INTERRUPT_PRIORITY; //choose a priority 00125 //do not enable the interrupt until we try to write to the UART 00126 00127 ENABLE_UART1(); //enable the UART 00128 } 00129 00130 /* ########################################################################### */ 00131 00132 00133 /****************************************************************************** 00134 * Function: void _esos_hw_InitSerialUart( void ) 00135 * 00136 * PreCondition: None 00137 * 00138 * Input: None 00139 * 00140 * Output: ptr to ESOS_COMM_BUFF_DSC structure with initialized ptrs 00141 * 00142 * Side Effects: Turns on USART hardware 00143 * 00144 *****************************************************************************/ 00145 void __esos_hw_InitCommSystem(void) { 00146 // use the MSSTATE PIC24 routines to init the RS232 comm subsystem 00147 // 8N1 @ 56k7 baud (DEFAULT_BAUDRATE) for now 00148 configUART1(DEFAULT_BAUDRATE) ; 00149 00150 } // end __esos_hw_InitCommSystem() 00151 00152 00153 /****************************************************************************** 00154 * Function: uint8 esos_GetCommSystemMaxInDataLen(void) 00155 * 00156 * PreCondition: None. 00157 * 00158 * Input: None 00159 * 00160 * Output: the maximum number of uint8s that the comm system will 00161 * receive in a single buffer transfer from the host -- OR -- 00162 * in the case of single uint8 xfers (like RS232), the maximum 00163 * number of uint8s that can be RX-ed before the buffers 00164 * overflow 00165 * 00166 * Side Effects: None 00167 * 00168 * Overview: A way for a run-time determination of the maximum buffer 00169 * size that the user can can expect. This number is 00170 * actually hard-coded in the USB CDC header file, but this 00171 * method will allow the user code to be more generic, if 00172 * it chooses to be. 00173 * 00174 *****************************************************************************/ 00175 uint8 esos_GetCommSystemMaxInDataLen(void) { 00176 return ESOS_SERIAL_OUT_EP_SIZE; 00177 } //end esos_GetCommSystemMaxInDataLen() 00178 00179 /****************************************************************************** 00180 * Function: uint8 esos_GetCommSystemMaxOutDataLen(void) 00181 * 00182 * PreCondition: None. 00183 * 00184 * Input: None 00185 * 00186 * Output: the maximum number of uint8s that the comm system will 00187 * transfer back to the host in a single buffer -- OR -- 00188 * in the case of singe uint8 xfers (like RS232), the maximum 00189 * number of uint8s in the output buffer before overflow 00190 * 00191 * Side Effects: None 00192 * 00193 * Overview: A way for a run-time determination of the maximum buffer 00194 * size that the user can can send efficiently. The USB system 00195 * will send a bigger buffer than getUSBCdcTxMax() size, but 00196 * will do so in several smaller getUSBCdcTxMax()-sized chunks. 00197 * 00198 * This number is actually hard-coded in the USB CDC header file, 00199 * but this method will allow the user code to be more generic, 00200 * if it chooses to be. 00201 * 00202 *****************************************************************************/ 00203 uint8 esos_GetCommSystemMaxOutDataLen(void) { 00204 return ESOS_SERIAL_IN_EP_SIZE; 00205 } //end esos_GetCommSystemMaxOutDataLen() 00206 00207 /****************************************************************************** 00208 * Function: uint8 _esos_hw_GetUartVersion(void) 00209 * 00210 * PreCondition: None. 00211 * 00212 * Input: None 00213 * 00214 * Output: Return the version number of the MSU Bulk CDC driver firmware 00215 * currently running. 00216 * The most-significant bit denotes we're running USB 00217 * The most-significant nibble is the major revision number 00218 * The least-significant nibble is the minor revision number 00219 * 00220 * Side Effects: None 00221 * 00222 *****************************************************************************/ 00223 uint8 _esos_hw_GetSerialUartVersion(void) { 00224 return ESOS_COMM_SYS_SERIAL_REV; 00225 } //end _esos_hw_GetUartVersion() 00226