summaryrefslogtreecommitdiff
path: root/digital/avr/modules/utils/byte.h
blob: f3565755bf3e92a123404fbfdeb1e9370271ed54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#ifndef byte_h
#define byte_h
/* byte.h */
/* avr.utils - Utilities AVR module. {{{
 *
 * Copyright (C) 2005 Nicolas Schodet
 *
 * Robot APB Team/Efrei 2006.
 *        Web: http://assos.efrei.fr/robot/
 *      Email: robot AT efrei DOT fr
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */

/** WARNING: Theses functions only works on little endian processor, do not
 * use them on other processors, prefer the portable bit shifts and or.  This
 * file only exist to override bad code production with avr-gcc in the bit
 * shifts idiom. */

/* Union needed for byte access. */
union _utils_byte_access
{
    uint8_t v8[4];
    uint16_t v16[2];
    uint32_t v32;
};

/** Byte packing macro, pack 4 bytes into a double word. */
extern inline uint32_t
v8_to_v32 (uint8_t b3, uint8_t b2, uint8_t b1, uint8_t b0)
{
    union _utils_byte_access ba;
    ba.v8[0] = b0;
    ba.v8[1] = b1;
    ba.v8[2] = b2;
    ba.v8[3] = b3;
    return ba.v32;
}

/** Byte packing macro, pack 2 bytes into a word. */
extern inline uint16_t
v8_to_v16 (uint8_t b1, uint8_t b0)
{
    union _utils_byte_access ba;
    ba.v8[0] = b0;
    ba.v8[1] = b1;
    return ba.v16[0];
}

/** Word packing macro, pack 2 words into a double word. */
extern inline uint32_t
v16_to_v32 (uint16_t w1, uint16_t w0)
{
    union _utils_byte_access ba;
    ba.v16[0] = w0;
    ba.v16[1] = w1;
    return ba.v32;
}

/** Byte unpacking macro, extract one specified byte from a double word. */
extern inline uint8_t
v32_to_v8 (uint32_t d, int pos)
{
    union _utils_byte_access ba;
    ba.v32 = d;
    return ba.v8[pos];
}

/** Byte unpacking macro, extract one specified byte from a word. */
extern inline uint8_t
v16_to_v8 (uint16_t w, int pos)
{
    union _utils_byte_access ba;
    ba.v16[0] = w;
    return ba.v8[pos];
}

/** Word unpacking macro, extract one specified word from a double word. */
extern inline uint16_t
v32_to_v16 (uint32_t d, int pos)
{
    union _utils_byte_access ba;
    ba.v32 = d;
    return ba.v16[pos];
}

#endif /* byte_h */