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"
- /*
- @file:user_driver.c
- @author: LiuHao
- Creation Date:2021/07/27
- Function: Add related drivers
- */
- 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;
- }
- /**
- #define FREQUENCY_48M_1US 48
- #define FREQUENCY_24M_1US 24
- #define FREQUENCY_12M_1US 12
- #define FREQUENCY_6M_1US 6
- #define FREQUENCY_3M_1US 3
- #define FREQUENCY_1500K_3US 2
- #define FREQUENCY_750K_3US 4
- #define FREQUENCY_375K_3US 8
- */
- uint16_t Hal_Timer_GetOneCycleCount(uint32_t frequency,uint8_t clock)
- {
- uint32_t count;
- switch (clock)
- {
- case FREQUENCY_DIVISION_0: //48M
- 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 = 2000000 / (3*frequency);
- 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))//error
- {
- #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);//no分频
- }
- }
- /*以下处理ADC相关代码*/
- void ADC_Configuration(void);
- /**
- * @brief ADC initialization function.
- * @param None
- * @retval None
- */
- 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
- // printf("adc_init suc, gpio mode test 0-1.2V!\n");
- 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;
- /*
- * @method Audio_sampling_init
- * @brief mic adc init
- * @retval None
- */
- void Audio_sampling_init()
- {
- DRV_Mic_Init();
- //DRV_Mic_Enable();
- DRV_Mic_Disable();
- }
- /*
- * @method Audio_get_buffer_len
- * @brief Get adc dma buf unread data length.
- * @retval None
- */
- 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));
- }
- }
- /*
- * @method Audio_to_uart_start
- * @brief read mic adc data for adc_dma_buf, and send mic adc data by uart.
- * @retval None
- */
- //short testPtr=NULL;
- //void Audio_to_uart_start()
- //{
- // uint8_t audioVal=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)
- // {
- // //Original sound sampling(pcm)
- // for(int indexL = 0; indexL < ENCODE_INPUT_LEN; indexL++)
- // {
- // audioVal = *(sMicCurVariable.mReadPtr + indexL);
- // printfsend(&audioVal,1);
- // //MyPrintf("n:%d v:%d\r\n",indexL,*(sMicCurVariable.mReadPtr + indexL));
- // }
- // sMicCurVariable.mReadPtr += ENCODE_INPUT_LEN;
- // if (sMicCurVariable.mReadPtr == sMicCurVariable.mEndPtr)
- // sMicCurVariable.mReadPtr = sMicCurVariable.mReadBufPtr;
- // }
- //}
- /*
- * @method Audio_to_uart_start
- * @brief read mic adc data for adc_dma_buf, and send mic adc data by uart.
- * @retval None
- */
- short * testPtr=NULL;
- short Audio_Buffer[ENCODE_INPUT_LEN]={0};
- //static unsigned long long AudioSumVal=0;//
- static unsigned int AudioCntIdx=0;
- static unsigned char AudioDbVal=0;//分贝值
- //static int AudioValMin=0;
- //static int AudioValMax=0;
- short user_audioVal=0;
- /*****************************************************
- 输入值:flags:选择中位值滤波或中位值平均滤波
- true:中位值平均滤波 false:中位值滤波
- 返回值:滤波结果
- 备注:中位值滤波:ADC_GetCnt取奇数,ADC_GetCnt=2^x次方+1滤波效果较好
- 中位值平均滤波: ADC_GetCnt=2^x次方+2 滤波效果较好
- ******************************************************/
- short GetAudio_MedianFilter(short *pBuff,uint8_t len,bool flags,uint8_t number) //中位值滤波 中位值平均滤波 N为采样次数,取奇数 Flag:中位值平均滤波使能
- {
- 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++)//中位再求平均 flags控制
- {
- 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()
- {
- // char audioVal=0;
- 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)
- {
- // AudioValMax=0;
- testPtr = (short *)sMicCurVariable.mReadPtr;
- for( indexL = 0; indexL < (ENCODE_INPUT_LEN>>1); indexL++)
- {
- // audioVal = *(testPtr+ indexL)/100; //串口显示波形用
- // printfsend((uint8_t*)&audioVal,1);
-
- audioVal = *(testPtr+ indexL);
- Audio_Buffer[indexL]=abs(audioVal);
- //MyPrintf("v:%d\r\n",audioVal);//计算分贝值打印
- // if(audioVal>0)
- // {
- // if(audioVal<AudioValMin)
- // {
- // AudioValMin = audioVal;
- // }
- // if(audioVal>AudioValMax)
- // {
- // AudioValMax = audioVal;
- // }
- //
- // if(AudioCntIdx<ENCODE_INPUT_LEN*100)
- // {
- // AudioSumVal+=abs(*(testPtr+ indexL));
- // AudioCntIdx++;
- // }else
- // {
- // AudioDbVal = (int)(10.0*log10(AudioSumVal));
- // AudioSumVal=0;
- // AudioCntIdx=0;
- // // MyPrintf("sys:%d val:%ddb AudioValMax:%d AudioValMin:%d SumVal:%d\r\n",sys_time_handle.get_run_tickms(),AudioDbVal,AudioValMax,AudioValMin,AudioSumVal);//计算分贝值打印
- // AudioValMin=0;
- // AudioValMax=0;
- // }
- // }
- }
- user_audioVal=GetAudio_MedianFilter(Audio_Buffer,(ENCODE_INPUT_LEN>>1),0,15)/2; //取40组数据的中位值 /2限制幅度
- //MyPrintf("m_v:%d\r\n",user_audioVal);//计算打印
- // if(sys_time_handle.get_run_tickms()%500==1)
- // {
- //
- // user_audioVal=GetAudio_MedianFilter(Audio_Buffer,(ENCODE_INPUT_LEN>>1),0,15);
- // MyPrintf("l_v:%d\r\n",user_audioVal);//计算打印
- // }
- // MyPrintf("l:%d\r\n",indexL);//计算分贝值打印
- // MyPrintf("val:%ddb\r\n",SimpleCalculate_DB(Audio_Buffer,ENCODE_INPUT_LEN));//计算分贝值打印
- sMicCurVariable.mReadPtr += ENCODE_INPUT_LEN;
- if (sMicCurVariable.mReadPtr == sMicCurVariable.mEndPtr)
- sMicCurVariable.mReadPtr = sMicCurVariable.mReadBufPtr;
-
- //user_audioVal = AudioValMax/20;//衰减128
- }
-
- }
- uint8_t Get_Audio_val(void)
- {
- // Audio_to_uart_start();
- //if(user_audioVal>255)user_audioVal=255;
- return user_audioVal;
- }
- void u_ble_data_send(uint8_t *send_data, uint8_t send_len) /*实现发送ble数据包功能函数*/
- {
- 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;
- }
- //实现对flash的操作 api
- 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));
-
- }
|