当系统开机后,只需执行一次初始化设备,使用两个while循环的好处,当线程未收到退出消息时,将持续执行内循环,循环未发生异常时,一直执行,发生异常时,重新执行外循环初始化硬件,但不退出线程。
内循环发生异常,热插拔耳机时,内循环注销设备,退出循环,重新执行一次初始化设备打开设备。
g_char = SX_FALSE;
#define MYFABS(x,y) (((x) > (y)) ? ((x)-(y)) : ((y)-(x)))
int main(int argc, char *argv[])
{
HI_S32 s32Ret = 0;
int fd0 = -1;
char *filename0 = NULL;
char buf[10]={0};
int audioret = -1;
filename0 = "/dev/tlv320aic31";
SX_U32 ts0 = get_sys_ms();
SX_U32 oldMs = ts0;
SX_U32 currentMs = ts0;
char reopen = 0,time = 0,detect_interrupt = 0;
g_bQuit = SX_FALSE;
while( g_bQuit == SX_FALSE)
{
//init system
CONFIG_Open( handle );
SYS_Open( handle );
VI_Open( handle );
// open dev
fd0 = open(filename0, O_RDWR);
if (fd0 < 0) {
printf("app open %s fail\n", filename0);
return 0;
} else {
printf("app open %s success\r\n",filename0);
read(fd0,buf,4);
}
while( g_bQuit == SX_FALSE )
{
currentMs = get_sys_ms();
if( MYFABS(currentMs, oldMs) > 1000 )
{
oldMs = currentMs;
SX_S32 second = MYFABS(currentMs, ts0) / 1000;
if( second % 60 == 0 )
{
//TRACE( DL_INFO, "system run %ld hour %ld minutes\n", second/3600, (second%3600 ) / 60 );
}
if( 1 )
{
if(reopen == 1)
{
reopen = 0;
fd0 = open(filename0, O_RDWR);
if (fd0 < 0) {
printf("app open %s fail\n", filename0);
return 0;
} else {
printf("app open %s success\r\n",filename0);
}
}
if(detect_interrupt == 1)
{
time ++;
if(time < 2)
{
read(fd0,buf,4); //中断异常,重新读,才能关闭中断,这是bug
continue;
}
else
time = 0;
detect_interrupt = 0;
}
audioret = read(fd0,buf,4); //AudioStatus
if(audioret < 0 ) {
printf("app read audiostatus fail\n");
} else {
if(( (buf[0] & 0x10) == 0x10) && ( (buf[1] & 0x04) == 0x04)) // headset remmov then insert detect ,reset aicfg
{
close(fd0);
reopen = 1;
detect_interrupt = 1;
mySAMPLE_AUDIO_AdecAo_exit();
mySAMPLE_AUDIO_AiAenc_exit();
system("/root/ko/reloadao.sh"); //rm and reload tl320 ko
my_audio_init();
mySAMPLE_AUDIO_AiAenc_start();
mySAMPLE_AUDIO_AdecAo_start();
mySAMPLE_AUDIO_SetAo_Volume(6);
continue;
}
}
}
}
}
}
TRACE(DL_INFO, "exit main ok\n");
return 0;
}