Hello~
I bought STM32F04C6 from internet.( Look STM32F0406 datasheet or buy it please click this link: STM32F042C6T6 ) And I made some troubles when I debug I2C Slave mode. Could anybody here give me some feasible suggestions?
Trouble:The I2C Slave mode cannot enter into interrupt.
The program I set:
[code]void STM32_config_iic_slavemode()
{
uint32_t tmpreg = 0;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
// Disable I2C1 Peripheral
I2C1->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_PE);
//---------------------------- Deinitialize I2C1 clock ------------------
// Reset I2C1 device clock in order to avoid non-cleared error flags
RCC_APB1PeriphResetCmd((RCC_APB1Periph_I2C1),ENABLE);
RCC_APB1PeriphResetCmd((RCC_APB1Periph_I2C1),DISABLE);
// Disable I2C1 device clock
RCC_APB1PeriphClockCmd((RCC_APB1Periph_I2C1),DISABLE);
//---------------------------- GPIO pins configuration ------------------
// Initialize I2C1 GPIO
// Enable I2C1 SCL and SDA Pin Clock
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,ENABLE);
// Connect PXx to I2C_SCL
GPIO_PinAFConfig((GPIO_TypeDef*)GPIOB,GPIO_PinSource6,GPIO_AF_1);
// Connect PXx to I2C_SDA
GPIO_PinAFConfig((GPIO_TypeDef*)GPIOB,GPIO_PinSource7,GPIO_AF_1);
// Set GPIO frequency to 50MHz
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// Select Alternate function mode
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
//Select output Open Drain type
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
// Disable internal Pull-up
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
// Initialize I2C1 SCL Pin
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init((GPIO_TypeDef*)GPIOB, &GPIO_InitStructure);
// Initialize I2C1 SDA Pin
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init((GPIO_TypeDef*)GPIOB, &GPIO_InitStructure);
// Initialize I2C1 clock
// Reset I2C1 device clock in order to avoid non-cleared error flags
//__I2C_RCC_RESET(CPAL_I2C_CLK [Device]);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1,ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1,DISABLE);
// Enable I2C1 device clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
//I2C1 FILTERS Configuration
// Get the I2C1 CR1 value
tmpreg = I2C1->CR1;
// Clear I2C1 CR1 register
tmpreg &= CR1_CLEAR_MASK;
// Configure I2C1: analog and digital filter
// Set ANFOFF bit according to I2C_AnalogFilter value
// Set DFN bits according to I2C_DigitalFilter value
tmpreg |= (uint32_t)0x00000000 | (0 << 8); // Analog noise filter enabled, Digital filter disabled
//tmpreg |= I2C_CR1_NOSTRETCH; // disable clcok stretching
// Write to I2C1 CR1
I2C1->CR1 = tmpreg;
//I2C1 TIMING Configuration
// Configure I2C1: Timing
// Set TIMINGR bits according to I2C_Timing
// Write to I2C1 TIMING
//I2C1->TIMINGR = 0x00000000 & TIMING_CLEAR_MASK;
I2C1->TIMINGR = I2C_TIMING;
// Enable I2C1 Peripheral
I2C1->CR1 |= I2C_CR1_PE;
//I2C1 OAR1 Configuration
// Clear tmpreg local variable
tmpreg = 0;
// Clear OAR1 register
I2C1->OAR1 = (uint32_t)tmpreg;
// Clear OAR2 register
I2C1->OAR2 = (uint32_t)tmpreg;
// Configure I2C1: Own Address1 and acknowledged address
// Set OA1MODE bit according to I2C_AcknowledgedAddress value
// Set OA1 bits according to I2C_OwnAddress1 value
tmpreg = (uint32_t)((uint32_t)0x00000000 | \
(uint32_t)OWN_ADDRESS1);
// Write to I2C1 OAR1
I2C1->OAR1 = tmpreg;
// Enable Own Address1 acknowledgement
I2C1->OAR1 |= I2C_OAR1_OA1EN;
//I2C1 MODE Configuration
// Configure I2C1: mode
// Set SMBDEN and SMBHEN bits according to I2C_Mode value
tmpreg = 0;
// Write to I2C1 CR1
I2C1->CR1 |= tmpreg;
//I2C1 ACK Configuration
//Get the I2C1 CR2 value
tmpreg = I2C1->CR2;
//Clear I2C1 CR2 register
tmpreg &= CR2_CLEAR_MASK;
//Configure I2C1: acknowledgement
//Set NACK bit according to I2C_Ack value
tmpreg |= (uint32_t)0x00008000;
// Write to I2C1 CR2
I2C1->CR2 = tmpreg;
//I2C1->OAR2 |= I2C_OAR2_OA2EN;
//I2C1->CR1 |= I2C_CR1_GCEN;
// Enable slave byte control
//I2C1->CR1 |= I2C_CR1_SBC;
I2C1->CR1 |= (I2C_CR1_ADDRIE | I2C_CR1_STOPIE | I2C_CR1_TXIE | I2C_CR1_RXIE);
//---------------------------- Peripheral and DMA interrupts Initialization ------------------
// Initialize I2C1 interrupts
/* Enable the IRQ channel */
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
/* Configure NVIC for I2C1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = I2C1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
NVIC_Init(&NVIC_InitStructure);
// If I2C ERR Interrupt Option Bit not selected
//NULL
// Initialize Timeout procedure
//NULL
}[/code]
My interrupt set is : address interrupt, interrupt receiving, interrupt sending. What’s wrong?
Thank you.