ppp_device_ml305.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2006-2019, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-06-23 HelloByeAll the first version
  9. */
  10. #include <ppp_device.h>
  11. #include <ppp_chat.h>
  12. #include <rtdevice.h>
  13. #define DBG_TAG "ppp.ml305"
  14. #ifdef PPP_DEVICE_DEBUG
  15. #define DBG_LVL DBG_LOG
  16. #else
  17. #define DBG_LVL DBG_INFO
  18. #endif
  19. #include <rtdbg.h>
  20. #define ML305POWER_ON PIN_HIGH
  21. #define ML305POWER_OFF PIN_LOW
  22. #ifndef ML305POWER_PIN
  23. #define ML305POWER_PIN -1
  24. #endif
  25. #define ML305WARTING_TIME_BASE 2000
  26. #define PPP_DAIL_CMD "ATD*99***1#" /* redefine DAIL CMD */
  27. #ifndef PKG_USING_CMUX
  28. static const struct modem_chat_data rst_mcd[] =
  29. {
  30. {"AT+MREBOOT", MODEM_CHAT_RESP_NOT_NEED, 30, 1, RT_FALSE},
  31. {"AT", MODEM_CHAT_RESP_OK, 30, 10,RT_FALSE},
  32. {"+++", MODEM_CHAT_RESP_NOT_NEED, 30, 1, RT_TRUE},
  33. {"ATH", MODEM_CHAT_RESP_OK, 30, 1, RT_FALSE},
  34. };
  35. static const struct modem_chat_data mcd[] =
  36. {
  37. {"AT", MODEM_CHAT_RESP_OK, 10, 1, RT_FALSE},
  38. {"ATE0", MODEM_CHAT_RESP_OK, 1, 1, RT_FALSE},
  39. {"AT+CPIN?", MODEM_CHAT_RESP_READY, 10, 1, RT_FALSE},
  40. {"AT+CFUN?", MODEM_CHAT_RESP_OK, 10, 1, RT_FALSE},
  41. {"AT+CSQ", MODEM_CHAT_RESP_OK, 10, 1, RT_FALSE},
  42. {PPP_APN_CMD, MODEM_CHAT_RESP_OK, 1, 5, RT_FALSE},
  43. {"AT+CGACT=1,1", MODEM_CHAT_RESP_OK, 10, 1, RT_FALSE},
  44. {PPP_DAIL_CMD, MODEM_CHAT_RESP_CONNECT, 1, 30, RT_FALSE},
  45. };
  46. #else
  47. static const struct modem_chat_data mcd[] =
  48. {
  49. {"AT", MODEM_CHAT_RESP_NOT_NEED, 10, 1, RT_FALSE},
  50. {PPP_APN_CMD, MODEM_CHAT_RESP_NOT_NEED, 1, 5, RT_FALSE},
  51. {PPP_DAIL_CMD, MODEM_CHAT_RESP_NOT_NEED, 1, 30, RT_FALSE},
  52. };
  53. #endif
  54. static rt_err_t ppp_ml305prepare(struct ppp_device *device)
  55. {
  56. #ifndef PKG_USING_CMUX
  57. if (device->power_pin >= 0)
  58. {
  59. rt_pin_write(device->power_pin, ML305POWER_OFF);
  60. rt_thread_mdelay(ML305WARTING_TIME_BASE / 20);
  61. rt_pin_write(device->power_pin, ML305POWER_ON);
  62. rt_thread_mdelay(ML305WARTING_TIME_BASE);
  63. }
  64. else
  65. {
  66. rt_err_t err;
  67. err = modem_chat(device->uart, rst_mcd, sizeof(rst_mcd) / sizeof(rst_mcd[0]));
  68. if (err)
  69. return err;
  70. }
  71. #endif
  72. return modem_chat(device->uart, mcd, sizeof(mcd) / sizeof(mcd[0]));
  73. }
  74. /* ppp_mlm305ops for ppp_device_ops , a common interface */
  75. static struct ppp_device_ops ml305ops =
  76. {
  77. .prepare = ppp_ml305prepare,
  78. };
  79. /**
  80. * register air720 into ppp_device
  81. *
  82. * @return =0: ppp_device register successfully
  83. * <0: ppp_device register failed
  84. */
  85. int ppp_ml305register(void)
  86. {
  87. struct ppp_device *ppp_device = RT_NULL;
  88. ppp_device = rt_malloc(sizeof(struct ppp_device));
  89. if(ppp_device == RT_NULL)
  90. {
  91. LOG_E("No memory for ml305 ppp_device.");
  92. return -RT_ENOMEM;
  93. }
  94. ppp_device->power_pin = ML305POWER_PIN;
  95. if (ppp_device->power_pin >= 0)
  96. {
  97. rt_pin_mode(ppp_device->power_pin, PIN_MODE_OUTPUT);
  98. rt_pin_write(ppp_device->power_pin, ML305POWER_ON);
  99. rt_thread_mdelay(ML305WARTING_TIME_BASE);
  100. }
  101. ppp_device->ops = &ml305ops;
  102. LOG_D("ppp ml305 is registering ppp_device");
  103. return ppp_device_register(ppp_device, PPP_DEVICE_NAME, RT_NULL, RT_NULL);
  104. }
  105. INIT_COMPONENT_EXPORT(ppp_ml305register);