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