123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
- #include "user_driver.h"
- #include "Drv_adc.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 "yc11xx_gpio.h"
- #include "Drv_mic.h"
- #include <stdlib.h>
- #include <math.h>
- #include "yc11xx_wdt.h"
- #include "u_main.h"
- volatile uint8_t ble_connected_state=0; //ble 连接状态
- volatile uint8_t g_ir_recv_done_flag = 0x00;//解码成功标志
- uint8_t g_ir_code_buffer[4] = {0x00};//红外解码值缓存
- static volatile unsigned int sys_run_tickms=0; //从定时器初始化运行到当前的毫秒
- static volatile unsigned int sys_run_ticks=0; //从定时器初始化运行到当前的秒
- static volatile unsigned short ms_cnt_temp = 0;
- static unsigned int system_time_get_run_tickms(void)
- {
- return sys_run_tickms;
- }
- static unsigned int system_time_get_run_ticks(void)
- {
- return sys_run_ticks;
- }
- void system_time_run_tick_cnt(void)
- {
- if(++ms_cnt_temp>999)
- {
- ms_cnt_temp=0;
- sys_run_ticks++;
- }
- sys_run_tickms++;
- }
- 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 Hal_Set_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
- }
- 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_dma_handle
- * @brief read mic adc data for adc_dma_buf, and send mic adc data by uart.
- * @retval None
- */
- static short * testPtr=NULL;
- static short Audio_Buffer[ENCODE_INPUT_LEN]={0};
- static unsigned int AudioCntIdx=0;
- static short mic_audio_val=0;
- 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_dma_handle(void)
- {
- 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)/100; //串口显示波形用
- // printfsend((uint8_t*)&audioVal,1);
- audioVal = *(testPtr+ indexL);
- Audio_Buffer[indexL]=abs(audioVal);
- }
- mic_audio_val=GetAudio_MedianFilter(Audio_Buffer,(ENCODE_INPUT_LEN>>1),0,15)/2; //取40组数据的中位值 /2限制幅度
- sMicCurVariable.mReadPtr += ENCODE_INPUT_LEN;
- if (sMicCurVariable.mReadPtr == sMicCurVariable.mEndPtr)
- sMicCurVariable.mReadPtr = sMicCurVariable.mReadBufPtr;
- }
- }
- void delay_us(uint32_t num)//需要调整参数
- {
- uint32_t i,j;
- for(i=0; i<num; i++)
- for(j=0; j<3; j++)
- {
- ;
- }
- }
- void read_otp_values(void)//上电读取otp 有问题的芯片直接不往后面运行
- {
- uint8_t otp_data[14]={0};
- uint16_t read_otp_flag=0;
- delay_us(1000);//稳定一下再读取
- read_otp_data(OTP_ADC_IO_0_5V_ADDR,otp_data,14);
- read_otp_flag=(otp_data[13]<<8)|otp_data[12];
- if((read_otp_flag!=OTP_ADC_FLAG)||(otp_data[0]==0&&otp_data[1]==0)||(otp_data[6]==0&&otp_data[7]==0))
- {
- delay_us(1000);//稳定一下再读取一次
- read_otp_data(OTP_ADC_IO_0_5V_ADDR,otp_data,14);
- read_otp_flag=(otp_data[13]<<8)|otp_data[12];
- if((read_otp_flag!=OTP_ADC_FLAG)||(otp_data[0]==0&&otp_data[1]==0)||(otp_data[6]==0&&otp_data[7]==0))
- {
- while(1)
- {
- //黄色的RGB是255、255、0
- Hal_Set_OutPWM(PWM_CHANCE_B,PWM_CHANNEL_1,20000,0);//B
- Hal_Set_OutPWM(PWM_CHANCE_G,PWM_CHANNEL_0,20000,99);// G
- Hal_Set_OutPWM(PWM_CHANCE_R,PWM_CHANNEL_2,20000,99);//R
- #ifdef HW_WATCH_DOG_FUNC
- WDT_Enable();//看门狗
- #endif
- }
- }
- }
- }
- #if (NEC_ENABLE==1)
- //选择 nec接收的gpio, 开使能
- void nec_receive_enable(GPIO_NUM gpio)
- {
- GPIO_SetGpioMultFunction(gpio,GPCFG_PULLUP);
- HWRITE(mem_nec_gpio,gpio);
- HWRITE(mem_nec_receive_enable,1);
- }
- #define NEC_CMD 0x01
- #define NEC_REPEAT_CMD 0x02
- //static unsigned char ReverseByteBits(volatile unsigned char num)
- //{
- // volatile unsigned char temp = 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
- // temp = (((temp & 0xaa) >> 1) | ((temp & 0x55) << 1));
- // temp = (((temp & 0xcc) >> 2) | ((temp & 0x33) << 2));
- // return ((temp >> 4) | (temp << 4));
- //}
- void Bt_NecCallBack(uint8_t len,uint8_t *dataPtr)
- {
- int i=0;
- switch(*dataPtr)
- {
- case NEC_CMD:
- #ifdef DEBUG_DRV_APP
- u_ble_data_send(dataPtr,len);
- #endif
- if(len==5)
- {
- for( i=0;i<len-1;i++)
- {
- g_ir_code_buffer[i]=dataPtr[i+1];
- #ifdef DEBUG_DRV_APP
- MyPrintf("nec data[%d]=%x\r\n",i,dataPtr[i+1]);
- #endif
- }
- g_ir_recv_done_flag=1;
- }
- break;
- case NEC_REPEAT_CMD:
- #ifdef DEBUG_DRV_APP
- MyPrintf("\r\n*************NEC_REPEAT_CMD****************\r\n");
- #endif
- break;
- default:
- break;
- }
- }
- #endif
- void nec_decode_init(void) //nec 解码初始化函数
- {
- #if (NEC_ENABLE==1)
- nec_receive_enable(NEC_GPIO);
- #endif
- }
- uint8_t *get_decode_value(void) //获取解码值
- {
- return (g_ir_code_buffer);
- }
- /****************** 判断是否带麦************************/
- void firmware_mic_check_init(void)
- {
- #if (firmware_is_mic==1)
- g_light_para.mic=1;
- #else
- g_light_para.mic=0;
- #endif
- // GPIO_config(2,2,0);
- // GPIO_config(3,1,2);
- // if( gpio_input(2,2) ==0)
- // {
- // g_light_para.mic=0;
- // }else
- // {
- // g_light_para.mic=1;
- // }
- }
- void user_driver_init(void)//用户驱动初始化
- {
- Audio_sampling_init();
- nec_decode_init();
- ADC_Configuration();
-
- }
- uint16_t get_shortout_adc_value(void)//获取短路保护引脚的adc电压值 单位mv
- {
- int adc_value=0;
- adc_value = ADC_GetVoltage(ADC_CHANNEL_5);
- if(adc_value>0)
- {
- return (uint16_t)adc_value;
- }
- return 0;
- }
- uint8_t get_mic_audio_value(void)//获取检测到mic的adc值
- {
- return mic_audio_val;
- }
- void u_ble_data_send(uint8_t *send_data, uint8_t send_len) //实现发送ble数据包功能函数
- {
- if(ble_connected_state==1)
- Bt_SndBleData(BLE_SEND_HANDLE,send_data,send_len);
- }
- //实现对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);
- }
- void user_set_pwmout_dutycycle(uint8_t channel,uint32_t duty) //设置3路 pwm输出占空比
- {
- switch(channel)
- {
- case RED_PWM_CHANNEL:
- Hal_Set_OutPWM(PWM_CHANCE_R,PWM_CHANNEL_0,20000,duty);//R
- break;
-
- case GREEN_PWM_CHANNEL:
- Hal_Set_OutPWM(PWM_CHANCE_G,PWM_CHANNEL_2,20000,duty);
- break;
-
- case BLUE_PWM_CHANNEL:
- Hal_Set_OutPWM(PWM_CHANCE_B,PWM_CHANNEL_1,20000,duty);//B
- break;
- }
- }
- sys_time_handle_t sys_time_handle={
- .init = NULL,
- .run_tickcnt = system_time_run_tick_cnt,
- .get_run_ticks = system_time_get_run_ticks,
- .get_run_tickms=system_time_get_run_tickms
- } ; //系统时间句柄
|