at_base_cmd.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * File : at_base_cmd.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-04-01 armink first version
  23. * 2018-04-04 chenyong add base commands
  24. */
  25. #include <at.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <rtdevice.h>
  29. #define AT_ECHO_MODE_CLOSE 0
  30. #define AT_ECHO_MODE_OPEN 1
  31. extern at_server_t at_get_server(void);
  32. static at_result_t at_exec(void)
  33. {
  34. return AT_RESULT_OK;
  35. }
  36. AT_CMD_EXPORT("AT", RT_NULL, RT_NULL, RT_NULL, RT_NULL, at_exec);
  37. static at_result_t atz_exec(void)
  38. {
  39. at_server_printfln("OK");
  40. at_port_factory_reset();
  41. return AT_RESULT_NULL;
  42. }
  43. AT_CMD_EXPORT("ATZ", RT_NULL, RT_NULL, RT_NULL, RT_NULL, atz_exec);
  44. static at_result_t at_rst_exec(void)
  45. {
  46. at_server_printfln("OK");
  47. at_port_reset();
  48. return AT_RESULT_NULL;
  49. }
  50. AT_CMD_EXPORT("AT+RST", RT_NULL, RT_NULL, RT_NULL, RT_NULL, at_rst_exec);
  51. static at_result_t ate_setup(const char *args)
  52. {
  53. int echo_mode = atoi(args);
  54. if(echo_mode == AT_ECHO_MODE_CLOSE || echo_mode == AT_ECHO_MODE_OPEN)
  55. {
  56. at_get_server()->echo_mode = echo_mode;
  57. }
  58. else
  59. {
  60. return AT_RESULT_FAILE;
  61. }
  62. return AT_RESULT_OK;
  63. }
  64. AT_CMD_EXPORT("ATE", "<value>", RT_NULL, RT_NULL, ate_setup, RT_NULL);
  65. static at_result_t at_show_cmd_exec(void)
  66. {
  67. extern void rt_at_server_print_all_cmd(void);
  68. rt_at_server_print_all_cmd();
  69. return AT_RESULT_OK;
  70. }
  71. AT_CMD_EXPORT("AT&L", RT_NULL, RT_NULL, RT_NULL, RT_NULL, at_show_cmd_exec);
  72. static at_result_t at_uart_query(void)
  73. {
  74. struct rt_serial_device *serial = (struct rt_serial_device *)at_get_server()->device;
  75. at_server_printfln("AT+UART=%d,%d,%d,%d,%d", serial->config.baud_rate, serial->config.data_bits,
  76. serial->config.stop_bits, serial->config.parity, 1);
  77. return AT_RESULT_OK;
  78. }
  79. static at_result_t at_uart_setup(const char *args)
  80. {
  81. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  82. int baudrate, databits, stopbits, parity, flow_control, argc;
  83. const char *req_expr = "=%d,%d,%d,%d,%d";
  84. argc = at_req_parse_args(args, req_expr, &baudrate, &databits, &stopbits, &parity, &flow_control);
  85. if (argc != 5)
  86. {
  87. return AT_RESULT_PARSE_FAILE;
  88. }
  89. at_server_printfln("UART baudrate : %d", baudrate);
  90. at_server_printfln("UART databits : %d", databits);
  91. at_server_printfln("UART stopbits : %d", stopbits);
  92. at_server_printfln("UART parity : %d", parity);
  93. at_server_printfln("UART control : %d", flow_control);
  94. config.baud_rate = baudrate;
  95. config.data_bits = databits;
  96. config.stop_bits = stopbits;
  97. config.parity = parity;
  98. if(rt_device_control(at_get_server()->device, RT_DEVICE_CTRL_CONFIG, &config) != RT_EOK)
  99. {
  100. return AT_RESULT_FAILE;
  101. }
  102. return AT_RESULT_OK;
  103. }
  104. AT_CMD_EXPORT("AT+UART", "=<baudrate>,<databits>,<stopbits>,<parity>,<flow_control>", RT_NULL, at_uart_query, at_uart_setup, RT_NULL);