AnsweredAssumed Answered

STM32F050F4P6 - I2C slave error

Question asked by cuong.ngo on May 15, 2014
I am use STM32F050F4P6 as I2C slave device with address 0xC8
But it not work
When i send 0xC8 or 0xC9 from master, STM32F0 is not ack and not work
Please help me
code main.c
[CODE]
#include <stm32f0xx.h>


void Init(void);
void Interrupt_conf(void);
void I2C_Begin(void);


int main(void) {
     
     Init();
     
     Interrupt_conf();
     
     I2C_Begin();
     
     while(1) {
     
     }
     
}


void Interrupt_conf(void) {
     
     NVIC_InitTypeDef NVIC_InitStructure;
  
     /* Reconfigure and enable I2C1 error interrupt to have the higher priority */
     NVIC_InitStructure.NVIC_IRQChannel = I2C1_IRQn;
     NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
     NVIC_Init(&NVIC_InitStructure);
     
}


void Init(void) {
     //khoi tao I2C
     //===============================================================
     GPIO_InitTypeDef  GPIO_InitStructure;
    I2C_InitTypeDef  I2C_InitStructure;
     
     RCC_I2CCLKConfig(RCC_I2C1CLK_HSI);
    // cho phep clock GPIOA
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
    /* I2C1 clock enable */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
     
     GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
     GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
    /* I2C1 SDA and SCL configuration */
     // SCL PA9
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
     GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
     
    GPIO_Init(GPIOA, &GPIO_InitStructure);
     
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
     GPIO_Init(GPIOA, &GPIO_InitStructure);
     
     I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
     I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
     I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Disable;
     I2C_InitStructure.I2C_DigitalFilter = 0x00;
    I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
    I2C_InitStructure.I2C_OwnAddress1 = 0xC8;
    I2C_InitStructure.I2C_Timing = 0x00210507;
    I2C_Init(I2C1, &I2C_InitStructure);
     I2C_Cmd(I2C1, ENABLE);
}


void I2C_Begin(void) {
     /* Enable Event IT needed for ADDR and STOPF events ITs */
     I2C1->CR1 |= I2C_CR1_ADDRIE ;
     I2C1->CR1 |= I2C_CR1_STOPIE ;


     /* Enable Error IT */
     I2C1->CR1 |= I2C_CR1_ERRIE;


     /* Enable Buffer IT (TXE and RXNE ITs) */
     I2C1->CR1 |= I2C_CR1_TXIE;
     I2C1->CR1 |= I2C_CR1_RXIE;


}


[/CODE]
code isr.c
[CODE]
#include <stm32f0xx.h>


uint8_t tmp;
uint16_t Tx_Idx1=0;


uint8_t Buffer_Rx[256];


uint8_t numOfSend;
uint8_t countOfSend;


