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