task.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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,BufferSize);
  54. data_acquisition();
  55. /*
  56. master_task(string,bufferLength); //进入发送流程
  57. int i=0;
  58. */
  59. }
  60. if(current_node->nextNode!=NULL)
  61. {
  62. current_node=current_node->nextNode;
  63. nodeIndex++;
  64. }
  65. else
  66. {
  67. nodeIndex=0;
  68. current_node=get->node_params;
  69. }
  70. }
  71. #else
  72. Radio->StartRx();
  73. while (1)
  74. {
  75. slave_task();
  76. OSTimeDlyHMSM(0, 0, 0, 200);
  77. }
  78. #endif
  79. while (1)
  80. {
  81. }
  82. // Radio = RadioDriverInit();
  83. // Radio->Init();
  84. // #ifdef MASTER
  85. // Radio->SetTxPacket(PingMsg, strlen((const char *)PingMsg));
  86. // printf("MASTER");
  87. // #else
  88. // Radio->StartRx();
  89. // printf("SLAVE");
  90. // #endif
  91. // while (1)
  92. // {
  93. // #ifdef MASTER
  94. // master_task("a");
  95. // #else
  96. // slave_task();
  97. // #endif
  98. // }
  99. }
  100. /*
  101. *********************************************************************************************************
  102. * 函 数 名: void master_task(char *string)
  103. * 功能说明: 主网关sx1278轮询发送调用接口,发送结束后就将状态切换到接收状态,等待从机的响应值,当从机超时或接收到数据进行后续数据处理的流程
  104. * 形 参:无
  105. * 返 回 值: 无
  106. *********************************************************************************************************
  107. */
  108. volatile uint32_t startTime; // 记录开始的时间且开始时间到结束时间超过一定范围则判定为超时状态,这时对其进行再次发送
  109. void master_task(uint8_t *string,uint16_t stringlength)
  110. {
  111. int retry_count=0; //发送计数,暂时不开启失败重发机制
  112. while (1)
  113. {
  114. switch (Radio->Process())
  115. {
  116. case RF_RX_DONE:
  117. Radio->GetRxPacket(Buffer, &BufferSize);
  118. break;
  119. case RF_TX_DONE:
  120. startTime = OSTimeGet();
  121. Radio->StartRx();
  122. case RF_BUSY:
  123. case RF_IDLE:
  124. if(retry_count==0) //第一次发送
  125. {
  126. Radio->SetTxPacket(string, stringlength);
  127. retry_count++;
  128. }
  129. if (OSTimeGet() - startTime > 10000) // 每次发送信号时记录上一次发送的时间如果超过一段时间没有响应则进行下一次发送
  130. {
  131. return;
  132. }
  133. default:
  134. OSTimeDlyHMSM(0, 0, 0, 200);
  135. break;
  136. }
  137. }
  138. }
  139. /*
  140. *********************************************************************************************************
  141. * 函 数 名: void slave_task(char *string)
  142. * 功能说明: 负责从站数据处理
  143. * 形 参:无
  144. * 返 回 值: 无
  145. *********************************************************************************************************
  146. */
  147. volatile bool rxflag = false;
  148. void slave_task()
  149. {
  150. switch (Radio->Process())
  151. {
  152. case RF_RX_DONE:
  153. Radio->GetRxPacket(Buffer, &BufferSize);
  154. if(SlaveProtocolAnalysis(Buffer,BufferSize)) //判断是否为该节点信息
  155. {
  156. data_acquisition();
  157. }
  158. Radio->StartRx();
  159. break;
  160. case RF_TX_DONE:
  161. Radio->StartRx();
  162. break;
  163. case RF_IDLE:
  164. case RF_BUSY:
  165. default:
  166. OSTimeDlyHMSM(0, 0, 0, 200);
  167. }
  168. }
  169. void processString(char *string)
  170. {
  171. }