Kinetis® K20 72 MHz MCU系列为中等性能的Kinetis产品组合提供了可扩展的入门级产品,具有差异化的集成,配备高精度模拟集成和灵活的低功耗功能。其相关资源可在NXP的官网获得。
PF81/PF82为PMIC系列专为高性能处理应用而设计,如娱乐中控、车载信息服务、仪表板、汽车网络、ADAS、视图和传感器融合,内部集成了硬件看门狗等功能。本例主要使用NXP平台提供的I2C控制器驱动PF80实现硬件看门狗功能。
下面是本篇文章涉及的一些概念说明,帮助读者更好的理解:
嵌入式程序运行在以前飞思卡尔的KL24Z开发平台,与PF81通过IIC进行通信配置寄存器控制各BUCK/LDO输出电压以及错误管理与本例着重讲述的看门狗,下面是他的原理示意图:
NXP官网提供了PF8x的各个功能的配置接口,包括:
下面代码片主要展现的是看门口配置相关的接口,主要包括:
内部补充了中文注释帮助您理解:
//基于提供的地址,通过IIC执行读取寄存器
pf8x_status_t PF8X_ReadRegister(pf8x_drv_data_t* drvData, uint8_t address,
uint8_t* rxData)
{
pf8x_status_t status;
uint8_t i2cAddress;
uint8_t txFrame[PF8X_READ_FRAME_LENGTH] = {0};
uint8_t rxFrameCrc[PF8X_FRAME_SIZE] = {0};
uint8_t rxFrame[PF8X_FRAME_SIZE - 3U] = {0};
FS_ASSERT(drvData != NULL);
FS_ASSERT(rxData != NULL);
i2cAddress = PF8X_GetI2CAddress(drvData);//获取IIC地址
txFrame[0] = address;
/* Send I2C command with register address only. */
status = MCU_I2C_SendData(txFrame, PF8X_READ_FRAME_LENGTH, i2cAddress);//发送数据
if (status != pf8xStatusOk)
{
return status;
}
if(drvData->crcEnabled)//如果当前芯片I2C通信开启CRC验证
{
/* Read 2 bytes - 1 data byte, 1 CRC byte. */
status = MCU_I2C_ReceiveData(PF8X_RX_SIZE, i2cAddress, rxFrameCrc);//读取寄存器数据
if (status != pf8xStatusOk)
{
return status;
}
/* I2C response frame contains just data and CRC.
* The Device address and Register address is added
* for proper CRC check. */
rxFrameCrc[2] = address;
rxFrameCrc[3] = (uint8_t)((i2cAddress << 1U) | 0x01U); /* R/W bit is 1 for I2C read */
/* Check CRC. */
status = PF8X_CheckCRC(rxFrameCrc, PF8X_FRAME_SIZE);
if (status != pf8xStatusOk)
{
return status;
}
*rxData = rxFrameCrc[1];
}
else//没有开启crc
{
/* Read 1 byte - data byte only, no CRC. */
status = MCU_I2C_ReceiveData(PF8X_RX_SIZE - 1U, i2cAddress, rxFrame);//读取crc数据
if (status != pf8xStatusOk)
{
return status;
}
*rxData = rxFrame[0];
}
return pf8xStatusOk;
}
//写寄存器
pf8x_status_t PF8X_WriteRegister(pf8x_drv_data_t* drvData, uint8_t address,
uint8_t writeData)
{
uint8_t i2cAddress;
uint8_t txFrameCrc[PF8X_FRAME_SIZE] = {0};
uint8_t txFrame[PF8X_FRAME_SIZE - 1U] = {0};
FS_ASSERT(drvData != NULL);
i2cAddress = PF8X_GetI2CAddress(drvData);//获取寄存器地址
if(drvData->crcEnabled)//如果crc功能打开
{
txFrameCrc[3] = (uint8_t)(i2cAddress << 1U); /* R/W bit is 0 for write */
/* Sets address of the register (first two bits are ignored). */
txFrameCrc[2] = address;
/* Sets data. */
txFrameCrc[1] = writeData;
/* Add CRC. */
txFrameCrc[0] = PF8X_CalcCRC(txFrameCrc, PF8X_FRAME_SIZE);//计算crc
/* Write 3 bytes - register address byte, data byte, CRC byte. */
return MCU_I2C_SendData(txFrameCrc, PF8X_COMM_FRAME_SIZE, i2cAddress);//发送数据
}
else
{
txFrame[2] = (uint8_t)(i2cAddress << 1U); /* R/W bit is 0 for write */
/* Sets address of the register (first two bits are ignored). */
txFrame[1] = address;
/* Sets data. */
txFrame[0] = writeData;
/* Write 2 bytes - register address byte, data byte, no CRC. */
return MCU_I2C_SendData(txFrame, PF8X_COMM_FRAME_SIZE - 1U, i2cAddress);//发送配置数据
}
}
//配置看门狗模块,包括超时周期等函数
pf8x_status_t PF8X_SetWatchdogConfig(pf8x_drv_data_t* drvData, bool wdEnable,
bool wdStbyEnable, uint8_t wdDuration, uint8_t wdMaxExpire, uint8_t wdMaxCnt)
{
pf8x_status_t status;
uint8_t rxData = 0;
FS_ASSERT(drvData != NULL);
/* Read CTRL1 register. */
status = PF8X_ReadRegister(drvData, PF8X_CTRL1_ADDR, &rxData);
if (status != pf8xStatusOk)
{
return status;
}
/* Clear bits before changing them. */
rxData &= ~(PF8X_F_WD_EN_MASK | PF8X_F_WD_STBY_EN_MASK);
/* Modify register content. */
rxData |= ((uint8_t)wdEnable << PF8X_F_WD_EN_SHIFT) |
((uint8_t)wdStbyEnable << PF8X_F_WD_STBY_EN_SHIFT);
/* Write settings to CTRL1 register. */
if(drvData->secureEnabled)
{
status = PF8X_WriteRegisterSecured(drvData, PF8X_CTRL1_ADDR, rxData);
}
else
{
status = PF8X_WriteRegister(drvData, PF8X_CTRL1_ADDR, rxData);
}
if (status != pf8xStatusOk)
{
return status;
}
/* Write settings to WD_CONFIG register. */
status = PF8X_WriteRegister(drvData, PF8X_WD_CONFIG_ADDR, wdDuration);
if (status != pf8xStatusOk)
{
return status;
}
/* Read WD_EXPIRE register. */
status = PF8X_ReadRegister(drvData, PF8X_WD_EXPIRE_ADDR, &rxData);
if (status != pf8xStatusOk)
{
return status;
}
/* Clear bits before changing them. */
rxData &= ~(PF8X_F_WD_MAX_EXPIRE_MASK);
/* Modify register content. */
rxData |= wdMaxExpire;
/* Write settings to WD_EXPIRE register. */
status = PF8X_WriteRegister(drvData, PF8X_WD_EXPIRE_ADDR, rxData);
if (status != pf8xStatusOk)
{
return status;
}
/* Read WD_COUNTER register. */
status = PF8X_ReadRegister(drvData, PF8X_WD_COUNTER_ADDR, &rxData);
if (status != pf8xStatusOk)
{
return status;
}
/* Clear bits before changing them. */
rxData &= ~(PF8X_F_WD_MAX_CNT_MASK);
/* Modify register content. */
rxData |= wdMaxCnt;
/* Write settings to WD_COUNTER register. */
return PF8X_WriteRegister(drvData, PF8X_WD_COUNTER_ADDR, rxData);
}
/* 获取当前看门狗状态*/
pf8x_status_t PF8X_GetWatchdogStatus(pf8x_drv_data_t* drvData, uint8_t* wdExpireCnt, uint8_t* wdEventCnt)
{
pf8x_status_t status;
uint8_t rxData = 0;
FS_ASSERT(drvData != NULL);
FS_ASSERT(wdExpireCnt != NULL);
FS_ASSERT(wdEventCnt != NULL);
/* Read WD_EXPIRE register. */
status = PF8X_ReadRegister(drvData, PF8X_WD_EXPIRE_ADDR, &rxData);
if (status != pf8xStatusOk)
{
return status;
}
*wdExpireCnt = (rxData & PF8X_F_WD_EXPIRE_CNT_MASK) >> PF8X_F_WD_EXPIRE_CNT_SHIFT;
/* Read WD_COUNTER register. */
status = PF8X_ReadRegister(drvData, PF8X_WD_COUNTER_ADDR, &rxData);
if (status != pf8xStatusOk)
{
return status;
}
*wdEventCnt = (rxData & PF8X_F_WD_EVENT_CNT_MASK) >> PF8X_F_WD_EVENT_CNT_SHIFT;
return status;
}
//清除看门狗,喂狗
pf8x_status_t PF8X_ClearWatchdog(pf8x_drv_data_t* drvData)
{
pf8x_status_t status;
FS_ASSERT(drvData != NULL);
/* Write to WD_CLEAR register. */
if(drvData->secureEnabled)
{
status = PF8X_WriteRegisterSecured(drvData, PF8X_WD_CLEAR_ADDR, PF8X_F_WD_CLEAR_1);
}
else
{
status = PF8X_WriteRegister(drvData, PF8X_WD_CLEAR_ADDR, PF8X_F_WD_CLEAR_1);
}
return status;
}
下面代码是一个窗口看门狗的实现,配置使能窗口看门狗模式,窗口设置大小120ms,下图是官方手册说明窗口模式和单端模式的不同喂狗时机,可以看出单端在整个窗口时间范围内喂狗就行,而窗口模式必循在60ms~120ms喂狗:
下面是它的实现代码,已经在必要的位置增加了中文注释帮助您理解:
int main(void)
{
//定时器配置结构体
pit_config_t pitConfig = { false };
pf8x_drv_data_t drvData = { .i2cAddressOtp = 0x00U, .secureEnabled = false, .crcEnabled = false };//初始化I2C设备结构体
i2c_master_config_t masterConfig;//I2Cmaster 配置
uint32_t sourceClock;
//注册一个750ms唤醒定时器,定时器将wdFlag 置为真
PIT_Init(PIT, &pitConfig);
PIT_StopTimer(PIT, kPIT_Chnl_0);
/* Set 750 ms timer period for channel 0 (3/4 of watchdog window). */
PIT_SetTimerPeriod(PIT, kPIT_Chnl_0, MSEC_TO_COUNT(750, PIT_CLK));
/* Enable timer interrupts for channel 0. */
PIT_EnableInterrupts(PIT, kPIT_Chnl_0, kPIT_TimerInterruptEnable);
/* Enable at the NVIC. */
EnableIRQ(PIT_IRQ);
/* Change default baudrate. */
masterConfig.baudRate_Bps = I2C_BAUDRATE;
//初始化I2C控制器
sourceClock = I2C_MASTER_CLK;
I2C_MasterInit(I2C_MASTER_BASEADDR, &masterConfig, sourceClock);
/* 看门狗配置 - 看门狗使能, 使能窗口模式, 1024 ms 窗口. */
status = PF8X_SetWatchdogConfig(&drvData, true, false, PF8X_F_WD_DURATION_1024_MS, PF8X_F_WD_MAX_EXPIRE_7, PF8X_F_WD_MAX_CNT_15);
if (status != pf8xStatusOk)
{
//I2C通信错误
while (1)
{
if(wdFlag == true)//注册定时器将喂狗标志位置为真
{
status = PF8X_ClearWatchdog(&drvData);
if (status != pf8xStatusOk)
{
/* I2C error. */
while (1) {}
}
/* Read watchdog status. */
status = PF8X_GetWatchdogStatus(&drvData, &wdExpCnt, &wdEvCnt);
if (status != pf8xStatusOk)
{
/* I2C error. */
while (1) {}
}
}
}
}
}
十六宿舍 原创作品,转载必须标注原文链接。
©2023 Yang Li. All rights reserved.
欢迎关注 『十六宿舍』,大家喜欢的话,给个,更多关于嵌入式相关技术的内容持续更新中。