]> git.sur5r.net Git - freertos/blob - Demo/PIC18_WizC/serial/serial.c
Prepare for V5.3.0 release.
[freertos] / Demo / PIC18_WizC / serial / serial.c
1 /*\r
2         FreeRTOS.org V5.3.0 - Copyright (C) 2003-2009 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 it\r
7         under the terms of the GNU General Public License (version 2) as published\r
8         by the Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS.org without being obliged to provide\r
11         the source code for any proprietary components.  Alternative commercial\r
12         license and support terms are also available upon request.  See the \r
13         licensing section of http://www.FreeRTOS.org for full details.\r
14 \r
15         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
16         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
17         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
18         more details.\r
19 \r
20         You should have received a copy of the GNU General Public License along\r
21         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59\r
22         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\r
29         * This is a concise, step by step, 'hands on' guide that describes both   *\r
30         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
31         * explains numerous examples that are written using the FreeRTOS API.     *\r
32         * Full source code for all the examples is provided in an accompanying    *\r
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 /*\r
53 Changes from V3.0.0\r
54         + ISRcode removed. Is now pulled inline to reduce stack-usage.\r
55 \r
56 Changes from V3.0.1\r
57 */\r
58 \r
59 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */\r
60 \r
61 /* Scheduler header files. */\r
62 #include "FreeRTOS.h"\r
63 #include "task.h"\r
64 #include "queue.h"\r
65 \r
66 #include "serial.h"\r
67 \r
68 /* Hardware pin definitions. */\r
69 #define serTX_PIN                               bTRC6\r
70 #define serRX_PIN                               bTRC7\r
71 \r
72 /* Bit/register definitions. */\r
73 #define serINPUT                                ( 1 )\r
74 #define serOUTPUT                               ( 0 )\r
75 #define serINTERRUPT_ENABLED    ( 1 )\r
76 \r
77 /* All ISR's use the PIC18 low priority interrupt. */\r
78 #define serLOW_PRIORITY                 ( 0 )\r
79 \r
80 /*-----------------------------------------------------------*/\r
81 \r
82 /* Queues to interface between comms API and interrupt routines. */\r
83 xQueueHandle xRxedChars; \r
84 xQueueHandle xCharsForTx;\r
85 portBASE_TYPE xHigherPriorityTaskWoken;\r
86 \r
87 /*-----------------------------------------------------------*/\r
88 \r
89 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portCHAR ucQueueLength )\r
90 {\r
91         unsigned portSHORT usSPBRG;\r
92         \r
93         /* Create the queues used by the ISR's to interface to tasks. */\r
94         xRxedChars = xQueueCreate( ucQueueLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );\r
95         xCharsForTx = xQueueCreate( ucQueueLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );\r
96 \r
97         portENTER_CRITICAL();\r
98 \r
99         /* Setup the IO pins to enable the USART IO. */\r
100         serTX_PIN       = serINPUT;             // YES really! See datasheet\r
101         serRX_PIN       = serINPUT;\r
102 \r
103         /* Set the TX config register. */\r
104         TXSTA = 0b00100000;\r
105                 //        ||||||||--bit0: TX9D  = n/a\r
106                 //        |||||||---bit1: TRMT  = ReadOnly\r
107                 //        ||||||----bit2: BRGH  = High speed\r
108                 //        |||||-----bit3: SENDB = n/a\r
109                 //        ||||------bit4: SYNC  = Asynchronous mode\r
110                 //        |||-------bit5: TXEN  = Transmit enable\r
111                 //        ||--------bit6: TX9   = 8-bit transmission\r
112                 //        |---------bit7: CSRC  = n/a\r
113 \r
114         /* Set the Receive config register. */\r
115         RCSTA = 0b10010000;\r
116                 //        ||||||||--bit0: RX9D  = ReadOnly\r
117                 //        |||||||---bit1: OERR  = ReadOnly\r
118                 //        ||||||----bit2: FERR  = ReadOnly\r
119                 //        |||||-----bit3: ADDEN = n/a\r
120                 //        ||||------bit4: CREN  = Enable receiver\r
121                 //        |||-------bit5: SREN  = n/a\r
122                 //        ||--------bit6: RX9   = 8-bit reception\r
123                 //        |---------bit7: SPEN  = Serial port enabled\r
124 \r
125         /* Calculate the baud rate generator value.\r
126            We use low-speed (BRGH=0), the formula is\r
127            SPBRG = ( ( FOSC / Desired Baud Rate ) / 64 ) - 1 */\r
128         usSPBRG = ( ( APROCFREQ / ulWantedBaud ) / 64 ) - 1;\r
129         if( usSPBRG > 255 )\r
130         {\r
131                 SPBRG = 255;\r
132         }\r
133         else\r
134         {\r
135                 SPBRG = usSPBRG;\r
136         }\r
137 \r
138         /* Set the serial interrupts to use the same priority as the tick. */\r
139         bTXIP = serLOW_PRIORITY;\r
140         bRCIP = serLOW_PRIORITY;\r
141 \r
142         /* Enable the Rx interrupt now, the Tx interrupt will get enabled when\r
143         we have data to send. */\r
144         bRCIE = serINTERRUPT_ENABLED;\r
145         \r
146         portEXIT_CRITICAL();\r
147 \r
148         /* Unlike other ports, this serial code does not allow for more than one\r
149         com port.  We therefore don't return a pointer to a port structure and \r
150         can     instead just return NULL. */\r
151         return NULL;\r
152 }\r
153 /*-----------------------------------------------------------*/\r
154 \r
155 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portCHAR ucBufferLength )\r
156 {\r
157         /* This is not implemented in this port.\r
158         Use xSerialPortInitMinimal() instead. */\r
159         return NULL;\r
160 }\r
161 /*-----------------------------------------------------------*/\r
162 \r
163 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, portCHAR *pcRxedChar, portTickType xBlockTime )\r
164 {\r
165         /* Get the next character from the buffer.  Return false if no characters\r
166         are available, or arrive before xBlockTime expires. */\r
167         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
168         {\r
169                 return ( portCHAR ) pdTRUE;\r
170         }\r
171 \r
172         return ( portCHAR ) pdFALSE;\r
173 }\r
174 /*-----------------------------------------------------------*/\r
175 \r
176 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, portCHAR cOutChar, portTickType xBlockTime )\r
177 {\r
178         /* Return false if after the block time there is no room on the Tx queue. */\r
179         if( xQueueSend( xCharsForTx, ( const void * ) &cOutChar, xBlockTime ) != ( portCHAR ) pdPASS )\r
180         {\r
181                 return pdFAIL;\r
182         }\r
183 \r
184         /* Turn interrupt on - ensure the compiler only generates a single \r
185         instruction for this. */\r
186         bTXIE = serINTERRUPT_ENABLED;\r
187 \r
188         return pdPASS;\r
189 }\r
190 /*-----------------------------------------------------------*/\r
191 \r
192 void vSerialClose( xComPortHandle xPort )\r
193 {\r
194         /* Not implemented for this port.\r
195         To implement, turn off the interrupts and delete the memory\r
196         allocated to the queues. */\r
197 }\r