K20 I2C_PDD.h Example

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

K20 I2C_PDD.h Example

1,259 Views
ryan_pearson
Contributor III

Is there a simple example of using the I2C_PDD.h functions?  Something that will initialize the i2c interface, set a slave address, and send some bytes?

0 Kudos
Reply
4 Replies

1,140 Views
Robin_Shen
NXP TechSupport
NXP TechSupport

Hi Ryan,

Please refer the Typical Usages of Component I2C_LDD, they use I2C_PDD.h functions.

I2C_LDD I2C_PDD.png

I2C1_Init.png

Best Regards,

Robin

 

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

1,140 Views
ryan_pearson
Contributor III

Thanks Robin!  I added the I2C LDD to my project, but I still don't see any activity on the I2C lines.  Do you see anything missing?

Here is my code:

MyI2CPtr = CI2C1_Init(NULL);

dat[0] = 0x8C;
dat[1] = 0xFD;
dat[2] = 0xC3;
dat[3] = 0x19;
CI2C1_MasterSendBlock(MyI2CPtr, (void *)dat, 4, LDD_I2C_SEND_STOP);

The Generated Init function:

LDD_TDeviceData* CI2C1_Init(LDD_TUserData *UserDataPtr)
{
/* Allocate HAL device structure */
CI2C1_TDeviceData *DeviceDataPrv;
/* {Default RTOS Adapter} Driver memory allocation: Dynamic allocation is simulated by a pointer to the static object */
DeviceDataPrv = &DeviceDataPrv__DEFAULT_RTOS_ALLOC;

DeviceDataPrv->UserData = UserDataPtr; /* Store the RTOS device structure */

/* Allocate interrupt vector */
/* {Default RTOS Adapter} Set interrupt vector: IVT is static, ISR parameter is passed by the global variable */
INT_I2C0__DEFAULT_RTOS_ISRPARAM = DeviceDataPrv;
DeviceDataPrv->SerFlag = 0x00U; /* Reset all flags */
DeviceDataPrv->SendStop = LDD_I2C_SEND_STOP; /* Set variable for sending stop condition (for master mode) */
DeviceDataPrv->InpLenM = 0x00U; /* Set zero counter of data of reception */
DeviceDataPrv->OutLenM = 0x00U; /* Set zero counter of data of transmission */
/* SIM_SCGC4: I2C0=1 */
SIM_SCGC4 |= SIM_SCGC4_I2C0_MASK;
/* I2C0_C1: IICEN=0,IICIE=0,MST=0,TX=0,TXAK=0,RSTA=0,WUEN=0,DMAEN=0 */
I2C0_C1 = 0x00U; /* Clear control register */
/* I2C0_S: TCF=0,IAAS=0,BUSY=0,ARBL=0,RAM=0,SRW=0,IICIF=1,RXAK=0 */
I2C0_S = I2C_S_IICIF_MASK; /* Clear interrupt flag */
/* PORTB_PCR1: ISF=0,MUX=2 */
PORTB_PCR1 = (uint32_t)((PORTB_PCR1 & (uint32_t)~(uint32_t)(
PORT_PCR_ISF_MASK |
PORT_PCR_MUX(0x05)
)) | (uint32_t)(
PORT_PCR_MUX(0x02)
));
PORT_PDD_SetPinOpenDrain(PORTB_BASE_PTR, 0x01u, PORT_PDD_OPEN_DRAIN_ENABLE); /* Set SDA pin as open drain */
/* PORTB_PCR0: ISF=0,MUX=2 */
PORTB_PCR0 = (uint32_t)((PORTB_PCR0 & (uint32_t)~(uint32_t)(
PORT_PCR_ISF_MASK |
PORT_PCR_MUX(0x05)
)) | (uint32_t)(
PORT_PCR_MUX(0x02)
));
PORT_PDD_SetPinOpenDrain(PORTB_BASE_PTR, 0x00u, PORT_PDD_OPEN_DRAIN_ENABLE); /* Set SCL pin as open drain */
/* NVICIP11: PRI11=0x80 */
NVICIP11 = NVIC_IP_PRI11(0x80);
/* NVICISER0: SETENA|=0x0800 */
NVICISER0 |= NVIC_ISER_SETENA(0x0800);
/* I2C0_C2: GCAEN=0,ADEXT=0,HDRS=0,SBRC=0,RMEN=0,AD=0 */
I2C0_C2 = I2C_C2_AD(0x00);
/* I2C0_FLT: ??=0,??=0,??=0,FLT=0 */
I2C0_FLT = I2C_FLT_FLT(0x00); /* Set glitch filter register */
/* I2C0_SMB: FACK=0,ALERTEN=0,SIICAEN=0,TCKSEL=0,SLTF=1,SHTF1=0,SHTF2=0,SHTF2IE=0 */
I2C0_SMB = I2C_SMB_SLTF_MASK;
/* I2C0_F: MULT=2,ICR=0x1E */
I2C0_F = (I2C_F_MULT(0x02) | I2C_F_ICR(0x1E)); /* Set prescaler bits */
I2C_PDD_EnableDevice(I2C0_BASE_PTR, PDD_ENABLE); /* Enable device */
I2C_PDD_EnableInterrupt(I2C0_BASE_PTR); /* Enable interrupt */
/* Registration of the device structure */
PE_LDD_RegisterDeviceStructure(PE_LDD_COMPONENT_CI2C1_ID,DeviceDataPrv);
return ((LDD_TDeviceData *)DeviceDataPrv); /* Return pointer to the data data structure */
}

