main.c 5.3 KB

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