| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- #include "mb.h"
- #include "list.h"
- #define MALLOC malloc
- #define FREE free
- typedef struct {
- mb_inst_t *hinst;
- uint8_t mode;
- uint8_t type;
-
- rt_thread_t tid;
- uint8_t quit;
-
-
- }mb_handle_t;
- static mb_handle_t mbHandle={0};
- typedef void (*thread_fn_t)(void *arg);
- static int mb_stop(mb_handle_t *h);
- static void mb_thread(void *arg)
- {
- mb_handle_t *h=(mb_handle_t*)arg;
-
- while(h->quit==0) {
- mb_slave_fsm(h->hinst);
- }
- h->quit = 2;
- }
- static int mb_start(mb_handle_t *h)
- {
- if(h->mode==MB_MODE_MASTER) {
- return -1;
- }
-
- if(h->tid) {
- mb_stop(h);
- }
-
- h->tid = rt_thread_create(NULL, mb_thread, h, 2048, 5, 20);
- if(h->tid==NULL) {
- //LOG_E("___ thread create failed\n");
- return -1;
- }
-
- h->quit = 0;
- rt_thread_startup(h->tid);
-
- return 0;
- }
- static int mb_stop(mb_handle_t *h)
- {
- if(h->mode==MB_MODE_MASTER || h->tid==NULL) {
- return -1;
- }
- h->quit = 1;
- mb_destory(h->hinst);
- while(h->quit!=2);
- rt_thread_delete(h->tid);
- h->tid = NULL;
-
- return 0;
- }
- void* mb_init(mb_para_t *para)
- {
- list_cfg_t lc={0,LIST_FULL_FIFO,10};
- mb_handle_t *h=NULL;
-
- if(!para) {
- return NULL;
- }
-
- h = (mb_handle_t*)MALLOC(sizeof(mb_handle_t));
- if(h) {
- memset(h, 0, sizeof(mb_handle_t));
- mb_setup(h, para);
- }
-
- return h;
- }
- int mb_setup(void *h, mb_para_t *para)
- {
- int r;
- mb_backend_param_t param={0};
- mb_handle_t *mh=(mb_handle_t*)h;
-
- if(!mh || !para) {
- return -1;
- }
-
- mh->type = para->type;
- mh->mode = para->mode;
- if(mh->type==MB_TYPE_RTU) {
- if(mh->hinst) {
- mb_disconn(mh->hinst);
- }
-
- param.rtu = para->para.rtu;
- mh->hinst = mb_create(mh->type, ¶m);
- if(!mh->hinst) {
- return -1;
- }
- r = mb_connect(mh->hinst);
- if(r) {
- LOGE("___ mb_connect failed\n");
- }
- }
- else if(mh->type==MB_TYPE_TCP) {
-
- }
- else {
- return -1;
- }
- mb_start(h);
-
- return 0;
- }
- int mb_deinit(void *h)
- {
- mb_handle_t *mh=(mb_handle_t*)h;
- if(!mh) {
- return -1;
- }
- mb_stop(mh);
- FREE(mh);
-
- return 0;
- }
- //
- int mb_read(void *h, uint8_t addr, uint16_t reg, uint16_t *data, int cnt, int timeout)
- {
- int r=-1,sz=0;
- mb_handle_t *mh=(mb_handle_t*)h;
-
- if(!mh || !data || cnt<=0) {
- return -1;
- }
- #if 0
- rt_tick_t end = rt_tick_get_millisecond()+timeout;
- while(1) {
- sz = list_size(mh->rx);
- if(sz>0) {
- break;
- }
- rt_thread_mdelay(1);
-
- if(timeout>0 && rt_tick_get_millisecond()>end) {
- sz = list_size(mh->rx);
- break;
- }
- }
-
- if(sz>0) {
- list_node_t *ln=NULL;
- r = list_take_node(mh->rx, &ln, 0);
- if(r==0) {
- node_t *nd=&ln->data;
-
- if(len>=nd->dlen) {
- memcpy(data, nd->buf, nd->dlen);
- }
- else {
- r = -1;
- }
- list_back_node(mh->rx, ln);
- }
- }
- #else
- mb_set_tmo(mh->hinst, timeout, 50);
- mb_set_slave(mh->hinst, addr);
- sz = mb_read_regs(mh->hinst, reg, cnt, data);
- if(sz==cnt) {
- r = 0;
- }
- #endif
-
- return r;
- }
- int mb_write(void *h, uint8_t addr, uint16_t reg, uint16_t *data, int cnt)
- {
- int r=-1,sz;
- mb_handle_t *mh=(mb_handle_t*)h;
-
- if(!mh || !data || cnt<=0) {
- return -1;
- }
- #if 0
- r = list_append(mh->tx, 0, data, len);
- #else
- mb_set_slave(mh->hinst, addr);
- r = mb_write_regs(mh->hinst, reg, cnt, data);
- if(sz==cnt) {
- r = 0;
- }
- #endif
-
- return r;
- }
|