]> git.sur5r.net Git - freertos/blob - Demo/PIC18_MPLAB/serial/serial.c
Prepare for V5.3.0 release.
[freertos] / Demo / PIC18_MPLAB / serial / serial.c
1 /*\r
2         FreeRTOS.org V5.3.0 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify it\r
7         under the terms of the GNU General Public License (version 2) as published\r
8         by the Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS.org without being obliged to provide\r
11         the source code for any proprietary components.  Alternative commercial\r
12         license and support terms are also available upon request.  See the \r
13         licensing section of http://www.FreeRTOS.org for full details.\r
14 \r
15         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
16         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
17         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
18         more details.\r
19 \r
20         You should have received a copy of the GNU General Public License along\r
21         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59\r
22         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\r
29         * This is a concise, step by step, 'hands on' guide that describes both   *\r
30         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
31         * explains numerous examples that are written using the FreeRTOS API.     *\r
32         * Full source code for all the examples is provided in an accompanying    *\r
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 /*\r
53 Changes from V1.2.5\r
54 \r
55         +  Clear overrun errors in the Rx ISR.  Overrun errors prevent any further\r
56            characters being received.\r
57 \r
58 Changes from V2.0.0\r
59 \r
60         + Use portTickType in place of unsigned pdLONG for delay periods.\r
61         + cQueueReieveFromISR() used in place of xQueueReceive() in ISR.\r
62 */\r
63 \r
64 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */\r
65 \r
66 /* Scheduler header files. */\r
67 #include "FreeRTOS.h"\r
68 #include "task.h"\r
69 #include "serial.h"\r
70 #include "queue.h"\r
71 \r
72 /*\r
73  * Prototypes for ISR's.  The PIC architecture means that these functions\r
74  * have to be called from port.c.  The prototypes are not however included\r
75  * in the header as the header is common to all ports.\r
76  */\r
77 void vSerialTxISR( void );\r
78 void vSerialRxISR( void );\r
79 \r
80 /* Hardware pin definitions. */\r
81 #define serTX_PIN       TRISCbits.TRISC6\r
82 #define serRX_PIN       TRISCbits.TRISC7\r
83 \r
84 /* Bit/register definitions. */\r
85 #define serINPUT                                ( 1 )\r
86 #define serOUTPUT                               ( 0 )\r
87 #define serTX_ENABLE                    ( ( unsigned portSHORT ) 1 )\r
88 #define serRX_ENABLE                    ( ( unsigned portSHORT ) 1 )\r
89 #define serHIGH_SPEED                   ( ( unsigned portSHORT ) 1 )\r
90 #define serCONTINUOUS_RX                ( ( unsigned portSHORT ) 1 )\r
91 #define serCLEAR_OVERRUN                ( ( unsigned portSHORT ) 0 )\r
92 #define serINTERRUPT_ENABLED    ( ( unsigned portSHORT ) 1 )\r
93 #define serINTERRUPT_DISABLED   ( ( unsigned portSHORT ) 0 )\r
94 \r
95 /* All ISR's use the PIC18 low priority interrupt. */\r
96 #define                                                 serLOW_PRIORITY ( 0 )\r
97 \r
98 /*-----------------------------------------------------------*/\r
99 \r
100 /* Queues to interface between comms API and interrupt routines. */\r
101 static xQueueHandle xRxedChars; \r
102 static xQueueHandle xCharsForTx;\r
103 \r
104 /*-----------------------------------------------------------*/\r
105 \r
106 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
107 {\r
108 unsigned portLONG ulBaud;\r
109 \r
110         /* Calculate the baud rate generator constant.\r
111         SPBRG = ( (FOSC / Desired Baud Rate) / 16 ) - 1 */\r
112         ulBaud = configCPU_CLOCK_HZ / ulWantedBaud;\r
113         ulBaud /= ( unsigned portLONG ) 16;\r
114         ulBaud -= ( unsigned portLONG ) 1;\r
115 \r
116         /* Create the queues used by the ISR's to interface to tasks. */\r
117         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );\r
118         xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );\r
119 \r
120         portENTER_CRITICAL();\r
121         {\r
122                 /* Start with config registers cleared, so we can just set the wanted\r
123                 bits. */\r
124                 TXSTA = ( unsigned portSHORT ) 0;\r
125                 RCSTA = ( unsigned portSHORT ) 0;\r
126 \r
127                 /* Set the baud rate generator using the above calculated constant. */\r
128                 SPBRG = ( unsigned portCHAR ) ulBaud;\r
129 \r
130                 /* Setup the IO pins to enable the USART IO. */\r
131                 serTX_PIN = serOUTPUT;\r
132                 serRX_PIN = serINPUT;\r
133 \r
134                 /* Set the serial interrupts to use the same priority as the tick. */\r
135                 IPR1bits.TXIP = serLOW_PRIORITY;\r
136                 IPR1bits.RCIP = serLOW_PRIORITY;\r
137 \r
138                 /* Setup Tx configuration. */\r
139                 TXSTAbits.BRGH = serHIGH_SPEED;\r
140                 TXSTAbits.TXEN = serTX_ENABLE;\r
141 \r
142                 /* Setup Rx configuration. */\r
143                 RCSTAbits.SPEN = serRX_ENABLE;\r
144                 RCSTAbits.CREN = serCONTINUOUS_RX;\r
145 \r
146                 /* Enable the Rx interrupt now, the Tx interrupt will get enabled when\r
147                 we have data to send. */\r
148                 PIE1bits.RCIE = serINTERRUPT_ENABLED;\r
149         }\r
150         portEXIT_CRITICAL();\r
151 \r
152         /* Unlike other ports, this serial code does not allow for more than one\r
153         com port.  We therefore don't return a pointer to a port structure and \r
154         can     instead just return NULL. */\r
155         return NULL;\r
156 }\r
157 /*-----------------------------------------------------------*/\r
158 \r
159 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength )\r
160 {\r
161         /* This is not implemented in this port.\r
162         Use xSerialPortInitMinimal() instead. */\r
163 }\r
164 /*-----------------------------------------------------------*/\r
165 \r
166 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
167 {\r
168         /* Get the next character from the buffer.  Return false if no characters\r
169         are available, or arrive before xBlockTime expires. */\r
170         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
171         {\r
172                 return pdTRUE;\r
173         }\r
174         else\r
175         {\r
176                 return pdFALSE;\r
177         }\r
178 }\r
179 /*-----------------------------------------------------------*/\r
180 \r
181 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
182 {\r
183         /* Return false if after the block time there is no room on the Tx queue. */\r
184         if( xQueueSend( xCharsForTx, ( const void * ) &cOutChar, xBlockTime ) != pdPASS )\r
185         {\r
186                 return pdFAIL;\r
187         }\r
188 \r
189         /* Turn interrupt on - ensure the compiler only generates a single \r
190         instruction for this. */\r
191         PIE1bits.TXIE = serINTERRUPT_ENABLED;\r
192 \r
193         return pdPASS;\r
194 }\r
195 /*-----------------------------------------------------------*/\r
196 \r
197 void vSerialClose( xComPortHandle xPort )\r
198 {\r
199         /* Not implemented for this port.\r
200         To implement, turn off the interrupts and delete the memory\r
201         allocated to the queues. */\r
202 }\r
203 /*-----------------------------------------------------------*/\r
204 \r
205 #pragma interruptlow vSerialRxISR save=PRODH, PRODL, TABLAT, section(".tmpdata")\r
206 void vSerialRxISR( void )\r
207 {\r
208 portCHAR cChar;\r
209 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
210 \r
211         /* Get the character and post it on the queue of Rxed characters.\r
212         If the post causes a task to wake force a context switch as the woken task\r
213         may have a higher priority than the task we have interrupted. */\r
214         cChar = RCREG;\r
215 \r
216         /* Clear any overrun errors. */\r
217         if( RCSTAbits.OERR )\r
218         {\r
219                 RCSTAbits.CREN = serCLEAR_OVERRUN;\r
220                 RCSTAbits.CREN = serCONTINUOUS_RX;      \r
221         }\r
222 \r
223         xQueueSendFromISR( xRxedChars, ( const void * ) &cChar, &xHigherPriorityTaskWoken );\r
224 \r
225         if( xHigherPriorityTaskWoken )\r
226         {\r
227                 taskYIELD();\r
228         }\r
229 }\r
230 /*-----------------------------------------------------------*/\r
231 \r
232 #pragma interruptlow vSerialTxISR save=PRODH, PRODL, TABLAT, section(".tmpdata")\r
233 void vSerialTxISR( void )\r
234 {\r
235 portCHAR cChar, cTaskWoken = pdFALSE;\r
236 \r
237         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )\r
238         {\r
239                 /* Send the next character queued for Tx. */\r
240                 TXREG = cChar;\r
241         }\r
242         else\r
243         {\r
244                 /* Queue empty, nothing to send. */\r
245                 PIE1bits.TXIE = serINTERRUPT_DISABLED;\r
246         }\r
247 }\r
248 \r
249 \r
250 \r