nodelink_master_app.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. #include "global.h"
  2. void nodelink_timer_init(void);
  3. //扩展信息
  4. typedef struct
  5. {
  6. char *inputs; //输入指针
  7. char *outputs; //输出指针
  8. char input_number; //输入IO个数
  9. char output_number; //输出IO个数
  10. char input_shift; //输入偏移位
  11. char output_shift; //输出偏移位
  12. int missing; //丢包计数
  13. } io_extern_data_t;
  14. //#if USE_EXTEND_INOUTPUT
  15. static char io_extern_count = 0; //IO扩展个数统计
  16. static char io_extern_input_count = 0;
  17. static char io_extern_output_count = 0;
  18. static io_extern_data_t io_extern_datas[IO_EXTERN_NUMBER]; //IO扩展信息结构体
  19. //IO扩展缓存
  20. byte_bits_t io_extern_inputs[(EXTEND_IO_INPUT_NUMBER + 7)>> 3];
  21. byte_bits_t io_extern_outputs[(EXTEND_IO_OUTPUT_NUMBER + 7)>> 3];
  22. unsigned char ResetFlg;
  23. unsigned char BeginRecvFlg;
  24. unsigned short sendtime=2000;
  25. //==================================================扩展应用程序=========================================//
  26. /**
  27. * 发送数据生成
  28. *
  29. * @author LXZ (070720)
  30. *
  31. * @param device
  32. * @param data
  33. *
  34. * @return int
  35. */
  36. static int slave_on_send(nodelink_device_t *device, unsigned char *data) {
  37. int index = 0;
  38. int len = 2;
  39. io_extern_data_t *extern_dat = device->user_data;
  40. data[0] = device->id;
  41. data[1] = 0xA3;
  42. if (extern_dat->output_shift == 0) {
  43. memcpy(&data[len], extern_dat->outputs, (extern_dat->output_number + 7) >> 3);
  44. len += (extern_dat->output_number + 7) >> 3;
  45. }
  46. else {
  47. unsigned char dat = 0;
  48. int number = (extern_dat->output_number + 7) >> 3;
  49. while (index < number)
  50. {
  51. dat = extern_dat->outputs[index] >> extern_dat->output_shift;
  52. dat |= extern_dat->outputs[index + 1] << (8 - extern_dat->output_shift);
  53. data[len++] = dat;
  54. index++;
  55. }
  56. }
  57. return len;
  58. }
  59. /**
  60. * 接收数据生成
  61. *
  62. * @author LXZ (070720)
  63. *
  64. * @param device
  65. * @param data
  66. */
  67. static void slave_on_recv(nodelink_device_t *device, unsigned char *data) {
  68. int dst_index = 0;
  69. int src_index = 0;
  70. io_extern_data_t *extern_dat = device->user_data;
  71. dst_index = extern_dat->input_shift;
  72. while (src_index < extern_dat->input_number) {
  73. if (data[(src_index >> 3) + 2] & (1 << (src_index & 0x07)))
  74. {
  75. extern_dat->inputs[dst_index >> 3] |= 1 << (dst_index & 0x07);
  76. }
  77. else
  78. {
  79. extern_dat->inputs[dst_index >> 3] &= ~(1 << (dst_index & 0x07));
  80. }
  81. src_index++;
  82. dst_index++;
  83. }
  84. device->txcount=0;
  85. }
  86. /**
  87. * 收发超时
  88. *
  89. * @author LXZ (070720)
  90. *
  91. * @param device
  92. */
  93. static void slave_on_timeout(nodelink_device_t *device) {
  94. io_extern_data_t *dat = device->user_data;
  95. dat->missing++;
  96. }
  97. static nodelink_device_ops io_extern_ops = {
  98. slave_on_recv,
  99. slave_on_send,
  100. slave_on_timeout
  101. };
  102. //=================================================扩展主站APP==============================================//
  103. nodelink_master_t master;
  104. static int nl_recv_step = 0; //接收步骤
  105. int nl_send_step = 0; //发送步骤
  106. static sw_timer_t send_timer;
  107. static unsigned char recv_buffer[256];
  108. static unsigned char send_buffer[256];
  109. static int bus_timeout = 0;
  110. /**
  111. * 主站的回调函数
  112. *
  113. * @author LXZ (070720)
  114. *
  115. * @param master
  116. * @param cmd
  117. * @param argv
  118. */
  119. static void master_callback(nodelink_master_t *master, char cmd, void *argv) {
  120. switch (cmd) {
  121. case NL_MASTER_EVENT_FOUND_DEVICE: //发现一个扩展模块
  122. {
  123. nodelink_device_t *device = (nodelink_device_t *)argv;
  124. if (device->model == 1) { //根据型号注册信息
  125. device->user_data = &io_extern_datas[io_extern_count];
  126. device->ops = &io_extern_ops;
  127. io_extern_datas[io_extern_count].input_number = 8;
  128. io_extern_datas[io_extern_count].inputs = (char *)&io_extern_inputs[(((IO_PIN_INPUT_NUMBER%8)+io_extern_input_count + 7) >> 3)-1];
  129. io_extern_datas[io_extern_count].output_number = 6;
  130. io_extern_datas[io_extern_count].outputs = (char *)&io_extern_outputs[(((IO_PIN_OUTPUT_NUMBER%8)+io_extern_output_count + 7) >> 3) - 1];
  131. io_extern_datas[io_extern_count].input_shift = (io_extern_input_count + IO_PIN_INPUT_NUMBER) % 8;
  132. io_extern_datas[io_extern_count].output_shift = (io_extern_output_count + IO_PIN_OUTPUT_NUMBER) % 8;
  133. io_extern_datas[io_extern_count].missing = 0;
  134. io_extern_input_count += io_extern_datas[io_extern_count].input_number;
  135. io_extern_output_count += io_extern_datas[io_extern_count].output_number;
  136. io_extern_count++;
  137. }
  138. }
  139. break;
  140. }
  141. }
  142. //#endif
  143. /**
  144. * 扩展主站初始化
  145. *
  146. * @author LXZ (070620)
  147. *
  148. * @param void
  149. */
  150. void nodelink_master_app_init(void)
  151. {
  152. //#if USE_EXTEND_INOUTPUT
  153. nodelink_master_init(&master);
  154. {
  155. int value = 1000;
  156. //需要发送几次总线复位的命令
  157. hw_dma_uart_control(0, UART_CTRL_SET_RDFIN_TIME, &value);
  158. }
  159. //设置回调函数
  160. nodelink_master_set_callback(&master, master_callback);
  161. { //复位一下总线
  162. nodelink_master_reset(&master);
  163. io_extern_count =0;
  164. io_extern_input_count = 0;
  165. io_extern_output_count = 0;
  166. bus_timeout = dwTickCount + 4000;
  167. }
  168. nodelink_timer_init();
  169. ResetFlg=0;
  170. BeginRecvFlg=0;
  171. //#endif
  172. }
  173. /**
  174. * 主站应用执行
  175. *
  176. * @author LXZ (070620)
  177. *
  178. * @param void
  179. */
  180. void nodelink_master_app_run(void)
  181. {
  182. //#if USE_EXTEND_INOUTPUT
  183. static unsigned long connect_time;
  184. int length = 0;
  185. //接收数据
  186. switch (nl_recv_step) {
  187. case 0:
  188. hw_dma_uart_begin_read(2, recv_buffer, sizeof(recv_buffer));
  189. nl_recv_step++;
  190. connect_time = dwTickCount + 1000;
  191. if((GetAlarmCode(QDCT_ALARM_ADDR) == QDCT_EX_ALARM) && USE_EXTEND_ALARM)SetAlarmCode(QDCT_ALARM_ADDR,QDCT_NO_ALARM);
  192. break;
  193. case 1:
  194. length = hw_dma_uart_read_finish(2);
  195. if (length > 0) {
  196. bus_timeout = dwTickCount + 1000;
  197. nodelink_master_recv(&master, recv_buffer, length);
  198. hw_dma_uart_begin_read(2, recv_buffer, sizeof(recv_buffer));
  199. if((GetAlarmCode(QDCT_ALARM_ADDR) == QDCT_EX_ALARM) && USE_EXTEND_ALARM)SetAlarmCode(QDCT_ALARM_ADDR,QDCT_NO_ALARM);
  200. //nl_recv_step = 0;
  201. connect_time = dwTickCount + 1000;
  202. }
  203. if(master.bus_status == 0xFF) connect_time = dwTickCount + 1000;
  204. if((dwTickCount >= connect_time || ResetFlg==1) && master.bus_status != 0xFF)
  205. {
  206. if (USE_EXTEND_ALARM) SetAlarmCode(QDCT_ALARM_ADDR,QDCT_EX_ALARM);
  207. //复位一下总线
  208. nodelink_master_reset(&master);
  209. io_extern_count =0;
  210. io_extern_input_count = 0;
  211. io_extern_output_count = 0;
  212. nl_recv_step = 0;
  213. connect_time = dwTickCount + 1000;
  214. }
  215. if(master.bus_status == 0xF1 && master.data_in[1]==0xA1)
  216. {
  217. //复位一下总线
  218. nodelink_master_reset(&master);
  219. io_extern_count =0;
  220. io_extern_input_count = 0;
  221. io_extern_output_count = 0;
  222. nl_recv_step = 0;
  223. sendtime=1000;
  224. }
  225. break;
  226. }
  227. //每隔2毫秒送一次总线信号
  228. switch (nl_send_step) {
  229. case 0:
  230. sw_timer_start(&send_timer, 0, sendtime);
  231. bus_timeout = dwTickCount + 5000;
  232. nl_send_step++;
  233. break;
  234. case 1:
  235. if (sw_timer_expire(&send_timer))
  236. {
  237. if (bus_timeout < dwTickCount)
  238. {
  239. bus_timeout = dwTickCount + 5000;
  240. { //复位一下总线
  241. nodelink_master_reset(&master);
  242. io_extern_count =0;
  243. io_extern_input_count = 0;
  244. io_extern_output_count = 0;
  245. }
  246. }
  247. length = nodelink_master_send(&master, send_buffer);
  248. if (length == 1) {
  249. nl_send_step = 0;
  250. }
  251. else if (length > 0) {
  252. hw_dma_uart_begin_write(2, (const char *)send_buffer, length);
  253. nl_send_step++;
  254. }
  255. //sw_timer_start(&send_timer, 0, 2000);
  256. }
  257. break;
  258. case 2:
  259. if (hw_dma_uart_write_finish(2))
  260. {
  261. nl_send_step = 0;
  262. }
  263. break;
  264. }
  265. user_datas[529]=master.bus_status;
  266. user_datas[530]=master.step;
  267. user_datas[531]=master.dev_count;
  268. //#endif
  269. }
  270. void nodelink_timer_init(void)
  271. {
  272. RCC->APB1ENR |= (1<< 4);
  273. Sys_NVIC_Init(1, 1, TIM6_IRQn, 1);
  274. TIM6->CR1 = 0;
  275. TIM6->CR1 |= (0 << 8) | (1 << 7) | (0 << 5) | (0 << 4) | (0 << 3) | (1 << 2) | (0 << 1); // 禁止自动重载
  276. TIM6->CR2 = 0;
  277. TIM6->PSC = 0;
  278. TIM6->ARR = 120000000 / 10000 + 1;//50k
  279. //TIM6->CCER = 0;
  280. //TIM6->CCMR1 = 0;
  281. TIM6->DIER = 0;
  282. TIM6->DIER |= 1; // 允许更新中断
  283. TIM6->CNT = 0;
  284. TIM6->CR1 |= 1 << 0;
  285. TIM6->SR = 0;
  286. }
  287. void TIM6_IRQHandler(void)
  288. {
  289. if (TIM6->SR & TIM_SR_UIF)
  290. {
  291. nodelink_master_app_run();
  292. TIM6->SR &= ~TIM_SR_UIF;
  293. }
  294. }
  295. void nodelink_read_input(void)
  296. {
  297. unsigned char input_shift = IO_PIN_INPUT_NUMBER % 8;
  298. int index = 0;
  299. if(input_shift==0)
  300. {
  301. memcpy(&io_inputs[((IO_PIN_INPUT_NUMBER + 7) >> 3)],
  302. &io_extern_inputs[0], (EXTEND_IO_INPUT_NUMBER + 7)>> 3);
  303. }
  304. else
  305. {
  306. index=input_shift;
  307. while (index < 8) {
  308. if (io_extern_inputs[0].value & (1 << index))
  309. {
  310. io_inputs[((IO_PIN_INPUT_NUMBER + 7) >> 3)-1].value |= 1 << index;
  311. }
  312. else
  313. {
  314. io_inputs[((IO_PIN_INPUT_NUMBER + 7) >> 3)-1].value &= ~(1 << index);
  315. }
  316. index++;
  317. }
  318. memcpy(&io_inputs[((IO_PIN_INPUT_NUMBER + 7) >> 3)],
  319. &io_extern_inputs[1], ((EXTEND_IO_INPUT_NUMBER + 7)>> 3)-1);
  320. //memcpy(&io_inputs[((IO_PIN_INPUT_NUMBER + 7) >> 3)],
  321. //&io_extern_inputs[1], 5);
  322. }
  323. }
  324. void nodelink_write_output(void)
  325. {
  326. unsigned char output_shift = IO_PIN_OUTPUT_NUMBER % 8;
  327. int index = 0;
  328. if(output_shift==0)
  329. {
  330. memcpy(&io_extern_outputs[0], &io_outputs[((IO_PIN_OUTPUT_NUMBER + 7) >> 3)-1],
  331. (EXTEND_IO_OUTPUT_NUMBER + 7)>> 3);
  332. }
  333. else
  334. {
  335. index=output_shift;
  336. while (index < 8) {
  337. if (io_outputs[((IO_PIN_OUTPUT_NUMBER + 7) >> 3)-1].value & (1 << index))
  338. {
  339. io_extern_outputs[0].value |= 1 << index;
  340. }
  341. else
  342. {
  343. io_extern_outputs[0].value &= ~(1 << index);
  344. }
  345. index++;
  346. }
  347. memcpy(&io_extern_outputs[1],
  348. &io_outputs[((IO_PIN_OUTPUT_NUMBER + 7) >> 3)], (EXTEND_IO_OUTPUT_NUMBER - (8 - output_shift) + 7)>> 3);
  349. }
  350. }
  351. void nodelink_reset(void)
  352. {
  353. nodelink_master_reset(&master);
  354. io_extern_count =0;
  355. io_extern_input_count = 0;
  356. io_extern_output_count = 0;
  357. nl_recv_step = 0;
  358. }