user_driver.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. #include "user_driver.h"
  2. #include "Drv_adc.h"
  3. #include "system.h"
  4. #include "yc11xx_bt_interface.h"
  5. #include "att.h"
  6. #include "core_cm0.h"
  7. #include "yc11xx_pwm.h"
  8. #include "user_driver.h"
  9. #include "Drv_adc.h"
  10. #include "yc11xx_gpio.h"
  11. #include "yc11xx_qspi.h"
  12. #include "LH_TaskManager.h"
  13. /*
  14. @file:user_driver.c
  15. @author: LiuHao
  16. Creation Date:2021/07/27
  17. Function: Add related drivers
  18. */
  19. 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);
  20. 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)
  21. {
  22. PWM_InitTypeDef PWM_InitStruct;
  23. PWM_InitStruct.pwm_gpio = gpio;
  24. PWM_InitStruct.PWM_Channel = pwm_channel;
  25. PWM_InitStruct.HighLevelPeriod = pcnt;
  26. PWM_InitStruct.LowLevelPeriod = ncnt;
  27. PWM_InitStruct.pwm_ctrl.clk_div = clk_div;
  28. PWM_InitStruct.pwm_ctrl.StartLevel = LEVEL;
  29. PWM_InitStruct.pwm_ctrl.pwm_switch = SWITCH;
  30. PWM_Init(&PWM_InitStruct);
  31. }
  32. uint8_t Hal_Timer_CheckTimerClock(uint32_t frequency)
  33. {
  34. uint8_t div;
  35. if(frequency >= HAL_MIN_48M_MIN_FREQUENCY && frequency <= HAL_MAX_48M_MAX_FREQUENCY)
  36. {
  37. div = FREQUENCY_DIVISION_0;
  38. }
  39. else if(frequency >= HAL_MIN_24M_MIN_FREQUENCY && frequency <= HAL_MAX_24M_MAX_FREQUENCY)
  40. {
  41. div = FREQUENCY_DIVISION_1;
  42. }
  43. else if(frequency >= HAL_MIN_12M_MIN_FREQUENCY && frequency <= HAL_MAX_12M_MAX_FREQUENCY)
  44. {
  45. div = FREQUENCY_DIVISION_2;
  46. }
  47. else if(frequency >= HAL_MIN_6M_MIN_FREQUENCY && frequency <= HAL_MAX_6M_MAX_FREQUENCY)
  48. {
  49. div = FREQUENCY_DIVISION_3;
  50. }
  51. else if(frequency >= HAL_MIN_3M_MIN_FREQUENCY && frequency <= HAL_MAX_3M_MAX_FREQUENCY)
  52. {
  53. div = FREQUENCY_DIVISION_4;
  54. }
  55. else if(frequency >= HAL_MIN_1500K_MIN_FREQUENCY && frequency <= HAL_MAX_1500K_MAX_FREQUENCY)
  56. {
  57. div = FREQUENCY_DIVISION_5;
  58. }
  59. else if(frequency >= HAL_MIN_750K_MIN_FREQUENCY && frequency <= HAL_MAX_750K_MAX_FREQUENCY)
  60. {
  61. div = FREQUENCY_DIVISION_6;
  62. }
  63. else if(frequency >= HAL_MIN_375K_MIN_FREQUENCY && frequency <= HAL_MAX_375K_MAX_FREQUENCY)
  64. {
  65. div = FREQUENCY_DIVISION_7;
  66. }
  67. return div;
  68. }
  69. /**
  70. #define FREQUENCY_48M_1US 48
  71. #define FREQUENCY_24M_1US 24
  72. #define FREQUENCY_12M_1US 12
  73. #define FREQUENCY_6M_1US 6
  74. #define FREQUENCY_3M_1US 3
  75. #define FREQUENCY_1500K_3US 2
  76. #define FREQUENCY_750K_3US 4
  77. #define FREQUENCY_375K_3US 8
  78. */
  79. uint16_t Hal_Timer_GetOneCycleCount(uint32_t frequency,uint8_t clock)
  80. {
  81. uint32_t count;
  82. switch (clock)
  83. {
  84. case FREQUENCY_DIVISION_0: //48M
  85. count = 48000000 / frequency;
  86. break;
  87. case FREQUENCY_DIVISION_1:
  88. count = 24000000 / frequency;
  89. break;
  90. case FREQUENCY_DIVISION_2:
  91. count = 12000000 / frequency;
  92. break;
  93. case FREQUENCY_DIVISION_3:
  94. count = 6000000 / frequency;
  95. break;
  96. case FREQUENCY_DIVISION_4:
  97. count = 3000000 / frequency;
  98. break;
  99. case FREQUENCY_DIVISION_5:
  100. // count = 2000000 / (3*frequency);
  101. count = 1500000 / (frequency);
  102. break;
  103. case FREQUENCY_DIVISION_6:
  104. count = 750000 / (frequency);
  105. break;
  106. case FREQUENCY_DIVISION_7:
  107. count = 375000 / (frequency);
  108. break;
  109. default:
  110. break;
  111. }
  112. return count;
  113. }
  114. void UserSet_OutPWM(GPIO_NUM gpio, PWM_ChxTypeDef pwm_channel,uint32_t frequecy,uint32_t percent){
  115. if(percent>=100)
  116. {
  117. GPIO_SetOut(gpio,OUT_HIGH);
  118. }
  119. else if(percent==0)
  120. {
  121. GPIO_SetOut(gpio,OUT_LOW);
  122. }
  123. else
  124. {
  125. uint8_t divClk=0;
  126. uint32_t count=0;uint32_t pCnt=0,nCnt,temp=0;
  127. divClk = Hal_Timer_CheckTimerClock(frequecy*2); //分频数
  128. temp = Hal_Timer_GetOneCycleCount(frequecy*2, divClk); //单个周期计数值
  129. #ifdef DEBUG_PWM
  130. MyPrintf("DivClk=%x,temp=%x\r\n",divClk,temp);
  131. #endif
  132. pCnt = (uint32_t)((float)(temp * percent * 100)/10000.0);
  133. if(!(temp > pCnt))//error
  134. {
  135. #ifdef DEBUG_PWM
  136. MyPrintf("\r\nerror\r\n",pCnt,nCnt);
  137. #endif
  138. return;
  139. }
  140. nCnt = temp - pCnt;
  141. #ifdef DEBUG_PWM
  142. MyPrintf("\r\ntemp=%d,pCnt=%d,nCnt=%d\r\n",temp,pCnt,nCnt);
  143. #endif
  144. PWM_Config(gpio,pwm_channel,pCnt,nCnt,divClk,OutputLow,PWM_ENABLE);//no分频
  145. }
  146. }
  147. /*以下处理ADC相关代码*/
  148. void ADC_Configuration(void);
  149. /**
  150. * @brief ADC initialization function.
  151. * @param None
  152. * @retval None
  153. */
  154. void ADC_Configuration(void)
  155. {
  156. GPIO_SetGpioMultFunction(ADC_GET_GPIOx,GPCFG_NO_IE);
  157. ADC_InitTypeDef ADCInitStruct;
  158. ADCInitStruct.ADC_Channel = ADC_CHANNEL_5;
  159. ADCInitStruct.ADC_Mode = ADC_GPIO;
  160. #ifdef hvin_test
  161. ADCInitStruct.ADC_Mode = ADC_HVIN;
  162. #endif
  163. ADC_Init(&ADCInitStruct);
  164. #ifdef DEBUG_ADC
  165. // printf("adc_init suc, gpio mode test 0-1.2V!\n");
  166. MyPrintf("testing channel %d\n",ADCInitStruct.ADC_Channel);
  167. #endif
  168. }
  169. #include "yc11xx_gpio.h"
  170. #include "Drv_mic.h"
  171. #include <stdlib.h>
  172. #include <math.h>
  173. extern MIC_CUR_VARIABLE sMicCurVariable;
  174. /*
  175. * @method Audio_sampling_init
  176. * @brief mic adc init
  177. * @retval None
  178. */
  179. void Audio_sampling_init()
  180. {
  181. DRV_Mic_Init();
  182. //DRV_Mic_Enable();
  183. DRV_Mic_Disable();
  184. }
  185. /*
  186. * @method Audio_get_buffer_len
  187. * @brief Get adc dma buf unread data length.
  188. * @retval None
  189. */
  190. int Audio_get_buffer_len()
  191. {
  192. int audioWPtr = HREADW(CORE_ADCD_ADDR) ;
  193. int audioRPtr = TO_16BIT_ADDR(sMicCurVariable.mReadPtr) ;
  194. int audioBufferLen = (int)(sMicCurVariable.mEndPtr - sMicCurVariable.mReadBufPtr);
  195. if(audioRPtr <= audioWPtr)
  196. {
  197. return (audioWPtr - audioRPtr);
  198. }
  199. else
  200. {
  201. return (audioBufferLen - (audioRPtr - audioWPtr));
  202. }
  203. }
  204. /*
  205. * @method Audio_to_uart_start
  206. * @brief read mic adc data for adc_dma_buf, and send mic adc data by uart.
  207. * @retval None
  208. */
  209. //short testPtr=NULL;
  210. //void Audio_to_uart_start()
  211. //{
  212. // uint8_t audioVal=0;
  213. // if (sMicCurVariable.mMicEnable == MIC_DISABLE)
  214. // return;
  215. // int audioWPtr = HREADW(CORE_ADCD_ADDR) ;
  216. // int bufferLen = Audio_get_buffer_len();
  217. //
  218. // if (bufferLen != 0 && bufferLen >= ENCODE_INPUT_LEN)
  219. // {
  220. // //Original sound sampling(pcm)
  221. // for(int indexL = 0; indexL < ENCODE_INPUT_LEN; indexL++)
  222. // {
  223. // audioVal = *(sMicCurVariable.mReadPtr + indexL);
  224. // printfsend(&audioVal,1);
  225. // //MyPrintf("n:%d v:%d\r\n",indexL,*(sMicCurVariable.mReadPtr + indexL));
  226. // }
  227. // sMicCurVariable.mReadPtr += ENCODE_INPUT_LEN;
  228. // if (sMicCurVariable.mReadPtr == sMicCurVariable.mEndPtr)
  229. // sMicCurVariable.mReadPtr = sMicCurVariable.mReadBufPtr;
  230. // }
  231. //}
  232. /*
  233. * @method Audio_to_uart_start
  234. * @brief read mic adc data for adc_dma_buf, and send mic adc data by uart.
  235. * @retval None
  236. */
  237. short * testPtr=NULL;
  238. short Audio_Buffer[ENCODE_INPUT_LEN]={0};
  239. //static unsigned long long AudioSumVal=0;//
  240. static unsigned int AudioCntIdx=0;
  241. static unsigned char AudioDbVal=0;//分贝值
  242. //static int AudioValMin=0;
  243. //static int AudioValMax=0;
  244. short user_audioVal=0;
  245. /*****************************************************
  246. 输入值:flags:选择中位值滤波或中位值平均滤波
  247. true:中位值平均滤波 false:中位值滤波
  248. 返回值:滤波结果
  249. 备注:中位值滤波:ADC_GetCnt取奇数,ADC_GetCnt=2^x次方+1滤波效果较好
  250. 中位值平均滤波: ADC_GetCnt=2^x次方+2 滤波效果较好
  251. ******************************************************/
  252. short GetAudio_MedianFilter(short *pBuff,uint8_t len,bool flags,uint8_t number) //中位值滤波 中位值平均滤波 N为采样次数,取奇数 Flag:中位值平均滤波使能
  253. {
  254. uint16_t temp=0;
  255. short audio_tempval=0;
  256. unsigned long sum=0;
  257. uint8_t count,i,j,z;
  258. for(j=0;j<len-1;j++) //排序
  259. {
  260. for(i=0;i<len-j-1;i++)
  261. {
  262. if (pBuff[i] > pBuff[i+1] )
  263. {
  264. temp =pBuff[i];
  265. pBuff[i] = pBuff[i+1];
  266. pBuff[i+1] = temp;
  267. }
  268. }
  269. }
  270. if(flags)
  271. {
  272. for(count=number;count<(len-number);count++)//中位再求平均 flags控制
  273. {
  274. sum+=pBuff[count];
  275. }
  276. audio_tempval=(sum/(len-(number*2)));
  277. sum=0;
  278. }
  279. else
  280. {
  281. audio_tempval= pBuff[(len-1)/2]; //中位值
  282. }
  283. return audio_tempval;
  284. }
  285. void Audio_to_uart_start()
  286. {
  287. // char audioVal=0;
  288. short audioVal=0;
  289. int indexL=0;
  290. if (sMicCurVariable.mMicEnable == MIC_DISABLE)
  291. return;
  292. int audioWPtr = HREADW(CORE_ADCD_ADDR) ;
  293. int bufferLen = Audio_get_buffer_len();
  294. if (bufferLen != 0 && bufferLen >= ENCODE_INPUT_LEN)
  295. {
  296. // AudioValMax=0;
  297. testPtr = (short *)sMicCurVariable.mReadPtr;
  298. for( indexL = 0; indexL < (ENCODE_INPUT_LEN>>1); indexL++)
  299. {
  300. // audioVal = *(testPtr+ indexL)/100; //串口显示波形用
  301. // printfsend((uint8_t*)&audioVal,1);
  302. audioVal = *(testPtr+ indexL);
  303. Audio_Buffer[indexL]=abs(audioVal);
  304. //MyPrintf("v:%d\r\n",audioVal);//计算分贝值打印
  305. // if(audioVal>0)
  306. // {
  307. // if(audioVal<AudioValMin)
  308. // {
  309. // AudioValMin = audioVal;
  310. // }
  311. // if(audioVal>AudioValMax)
  312. // {
  313. // AudioValMax = audioVal;
  314. // }
  315. //
  316. // if(AudioCntIdx<ENCODE_INPUT_LEN*100)
  317. // {
  318. // AudioSumVal+=abs(*(testPtr+ indexL));
  319. // AudioCntIdx++;
  320. // }else
  321. // {
  322. // AudioDbVal = (int)(10.0*log10(AudioSumVal));
  323. // AudioSumVal=0;
  324. // AudioCntIdx=0;
  325. // // MyPrintf("sys:%d val:%ddb AudioValMax:%d AudioValMin:%d SumVal:%d\r\n",sys_time_handle.get_run_tickms(),AudioDbVal,AudioValMax,AudioValMin,AudioSumVal);//计算分贝值打印
  326. // AudioValMin=0;
  327. // AudioValMax=0;
  328. // }
  329. // }
  330. }
  331. user_audioVal=GetAudio_MedianFilter(Audio_Buffer,(ENCODE_INPUT_LEN>>1),0,15)/2; //取40组数据的中位值 /2限制幅度
  332. //MyPrintf("m_v:%d\r\n",user_audioVal);//计算打印
  333. // if(sys_time_handle.get_run_tickms()%500==1)
  334. // {
  335. //
  336. // user_audioVal=GetAudio_MedianFilter(Audio_Buffer,(ENCODE_INPUT_LEN>>1),0,15);
  337. // MyPrintf("l_v:%d\r\n",user_audioVal);//计算打印
  338. // }
  339. // MyPrintf("l:%d\r\n",indexL);//计算分贝值打印
  340. // MyPrintf("val:%ddb\r\n",SimpleCalculate_DB(Audio_Buffer,ENCODE_INPUT_LEN));//计算分贝值打印
  341. sMicCurVariable.mReadPtr += ENCODE_INPUT_LEN;
  342. if (sMicCurVariable.mReadPtr == sMicCurVariable.mEndPtr)
  343. sMicCurVariable.mReadPtr = sMicCurVariable.mReadBufPtr;
  344. //user_audioVal = AudioValMax/20;//衰减128
  345. }
  346. }
  347. uint8_t Get_Audio_val(void)
  348. {
  349. // Audio_to_uart_start();
  350. //if(user_audioVal>255)user_audioVal=255;
  351. return user_audioVal;
  352. }
  353. void u_ble_data_send(uint8_t *send_data, uint8_t send_len) /*实现发送ble数据包功能函数*/
  354. {
  355. Bt_SndBleData(BLE_SEND_HANDLE,send_data,send_len);
  356. }
  357. uint32_t HW_Get_Native_Clk_Avoid_Race(void) //返回系统运行时间毫秒数
  358. {
  359. uint32_t native_clk=0;
  360. native_clk = sys_time_handle.get_run_tickms();
  361. return native_clk;
  362. }
  363. //实现对flash的操作 api
  364. void flash_erase_sector(uint32_t address)
  365. {
  366. QSPI_SectorEraseFlash(address);
  367. }
  368. void flash_read_data (uint8_t *buffer, uint32_t address, uint32_t len)
  369. {
  370. QSPI_ReadFlashData(address,len,buffer);
  371. }
  372. void flash_write_data (uint8_t *buffer, uint32_t address, uint32_t len)
  373. {
  374. QSPI_WriteFlashData(address,len,buffer);
  375. }
  376. unsigned char ReverseByteBits(unsigned char num)
  377. {
  378. #if 0
  379. unsigned char ret=0;
  380. char i=0;
  381. for(i=0;i<8;++i)
  382. {
  383. ret <<=1;
  384. ret |=num&1;
  385. num >>=1;
  386. }
  387. return ret;
  388. #endif
  389. num = (((num & 0xaa) >> 1) | ((num & 0x55) << 1));
  390. num = (((num & 0xcc) >> 2) | ((num & 0x33) << 2));
  391. return ((num >> 4) | (num << 4));
  392. }