Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2 | Rev 5 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | /** |
1 | /** |
2 | ****************************************************************************** |
2 | ****************************************************************************** |
3 | * @file stm32f1xx_hal_usart.c |
3 | * @file stm32f1xx_hal_usart.c |
4 | * @author MCD Application Team |
4 | * @author MCD Application Team |
5 | * @version V1.0.1 |
5 | * @version V1.0.4 |
6 | * @date 31-July-2015 |
6 | * @date 29-April-2016 |
7 | * @brief USART HAL module driver. |
7 | * @brief USART HAL module driver. |
8 | * This file provides firmware functions to manage the following |
8 | * This file provides firmware functions to manage the following |
9 | * functionalities of the Universal Synchronous Asynchronous Receiver Transmitter (USART) peripheral: |
9 | * functionalities of the Universal Synchronous Asynchronous Receiver Transmitter (USART) peripheral: |
10 | * + Initialization and de-initialization functions |
10 | * + Initialization and de-initialization functions |
11 | * + IO operation functions |
11 | * + IO operation functions |
Line 107... | Line 107... | ||
107 | 107 | ||
108 | @endverbatim |
108 | @endverbatim |
109 | ****************************************************************************** |
109 | ****************************************************************************** |
110 | * @attention |
110 | * @attention |
111 | * |
111 | * |
112 | * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> |
112 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
113 | * |
113 | * |
114 | * Redistribution and use in source and binary forms, with or without modification, |
114 | * Redistribution and use in source and binary forms, with or without modification, |
115 | * are permitted provided that the following conditions are met: |
115 | * are permitted provided that the following conditions are met: |
116 | * 1. Redistributions of source code must retain the above copyright notice, |
116 | * 1. Redistributions of source code must retain the above copyright notice, |
117 | * this list of conditions and the following disclaimer. |
117 | * this list of conditions and the following disclaimer. |
Line 198... | Line 198... | ||
198 | in asynchronous and in synchronous modes. |
198 | in asynchronous and in synchronous modes. |
199 | (+) For the asynchronous mode only these parameters can be configured: |
199 | (+) For the asynchronous mode only these parameters can be configured: |
200 | (++) Baud Rate |
200 | (++) Baud Rate |
201 | (++) Word Length |
201 | (++) Word Length |
202 | (++) Stop Bit |
202 | (++) Stop Bit |
203 | (++) Parity: If the parity is enabled, then the MSB bit of the data written |
203 | (++) Parity |
204 | in the data register is transmitted but is changed by the parity bit. |
- | |
205 | Depending on the frame length defined by the M bit (8-bits or 9-bits), |
- | |
206 | the possible USART frame formats are as listed in the following table: |
- | |
207 | (+++) +-------------------------------------------------------------+ |
- | |
208 | (+++) | M bit | PCE bit | USART frame | |
- | |
209 | (+++) |---------------------|---------------------------------------| |
- | |
210 | (+++) | 0 | 0 | | SB | 8 bit data | STB | | |
- | |
211 | (+++) |---------|-----------|---------------------------------------| |
- | |
212 | (+++) | 0 | 1 | | SB | 7 bit data | PB | STB | | |
- | |
213 | (+++) |---------|-----------|---------------------------------------| |
- | |
214 | (+++) | 1 | 0 | | SB | 9 bit data | STB | | |
- | |
215 | (+++) |---------|-----------|---------------------------------------| |
- | |
216 | (+++) | 1 | 1 | | SB | 8 bit data | PB | STB | | |
- | |
217 | (+++) +-------------------------------------------------------------+ |
- | |
218 | (++) USART polarity |
204 | (++) USART polarity |
219 | (++) USART phase |
205 | (++) USART phase |
220 | (++) USART LastBit |
206 | (++) USART LastBit |
221 | (++) Receiver/transmitter modes |
207 | (++) Receiver/transmitter modes |
222 | 208 | ||
Line 226... | Line 212... | ||
226 | (RM0008 for STM32F10Xxx MCUs and RM0041 for STM32F100xx MCUs)). |
212 | (RM0008 for STM32F10Xxx MCUs and RM0041 for STM32F100xx MCUs)). |
227 | 213 | ||
228 | @endverbatim |
214 | @endverbatim |
229 | * @{ |
215 | * @{ |
230 | */ |
216 | */ |
- | 217 | ||
- | 218 | /* |
|
- | 219 | Additionnal remark: If the parity is enabled, then the MSB bit of the data written |
|
- | 220 | in the data register is transmitted but is changed by the parity bit. |
|
- | 221 | Depending on the frame length defined by the M bit (8-bits or 9-bits), |
|
- | 222 | the possible USART frame formats are as listed in the following table: |
|
- | 223 | +-------------------------------------------------------------+ |
|
- | 224 | | M bit | PCE bit | USART frame | |
|
- | 225 | |---------------------|---------------------------------------| |
|
- | 226 | | 0 | 0 | | SB | 8 bit data | STB | | |
|
- | 227 | |---------|-----------|---------------------------------------| |
|
- | 228 | | 0 | 1 | | SB | 7 bit data | PB | STB | | |
|
- | 229 | |---------|-----------|---------------------------------------| |
|
- | 230 | | 1 | 0 | | SB | 9 bit data | STB | | |
|
- | 231 | |---------|-----------|---------------------------------------| |
|
- | 232 | | 1 | 1 | | SB | 8 bit data | PB | STB | | |
|
- | 233 | +-------------------------------------------------------------+ |
|
- | 234 | */ |
|
231 | 235 | ||
232 | /** |
236 | /** |
233 | * @brief Initializes the USART mode according to the specified |
237 | * @brief Initializes the USART mode according to the specified |
234 | * parameters in the USART_InitTypeDef and create the associated handle. |
238 | * parameters in the USART_InitTypeDef and create the associated handle. |
235 | * @param husart: Pointer to a USART_HandleTypeDef structure that contains |
239 | * @param husart: Pointer to a USART_HandleTypeDef structure that contains |
Line 317... | Line 321... | ||
317 | * the configuration information for the specified USART module. |
321 | * the configuration information for the specified USART module. |
318 | * @retval None |
322 | * @retval None |
319 | */ |
323 | */ |
320 | __weak void HAL_USART_MspInit(USART_HandleTypeDef *husart) |
324 | __weak void HAL_USART_MspInit(USART_HandleTypeDef *husart) |
321 | { |
325 | { |
- | 326 | /* Prevent unused argument(s) compilation warning */ |
|
- | 327 | UNUSED(husart); |
|
322 | /* NOTE: This function should not be modified, when the callback is needed, |
328 | /* NOTE: This function should not be modified, when the callback is needed, |
323 | the HAL_USART_MspInit can be implemented in the user file |
329 | the HAL_USART_MspInit can be implemented in the user file |
324 | */ |
330 | */ |
325 | } |
331 | } |
326 | 332 | ||
Line 330... | Line 336... | ||
330 | * the configuration information for the specified USART module. |
336 | * the configuration information for the specified USART module. |
331 | * @retval None |
337 | * @retval None |
332 | */ |
338 | */ |
333 | __weak void HAL_USART_MspDeInit(USART_HandleTypeDef *husart) |
339 | __weak void HAL_USART_MspDeInit(USART_HandleTypeDef *husart) |
334 | { |
340 | { |
- | 341 | /* Prevent unused argument(s) compilation warning */ |
|
- | 342 | UNUSED(husart); |
|
335 | /* NOTE: This function should not be modified, when the callback is needed, |
343 | /* NOTE: This function should not be modified, when the callback is needed, |
336 | the HAL_USART_MspDeInit can be implemented in the user file |
344 | the HAL_USART_MspDeInit can be implemented in the user file |
337 | */ |
345 | */ |
338 | } |
346 | } |
339 | 347 | ||
Line 1144... | Line 1152... | ||
1144 | tmp_flag = __HAL_USART_GET_FLAG(husart, USART_FLAG_PE); |
1152 | tmp_flag = __HAL_USART_GET_FLAG(husart, USART_FLAG_PE); |
1145 | tmp_it_source = __HAL_USART_GET_IT_SOURCE(husart, USART_IT_PE); |
1153 | tmp_it_source = __HAL_USART_GET_IT_SOURCE(husart, USART_IT_PE); |
1146 | /* USART parity error interrupt occurred -----------------------------------*/ |
1154 | /* USART parity error interrupt occurred -----------------------------------*/ |
1147 | if((tmp_flag != RESET) && (tmp_it_source != RESET)) |
1155 | if((tmp_flag != RESET) && (tmp_it_source != RESET)) |
1148 | { |
1156 | { |
1149 | __HAL_USART_CLEAR_PEFLAG(husart); |
- | |
1150 | husart->ErrorCode |= HAL_USART_ERROR_PE; |
1157 | husart->ErrorCode |= HAL_USART_ERROR_PE; |
1151 | } |
1158 | } |
1152 | 1159 | ||
1153 | tmp_flag = __HAL_USART_GET_FLAG(husart, USART_FLAG_FE); |
1160 | tmp_flag = __HAL_USART_GET_FLAG(husart, USART_FLAG_FE); |
1154 | tmp_it_source = __HAL_USART_GET_IT_SOURCE(husart, USART_IT_ERR); |
1161 | tmp_it_source = __HAL_USART_GET_IT_SOURCE(husart, USART_IT_ERR); |
1155 | /* USART frame error interrupt occurred ------------------------------------*/ |
1162 | /* USART frame error interrupt occurred ------------------------------------*/ |
1156 | if((tmp_flag != RESET) && (tmp_it_source != RESET)) |
1163 | if((tmp_flag != RESET) && (tmp_it_source != RESET)) |
1157 | { |
1164 | { |
1158 | __HAL_USART_CLEAR_FEFLAG(husart); |
- | |
1159 | husart->ErrorCode |= HAL_USART_ERROR_FE; |
1165 | husart->ErrorCode |= HAL_USART_ERROR_FE; |
1160 | } |
1166 | } |
1161 | 1167 | ||
1162 | tmp_flag = __HAL_USART_GET_FLAG(husart, USART_FLAG_NE); |
1168 | tmp_flag = __HAL_USART_GET_FLAG(husart, USART_FLAG_NE); |
1163 | /* USART noise error interrupt occurred ------------------------------------*/ |
1169 | /* USART noise error interrupt occurred ------------------------------------*/ |
1164 | if((tmp_flag != RESET) && (tmp_it_source != RESET)) |
1170 | if((tmp_flag != RESET) && (tmp_it_source != RESET)) |
1165 | { |
1171 | { |
1166 | __HAL_USART_CLEAR_NEFLAG(husart); |
- | |
1167 | husart->ErrorCode |= HAL_USART_ERROR_NE; |
1172 | husart->ErrorCode |= HAL_USART_ERROR_NE; |
1168 | } |
1173 | } |
1169 | 1174 | ||
1170 | tmp_flag = __HAL_USART_GET_FLAG(husart, USART_FLAG_ORE); |
1175 | tmp_flag = __HAL_USART_GET_FLAG(husart, USART_FLAG_ORE); |
1171 | /* USART Over-Run interrupt occurred ---------------------------------------*/ |
1176 | /* USART Over-Run interrupt occurred ---------------------------------------*/ |
1172 | if((tmp_flag != RESET) && (tmp_it_source != RESET)) |
1177 | if((tmp_flag != RESET) && (tmp_it_source != RESET)) |
1173 | { |
1178 | { |
1174 | __HAL_USART_CLEAR_OREFLAG(husart); |
- | |
1175 | husart->ErrorCode |= HAL_USART_ERROR_ORE; |
1179 | husart->ErrorCode |= HAL_USART_ERROR_ORE; |
1176 | } |
1180 | } |
1177 | 1181 | ||
1178 | if(husart->ErrorCode != HAL_USART_ERROR_NONE) |
1182 | if(husart->ErrorCode != HAL_USART_ERROR_NONE) |
1179 | { |
1183 | { |
- | 1184 | /* Clear all the error flag at once */ |
|
- | 1185 | __HAL_USART_CLEAR_PEFLAG(husart); |
|
- | 1186 | ||
1180 | /* Set the USART state ready to be able to start again the process */ |
1187 | /* Set the USART state ready to be able to start again the process */ |
1181 | husart->State = HAL_USART_STATE_READY; |
1188 | husart->State = HAL_USART_STATE_READY; |
1182 | 1189 | ||
1183 | HAL_USART_ErrorCallback(husart); |
1190 | HAL_USART_ErrorCallback(husart); |
1184 | } |
1191 | } |
Line 1230... | Line 1237... | ||
1230 | * the configuration information for the specified USART module. |
1237 | * the configuration information for the specified USART module. |
1231 | * @retval None |
1238 | * @retval None |
1232 | */ |
1239 | */ |
1233 | __weak void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart) |
1240 | __weak void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart) |
1234 | { |
1241 | { |
- | 1242 | /* Prevent unused argument(s) compilation warning */ |
|
- | 1243 | UNUSED(husart); |
|
1235 | /* NOTE: This function should not be modified, when the callback is needed, |
1244 | /* NOTE: This function should not be modified, when the callback is needed, |
1236 | the HAL_USART_TxCpltCallback can be implemented in the user file |
1245 | the HAL_USART_TxCpltCallback can be implemented in the user file |
1237 | */ |
1246 | */ |
1238 | } |
1247 | } |
1239 | 1248 | ||
Line 1243... | Line 1252... | ||
1243 | * the configuration information for the specified USART module. |
1252 | * the configuration information for the specified USART module. |
1244 | * @retval None |
1253 | * @retval None |
1245 | */ |
1254 | */ |
1246 | __weak void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart) |
1255 | __weak void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart) |
1247 | { |
1256 | { |
- | 1257 | /* Prevent unused argument(s) compilation warning */ |
|
- | 1258 | UNUSED(husart); |
|
1248 | /* NOTE: This function should not be modified, when the callback is needed, |
1259 | /* NOTE: This function should not be modified, when the callback is needed, |
1249 | the HAL_USART_TxHalfCpltCallback can be implemented in the user file |
1260 | the HAL_USART_TxHalfCpltCallback can be implemented in the user file |
1250 | */ |
1261 | */ |
1251 | } |
1262 | } |
1252 | 1263 | ||
Line 1256... | Line 1267... | ||
1256 | * the configuration information for the specified USART module. |
1267 | * the configuration information for the specified USART module. |
1257 | * @retval None |
1268 | * @retval None |
1258 | */ |
1269 | */ |
1259 | __weak void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart) |
1270 | __weak void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart) |
1260 | { |
1271 | { |
- | 1272 | /* Prevent unused argument(s) compilation warning */ |
|
- | 1273 | UNUSED(husart); |
|
1261 | /* NOTE: This function should not be modified, when the callback is needed, |
1274 | /* NOTE: This function should not be modified, when the callback is needed, |
1262 | the HAL_USART_RxCpltCallback can be implemented in the user file |
1275 | the HAL_USART_RxCpltCallback can be implemented in the user file |
1263 | */ |
1276 | */ |
1264 | } |
1277 | } |
1265 | 1278 | ||
Line 1269... | Line 1282... | ||
1269 | * the configuration information for the specified USART module. |
1282 | * the configuration information for the specified USART module. |
1270 | * @retval None |
1283 | * @retval None |
1271 | */ |
1284 | */ |
1272 | __weak void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart) |
1285 | __weak void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart) |
1273 | { |
1286 | { |
- | 1287 | /* Prevent unused argument(s) compilation warning */ |
|
- | 1288 | UNUSED(husart); |
|
1274 | /* NOTE: This function should not be modified, when the callback is needed, |
1289 | /* NOTE: This function should not be modified, when the callback is needed, |
1275 | the HAL_USART_RxHalfCpltCallback can be implemented in the user file |
1290 | the HAL_USART_RxHalfCpltCallback can be implemented in the user file |
1276 | */ |
1291 | */ |
1277 | } |
1292 | } |
1278 | 1293 | ||
Line 1282... | Line 1297... | ||
1282 | * the configuration information for the specified USART module. |
1297 | * the configuration information for the specified USART module. |
1283 | * @retval None |
1298 | * @retval None |
1284 | */ |
1299 | */ |
1285 | __weak void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart) |
1300 | __weak void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart) |
1286 | { |
1301 | { |
- | 1302 | /* Prevent unused argument(s) compilation warning */ |
|
- | 1303 | UNUSED(husart); |
|
1287 | /* NOTE: This function should not be modified, when the callback is needed, |
1304 | /* NOTE: This function should not be modified, when the callback is needed, |
1288 | the HAL_USART_TxRxCpltCallback can be implemented in the user file |
1305 | the HAL_USART_TxRxCpltCallback can be implemented in the user file |
1289 | */ |
1306 | */ |
1290 | } |
1307 | } |
1291 | 1308 | ||
Line 1295... | Line 1312... | ||
1295 | * the configuration information for the specified USART module. |
1312 | * the configuration information for the specified USART module. |
1296 | * @retval None |
1313 | * @retval None |
1297 | */ |
1314 | */ |
1298 | __weak void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart) |
1315 | __weak void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart) |
1299 | { |
1316 | { |
- | 1317 | /* Prevent unused argument(s) compilation warning */ |
|
- | 1318 | UNUSED(husart); |
|
1300 | /* NOTE: This function should not be modified, when the callback is needed, |
1319 | /* NOTE: This function should not be modified, when the callback is needed, |
1301 | the HAL_USART_ErrorCallback can be implemented in the user file |
1320 | the HAL_USART_ErrorCallback can be implemented in the user file |
1302 | */ |
1321 | */ |
1303 | } |
1322 | } |
1304 | 1323 |