]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/HCS12_CodeWarrior_banked/serial/serial.c
Roll up the minor changes checked into svn since V10.0.0 into new V10.0.1 ready for...
[freertos] / FreeRTOS / Demo / HCS12_CodeWarrior_banked / serial / serial.c
1 /*\r
2  * FreeRTOS Kernel V10.0.1\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 \r
29 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER for port 1.\r
30 \r
31 Note that this driver is written to test the RTOS port and is not intended\r
32 to represent an optimised solution. */\r
33 \r
34 /* Processor Expert generated includes. */\r
35 #include "com0.h"\r
36 \r
37 /* Scheduler include files. */\r
38 #include "FreeRTOS.h"\r
39 #include "queue.h"\r
40 #include "task.h"\r
41 \r
42 /* Demo application include files. */\r
43 #include "serial.h"\r
44 \r
45 /* The queues used to communicate between the task code and the interrupt\r
46 service routines. */\r
47 static QueueHandle_t xRxedChars; \r
48 static QueueHandle_t xCharsForTx; \r
49 \r
50 /* Interrupt identification bits. */\r
51 #define serOVERRUN_INTERRUPT            ( 0x08 )\r
52 #define serRX_INTERRUPT                         ( 0x20 )\r
53 #define serTX_INTERRUPT                         ( 0x80 )\r
54 \r
55 /*-----------------------------------------------------------*/\r
56 \r
57 \r
58 /*\r
59  * Initialise port for interrupt driven communications.\r
60  */\r
61 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
62 {\r
63         /* Hardware setup is performed by the Processor Expert generated code.  \r
64         This function just creates the queues used to communicate between the \r
65         interrupt code and the task code - then sets the required baud rate. */\r
66 \r
67         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
68         xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
69 \r
70         COM0_SetBaudRateMode( ( char ) ulWantedBaud );\r
71 \r
72         return NULL;\r
73 }\r
74 /*-----------------------------------------------------------*/\r
75 \r
76 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
77 {\r
78         /* Get the next character from the buffer queue.  Return false if no characters\r
79         are available, or arrive before xBlockTime expires. */\r
80         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
81         {\r
82                 return pdTRUE;\r
83         }\r
84         else\r
85         {\r
86                 return pdFALSE;\r
87         }\r
88 }\r
89 /*-----------------------------------------------------------*/\r
90 \r
91 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
92 {\r
93         /* Place the character in the queue of characters to be transmitted. */\r
94         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
95         {\r
96                 return pdFAIL;\r
97         }\r
98 \r
99         /* Turn on the Tx interrupt so the ISR will remove the character from the\r
100         queue and send it.   This does not need to be in a critical section as\r
101         if the interrupt has already removed the character the next interrupt\r
102         will simply turn off the Tx interrupt again. */\r
103         SCI0CR2_SCTIE = 1;;\r
104 \r
105         return pdPASS;\r
106 }\r
107 /*-----------------------------------------------------------*/\r
108 \r
109 void vSerialClose( xComPortHandle xPort )\r
110 {       \r
111         /* Not supported. */\r
112         ( void ) xPort;\r
113 }\r
114 /*-----------------------------------------------------------*/\r
115 \r
116 \r
117 /* \r
118  * Interrupt service routine for the serial port.  Must be in non-banked\r
119  * memory. \r
120  */\r
121 \r
122 #pragma CODE_SEG __NEAR_SEG NON_BANKED\r
123 \r
124 __interrupt void vCOM0_ISR( void )\r
125 {\r
126 volatile unsigned char ucByte, ucStatus;\r
127 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
128 \r
129         /* What caused the interrupt? */\r
130         ucStatus = SCI0SR1;\r
131         \r
132         if( ucStatus & serOVERRUN_INTERRUPT )\r
133         {\r
134                 /* The interrupt was caused by an overrun.  Clear the error by reading\r
135                 the data register. */\r
136                 ucByte = SCI0DRL;\r
137         }\r
138 \r
139         if( ucStatus & serRX_INTERRUPT )\r
140         {       \r
141                 /* The interrupt was caused by a character being received.\r
142                 Read the received byte. */\r
143                 ucByte = SCI0DRL;                      \r
144 \r
145                 /* Post the character onto the queue of received characters - noting\r
146                 whether or not this wakes a task. */\r
147                 xQueueSendFromISR( xRxedChars, ( void * ) &ucByte, &xHigherPriorityTaskWoken );\r
148         }\r
149         \r
150         if( ( ucStatus & serTX_INTERRUPT ) && ( SCI0CR2_SCTIE ) )\r
151         {       \r
152                 /* The interrupt was caused by a character being transmitted. */\r
153                 if( xQueueReceiveFromISR( xCharsForTx, ( void * ) &ucByte, &xHigherPriorityTaskWoken ) == pdTRUE )\r
154                 {\r
155                         /* Clear the SCRF bit. */\r
156                         SCI0DRL = ucByte;\r
157                 }\r
158                 else\r
159                 {\r
160                         /* Disable transmit interrupt */\r
161                         SCI0CR2_SCTIE = 0;                 \r
162                 }\r
163         }\r
164 \r
165         if( xHigherPriorityTaskWoken )\r
166         {\r
167                 portYIELD();\r
168         }\r
169 }\r
170 \r
171 #pragma CODE_SEG DEFAULT\r
172 \r