Kinetis® K20 72 MHz MCU系列为中等性能的Kinetis产品组合提供了可扩展的入门级产品,具有差异化的集成,配备高精度模拟集成和灵活的低功耗功能。其相关资源可在NXP的官网获得。
PF81/PF82为PMIC系列专为高性能处理应用而设计,如娱乐中控、车载信息服务、仪表板、汽车网络、ADAS、视图和传感器融合,内部集成了硬件看门狗等功能。本例主要使用NXP平台提供的I2C控制器驱动PF80实现硬件看门狗功能。
概念说明
下面是本篇文章涉及的一些概念说明,帮助读者更好的理解:
- BUCK/LDO:BUCK与LDO都是直流变直流(DC-DC),BUCK转换器是一个通过降低电压来增加电流的转换器,LDO是一个直流线性电压控制器,它运作在输入输出压差非常小时。BUCK的输出纹波及稳压性不如LDO好,因此像DSP、ARM、FPGA等内核电源(1.2V、1.5V、2.5V等)宜用LDO,而高输入电压(>5V)、高输出电流(>2A),高输入/输出压差时,宜用BUCK。
- PMIC:PMIC(Power Management IC)又称电源管理IC,是一种特定用途的集成电路,其功能是为主系统提供二级电源管理等工作。本例使用的PF8100芯片内部集成多路的BUCK/LDO,并提供了电压/电流监控以及硬件看门狗等功能,本例主要讲硬件看门狗。
- 硬件看门狗:看门狗,全称WatchDog Timer,硬件看门狗是利用一个定时器电路,其定时输出连接到电路的复位端,程序在一定时间范围内对定时器清零(俗称“喂狗”),因此程序正常工作时,定时器总不能溢出,也就不能产生复位信号。其本身并不是用来解决系统出现的问题,而是对一些程序潜在错误和恶劣环境干扰等因素导致系统死机而在无人干预情况下自动恢复系统正常工作状态。
- 窗口看门狗:窗口看门狗之所以称为窗口,就是因为其喂狗时间是一个有上下限的范围内(窗口),可以通过设定相关寄存器,设定其上限时间(但是下限是固定的0x3F)。也就是说:喂狗的时间不能过早,也不能过晚,必须在上下限时间范围内喂狗。
- 系统电源设计:Soc一般涉及多档电源的需求,其通常被分为多个电源域,不同的电源域可以独立的上下电。如下面这个系统电源设计的例子,12V供电,先在以及转成3.3供MCU,然后5V供到两个PMIC芯片,最后PMIC芯片再给下一级MTK的各个电源域供电:
实现原理
嵌入式程序运行在以前飞思卡尔的KL24Z开发平台,与PF81通过IIC进行通信配置寄存器控制各BUCK/LDO输出电压以及错误管理与本例着重讲述的看门狗,下面是他的原理示意图:
嵌入式程序
NXP官网提供了PF8x的各个功能的配置接口,包括:
- 上下电顺序
- BUCK/LDO配置
- 电压/温度监控
- 看门狗配置
- 中断相关
下面代码片主要展现的是看门口配置相关的接口,主要包括:
- 看门狗模块窗口等参数配置
- 喂狗接口
- 获取当前看门狗运行状态
内部补充了中文注释帮助您理解:
//基于提供的地址,通过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.
欢迎关注 『十六宿舍』,大家喜欢的话,给个👍,更多关于嵌入式相关技术的内容持续更新中。