LORA.c 80 KB

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