此方案用于实现Android主板的定时开机与Watchdog功能,应用于一些特殊产品,如:广告机、自动售货机等。
框图
MCU原理图
说明
- Watch Dog
Watch Dog功能默认关闭,直到Android发送命令打开Watch Dog功能。如果Watch Dog功处于打开状态,Android系统会在小于Watch Dog超时时长内定时发送心跳数据给MCU,如果Watch Dog超时时长到,仍未收到心跳数据,则拉低RESET引脚10ms,使CPU复位重启。 - 定时开机
当MCU收到数据后启动计时,时间到拉低POWER引脚10ms,使CPU开机,同时清除本次定时任务,直到下次CPU重新下发新的定时任务。
协议
通信接口为I2C,从机地址:0x61
功能 | 命令 | 数据 | R/W |
---|---|---|---|
心跳 | 0x01 | 0x55 | W |
定时开机 | 0x02 | 4bytes (倒计时,单位:秒,最大值为1年的秒数:31536000,大端模式高位在前) 0x00:取消定时开机 | W |
开关Watch Dog | 0x03 | 0x01:打开 0x02:关闭 默认:关闭 | W |
设置Watch Dog超时时长 | 0x04 | 2bytes (倒计时,单位:秒,最大值:65535,大端模式高位在前) 默认:180秒(3分钟) | W |
源码
API使用
IMcuService.aidl
package android.os;
/** {@hide} */
interface IMcuService
{
int heartbeat();
int setUptime(int time);
int openWatchdog();
int closeWatchdog();
int setWatchdogDuration(int duration);
}
McuTest.java
package com.ayst.item;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.IBinder;
import android.os.IMcuService;
import android.os.RemoteException;
import java.lang.reflect.Method;
/**
* Created by Administrator on 2018/11/6.
*/
public class McuTest {
private IMcuService mMcuService;
@SuppressLint("WrongConstant")
public McuTest(Context context) {
Method method = null;
try {
method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
IBinder binder = (IBinder) method.invoke(null, new Object[]{"mcu"});
mMcuService = IMcuService.Stub.asInterface(binder);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Heartbeat
*/
public void heartbeat() {
if (null != mMcuService) {
try {
mMcuService.heartbeat();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
/**
* Set the boot countdown
* @param time (unit: second)
* @return <0:error
*/
public int setUptime(int time) {
if (null != mMcuService) {
try {
return mMcuService.setUptime(time);
} catch (RemoteException e) {
e.printStackTrace();
}
}
return -1;
}
/**
* Enable watchdog
* @return <0:error
*/
public int openWatchdog() {
if (null != mMcuService) {
try {
return mMcuService.openWatchdog();
} catch (RemoteException e) {
e.printStackTrace();
}
}
return -1;
}
/**
* Disable watchdog
* @return <0:error
*/
public int closeWatchdog() {
if (null != mMcuService) {
try {
return mMcuService.closeWatchdog();
} catch (RemoteException e) {
e.printStackTrace();
}
}
return -1;
}
/**
* Set watchdog over time duration
* @param duration (unit: second)
* @return <0:error
*/
public int setWatchdogDuration(int duration) {
if (null != mMcuService) {
try {
return mMcuService.setWatchdogDuration(duration);
} catch (RemoteException e) {
e.printStackTrace();
}
}
return -1;
}
}