]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC18_WizC/serial/serial.c
Prepare for V7.3.0 release.
[freertos] / FreeRTOS / Demo / PIC18_WizC / serial / serial.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT \r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32     >>>NOTE<<< The modification to the GPL is included to allow you to\r
33     distribute a combined work that includes FreeRTOS without being obliged to\r
34     provide the source code for proprietary components outside of the FreeRTOS\r
35     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
36     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
37     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
38     more details. You should have received a copy of the GNU General Public\r
39     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
40     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
41     by writing to Richard Barry, contact details for whom are available on the\r
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45     \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55     \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license \r
57     and contact details.  \r
58     \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \r
62     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
63     the code with commercial support, indemnification, and middleware, under \r
64     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
65     provide a safety engineered and independently SIL3 certified version under \r
66     the SafeRTOS brand: http://www.SafeRTOS.com.\r
67 */\r
68 \r
69 /*\r
70 Changes from V3.0.0\r
71         + ISRcode removed. Is now pulled inline to reduce stack-usage.\r
72 \r
73 Changes from V3.0.1\r
74 */\r
75 \r
76 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */\r
77 \r
78 /* Scheduler header files. */\r
79 #include "FreeRTOS.h"\r
80 #include "task.h"\r
81 #include "queue.h"\r
82 \r
83 #include "serial.h"\r
84 \r
85 /* Hardware pin definitions. */\r
86 #define serTX_PIN                               bTRC6\r
87 #define serRX_PIN                               bTRC7\r
88 \r
89 /* Bit/register definitions. */\r
90 #define serINPUT                                ( 1 )\r
91 #define serOUTPUT                               ( 0 )\r
92 #define serINTERRUPT_ENABLED    ( 1 )\r
93 \r
94 /* All ISR's use the PIC18 low priority interrupt. */\r
95 #define serLOW_PRIORITY                 ( 0 )\r
96 \r
97 /*-----------------------------------------------------------*/\r
98 \r
99 /* Queues to interface between comms API and interrupt routines. */\r
100 xQueueHandle xRxedChars; \r
101 xQueueHandle xCharsForTx;\r
102 portBASE_TYPE xHigherPriorityTaskWoken;\r
103 \r
104 /*-----------------------------------------------------------*/\r
105 \r
106 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned char ucQueueLength )\r
107 {\r
108         unsigned short usSPBRG;\r
109         \r
110         /* Create the queues used by the ISR's to interface to tasks. */\r
111         xRxedChars = xQueueCreate( ucQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );\r
112         xCharsForTx = xQueueCreate( ucQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );\r
113 \r
114         portENTER_CRITICAL();\r
115 \r
116         /* Setup the IO pins to enable the USART IO. */\r
117         serTX_PIN       = serINPUT;             // YES really! See datasheet\r
118         serRX_PIN       = serINPUT;\r
119 \r
120         /* Set the TX config register. */\r
121         TXSTA = 0b00100000;\r
122                 //        ||||||||--bit0: TX9D  = n/a\r
123                 //        |||||||---bit1: TRMT  = ReadOnly\r
124                 //        ||||||----bit2: BRGH  = High speed\r
125                 //        |||||-----bit3: SENDB = n/a\r
126                 //        ||||------bit4: SYNC  = Asynchronous mode\r
127                 //        |||-------bit5: TXEN  = Transmit enable\r
128                 //        ||--------bit6: TX9   = 8-bit transmission\r
129                 //        |---------bit7: CSRC  = n/a\r
130 \r
131         /* Set the Receive config register. */\r
132         RCSTA = 0b10010000;\r
133                 //        ||||||||--bit0: RX9D  = ReadOnly\r
134                 //        |||||||---bit1: OERR  = ReadOnly\r
135                 //        ||||||----bit2: FERR  = ReadOnly\r
136                 //        |||||-----bit3: ADDEN = n/a\r
137                 //        ||||------bit4: CREN  = Enable receiver\r
138                 //        |||-------bit5: SREN  = n/a\r
139                 //        ||--------bit6: RX9   = 8-bit reception\r
140                 //        |---------bit7: SPEN  = Serial port enabled\r
141 \r
142         /* Calculate the baud rate generator value.\r
143            We use low-speed (BRGH=0), the formula is\r
144            SPBRG = ( ( FOSC / Desired Baud Rate ) / 64 ) - 1 */\r
145         usSPBRG = ( ( APROCFREQ / ulWantedBaud ) / 64 ) - 1;\r
146         if( usSPBRG > 255 )\r
147         {\r
148                 SPBRG = 255;\r
149         }\r
150         else\r
151         {\r
152                 SPBRG = usSPBRG;\r
153         }\r
154 \r
155         /* Set the serial interrupts to use the same priority as the tick. */\r
156         bTXIP = serLOW_PRIORITY;\r
157         bRCIP = serLOW_PRIORITY;\r
158 \r
159         /* Enable the Rx interrupt now, the Tx interrupt will get enabled when\r
160         we have data to send. */\r
161         bRCIE = serINTERRUPT_ENABLED;\r
162         \r
163         portEXIT_CRITICAL();\r
164 \r
165         /* Unlike other ports, this serial code does not allow for more than one\r
166         com port.  We therefore don't return a pointer to a port structure and \r
167         can     instead just return NULL. */\r
168         return NULL;\r
169 }\r
170 /*-----------------------------------------------------------*/\r
171 \r
172 xComPortHandle xSerialPortInit( eCOMPort ePort, eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned char ucBufferLength )\r
173 {\r
174         /* This is not implemented in this port.\r
175         Use xSerialPortInitMinimal() instead. */\r
176         return NULL;\r
177 }\r
178 /*-----------------------------------------------------------*/\r
179 \r
180 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, char *pcRxedChar, portTickType xBlockTime )\r
181 {\r
182         /* Get the next character from the buffer.  Return false if no characters\r
183         are available, or arrive before xBlockTime expires. */\r
184         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
185         {\r
186                 return ( char ) pdTRUE;\r
187         }\r
188 \r
189         return ( char ) pdFALSE;\r
190 }\r
191 /*-----------------------------------------------------------*/\r
192 \r
193 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, char cOutChar, portTickType xBlockTime )\r
194 {\r
195         /* Return false if after the block time there is no room on the Tx queue. */\r
196         if( xQueueSend( xCharsForTx, ( const void * ) &cOutChar, xBlockTime ) != ( char ) pdPASS )\r
197         {\r
198                 return pdFAIL;\r
199         }\r
200 \r
201         /* Turn interrupt on - ensure the compiler only generates a single \r
202         instruction for this. */\r
203         bTXIE = serINTERRUPT_ENABLED;\r
204 \r
205         return pdPASS;\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 void vSerialClose( xComPortHandle xPort )\r
210 {\r
211         /* Not implemented for this port.\r
212         To implement, turn off the interrupts and delete the memory\r
213         allocated to the queues. */\r
214 }\r