1 /* MSPDebug - debugging tool for MSP430 MCUs
2  * Copyright (C) 2009-2013 Daniel Beer
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 
19 #ifndef BYTES_H_
20 #define BYTES_H_
21 
22 #include <stdint.h>
23 
24 /* Read/write big-endian 16-bit */
r16be(const uint8_t * buf)25 static inline uint16_t r16be(const uint8_t *buf)
26 {
27 	return (((uint16_t)buf[0]) << 8) | ((uint16_t)buf[1]);
28 }
29 
w16be(uint8_t * buf,uint16_t v)30 static inline void w16be(uint8_t *buf, uint16_t v)
31 {
32 	buf[0] = v >> 8;
33 	buf[1] = v;
34 }
35 
36 /* Read/write little-endian 16-bit */
r16le(const uint8_t * buf)37 static inline uint16_t r16le(const uint8_t *buf)
38 {
39 	return (((uint16_t)buf[1]) << 8) | ((uint16_t)buf[0]);
40 }
41 
w16le(uint8_t * buf,uint16_t v)42 static inline void w16le(uint8_t *buf, uint16_t v)
43 {
44 	buf[1] = v >> 8;
45 	buf[0] = v;
46 }
47 
48 /* Read/write big-endian 32-bit */
r32be(const uint8_t * buf)49 static inline uint32_t r32be(const uint8_t *buf)
50 {
51 	return (((uint32_t)buf[0]) << 24) |
52 	       (((uint32_t)buf[1]) << 16) |
53 	       (((uint32_t)buf[2]) << 8) |
54 	       ((uint32_t)buf[3]);
55 }
56 
w32be(uint8_t * buf,uint32_t v)57 static inline void w32be(uint8_t *buf, uint32_t v)
58 {
59 	buf[0] = v >> 24;
60 	buf[1] = v >> 16;
61 	buf[2] = v >> 8;
62 	buf[3] = v;
63 }
64 
65 /* Read/write little-endian 32-bit */
r32le(const uint8_t * buf)66 static inline uint32_t r32le(const uint8_t *buf)
67 {
68 	return (((uint32_t)buf[3]) << 24) |
69 	       (((uint32_t)buf[2]) << 16) |
70 	       (((uint32_t)buf[1]) << 8) |
71 	       ((uint32_t)buf[0]);
72 }
73 
w32le(uint8_t * buf,uint32_t v)74 static inline void w32le(uint8_t *buf, uint32_t v)
75 {
76 	buf[3] = v >> 24;
77 	buf[2] = v >> 16;
78 	buf[1] = v >> 8;
79 	buf[0] = v;
80 }
81 
82 #endif
83