]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_STM32L152_IAR/serial.c
bc9039117654cce039504c70c3991c14d0493d09
[freertos] / FreeRTOS / Demo / CORTEX_STM32L152_IAR / serial.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 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 UART0.\r
30         \r
31         ***Note*** This example uses queues to send each character into an interrupt\r
32         service routine and out of an interrupt service routine individually.  This\r
33         is done to demonstrate queues being used in an interrupt, and to deliberately\r
34         load the system to test the FreeRTOS port.  It is *NOT* meant to be an \r
35         example of an efficient implementation.  An efficient implementation should\r
36         use FIFO's or DMA if available, and only use FreeRTOS API functions when \r
37         enough has been received to warrant a task being unblocked to process the\r
38         data.\r
39 */\r
40 \r
41 /* Scheduler includes. */\r
42 #include "FreeRTOS.h"\r
43 #include "queue.h"\r
44 #include "semphr.h"\r
45 #include "comtest2.h"\r
46 \r
47 /* Library includes. */\r
48 #include "stm32l152_eval.h"\r
49 \r
50 /* Demo application includes. */\r
51 #include "serial.h"\r
52 /*-----------------------------------------------------------*/\r
53 \r
54 /* Misc defines. */\r
55 #define serINVALID_QUEUE                                ( ( QueueHandle_t ) 0 )\r
56 #define serNO_BLOCK                                             ( ( TickType_t ) 0 )\r
57 \r
58 /*-----------------------------------------------------------*/\r
59 \r
60 /* The queue used to hold received characters. */\r
61 static QueueHandle_t xRxedChars;\r
62 static QueueHandle_t xCharsForTx;\r
63 \r
64 /*-----------------------------------------------------------*/\r
65 \r
66 /*\r
67  * See the serial2.h header file.\r
68  */\r
69 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
70 {\r
71 USART_InitTypeDef USART_InitStructure;\r
72 xComPortHandle xReturn;\r
73 NVIC_InitTypeDef NVIC_InitStructure;\r
74 \r
75         /* Create the queues used to hold Rx/Tx characters. */\r
76         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
77         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
78         \r
79         /* If the queues were created correctly then setup the serial port\r
80         hardware. */\r
81         if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )\r
82         {\r
83                 USART_InitStructure.USART_BaudRate = ulWantedBaud;\r
84                 USART_InitStructure.USART_WordLength = USART_WordLength_8b;\r
85                 USART_InitStructure.USART_StopBits = USART_StopBits_1;\r
86                 USART_InitStructure.USART_Parity = USART_Parity_No;\r
87                 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;\r
88                 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;\r
89                 \r
90                 /* The Eval board COM2 is being used, which in reality is the STM32\r
91                 USART3. */\r
92                 STM_EVAL_COMInit( COM2, &USART_InitStructure );\r
93                 \r
94                 NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;\r
95                 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY;\r
96                 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; /* Not used as 4 bits are used for the pre-emption priority. */;\r
97                 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;\r
98                 NVIC_Init( &NVIC_InitStructure );\r
99                 USART_ITConfig( USART3, USART_IT_RXNE, ENABLE );\r
100         }\r
101         else\r
102         {\r
103                 xReturn = ( xComPortHandle ) 0;\r
104         }\r
105 \r
106         /* This demo file only supports a single port but we have to return\r
107         something to comply with the standard demo header file. */\r
108         return xReturn;\r
109 }\r
110 /*-----------------------------------------------------------*/\r
111 \r
112 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
113 {\r
114         /* The port handle is not required as this driver only supports one port. */\r
115         ( void ) pxPort;\r
116 \r
117         /* Get the next character from the buffer.  Return false if no characters\r
118         are available, or arrive before xBlockTime expires. */\r
119         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
120         {\r
121                 return pdTRUE;\r
122         }\r
123         else\r
124         {\r
125                 return pdFALSE;\r
126         }\r
127 }\r
128 /*-----------------------------------------------------------*/\r
129 \r
130 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
131 {\r
132 signed char *pxNext;\r
133 \r
134         /* A couple of parameters that this port does not use. */\r
135         ( void ) usStringLength;\r
136         ( void ) pxPort;\r
137 \r
138         /* NOTE: This implementation does not handle the queue being full as no\r
139         block time is used! */\r
140 \r
141         /* The port handle is not required as this driver only supports UART1. */\r
142         ( void ) pxPort;\r
143 \r
144         /* Send each character in the string, one at a time. */\r
145         pxNext = ( signed char * ) pcString;\r
146         while( *pxNext )\r
147         {\r
148                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
149                 pxNext++;\r
150         }\r
151 }\r
152 /*-----------------------------------------------------------*/\r
153 \r
154 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
155 {\r
156 signed portBASE_TYPE xReturn;\r
157 \r
158         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS )\r
159         {\r
160                 xReturn = pdPASS;\r
161                 USART_ITConfig( USART3, USART_IT_TXE, ENABLE );\r
162         }\r
163         else\r
164         {\r
165                 xReturn = pdFAIL;\r
166         }\r
167 \r
168         return xReturn;\r
169 }\r
170 /*-----------------------------------------------------------*/\r
171 \r
172 void vSerialClose( xComPortHandle xPort )\r
173 {\r
174         /* Not supported as not required by the demo application. */\r
175 }\r
176 /*-----------------------------------------------------------*/\r
177 \r
178 void USART3_IRQHandler( void )\r
179 {\r
180 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
181 char cChar;\r
182 \r
183         if( USART_GetITStatus( USART3, USART_IT_TXE ) == SET )\r
184         {\r
185                 /* The interrupt was caused by the TX register becoming empty.  Are \r
186                 there any more characters to transmit? */\r
187                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
188                 {\r
189                         /* A character was retrieved from the queue so can be sent to the\r
190                         USART now. */\r
191                         USART_SendData( USART3, cChar );\r
192                 }\r
193                 else\r
194                 {\r
195                         USART_ITConfig( USART3, USART_IT_TXE, DISABLE );                \r
196                 }               \r
197         }\r
198         \r
199         if( USART_GetITStatus( USART3, USART_IT_RXNE ) == SET )\r
200         {\r
201                 /* A character has been received on the USART, send it to the Rx\r
202                 handler task. */\r
203                 cChar = USART_ReceiveData( USART3 );\r
204                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
205         }       \r
206 \r
207         /* If sending or receiving from a queue has caused a task to unblock, and\r
208         the unblocked task has a priority equal to or higher than the currently \r
209         running task (the task this ISR interrupted), then xHigherPriorityTaskWoken \r
210         will have automatically been set to pdTRUE within the queue send or receive \r
211         function.  portEND_SWITCHING_ISR() will then ensure that this ISR returns \r
212         directly to the higher priority unblocked task. */\r
213         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
214 }\r
215 \r
216 \r
217 \r
218 \r
219 \r
220         \r