123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- #include "user_driver.h"
- #include "Drv_adc.h"
- #include "system.h"
- #include "yc11xx_bt_interface.h"
- #include "att.h"
- #include "core_cm0.h"
- #include "yc11xx_pwm.h"
- #include "user_driver.h"
- #include "Drv_adc.h"
- #include "yc11xx_gpio.h"
- #include "yc11xx_qspi.h"
- #include "LH_TaskManager.h"
- void PWM_Config(GPIO_NUM gpio, PWM_ChxTypeDef pwm_channel, uint16_t pcnt,uint16_t ncnt, PWM_ClkdivDef clk_div,START_TypeDef LEVEL, PWM_SwitchDef SWITCH);
- void PWM_Config(GPIO_NUM gpio, PWM_ChxTypeDef pwm_channel, uint16_t pcnt,uint16_t ncnt, PWM_ClkdivDef clk_div,START_TypeDef LEVEL, PWM_SwitchDef SWITCH)
- {
- PWM_InitTypeDef PWM_InitStruct;
- PWM_InitStruct.pwm_gpio = gpio;
- PWM_InitStruct.PWM_Channel = pwm_channel;
- PWM_InitStruct.HighLevelPeriod = pcnt;
- PWM_InitStruct.LowLevelPeriod = ncnt;
- PWM_InitStruct.pwm_ctrl.clk_div = clk_div;
- PWM_InitStruct.pwm_ctrl.StartLevel = LEVEL;
- PWM_InitStruct.pwm_ctrl.pwm_switch = SWITCH;
- PWM_Init(&PWM_InitStruct);
- }
- uint8_t Hal_Timer_CheckTimerClock(uint32_t frequency)
- {
- uint8_t div;
- if(frequency >= HAL_MIN_48M_MIN_FREQUENCY && frequency <= HAL_MAX_48M_MAX_FREQUENCY)
- {
- div = FREQUENCY_DIVISION_0;
- }
- else if(frequency >= HAL_MIN_24M_MIN_FREQUENCY && frequency <= HAL_MAX_24M_MAX_FREQUENCY)
- {
- div = FREQUENCY_DIVISION_1;
- }
- else if(frequency >= HAL_MIN_12M_MIN_FREQUENCY && frequency <= HAL_MAX_12M_MAX_FREQUENCY)
- {
- div = FREQUENCY_DIVISION_2;
- }
- else if(frequency >= HAL_MIN_6M_MIN_FREQUENCY && frequency <= HAL_MAX_6M_MAX_FREQUENCY)
- {
- div = FREQUENCY_DIVISION_3;
- }
- else if(frequency >= HAL_MIN_3M_MIN_FREQUENCY && frequency <= HAL_MAX_3M_MAX_FREQUENCY)
- {
- div = FREQUENCY_DIVISION_4;
- }
- else if(frequency >= HAL_MIN_1500K_MIN_FREQUENCY && frequency <= HAL_MAX_1500K_MAX_FREQUENCY)
- {
- div = FREQUENCY_DIVISION_5;
- }
- else if(frequency >= HAL_MIN_750K_MIN_FREQUENCY && frequency <= HAL_MAX_750K_MAX_FREQUENCY)
- {
- div = FREQUENCY_DIVISION_6;
- }
- else if(frequency >= HAL_MIN_375K_MIN_FREQUENCY && frequency <= HAL_MAX_375K_MAX_FREQUENCY)
- {
- div = FREQUENCY_DIVISION_7;
- }
- return div;
- }
- uint16_t Hal_Timer_GetOneCycleCount(uint32_t frequency,uint8_t clock)
- {
- uint32_t count;
- switch (clock)
- {
- case FREQUENCY_DIVISION_0:
- count = 48000000 / frequency;
- break;
- case FREQUENCY_DIVISION_1:
- count = 24000000 / frequency;
- break;
- case FREQUENCY_DIVISION_2:
- count = 12000000 / frequency;
- break;
- case FREQUENCY_DIVISION_3:
- count = 6000000 / frequency;
- break;
- case FREQUENCY_DIVISION_4:
- count = 3000000 / frequency;
- break;
- case FREQUENCY_DIVISION_5:
-
- count = 1500000 / (frequency);
- break;
- case FREQUENCY_DIVISION_6:
- count = 750000 / (frequency);
- break;
- case FREQUENCY_DIVISION_7:
- count = 375000 / (frequency);
- break;
- default:
- break;
- }
-
- return count;
- }
- void UserSet_OutPWM(GPIO_NUM gpio, PWM_ChxTypeDef pwm_channel,uint32_t frequecy,uint32_t percent){
-
- if(percent>=100)
- {
- GPIO_SetOut(gpio,OUT_HIGH);
- }
- else if(percent==0)
- {
- GPIO_SetOut(gpio,OUT_LOW);
- }
- else
- {
- uint8_t divClk=0;
- uint32_t count=0;uint32_t pCnt=0,nCnt,temp=0;
- divClk = Hal_Timer_CheckTimerClock(frequecy*2);
- temp = Hal_Timer_GetOneCycleCount(frequecy*2, divClk);
- #ifdef DEBUG_PWM
- MyPrintf("DivClk=%x,temp=%x\r\n",divClk,temp);
- #endif
-
- pCnt = (uint32_t)((float)(temp * percent * 100)/10000.0);
- if(!(temp > pCnt))
- {
- #ifdef DEBUG_PWM
- MyPrintf("\r\nerror\r\n",pCnt,nCnt);
- #endif
- return;
- }
- nCnt = temp - pCnt;
- #ifdef DEBUG_PWM
- MyPrintf("\r\ntemp=%d,pCnt=%d,nCnt=%d\r\n",temp,pCnt,nCnt);
- #endif
- PWM_Config(gpio,pwm_channel,pCnt,nCnt,divClk,OutputLow,PWM_ENABLE);
- }
- }
- void ADC_Configuration(void);
- void ADC_Configuration(void)
- {
- GPIO_SetGpioMultFunction(ADC_GET_GPIOx,GPCFG_NO_IE);
-
- ADC_InitTypeDef ADCInitStruct;
- ADCInitStruct.ADC_Channel = ADC_CHANNEL_5;
- ADCInitStruct.ADC_Mode = ADC_GPIO;
- #ifdef hvin_test
- ADCInitStruct.ADC_Mode = ADC_HVIN;
- #endif
- ADC_Init(&ADCInitStruct);
- #ifdef DEBUG_ADC
- MyPrintf("testing channel %d\n",ADCInitStruct.ADC_Channel);
- #endif
- }
- #include "yc11xx_gpio.h"
- #include "Drv_mic.h"
- #include <stdlib.h>
- #include <math.h>
- extern MIC_CUR_VARIABLE sMicCurVariable;
- void Audio_sampling_init()
- {
- DRV_Mic_Init();
-
- DRV_Mic_Disable();
- }
- int Audio_get_buffer_len()
- {
- int audioWPtr = HREADW(CORE_ADCD_ADDR) ;
- int audioRPtr = TO_16BIT_ADDR(sMicCurVariable.mReadPtr) ;
- int audioBufferLen = (int)(sMicCurVariable.mEndPtr - sMicCurVariable.mReadBufPtr);
- if(audioRPtr <= audioWPtr)
- {
- return (audioWPtr - audioRPtr);
- }
- else
- {
- return (audioBufferLen - (audioRPtr - audioWPtr));
- }
- }
- short * testPtr=NULL;
- short Audio_Buffer[ENCODE_INPUT_LEN]={0};
- static unsigned int AudioCntIdx=0;
- static unsigned char AudioDbVal=0;
- short user_audioVal=0;
-
- short GetAudio_MedianFilter(short *pBuff,uint8_t len,bool flags,uint8_t number)
- {
- uint16_t temp=0;
- short audio_tempval=0;
- unsigned long sum=0;
- uint8_t count,i,j,z;
- for(j=0;j<len-1;j++)
- {
- for(i=0;i<len-j-1;i++)
- {
- if (pBuff[i] > pBuff[i+1] )
- {
- temp =pBuff[i];
- pBuff[i] = pBuff[i+1];
- pBuff[i+1] = temp;
- }
- }
- }
- if(flags)
- {
- for(count=number;count<(len-number);count++)
- {
- sum+=pBuff[count];
- }
- audio_tempval=(sum/(len-(number*2)));
- sum=0;
- }
- else
- {
- audio_tempval= pBuff[(len-1)/2];
- }
- return audio_tempval;
- }
- void Audio_to_uart_start()
- {
- short audioVal=0;
- int indexL=0;
- if (sMicCurVariable.mMicEnable == MIC_DISABLE)
- return;
- int audioWPtr = HREADW(CORE_ADCD_ADDR) ;
- int bufferLen = Audio_get_buffer_len();
-
- if (bufferLen != 0 && bufferLen >= ENCODE_INPUT_LEN)
- {
- testPtr = (short *)sMicCurVariable.mReadPtr;
- for( indexL = 0; indexL < (ENCODE_INPUT_LEN>>1); indexL++)
- {
-
- audioVal = *(testPtr+ indexL);
- Audio_Buffer[indexL]=abs(audioVal);
-
- }
- user_audioVal=GetAudio_MedianFilter(Audio_Buffer,(ENCODE_INPUT_LEN>>1),0,15)/2;
-
-
-
- sMicCurVariable.mReadPtr += ENCODE_INPUT_LEN;
- if (sMicCurVariable.mReadPtr == sMicCurVariable.mEndPtr)
- sMicCurVariable.mReadPtr = sMicCurVariable.mReadBufPtr;
-
-
- }
-
- }
- uint8_t Get_Audio_val(void)
- {
-
- return user_audioVal;
- }
- void u_ble_data_send(uint8_t *send_data, uint8_t send_len)
- {
- Bt_SndBleData(BLE_SEND_HANDLE,send_data,send_len);
- }
- uint32_t HW_Get_Native_Clk_Avoid_Race(void)
- {
- uint32_t native_clk=0;
- native_clk = sys_time_handle.get_run_tickms();
- return native_clk;
- }
- void flash_erase_sector(uint32_t address)
- {
- QSPI_SectorEraseFlash(address);
- }
- void flash_read_data (uint8_t *buffer, uint32_t address, uint32_t len)
- {
- QSPI_ReadFlashData(address,len,buffer);
- }
- void flash_write_data (uint8_t *buffer, uint32_t address, uint32_t len)
- {
- QSPI_WriteFlashData(address,len,buffer);
- }
- unsigned char ReverseByteBits(unsigned char num)
- {
- #if 0
- unsigned char ret=0;
- char i=0;
- for(i=0;i<8;++i)
- {
- ret <<=1;
- ret |=num&1;
- num >>=1;
- }
- return ret;
- #endif
- num = (((num & 0xaa) >> 1) | ((num & 0x55) << 1));
- num = (((num & 0xcc) >> 2) | ((num & 0x33) << 2));
-
- return ((num >> 4) | (num << 4));
-
- }
|