lora_modem.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * File : lora-modem-driver.h
  3. * This file is driver for LoRa\LoRaWAN Modem part,base on RT-Thread RTOS.
  4. * now support to lorawan module:
  5. * - LSD4WN-2N717M91(CN470S\CN470)
  6. * - LSD4WN-2L717M90(CLAA)
  7. * - LSD4WN-2L817M90(EU868)
  8. * - LSD4WN-2K217M90(KR923)
  9. * Change Logs:
  10. * Date Author Notes
  11. * 2021-12-15 the first version
  12. */
  13. #ifndef __LORA_MODEM_DRIVER_H__
  14. #define __LORA_MODEM_DRIVER_H__
  15. #include <rtthread.h>
  16. #include <at.h>
  17. /*!
  18. * LoRa Modem software version number
  19. */
  20. #define LORA_MODEM_SW_VERSION "0.5.0"
  21. #ifndef PIN_LOW
  22. #define PIN_LOW 0x00
  23. #endif
  24. #ifndef PIN_HIGH
  25. #define PIN_HIGH 0x01
  26. #endif
  27. #define LORA_MODEM_USING_LORAWAN_MAC_JOIN_ACCEPT2_MS 6000
  28. #define LORA_MODEM_AT_CMD_CONNECT_WAIT_TIMEOUT 20
  29. #define LORA_MODEM_DRIVER_USING_POWER_CONTROL_ENABLE 1
  30. #define LORA_MODEM_DRIVER_USING_TEST_SHELL_EANBLE 1
  31. #define LORA_MODEM_DRIVER_USING_BUSY_PIN_INTERRUPT_ENABLE 0
  32. #define LORA_MODEM_SEND_TIMEOUT_MAX 0xFFFFFFFF
  33. #ifdef RT_USING_PIN
  34. #define LORA_MODEM_WAKE_PIN_MODE_OUTPUT() rt_pin_mode(LORA_MODEM_WAKE_PIN, PIN_MODE_OUTPUT)
  35. #define LORA_MODEM_WAKE_PIN_MODE_INPUT() rt_pin_mode(LORA_MODEM_WAKE_PIN, PIN_MODE_INPUT)
  36. #define LORA_MODEM_MODE_PIN_MODE_OUTPUT() rt_pin_mode(LORA_MODEM_MODE_PIN, PIN_MODE_OUTPUT)
  37. #define LORA_MODEM_NRST_PIN_MODE_OUTPUT() rt_pin_mode(LORA_MODEM_NRST_PIN, PIN_MODE_OUTPUT)
  38. #define LORA_MODEM_NRST_PIN_MODE_INPUT() rt_pin_mode(LORA_MODEM_NRST_PIN, PIN_MODE_INPUT)
  39. #define LORA_MODEM_STAT_PIN_MODE_INPUT() rt_pin_mode(LORA_MODEM_STAT_PIN, PIN_MODE_INPUT)
  40. #define LORA_MODEM_BUSY_PIN_MODE_INPUT() rt_pin_mode(LORA_MODEM_BUSY_PIN, PIN_MODE_INPUT)
  41. #define LORA_MODEM_P0_PIN_MODE_INPUT() rt_pin_mode(LORA_MODEM_P0_PIN, PIN_MODE_INPUT)
  42. #define LORA_MODEM_P1_PIN_MODE_INPUT() rt_pin_mode(LORA_MODEM_P1_PIN, PIN_MODE_INPUT)
  43. #define LORA_MODEM_P2_PIN_MODE_INPUT() rt_pin_mode(LORA_MODEM_P2_PIN, PIN_MODE_INPUT)
  44. #define LORA_MODEM_NRST_PIN_WRITE_HIGH() rt_pin_write(LORA_MODEM_NRST_PIN, PIN_HIGH)
  45. #define LORA_MODEM_NRST_PIN_WRITE_LOW() rt_pin_write(LORA_MODEM_NRST_PIN, PIN_LOW)
  46. #define LORA_MODEM_WAKE_PIN_WRITE_HIGH() rt_pin_write(LORA_MODEM_WAKE_PIN, PIN_HIGH)
  47. #define LORA_MODEM_WAKE_PIN_WRITE_LOW() rt_pin_write(LORA_MODEM_WAKE_PIN, PIN_LOW)
  48. #define LORA_MODEM_MODE_PIN_WRITE_HIGH() rt_pin_write(LORA_MODEM_MODE_PIN, PIN_HIGH)
  49. #define LORA_MODEM_MODE_PIN_WRITE_LOW() rt_pin_write(LORA_MODEM_MODE_PIN, PIN_LOW)
  50. #define LORA_MODEM_BUSY_PIN_READ() rt_pin_read(LORA_MODEM_BUSY_PIN)
  51. #define LORA_MODEM_STAT_PIN_READ() rt_pin_read(LORA_MODEM_STAT_PIN)
  52. #else
  53. #endif
  54. #define AT_CMD_SEND(cmd, timeout) \
  55. do \
  56. { \
  57. if (at_exec_cmd(at_resp_set_info(lwm_obj_get()->at_resp, 128, 0, rt_tick_from_millisecond(timeout)), cmd) < 0) \
  58. { \
  59. goto __exit; \
  60. } \
  61. } while(0);
  62. #define AT_CMD_EXEC(cmd, timeout, ...) at_exec_cmd(at_resp_set_info(lwm_obj_get()->at_resp, 128, 0, rt_tick_from_millisecond(timeout)), cmd)
  63. #define AT_RESP_PARSE_LINE_ARGS(resp_expr, ...) at_resp_parse_line_args(lwm_obj_get()->at_resp, 2, resp_expr, ##__VA_ARGS__)
  64. #define AT_RESP_PARSE_LINE_ARGS_BY_KW(cmd, resp_expr, ...) at_resp_parse_line_args_by_kw(lwm_obj_get()->at_resp, cmd, resp_expr, ##__VA_ARGS__)
  65. #define AT_CMD_EXEC_WICH_CHECK(cmd, timeout) \
  66. do \
  67. { \
  68. if (at_exec_cmd(at_resp_set_info(lwm_obj_get()->at_resp, 128, 0, rt_tick_from_millisecond(timeout)), cmd) < 0) \
  69. { \
  70. goto __exit; \
  71. } \
  72. } while(0);
  73. typedef enum
  74. {
  75. LORA_MODEM_OPMODE_DEEP_SLEEP,
  76. LORA_MODEM_OPMODE_TRANSPARENT,
  77. LORA_MODEM_OPMODE_COMMAND,
  78. LORA_MODEM_OPMODE_MAX,
  79. }lora_modem_opmode_t;
  80. typedef enum lora_modem_status_tag
  81. {
  82. LORA_MODEM_STATUS_COMM_UNINITIALIZED = 0x00,
  83. LORA_MODEM_STATUS_COMM_OK,
  84. LORA_MODEM_STATUS_COMM_START_SEND,
  85. LORA_MODEM_STATUS_COMM_UART_RESP_FAULT,
  86. LORA_MODEM_STATUS_COMM_LW_TRX_OK,
  87. LORA_MODEM_STATUS_COMM_LW_TRX_FAIL,
  88. LORA_MODEM_STATUS_COMM_LW_TRX_FAULT, /* if happened, maybe a serious question */
  89. }lora_modem_status_t;
  90. typedef enum lora_modem_status_sys_tag
  91. {
  92. LORA_MODEM_STATUS_SYS_UNINITIALIZED = 0x00,
  93. LORA_MODEM_STATUS_SYS_OK,
  94. LORA_MODEM_STATUS_SYS_START_SEND,
  95. LORA_MODEM_STATUS_SYS_BUSY,
  96. LORA_MODEM_STATUS_SYS_BUSY_FAULT,
  97. LORA_MODEM_STATUS_SYS_SLEEP_FAULT,
  98. LORA_MODEM_STATUS_SYS_WAKEUP_FAULT,
  99. }lora_modem_status_sys_t;
  100. struct lora_modem_common_configure
  101. {
  102. uint32_t device_type :2;
  103. uint32_t otaa :1;
  104. uint32_t confirm :1;
  105. uint32_t hot_start :1;
  106. uint32_t dr :3;
  107. uint32_t reserved :24;
  108. };
  109. struct lora_modem_status
  110. {
  111. uint32_t init :1;
  112. uint32_t net_joined :1;
  113. uint32_t sys_result :4;
  114. uint32_t comm_result :8;
  115. uint32_t rssi :8;
  116. uint32_t snr :8;
  117. uint32_t reserved :2;
  118. };
  119. typedef void (*recv_callback_t)(uint8_t *data, uint8_t len);
  120. struct lora_modem
  121. {
  122. rt_device_t device;
  123. rt_sem_t rx_notice;
  124. rt_mutex_t lock;
  125. rt_sem_t resp_notice;
  126. rt_int32_t rx_done_wait;
  127. struct lora_modem_common_configure config;
  128. struct lora_modem_status status;
  129. lora_modem_opmode_t cur_opmode;
  130. /* the current received one line data buffer */
  131. uint8_t *recv_buf;
  132. /* The length of the currently received one line data */
  133. uint8_t recv_len;
  134. /* The maximum supported receive data length */
  135. uint8_t recv_bufsz;
  136. rt_err_t (*at_client_rx_ind)(rt_device_t dev, rt_size_t size);
  137. at_response_t at_resp;
  138. recv_callback_t recv_callback;
  139. rt_thread_t recv_thread;
  140. };
  141. typedef struct lora_modem *lora_modem_t;
  142. extern int16_t lwm_init(const char *dev_name, uint8_t recv_bufsz, recv_callback_t recv_callback);
  143. extern int16_t lwm_at_urc_register(struct at_urc *at_urc_table,size_t size);
  144. extern int16_t lwm_data_send(const uint8_t *buf,uint8_t size,uint32_t timeout);
  145. extern int16_t lwm_netconn(uint8_t timeout);
  146. extern int16_t lwm_common_params_set(struct lora_modem_common_configure *conf);
  147. extern void lwm_opmode_set(lora_modem_opmode_t opmode);
  148. extern void lwm_hard_reset(void);
  149. extern lora_modem_t lwm_obj_get(void);
  150. #endif