/[H8]/trunk/docs/Microchip TCP_IP stack/TCPIP Stack/UART.c
ViewVC logotype

Annotation of /trunk/docs/Microchip TCP_IP stack/TCPIP Stack/UART.c

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20