]> git.sur5r.net Git - freertos/blob - Demo/PIC18_WizC/serial/serial.c
Update version numbers to V4.8.0
[freertos] / Demo / PIC18_WizC / serial / serial.c
1 /*\r
2         FreeRTOS.org V4.8.0 - Copyright (C) 2003-2008 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         ***************************************************************************\r
28         *                                                                                                                                                 *\r
29         * SAVE TIME AND MONEY!  Why not get us to quote to get FreeRTOS.org               *\r
30         * running on your hardware - or even write all or part of your application*\r
31         * for you?  See http://www.OpenRTOS.com for details.                                      *\r
32         *                                                                                                                                                 *\r
33         ***************************************************************************\r
34         ***************************************************************************\r
35 \r
36         Please ensure to read the configuration and relevant port sections of the\r
37         online documentation.\r
38 \r
39         http://www.FreeRTOS.org - Documentation, latest information, license and \r
40         contact details.\r
41 \r
42         http://www.SafeRTOS.com - A version that is certified for use in safety \r
43         critical systems.\r
44 \r
45         http://www.OpenRTOS.com - Commercial support, development, porting, \r
46         licensing and training services.\r
47 */\r
48 \r
49 /*\r
50 Changes from V3.0.0\r
51         + ISRcode removed. Is now pulled inline to reduce stack-usage.\r
52 \r
53 Changes from V3.0.1\r
54 */\r
55 \r
56 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */\r
57 \r
58 /* Scheduler header files. */\r
59 #include "FreeRTOS.h"\r
60 #include "task.h"\r
61 #include "queue.h"\r
62 \r
63 #include "serial.h"\r
64 \r
65 /* Hardware pin definitions. */\r
66 #define serTX_PIN                               bTRC6\r
67 #define serRX_PIN                               bTRC7\r
68 \r
69 /* Bit/register definitions. */\r
70 #define serINPUT                                ( 1 )\r
71 #define serOUTPUT                               ( 0 )\r
72 #define serINTERRUPT_ENABLED    ( 1 )\r
73 \r
74 /* All ISR's use the PIC18 low priority interrupt. */\r
75 #define serLOW_PRIORITY                 ( 0 )\r
76 \r
77 /*-----------------------------------------------------------*/\r
78 \r
79 /* Queues to interface between comms API and interrupt routines. */\r
80 xQueueHandle xRxedChars; \r
81 xQueueHandle xCharsForTx;\r
82 \r
83 /*-----------------------------------------------------------*/\r
84 \r
85 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portCHAR ucQueueLength )\r
86 {\r
87         unsigned portSHORT usSPBRG;\r
88         \r
89         /* Create the queues used by the ISR's to interface to tasks. */\r
90         xRxedChars = xQueueCreate( ucQueueLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );\r
91         xCharsForTx = xQueueCreate( ucQueueLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );\r
92 \r
93         portENTER_CRITICAL();\r
94 \r
95         /* Setup the IO pins to enable the USART IO. */\r
96         serTX_PIN       = serINPUT;             // YES really! See datasheet\r
97         serRX_PIN       = serINPUT;\r
98 \r
99         /* Set the TX config register. */\r
100         TXSTA = 0b00100000;\r
101                 //        ||||||||--bit0: TX9D  = n/a\r
102                 //        |||||||---bit1: TRMT  = ReadOnly\r
103                 //        ||||||----bit2: BRGH  = High speed\r
104                 //        |||||-----bit3: SENDB = n/a\r
105                 //        ||||------bit4: SYNC  = Asynchronous mode\r
106                 //        |||-------bit5: TXEN  = Transmit enable\r
107                 //        ||--------bit6: TX9   = 8-bit transmission\r
108                 //        |---------bit7: CSRC  = n/a\r
109 \r
110         /* Set the Receive config register. */\r
111         RCSTA = 0b10010000;\r
112                 //        ||||||||--bit0: RX9D  = ReadOnly\r
113                 //        |||||||---bit1: OERR  = ReadOnly\r
114                 //        ||||||----bit2: FERR  = ReadOnly\r
115                 //        |||||-----bit3: ADDEN = n/a\r
116                 //        ||||------bit4: CREN  = Enable receiver\r
117                 //        |||-------bit5: SREN  = n/a\r
118                 //        ||--------bit6: RX9   = 8-bit reception\r
119                 //        |---------bit7: SPEN  = Serial port enabled\r
120 \r
121         /* Calculate the baud rate generator value.\r
122            We use low-speed (BRGH=0), the formula is\r
123            SPBRG = ( ( FOSC / Desired Baud Rate ) / 64 ) - 1 */\r
124         usSPBRG = ( ( APROCFREQ / ulWantedBaud ) / 64 ) - 1;\r
125         if( usSPBRG > 255 )\r
126         {\r
127                 SPBRG = 255;\r
128         }\r
129         else\r
130         {\r
131                 SPBRG = usSPBRG;\r
132         }\r
133 \r
134         /* Set the serial interrupts to use the same priority as the tick. */\r
135         bTXIP = serLOW_PRIORITY;\r
136         bRCIP = serLOW_PRIORITY;\r
137 \r
138         /* Enable the Rx interrupt now, the Tx interrupt will get enabled when\r
139         we have data to send. */\r
140         bRCIE = serINTERRUPT_ENABLED;\r
141         \r
142         portEXIT_CRITICAL();\r
143 \r
144         /* Unlike other ports, this serial code does not allow for more than one\r
145         com port.  We therefore don't return a pointer to a port structure and \r
146         can     instead just return NULL. */\r
147         return NULL;\r
148 }\r
149 /*-----------------------------------------------------------*/\r
150 \r
151 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portCHAR ucBufferLength )\r
152 {\r
153         /* This is not implemented in this port.\r
154         Use xSerialPortInitMinimal() instead. */\r
155         return NULL;\r
156 }\r
157 /*-----------------------------------------------------------*/\r
158 \r
159 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, portCHAR *pcRxedChar, portTickType xBlockTime )\r
160 {\r
161         /* Get the next character from the buffer.  Return false if no characters\r
162         are available, or arrive before xBlockTime expires. */\r
163         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
164         {\r
165                 return ( portCHAR ) pdTRUE;\r
166         }\r
167 \r
168         return ( portCHAR ) pdFALSE;\r
169 }\r
170 /*-----------------------------------------------------------*/\r
171 \r
172 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, portCHAR cOutChar, portTickType xBlockTime )\r
173 {\r
174         /* Return false if after the block time there is no room on the Tx queue. */\r
175         if( xQueueSend( xCharsForTx, ( const void * ) &cOutChar, xBlockTime ) != ( portCHAR ) pdPASS )\r
176         {\r
177                 return pdFAIL;\r
178         }\r
179 \r
180         /* Turn interrupt on - ensure the compiler only generates a single \r
181         instruction for this. */\r
182         bTXIE = serINTERRUPT_ENABLED;\r
183 \r
184         return pdPASS;\r
185 }\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 void vSerialClose( xComPortHandle xPort )\r
189 {\r
190         /* Not implemented for this port.\r
191         To implement, turn off the interrupts and delete the memory\r
192         allocated to the queues. */\r
193 }\r