stm32l152 作为 I2C 从机不确认地址

stm32l152 as I2C slave not acknowledging address

我正在使用两个 stm32l152 discovery boar。一个配置为主,另一个配置为从。我在它们两个上都启用了确认,但是当主机发送地址时,从机不会在第 9 个时钟脉冲发送确认位。端口设置为复用功能 4 和漏极开路。我使用外部 4.7k 上拉电阻至 3.3V。我已经多次检查了所有寄存器,但我不知道为什么从站不识别它的地址。

This is the output from the logic analyser

D3是master的起始位
D4是slave上的地址匹配位。
这是奴隶代码:

#define USE_STDPERIPH_DRIVER
#include "stm32l1xx.h"
#include "stm32l1xx_conf.h"

//Quick hack, approximately 1ms delay
void ms_delay(int ms)
{
    while (ms> 0) {
        volatile int x = 5971;
        while (x> 0) {
            x--;
        }
        ms--;
    }
}
#define SCL 8
#define SDA 9

int main(void)
{
    RCC->AHBENR |= (0x1 << 1);

    //set port to alternate function
    GPIOB->MODER &= ~((0x3 << (2 * SCL)) | (0x3 << (2 * SDA)) | (0x3 << (2 * 5)));
    GPIOB->MODER |= ((0x2 << (2 * SCL)) | (0x2 << (2 * SDA)) | (0x1 << (2 * 5)));

    GPIOB->OTYPER |= ((1 << SCL) | (1 << SDA)); //set output PB6 and PB7 to open drain

    //set PB6 and PB7 to no pullup no pulldown
    GPIOB->PUPDR &= ~((0x3 << (2 * SCL)) | (0x3 << (2 * SDA)) | (0x3 << (2 * 5))); 

    //set PB6 and PB7 to alternate function 4(I2C)
    GPIOB->AFR[1] &= ~((0b1111 << (4 * 0)) | (0b1111 << (4 * 1)));
    //set PB6 and PB7 to alternate function 4(I2C)
    GPIOB->AFR[1] |= ((0b0100 << (4 * 0)) | (0b0100 << (4 * 1)));

    RCC->APB1ENR |= (1 << 21);

    //reset I2C
    I2C1->CR1 |= (1 << 15);
    ms_delay(1);
    I2C1->CR1 &= ~(1 << 15);

    I2C1->CR2 |= 0b001000; //peripheral clock set to 8MHz
    I2C1->CR1 |= (1 << 10); //ACK enabled

    I2C1->OAR1 |= (0x05 << 1); //setting primary address

    I2C1->CR1 |= 1; //I2C peripheral enabled when configuration is done

    for (;;) {
        if ((I2C1->SR1&(1 << 1)) != 0) {
            GPIOB->ODR |= (1 << 5);
        }
        else {
            GPIOB->ODR &= ~(1 << 5);
        }
    }
}

这是主密码:

#define USE_STDPERIPH_DRIVER
#include "stm32l1xx.h"
#include "stm32l1xx_conf.h"

#define SCL 8
#define SDA 9

int main(void)
{
    RCC->AHBENR |= (0x1 << 1);

    //set port to alternate function
    GPIOB->MODER &= ~((0x3 << (2 * SCL)) | (0x3 << (2 * SDA)) | (0x3 << (2 * 5)));
    GPIOB->MODER |= ((0x2 << (2 * SCL)) | (0x2 << (2 * SDA)) | (0x1 << (2 * 5)));

    //set output PB6 and PB7 to open drain
    GPIOB->OTYPER |= ((1 << SCL) | (1 << SDA));
    //set PB6 and PB7 to no pullup no pulldown
    GPIOB->PUPDR &= ~((0x3 << (2 * SCL)) | (0x3 << (2 * SDA)) | (0x3 << (2 * 5)));

    //set PB6 and PB7 to alternate function 4(I2C)
    GPIOB->AFR[1] &= ~((0b1111 << (4 * 0)) | (0b1111 << (4 * 1)));
    //set PB6 and PB7 to alternate function 4(I2C)
    GPIOB->AFR[1] |= ((0b0100 << (4 * 0)) | (0b0100 << (4 * 1)));

    I2C1->CR1 |= (1 << 15);
    I2C1->CR1 &= ~(1 << 15);

    RCC->APB1ENR |= (1 << 21);

    I2C1->CR2 |= 0x08; //peripheral clock set to 8MHz
    I2C1->CCR |= 0x28; //
    I2C1->TRISE |= 0x09;
    I2C1->CR1 |= (1 << 10); //ACK enabled

    I2C1->CR1 |= 1; //I2C peripheral enabled when configuration is done
    I2C1->CR1 |= (1 << 8); //generate start condition (master mode)

    for (;;) {
        //check start condition
        if ((I2C1->SR1&(1 << 0)) != 0) {
            GPIOB->ODR |= (1 << 5);
            I2C1->DR = 0x0b << 0; //send slave addres
        }
        else {
            GPIOB->ODR &= ~(1 << 5);
        }

        if ((I2C1->SR1&(1 << 1)) != 0) {
            GPIOB->ODR |= (1 << 5);
        }
        else {
            GPIOB->ODR &= ~(1 << 5);
        }
    }
}

我正在使用 arm-none-eabi-gcc 进行编译并使用来自 stm

的 stsw-stm32077 库

代码的问题是你必须在外设启用后设置ack位,如果你之前这样做,ack位会自动重置。