board.c 8.5 KB

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