AnsweredAssumed Answered

how to configure i2c in stm32f446 nucleo 144

Question asked by ANNU CHERIAN on Sep 9, 2017
Latest reply on Sep 14, 2017 by ANNU CHERIAN

#include "stm32f4xx.h"                          // Device header

#include "RTE_Components.h"             // Component selection

 

void clock_enable()

{

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

                PWR->CR |= (PWR_CR_VOS_0);

                RCC->CR |= (1<<0);

                RCC->CR |= (RCC_CR_HSICAL_4);

                RCC->CFGR |= RCC_CFGR_SW_HSI;

               

                RCC->CFGR |= RCC_CFGR_PPRE2_DIV1;

                RCC->CFGR |= RCC_CFGR_PPRE1_DIV2;

                RCC->CFGR |= RCC_CFGR_HPRE_DIV1;

               

                /* Configure Flash prefetch, Instruction cache, Data cache and wait state */

               FLASH->ACR = FLASH_ACR_ICEN | FLASH_ACR_DCEN | FLASH_ACR_PRFTEN |                                    FLASH_ACR_LATENCY_0WS;       

 

}

 

void gpio_init()

{

                /* configure pins used by I2C

                 * PF1 = I2C1_SCL

                 * PF0 = I2C1_SDA

                 */

               

                //configure IO pin for alternate function

                GPIOF->MODER |= (1<<3 | 1<<1);

                GPIOF->MODER &= ~(1<<2 | 1<<0);

               

                //output type as push pull

                //GPIOF->OTYPER |= (1<<1 | 1<<0);          // open drain

                GPIOF->OTYPER &= ~(1<<1 | 1<<0);

               

                //high speed

                GPIOF->OSPEEDR |= (1<<3 | 1<<2 | 1<<1 | 1<<0);

               

                // no pull-up/pull down          

                GPIOF->PUPDR &= ~(1<<3 | 1<<2 | 1<<1 |1<<0);

               

                //Set alternative function to AF4

               GPIOF->AFR[0] |= (4 << 0 | 4 << 4);

}

 

void i2c_init()

{

               

                I2C2->CR1 &= ~(1<<0);                         // Peripheral disable

                I2C2->CR2 |= I2C_CR2_FREQ_3;         // Peripheral clock frequency (8 MHz)

                I2C2->CCR = 0X0028;                           // Sm mode I2C, Clock control register = 40

                I2C2->TRISE = 0x09;                             // rise time

                I2C2->CR1 &= ~(1<<1);                         // I2C mode

               //I2C2->OAR1 |= 0x40DE;                         // slave address

                I2C2->CR1 |= 1<<0 | 1<<10;                   // Peripheral Enable, Acknowledge enable

}

 

void i2c_start()

{

                while(I2C2->SR2 & (1<<1));                                           // wait until I2C1 is not busy anymore

                I2C2->CR1 &= ~(1<<11);                                               // Disable POS

                I2C2->CR1 |= (I2C_CR1_START);                                        // Send I2C1 START condition

                while(!((I2C2->SR1 & I2C_SR1_SB) && (I2C2->SR2 & 0x03)));            // wait for I2C1 EV5 (check start condition,bus busy,master mode)

                I2C2->DR |= 0XDE;                                                    // slave address

                while(!((I2C2->SR1 & 0X82/*I2C_SR1_ADDR*/) && (I2C2->SR2 & 0x07)));  // wait for I2C1 EV6 (check address sent,data bytes transmitted,bus busy,master mode)

}

 

void i2c_write()

{

                I2C2->DR |= 0x00;

                while(!((I2C2->SR1 & 0x84) && (I2C2->SR2 & 0x07)));         // check data register empty,data byte transfer    succeeded

                //I2C1->DR |= 0xFF;

}

 

void i2c_stop()

{

                I2C2->SR1 &= ~(I2C_SR1_AF);

                I2C2->CR1 |= (I2C_CR1_STOP);

}

 

int main()

{

                RCC->APB1ENR |= (RCC_APB1ENR_I2C2EN);    // Enable peripheral clock

                RCC->AHB1ENR |= (RCC_AHB1ENR_GPIOFEN);   // enable clock for used IO pins

               

                clock_enable();

                i2c_init();            

                gpio_init();

                i2c_start();

                i2c_write();

                i2c_stop();

}

 

When this code is executed following happens...

    start condition is sent and EV5 event statisfied but no slave address is sent and is waiting on event EV6. We are trying to communicate with RTC slave device as atesting partner, but no slave address transfer occurs. 

Outcomes