board(4728).c 9.2 KB

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