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_uart.c |
3 | * @file stm32f1xx_hal_uart.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 UART HAL module driver. |
7 | * @brief UART 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 Asynchronous Receiver Transmitter (UART) peripheral: |
9 | * functionalities of the Universal Asynchronous Receiver Transmitter (UART) peripheral: |
10 | * + Initialization and de-initialization functions |
10 | * + Initialization and de-initialization functions |
11 | * + IO operation functions |
11 | * + IO operation functions |
Line 125... | Line 125... | ||
125 | |
125 | |
126 | @endverbatim |
126 | @endverbatim |
127 | ****************************************************************************** |
127 | ****************************************************************************** |
128 | * @attention |
128 | * @attention |
129 | * |
129 | * |
130 | * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> |
130 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
131 | * |
131 | * |
132 | * Redistribution and use in source and binary forms, with or without modification, |
132 | * Redistribution and use in source and binary forms, with or without modification, |
133 | * are permitted provided that the following conditions are met: |
133 | * are permitted provided that the following conditions are met: |
134 | * 1. Redistributions of source code must retain the above copyright notice, |
134 | * 1. Redistributions of source code must retain the above copyright notice, |
135 | * this list of conditions and the following disclaimer. |
135 | * this list of conditions and the following disclaimer. |
Line 207... | Line 207... | ||
207 | in asynchronous mode. |
207 | in asynchronous mode. |
208 | (+) For the asynchronous mode only these parameters can be configured: |
208 | (+) For the asynchronous mode only these parameters can be configured: |
209 | (++) Baud Rate |
209 | (++) Baud Rate |
210 | (++) Word Length |
210 | (++) Word Length |
211 | (++) Stop Bit |
211 | (++) Stop Bit |
212 | (++) Parity: If the parity is enabled, then the MSB bit of the data written |
212 | (++) Parity |
213 | in the data register is transmitted but is changed by the parity bit. |
- | |
214 | Depending on the frame length defined by the M bit (8-bits or 9-bits), |
- | |
215 | the possible UART frame formats are as listed in the following table: |
- | |
216 | (+++) +-------------------------------------------------------------+ |
- | |
217 | (+++) | M bit | PCE bit | UART frame | |
- | |
218 | (+++) |---------------------|---------------------------------------| |
- | |
219 | (+++) | 0 | 0 | | SB | 8 bit data | STB | | |
- | |
220 | (+++) |---------|-----------|---------------------------------------| |
- | |
221 | (+++) | 0 | 1 | | SB | 7 bit data | PB | STB | | |
- | |
222 | (+++) |---------|-----------|---------------------------------------| |
- | |
223 | (+++) | 1 | 0 | | SB | 9 bit data | STB | | |
- | |
224 | (+++) |---------|-----------|---------------------------------------| |
- | |
225 | (+++) | 1 | 1 | | SB | 8 bit data | PB | STB | | |
- | |
226 | (+++) +-------------------------------------------------------------+ |
- | |
227 | (++) Hardware flow control |
213 | (++) Hardware flow control |
228 | (++) Receiver/transmitter modes |
214 | (++) Receiver/transmitter modes |
229 | [..] |
215 | [..] |
230 | The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init() and HAL_MultiProcessor_Init() APIs |
216 | The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init() and HAL_MultiProcessor_Init() APIs |
231 | follow respectively the UART asynchronous, UART Half duplex, LIN and Multi-Processor |
217 | follow respectively the UART asynchronous, UART Half duplex, LIN and Multi-Processor |
Line 235... | Line 221... | ||
235 | 221 | ||
236 | @endverbatim |
222 | @endverbatim |
237 | * @{ |
223 | * @{ |
238 | */ |
224 | */ |
239 | 225 | ||
- | 226 | /* |
|
- | 227 | Additionnal remark: If the parity is enabled, then the MSB bit of the data written |
|
- | 228 | in the data register is transmitted but is changed by the parity bit. |
|
- | 229 | Depending on the frame length defined by the M bit (8-bits or 9-bits), |
|
- | 230 | the possible UART frame formats are as listed in the following table: |
|
- | 231 | +-------------------------------------------------------------+ |
|
- | 232 | | M bit | PCE bit | UART frame | |
|
- | 233 | |---------------------|---------------------------------------| |
|
- | 234 | | 0 | 0 | | SB | 8 bit data | STB | | |
|
- | 235 | |---------|-----------|---------------------------------------| |
|
- | 236 | | 0 | 1 | | SB | 7 bit data | PB | STB | | |
|
- | 237 | |---------|-----------|---------------------------------------| |
|
- | 238 | | 1 | 0 | | SB | 9 bit data | STB | | |
|
- | 239 | |---------|-----------|---------------------------------------| |
|
- | 240 | | 1 | 1 | | SB | 8 bit data | PB | STB | | |
|
- | 241 | +-------------------------------------------------------------+ |
|
- | 242 | */ |
|
- | 243 | ||
240 | /** |
244 | /** |
241 | * @brief Initializes the UART mode according to the specified parameters in |
245 | * @brief Initializes the UART mode according to the specified parameters in |
242 | * the UART_InitTypeDef and create the associated handle. |
246 | * the UART_InitTypeDef and create the associated handle. |
243 | * @param huart: Pointer to a UART_HandleTypeDef structure that contains |
247 | * @param huart: Pointer to a UART_HandleTypeDef structure that contains |
244 | * the configuration information for the specified UART module. |
248 | * the configuration information for the specified UART module. |
Line 532... | Line 536... | ||
532 | * the configuration information for the specified UART module. |
536 | * the configuration information for the specified UART module. |
533 | * @retval None |
537 | * @retval None |
534 | */ |
538 | */ |
535 | __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart) |
539 | __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart) |
536 | { |
540 | { |
- | 541 | /* Prevent unused argument(s) compilation warning */ |
|
- | 542 | UNUSED(huart); |
|
537 | /* NOTE: This function should not be modified, when the callback is needed, |
543 | /* NOTE: This function should not be modified, when the callback is needed, |
538 | the HAL_UART_MspInit can be implemented in the user file |
544 | the HAL_UART_MspInit can be implemented in the user file |
539 | */ |
545 | */ |
540 | } |
546 | } |
541 | 547 | ||
Line 545... | Line 551... | ||
545 | * the configuration information for the specified UART module. |
551 | * the configuration information for the specified UART module. |
546 | * @retval None |
552 | * @retval None |
547 | */ |
553 | */ |
548 | __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart) |
554 | __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart) |
549 | { |
555 | { |
- | 556 | /* Prevent unused argument(s) compilation warning */ |
|
- | 557 | UNUSED(huart); |
|
550 | /* NOTE: This function should not be modified, when the callback is needed, |
558 | /* NOTE: This function should not be modified, when the callback is needed, |
551 | the HAL_UART_MspDeInit can be implemented in the user file |
559 | the HAL_UART_MspDeInit can be implemented in the user file |
552 | */ |
560 | */ |
553 | } |
561 | } |
554 | 562 | ||
Line 846... | Line 854... | ||
846 | } |
854 | } |
847 | 855 | ||
848 | /* Process Unlocked */ |
856 | /* Process Unlocked */ |
849 | __HAL_UNLOCK(huart); |
857 | __HAL_UNLOCK(huart); |
850 | 858 | ||
851 | /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */ |
- | |
852 | __HAL_UART_ENABLE_IT(huart, UART_IT_ERR); |
- | |
853 | - | ||
854 | /* Enable the UART Transmit data register empty Interrupt */ |
859 | /* Enable the UART Transmit data register empty Interrupt */ |
855 | __HAL_UART_ENABLE_IT(huart, UART_IT_TXE); |
860 | __HAL_UART_ENABLE_IT(huart, UART_IT_TXE); |
856 | 861 | ||
857 | return HAL_OK; |
862 | return HAL_OK; |
858 | } |
863 | } |
Line 1188... | Line 1193... | ||
1188 | tmp_flag = __HAL_UART_GET_FLAG(huart, UART_FLAG_PE); |
1193 | tmp_flag = __HAL_UART_GET_FLAG(huart, UART_FLAG_PE); |
1189 | tmp_it_source = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_PE); |
1194 | tmp_it_source = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_PE); |
1190 | /* UART parity error interrupt occurred ------------------------------------*/ |
1195 | /* UART parity error interrupt occurred ------------------------------------*/ |
1191 | if((tmp_flag != RESET) && (tmp_it_source != RESET)) |
1196 | if((tmp_flag != RESET) && (tmp_it_source != RESET)) |
1192 | { |
1197 | { |
1193 | __HAL_UART_CLEAR_PEFLAG(huart); |
- | |
1194 | - | ||
1195 | huart->ErrorCode |= HAL_UART_ERROR_PE; |
1198 | huart->ErrorCode |= HAL_UART_ERROR_PE; |
1196 | } |
1199 | } |
1197 | 1200 | ||
1198 | tmp_flag = __HAL_UART_GET_FLAG(huart, UART_FLAG_FE); |
1201 | tmp_flag = __HAL_UART_GET_FLAG(huart, UART_FLAG_FE); |
1199 | tmp_it_source = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR); |
1202 | tmp_it_source = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR); |
1200 | /* UART frame error interrupt occurred -------------------------------------*/ |
1203 | /* UART frame error interrupt occurred -------------------------------------*/ |
1201 | if((tmp_flag != RESET) && (tmp_it_source != RESET)) |
1204 | if((tmp_flag != RESET) && (tmp_it_source != RESET)) |
1202 | { |
1205 | { |
1203 | __HAL_UART_CLEAR_FEFLAG(huart); |
- | |
1204 | - | ||
1205 | huart->ErrorCode |= HAL_UART_ERROR_FE; |
1206 | huart->ErrorCode |= HAL_UART_ERROR_FE; |
1206 | } |
1207 | } |
1207 | 1208 | ||
1208 | tmp_flag = __HAL_UART_GET_FLAG(huart, UART_FLAG_NE); |
1209 | tmp_flag = __HAL_UART_GET_FLAG(huart, UART_FLAG_NE); |
1209 | /* UART noise error interrupt occurred -------------------------------------*/ |
1210 | /* UART noise error interrupt occurred -------------------------------------*/ |
1210 | if((tmp_flag != RESET) && (tmp_it_source != RESET)) |
1211 | if((tmp_flag != RESET) && (tmp_it_source != RESET)) |
1211 | { |
1212 | { |
1212 | __HAL_UART_CLEAR_NEFLAG(huart); |
- | |
1213 | - | ||
1214 | huart->ErrorCode |= HAL_UART_ERROR_NE; |
1213 | huart->ErrorCode |= HAL_UART_ERROR_NE; |
1215 | } |
1214 | } |
1216 | 1215 | ||
1217 | tmp_flag = __HAL_UART_GET_FLAG(huart, UART_FLAG_ORE); |
1216 | tmp_flag = __HAL_UART_GET_FLAG(huart, UART_FLAG_ORE); |
1218 | /* UART Over-Run interrupt occurred ----------------------------------------*/ |
1217 | /* UART Over-Run interrupt occurred ----------------------------------------*/ |
1219 | if((tmp_flag != RESET) && (tmp_it_source != RESET)) |
1218 | if((tmp_flag != RESET) && (tmp_it_source != RESET)) |
1220 | { |
1219 | { |
1221 | __HAL_UART_CLEAR_OREFLAG(huart); |
- | |
1222 | - | ||
1223 | huart->ErrorCode |= HAL_UART_ERROR_ORE; |
1220 | huart->ErrorCode |= HAL_UART_ERROR_ORE; |
1224 | } |
1221 | } |
1225 | 1222 | ||
1226 | tmp_flag = __HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE); |
1223 | tmp_flag = __HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE); |
1227 | tmp_it_source = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE); |
1224 | tmp_it_source = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE); |
Line 1247... | Line 1244... | ||
1247 | UART_EndTransmit_IT(huart); |
1244 | UART_EndTransmit_IT(huart); |
1248 | } |
1245 | } |
1249 | 1246 | ||
1250 | if(huart->ErrorCode != HAL_UART_ERROR_NONE) |
1247 | if(huart->ErrorCode != HAL_UART_ERROR_NONE) |
1251 | { |
1248 | { |
- | 1249 | /* Clear all the error flag at once */ |
|
- | 1250 | __HAL_UART_CLEAR_PEFLAG(huart); |
|
- | 1251 | ||
1252 | /* Set the UART state ready to be able to start again the process */ |
1252 | /* Set the UART state ready to be able to start again the process */ |
1253 | huart->State = HAL_UART_STATE_READY; |
1253 | huart->State = HAL_UART_STATE_READY; |
1254 | 1254 | ||
1255 | HAL_UART_ErrorCallback(huart); |
1255 | HAL_UART_ErrorCallback(huart); |
1256 | } |
1256 | } |
Line 1262... | Line 1262... | ||
1262 | * the configuration information for the specified UART module. |
1262 | * the configuration information for the specified UART module. |
1263 | * @retval None |
1263 | * @retval None |
1264 | */ |
1264 | */ |
1265 | __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) |
1265 | __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) |
1266 | { |
1266 | { |
- | 1267 | /* Prevent unused argument(s) compilation warning */ |
|
- | 1268 | UNUSED(huart); |
|
1267 | /* NOTE: This function should not be modified, when the callback is needed, |
1269 | /* NOTE: This function should not be modified, when the callback is needed, |
1268 | the HAL_UART_TxCpltCallback can be implemented in the user file |
1270 | the HAL_UART_TxCpltCallback can be implemented in the user file |
1269 | */ |
1271 | */ |
1270 | } |
1272 | } |
1271 | 1273 | ||
Line 1275... | Line 1277... | ||
1275 | * the configuration information for the specified UART module. |
1277 | * the configuration information for the specified UART module. |
1276 | * @retval None |
1278 | * @retval None |
1277 | */ |
1279 | */ |
1278 | __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart) |
1280 | __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart) |
1279 | { |
1281 | { |
- | 1282 | /* Prevent unused argument(s) compilation warning */ |
|
- | 1283 | UNUSED(huart); |
|
1280 | /* NOTE: This function should not be modified, when the callback is needed, |
1284 | /* NOTE: This function should not be modified, when the callback is needed, |
1281 | the HAL_UART_TxHalfCpltCallback can be implemented in the user file |
1285 | the HAL_UART_TxHalfCpltCallback can be implemented in the user file |
1282 | */ |
1286 | */ |
1283 | } |
1287 | } |
1284 | 1288 | ||
Line 1288... | Line 1292... | ||
1288 | * the configuration information for the specified UART module. |
1292 | * the configuration information for the specified UART module. |
1289 | * @retval None |
1293 | * @retval None |
1290 | */ |
1294 | */ |
1291 | __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) |
1295 | __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) |
1292 | { |
1296 | { |
- | 1297 | /* Prevent unused argument(s) compilation warning */ |
|
- | 1298 | UNUSED(huart); |
|
1293 | /* NOTE: This function should not be modified, when the callback is needed, |
1299 | /* NOTE: This function should not be modified, when the callback is needed, |
1294 | the HAL_UART_RxCpltCallback can be implemented in the user file |
1300 | the HAL_UART_RxCpltCallback can be implemented in the user file |
1295 | */ |
1301 | */ |
1296 | } |
1302 | } |
1297 | 1303 | ||
Line 1301... | Line 1307... | ||
1301 | * the configuration information for the specified UART module. |
1307 | * the configuration information for the specified UART module. |
1302 | * @retval None |
1308 | * @retval None |
1303 | */ |
1309 | */ |
1304 | __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart) |
1310 | __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart) |
1305 | { |
1311 | { |
- | 1312 | /* Prevent unused argument(s) compilation warning */ |
|
- | 1313 | UNUSED(huart); |
|
1306 | /* NOTE: This function should not be modified, when the callback is needed, |
1314 | /* NOTE: This function should not be modified, when the callback is needed, |
1307 | the HAL_UART_RxHalfCpltCallback can be implemented in the user file |
1315 | the HAL_UART_RxHalfCpltCallback can be implemented in the user file |
1308 | */ |
1316 | */ |
1309 | } |
1317 | } |
1310 | 1318 | ||
Line 1314... | Line 1322... | ||
1314 | * the configuration information for the specified UART module. |
1322 | * the configuration information for the specified UART module. |
1315 | * @retval None |
1323 | * @retval None |
1316 | */ |
1324 | */ |
1317 | __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) |
1325 | __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) |
1318 | { |
1326 | { |
- | 1327 | /* Prevent unused argument(s) compilation warning */ |
|
- | 1328 | UNUSED(huart); |
|
1319 | /* NOTE: This function should not be modified, when the callback is needed, |
1329 | /* NOTE: This function should not be modified, when the callback is needed, |
1320 | the HAL_UART_ErrorCallback can be implemented in the user file |
1330 | the HAL_UART_ErrorCallback can be implemented in the user file |
1321 | */ |
1331 | */ |
1322 | } |
1332 | } |
1323 | 1333 | ||
Line 1769... | Line 1779... | ||
1769 | { |
1779 | { |
1770 | huart->State = HAL_UART_STATE_BUSY_RX; |
1780 | huart->State = HAL_UART_STATE_BUSY_RX; |
1771 | } |
1781 | } |
1772 | else |
1782 | else |
1773 | { |
1783 | { |
1774 | /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */ |
- | |
1775 | __HAL_UART_DISABLE_IT(huart, UART_IT_ERR); |
- | |
1776 | - | ||
1777 | huart->State = HAL_UART_STATE_READY; |
1784 | huart->State = HAL_UART_STATE_READY; |
1778 | } |
1785 | } |
1779 | 1786 | ||
1780 | HAL_UART_TxCpltCallback(huart); |
1787 | HAL_UART_TxCpltCallback(huart); |
1781 | 1788 |