| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 32 | kaklik | /********************************************************************* |
| 2 | * |
||
| 3 | * UART access routines for C18 and C30 |
||
| 4 | * |
||
| 5 | ********************************************************************* |
||
| 6 | * FileName: UART.c |
||
| 7 | * Dependencies: Hardware UART module |
||
| 8 | * Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F |
||
| 9 | * Compiler: Microchip C30 v3.12 or higher |
||
| 10 | * Microchip C18 v3.30 or higher |
||
| 11 | * HI-TECH PICC-18 PRO 9.63PL2 or higher |
||
| 12 | * Company: Microchip Technology, Inc. |
||
| 13 | * |
||
| 14 | * Software License Agreement |
||
| 15 | * |
||
| 16 | * Copyright (C) 2002-2009 Microchip Technology Inc. All rights |
||
| 17 | * reserved. |
||
| 18 | * |
||
| 19 | * Microchip licenses to you the right to use, modify, copy, and |
||
| 20 | * distribute: |
||
| 21 | * (i) the Software when embedded on a Microchip microcontroller or |
||
| 22 | * digital signal controller product ("Device") which is |
||
| 23 | * integrated into Licensee's product; or |
||
| 24 | * (ii) ONLY the Software driver source files ENC28J60.c, ENC28J60.h, |
||
| 25 | * ENCX24J600.c and ENCX24J600.h ported to a non-Microchip device |
||
| 26 | * used in conjunction with a Microchip ethernet controller for |
||
| 27 | * the sole purpose of interfacing with the ethernet controller. |
||
| 28 | * |
||
| 29 | * You should refer to the license agreement accompanying this |
||
| 30 | * Software for additional information regarding your rights and |
||
| 31 | * obligations. |
||
| 32 | * |
||
| 33 | * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT |
||
| 34 | * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT |
||
| 35 | * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A |
||
| 36 | * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
||
| 37 | * MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR |
||
| 38 | * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF |
||
| 39 | * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS |
||
| 40 | * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE |
||
| 41 | * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER |
||
| 42 | * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT |
||
| 43 | * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE. |
||
| 44 | * |
||
| 45 | * |
||
| 46 | * Author Date Comment |
||
| 47 | *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
| 48 | * Howard Schlunder 4/04/06 Copied from dsPIC30 libraries |
||
| 49 | * Howard Schlunder 6/16/06 Added PIC18 |
||
| 50 | ********************************************************************/ |
||
| 51 | #define __UART_C |
||
| 52 | |||
| 53 | #include "TCPIPConfig.h" |
||
| 54 | |||
| 55 | #if defined(STACK_USE_UART) |
||
| 56 | |||
| 57 | #include "TCPIP Stack/TCPIP.h" |
||
| 58 | |||
| 59 | |||
| 60 | BYTE ReadStringUART(BYTE *Dest, BYTE BufferLen) |
||
| 61 | { |
||
| 62 | BYTE c; |
||
| 63 | BYTE count = 0; |
||
| 64 | |||
| 65 | while(BufferLen--) |
||
| 66 | { |
||
| 67 | *Dest = '\0'; |
||
| 68 | |||
| 69 | while(!DataRdyUART()); |
||
| 70 | c = ReadUART(); |
||
| 71 | |||
| 72 | if(c == '\r' || c == '\n') |
||
| 73 | break; |
||
| 74 | |||
| 75 | count++; |
||
| 76 | *Dest++ = c; |
||
| 77 | } |
||
| 78 | |||
| 79 | return count; |
||
| 80 | } |
||
| 81 | |||
| 82 | |||
| 83 | |||
| 84 | #if defined(__18CXX) // PIC18 |
||
| 85 | char BusyUSART(void) |
||
| 86 | { |
||
| 87 | return !TXSTAbits.TRMT; |
||
| 88 | } |
||
| 89 | |||
| 90 | void CloseUSART(void) |
||
| 91 | { |
||
| 92 | RCSTA &= 0x4F; // Disable the receiver |
||
| 93 | TXSTAbits.TXEN = 0; // and transmitter |
||
| 94 | |||
| 95 | PIE1 &= 0xCF; // Disable both interrupts |
||
| 96 | } |
||
| 97 | |||
| 98 | char DataRdyUSART(void) |
||
| 99 | { |
||
| 100 | if(RCSTAbits.OERR) |
||
| 101 | { |
||
| 102 | RCSTAbits.CREN = 0; |
||
| 103 | RCSTAbits.CREN = 1; |
||
| 104 | } |
||
| 105 | return PIR1bits.RCIF; |
||
| 106 | } |
||
| 107 | |||
| 108 | char ReadUSART(void) |
||
| 109 | { |
||
| 110 | return RCREG; // Return the received data |
||
| 111 | } |
||
| 112 | |||
| 113 | void WriteUSART(char data) |
||
| 114 | { |
||
| 115 | TXREG = data; // Write the data byte to the USART |
||
| 116 | } |
||
| 117 | |||
| 118 | void getsUSART(char *buffer, unsigned char len) |
||
| 119 | { |
||
| 120 | char i; // Length counter |
||
| 121 | unsigned char data; |
||
| 122 | |||
| 123 | for(i=0;i<len;i++) // Only retrieve len characters |
||
| 124 | { |
||
| 125 | while(!DataRdyUSART());// Wait for data to be received |
||
| 126 | |||
| 127 | data = getcUART(); // Get a character from the USART |
||
| 128 | // and save in the string |
||
| 129 | *buffer = data; |
||
| 130 | buffer++; // Increment the string pointer |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | void putsUSART( char *data) |
||
| 135 | { |
||
| 136 | do |
||
| 137 | { // Transmit a byte |
||
| 138 | while(BusyUSART()); |
||
| 139 | putcUART(*data); |
||
| 140 | } while( *data++ ); |
||
| 141 | } |
||
| 142 | |||
| 143 | void putrsUSART(const rom char *data) |
||
| 144 | { |
||
| 145 | do |
||
| 146 | { // Transmit a byte |
||
| 147 | while(BusyUSART()); |
||
| 148 | putcUART(*data); |
||
| 149 | } while( *data++ ); |
||
| 150 | } |
||
| 151 | |||
| 152 | |||
| 153 | #elif defined(__C30__) // PIC24F, PIC24H, dsPIC30, dsPIC33 |
||
| 154 | |||
| 155 | /*************************************************************************** |
||
| 156 | * Function Name : putsUART2 * |
||
| 157 | * Description : This function puts the data string to be transmitted * |
||
| 158 | * into the transmit buffer (till NULL character) * |
||
| 159 | * Parameters : unsigned int * address of the string buffer to be * |
||
| 160 | * transmitted * |
||
| 161 | * Return Value : None * |
||
| 162 | ***************************************************************************/ |
||
| 163 | |||
| 164 | void putsUART2(unsigned int *buffer) |
||
| 165 | { |
||
| 166 | char * temp_ptr = (char *) buffer; |
||
| 167 | |||
| 168 | /* transmit till NULL character is encountered */ |
||
| 169 | |||
| 170 | if(U2MODEbits.PDSEL == 3) /* check if TX is 8bits or 9bits */ |
||
| 171 | { |
||
| 172 | while(*buffer != '\0') |
||
| 173 | { |
||
| 174 | while(U2STAbits.UTXBF); /* wait if the buffer is full */ |
||
| 175 | U2TXREG = *buffer++; /* transfer data word to TX reg */ |
||
| 176 | } |
||
| 177 | } |
||
| 178 | else |
||
| 179 | { |
||
| 180 | while(*temp_ptr != '\0') |
||
| 181 | { |
||
| 182 | while(U2STAbits.UTXBF); /* wait if the buffer is full */ |
||
| 183 | U2TXREG = *temp_ptr++; /* transfer data byte to TX reg */ |
||
| 184 | } |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | |||
| 189 | /****************************************************************************** |
||
| 190 | * Function Name : getsUART2 * |
||
| 191 | * Description : This function gets a string of data of specified length * |
||
| 192 | * if available in the UxRXREG buffer into the buffer * |
||
| 193 | * specified. * |
||
| 194 | * Parameters : unsigned int length the length expected * |
||
| 195 | * unsigned int *buffer the received data to be * |
||
| 196 | * recorded to this array * |
||
| 197 | * unsigned int uart_data_wait timeout value * |
||
| 198 | * Return Value : unsigned int number of data bytes yet to be received * |
||
| 199 | ******************************************************************************/ |
||
| 200 | |||
| 201 | unsigned int getsUART2(unsigned int length,unsigned int *buffer, |
||
| 202 | unsigned int uart_data_wait) |
||
| 203 | |||
| 204 | { |
||
| 205 | unsigned int wait = 0; |
||
| 206 | char *temp_ptr = (char *) buffer; |
||
| 207 | |||
| 208 | while(length) /* read till length is 0 */ |
||
| 209 | { |
||
| 210 | while(!DataRdyUART2()) |
||
| 211 | { |
||
| 212 | if(wait < uart_data_wait) |
||
| 213 | wait++ ; /*wait for more data */ |
||
| 214 | else |
||
| 215 | return(length); /*Time out- Return words/bytes to be read */ |
||
| 216 | } |
||
| 217 | wait=0; |
||
| 218 | if(U2MODEbits.PDSEL == 3) /* check if TX/RX is 8bits or 9bits */ |
||
| 219 | *buffer++ = U2RXREG; /* data word from HW buffer to SW buffer */ |
||
| 220 | else |
||
| 221 | *temp_ptr++ = U2RXREG & 0xFF; /* data byte from HW buffer to SW buffer */ |
||
| 222 | |||
| 223 | length--; |
||
| 224 | } |
||
| 225 | |||
| 226 | return(length); /* number of data yet to be received i.e.,0 */ |
||
| 227 | } |
||
| 228 | |||
| 229 | |||
| 230 | /********************************************************************* |
||
| 231 | * Function Name : DataRdyUart2 * |
||
| 232 | * Description : This function checks whether there is any data * |
||
| 233 | * that can be read from the input buffer, by * |
||
| 234 | * checking URXDA bit * |
||
| 235 | * Parameters : None * |
||
| 236 | * Return Value : char if any data available in buffer * |
||
| 237 | *********************************************************************/ |
||
| 238 | |||
| 239 | char DataRdyUART2(void) |
||
| 240 | { |
||
| 241 | return(U2STAbits.URXDA); |
||
| 242 | } |
||
| 243 | |||
| 244 | |||
| 245 | /************************************************************************* |
||
| 246 | * Function Name : BusyUART2 * |
||
| 247 | * Description : This returns status whether the transmission * |
||
| 248 | * is in progress or not, by checking Status bit TRMT * |
||
| 249 | * Parameters : None * |
||
| 250 | * Return Value : char info whether transmission is in progress * |
||
| 251 | *************************************************************************/ |
||
| 252 | |||
| 253 | char BusyUART2(void) |
||
| 254 | { |
||
| 255 | return(!U2STAbits.TRMT); |
||
| 256 | } |
||
| 257 | |||
| 258 | |||
| 259 | /*************************************************************************** |
||
| 260 | * Function Name : ReadUART2 * |
||
| 261 | * Description : This function returns the contents of UxRXREG buffer * |
||
| 262 | * Parameters : None * |
||
| 263 | * Return Value : unsigned int value from UxRXREG receive buffer * |
||
| 264 | ***************************************************************************/ |
||
| 265 | |||
| 266 | unsigned int ReadUART2(void) |
||
| 267 | { |
||
| 268 | if(U2MODEbits.PDSEL == 3) |
||
| 269 | return (U2RXREG); |
||
| 270 | else |
||
| 271 | return (U2RXREG & 0xFF); |
||
| 272 | } |
||
| 273 | |||
| 274 | |||
| 275 | /********************************************************************* |
||
| 276 | * Function Name : WriteUART2 * |
||
| 277 | * Description : This function writes data into the UxTXREG, * |
||
| 278 | * Parameters : unsigned int data the data to be written * |
||
| 279 | * Return Value : None * |
||
| 280 | *********************************************************************/ |
||
| 281 | |||
| 282 | void WriteUART2(unsigned int data) |
||
| 283 | { |
||
| 284 | if(U2MODEbits.PDSEL == 3) |
||
| 285 | U2TXREG = data; |
||
| 286 | else |
||
| 287 | U2TXREG = data & 0xFF; |
||
| 288 | } |
||
| 289 | |||
| 290 | #endif |
||
| 291 | |||
| 292 | |||
| 293 | #endif //STACK_USE_UART |
Powered by WebSVN v2.8.3