X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Futil.h;h=18e36524b422304b6ec1b0aa98e5bd0e8486dcc9;hb=bb37203461a4380e32be26594a74b77727dda97d;hp=a84d09975475d03fe05a7bca1715aeb57b3c25b6;hpb=59f04eaa0a96f06ca4b94474c8a3a6ee677541fa;p=home-hw.git diff --git a/lib/util.h b/lib/util.h index a84d099..18e3652 100644 --- a/lib/util.h +++ b/lib/util.h @@ -1,9 +1,17 @@ +/* + * General Utility Library for STM32 + * + * (c) 2018--2019 Martin Mareš + */ + #include #include #include #include "config.h" +// Types + typedef unsigned int uint; typedef uint8_t byte; typedef uint16_t u16; @@ -11,11 +19,17 @@ typedef int16_t s16; typedef uint32_t u32; typedef int32_t s32; +// Macros + #define MIN(x,y) ((x) < (y) ? (x) : (y)) #define MAX(x,y) ((x) > (y) ? (x) : (y)) +#define CLAMP(x,min,max) ({ typeof(x) _t=x; (_t < min) ? min : (_t > max) ? max : _t; }) +#define ARRAY_SIZE(ary) (sizeof(ary)/sizeof((ary)[0])) #define UNUSED __attribute__((unused)) +// Unaligned access to data + static inline uint get_u16_le(byte *p) { return (p[1] << 8) | p[0]; @@ -64,8 +78,26 @@ static inline void put_u32_le(byte *p, u32 x) p[0] = x & 0xff; } -// debug.c +// CPU instructions not covered by libopencm3 + +static inline void wait_for_interrupt(void) +{ + asm volatile ("wfi"); +} -void debug_printf(const char *fmt, ...); +// A compiler memory barrier + +static inline void barrier(void) +{ + asm volatile ("" : : : "memory"); +} + +// util-debug.c + +void debug_printf(const char *fmt, ...) __attribute__((format(printf,1,2))); void debug_puts(const char *s); void debug_putc(int c); +void debug_flush(void); + +void debug_led(bool light); +void debug_led_toggle(void);