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