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