board.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. #include "includes.h"
  2. bool usb_log_status=false;
  3. static void gpiosInit(void);
  4. static void spiFlashInit(void);
  5. static void lcdsInit(void);
  6. void boardInit(void){
  7. logPortInit();//摰鮋�撌亦��嗆����枏�
  8. gpiosInit();
  9. lcdsInit();
  10. }
  11. //////////////////////撠��銝�銝𧘹T�亙藁/////////////////////////////////
  12. void msgAtSend(char *at){
  13. ohpoc_msg_send(at, strlen(at));
  14. }
  15. void msgAtSends(char *at ,int len){
  16. ohpoc_msg_send(at, len);
  17. }
  18. //////////////////////撠��AT�亙藁蝏𤘪�/////////////////////////////////
  19. //////////////////////�亙�銝脣藁�滨蔭撘�憪?////////////////////////////////
  20. void logDataCb(const char *data,uint32 length){
  21. usbCmdHandler(data,length);
  22. }
  23. bool logPortInit(void){
  24. bool ret;
  25. ret=nwy_usb_serial_reg_recv_cb(logDataCb);
  26. if(false==ret) OSI_LOGI(0,"nwy_usb_serial_reg_recv_cb failed");
  27. else{
  28. usb_log_status=true;
  29. MSG_INFO(1,"logPortInit ok");
  30. }
  31. return ret;
  32. }
  33. //////////////////////�亙��枏㫲蝏𤘪�///////////////////////////////
  34. ///////////////////////GPIOS�嘥��?/////////////////////////////
  35. unsigned short keyStatus=0;//靽嘥��桀�潛𠶖�?
  36. unsigned char hpStatus=0;
  37. const unsigned char keyTable[KS_KEY_MAX]={
  38. G_KEY_MIC_PTT
  39. };
  40. const unsigned char mKeyTable[MKS_KEY_INS]={
  41. G_KEY_IN1,
  42. G_KEY_IN2,
  43. G_KEY_IN3
  44. };
  45. static void startKeyTimer(void);
  46. static void gpioIsqActive(int param){
  47. unsigned short i;
  48. tryWakeupApp();//�㗇��桀𢆡雿頣��日�
  49. startKeyTimer();
  50. //璉�瘚𧢲芦�𡁏��?撠��潸挽蝵株�keyStatus�喳虾嚗�縧�硋𢆡蝑㗇�雿𨅯銁摰𡁏𧒄�其葉摰峕�
  51. for(i=0;i<KS_KEY_MAX;i++){
  52. if(keyTable[i]==param){
  53. if(0==nwy_gpio_get_value(param)) keyStatus |= 1<<i;
  54. else keyStatus &= ~(1<<i);
  55. return;
  56. }
  57. }
  58. }
  59. /*
  60. pid:gpio_pid
  61. up_down:0-down,1-up,2-null
  62. defVal:nwy_low nwy_high
  63. */
  64. void gpioOutPutInit(uint32 pid, char up_down,nwy_value_t defVal){
  65. nwy_gpio_set_direction(pid,nwy_output);//霈曄蔭銝箸䲮�?
  66. nwy_gpio_pullup_or_pulldown(pid,up_down);
  67. nwy_gpio_set_value(pid,defVal);
  68. }
  69. /*
  70. pid:gpio_pid
  71. up_down:0-down,1-up,2-null,3-no_config_up_down
  72. irq_type:0 銝𠰴�撱?1 銝钅�撱?2 銝𠰴�+銝钅�撱?3 擃?4 雿?
  73. */
  74. void gpioInputInit(uint32 pid,char up_down, char irq_type){
  75. nwy_close_gpio(pid);
  76. if(nwy_open_gpio_irq_config(pid,irq_type,gpioIsqActive)) nwy_gpio_open_irq_enable(pid);
  77. if(up_down!=3) nwy_gpio_pullup_or_pulldown(pid,up_down);
  78. }
  79. nwy_osiTimer_t *pKeytimer_t = NULL,*pBackLightTimer_t=NULL;
  80. #ifdef ENABLE_PWM_BEEP
  81. nwy_osiTimer_t *pPwmTimer=NULL;
  82. #endif
  83. bool pkeyTimerCtl=false;
  84. void startBackLightTimer(int time){
  85. if(NULL==pBackLightTimer_t) return;
  86. nwy_start_timer(pBackLightTimer_t,time);
  87. }
  88. static void startKeyTimer(void){
  89. if(NULL==pKeytimer_t) return;
  90. if(pkeyTimerCtl==true) return;
  91. pkeyTimerCtl=false;
  92. nwy_start_timer_periodic(pKeytimer_t,KEY_TIMER_TICK);
  93. }
  94. void stopKeyTimer(void){
  95. pkeyTimerCtl=false;
  96. nwy_stop_timer(pKeytimer_t);
  97. }
  98. static unsigned int msTick=0;//�其��𡁜��典����隡𤑳��嗅��嗅膥
  99. unsigned int getAppTick(void){return msTick;}
  100. static void Timeroutcallback(void *param){
  101. //瘥?00蝘鍦縧璉�瘚钅睸�潛𠶖�?�餅��?
  102. //璉�瘚𧢲��?
  103. keyCheck(keyStatus);
  104. //璉�瘚讠畆�菜��?
  105. mKeyCheck();
  106. msTick ++;
  107. }
  108. static void pwmTimerCb(void *param);
  109. static void setPin39OutIsr(void);
  110. static void gpioTask(void *param){
  111. unsigned short i;
  112. nwy_osiEvent_t event = {};
  113. unsigned int param1,param2,param3;
  114. MSG_WARN(1,"gpioThread start");
  115. //�𥕦遣銝�銝芸��嗅膥
  116. if(NULL == pKeytimer_t) pKeytimer_t = nwy_timer_init(nwy_get_current_thread(), Timeroutcallback, NULL);
  117. if(NULL == pKeytimer_t) MSG_WARN(1,"gpio timer init failed");
  118. if(NULL == pBackLightTimer_t) pBackLightTimer_t = nwy_timer_init(nwy_get_current_thread(), backLightCb, NULL);
  119. if(NULL == pBackLightTimer_t) MSG_WARN(1,"backlight timer init failed");
  120. #ifdef ENABLE_PWM_BEEP
  121. if(NULL == pPwmTimer) pPwmTimer = nwy_timer_init(nwy_get_current_thread(), pwmTimerCb, NULL);
  122. if(NULL == pPwmTimer) MSG_WARN(1,"pPwmTimer timer init failed");
  123. #endif
  124. startKeyTimer();//100ms銝�甈?
  125. //�嘥��𨰦PIO蝖砌辣IO銝箄��?
  126. gpioOutPutInit(G_SD_PWR, 1, nwy_low);
  127. gpioOutPutInit(G_KEY_OUT1, 1, nwy_low);
  128. gpioOutPutInit(G_KEY_OUT2, 1, nwy_low);
  129. gpioOutPutInit(G_KEY_OUT3, 1, nwy_low);
  130. gpioOutPutInit(G_LED1, 1, nwy_high);
  131. gpioOutPutInit(G_LED2, 1, nwy_low);
  132. gpioOutPutInit(G_POWER_HOLD, 1, nwy_high);//撘��箏��喲�雿讐㩞皞?銝漤�閬���橘�閬���箸𧒄嚗����誘頧臭辣�單㦤�喳虾
  133. gpioOutPutInit(G_LCD_BL, 1, nwy_low);
  134. gpioOutPutInit(G_SPK_EN, 1, nwy_low);
  135. gpioOutPutInit(G_LAN_EN, 1, nwy_low);
  136. //�嘥��𨰦PIO蝖砌辣IO銝箄��?
  137. gpioInputInit(G_KEY_MIC_PTT,1,2);//ISR 銝𠹺�撱塚� ���銝𦠜�
  138. gpioInputInit(G_KEY_IN1,1,1);//銝𦠜�颲枏�嚗䔶��滢葉�?
  139. gpioInputInit(G_KEY_IN2,1,1);
  140. gpioInputInit(G_KEY_IN3,1,1);
  141. //�嘥��𣇉畆�菜��桃𠶖�?
  142. keyStatusCtlAll(0);
  143. for(;;){
  144. nwy_wait_thead_event(nwy_get_current_thread(), &event,0);
  145. param1=event.param1;
  146. param2=event.param2;
  147. param3=event.param3;
  148. if(event.id==OHPOC_EVENT_KEY_NOR) keyHandler((unsigned short)param1,(unsigned char)param2);
  149. else if(event.id==OHPOC_EVENT_KEY_MAT) mKeyHandler((unsigned char)param1);
  150. }
  151. }
  152. static void gpiosInit(void){
  153. if(NULL==nwy_create_thread("gpio", gpioTask, NULL, NWY_OSI_PRIORITY_NORMAL, GPIO_TASK_THREAD_STACK, 5)) MSG_WARN(1,"gpioThread Error");
  154. else MSG_WARN(1,"gpioThread ok");
  155. }
  156. ///////////////////////GPIOS蝏𤘪�////////////////////////////////
  157. ////////////////////////SPIFLASH�亙藁////////////////////////////
  158. nwyspiflash_t *flashFp=NULL;
  159. static void spiFlashInit(void){
  160. nwyspiflash_t *fp;
  161. fp=nwy_flash_open(NAME_FLASH_1);
  162. if(NULL==fp){
  163. MSG_WARN(1,"nwy_flash_open failed");
  164. return;
  165. }
  166. flashFp=fp;
  167. //�唳旿霂餃��硋�憪见��滢�
  168. }
  169. ////////////////////////SPIFLASH蝏𤘪�////////////////////////////
  170. ////////////////////////AUTOPWR�亙藁/////////////////////////////
  171. void autoPwrSms(NWY_SVR_MSG_SERVICE_E msg, uint32_t param)
  172. {
  173. MSG_WARN(1,"SrvMsg:%d,%d",msg,param);
  174. switch(msg){
  175. case NWY_CAP_IND:MSG_WARN(1,"NWY_CAP_IND");break;
  176. case NWY_CHARGE_START_IND:MSG_WARN(1,"NWY_CHARGE_START_IND");break;
  177. case NWY_CHARGE_END_IND:MSG_WARN(1,"NWY_CHARGE_END_IND");break;
  178. case NWY_WARNING_IND:MSG_WARN(1,"NWY_WARNING_IND");break;
  179. case NWY_SHUTDOWN_IND:MSG_WARN(1,"NWY_SHUTDOWN_IND");break;
  180. case NWY_CHARGE_FINISH:MSG_WARN(1,"NWY_CHARGE_FINISH");break;
  181. case NWY_CHARGE_DISCONNECT:MSG_WARN(1,"NWY_CHARGE_DISCONNECT");break;
  182. case NWY_CHARGE_FAULT:MSG_WARN(1,"NWY_CHARGE_FAULT");break;
  183. default:break;
  184. }
  185. }
  186. static void autoPwrInit(void){
  187. nwy_set_auto_poweroff(3500,3400,20,autoPwrSms);
  188. }
  189. /////////////////////////AUTOPWR蝏𤘪�////////////////////////////
  190. /////////////////////////LCD�亙藁�嘥��?/////////////////////////
  191. #include "lcdDrv.h"
  192. static void lcdInitTask(void *param){
  193. MSG_WARN(1,"lcd init thread start");
  194. lcdDrv_Init(0);
  195. guiInit();
  196. guiTest();
  197. MSG_WARN(1,"lcd init thread end");
  198. sutApp.guiStatus=1;
  199. nwy_exit_thread();
  200. }
  201. static void lcdsInit(void){
  202. if(NULL==nwy_create_thread("lcdinit", lcdInitTask, NULL, NWY_OSI_PRIORITY_NORMAL, LCD_INIT_TASK_THREAD_STACK, 2)){
  203. MSG_WARN(1,"lcd init thread error");
  204. return;
  205. }
  206. }
  207. //////////////////////////LCD�亙藁蝏𤘪�///////////////////////////
  208. //////////////////////////PA�批��亙藁(憭𡝗𦆮雿輻鍂�齿�嚗諹�單㦤FM)///////////////////////////
  209. //8735
  210. //#define PA_MODE_AB
  211. //#define PA_MODE_D
  212. //#define PA_MODE_FM
  213. //#define PA_MODE_K
  214. //#define PA_MODE_MIANTI
  215. void delayUs(int64_t time){
  216. int64_t i;
  217. i=nwy_get_up_time_us()+time;
  218. while(nwy_get_up_time_us()<i);
  219. }
  220. void paControl(bool status){
  221. char i=0;
  222. #if 0
  223. #if defined (PA_MODE_AB)
  224. #define PACNT 0
  225. #elif defined (PA_MODE_D)
  226. #define PACNT 1
  227. #elif defined (PA_MODE_FM)
  228. #define PACNT 2
  229. #elif defined (PA_MODE_K)
  230. #define PACNT 3
  231. #elif defined (PA_MODE_MIANTI)
  232. #define PACNT 4
  233. #else
  234. #error "pls select pa control type"
  235. #endif
  236. #endif
  237. char PACNT=sutApp.pcant;
  238. if(status==true){
  239. MSG_INFO(1, "PACNT:%d",PACNT);
  240. for(i=0;i<PACNT;i++){
  241. CTL_SPK_EN(1);
  242. delayUs(2);
  243. CTL_SPK_EN(0);
  244. delayUs(2);
  245. }
  246. CTL_SPK_EN(1);
  247. }else CTL_SPK_EN(0);
  248. }
  249. ///////////////////////PA�批�蝏𤘪�/////////////////////////////////
  250. static void setPin39OutIsr(void){
  251. nwy_gpio_set_direction(G_PWR_DET,nwy_output);//餈嗘葵銝滚�隡𡁏瓷銝剜鱏嚗����
  252. nwy_gpio_pullup_or_pulldown(G_PWR_DET,1);
  253. //gpioInputInit(G_PWR_DET,1,2);//銝𦠜�嚗䔶���葉�踹��������漤�銝剜鱏鈭�)
  254. }
  255. #ifdef ENABLE_PWM_BEEP
  256. ///////////////////////PWM�批�撘�憪�////////////////////////////////
  257. static nwy_pwm_t* pwm_p=NULL;
  258. void pwmDeInitToSleep(void){
  259. nwy_pwm_deinit(pwm_p);//靚�鍂�穥CD隡𡁜枂�啁蒾撅讐緵鞊�
  260. }
  261. static void pwmTimerCb(void *param);
  262. void pwmBeepStart(int freq, int duty){
  263. sutPocStatus.beep=1;
  264. pwm_p=nwy_pwm_init(NAME_PWM_1, freq, duty);//freq,duty
  265. if(NULL==pwm_p){
  266. MSG_INFO(1, "PWM init failed");
  267. sutPocStatus.beep=0;
  268. return;
  269. }
  270. paControl(true);
  271. nwy_pwm_start(pwm_p);
  272. MSG_INFO(1, "PWM start");
  273. if(pPwmTimer!=NULL) nwy_start_timer(pPwmTimer, 100);
  274. }
  275. int getDutyBySpk(void){
  276. unsigned char vol;
  277. vol=newPara.spkVol;
  278. if(vol > 10) vol=10;
  279. //return vol/2;
  280. return vol;
  281. }
  282. void pttBeepStart(bool isMicReq){
  283. if(true==isMicReq) pwmBeepStart(500, 50);
  284. else pwmBeepStart(500, 50);
  285. }
  286. void beepStart(int freq){
  287. //const unsigned char duty[5]={100,99,98,97,96};
  288. int index=getDutyBySpk();
  289. if(index==0) return;//��撠誩ㄟ�嗥凒�乩���閬�偘�曆�
  290. pwmBeepStart(freq, index);//�删征瘥�80
  291. }
  292. void beepStop(void){
  293. nwy_pwm_stop(pwm_p);
  294. MSG_INFO(1, "beep stop");
  295. }
  296. static void pwmTimerCb(void *param){//beep 蝏𤘪��滢�
  297. beepStop();
  298. sutPocStatus.beep=0;
  299. }
  300. #else
  301. void beepStart(int freq){}
  302. #endif
  303. ///////////////////////PWM�批�蝏𤘪�////////////////////////////////
  304. //////////////////////靽格㺿EQ憓䂿��滨蔭��///////////////////////////////
  305. void modifyEqData(void){
  306. const char *handFreeEq="AT+CAVQE=0,1,12,\"01000700c800070002000000800c07000300f4ffa4060f000300fbff9001070000000000e803070000000000d007070000000000a60e070001000000\"\r\n";
  307. msgAtSend(handFreeEq);
  308. }
  309. //////////////////////靽格㺿颲枏枂憓䂿��滨蔭///////////////////////////////
  310. void modifyAudioOutGainConfig(void){
  311. char cmd[20]="AT+CACCP=0,1,2,\"";
  312. unsigned char alg1=211,alg2=0xff;
  313. unsigned char dac=125;//default=46
  314. int i;
  315. msgAtSend(cmd);
  316. for(i=0;i<16;i++){
  317. if(i==15){
  318. alg1=0;
  319. alg2=0;
  320. }
  321. snprintf(cmd, sizeof(cmd), "0000%02x00%02x%02x0000", dac,alg1,alg2);
  322. msgAtSend(cmd);
  323. alg1 += 3;
  324. }
  325. msgAtSend("\"\r\n");
  326. }