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