board.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. bool pkeyTimerCtl=false;
  92. unsigned short keyStatus=0;
  93. unsigned short getKeyValue(){return keyStatus;}
  94. //规避龙尚驱动矩阵不足问题
  95. void clearKeyValue(){
  96. keyStatus=0;
  97. if(keyStatus | MKEY_VALUE_PTT){//规避组合键出现PTT一直按下状态问题
  98. if(sutApp.pttReq){
  99. sutApp.pttReq=0;
  100. msgAtSend("AT+POC=0C0000\r\n");
  101. }
  102. }
  103. }
  104. static void startKeyTimer(void){
  105. if(NULL==pKeytimer_t) return;
  106. if(pkeyTimerCtl==true) return;
  107. pkeyTimerCtl=true;
  108. LSAPI_OSI_TimerStart(pKeytimer_t,KEY_TIMER_TICK);
  109. }
  110. void stopKeyTimer(void){
  111. pkeyTimerCtl=false;
  112. LSAPI_OSI_TimerStop(pKeytimer_t);
  113. }
  114. static unsigned int msTick=0;//用于做应用层的非休眠时定时器
  115. unsigned int getAppTick(void){return msTick;}
  116. static void Timeroutcallback(void *param){
  117. //毿00秒去检测键值状怿去抖势
  118. //检测按锿
  119. keyCheck(keyStatus);
  120. msTick ++;
  121. //EarDlyCheck();
  122. if(pkeyTimerCtl==true) LSAPI_OSI_TimerStart(pKeytimer_t,KEY_TIMER_TICK);
  123. }
  124. LSAPI_Device_t *SpkGpio;
  125. LSAPI_Device_t *PwrHoldGpio;
  126. static LSAPI_Device_t * RedGpio = NULL;
  127. static LSAPI_Device_t * GreenGpio = NULL;
  128. //#define RED_GPIO_ID 23
  129. //#define GREEN_GPIO_ID 18
  130. #define SPK_GPIO_ID 19
  131. #define PWR_HOLD_ID 22
  132. void gpioConfigOutput(void){
  133. bool write_value = false;
  134. LSAPI_GpioConfig_t gpioConfig = {0};
  135. // config property
  136. gpioConfig.mode = LS_GPIO_OUTPUT;
  137. gpioConfig.out_level=true;
  138. // create instance
  139. RedGpio=LSAPI_Device_LedCreate(LS_LED_2);
  140. GreenGpio=LSAPI_Device_LedCreate(LS_LED_1);
  141. if(LSAPI_Device_Open(RedGpio)==false){
  142. MSG_INFO(1,"RedGpio---fail");
  143. }
  144. if(LSAPI_Device_Open(GreenGpio)==false){
  145. MSG_INFO(1,"GreenGpio----fail");
  146. }
  147. gpioConfig.id = SPK_GPIO_ID;
  148. SpkGpio = LSAPI_Device_GPIOCreate(&gpioConfig);
  149. gpioConfig.id = PWR_HOLD_ID;
  150. PwrHoldGpio = LSAPI_Device_GPIOCreate(&gpioConfig);
  151. LSAPI_Device_Open(SpkGpio);
  152. LSAPI_Device_Write(SpkGpio, (void *)&write_value, 1);
  153. LSAPI_Device_Open(PwrHoldGpio);
  154. write_value=true;//启动成功后,默认把电源给锁稳
  155. LSAPI_Device_Write(PwrHoldGpio, (void *)&write_value, 1);
  156. gpioBleConfigOutput();
  157. gpioConfigInput();
  158. }
  159. #define ENCODE1_GPIO_ID 18
  160. #define ENCODE2_GPIO_ID 23
  161. static LSAPI_Device_t * Encode1Ctl = NULL;
  162. static LSAPI_Device_t * Encode2Ctl = NULL;
  163. unsigned char g_iEncode=ENCODE_IDLE;
  164. unsigned char g_iStatus=0;
  165. unsigned char g_iLastStatus=0;
  166. void encodeReleased(void){
  167. g_iEncode=ENCODE_IDLE;
  168. }
  169. LSAPI_GpioIntrCB_t inputCB_Encode(void *ctx){
  170. bool encode1,encode2;
  171. bool encodeValid1=false,encodeValid2=false;
  172. if(NULL != Encode1Ctl){
  173. if (1 == LSAPI_Device_Read(Encode1Ctl, (void *)&encode1, 1)) encodeValid1=true;
  174. }
  175. if(NULL != Encode2Ctl){
  176. if (1 == LSAPI_Device_Read(Encode2Ctl, (void *)&encode2, 1)) encodeValid2=true;
  177. }
  178. if(encodeValid1==true){
  179. if(encode1==true) g_iStatus |= (1<<0);
  180. else g_iStatus &= ~(1<<0);
  181. }
  182. if(encodeValid2==true){
  183. if(encode2==true) g_iStatus |= (1<<1);
  184. else g_iStatus &= ~(1<<1);
  185. }
  186. MSG_INFO(0, "g_iStatus======%d",g_iStatus);
  187. if(encodeValid1==true || encodeValid2==true){
  188. if(g_iEncode==ENCODE_IDLE){
  189. switch(g_iStatus){
  190. case 0:
  191. if(g_iLastStatus==1) g_iEncode=ENCODE_NI;
  192. else if(g_iLastStatus==2) g_iEncode=ENCODE_SHUN;
  193. else if(g_iLastStatus==0) g_iEncode=ENCODE_SHUN;
  194. break;
  195. case 3:
  196. if(g_iLastStatus==1) g_iEncode=ENCODE_SHUN;
  197. else if(g_iLastStatus==2) g_iEncode=ENCODE_NI;
  198. else if(g_iLastStatus==3) g_iEncode=ENCODE_NI;
  199. break;
  200. }
  201. if(g_iEncode) encodeSendEvent(g_iEncode);
  202. }
  203. MSG_INFO(0, "\r\n",g_iEncode);//这个不要去掉,否则按一个方向,偶尔还是会反向一下
  204. g_iLastStatus=g_iStatus;
  205. }
  206. }
  207. void gpioConfigInput(){
  208. LSAPI_GpioConfig_t gpioConfig = {0};
  209. gpioConfig.mode = LS_GPIO_INPUT;
  210. gpioConfig.intr_enabled=true;
  211. gpioConfig.intr_level=false;
  212. gpioConfig.rising=true;
  213. gpioConfig.falling=true;
  214. gpioConfig.cb_ctx = NULL;
  215. gpioConfig.id = ENCODE1_GPIO_ID;
  216. gpioConfig.cb=inputCB_Encode;
  217. Encode1Ctl = LSAPI_Device_GPIOCreate(&gpioConfig);
  218. LSAPI_Device_Open(Encode1Ctl);
  219. gpioConfig.id = ENCODE2_GPIO_ID;
  220. gpioConfig.cb=inputCB_Encode;
  221. Encode2Ctl = LSAPI_Device_GPIOCreate(&gpioConfig);
  222. LSAPI_Device_Open(Encode2Ctl);
  223. }
  224. void pwrHoldCtl(bool onoff){
  225. bool write_value = onoff;
  226. LSAPI_Device_Write(PwrHoldGpio, (void *)&write_value, 1);
  227. }
  228. void redLedCtl(bool onoff){
  229. bool write_value = onoff;
  230. LSAPI_Device_Write(RedGpio, (void *)&write_value, 1);
  231. }
  232. void greenLedCtl(bool onoff){
  233. bool write_value = onoff;
  234. LSAPI_Device_Write(GreenGpio, (void *)&write_value, 1);
  235. }
  236. void spkCtl(bool onoff){
  237. bool write_value = onoff;
  238. LSAPI_Device_Write(SpkGpio, (void *)&write_value, 1);
  239. }
  240. //void lcdBackList(char onoff){
  241. // bool write_value = onoff;
  242. // //LSAPI_Device_Write(LcdBlGpio, (void *)&write_value, 1);
  243. // LSAPI_PmuSwitchPower(OSI_MAKE_TAG('C', 'A', 'M', 'D '), onoff, false);
  244. //}
  245. void startBackLightTimer(int time){
  246. if(NULL==pBackLightTimer_t) return;
  247. LSAPI_OSI_TimerStart(pBackLightTimer_t,time);
  248. }
  249. static void gpioTask(void *param){
  250. unsigned int param1,param2,param3;
  251. LSAPI_OSI_Event_t event = {};
  252. //创建一个定时器
  253. if(NULL == pKeytimer_t) pKeytimer_t = LSAPI_OSI_TimerCreate(LSAPI_OSI_ThreadCurrent(), Timeroutcallback, NULL);
  254. if(NULL == pKeytimer_t) MSG_WARN(1,"gpio timer init failed");
  255. if(NULL == pBackLightTimer_t) pBackLightTimer_t = LSAPI_OSI_TimerCreate(LSAPI_OSI_ThreadCurrent(), backLightCb, NULL);
  256. if(NULL == pBackLightTimer_t) MSG_WARN(1,"backlight timer init failed");
  257. // if(NULL == BleTimer_t) BleTimer_t = LSAPI_OSI_TimerCreate(LSAPI_OSI_ThreadCurrent(), bleCb, NULL);
  258. // if(NULL == BleTimer_t) MSG_WARN(1,"ble timer init failed");
  259. startKeyTimer();
  260. gpioConfigOutput();
  261. uioStatusInit();
  262. for(;;){
  263. LSAPI_OSI_EventWait(LSAPI_OSI_ThreadCurrent(), &event);
  264. param1=event.param1;
  265. param2=event.param2;
  266. param3=event.param3;
  267. if(event.id==OHPOC_EVENT_KEY_NOR) keyHandler((unsigned short)param1,(unsigned char)param2);
  268. else if(event.id==OHPOC_EVENT_ENCODE) encodeHandler((unsigned short)param1);
  269. }
  270. }
  271. LSAPI_OSI_Thread_t *gGpioTd=NULL;
  272. static void gpiosInit(void){
  273. gGpioTd=LSAPI_OSI_ThreadCreate("gpio", gpioTask, NULL, LSAPI_OSI_PRIORITY_NORMAL, 1024*32, 5);
  274. if(NULL==gGpioTd) MSG_WARN(1,"gpioThread Error");
  275. else MSG_WARN(1,"gpioThread ok");
  276. }
  277. void keySendEvent(unsigned short key, unsigned char keyType){
  278. threadSendEvent(gGpioTd,OHPOC_EVENT_KEY_NOR, key,keyType,NULL);
  279. tryWakeupApp();
  280. }
  281. void encodeSendEvent(unsigned short encode){
  282. threadSendEvent(gGpioTd,OHPOC_EVENT_ENCODE, encode,NULL,NULL);
  283. tryWakeupApp();
  284. }
  285. //////////////////按键实始化//////////////////////////
  286. #define POWER_KEY_PRESSED_TIMEOUT (3000)
  287. typedef struct ls_key{
  288. bool ready_shutdown;
  289. LSAPI_OSI_Timer_t *key_timer;
  290. }lsKey_t;
  291. static void _lsKeyTimer(void *p){
  292. lsKey_t *key = (lsKey_t *)p;
  293. key->ready_shutdown = true;
  294. }
  295. static void _lsKeyCB(lskeyMap_t id, lskeyState_t evt, void *p){
  296. lsKey_t *key = (lsKey_t *)p;
  297. uint8_t status;
  298. if (evt & LS_KEY_STATE_PRESS) keyStatus |= (1<<id);
  299. if (evt & LS_KEY_STATE_RELEASE) keyStatus &= ~(1<<id);
  300. startKeyTimer();
  301. }
  302. void keyPadInit(void){
  303. lsKey_t *key = (lsKey_t *)LSAPI_OSI_Malloc(sizeof(lsKey_t));
  304. if (key == NULL){
  305. MSG_WARN(1,"ls_keypad:calloc faild");
  306. return;
  307. }
  308. key->key_timer = LSAPI_OSI_TimerCreate(NULL, _lsKeyTimer, key);
  309. if (key->key_timer == NULL){
  310. LSAPI_OSI_Free(key);
  311. return;
  312. }
  313. LSAPI_Device_KeypadCreate(_lsKeyCB, key);
  314. }
  315. static LSAPI_Device_t *vibrator = NULL;
  316. static void ls_vibrator_Init(void){
  317. LSAPI_VibrConfig_t cfg = {
  318. .type = LS_VIBR_PMIC,
  319. };
  320. vibrator = LSAPI_Device_VibrCreate(&cfg);
  321. if (!vibrator) {
  322. LSAPI_Log_Debug("ls_vibrator_demo: create vibrator faild!\n");
  323. } else
  324. {
  325. LSAPI_Log_Debug("ls_vibrator_demo: start test vibrator!!!\n");
  326. }
  327. }
  328. void VibCtrl(bool statue){
  329. if(statue)LSAPI_Device_Open(vibrator);
  330. else LSAPI_Device_Close(vibrator);
  331. }
  332. //////////////////////////PA控制接口(外放使用免提,耳机FM)///////////////////////////
  333. //8735
  334. //#define PA_MODE_AB
  335. //#define PA_MODE_D
  336. //#define PA_MODE_FM
  337. //#define PA_MODE_K
  338. #define PA_MODE_MIANTI
  339. void delayUs(int64_t time){
  340. int64_t i;
  341. i=LSAPI_OSI_UpTimeUs()+time;
  342. while(LSAPI_OSI_UpTimeUs()<i);
  343. }
  344. void paControl(bool status){
  345. char i=0;
  346. char PACNT=sutApp.pcant;
  347. if(status==true){
  348. MSG_INFO(1, "PACNT:%d",PACNT);
  349. for(i=0;i<PACNT;i++){
  350. spkCtl(true);
  351. delayUs(2);
  352. spkCtl(false);
  353. delayUs(2);
  354. }
  355. spkCtl(true);
  356. }else spkCtl(false);
  357. }
  358. ///////////////////////PA控制结束/////////////////////////////////
  359. /////////////////////PWM//////////////////////////
  360. void pttBeepStart(bool isMicReq){
  361. // if(sutApp.volLev<=0) return;//如果音量调到最小,对讲不产生tone音,当然后对讲播放也没有声音了
  362. // if(true==isMicReq) pwmBeepStart(500, 50);
  363. // else pwmBeepStart(500, 50);
  364. }
  365. void beepStart(int freq){
  366. // //const unsigned char duty[5]={100,99,98,97,96};
  367. // //const unsigned char duty[10]={10,9,8,7,6,5,4,3,2,1};
  368. // int index=getDutyBySpk();
  369. // if(index==0) return;//最小声时直接不需要播放了
  370. // pwmBeepStart(freq, index);
  371. }
  372. void beepStop(void){
  373. // nwy_pwm_stop(pwm_p);
  374. // MSG_INFO(1, "beep stop");
  375. }
  376. //////////////////对外输出板级初始化接口
  377. void boardInit(void){
  378. if(logPortInit()==true) MSG_INFO(1, "Log ok");
  379. gpiosInit();
  380. CreateSerialAtThead();//铏氭嫙涓插彛
  381. CreateUART1_Thread();
  382. lcdsInit();
  383. keyPadInit();
  384. }