2006年7月24日 星期一

MSB/LSB issue


If your IC sends MSB first but the destination receives LSB first, transformation of the data has to be taken.



ex:



We send 0x41 (0100 0001), but the receiver will receive 0x82 (1000 0010).

So before the sending, we have to transform 0x41 to 0x82 first.



That's why this routine exists.



static inline uint8_t MSB_LSB( uint8_t indata )

{

   int i = 0, shifts = 0;

   int max_shift = 7;

   uint8_t tmp_var, outdata = 0;

 

   for (i = 0; i < max_shift + 1; ++i)

   {

      tmp_var = indata & (0x01 << i);

      shifts = max_shift - (i << 1);

      if (shifts > 0)

         tmp_var <<= shifts;

      else

      {

         shifts = (i << 1) - max_shift;

         tmp_var >>= shifts;

      }

      outdata |= tmp_var;

   }

 

   printk("MSB_LSB(0x%x) -> 0x%x\n", indata, outdata);

   return outdata;

}


1 則留言:

  1. It&#039;s a Big-Endian or Little-Endian ?
    maybe it depends on hardware circuit~
    sometimes, we always can use hardware-strapping to decide which
    type we needed~


    版主回覆:(07/06/2012 02:56:31 PM)

    回覆刪除