modbus/mb_runtime.c
changeset 1913 338e2f51b685
parent 1912 8391c11477f4
child 2011 64268e87613e
equal deleted inserted replaced
1912:8391c11477f4 1913:338e2f51b685
   204   {return __unpack_bits(((server_mem_t *)mem_map)->rw_bits, start_addr, bit_count, data_bytes); }
   204   {return __unpack_bits(((server_mem_t *)mem_map)->rw_bits, start_addr, bit_count, data_bytes); }
   205 
   205 
   206 
   206 
   207 
   207 
   208 static int __read_inwords  (void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) {
   208 static int __read_inwords  (void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) {
   209   u16 count;
   209 
   210   // return -ERR_ILLEGAL_FUNCTION; /* function not yet supported *//* ERR_ILLEGAL_FUNCTION defined in mb_util.h */
       
   211   
       
   212   if ((start_addr + word_count) > MEM_AREA_SIZE)
   210   if ((start_addr + word_count) > MEM_AREA_SIZE)
   213     return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */
   211     return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */
   214   
   212 
   215   /* use memcpy() as it is more efficient...
   213   /* use memcpy() because loop with pointers (u16 *) caused alignment problems */
   216   for (count = 0; count < word_count ; count++)
       
   217     data_words[count] = ((server_mem_t *)mem_map)->ro_words[count + start_addr];
       
   218   */
       
   219   memcpy(/* dest */ (void *)data_words,
   214   memcpy(/* dest */ (void *)data_words,
   220          /* src  */ (void *)&(((server_mem_t *)mem_map)->ro_words[start_addr]),
   215          /* src  */ (void *)&(((server_mem_t *)mem_map)->ro_words[start_addr]),
   221          /* size */ word_count * 2);
   216          /* size */ word_count * 2);
   222   return 0;
   217   return 0;
   223 }
   218 }
   224 
   219 
   225 
   220 
   226 
   221 
   227 static int __read_outwords (void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) {
   222 static int __read_outwords (void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) {
   228   u16 count;
   223 
   229   // return -ERR_ILLEGAL_FUNCTION; /* function not yet supported *//* ERR_ILLEGAL_FUNCTION defined in mb_util.h */
       
   230   
       
   231   if ((start_addr + word_count) > MEM_AREA_SIZE)
   224   if ((start_addr + word_count) > MEM_AREA_SIZE)
   232     return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */
   225     return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */
   233   
   226 
   234   /* use memcpy() as it is more efficient...
   227   /* use memcpy() because loop with pointers (u16 *) caused alignment problems */
   235   for (count = 0; count < word_count ; count++)
       
   236     data_words[count] = ((server_mem_t *)mem_map)->rw_words[count + start_addr];
       
   237   */
       
   238   memcpy(/* dest */ (void *)data_words,
   228   memcpy(/* dest */ (void *)data_words,
   239          /* src  */ (void *)&(((server_mem_t *)mem_map)->rw_words[start_addr]),
   229          /* src  */ (void *)&(((server_mem_t *)mem_map)->rw_words[start_addr]),
   240          /* size */ word_count * 2);
   230          /* size */ word_count * 2);
   241   return 0;
   231   return 0;
   242 }
   232 }
   243 
   233 
   244 
   234 
   245 
   235 
   246 
   236 
   247 static int __write_outwords(void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) {
   237 static int __write_outwords(void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) {
   248   u16 count;
   238 
   249   // return -ERR_ILLEGAL_FUNCTION; /* function not yet supported *//* ERR_ILLEGAL_FUNCTION defined in mb_util.h */
       
   250   
       
   251   if ((start_addr + word_count) > MEM_AREA_SIZE)
   239   if ((start_addr + word_count) > MEM_AREA_SIZE)
   252     return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */
   240     return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */
   253 
   241 
   254   /* WARNING: The data returned in the data_words[] array is not guaranteed to be 16 bit aligned.
   242   /* WARNING: The data returned in the data_words[] array is not guaranteed to be 16 bit aligned.
   255    *           It is not therefore safe to cast it to an u16 data type.
   243    *           It is not therefore safe to cast it to an u16 data type.
   395 	// humour the compiler.
   383 	// humour the compiler.
   396 	return NULL;
   384 	return NULL;
   397 }
   385 }
   398 
   386 
   399 
   387 
   400 
   388 int __cleanup_%(locstr)s ();
   401 int __init_%(locstr)s (int argc, char **argv){
   389 int __init_%(locstr)s (int argc, char **argv){
   402 	int index;
   390 	int index;
   403 
   391 
   404 	for (index=0; index < NUMBER_OF_CLIENT_NODES;index++)
   392 	for (index=0; index < NUMBER_OF_CLIENT_NODES;index++)
   405 		client_nodes[index].mb_nd = -1;
   393 		client_nodes[index].mb_nd = -1;