mb.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include "mb.h"
  2. #include "list.h"
  3. #define MALLOC malloc
  4. #define FREE free
  5. typedef struct {
  6. mb_inst_t *hinst;
  7. uint8_t mode;
  8. uint8_t type;
  9. rt_thread_t tid;
  10. uint8_t quit;
  11. }mb_handle_t;
  12. static mb_handle_t mbHandle={0};
  13. typedef void (*thread_fn_t)(void *arg);
  14. static int mb_stop(mb_handle_t *h);
  15. static void mb_thread(void *arg)
  16. {
  17. mb_handle_t *h=(mb_handle_t*)arg;
  18. while(h->quit==0) {
  19. mb_slave_fsm(h->hinst);
  20. }
  21. h->quit = 2;
  22. }
  23. static int mb_start(mb_handle_t *h)
  24. {
  25. if(h->mode==MB_MODE_MASTER) {
  26. return -1;
  27. }
  28. if(h->tid) {
  29. mb_stop(h);
  30. }
  31. h->tid = rt_thread_create(NULL, mb_thread, h, 2048, 5, 20);
  32. if(h->tid==NULL) {
  33. //LOG_E("___ thread create failed\n");
  34. return -1;
  35. }
  36. h->quit = 0;
  37. rt_thread_startup(h->tid);
  38. return 0;
  39. }
  40. static int mb_stop(mb_handle_t *h)
  41. {
  42. if(h->mode==MB_MODE_MASTER || h->tid==NULL) {
  43. return -1;
  44. }
  45. h->quit = 1;
  46. mb_destory(h->hinst);
  47. while(h->quit!=2);
  48. rt_thread_delete(h->tid);
  49. h->tid = NULL;
  50. return 0;
  51. }
  52. void* mb_init(mb_para_t *para)
  53. {
  54. list_cfg_t lc={0,LIST_FULL_FIFO,10};
  55. mb_handle_t *h=NULL;
  56. if(!para) {
  57. return NULL;
  58. }
  59. h = (mb_handle_t*)MALLOC(sizeof(mb_handle_t));
  60. if(h) {
  61. memset(h, 0, sizeof(mb_handle_t));
  62. mb_setup(h, para);
  63. }
  64. return h;
  65. }
  66. int mb_setup(void *h, mb_para_t *para)
  67. {
  68. int r;
  69. mb_backend_param_t param={0};
  70. mb_handle_t *mh=(mb_handle_t*)h;
  71. if(!mh || !para) {
  72. return -1;
  73. }
  74. mh->type = para->type;
  75. mh->mode = para->mode;
  76. if(mh->type==MB_TYPE_RTU) {
  77. if(mh->hinst) {
  78. mb_disconn(mh->hinst);
  79. }
  80. param.rtu = para->para.rtu;
  81. mh->hinst = mb_create(mh->type, &param);
  82. if(!mh->hinst) {
  83. return -1;
  84. }
  85. r = mb_connect(mh->hinst);
  86. if(r) {
  87. LOGE("___ mb_connect failed\n");
  88. }
  89. }
  90. else if(mh->type==MB_TYPE_TCP) {
  91. }
  92. else {
  93. return -1;
  94. }
  95. mb_start(h);
  96. return 0;
  97. }
  98. int mb_deinit(void *h)
  99. {
  100. mb_handle_t *mh=(mb_handle_t*)h;
  101. if(!mh) {
  102. return -1;
  103. }
  104. mb_stop(mh);
  105. FREE(mh);
  106. return 0;
  107. }
  108. //
  109. int mb_read(void *h, uint8_t addr, uint16_t reg, uint16_t *data, int cnt, int timeout)
  110. {
  111. int r=-1,sz=0;
  112. mb_handle_t *mh=(mb_handle_t*)h;
  113. if(!mh || !data || cnt<=0) {
  114. return -1;
  115. }
  116. #if 0
  117. rt_tick_t end = rt_tick_get_millisecond()+timeout;
  118. while(1) {
  119. sz = list_size(mh->rx);
  120. if(sz>0) {
  121. break;
  122. }
  123. rt_thread_mdelay(1);
  124. if(timeout>0 && rt_tick_get_millisecond()>end) {
  125. sz = list_size(mh->rx);
  126. break;
  127. }
  128. }
  129. if(sz>0) {
  130. list_node_t *ln=NULL;
  131. r = list_take_node(mh->rx, &ln, 0);
  132. if(r==0) {
  133. node_t *nd=&ln->data;
  134. if(len>=nd->dlen) {
  135. memcpy(data, nd->buf, nd->dlen);
  136. }
  137. else {
  138. r = -1;
  139. }
  140. list_back_node(mh->rx, ln);
  141. }
  142. }
  143. #else
  144. mb_set_tmo(mh->hinst, timeout, 50);
  145. mb_set_slave(mh->hinst, addr);
  146. sz = mb_read_regs(mh->hinst, reg, cnt, data);
  147. if(sz==cnt) {
  148. r = 0;
  149. }
  150. else {
  151. r = -1;
  152. }
  153. #endif
  154. return r;
  155. }
  156. int mb_write(void *h, uint8_t addr, uint16_t reg, uint16_t *data, int cnt)
  157. {
  158. int r,sz;
  159. mb_handle_t *mh=(mb_handle_t*)h;
  160. if(!mh) {
  161. return -1;
  162. }
  163. #if 0
  164. r = list_append(mh->tx, 0, data, len);
  165. #else
  166. mb_set_slave(mh->hinst, addr);
  167. r = mb_write_regs(mh->hinst, reg, cnt, data);
  168. if(sz==cnt) {
  169. r = 0;
  170. }
  171. else {
  172. r = -1;
  173. }
  174. #endif
  175. return r;
  176. }