task.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include "task.h"
  2. #include "cjson.h"
  3. #include "myFile.h"
  4. #include "gateway_message.h"
  5. #include "log.h"
  6. #include "malloc.h"
  7. #include "sx1276.h"
  8. #include "radio.h"
  9. #include "protocol.h"
  10. #include "usart.h"
  11. #include "node_data_acquisition.h"
  12. void master_task(uint8_t *string,uint16_t stringlength);
  13. void slave_task();
  14. tRadioDriver *Radio = NULL;
  15. uint16_t BufferSize;
  16. uint8_t Buffer[256];
  17. uint32_t rx_num = 0;
  18. uint8_t PingMsg[] = "PING\0";
  19. uint8_t PongMsg[] = "PONG\0";
  20. /*
  21. *********************************************************************************************************
  22. * 函 数 名: void data_task(void *pdata)
  23. * 功能说明: 主要是data_task处理线程,优先级高。其运行逻辑是将nandflash中的数据解析出来轮询发送数据
  24. * 形 参:无
  25. * 返 回 值: 无
  26. *********************************************************************************************************
  27. */
  28. void data_task(void *pdata)
  29. {
  30. OS_CPU_SR cpu_sr;
  31. pdata = pdata;
  32. //初始化lora
  33. Radio = RadioDriverInit();
  34. Radio->Init();
  35. dlt645_init(100);
  36. mmodbus_init(100);
  37. char *lora_config_json = mymalloc(SRAMEX, 9 * 1024);
  38. read_file("lora_json.txt", lora_config_json);
  39. addGatewayParams(lora_config_json);
  40. myfree(SRAMEX, lora_config_json);
  41. #ifdef MASTER
  42. GATEWAY_PARAMS *get;
  43. get= get_gateway_config_params();
  44. int nodeIndex=0;
  45. NODE_PARAMS *current_node=get->node_params;
  46. uint8_t string[256];
  47. uint16_t bufferLength;
  48. while (current_node!=NULL)
  49. {
  50. while(!masterSendNodeString(nodeIndex,string,&bufferLength)) //轮询读出
  51. {
  52. //测试代码不经过转发直接测试
  53. SlaveProtocolAnalysis(string,bufferLength);
  54. data_acquisition();
  55. uint8_t node_string[256];
  56. uint16_t node_string_Length;
  57. nodeSendReaddValue(node_string,&node_string_Length);
  58. GatewayProtocolAnalysis(node_string,node_string_Length);
  59. /*
  60. master_task(string,bufferLength); //进入发送流程
  61. int i=0;
  62. */
  63. }
  64. if(current_node->nextNode!=NULL)
  65. {
  66. current_node=current_node->nextNode;
  67. nodeIndex++;
  68. }
  69. else
  70. {
  71. nodeIndex=0;
  72. current_node=get->node_params;
  73. }
  74. }
  75. #else
  76. Radio->StartRx();
  77. while (1)
  78. {
  79. slave_task();
  80. OSTimeDlyHMSM(0, 0, 0, 200);
  81. }
  82. #endif
  83. while (1)
  84. {
  85. }
  86. // Radio = RadioDriverInit();
  87. // Radio->Init();
  88. // #ifdef MASTER
  89. // Radio->SetTxPacket(PingMsg, strlen((const char *)PingMsg));
  90. // printf("MASTER");
  91. // #else
  92. // Radio->StartRx();
  93. // printf("SLAVE");
  94. // #endif
  95. // while (1)
  96. // {
  97. // #ifdef MASTER
  98. // master_task("a");
  99. // #else
  100. // slave_task();
  101. // #endif
  102. // }
  103. }
  104. /*
  105. *********************************************************************************************************
  106. * 函 数 名: void master_task(char *string)
  107. * 功能说明: 主网关sx1278轮询发送调用接口,发送结束后就将状态切换到接收状态,等待从机的响应值,当从机超时或接收到数据进行后续数据处理的流程
  108. * 形 参:无
  109. * 返 回 值: 无
  110. *********************************************************************************************************
  111. */
  112. volatile uint32_t startTime; // 记录开始的时间且开始时间到结束时间超过一定范围则判定为超时状态,这时对其进行再次发送
  113. void master_task(uint8_t *string,uint16_t stringlength)
  114. {
  115. int retry_count=0; //发送计数,暂时不开启失败重发机制
  116. while (1)
  117. {
  118. switch (Radio->Process())
  119. {
  120. case RF_RX_DONE:
  121. Radio->GetRxPacket(Buffer, &BufferSize);
  122. break;
  123. case RF_TX_DONE:
  124. startTime = OSTimeGet();
  125. Radio->StartRx();
  126. case RF_BUSY:
  127. case RF_IDLE:
  128. if(retry_count==0) //第一次发送
  129. {
  130. Radio->SetTxPacket(string, stringlength);
  131. retry_count++;
  132. }
  133. if (OSTimeGet() - startTime > 10000) // 每次发送信号时记录上一次发送的时间如果超过一段时间没有响应则进行下一次发送
  134. {
  135. return;
  136. }
  137. default:
  138. OSTimeDlyHMSM(0, 0, 0, 200);
  139. break;
  140. }
  141. }
  142. }
  143. /*
  144. *********************************************************************************************************
  145. * 函 数 名: void slave_task(char *string)
  146. * 功能说明: 负责从站数据处理
  147. * 形 参:无
  148. * 返 回 值: 无
  149. *********************************************************************************************************
  150. */
  151. volatile bool rxflag = false;
  152. void slave_task()
  153. {
  154. switch (Radio->Process())
  155. {
  156. case RF_RX_DONE:
  157. Radio->GetRxPacket(Buffer, &BufferSize);
  158. if(SlaveProtocolAnalysis(Buffer,BufferSize)) //判断是否为该节点信息
  159. {
  160. data_acquisition();
  161. }
  162. Radio->StartRx();
  163. break;
  164. case RF_TX_DONE:
  165. Radio->StartRx();
  166. break;
  167. case RF_IDLE:
  168. case RF_BUSY:
  169. default:
  170. OSTimeDlyHMSM(0, 0, 0, 200);
  171. }
  172. }
  173. void processString(char *string)
  174. {
  175. }