nodelink_master_app.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include "global.h"
  2. #define IO_EXTERN_NUMBER 4 //支持的IO扩展个数
  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[4]; //IO扩展信息结构体
  19. //==================================================扩展应用程序=========================================//
  20. /**
  21. * 发送数据生成
  22. *
  23. * @author LXZ (070720)
  24. *
  25. * @param device
  26. * @param data
  27. *
  28. * @return int
  29. */
  30. static int slave_on_send(nodelink_device_t *device, unsigned char *data) {
  31. int index = 0;
  32. int len = 0;
  33. io_extern_data_t *extern_dat = device->user_data;
  34. data[len++] = 0x01;
  35. if (extern_dat->input_shift == 0) {
  36. memcpy(&data[len], extern_dat->outputs, (extern_dat->output_number + 7) >> 3);
  37. len += (extern_dat->output_number + 7) >> 3;
  38. }
  39. else {
  40. unsigned char dat = 0;
  41. int number = (extern_dat->input_number + 7) >> 3;
  42. while (index < number)
  43. {
  44. dat = extern_dat->outputs[index] >> extern_dat->output_shift;
  45. dat |= extern_dat->outputs[index + 1] << (8 - extern_dat->output_shift);
  46. data[len++] = dat;
  47. index++;
  48. }
  49. }
  50. return len;
  51. }
  52. /**
  53. * 接收数据生成
  54. *
  55. * @author LXZ (070720)
  56. *
  57. * @param device
  58. * @param data
  59. */
  60. static void slave_on_recv(nodelink_device_t *device, unsigned char *data) {
  61. int dst_index = 0;
  62. int src_index = 0;
  63. io_extern_data_t *extern_dat = device->user_data;
  64. if (data[0] == 0x01) {
  65. dst_index = extern_dat->input_shift;
  66. while (src_index < extern_dat->input_number) {
  67. if (data[(src_index >> 3) + 1] & (1 << (src_index & 0x07)))
  68. {
  69. extern_dat->inputs[dst_index >> 3] |= 1 << (dst_index & 0x07);
  70. }
  71. else
  72. {
  73. extern_dat->inputs[dst_index >> 3] &= ~(1 << (dst_index & 0x07));
  74. }
  75. src_index++;
  76. dst_index++;
  77. }
  78. }
  79. }
  80. /**
  81. * 收发超时
  82. *
  83. * @author LXZ (070720)
  84. *
  85. * @param device
  86. */
  87. static void slave_on_timeout(nodelink_device_t *device) {
  88. io_extern_data_t *dat = device->user_data;
  89. dat->missing++;
  90. }
  91. static nodelink_device_ops io_extern_ops = {
  92. slave_on_recv,
  93. slave_on_send,
  94. slave_on_timeout
  95. };
  96. //=================================================扩展主站APP==============================================//
  97. static nodelink_master_t master;
  98. static int nl_recv_step = 0; //接收步骤
  99. static int nl_send_step = 0; //发送步骤
  100. static sw_timer_t send_timer;
  101. static unsigned char recv_buffer[256];
  102. static unsigned char send_buffer[256];
  103. static int bus_timeout = 0;
  104. /**
  105. * 主站的回调函数
  106. *
  107. * @author LXZ (070720)
  108. *
  109. * @param master
  110. * @param cmd
  111. * @param argv
  112. */
  113. static void master_callback(nodelink_master_t *master, char cmd, void *argv) {
  114. switch (cmd) {
  115. case NL_MASTER_EVENT_FOUND_DEVICE: //发现一个扩展模块
  116. {
  117. nodelink_device_t *device = (nodelink_device_t *)argv;
  118. if (device->model == 1) { //根据型号注册信息
  119. device->user_data = &io_extern_datas[io_extern_count];
  120. device->ops = &io_extern_ops;
  121. io_extern_datas[io_extern_count].input_number = 6;
  122. io_extern_datas[io_extern_count].inputs = (char *)&io_inputs[((IO_PIN_INPUT_NUMBER + 7) >> 3) +
  123. (io_extern_input_count >> 3)];
  124. io_extern_datas[io_extern_count].output_number = 7;
  125. io_extern_datas[io_extern_count].outputs = (char *)&io_outputs[((IO_PIN_OUTPUT_NUMBER + 7) >> 3) +
  126. (io_extern_output_count >> 3)];
  127. io_extern_datas[io_extern_count].input_shift = io_extern_input_count & 0x07;
  128. io_extern_datas[io_extern_count].output_shift = io_extern_output_count & 0x07;
  129. io_extern_datas[io_extern_count].missing = 0;
  130. io_extern_input_count += io_extern_datas[io_extern_count].input_number;
  131. io_extern_output_count += io_extern_datas[io_extern_count].output_number;
  132. io_extern_count++;
  133. }
  134. }
  135. break;
  136. }
  137. }
  138. //#endif
  139. /**
  140. * 扩展主站初始化
  141. *
  142. * @author LXZ (070620)
  143. *
  144. * @param void
  145. */
  146. void nodelink_master_app_init(void)
  147. {
  148. if (USE_EXTEND_INOUTPUT)
  149. {
  150. nodelink_master_init(&master);
  151. {
  152. int value = 1000;
  153. //需要发送几次总线复位的命令
  154. hw_dma_uart_control(0, UART_CTRL_SET_RDFIN_TIME, &value);
  155. }
  156. //设置回调函数
  157. nodelink_master_set_callback(&master, master_callback);
  158. { //复位一下总线
  159. nodelink_master_reset(&master);
  160. io_extern_count =0;
  161. io_extern_input_count = 0;
  162. io_extern_output_count = 0;
  163. bus_timeout = dwTickCount + 4000;
  164. }
  165. }
  166. }
  167. /**
  168. * 主站应用执行
  169. *
  170. * @author LXZ (070620)
  171. *
  172. * @param void
  173. */
  174. void nodelink_master_app_run(void)
  175. {
  176. if (USE_EXTEND_INOUTPUT)
  177. {
  178. static unsigned long connect_time;
  179. int length = 0;
  180. io_inputs_old[3] = io_inputs[3];
  181. //接收数据
  182. switch (nl_recv_step) {
  183. case 0:
  184. hw_dma_uart_begin_read(0, recv_buffer, sizeof(recv_buffer));
  185. nl_recv_step++;
  186. connect_time = dwTickCount + 500;
  187. break;
  188. case 1:
  189. length = hw_dma_uart_read_finish(0);
  190. if (length > 0) {
  191. bus_timeout = dwTickCount + 1000;
  192. nodelink_master_recv(&master, recv_buffer, length);
  193. nl_recv_step = 0;
  194. }
  195. break;
  196. }
  197. //每隔10毫秒送一次总线信号
  198. switch (nl_send_step) {
  199. case 0:
  200. sw_timer_start(&send_timer, 0, 10000);
  201. nl_send_step++;
  202. break;
  203. case 1:
  204. if (sw_timer_expire(&send_timer))
  205. {
  206. if (bus_timeout < dwTickCount)
  207. {
  208. bus_timeout = dwTickCount + 1000;
  209. { //复位一下总线
  210. nodelink_master_reset(&master);
  211. io_extern_count =0;
  212. io_extern_input_count = 0;
  213. io_extern_output_count = 0;
  214. }
  215. }
  216. length = nodelink_master_send(&master, send_buffer);
  217. if (length == 1) {
  218. nl_send_step = 0;
  219. }
  220. else if (length > 0) {
  221. hw_dma_uart_begin_write(0, (const char *)send_buffer, length);
  222. nl_send_step++;
  223. }
  224. }
  225. break;
  226. case 2:
  227. if (hw_dma_uart_write_finish(0))
  228. {
  229. nl_send_step = 0;
  230. }
  231. break;
  232. }
  233. }
  234. }