The generated MasterSendBlock function:

LDD_TError CI2C1_MasterSendBlock(LDD_TDeviceData *DeviceDataPtr, LDD_TData *BufferPtr, LDD_I2C_TSize Size, LDD_I2C_TSendStop SendStop)
{
CI2C1_TDeviceData *DeviceDataPrv = (CI2C1_TDeviceData *)DeviceDataPtr;

if (Size == 0x00U) { /* Test variable Size on zero */
return ERR_OK; /* If zero then OK */
}
if (DeviceDataPrv->SendStop == LDD_I2C_SEND_STOP) {
if ((I2C_PDD_GetBusStatus(I2C0_BASE_PTR) == I2C_PDD_BUS_BUSY) || /* Is the bus busy? */ \
((DeviceDataPrv->SerFlag & MASTER_IN_PROGRES) != 0x00U) || \
(DeviceDataPrv->OutLenM != 0x00U)) {
return ERR_BUSY; /* If yes then error */
}
} else {
if (((DeviceDataPrv->SerFlag & MASTER_IN_PROGRES) != 0x00U) || /* Is the bus busy? */ \
(DeviceDataPrv->OutLenM != 0x00U)) {
return ERR_BUSY; /* If yes then error */
}
}
/* {Default RTOS Adapter} Critical section begin, general PE function is used */
EnterCritical();
DeviceDataPrv->SerFlag |= MASTER_IN_PROGRES; /* Set flag "busy" */
DeviceDataPrv->OutPtrM = (uint8_t *)BufferPtr; /* Save pointer to data for transmitting */
DeviceDataPrv->OutLenM = Size; /* Set the counter of output bufer's content */
DeviceDataPrv->SendStop = SendStop; /* Set generating stop condition */
I2C_PDD_SetTransmitMode(I2C0_BASE_PTR, I2C_PDD_TX_DIRECTION); /* Set TX mode */
if (I2C_PDD_GetMasterMode(I2C0_BASE_PTR) == I2C_PDD_MASTER_MODE) { /* Is device in master mode? */
I2C_PDD_RepeatStart(I2C0_BASE_PTR); /* If yes then repeat start cycle generated */
} else {
I2C_PDD_SetMasterMode(I2C0_BASE_PTR, I2C_PDD_MASTER_MODE); /* If no then start signal generated */
}
I2C_PDD_WriteDataReg(I2C0_BASE_PTR, 0x46U); /* Send slave address */
/* {Default RTOS Adapter} Critical section end, general PE function is used */
ExitCritical();
return ERR_OK; /* OK */
}

0 Kudos
Reply

1,140 Views
Robin_Shen
NXP TechSupport
NXP TechSupport

Maybe you forgot to add external pull-up resistors.

external pull-up resistors.png

Best Regards,

Robin

 

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

1,140 Views
ryan_pearson
Contributor III

Thanks, Robin, we did discover a problem with a pull-up resistor.

0 Kudos
Reply