]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_STM32F103_IAR/serial/serial.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTEX_STM32F103_IAR / 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 UART0.\r
31 */\r
32 \r
33 /* Scheduler includes. */\r
34 #include "FreeRTOS.h"\r
35 #include "queue.h"\r
36 #include "semphr.h"\r
37 \r
38 /* Library includes. */\r
39 #include "stm32f10x_lib.h"\r
40 \r
41 /* Demo application includes. */\r
42 #include "serial.h"\r
43 /*-----------------------------------------------------------*/\r
44 \r
45 /* Misc defines. */\r
46 #define serINVALID_QUEUE                                ( ( QueueHandle_t ) 0 )\r
47 #define serNO_BLOCK                                             ( ( TickType_t ) 0 )\r
48 #define serTX_BLOCK_TIME                                ( 40 / portTICK_PERIOD_MS )\r
49 \r
50 /*-----------------------------------------------------------*/\r
51 \r
52 /* The queue used to hold received characters. */\r
53 static QueueHandle_t xRxedChars;\r
54 static QueueHandle_t xCharsForTx;\r
55 \r
56 /*-----------------------------------------------------------*/\r
57 \r
58 /* UART interrupt handler. */\r
59 void vUARTInterruptHandler( void );\r
60 \r
61 /*-----------------------------------------------------------*/\r
62 \r
63 /*\r
64  * See the serial2.h header file.\r
65  */\r
66 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
67 {\r
68 xComPortHandle xReturn;\r
69 USART_InitTypeDef USART_InitStructure;\r
70 NVIC_InitTypeDef NVIC_InitStructure;\r
71 GPIO_InitTypeDef GPIO_InitStructure;\r
72 \r
73         /* Create the queues used to hold Rx/Tx characters. */\r
74         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
75         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
76         \r
77         /* If the queue/semaphore was created correctly then setup the serial port\r
78         hardware. */\r
79         if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )\r
80         {\r
81                 /* Enable USART1 clock */\r
82                 RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE ); \r
83 \r
84                 /* Configure USART1 Rx (PA10) as input floating */\r
85                 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;\r
86                 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;\r
87                 GPIO_Init( GPIOA, &GPIO_InitStructure );\r
88                 \r
89                 /* Configure USART1 Tx (PA9) as alternate function push-pull */\r
90                 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;\r
91                 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;\r
92                 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;\r
93                 GPIO_Init( GPIOA, &GPIO_InitStructure );\r
94 \r
95                 USART_InitStructure.USART_BaudRate = ulWantedBaud;\r
96                 USART_InitStructure.USART_WordLength = USART_WordLength_8b;\r
97                 USART_InitStructure.USART_StopBits = USART_StopBits_1;\r
98                 USART_InitStructure.USART_Parity = USART_Parity_No ;\r
99                 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;\r
100                 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;\r
101                 USART_InitStructure.USART_Clock = USART_Clock_Disable;\r
102                 USART_InitStructure.USART_CPOL = USART_CPOL_Low;\r
103                 USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;\r
104                 USART_InitStructure.USART_LastBit = USART_LastBit_Disable;\r
105                 \r
106                 USART_Init( USART1, &USART_InitStructure );\r
107                 \r
108                 USART_ITConfig( USART1, USART_IT_RXNE, ENABLE );\r
109                 \r
110                 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;\r
111                 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configLIBRARY_KERNEL_INTERRUPT_PRIORITY;\r
112                 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;\r
113                 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;\r
114                 NVIC_Init( &NVIC_InitStructure );\r
115                 \r
116                 USART_Cmd( USART1, ENABLE );            \r
117         }\r
118         else\r
119         {\r
120                 xReturn = ( xComPortHandle ) 0;\r
121         }\r
122 \r
123         /* This demo file only supports a single port but we have to return\r
124         something to comply with the standard demo header file. */\r
125         return xReturn;\r
126 }\r
127 /*-----------------------------------------------------------*/\r
128 \r
129 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
130 {\r
131         /* The port handle is not required as this driver only supports one port. */\r
132         ( void ) pxPort;\r
133 \r
134         /* Get the next character from the buffer.  Return false if no characters\r
135         are available, or arrive before xBlockTime expires. */\r
136         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
137         {\r
138                 return pdTRUE;\r
139         }\r
140         else\r
141         {\r
142                 return pdFALSE;\r
143         }\r
144 }\r
145 /*-----------------------------------------------------------*/\r
146 \r
147 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
148 {\r
149 signed char *pxNext;\r
150 \r
151         /* A couple of parameters that this port does not use. */\r
152         ( void ) usStringLength;\r
153         ( void ) pxPort;\r
154 \r
155         /* NOTE: This implementation does not handle the queue being full as no\r
156         block time is used! */\r
157 \r
158         /* The port handle is not required as this driver only supports UART1. */\r
159         ( void ) pxPort;\r
160 \r
161         /* Send each character in the string, one at a time. */\r
162         pxNext = ( signed char * ) pcString;\r
163         while( *pxNext )\r
164         {\r
165                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
166                 pxNext++;\r
167         }\r
168 }\r
169 /*-----------------------------------------------------------*/\r
170 \r
171 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
172 {\r
173 signed portBASE_TYPE xReturn;\r
174 \r
175         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS )\r
176         {\r
177                 xReturn = pdPASS;\r
178                 USART_ITConfig( USART1, USART_IT_TXE, ENABLE );\r
179         }\r
180         else\r
181         {\r
182                 xReturn = pdFAIL;\r
183         }\r
184 \r
185         return xReturn;\r
186 }\r
187 /*-----------------------------------------------------------*/\r
188 \r
189 void vSerialClose( xComPortHandle xPort )\r
190 {\r
191         /* Not supported as not required by the demo application. */\r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 void vUARTInterruptHandler( void )\r
196 {\r
197 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
198 char cChar;\r
199 \r
200         if( USART_GetITStatus( USART1, USART_IT_TXE ) == SET )\r
201         {\r
202                 /* The interrupt was caused by the THR becoming empty.  Are there any\r
203                 more characters to transmit? */\r
204                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
205                 {\r
206                         /* A character was retrieved from the queue so can be sent to the\r
207                         THR now. */\r
208                         USART_SendData( USART1, cChar );\r
209                 }\r
210                 else\r
211                 {\r
212                         USART_ITConfig( USART1, USART_IT_TXE, DISABLE );                \r
213                 }               \r
214         }\r
215         \r
216         if( USART_GetITStatus( USART1, USART_IT_RXNE ) == SET )\r
217         {\r
218                 cChar = USART_ReceiveData( USART1 );\r
219                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
220         }       \r
221         \r
222         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
223 }\r
224 \r
225 \r
226 \r
227 \r
228 \r
229         \r