bsp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. #include "includes.h"
  2. #define SIZE_FROM_INNER (TUP_DATA_MAX+EXT_DATA) //内部(module)传输最大缓存
  3. #define SIZE_FROM_OUTTER (TUP_DATA_MAX+EXT_DATA) //外部(串口)传输最大缓存
  4. /*
  5. bspStartNotice
  6. 应用就绪标识
  7. 用于通知外部(串口)应用已正常启动
  8. */
  9. static void bspStartNotice(void){
  10. //char cmd[]="+POC:START\r\n";
  11. //msgToOutter(cmd);
  12. }
  13. static void msgFromInnerHandler(T_UINT8 *data, T_UINT16 length);
  14. static void msgFromOutterHandler(T_UINT8 *data, T_UINT16 length);
  15. #define VIR_WAIT_FOREVER (-1U)
  16. static LSAPI_OSI_Thread_t *gUartWRTd = NULL;
  17. static void usartCallBack(void *param, uint32_t evt);
  18. LSAPI_Device_UartConfig_t uart_cfg = {
  19. .name = LSAPI_MAKE_TAG('U', 'R', 'T', '1'),//LSAPI_MAKE_TAG('U', 'R', 'T', '1')/LSAPI_MAKE_TAG('U', 'R', 'T', '3')
  20. .baud = 115200,
  21. .format = LSAPI_DEVICE_FORMAT_8N1,
  22. .parity = LSAPI_DEVICE_PARITY_ODD,
  23. .event_cb=usartCallBack,
  24. };
  25. LSAPI_Device_t *uart=NULL;
  26. LSAPI_OSI_Pipe_t *at_rx_pipe = NULL;
  27. LSAPI_OSI_Pipe_t *at_tx_pipe = NULL;
  28. static void usartCallBack(void *param, uint32_t evt){
  29. LSAPI_OSI_Event_t uart_event;
  30. //wlog_info("usartCallBack(evt:%d)>> enter",evt);
  31. uart_event.id = USER_EVENT_UART;
  32. uart_event.param1 = evt;
  33. /*Note: must use osiEventTrySend, not to use osiEventSend*/
  34. if(osiEventTrySend((LSAPI_OSI_Thread_t *)param, &uart_event, 0) == FALSE)
  35. wlog_error("uart send event failed");
  36. }
  37. /*
  38. UART1初始化及处理线程
  39. */
  40. void uartThread(void *param){
  41. int read_size;
  42. static char gPtrReadBuf[SIZE_FROM_OUTTER];
  43. LSAPI_OSI_Event_t waitevent;
  44. wlog_info("uartThread start");
  45. for(;;){
  46. LSAPI_OSI_EventWait(LSAPI_OSI_ThreadCurrent(), &waitevent);
  47. switch(waitevent.param1){
  48. case LS_UART_EVENT_RX_ARRIVED:
  49. while(LSAPI_Device_ReadAvail(uart)>0){
  50. read_size = LSAPI_Device_Read(uart, gPtrReadBuf, SIZE_FROM_OUTTER);
  51. if(read_size< SIZE_FROM_OUTTER) gPtrReadBuf[read_size]=0;
  52. //wlog_info("UART:%d,%02x,%02x,%s",read_size,gPtrReadBuf[0],gPtrReadBuf[1],gPtrReadBuf);
  53. if(TRUE==app.tmMode){
  54. #ifdef ENABLE_USB_PORT
  55. usbOutPut(gPtrReadBuf,read_size);
  56. #endif
  57. }else{
  58. if(0==miapRecvFromUart(gPtrReadBuf,read_size))//优先处理PCTA消息
  59. msgFromOutterHandler(gPtrReadBuf,read_size);
  60. }
  61. }
  62. WakeupNow("usartCallBack");
  63. break;
  64. case LS_UART_EVENT_RX_OVERFLOW:
  65. break;
  66. case LS_UART_EVENT_TX_COMPLETE:
  67. break;
  68. default:break;
  69. }
  70. }
  71. gUartWRTd=NULL;
  72. LSAPI_Device_Close(uart);
  73. LSAPI_OSI_ThreadExit();
  74. }
  75. static void prvVirtAtRespCallback(void *param, unsigned event)
  76. {
  77. LSAPI_OSI_Pipe_t *pipe = (LSAPI_OSI_Pipe_t *)param;
  78. char buf[256];
  79. for (;;)
  80. {
  81. int bytes = LSAPI_OSI_PipeRead(pipe, buf, sizeof(buf)-1);
  82. if (bytes <= 0)
  83. break;
  84. buf[bytes] = '\0';
  85. //wlog_info("VAT1:%d,%s",bytes,buf);
  86. msgFromInnerHandler(buf,bytes);
  87. }
  88. WakeupNow("prvVirtAtRespCallback");
  89. }
  90. void ptInitViratThread(void *param){
  91. at_rx_pipe = LSAPI_OSI_PipeCreate(1024);
  92. at_tx_pipe = LSAPI_OSI_PipeCreate(1024);
  93. LSAPI_OSI_PipeSetReaderCallback(at_tx_pipe, LSAPI_PIPE_EVENT_RX_ARRIVED,
  94. prvVirtAtRespCallback, at_tx_pipe);
  95. LSAPI_Device_AtVirtConfig_t cfg = {
  96. .name = LSAPI_MAKE_TAG('V', 'A', 'T', '1'),
  97. .rx_pipe = at_rx_pipe,
  98. .tx_pipe = at_tx_pipe,
  99. };
  100. LSAPI_Device_t *device = LSAPI_Device_AtVirtCreate(&cfg);
  101. LSAPI_Device_AtDispatch_t *dispatch = LSAPI_Device_AtDispatchCreate(device);
  102. LSAPI_Device_AtSetDispatch(device, dispatch);
  103. LSAPI_Device_Open(device);
  104. LSAPI_OSI_ThreadExit();
  105. }
  106. #ifdef ENABLE_USB_PORT
  107. void usbComInit(void);
  108. #endif
  109. void uartComInit(void){
  110. bool ret;
  111. //init usart part
  112. gUartWRTd=LSAPI_OSI_ThreadCreate("uart1", uartThread, NULL, LSAPI_OSI_PRIORITY_NORMAL, UART_THREAD_STACK, 4);
  113. if(NULL==gUartWRTd) wlog_error("uart thread create err");
  114. else{
  115. uart_cfg.event_cb_ctx = gUartWRTd;
  116. uart = LSAPI_Device_UartCreate(&uart_cfg);
  117. if(uart==NULL){
  118. wlog_error("LSAPI_Device_UartCreate err");
  119. return;
  120. }
  121. ret = LSAPI_Device_Open(uart);
  122. if(false==ret){
  123. wlog_error("LSAPI_Device_Open failed");
  124. return;
  125. }
  126. LSAPI_Device_SetAutoSleep(uart, 500);
  127. }
  128. }
  129. void viratInit(void){
  130. if(NULL==LSAPI_OSI_ThreadCreate("virat", ptInitViratThread, NULL, LSAPI_OSI_PRIORITY_NORMAL, VAT_THREAD_STACK, 4))
  131. wlog_error("virat thread create err");
  132. }
  133. void bspInit(void){
  134. uartComInit();
  135. viratInit();
  136. //他奶奶的,下面的延时是一定要的,否则VAT1的回调无法进入
  137. //情况是:如果不加下面的延时,只要执行bspStartNotice往串口输出数据,VAT1就无法回调了
  138. //搞死我了,我顶你的肺
  139. LSAPI_OSI_ThreadSleep(500);
  140. bspStartNotice();
  141. setAudioChannelAndVol();
  142. #ifdef ENABLE_USB_PORT
  143. usbComInit();
  144. #endif
  145. }
  146. void uartOutPut(unsigned char *info, int len){
  147. if(NULL==uart) return;
  148. if(LSAPI_Device_WriteAvail(uart) != -1) LSAPI_Device_Write(uart, info,len);
  149. }
  150. /*
  151. outterInfo
  152. 发送数据到外部(串口)API
  153. */
  154. void outterInfo(T_UINT8 *info, T_UINT32 len){
  155. #ifndef ENABLE_UART1_MCU_CMD_ONLY
  156. uartOutPut(info, len);
  157. #endif
  158. #ifdef ENABLE_USB_PORT
  159. usbOutPut(info, len);
  160. #endif
  161. }
  162. /*
  163. innerInfo
  164. 发送数据到内部(module)API
  165. */
  166. void innerInfo(T_UINT8 *info, T_UINT32 len){
  167. if(len>0){
  168. if(0>LSAPI_OSI_PipeWriteAll(at_rx_pipe, info, len, VIR_WAIT_FOREVER))
  169. wlog_info("LSAPI_OSI_PipeWriteAll write failed");
  170. }
  171. }
  172. /*
  173. msgFromInnerHandler
  174. 内部(module)数据入队列操作
  175. */
  176. static void msgFromInnerHandler(T_UINT8 *data, T_UINT16 length){
  177. static T_UINT8 msg[SIZE_FROM_INNER];
  178. static T_UINT16 dLen=0;
  179. T_UINT16 i;
  180. static T_UINT8 lastData=0;
  181. for(i=0;i<length;i++){
  182. if(dLen < sizeof(msg)) msg[dLen++]=data[i];
  183. else{
  184. dLen=0;
  185. lastData=0;
  186. wlog_warn("Inner Data over");
  187. continue;
  188. }
  189. if(lastData=='\r' && data[i] == '\n'){
  190. switch(msgFromInner(msg, dLen)){
  191. case 0://queue ok
  192. break;
  193. case 1://queue full
  194. wlog_warn("queue full");
  195. break;
  196. case 2://queue buffer full
  197. wlog_warn("queue buffer full");
  198. break;
  199. case 3://unsupport queue
  200. wlog_warn("queue invalid");
  201. break;
  202. default:break;
  203. }
  204. dLen=0;
  205. }
  206. lastData=data[i];
  207. }
  208. }
  209. /*
  210. msgFromOutterHandler
  211. 外部(串口)数据入队列操作
  212. */
  213. static void msgFromOutterHandler(T_UINT8 *data, T_UINT16 length){
  214. static T_UINT8 msg[SIZE_FROM_OUTTER];
  215. static T_UINT16 dLen=0;
  216. static T_UINT8 douhaoNum=0;
  217. static T_UINT32 num;
  218. static T_UINT8 getNum=0,strLen;
  219. static T_UINT16 i;
  220. static T_UINT8 lastData=0;
  221. for(i=0;i<length;i++){
  222. if(dLen < sizeof(msg)) msg[dLen++]=data[i];
  223. else{
  224. dLen=0;
  225. lastData=0;
  226. wlog_warn("Outter data over");
  227. continue;
  228. }
  229. if(0==memcmp(msg, "AT+TCPSEND=", 11)){//AT+TUPSEND=1,1,1
  230. if(dLen >= 12 && (msg[11] < '0' || msg[11] > '9')) goto CMD_ERR_CLEAR;
  231. if(dLen >= 13 && msg[12] != ',') goto CMD_ERR_CLEAR;
  232. if(dLen >= 14 && (msg[13] < '0' || msg[13] > '9')) goto CMD_ERR_CLEAR;
  233. if(dLen >= 15){
  234. if(data[i]== ','){
  235. douhaoNum++;
  236. if(douhaoNum==1){
  237. strLen=dLen;
  238. num=atoi(msg+13);
  239. if(num > TUP_DATA_MAX) goto CMD_ERR_CLEAR;
  240. else getNum=1;
  241. }
  242. }else if(data[i] < '0' || data[i] > '9'){
  243. if(douhaoNum==0) goto CMD_ERR_CLEAR;
  244. }
  245. if(getNum!=0){
  246. if(dLen >= (num+strLen)){
  247. getNum=0;
  248. douhaoNum=0;
  249. num=0;
  250. strLen=0;
  251. goto CMD_QUE_PACKET;
  252. }
  253. }
  254. }
  255. }else if(lastData=='\r' && data[i] == '\n')
  256. goto CMD_QUE_PACKET;
  257. goto T_CONINUE;
  258. CMD_QUE_PACKET:
  259. switch(msgFromOutter(msg, dLen))
  260. {
  261. case 0://queue ok
  262. break;
  263. case 1://queue full
  264. wlog_warn("queue full");
  265. break;
  266. case 2://queue buffer full
  267. wlog_warn("queue buffer full");
  268. break;
  269. case 3://unsupport queue
  270. wlog_warn("queue invalid");
  271. break;
  272. default:
  273. break;
  274. }
  275. //memset(msg,0,sizeof(msg));
  276. dLen=0;
  277. goto T_CONINUE;
  278. CMD_ERR_CLEAR:
  279. getNum=0;
  280. douhaoNum=0;
  281. num=0;
  282. strLen=0;
  283. msgToOutter("+ERROR\r\n");
  284. wlog_error("outter cmd error");
  285. //memset(msg,0,sizeof(msg));
  286. dLen=0;
  287. T_CONINUE:
  288. lastData=data[i];
  289. }
  290. }
  291. #ifdef ENABLE_USB_PORT
  292. /////////////////////////////////////////////////////
  293. #include "lsapi_device.h"
  294. #include "lsapi_menu.h"
  295. #include "lsapi_event.h"
  296. static LSAPI_OSI_Thread_t *gUsbWRTd = NULL;
  297. LSAPI_Device_t *usbdev=NULL;
  298. void my_usb_evt_cb(lsUSBEvent_t event, unsigned long p){
  299. LSAPI_OSI_Event_t usb_event;
  300. usb_event.id = USER_EVENT_USB;
  301. usb_event.param1=event;
  302. wlog_info("my_usb_evt_cb:%d",event);
  303. osiEventTrySend((LSAPI_OSI_Thread_t *)p, &usb_event, 0);
  304. }
  305. LSAPI_Device_USBConfig_t usb_cfg = {
  306. .name = LSAPI_DEV_USB_COM7,
  307. .event_cb = my_usb_evt_cb,
  308. };
  309. void usbOutPut(unsigned char *info, int len){
  310. if(NULL==usbdev) return;
  311. if(LSAPI_Device_WriteAvail(usbdev) != -1) LSAPI_Device_Write(usbdev, info,len);
  312. }
  313. void usbPortHandler(void *param){
  314. int ret;
  315. unsigned char buffer[100];
  316. LSAPI_OSI_Event_t waitevent;
  317. wlog_info("usb create ok");
  318. for(;;){
  319. LSAPI_OSI_EventWait(LSAPI_OSI_ThreadCurrent(), &waitevent);
  320. if(waitevent.param1==LS_USB_EVENT_RX_ARRIVED){
  321. while(LSAPI_Device_ReadAvail(usbdev)>0){
  322. ret = LSAPI_Device_Read(usbdev, buffer, sizeof(buffer));
  323. if(ret< sizeof(buffer)) buffer[ret]=0;
  324. usbCmdHandler(buffer,ret);
  325. }
  326. WakeupNow("usbCallBack");
  327. }
  328. }
  329. wlog_info("usbPortHandler exit");
  330. LSAPI_Device_Close(usbdev);
  331. gUsbWRTd=NULL;
  332. LSAPI_OSI_ThreadExit();
  333. }
  334. void usbComInit(void){
  335. bool ret;
  336. //init usart part
  337. gUsbWRTd=LSAPI_OSI_ThreadCreate("usb", usbPortHandler, NULL, LSAPI_OSI_PRIORITY_NORMAL, USB_THREAD_STACK, 4);
  338. if(NULL==gUsbWRTd) wlog_error("usb thread create err");
  339. else{
  340. usb_cfg.event_cb_ctx = gUsbWRTd;
  341. usbdev = LSAPI_Device_USBCreateEx(&usb_cfg);
  342. if(usbdev==NULL){
  343. wlog_error("usbex create err");
  344. return;
  345. }
  346. ret = LSAPI_Device_Open(usbdev);
  347. if(false==ret){
  348. wlog_error("usb LSAPI_Device_Open failed");
  349. return;
  350. }
  351. LSAPI_Device_SetAutoSleep(usbdev, 500);
  352. wlog_info("usbComInit done");
  353. }
  354. }
  355. #endif