]> git.sur5r.net Git - freertos/blob - Demo/PIC24_MPLAB/serial/serial.c
Add PIC24, dsPIC and Coldfire files.
[freertos] / Demo / PIC24_MPLAB / serial / serial.c
1 /*\r
2         FreeRTOS.org V4.1.0 - Copyright (C) 2003-2006 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         See http://www.FreeRTOS.org for documentation, latest information, license \r
28         and contact details.  Please ensure to read the configuration and relevant \r
29         port sections of the online documentation.\r
30         ***************************************************************************\r
31 */\r
32 \r
33 \r
34 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. \r
35 \r
36 NOTE:  This driver is primarily to test the scheduler functionality.  It does\r
37 not effectively use the buffers or DMA and is therefore not intended to be\r
38 an example of an efficient driver. */\r
39 \r
40 /* Standard include file. */\r
41 #include <stdlib.h>\r
42 \r
43 /* Scheduler include files. */\r
44 #include "FreeRTOS.h"\r
45 #include "queue.h"\r
46 #include "task.h"\r
47 \r
48 /* Demo app include files. */\r
49 #include "serial.h"\r
50 \r
51 /* Hardware setup. */\r
52 #define serOUTPUT                                               0\r
53 #define serINPUT                                                1\r
54 #define serLOW_SPEED                                    0\r
55 #define serONE_STOP_BIT                                 0\r
56 #define serEIGHT_DATA_BITS_NO_PARITY    0\r
57 #define serNORMAL_IDLE_STATE                    0\r
58 #define serAUTO_BAUD_OFF                                0\r
59 #define serLOOPBACK_OFF                                 0\r
60 #define serWAKE_UP_DISABLE                              0\r
61 #define serNO_HARDWARE_FLOW_CONTROL             0\r
62 #define serSTANDARD_IO                                  0\r
63 #define serNO_IRDA                                              0\r
64 #define serCONTINUE_IN_IDLE_MODE                0\r
65 #define serUART_ENABLED                                 1\r
66 #define serINTERRUPT_ON_SINGLE_CHAR             0\r
67 #define serTX_ENABLE                                    1\r
68 #define serINTERRUPT_ENABLE                             1\r
69 #define serINTERRUPT_DISABLE                    0\r
70 #define serCLEAR_FLAG                                   0\r
71 #define serSET_FLAG                                             1\r
72 \r
73 \r
74 /* The queues used to communicate between tasks and ISR's. */\r
75 static xQueueHandle xRxedChars; \r
76 static xQueueHandle xCharsForTx; \r
77 \r
78 static portBASE_TYPE xTxHasEnded;\r
79 /*-----------------------------------------------------------*/\r
80 \r
81 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
82 {\r
83 portCHAR cChar;\r
84 \r
85         /* Create the queues used by the com test task. */\r
86         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
87         xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
88 \r
89         /* Setup the UART. */\r
90         U2MODEbits.BRGH         = serLOW_SPEED;\r
91         U2MODEbits.STSEL        = serONE_STOP_BIT;\r
92         U2MODEbits.PDSEL        = serEIGHT_DATA_BITS_NO_PARITY;\r
93         U2MODEbits.RXINV        = serNORMAL_IDLE_STATE;\r
94         U2MODEbits.ABAUD        = serAUTO_BAUD_OFF;\r
95         U2MODEbits.LPBACK       = serLOOPBACK_OFF;\r
96         U2MODEbits.WAKE         = serWAKE_UP_DISABLE;\r
97         U2MODEbits.UEN          = serNO_HARDWARE_FLOW_CONTROL;\r
98         U2MODEbits.IREN         = serNO_IRDA;\r
99         U2MODEbits.USIDL        = serCONTINUE_IN_IDLE_MODE;\r
100         U2MODEbits.UARTEN       = serUART_ENABLED;\r
101 \r
102         U2BRG = (unsigned portSHORT)(( (float)configCPU_CLOCK_HZ / ( (float)16 * (float)ulWantedBaud ) ) - (float)0.5);\r
103 \r
104         U2STAbits.URXISEL       = serINTERRUPT_ON_SINGLE_CHAR;\r
105         U2STAbits.UTXEN         = serTX_ENABLE;\r
106         U2STAbits.UTXINV        = serNORMAL_IDLE_STATE;\r
107         U2STAbits.UTXISEL0      = serINTERRUPT_ON_SINGLE_CHAR;\r
108         U2STAbits.UTXISEL1      = serINTERRUPT_ON_SINGLE_CHAR;\r
109 \r
110         /* It is assumed that this function is called prior to the scheduler being\r
111         started.  Therefore interrupts must not be allowed to occur yet as they\r
112         may attempt to perform a context switch. */\r
113         portDISABLE_INTERRUPTS();\r
114 \r
115         IFS1bits.U2RXIF = serCLEAR_FLAG;\r
116         IFS1bits.U2TXIF = serCLEAR_FLAG;\r
117         IPC7bits.U2RXIP = portKERNEL_INTERRUPT_PRIORITY;\r
118         IPC7bits.U2TXIP = portKERNEL_INTERRUPT_PRIORITY;\r
119         IEC1bits.U2TXIE = serINTERRUPT_ENABLE;\r
120         IEC1bits.U2RXIE = serINTERRUPT_ENABLE;\r
121 \r
122         /* Clear the Rx buffer. */\r
123         while( U2STAbits.URXDA == serSET_FLAG )\r
124         {\r
125                 cChar = U2RXREG;\r
126         }\r
127 \r
128         xTxHasEnded = pdTRUE;\r
129 \r
130         return NULL;\r
131 }\r
132 /*-----------------------------------------------------------*/\r
133 \r
134 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
135 {\r
136         /* Only one port is supported. */\r
137         ( void ) pxPort;\r
138 \r
139         /* Get the next character from the buffer.  Return false if no characters\r
140         are available or arrive before xBlockTime expires. */\r
141         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
142         {\r
143                 return pdTRUE;\r
144         }\r
145         else\r
146         {\r
147                 return pdFALSE;\r
148         }\r
149 }\r
150 /*-----------------------------------------------------------*/\r
151 \r
152 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
153 {\r
154         /* Only one port is supported. */\r
155         ( void ) pxPort;\r
156 \r
157         /* Return false if after the block time there is no room on the Tx queue. */\r
158         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
159         {\r
160                 return pdFAIL;\r
161         }\r
162 \r
163         /* A critical section should not be required as xTxHasEnded will not be\r
164         written to by the ISR if it is already 0 (is this correct?). */\r
165         if( xTxHasEnded )\r
166         {\r
167                 xTxHasEnded = pdFALSE;\r
168                 IFS1bits.U2TXIF = serSET_FLAG;\r
169         }\r
170 \r
171         return pdPASS;\r
172 }\r
173 /*-----------------------------------------------------------*/\r
174 \r
175 void vSerialClose( xComPortHandle xPort )\r
176 {\r
177 }\r
178 /*-----------------------------------------------------------*/\r
179 \r
180 volatile short s = 0;\r
181 char c[80] = {0};\r
182 \r
183 void __attribute__((__interrupt__)) _U2RXInterrupt( void )\r
184 {\r
185 portCHAR cChar;\r
186 portBASE_TYPE xYieldRequired = pdFALSE;\r
187 \r
188         /* Get the character and post it on the queue of Rxed characters.\r
189         If the post causes a task to wake force a context switch as the woken task\r
190         may have a higher priority than the task we have interrupted. */\r
191         IFS1bits.U2RXIF = serCLEAR_FLAG;\r
192         while( U2STAbits.URXDA )\r
193         {\r
194                 cChar = U2RXREG;\r
195                 xYieldRequired = xQueueSendFromISR( xRxedChars, &cChar, xYieldRequired );\r
196         }\r
197 \r
198         if( xYieldRequired != pdFALSE )\r
199         {\r
200                 taskYIELD();\r
201         }\r
202 }\r
203 /*-----------------------------------------------------------*/\r
204 \r
205 void __attribute__((__interrupt__)) _U2TXInterrupt( void )\r
206 {\r
207 signed portCHAR cChar;\r
208 portBASE_TYPE xTaskWoken = pdFALSE;\r
209 \r
210         /* If the transmit buffer is full we cannot get the next character.\r
211         Another interrupt will occur the next time there is space so this does\r
212         not matter. */\r
213         IFS1bits.U2TXIF = serCLEAR_FLAG;\r
214         while( !( U2STAbits.UTXBF ) )\r
215         {\r
216                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )\r
217                 {\r
218                         /* Send the next character queued for Tx. */\r
219                         U2TXREG = cChar;\r
220                 }\r
221                 else\r
222                 {\r
223                         /* Queue empty, nothing to send. */\r
224                         xTxHasEnded = pdTRUE;\r
225                         break;\r
226                 }\r
227         }\r
228 \r
229         if( xTaskWoken != pdFALSE )\r
230         {\r
231                 taskYIELD();\r
232         }\r
233 }\r
234 \r
235 \r