user_driver.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. }
  184. /*
  185. * @method Audio_get_buffer_len
  186. * @brief Get adc dma buf unread data length.
  187. * @retval None
  188. */
  189. int Audio_get_buffer_len()
  190. {
  191. int audioWPtr = HREADW(CORE_ADCD_ADDR) ;
  192. int audioRPtr = TO_16BIT_ADDR(sMicCurVariable.mReadPtr) ;
  193. int audioBufferLen = (int)(sMicCurVariable.mEndPtr - sMicCurVariable.mReadBufPtr);
  194. if(audioRPtr <= audioWPtr)
  195. {
  196. return (audioWPtr - audioRPtr);
  197. }
  198. else
  199. {
  200. return (audioBufferLen - (audioRPtr - audioWPtr));
  201. }
  202. }
  203. /*
  204. * @method Audio_to_uart_start
  205. * @brief read mic adc data for adc_dma_buf, and send mic adc data by uart.
  206. * @retval None
  207. */
  208. //short testPtr=NULL;
  209. //void Audio_to_uart_start()
  210. //{
  211. // uint8_t audioVal=0;
  212. // if (sMicCurVariable.mMicEnable == MIC_DISABLE)
  213. // return;
  214. // int audioWPtr = HREADW(CORE_ADCD_ADDR) ;
  215. // int bufferLen = Audio_get_buffer_len();
  216. //
  217. // if (bufferLen != 0 && bufferLen >= ENCODE_INPUT_LEN)
  218. // {
  219. // //Original sound sampling(pcm)
  220. // for(int indexL = 0; indexL < ENCODE_INPUT_LEN; indexL++)
  221. // {
  222. // audioVal = *(sMicCurVariable.mReadPtr + indexL);
  223. // printfsend(&audioVal,1);
  224. // //MyPrintf("n:%d v:%d\r\n",indexL,*(sMicCurVariable.mReadPtr + indexL));
  225. // }
  226. // sMicCurVariable.mReadPtr += ENCODE_INPUT_LEN;
  227. // if (sMicCurVariable.mReadPtr == sMicCurVariable.mEndPtr)
  228. // sMicCurVariable.mReadPtr = sMicCurVariable.mReadBufPtr;
  229. // }
  230. //}
  231. /*
  232. * @method Audio_to_uart_start
  233. * @brief read mic adc data for adc_dma_buf, and send mic adc data by uart.
  234. * @retval None
  235. */
  236. short * testPtr=NULL;
  237. short Audio_Buffer[ENCODE_INPUT_LEN]={0};
  238. //参数为数据,采样个数
  239. //返回值为分贝
  240. #define VOLUMEMAX 32767
  241. int SimpleCalculate_DB(short* pcmData, int sample)
  242. {
  243. signed short ret = 0;
  244. if (sample > 0){
  245. long sum = 0;
  246. signed short* pos = (signed short *)pcmData;
  247. for (int i = 0; i < sample; i++){
  248. sum += abs(*pos);
  249. pos++;
  250. }
  251. ret = sum * 500.0 / (sample * VOLUMEMAX);
  252. if (ret >= 100){
  253. ret = 100;
  254. }
  255. }
  256. return ret;
  257. }
  258. //计算均方根(RMS) 即能量值
  259. static const float kMaxSquaredLevel = 32768 * 32768;
  260. const float kMinLevel = 30.f;
  261. int Process(const int16_t* data, size_t length)
  262. {
  263. float sum_square_ = 0;
  264. size_t sample_count_ = 0;
  265. for (size_t i = 0; i < length; ++i) {
  266. sum_square_ += data[i] * data[i];
  267. }
  268. sample_count_ += length;
  269. float rms = sum_square_ / (sample_count_ * kMaxSquaredLevel);
  270. //20log_10(x^0.5) = 10log_10(x)
  271. rms = 10 * log10(rms);
  272. if (rms < -kMinLevel)
  273. rms = -kMinLevel;
  274. rms = -rms;
  275. return (rms + 0.5);
  276. }
  277. static unsigned long long AudioSumVal=0;//
  278. static unsigned int AudioCntIdx=0;
  279. static unsigned char AudioDbVal=0;//分贝值
  280. static int AudioValMin=0;
  281. static int AudioValMax=0;
  282. void Audio_to_uart_start()
  283. {
  284. // char audioVal=0;
  285. short audioVal=0;
  286. int indexL=0;
  287. if (sMicCurVariable.mMicEnable == MIC_DISABLE)
  288. return;
  289. int audioWPtr = HREADW(CORE_ADCD_ADDR) ;
  290. int bufferLen = Audio_get_buffer_len();
  291. if (bufferLen != 0 && bufferLen >= ENCODE_INPUT_LEN)
  292. {
  293. //Original sound sampling(pcm)
  294. testPtr = (short *)sMicCurVariable.mReadPtr;
  295. for( indexL = 0; indexL < (ENCODE_INPUT_LEN>>1); indexL++)
  296. {
  297. // audioVal = *(testPtr+ indexL)/100; //串口显示波形用
  298. // printfsend((uint8_t*)&audioVal,1);
  299. audioVal = *(testPtr+ indexL);
  300. if(audioVal<AudioValMin)
  301. {
  302. AudioValMin = audioVal;
  303. }
  304. if(audioVal>AudioValMax)
  305. {
  306. AudioValMax = audioVal;
  307. }
  308. if(AudioCntIdx<ENCODE_INPUT_LEN*100)
  309. {
  310. AudioSumVal+=abs(*(testPtr+ indexL));
  311. AudioCntIdx++;
  312. }else
  313. {
  314. AudioDbVal = (int)(10.0*log10(AudioSumVal));
  315. AudioSumVal=0;
  316. AudioCntIdx=0;
  317. MyPrintf("sys:%d val:%ddb AudioValMax:%d AudioValMin:%d SumVal:%d\r\n",sys_time_handle.get_run_tickms(),AudioDbVal,AudioValMax,AudioValMin,AudioSumVal);//计算分贝值打印
  318. // MyPrintf("val:%ddb \r\n",AudioDbVal);
  319. AudioValMin=0;AudioValMax=0;
  320. }
  321. //MyPrintf("%c",*(sMicCurVariable.mReadPtr + indexL));
  322. //MyPrintf("%d\r\n",*(testPtr+ indexL)/10);
  323. }
  324. // MyPrintf("val:%ddb\r\n",SimpleCalculate_DB(Audio_Buffer,ENCODE_INPUT_LEN));//计算分贝值打印
  325. sMicCurVariable.mReadPtr += ENCODE_INPUT_LEN;
  326. if (sMicCurVariable.mReadPtr == sMicCurVariable.mEndPtr)
  327. sMicCurVariable.mReadPtr = sMicCurVariable.mReadBufPtr;
  328. }
  329. }
  330. void u_ble_data_send(uint8_t *send_data, uint8_t send_len) /*实现发送ble数据包功能函数*/
  331. {
  332. Bt_SndBleData(BLE_SEND_HANDLE,send_data,send_len);
  333. }
  334. uint32_t HW_Get_Native_Clk_Avoid_Race(void) //返回系统运行时间毫秒数
  335. {
  336. uint32_t native_clk=0;
  337. native_clk = sys_time_handle.get_run_tickms();
  338. return native_clk;
  339. }
  340. //实现对flash的操作 api
  341. void flash_erase_sector(uint32_t address)
  342. {
  343. QSPI_SectorEraseFlash(address);
  344. }
  345. void flash_read_data (uint8_t *buffer, uint32_t address, uint32_t len)
  346. {
  347. QSPI_ReadFlashData(address,len,buffer);
  348. }
  349. void flash_write_data (uint8_t *buffer, uint32_t address, uint32_t len)
  350. {
  351. QSPI_WriteFlashData(address,len,buffer);
  352. }