phy_console.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. #include "phy_console.h"
  29. #include "uart.h"
  30. #include "error.h"
  31. #include "OSAL.h"
  32. #include "string.h"
  33. #include "pwrmgr.h"
  34. #include "log.h"
  35. typedef enum{
  36. CONS_ST_IDLE,
  37. CONS_ST_RX,
  38. CONS_ST_CMD
  39. }cons_state_t;
  40. typedef struct{
  41. cons_state_t state;
  42. uint16_t rx_cnt;
  43. uint8_t rx_buf[CONS_CMD_RXBUF_MAX];
  44. const cons_cmd_t* cmd_list;
  45. cons_callback_t callback;
  46. }cons_ctx_t;
  47. cons_ctx_t s_cons_ctx;
  48. void console_parse_cmd(void)
  49. {
  50. uint16_t i,j;
  51. uint16_t cmd_id = 0;
  52. char* param_table[CONS_PARAM_NUM_MAX];
  53. cons_ctx_t* pctx = &s_cons_ctx;
  54. const cons_cmd_t* cmd_list = pctx->cmd_list;
  55. uint8_t* prx = s_cons_ctx.rx_buf;
  56. char* pcmd = NULL;
  57. char* pparam = NULL;
  58. uint8_t param_num = 0;
  59. //LOG("\n");
  60. //for(i = 0; i< pctx->rx_cnt; i++)
  61. // LOG("%x ",prx[i]);
  62. // LOG("\n");
  63. //parse "at"
  64. for(i = 1; i< pctx->rx_cnt; i++){
  65. if(prx[i-1] == 'a' && prx[i] == 't'){
  66. i++;
  67. break;
  68. }
  69. }
  70. if(i == pctx->rx_cnt){
  71. LOG("Parse filed, did not found \"at\"\n");
  72. return;
  73. }
  74. //parse cmd
  75. {
  76. pcmd = (char*)prx+i;
  77. for(; i< pctx->rx_cnt; i++){
  78. if(prx[i] == ' ' || prx[i] == '\0' ){
  79. prx[i] = '\0';
  80. break;
  81. }
  82. if(prx[i] < 0x20 || prx[i] > 0x7d){
  83. LOG("Parse failed, cmd has illegal character\n");
  84. return;
  85. }
  86. }
  87. if(osal_strlen(pcmd) == 0){
  88. LOG("Parse failed, cmd is empty\n");
  89. return;
  90. }
  91. for(j = 0; j<CONS_CMD_NUM_MAX; j++){
  92. if(cmd_list[j].cmd_id == 0 || cmd_list[j].cmd_name == NULL ){
  93. LOG("Parse failed, cmd is not match cmdlist\n");
  94. return;
  95. }
  96. if(strcmp(pcmd, cmd_list[j].cmd_name) == 0){
  97. //match
  98. cmd_id = cmd_list[j].cmd_id;
  99. break;
  100. }
  101. }
  102. if(cmd_id == 0){
  103. LOG("Parse failed, cmd is not match cmdlist_1\n");
  104. return;
  105. }
  106. }
  107. i++;
  108. //parse parameter
  109. pparam = (char*)prx+i;
  110. for(; i< pctx->rx_cnt; i++){
  111. if(prx[i] == ' '){
  112. prx[i] = '\0';
  113. if(osal_strlen(pparam) == 0){
  114. break;
  115. }
  116. param_table[param_num] = pparam;
  117. param_num++;
  118. if(param_num >= CONS_PARAM_NUM_MAX)
  119. break;
  120. pparam = (char*)prx+i+1;
  121. continue;
  122. }
  123. if(prx[i] == '\0'){
  124. if(osal_strlen(pparam) == 0){
  125. break;
  126. }
  127. param_table[param_num] = pparam;
  128. param_num++;
  129. break;
  130. }
  131. if(prx[i] < 0x20 || prx[i] > 0x7d){
  132. LOG("Parse failed, parameter has illegal character\n");
  133. return;
  134. }
  135. }
  136. pctx->callback(cmd_id, param_num, param_table);
  137. }
  138. void console_sleep_handler(void)
  139. {
  140. hal_gpio_fmux(P10, Bit_DISABLE); //enable fullmux fuction; enable or disable
  141. hal_gpio_wakeup_set(P10, NEGEDGE);
  142. }
  143. void console_wakeup_handler(void)
  144. {
  145. int i;
  146. hal_gpio_write(P14, 0);
  147. hal_gpio_write(P14, 1);
  148. hal_gpio_write(P14, 0);
  149. for(i = 0; i< 20; i++){
  150. if(hal_gpio_read(P10)==0)
  151. {
  152. hal_pwrmgr_lock(MOD_CONSOLE);
  153. break;
  154. }
  155. }
  156. hal_gpio_write(P14, 0);
  157. hal_gpio_write(P14, 1);
  158. hal_gpio_write(P14, 0);
  159. s_cons_ctx.state = CONS_ST_IDLE;
  160. }
  161. void console_rx_handler(uart_Evt_t* pev)
  162. {
  163. cons_ctx_t* pctx = &s_cons_ctx;
  164. uint8_t* prx = s_cons_ctx.rx_buf;
  165. switch(pev->type){
  166. case UART_EVT_TYPE_RX_DATA:
  167. case UART_EVT_TYPE_RX_DATA_TO:
  168. {
  169. uint8_t i;
  170. uint8_t* prx_msg = pev->data;
  171. //for(i = 0; i< pev->len; i++)
  172. // LOG("%x ",pev->data[i]);
  173. //LOG("\n");
  174. if(pctx->state == CONS_ST_IDLE){
  175. hal_pwrmgr_lock(MOD_CONSOLE);
  176. pctx->state = CONS_ST_RX;
  177. }
  178. if(pctx->state == CONS_ST_RX){
  179. for(i = 0; i< pev->len; i++){
  180. if(prx_msg[i] == '\r' ||prx_msg[i] == '\n'){
  181. prx[pctx->rx_cnt] = 0;
  182. pctx->rx_cnt++;
  183. pctx->state = CONS_ST_CMD;
  184. console_parse_cmd();
  185. pctx->state = CONS_ST_IDLE;
  186. pctx->rx_cnt = 0;
  187. hal_pwrmgr_unlock(MOD_CONSOLE);
  188. break;
  189. }
  190. prx[pctx->rx_cnt] = prx_msg[i];
  191. pctx->rx_cnt++;
  192. }
  193. }
  194. else
  195. {
  196. hal_pwrmgr_unlock(MOD_CONSOLE);
  197. }
  198. break;
  199. }
  200. default:
  201. break;
  202. }
  203. }
  204. int console_init(const cons_cmd_t* cmdlist, cons_callback_t callback)
  205. {
  206. uart_Cfg_t cfg = {
  207. .tx_pin = P9,
  208. .rx_pin = P10,
  209. .rts_pin = GPIO_DUMMY,
  210. .cts_pin = GPIO_DUMMY,
  211. .baudrate = 115200,
  212. .use_fifo = TRUE,
  213. .hw_fwctrl = FALSE,
  214. .use_tx_buf = FALSE,
  215. .parity = FALSE,
  216. .evt_handler = console_rx_handler,
  217. };
  218. if(callback == NULL)
  219. return PPlus_ERR_INVALID_PARAM;
  220. hal_pwrmgr_register(MOD_CONSOLE, console_sleep_handler, console_wakeup_handler);
  221. hal_uart_init(cfg);//uart init
  222. s_cons_ctx.cmd_list = cmdlist;
  223. s_cons_ctx.state = CONS_ST_IDLE;
  224. s_cons_ctx.rx_cnt = 0;
  225. s_cons_ctx.callback = callback;
  226. return PPlus_SUCCESS;
  227. }