ble.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include "includes.h"
  2. typedef enum
  3. {
  4. BLE_PTT_PRESS=0x01,
  5. BLE_PTT_FREE=0x02,
  6. BLE_CONNECT_SUC=0x03,
  7. BLE_CONNECT_FAIL=0x04,
  8. BLE_AUDCONNECT_SUC=0x05,
  9. BLE_BREAK_AUDCONNECT_SUC=0x06,
  10. BLE_SETMIC_SUC=0XE0,
  11. BLE_SETSPK_SUC=0XE1,
  12. }BLE_Result;
  13. const char Ble_IO2AT[5]={0x01,0xE0,0xFC,0x01,0xDB}; //03 succ 4 faile
  14. const char Ble_AT_ASK[]={0x04,0x0e}; //单个字节 0x01
  15. const char BLE_BREAK_BASECONNECT_ASK[]={0x01,0xe0,0xfc,0x01,0xdc};
  16. const char BLE_AUDCONNECT_ASK[]={0x01,0xe0,0xfc,0x01,0xde};
  17. const char BLE_BREAK_AUDCONNECT_ASK[]={0x01,0xe0,0xfc,0x01,0xdf};
  18. Sut_BLE Sut_ble;
  19. static LSAPI_Device_t * BT_POWER_Gpio = NULL;
  20. static LSAPI_Device_t * BT_CTL_Gpio = NULL;
  21. static LSAPI_Device_t * BLE_CTL_Gpio = NULL;
  22. #define BT_POWER_ID 21
  23. #define BT_CTL_ID 5
  24. #define BLE_CTL_ID 4
  25. #define BlE_TIMER_TICK 10
  26. void gpioBleConfigOutput(void){
  27. bool write_value = false;
  28. LSAPI_GpioConfig_t gpioConfig = {0};
  29. gpioConfig.mode = LS_GPIO_OUTPUT;
  30. gpioConfig.out_level=true;
  31. gpioConfig.id = BT_POWER_ID;
  32. BT_POWER_Gpio = LSAPI_Device_GPIOCreate(&gpioConfig);
  33. gpioConfig.id = BT_CTL_ID;
  34. BT_CTL_Gpio = LSAPI_Device_GPIOCreate(&gpioConfig);
  35. gpioConfig.id = BLE_CTL_ID;
  36. BLE_CTL_Gpio = LSAPI_Device_GPIOCreate(&gpioConfig);
  37. LSAPI_Device_Open(BT_POWER_Gpio);
  38. LSAPI_Device_Write(BT_POWER_Gpio, (void *)&write_value, 1);
  39. LSAPI_Device_Open(BT_CTL_Gpio);
  40. LSAPI_Device_Write(BT_CTL_Gpio, (void *)&write_value, 1);
  41. LSAPI_Device_Open(BLE_CTL_Gpio);
  42. LSAPI_Device_Write(BLE_CTL_Gpio, (void *)&write_value, 1);
  43. }
  44. LSAPI_OSI_Thread_t *threadid_Bletimer=NULL;
  45. LSAPI_OSI_Timer_t *BleTimer_t=NULL;
  46. void bleCb(void *param){
  47. static bool fanzhuan=true;
  48. bool lastValue=true;
  49. static short MC_cnt=0;
  50. if(Sut_ble.BleRingInit){
  51. fanzhuan=!fanzhuan;
  52. LSAPI_Device_Write(BLE_CTL_Gpio, (void *)&fanzhuan, 1);
  53. //MSG_INFO(1,"BLE_CTL_Gpio fanzhuan=%d",fanzhuan);
  54. LSAPI_OSI_TimerStart(BleTimer_t,BlE_TIMER_TICK);
  55. if(MC_cnt++>6000/BlE_TIMER_TICK){
  56. Sut_ble.BleRingInit=0;
  57. LSAPI_Device_Write(BLE_CTL_Gpio, (void *)&lastValue, 1);
  58. // MSG_INFO(1,"BLE_CTL_Gpio Over set high=%d",fanzhuan);
  59. MC_cnt=0;
  60. }
  61. }
  62. }
  63. void Ble_Init(){
  64. static int timer=0;
  65. }
  66. void BLETimerEntry(void *param)
  67. {
  68. Sut_ble.BleRingInit=1;
  69. if(NULL == BleTimer_t) BleTimer_t = LSAPI_OSI_TimerCreate(LSAPI_OSI_ThreadCurrent(), bleCb, NULL);
  70. if(NULL == BleTimer_t) MSG_WARN(1,"ble timer init failed");
  71. LSAPI_OSI_TimerStart(BleTimer_t,BlE_TIMER_TICK);
  72. LSAPI_OSI_Event_t event = {};
  73. for(;;)
  74. {
  75. LSAPI_OSI_EventWait(LSAPI_OSI_ThreadCurrent(), &event);
  76. }
  77. }
  78. void BLE_Open(){
  79. bool write_value = true;
  80. char write_low=false;
  81. // threadid_Bletimer = LSAPI_OSI_ThreadCreate("ostimer", BLETimerEntry,NULL,LSAPI_OSI_PRIORITY_NORMAL,1024,4);
  82. // if(threadid_Bletimer==0)MSG_WARN(1,"create threadid_Bletimer fail");
  83. // Ble_Info.BleRingInit=1;
  84. // LSAPI_OSI_TimerStart(BleTimer_t,BlE_TIMER_TICK);
  85. LSAPI_Device_Write(BT_CTL_Gpio, (void *)&write_value, 1); //需要先置高
  86. LSAPI_Device_Write(BT_POWER_Gpio, (void *)&write_value, 1);
  87. LSAPI_Device_Write(BLE_CTL_Gpio, (void *)&write_low, 1);//正常为低
  88. newPara.BleEnable=1;
  89. }
  90. void BT_Match_IOctr(bool ctr){//
  91. bool write_value;
  92. write_value=ctr;
  93. LSAPI_Device_Write(BT_CTL_Gpio, (void *)&write_value, 1);//正常为低
  94. if(write_value)MSG_INFO(1,"%s=====SET HIGH",*BT_Match_IOctr);
  95. else MSG_INFO(1,"%s=====SET LOW",*BT_Match_IOctr);
  96. }
  97. void Ble_Match_IOCTR(bool ctr){//
  98. bool write_value=true ;
  99. write_value=ctr;
  100. LSAPI_Device_Write(BLE_CTL_Gpio, (void *)&write_value, 1);//正常为低
  101. if(write_value)MSG_INFO(1,"BLE_CTR=====SET HIGH");
  102. else MSG_INFO(1,"BLE_CTR=====SET LOW");
  103. }
  104. void BLE_Close(){
  105. bool write_value = false;
  106. LSAPI_Device_Write(BT_POWER_Gpio, (void *)&write_value, 1);
  107. LSAPI_Device_Write(BT_CTL_Gpio, (void *)&write_value, 1);
  108. LSAPI_Device_Write(BLE_CTL_Gpio, (void *)&write_value, 1);
  109. }
  110. void BLE_at_process(char *r_buffer,int read_size){
  111. //if(sutApp.authReady==0) return;
  112. if(memcmp(r_buffer,Ble_AT_ASK,sizeof(Ble_AT_ASK)))return;
  113. newPara.BleEnable=1;
  114. MSG_INFO(1,"Ble_result===%02x",r_buffer[read_size-1]);
  115. switch(r_buffer[read_size-1]){
  116. case BLE_PTT_PRESS:
  117. micPttHandler(1);
  118. break;
  119. case BLE_PTT_FREE:
  120. micPttHandler(0);
  121. break;
  122. case BLE_CONNECT_SUC:
  123. Sut_ble.Base_Conected=1;
  124. break;
  125. case BLE_CONNECT_FAIL:
  126. Sut_ble.Base_Conected=0;
  127. ModemSendAt("AT+AUDCH=0,0\r\n");
  128. break;
  129. case BLE_AUDCONNECT_SUC:
  130. Sut_ble.Audio_Conected=1;
  131. ModemSendAt("AT+AUDCH=1,3\r\n");
  132. break;
  133. case BLE_BREAK_AUDCONNECT_SUC:
  134. Sut_ble.Audio_Conected=0;
  135. break;
  136. case BLE_SETMIC_SUC:
  137. break;
  138. case BLE_SETSPK_SUC:
  139. break;
  140. }
  141. }
  142. void Ble_handler(unsigned int interval){
  143. static unsigned short cnt=0,lcnt=0;
  144. if(!newPara.BleEnable)return;
  145. if(Sut_ble.Base_Conected&&Sut_ble.Audio_Conected)return;
  146. if(++cnt<(2000/interval)) return;//1秒读一次ADC即可
  147. cnt=0;
  148. if(Sut_ble.Base_Conected==0){
  149. Ble_SendAT(Ble_IO2AT,sizeof(Ble_IO2AT));
  150. return;
  151. }
  152. if(Sut_ble.Audio_Conected==0){
  153. Ble_SendAT(BLE_AUDCONNECT_ASK,sizeof(BLE_AUDCONNECT_ASK));
  154. return;
  155. }
  156. }