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