]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC18_MPLAB/serial/serial.c
0ab0f88b23bc0b04482b0c829477446961fd261c
[freertos] / FreeRTOS / Demo / PIC18_MPLAB / serial / serial.c
1 /*\r
2     FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /*\r
66 Changes from V1.2.5\r
67 \r
68         +  Clear overrun errors in the Rx ISR.  Overrun errors prevent any further\r
69            characters being received.\r
70 \r
71 Changes from V2.0.0\r
72 \r
73         + Use portTickType in place of unsigned pdLONG for delay periods.\r
74         + cQueueReieveFromISR() used in place of xQueueReceive() in ISR.\r
75 */\r
76 \r
77 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */\r
78 \r
79 /* Scheduler header files. */\r
80 #include "FreeRTOS.h"\r
81 #include "task.h"\r
82 #include "serial.h"\r
83 #include "queue.h"\r
84 \r
85 /*\r
86  * Prototypes for ISR's.  The PIC architecture means that these functions\r
87  * have to be called from port.c.  The prototypes are not however included\r
88  * in the header as the header is common to all ports.\r
89  */\r
90 void vSerialTxISR( void );\r
91 void vSerialRxISR( void );\r
92 \r
93 /* Hardware pin definitions. */\r
94 #define serTX_PIN       TRISCbits.TRISC6\r
95 #define serRX_PIN       TRISCbits.TRISC7\r
96 \r
97 /* Bit/register definitions. */\r
98 #define serINPUT                                ( 1 )\r
99 #define serOUTPUT                               ( 0 )\r
100 #define serTX_ENABLE                    ( ( unsigned short ) 1 )\r
101 #define serRX_ENABLE                    ( ( unsigned short ) 1 )\r
102 #define serHIGH_SPEED                   ( ( unsigned short ) 1 )\r
103 #define serCONTINUOUS_RX                ( ( unsigned short ) 1 )\r
104 #define serCLEAR_OVERRUN                ( ( unsigned short ) 0 )\r
105 #define serINTERRUPT_ENABLED    ( ( unsigned short ) 1 )\r
106 #define serINTERRUPT_DISABLED   ( ( unsigned short ) 0 )\r
107 \r
108 /* All ISR's use the PIC18 low priority interrupt. */\r
109 #define                                                 serLOW_PRIORITY ( 0 )\r
110 \r
111 /*-----------------------------------------------------------*/\r
112 \r
113 /* Queues to interface between comms API and interrupt routines. */\r
114 static xQueueHandle xRxedChars; \r
115 static xQueueHandle xCharsForTx;\r
116 \r
117 /*-----------------------------------------------------------*/\r
118 \r
119 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
120 {\r
121 unsigned long ulBaud;\r
122 \r
123         /* Calculate the baud rate generator constant.\r
124         SPBRG = ( (FOSC / Desired Baud Rate) / 16 ) - 1 */\r
125         ulBaud = configCPU_CLOCK_HZ / ulWantedBaud;\r
126         ulBaud /= ( unsigned long ) 16;\r
127         ulBaud -= ( unsigned long ) 1;\r
128 \r
129         /* Create the queues used by the ISR's to interface to tasks. */\r
130         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );\r
131         xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );\r
132 \r
133         portENTER_CRITICAL();\r
134         {\r
135                 /* Start with config registers cleared, so we can just set the wanted\r
136                 bits. */\r
137                 TXSTA = ( unsigned short ) 0;\r
138                 RCSTA = ( unsigned short ) 0;\r
139 \r
140                 /* Set the baud rate generator using the above calculated constant. */\r
141                 SPBRG = ( unsigned char ) ulBaud;\r
142 \r
143                 /* Setup the IO pins to enable the USART IO. */\r
144                 serTX_PIN = serOUTPUT;\r
145                 serRX_PIN = serINPUT;\r
146 \r
147                 /* Set the serial interrupts to use the same priority as the tick. */\r
148                 IPR1bits.TXIP = serLOW_PRIORITY;\r
149                 IPR1bits.RCIP = serLOW_PRIORITY;\r
150 \r
151                 /* Setup Tx configuration. */\r
152                 TXSTAbits.BRGH = serHIGH_SPEED;\r
153                 TXSTAbits.TXEN = serTX_ENABLE;\r
154 \r
155                 /* Setup Rx configuration. */\r
156                 RCSTAbits.SPEN = serRX_ENABLE;\r
157                 RCSTAbits.CREN = serCONTINUOUS_RX;\r
158 \r
159                 /* Enable the Rx interrupt now, the Tx interrupt will get enabled when\r
160                 we have data to send. */\r
161                 PIE1bits.RCIE = serINTERRUPT_ENABLED;\r
162         }\r
163         portEXIT_CRITICAL();\r
164 \r
165         /* Unlike other ports, this serial code does not allow for more than one\r
166         com port.  We therefore don't return a pointer to a port structure and \r
167         can     instead just return NULL. */\r
168         return NULL;\r
169 }\r
170 /*-----------------------------------------------------------*/\r
171 \r
172 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength )\r
173 {\r
174         /* This is not implemented in this port.\r
175         Use xSerialPortInitMinimal() instead. */\r
176 }\r
177 /*-----------------------------------------------------------*/\r
178 \r
179 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )\r
180 {\r
181         /* Get the next character from the buffer.  Return false if no characters\r
182         are available, or arrive before xBlockTime expires. */\r
183         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
184         {\r
185                 return pdTRUE;\r
186         }\r
187         else\r
188         {\r
189                 return pdFALSE;\r
190         }\r
191 }\r
192 /*-----------------------------------------------------------*/\r
193 \r
194 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )\r
195 {\r
196         /* Return false if after the block time there is no room on the Tx queue. */\r
197         if( xQueueSend( xCharsForTx, ( const void * ) &cOutChar, xBlockTime ) != pdPASS )\r
198         {\r
199                 return pdFAIL;\r
200         }\r
201 \r
202         /* Turn interrupt on - ensure the compiler only generates a single \r
203         instruction for this. */\r
204         PIE1bits.TXIE = serINTERRUPT_ENABLED;\r
205 \r
206         return pdPASS;\r
207 }\r
208 /*-----------------------------------------------------------*/\r
209 \r
210 void vSerialClose( xComPortHandle xPort )\r
211 {\r
212         /* Not implemented for this port.\r
213         To implement, turn off the interrupts and delete the memory\r
214         allocated to the queues. */\r
215 }\r
216 /*-----------------------------------------------------------*/\r
217 \r
218 #pragma interruptlow vSerialRxISR save=PRODH, PRODL, TABLAT, section(".tmpdata")\r
219 void vSerialRxISR( void )\r
220 {\r
221 char cChar;\r
222 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
223 \r
224         /* Get the character and post it on the queue of Rxed characters.\r
225         If the post causes a task to wake force a context switch as the woken task\r
226         may have a higher priority than the task we have interrupted. */\r
227         cChar = RCREG;\r
228 \r
229         /* Clear any overrun errors. */\r
230         if( RCSTAbits.OERR )\r
231         {\r
232                 RCSTAbits.CREN = serCLEAR_OVERRUN;\r
233                 RCSTAbits.CREN = serCONTINUOUS_RX;      \r
234         }\r
235 \r
236         xQueueSendFromISR( xRxedChars, ( const void * ) &cChar, &xHigherPriorityTaskWoken );\r
237 \r
238         if( xHigherPriorityTaskWoken )\r
239         {\r
240                 taskYIELD();\r
241         }\r
242 }\r
243 /*-----------------------------------------------------------*/\r
244 \r
245 #pragma interruptlow vSerialTxISR save=PRODH, PRODL, TABLAT, section(".tmpdata")\r
246 void vSerialTxISR( void )\r
247 {\r
248 char cChar, cTaskWoken = pdFALSE;\r
249 \r
250         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )\r
251         {\r
252                 /* Send the next character queued for Tx. */\r
253                 TXREG = cChar;\r
254         }\r
255         else\r
256         {\r
257                 /* Queue empty, nothing to send. */\r
258                 PIE1bits.TXIE = serINTERRUPT_DISABLED;\r
259         }\r
260 }\r
261 \r
262 \r
263 \r