[STM32F1]

关于STM32的GPIO的AF配置的疑问

[复制链接]
9522|4
手机看帖
扫描二维码
随时随地手机跟帖
zhjerry|  楼主 | 2015-7-20 16:47 | 显示全部楼层 |阅读模式

最近在调试STM32的I2S,偶然发现其GPIO的AF配置有问题,进一步追踪发现问题不那么简单,具体描述如下:
1. 从GPIO的管脚结构图中我们得知,如果启用GPIO_Mode_AF_PP的设置,该管脚的电平将被内部电平强制推拉,因此这种状态不可以用于输入功能,除了功能可能不正常外,甚至有拉坏自己的管脚以及外设备管脚的危险,当然长期工作一定是不可能的。
2. 看到我以前的程序里面使用SPI的管脚设置是这样的,  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
显然把MISO输入脚配置为GPIO_Mode_AF_PP,吓得我一声冷汗。这个代码使用了N多年生产了N多产品,都带有严重的问题出厂了?


3.回头想追踪这个配置方法的来源,大家都是STM的学生,原始来源毋庸置疑是谁,关键是STM对这个问题有没有认识?
   我的代码来源于2009年的V3.00的代码库。
   找到2011年出的V3.50代码库,找到SPI部分发现人家已经改了(中间的版本没有找到,大家有兴趣可以追一下从那时开始改的):
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  /* Confugure MISO pin as Input Floating  */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

但是同一库内的I2S例程中,还是把输入按照GPIO_Mode_AF_PP配置:
(I2S_SPI_I2S_Switch SPI_I2S_Switch example,I2S2 is configured as master transmitter and I2S3 as slave receiver)

void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
#ifdef USE_STM3210E_EVAL
  /* Disable the JTAG interface and enable the SWJ interface
      This operation is not necessary for Connectivity Line devices since
      SPI3 I/Os can be remapped on other GPIO pins */
  GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
#endif /* USE_STM3210E_EVAL */
  /* Configure SPI2 pins: CK, WS and SD ---------------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_15;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
#ifdef USE_STM3210C_EVAL  
  /* Remap SPI3 on PC10-PC11-PC12-PA4 GPIO pins ------------------------*/
  GPIO_PinRemapConfig(GPIO_Remap_SPI3, ENABLE);  
  /* Configure SPI3 pins: CK and SD ------------------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_12;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  /* Configure SPI3 pins: WS -------------------------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  GPIO_Init(GPIOA, &GPIO_InitStructure);  
#elif defined (USE_STM3210E_EVAL)  
  /* Configure SPI3 pins: CK and SD ------------------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_5;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  /* Configure SPI3 pins: WS -------------------------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
  GPIO_Init(GPIOA, &GPIO_InitStructure);  
#endif /* USE_STM3210C_EVAL */
}

4. 疑问: 从STM的例程有改正来看,1的观点是正确的。但是为什么错误的配置不出错,而且长期工作没有问题呢?难道STM给我们的GPIO示意图不是那么回事?






598330983| | 2015-7-20 22:03 | 显示全部楼层
实际用的时候有问题吗?都出厂很多年了,有问题还发现不了?忽悠人吧

使用特权

评论回复
戈卫东| | 2015-7-21 02:32 | 显示全部楼层
其实AF_PP不一定是输出。。。。。。到底是什么由那个AF决定

使用特权

评论回复
airwill| | 2015-7-21 08:01 | 显示全部楼层
楼上正解.
GPIO_Mode_AF_PP 以后:  
GPIO 的 IO 功能被屏蔽了(其实只是输出功能被屏蔽).
GPIO_Mode_AF_PP 到底是不是输出模式, 还得外设的功能说了算.

使用特权

评论回复
zhjerry|  楼主 | 2015-7-21 10:11 | 显示全部楼层
大家看看STM给出的GPIO结构图,启用GPIO_Mode_AF_PP 以后输出驱动部分就被使能,管脚被强制推拉。如果选择的AF功能在该管脚没有实际输出则Alternate function output处于逻辑不定态,管脚也要受到这个逻辑态的强制控制。其实这就是本文所说的的疑惑的地方,STM给的这个图并不反映实际情况?

GPIO.JPG

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

53

主题

232

帖子

4

粉丝