board.c 11 KB

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