KL26Z i2c problem - IICIF flag.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

KL26Z i2c problem - IICIF flag.

514 Views
arkadiuszmotyka
Contributor I

Hi, 

Firstly I want to apologise for my English, it's not my native language.

I'm using KL26Z128VLH4 processor and I want to start i2c module. The code hangs while waiting for IICIF flag. I tried with Processor Expert and without, with different CPU speed, with different GPIO. The best, my friend have got the same issue with KL46Z. 

Below is code I'm using:

void InitI2C(void){
SIM_BASE_PTR->SCGC5 |= SIM_SCGC5_PORTB_MASK; // clock for port B
SIM_BASE_PTR->SCGC4 |= SIM_SCGC4_I2C0_MASK; // clock for  I2C0

PORTB_BASE_PTR->PCR[0] = PORT_PCR_MUX(1UL<<2); //PTB0 I2C0 SCL
PORTB_BASE_PTR->PCR[1] = PORT_PCR_MUX(1UL<<2); //PTB1 I2C0 SDA


I2C0_BASE_PTR->F = 0x14; // baudrate: 300kHz
I2C0_BASE_PTR->C1 |= I2C_C1_IICEN_MASK; // enable I2C0
}

uint8_t BusyI2C(void){
while((I2C0_BASE_PTR->S & I2C_S_IICIF_MASK)==0){} //here program hangs
if(I2C0_BASE_PTR->S & I2C_S_RXAK_MASK){return 0;}
I2C0_BASE_PTR->S |= I2C_S_IICIF_MASK; // reset IICIF
return 1;
}

Thank you in advance for help :smileylaugh:

0 Kudos
2 Replies

382 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hello Arkadiusz Motyka,

    About the I2C code for KL26, I highly recommend you to use our KSDK sample code directly.

  You can download KSDK2.2_FRDM-KL26 from this link:

Welcome to MCUXpresso | MCUXpresso Config Tools 

Click SDK Builder, then choose the board as FRDM-KL26, generate the code and download it.

 You can find the official I2C code in folder: SDK_2.2_FRDM-KL26Z\boards\frdmkl26z\driver_examples\i2c

Please try official code at first.

If you still have question about it, please let me know!


Have a great day,
Kerry

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos

382 Views
arkadiuszmotyka
Contributor I

Ohhh I forgot about Read function, where Busy function is used:

uint8_t ReadRegisterI2C(uint8_t adres){
uint8_t dane=0;
EnableI2C();
SendModeI2C();
MasterModeI2C();
SendByteI2C(ADXL375_WRITE_ADDR); //adres slave
BusyI2C();
SendByteI2C(adres); 
BusyI2C();
SendRestartI2C();
SendByteI2C(ADXL375_READ_ADDR);
BusyI2C();
ReceiveModeI2C();
DisableAckI2C();
dane=ReadByteI2C();
BusyI2C();
SlaveModeI2C();
ReceiveModeI2C();
dane=ReadByteI2C();
DisableI2C();
return dane;
}

0 Kudos