main.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #include "main.h"
  2. #include "sram.h"
  3. #include "nandflash.h"
  4. #include "usart.h"
  5. #include "malloc.h"
  6. #include "demo_nand_fatfs.h"
  7. #include "cjson.h"
  8. #include "delay.h"
  9. #include "string.h"
  10. #include "includes.h"
  11. #include "lwip/timers.h"
  12. #include "led.h"
  13. #include "sys_tcpdemo.h"
  14. #include "sys_mqtt.h"
  15. #include "sys_http.h"
  16. #include "task.h"
  17. #include "sys_sx1278.h"
  18. #include "node_data_acquisition.h"
  19. #include "log.h"
  20. #include "tcp_server.h"
  21. #include "lte.h"
  22. #define UNIQUE_ID 0x1fff7a10
  23. /*关于任务优先级分配(上限为12个、需要增加请修改上限)
  24. * 0: 保留
  25. * 1: 用于ucos的软件定时器
  26. * 2: 保留
  27. * 3: 用于 以太网的网卡数据包读取 ETHERNETIF_THREAD_PRO
  28. * 4: LWIP 核心线程 TCPIP_THREAD_PRIO
  29. * 5: 用于 ota升级
  30. * 6: 用于 MQTT数据发送线程 APP_TASK_MQTTMAIN_PRIO
  31. * 7: 用于 MQTT数据接收线程 APP_TASK_MQTTRECEIVE_PRIO
  32. * 8: 用于 sx1278发送/接收处理数据 DATA_TASK_PRIO
  33. * 9: 用于 本机状态led显示 / 现用于tcpServer_init TCP_TASK_PRIO /
  34. * 10:用于网络状态led显示
  35. * 11:用于轮询读出本机保存信息发送到sx1278发送接收线程
  36. * 12:用于管理所有线程
  37. */
  38. ///////////////////////////////////////
  39. #define PERIOD_TASK_PRIO 2
  40. #define PERIOD_STK_SIZE 2048
  41. OS_STK PERIOD_TASK_STK[PERIOD_STK_SIZE];
  42. void period_taskFuntcion(void *arg);
  43. //////////////////////////////////////////////
  44. //LED
  45. //LED优先级
  46. #define LED_TASK_PRIO 28
  47. //LED栈空间大小
  48. #define LED_STK_SIZE 128
  49. //任务堆栈
  50. OS_STK LED_TASK_STK[LED_STK_SIZE];
  51. //led运行任务
  52. void led_taskFuntcion(void *pdata);
  53. ////////////////////////////////////////////////////////
  54. /*---------------------------本地数据线程堆栈----------------------------------------------------------------*/
  55. __align(8) OS_STK DATA_TASK_STK[DATA_STK_SIZE];
  56. /*---------------------------lora通信线程堆栈----------------------------------------------------------------*/
  57. __align(8) OS_STK SX1278_TASK_STK[SX1278_STK_SIZE];
  58. void NVIC_Configuration(void);
  59. void load_unique(void);
  60. char gatewayId[11];
  61. int main(void)
  62. {
  63. int status;
  64. load_unique();
  65. sprintf(gatewayId,"DTtest0001");//DT8pd3ac6h DTbma5ac6h DTtest0001 DTrzts0001
  66. NVIC_Configuration();
  67. my_mem_init(SRAMEX);
  68. my_mem_init(SRAMIN);
  69. delay_init();
  70. //nandflash并不用初始化,调用fafts时会初始化
  71. NET_STATUS_LED_Config();
  72. USART_485_config();
  73. // USART_485_DE_TX();
  74. USART_232_config();
  75. DEBUG_USART_Config();
  76. USART_DMA_Config();
  77. status = NAND_Init();
  78. while(status){
  79. NAND_Format();
  80. status = NAND_Init();
  81. }
  82. OSInit();
  83. #if 0
  84. //OSTaskCreate(start_task,(void*)0,(OS_STK*)&START_TASK_STK[START_STK_SIZE-1],START_TASK_PRIO);
  85. OSTaskCreate(period_taskFuntcion,(void*)0,(OS_STK*)&PERIOD_TASK_STK[PERIOD_STK_SIZE-1],PERIOD_TASK_PRIO);
  86. OSTaskCreate(led_taskFuntcion,(void*)0,(OS_STK*)&LED_TASK_STK[LED_STK_SIZE-1],LED_TASK_PRIO);
  87. OSTaskCreate(sx1278_send_task,(void *)0,(OS_STK*)&SX1278_TASK_STK[SX1278_STK_SIZE-1],SX1278_TASK_PRIO);
  88. #endif
  89. OSTaskCreate(period_taskFuntcion,(void*)0,(OS_STK*)&PERIOD_TASK_STK[PERIOD_STK_SIZE-1],PERIOD_TASK_PRIO);
  90. OSTaskCreate(data_task,(void*)0,(OS_STK*)&DATA_TASK_STK[DATA_STK_SIZE-1],DATA_TASK_PRIO);
  91. printf("system start \r\n");
  92. OSStart(); //ucos启动
  93. /* Infinite loop */
  94. while (1)
  95. {
  96. }
  97. }
  98. void period_taskFuntcion(void *arg)
  99. {
  100. OS_CPU_SR cpu_sr;
  101. #if OS_TASK_STAT_EN > 0u
  102. OSStatInit();
  103. #endif
  104. lwIP_Init();
  105. // 输出日志
  106. // log_init();
  107. // tcp_server_init();
  108. // LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "system start");
  109. http_getDemo();
  110. http_postDemo();
  111. mqtt_threadCreate();
  112. OSTaskSuspend(OS_PRIO_SELF);
  113. }
  114. //测试任务
  115. void led_taskFuntcion(void *pdata)
  116. {
  117. int tms;
  118. while(1)
  119. {
  120. tms = hd_netledOpen();
  121. OSTimeDly(tms);
  122. tms = hd_netledClose();
  123. OSTimeDly(tms);
  124. }
  125. }
  126. void bsp_Idle(void)
  127. {
  128. /* --- 喂狗 */
  129. /* --- 让CPU进入休眠,由Systick定时中断唤醒或者其他中断唤醒 */
  130. }
  131. /**
  132. * @brief 配置嵌套向量中断控制器NVIC分组
  133. * @param 无
  134. * @retval 无
  135. */
  136. void NVIC_Configuration(void)
  137. {
  138. // NVIC_InitTypeDef NVIC_InitStructure;
  139. /* 嵌套向量中断控制器组选择 */
  140. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  141. }
  142. #ifdef USE_FULL_ASSERT
  143. /**
  144. * @brief Reports the name of the source file and the source line number
  145. * where the assert_param error has occurred.
  146. * @param file: pointer to the source file name
  147. * @param line: assert_param error line source number
  148. * @retval None
  149. */
  150. void assert_failed(uint8_t* file, uint32_t line)
  151. {
  152. /* User can add his own implementation to report the file name and line number,
  153. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  154. /* Infinite loop */
  155. // while (1)
  156. //{}
  157. }
  158. #endif
  159. /*
  160. * 函数名:void readID()
  161. * 输入参数:无
  162. * 输出参数:strId
  163. * 返回值:无
  164. * 函数作用:读取长度12的芯片Id并组合从中取出对应字符防止其重复
  165. */
  166. void load_unique(void)
  167. {
  168. char *hex_table = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN";
  169. uint32_t CpuID1, CpuID2, CpuID3;
  170. CpuID1 = *(volatile uint32_t *)(UNIQUE_ID);
  171. CpuID2 = *(volatile uint32_t *)(UNIQUE_ID+8);
  172. CpuID3 = *(volatile uint32_t *)(UNIQUE_ID+16);
  173. char strId[11];
  174. for (uint8_t i = 0, j = 0; i < 8; i += 2)
  175. {
  176. uint32_t sum1 = (CpuID1 >> ((j % 4) * 8)) & 0xFF; // 按字节取出
  177. uint8_t index1 = sum1 / 16;
  178. uint8_t remainder1 = sum1 % 16;
  179. uint32_t sum2 = (CpuID2 >> ((j % 4) * 8)) & 0xFF; // 按字节取出
  180. uint8_t index2 = sum2 / 16;
  181. uint8_t remainder2 = sum2 % 16;
  182. uint32_t sum3 = (CpuID3 >> ((j % 4) * 8)) & 0xFF; // 按字节取出
  183. uint8_t index3 = sum3 / 16;
  184. uint8_t remainder3 = sum3 % 16;
  185. strId[i + 2] = hex_table[index1 + index2 + index3];
  186. strId[i + 1 + 2] = hex_table[remainder1 + remainder2 + remainder3];
  187. j++;
  188. }
  189. strId[10] = '\0';
  190. strId[0]='L';
  191. strId[1]='R';
  192. strcpy(gatewayId,strId);
  193. }
  194. /*
  195. * 函数名:void load_232_config()
  196. * 输入参数:无
  197. * 输出参数:strId
  198. * 返回值:无
  199. * 函数作用:加载本地存储配置如果上位机配置过则不走http配置路线,防止两线冲突
  200. */