board(185).c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. #include "includes.h"
  2. //////////////////////封装一下AT接口/////////////////////////////////
  3. void msgAtSend(char *at){
  4. }
  5. void msgAtSends(char *at ,int len){
  6. }
  7. //////日志打印口配置/////////////////
  8. void my_usb_evt_cb(lsUSBEvent_t event, unsigned long p){
  9. LSAPI_OSI_Event_t usb_event;
  10. usb_event.id = OHPOC_EVENT_USB;
  11. usb_event.param1=event;
  12. //wlog_info("my_usb_evt_cb:%d",event);
  13. osiEventTrySend((LSAPI_OSI_Thread_t *)p, &usb_event, 0);
  14. }
  15. LSAPI_Device_USBConfig_t usb_cfg = {
  16. .name = LSAPI_DEV_USB_COM7,
  17. .event_cb = my_usb_evt_cb,
  18. };
  19. bool usbThreadExit=false;
  20. LSAPI_Device_t *usbdev=NULL;
  21. LSAPI_OSI_Thread_t *gUsbWRTd;
  22. static void usbPortHandler(void *param){
  23. int ret;
  24. unsigned char buffer[100];
  25. LSAPI_OSI_Event_t waitevent;
  26. LSAPI_Log_Debug("usb create ok");
  27. while(usbThreadExit==false){
  28. LSAPI_OSI_EventWait(LSAPI_OSI_ThreadCurrent(), &waitevent);
  29. if(waitevent.param1==LS_USB_EVENT_RX_ARRIVED){
  30. while(LSAPI_Device_ReadAvail(usbdev)>0){
  31. ret = LSAPI_Device_Read(usbdev, buffer, sizeof(buffer));
  32. if(ret< sizeof(buffer)) buffer[ret]=0;
  33. usbCmdHandler(buffer,ret);
  34. }
  35. //WakeupNow("usbCallBack");
  36. }
  37. }
  38. LSAPI_Log_Debug("usbPortHandler exit");
  39. LSAPI_Device_Close(usbdev);
  40. gUsbWRTd=NULL;
  41. LSAPI_OSI_ThreadExit();
  42. }
  43. static bool logPortInit(void){
  44. gUsbWRTd=LSAPI_OSI_ThreadCreate("usb", usbPortHandler, NULL, LSAPI_OSI_PRIORITY_NORMAL, 1024*32, 4);
  45. if(gUsbWRTd==NULL) return false;
  46. usb_cfg.event_cb_ctx = gUsbWRTd;
  47. usbdev = LSAPI_Device_USBCreateEx(&usb_cfg);
  48. if(usbdev==NULL){
  49. usbThreadExit=true;
  50. LSAPI_Log_Debug("usbex create err");
  51. return false;
  52. }
  53. if(false==LSAPI_Device_Open(usbdev)){
  54. LSAPI_Log_Debug("usb LSAPI_Device_Open failed");
  55. return false;
  56. }
  57. LSAPI_Device_SetAutoSleep(usbdev, 500);
  58. LSAPI_Log_Debug("usbComInit done");
  59. return true;
  60. }
  61. void usbOutPut(unsigned char *info, int len){
  62. if(NULL==usbdev) return;
  63. if(LSAPI_Device_WriteAvail(usbdev) != -1) LSAPI_Device_Write(usbdev, info,len);
  64. }
  65. void usbOutPutStr(unsigned char *info){
  66. if(NULL==usbdev) return;
  67. if(LSAPI_Device_WriteAvail(usbdev) != -1) LSAPI_Device_Write(usbdev, info,strlen(info));
  68. }
  69. static void lcdInitTask(void *param){
  70. //MSG_WARN(1,"lcd init thread start");
  71. if(0==lcdDrv_Init(0)) MSG_INFO(1, "lcd init ok");
  72. else MSG_INFO(1, "lcd init failed");
  73. guiInit();
  74. MSG_WARN(1,"lcd init thread end");
  75. sutApp.guiStatus=1;
  76. LSAPI_OSI_ThreadExit();
  77. }
  78. static void lcdsInit(void){
  79. if(NULL==LSAPI_OSI_ThreadCreate("lcdinit", lcdInitTask, NULL, LSAPI_OSI_PRIORITY_NORMAL, LCD_INIT_TASK_THREAD_STACK, 2)){
  80. MSG_WARN(1,"lcd init thread error");
  81. return;
  82. }
  83. }
  84. //////////////////gpio及定时器/////////////////////////////
  85. LSAPI_OSI_Timer_t *pKeytimer_t=NULL;
  86. LSAPI_OSI_Timer_t *pBackLightTimer_t=NULL;
  87. bool pkeyTimerCtl=false;
  88. unsigned short keyStatus=0;
  89. unsigned short getKeyValue(){return keyStatus;}
  90. //规避龙尚驱动矩阵不足问题
  91. void clearKeyValue(){
  92. keyStatus=0;
  93. if(keyStatus | MKEY_VALUE_PTT){//规避组合键出现PTT一直按下状态问题
  94. if(sutApp.pttReq){
  95. sutApp.pttReq=0;
  96. msgAtSend("AT+POC=0C0000\r\n");
  97. }
  98. }
  99. }
  100. static void startKeyTimer(void){
  101. if(NULL==pKeytimer_t) return;
  102. if(pkeyTimerCtl==true) return;
  103. pkeyTimerCtl=true;
  104. LSAPI_OSI_TimerStart(pKeytimer_t,KEY_TIMER_TICK);
  105. }
  106. void stopKeyTimer(void){
  107. pkeyTimerCtl=false;
  108. LSAPI_OSI_TimerStop(pKeytimer_t);
  109. }
  110. static unsigned int msTick=0;//用于做应用层的非休眠时定时器
  111. unsigned int getAppTick(void){return msTick;}
  112. static void Timeroutcallback(void *param){
  113. //毿00秒去检测键值状怿去抖势
  114. //检测按锿
  115. keyCheck(keyStatus);
  116. msTick ++;
  117. //EarDlyCheck();
  118. if(pkeyTimerCtl==true) LSAPI_OSI_TimerStart(pKeytimer_t,KEY_TIMER_TICK);
  119. }
  120. LSAPI_Device_t *LcdBlGpio;
  121. LSAPI_Device_t *RedGpio;
  122. LSAPI_Device_t *GreenGpio;
  123. LSAPI_Device_t *SpkGpio;
  124. LSAPI_Device_t *PwrHoldGpio;
  125. #define RED_GPIO_ID 22
  126. #define GREEN_GPIO_ID 18
  127. #define SPK_GPIO_ID 19
  128. //#define PWR_HOLD_ID 19//22
  129. #define LCD_BAK_ID 21
  130. void gpioConfigOutput(void){
  131. bool write_value = false;
  132. LSAPI_GpioConfig_t gpioConfig = {0};
  133. // config property
  134. gpioConfig.mode = LS_GPIO_OUTPUT;
  135. gpioConfig.out_level=true;
  136. // create instance
  137. gpioConfig.id = RED_GPIO_ID;
  138. RedGpio = LSAPI_Device_GPIOCreate(&gpioConfig);
  139. gpioConfig.id = GREEN_GPIO_ID;
  140. GreenGpio = LSAPI_Device_GPIOCreate(&gpioConfig);
  141. gpioConfig.id = SPK_GPIO_ID;
  142. SpkGpio = LSAPI_Device_GPIOCreate(&gpioConfig);
  143. // gpioConfig.id = PWR_HOLD_ID;
  144. // PwrHoldGpio = LSAPI_Device_GPIOCreate(&gpioConfig);
  145. gpioConfig.id = LCD_BAK_ID;
  146. LcdBlGpio = LSAPI_Device_GPIOCreate(&gpioConfig);
  147. LSAPI_Device_Open(RedGpio);
  148. LSAPI_Device_Write(RedGpio, (void *)&write_value, 1);
  149. LSAPI_Device_Open(GreenGpio);
  150. LSAPI_Device_Write(GreenGpio, (void *)&write_value, 1);
  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. LSAPI_Device_Open(LcdBlGpio);
  157. LSAPI_Device_Write(LcdBlGpio, (void *)&write_value, 1);
  158. }
  159. void pwrHoldCtl(bool onoff){
  160. bool write_value = onoff;
  161. LSAPI_Device_Write(RedGpio, (void *)&write_value, 1);
  162. }
  163. void redLedCtl(bool onoff){
  164. bool write_value = onoff;
  165. LSAPI_Device_Write(RedGpio, (void *)&write_value, 1);
  166. }
  167. void greenLedCtl(bool onoff){
  168. bool write_value = onoff;
  169. LSAPI_Device_Write(GreenGpio, (void *)&write_value, 1);
  170. }
  171. void spkCtl(bool onoff){
  172. bool write_value = onoff;
  173. LSAPI_Device_Write(SpkGpio, (void *)&write_value, 1);
  174. }
  175. void lcdBackList(char onoff){
  176. bool write_value = onoff;
  177. LSAPI_Device_Write(LcdBlGpio, (void *)&write_value, 1);
  178. }
  179. void startBackLightTimer(int time){
  180. if(NULL==pBackLightTimer_t) return;
  181. LSAPI_OSI_TimerStart(pBackLightTimer_t,time);
  182. }
  183. static void gpioTask(void *param){
  184. unsigned int param1,param2,param3;
  185. LSAPI_OSI_Event_t event = {};
  186. //创建一个定时器
  187. if(NULL == pKeytimer_t) pKeytimer_t = LSAPI_OSI_TimerCreate(LSAPI_OSI_ThreadCurrent(), Timeroutcallback, NULL);
  188. if(NULL == pKeytimer_t) MSG_WARN(1,"gpio timer init failed");
  189. if(NULL == pBackLightTimer_t) pBackLightTimer_t = LSAPI_OSI_TimerCreate(LSAPI_OSI_ThreadCurrent(), backLightCb, NULL);
  190. if(NULL == pBackLightTimer_t) MSG_WARN(1,"backlight timer init failed");
  191. startKeyTimer();
  192. gpioConfigOutput();
  193. uioStatusInit();
  194. for(;;){
  195. LSAPI_OSI_EventWait(LSAPI_OSI_ThreadCurrent(), &event);
  196. param1=event.param1;
  197. param2=event.param2;
  198. param3=event.param3;
  199. if(event.id==OHPOC_EVENT_KEY_NOR) keyHandler((unsigned short)param1,(unsigned char)param2);
  200. }
  201. }
  202. LSAPI_OSI_Thread_t *gGpioTd=NULL;
  203. static void gpiosInit(void){
  204. gGpioTd=LSAPI_OSI_ThreadCreate("gpio", gpioTask, NULL, LSAPI_OSI_PRIORITY_NORMAL, 1024*32, 5);
  205. if(NULL==gGpioTd) MSG_WARN(1,"gpioThread Error");
  206. else MSG_WARN(1,"gpioThread ok");
  207. }
  208. void keySendEvent(unsigned short key, unsigned char keyType){
  209. threadSendEvent(gGpioTd,OHPOC_EVENT_KEY_NOR, key,keyType,NULL);
  210. tryWakeupApp();
  211. }
  212. //////////////////按键实始化//////////////////////////
  213. #define POWER_KEY_PRESSED_TIMEOUT (3000)
  214. typedef struct ls_key{
  215. bool ready_shutdown;
  216. LSAPI_OSI_Timer_t *key_timer;
  217. }lsKey_t;
  218. static void _lsKeyTimer(void *p){
  219. lsKey_t *key = (lsKey_t *)p;
  220. key->ready_shutdown = true;
  221. }
  222. static void _lsKeyCB(lskeyMap_t id, lskeyState_t evt, void *p){
  223. lsKey_t *key = (lsKey_t *)p;
  224. #if 0
  225. //char info[30];
  226. //snprintf(info, sizeof(info), ">>id:%d,st:%d", id, evt);
  227. //MSG_INFO(1, info);
  228. //newGuiShowStr(10, 10, info,0,1);
  229. return;
  230. #endif
  231. if (evt & LS_KEY_STATE_PRESS) keyStatus |= (1<<id);
  232. if (evt & LS_KEY_STATE_RELEASE) keyStatus &= ~(1<<id);
  233. startKeyTimer();
  234. }
  235. void keyPadInit(void){
  236. lsKey_t *key = (lsKey_t *)LSAPI_OSI_Malloc(sizeof(lsKey_t));
  237. if (key == NULL){
  238. MSG_WARN(1,"ls_keypad:calloc faild");
  239. return;
  240. }
  241. key->key_timer = LSAPI_OSI_TimerCreate(NULL, _lsKeyTimer, key);
  242. if (key->key_timer == NULL){
  243. LSAPI_OSI_Free(key);
  244. return;
  245. }
  246. LSAPI_Device_KeypadCreate(_lsKeyCB, key);
  247. }
  248. //////////////////////////PA控制接口(外放使用免提,耳机FM)///////////////////////////
  249. //8735
  250. //#define PA_MODE_AB
  251. //#define PA_MODE_D
  252. //#define PA_MODE_FM
  253. //#define PA_MODE_K
  254. #define PA_MODE_MIANTI
  255. void delayUs(int64_t time){
  256. int64_t i;
  257. i=LSAPI_OSI_UpTimeUs()+time;
  258. while(LSAPI_OSI_UpTimeUs()<i);
  259. }
  260. void paControl(bool status){
  261. char i=0;
  262. #if 0
  263. #if defined (PA_MODE_AB)
  264. #define PACNT 0
  265. #elif defined (PA_MODE_D)
  266. #define PACNT 1
  267. #elif defined (PA_MODE_FM)
  268. #define PACNT 2
  269. #elif defined (PA_MODE_K)
  270. #define PACNT 3
  271. #elif defined (PA_MODE_MIANTI)
  272. #define PACNT 4
  273. #else
  274. #error "pls select pa control type"
  275. #endif
  276. #endif
  277. char PACNT=sutApp.pcant;
  278. if(status==true){
  279. MSG_INFO(1, "PACNT:%d",PACNT);
  280. for(i=0;i<PACNT;i++){
  281. spkCtl(true);
  282. delayUs(2);
  283. spkCtl(false);
  284. delayUs(2);
  285. }
  286. spkCtl(true);
  287. }else spkCtl(false);
  288. }
  289. ///////////////////////PA控制结束/////////////////////////////////
  290. /////////////////////PWM//////////////////////////
  291. void pttBeepStart(bool isMicReq){
  292. // if(sutApp.volLev<=0) return;//如果音量调到最小,对讲不产生tone音,当然后对讲播放也没有声音了
  293. // if(true==isMicReq) pwmBeepStart(500, 50);
  294. // else pwmBeepStart(500, 50);
  295. }
  296. void beepStart(int freq){
  297. // //const unsigned char duty[5]={100,99,98,97,96};
  298. // //const unsigned char duty[10]={10,9,8,7,6,5,4,3,2,1};
  299. // int index=getDutyBySpk();
  300. // if(index==0) return;//最小声时直接不需要播放了
  301. // pwmBeepStart(freq, index);
  302. }
  303. void beepStop(void){
  304. // nwy_pwm_stop(pwm_p);
  305. // MSG_INFO(1, "beep stop");
  306. }
  307. //////////////////对外输出板级初始化接口
  308. void boardInit(void){
  309. if(logPortInit()==true) MSG_INFO(1, "Log ok");
  310. gpiosInit();
  311. lcdsInit();
  312. keyPadInit();
  313. }