main.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. #define UNIQUE_ID 0x1fff7a10
  22. /*关于任务优先级分配(上限为12个、需要增加请修改上限)
  23. * 0: 保留
  24. * 1: 用于ucos的软件定时器
  25. * 2: 保留
  26. * 3: 用于 以太网的网卡数据包读取 ETHERNETIF_THREAD_PRO
  27. * 4: LWIP 核心线程 TCPIP_THREAD_PRIO
  28. * 5: 用于 ota升级
  29. * 6: 用于 MQTT数据发送线程 APP_TASK_MQTTMAIN_PRIO
  30. * 7: 用于 MQTT数据接收线程 APP_TASK_MQTTRECEIVE_PRIO
  31. * 8: 用于 sx1278发送/接收处理数据 DATA_TASK_PRIO
  32. * 9: 用于 本机状态led显示 / 现用于tcpServer_init TCP_TASK_PRIO /
  33. * 10:用于网络状态led显示
  34. * 11:用于轮询读出本机保存信息发送到sx1278发送接收线程
  35. * 12:用于管理所有线程
  36. */
  37. ///////////////////////////////////////
  38. #define PERIOD_TASK_PRIO 2
  39. #define PERIOD_STK_SIZE 2048
  40. OS_STK PERIOD_TASK_STK[PERIOD_STK_SIZE];
  41. void period_taskFuntcion(void *arg);
  42. //////////////////////////////////////////////
  43. //LED
  44. //LED优先级
  45. #define LED_TASK_PRIO 28
  46. //LED栈空间大小
  47. #define LED_STK_SIZE 128
  48. //任务堆栈
  49. OS_STK LED_TASK_STK[LED_STK_SIZE];
  50. //led运行任务
  51. void led_taskFuntcion(void *pdata);
  52. ////////////////////////////////////////////////////////
  53. /*---------------------------本地数据线程堆栈----------------------------------------------------------------*/
  54. __align(8) OS_STK DATA_TASK_STK[DATA_STK_SIZE];
  55. /*---------------------------lora通信线程堆栈----------------------------------------------------------------*/
  56. __align(8) OS_STK SX1278_TASK_STK[SX1278_STK_SIZE];
  57. void NVIC_Configuration(void);
  58. void load_unique(void);
  59. char gatewayId[11];
  60. int main(void)
  61. {
  62. int status;
  63. load_unique();
  64. sprintf(gatewayId,"DTtest0001");//DT8pd3ac6h DTbma5ac6h DTtest0001
  65. NVIC_Configuration();
  66. my_mem_init(SRAMEX);
  67. my_mem_init(SRAMIN);
  68. delay_init();
  69. //nandflash并不用初始化,调用fafts时会初始化
  70. NET_STATUS_LED_Config();
  71. USART_485_config();
  72. USART_485_DE_TX();
  73. USART_232_config();
  74. status = NAND_Init();
  75. while(status){
  76. NAND_Format();
  77. status = NAND_Init();
  78. }
  79. OSInit();
  80. #if 0
  81. //OSTaskCreate(start_task,(void*)0,(OS_STK*)&START_TASK_STK[START_STK_SIZE-1],START_TASK_PRIO);
  82. OSTaskCreate(period_taskFuntcion,(void*)0,(OS_STK*)&PERIOD_TASK_STK[PERIOD_STK_SIZE-1],PERIOD_TASK_PRIO);
  83. OSTaskCreate(led_taskFuntcion,(void*)0,(OS_STK*)&LED_TASK_STK[LED_STK_SIZE-1],LED_TASK_PRIO);
  84. OSTaskCreate(sx1278_send_task,(void *)0,(OS_STK*)&SX1278_TASK_STK[SX1278_STK_SIZE-1],SX1278_TASK_PRIO);
  85. #endif
  86. OSTaskCreate(period_taskFuntcion,(void*)0,(OS_STK*)&PERIOD_TASK_STK[PERIOD_STK_SIZE-1],PERIOD_TASK_PRIO);
  87. OSTaskCreate(data_task,(void*)0,(OS_STK*)&DATA_TASK_STK[DATA_STK_SIZE-1],DATA_TASK_PRIO);
  88. printf("system start \r\n");
  89. OSStart(); //ucos启动
  90. /* Infinite loop */
  91. while (1)
  92. {
  93. }
  94. }
  95. void period_taskFuntcion(void *arg)
  96. {
  97. OS_CPU_SR cpu_sr;
  98. #if OS_TASK_STAT_EN > 0u
  99. OSStatInit();
  100. #endif
  101. int time1,time2;
  102. time1 = OSTimeGet();
  103. lwIP_Init();
  104. // 输出日志
  105. // log_init();
  106. // tcp_server_init();
  107. // LogPrint(LOG_INFO,__FILE__, __FUNCTION__, __LINE__, "system start");
  108. time2 = OSTimeGet() - time1;
  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. */