LORA.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #ifndef _LORA_H_
  2. #define _LORA_H_
  3. //#define lora_channel 0x02 //信道
  4. #define ringbuffer_size 1024*60
  5. // #define LORA_POWER_PIN (20)
  6. // #define LORA_TXD_PIN (17)
  7. // #define LORA_RXD_PIN (18)
  8. // #define LORA_BUSY_PIN (5)
  9. /*
  10. BUSY_STA,忙状态指示。高电平表示设备当前工作状态忙,不可进行串口操
  11. 作,低电平表示外设可以进行串口操作
  12. */
  13. #define LORA_DATA_MAX_LEN 1024
  14. #define uart_fifo_size 120
  15. #define buffer_size 1024*20
  16. #define uart_timerout_times 30 //ms
  17. typedef struct
  18. {
  19. char version;
  20. char wireless_ch;
  21. char wireless_freq_range;
  22. char wireless_tx_power;
  23. char wireless_bps;
  24. char serial_bps;
  25. char packet_fmt;
  26. char auto_ack;
  27. char outdir;
  28. char wireless_ch_interval_bandwidth;
  29. }LORA_CFG_T;
  30. typedef enum
  31. {
  32. uart_idle = 0,
  33. uart_receving = 1,
  34. uart_receive_complete = 2,
  35. }uart_status_t;
  36. typedef struct {
  37. uint8_t data_len;
  38. uint8_t data_buf[LORA_DATA_MAX_LEN];
  39. }LORA_DATA_T;
  40. typedef LORA_DATA_T USE_DATA_T;
  41. extern LORA_CFG_T lora_cfg_data;
  42. extern uint8_t lora_rssi_data;
  43. extern uint8_t lora_software_version;
  44. void lora_send_queue_callback(uint8_t* data_buf,int len);
  45. void lora_init(void);
  46. void lora_uart_pin_init(void);
  47. void lora_event_task(void *pvParameters);
  48. void lora_set_power_level(int level);
  49. int lora_set_by_serial(uint8_t cmd,uint8_t value);
  50. int lora_reset_software(void);
  51. int lora_set_factory(void);
  52. int lora_set_wireless_channel_interval_bandwidth(uint8_t bandwidth);
  53. int lora_set_data_outdir(uint8_t dir);
  54. int lora_set_auto_ack(uint8_t ack);
  55. int lora_set_packet_fmt(uint8_t fmt);
  56. int lora_set_Wireless_Channel (uint8_t Channel);
  57. int lora_set_tx_power(uint8_t power);
  58. int lora_set_Wireless_bps(uint8_t bps);
  59. int lora_set_serial_bps(uint8_t bps);
  60. int lora_get_rssi(uint32_t rssi);
  61. int lora_get_version(void);
  62. int lora_get_cfg(void);
  63. int lora_send_data(const char* lora_data,int lora_data_len);
  64. int lora_set_cfg_mode(uint8_t mode);
  65. int get_lora_busy_pin();
  66. //int lora_analytic_data(const uint8_t* data_buf,int len);
  67. #if 1
  68. #define USER_QIXIN 1 //使用祁鑫的初始化
  69. #define RX_MAX_LENGTH 1024
  70. #include<stdbool.h>
  71. #define LORA_CHANENL 0x00
  72. #define HPD_LORA_ENABLE 0
  73. #define VJ_LORA_ENABLE 1
  74. #define LORA_UART UART_NUM_1
  75. // IO5 I LORA_BUSY LORA总线忙输入,参考LORA模块PDF文档 仅合普顿模块有效
  76. // IO6 I LORA_SET LORA设置参数IO,参考LORA模块PDF文档
  77. // IO7 O LORA_RST LORA复位输出IO,参考LORA模块PDF文档
  78. // IO17 O LORA_RXD LORA串口数据RXD
  79. // IO18 I LORA_TXD LORA串口数据TXD
  80. // IO20 O LORA_POWER_SD LORA模块电源控制,默认高,低电平LORA模块上电。
  81. #define LORA_BUSY_PIN 5
  82. #define LORA_SET_PIN 6
  83. #define LORA_RST_PIN 7
  84. #define LORA_RXD_PIN 18
  85. #define LORA_TXD_PIN 17
  86. #define LORA_POWER_PIN 20
  87. #define LORA_CMD_MAX_LENGTH 256
  88. #define LORA_CMD_NUM 16
  89. #define MAX_RETRY_NUM 5
  90. #define LORA_CMD_AH_CMD "AH"
  91. #define LORA_CMD_AH_CMD_RSP "OK"
  92. //设置波特率
  93. #define LORA_CMD_AH_BAUDRATE "AH+B"
  94. #define LORA_CMD_AH_BAUDRATE_RSP "OK+B"
  95. #define AT_MAX_RETRY_TIMES 10
  96. #if VJ_LORA_ENABLE
  97. #define LORA_SUCCESS 0x55 //沃进成功返回命令码
  98. #define LORA_FAIL 0xEE //沃进失败返回命令码
  99. #endif
  100. typedef enum
  101. {
  102. Lora_Cmd_Mode = 0, //AT命令模式
  103. Lora_UART_Mode = 1, //串口透传模式
  104. // Lora_read_data = 2,
  105. }Lora_Mode_t;
  106. typedef enum
  107. {
  108. lora_cmd_system_type = 0, //系统上电配置的AT初始化命令模式
  109. lora_cmd_user_type = 1, //用户自定义可以随时的发送命令
  110. }Lora_cmd_type_t;
  111. //命令解析函数
  112. typedef struct
  113. {
  114. uint8_t cmd[LORA_CMD_MAX_LENGTH]; //命令字符串
  115. uint8_t cmd_len; //命令长度
  116. uint32_t cmd_mode; //当前命令
  117. void (*lora_send_cmd_handler)(uint32_t cmd_mode);
  118. uint8_t cmd_rsp[LORA_CMD_MAX_LENGTH]; //命令回复字符串
  119. uint8_t cmd_rsp_len; //回复命令长度
  120. void(*success_hanlder)(unsigned char *cmd,unsigned char *cmd_rsp,int len); //回调函数
  121. void(*timeout_hanlder)(unsigned char *cmd,unsigned char *cmd_rsp,int len); //超时未响应回调函数
  122. }lora_cmd_t;
  123. typedef struct
  124. {
  125. uint8_t lora_mode; //Lora_Mode_t
  126. lora_cmd_t *lora_cmd;
  127. uint8_t cmd_index; //当前执行命令索引值
  128. bool timerout;
  129. uint8_t retry_times; //最大重新传输次数
  130. uint8_t cmd_type;
  131. }Lora_t;
  132. extern uint8_t rssi;
  133. void lora_init();
  134. int lora_sendData(unsigned char* data,int len);
  135. void lora_cmd_success_Hander(unsigned char *cmd,unsigned char *cmd_rsp,int len);
  136. void lora_cmd_timerout_Hander(unsigned char *cmd,unsigned char *cmd_rsp,int len);
  137. void VJ_Lora_set_cfg_mode(uint8_t mode);
  138. void lora_set_config_mode(uint8_t mode);
  139. void lora_set_channel(uint8_t channel);
  140. void lora_set_bps(uint8_t bps);
  141. void lora_set_mode(uint8_t mode);
  142. void lora_send_ack(uint8_t cmd,uint8_t *mac,int id_num,uint8_t *cmd_index,uint8_t status);
  143. void dymatic_change_chanel(uint8_t chanel);
  144. void lora_set_device_id(uint32_t id);
  145. void lora_set_dst_device_id(uint32_t id);
  146. void dymatic_change_device_id(uint32_t id);
  147. void dymatic_change_dst_device_id(uint32_t id);
  148. void uart_sleep_in_config();
  149. void uart_sleep_out_config();
  150. void timer_wake_uart_sleep_out_config();
  151. void lora_factory_config();
  152. void reset_lora(uint8_t new_channel);
  153. uint8_t set_lora(uint8_t new_channel,uint8_t eflag);
  154. uint8_t crc8( uint8_t *pData,
  155. uint16_t dataLen,
  156. uint8_t initialValue,
  157. uint8_t polynomial );
  158. #if 0
  159. bool lora_uart_is_normal(int Baud);
  160. bool lora_send_cmd(String cmd,String cmd_rsp);
  161. bool lora_set_Baud(int baud);
  162. bool lora_set_default();
  163. bool lora_set_work_freq(String freq);
  164. bool lora_set_send_power(int baud);
  165. bool lora_set_bw(int baud);
  166. bool lora_set_sf(int baud);
  167. bool lora_set_net_id(String id);
  168. bool lora_set_rssi(int rssi);
  169. #endif
  170. void print_lora(void);
  171. void print_lora_set_info(void);
  172. #endif
  173. #endif/*_LORA_H_*/