[英]Configuring STM32 output ports for I2C
I am currently trying to get a pair of stm32l152 (discovery kit) to communicate via I2C.我目前正在尝试使用一对 stm32l152(探索套件)通过 I2C 进行通信。 (i am not using the standard peripheral library provided by STM as i want to try to implement the i2c myself..)
(我没有使用 STM 提供的标准外设库,因为我想尝试自己实现 i2c ..)
My configuration is as followed:我的配置如下:
7-bit addressing mode (no dual address, only OAR1) 7位寻址方式(无双地址,只有OAR1)
100khz speed 100khz速度
ACK enabled (on slave) ACK 启用(在从机上)
ACK disabled (on master, since only 1 byte is transferred between master/slave at any time) ACK 禁用(在主机上,因为在任何时候主机/从机之间只传输 1 个字节)
on both master/slave, using GPIOB (PB10) as SCL and GPIOB (PB11) as SDA在主/从,使用 GPIOB (PB10) 作为 SCL 和 GPIOB (PB11) 作为 SDA
Here's the weird part: when i configure the 2 GPIOB pins as push-pull (no pullup/down), i am able to see my master send out the start bit, address byte.. on the oscilloscope.这是奇怪的部分:当我将 2 个 GPIOB 引脚配置为推挽(无上拉/下拉)时,我能够在示波器上看到我的主机发送起始位、地址字节..。
But my slave is not sending back an acknowledgement.但是我的奴隶没有发回确认。 i know this for sure because the master did not assert ADDR bit in SR1, i see no ack on the scope and also the slave did not go into interrupt ( i enabled I2C event interrupt on the slave).
我肯定知道这一点,因为主机没有断言 SR1 中的 ADDR 位,我在示波器上看不到 ack,而且从机也没有进入中断(我在从机上启用了 I2C 事件中断)。
on closer look at the waveform on the scope, i realised the signal (both SCL, SDA) did not fully go to 0Volts for logic low, they are at 1 volts instead.仔细观察示波器上的波形,我意识到信号(SCL、SDA)没有完全达到逻辑低电平的 0 伏,而是 1 伏。 i am guessing this maybe why the slave cannot read the address byte from the master and therefore did not send back ack.
我猜这可能是为什么从站无法从主站读取地址字节,因此没有发回 ack。
So i configure the 2 GPIO pins to be open-drain (no pullup/down)..but now my master cannot send out the start bit.. i step through the code and saw that the master's SB bit in SR1 is set when i set the START bit.. but i do not see the any response on the SDA and SCL signals on the scope (both are high at 3V).因此,我将 2 个 GPIO 引脚配置为漏极开路(无上拉/下拉)。但现在我的主机无法发送起始位。设置 START 位 .. 但我没有看到示波器上的 SDA 和 SCL 信号有任何响应(两者都为 3V 高)。
(i pulled both SCL and SDA to 3v using 10kohms on each line). (我在每条线上使用 10kohms 将 SCL 和 SDA 拉至 3v)。
ok, i think i found the answer.. I looked up the standard peripheral library provided by STM and followed their configuration sequence.好的,我想我找到了答案..我查找了STM提供的标准外设库并遵循了它们的配置顺序。 Before that, i was using the wrong sequence of configuring the GPIO ports...
在此之前,我使用了错误的 GPIO 端口配置顺序...
The sequence tested to be working is:测试工作的顺序是:
Enable i2c and GPIOs clocks.启用 i2c 和 GPIO 时钟。
Configure the desired AF of the ports.配置所需的端口 AF。 (AF4 for i2c in stm32).
(stm32 中 i2c 的 AF4)。
Configure GPIOs:配置 GPIO:
3a. 3a. set GPIOs as AF.
将 GPIO 设置为 AF。
3b. 3b. set port speed.
设置端口速度。
3c. 3c。 set port type (open-drain).
设置端口类型(开漏)。
3d. 3d。 set to no pull-up/pull-down.
设置为无上拉/下拉。
Configure i2c.配置 i2c。 (registers CR2, CCR, CR1, OAR1, ...).
(寄存器 CR2、CCR、CR1、OAR1、...)。
Previously, i was doing step 3 BEFORE step 2, and there is no output from the ports when i generated the START bit.以前,我在第 2 步之前执行第 3 步,当我生成 START 位时,端口没有输出。
Hope this helps those who have the same problem.希望这可以帮助那些有同样问题的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.