LORA.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "LORA.h"
  4. #include "freertos/FreeRTOS.h"
  5. #include "freertos/task.h"
  6. #include "freertos/queue.h"
  7. #include "driver/uart.h"
  8. #include "esp_log.h"
  9. #include "y_ringbuf.h"
  10. #include <inttypes.h>
  11. #include "driver/gpio.h"
  12. #include "freertos/timers.h"
  13. #include "freertos/FreeRTOS.h"
  14. #include "freertos/task.h"
  15. #include "esp_check.h"
  16. #include "esp_sleep.h"
  17. #include "soc/uart_pins.h"
  18. #include "driver/uart.h"
  19. #include "driver/gpio.h"
  20. #include "driver/rtc_io.h"
  21. #include "sdkconfig.h"
  22. #include "../../main/user_sleep.h"
  23. #include "esp_sleep.h"
  24. #include "SPIFFS.h"
  25. QueueHandle_t lora_receiveQueue;
  26. SemaphoreHandle_t debug_Mutex;
  27. extern struct RINGBUF_st;
  28. RINGBUF_st *lora_ringbuf;
  29. uart_status_t lora_uart_status = uart_idle;
  30. // 声明一个TimerHandle_t类型的变量,用于存储定时器句柄
  31. TimerHandle_t lora_uart_Timerout;
  32. bool is_change_chanel = false;
  33. static const char *LOG_TAG = "LORA";
  34. uint8_t rawData_16[1024];
  35. extern QueueHandle_t lora_data_queue;
  36. // uint8_t rec_rssi;
  37. static const int LORA_BUF_SIZE = 1024;
  38. static bool half_packet_flag = false;
  39. static QueueHandle_t lora_queue;
  40. LORA_CFG_T lora_cfg_data;
  41. // uint8_t lora_rssi_data = 0;
  42. uint8_t lora_software_version = 0;
  43. bool is_uart_wake = false;
  44. static esp_err_t uart_wakeup_config(void);
  45. // #if USER_QIXIN //添加的初始化代码
  46. TaskHandle_t lora_uart_tx_handle;
  47. QueueHandle_t lora_uart_Queue; // 串口接受队列
  48. TimerHandle_t lora_cmd_Timerout; // 初始化命令超时函数
  49. QueueHandle_t lora_dealhandle; // 开始处理逻辑的数据
  50. // 沃进初始化命令配置
  51. lora_cmd_t lora_cmd[] =
  52. {
  53. #if 0
  54. { {0x03,0x26,0x01,0x8E},4,"",lora_cmd_success_Hander,lora_cmd_timerout_Hander},/*测试AT*/
  55. { {0x03,0x01,0x15,0x27},4,"",lora_cmd_success_Hander,lora_cmd_timerout_Hander},/*恢复默认设置*/
  56. { {0x03,0x26,0x00,0x89},4,"",lora_cmd_success_Hander,lora_cmd_timerout_Hander},/*设置工作频率*/
  57. //{ {0x03,0x26,0x01},"",lora_cmd_success_Hander,lora_cmd_timerout_Hander},/*设置波特率115200*/
  58. #else
  59. {.cmd_mode = 0x01, .lora_send_cmd_handler = lora_set_config_mode, .success_hanlder = lora_cmd_success_Hander, .timeout_hanlder = lora_cmd_timerout_Hander}, /*进入配置模式*/
  60. {.cmd_mode = LORA_CHANENL, .lora_send_cmd_handler = lora_set_channel, .success_hanlder = lora_cmd_success_Hander, .timeout_hanlder = lora_cmd_timerout_Hander}, /*设置信道模式*/
  61. {.cmd_mode = 0x06, .lora_send_cmd_handler = lora_set_bps, .success_hanlder = lora_cmd_success_Hander, .timeout_hanlder = lora_cmd_timerout_Hander}, /*设置波特率*/
  62. // 设置设备ID 地址 0xFFFFFFFF
  63. {.cmd_mode = 0x00000001, .lora_send_cmd_handler = lora_set_device_id, .success_hanlder = lora_cmd_success_Hander, .timeout_hanlder = lora_cmd_timerout_Hander},
  64. // 设置目标地址 0xFFFFFFFF 所有都可以接收到
  65. {.cmd_mode = 0xFFFFFFFF, .lora_send_cmd_handler = lora_set_dst_device_id, .success_hanlder = lora_cmd_success_Hander, .timeout_hanlder = lora_cmd_timerout_Hander},
  66. {.cmd_mode = 0x07, .lora_send_cmd_handler = lora_get_rssi, .success_hanlder = lora_cmd_success_Hander, .timeout_hanlder = lora_cmd_timerout_Hander}, /*rssi*/
  67. //{ .cmd_mode = 0x01, .lora_send_cmd_handler = lora_set_mode,.success_hanlder =lora_cmd_success_Hander,.timeout_hanlder=lora_cmd_timerout_Hander}, /*设置透传模式*/
  68. {.cmd_mode = 0x00, .lora_send_cmd_handler = lora_set_config_mode, .success_hanlder = lora_cmd_success_Hander, .timeout_hanlder = lora_cmd_timerout_Hander}, /*退出配置模式*/
  69. #endif
  70. };
  71. // lora_cmd_t lora_cmd_rssi[]=
  72. // {
  73. // #if 0
  74. // { {0x03,0x26,0x01,0x8E},4,"",lora_cmd_success_Hander,lora_cmd_timerout_Hander},/*测试AT*/
  75. // { {0x03,0x01,0x15,0x27},4,"",lora_cmd_success_Hander,lora_cmd_timerout_Hander},/*恢复默认设置*/
  76. // { {0x03,0x26,0x00,0x89},4,"",lora_cmd_success_Hander,lora_cmd_timerout_Hander},/*设置工作频率*/
  77. // //{ {0x03,0x26,0x01},"",lora_cmd_success_Hander,lora_cmd_timerout_Hander},/*设置波特率115200*/
  78. // #else
  79. // { .cmd_mode = 0x01, .lora_send_cmd_handler = lora_set_config_mode,.success_hanlder =lora_cmd_success_Hander,.timeout_hanlder=lora_cmd_timerout_Hander}, /*进入配置模式*/
  80. // { .cmd_mode = 0x07, .lora_send_cmd_handler = lora_get_rssi,.success_hanlder =lora_cmd_success_Hander,.timeout_hanlder=lora_cmd_timerout_Hander}, /*rssi*/
  81. // //{ .cmd_mode = 0x01, .lora_send_cmd_handler = lora_set_mode,.success_hanlder =lora_cmd_success_Hander,.timeout_hanlder=lora_cmd_timerout_Hander}, /*设置透传模式*/
  82. // { .cmd_mode = 0x00, .lora_send_cmd_handler = lora_set_config_mode,.success_hanlder =lora_cmd_success_Hander,.timeout_hanlder=lora_cmd_timerout_Hander}, /*退出配置模式*/
  83. // #endif
  84. // };
  85. // 初始化结构体
  86. Lora_t lora = {
  87. .lora_mode = Lora_Cmd_Mode,
  88. .lora_cmd = lora_cmd,
  89. .cmd_index = 0,
  90. .timerout = false,
  91. .cmd_type = lora_cmd_system_type,
  92. };
  93. /*********************************************************************************
  94. * function : lora_cmd_success_Hander
  95. * Description : lora AT命令配置回复成功
  96. * Input :
  97. * Output :
  98. * Author : 祁鑫 Data : 2023 7.18
  99. **********************************************************************************/
  100. void lora_cmd_success_Hander(unsigned char *cmd, unsigned char *cmd_rsp, int len)
  101. {
  102. ESP_LOGE(LOG_TAG, "%s,cmd = %s,cmd rsp =%s len = %d\r\n", __FUNCTION__, cmd, cmd_rsp, len);
  103. }
  104. /*********************************************************************************
  105. * function : lora_sendData
  106. * Description : lora AT命令配置回复超时或失败
  107. * Input :
  108. * Output :
  109. * Author : 祁鑫 Data : 2023 7.18
  110. **********************************************************************************/
  111. void lora_cmd_timerout_Hander(unsigned char *cmd, unsigned char *cmd_rsp, int len)
  112. {
  113. printf("%s,cmd = %s,cmd rsp =%s len = %d\r\n", __FUNCTION__, cmd, cmd_rsp, len);
  114. }
  115. /*********************************************************************************
  116. * function : lora_sendData
  117. * Description : lora 发送数据函数
  118. * Input :
  119. * Output :
  120. * Author : 祁鑫 Data : 2023 7.18
  121. **********************************************************************************/
  122. int lora_sendData(unsigned char *data, int len)
  123. {
  124. // const int len = strlen(data);
  125. int txBytes = 0;
  126. int offset = 0;
  127. #if 1
  128. #define MAX_LEN 64 // 50*2
  129. #define TIME_SET 50
  130. #else
  131. #define MAX_LEN 200
  132. #define TIME_SET 30
  133. #endif
  134. offset = 0;
  135. while (len > 0)
  136. {
  137. if (len > MAX_LEN)
  138. {
  139. #if 0
  140. send_lora_data(result+offset,MAX_LEN);
  141. #else
  142. txBytes = uart_write_bytes(LORA_UART, data + offset, MAX_LEN);
  143. vTaskDelay(TIME_SET / portTICK_PERIOD_MS);
  144. #endif
  145. offset = offset + MAX_LEN;
  146. len = len - MAX_LEN;
  147. }
  148. else
  149. {
  150. #if 0
  151. send_lora_data(result+offset,len);
  152. #else
  153. txBytes = uart_write_bytes(LORA_UART, data + offset, len);
  154. vTaskDelay(TIME_SET / portTICK_PERIOD_MS);
  155. #endif
  156. len = 0;
  157. // Serial.println("send end packet.");
  158. }
  159. }
  160. #if 0 // 可以发送的
  161. const int txBytes = uart_write_bytes(LORA_UART, data, len);
  162. #endif
  163. // printf("Write %d bytes\r\n", txBytes);
  164. return txBytes;
  165. }
  166. /*********************************************************************************
  167. * function : lora_send_index_init_cmd
  168. * Description : lora 发送初始化指定索引位置命令函数
  169. * Input :
  170. * Output :
  171. * Author : 祁鑫 Data : 2023 7.18
  172. **********************************************************************************/
  173. void lora_send_index_init_cmd(uint8_t index)
  174. {
  175. if (index >= (sizeof(lora_cmd) / sizeof(lora_cmd[0])))
  176. return;
  177. if (lora_cmd[index].lora_send_cmd_handler != NULL)
  178. {
  179. lora_cmd[index].lora_send_cmd_handler(lora_cmd[index].cmd_mode);
  180. }
  181. }
  182. int find_value(const unsigned char *hex_array, size_t array_size, unsigned char target_value)
  183. {
  184. for (size_t i = 0; i < array_size; i++)
  185. {
  186. if (hex_array[i] == target_value)
  187. {
  188. return (int)i;
  189. }
  190. }
  191. return -1;
  192. }
  193. /*********************************************************************************
  194. * function : lora_cmd_timerout_Callback
  195. * Description : lora AT命令配置回复超时定时器
  196. * Input :
  197. * Output :
  198. * Author : 祁鑫 Data : 2023 7.18
  199. **********************************************************************************/
  200. void lora_cmd_timerout_Callback(TimerHandle_t xTimer)
  201. {
  202. lora.timerout = true;
  203. printf("lora_cmd_timerout_Callback\r\n");
  204. }
  205. void lora_init_cmd_handler(unsigned char *data, void *param)
  206. {
  207. #if VJ_LORA_ENABLE
  208. uint16_t len = *(uint16_t *)param;
  209. if ((find_value(data, len, LORA_SUCCESS) != -1) || (find_value(data, len, 0x07) != -1))
  210. // if(strncmp((char*)data,(char *)lora_cmd[lora.cmd_index].cmd_rsp,strlen((char *)lora_cmd[lora.cmd_index].cmd_rsp)) == 0 )
  211. {
  212. // printf("cmd is ok %d\r\n",lora.cmd_index);
  213. if (lora.cmd_index == (sizeof(lora_cmd) / sizeof(lora_cmd[0]) - 1))
  214. {
  215. xTimerStop(lora_cmd_Timerout, 0);
  216. lora.lora_mode = Lora_UART_Mode;
  217. gpio_set_level(LORA_SET_PIN, 1);
  218. // if(is_change_chanel == true)
  219. // {
  220. // is_change_chanel = false;
  221. // }
  222. // uart_set_baudrate(LORA_UART,115200);
  223. printf("lora at all cmd init ok \r\n");
  224. #if 0
  225. extern void user_i2s_init(void);
  226. user_i2s_init();
  227. #else
  228. // ota_init();
  229. void lora_send_test_handler();
  230. // xTaskCreate(lora_send_test_handler, "lora_send_test_task", 1024*4, NULL, configMAX_PRIORITIES, NULL);
  231. #endif
  232. }
  233. else
  234. {
  235. xTimerStop(lora_cmd_Timerout, 0);
  236. lora.cmd_index += 1;
  237. #if 0
  238. lora_sendData((unsigned char*)lora_cmd[lora.cmd_index].cmd,4);
  239. #else
  240. lora_send_index_init_cmd(lora.cmd_index);
  241. #endif
  242. xTimerStart(lora_cmd_Timerout, 0);
  243. }
  244. }
  245. else if (find_value(data, len, LORA_FAIL) != -1)
  246. {
  247. printf("cmd is fail\r\n");
  248. if (lora.timerout == true)
  249. {
  250. lora.timerout = false;
  251. }
  252. if (lora.retry_times >= MAX_RETRY_NUM)
  253. {
  254. lora.retry_times = 0;
  255. // xTimerStop(lora_cmd_Timerout,0);
  256. printf("cmd is running retry_times max contiune next cmd\r\n");
  257. return;
  258. }
  259. lora.retry_times += 1;
  260. }
  261. #endif
  262. // xTimerStart(lora_cmd_Timerout, 30);
  263. // if(xTimerIsTimerActive(lora_cmd_Timerout) != pdFALSE)
  264. // {
  265. // xTimerReset(lora_cmd_Timerout,0);
  266. // //printf("receiving\r\n");
  267. // //printf("timer is running");
  268. // }
  269. }
  270. /*********************************************************************************
  271. * function : dymatic_change_chanel
  272. * Description : lora AT命令配置动态切换信道
  273. * Input :
  274. * Output :
  275. * Author : 祁鑫 Data : 2023 10.23
  276. **********************************************************************************/
  277. void dymatic_change_chanel(uint8_t chanel)
  278. {
  279. // ESP_LOGW(LOG_TAG,"切换chanel = %d ",chanel);
  280. lora.cmd_index = 0;
  281. lora.lora_mode = Lora_Cmd_Mode; // 设置进入命令模式
  282. lora_cmd[1].cmd_mode = chanel; // 设置信道
  283. lora_send_index_init_cmd(lora.cmd_index); //
  284. xTimerStart(lora_cmd_Timerout, 0);
  285. }
  286. /*********************************************************************************
  287. * function : dymatic_change_device_id
  288. * Description : lora AT命令配置动态修改设备ID
  289. * Input :
  290. * Output :
  291. * Author : 祁鑫 Data : 2023 10.23
  292. **********************************************************************************/
  293. void dymatic_change_device_id(uint32_t id)
  294. {
  295. for (int i = 0; i < sizeof(lora_cmd) / sizeof(lora_cmd[0]); i++)
  296. {
  297. if (lora_cmd[i].lora_send_cmd_handler == lora_set_device_id)
  298. {
  299. lora_cmd[i].cmd_mode = id;
  300. printf("cmd index %ld,index %d\r\n", id, i);
  301. break;
  302. }
  303. }
  304. }
  305. /*********************************************************************************
  306. * function : dymatic_change_dst_device_id
  307. * Description : lora AT命令配置动态修改目的地址
  308. * Input :
  309. * Output :
  310. * Author : 祁鑫 Data : 2023 10.23
  311. **********************************************************************************/
  312. void dymatic_change_dst_device_id(uint32_t id)
  313. {
  314. for (int i = 0; i < sizeof(lora_cmd) / sizeof(lora_cmd[0]); i++)
  315. {
  316. if (lora_cmd[i].lora_send_cmd_handler == lora_set_dst_device_id)
  317. {
  318. lora_cmd[i].cmd_mode = id;
  319. printf("cmd index %ld,index %d\r\n", id, i);
  320. break;
  321. }
  322. }
  323. }
  324. /*********************************************************************************
  325. * function : lora_set_device_id
  326. * Description : lora AT命令配置LORA设备ID
  327. * Input : 4个字节数据
  328. * Output :
  329. * Author : 祁鑫 Data : 2023 10.23
  330. **********************************************************************************/
  331. void lora_set_device_id(uint32_t id)
  332. {
  333. printf("%s id = %ld\r\n", __FUNCTION__, id);
  334. uint8_t temp = 0x00;
  335. uint32_t ID = id;
  336. #if 1
  337. uint8_t lorabuf[7];
  338. lorabuf[0] = 0x06;
  339. lorabuf[1] = 0x0D;
  340. #if 0
  341. lorabuf[2]=id&0xff;
  342. lorabuf[3]=(id>>8)&0xff;
  343. lorabuf[4]=(id>>16)&0xff;
  344. lorabuf[5]=(id>>24)&0xff;
  345. #else
  346. temp = (ID >> 0) & 0xff;
  347. lorabuf[2] = temp;
  348. temp = (ID >> 8) & 0xff;
  349. lorabuf[3] = temp;
  350. temp = (ID >> 16) & 0xff;
  351. lorabuf[4] = temp;
  352. temp = (ID >> 24) & 0xff;
  353. lorabuf[5] = temp;
  354. #endif
  355. lorabuf[6] = crc8(lorabuf, 6, 0x55, 0x07);
  356. lora_sendData(lorabuf, sizeof(lorabuf));
  357. #endif
  358. }
  359. /*********************************************************************************
  360. * function : lora_set_dst_device_id
  361. * Description : lora AT命令配置LORA目标设备ID
  362. * Input : 4个字节数据
  363. * Output :
  364. * Author : 祁鑫 Data : 2023 10.23
  365. **********************************************************************************/
  366. void lora_set_dst_device_id(uint32_t id)
  367. {
  368. printf("%s id = %ld\r\n", __FUNCTION__, id);
  369. #if 1
  370. uint8_t lorabuf[7];
  371. uint8_t temp = 0x00;
  372. uint32_t ID = id;
  373. lorabuf[0] = 0x06;
  374. lorabuf[1] = 0x0E;
  375. #if 0
  376. lorabuf[2]=id&0xff;
  377. lorabuf[3]=(id>>8)&0xff;
  378. lorabuf[4]=(id>>16)&0xff;
  379. lorabuf[5]=(id>>24)&0xff;
  380. #else
  381. temp = (ID >> 0) & 0xff;
  382. lorabuf[2] = temp;
  383. temp = (ID >> 8) & 0xff;
  384. lorabuf[3] = temp;
  385. temp = (ID >> 16) & 0xff;
  386. lorabuf[4] = temp;
  387. temp = (ID >> 24) & 0xff;
  388. lorabuf[5] = temp;
  389. #endif
  390. lorabuf[6] = crc8(lorabuf, 6, 0x55, 0x07);
  391. lora_sendData(lorabuf, sizeof(lorabuf));
  392. #endif
  393. }
  394. uint8_t set_lora(uint8_t new_channel, uint8_t eflag)
  395. {
  396. uint8_t ret = 0;
  397. uint8_t data[50] = {0};
  398. int user_size = 0;
  399. lora.lora_mode = Lora_Cmd_Mode;
  400. gpio_set_level(LORA_SET_PIN, 0);
  401. lora_set_config_mode(0x01);
  402. vTaskDelay(100 / portTICK_PERIOD_MS);
  403. user_size = y_ringbuf_get_used_size(lora_ringbuf);
  404. y_ringbuf_read_clear(lora_ringbuf, data, user_size);
  405. if (user_size != 0)
  406. {
  407. printf("rsp len = %d\r\n", user_size);
  408. for (int i = 0; i < user_size; i++)
  409. printf("%02x", data[i]);
  410. }
  411. lora_set_channel(new_channel);
  412. vTaskDelay(100 / portTICK_PERIOD_MS);
  413. user_size = y_ringbuf_get_used_size(lora_ringbuf);
  414. y_ringbuf_read_clear(lora_ringbuf, data, user_size);
  415. // while(1)
  416. // {
  417. // if((data[0] == 0x03)&&(data[1] == 0xee))
  418. // {
  419. // printf("error rsp fail len = %d\r\n",user_size);
  420. // lora_set_channel(new_channel);
  421. // vTaskDelay(100/ portTICK_PERIOD_MS);
  422. // user_size = y_ringbuf_get_used_size(lora_ringbuf);
  423. // y_ringbuf_read_clear(lora_ringbuf, data, user_size);
  424. // }
  425. // if((data[0] == 0x03)&&(data[1] == 0x55))
  426. // {
  427. // printf("set config ok\r\n");
  428. // break;
  429. // }
  430. if (user_size != 0)
  431. {
  432. printf("rsp len = %d\r\n", user_size);
  433. for (int i = 0; i < user_size; i++)
  434. printf("%02x", data[i]);
  435. }
  436. // vTaskDelay(100/ portTICK_PERIOD_MS);
  437. // }
  438. lora_set_bps(6);
  439. vTaskDelay(100 / portTICK_PERIOD_MS);
  440. user_size = y_ringbuf_get_used_size(lora_ringbuf);
  441. y_ringbuf_read_clear(lora_ringbuf, data, user_size);
  442. if (user_size != 0)
  443. {
  444. printf("rsp len = %d\r\n", user_size);
  445. for (int i = 0; i < user_size; i++)
  446. printf("%02x", data[i]);
  447. }
  448. lora_set_device_id(eflag + 1);
  449. vTaskDelay(100 / portTICK_PERIOD_MS);
  450. user_size = y_ringbuf_get_used_size(lora_ringbuf);
  451. y_ringbuf_read_clear(lora_ringbuf, data, user_size);
  452. if (user_size != 0)
  453. {
  454. printf("rsp len = %d\r\n", user_size);
  455. for (int i = 0; i < user_size; i++)
  456. printf("%02x", data[i]);
  457. }
  458. lora_set_dst_device_id(0x00000001);
  459. vTaskDelay(100 / portTICK_PERIOD_MS);
  460. user_size = y_ringbuf_get_used_size(lora_ringbuf);
  461. y_ringbuf_read_clear(lora_ringbuf, data, user_size);
  462. if (user_size != 0)
  463. {
  464. printf("rsp len = %d\r\n", user_size);
  465. for (int i = 0; i < user_size; i++)
  466. printf("%02x", data[i]);
  467. }
  468. lora_get_rssi(0x07);
  469. vTaskDelay(100 / portTICK_PERIOD_MS);
  470. user_size = y_ringbuf_get_used_size(lora_ringbuf);
  471. y_ringbuf_read_clear(lora_ringbuf, data, user_size);
  472. if (user_size != 0)
  473. {
  474. ret = data[2];
  475. printf("rssi = %d\r\n", ret);
  476. }
  477. lora_set_config_mode(0x00);
  478. vTaskDelay(100 / portTICK_PERIOD_MS);
  479. user_size = y_ringbuf_get_used_size(lora_ringbuf);
  480. y_ringbuf_read_clear(lora_ringbuf, data, user_size);
  481. if (user_size != 0)
  482. {
  483. printf("rsp len = %d\r\n", user_size);
  484. for (int i = 0; i < user_size; i++)
  485. printf("%02x", data[i]);
  486. }
  487. lora.lora_mode = Lora_UART_Mode;
  488. gpio_set_level(LORA_SET_PIN, 1);
  489. return ret;
  490. }
  491. void list_lora(void)
  492. {
  493. printf("%s\r\n", __FUNCTION__);
  494. #if 1
  495. uint8_t lorabuf[4];
  496. lorabuf[0] = 0x03;
  497. lorabuf[1] = 0x24;
  498. lorabuf[2] = 1;
  499. lorabuf[3] = crc8(lorabuf, 3, 0x55, 0x07);
  500. lora_sendData(lorabuf, sizeof(lorabuf));
  501. #endif
  502. }
  503. void print_lora(void)
  504. {
  505. printf("%s\r\n", __FUNCTION__);
  506. int user_size = 0;
  507. uint8_t data[50] = {0};
  508. lora.lora_mode = Lora_Cmd_Mode;
  509. gpio_set_level(LORA_SET_PIN, 0);
  510. lora_set_config_mode(0x01);
  511. vTaskDelay(100 / portTICK_PERIOD_MS);
  512. user_size = y_ringbuf_get_used_size(lora_ringbuf);
  513. y_ringbuf_read_clear(lora_ringbuf, data, user_size);
  514. list_lora();
  515. vTaskDelay(100 / portTICK_PERIOD_MS);
  516. user_size = y_ringbuf_get_used_size(lora_ringbuf);
  517. y_ringbuf_read_clear(lora_ringbuf, data, user_size);
  518. // if(user_size!=0)
  519. {
  520. printf("list_lora len = %d\r\n", user_size);
  521. for (int i = 0; i < user_size; i++)
  522. printf("%02x", data[i]);
  523. printf("\n\n");
  524. }
  525. lora_set_config_mode(0x00);
  526. vTaskDelay(100 / portTICK_PERIOD_MS);
  527. user_size = y_ringbuf_get_used_size(lora_ringbuf);
  528. y_ringbuf_read_clear(lora_ringbuf, data, user_size);
  529. lora.lora_mode = Lora_UART_Mode;
  530. gpio_set_level(LORA_SET_PIN, 1);
  531. }
  532. void reset_lora(uint8_t new_channel)
  533. {
  534. gpio_set_level(LORA_SET_PIN, 0);
  535. // for(int index = 0;index<sizeof(lora_cmd)/sizeof(lora_cmd[0]);index++)
  536. {
  537. uint8_t data[50] = {0};
  538. int user_size = 0;
  539. lora.lora_mode = Lora_Cmd_Mode;
  540. lora_set_config_mode(0x01);
  541. vTaskDelay(100 / portTICK_PERIOD_MS);
  542. user_size = y_ringbuf_get_used_size(lora_ringbuf);
  543. y_ringbuf_read_clear(lora_ringbuf, data, user_size);
  544. // if(user_size!=0)
  545. // {
  546. // printf("lora_set_config_mode len = %d\r\n",user_size);
  547. // for(int i = 0;i<user_size;i++)
  548. // printf("%02x",data[i]);
  549. // printf("\n\n");
  550. // }
  551. lora_set_channel(new_channel);
  552. vTaskDelay(100 / portTICK_PERIOD_MS);
  553. user_size = y_ringbuf_get_used_size(lora_ringbuf);
  554. y_ringbuf_read_clear(lora_ringbuf, data, user_size);
  555. if (data[1] == 0xee)
  556. {
  557. printf("->err\n\n\n");
  558. lora_set_channel(LORA_CHANENL);
  559. vTaskDelay(100 / portTICK_PERIOD_MS);
  560. user_size = y_ringbuf_get_used_size(lora_ringbuf);
  561. y_ringbuf_read_clear(lora_ringbuf, data, user_size);
  562. }
  563. if (user_size != 0)
  564. {
  565. printf("lora_set_channel len = %d\r\n", user_size);
  566. for (int i = 0; i < user_size; i++)
  567. printf("%02x", data[i]);
  568. printf("\n\n");
  569. }
  570. lora_set_bps(6);
  571. vTaskDelay(100 / portTICK_PERIOD_MS);
  572. user_size = y_ringbuf_get_used_size(lora_ringbuf);
  573. y_ringbuf_read_clear(lora_ringbuf, data, user_size);
  574. // if(user_size!=0)
  575. // {
  576. // printf("lora_set_bps len = %d\r\n",user_size);
  577. // for(int i = 0;i<user_size;i++)
  578. // printf("%02x",data[i]);
  579. // printf("\n\n");
  580. // }
  581. lora_set_device_id(0x00000001);
  582. vTaskDelay(100 / portTICK_PERIOD_MS);
  583. user_size = y_ringbuf_get_used_size(lora_ringbuf);
  584. y_ringbuf_read_clear(lora_ringbuf, data, user_size);
  585. // if(user_size!=0)
  586. // {
  587. // printf("lora_set_device_id len = %d\r\n",user_size);
  588. // for(int i = 0;i<user_size;i++)
  589. // printf("%02x",data[i]);
  590. // printf("\n\n");
  591. // }
  592. lora_set_dst_device_id(0xFFFFFFFF);
  593. vTaskDelay(100 / portTICK_PERIOD_MS);
  594. user_size = y_ringbuf_get_used_size(lora_ringbuf);
  595. y_ringbuf_read_clear(lora_ringbuf, data, user_size);
  596. // if(user_size!=0)
  597. // {
  598. // printf("lora_set_dst_device_id len = %d\r\n",user_size);
  599. // for(int i = 0;i<user_size;i++)
  600. // printf("%02x",data[i]);
  601. // printf("\n\n");
  602. // }
  603. // lora_get_rssi(0x07);
  604. // vTaskDelay(100 / portTICK_PERIOD_MS);
  605. // user_size = y_ringbuf_get_used_size(lora_ringbuf);
  606. // y_ringbuf_read_clear(lora_ringbuf, data, user_size);
  607. // if(user_size!=0)
  608. // {
  609. // printf("lora_get_rssi len = %d\r\n",user_size);
  610. // for(int i = 0;i<user_size;i++)
  611. // printf("%02x",data[i]);
  612. // printf("\n\n");
  613. // }
  614. lora_set_config_mode(0x00);
  615. vTaskDelay(100 / portTICK_PERIOD_MS);
  616. user_size = y_ringbuf_get_used_size(lora_ringbuf);
  617. y_ringbuf_read_clear(lora_ringbuf, data, user_size);
  618. // if(user_size!=0)
  619. // {
  620. // printf("lora_set_config_mode len = %d\r\n",user_size);
  621. // for(int i = 0;i<user_size;i++)
  622. // printf("%02x",data[i]);
  623. // printf("\n\n");
  624. // }
  625. lora.lora_mode = Lora_UART_Mode;
  626. gpio_set_level(LORA_SET_PIN, 1);
  627. // free(data);
  628. }
  629. }
  630. static void tx_task(void *arg)
  631. {
  632. // //Cmd_t cmd;
  633. // int index = 0;
  634. // unsigned char receivedMessage[30];
  635. // 发送lora初始化AT指令
  636. #if HPD_LORA_ENABLE
  637. lora_sendData((unsigned char *)lora_cmd[lora.cmd_index].cmd, strlen((char *)lora_cmd[lora.cmd_index].cmd));
  638. #endif
  639. #if VJ_LORA_ENABLE
  640. #if 1
  641. reset_lora(LORA_CHANENL);
  642. // lora_sendData((unsigned char*)lora_cmd[lora.cmd_index].cmd,4);
  643. #else
  644. lora_send_index_init_cmd(lora.cmd_index);
  645. #endif
  646. #endif
  647. // xTimerStart(lora_cmd_Timerout, 0);
  648. while (1)
  649. {
  650. vTaskDelay(1000 / portTICK_PERIOD_MS);
  651. }
  652. }
  653. static void rx_task(void *arg)
  654. {
  655. uart_event_t event;
  656. int length = 0, uart = 0;
  657. unsigned char receivedMessage[30];
  658. static const char *RX_TASK_TAG = "RX_TASK";
  659. esp_log_level_set(RX_TASK_TAG, ESP_LOG_INFO);
  660. uint8_t *data = (uint8_t *)malloc(RX_MAX_LENGTH + 1);
  661. int receive_len = 0;
  662. #if 1
  663. while (1)
  664. {
  665. if (xQueueReceive(lora_uart_Queue, (void *)&event, (TickType_t)portMAX_DELAY))
  666. {
  667. switch (event.type)
  668. {
  669. // Event of UART receving data
  670. /*We'd better handler data event fast, there would be much more data events than
  671. other types of events. If we take too much time on data event, the queue might
  672. be full.*/
  673. case UART_DATA:
  674. // xSemaphoreTake( debug_Mutex, portMAX_DELAY );
  675. if (event.size > 0)
  676. {
  677. // 启动定时器,第二个参数为定时器超时时间,设置为0表示立即启动
  678. if (lora_uart_status == uart_idle)
  679. {
  680. lora_uart_status = uart_receving;
  681. xTimerStart(lora_uart_Timerout, 0);
  682. // printf("first rev\r\n");
  683. }
  684. else if (lora_uart_status == uart_receving)
  685. {
  686. if (xTimerIsTimerActive(lora_uart_Timerout) != pdFALSE)
  687. {
  688. xTimerReset(lora_uart_Timerout, 0);
  689. // printf("receiving\r\n");
  690. // printf("timer is running");
  691. }
  692. else
  693. {
  694. // printf("timer is run ok");
  695. }
  696. // portTICK_PERIOD_MS
  697. if (event.size <= uart_fifo_size)
  698. {
  699. lora_uart_status = uart_receving;
  700. }
  701. }
  702. receive_len = event.size;
  703. int len = uart_read_bytes(LORA_UART, data, event.size, /*uart_timerout_times / portTICK_PERIOD_MS*/ portMAX_DELAY);
  704. receive_len = len;
  705. y_ringbuf_write(lora_ringbuf, data, receive_len);
  706. }
  707. // xSemaphoreGive( debug_Mutex );
  708. break;
  709. // Event of HW FIFO overflow detected
  710. case UART_FIFO_OVF:
  711. printf("UART_FIFO_OVF\r\n");
  712. // If fifo overflow happened, you should consider adding flow control for your application.
  713. // The ISR has already reset the rx FIFO,
  714. // As an example, we directly flush the rx buffer here in order to read more data.
  715. uart_flush_input(LORA_UART);
  716. xQueueReset(lora_uart_Queue);
  717. break;
  718. // Event of UART ring buffer full
  719. case UART_BUFFER_FULL:
  720. printf("UART_BUFFER_FULL\r\n");
  721. // If buffer full happened, you should consider encreasing your buffer size
  722. // As an example, we directly flush the rx buffer here in order to read more data.
  723. uart_flush_input(LORA_UART);
  724. xQueueReset(lora_uart_Queue);
  725. break;
  726. // Event of UART RX break detected
  727. case UART_BREAK:
  728. // printf("UART_BREAK\r\n");
  729. break;
  730. // Event of UART parity check error
  731. case UART_PARITY_ERR:
  732. printf("UART_PARITY_ERR\r\n");
  733. break;
  734. // Event of UART frame error
  735. case UART_FRAME_ERR:
  736. printf("UART_FRAME_ERR\r\n");
  737. break;
  738. // UART_PATTERN_DET
  739. case UART_PATTERN_DET:
  740. printf("UART_PATTERN_DET\r\n");
  741. break;
  742. // Event of waking up by UART
  743. case UART_WAKEUP:
  744. printf("uart wakeup\r\n");
  745. if (xTimerIsTimerActive(lora_uart_Timerout) != pdFALSE)
  746. {
  747. printf("uart timer run\r\n");
  748. xTimerReset(lora_uart_Timerout, 0);
  749. }
  750. else
  751. {
  752. printf("uart timer not run\r\n");
  753. }
  754. break;
  755. // Others
  756. default:
  757. printf("other error\r\n");
  758. break;
  759. }
  760. }
  761. }
  762. #endif
  763. free(data);
  764. // free(cmd);
  765. }
  766. void lora_receive_callback_handler()
  767. {
  768. int length = 0;
  769. uint8_t *data = (uint8_t *)malloc(buffer_size + 1);
  770. // int length = 0;
  771. // uint8_t* result = (uint8_t*) malloc(buffer_size+1);
  772. int user_size = 0;
  773. // #include "../../main/yc_protocol.h"
  774. // extern YC_DATA_T yc_data;
  775. while (1)
  776. {
  777. // 从队列中接收数据
  778. if (xQueueReceive(lora_receiveQueue, &length, portMAX_DELAY) == pdPASS)
  779. {
  780. user_size = y_ringbuf_get_used_size(lora_ringbuf);
  781. if (lora.lora_mode == Lora_Cmd_Mode)
  782. {
  783. // y_ringbuf_read_clear(lora_ringbuf, data, user_size);
  784. // printf("user_size = %d\r\n",user_size);
  785. // lora 命令模式处理
  786. // if(lora.cmd_type == lora_cmd_system_type)
  787. // {
  788. // for(int i = 0;i<user_size;i++)
  789. // {
  790. // printf("%02x,",data[i]);
  791. // if((data[i] == 0x03)&&(data[i+1] == 0x07))
  792. // {
  793. // //printf("-->rssi = %02x----,",data[i+2]);
  794. // if(data[i+2] >100)
  795. // rssi = 100;
  796. // else
  797. // rssi = data[i+2];
  798. // }
  799. // }
  800. // #if 1
  801. // lora_init_cmd_handler(data,&user_size);
  802. // #else
  803. // //发送回复显示
  804. // lora_sendData(data,user_size);
  805. // #endif
  806. // }
  807. }
  808. else if (lora.lora_mode == Lora_UART_Mode)
  809. {
  810. // //lora 透传模式处理
  811. // for(int i = 0;i<user_size;i++)
  812. // printf("_%02x",data[i]);
  813. // printf("\r\n");
  814. // printf("string =%s\r\n",data);
  815. // printf(" -> Lora_UART_Mode %.*s\n", user_size, data);
  816. // 发送消息处理
  817. #if 0
  818. xQueueSend(lora_dealhandle, &length, portMAX_DELAY);
  819. #else
  820. if (xQueueSend(lora_dealhandle, &length, portMAX_DELAY) == pdPASS)
  821. {
  822. // 消息发送成功
  823. // ESP_LOGW(LOG_TAG,"send ok");
  824. }
  825. else
  826. {
  827. // 消息发送失败,可能是消息队列已满
  828. ESP_LOGE(LOG_TAG, "send fail");
  829. }
  830. }
  831. }
  832. }
  833. free(data);
  834. }
  835. #if VJ_LORA_ENABLE
  836. /**沃进方案校验和计算法
  837. * @funtion:crc8多项式冗余校验
  838. * @param 1:pData,计算数据源地址
  839. * @param 2:dataLen,计算数据源长度
  840. * @param 3:initialValue,crc结果初值
  841. * @param 4:polynomial,多项式
  842. *
  843. * @return :校验结果
  844. */
  845. uint8_t crc8(uint8_t *pData,
  846. uint16_t dataLen,
  847. uint8_t initialValue,
  848. uint8_t polynomial)
  849. {
  850. uint8_t i;
  851. uint8_t crc;
  852. crc = initialValue;
  853. while (dataLen--)
  854. {
  855. crc ^= *pData++;
  856. for (i = 0; i < 8; i++)
  857. {
  858. if (crc & 0x80)
  859. {
  860. crc <<= 1; // shift left once
  861. crc ^= polynomial; // XOR with polynomial
  862. }
  863. else
  864. {
  865. crc <<= 1; // shift left once
  866. }
  867. }
  868. }
  869. return crc;
  870. }
  871. //=0,在配置模式下,退出配置模式,非配置模式下,认为是透传数据
  872. //=1,进入配置模式,此时可以配置其他命令
  873. void VJ_Lora_set_cfg_mode(uint8_t mode)
  874. {
  875. uint8_t cmd[4] = {0};
  876. cmd[0] = 0x03;
  877. cmd[1] = 0x26;
  878. cmd[2] = mode;
  879. cmd[3] = crc8(cmd, 3, 0x55, 0x07);
  880. printf("cmd[3] = %02x\r\n", cmd[3]);
  881. lora_sendData(cmd, sizeof(cmd));
  882. }
  883. /**沃进方案设置无线信道**********************************************
  884. 范围:0~31
  885. 具体对应的频率与无线频段范围和信道间隔带宽的设置相关。
  886. 比如信道间隔 1MHz,无线频段范围为 433MHz 频段时,
  887. =0,对应 433MHz
  888. =1,对应 434MHz
  889. =31,对应 464MHz
  890. 无线频段范围为 868MHz 频段时,
  891. =0,对应 868MHz
  892. =1,对应 869MHz
  893. =31,对应 899MHz
  894. 默认为 0 信道
  895. 设置立即生效,支持掉电保存
  896. ******************************************************************/
  897. int vjlora_set_Wireless_Channel(uint8_t Channel)
  898. {
  899. int ret = 0;
  900. VJ_Lora_set_cfg_mode(1);
  901. // ret=vjlora_set_by_serial(0x01,Channel);
  902. VJ_Lora_set_cfg_mode(0);
  903. return ret;
  904. }
  905. #endif
  906. #if 0
  907. /*********************************************************************************
  908. * function : lora_uart_is_normal
  909. * Description : 判断通信是否正常
  910. * Input : boatu
  911. * Output :
  912. * Author : 祁鑫 Data : 2023 7.10
  913. **********************************************************************************/
  914. bool lora_uart_is_normal(int Baud)
  915. {
  916. uint8_t i = 0, times = 0,length = 0;
  917. #if 1
  918. String cmd = "AH";
  919. String cmd_rsp = "OK";
  920. loraSerial.begin(Baud,SERIAL_8N1,LORA_RX,LORA_TX); // 启动串口通讯,波特率设置为115200,并配置对应端口 TX48 RX45
  921. bool is = lora_send_cmd(cmd,cmd_rsp);
  922. if(is)
  923. {
  924. return true;
  925. }else{
  926. return false;
  927. }
  928. #else
  929. loraSerial.begin(Baud,SERIAL_8N1,LORA_RX,LORA_TX); // 启动串口通讯,波特率设置为115200,并配置对应端口 TX48 RX45
  930. //loraSerial.println("AH");/* code */
  931. while (1)
  932. {
  933. times++;
  934. //while(loraSerial.read() >0){}
  935. //loraSerial.flush();
  936. loraSerial.println("AH");/* code */
  937. //delay(200);
  938. memset(receive_buf,0x00,sizeof(receive_buf));
  939. i=0;
  940. while (loraSerial.available()>0)
  941. {
  942. //if(loraSerial.available() == 4)
  943. {
  944. //for(i = 0;i<4;i++)
  945. receive_buf[i]= loraSerial.read();
  946. //i++;
  947. delay(100);
  948. i++;
  949. //break;
  950. }
  951. }
  952. #if 1
  953. Serial.println("receive data = ");
  954. for(int j = 0;j<20;j++)
  955. Serial.println(receive_buf[j],HEX);
  956. Serial.println("receive data ok");
  957. #endif
  958. if(receive_buf[0]=='O' && receive_buf[1]=='K')
  959. {
  960. //Serial.print("uart init ok\r\n");
  961. return true;
  962. break;
  963. }
  964. //if(receive_buf.find("OK"))
  965. // {
  966. // //Serial.print("uart init ok\r\n");
  967. // return true;
  968. // break;
  969. // }
  970. else {
  971. //Serial.print("uart init fail\r\n");
  972. if(times>AT_MAX_RETRY_TIMES)
  973. {
  974. return false;
  975. break;
  976. }
  977. delay(200);
  978. }
  979. }
  980. #endif
  981. }
  982. /*********************************************************************************
  983. * function : lora_send_cmd
  984. * Description : 发送AT字符串命令并判断返回字符串
  985. * Input :
  986. * Output :
  987. * Author : 祁鑫 Data : 2023 7.10
  988. **********************************************************************************/
  989. bool lora_send_cmd(String cmd,String cmd_rsp)
  990. {
  991. uint8_t i = 0, times = 0;
  992. bool is = false;
  993. String read_str;
  994. #if 1
  995. while (1)
  996. {
  997. times++;
  998. loraSerial.println(cmd);/* code */
  999. //delay(200);
  1000. memset(receive_buf,0x00,sizeof(receive_buf));
  1001. i=0;
  1002. while (loraSerial.available()>0)
  1003. {
  1004. Serial.println(loraSerial.available());
  1005. #if 1
  1006. delay(20);
  1007. read_str = loraSerial.readString();
  1008. #else
  1009. receive_buf[i] = loraSerial.read();
  1010. #endif
  1011. i++;
  1012. }
  1013. #if 0
  1014. Serial.println("receive data = ");
  1015. Serial.println(read_str);
  1016. //Serial.println(receive_buf[1],HEX);
  1017. #endif
  1018. //if(read_str.equals(cmd_rsp))
  1019. if(read_str.startsWith(cmd_rsp) || read_str.startsWith("OK"))
  1020. {
  1021. //Serial.println("cmd rsp is ok");
  1022. is = true;
  1023. break;
  1024. }else{
  1025. if(times>AT_MAX_RETRY_TIMES)
  1026. {
  1027. //Serial.println("cmd rsp is fail");
  1028. is = false;
  1029. break;
  1030. }
  1031. delay(20);
  1032. }
  1033. }
  1034. if(is)
  1035. {
  1036. return true;
  1037. }else
  1038. {
  1039. return false;
  1040. }
  1041. #endif
  1042. }
  1043. /*********************************************************************************
  1044. * function : lora_set_Baud
  1045. * Description : 设置波特率
  1046. * Input : baud 1-8
  1047. * Output : X 取值 1-8,可设置波特率为 1200bps、 2400bps、 4800bps、
  1048. 9600bps、 19200bps、 38400bps、 57600bps 和 115200bps。
  1049. 出厂默认为 9600bps。
  1050. * Author : 祁鑫 Data : 2023 7.10
  1051. **********************************************************************************/
  1052. bool lora_set_Baud(int baud)
  1053. {
  1054. //char baute[6] = {0};
  1055. bool is = false;
  1056. String baute = LORA_CMD_AH_BAUDRATE;
  1057. String baute_rsp = LORA_CMD_AH_BAUDRATE_RSP;
  1058. if(baud == 1){
  1059. baute.concat("1");
  1060. baute_rsp.concat("1");
  1061. is = lora_send_cmd(baute,baute_rsp);
  1062. }else if(baud == 2)
  1063. {
  1064. baute.concat("2");
  1065. baute_rsp.concat("2");
  1066. is = lora_send_cmd(baute,baute_rsp);
  1067. }else if(baud == 3)
  1068. {
  1069. baute.concat("3");
  1070. baute_rsp.concat("3");
  1071. is = lora_send_cmd(baute,baute_rsp);
  1072. }
  1073. else if(baud == 4)
  1074. {
  1075. baute.concat("4");
  1076. baute_rsp.concat("4");
  1077. is = lora_send_cmd(baute,baute_rsp);
  1078. }
  1079. else if(baud == 5)
  1080. {
  1081. baute.concat("5");
  1082. baute_rsp.concat("5");
  1083. is = lora_send_cmd(baute,baute_rsp);
  1084. }
  1085. else if(baud == 6)
  1086. {
  1087. baute.concat("6");
  1088. baute_rsp.concat("6");
  1089. is = lora_send_cmd(baute,baute_rsp);
  1090. }
  1091. else if(baud == 7)
  1092. {
  1093. baute.concat("7");
  1094. baute_rsp.concat("7");
  1095. is = lora_send_cmd(baute,baute_rsp);
  1096. }
  1097. else if(baud == 8)
  1098. {
  1099. baute.concat("8");
  1100. baute_rsp.concat("8");
  1101. is = lora_send_cmd(baute,baute_rsp);
  1102. }
  1103. if(is)
  1104. {
  1105. return true;
  1106. }else
  1107. {
  1108. return false;
  1109. }
  1110. #if 0
  1111. switch (baud)
  1112. {
  1113. case 1 :
  1114. baute.concat("1");
  1115. baute_rsp.concat("1");
  1116. is = lora_send_cmd(baute,baute_rsp);
  1117. break;
  1118. case 2 :
  1119. baute.concat("2");
  1120. baute_rsp.concat("2");
  1121. is = lora_send_cmd(baute,baute_rsp);
  1122. break;
  1123. case 3 :
  1124. baute.concat("3");
  1125. baute_rsp.concat("3");
  1126. is = lora_send_cmd(baute,baute_rsp);
  1127. break;
  1128. case 4 :
  1129. baute.concat("4");
  1130. baute_rsp.concat("4");
  1131. is = lora_send_cmd(baute,baute_rsp);
  1132. break;
  1133. case 5 :
  1134. baute.concat("5");
  1135. baute_rsp.concat("5");
  1136. is = lora_send_cmd(baute,baute_rsp);
  1137. break;
  1138. case 6 :
  1139. baute.concat("6");
  1140. baute_rsp.concat("6");
  1141. is = lora_send_cmd(baute,baute_rsp);
  1142. break;
  1143. case 7 :
  1144. baute.concat("7");
  1145. baute_rsp.concat("7");
  1146. is = lora_send_cmd(baute,baute_rsp);
  1147. break;
  1148. case 8 :
  1149. baute.concat("8");
  1150. baute_rsp.concat("8");
  1151. is = lora_send_cmd(baute,baute_rsp);
  1152. break;
  1153. default:
  1154. break;
  1155. }
  1156. #endif
  1157. }
  1158. /*********************************************************************************
  1159. * function : lora_set_default
  1160. * Description : 设置默认参数
  1161. * Input :
  1162. * Output :
  1163. 模组出厂设置参数为:
  1164. 串口波特率 9600bps, 1 停止位, 8 数据位,无奇偶校验;
  1165. 通信频率: 433.92MHz
  1166. 发射功率: P8( Max) =21dBm
  1167. LoRa 参数: SF=9、 BW=125KHz、 CR=4/5
  1168. 模组 ID: 0000
  1169. 当前数据包 RSSI 状态: OFF
  1170. * Author : 祁鑫 Data : 2023 7.18
  1171. **********************************************************************************/
  1172. bool lora_set_default()
  1173. {
  1174. bool is = false;
  1175. String cmd = "AH+D";
  1176. String cmd_rsp = "OK+DEFAULT";
  1177. is = lora_send_cmd(cmd,cmd_rsp);
  1178. if(is)
  1179. {
  1180. Serial.println("lora_set_default ok");
  1181. return true;
  1182. }else
  1183. {
  1184. Serial.println("lora_set_default fail");
  1185. return false;
  1186. }
  1187. }
  1188. /*********************************************************************************
  1189. * function : lora_set_work_freq
  1190. * Description : 更改射频中心频率(通信频率或工作频率)
  1191. * Input : baud 1-8
  1192. * Output :
  1193. * Author : 祁鑫 Data : 2023 7.18
  1194. **********************************************************************************/
  1195. bool lora_set_work_freq(String freq)
  1196. {
  1197. bool is = false;
  1198. String cmd = "AH+C";
  1199. String cmd_rsp = "OK+C";
  1200. cmd.concat(freq);
  1201. cmd_rsp.concat(freq);
  1202. is = lora_send_cmd(cmd,cmd_rsp);
  1203. if(is)
  1204. {
  1205. Serial.println("lora_set_work_freq ok");
  1206. return true;
  1207. }else
  1208. {
  1209. Serial.println("lora_set_work_freq fail");
  1210. return false;
  1211. }
  1212. }
  1213. /*********************************************************************************
  1214. * function : lora_set_send_power
  1215. * Description : 设置发射功率
  1216. * Input : baud 1-8
  1217. * Output : X 取值 1-8,出厂默认设置为 8,发射功率最大。发射功率等级设置
  1218. 为 1,发射功率最小。
  1219. X 值 1 2 3 4 5 6 7 8 对应模块发射功率 -1 2 5 8 11 14 17 21,
  1220. 发射功率单位 dBm
  1221. * Author : 祁鑫 Data : 2023 7.10
  1222. **********************************************************************************/
  1223. bool lora_set_send_power(int baud)
  1224. {
  1225. bool is = false;
  1226. String cmd = "AH+P";
  1227. String cmd_rsp = "OK+P";
  1228. if(baud == 1){
  1229. cmd.concat("1");
  1230. cmd_rsp.concat("1");
  1231. is = lora_send_cmd(cmd,cmd_rsp);
  1232. }else if(baud == 2)
  1233. {
  1234. cmd.concat("2");
  1235. cmd_rsp.concat("2");
  1236. is = lora_send_cmd(cmd,cmd_rsp);
  1237. }else if(baud == 3)
  1238. {
  1239. cmd.concat("3");
  1240. cmd_rsp.concat("3");
  1241. is = lora_send_cmd(cmd,cmd_rsp);
  1242. }
  1243. else if(baud == 4)
  1244. {
  1245. cmd.concat("4");
  1246. cmd_rsp.concat("4");
  1247. is = lora_send_cmd(cmd,cmd_rsp);
  1248. }
  1249. else if(baud == 5)
  1250. {
  1251. cmd.concat("5");
  1252. cmd_rsp.concat("5");
  1253. is = lora_send_cmd(cmd,cmd_rsp);
  1254. }
  1255. else if(baud == 6)
  1256. {
  1257. cmd.concat("6");
  1258. cmd_rsp.concat("6");
  1259. is = lora_send_cmd(cmd,cmd_rsp);
  1260. }
  1261. else if(baud == 7)
  1262. {
  1263. cmd.concat("7");
  1264. cmd_rsp.concat("7");
  1265. is = lora_send_cmd(cmd,cmd_rsp);
  1266. }
  1267. else if(baud == 8)
  1268. {
  1269. cmd.concat("8");
  1270. cmd_rsp.concat("8");
  1271. is = lora_send_cmd(cmd,cmd_rsp);
  1272. }
  1273. if(is)
  1274. {
  1275. return true;
  1276. }else
  1277. {
  1278. return false;
  1279. }
  1280. }
  1281. /*********************************************************************************
  1282. * function : lora_set_sf
  1283. * Description : 设置 Lora 扩频因子(SF)值
  1284. * Input : baud 1-8
  1285. * Output : X 取值 1-8, 分别对应 SF5、 SF6、 SF7、 SF8、 SF9、 SF10、 SF11、
  1286. SF12
  1287. * Author : 祁鑫 Data : 2023 7.10
  1288. **********************************************************************************/
  1289. bool lora_set_sf(int baud)
  1290. {
  1291. bool is = false;
  1292. String cmd = "AH+S";
  1293. String cmd_rsp = "OK+S";
  1294. if(baud == 1){
  1295. cmd.concat("1");
  1296. cmd_rsp.concat("1");
  1297. is = lora_send_cmd(cmd,cmd_rsp);
  1298. }else if(baud == 2)
  1299. {
  1300. cmd.concat("2");
  1301. cmd_rsp.concat("2");
  1302. is = lora_send_cmd(cmd,cmd_rsp);
  1303. }else if(baud == 3)
  1304. {
  1305. cmd.concat("3");
  1306. cmd_rsp.concat("3");
  1307. is = lora_send_cmd(cmd,cmd_rsp);
  1308. }
  1309. else if(baud == 4)
  1310. {
  1311. cmd.concat("4");
  1312. cmd_rsp.concat("4");
  1313. is = lora_send_cmd(cmd,cmd_rsp);
  1314. }
  1315. else if(baud == 5)
  1316. {
  1317. cmd.concat("5");
  1318. cmd_rsp.concat("5");
  1319. is = lora_send_cmd(cmd,cmd_rsp);
  1320. }
  1321. else if(baud == 6)
  1322. {
  1323. cmd.concat("6");
  1324. cmd_rsp.concat("6");
  1325. is = lora_send_cmd(cmd,cmd_rsp);
  1326. }
  1327. else if(baud == 7)
  1328. {
  1329. cmd.concat("7");
  1330. cmd_rsp.concat("7");
  1331. is = lora_send_cmd(cmd,cmd_rsp);
  1332. }
  1333. else if(baud == 8)
  1334. {
  1335. cmd.concat("8");
  1336. cmd_rsp.concat("8");
  1337. is = lora_send_cmd(cmd,cmd_rsp);
  1338. }
  1339. if(is)
  1340. {
  1341. return true;
  1342. }else
  1343. {
  1344. return false;
  1345. }
  1346. }
  1347. /*********************************************************************************
  1348. * function : lora_set_bw
  1349. * Description : 设置 Lora 带宽(BW)
  1350. * Input :
  1351. * Output : X 取值 0-9, 分别对应 7.81 kHz、 10.42 kHz、 15.63 kHz、 20.83
  1352. kHz、 31.25 kHz、 41.67 kHz、 62.50 kHz、 125 kHz、 250 kHz、 500
  1353. kHz。
  1354. * Author : 祁鑫 Data : 2023 7.10
  1355. **********************************************************************************/
  1356. bool lora_set_bw(int baud)
  1357. {
  1358. bool is = false;
  1359. String cmd = "AH+W";
  1360. String cmd_rsp = "OK+W";
  1361. if(baud == 1){
  1362. cmd.concat("1");
  1363. cmd_rsp.concat("1");
  1364. is = lora_send_cmd(cmd,cmd_rsp);
  1365. }else if(baud == 2)
  1366. {
  1367. cmd.concat("2");
  1368. cmd_rsp.concat("2");
  1369. is = lora_send_cmd(cmd,cmd_rsp);
  1370. }else if(baud == 3)
  1371. {
  1372. cmd.concat("3");
  1373. cmd_rsp.concat("3");
  1374. is = lora_send_cmd(cmd,cmd_rsp);
  1375. }
  1376. else if(baud == 4)
  1377. {
  1378. cmd.concat("4");
  1379. cmd_rsp.concat("4");
  1380. is = lora_send_cmd(cmd,cmd_rsp);
  1381. }
  1382. else if(baud == 5)
  1383. {
  1384. cmd.concat("5");
  1385. cmd_rsp.concat("5");
  1386. is = lora_send_cmd(cmd,cmd_rsp);
  1387. }
  1388. else if(baud == 6)
  1389. {
  1390. cmd.concat("6");
  1391. cmd_rsp.concat("6");
  1392. is = lora_send_cmd(cmd,cmd_rsp);
  1393. }
  1394. else if(baud == 7)
  1395. {
  1396. cmd.concat("7");
  1397. cmd_rsp.concat("7");
  1398. is = lora_send_cmd(cmd,cmd_rsp);
  1399. }
  1400. else if(baud == 8)
  1401. {
  1402. cmd.concat("8");
  1403. cmd_rsp.concat("8");
  1404. is = lora_send_cmd(cmd,cmd_rsp);
  1405. }
  1406. else if(baud == 9)
  1407. {
  1408. cmd.concat("9");
  1409. cmd_rsp.concat("9");
  1410. is = lora_send_cmd(cmd,cmd_rsp);
  1411. }
  1412. if(is)
  1413. {
  1414. return true;
  1415. }else
  1416. {
  1417. return false;
  1418. }
  1419. }
  1420. /*********************************************************************************
  1421. * function : lora_set_net_id
  1422. * Description : 设置模组网络 ID
  1423. * Input : X 为 0-9, 可以设置 0000-9999
  1424. * Output :
  1425. * Author : 祁鑫 Data : 2023 7.18
  1426. **********************************************************************************/
  1427. bool lora_set_net_id(String id)
  1428. {
  1429. bool is = false;
  1430. String cmd = "AH+I";
  1431. String cmd_rsp = "OK+I";
  1432. cmd.concat(id);
  1433. cmd_rsp.concat(id);
  1434. is = lora_send_cmd(cmd,cmd_rsp);
  1435. if(is)
  1436. {
  1437. Serial.println("lora_set_net_id ok");
  1438. return true;
  1439. }else
  1440. {
  1441. Serial.println("lora_set_net_id fail");
  1442. return false;
  1443. }
  1444. }
  1445. /*********************************************************************************
  1446. * function : lora_set_rssi
  1447. * Description : 设置当前数据包 RSSI 状态
  1448. * Input : X=0 RSSI 状态为 OFF, X=1 RSSI 状态为 0N
  1449. * Output :
  1450. * Author : 祁鑫 Data : 2023 7.18
  1451. **********************************************************************************/
  1452. bool lora_set_rssi(int rssi)
  1453. {
  1454. bool is = false;
  1455. String cmd = "AH+Q";
  1456. String cmd_rsp = "OK+Q";
  1457. if(rssi == 0){
  1458. cmd.concat("0");
  1459. cmd_rsp.concat("0");
  1460. is = lora_send_cmd(cmd,cmd_rsp);
  1461. }else if(rssi == 1)
  1462. {
  1463. cmd.concat("1");
  1464. cmd_rsp.concat("1");
  1465. is = lora_send_cmd(cmd,cmd_rsp);
  1466. }
  1467. if(is)
  1468. {
  1469. if(rssi == 0){
  1470. Serial.println("lora_set_rssi off ok");
  1471. }else if(rssi == 1){
  1472. Serial.println("lora_set_rssi on ok");
  1473. }
  1474. return true;
  1475. }else
  1476. {
  1477. if(rssi == 0){
  1478. Serial.println("lora_set_rssi off fail");
  1479. }else if(rssi == 1){
  1480. Serial.println("lora_set_rssi on fail");
  1481. }
  1482. return false;
  1483. }
  1484. }
  1485. #endif
  1486. void lora_set_config_mode(uint8_t mode)
  1487. {
  1488. // printf("%s\r\n",__FUNCTION__);
  1489. #if 1
  1490. uint8_t lorabuf[4];
  1491. lorabuf[0] = 0x03;
  1492. lorabuf[1] = 0x26;
  1493. lorabuf[2] = mode;
  1494. lorabuf[3] = crc8(lorabuf, 3, 0x55, 0x07);
  1495. lora_sendData(lorabuf, sizeof(lorabuf));
  1496. #endif
  1497. }
  1498. void lora_set_into_config_mode()
  1499. {
  1500. #if 1
  1501. uint8_t lorabuf[4];
  1502. lorabuf[0] = 0x03;
  1503. lorabuf[1] = 0x26;
  1504. lorabuf[2] = 0x01;
  1505. lorabuf[3] = crc8(lorabuf, 3, 0x55, 0x07);
  1506. lora_sendData(lorabuf, sizeof(lorabuf));
  1507. #endif
  1508. }
  1509. void lora_set_exit_config_mode()
  1510. {
  1511. #if 1
  1512. uint8_t lorabuf[4];
  1513. lorabuf[0] = 0x03;
  1514. lorabuf[1] = 0x00;
  1515. lorabuf[2] = 0x01;
  1516. lorabuf[3] = crc8(lorabuf, 3, 0x55, 0x07);
  1517. lora_sendData(lorabuf, sizeof(lorabuf));
  1518. #endif
  1519. }
  1520. void lora_set_channel(uint8_t channel)
  1521. {
  1522. ESP_LOGW(LOG_TAG, " ------>%s channel = %d\r\n", __FUNCTION__, channel);
  1523. #if 1
  1524. uint8_t lorabuf[4];
  1525. lorabuf[0] = 0x03;
  1526. lorabuf[1] = 0x01;
  1527. lorabuf[2] = channel;
  1528. lorabuf[3] = crc8(lorabuf, 3, 0x55, 0x07);
  1529. lora_sendData(lorabuf, sizeof(lorabuf));
  1530. #endif
  1531. }
  1532. /**设置无线波特率************************************************************
  1533. 范围:0~6
  1534. =0, 预留
  1535. =1, 预留
  1536. =2, 对应 1220bps(默认)
  1537. =3, 对应 2440bps
  1538. =4, 对应 5000bps
  1539. =5, 对应 12500bps
  1540. =6, 对应 37500bps
  1541. =其他,无效
  1542. 设置重启生效,支持掉电保存
  1543. 越低的无线波特率可以获得更
  1544. 远的通讯距离
  1545. ***************************************************************************/
  1546. void lora_set_bps(uint8_t bps)
  1547. {
  1548. // printf("%s\r\n",__FUNCTION__);
  1549. #if 1
  1550. uint8_t lorabuf[4];
  1551. lorabuf[0] = 0x03;
  1552. lorabuf[1] = 0x04;
  1553. lorabuf[2] = bps;
  1554. lorabuf[3] = crc8(lorabuf, 3, 0x55, 0x07);
  1555. lora_sendData(lorabuf, sizeof(lorabuf));
  1556. #endif
  1557. }
  1558. /*设置透传模式*/
  1559. void lora_set_mode(uint8_t mode)
  1560. {
  1561. printf("%s\r\n", __FUNCTION__);
  1562. #if 1
  1563. uint8_t lorabuf[4];
  1564. lorabuf[0] = 0x03;
  1565. lorabuf[1] = 0x08;
  1566. lorabuf[2] = mode;
  1567. lorabuf[3] = crc8(lorabuf, 3, 0x55, 0x07);
  1568. lora_sendData(lorabuf, sizeof(lorabuf));
  1569. #endif
  1570. }
  1571. /*出厂默认配置回复*/
  1572. void lora_factory_config()
  1573. {
  1574. printf("%s\r\n", __FUNCTION__);
  1575. #if 1
  1576. uint8_t lorabuf[4];
  1577. lorabuf[0] = 0x03;
  1578. lorabuf[1] = 0x21;
  1579. lorabuf[2] = 0x01;
  1580. lorabuf[3] = crc8(lorabuf, 3, 0x55, 0x07);
  1581. lora_sendData(lorabuf, sizeof(lorabuf));
  1582. #endif
  1583. }
  1584. void lora_software_reset()
  1585. {
  1586. #if 0
  1587. uint8_t lorabuf[8];
  1588. lorabuf[0]=0x03;
  1589. lorabuf[1]=0x22;
  1590. lorabuf[2]=0x01;
  1591. lorabuf[3]=crc8(lorabuf,3,0x55,0x07);
  1592. lorabuf[4]=0x00;
  1593. loraSerial.write((char *)lorabuf,4);
  1594. Serial.write((char *)lorabuf,4);
  1595. //delay(100);
  1596. while(1)
  1597. {
  1598. if(lora_packetComplete==true)
  1599. {
  1600. lora_packetComplete =false;
  1601. #if 0
  1602. printf("len =%d\r\n",lora_get_len);
  1603. for(int i = 0;i<lora_get_len;i++)
  1604. {
  1605. printf("%02x",Lora_Receivebuffer[i]);
  1606. }
  1607. printf("\r\n");
  1608. #endif
  1609. if((Lora_Receivebuffer[0] == 0x03 )&& (Lora_Receivebuffer[1] == 0x55 ))
  1610. {
  1611. lora_packetComplete =false;
  1612. lora_get_len =0x00;
  1613. //数据解析完成释放内存
  1614. heap_caps_free(Lora_Receivebuffer);
  1615. lora_max_buffer_size = MAX_BUFFER_SIZE;
  1616. return true;
  1617. }else if((Lora_Receivebuffer[0] == 0x03 )&& (Lora_Receivebuffer[1] == 0xEE ))
  1618. {
  1619. lora_packetComplete =false;
  1620. lora_get_len =0x00;
  1621. //数据解析完成释放内存
  1622. heap_caps_free(Lora_Receivebuffer);
  1623. lora_max_buffer_size = MAX_BUFFER_SIZE;
  1624. return false;
  1625. }
  1626. }
  1627. delay(100);
  1628. }
  1629. #endif
  1630. }
  1631. void lora_send_ack(uint8_t cmd, uint8_t *mac, int id_num, uint8_t *cmd_index, uint8_t status)
  1632. {
  1633. int index = 0;
  1634. int crc = 0;
  1635. uint8_t buffer[200] = {0};
  1636. buffer[0] = 0x55;
  1637. buffer[1] = 0x51;
  1638. buffer[2] = crc;
  1639. buffer[3] = mac[0];
  1640. buffer[4] = mac[1];
  1641. buffer[5] = mac[2];
  1642. buffer[6] = mac[3];
  1643. buffer[7] = mac[4];
  1644. buffer[8] = mac[5];
  1645. buffer[9] = id_num; // id
  1646. buffer[10] = cmd; // cmd
  1647. buffer[11] = cmd_index[0];
  1648. buffer[12] = cmd_index[1];
  1649. buffer[13] = 0x00;
  1650. buffer[14] = 0x01; // len
  1651. buffer[15] = status;
  1652. crc =
  1653. (buffer[0] + buffer[1] +
  1654. buffer[3] + buffer[4] +
  1655. buffer[5] + buffer[6] +
  1656. buffer[7] + buffer[8] +
  1657. buffer[9] + buffer[10] +
  1658. buffer[11] + buffer[12] +
  1659. buffer[13] + buffer[14] + buffer[15]) &
  1660. 0xff;
  1661. buffer[2] = crc;
  1662. lora_sendData((unsigned char *)buffer, 16);
  1663. }
  1664. ///////////////////////////////////////////////////////
  1665. #endif
  1666. void lora_uart_timerout_Callback(TimerHandle_t xTimer);
  1667. void uart_sleep_in_config()
  1668. {
  1669. uart_driver_delete(UART_NUM_1);
  1670. }
  1671. void uart_sleep_out_config()
  1672. {
  1673. ESP_LOGW(LOG_TAG, "uart1 sleep out");
  1674. #if VJ_LORA_ENABLE
  1675. const uart_config_t uart_config = {
  1676. .baud_rate = 115200,
  1677. .data_bits = UART_DATA_8_BITS,
  1678. .parity = UART_PARITY_DISABLE,
  1679. .stop_bits = UART_STOP_BITS_1,
  1680. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  1681. .source_clk = UART_SCLK_DEFAULT,
  1682. };
  1683. #endif
  1684. // We won't use a buffer for sending data.
  1685. esp_err_t err = ESP_OK;
  1686. if (!uart_is_driver_installed(LORA_UART))
  1687. {
  1688. // printf("uart not init\r\n");
  1689. err = uart_driver_install(LORA_UART, RX_MAX_LENGTH * 2, 0, 300, &lora_uart_Queue, 0);
  1690. if (err != ESP_OK)
  1691. {
  1692. printf("uart driver install fail err code %x\r\n", err);
  1693. }
  1694. }
  1695. err = uart_param_config(LORA_UART, &uart_config);
  1696. if (err != ESP_OK)
  1697. {
  1698. printf("uart_param_config fail err code %x\r\n", err);
  1699. }
  1700. err = uart_set_pin(LORA_UART, LORA_TXD_PIN, LORA_RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
  1701. if (err != ESP_OK)
  1702. {
  1703. printf("uart_set_pin fail err code %x\r\n", err);
  1704. }
  1705. uart_flush(LORA_UART);
  1706. }
  1707. void timer_wake_uart_sleep_out_config()
  1708. {
  1709. gpio_reset_pin(LORA_TXD_PIN);
  1710. gpio_reset_pin(LORA_RXD_PIN);
  1711. #if VJ_LORA_ENABLE
  1712. const uart_config_t uart_config = {
  1713. .baud_rate = 115200,
  1714. .data_bits = UART_DATA_8_BITS,
  1715. .parity = UART_PARITY_DISABLE,
  1716. .stop_bits = UART_STOP_BITS_1,
  1717. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  1718. .source_clk = UART_SCLK_DEFAULT,
  1719. };
  1720. #endif
  1721. // We won't use a buffer for sending data.
  1722. esp_err_t err = ESP_OK;
  1723. err = uart_driver_install(LORA_UART, RX_MAX_LENGTH * 2, 0, 300, &lora_uart_Queue, 0);
  1724. if (err != ESP_OK)
  1725. {
  1726. printf("uart driver install fail err code %x\r\n", err);
  1727. }
  1728. err = uart_param_config(LORA_UART, &uart_config);
  1729. if (err != ESP_OK)
  1730. {
  1731. printf("uart_param_config fail err code %x\r\n", err);
  1732. }
  1733. err = uart_set_pin(LORA_UART, LORA_TXD_PIN, LORA_RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
  1734. if (err != ESP_OK)
  1735. {
  1736. printf("uart_set_pin fail err code %x\r\n", err);
  1737. }
  1738. uart_flush(LORA_UART);
  1739. // xTaskCreate(rx_task, "lora_uart_rx_task", 1024*2, NULL, configMAX_PRIORITIES, NULL);
  1740. }
  1741. void lora_init(void)
  1742. {
  1743. #if !USER_QIXIN
  1744. // lora_uart_pin_init();
  1745. // //lora_get_cfg();
  1746. // lora_get_rssi();
  1747. // lora_set_Wireless_Channel(lora_channel);
  1748. // lora_set_Wireless_bps(6);
  1749. // //lora_set_auto_ack(0);
  1750. // //创建ringbuffer
  1751. // lora_ringbuf = y_ringbuf_create(ringbuffer_size);
  1752. // if (lora_ringbuf == NULL)
  1753. // {
  1754. // printf("lora_ringbuf init fail");
  1755. // }
  1756. #else
  1757. #if 0
  1758. //配置lora电源控制引脚
  1759. gpio_config_t lora_power_pin_cfg = {};
  1760. lora_power_pin_cfg.intr_type = GPIO_INTR_DISABLE;
  1761. lora_power_pin_cfg.mode = GPIO_MODE_OUTPUT;
  1762. lora_power_pin_cfg.pin_bit_mask =(1ULL<<LORA_POWER_PIN);
  1763. lora_power_pin_cfg.pull_down_en = 0;
  1764. lora_power_pin_cfg.pull_up_en = 0;
  1765. gpio_config(&lora_power_pin_cfg);
  1766. lora_set_power_level(1);
  1767. #endif
  1768. #if USER_DEEP_SLEEP_ENABLE
  1769. gpio_reset_pin(LORA_POWER_PIN);
  1770. // 配置调试引脚为输入模式
  1771. gpio_config_t io_conf;
  1772. io_conf.intr_type = GPIO_INTR_DISABLE; // 禁用中断触发
  1773. io_conf.mode = GPIO_MODE_OUTPUT; // 配置为输入模式
  1774. io_conf.pin_bit_mask = (1ULL << LORA_POWER_PIN);
  1775. io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE; // 可根据需要启用或禁用下拉电阻
  1776. io_conf.pull_up_en = GPIO_PULLUP_DISABLE; // 可根据需要启用或禁用上拉电阻
  1777. gpio_config(&io_conf);
  1778. gpio_set_level(LORA_POWER_PIN, 1);
  1779. #endif
  1780. #if USER_LIGHT_SLEEP_ENABLE
  1781. #if 1
  1782. rtc_gpio_init(LORA_POWER_PIN);
  1783. rtc_gpio_set_direction(LORA_POWER_PIN, RTC_GPIO_MODE_OUTPUT_ONLY);
  1784. rtc_gpio_pullup_dis(LORA_POWER_PIN);
  1785. rtc_gpio_pulldown_dis(LORA_POWER_PIN);
  1786. rtc_gpio_set_level(LORA_POWER_PIN, 1);
  1787. // rtc_gpio_hold_en(LORA_POWER_PIN);
  1788. #else
  1789. // 配置调试引脚为输入模式
  1790. gpio_config_t io_conf;
  1791. io_conf.intr_type = GPIO_INTR_DISABLE; // 禁用中断触发
  1792. io_conf.mode = GPIO_MODE_OUTPUT; // 配置为输入模式
  1793. io_conf.pin_bit_mask = (1ULL << LORA_POWER_PIN);
  1794. io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE; // 可根据需要启用或禁用下拉电阻
  1795. io_conf.pull_up_en = GPIO_PULLUP_DISABLE; // 可根据需要启用或禁用上拉电阻
  1796. gpio_config(&io_conf);
  1797. gpio_set_level(LORA_POWER_PIN, 1);
  1798. #endif
  1799. #endif
  1800. /* Set the GPIO as a push/pull output */
  1801. // gpio_set_direction(LORA_POWER_PIN, GPIO_MODE_OUTPUT);
  1802. gpio_set_direction(LORA_SET_PIN, GPIO_MODE_OUTPUT);
  1803. #if 0
  1804. gpio_set_level(LORA_SET_PIN, 0);
  1805. #else
  1806. gpio_set_level(LORA_SET_PIN, 1);
  1807. #endif
  1808. #if HPD_LORA_ENABLE
  1809. const uart_config_t uart_config = {
  1810. .baud_rate = 9600,
  1811. .data_bits = UART_DATA_8_BITS,
  1812. .parity = UART_PARITY_DISABLE,
  1813. .stop_bits = UART_STOP_BITS_1,
  1814. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  1815. .source_clk = UART_SCLK_DEFAULT,
  1816. };
  1817. #endif
  1818. #if VJ_LORA_ENABLE
  1819. const uart_config_t uart_config = {
  1820. .baud_rate = 115200,
  1821. .data_bits = UART_DATA_8_BITS,
  1822. .parity = UART_PARITY_DISABLE,
  1823. .stop_bits = UART_STOP_BITS_1,
  1824. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  1825. .source_clk = UART_SCLK_DEFAULT,
  1826. };
  1827. #endif
  1828. // We won't use a buffer for sending data.
  1829. uart_driver_install(LORA_UART, RX_MAX_LENGTH * 2, 0, 300, &lora_uart_Queue, 0);
  1830. uart_param_config(LORA_UART, &uart_config);
  1831. uart_set_pin(LORA_UART, LORA_TXD_PIN, LORA_RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
  1832. uart_flush(LORA_UART);
  1833. // uart_disable_rx_intr(LORA_UART);
  1834. // uart_disable_tx_intr(LORA_UART);
  1835. // 创建队列
  1836. // lora_sendQueue = xQueueCreate(10, sizeof(100));
  1837. lora_receiveQueue = xQueueCreate(10, sizeof(100));
  1838. #if 1
  1839. // 创建串口超时定时器
  1840. lora_uart_Timerout = xTimerCreate(
  1841. "lora_uart_Timer", // 定时器名称(可以为NULL)
  1842. pdMS_TO_TICKS(uart_timerout_times), // 定时器超时时间(以毫秒为单位)
  1843. pdFALSE, // 定时器为周期性(pdTRUE)还是单次(pdFALSE)
  1844. 0, // 定时器ID(可以为0)
  1845. lora_uart_timerout_Callback // 定时器回调函数
  1846. );
  1847. // 创建命令回复超时定时器
  1848. lora_cmd_Timerout = xTimerCreate(
  1849. "lora_cmd_Timer", // 定时器名称(可以为NULL)
  1850. pdMS_TO_TICKS(1000), // 定时器超时时间(以毫秒为单位)
  1851. pdFALSE, // 定时器为周期性(pdTRUE)还是单次(pdFALSE)
  1852. 0, // 定时器ID(可以为0)
  1853. lora_cmd_timerout_Callback // 定时器回调函数
  1854. );
  1855. lora_ringbuf = y_ringbuf_create(buffer_size);
  1856. if (lora_ringbuf == NULL)
  1857. {
  1858. printf("lora_ringbuf init fail");
  1859. }
  1860. #endif
  1861. #if 0
  1862. lora_set_config_mode(0x01);
  1863. vTaskDelay(1000 / portTICK_PERIOD_MS);
  1864. lora_factory_config();
  1865. vTaskDelay(5000 / portTICK_PERIOD_MS);
  1866. #else
  1867. #endif
  1868. xTaskCreate(rx_task, "lora_uart_rx_task", 1024 * 2, NULL, configMAX_PRIORITIES, NULL);
  1869. #if USER_NOT_SLEEP_ENABLE || USER_LIGHT_SLEEP_ENABLE || USER_DEEP_SLEEP_ENABLE
  1870. int reson = is_wake_up_reson(); // 返回唤醒的原因
  1871. printf("wake up lora cmd init , reson = %d\r\n", reson); // 软件复位是2,硬件复位是0
  1872. if ((reson != ESP_SLEEP_WAKEUP_ULP) && (reson != ESP_SLEEP_WAKEUP_TIMER) /*&& (reson != ESP_SLEEP_WAKEUP_EXT0)*/) // ota完后软件复位,重新配置lora
  1873. {
  1874. extern Machine_info_t Machine_info;
  1875. if (Machine_info.lora_new_channel == LORA_CHANENL)
  1876. {
  1877. printf("lora cmd init start\r\n");
  1878. xTaskCreate(tx_task, "uart_tx_task", 1024 * 3, NULL, configMAX_PRIORITIES - 1, &lora_uart_tx_handle);
  1879. }
  1880. else
  1881. {
  1882. lora.lora_mode = Lora_UART_Mode; // 复位直接进入透传模式
  1883. ESP_LOGE(LOG_TAG, "not set LORA_CHANENL");
  1884. }
  1885. }
  1886. else
  1887. {
  1888. ESP_LOGE(LOG_TAG, "not set LORA_CHANENL");
  1889. lora.lora_mode = Lora_UART_Mode; // 复位直接进入透传模式
  1890. }
  1891. #endif
  1892. xTaskCreate(lora_receive_callback_handler, "lora_at_cmd_task", 1024 * 4, NULL, configMAX_PRIORITIES, NULL);
  1893. // 配置串口唤醒
  1894. #if 0
  1895. uart_wakeup_config();
  1896. #else
  1897. // 修复唤醒是串口会多一个
  1898. // gpio_sleep_set_direction(LORA_RXD_PIN, GPIO_MODE_OUTPUT);
  1899. // gpio_sleep_set_pull_mode(LORA_RXD_PIN, GPIO_FLOATING);
  1900. // gpio_sleep_set_direction(LORA_TXD_PIN, GPIO_MODE_OUTPUT);
  1901. // gpio_sleep_set_pull_mode(LORA_TXD_PIN, GPIO_FLOATING /*!< Pad pull down */
  1902. // );
  1903. // gpio_sleep_set_direction(LORA_RXD_PIN, GPIO_MODE_INPUT);
  1904. // gpio_sleep_set_pull_mode(LORA_RXD_PIN, GPIO_PULLUP_ONLY /*!< Pad pull down */
  1905. // );
  1906. // uart_wakeup_config();
  1907. // //禁止串口唤醒
  1908. // esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_UART);
  1909. #endif
  1910. // vTaskDelay(1000 / portTICK_PERIOD_MS);
  1911. #if 0
  1912. xTaskCreate(lora_send_callback_handler, "lora_send_cmd_task", 1024*4, NULL, configMAX_PRIORITIES, NULL);
  1913. #endif
  1914. #if 0
  1915. VJ_Lora_set_cfg_mode(1);
  1916. VJ_Lora_set_cfg_mode(0);
  1917. uint8_t crc8( uint8_t *pData,
  1918. uint16_t dataLen,
  1919. uint8_t initialValue,
  1920. uint8_t polynomial );
  1921. uint8_t cmd[4]={0};
  1922. cmd[0]=0x03;
  1923. cmd[1]=0x01;
  1924. cmd[2]=0x15;
  1925. cmd[3]=crc8(cmd,3,0x55,0x07);
  1926. printf("cmd[3] = %02x\r\n",cmd[3]);
  1927. #endif
  1928. printf("lora init\r\n");
  1929. if ((reson != ESP_SLEEP_WAKEUP_ULP) && (reson != ESP_SLEEP_WAKEUP_TIMER))
  1930. {
  1931. vTaskDelay(2000 / portTICK_PERIOD_MS);
  1932. }
  1933. #endif
  1934. }
  1935. #define EXAMPLE_UART_WAKEUP_THRESHOLD 3
  1936. static esp_err_t uart_wakeup_config(void)
  1937. {
  1938. /* UART will wakeup the chip up from light sleep if the edges that RX pin received has reached the threshold
  1939. * Besides, the Rx pin need extra configuration to enable it can work during light sleep */
  1940. gpio_sleep_set_direction(LORA_RXD_PIN, GPIO_MODE_INPUT);
  1941. gpio_sleep_set_pull_mode(LORA_RXD_PIN, GPIO_PULLUP_ONLY);
  1942. uart_set_wakeup_threshold(UART_NUM_1, EXAMPLE_UART_WAKEUP_THRESHOLD);
  1943. /* Only uart0 and uart1 (if has) support to be configured as wakeup source */
  1944. esp_sleep_enable_uart_wakeup(UART_NUM_1);
  1945. return ESP_OK;
  1946. }
  1947. void lora_uart_pin_init(void)
  1948. {
  1949. const uart_config_t lora_uart_config = {
  1950. .baud_rate = 115200,
  1951. .data_bits = UART_DATA_8_BITS,
  1952. .parity = UART_PARITY_DISABLE,
  1953. .stop_bits = UART_STOP_BITS_1,
  1954. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  1955. .source_clk = UART_SCLK_DEFAULT,
  1956. };
  1957. uart_driver_install(UART_NUM_1, LORA_BUF_SIZE * 2, LORA_BUF_SIZE * 2, 20, &lora_queue, 0);
  1958. uart_param_config(UART_NUM_1, &lora_uart_config);
  1959. uart_set_pin(UART_NUM_1, LORA_TXD_PIN, LORA_RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
  1960. #if 0
  1961. gpio_config_t lora_power_pin_cfg = {};
  1962. lora_power_pin_cfg.intr_type = GPIO_INTR_DISABLE;
  1963. lora_power_pin_cfg.mode = GPIO_MODE_OUTPUT;
  1964. lora_power_pin_cfg.pin_bit_mask =(1ULL<<LORA_POWER_PIN);
  1965. lora_power_pin_cfg.pull_down_en = 0;
  1966. lora_power_pin_cfg.pull_up_en = 0;
  1967. gpio_config(&lora_power_pin_cfg);
  1968. lora_set_power_level(1);
  1969. #else
  1970. rtc_gpio_init(LORA_POWER_PIN);
  1971. rtc_gpio_set_direction(LORA_POWER_PIN, RTC_GPIO_MODE_OUTPUT_ONLY);
  1972. rtc_gpio_set_level(LORA_POWER_PIN, 1);
  1973. #endif
  1974. gpio_config_t lora_busy_pin_cfg = {};
  1975. lora_busy_pin_cfg.intr_type = GPIO_INTR_DISABLE;
  1976. lora_busy_pin_cfg.mode = GPIO_MODE_INPUT;
  1977. lora_busy_pin_cfg.pin_bit_mask = (1ULL << LORA_BUSY_PIN);
  1978. lora_busy_pin_cfg.pull_down_en = 0;
  1979. lora_busy_pin_cfg.pull_up_en = 0;
  1980. gpio_config(&lora_busy_pin_cfg);
  1981. // 创建串口超时定时器
  1982. lora_uart_Timerout = xTimerCreate(
  1983. "lora_uart_Timer", // 定时器名称(可以为NULL)
  1984. pdMS_TO_TICKS(uart_timerout_times), // 定时器超时时间(以毫秒为单位)
  1985. pdFALSE, // 定时器为周期性(pdTRUE)还是单次(pdFALSE)
  1986. 0, // 定时器ID(可以为0)
  1987. lora_uart_timerout_Callback // 定时器回调函数
  1988. );
  1989. // 配置串口唤醒
  1990. uart_wakeup_config();
  1991. }
  1992. int get_lora_busy_pin()
  1993. {
  1994. return gpio_get_level(LORA_BUSY_PIN);
  1995. }
  1996. void lora_set_power_level(int level)
  1997. {
  1998. ESP_LOGW(LOG_TAG, "set lora [%s]", level ? "poweron" : "poweroff");
  1999. #if 0
  2000. gpio_set_level(LORA_POWER_PIN,level);
  2001. #else
  2002. rtc_gpio_set_level(LORA_POWER_PIN, level);
  2003. #endif
  2004. }
  2005. void lora_uart_timerout_Callback(TimerHandle_t xTimer)
  2006. {
  2007. // 在这里执行定时器超时后的任务
  2008. // 注意:回调函数中不要执行耗时的操作,避免影响其他任务的运行
  2009. int length = 0;
  2010. if (lora_uart_status == uart_idle)
  2011. {
  2012. return;
  2013. }
  2014. else if (lora_uart_status == uart_receving)
  2015. {
  2016. // printf("uart_receving-----%s----\r\n",__FUNCTION__);
  2017. xQueueSend(lora_receiveQueue, &length, portMAX_DELAY);
  2018. lora_uart_status = uart_idle;
  2019. }
  2020. else if (lora_uart_status == uart_receive_complete)
  2021. {
  2022. // printf("uart_receive_complete-----%s----\r\n",__FUNCTION__);
  2023. lora_uart_status = uart_idle;
  2024. }
  2025. }
  2026. static void uart_event_task(void *pvParameters)
  2027. {
  2028. uart_event_t event;
  2029. size_t buffered_size;
  2030. uint8_t *dtmp = (uint8_t *)malloc(LORA_BUF_SIZE);
  2031. uint8_t *half_buf = NULL;
  2032. int receive_len = 0;
  2033. for (;;)
  2034. {
  2035. // Waiting for UART event.
  2036. if (xQueueReceive(lora_queue, (void *)&event, (TickType_t)portMAX_DELAY))
  2037. {
  2038. bzero(dtmp, LORA_BUF_SIZE);
  2039. ESP_LOGD(LOG_TAG, "LORA event:");
  2040. switch (event.type)
  2041. {
  2042. // Event of UART receving data
  2043. /*We'd better handler data event fast, there would be much more data events than
  2044. other types of events. If we take too much time on data event, the queue might
  2045. be full.*/
  2046. case UART_DATA:
  2047. printf("uart data len = %d\r\n", event.size);
  2048. if (event.size > 0)
  2049. {
  2050. // 启动定时器,第二个参数为定时器超时时间,设置为0表示立即启动
  2051. if (lora_uart_status == uart_idle)
  2052. {
  2053. lora_uart_status = uart_receving;
  2054. xTimerStart(lora_uart_Timerout, 0);
  2055. // printf("first rev\r\n");
  2056. }
  2057. else if (lora_uart_status == uart_receving)
  2058. {
  2059. if (xTimerIsTimerActive(lora_uart_Timerout) != pdFALSE)
  2060. {
  2061. xTimerReset(lora_uart_Timerout, 0);
  2062. // printf("receiving\r\n");
  2063. // printf("timer is running");
  2064. }
  2065. else
  2066. {
  2067. // printf("timer is run ok");
  2068. }
  2069. // portTICK_PERIOD_MS
  2070. if (event.size <= uart_fifo_size)
  2071. {
  2072. lora_uart_status = uart_receving;
  2073. }
  2074. }
  2075. receive_len = event.size;
  2076. int len = uart_read_bytes(UART_NUM_1, dtmp, event.size, /*uart_timerout_times / portTICK_PERIOD_MS*/ portMAX_DELAY);
  2077. for (int i = 0; i < event.size; i++)
  2078. printf("-%02X", dtmp[i]);
  2079. printf("\r\n");
  2080. receive_len = len;
  2081. y_ringbuf_write_remalloc_memory(lora_ringbuf, dtmp, receive_len);
  2082. }
  2083. // lora_set_receive(dtmp,event.size);
  2084. // lora_analytic_data(dtmp,event.size);
  2085. // rx_to_rawData(dtmp,rawData_16);
  2086. // ESP_LOGI(LOG_TAG, "[uart_rx] = %x",(int)dtmp);
  2087. // uart_write_bytes(UART_NUM_1, (const char*) dtmp, event.size);
  2088. break;
  2089. // Event of HW FIFO overflow detected
  2090. case UART_FIFO_OVF:
  2091. ESP_LOGI(LOG_TAG, "hw fifo overflow");
  2092. // If fifo overflow happened, you should consider adding flow control for your application.
  2093. // The ISR has already reset the rx FIFO,
  2094. // As an example, we directly flush the rx buffer here in order to read more data.
  2095. uart_flush_input(UART_NUM_1);
  2096. xQueueReset(lora_queue);
  2097. break;
  2098. // Event of UART ring buffer full
  2099. case UART_BUFFER_FULL:
  2100. ESP_LOGI(LOG_TAG, "ring buffer full");
  2101. // If buffer full happened, you should consider increasing your buffer size
  2102. // As an example, we directly flush the rx buffer here in order to read more data.
  2103. uart_flush_input(UART_NUM_1);
  2104. xQueueReset(lora_queue);
  2105. break;
  2106. // Event of UART RX break detected
  2107. case UART_BREAK:
  2108. ESP_LOGI(LOG_TAG, "uart rx break");
  2109. break;
  2110. // Event of UART parity check error
  2111. case UART_PARITY_ERR:
  2112. ESP_LOGI(LOG_TAG, "uart parity error");
  2113. break;
  2114. // Event of UART frame error
  2115. case UART_FRAME_ERR:
  2116. ESP_LOGI(LOG_TAG, "uart frame error");
  2117. break;
  2118. // UART_PATTERN_DET
  2119. case UART_PATTERN_DET:
  2120. uart_get_buffered_data_len(UART_NUM_1, &buffered_size);
  2121. int pos = uart_pattern_pop_pos(UART_NUM_1);
  2122. ESP_LOGI(LOG_TAG, "[UART PATTERN DETECTED] pos: %d, buffered size: %d", pos, buffered_size);
  2123. if (pos == -1)
  2124. {
  2125. // There used to be a UART_PATTERN_DET event, but the pattern position queue is full so that it can not
  2126. // record the position. We should set a larger queue size.
  2127. // As an example, we directly flush the rx buffer here.
  2128. uart_flush_input(UART_NUM_1);
  2129. }
  2130. else
  2131. {
  2132. uart_read_bytes(UART_NUM_1, dtmp, pos, 100 / portTICK_PERIOD_MS);
  2133. // uint8_t pat[PATTERN_CHR_NUM + 1];
  2134. uint8_t pat[1024 + 1];
  2135. memset(pat, 0, sizeof(pat));
  2136. uart_read_bytes(UART_NUM_1, pat, 1024, 100 / portTICK_PERIOD_MS);
  2137. ESP_LOGI(LOG_TAG, "read data: %s", dtmp);
  2138. ESP_LOGI(LOG_TAG, "read pat : %s", pat);
  2139. }
  2140. break;
  2141. // Event of waking up by UART
  2142. case UART_WAKEUP:
  2143. printf("uart wakeup\r\n");
  2144. if (xTimerIsTimerActive(lora_uart_Timerout) != pdFALSE)
  2145. {
  2146. printf("uart timer run\r\n");
  2147. xTimerReset(lora_uart_Timerout, 0);
  2148. }
  2149. else
  2150. {
  2151. printf("uart timer not run\r\n");
  2152. }
  2153. break;
  2154. // Others
  2155. default:
  2156. printf("uart event type: %d", event.type);
  2157. break;
  2158. }
  2159. }
  2160. // else{vTaskDelay(100 / portTICK_PERIOD_MS);}
  2161. }
  2162. free(dtmp);
  2163. dtmp = NULL;
  2164. vTaskDelete(NULL);
  2165. ESP_LOGE(LOG_TAG, "vTaskDelete(NULL)");
  2166. }
  2167. void lora_event_task(void *pvParameters)
  2168. {
  2169. uart_event_task(pvParameters);
  2170. }
  2171. /**沃进方案校验和计算法
  2172. * @funtion:crc8多项式冗余校验
  2173. * @param 1:pData,计算数据源地址
  2174. * @param 2:dataLen,计算数据源长度
  2175. * @param 3:initialValue,crc结果初值
  2176. * @param 4:polynomial,多项式
  2177. *
  2178. * @return :校验结果
  2179. */
  2180. uint8_t lora_crc8(uint8_t *pData,
  2181. uint16_t dataLen,
  2182. uint8_t initialValue,
  2183. uint8_t polynomial)
  2184. {
  2185. uint8_t i;
  2186. uint8_t crc;
  2187. crc = initialValue;
  2188. while (dataLen--)
  2189. {
  2190. crc ^= *pData++;
  2191. for (i = 0; i < 8; i++)
  2192. {
  2193. if (crc & 0x80)
  2194. {
  2195. crc <<= 1; // shift left once
  2196. crc ^= polynomial; // XOR with polynomial
  2197. }
  2198. else
  2199. {
  2200. crc <<= 1; // shift left once
  2201. }
  2202. }
  2203. }
  2204. return crc;
  2205. }
  2206. int lora_set_by_serial(uint8_t cmd, uint8_t value)
  2207. {
  2208. uint8_t lora_tx_buf[5];
  2209. lora_tx_buf[0] = 0x03;
  2210. lora_tx_buf[1] = cmd;
  2211. lora_tx_buf[2] = value;
  2212. lora_tx_buf[3] = lora_crc8(lora_tx_buf, 3, 0x55, 0x07);
  2213. lora_tx_buf[4] = 0x00;
  2214. return uart_write_bytes(UART_NUM_1, (const char *)lora_tx_buf, 4);
  2215. }
  2216. int lora_set_cfg_mode(uint8_t mode)
  2217. {
  2218. return lora_set_by_serial(0x26, mode);
  2219. }
  2220. int lora_reset_software(void)
  2221. {
  2222. int ret;
  2223. lora_set_cfg_mode(1);
  2224. vTaskDelay(10 / portTICK_PERIOD_MS);
  2225. ret = lora_set_by_serial(0x22, 1);
  2226. vTaskDelay(10 / portTICK_PERIOD_MS);
  2227. lora_set_cfg_mode(0);
  2228. vTaskDelay(10 / portTICK_PERIOD_MS);
  2229. return ret;
  2230. }
  2231. int lora_set_factory(void)
  2232. {
  2233. int ret;
  2234. lora_set_cfg_mode(1);
  2235. vTaskDelay(10 / portTICK_PERIOD_MS);
  2236. ret = lora_set_by_serial(0x21, 1);
  2237. vTaskDelay(10 / portTICK_PERIOD_MS);
  2238. lora_set_cfg_mode(0);
  2239. vTaskDelay(10 / portTICK_PERIOD_MS);
  2240. return ret;
  2241. }
  2242. int lora_set_wireless_channel_interval_bandwidth(uint8_t bandwidth)
  2243. {
  2244. int ret;
  2245. lora_set_cfg_mode(1);
  2246. vTaskDelay(10 / portTICK_PERIOD_MS);
  2247. ret = lora_set_by_serial(0x0B, bandwidth);
  2248. vTaskDelay(10 / portTICK_PERIOD_MS);
  2249. lora_set_cfg_mode(0);
  2250. vTaskDelay(10 / portTICK_PERIOD_MS);
  2251. return ret;
  2252. }
  2253. int lora_set_data_outdir(uint8_t dir)
  2254. {
  2255. int ret;
  2256. lora_set_cfg_mode(1);
  2257. vTaskDelay(10 / portTICK_PERIOD_MS);
  2258. ret = lora_set_by_serial(0x0a, dir);
  2259. vTaskDelay(10 / portTICK_PERIOD_MS);
  2260. lora_set_cfg_mode(0);
  2261. vTaskDelay(10 / portTICK_PERIOD_MS);
  2262. return ret;
  2263. }
  2264. int lora_set_auto_ack(uint8_t ack)
  2265. {
  2266. int ret;
  2267. lora_set_cfg_mode(1);
  2268. vTaskDelay(10 / portTICK_PERIOD_MS);
  2269. ret = lora_set_by_serial(0x09, ack);
  2270. vTaskDelay(10 / portTICK_PERIOD_MS);
  2271. lora_set_cfg_mode(0);
  2272. vTaskDelay(10 / portTICK_PERIOD_MS);
  2273. return ret;
  2274. }
  2275. int lora_set_packet_fmt(uint8_t fmt)
  2276. {
  2277. int ret;
  2278. lora_set_cfg_mode(1);
  2279. vTaskDelay(10 / portTICK_PERIOD_MS);
  2280. ret = lora_set_by_serial(0x08, fmt);
  2281. vTaskDelay(10 / portTICK_PERIOD_MS);
  2282. lora_set_cfg_mode(0);
  2283. vTaskDelay(10 / portTICK_PERIOD_MS);
  2284. return ret;
  2285. }
  2286. int lora_set_Wireless_Channel(uint8_t Channel)
  2287. {
  2288. int ret;
  2289. lora_set_cfg_mode(1);
  2290. vTaskDelay(10 / portTICK_PERIOD_MS);
  2291. ret = lora_set_by_serial(0x01, Channel);
  2292. vTaskDelay(10 / portTICK_PERIOD_MS);
  2293. lora_set_cfg_mode(0);
  2294. vTaskDelay(10 / portTICK_PERIOD_MS);
  2295. return ret;
  2296. }
  2297. int lora_set_tx_power(uint8_t power)
  2298. {
  2299. int ret;
  2300. lora_set_cfg_mode(1);
  2301. vTaskDelay(10 / portTICK_PERIOD_MS);
  2302. ret = lora_set_by_serial(0x03, power);
  2303. vTaskDelay(10 / portTICK_PERIOD_MS);
  2304. lora_set_cfg_mode(0);
  2305. vTaskDelay(10 / portTICK_PERIOD_MS);
  2306. return ret;
  2307. }
  2308. int lora_set_Wireless_bps(uint8_t bps)
  2309. {
  2310. int ret;
  2311. lora_set_cfg_mode(1);
  2312. vTaskDelay(10 / portTICK_PERIOD_MS);
  2313. ret = lora_set_by_serial(0x04, bps);
  2314. vTaskDelay(10 / portTICK_PERIOD_MS);
  2315. lora_set_cfg_mode(0);
  2316. vTaskDelay(10 / portTICK_PERIOD_MS);
  2317. return ret;
  2318. }
  2319. int lora_set_serial_bps(uint8_t bps)
  2320. {
  2321. int ret;
  2322. lora_set_cfg_mode(1);
  2323. vTaskDelay(10 / portTICK_PERIOD_MS);
  2324. ret = lora_set_by_serial(0x05, bps);
  2325. vTaskDelay(10 / portTICK_PERIOD_MS);
  2326. lora_set_cfg_mode(0);
  2327. vTaskDelay(10 / portTICK_PERIOD_MS);
  2328. return ret;
  2329. }
  2330. int lora_get_rssi(uint32_t rssi)
  2331. {
  2332. #if 0
  2333. int ret;
  2334. lora_set_cfg_mode(1);
  2335. vTaskDelay(10 / portTICK_PERIOD_MS);
  2336. ret=lora_set_by_serial(0x07,1);
  2337. vTaskDelay(10 / portTICK_PERIOD_MS);
  2338. lora_set_cfg_mode(0);
  2339. vTaskDelay(10 / portTICK_PERIOD_MS);
  2340. return ret;
  2341. #else
  2342. #if 1
  2343. uint8_t lorabuf[4];
  2344. lorabuf[0] = 0x03;
  2345. lorabuf[1] = rssi;
  2346. lorabuf[2] = 0x01;
  2347. lorabuf[3] = crc8(lorabuf, 3, 0x55, 0x07);
  2348. lora_sendData(lorabuf, sizeof(lorabuf));
  2349. #endif
  2350. return 0;
  2351. #endif
  2352. }
  2353. int lora_get_version(void)
  2354. {
  2355. int ret;
  2356. lora_set_cfg_mode(1);
  2357. vTaskDelay(10 / portTICK_PERIOD_MS);
  2358. ret = lora_set_by_serial(0x25, 1);
  2359. vTaskDelay(10 / portTICK_PERIOD_MS);
  2360. lora_set_cfg_mode(0);
  2361. vTaskDelay(10 / portTICK_PERIOD_MS);
  2362. return ret;
  2363. }
  2364. int lora_get_cfg(void)
  2365. {
  2366. printf("cfg\n");
  2367. int ret;
  2368. lora_set_cfg_mode(1);
  2369. vTaskDelay(10 / portTICK_PERIOD_MS);
  2370. ret = lora_set_by_serial(0x24, 1);
  2371. vTaskDelay(10 / portTICK_PERIOD_MS);
  2372. lora_set_cfg_mode(0);
  2373. vTaskDelay(10 / portTICK_PERIOD_MS);
  2374. return ret;
  2375. }
  2376. int lora_send_data(const char *lora_data, int lora_data_len)
  2377. {
  2378. return uart_write_bytes(UART_NUM_1, (const char *)lora_data, lora_data_len);
  2379. }
  2380. void print_lora_set_info(void)
  2381. {
  2382. ESP_LOGE(LOG_TAG, "lora_uart_Timerout %s", xTimerIsTimerActive(lora_uart_Timerout) ? "Active" : "not Active");
  2383. ESP_LOGE(LOG_TAG, "lora_uart_status = %d", lora_uart_status);
  2384. }