uint8_t Buffer_Tx1[] = {
     0x04, 0x11, 0x33, 0x43, \
     0x23, 0xAB, 0x03, 0x4D, 0x9D, 0x47, 0xC6, 0x1F, 0xF1, 0x6D, 0x54, 0xA8, 0xF5, 0x75, 0x36, 0x9D, \
     0x34, 0x9D, 0x24, 0xDE, 0xA2, 0x4A, 0x82, 0xA1, 0x04, 0x18, 0xD4, 0x19, 0x4C, 0xC2, 0xF3, 0x5E, \
     0xCE, 0x24, 0xB6, \
     0x23, 0x5D, 0x5B, 0x41, 0xBF, 0x5A, 0x2A, 0x95, 0x75, 0x0A, 0xD5, 0xF5, 0x34, 0xAB, 0xDE, 0x90, \
     0x48, 0xFE, 0x28, 0xF9, 0xEC, 0x9C, 0x49, 0xCE, 0x3A, 0x0F, 0xB7, 0xCD, 0x1A, 0xE7, 0x88, 0xF1, \
     0x53, 0xED, 0x2B, \
     0x04, 0x00, 0x03, 0x40, \
     0x04, 0x00, 0x03, 0x40, \
     0x04, 0x11, 0x33, 0x43, \
     0x04, 0x00, 0x03, 0x40, \
     0x04, 0x11, 0x33, 0x43, \
     0x04, 0x00, 0x03, 0x40, \
     0x04, 0x00, 0x03, 0x40, \
     0x04, 0x11, 0x33, 0x43, \
     0x04, 0x00, 0x03, 0x40, \
     0x23, 0x3F, 0x09, 0x75, 0x39, 0xC9, 0x10, 0x84, 0x7D, 0x4D, 0x24, 0x1C, 0xF2, 0x1F, 0xD5, 0xF4, \
     0xEE, 0xAB, 0x1F, 0x1B, 0xAE, 0x21, 0x4F, 0x4B, 0x65, 0x10, 0xF6, 0x16, 0xF1, 0x94, 0x5A, 0x99, \
     0xC7, 0xB5, 0x05, \
     0x04, 0x11, 0x33, 0x43, \
     0x23, 0xAD, 0xB3, 0xE6, 0x97, 0x58, 0x2C, 0x88, 0x9D, 0x7C, 0x44, 0x7A, 0x6C, 0xC0, 0xBF, 0x11, \
     0x5D, 0xD4, 0x9A, 0x9A, 0x58, 0x80, 0xB2, 0xE8, 0xAF, 0x56, 0xB6, 0xA7, 0x09, 0xB4, 0x43, 0x33, \
     0x3D, 0xA3, 0x3A, \
     0x04, 0x00, 0x03, 0x40, \
     0x23, 0x5E, 0x06, 0xB3, 0x48, 0x76, 0xB0, 0x60, 0x71, 0x27, 0x8F, 0xFC, 0x00, 0x62, 0x23, 0xBB, \
     0x69, 0x92, 0x19, 0x9F, 0x4A, 0xF2, 0xF0, 0x23, 0x21, 0xEC, 0x81, 0x45, 0x98, 0xA6, 0x3D, 0xD4, \
     0x6C, 0x7E, 0x90
};


//ham ngat I2C1


void I2C1_IRQHandler(void) {
     //ham ngat I2C1
     //kiem tra xem cai gi *** ra ngat
     
     uint32_t isr;
     isr=I2C1->ISR;
     
     //kiem tra xem co trong che do SLAVE khong
     //ADDRESS matched
     if(isr&I2C_ISR_ADDR) {
          //kiem tra chieu truyen du lieu
          if(isr&I2C_ISR_DIR) {
               //chieu truyen du lieu
               //slave truyen du lieu
               numOfSend=Buffer_Tx1[Tx_Idx1];
               countOfSend=0;
          } else {
               //slave nhan du lieu
               //=======================
          }
          isr=0;
     }
     if(isr & I2C_ISR_TXE) {
          ++countOfSend;
          if(countOfSend>numOfSend) {
               //==================================================================
               I2C1->TXDR = 0xFF;
          } else {
               tmp=Buffer_Tx1[Tx_Idx1++];
               I2C1->TXDR = tmp;
               //==================================================================
          }
          isr=0;
     }
     if(isr & I2C_ISR_RXNE) {
          tmp=I2C1->RXDR;
          isr=0;
     }
     
     if(isr & I2C_ISR_STOPF) {
          I2C1->CR1=I2C_CR1_PE;
          isr=0;
     }
     
     //kiem tra loi
     if(isr & I2C_ISR_ARLO) {
          I2C1->ICR |= I2C_ICR_ARLOCF;
          isr=0;
     }
     
     if(isr & I2C_ISR_BERR) {
          I2C1->ICR |= I2C_ICR_BERRCF;
          isr=0;
     }
     
     if(isr & I2C_ISR_OVR) {
          I2C1->ICR |= I2C_ICR_OVRCF;
          isr=0;
     }
     
}




[/CODE]

Outcomes