Search code examples
stm32i2cstm32f3

STM32 I2C Only Transmits Once


I'm using stm32f3 Datasheet as reference for register level programming instead of HAL. I'm pretty sure I set up everything correctly since I am actually able to transmit bytes of data through I2C peripheral however it fails to send a byte when I change the slave address to a different device (from accelerometer to magnetometer) I get a NACK and Stop condition. Why would that be?

Here is my clock setup:

void ResetClockControl() {

RCC->CFGR |= RCC_CFGR_SW_PLL|RCC_CFGR_HPRE_DIV1|RCC_CFGR_PPRE1_DIV2|RCC_CFGR_PPRE2_DIV1
        |RCC_CFGR_PLLSRC_HSE_PREDIV|RCC_CFGR_PLLXTPRE_HSE_PREDIV_DIV1|RCC_CFGR_PLLMUL6|RCC_CFGR_MCO_NOCLOCK;
RCC->CR &= ~(RCC_CR_HSION);                                                                                                                             //Turn off HSI
RCC->CSR &= ~(RCC_CSR_LSION);                                                                                                                           //Turn off LSI

/* Set bit PLLON last and wait until it is locked */
RCC->CR |= RCC_CR_HSEON|RCC_CR_HSEBYP|RCC_CR_PLLON;
while(!(RCC->CR & RCC_CR_PLLRDY));}

And here is my I2C init function (initializes slave register settings as well):

void I2C_Init() {
/* Peripheral Clock Enable */
RCC->APB1ENR |= RCC_APB1ENR_I2C1EN;         //Enable clock to I2C
RCC->AHBENR |= RCC_AHBENR_GPIOBEN|RCC_AHBENR_DMA1EN; // GPIO/DMA Init
RCC->CFGR3 |= RCC_CFGR3_I2C1SW_SYSCLK; //Set SYSCLK as source

/* GPIO Configuration */
GPIOB->MODER |= GPIO_MODER_MODER6_1|GPIO_MODER_MODER7_1;//alternate function
GPIOB->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR6|GPIO_OSPEEDER_OSPEEDR7;
GPIOB->OTYPER |= GPIO_OTYPER_OT_6|GPIO_OTYPER_OT_7;  //open drain
GPIOB->PUPDR |= GPIO_PUPDR_PUPDR6_0|GPIO_PUPDR_PUPDR7_0; // pulllup
GPIOB->AFR[0] |= 0x04000000|0x40000000; ; //AF for SCL and SDA no bit values in 
header...

/* Configure DMA Rx */
DMA1_Channel7->CNDTR |= 0x0006; //6 bytes of acc/mag data to be transferred
DMA1_Channel7->CPAR = (uint32_t)&I2C1->RXDR;//0x40005424 I2C receive register address
DMA1_Channel7->CMAR = (uint32_t)test; // DMA memory address to write to
DMA1_Channel7->CCR |= DMA_CCR_EN|DMA_CCR_CIRC|DMA_CCR_MINC|DMA_CCR_TCIE; //memory 
increase, interrupt is not enabled assumed to be completed. Circular mode


/* I2c Configuration */
I2C1->TIMINGR |= 0x2010091A; //400KHz fast mode generated from cubeMX
I2C1->CR1 |= I2C_CR1_PE|I2C_CR1_RXDMAEN;// DMA Rx Enable

/* LSM303AGR_Init(); */
uint8_t Data[2] = {0};
Data[0]= 0x20;
Data[1]= 0x57;
I2C_WriteByte(0x19<<1,Data);
Data[0]= 0x23;
Data[1]= 0x20;
I2C_WriteByte(0x19<<1,Data);
Data[0]= 0x60;
Data[1]= 0x0C;
I2C_WriteByte(0x1E<<1,Data);
Data[0]= 0x62;
Data[1]= 0x10;
I2C_WriteByte(0x1E<<1,Data);
}

void I2C_WriteByte(uint8_t sAddress,uint8_t* sData) {

I2C1->CR2 |= 0x02<<I2C_CR2_NBYTES_Pos|sAddress; //NbyteToWrite,slave address,Read/Write
I2C1->CR2 |= I2C_CR2_START; //Start I2C transfer
while(!(I2C1->ISR & I2C_ISR_TXIS)){} //wait until ready to send byte
I2C1->TXDR = sData[0]; //sub reg
while(!(I2C1->ISR & I2C_ISR_TXIS)){} //wait until ready to send byte
I2C1->TXDR = sData[1]; //data
I2C1->CR2 |= I2C_CR2_STOP;
I2C1->ICR |= I2C_ICR_STOPCF;
}

These 2 functions are called from main. ResetClockControl is called first.

Also, the timing register value was generated by CubeMX (positive I set up cubeMX identical to the clock parameters here)

Can anyone identify the problem here?

Thanks


Solution

  • Not sure why this works but before I switch to another slave on I2C bus, I need to reset the number of bytes I intend to send to value of 0. I use this line in between the 2 slave device transfers to do this:

    I2C1->CR2 = 0x00<<I2C_CR2_NBYTES_Pos; //reset NBYTES when switching slave address

    I couldn't find anywhere in the datasheet that says I have to reset NBYTES before switching slave devices or any other reason to reset NBYTES but this seems to work for me.