OpenOCD
buffer.c
Go to the documentation of this file.
1 /*
2  * This file is part of the libjaylink project.
3  *
4  * Copyright (C) 2014-2015 Marc Schink <jaylink-dev@marcschink.de>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <stdint.h>
21 #include <string.h>
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include "libjaylink-internal.h"
27 
43 JAYLINK_PRIV void buffer_set_u16(uint8_t *buffer, uint16_t value,
44  size_t offset)
45 {
46  /*
47  * Store the value in the buffer and swap byte order depending on the
48  * host byte order.
49  */
50 #ifdef WORDS_BIGENDIAN
51  buffer[offset + 0] = value;
52  buffer[offset + 1] = value >> 8;
53 #else
54  memcpy(buffer + offset, &value, sizeof(value));
55 #endif
56 }
57 
68 JAYLINK_PRIV uint16_t buffer_get_u16(const uint8_t *buffer, size_t offset)
69 {
70  uint16_t value;
71 
72  /*
73  * Read the value from the buffer and swap byte order depending on the
74  * host byte order.
75  */
76 #ifdef WORDS_BIGENDIAN
77  value = (((uint16_t)buffer[offset + 1])) |
78  (((uint16_t)buffer[offset + 0]) << 8);
79 #else
80  memcpy(&value, buffer + offset, sizeof(value));
81 #endif
82 
83  return value;
84 }
85 
95 JAYLINK_PRIV void buffer_set_u32(uint8_t *buffer, uint32_t value,
96  size_t offset)
97 {
98  /*
99  * Store the value in the buffer and swap byte order depending on the
100  * host byte order.
101  */
102 #ifdef WORDS_BIGENDIAN
103  buffer[offset + 0] = value;
104  buffer[offset + 1] = value >> 8;
105  buffer[offset + 2] = value >> 16;
106  buffer[offset + 3] = value >> 24;
107 #else
108  memcpy(buffer + offset, &value, sizeof(value));
109 #endif
110 }
111 
122 JAYLINK_PRIV uint32_t buffer_get_u32(const uint8_t *buffer, size_t offset)
123 {
124  uint32_t value;
125 
126  /*
127  * Read the value from the buffer and swap byte order depending on the
128  * host byte order.
129  */
130 #ifdef WORDS_BIGENDIAN
131  value = (((uint32_t)buffer[offset + 3])) |
132  (((uint32_t)buffer[offset + 2]) << 8) |
133  (((uint32_t)buffer[offset + 1]) << 16) |
134  (((uint32_t)buffer[offset + 0]) << 24);
135 #else
136  memcpy(&value, buffer + offset, sizeof(value));
137 #endif
138 
139  return value;
140 }
JAYLINK_PRIV uint32_t buffer_get_u32(const uint8_t *buffer, size_t offset)
Read a 32-bit unsigned integer value from a buffer.
Definition: buffer.c:122
uint32_t offset
Definition: arm_cti.c:179
JAYLINK_PRIV void buffer_set_u16(uint8_t *buffer, uint16_t value, size_t offset)
Write a 16-bit unsigned integer value to a buffer.
Definition: buffer.c:43
JAYLINK_PRIV uint16_t buffer_get_u16(const uint8_t *buffer, size_t offset)
Read a 16-bit unsigned integer value from a buffer.
Definition: buffer.c:68
JAYLINK_PRIV void buffer_set_u32(uint8_t *buffer, uint32_t value, size_t offset)
Write a 32-bit unsigned integer value to a buffer.
Definition: buffer.c:95