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