user_driver.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. #include "user_driver.h"
  2. #include "Drv_adc.h"
  3. #include "yc11xx_bt_interface.h"
  4. #include "att.h"
  5. #include "core_cm0.h"
  6. #include "yc11xx_pwm.h"
  7. #include "user_driver.h"
  8. #include "Drv_adc.h"
  9. #include "yc11xx_gpio.h"
  10. #include "yc11xx_qspi.h"
  11. #include "yc11xx_gpio.h"
  12. #include "Drv_mic.h"
  13. #include <stdlib.h>
  14. #include <math.h>
  15. #include "yc11xx_wdt.h"
  16. #include "u_main.h"
  17. volatile uint8_t ble_connected_state=0; //ble 连接状态
  18. volatile uint8_t g_ir_recv_done_flag = 0x00;//解码成功标志
  19. uint8_t g_ir_code_buffer[4] = {0x00};//红外解码值缓存
  20. static volatile unsigned int sys_run_tickms=0; //从定时器初始化运行到当前的毫秒
  21. static volatile unsigned int sys_run_ticks=0; //从定时器初始化运行到当前的秒
  22. static volatile unsigned short ms_cnt_temp = 0;
  23. static unsigned int system_time_get_run_tickms(void)
  24. {
  25. return sys_run_tickms;
  26. }
  27. static unsigned int system_time_get_run_ticks(void)
  28. {
  29. return sys_run_ticks;
  30. }
  31. void system_time_run_tick_cnt(void)
  32. {
  33. if(++ms_cnt_temp>999)
  34. {
  35. ms_cnt_temp=0;
  36. sys_run_ticks++;
  37. }
  38. sys_run_tickms++;
  39. }
  40. 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);
  41. 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)
  42. {
  43. PWM_InitTypeDef PWM_InitStruct;
  44. PWM_InitStruct.pwm_gpio = gpio;
  45. PWM_InitStruct.PWM_Channel = pwm_channel;
  46. PWM_InitStruct.HighLevelPeriod = pcnt;
  47. PWM_InitStruct.LowLevelPeriod = ncnt;
  48. PWM_InitStruct.pwm_ctrl.clk_div = clk_div;
  49. PWM_InitStruct.pwm_ctrl.StartLevel = LEVEL;
  50. PWM_InitStruct.pwm_ctrl.pwm_switch = SWITCH;
  51. PWM_Init(&PWM_InitStruct);
  52. }
  53. uint8_t Hal_Timer_CheckTimerClock(uint32_t frequency)
  54. {
  55. uint8_t div;
  56. if(frequency >= HAL_MIN_48M_MIN_FREQUENCY && frequency <= HAL_MAX_48M_MAX_FREQUENCY)
  57. {
  58. div = FREQUENCY_DIVISION_0;
  59. }
  60. else if(frequency >= HAL_MIN_24M_MIN_FREQUENCY && frequency <= HAL_MAX_24M_MAX_FREQUENCY)
  61. {
  62. div = FREQUENCY_DIVISION_1;
  63. }
  64. else if(frequency >= HAL_MIN_12M_MIN_FREQUENCY && frequency <= HAL_MAX_12M_MAX_FREQUENCY)
  65. {
  66. div = FREQUENCY_DIVISION_2;
  67. }
  68. else if(frequency >= HAL_MIN_6M_MIN_FREQUENCY && frequency <= HAL_MAX_6M_MAX_FREQUENCY)
  69. {
  70. div = FREQUENCY_DIVISION_3;
  71. }
  72. else if(frequency >= HAL_MIN_3M_MIN_FREQUENCY && frequency <= HAL_MAX_3M_MAX_FREQUENCY)
  73. {
  74. div = FREQUENCY_DIVISION_4;
  75. }
  76. else if(frequency >= HAL_MIN_1500K_MIN_FREQUENCY && frequency <= HAL_MAX_1500K_MAX_FREQUENCY)
  77. {
  78. div = FREQUENCY_DIVISION_5;
  79. }
  80. else if(frequency >= HAL_MIN_750K_MIN_FREQUENCY && frequency <= HAL_MAX_750K_MAX_FREQUENCY)
  81. {
  82. div = FREQUENCY_DIVISION_6;
  83. }
  84. else if(frequency >= HAL_MIN_375K_MIN_FREQUENCY && frequency <= HAL_MAX_375K_MAX_FREQUENCY)
  85. {
  86. div = FREQUENCY_DIVISION_7;
  87. }
  88. return div;
  89. }
  90. /**
  91. #define FREQUENCY_48M_1US 48
  92. #define FREQUENCY_24M_1US 24
  93. #define FREQUENCY_12M_1US 12
  94. #define FREQUENCY_6M_1US 6
  95. #define FREQUENCY_3M_1US 3
  96. #define FREQUENCY_1500K_3US 2
  97. #define FREQUENCY_750K_3US 4
  98. #define FREQUENCY_375K_3US 8
  99. */
  100. uint16_t Hal_Timer_GetOneCycleCount(uint32_t frequency,uint8_t clock)
  101. {
  102. uint32_t count;
  103. switch (clock)
  104. {
  105. case FREQUENCY_DIVISION_0: //48M
  106. count = 48000000 / frequency;
  107. break;
  108. case FREQUENCY_DIVISION_1:
  109. count = 24000000 / frequency;
  110. break;
  111. case FREQUENCY_DIVISION_2:
  112. count = 12000000 / frequency;
  113. break;
  114. case FREQUENCY_DIVISION_3:
  115. count = 6000000 / frequency;
  116. break;
  117. case FREQUENCY_DIVISION_4:
  118. count = 3000000 / frequency;
  119. break;
  120. case FREQUENCY_DIVISION_5:
  121. // count = 2000000 / (3*frequency);
  122. count = 1500000 / (frequency);
  123. break;
  124. case FREQUENCY_DIVISION_6:
  125. count = 750000 / (frequency);
  126. break;
  127. case FREQUENCY_DIVISION_7:
  128. count = 375000 / (frequency);
  129. break;
  130. default:
  131. break;
  132. }
  133. return count;
  134. }
  135. void Hal_Set_OutPWM(GPIO_NUM gpio, PWM_ChxTypeDef pwm_channel,uint32_t frequecy,uint32_t percent)
  136. {
  137. if(percent>=100)
  138. {
  139. GPIO_SetOut(gpio,OUT_HIGH);
  140. }
  141. else if(percent==0)
  142. {
  143. GPIO_SetOut(gpio,OUT_LOW);
  144. }
  145. else
  146. {
  147. uint8_t divClk=0;
  148. uint32_t count=0;uint32_t pCnt=0,nCnt,temp=0;
  149. divClk = Hal_Timer_CheckTimerClock(frequecy*2); //分频数
  150. temp = Hal_Timer_GetOneCycleCount(frequecy*2, divClk); //单个周期计数值
  151. #ifdef DEBUG_PWM
  152. MyPrintf("DivClk=%x,temp=%x\r\n",divClk,temp);
  153. #endif
  154. pCnt = (uint32_t)((float)(temp * percent * 100)/10000.0);
  155. if(!(temp > pCnt))//error
  156. {
  157. #ifdef DEBUG_PWM
  158. MyPrintf("\r\nerror\r\n",pCnt,nCnt);
  159. #endif
  160. return;
  161. }
  162. nCnt = temp - pCnt;
  163. #ifdef DEBUG_PWM
  164. MyPrintf("\r\ntemp=%d,pCnt=%d,nCnt=%d\r\n",temp,pCnt,nCnt);
  165. #endif
  166. PWM_Config(gpio,pwm_channel,pCnt,nCnt,divClk,OutputLow,PWM_ENABLE);//no分频
  167. }
  168. }
  169. /*以下处理ADC相关代码*/
  170. void ADC_Configuration(void);
  171. /**
  172. * @brief ADC initialization function.
  173. * @param None
  174. * @retval None
  175. */
  176. void ADC_Configuration(void)
  177. {
  178. GPIO_SetGpioMultFunction(ADC_GET_GPIOx,GPCFG_NO_IE);
  179. ADC_InitTypeDef ADCInitStruct;
  180. ADCInitStruct.ADC_Channel = ADC_CHANNEL_5;
  181. ADCInitStruct.ADC_Mode = ADC_GPIO;
  182. #ifdef hvin_test
  183. ADCInitStruct.ADC_Mode = ADC_HVIN;
  184. #endif
  185. ADC_Init(&ADCInitStruct);
  186. #ifdef DEBUG_ADC
  187. // printf("adc_init suc, gpio mode test 0-1.2V!\n");
  188. MyPrintf("testing channel %d\n",ADCInitStruct.ADC_Channel);
  189. #endif
  190. }
  191. extern MIC_CUR_VARIABLE sMicCurVariable;
  192. /*
  193. * @method Audio_sampling_init
  194. * @brief mic adc init
  195. * @retval None
  196. */
  197. void Audio_sampling_init()
  198. {
  199. DRV_Mic_Init();
  200. //DRV_Mic_Enable();
  201. DRV_Mic_Disable();
  202. }
  203. /*
  204. * @method Audio_get_buffer_len
  205. * @brief Get adc dma buf unread data length.
  206. * @retval None
  207. */
  208. int Audio_get_buffer_len()
  209. {
  210. int audioWPtr = HREADW(CORE_ADCD_ADDR) ;
  211. int audioRPtr = TO_16BIT_ADDR(sMicCurVariable.mReadPtr) ;
  212. int audioBufferLen = (int)(sMicCurVariable.mEndPtr - sMicCurVariable.mReadBufPtr);
  213. if(audioRPtr <= audioWPtr)
  214. {
  215. return (audioWPtr - audioRPtr);
  216. }
  217. else
  218. {
  219. return (audioBufferLen - (audioRPtr - audioWPtr));
  220. }
  221. }
  222. /*
  223. * @method audio_dma_handle
  224. * @brief read mic adc data for adc_dma_buf, and send mic adc data by uart.
  225. * @retval None
  226. */
  227. static short * testPtr=NULL;
  228. static short Audio_Buffer[ENCODE_INPUT_LEN]={0};
  229. static unsigned int AudioCntIdx=0;
  230. static short mic_audio_val=0;
  231. short GetAudio_MedianFilter(short *pBuff,uint8_t len,bool flags,uint8_t number) //中位值滤波 中位值平均滤波 N为采样次数,取奇数 Flag:中位值平均滤波使能
  232. {
  233. uint16_t temp=0;
  234. short audio_tempval=0;
  235. unsigned long sum=0;
  236. uint8_t count,i,j,z;
  237. for(j=0;j<len-1;j++) //排序
  238. {
  239. for(i=0;i<len-j-1;i++)
  240. {
  241. if (pBuff[i] > pBuff[i+1] )
  242. {
  243. temp =pBuff[i];
  244. pBuff[i] = pBuff[i+1];
  245. pBuff[i+1] = temp;
  246. }
  247. }
  248. }
  249. if(flags)
  250. {
  251. for(count=number;count<(len-number);count++)//中位再求平均 flags控制
  252. {
  253. sum+=pBuff[count];
  254. }
  255. audio_tempval=(sum/(len-(number*2)));
  256. sum=0;
  257. }
  258. else
  259. {
  260. audio_tempval= pBuff[(len-1)/2]; //中位值
  261. }
  262. return audio_tempval;
  263. }
  264. void audio_dma_handle(void)
  265. {
  266. short audioVal=0;
  267. int indexL=0;
  268. if (sMicCurVariable.mMicEnable == MIC_DISABLE)
  269. return;
  270. int audioWPtr = HREADW(CORE_ADCD_ADDR) ;
  271. int bufferLen = Audio_get_buffer_len();
  272. if (bufferLen != 0 && bufferLen >= ENCODE_INPUT_LEN)
  273. {
  274. testPtr = (short *)sMicCurVariable.mReadPtr;
  275. for( indexL = 0; indexL < (ENCODE_INPUT_LEN>>1); indexL++)
  276. {
  277. // audioVal = *(testPtr+ indexL)/100; //串口显示波形用
  278. // printfsend((uint8_t*)&audioVal,1);
  279. audioVal = *(testPtr+ indexL);
  280. Audio_Buffer[indexL]=abs(audioVal);
  281. }
  282. mic_audio_val=GetAudio_MedianFilter(Audio_Buffer,(ENCODE_INPUT_LEN>>1),0,15)/2; //取40组数据的中位值 /2限制幅度
  283. sMicCurVariable.mReadPtr += ENCODE_INPUT_LEN;
  284. if (sMicCurVariable.mReadPtr == sMicCurVariable.mEndPtr)
  285. sMicCurVariable.mReadPtr = sMicCurVariable.mReadBufPtr;
  286. }
  287. }
  288. void delay_us(uint32_t num)//需要调整参数
  289. {
  290. uint32_t i,j;
  291. for(i=0; i<num; i++)
  292. for(j=0; j<3; j++)
  293. {
  294. ;
  295. }
  296. }
  297. void read_otp_values(void)//上电读取otp 有问题的芯片直接不往后面运行
  298. {
  299. uint8_t otp_data[14]={0};
  300. uint16_t read_otp_flag=0;
  301. delay_us(1000);//稳定一下再读取
  302. read_otp_data(OTP_ADC_IO_0_5V_ADDR,otp_data,14);
  303. read_otp_flag=(otp_data[13]<<8)|otp_data[12];
  304. if((read_otp_flag!=OTP_ADC_FLAG)||(otp_data[0]==0&&otp_data[1]==0)||(otp_data[6]==0&&otp_data[7]==0))
  305. {
  306. delay_us(1000);//稳定一下再读取一次
  307. read_otp_data(OTP_ADC_IO_0_5V_ADDR,otp_data,14);
  308. read_otp_flag=(otp_data[13]<<8)|otp_data[12];
  309. if((read_otp_flag!=OTP_ADC_FLAG)||(otp_data[0]==0&&otp_data[1]==0)||(otp_data[6]==0&&otp_data[7]==0))
  310. {
  311. while(1)
  312. {
  313. //黄色的RGB是255、255、0
  314. Hal_Set_OutPWM(PWM_CHANCE_B,PWM_CHANNEL_1,20000,0);//B
  315. Hal_Set_OutPWM(PWM_CHANCE_G,PWM_CHANNEL_0,20000,99);// G
  316. Hal_Set_OutPWM(PWM_CHANCE_R,PWM_CHANNEL_2,20000,99);//R
  317. #ifdef HW_WATCH_DOG_FUNC
  318. WDT_Enable();//看门狗
  319. #endif
  320. }
  321. }
  322. }
  323. }
  324. #if (NEC_ENABLE==1)
  325. //选择 nec接收的gpio, 开使能
  326. void nec_receive_enable(GPIO_NUM gpio)
  327. {
  328. GPIO_SetGpioMultFunction(gpio,GPCFG_PULLUP);
  329. HWRITE(mem_nec_gpio,gpio);
  330. HWRITE(mem_nec_receive_enable,1);
  331. }
  332. #define NEC_CMD 0x01
  333. #define NEC_REPEAT_CMD 0x02
  334. //static unsigned char ReverseByteBits(volatile unsigned char num)
  335. //{
  336. // volatile unsigned char temp = num;
  337. // #if 0
  338. // unsigned char ret=0;
  339. // char i=0;
  340. // for(i=0;i<8;++i)
  341. // {
  342. // ret <<=1;
  343. // ret |=num&1;
  344. // num >>=1;
  345. // }
  346. // return ret;
  347. // #endif
  348. // temp = (((temp & 0xaa) >> 1) | ((temp & 0x55) << 1));
  349. // temp = (((temp & 0xcc) >> 2) | ((temp & 0x33) << 2));
  350. // return ((temp >> 4) | (temp << 4));
  351. //}
  352. void Bt_NecCallBack(uint8_t len,uint8_t *dataPtr)
  353. {
  354. int i=0;
  355. switch(*dataPtr)
  356. {
  357. case NEC_CMD:
  358. #ifdef DEBUG_DRV_APP
  359. u_ble_data_send(dataPtr,len);
  360. #endif
  361. if(len==5)
  362. {
  363. for( i=0;i<len-1;i++)
  364. {
  365. g_ir_code_buffer[i]=dataPtr[i+1];
  366. #ifdef DEBUG_DRV_APP
  367. MyPrintf("nec data[%d]=%x\r\n",i,dataPtr[i+1]);
  368. #endif
  369. }
  370. g_ir_recv_done_flag=1;
  371. }
  372. break;
  373. case NEC_REPEAT_CMD:
  374. #ifdef DEBUG_DRV_APP
  375. MyPrintf("\r\n*************NEC_REPEAT_CMD****************\r\n");
  376. #endif
  377. break;
  378. default:
  379. break;
  380. }
  381. }
  382. #endif
  383. void nec_decode_init(void) //nec 解码初始化函数
  384. {
  385. #if (NEC_ENABLE==1)
  386. nec_receive_enable(NEC_GPIO);
  387. #endif
  388. }
  389. uint8_t *get_decode_value(void) //获取解码值
  390. {
  391. return (g_ir_code_buffer);
  392. }
  393. /****************** 判断是否带麦************************/
  394. void firmware_mic_check_init(void)
  395. {
  396. #if (firmware_is_mic==1)
  397. g_light_para.mic=1;
  398. #else
  399. g_light_para.mic=0;
  400. #endif
  401. // GPIO_config(2,2,0);
  402. // GPIO_config(3,1,2);
  403. // if( gpio_input(2,2) ==0)
  404. // {
  405. // g_light_para.mic=0;
  406. // }else
  407. // {
  408. // g_light_para.mic=1;
  409. // }
  410. }
  411. void user_driver_init(void)//用户驱动初始化
  412. {
  413. Audio_sampling_init();
  414. nec_decode_init();
  415. ADC_Configuration();
  416. }
  417. uint16_t get_shortout_adc_value(void)//获取短路保护引脚的adc电压值 单位mv
  418. {
  419. int adc_value=0;
  420. adc_value = ADC_GetVoltage(ADC_CHANNEL_5);
  421. if(adc_value>0)
  422. {
  423. return (uint16_t)adc_value;
  424. }
  425. return 0;
  426. }
  427. uint8_t get_mic_audio_value(void)//获取检测到mic的adc值
  428. {
  429. return mic_audio_val;
  430. }
  431. void u_ble_data_send(uint8_t *send_data, uint8_t send_len) //实现发送ble数据包功能函数
  432. {
  433. if(ble_connected_state==1)
  434. Bt_SndBleData(BLE_SEND_HANDLE,send_data,send_len);
  435. }
  436. //实现对flash的操作 api 保存配置数据用
  437. void flash_erase_sector(uint32_t address)
  438. {
  439. QSPI_SectorEraseFlash(address);
  440. }
  441. void flash_read_data (uint8_t *buffer, uint32_t address, uint32_t len)
  442. {
  443. QSPI_ReadFlashData(address,len,buffer);
  444. }
  445. void flash_write_data(uint8_t *buffer, uint32_t address, uint32_t len)
  446. {
  447. QSPI_WriteFlashData(address,len,buffer);
  448. }
  449. void user_set_pwmout_dutycycle(uint8_t channel,uint32_t duty) //设置3路 pwm输出占空比
  450. {
  451. switch(channel)
  452. {
  453. case RED_PWM_CHANNEL:
  454. Hal_Set_OutPWM(PWM_CHANCE_R,PWM_CHANNEL_0,20000,duty);//R
  455. break;
  456. case GREEN_PWM_CHANNEL:
  457. Hal_Set_OutPWM(PWM_CHANCE_G,PWM_CHANNEL_2,20000,duty);
  458. break;
  459. case BLUE_PWM_CHANNEL:
  460. Hal_Set_OutPWM(PWM_CHANCE_B,PWM_CHANNEL_1,20000,duty);//B
  461. break;
  462. }
  463. }
  464. sys_time_handle_t sys_time_handle={
  465. .init = NULL,
  466. .run_tickcnt = system_time_run_tick_cnt,
  467. .get_run_ticks = system_time_get_run_ticks,
  468. .get_run_tickms=system_time_get_run_tickms
  469. } ; //系统时间句柄