ppp_device_m6312.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. * 2019-09-24 xiaofao the first version
  9. */
  10. #include <ppp_device.h>
  11. #include <ppp_chat.h>
  12. #include <rtdevice.h>
  13. #define DBG_TAG "ppp.m6312"
  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 M6312_POWER_ON PIN_HIGH
  21. #define M6312_POWER_OFF PIN_LOW
  22. #ifndef M6312_POWER_PIN
  23. #define M6312_POWER_PIN -1
  24. #endif
  25. #define M6312_WARTING_TIME_BASE 500
  26. static const struct modem_chat_data rst_mcd[] =
  27. {
  28. {"+++", MODEM_CHAT_RESP_OK, 10, 1, RT_FALSE},
  29. {"ATH", MODEM_CHAT_RESP_OK, 1, 1, RT_FALSE},
  30. {"AT+CMRESET", MODEM_CHAT_RESP_NOT_NEED, 1, 1, RT_FALSE},
  31. };
  32. static const struct modem_chat_data mcd[] =
  33. {
  34. {"AT", MODEM_CHAT_RESP_OK, 10, 1, RT_FALSE},
  35. {"ATE0", MODEM_CHAT_RESP_OK, 1, 1, RT_FALSE},
  36. {PPP_APN_CMD, MODEM_CHAT_RESP_OK, 1, 5, RT_FALSE},
  37. {PPP_DAIL_CMD, MODEM_CHAT_RESP_CONNECT, 1, 30, RT_FALSE},
  38. };
  39. static rt_err_t ppp_m6312_prepare(struct ppp_device *device)
  40. {
  41. if (device->power_pin >= 0)
  42. {
  43. rt_pin_write(device->power_pin, M6312_POWER_OFF);
  44. rt_thread_mdelay(M6312_WARTING_TIME_BASE);
  45. rt_pin_write(device->power_pin, M6312_POWER_ON);
  46. }
  47. else
  48. {
  49. rt_err_t err;
  50. err = modem_chat(device->uart, rst_mcd, sizeof(rst_mcd) / sizeof(rst_mcd[0]));
  51. if (err)
  52. return err;
  53. }
  54. return modem_chat(device->uart, mcd, sizeof(mcd) / sizeof(mcd[0]));
  55. }
  56. /* ppp_m6312_ops for ppp_device_ops , a common interface */
  57. static struct ppp_device_ops m6312_ops =
  58. {
  59. .prepare = ppp_m6312_prepare,
  60. };
  61. /**
  62. * register m6312 into ppp_device
  63. *
  64. * @return =0: ppp_device register successfully
  65. * <0: ppp_device register failed
  66. */
  67. int ppp_m6312_register(void)
  68. {
  69. struct ppp_device *ppp_device = RT_NULL;
  70. ppp_device = rt_malloc(sizeof(struct ppp_device));
  71. if(ppp_device == RT_NULL)
  72. {
  73. LOG_E("No memory for struct m6312 ppp_device.");
  74. return -RT_ENOMEM;
  75. }
  76. ppp_device->power_pin = M6312_POWER_PIN;
  77. if (ppp_device->power_pin >= 0)
  78. {
  79. rt_pin_mode(ppp_device->power_pin, PIN_MODE_OUTPUT);
  80. rt_pin_write(ppp_device->power_pin, M6312_POWER_OFF);
  81. }
  82. ppp_device->ops = &m6312_ops;
  83. LOG_D("ppp m6312 is registering ppp_device");
  84. return ppp_device_register(ppp_device, PPP_DEVICE_NAME, RT_NULL, RT_NULL);
  85. }
  86. INIT_COMPONENT_EXPORT(ppp_m6312_register);