* Spliting u16, u32 to u8 array
@ 2015-08-06 9:39 Crt Mori
2015-08-06 10:42 ` Peter Meerwald
0 siblings, 1 reply; 4+ messages in thread
From: Crt Mori @ 2015-08-06 9:39 UTC (permalink / raw)
To: linux-iio
Hi,
I am sure at least some of you were faced to split a u16 or u32 value
in u8 array without being affected by host endianness. My search
turned empty for a standardized function for this task, but this seems
quite improbable. My task is to send 16 bit LE data through i2c.
I am looking for something like:
static void u16_to_u8_array(u16 value, u8 *array)
{
*array = cpu_to_le16(value) >> 8;
*(++array) = (u8) cpu_to_le16(value);
}
Best regards,
Crt
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Spliting u16, u32 to u8 array
2015-08-06 9:39 Spliting u16, u32 to u8 array Crt Mori
@ 2015-08-06 10:42 ` Peter Meerwald
2015-08-06 11:00 ` Lars-Peter Clausen
0 siblings, 1 reply; 4+ messages in thread
From: Peter Meerwald @ 2015-08-06 10:42 UTC (permalink / raw)
To: Crt Mori; +Cc: linux-iio
Hello,
> I am sure at least some of you were faced to split a u16 or u32 value
> in u8 array without being affected by host endianness. My search
> turned empty for a standardized function for this task, but this seems
> quite improbable. My task is to send 16 bit LE data through i2c.
>
> I am looking for something like:
> static void u16_to_u8_array(u16 value, u8 *array)
> {
> *array = cpu_to_le16(value) >> 8;
> *(++array) = (u8) cpu_to_le16(value);
> }
how about
static void u16_to_le16_array(u16 value, __le16 *array)
{
*array++ = cpu_to_le16(value);
}
and calling it with a cast if need be, such as
char *u8array;
u16_to_le16_array(123, (__le16 *) u8array)
an issue could be alignment of the u8array
I'd try to avoid u8 altogether and work with le16 as a datatype
p.
--
Peter Meerwald
+43-664-2444418 (mobile)
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Spliting u16, u32 to u8 array
2015-08-06 10:42 ` Peter Meerwald
@ 2015-08-06 11:00 ` Lars-Peter Clausen
2015-08-06 11:35 ` Crt Mori
0 siblings, 1 reply; 4+ messages in thread
From: Lars-Peter Clausen @ 2015-08-06 11:00 UTC (permalink / raw)
To: Peter Meerwald, Crt Mori; +Cc: linux-iio
On 08/06/2015 12:42 PM, Peter Meerwald wrote:
> Hello,
>
>> I am sure at least some of you were faced to split a u16 or u32 value
>> in u8 array without being affected by host endianness. My search
>> turned empty for a standardized function for this task, but this seems
>> quite improbable. My task is to send 16 bit LE data through i2c.
>>
>> I am looking for something like:
>> static void u16_to_u8_array(u16 value, u8 *array)
>> {
>> *array = cpu_to_le16(value) >> 8;
>> *(++array) = (u8) cpu_to_le16(value);
>> }
>
> how about
>
> static void u16_to_le16_array(u16 value, __le16 *array)
> {
> *array++ = cpu_to_le16(value);
> }
>
> and calling it with a cast if need be, such as
>
> char *u8array;
> u16_to_le16_array(123, (__le16 *) u8array)
>
> an issue could be alignment of the u8array
There is put_unaligned_le16() and friends for this situation where the
target buffer is just a generic bytestream. This is architecture optimized,
so if the architecture supports unaligned access it will just do a store, if
it doesn't it will split the operation into multiple stores.
>
> I'd try to avoid u8 altogether and work with le16 as a datatype
That seems to be the best solution in this case.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Spliting u16, u32 to u8 array
2015-08-06 11:00 ` Lars-Peter Clausen
@ 2015-08-06 11:35 ` Crt Mori
0 siblings, 0 replies; 4+ messages in thread
From: Crt Mori @ 2015-08-06 11:35 UTC (permalink / raw)
To: Lars-Peter Clausen; +Cc: Peter Meerwald, linux-iio
On 6 August 2015 at 13:00, Lars-Peter Clausen <lars@metafoo.de> wrote:
> On 08/06/2015 12:42 PM, Peter Meerwald wrote:
>> Hello,
>>
>>> I am sure at least some of you were faced to split a u16 or u32 value
>>> in u8 array without being affected by host endianness. My search
>>> turned empty for a standardized function for this task, but this seems
>>> quite improbable. My task is to send 16 bit LE data through i2c.
>>>
>>> I am looking for something like:
>>> static void u16_to_u8_array(u16 value, u8 *array)
>>> {
>>> *array = cpu_to_le16(value) >> 8;
>>> *(++array) = (u8) cpu_to_le16(value);
>>> }
>>
>> how about
>>
>> static void u16_to_le16_array(u16 value, __le16 *array)
>> {
>> *array++ = cpu_to_le16(value);
>> }
>>
>> and calling it with a cast if need be, such as
>>
>> char *u8array;
>> u16_to_le16_array(123, (__le16 *) u8array)
>>
>> an issue could be alignment of the u8array
>
> There is put_unaligned_le16() and friends for this situation where the
> target buffer is just a generic bytestream. This is architecture optimized,
> so if the architecture supports unaligned access it will just do a store, if
> it doesn't it will split the operation into multiple stores.
>
>>
>> I'd try to avoid u8 altogether and work with le16 as a datatype
>
> That seems to be the best solution in this case.
So i2c_master_send takes a char buf as input, and if I would send a le16
array[2] then the cast would nicely fit values it into that.
Thanks for the put_unaligned_le16() hint - exactly what I wanted, but
will investigate the best solution in this case as it seems to require a lot
less lines.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2015-08-06 11:35 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-08-06 9:39 Spliting u16, u32 to u8 array Crt Mori
2015-08-06 10:42 ` Peter Meerwald
2015-08-06 11:00 ` Lars-Peter Clausen
2015-08-06 11:35 ` Crt Mori
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.