utils.h
Go to the documentation of this file.
1 // coding: utf-8
2 // ----------------------------------------------------------------------------
3 /*
4  * Copyright (c) 2007 Roboterclub Aachen e.V.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 // ----------------------------------------------------------------------------
29 
30 #ifndef UTILS_H
31 #define UTILS_H
32 
33 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
41 
42 #include <avr/io.h>
43 #include <avr/interrupt.h>
44 
45 #include <util/delay.h>
46 
47 //#include "config.h"
48 
49 // ----------------------------------------------------------------------------
50 
51 #ifndef TRUE
52  #define TRUE (1==1)
53 #elif !TRUE
54  #error fehlerhafte Definition fuer TRUE
55 #endif
56 
57 #ifndef FALSE
58  #define FALSE (1!=1)
59 #elif FALSE
60  #error fehlerhafte Definition fuer FALSE
61 #endif
62 
63 #ifndef NULL
64  #define NULL ((void*)0)
65 #endif
66 
67 // ----------------------------------------------------------------------------
73 
74 #define DEGREE_TO_RAD(x) ((x * M_PI) / 180)
75 
77 // ----------------------------------------------------------------------------
83 
84 #define LOW_BYTE(x) ((uint8_t) (x & 0xff))
85 #define HIGH_BYTE(x) ((uint8_t) (x >> 8))
86 #define LOW_WORD(x) ((uint16_t) (x & 0xffff))
87 #define HIGH_WORD(x) ((uint16_t) (x >> 16))
88 
90 // ----------------------------------------------------------------------------
94 typedef struct {
95  uint8_t b4; // lsb
98  uint8_t b1; // msb
100 
101 // ----------------------------------------------------------------------------
107 
108 #if defined(__DOXYGEN__)
109 
110 #define ENTER_CRITICAL_SECTION
111 #define LEAVE_CRITICAL_SECTION
112 
113 #else /* !DOXYGEN */
114 
115 #if __AVR_LIBC_VERSION__ >= 10600 && !defined (__cplusplus)
116 
117  #include <util/atomic.h>
118 
119  #define ENTER_CRITICAL_SECTION ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
120  #define LEAVE_CRITICAL_SECTION }
121 
122  #define IRQ_LOCK ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
123 
124 #else
125 
126  #define ENTER_CRITICAL_SECTION do { unsigned char sreg_ = SREG; cli();
127  #define LEAVE_CRITICAL_SECTION SREG = sreg_; } while (0);
128 
129 #endif
130 #endif /* DOXYGEN */
131 
133 // ----------------------------------------------------------------------------
138 static inline uint8_t read_and_replace_atomar(volatile uint8_t *data, uint8_t new_data)
139 {
140  uint8_t old_data;
141 
143 
144  // Daten tauschen
145  old_data = *data;
146  *data = new_data;
147 
149 
150  return old_data;
151 }
152 
153 // ----------------------------------------------------------------------------
159 
160 #define vu8(x) (*(volatile uint8_t*)&(x))
161 #define vs8(x) (*(volatile int8_t*)&(x))
162 #define vu16(x) (*(volatile uint16_t*)&(x))
163 #define vs16(x) (*(volatile int16_t*)&(x))
164 #define vu32(x) (*(volatile uint32_t*)&(x))
165 #define vs32(x) (*(volatile int32_t*)&(x))
166 
168 // ----------------------------------------------------------------------------
203 #if defined(__DOXYGEN__)
204 
205 #define RESET(x)
206 #define SET(x)
207 #define TOGGLE(x)
208 
209 #define SET_OUTPUT(x)
210 #define SET_INPUT(x)
211 #define SET_PULLUP(x)
212 
213 #define SET_INPUT_WITH_PULLUP(x)
214 
215 #define IS_SET(x)
216 
217 #else /* !DOXYGEN */
218 
219 /* Warum hier zum Teil so seltsame Konstrukte notwendig sind wird zum Beispiel
220  * in http://www.mikrocontroller.net/forum/read-1-324854.html#324980 erklärt.
221  */
222 #define PORT(x) _port2(x)
223 #define DDR(x) _ddr2(x)
224 #define PIN(x) _pin2(x)
225 #define REG(x) _reg(x)
226 #define PIN_NUM(x) _pin_num(x)
227 
228 #define RESET(x) RESET2(x)
229 #define SET(x) SET2(x)
230 #define TOGGLE(x) TOGGLE2(x)
231 #define SET_OUTPUT(x) SET_OUTPUT2(x)
232 #define SET_INPUT(x) SET_INPUT2(x)
233 #define SET_PULLUP(x) SET2(x)
234 #define IS_SET(x) IS_SET2(x)
235 
236 #define SET_INPUT_WITH_PULLUP(x) SET_INPUT_WITH_PULLUP2(x)
237 
238 #define _port2(x) PORT ## x
239 #define _ddr2(x) DDR ## x
240 #define _pin2(x) PIN ## x
241 
242 #define _reg(x,y) x
243 #define _pin_num(x,y) y
244 
245 #define RESET2(x,y) PORT(x) &= ~(1<<y)
246 #define SET2(x,y) PORT(x) |= (1<<y)
247 #define TOGGLE2(x,y) PORT(x) ^= (1<<y)
248 
249 #define SET_OUTPUT2(x,y) DDR(x) |= (1<<y)
250 #define SET_INPUT2(x,y) DDR(x) &= ~(1<<y)
251 #define SET_INPUT_WITH_PULLUP2(x,y) SET_INPUT2(x,y);SET2(x,y)
252 
253 #define IS_SET2(x,y) ((PIN(x) & (1<<y)) != 0)
254 
255 #endif /* DOXYGEN */
256 
257 
258 // ----------------------------------------------------------------------------
264 #if defined(__DOXYGEN__)
265 
266 #define _bit_is_set(pin, bit)
267 #define _bit_is_clear(pin, bit)
268 
269 #define STRING(x)
270 
271 #else /* !DOXYGEN */
272 
273 #define _bit_is_set(pin, bit) (pin & (1<<bit))
274 #define _bit_is_clear(pin, bit) (!(pin & (1<<bit)))
275 
276 #define STRING(x) _STRING(x)
277 #define _STRING(x) #x
278 
279 #endif /* DOXYGEN */
280 
281 
282 // ----------------------------------------------------------------------------
289 static inline uint8_t swap (uint8_t x)
290 {
291  if (__builtin_constant_p(x))
292  x = (x << 4) | (x >> 4);
293  else
294  asm volatile ("swap %0" : "=r" (x) : "0" (x));
295 
296  return x;
297 }
298 
299 // ----------------------------------------------------------------------------
304 #if defined(DEBUG_LEVEL) && DEBUG_LEVEL
305  #include <stdio.h>
306  #define DEBUG_PRINT(s, ...) do { static const char __s[] PROGMEM = (s); \
307  printf_P(__s, ## __VA_ARGS__); } while (0)
308 #else
309  #define DEBUG_PRINT(s, ...)
310 
311 #endif
312 
313 // ----------------------------------------------------------------------------
323 static inline uint8_t bit_count8(uint8_t n)
324 {
325  n = ((n >> 1) & 0x55) + (n & 0x55);
326  n = ((n >> 2) & 0x33) + (n & 0x33);
327  n = ((n >> 4) + n) & 0xf;
328 
329  return n;
330 }
331 
332 #define MASK_01010101 (((uint32_t)(-1))/3)
333 #define MASK_00110011 (((uint32_t)(-1))/5)
334 #define MASK_00001111 (((uint32_t)(-1))/17)
335 
336 // ----------------------------------------------------------------------------
344 static inline uint8_t bit_count32(uint32_t n)
345 {
346  n = (n & MASK_01010101) + ((n >> 1) & MASK_01010101);
347  n = (n & MASK_00110011) + ((n >> 2) & MASK_00110011);
348  n = (n & MASK_00001111) + ((n >> 4) & MASK_00001111);
349 
350  return n % 255 ;
351 }
352 
353 
354 #define START_TIMED_BLOCK(time, gettime) \
355  do { \
356  static uint16_t last_time__; \
357  uint16_t current_time__ = gettime; \
358  if ((uint16_t) (current_time__ - last_time__) > time) { \
359  last_time__ = current_time__;
360 
361 #define END_TIMED_BLOCK \
362  } \
363  } while (0);
364 
365 // ----------------------------------------------------------------------------
366 #define TO_DEG(x) (x * 180.0 / M_PI)
367 #define TO_RAD(x) (x * M_PI / 180.0)
368 
374 #define USE_IT(x) (void) x
375 
376 #endif // UTILS_H
ENTER_CRITICAL_SECTION
#define ENTER_CRITICAL_SECTION
Definition: utils.h:126
long_to_byte_t::b3
uint8_t b3
Definition: utils.h:96
uavcan::uint32_t
std::uint32_t uint32_t
Definition: std.hpp:26
MASK_00001111
#define MASK_00001111
Definition: utils.h:334
MASK_00110011
#define MASK_00110011
Definition: utils.h:333
read_and_replace_atomar
static uint8_t read_and_replace_atomar(volatile uint8_t *data, uint8_t new_data)
atomare Operationen
Definition: utils.h:138
long_to_byte_t::b2
uint8_t b2
Definition: utils.h:97
uavcan::uint8_t
std::uint8_t uint8_t
Definition: std.hpp:24
MASK_01010101
#define MASK_01010101
Definition: utils.h:332
bit_count8
static uint8_t bit_count8(uint8_t n)
Zählt die Anzahl der gesetzten Bits in einem Byte.
Definition: utils.h:323
swap
static uint8_t swap(uint8_t x)
Dreht die beiden Nibble in einem Byte um.
Definition: utils.h:289
long_to_byte_t::b4
uint8_t b4
Definition: utils.h:95
LEAVE_CRITICAL_SECTION
#define LEAVE_CRITICAL_SECTION
Definition: utils.h:127
bit_count32
static uint8_t bit_count32(uint32_t n)
Zählt die Anzahl der gesetzten Bits in einem Byte.
Definition: utils.h:344
long_to_byte_t
Definition: utils.h:94
long_to_byte_t::b1
uint8_t b1
Definition: utils.h:98
pyuavcan_v0.driver.timestamp_estimator.x
x
Definition: timestamp_estimator.py:221


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:03