LORA.c 83 KB

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