board.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. #include "includes.h"
  2. //LSAPI_OSI_Timer_t *BleTimer_t=NULL;
  3. void gpioConfigInput();
  4. //////////////////////封装一下AT接口/////////////////////////////////
  5. void msgAtSend(char *at){
  6. ModemSendAt(at);
  7. }
  8. void msgAtSends(char *at ,int len){
  9. ModemSendAt(at);
  10. }
  11. //////日志打印口配置/////////////////
  12. void my_usb_evt_cb(lsUSBEvent_t event, unsigned long p){
  13. LSAPI_OSI_Event_t usb_event;
  14. usb_event.id = OHPOC_EVENT_USB;
  15. usb_event.param1=event;
  16. //wlog_info("my_usb_evt_cb:%d",event);
  17. osiEventTrySend((LSAPI_OSI_Thread_t *)p, &usb_event, 0);
  18. }
  19. LSAPI_Device_USBConfig_t usb_cfg = {
  20. .name = LSAPI_DEV_USB_COM7,
  21. .event_cb = my_usb_evt_cb,
  22. };
  23. bool usbThreadExit=false;
  24. LSAPI_Device_t *usbdev=NULL;
  25. LSAPI_OSI_Thread_t *gUsbWRTd;
  26. static void usbPortHandler(void *param){
  27. int ret;
  28. unsigned char buffer[100];
  29. LSAPI_OSI_Event_t waitevent;
  30. LSAPI_Log_Debug("usb create ok");
  31. while(usbThreadExit==false){
  32. LSAPI_OSI_EventWait(LSAPI_OSI_ThreadCurrent(), &waitevent);
  33. if(waitevent.param1==LS_USB_EVENT_RX_ARRIVED){
  34. while(LSAPI_Device_ReadAvail(usbdev)>0){
  35. ret = LSAPI_Device_Read(usbdev, buffer, sizeof(buffer));
  36. if(ret< sizeof(buffer)) buffer[ret]=0;
  37. usbCmdHandler(buffer,ret);
  38. }
  39. //WakeupNow("usbCallBack");
  40. }
  41. }
  42. LSAPI_Log_Debug("usbPortHandler exit");
  43. LSAPI_Device_Close(usbdev);
  44. gUsbWRTd=NULL;
  45. LSAPI_OSI_ThreadExit();
  46. }
  47. static bool logPortInit(void){
  48. gUsbWRTd=LSAPI_OSI_ThreadCreate("usb", usbPortHandler, NULL, LSAPI_OSI_PRIORITY_NORMAL, 1024*32, 4);
  49. if(gUsbWRTd==NULL) return false;
  50. usb_cfg.event_cb_ctx = gUsbWRTd;
  51. usbdev = LSAPI_Device_USBCreateEx(&usb_cfg);
  52. if(usbdev==NULL){
  53. usbThreadExit=true;
  54. LSAPI_Log_Debug("usbex create err");
  55. return false;
  56. }
  57. if(false==LSAPI_Device_Open(usbdev)){
  58. LSAPI_Log_Debug("usb LSAPI_Device_Open failed");
  59. return false;
  60. }
  61. LSAPI_Device_SetAutoSleep(usbdev, 500);
  62. LSAPI_Log_Debug("usbComInit done");
  63. return true;
  64. }
  65. void usbOutPut(unsigned char *info, int len){
  66. if(NULL==usbdev) return;
  67. if(LSAPI_Device_WriteAvail(usbdev) != -1) LSAPI_Device_Write(usbdev, info,len);
  68. }
  69. void usbOutPutStr(unsigned char *info){
  70. if(NULL==usbdev) return;
  71. if(LSAPI_Device_WriteAvail(usbdev) != -1) LSAPI_Device_Write(usbdev, info,strlen(info));
  72. }
  73. static void lcdInitTask(void *param){
  74. //MSG_WARN(1,"lcd init thread start");
  75. if(0==lcdDrv_Init(0)) MSG_INFO(1, "lcd init ok");
  76. else MSG_INFO(1, "lcd init failed");
  77. guiInit();
  78. MSG_WARN(1,"lcd init thread end");
  79. sutApp.guiStatus=1;
  80. LSAPI_OSI_ThreadExit();
  81. }
  82. static void lcdsInit(void){
  83. if(NULL==LSAPI_OSI_ThreadCreate("lcdinit", lcdInitTask, NULL, LSAPI_OSI_PRIORITY_NORMAL, LCD_INIT_TASK_THREAD_STACK, 2)){
  84. MSG_WARN(1,"lcd init thread error");
  85. return;
  86. }
  87. }
  88. //////////////////gpio及定时器/////////////////////////////
  89. LSAPI_OSI_Timer_t *pKeytimer_t=NULL;
  90. LSAPI_OSI_Timer_t *pBackLightTimer_t=NULL;
  91. LSAPI_OSI_Timer_t *pVibTimer_t=NULL;
  92. bool pkeyTimerCtl=false;
  93. unsigned short keyStatus=0;
  94. unsigned short getKeyValue(){
  95. unsigned char encodekey=getEncodeStatus();
  96. have_encode=1;
  97. if(ENCODE_SHUN==encodekey){
  98. keyStatus=MKEY_VALUE_DOWN;
  99. }
  100. else if(ENCODE_NI==encodekey){
  101. keyStatus=MKEY_VALUE_UP;
  102. }
  103. else have_encode=0;
  104. return keyStatus;
  105. }
  106. unsigned short SetZeroKeyValue(){
  107. keyStatus=0;
  108. }
  109. //规避龙尚驱动矩阵不足问题
  110. void clearKeyValue(){
  111. keyStatus=0;
  112. if(keyStatus | MKEY_VALUE_PTT){//规避组合键出现PTT一直按下状态问题
  113. if(sutApp.pttReq){
  114. sutApp.pttReq=0;
  115. msgAtSend("AT+POC=0C0000\r\n");
  116. }
  117. }
  118. }
  119. static void startKeyTimer(void){
  120. if(NULL==pKeytimer_t) return;
  121. if(pkeyTimerCtl==true) return;
  122. pkeyTimerCtl=true;
  123. LSAPI_OSI_TimerStart(pKeytimer_t,KEY_TIMER_TICK);
  124. }
  125. void stopKeyTimer(void){
  126. pkeyTimerCtl=false;
  127. LSAPI_OSI_TimerStop(pKeytimer_t);
  128. }
  129. static unsigned int msTick=0;//用于做应用层的非休眠时定时器
  130. unsigned int getAppTick(void){return msTick;}
  131. static void AppTimeroutcallback(void *param){
  132. //毿00秒去检测键值状怿去抖势
  133. //检测按锿
  134. keyCheck(keyStatus);
  135. msTick ++;
  136. //EarDlyCheck();
  137. if(pkeyTimerCtl==true) LSAPI_OSI_TimerStart(pKeytimer_t,KEY_TIMER_TICK);
  138. }
  139. LSAPI_Device_t *SpkGpio;
  140. LSAPI_Device_t *PwrHoldGpio;
  141. static LSAPI_Device_t * RedGpio = NULL;
  142. static LSAPI_Device_t * GreenGpio = NULL;
  143. //#define RED_GPIO_ID 23
  144. //#define GREEN_GPIO_ID 18
  145. #define SPK_GPIO_ID 19 //19
  146. #define PWR_HOLD_ID 22
  147. void gpioConfigOutput(void){
  148. bool write_value = false;
  149. LSAPI_GpioConfig_t gpioConfig = {0};
  150. // config property
  151. gpioConfig.mode = LS_GPIO_OUTPUT;
  152. gpioConfig.out_level=true;
  153. // create instance
  154. RedGpio=LSAPI_Device_LedCreate(LS_LED_2);
  155. GreenGpio=LSAPI_Device_LedCreate(LS_LED_1);
  156. if(LSAPI_Device_Open(RedGpio)==false){
  157. MSG_INFO(1,"RedGpio---fail");
  158. }
  159. if(LSAPI_Device_Open(GreenGpio)==false){
  160. MSG_INFO(1,"GreenGpio----fail");
  161. }
  162. gpioConfig.id = SPK_GPIO_ID;
  163. SpkGpio = LSAPI_Device_GPIOCreate(&gpioConfig);
  164. gpioConfig.id = PWR_HOLD_ID;
  165. PwrHoldGpio = LSAPI_Device_GPIOCreate(&gpioConfig);
  166. LSAPI_Device_Open(SpkGpio);
  167. LSAPI_Device_Write(SpkGpio, (void *)&write_value, 1);
  168. LSAPI_Device_Open(PwrHoldGpio);
  169. write_value=true;//启动成功后,默认把电源给锁稳
  170. LSAPI_Device_Write(PwrHoldGpio, (void *)&write_value, 1);
  171. gpioBleConfigOutput();
  172. gpioConfigInput();
  173. redLedCtl(write_value);
  174. }
  175. #define ENCODE1_GPIO_ID 18
  176. #define ENCODE2_GPIO_ID 23
  177. static LSAPI_Device_t * Encode1Ctl = NULL;
  178. static LSAPI_Device_t * Encode2Ctl = NULL;
  179. unsigned char g_iEncode=ENCODE_IDLE;
  180. unsigned char g_iStatus=0;
  181. unsigned char g_iLastStatus=0;
  182. void encodeReleased(void){
  183. g_iEncode=ENCODE_IDLE;
  184. }
  185. LSAPI_GpioIntrCB_t inputCB_Encode(void *ctx){
  186. bool encode1,encode2;
  187. bool encodeValid1=false,encodeValid2=false;
  188. if(NULL != Encode1Ctl){
  189. if (1 == LSAPI_Device_Read(Encode1Ctl, (void *)&encode1, 1)) encodeValid1=true;
  190. }
  191. if(NULL != Encode2Ctl){
  192. if (1 == LSAPI_Device_Read(Encode2Ctl, (void *)&encode2, 1)) encodeValid2=true;
  193. }
  194. if(encodeValid1==true){
  195. if(encode1==true) g_iStatus |= (1<<0);
  196. else g_iStatus &= ~(1<<0);
  197. }
  198. if(encodeValid2==true){
  199. if(encode2==true) g_iStatus |= (1<<1);
  200. else g_iStatus &= ~(1<<1);
  201. }
  202. MSG_INFO(0, "g_iStatus======%d",g_iStatus);
  203. if(encodeValid1==true || encodeValid2==true){
  204. if(g_iEncode==ENCODE_IDLE){
  205. switch(g_iStatus){
  206. case 0:
  207. if(g_iLastStatus==1) g_iEncode=ENCODE_NI;
  208. else if(g_iLastStatus==2) g_iEncode=ENCODE_SHUN;
  209. else if(g_iLastStatus==0) g_iEncode=ENCODE_SHUN;
  210. break;
  211. case 3:
  212. if(g_iLastStatus==1) g_iEncode=ENCODE_SHUN;
  213. else if(g_iLastStatus==2) g_iEncode=ENCODE_NI;
  214. else if(g_iLastStatus==3) g_iEncode=ENCODE_NI;
  215. break;
  216. }
  217. if(g_iEncode) encodeSendEvent(g_iEncode);
  218. }
  219. MSG_INFO(0, "\r\n",g_iEncode);//这个不要去掉,否则按一个方向,偶尔还是会反向一下
  220. g_iLastStatus=g_iStatus;
  221. }
  222. }
  223. void gpioConfigInput(){
  224. LSAPI_GpioConfig_t gpioConfig = {0};
  225. gpioConfig.mode = LS_GPIO_INPUT;
  226. gpioConfig.intr_enabled=true;
  227. gpioConfig.intr_level=false;
  228. gpioConfig.rising=true;
  229. gpioConfig.falling=true;
  230. gpioConfig.cb_ctx = NULL;
  231. gpioConfig.id = ENCODE1_GPIO_ID;
  232. gpioConfig.cb=inputCB_Encode;
  233. Encode1Ctl = LSAPI_Device_GPIOCreate(&gpioConfig);
  234. LSAPI_Device_Open(Encode1Ctl);
  235. gpioConfig.id = ENCODE2_GPIO_ID;
  236. gpioConfig.cb=inputCB_Encode;
  237. Encode2Ctl = LSAPI_Device_GPIOCreate(&gpioConfig);
  238. LSAPI_Device_Open(Encode2Ctl);
  239. }
  240. void pwrHoldCtl(bool onoff){
  241. bool write_value = onoff;
  242. LSAPI_Device_Write(PwrHoldGpio, (void *)&write_value, 1);
  243. }
  244. void redLedCtl(bool onoff){
  245. bool write_value = onoff;
  246. LSAPI_Device_Write(RedGpio, (void *)&write_value, 1);
  247. }
  248. void greenLedCtl(bool onoff){
  249. bool write_value = onoff;
  250. LSAPI_Device_Write(GreenGpio, (void *)&write_value, 1);
  251. }
  252. void spkCtl(bool onoff){
  253. bool write_value = onoff;
  254. LSAPI_Device_Write(SpkGpio, (void *)&write_value, 1);
  255. }
  256. //void lcdBackList(char onoff){
  257. // bool write_value = onoff;
  258. // //LSAPI_Device_Write(LcdBlGpio, (void *)&write_value, 1);
  259. // LSAPI_PmuSwitchPower(OSI_MAKE_TAG('C', 'A', 'M', 'D '), onoff, false);
  260. //}
  261. void VibCb(void *param){
  262. VibCtrl(0);
  263. LSAPI_OSI_TimerStop(pVibTimer_t);
  264. }
  265. void startBackLightTimer(int time){
  266. if(NULL==pBackLightTimer_t) return;
  267. LSAPI_OSI_TimerStart(pBackLightTimer_t,time);
  268. }
  269. static void gpioTask(void *param){
  270. unsigned int param1,param2,param3;
  271. LSAPI_OSI_Event_t event = {};
  272. //创建一个定时器
  273. if(NULL == pKeytimer_t) pKeytimer_t = LSAPI_OSI_TimerCreate(LSAPI_OSI_ThreadCurrent(), AppTimeroutcallback, NULL);
  274. if(NULL == pKeytimer_t) MSG_WARN(1,"gpio timer init failed");
  275. if(NULL == pBackLightTimer_t) pBackLightTimer_t = LSAPI_OSI_TimerCreate(LSAPI_OSI_ThreadCurrent(), backLightCb, NULL);
  276. if(NULL == pBackLightTimer_t) MSG_WARN(1,"backlight timer init failed");
  277. if(NULL == pVibTimer_t) pVibTimer_t = LSAPI_OSI_TimerCreate(LSAPI_OSI_ThreadCurrent(), VibCb, NULL);
  278. if(NULL == pVibTimer_t) MSG_WARN(1,"Vib timer init failed");
  279. // if(NULL == BleTimer_t) BleTimer_t = LSAPI_OSI_TimerCreate(LSAPI_OSI_ThreadCurrent(), bleCb, NULL);
  280. // if(NULL == BleTimer_t) MSG_WARN(1,"ble timer init failed");
  281. startKeyTimer();
  282. gpioConfigOutput();
  283. uioStatusInit();
  284. for(;;){
  285. LSAPI_OSI_EventWait(LSAPI_OSI_ThreadCurrent(), &event);
  286. param1=event.param1;
  287. param2=event.param2;
  288. param3=event.param3;
  289. if(event.id==OHPOC_EVENT_KEY_NOR) keyHandler((unsigned short)param1,(unsigned char)param2);
  290. else if(event.id==OHPOC_EVENT_ENCODE) encodeHandler((unsigned short)param1);
  291. }
  292. }
  293. LSAPI_OSI_Thread_t *gGpioTd=NULL;
  294. static void gpiosInit(void){
  295. gGpioTd=LSAPI_OSI_ThreadCreate("gpio", gpioTask, NULL, LSAPI_OSI_PRIORITY_NORMAL, 1024*32, 5);
  296. if(NULL==gGpioTd) MSG_WARN(1,"gpioThread Error");
  297. else MSG_WARN(1,"gpioThread ok");
  298. }
  299. void keySendEvent(unsigned short key, unsigned char keyType){
  300. threadSendEvent(gGpioTd,OHPOC_EVENT_KEY_NOR, key,keyType,NULL);
  301. tryWakeupApp();
  302. }
  303. void encodeSendEvent(unsigned short encode){
  304. threadSendEvent(gGpioTd,OHPOC_EVENT_ENCODE, encode,NULL,NULL);
  305. tryWakeupApp();
  306. }
  307. //////////////////按键实始化//////////////////////////
  308. #define POWER_KEY_PRESSED_TIMEOUT (3000)
  309. typedef struct ls_key{
  310. bool ready_shutdown;
  311. LSAPI_OSI_Timer_t *key_timer;
  312. }lsKey_t;
  313. static void _lsKeyTimer(void *p){
  314. lsKey_t *key = (lsKey_t *)p;
  315. key->ready_shutdown = true;
  316. }
  317. static void _lsKeyCB(lskeyMap_t id, lskeyState_t evt, void *p){
  318. lsKey_t *key = (lsKey_t *)p;
  319. uint8_t status;
  320. if (evt & LS_KEY_STATE_PRESS) keyStatus |= (1<<id);
  321. if (evt & LS_KEY_STATE_RELEASE) keyStatus &= ~(1<<id);
  322. startKeyTimer();
  323. }
  324. void keyPadInit(void){
  325. lsKey_t *key = (lsKey_t *)LSAPI_OSI_Malloc(sizeof(lsKey_t));
  326. if (key == NULL){
  327. MSG_WARN(1,"ls_keypad:calloc faild");
  328. return;
  329. }
  330. key->key_timer = LSAPI_OSI_TimerCreate(NULL, _lsKeyTimer, key);
  331. if (key->key_timer == NULL){
  332. LSAPI_OSI_Free(key);
  333. return;
  334. }
  335. LSAPI_Device_KeypadCreate(_lsKeyCB, key);
  336. }
  337. static LSAPI_Device_t *vibrator = NULL;
  338. static void ls_vibrator_Init(void){
  339. LSAPI_VibrConfig_t cfg = {
  340. .type = LS_VIBR_PMIC,
  341. };
  342. vibrator = LSAPI_Device_VibrCreate(&cfg);
  343. if (!vibrator) {
  344. LSAPI_Log_Debug("ls_vibrator_demo: create vibrator faild!\n");
  345. }else
  346. {
  347. LSAPI_Log_Debug("ls_vibrator_demo: start test vibrator!!!\n");
  348. }
  349. }
  350. void VibCtrl(bool statue){
  351. if(statue)LSAPI_Device_Open(vibrator);
  352. else LSAPI_Device_Close(vibrator);
  353. }
  354. void VibStart(char time){
  355. VibCtrl(1);
  356. LSAPI_OSI_TimerStart(pVibTimer_t,time);
  357. }
  358. //////////////////////////PA控制接口(外放使用免提,耳机FM)///////////////////////////
  359. //8735
  360. //#define PA_MODE_AB
  361. //#define PA_MODE_D
  362. //#define PA_MODE_FM
  363. //#define PA_MODE_K
  364. #define PA_MODE_MIANTI
  365. void delayUs(int64_t time){
  366. int64_t i;
  367. i=LSAPI_OSI_UpTimeUs()+time;
  368. while(LSAPI_OSI_UpTimeUs()<i);
  369. }
  370. void paControl(bool status){
  371. char i=0;
  372. char PACNT=sutApp.pcant;
  373. if(status==true){
  374. MSG_INFO(1, "PACNT:%d",PACNT);
  375. for(i=0;i<PACNT;i++){
  376. spkCtl(true);
  377. delayUs(2);
  378. spkCtl(false);
  379. delayUs(2);
  380. }
  381. spkCtl(true);
  382. }else spkCtl(false);
  383. }
  384. ///////////////////////PA控制结束/////////////////////////////////
  385. /////////////////////PWM//////////////////////////
  386. void pttBeepStart(bool isMicReq){
  387. // if(sutApp.volLev<=0) return;//如果音量调到最小,对讲不产生tone音,当然后对讲播放也没有声音了
  388. // if(true==isMicReq) pwmBeepStart(500, 50);
  389. // else pwmBeepStart(500, 50);
  390. }
  391. void beepStart(int freq){
  392. // //const unsigned char duty[5]={100,99,98,97,96};
  393. // //const unsigned char duty[10]={10,9,8,7,6,5,4,3,2,1};
  394. // int index=getDutyBySpk();
  395. // if(index==0) return;//最小声时直接不需要播放了
  396. // pwmBeepStart(freq, index);
  397. }
  398. void beepStop(void){
  399. // nwy_pwm_stop(pwm_p);
  400. // MSG_INFO(1, "beep stop");
  401. }
  402. //////////////////对外输出板级初始化接口
  403. void boardInit(void){
  404. if(logPortInit()==true) MSG_INFO(1, "Log ok");
  405. ls_vibrator_Init();
  406. gpiosInit();
  407. CreateSerialAtThead();//铏氭嫙涓插彛
  408. LSAPI_OSI_ThreadSleep(200);
  409. CreateUART1_Thread();
  410. lcdsInit();
  411. keyPadInit();
  412. start_ringClearList();
  413. }