Skip to content

File daisy_core.h

File List > external-docs > libDaisy > src > daisy_core.h

Go to the documentation of this file

Source Code

#pragma once
#ifndef DSY_CORE_HW_H
#define DSY_CORE_HW_H 
#include <stdint.h>
#include <stdlib.h>

#if defined(_MSC_VER)
#define FORCE_INLINE __forceinline 
#elif defined(__clang__)
#define FORCE_INLINE inline __attribute__((always_inline)) 
#pragma clang diagnostic ignored "-Wduplicate-decl-specifier"
#elif defined(__GNUC__)
#define FORCE_INLINE inline __attribute__((always_inline)) 
#else
#error unknown compiler
#endif

#define DMA_BUFFER_MEM_SECTION __attribute__((section(".sram1_bss")))
#define DTCM_MEM_SECTION __attribute__((section(".dtcmram_bss")))

#define FBIPMAX 0.999985f             
#define FBIPMIN (-FBIPMAX)            
#define U82F_SCALE 0.0078740f         
#define F2U8_SCALE 127.0f             
#define S82F_SCALE 0.0078125f         
#define F2S8_SCALE 127.0f             
#define S162F_SCALE 3.0517578125e-05f 
#define F2S16_SCALE 32767.0f          
#define F2S24_SCALE 8388608.0f        
#define S242F_SCALE 1.192092896e-07f  
#define S24SIGN 0x800000              
#define S322F_SCALE 4.6566129e-10f    
#define F2S32_SCALE 2147483647.f      
#define OUT_L out[0]

#define OUT_R out[1]

#define IN_L in[0]

#define IN_R in[1]

FORCE_INLINE float cube(float x)
{
    return (x * x) * x;
}

FORCE_INLINE float u82f(uint8_t x)
{
    return ((float)x - 127.f) * U82F_SCALE;
}

FORCE_INLINE uint8_t f2u8(float x)
{
    x = x <= FBIPMIN ? FBIPMIN : x;
    x = x >= FBIPMAX ? FBIPMAX : x;
    return (uint8_t)((x * F2U8_SCALE) + F2U8_SCALE);
}


FORCE_INLINE float s82f(int8_t x)
{
    return (float)x * S82F_SCALE;
}

FORCE_INLINE int8_t f2s8(float x)
{
    x = x <= FBIPMIN ? FBIPMIN : x;
    x = x >= FBIPMAX ? FBIPMAX : x;
    return (int32_t)(x * F2S8_SCALE);
}

FORCE_INLINE float s162f(int16_t x)
{
    return (float)x * S162F_SCALE;
}

FORCE_INLINE int16_t f2s16(float x)
{
    x = x <= FBIPMIN ? FBIPMIN : x;
    x = x >= FBIPMAX ? FBIPMAX : x;
    return (int32_t)(x * F2S16_SCALE);
}

FORCE_INLINE float s242f(int32_t x)
{
    x = (x ^ S24SIGN) - S24SIGN; //sign extend aka ((x<<8)>>8)
    return (float)x * S242F_SCALE;
}
FORCE_INLINE int32_t f2s24(float x)
{
    x = x <= FBIPMIN ? FBIPMIN : x;
    x = x >= FBIPMAX ? FBIPMAX : x;
    return (int32_t)(x * F2S24_SCALE);
}

FORCE_INLINE float s322f(int32_t x)
{
    return (float)x * S322F_SCALE;
}
FORCE_INLINE int32_t f2s32(float x)
{
    x = x <= FBIPMIN ? FBIPMIN : x;
    x = x >= FBIPMAX ? FBIPMAX : x;
    return (int32_t)(x * F2S32_SCALE);
}

#ifdef __cplusplus

namespace daisy
{
enum GPIOPort
{
    PORTA, 
    PORTB, 
    PORTC, 
    PORTD, 
    PORTE, 
    PORTF, 
    PORTG, 
    PORTH, 
    PORTI, 
    PORTJ, 
    PORTK, 
    PORTX, 
};

struct Pin
{
    GPIOPort port;
    uint8_t  pin;

    constexpr Pin(const GPIOPort pt, const uint8_t pn) : port(pt), pin(pn) {}

    constexpr Pin() : port(PORTX), pin(255) {}

    constexpr bool IsValid() const { return port != PORTX && pin < 16; }

    constexpr bool operator==(const Pin &rhs) const
    {
        return (rhs.port == port) && (rhs.pin == pin);
    }

    constexpr bool operator!=(const Pin &rhs) const { return !operator==(rhs); }
};


typedef enum
{
    DSY_GPIOA, 
    DSY_GPIOB, 
    DSY_GPIOC, 
    DSY_GPIOD, 
    DSY_GPIOE, 
    DSY_GPIOF, 
    DSY_GPIOG, 
    DSY_GPIOH, 
    DSY_GPIOI, 
    DSY_GPIOJ, 
    DSY_GPIOK, 
    DSY_GPIOX, 
    DSY_GPIO_LAST, 
} dsy_gpio_port;

[[deprecated("Use daisy::Pin instead")]] typedef struct
{
    dsy_gpio_port port; 
    uint8_t       pin;  
    constexpr operator Pin() const
    {
        return Pin(static_cast<GPIOPort>(port), pin);
    }

} dsy_gpio_pin;

} // namespace daisy

#endif // __cplusplus

#endif