📄 speaker.c
字号:
//*****************************************************************************
// File: speaker.c
//
// Description:
// This file contains the functions: control the speaker to sound
//
//
// Author: sky lo
//
//
// 09.19.2006 - Created.
//11.30.2006 -modify to adjust kimo project
//
//09.27.2006 modify by sky 1).soundplay(SOUND_PROFILE *pSoundProfile ) direct setvolum with pSoundProfile->volum
// so not use call setvolum() after call soundplay(SOUND_PROFILE *pSoundProfile)
// 2).deal with system will die when input frequency1=0 and frequency2=0
// Copyright 2006(c)primax. All Rights Reserved
//*****************************************************************************
#include "fct.h"
#include "Iomacros.h"
#include "ts.h"
#include "standard.h"
#include "icu.h"
#include "syt.h"
#include "speaker.h"
#include "uinew.h"
enum
{
F2FreqLargeThan0Hz,
F2FreqIs0Hz
}F2freq;
enum
{
SingleTone,
MutiTone
}SoundTone;
enum
{
soundncycle,
soundforever
}SoundMode;
enum
{
cadenceontimephase,
cadenceofftimephase
}CadencePhase;
#define SPK_OFF 46
#define SPK_KEY 45
unsigned char Timer3InitAlready=0;
static Uint32 counter1;
static Uint32 counter2;
static Uint32 counter3;
static Uint32 counter4;
static Uint32 counter1_backup;
static Uint32 counter2_backup;
static Uint32 counter3_backup;
static Uint32 counter4_backup;
static Uint32 cycle;
static Uint8 statusflag; //00: tone 1; 0x01:tone 2;0x03:silent
static Uint8 wavestatusflag; //0x00: hight(sound) 0x01:low(not sound)
static Uint32 frequency1;
static Uint32 frequency2;
static Uint32 frequency3;
extern UIMaintain gUIMain;
void init_speaker(void)
{
ChangePinOveylay(SPK_OFF);
ChangeDir_Mask(SPK_OFF,PinDirOutput);
ChangePinOveylay(SPK_KEY);
ChangeDir_Mask(SPK_KEY,PinDirOutput);
}
unsigned char ReadPIORegisterData(unsigned char GPIOPinNumber)
{
Uint8 PIORegisterDataValue;
Uint32 TempVar1;
Uint32 TempVar2;
Uint32 TempVar3;
Uint32 TempVar4;
TempVar1=GPIOPinNumber/32;
TempVar2=GPIOPinNumber-TempVar1*32;
TempVar3=0X00000001;
TempVar2=TempVar3<<TempVar2;
TempVar3=IO_READ32(PinDataRegAddressBase+TempVar1*0x04);
TempVar4=IO_READ32(InvctlxRegisterAddressBase+TempVar1*0x04);
if (TempVar4 & TempVar2)
{
if (TempVar3 & TempVar2)
{
PIORegisterDataValue=0x00;
}
else
{
PIORegisterDataValue=0x01;
}
}
else
{
if (TempVar3 & TempVar2)
{
PIORegisterDataValue=0x01;
}
else
{
PIORegisterDataValue=0x00;
}
}
return PIORegisterDataValue;
}
//******************************状态机编程 *************
void OS_Timer3LISR(void)
{
ICU_InterruptClear(IRQ_TIM3);
if ((cycle>0) || (SoundMode==soundforever))
{
if (CadencePhase == cadenceontimephase)
{//state1 and state2
if (statusflag == 0x00)
{//state1:sound in 1st tone
switch (wavestatusflag)
{
case 0x00:
{//子状态1
wavestatusflag=0x01;
SetGpio_1(SPK_KEY);
}break;
case 0x01:
{//子状态2
counter1--;
if (counter1==0x00)
{
if (SoundTone==MutiTone)
{//switch to state2
CadencePhase = cadenceontimephase;
counter2=counter2_backup;
statusflag=0x01;
wavestatusflag=0x00;
//init for the first time sound wave2
SetGpio_0(SPK_KEY);
IO_WRITE32(SYTTC3, frequency2);
IO_WRITE32(SYTCNT3, frequency2);
}
else
{//soundtone=singletone
counter3--;
if (counter3==0x00)
{//switch to state3
CadencePhase = cadenceofftimephase;
counter4=counter4_backup;
//init for the first time sound wave2
SetGpio_0(SPK_KEY);
IO_WRITE32(SYTTC3, frequency3);
IO_WRITE32(SYTCNT3, frequency3);
}
else
{//still in state1,but cadenceontimecycle--
CadencePhase = cadenceontimephase;
statusflag=0x00;
counter1=counter1_backup;
wavestatusflag=0x00;
SetGpio_0(SPK_KEY);
}
}
}
else
{
wavestatusflag=0x00;
SetGpio_0(SPK_KEY);
}
}break;
default: ;
}
}
else
{//state2: sound in second tone
if (F2freq == F2FreqIs0Hz)
{//silence
counter2--;
if (counter2 == 0)
{
counter3--;
if (counter3 == 0)
{//change state to state3
CadencePhase = cadenceofftimephase;
counter4=counter4_backup;
//init for the first time sound wave2
SetGpio_0(SPK_KEY);
IO_WRITE32(SYTTC3, frequency3);
IO_WRITE32(SYTCNT3, frequency3);
}
else
{//change state to state1
CadencePhase = cadenceontimephase;
statusflag=0x00;
wavestatusflag=0x00;
counter1=counter1_backup;
//init for the first time sound wave2
SetGpio_0(SPK_KEY);
IO_WRITE32(SYTTC3, frequency1);
IO_WRITE32(SYTCNT3, frequency1);
}
}
else
{//F2ontime is over than 1 second
//the first frequency2 is maybe not 1s,so here must change the frequency to 1 second
IO_WRITE32(SYTTC3, 183000000);
IO_WRITE32(SYTCNT3, 183000000);
}
}else
{//sound second tone
switch (wavestatusflag)
{
case 0x00:
{
SetGpio_1(SPK_KEY);
wavestatusflag=0x01;
}break;
case 0x01:
{
counter2--;
if (counter2 ==0)
{
counter3--;
if (counter3 == 0)
{//change state to state3
CadencePhase = cadenceofftimephase;
counter4=counter4_backup;
//init for the first time sound wave2
SetGpio_0(SPK_KEY);
IO_WRITE32(SYTTC3, frequency3);
IO_WRITE32(SYTCNT3, frequency3);
}
else
{//change state to state1
//change the state flag to state1
CadencePhase = cadenceontimephase;
counter1=counter1_backup;
statusflag=0x00;
wavestatusflag=0x00;
//init for the first time sound wave1
SetGpio_0(SPK_KEY);
IO_WRITE32(SYTTC3, frequency1);
IO_WRITE32(SYTCNT3, frequency1);
}
}
else
{
SetGpio_0(SPK_KEY);
wavestatusflag=0x00;
}
}break;
default: ;
}
}
}
}
else
{//state3: silence last for cadenceofftime
counter4--;
if (counter4==0x00)
{
if (SoundMode == soundforever)
{//switch to state1
//change the state flag to state1
CadencePhase = cadenceontimephase;
counter1=counter1_backup;
counter3=counter3_backup; //cadenceontime cycle
statusflag=0x00;
wavestatusflag=0x00;
//init for the first time sound wave1
SetGpio_0(SPK_KEY);
IO_WRITE32(SYTTC3, frequency1);
IO_WRITE32(SYTCNT3, frequency1);
}
else
{//soundncyle
cycle--;
if (cycle == 0x00)
{
SoundPlayOff();
}
else
{//swtich to state1
//change the state flag to state1
CadencePhase = cadenceontimephase;
counter1=counter1_backup;
counter3=counter3_backup; //cadenceontime cycle
statusflag=0x00;
wavestatusflag=0x00;
//init for the first time sound wave1
SetGpio_0(SPK_KEY);
IO_WRITE32(SYTTC3, frequency1);
IO_WRITE32(SYTCNT3, frequency1);
}
}
}
else
{//cadenceofftime large than 1 second,so the first frequency is maybe not 1s,so here must change the frequency to 1 second
IO_WRITE32(SYTTC3, 183000000);
IO_WRITE32(SYTCNT3, 183000000);
}
}
}
}
int SoundPlay(SOUND_PROFILE *pSoundProfile)
{
Uint32 val;
switch( pSoundProfile->SoundType )
{
case SOUND_OF_KEYPAD:
case SOUND_OF_RING:
case SOUND_OF_JOB_END_TONE:
case SOUND_OF_JOB_ERROR_TONE:
case SOUND_OF_ALARM:
//****************input parameter is wrong*******
if (pSoundProfile->F1Freq == 0)
{
PSPRINTF("F1Freq should 10Hz~ 10kHz\n");
return 0;
}
//****************************init****************
if (pSoundProfile->F2OnTime == 0)
{
SoundTone=SingleTone;
}
else
{
SoundTone=MutiTone;
}
if (pSoundProfile->F2Freq == 0)
{
F2freq=F2FreqIs0Hz;
}
else
{
F2freq=F2FreqLargeThan0Hz;
}
if (pSoundProfile->CadenceCycles==0)
{
SoundMode=soundforever;
cycle=0;
}
else
{
cycle=pSoundProfile->CadenceCycles;
SoundMode=soundncycle;
}
init_speaker();
//************************wave 1****************
frequency1=66500000/pSoundProfile->F1Freq;
counter1=(pSoundProfile->F1OnTime * pSoundProfile->F1Freq)/1000;
counter1_backup=counter1;
if (counter1 ==0)
{
PSPRINTF("pSoundProfile->F1OnTime should > one cycle time \n");
return 0;
}
//************************wave 2*****************
if (SoundTone==MutiTone)
{
if (pSoundProfile->F2Freq ==0)
{
if (pSoundProfile->F2OnTime > 1000)
{
counter2= pSoundProfile->F2OnTime /1000;
val=pSoundProfile->F2OnTime - counter2 * 1000;
if (val == 0)
{
frequency2=133000000; //1s
}
else
{
frequency2=133000 * val;
counter2=counter2+1; //the first wave frequency1 < 1s, remain wave frequency is 1s.
}
counter2_backup=counter2;
}
else
{
counter2=1;
counter2_backup=counter2;
frequency2=133000 * pSoundProfile->F2OnTime;
}
}
else
{
frequency2=66500000/pSoundProfile->F2Freq;
counter2=(pSoundProfile->F2OnTime * pSoundProfile->F2Freq)/1000;
counter2_backup=counter2;
if (counter2==0)
{
PSPRINTF("pSoundProfile->F2OnTime should > one cycle time \n");
return 0;
}
}
}
//**************************** cadanceontime**********
counter3=pSoundProfile->CadenceOnTime /(pSoundProfile->F1OnTime + pSoundProfile->F2OnTime);
counter3_backup=counter3;
//****************************cadanceofftime***********
if (pSoundProfile->CadenceOffTime> 1000)
{
counter4= pSoundProfile->CadenceOffTime / 1000;
val=pSoundProfile->CadenceOffTime- counter4 * 1000;
if (val == 0)
{
frequency3=133000000; //1s
}
else
{
frequency3=133000 * val;
counter4=counter4+1; //the first wave frequency1 < 1s, remain wave frequency is 1s.
}
counter4_backup=counter4;
}
else
{
frequency3=133000 * pSoundProfile->CadenceOffTime;
counter4=1;
counter4_backup=counter4;
}
//**********************start timer3***************************
statusflag=0x00;
wavestatusflag=0x00;
CadencePhase=cadenceontimephase;
if (Timer3InitAlready)
{
IO_WRITE32(SYTTC3, frequency1);
IO_WRITE32(SYTCNT3, frequency1);
IO_WRITE32(SYTCTL3, 0x00000007);
}
else
{
SYT_Init(SYT_Timer3, frequency1, OS_Timer3LISR);
Timer3InitAlready=0x01;
}
SetGpio_0(SPK_KEY);
SetGpio_0(SPK_OFF);
return 1;
case SOUND_OF_OFFHOOK:
case SOUND_OF_LINE_MONITOR:
init_speaker();
SetGpio_0(SPK_OFF); //open speaker
return 1;
default:
return 0;
}
}
int SoundPlayOff(void)
{
SetGpio_1(SPK_OFF);
IO_WRITE32(SYTCTL3, 0x00000005);
return 1;
}
int SoundSetVolume(Uint8 Volume)
{
if(Volume == 0)
SoundPlayOff();
return 1;
}
int SoundGetVolume(Uint8 *pVolume)
{
*pVolume = gUIMain.commonsetting.Speaker;
return 1;
}
//add foy junny
void SpeakerSoundPlay(SOUND_TYPES sound_type)
{
SOUND_PROFILE sound_profile_var;
switch (sound_type)
{
case SOUND_OF_KEYPAD:
{
sound_profile_var.F1Freq=700;
sound_profile_var.F1OnTime=10;
sound_profile_var.F2Freq=0;
sound_profile_var.F2OnTime=0;
sound_profile_var.CadenceOnTime=100;
sound_profile_var.CadenceOffTime=0;
sound_profile_var.CadenceCycles=1;
sound_profile_var.SoundType=SOUND_OF_KEYPAD;
SoundPlay(&sound_profile_var);
}break;
case SOUND_OF_RING:
{
sound_profile_var.F1Freq=400;
sound_profile_var.F1OnTime=25;
sound_profile_var.F2Freq=0;
sound_profile_var.F2OnTime=25;
sound_profile_var.CadenceOnTime=1000;
sound_profile_var.CadenceOffTime=3000;
sound_profile_var.CadenceCycles=0;
sound_profile_var.SoundType=SOUND_OF_RING;
SoundPlay(&sound_profile_var);
}break;
case SOUND_OF_ALARM:
{
sound_profile_var.F1Freq=833;
sound_profile_var.F1OnTime=100;
sound_profile_var.F2Freq=0;
sound_profile_var.F2OnTime=0;
sound_profile_var.CadenceOnTime=500;
sound_profile_var.CadenceOffTime=1000;
sound_profile_var.CadenceCycles=5;
sound_profile_var.SoundType=SOUND_OF_ALARM;
SoundPlay(&sound_profile_var);
}break;
case SOUND_OF_JOB_END_TONE:
{
sound_profile_var.F1Freq=833;
sound_profile_var.F1OnTime=100;
sound_profile_var.F2Freq=0;
sound_profile_var.F2OnTime=0;
sound_profile_var.CadenceOnTime=1000;
sound_profile_var.CadenceOffTime=0;
sound_profile_var.CadenceCycles=1;
sound_profile_var.SoundType=SOUND_OF_JOB_END_TONE;
SoundPlay(&sound_profile_var);
}break;
case SOUND_OF_JOB_ERROR_TONE:
{
sound_profile_var.F1Freq=833;
sound_profile_var.F1OnTime=50;
sound_profile_var.F2Freq=0;
sound_profile_var.F2OnTime=50;
sound_profile_var.CadenceOnTime=200;
sound_profile_var.CadenceOffTime=1500;
sound_profile_var.CadenceCycles=0;
sound_profile_var.SoundType=SOUND_OF_JOB_ERROR_TONE;
SoundPlay(&sound_profile_var);
}break;
case SOUND_OF_LINE_MONITOR:
{
sound_profile_var.SoundType=SOUND_OF_LINE_MONITOR;
SoundPlay(&sound_profile_var);
}break;
case SOUND_OF_OFFHOOK:
{
sound_profile_var.SoundType=SOUND_OF_OFFHOOK;
SoundPlay(&sound_profile_var);
}break;
default:;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -