modbus/mb_runtime.c
changeset 2722 5d72a52b8f9c
parent 2721 367eb26972b1
child 2723 cde2e410b874
equal deleted inserted replaced
2721:367eb26972b1 2722:5d72a52b8f9c
   199 
   199 
   200 
   200 
   201 static int __read_inbits   (void *mem_map, u16 start_addr, u16 bit_count, u8  *data_bytes) {
   201 static int __read_inbits   (void *mem_map, u16 start_addr, u16 bit_count, u8  *data_bytes) {
   202   int res = __pack_bits(((server_mem_t *)mem_map)->ro_bits, start_addr, bit_count, data_bytes);
   202   int res = __pack_bits(((server_mem_t *)mem_map)->ro_bits, start_addr, bit_count, data_bytes);
   203   
   203   
   204   if (res >= 0)
   204   if (res >= 0) {
   205     /* update the counter of Modbus requests we have processed. */
   205     /* update the flag and counter of Modbus requests we have processed. */
   206     ((server_mem_t *)mem_map)->flag_read_req_counter++;
   206     ((server_mem_t *)mem_map)->flag_read_req_counter++;
       
   207     ((server_mem_t *)mem_map)->flag_read_req_flag = 1;
       
   208   }
   207 
   209 
   208   return res;
   210   return res;
   209 }
   211 }
   210 
   212 
   211 static int __read_outbits  (void *mem_map, u16 start_addr, u16 bit_count, u8  *data_bytes) {
   213 static int __read_outbits  (void *mem_map, u16 start_addr, u16 bit_count, u8  *data_bytes) {
   212   int res = __pack_bits(((server_mem_t *)mem_map)->rw_bits, start_addr, bit_count, data_bytes);
   214   int res = __pack_bits(((server_mem_t *)mem_map)->rw_bits, start_addr, bit_count, data_bytes);
   213   
   215   
   214   if (res >= 0)
   216   if (res >= 0) {
   215     /* update the counter of Modbus requests we have processed. */
   217     /* update the flag and counter of Modbus requests we have processed. */
   216     ((server_mem_t *)mem_map)->flag_read_req_counter++;
   218     ((server_mem_t *)mem_map)->flag_read_req_counter++;
       
   219     ((server_mem_t *)mem_map)->flag_read_req_flag = 1;
       
   220   }
   217 
   221 
   218   return res;
   222   return res;
   219 }
   223 }
   220 
   224 
   221 static int __write_outbits (void *mem_map, u16 start_addr, u16 bit_count, u8  *data_bytes) {
   225 static int __write_outbits (void *mem_map, u16 start_addr, u16 bit_count, u8  *data_bytes) {
   222   int res = __unpack_bits(((server_mem_t *)mem_map)->rw_bits, start_addr, bit_count, data_bytes);
   226   int res = __unpack_bits(((server_mem_t *)mem_map)->rw_bits, start_addr, bit_count, data_bytes);
   223   
   227   
   224   if (res >= 0)
   228   if (res >= 0) {
   225     /* update the counter of Modbus requests we have processed. */
   229     /* update the flag and counter of Modbus requests we have processed. */
   226     ((server_mem_t *)mem_map)->flag_write_req_counter++;
   230     ((server_mem_t *)mem_map)->flag_write_req_counter++;
       
   231     ((server_mem_t *)mem_map)->flag_write_req_flag = 1;
       
   232   }
   227 
   233 
   228   return res;
   234   return res;
   229 }
   235 }
   230 
   236 
   231 
   237 
   234 static int __read_inwords  (void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) {
   240 static int __read_inwords  (void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) {
   235 
   241 
   236   if ((start_addr + word_count) > MEM_AREA_SIZE)
   242   if ((start_addr + word_count) > MEM_AREA_SIZE)
   237     return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */
   243     return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */
   238 
   244 
   239   /* update the counter of Modbus requests we have processed. */
   245   /* update the flag and counter of Modbus requests we have processed. */
   240   ((server_mem_t *)mem_map)->flag_read_req_counter++;
   246   ((server_mem_t *)mem_map)->flag_read_req_counter++;
       
   247   ((server_mem_t *)mem_map)->flag_read_req_flag = 1;
   241 
   248 
   242   /* use memcpy() because loop with pointers (u16 *) caused alignment problems */
   249   /* use memcpy() because loop with pointers (u16 *) caused alignment problems */
   243   memcpy(/* dest */ (void *)data_words,
   250   memcpy(/* dest */ (void *)data_words,
   244          /* src  */ (void *)&(((server_mem_t *)mem_map)->ro_words[start_addr]),
   251          /* src  */ (void *)&(((server_mem_t *)mem_map)->ro_words[start_addr]),
   245          /* size */ word_count * 2);
   252          /* size */ word_count * 2);
   251 static int __read_outwords (void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) {
   258 static int __read_outwords (void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) {
   252 
   259 
   253   if ((start_addr + word_count) > MEM_AREA_SIZE)
   260   if ((start_addr + word_count) > MEM_AREA_SIZE)
   254     return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */
   261     return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */
   255 
   262 
   256   /* update the counter of Modbus requests we have processed. */
   263   /* update the flag and counter of Modbus requests we have processed. */
   257   ((server_mem_t *)mem_map)->flag_read_req_counter++;
   264   ((server_mem_t *)mem_map)->flag_read_req_counter++;
       
   265   ((server_mem_t *)mem_map)->flag_read_req_flag = 1;
   258 
   266 
   259   /* use memcpy() because loop with pointers (u16 *) caused alignment problems */
   267   /* use memcpy() because loop with pointers (u16 *) caused alignment problems */
   260   memcpy(/* dest */ (void *)data_words,
   268   memcpy(/* dest */ (void *)data_words,
   261          /* src  */ (void *)&(((server_mem_t *)mem_map)->rw_words[start_addr]),
   269          /* src  */ (void *)&(((server_mem_t *)mem_map)->rw_words[start_addr]),
   262          /* size */ word_count * 2);
   270          /* size */ word_count * 2);
   269 static int __write_outwords(void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) {
   277 static int __write_outwords(void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) {
   270 
   278 
   271   if ((start_addr + word_count) > MEM_AREA_SIZE)
   279   if ((start_addr + word_count) > MEM_AREA_SIZE)
   272     return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */
   280     return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */
   273 
   281 
   274   /* update the counter of Modbus requests we have processed. */
   282   /* update the flag and counter of Modbus requests we have processed. */
   275   ((server_mem_t *)mem_map)->flag_write_req_counter++;
   283   ((server_mem_t *)mem_map)->flag_write_req_counter++;
       
   284   ((server_mem_t *)mem_map)->flag_write_req_flag = 1;
   276 
   285 
   277   /* WARNING: The data returned in the data_words[] array is not guaranteed to be 16 bit aligned.
   286   /* WARNING: The data returned in the data_words[] array is not guaranteed to be 16 bit aligned.
   278    *           It is not therefore safe to cast it to an u16 data type.
   287    *           It is not therefore safe to cast it to an u16 data type.
   279    *           The following code cannot be used. memcpy() is used instead.
   288    *           The following code cannot be used. memcpy() is used instead.
   280    */
   289    */
   556 		// mb_nd with negative numbers indicate how far it has been initialised (or not)
   565 		// mb_nd with negative numbers indicate how far it has been initialised (or not)
   557 		//   -2  --> no modbus node created;  no thread  created
   566 		//   -2  --> no modbus node created;  no thread  created
   558 		//   -1  -->    modbus node created!; no thread  created
   567 		//   -1  -->    modbus node created!; no thread  created
   559 		//  >=0  -->    modbus node created!;    thread  created!
   568 		//  >=0  -->    modbus node created!;    thread  created!
   560 		server_nodes[index].mb_nd = -2; 
   569 		server_nodes[index].mb_nd = -2; 
       
   570 		server_nodes[index].mem_area.flag_write_req_flag    = 0; 
   561 		server_nodes[index].mem_area.flag_write_req_counter = 0; 
   571 		server_nodes[index].mem_area.flag_write_req_counter = 0; 
   562 		server_nodes[index].mem_area.flag_read_req_counter  = 0; 
   572 		server_nodes[index].mem_area.flag_read_req_counter  = 0; 
       
   573 		server_nodes[index].mem_area.flag_read_req_flag     = 0; 
   563         /* see comment in mb_runtime.h to understad why we need to initialize these entries */
   574         /* see comment in mb_runtime.h to understad why we need to initialize these entries */
   564         switch (server_nodes[index].node_address.naf) {
   575         switch (server_nodes[index].node_address.naf) {
   565             case naf_tcp:
   576             case naf_tcp:
   566                 server_nodes[index].node_address.addr.tcp.host    = server_nodes[index].str1;
   577                 server_nodes[index].node_address.addr.tcp.host    = server_nodes[index].str1;
   567                 server_nodes[index].node_address.addr.tcp.service = server_nodes[index].str2;
   578                 server_nodes[index].node_address.addr.tcp.service = server_nodes[index].str2;