Hello,
I am currently working on writing a very basic I2C communication library for KL46Z who is capable of sending start, stop signals and sending to or receiving from a slave device . My environment is similar to KSDK. I am trying to use the I2C1 on port E pin 1 and pin 0 on the development board. But after initializing the corresponding port and pins, and ungating the clock, I could not write to the I2C1 registers. For example, to activate the I2C module on I2C1, the bit 7 of register I2C1_C1 must be set to 1. So my code is simply:
void I2C_activate(I2C_Type *base){
base->C1 |= 1U<<7;
}
Using it afterwards is something like:
I2C_activate(I2C1);
Normally, this function would simply set the 7th bit of I2C1_C1 register. But instead, I get a hard fault...
Can somebody please tell me if I am doing right? Did I forget something before writing in the register?
Thank you in advance!
Hi,
Exactly at which line are you getting hard fault?
Are you enabling all necessary clocks. You need to enable each port clocks for the I2C pins, in addition to I2C clock.
In KSDK 2.0, if Port E has the I2C pins:
CLOCK_EnableClock(kCLOCK_PortE);
Hi Visakhan,
Thank you for your comment! And yes it was due to a clock that I did not enable. I added CLOCK_EnableClock(kCLOCK_I2C) and the hard fault was gone! Thanks again for your help!
Wenbo