libDaisy
Hardware Library for Daisy
Loading...
Searching...
No Matches
daisy_core.h
Go to the documentation of this file.
1#pragma once
2#ifndef DSY_CORE_HW_H
3#define DSY_CORE_HW_H
4#include <stdint.h>
5#include <stdlib.h>
6
7#if defined(_MSC_VER)
8#define FORCE_INLINE __forceinline
9#elif defined(__clang__)
10#define FORCE_INLINE inline __attribute__((always_inline))
11#pragma clang diagnostic ignored "-Wduplicate-decl-specifier"
12#elif defined(__GNUC__)
13#define FORCE_INLINE inline __attribute__((always_inline))
14#else
15#error unknown compiler
16#endif
17
25#define DMA_BUFFER_MEM_SECTION __attribute__((section(".sram1_bss")))
31#define DTCM_MEM_SECTION __attribute__((section(".dtcmram_bss")))
32
33#define FBIPMAX 0.999985f
34#define FBIPMIN (-FBIPMAX)
35#define U82F_SCALE 0.0078740f
36#define F2U8_SCALE 127.0f
37#define S82F_SCALE 0.0078125f
38#define F2S8_SCALE 127.0f
39#define S162F_SCALE 3.0517578125e-05f
40#define F2S16_SCALE 32767.0f
41#define F2S24_SCALE 8388608.0f
42#define S242F_SCALE 1.192092896e-07f
43#define S24SIGN 0x800000
44#define S322F_SCALE 4.6566129e-10f
45#define F2S32_SCALE 2147483647.f
50#define OUT_L out[0]
51
54#define OUT_R out[1]
55
58#define IN_L in[0]
59
62#define IN_R in[1]
63
69FORCE_INLINE float cube(float x)
70{
71 return (x * x) * x;
72}
73
79FORCE_INLINE float u82f(uint8_t x)
80{
81 return ((float)x - 127.f) * U82F_SCALE;
82}
83
87FORCE_INLINE uint8_t f2u8(float x)
88{
89 x = x <= FBIPMIN ? FBIPMIN : x;
90 x = x >= FBIPMAX ? FBIPMAX : x;
91 return (uint8_t)((x * F2U8_SCALE) + F2U8_SCALE);
92}
93
94
100FORCE_INLINE float s82f(int8_t x)
101{
102 return (float)x * S82F_SCALE;
103}
104
108FORCE_INLINE int8_t f2s8(float x)
109{
110 x = x <= FBIPMIN ? FBIPMIN : x;
111 x = x >= FBIPMAX ? FBIPMAX : x;
112 return (int32_t)(x * F2S8_SCALE);
113}
114
120FORCE_INLINE float s162f(int16_t x)
121{
122 return (float)x * S162F_SCALE;
123}
124
128FORCE_INLINE int16_t f2s16(float x)
129{
130 x = x <= FBIPMIN ? FBIPMIN : x;
131 x = x >= FBIPMAX ? FBIPMAX : x;
132 return (int32_t)(x * F2S16_SCALE);
133}
134
138FORCE_INLINE float s242f(int32_t x)
139{
140 x = (x ^ S24SIGN) - S24SIGN; //sign extend aka ((x<<8)>>8)
141 return (float)x * S242F_SCALE;
142}
146FORCE_INLINE int32_t f2s24(float x)
147{
148 x = x <= FBIPMIN ? FBIPMIN : x;
149 x = x >= FBIPMAX ? FBIPMAX : x;
150 return (int32_t)(x * F2S24_SCALE);
151}
152
156FORCE_INLINE float s322f(int32_t x)
157{
158 return (float)x * S322F_SCALE;
159}
163FORCE_INLINE int32_t f2s32(float x)
164{
165 x = x <= FBIPMIN ? FBIPMIN : x;
166 x = x >= FBIPMAX ? FBIPMAX : x;
167 return (int32_t)(x * F2S32_SCALE);
168}
169
170
196
204typedef struct
205{
207 uint8_t pin;
209
217FORCE_INLINE dsy_gpio_pin dsy_pin(dsy_gpio_port port, uint8_t pin)
218{
219 dsy_gpio_pin p;
220 p.port = port;
221 p.pin = pin;
222 return p;
223}
224
233FORCE_INLINE uint8_t dsy_pin_cmp(dsy_gpio_pin *a, dsy_gpio_pin *b)
234{
235 return ((a->port == b->port) && (a->pin == b->pin));
236}
237
238#ifdef __cplusplus
239
240namespace daisy
241{
258
260struct Pin
261{
263 uint8_t pin;
264
269 constexpr Pin(const GPIOPort pt, const uint8_t pn) : port(pt), pin(pn) {}
270
272 constexpr Pin() : port(PORTX), pin(255) {}
273
277 constexpr bool IsValid() const { return port != PORTX && pin < 16; }
278
280 constexpr bool operator==(const Pin &rhs) const
281 {
282 return (rhs.port == port) && (rhs.pin == pin);
283 }
284
286 constexpr bool operator!=(const Pin &rhs) const { return !operator==(rhs); }
287
294 constexpr operator dsy_gpio_pin() const
295 {
296 return dsy_gpio_pin{.port = static_cast<dsy_gpio_port>(port),
297 .pin = pin};
298 }
299};
300
301} // namespace daisy
302
303#endif // __cplusplus
304
305#endif
#define F2S32_SCALE
Definition daisy_core.h:45
FORCE_INLINE float u82f(uint8_t x)
Definition daisy_core.h:79
FORCE_INLINE float s82f(int8_t x)
Definition daisy_core.h:100
FORCE_INLINE int16_t f2s16(float x)
Definition daisy_core.h:128
#define F2S16_SCALE
Definition daisy_core.h:40
dsy_gpio_port
Definition daisy_core.h:181
#define S242F_SCALE
Definition daisy_core.h:42
#define S24SIGN
Definition daisy_core.h:43
FORCE_INLINE float s162f(int16_t x)
Definition daisy_core.h:120
FORCE_INLINE float cube(float x)
Definition daisy_core.h:69
#define F2S24_SCALE
Definition daisy_core.h:41
FORCE_INLINE float s322f(int32_t x)
Definition daisy_core.h:156
FORCE_INLINE int32_t f2s32(float x)
Definition daisy_core.h:163
FORCE_INLINE uint8_t dsy_pin_cmp(dsy_gpio_pin *a, dsy_gpio_pin *b)
Definition daisy_core.h:233
FORCE_INLINE int8_t f2s8(float x)
Definition daisy_core.h:108
#define F2U8_SCALE
Definition daisy_core.h:36
#define FBIPMAX
Definition daisy_core.h:33
#define FBIPMIN
Definition daisy_core.h:34
FORCE_INLINE uint8_t f2u8(float x)
Definition daisy_core.h:87
#define F2S8_SCALE
Definition daisy_core.h:38
#define S322F_SCALE
Definition daisy_core.h:44
#define S162F_SCALE
Definition daisy_core.h:39
FORCE_INLINE dsy_gpio_pin dsy_pin(dsy_gpio_port port, uint8_t pin)
Definition daisy_core.h:217
#define U82F_SCALE
Definition daisy_core.h:35
#define S82F_SCALE
Definition daisy_core.h:37
FORCE_INLINE int32_t f2s24(float x)
Definition daisy_core.h:146
FORCE_INLINE float s242f(int32_t x)
Definition daisy_core.h:138
@ DSY_GPIOB
Definition daisy_core.h:183
@ DSY_GPIOI
Definition daisy_core.h:190
@ DSY_GPIOG
Definition daisy_core.h:188
@ DSY_GPIOJ
Definition daisy_core.h:191
@ DSY_GPIOK
Definition daisy_core.h:192
@ DSY_GPIOD
Definition daisy_core.h:185
@ DSY_GPIOH
Definition daisy_core.h:189
@ DSY_GPIOX
Definition daisy_core.h:193
@ DSY_GPIOF
Definition daisy_core.h:187
@ DSY_GPIOE
Definition daisy_core.h:186
@ DSY_GPIOA
Definition daisy_core.h:182
@ DSY_GPIOC
Definition daisy_core.h:184
@ DSY_GPIO_LAST
Definition daisy_core.h:194
Hardware defines and helpers for daisy field platform.
Definition index.h:2
GPIOPort
GPIO Port names.
Definition daisy_core.h:244
@ PORTX
Definition daisy_core.h:256
@ PORTH
Definition daisy_core.h:252
@ PORTE
Definition daisy_core.h:249
@ PORTJ
Definition daisy_core.h:254
@ PORTK
Definition daisy_core.h:255
@ PORTA
Definition daisy_core.h:245
@ PORTF
Definition daisy_core.h:250
@ PORTB
Definition daisy_core.h:246
@ PORTI
Definition daisy_core.h:253
@ PORTC
Definition daisy_core.h:247
@ PORTD
Definition daisy_core.h:248
@ PORTG
Definition daisy_core.h:251
representation of hardware port/pin combination
Definition daisy_core.h:261
GPIOPort port
Definition daisy_core.h:262
constexpr Pin()
Basic Constructor creates an invalid Pin object.
Definition daisy_core.h:272
constexpr bool operator!=(const Pin &rhs) const
comparison operator for checking inequality between Pin objects
Definition daisy_core.h:286
constexpr bool operator==(const Pin &rhs) const
comparison operator for checking equality between Pin objects
Definition daisy_core.h:280
uint8_t pin
Definition daisy_core.h:263
constexpr bool IsValid() const
checks validity of a Pin
Definition daisy_core.h:277
constexpr Pin(const GPIOPort pt, const uint8_t pn)
Constructor creates a valid pin.
Definition daisy_core.h:269
Definition daisy_core.h:205
uint8_t pin
Definition daisy_core.h:207
dsy_gpio_port port
Definition daisy_core.h:206