]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTUS_APS3_GCC/Demo/serial.c
Update demo project for Tensilita - work in progres..
[freertos] / FreeRTOS / Demo / CORTUS_APS3_GCC / Demo / 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 /*  Basic interrupt driven serial port driver for uart1.\r
29 */\r
30 \r
31 /* Standard includes. */\r
32 #include <stdlib.h>\r
33 \r
34 /* Kernel includes. */\r
35 #include "FreeRTOS.h"\r
36 #include "queue.h"\r
37 #include "task.h"\r
38 \r
39 /* Demo application includes. */\r
40 #include <machine/uart.h>\r
41 #include <machine/ic.h>\r
42 #include "serial.h"\r
43 /*-----------------------------------------------------------*/\r
44 \r
45 #define comBLOCK_RETRY_TIME                             10\r
46 /*-----------------------------------------------------------*/\r
47 \r
48 /* The interrupt handlers are naked functions that call C handlers.  The C\r
49 handlers are marked as noinline to ensure they work correctly when the\r
50 optimiser is on. */\r
51 void interrupt5_handler( void ) __attribute__((naked));\r
52 static void prvTxHandler( void ) __attribute__((noinline));\r
53 void interrupt6_handler( void ) __attribute__((naked));\r
54 static void prvRxHandler( void ) __attribute__((noinline));\r
55 \r
56 /*-----------------------------------------------------------*/\r
57 \r
58 /* Queues used to hold received characters, and characters waiting to be\r
59 transmitted. */\r
60 static QueueHandle_t xRxedChars;\r
61 static QueueHandle_t xCharsForTx;\r
62 /*-----------------------------------------------------------*/\r
63 \r
64 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
65 {\r
66         /* Create the queues used to hold Rx and Tx characters. */\r
67         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
68         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
69 \r
70         if( ( xRxedChars ) && ( xCharsForTx ) )\r
71         {\r
72                 /* Set up interrupts */\r
73                 /* tx interrupt will be enabled when we need to send something */\r
74                 uart1->tx_mask = 0;\r
75                 uart1->rx_mask = 1;\r
76                 irq[IRQ_UART1_TX].ien = 1;\r
77                 irq[IRQ_UART1_RX].ien = 1;\r
78         }\r
79 \r
80         return ( xComPortHandle ) 0;\r
81 }\r
82 /*-----------------------------------------------------------*/\r
83 \r
84 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
85 {\r
86         /* The port handle is not required as this driver only supports uart1. */\r
87         (void) pxPort;\r
88 \r
89         /* Get the next character from the buffer.      Return false if no characters\r
90            are available, or arrive before xBlockTime expires. */\r
91         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
92         {\r
93                 return pdTRUE;\r
94         }\r
95         else\r
96         {\r
97                 return pdFALSE;\r
98         }\r
99 }\r
100 /*-----------------------------------------------------------*/\r
101 \r
102 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
103 {\r
104         int i;\r
105         signed char *pChNext;\r
106 \r
107         /* Send each character in the string, one at a time. */\r
108         pChNext = ( signed char * )pcString;\r
109         for( i = 0; i < usStringLength; i++ )\r
110         {\r
111                 /* Block until character has been transmitted. */\r
112                 while( xSerialPutChar( pxPort, *pChNext, comBLOCK_RETRY_TIME ) != pdTRUE ); pChNext++;\r
113         }\r
114 }\r
115 /*-----------------------------------------------------------*/\r
116 \r
117 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
118 {\r
119         ( void ) pxPort;\r
120 \r
121         /* Place the character in the queue of characters to be transmitted. */\r
122         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
123         {\r
124                 return pdFAIL;\r
125         }\r
126 \r
127         /* Turn on the Tx interrupt so the ISR will remove the character from the\r
128            queue and send it. This does not need to be in a critical section as\r
129            if the interrupt has already removed the character the next interrupt\r
130            will simply turn off the Tx interrupt again. */\r
131         uart1->tx_mask = 1;\r
132 \r
133         return pdPASS;\r
134 }\r
135 /*-----------------------------------------------------------*/\r
136 \r
137 void vSerialClose( xComPortHandle xPort )\r
138 {\r
139         /* Not supported as not required by the demo application. */\r
140         ( void ) xPort;\r
141 }\r
142 /*-----------------------------------------------------------*/\r
143 \r
144 /* UART Tx interrupt handler. */\r
145 void interrupt5_handler( void )\r
146 {\r
147         /* This is a naked function. */\r
148         portSAVE_CONTEXT();\r
149         prvTxHandler();\r
150         portRESTORE_CONTEXT();\r
151 }\r
152 /*-----------------------------------------------------------*/\r
153 \r
154 static void prvTxHandler( void )\r
155 {\r
156 signed char cChar;\r
157 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
158 \r
159         /* The interrupt was caused by the transmit fifo having space for at least one\r
160         character. Are there any more characters to transmit? */\r
161         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
162         {\r
163                 /* A character was retrieved from the queue so can be sent to the uart now. */\r
164                 uart1->tx_data = cChar;\r
165         }\r
166         else\r
167         {\r
168                 /* Queue empty, nothing to send so turn off the Tx interrupt. */\r
169                 uart1->tx_mask = 0;\r
170         }\r
171 \r
172         /* If an event caused a task to unblock then we call "Yield from ISR" to\r
173         ensure that the unblocked task is the task that executes when the interrupt\r
174         completes if the unblocked task has a priority higher than the interrupted\r
175         task. */\r
176         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
177 }\r
178 /*-----------------------------------------------------------*/\r
179 \r
180 /* UART Rx interrupt. */\r
181 void interrupt6_handler( void )\r
182 {\r
183         portSAVE_CONTEXT();\r
184         prvRxHandler();\r
185         portRESTORE_CONTEXT();\r
186 }\r
187 /*-----------------------------------------------------------*/\r
188 \r
189 static void prvRxHandler( void )\r
190 {\r
191 signed char cChar;\r
192 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
193 \r
194         /* The interrupt was caused by the receiver getting data. */\r
195         cChar = uart1->rx_data;\r
196 \r
197         xQueueSendFromISR(xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
198 \r
199         /* If an event caused a task to unblock then we call "Yield from ISR" to\r
200         ensure that the unblocked task is the task that executes when the interrupt\r
201         completes if the unblocked task has a priority higher than the interrupted\r
202         task. */\r
203         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
204 }\r
205 /*-----------------------------------------------------------*/\r
206 \r
207 \r