你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

求大神解答:STM32L452RET6 UART4的配置问题

[复制链接]
包子不 提问时间:2017-7-13 15:59 /
悬赏1ST金币未解决
使用STM32CubeMX进行硬件配置UART4时,如果配置PC10 & PC11位串口的收发,配置如下:
  1. void HAL_UART_MspInit(UART_HandleTypeDef* huart)
  2. {

  3.   GPIO_InitTypeDef GPIO_InitStruct;
  4.   if(huart->Instance==UART4)
  5.   {
  6.   /* USER CODE BEGIN UART4_MspInit 0 */

  7.   /* USER CODE END UART4_MspInit 0 */
  8.     /* Peripheral clock enable */
  9.     __HAL_RCC_UART4_CLK_ENABLE();
  10.   
  11.     /**UART4 GPIO Configuration   
  12.     PC10     ------> UART4_TX
  13.     PC11     ------> UART4_RX
  14.     */
  15.     GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
  16.     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  17.     GPIO_InitStruct.Pull = GPIO_PULLUP;
  18.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  19.     GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
  20.     HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  21.   /* USER CODE BEGIN UART4_MspInit 1 */

  22.   /* USER CODE END UART4_MspInit 1 */
  23.   }

  24. }
复制代码
如果选择PA0 & PA1为串口收发IO,配置如下:
  1. void HAL_UART_MspInit(UART_HandleTypeDef* huart)
  2. {

  3.   GPIO_InitTypeDef GPIO_InitStruct;
  4.   if(huart->Instance==UART4)
  5.   {
  6.   /* USER CODE BEGIN UART4_MspInit 0 */

  7.   /* USER CODE END UART4_MspInit 0 */
  8.     /* Peripheral clock enable */
  9.     __HAL_RCC_UART4_CLK_ENABLE();
  10.   
  11.     /**UART4 GPIO Configuration   
  12.     PA0     ------> UART4_TX
  13.     PA1     ------> UART4_RX
  14.     */
  15.     GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
  16.     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  17.     GPIO_InitStruct.Pull = GPIO_PULLUP;
  18.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  19.     GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
  20.     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  21.   /* USER CODE BEGIN UART4_MspInit 1 */

  22.   /* USER CODE END UART4_MspInit 1 */
  23.   }

  24. }
复制代码


而根据官方STM32L452的datasheet,AF8对应的复用功能有UART4,同时对应的有PA0 & PA1和 PC10 & PC11。
1111.png

22222222.png
那么问题是,单片机是如何用户设置的UART4引脚对应的是PA0 & PA1还是 PC10 & PC11?
郁闷一天了。


收藏 评论5 发布时间:2017-7-13 15:59

举报

5个回答
包子不 回答时间:2017-7-13 16:55:47

沉下心,仔细查看HAL库函数源码,HAL_GPIO_Init()函数中
  1. /**
  2.   * @brief  Initialize the GPIOx peripheral according to the specified parameters in the GPIO_Init.
  3.   * @param  GPIOx: where x can be (A..H) to select the GPIO peripheral for STM32L4 family
  4.   * @param  GPIO_Init: pointer to a GPIO_InitTypeDef structure that contains
  5.   *         the configuration information for the specified GPIO peripheral.
  6.   * @retval None
  7.   */
  8. void HAL_GPIO_Init(GPIO_TypeDef  *GPIOx, GPIO_InitTypeDef *GPIO_Init)
  9. {
  10.   uint32_t position = 0x00;
  11.   uint32_t iocurrent = 0x00;
  12.   uint32_t temp = 0x00;
  13.   /* Check the parameters */
  14.   assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
  15.   assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
  16.   assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
  17.   assert_param(IS_GPIO_PULL(GPIO_Init->Pull));

  18.   /* Configure the port pins */
  19.   while (((GPIO_Init->Pin) >> position) != RESET)
  20.   {
  21.     /* Get current io position */
  22.     iocurrent = (GPIO_Init->Pin) & (1U << position);

  23.     if(iocurrent)
  24.     {
  25.       /*--------------------- GPIO Mode Config<span style="line-height: 1.5;">uration ------------------------*/</span>
复制代码
根据初始化不同port口,进行不同的初始化,对不同的寄存器赋值。
仿真结果如下:
AFRH 或AFRL寄存器的值都是AF8(0x8),bit位不同。
333333.png
最后把PA 或者PC作为UART4复用功能IO。



菜鸟见解,大神看到多多指教!
MrJiu 回答时间:2017-7-14 09:29:37
你的解释是对的,功能的映射有专门的寄存器!!!也就是你说的AF,其实可以看一看参考手册,里面介绍的比较详细!!!!
包子不 回答时间:2017-7-17 10:58:01
MrJiu 发表于 2017-7-14 09:29
你的解释是对的,功能的映射有专门的寄存器!!!也就是你说的AF,其实可以看一看参考手册,里面介绍的比较 ...

多谢指导
爱电子辉辉 回答时间:2017-9-4 10:26:27
请问楼主,你的串口4配置好了。可以正常使用吗?我的程序,只要串口4一进入中断,程序就卡死了。请问这个是什么问题呢,本人菜鸟一枚,望指教。。
包子不 回答时间:2017-9-28 17:28:43
爱电子辉辉 发表于 2017-9-4 10:26
请问楼主,你的串口4配置好了。可以正常使用吗?我的程序,只要串口4一进入中断,程序就卡死了。请问这个是 ...

卡死是什么情况?
有没有清除中断标志位?
关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版