main.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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发送/接收处理数据 DATA_TASK_PRIO
  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. int status;
  61. load_unique();
  62. sprintf(gatewayId,"DT8pd3ac6h");//DT8pd3ac6h DTbma5ac6h DTtest0001
  63. NVIC_Configuration();
  64. my_mem_init(SRAMEX);
  65. my_mem_init(SRAMIN);
  66. delay_init();
  67. //nandflash并不用初始化,调用fafts时会初始化
  68. NET_STATUS_LED_Config();
  69. USART_485_config();
  70. USART_485_DE_TX();
  71. USART_232_config();
  72. status = NAND_Init();
  73. while(status){
  74. NAND_Format();
  75. status = NAND_Init();
  76. }
  77. OSInit();
  78. #if 0
  79. //OSTaskCreate(start_task,(void*)0,(OS_STK*)&START_TASK_STK[START_STK_SIZE-1],START_TASK_PRIO);
  80. OSTaskCreate(period_taskFuntcion,(void*)0,(OS_STK*)&PERIOD_TASK_STK[PERIOD_STK_SIZE-1],PERIOD_TASK_PRIO);
  81. OSTaskCreate(led_taskFuntcion,(void*)0,(OS_STK*)&LED_TASK_STK[LED_STK_SIZE-1],LED_TASK_PRIO);
  82. OSTaskCreate(sx1278_send_task,(void *)0,(OS_STK*)&SX1278_TASK_STK[SX1278_STK_SIZE-1],SX1278_TASK_PRIO);
  83. #endif
  84. OSTaskCreate(period_taskFuntcion,(void*)0,(OS_STK*)&PERIOD_TASK_STK[PERIOD_STK_SIZE-1],PERIOD_TASK_PRIO);
  85. OSTaskCreate(data_task,(void*)0,(OS_STK*)&DATA_TASK_STK[DATA_STK_SIZE-1],DATA_TASK_PRIO);
  86. printf("system start \r\n");
  87. OSStart(); //ucos启动
  88. /* Infinite loop */
  89. while (1)
  90. {
  91. }
  92. }
  93. void period_taskFuntcion(void *arg)
  94. {
  95. OS_CPU_SR cpu_sr;
  96. #if OS_TASK_STAT_EN > 0u
  97. OSStatInit();
  98. #endif
  99. lwIP_Init();
  100. http_getDemo();
  101. http_postDemo();
  102. mqtt_threadCreate();
  103. OSTaskSuspend(OS_PRIO_SELF);
  104. }
  105. //测试任务
  106. void led_taskFuntcion(void *pdata)
  107. {
  108. int tms;
  109. while(1)
  110. {
  111. tms = hd_netledOpen();
  112. OSTimeDly(tms);
  113. tms = hd_netledClose();
  114. OSTimeDly(tms);
  115. }
  116. }
  117. void bsp_Idle(void)
  118. {
  119. /* --- 喂狗 */
  120. /* --- 让CPU进入休眠,由Systick定时中断唤醒或者其他中断唤醒 */
  121. }
  122. /**
  123. * @brief 配置嵌套向量中断控制器NVIC分组
  124. * @param 无
  125. * @retval 无
  126. */
  127. void NVIC_Configuration(void)
  128. {
  129. // NVIC_InitTypeDef NVIC_InitStructure;
  130. /* 嵌套向量中断控制器组选择 */
  131. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  132. }
  133. #ifdef USE_FULL_ASSERT
  134. /**
  135. * @brief Reports the name of the source file and the source line number
  136. * where the assert_param error has occurred.
  137. * @param file: pointer to the source file name
  138. * @param line: assert_param error line source number
  139. * @retval None
  140. */
  141. void assert_failed(uint8_t* file, uint32_t line)
  142. {
  143. /* User can add his own implementation to report the file name and line number,
  144. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  145. /* Infinite loop */
  146. // while (1)
  147. //{}
  148. }
  149. #endif
  150. /*
  151. * 函数名:void readID()
  152. * 输入参数:无
  153. * 输出参数:strId
  154. * 返回值:无
  155. * 函数作用:读取长度12的芯片Id并组合从中取出对应字符防止其重复
  156. */
  157. void load_unique(void)
  158. {
  159. char *hex_table = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN";
  160. uint32_t CpuID1, CpuID2, CpuID3;
  161. CpuID1 = *(volatile uint32_t *)(UNIQUE_ID);
  162. CpuID2 = *(volatile uint32_t *)(UNIQUE_ID+8);
  163. CpuID3 = *(volatile uint32_t *)(UNIQUE_ID+16);
  164. char strId[11];
  165. for (uint8_t i = 0, j = 0; i < 8; i += 2)
  166. {
  167. uint32_t sum1 = (CpuID1 >> ((j % 4) * 8)) & 0xFF; // 按字节取出
  168. uint8_t index1 = sum1 / 16;
  169. uint8_t remainder1 = sum1 % 16;
  170. uint32_t sum2 = (CpuID2 >> ((j % 4) * 8)) & 0xFF; // 按字节取出
  171. uint8_t index2 = sum2 / 16;
  172. uint8_t remainder2 = sum2 % 16;
  173. uint32_t sum3 = (CpuID3 >> ((j % 4) * 8)) & 0xFF; // 按字节取出
  174. uint8_t index3 = sum3 / 16;
  175. uint8_t remainder3 = sum3 % 16;
  176. strId[i + 2] = hex_table[index1 + index2 + index3];
  177. strId[i + 1 + 2] = hex_table[remainder1 + remainder2 + remainder3];
  178. j++;
  179. }
  180. strId[10] = '\0';
  181. strId[0]='L';
  182. strId[1]='R';
  183. strcpy(gatewayId,strId);
  184. }
  185. /*
  186. * 函数名:void load_232_config()
  187. * 输入参数:无
  188. * 输出参数:strId
  189. * 返回值:无
  190. * 函数作用:加载本地存储配置如果上位机配置过则不走http配置路线,防止两线冲突
  191. */