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