From f7dd38f85aecf8995ffa5068d7a2cccc590c2d88 Mon Sep 17 00:00:00 2001 From: Katherine Whitlock Date: Fri, 20 Sep 2024 11:43:50 -0400 Subject: [PATCH] Global GPIO/Pin Migration (#584) * Update DaisySeed to use GPIO * Update DaisyPatch to use GPIO * Update daisy::patch_sm to use GPIO * Update DaisyField to use GPIO * Update LED and RGBLed to use GPIO and Pin * Update AK4556 CODEC to use GPIO and Pin This also finally switches the AK4556 CODEC from a static set of functions to instance variables. This is required due to how GPIO instances update their internal port_base_addr_ on Init(), which is then later required for DeInit(). * Update HD44780 driver to use GPIO and Pin * Update PCA9685 driver to use GPIO and Pin * Update SSD130X driver to use GPIO and Pin * Move SR595 Driver into daisy namespace * Update SR595 driver to use GPIO and Pin * Update 4021 driver with GPIO and Pin * Update Encoder to use GPIO and Pin * Update Switch driver to use GPIO and Pin Also migrates from internal custom Pull representation to the global GPIO::Pull * Update Switch3 to use GPIO and Pin * Add Init() to GPIO for 0-parameter initialization * Update ADC to use GPIO and Pin * Update system to use GPIO and Pin * Update multi-peripheral SPI driver with GPIO/Pin * Update Midi to use Pin * Update TLV493D to use Pin * Update MCP23x17 and MAX11300 to use Pin * Update DAC driver to use GPIO and Pin * Update I2C driver to use GPIO and Pin * Update UART to use GPIO and Pin * Update SPI peripheral to use GPIO and Pin * Update SAI peripheral to use GPIO and Pin * Update QSPI peripheral to use GPIO and Pin * Update outdated documentation * Style compliance * Fix ResetToBootloader (merge error) * Replace old dsy_gpio in DaisySeed with modern GPIO * Move patch_sm to modern GPIO * Rip up all old GPIO usage * Remove hal_map.c from build systems * Reintroduce dsy_gpio_pin + implicit conversion to daisy::Pin Also: Added a deprecation warning to the dsy_gpio_pin struct Left out some of the additional stuff surrounding that struct, e.g. comparison operators, implicit cast from Pin to dsy_gpio_pin These would throw the deprecation warning, so theyve been left out I also added an implicit cast from dsy_gpio_pin to daisy::Pin This required moving the dsy_gpio_pin and port inside the daisy namespace * Fix unit tests Remove test making sure new pin could cast to old Add two conditionals so the hal_map wont be included during some tests * Initialize DaisySeed::seedgpio using constexpr pins from header --------- Co-authored-by: beserge --- CMakeLists.txt | 1 - Makefile | 1 - src/daisy.h | 1 + src/daisy_core.h | 122 ++++++++-------------- src/daisy_field.cpp | 5 +- src/daisy_field.h | 2 +- src/daisy_patch.cpp | 9 +- src/daisy_patch.h | 13 +-- src/daisy_patch_sm.cpp | 21 +--- src/daisy_patch_sm.h | 6 +- src/daisy_seed.cpp | 207 ++++++++++++++++++-------------------- src/daisy_seed.h | 5 +- src/dev/codec_ak4556.cpp | 20 ++-- src/dev/codec_ak4556.h | 12 +-- src/dev/lcd_hd44780.cpp | 45 ++------- src/dev/lcd_hd44780.h | 18 ++-- src/dev/leddriver.h | 19 ++-- src/dev/max11300.h | 8 +- src/dev/mcp23x17.h | 4 +- src/dev/oled_ssd130x.h | 104 +++++++++---------- src/dev/sdram.cpp | 4 - src/dev/sr_4021.h | 39 +++---- src/dev/sr_595.cpp | 24 ++--- src/dev/sr_595.h | 14 ++- src/dev/tlv493d.h | 8 +- src/hid/encoder.cpp | 19 +--- src/hid/encoder.h | 7 +- src/hid/gatein.cpp | 11 -- src/hid/gatein.h | 15 --- src/hid/led.cpp | 15 +-- src/hid/led.h | 14 +-- src/hid/midi.cpp | 4 +- src/hid/midi.h | 4 +- src/hid/rgb_led.cpp | 5 +- src/hid/rgb_led.h | 3 +- src/hid/switch.cpp | 36 +++---- src/hid/switch.h | 22 ++-- src/hid/switch3.h | 21 ++-- src/per/adc.cpp | 170 ++++++++++++------------------- src/per/adc.h | 14 +-- src/per/dac.cpp | 24 +---- src/per/dac.h | 2 +- src/per/gpio.cpp | 95 +---------------- src/per/gpio.h | 75 +------------- src/per/i2c.cpp | 20 ++-- src/per/i2c.h | 7 +- src/per/qspi.cpp | 26 ++--- src/per/qspi.h | 15 +-- src/per/rng.cpp | 2 +- src/per/sai.cpp | 49 ++++----- src/per/sai.h | 3 +- src/per/sdmmc.cpp | 3 - src/per/sdmmc.h | 2 +- src/per/spi.cpp | 127 +++++++++++------------ src/per/spi.h | 12 ++- src/per/spiMultislave.cpp | 11 +- src/per/spiMultislave.h | 10 +- src/per/tim.cpp | 1 - src/per/tim.h | 1 + src/per/uart.cpp | 88 ++++++++-------- src/per/uart.h | 9 +- src/sys/system.cpp | 11 +- src/util/hal_map.c | 74 -------------- src/util/hal_map.h | 67 ++++++++---- tests/Pin_gtest.cpp | 13 --- 65 files changed, 667 insertions(+), 1152 deletions(-) delete mode 100644 src/util/hal_map.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 1fafa419f..58b0ea766 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,7 +16,6 @@ add_library(${TARGET} STATIC ${MODULE_DIR}/per/sai.cpp ${MODULE_DIR}/per/sdmmc.cpp ${MODULE_DIR}/util/bsp_sd_diskio.c - ${MODULE_DIR}/util/hal_map.c ${MODULE_DIR}/util/oled_fonts.c ${MODULE_DIR}/util/sd_diskio.c ${MODULE_DIR}/util/usbh_diskio.c diff --git a/Makefile b/Makefile index c3232f287..664a0811f 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,6 @@ per/gpio \ per/sai \ per/sdmmc \ util/bsp_sd_diskio \ -util/hal_map \ util/oled_fonts \ util/sd_diskio \ util/unique_id \ diff --git a/src/daisy.h b/src/daisy.h index 88a26795c..c629ba8b3 100644 --- a/src/daisy.h +++ b/src/daisy.h @@ -43,6 +43,7 @@ #include "hid/rgb_led.h" #include "dev/sr_595.h" #include "dev/apds9960.h" +#include "dev/codec_ak4556.h" #include "dev/codec_pcm3060.h" #include "dev/codec_wm8731.h" #include "dev/dps310.h" diff --git a/src/daisy_core.h b/src/daisy_core.h index c0bfe3cdc..0908c7044 100644 --- a/src/daisy_core.h +++ b/src/daisy_core.h @@ -167,74 +167,6 @@ FORCE_INLINE int32_t f2s32(float x) return (int32_t)(x * F2S32_SCALE); } - -/** Enums and a simple struct for defining a hardware pin on the MCU - * These correlate with the stm32 datasheet, and are used to configure - * the hardware. - * - * This along with the dsy_gpio_pin class should no longer be used. - * They are available for backwards compatability. - * - * Please use GPIOPort enum and the Pin struct instead. - */ -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, /** This is a non-existant port for unsupported bits of hardware. */ - DSY_GPIO_LAST, /** Final enum member */ -} dsy_gpio_port; - -/** Hardware define pins - * - * The dsy_gpio_pin struct should no longer be used, and is only available for - * backwards compatability. - * - * Please use Pin struct instead. - */ -typedef struct -{ - dsy_gpio_port port; /**< & */ - uint8_t pin; /**< number 0-15 */ -} dsy_gpio_pin; - -/** Helper for creating pins from port/pin combos easily - * - * The dsy_gpio_pin struct should no longer be used, and is only available for - * backwards compatability. - * - * Please use Pin struct instead. -*/ -FORCE_INLINE dsy_gpio_pin dsy_pin(dsy_gpio_port port, uint8_t pin) -{ - dsy_gpio_pin p; - p.port = port; - p.pin = pin; - return p; -} - -/** Helper for testing sameness of two dsy_gpio_pins - * \return 1 if same, 0 if different - * - * The dsy_gpio_pin struct should no longer be used, and is only available for - * backwards compatability. - * - * Please use Pin struct instead. - */ -FORCE_INLINE uint8_t dsy_pin_cmp(dsy_gpio_pin *a, dsy_gpio_pin *b) -{ - return ((a->port == b->port) && (a->pin == b->pin)); -} - #ifdef __cplusplus namespace daisy @@ -284,19 +216,53 @@ struct Pin /** @brief comparison operator for checking inequality between Pin objects */ constexpr bool operator!=(const Pin &rhs) const { return !operator==(rhs); } +}; + - /** @brief conversion operation for converting to the old-style representation - * of a pin. - * - * This allows the new Pin type to be used in place of the older, dsy_gpio_pin - * type. - */ - constexpr operator dsy_gpio_pin() const +/** Enums and a simple struct for defining a hardware pin on the MCU + * These correlate with the stm32 datasheet, and are used to configure + * the hardware. + * + * This along with the dsy_gpio_pin class should no longer be used. + * They are available for backwards compatability. + * + * Please use GPIOPort enum and the Pin struct instead. + */ +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, /** This is a non-existant port for unsupported bits of hardware. */ + DSY_GPIO_LAST, /** Final enum member */ +} dsy_gpio_port; + +/** Hardware define pins + * + * The dsy_gpio_pin struct should no longer be used, and is only available for + * backwards compatability. + * + * Please use Pin struct instead. + */ +[[deprecated("Use daisy::Pin instead")]] typedef struct +{ + dsy_gpio_port port; /**< & */ + uint8_t pin; /**< number 0-15 */ + + constexpr operator Pin() const { - return dsy_gpio_pin{.port = static_cast(port), - .pin = pin}; + return Pin(static_cast(port), pin); } -}; + +} dsy_gpio_pin; } // namespace daisy diff --git a/src/daisy_field.cpp b/src/daisy_field.cpp index f5c851528..747ff3ec4 100644 --- a/src/daisy_field.cpp +++ b/src/daisy_field.cpp @@ -142,10 +142,7 @@ void DaisyField::Init(bool boost) // Gate In gate_in.Init(PIN_GATE_IN); // Gate Out - gate_out.mode = DSY_GPIO_MODE_OUTPUT_PP; - gate_out.pull = DSY_GPIO_NOPULL; - gate_out.pin = PIN_GATE_OUT; - dsy_gpio_init(&gate_out); + gate_out.Init(PIN_GATE_OUT, GPIO::Mode::OUTPUT); //midi MidiUartHandler::Config midi_config; diff --git a/src/daisy_field.h b/src/daisy_field.h index c31ef368d..574270541 100644 --- a/src/daisy_field.h +++ b/src/daisy_field.h @@ -212,7 +212,7 @@ class DaisyField DaisySeed seed; OledDisplay display; - dsy_gpio gate_out; + GPIO gate_out; GateIn gate_in; LedDriverPca9685<2, true> led_driver; Switch sw[SW_LAST]; diff --git a/src/daisy_patch.cpp b/src/daisy_patch.cpp index 6e070a29b..172631520 100644 --- a/src/daisy_patch.cpp +++ b/src/daisy_patch.cpp @@ -1,5 +1,4 @@ #include "daisy_patch.h" -#include "dev/codec_ak4556.h" using namespace daisy; @@ -216,8 +215,7 @@ void DaisyPatch::InitAudio() // Reset Pin for AK4556 // Built-in AK4556 was reset during Seed Init - dsy_gpio_pin codec_reset_pin = PIN_AK4556_RESET; - Ak4556::Init(codec_reset_pin); + codec.Init(PIN_AK4556_RESET); // Reinit Audio for _both_ codecs... AudioHandle::Config cfg; @@ -286,10 +284,7 @@ void DaisyPatch::InitEncoder() void DaisyPatch::InitGates() { // Gate Output - gate_output.pin = PIN_GATE_OUT; - gate_output.mode = DSY_GPIO_MODE_OUTPUT_PP; - gate_output.pull = DSY_GPIO_NOPULL; - dsy_gpio_init(&gate_output); + gate_output.Init(PIN_GATE_OUT, GPIO::Mode::OUTPUT); // Gate Inputs gate_input[GATE_IN_1].Init(PIN_GATE_IN_1); diff --git a/src/daisy_patch.h b/src/daisy_patch.h index 68b16c500..a3f01d1ab 100644 --- a/src/daisy_patch.h +++ b/src/daisy_patch.h @@ -114,15 +114,16 @@ class DaisyPatch /* These are exposed for the user to access and manipulate directly Helper functions above provide easier access to much of what they are capable of. */ - DaisySeed seed; /**< Seed object */ - Encoder encoder; /**< Encoder object */ - AnalogControl controls[CTRL_LAST]; /**< Array of controls*/ - GateIn gate_input[GATE_IN_LAST]; /**< Gate inputs */ - MidiUartHandler midi; /**< Handles midi*/ + DaisySeed seed; /**< Seed object */ + Ak4556 codec; /**< Patch's second CODEC */ + Encoder encoder; /**< Encoder object */ + AnalogControl controls[CTRL_LAST]; /**< Array of controls*/ + GateIn gate_input[GATE_IN_LAST]; /**< Gate inputs */ + MidiUartHandler midi; /**< Handles midi*/ OledDisplay display; /**< & */ // TODO: Add class for Gate output - dsy_gpio gate_output; /**< & */ + GPIO gate_output; /**< & */ private: diff --git a/src/daisy_patch_sm.cpp b/src/daisy_patch_sm.cpp index f56b8eca9..6ca02b711 100644 --- a/src/daisy_patch_sm.cpp +++ b/src/daisy_patch_sm.cpp @@ -330,23 +330,12 @@ namespace patch_sm } /** Fixed-function Digital I/O */ - user_led.mode = DSY_GPIO_MODE_OUTPUT_PP; - user_led.pull = DSY_GPIO_NOPULL; - user_led.pin = PIN_USER_LED; - dsy_gpio_init(&user_led); - //gate_in_1.Init((dsy_gpio_pin *)&DaisyPatchSM::B10); + user_led.Init(PIN_USER_LED, GPIO::Mode::OUTPUT); gate_in_1.Init(B10); gate_in_2.Init(B9); - gate_out_1.mode = DSY_GPIO_MODE_OUTPUT_PP; - gate_out_1.pull = DSY_GPIO_NOPULL; - gate_out_1.pin = B5; - dsy_gpio_init(&gate_out_1); - - gate_out_2.mode = DSY_GPIO_MODE_OUTPUT_PP; - gate_out_2.pull = DSY_GPIO_NOPULL; - gate_out_2.pin = B6; - dsy_gpio_init(&gate_out_2); + gate_out_1.Init(B5, GPIO::Mode::OUTPUT); + gate_out_2.Init(B6, GPIO::Mode::OUTPUT); /** DAC init */ pimpl_->InitDac(); @@ -453,7 +442,7 @@ namespace patch_sm float DaisyPatchSM::GetAdcValue(int idx) { return controls[idx].Value(); } - dsy_gpio_pin DaisyPatchSM::GetPin(const PinBank bank, const int idx) + Pin DaisyPatchSM::GetPin(const PinBank bank, const int idx) { if(idx <= 0 || idx > 10) return DUMMYPIN; @@ -473,7 +462,7 @@ namespace patch_sm pimpl_->WriteCvOut(channel, voltage); } - void DaisyPatchSM::SetLed(bool state) { dsy_gpio_write(&user_led, state); } + void DaisyPatchSM::SetLed(bool state) { user_led.Write(state); } bool DaisyPatchSM::ValidateSDRAM() { diff --git a/src/daisy_patch_sm.h b/src/daisy_patch_sm.h index 54e18df01..e9a5548a2 100644 --- a/src/daisy_patch_sm.h +++ b/src/daisy_patch_sm.h @@ -143,7 +143,7 @@ namespace patch_sm * \param idx pin number between 1 and 10 for each of the pins on each header. * \deprecated please use the Pin definitions in daisy::patch_sm instead */ - dsy_gpio_pin GetPin(const PinBank bank, const int idx); + Pin GetPin(const PinBank bank, const int idx); /** Starts the DAC for the CV Outputs * @@ -251,10 +251,10 @@ namespace patch_sm DacHandle dac; /** Dedicated Function Pins */ - dsy_gpio user_led; + GPIO user_led; AnalogControl controls[ADC_LAST]; GateIn gate_in_1, gate_in_2; - dsy_gpio gate_out_1, gate_out_2; + GPIO gate_out_1, gate_out_2; /** Pin Accessors for the DaisyPatchSM hardware diff --git a/src/daisy_seed.cpp b/src/daisy_seed.cpp index 69199db9f..88e74ce4e 100644 --- a/src/daisy_seed.cpp +++ b/src/daisy_seed.cpp @@ -1,68 +1,61 @@ #include "daisy_seed.h" -extern "C" -{ -#include "dev/codec_ak4556.h" -} - using namespace daisy; -#define SEED_LED_PORT DSY_GPIOC -#define SEED_LED_PIN 7 +constexpr GPIOPort SEED_LED_PORT = PORTC; +constexpr uint8_t SEED_LED_PIN = 7; -#define SEED_TEST_POINT_PORT DSY_GPIOG -#define SEED_TEST_POINT_PIN 14 +constexpr GPIOPort SEED_TEST_POINT_PORT = PORTG; +constexpr uint8_t SEED_TEST_POINT_PIN = 14; #ifndef SEED_REV2 -const dsy_gpio_pin seedgpio[33] = { +const Pin seedgpio[33] = { // GPIO 1-8 - //{DSY_GPIOA, 8}, // removed on Rev4 - {DSY_GPIOB, 12}, - {DSY_GPIOC, 11}, - {DSY_GPIOC, 10}, - {DSY_GPIOC, 9}, - {DSY_GPIOC, 8}, - {DSY_GPIOD, 2}, - {DSY_GPIOC, 12}, + //{PORTA, 8}, // removed on Rev4 + seed::D0, + seed::D1, + seed::D2, + seed::D3, + seed::D4, + seed::D5, + seed::D6, // GPIO 9-16 - {DSY_GPIOG, 10}, - {DSY_GPIOG, 11}, - {DSY_GPIOB, 4}, - {DSY_GPIOB, 5}, - {DSY_GPIOB, 8}, - {DSY_GPIOB, 9}, - {DSY_GPIOB, 6}, - {DSY_GPIOB, 7}, + seed::D7, + seed::D8, + seed::D9, + seed::D10, + seed::D11, + seed::D12, + seed::D13, + seed::D14, // GPIO 17-24 - {DSY_GPIOC, 0}, - {DSY_GPIOA, 3}, - {DSY_GPIOB, 1}, - {DSY_GPIOA, 7}, - {DSY_GPIOA, 6}, - {DSY_GPIOC, 1}, - {DSY_GPIOC, 4}, - {DSY_GPIOA, 5}, + seed::D15, + seed::D16, + seed::D17, + seed::D18, + seed::D19, + seed::D20, + seed::D21, + seed::D22, // GPIO 25-31 - {DSY_GPIOA, 4}, - {DSY_GPIOA, 1}, - {DSY_GPIOA, 0}, - {DSY_GPIOD, 11}, - {DSY_GPIOG, 9}, - {DSY_GPIOA, 2}, - {DSY_GPIOB, 14}, - {DSY_GPIOB, 15}, + seed::D23, + seed::D24, + seed::D25, + seed::D26, + seed::D27, + seed::D28, + seed::D29, + seed::D30, // Seed2DFM exclusive pins - {DSY_GPIOC, 2}, - {DSY_GPIOC, 3}, + seed::D31, + seed::D32, }; #else -const dsy_gpio_port seed_ports[32] = { - DSY_GPIOA, DSY_GPIOB, DSY_GPIOC, DSY_GPIOC, DSY_GPIOC, DSY_GPIOC, DSY_GPIOD, - DSY_GPIOC, DSY_GPIOG, DSY_GPIOG, DSY_GPIOB, DSY_GPIOB, DSY_GPIOB, DSY_GPIOB, - DSY_GPIOB, DSY_GPIOB, DSY_GPIOC, DSY_GPIOA, DSY_GPIOA, DSY_GPIOB, DSY_GPIOA, - DSY_GPIOA, DSY_GPIOC, DSY_GPIOC, DSY_GPIOA, DSY_GPIOA, DSY_GPIOA, DSY_GPIOD, - DSY_GPIOG, DSY_GPIOA, DSY_GPIOB, DSY_GPIOB, +const GPIOPort seed_ports[32] = { + PORTA, PORTB, PORTC, PORTC, PORTC, PORTC, PORTD, PORTC, PORTG, PORTG, PORTB, + PORTB, PORTB, PORTB, PORTB, PORTB, PORTC, PORTA, PORTA, PORTB, PORTA, PORTA, + PORTC, PORTC, PORTA, PORTA, PORTA, PORTD, PORTG, PORTA, PORTB, PORTB, }; const uint8_t seed_pins[32] = { @@ -70,23 +63,23 @@ const uint8_t seed_pins[32] = { 0, 1, 3, 1, 7, 6, 1, 5, 5, 4, 0, 11, 9, 2, 14, 15, }; -const dsy_gpio_pin seedgpio[32] = { - {seed_ports[0], seed_pins[0]}, {seed_ports[1], seed_pins[1]}, - {seed_ports[2], seed_pins[2]}, {seed_ports[3], seed_pins[3]}, - {seed_ports[4], seed_pins[4]}, {seed_ports[5], seed_pins[5]}, - {seed_ports[6], seed_pins[6]}, {seed_ports[7], seed_pins[7]}, - {seed_ports[8], seed_pins[8]}, {seed_ports[9], seed_pins[9]}, - {seed_ports[10], seed_pins[10]}, {seed_ports[11], seed_pins[11]}, - {seed_ports[12], seed_pins[12]}, {seed_ports[13], seed_pins[13]}, - {seed_ports[14], seed_pins[14]}, {seed_ports[15], seed_pins[15]}, - {seed_ports[16], seed_pins[16]}, {seed_ports[17], seed_pins[17]}, - {seed_ports[18], seed_pins[18]}, {seed_ports[19], seed_pins[19]}, - {seed_ports[20], seed_pins[20]}, {seed_ports[21], seed_pins[21]}, - {seed_ports[22], seed_pins[22]}, {seed_ports[23], seed_pins[23]}, - {seed_ports[24], seed_pins[24]}, {seed_ports[25], seed_pins[25]}, - {seed_ports[26], seed_pins[26]}, {seed_ports[27], seed_pins[27]}, - {seed_ports[28], seed_pins[28]}, {seed_ports[29], seed_pins[29]}, - {seed_ports[30], seed_pins[30]}, {seed_ports[31], seed_pins[31]}, +const Pin seedgpio[32] = { + Pin(seed_ports[0], seed_pins[0]), Pin(seed_ports[1], seed_pins[1]), + Pin(seed_ports[2], seed_pins[2]), Pin(seed_ports[3], seed_pins[3]), + Pin(seed_ports[4], seed_pins[4]), Pin(seed_ports[5], seed_pins[5]), + Pin(seed_ports[6], seed_pins[6]), Pin(seed_ports[7], seed_pins[7]), + Pin(seed_ports[8], seed_pins[8]), Pin(seed_ports[9], seed_pins[9]), + Pin(seed_ports[10], seed_pins[10]), Pin(seed_ports[11], seed_pins[11]), + Pin(seed_ports[12], seed_pins[12]), Pin(seed_ports[13], seed_pins[13]), + Pin(seed_ports[14], seed_pins[14]), Pin(seed_ports[15], seed_pins[15]), + Pin(seed_ports[16], seed_pins[16]), Pin(seed_ports[17], seed_pins[17]), + Pin(seed_ports[18], seed_pins[18]), Pin(seed_ports[19], seed_pins[19]), + Pin(seed_ports[20], seed_pins[20]), Pin(seed_ports[21], seed_pins[21]), + Pin(seed_ports[22], seed_pins[22]), Pin(seed_ports[23], seed_pins[23]), + Pin(seed_ports[24], seed_pins[24]), Pin(seed_ports[25], seed_pins[25]), + Pin(seed_ports[26], seed_pins[26]), Pin(seed_ports[27], seed_pins[27]), + Pin(seed_ports[28], seed_pins[28]), Pin(seed_ports[29], seed_pins[29]), + Pin(seed_ports[30], seed_pins[30]), Pin(seed_ports[31], seed_pins[31]), }; #endif @@ -105,13 +98,14 @@ void DaisySeed::Init(bool boost) ConfigureQspi(); // Configure the built-in GPIOs. - led.pin.port = SEED_LED_PORT; - led.pin.pin = SEED_LED_PIN; - led.mode = DSY_GPIO_MODE_OUTPUT_PP; - testpoint.pin.port = SEED_TEST_POINT_PORT; - testpoint.pin.pin = SEED_TEST_POINT_PIN; - testpoint.mode = DSY_GPIO_MODE_OUTPUT_PP; + GPIO::Config &led_config = led.GetConfig(); + GPIO::Config &testpoint_config = testpoint.GetConfig(); + + led_config.pin = Pin(SEED_LED_PORT, SEED_LED_PIN); + led_config.mode = GPIO::Mode::OUTPUT; + testpoint_config.pin = Pin(SEED_TEST_POINT_PORT, SEED_TEST_POINT_PIN); + testpoint_config.mode = GPIO::Mode::OUTPUT; auto memory = System::GetProgramMemoryRegion(); auto boot_version = System::GetBootloaderVersion(); @@ -131,8 +125,8 @@ void DaisySeed::Init(bool boost) || (boot_version == System::BootInfo::Version::LT_v6_0 && memory == System::MemoryRegion::INTERNAL_FLASH)) { - dsy_gpio_init(&led); - dsy_gpio_init(&testpoint); + led.Init(led.GetConfig()); + testpoint.Init(testpoint.GetConfig()); sdram_handle.Init(); } @@ -151,30 +145,21 @@ void DaisySeed::DeInit() // This is intended to be used by the bootloader, but // we don't want to reinitialize pretty much anything in the // target application, so... - // qspi.DeInit(); - // sdram_handle.DeInit(); - // dsy_gpio_deinit(&led); - // dsy_gpio_deinit(&testpoint); - - // dsy_gpio_pin codec_reset_pin; - // codec_reset_pin = {DSY_GPIOB, 11}; - // // Perhaps a bit unnecessary, but maybe we'll make - // // this non-static at some point - // Ak4556::DeInit(codec_reset_pin); - // audio_handle.DeInit(); system.DeInit(); } -dsy_gpio_pin DaisySeed::GetPin(uint8_t pin_idx) +Pin DaisySeed::GetPin(uint8_t pin_idx) { - dsy_gpio_pin p; + Pin p; pin_idx = pin_idx < sizeof(seedgpio) / sizeof(seedgpio[0]) ? pin_idx : 0; + #ifndef SEED_REV2 p = seedgpio[pin_idx]; #else p = {seed_ports[pin_idx], seed_pins[pin_idx]}; #endif + return p; } @@ -237,15 +222,15 @@ float DaisySeed::AudioCallbackRate() const void DaisySeed::SetLed(bool state) { - dsy_gpio_write(&led, state); + led.Write(state); } void DaisySeed::SetTestPoint(bool state) { - dsy_gpio_write(&testpoint, state); + testpoint.Write(state); } -const SaiHandle& DaisySeed::AudioSaiHandle() const +const SaiHandle &DaisySeed::AudioSaiHandle() const { return sai_1_handle_; } @@ -257,12 +242,12 @@ void DaisySeed::ConfigureQspi() qspi_config.device = QSPIHandle::Config::Device::IS25LP064A; qspi_config.mode = QSPIHandle::Config::Mode::MEMORY_MAPPED; - qspi_config.pin_config.io0 = dsy_pin(DSY_GPIOF, 8); - qspi_config.pin_config.io1 = dsy_pin(DSY_GPIOF, 9); - qspi_config.pin_config.io2 = dsy_pin(DSY_GPIOF, 7); - qspi_config.pin_config.io3 = dsy_pin(DSY_GPIOF, 6); - qspi_config.pin_config.clk = dsy_pin(DSY_GPIOF, 10); - qspi_config.pin_config.ncs = dsy_pin(DSY_GPIOG, 6); + qspi_config.pin_config.io0 = Pin(PORTF, 8); + qspi_config.pin_config.io1 = Pin(PORTF, 9); + qspi_config.pin_config.io2 = Pin(PORTF, 7); + qspi_config.pin_config.io3 = Pin(PORTF, 6); + qspi_config.pin_config.clk = Pin(PORTF, 10); + qspi_config.pin_config.ncs = Pin(PORTG, 6); } void DaisySeed::ConfigureAudio() { @@ -274,9 +259,9 @@ void DaisySeed::ConfigureAudio() sai_config.bit_depth = SaiHandle::Config::BitDepth::SAI_24BIT; sai_config.a_sync = SaiHandle::Config::Sync::MASTER; sai_config.b_sync = SaiHandle::Config::Sync::SLAVE; - sai_config.pin_config.fs = {DSY_GPIOE, 4}; - sai_config.pin_config.mclk = {DSY_GPIOE, 2}; - sai_config.pin_config.sck = {DSY_GPIOE, 5}; + sai_config.pin_config.fs = Pin(PORTE, 4); + sai_config.pin_config.mclk = Pin(PORTE, 2); + sai_config.pin_config.sck = Pin(PORTE, 5); // Device-based Init switch(CheckBoardVersion()) @@ -285,15 +270,15 @@ void DaisySeed::ConfigureAudio() { // Data Line Directions sai_config.a_dir = SaiHandle::Config::Direction::RECEIVE; - sai_config.pin_config.sa = {DSY_GPIOE, 6}; + sai_config.pin_config.sa = Pin(PORTE, 6); sai_config.b_dir = SaiHandle::Config::Direction::TRANSMIT; - sai_config.pin_config.sb = {DSY_GPIOE, 3}; + sai_config.pin_config.sb = Pin(PORTE, 3); I2CHandle::Config i2c_config; i2c_config.mode = I2CHandle::Config::Mode::I2C_MASTER; i2c_config.periph = I2CHandle::Config::Peripheral::I2C_2; i2c_config.speed = I2CHandle::Config::Speed::I2C_400KHZ; - i2c_config.pin_config.scl = {DSY_GPIOH, 4}; - i2c_config.pin_config.sda = {DSY_GPIOB, 11}; + i2c_config.pin_config.scl = Pin(PORTH, 4); + i2c_config.pin_config.sda = Pin(PORTB, 11); I2CHandle i2c_handle; i2c_handle.Init(i2c_config); Wm8731::Config codec_cfg; @@ -306,9 +291,9 @@ void DaisySeed::ConfigureAudio() { // Data Line Directions sai_config.a_dir = SaiHandle::Config::Direction::TRANSMIT; - sai_config.pin_config.sa = {DSY_GPIOE, 6}; + sai_config.pin_config.sa = Pin(PORTE, 6); sai_config.b_dir = SaiHandle::Config::Direction::RECEIVE; - sai_config.pin_config.sb = {DSY_GPIOE, 3}; + sai_config.pin_config.sb = Pin(PORTE, 3); /** PCM3060 disable deemphasis pin */ GPIO deemp; deemp.Init(Pin(PORTB, 11), GPIO::Mode::OUTPUT); @@ -320,12 +305,12 @@ void DaisySeed::ConfigureAudio() { // Data Line Directions sai_config.a_dir = SaiHandle::Config::Direction::TRANSMIT; - sai_config.pin_config.sa = {DSY_GPIOE, 6}; + sai_config.pin_config.sa = Pin(PORTE, 6); sai_config.b_dir = SaiHandle::Config::Direction::RECEIVE; - sai_config.pin_config.sb = {DSY_GPIOE, 3}; - dsy_gpio_pin codec_reset_pin; - codec_reset_pin = {DSY_GPIOB, 11}; - Ak4556::Init(codec_reset_pin); + sai_config.pin_config.sb = Pin(PORTE, 3); + + constexpr Pin codec_reset_pin = Pin(PORTB, 11); + codec.Init(codec_reset_pin); } break; } diff --git a/src/daisy_seed.h b/src/daisy_seed.h index cc15472bd..af449ce26 100644 --- a/src/daisy_seed.h +++ b/src/daisy_seed.h @@ -52,7 +52,7 @@ class DaisySeed Returns the gpio_pin corresponding to the index 0-31. For the given GPIO on the Daisy Seed (labeled 1-32 in docs). */ - static dsy_gpio_pin GetPin(uint8_t pin_idx); + static Pin GetPin(uint8_t pin_idx); /** Begins the audio for the seeds builtin audio. the specified callback will get called whenever @@ -145,8 +145,9 @@ class DaisySeed AdcHandle adc; /**< & */ DacHandle dac; UsbHandle usb_handle; /**< & */ - dsy_gpio led, testpoint; + GPIO led, testpoint; System system; + Ak4556 codec; /** Internal indices for DaisySeed-equivalent devices * This shouldn't have any effect on user-facing code, diff --git a/src/dev/codec_ak4556.cpp b/src/dev/codec_ak4556.cpp index 7df1af035..8327af366 100644 --- a/src/dev/codec_ak4556.cpp +++ b/src/dev/codec_ak4556.cpp @@ -4,25 +4,19 @@ namespace daisy { -void Ak4556::Init(dsy_gpio_pin reset_pin) +void Ak4556::Init(Pin reset_pin) { - dsy_gpio reset; - reset.pin = reset_pin; - reset.mode = DSY_GPIO_MODE_OUTPUT_PP; - reset.pull = DSY_GPIO_NOPULL; - dsy_gpio_init(&reset); - dsy_gpio_write(&reset, 1); + reset_.Init(reset_pin, GPIO::Mode::OUTPUT); + reset_.Write(1); System::Delay(1); - dsy_gpio_write(&reset, 0); + reset_.Write(0); System::Delay(1); - dsy_gpio_write(&reset, 1); + reset_.Write(1); } -void Ak4556::DeInit(dsy_gpio_pin reset_pin) +void Ak4556::DeInit() { - dsy_gpio reset; - reset.pin = reset_pin; - dsy_gpio_deinit(&reset); + reset_.DeInit(); } diff --git a/src/dev/codec_ak4556.h b/src/dev/codec_ak4556.h index 612391b5e..ee8b11f29 100644 --- a/src/dev/codec_ak4556.h +++ b/src/dev/codec_ak4556.h @@ -3,6 +3,7 @@ #define DSY_CODEC_AK4556_H #include "daisy_core.h" +#include "per/gpio.h" namespace daisy { @@ -20,16 +21,15 @@ class Ak4556 ~Ak4556() {} /** Initialization function for Ak4556 - ** Can be called statically: - ** Ak4556::Init(pin); ** */ - static void Init(dsy_gpio_pin reset_pin); + void Init(Pin reset_pin); /** Deinitialization function for Ak4556 - ** Can be called statically: - ** Ak4556::DeInit(pin); ** */ - static void DeInit(dsy_gpio_pin reset_pin); + void DeInit(); + + private: + GPIO reset_; }; } // namespace daisy diff --git a/src/dev/lcd_hd44780.cpp b/src/dev/lcd_hd44780.cpp index 5d09332d1..24491f41a 100644 --- a/src/dev/lcd_hd44780.cpp +++ b/src/dev/lcd_hd44780.cpp @@ -51,36 +51,13 @@ namespace daisy void LcdHD44780::Init(const Config& config) { // init pins + lcd_pin_rs.Init(config.rs, GPIO::Mode::OUTPUT); + lcd_pin_en.Init(config.en, GPIO::Mode::OUTPUT); - lcd_pin_rs.pin = config.rs; - lcd_pin_rs.mode = DSY_GPIO_MODE_OUTPUT_PP; - lcd_pin_rs.pull = DSY_GPIO_NOPULL; - dsy_gpio_init(&lcd_pin_rs); - - lcd_pin_en.pin = config.en; - lcd_pin_en.mode = DSY_GPIO_MODE_OUTPUT_PP; - lcd_pin_en.pull = DSY_GPIO_NOPULL; - dsy_gpio_init(&lcd_pin_en); - - lcd_data_pin[0].pin = config.d4; - lcd_data_pin[0].mode = DSY_GPIO_MODE_OUTPUT_PP; - lcd_data_pin[0].pull = DSY_GPIO_NOPULL; - dsy_gpio_init(&lcd_data_pin[0]); - - lcd_data_pin[1].pin = config.d5; - lcd_data_pin[1].mode = DSY_GPIO_MODE_OUTPUT_PP; - lcd_data_pin[1].pull = DSY_GPIO_NOPULL; - dsy_gpio_init(&lcd_data_pin[1]); - - lcd_data_pin[2].pin = config.d6; - lcd_data_pin[2].mode = DSY_GPIO_MODE_OUTPUT_PP; - lcd_data_pin[2].pull = DSY_GPIO_NOPULL; - dsy_gpio_init(&lcd_data_pin[2]); - - lcd_data_pin[3].pin = config.d7; - lcd_data_pin[3].mode = DSY_GPIO_MODE_OUTPUT_PP; - lcd_data_pin[3].pull = DSY_GPIO_NOPULL; - dsy_gpio_init(&lcd_data_pin[3]); + lcd_data_pin[0].Init(config.d4, GPIO::Mode::OUTPUT); + lcd_data_pin[1].Init(config.d5, GPIO::Mode::OUTPUT); + lcd_data_pin[2].Init(config.d6, GPIO::Mode::OUTPUT); + lcd_data_pin[3].Init(config.d7, GPIO::Mode::OUTPUT); cursor_on = config.cursor_on; cursor_blink = config.cursor_blink; @@ -150,7 +127,7 @@ void LcdHD44780::Clear() void LcdHD44780::WriteCommand(uint8_t command) { - dsy_gpio_write(&lcd_pin_rs, LCD_COMMAND_REG); + lcd_pin_rs.Write(LCD_COMMAND_REG); Write((command >> 4), LCD_NIB); Write(command & 0x0F, LCD_NIB); @@ -161,7 +138,7 @@ void LcdHD44780::WriteCommand(uint8_t command) void LcdHD44780::WriteData(uint8_t data) { - dsy_gpio_write(&lcd_pin_rs, LCD_DATA_REG); + lcd_pin_rs.Write(LCD_DATA_REG); Write(data >> 4, LCD_NIB); Write(data & 0x0F, LCD_NIB); @@ -174,14 +151,14 @@ void LcdHD44780::Write(uint8_t data, uint8_t len) { for(uint8_t i = 0; i < len; i++) { - dsy_gpio_write(&lcd_data_pin[i], (data >> i) & 0x01); + lcd_data_pin[i].Write((data >> i) & 0x01); } // toggle - dsy_gpio_write(&lcd_pin_en, 1); + lcd_pin_en.Write(1); System::Delay(1); - dsy_gpio_write(&lcd_pin_en, 0); + lcd_pin_en.Write(0); } } // namespace daisy diff --git a/src/dev/lcd_hd44780.h b/src/dev/lcd_hd44780.h index 8261bd6b7..85a4a4d81 100644 --- a/src/dev/lcd_hd44780.h +++ b/src/dev/lcd_hd44780.h @@ -24,14 +24,14 @@ class LcdHD44780 struct Config { - bool cursor_on; - bool cursor_blink; - dsy_gpio_pin rs, en, d4, d5, d6, d7; + bool cursor_on; + bool cursor_blink; + Pin rs, en, d4, d5, d6, d7; }; /** Initializes the LCD. - * \param config is a struct that sets cursor on/off, cursor blink on/off and the dsy_gpio_pin's that connects to the LCD. + * \param config is a struct that sets cursor on/off, cursor blink on/off and the Pin's that connects to the LCD. */ void Init(const Config &config); @@ -60,11 +60,11 @@ class LcdHD44780 void Clear(); private: - bool cursor_on; - bool cursor_blink; - dsy_gpio lcd_pin_rs; - dsy_gpio lcd_pin_en; - dsy_gpio lcd_data_pin[4]; // D4-D7 + bool cursor_on; + bool cursor_blink; + GPIO lcd_pin_rs; + GPIO lcd_pin_en; + GPIO lcd_data_pin[4]; // D4-D7 void WriteData(uint8_t); void WriteCommand(uint8_t); diff --git a/src/dev/leddriver.h b/src/dev/leddriver.h index 6c2b99ef0..9c70caeb9 100644 --- a/src/dev/leddriver.h +++ b/src/dev/leddriver.h @@ -64,9 +64,9 @@ class LedDriverPca9685 */ void Init(I2CHandle i2c, const uint8_t (&addresses)[numDrivers], - DmaBuffer dma_buffer_a, - DmaBuffer dma_buffer_b, - dsy_gpio_pin oe_pin = {DSY_GPIOX, 0}) + DmaBuffer dma_buffer_a, + DmaBuffer dma_buffer_b, + Pin oe_pin = Pin(PORTX, 0)) { i2c_ = i2c; draw_buffer_ = dma_buffer_a; @@ -218,13 +218,10 @@ class LedDriverPca9685 void InitializeDrivers() { // init OE pin and pull low to enable outputs - if(oe_pin_.port != DSY_GPIOX) + if(oe_pin_.port != PORTX) { - oe_pin_gpio_.pin = oe_pin_; - oe_pin_gpio_.mode = DSY_GPIO_MODE_OUTPUT_PP; - oe_pin_gpio_.pull = DSY_GPIO_NOPULL; - dsy_gpio_init(&oe_pin_gpio_); - dsy_gpio_write(&oe_pin_gpio_, 0); + oe_pin_gpio_.Init(oe_pin_, GPIO::Mode::OUTPUT); + oe_pin_gpio_.Write(0); } // init the individual drivers @@ -275,8 +272,8 @@ class LedDriverPca9685 PCA9685TransmitBuffer* draw_buffer_; PCA9685TransmitBuffer* transmit_buffer_; uint8_t addresses_[numDrivers]; - dsy_gpio_pin oe_pin_; - dsy_gpio oe_pin_gpio_; + Pin oe_pin_; + GPIO oe_pin_gpio_; // index of the dirver that is currently updated. volatile int8_t current_driver_idx_; const uint16_t gamma_table_[256] = { diff --git a/src/dev/max11300.h b/src/dev/max11300.h index 281104582..fbac62226 100644 --- a/src/dev/max11300.h +++ b/src/dev/max11300.h @@ -128,10 +128,10 @@ class MAX11300MultiSlaveSpiTransport { struct PinConfig { - dsy_gpio_pin nss[numDevices] = {{DSY_GPIOG, 10}}; // Pin 7 - dsy_gpio_pin mosi = {DSY_GPIOB, 5}; // Pin 10 - dsy_gpio_pin miso = {DSY_GPIOB, 4}; // Pin 9 - dsy_gpio_pin sclk = {DSY_GPIOG, 11}; // Pin 8 + Pin nss[numDevices] = {Pin(PORTG, 10)}; // Pin 7 + Pin mosi = Pin(PORTB, 5); // Pin 10 + Pin miso = Pin(PORTB, 4); // Pin 9 + Pin sclk = Pin(PORTG, 11); // Pin 8 } pin_config; SpiHandle::Config::Peripheral periph diff --git a/src/dev/mcp23x17.h b/src/dev/mcp23x17.h index f4229fb66..9b026bd17 100644 --- a/src/dev/mcp23x17.h +++ b/src/dev/mcp23x17.h @@ -93,8 +93,8 @@ class Mcp23017Transport i2c_config.periph = I2CHandle::Config::Peripheral::I2C_1; i2c_config.speed = I2CHandle::Config::Speed::I2C_1MHZ; i2c_config.mode = I2CHandle::Config::Mode::I2C_MASTER; - i2c_config.pin_config.scl = {DSY_GPIOB, 8}; - i2c_config.pin_config.sda = {DSY_GPIOB, 9}; + i2c_config.pin_config.scl = Pin(PORTB, 8); + i2c_config.pin_config.sda = Pin(PORTB, 9); i2c_address = 0x27; } }; diff --git a/src/dev/oled_ssd130x.h b/src/dev/oled_ssd130x.h index ee2b354f8..46d4770f0 100644 --- a/src/dev/oled_ssd130x.h +++ b/src/dev/oled_ssd130x.h @@ -29,8 +29,8 @@ class SSD130xI2CTransport i2c_config.periph = I2CHandle::Config::Peripheral::I2C_1; i2c_config.speed = I2CHandle::Config::Speed::I2C_1MHZ; i2c_config.mode = I2CHandle::Config::Mode::I2C_MASTER; - i2c_config.pin_config.scl = {DSY_GPIOB, 8}; - i2c_config.pin_config.sda = {DSY_GPIOB, 9}; + i2c_config.pin_config.scl = Pin(PORTB, 8); + i2c_config.pin_config.sda = Pin(PORTB, 9); i2c_address = 0x3C; } }; @@ -75,8 +75,8 @@ class SSD130x4WireSpiTransport SpiHandle::Config spi_config; struct { - dsy_gpio_pin dc; /**< & */ - dsy_gpio_pin reset; /**< & */ + Pin dc; /**< & */ + Pin reset; /**< & */ } pin_config; void Defaults() { @@ -91,50 +91,46 @@ class SSD130x4WireSpiTransport spi_config.nss = SpiHandle::Config::NSS::HARD_OUTPUT; spi_config.baud_prescaler = SpiHandle::Config::BaudPrescaler::PS_8; // SPI pin config - spi_config.pin_config.sclk = {DSY_GPIOG, 11}; - spi_config.pin_config.miso = {DSY_GPIOX, 0}; - spi_config.pin_config.mosi = {DSY_GPIOB, 5}; - spi_config.pin_config.nss = {DSY_GPIOG, 10}; + spi_config.pin_config.sclk = Pin(PORTG, 11); + spi_config.pin_config.miso = Pin(PORTX, 0); + spi_config.pin_config.mosi = Pin(PORTB, 5); + spi_config.pin_config.nss = Pin(PORTG, 10); // SSD130x control pin config - pin_config.dc = {DSY_GPIOB, 4}; - pin_config.reset = {DSY_GPIOB, 15}; + pin_config.dc = Pin(PORTB, 4); + pin_config.reset = Pin(PORTB, 15); } }; void Init(const Config& config) { // Initialize both GPIO - pin_dc_.mode = DSY_GPIO_MODE_OUTPUT_PP; - pin_dc_.pin = config.pin_config.dc; - dsy_gpio_init(&pin_dc_); - pin_reset_.mode = DSY_GPIO_MODE_OUTPUT_PP; - pin_reset_.pin = config.pin_config.reset; - dsy_gpio_init(&pin_reset_); + pin_dc_.Init(config.pin_config.dc, GPIO::Mode::OUTPUT); + pin_reset_.Init(config.pin_config.reset, GPIO::Mode::OUTPUT); // Initialize SPI spi_.Init(config.spi_config); // Reset and Configure OLED. - dsy_gpio_write(&pin_reset_, 0); + pin_reset_.Write(0); System::Delay(10); - dsy_gpio_write(&pin_reset_, 1); + pin_reset_.Write(1); System::Delay(10); }; void SendCommand(uint8_t cmd) { - dsy_gpio_write(&pin_dc_, 0); + pin_dc_.Write(0); spi_.BlockingTransmit(&cmd, 1); }; void SendData(uint8_t* buff, size_t size) { - dsy_gpio_write(&pin_dc_, 1); + pin_dc_.Write(1); spi_.BlockingTransmit(buff, size); }; private: SpiHandle spi_; - dsy_gpio pin_reset_; - dsy_gpio pin_dc_; + GPIO pin_reset_; + GPIO pin_dc_; }; /** @@ -152,58 +148,50 @@ class SSD130x4WireSoftSpiTransport } struct { - uint32_t sclk_delay; - dsy_gpio_pin sclk; - dsy_gpio_pin mosi; - dsy_gpio_pin dc; - dsy_gpio_pin reset; + uint32_t sclk_delay; + Pin sclk; + Pin mosi; + Pin dc; + Pin reset; } pin_config; void Defaults() { pin_config.sclk_delay = 0; // fast as possible?! // SPI peripheral config - pin_config.sclk = {DSY_GPIOD, 3}; /**< D10 - SPI2 SCK */ - pin_config.mosi = {DSY_GPIOC, 3}; /**< D9 - SPI2 MOSI */ + pin_config.sclk = Pin(PORTD, 3); /**< D10 - SPI2 SCK */ + pin_config.mosi = Pin(PORTC, 3); /**< D9 - SPI2 MOSI */ // SSD130x control pin config - pin_config.dc = {DSY_GPIOC, 11}; //D2 - pin_config.reset = {DSY_GPIOC, 10}; //D3 + pin_config.dc = Pin(PORTC, 11); //D2 + pin_config.reset = Pin(PORTC, 10); //D3 } }; void Init(const Config& config) { // Initialize both GPIO - pin_sclk_.mode = DSY_GPIO_MODE_OUTPUT_PP; - pin_sclk_.pin = config.pin_config.sclk; - dsy_gpio_init(&pin_sclk_); - dsy_gpio_write(&pin_sclk_, 1); //ClockPolarity::LOW - clk_delay = config.pin_config.sclk_delay; - pin_mosi_.mode = DSY_GPIO_MODE_OUTPUT_PP; - pin_mosi_.pin = config.pin_config.mosi; - dsy_gpio_init(&pin_mosi_); - dsy_gpio_write(&pin_mosi_, 0); - - pin_dc_.mode = DSY_GPIO_MODE_OUTPUT_PP; - pin_dc_.pin = config.pin_config.dc; - dsy_gpio_init(&pin_dc_); - pin_reset_.mode = DSY_GPIO_MODE_OUTPUT_PP; - pin_reset_.pin = config.pin_config.reset; - dsy_gpio_init(&pin_reset_); + pin_sclk_.Init(config.pin_config.sclk, GPIO::Mode::OUTPUT); + pin_sclk_.Write(1); //ClockPolarity::LOW + clk_delay = config.pin_config.sclk_delay; + pin_mosi_.Init(config.pin_config.mosi, GPIO::Mode::OUTPUT); + pin_mosi_.Write(0); + + pin_dc_.Init(config.pin_config.dc, GPIO::Mode::OUTPUT); + pin_reset_.Init(config.pin_config.reset, GPIO::Mode::OUTPUT); // Reset and Configure OLED. - dsy_gpio_write(&pin_reset_, 0); + pin_reset_.Write(0); System::Delay(10); - dsy_gpio_write(&pin_reset_, 1); + pin_reset_.Write(1); System::Delay(10); }; void SendCommand(uint8_t cmd) { - dsy_gpio_write(&pin_dc_, 0); + pin_dc_.Write(0); SoftSpiTransmit(cmd); }; void SendData(uint8_t* buff, size_t size) { - dsy_gpio_write(&pin_dc_, 1); + pin_dc_.Write(1); for(size_t i = 0; i < size; i++) SoftSpiTransmit(buff[i]); }; @@ -218,23 +206,23 @@ class SSD130x4WireSoftSpiTransport for(uint8_t bit = 0u; bit < 8u; bit++) { - dsy_gpio_write(&pin_mosi_, ((val & (1 << bit)) ? 1 : 0)); + pin_mosi_.Write((val & (1 << bit)) ? 1 : 0); System::DelayTicks(clk_delay); - dsy_gpio_toggle(&pin_sclk_); + pin_sclk_.Toggle(); System::DelayTicks(clk_delay); - dsy_gpio_toggle(&pin_sclk_); + pin_sclk_.Toggle(); } } uint32_t clk_delay; - dsy_gpio pin_sclk_; - dsy_gpio pin_mosi_; - dsy_gpio pin_reset_; - dsy_gpio pin_dc_; + GPIO pin_sclk_; + GPIO pin_mosi_; + GPIO pin_reset_; + GPIO pin_dc_; }; diff --git a/src/dev/sdram.cpp b/src/dev/sdram.cpp index 366aab3df..47ca64f63 100644 --- a/src/dev/sdram.cpp +++ b/src/dev/sdram.cpp @@ -1,9 +1,5 @@ #include #include "dev/sdram.h" -extern "C" -{ -#include "util/hal_map.h" -} // TODO: // - Consider alternative to libdaisy.h inclusion for board specific details. diff --git a/src/dev/sr_4021.h b/src/dev/sr_4021.h index 9ccbca027..881cb8a95 100644 --- a/src/dev/sr_4021.h +++ b/src/dev/sr_4021.h @@ -40,9 +40,9 @@ class ShiftRegister4021 /** Configuration Structure for handling the pin setting of the device */ struct Config { - dsy_gpio_pin clk; /**< Clock pin to attach to pin 10 of device(s) */ - dsy_gpio_pin latch; /**< Latch pin to attach to pin 9 of device(s) */ - dsy_gpio_pin data[num_parallel]; /**< Data Pin(s) */ + Pin clk; /**< Clock pin to attach to pin 10 of device(s) */ + Pin latch; /**< Latch pin to attach to pin 9 of device(s) */ + Pin data[num_parallel]; /**< Data Pin(s) */ }; ShiftRegister4021() {} @@ -53,20 +53,11 @@ class ShiftRegister4021 { config_ = cfg; // Init GPIO - clk_.mode = DSY_GPIO_MODE_OUTPUT_PP; - clk_.pull = DSY_GPIO_NOPULL; - clk_.pin = cfg.clk; - dsy_gpio_init(&clk_); - latch_.mode = DSY_GPIO_MODE_OUTPUT_PP; - latch_.pull = DSY_GPIO_NOPULL; - latch_.pin = cfg.latch; - dsy_gpio_init(&latch_); + clk_.Init(cfg.clk, GPIO::Mode::OUTPUT); + latch_.Init(cfg.latch, GPIO::Mode::OUTPUT); for(size_t i = 0; i < num_parallel; i++) { - data_[i].mode = DSY_GPIO_MODE_INPUT; - data_[i].pull = DSY_GPIO_NOPULL; - data_[i].pin = cfg.data[i]; - dsy_gpio_init(&data_[i]); + data_[i].Init(cfg.data[i], GPIO::Mode::INPUT); } // Init States for(size_t i = 0; i < kTotalStates; i++) @@ -78,22 +69,22 @@ class ShiftRegister4021 /** Reads the states of all pins on the connected device(s) */ void Update() { - dsy_gpio_write(&clk_, 0); - dsy_gpio_write(&latch_, 1); + clk_.Write(0); + latch_.Write(1); System::DelayTicks(1); - dsy_gpio_write(&latch_, 0); + latch_.Write(0); uint32_t idx; for(size_t i = 0; i < 8 * num_daisychained; i++) { - dsy_gpio_write(&clk_, 0); + clk_.Write(0); System::DelayTicks(1); for(size_t j = 0; j < num_parallel; j++) { idx = (8 * num_daisychained - 1) - i; idx += (8 * num_daisychained * j); - states_[idx] = dsy_gpio_read(&data_[j]); + states_[idx] = data_[j].Read(); } - dsy_gpio_write(&clk_, 1); + clk_.Write(1); System::DelayTicks(1); } } @@ -112,9 +103,9 @@ class ShiftRegister4021 static constexpr int kTotalStates = 8 * num_daisychained * num_parallel; Config config_; bool states_[kTotalStates]; - dsy_gpio clk_; - dsy_gpio latch_; - dsy_gpio data_[num_parallel]; + GPIO clk_; + GPIO latch_; + GPIO data_[num_parallel]; }; } // namespace daisy diff --git a/src/dev/sr_595.cpp b/src/dev/sr_595.cpp index c507251f7..613290399 100644 --- a/src/dev/sr_595.cpp +++ b/src/dev/sr_595.cpp @@ -1,14 +1,13 @@ #include #include "dev/sr_595.h" -void ShiftRegister595::Init(dsy_gpio_pin *pin_cfg, size_t num_daisy_chained) +namespace daisy +{ +void ShiftRegister595::Init(Pin *pin_cfg, size_t num_daisy_chained) { // Initialize Pins as outputs for(size_t i = 0; i < NUM_PINS; i++) { - pin_[i].pin = pin_cfg[i]; - pin_[i].mode = DSY_GPIO_MODE_OUTPUT_PP; - pin_[i].pull = DSY_GPIO_NOPULL; - dsy_gpio_init(&pin_[i]); + pin_[i].Init(pin_cfg[i], GPIO::Mode::OUTPUT); } std::fill(state_, state_ + kMaxSr595DaisyChain, 0x00); num_devices_ = num_daisy_chained; @@ -30,14 +29,15 @@ void ShiftRegister595::Write() { // This is about 2MHz clock speeds without delays // Max Freq is 4-6 MHz at 2V, and 21-31MHz at 4V5. - dsy_gpio_write(&pin_[PIN_LATCH], 0); + pin_[PIN_LATCH].Write(0); for(size_t i = 0; i < num_devices_ * 8; i++) { - dsy_gpio_write(&pin_[PIN_CLK], 0); - dsy_gpio_write(&pin_[PIN_DATA], - state_[((num_devices_ - 1) - (i / 8))] - & (1 << (7 - (i % 8)))); - dsy_gpio_write(&pin_[PIN_CLK], 1); + pin_[PIN_CLK].Write(0); + pin_[PIN_DATA].Write(state_[((num_devices_ - 1) - (i / 8))] + & (1 << (7 - (i % 8)))); + pin_[PIN_CLK].Write(1); } - dsy_gpio_write(&pin_[PIN_LATCH], 1); + pin_[PIN_LATCH].Write(1); } + +} // namespace daisy \ No newline at end of file diff --git a/src/dev/sr_595.h b/src/dev/sr_595.h index 6c2f666c7..2a73d9b75 100644 --- a/src/dev/sr_595.h +++ b/src/dev/sr_595.h @@ -5,6 +5,8 @@ #include "daisy_core.h" #include "per/gpio.h" +namespace daisy +{ const size_t kMaxSr595DaisyChain = 16; /**< Maximum Number of chained devices Connect device's QH' pin to the next chips serial input*/ @@ -23,6 +25,7 @@ class ShiftRegister595 public: /** The following pins correspond to the hardware connections to the 595. + \note TODO: This should probably be switched to using a pin config structure similar to other drivers */ enum Pins { @@ -36,10 +39,10 @@ class ShiftRegister595 /** Initializes the GPIO, and data for the ShiftRegister - * \param pin_cfg is an array of dsy_gpio_pin corresponding the the Pins enum above. + * \param pin_cfg is an array of Pin corresponding the the Pins enum above. * \param num_daisy_chained (default = 1) is the number of 595 devices daisy chained together. */ - void Init(dsy_gpio_pin *pin_cfg, size_t num_daisy_chained = 1); + void Init(Pin *pin_cfg, size_t num_daisy_chained = 1); /** Sets the state of the specified output. \param idx The index starts with QA on the first device and ends with QH on the last device. @@ -52,10 +55,11 @@ class ShiftRegister595 void Write(); private: - dsy_gpio pin_[NUM_PINS]; - uint8_t state_[kMaxSr595DaisyChain]; - size_t num_devices_; + GPIO pin_[NUM_PINS]; + uint8_t state_[kMaxSr595DaisyChain]; + size_t num_devices_; }; +} // namespace daisy #endif /** @} */ diff --git a/src/dev/tlv493d.h b/src/dev/tlv493d.h index d64d8e540..b0b43e22e 100644 --- a/src/dev/tlv493d.h +++ b/src/dev/tlv493d.h @@ -36,8 +36,8 @@ class Tlv493dI2CTransport { I2CHandle::Config::Peripheral periph; I2CHandle::Config::Speed speed; - dsy_gpio_pin scl; - dsy_gpio_pin sda; + Pin scl; + Pin sda; uint8_t address; @@ -46,8 +46,8 @@ class Tlv493dI2CTransport periph = I2CHandle::Config::Peripheral::I2C_1; speed = I2CHandle::Config::Speed::I2C_400KHZ; - scl = {DSY_GPIOB, 8}; - sda = {DSY_GPIOB, 9}; + scl = Pin(PORTB, 8); + sda = Pin(PORTB, 9); address = TLV493D_ADDRESS1; } diff --git a/src/hid/encoder.cpp b/src/hid/encoder.cpp index ceec8bba0..a6064a6e3 100644 --- a/src/hid/encoder.cpp +++ b/src/hid/encoder.cpp @@ -2,23 +2,14 @@ using namespace daisy; -void Encoder::Init(dsy_gpio_pin a, - dsy_gpio_pin b, - dsy_gpio_pin click, - float update_rate) +void Encoder::Init(Pin a, Pin b, Pin click, float update_rate) { last_update_ = System::GetNow(); updated_ = false; // Init GPIO for A, and B - hw_a_.pin = a; - hw_a_.mode = DSY_GPIO_MODE_INPUT; - hw_a_.pull = DSY_GPIO_PULLUP; - hw_b_.pin = b; - hw_b_.mode = DSY_GPIO_MODE_INPUT; - hw_b_.pull = DSY_GPIO_PULLUP; - dsy_gpio_init(&hw_a_); - dsy_gpio_init(&hw_b_); + hw_a_.Init(a, GPIO::Mode::INPUT, GPIO::Pull::PULLUP); + hw_b_.Init(b, GPIO::Mode::INPUT, GPIO::Pull::PULLUP); // Default Initialization for Switch sw_.Init(click); // Set initial states, etc. @@ -38,8 +29,8 @@ void Encoder::Debounce() updated_ = true; // Shift Button states to debounce - a_ = (a_ << 1) | dsy_gpio_read(&hw_a_); - b_ = (b_ << 1) | dsy_gpio_read(&hw_b_); + a_ = (a_ << 1) | hw_a_.Read(); + b_ = (b_ << 1) | hw_b_.Read(); // infer increment direction inc_ = 0; // reset inc_ first diff --git a/src/hid/encoder.h b/src/hid/encoder.h index e2e7601b2..6d89a635c 100644 --- a/src/hid/encoder.h +++ b/src/hid/encoder.h @@ -23,10 +23,7 @@ class Encoder /** Initializes the encoder with the specified hardware pins. * Update rate is to be deprecated in a future release */ - void Init(dsy_gpio_pin a, - dsy_gpio_pin b, - dsy_gpio_pin click, - float update_rate = 0.f); + void Init(Pin a, Pin b, Pin click, float update_rate = 0.f); /** Called at update_rate to debounce and handle timing for the switch. * In order for events not to be missed, its important that the Edge/Pressed checks be made at the same rate as the debounce function is being called. */ @@ -56,7 +53,7 @@ class Encoder uint32_t last_update_; bool updated_; Switch sw_; - dsy_gpio hw_a_, hw_b_; + GPIO hw_a_, hw_b_; uint8_t a_, b_; int32_t inc_; }; diff --git a/src/hid/gatein.cpp b/src/hid/gatein.cpp index c405791a9..76a98dea9 100644 --- a/src/hid/gatein.cpp +++ b/src/hid/gatein.cpp @@ -2,17 +2,6 @@ using namespace daisy; -void GateIn::Init(dsy_gpio_pin *pin_cfg, bool invert) -{ - /** Converting old type to new type */ - Pin p(static_cast(pin_cfg->port), pin_cfg->pin); - pin_.Init(p, GPIO::Mode::INPUT); - prev_state_ = false; - state_ = false; - invert_ = invert; -} - - void GateIn::Init(Pin pin_cfg, bool invert) { pin_.Init(pin_cfg, GPIO::Mode::INPUT); diff --git a/src/hid/gatein.h b/src/hid/gatein.h index f390930f8..40608ebb1 100644 --- a/src/hid/gatein.h +++ b/src/hid/gatein.h @@ -31,21 +31,6 @@ class GateIn */ void Init(Pin pin, bool invert = true); - /** @brief Initializes the gate input with specified hardware pin - * - * @param pin_cfg pointer to pin to initialize - * @param invert True if the pin state is HIGH when 0V is present - * at the input. False if input signal matches the pin state. - * - * @note the default for invert is true because it is typical to use - * an inverting input circuit (e.g. a BJT circuit) for eurorack gate inputs. - * - * @note deprectated - this function still works, but will eventually be removed. - * It uses the old style dsy_gpio_pin in a way that it is not compatible with the - * the new Pin class. - */ - void Init(dsy_gpio_pin *pin_cfg, bool invert = true); - /** Checks current state of gate input. * @return True if the GPIO just transitioned. */ diff --git a/src/hid/led.cpp b/src/hid/led.cpp index 1733f7875..6a3b28e3a 100644 --- a/src/hid/led.cpp +++ b/src/hid/led.cpp @@ -5,13 +5,11 @@ using namespace daisy; #define RESOLUTION_MAX (65535) -void Led::Init(dsy_gpio_pin pin, bool invert, float samplerate) +void Led::Init(Pin pin, bool invert, float samplerate) { // Init hardware LED // Simple OUTPUT GPIO for now. - hw_pin_.pin = pin; - hw_pin_.mode = DSY_GPIO_MODE_OUTPUT_PP; - dsy_gpio_init(&hw_pin_); + hw_pin_.Init(pin, GPIO::Mode::OUTPUT); // Set internal stuff. bright_ = 0.0f; pwm_cnt_ = 0; @@ -41,12 +39,5 @@ void Led::Update() pwm_ += 120.f / samplerate_; if(pwm_ > 1.f) pwm_ -= 1.f; - dsy_gpio_write(&hw_pin_, bright_ > pwm_ ? on_ : off_); - - // Once we have a slower timer set up: - // Right now its too fast. - - // dsy_gpio_write(&hw_pin_, - // (dsy_tim_get_tick() & RESOLUTION_MAX) < pwm_thresh_ ? on_ - // : off_); + hw_pin_.Write(bright_ > pwm_ ? on_ : off_); } diff --git a/src/hid/led.h b/src/hid/led.h index e4f38aa66..543784dd0 100644 --- a/src/hid/led.h +++ b/src/hid/led.h @@ -30,7 +30,7 @@ class Led \param invert will set whether to internally invert the brightness due to hardware config. \param samplerate sets the rate at which 'Update()' will be called (used for software PWM) */ - void Init(dsy_gpio_pin pin, bool invert, float samplerate = 1000.0f); + void Init(Pin pin, bool invert, float samplerate = 1000.0f); /** Sets the brightness of the Led. @@ -51,12 +51,12 @@ class Led inline void SetSampleRate(float sample_rate) { samplerate_ = sample_rate; } private: - size_t pwm_cnt_, pwm_thresh_; - float bright_; - float pwm_; - float samplerate_; - bool invert_, on_, off_; - dsy_gpio hw_pin_; + size_t pwm_cnt_, pwm_thresh_; + float bright_; + float pwm_; + float samplerate_; + bool invert_, on_, off_; + GPIO hw_pin_; }; } // namespace daisy diff --git a/src/hid/midi.cpp b/src/hid/midi.cpp index 71f493d5e..360299586 100644 --- a/src/hid/midi.cpp +++ b/src/hid/midi.cpp @@ -10,8 +10,8 @@ static uint8_t DMA_BUFFER_MEM_SECTION MidiUartTransport::Config::Config() { periph = UartHandler::Config::Peripheral::USART_1; - rx = {DSY_GPIOB, 7}; - tx = {DSY_GPIOB, 6}; + rx = Pin(PORTB, 7); + tx = Pin(PORTB, 6); rx_buffer = default_midi_rx_buffer; rx_buffer_size = kDefaultMidiRxBufferSize; } diff --git a/src/hid/midi.h b/src/hid/midi.h index b212b3b3a..6bde211a3 100644 --- a/src/hid/midi.h +++ b/src/hid/midi.h @@ -35,8 +35,8 @@ class MidiUartTransport struct Config { UartHandler::Config::Peripheral periph; - dsy_gpio_pin rx; - dsy_gpio_pin tx; + Pin rx; + Pin tx; /** Pointer to buffer for DMA UART rx byte transfer in background. * diff --git a/src/hid/rgb_led.cpp b/src/hid/rgb_led.cpp index 3226268af..88959807e 100644 --- a/src/hid/rgb_led.cpp +++ b/src/hid/rgb_led.cpp @@ -1,9 +1,6 @@ #include "hid/rgb_led.h" using namespace daisy; -void RgbLed::Init(dsy_gpio_pin red, - dsy_gpio_pin green, - dsy_gpio_pin blue, - bool invert) +void RgbLed::Init(Pin red, Pin green, Pin blue, bool invert) { r_.Init(red, invert); g_.Init(green, invert); diff --git a/src/hid/rgb_led.h b/src/hid/rgb_led.h index 382df448f..a3e01974d 100644 --- a/src/hid/rgb_led.h +++ b/src/hid/rgb_led.h @@ -23,8 +23,7 @@ class RgbLed \param blue Blue element \param invert Flips led polarity */ - void - Init(dsy_gpio_pin red, dsy_gpio_pin green, dsy_gpio_pin blue, bool invert); + void Init(Pin red, Pin green, Pin blue, bool invert); /** Sets each element of the LED with a floating point number 0-1 \param r Red element diff --git a/src/hid/switch.cpp b/src/hid/switch.cpp index 56dbce7e5..989d9f5aa 100644 --- a/src/hid/switch.cpp +++ b/src/hid/switch.cpp @@ -1,11 +1,11 @@ #include "hid/switch.h" using namespace daisy; -void Switch::Init(dsy_gpio_pin pin, - float update_rate, - Type t, - Polarity pol, - Pull pu) +void Switch::Init(Pin pin, + float update_rate, + Type t, + Polarity pol, + GPIO::Pull pu) { last_update_ = System::GetNow(); updated_ = false; @@ -13,21 +13,16 @@ void Switch::Init(dsy_gpio_pin pin, t_ = t; // Flip may seem opposite to logical direction, // but here 1 is pressed, 0 is not. - flip_ = pol == POLARITY_INVERTED ? true : false; - hw_gpio_.pin = pin; - hw_gpio_.mode = DSY_GPIO_MODE_INPUT; - switch(pu) - { - case PULL_UP: hw_gpio_.pull = DSY_GPIO_PULLUP; break; - case PULL_DOWN: hw_gpio_.pull = DSY_GPIO_PULLDOWN; break; - case PULL_NONE: hw_gpio_.pull = DSY_GPIO_NOPULL; break; - default: hw_gpio_.pull = DSY_GPIO_PULLUP; break; - } - dsy_gpio_init(&hw_gpio_); + flip_ = pol == POLARITY_INVERTED ? true : false; + hw_gpio_.Init(pin, GPIO::Mode::INPUT, pu); } -void Switch::Init(dsy_gpio_pin pin, float update_rate) +void Switch::Init(Pin pin, float update_rate) { - Init(pin, update_rate, TYPE_MOMENTARY, POLARITY_INVERTED, PULL_UP); + Init(pin, + update_rate, + TYPE_MOMENTARY, + POLARITY_INVERTED, + GPIO::Pull::PULLUP); } void Switch::Debounce() @@ -42,9 +37,8 @@ void Switch::Debounce() updated_ = true; // shift over, and introduce new state. - state_ - = (state_ << 1) - | (flip_ ? !dsy_gpio_read(&hw_gpio_) : dsy_gpio_read(&hw_gpio_)); + const bool new_val = hw_gpio_.Read(); + state_ = (state_ << 1) | (flip_ ? !new_val : new_val); // Set time at which button was pressed if(state_ == 0x7f) rising_edge_time_ = System::GetNow(); diff --git a/src/hid/switch.h b/src/hid/switch.h index f6bc87509..d0c14d14a 100644 --- a/src/hid/switch.h +++ b/src/hid/switch.h @@ -30,14 +30,6 @@ class Switch POLARITY_INVERTED, /**< & */ }; - /** Specifies whether to use built-in Pull Up/Down resistors to hold button at a given state when not engaged. */ - enum Pull - { - PULL_UP, /**< & */ - PULL_DOWN, /**< & */ - PULL_NONE, /**< & */ - }; - Switch() {} ~Switch() {} @@ -49,15 +41,18 @@ class Switch \param pol switch polarity -- Default: POLARITY_INVERTED \param pu switch pull up/down -- Default: PULL_UP */ - void - Init(dsy_gpio_pin pin, float update_rate, Type t, Polarity pol, Pull pu); + void Init(Pin pin, + float update_rate, + Type t, + Polarity pol, + GPIO::Pull pu = GPIO::Pull::PULLUP); /** Simplified Init. \param pin port/pin object to tell the switch which hardware pin to use. \param update_rate Left for backwards compatibility until next breaking change. */ - void Init(dsy_gpio_pin pin, float update_rate = 0.f); + void Init(Pin pin, float update_rate = 0.f); /** Called at update_rate to debounce and handle timing for the switch. @@ -81,7 +76,8 @@ class Switch /** \return true if the button is held down, without debouncing */ inline bool RawState() { - return flip_ ? !dsy_gpio_read(&hw_gpio_) : dsy_gpio_read(&hw_gpio_); + const bool raw = hw_gpio_.Read(); + return flip_ ? !raw : raw; } /** \return the time in milliseconds that the button has been held (or toggle has been on) */ @@ -99,7 +95,7 @@ class Switch uint32_t last_update_; bool updated_; Type t_; - dsy_gpio hw_gpio_; + GPIO hw_gpio_; uint8_t state_; bool flip_; float rising_edge_time_; diff --git a/src/hid/switch3.h b/src/hid/switch3.h index 3fc2678f3..42bb83b68 100644 --- a/src/hid/switch3.h +++ b/src/hid/switch3.h @@ -20,31 +20,24 @@ class Switch3 Switch3() {} ~Switch3() {} - void Init(dsy_gpio_pin pina, dsy_gpio_pin pinb) + void Init(Pin pina, Pin pinb) { - pina_gpio_.pin = pina; - pina_gpio_.mode = DSY_GPIO_MODE_INPUT; - pina_gpio_.pull = DSY_GPIO_PULLUP; - dsy_gpio_init(&pina_gpio_); - - pinb_gpio_.pin = pinb; - pinb_gpio_.mode = DSY_GPIO_MODE_INPUT; - pinb_gpio_.pull = DSY_GPIO_PULLUP; - dsy_gpio_init(&pinb_gpio_); + pina_gpio_.Init(pina, GPIO::Mode::INPUT, GPIO::Pull::PULLUP); + pinb_gpio_.Init(pinb, GPIO::Mode::INPUT, GPIO::Pull::PULLUP); } int Read() { - if(!dsy_gpio_read(&pina_gpio_)) + if(!pina_gpio_.Read()) return POS_UP; - if(!dsy_gpio_read(&pinb_gpio_)) + if(!pinb_gpio_.Read()) return POS_DOWN; return POS_CENTER; } private: - dsy_gpio pina_gpio_; - dsy_gpio pinb_gpio_; + GPIO pina_gpio_; + GPIO pinb_gpio_; }; } // namespace daisy diff --git a/src/per/adc.cpp b/src/per/adc.cpp index 1ac21acf0..a1f6bdd98 100644 --- a/src/per/adc.cpp +++ b/src/per/adc.cpp @@ -1,6 +1,5 @@ #include #include "per/adc.h" -#include "util/hal_map.h" using namespace daisy; @@ -10,73 +9,23 @@ static void Error_Handler() while(1) {} } -// Pinout by channel as dsy_gpio_pin -// TODO Figure out how to get this formatting -// to not suck.. -#define PIN_CHN_3 \ - { \ - DSY_GPIOA, 6 \ - } -#define PIN_CHN_4 \ - { \ - DSY_GPIOC, 4 \ - } -#define PIN_CHN_5 \ - { \ - DSY_GPIOB, 1 \ - } -#define PIN_CHN_7 \ - { \ - DSY_GPIOA, 7 \ - } -#define PIN_CHN_8 \ - { \ - DSY_GPIOC, 5 \ - } -#define PIN_CHN_9 \ - { \ - DSY_GPIOB, 0 \ - } -#define PIN_CHN_10 \ - { \ - DSY_GPIOC, 0 \ - } -#define PIN_CHN_11 \ - { \ - DSY_GPIOC, 1 \ - } -#define PIN_CHN_12 \ - { \ - DSY_GPIOC, 2 \ - } -#define PIN_CHN_13 \ - { \ - DSY_GPIOC, 3 \ - } -#define PIN_CHN_14 \ - { \ - DSY_GPIOA, 2 \ - } -#define PIN_CHN_15 \ - { \ - DSY_GPIOA, 3 \ - } -#define PIN_CHN_16 \ - { \ - DSY_GPIOA, 0 \ - } -#define PIN_CHN_17 \ - { \ - DSY_GPIOA, 1 \ - } -#define PIN_CHN_18 \ - { \ - DSY_GPIOA, 4 \ - } -#define PIN_CHN_19 \ - { \ - DSY_GPIOA, 5 \ - } +// Pinout by channel +constexpr Pin PIN_CHN_3 = Pin(PORTA, 6); +constexpr Pin PIN_CHN_4 = Pin(PORTC, 4); +constexpr Pin PIN_CHN_5 = Pin(PORTB, 1); +constexpr Pin PIN_CHN_7 = Pin(PORTA, 7); +constexpr Pin PIN_CHN_8 = Pin(PORTC, 5); +constexpr Pin PIN_CHN_9 = Pin(PORTB, 0); +constexpr Pin PIN_CHN_10 = Pin(PORTC, 0); +constexpr Pin PIN_CHN_11 = Pin(PORTC, 1); +constexpr Pin PIN_CHN_12 = Pin(PORTC, 2); +constexpr Pin PIN_CHN_13 = Pin(PORTC, 3); +constexpr Pin PIN_CHN_14 = Pin(PORTA, 2); +constexpr Pin PIN_CHN_15 = Pin(PORTA, 3); +constexpr Pin PIN_CHN_16 = Pin(PORTA, 0); +constexpr Pin PIN_CHN_17 = Pin(PORTA, 1); +constexpr Pin PIN_CHN_18 = Pin(PORTA, 4); +constexpr Pin PIN_CHN_19 = Pin(PORTA, 5); #define DSY_ADC_MAX_MUX_CHANNELS 8 #define DSY_ADC_MAX_RESOLUTION 65536.0f @@ -163,12 +112,12 @@ static int get_num_mux_pins_required(int num_mux_ch) } static void write_mux_value(uint8_t chn, uint8_t idx, uint8_t num_mux_pins_to_write); -static const uint32_t adc_channel_from_pin(dsy_gpio_pin* pin); +static const uint32_t adc_channel_from_pin(Pin pin); -static const uint32_t adc_channel_from_pin(dsy_gpio_pin* pin) +static const uint32_t adc_channel_from_pin(Pin pin) { // For now just a rough switch case for all ADC_CHANNEL values - dsy_gpio_pin adcpins[DSY_ADC_MAX_CHANNELS] = { + Pin adcpins[DSY_ADC_MAX_CHANNELS] = { PIN_CHN_3, PIN_CHN_4, PIN_CHN_5, @@ -188,7 +137,7 @@ static const uint32_t adc_channel_from_pin(dsy_gpio_pin* pin) }; for(size_t i = 0; i < DSY_ADC_MAX_CHANNELS; i++) { - if(dsy_pin_cmp(&adcpins[i], pin)) + if(adcpins[i] == pin) return dsy_adc_channel_map[i]; } return 0; // we should check what zero actually means in this context. @@ -199,37 +148,46 @@ static dsy_adc adc; // Begin AdcChannelConfig Implementations -void AdcChannelConfig::InitSingle(dsy_gpio_pin pin, +void AdcChannelConfig::InitSingle(Pin pin, AdcChannelConfig::ConversionSpeed speed) { - pin_.pin = pin; - mux_channels_ = 0; - pin_.mode = DSY_GPIO_MODE_ANALOG; - pin_.pull = DSY_GPIO_NOPULL; - speed_ = speed; + GPIO::Config& pin_config = pin_.GetConfig(); + + pin_config.pin = pin; + mux_channels_ = 0; + pin_config.mode = GPIO::Mode::ANALOG; + pin_config.pull = GPIO::Pull::NOPULL; + speed_ = speed; } -void AdcChannelConfig::InitMux(dsy_gpio_pin adc_pin, +void AdcChannelConfig::InitMux(Pin adc_pin, size_t mux_channels, - dsy_gpio_pin mux_0, - dsy_gpio_pin mux_1, - dsy_gpio_pin mux_2, + Pin mux_0, + Pin mux_1, + Pin mux_2, AdcChannelConfig::ConversionSpeed speed) { size_t pins_to_init; + + GPIO::Config& pin_config = pin_.GetConfig(); + // Init ADC Pin - pin_.pin = adc_pin; - pin_.mode = DSY_GPIO_MODE_ANALOG; - pin_.pull = DSY_GPIO_NOPULL; + pin_config.pin = adc_pin; + pin_config.mode = GPIO::Mode::ANALOG; + pin_config.pull = GPIO::Pull::NOPULL; + // Init Muxes - mux_pin_[0].pin = mux_0; - mux_pin_[1].pin = mux_1; - mux_pin_[2].pin = mux_2; - mux_channels_ = mux_channels < 8 ? mux_channels : 8; - pins_to_init = get_num_mux_pins_required(mux_channels_); + mux_pin_[0].GetConfig().pin = mux_0; + mux_pin_[1].GetConfig().pin = mux_1; + mux_pin_[2].GetConfig().pin = mux_2; + + mux_channels_ = mux_channels < 8 ? mux_channels : 8; + pins_to_init = get_num_mux_pins_required(mux_channels_); for(size_t i = 0; i < pins_to_init; i++) { - mux_pin_[i].mode = DSY_GPIO_MODE_OUTPUT_PP; - mux_pin_[i].pull = DSY_GPIO_NOPULL; + GPIO::Config& mux_pin_config = mux_pin_[i].GetConfig(); + + mux_pin_config.mode = GPIO::Mode::OUTPUT; + mux_pin_config.pull = GPIO::Pull::NOPULL; } speed_ = speed; } @@ -368,7 +326,7 @@ void AdcHandle::Init(AdcChannelConfig* cfg, sConfig.Offset = 0; for(uint8_t i = 0; i < adc.channels; i++) { - const auto& cfg = adc.pin_cfg[i]; + AdcChannelConfig& cfg = adc.pin_cfg[i]; /** Handle per-channel conversions */ switch(cfg.speed_) @@ -400,7 +358,7 @@ void AdcHandle::Init(AdcChannelConfig* cfg, } // init ADC pin - dsy_gpio_init(&cfg.pin_); + cfg.pin_.Init(); // init mux pins (if any) adc.num_mux_pins_required[i] @@ -408,12 +366,13 @@ void AdcHandle::Init(AdcChannelConfig* cfg, if(cfg.mux_channels_ > 0) { for(int j = 0; j < adc.num_mux_pins_required[i]; j++) - dsy_gpio_init(&cfg.mux_pin_[j]); + cfg.mux_pin_[j].Init(); } // init adc channel sequence - sConfig.Channel = adc_channel_from_pin(&adc.pin_cfg[i].pin_.pin); - sConfig.Rank = dsy_adc_rank_map[i]; + sConfig.Channel + = adc_channel_from_pin(adc.pin_cfg[i].pin_.GetConfig().pin); + sConfig.Rank = dsy_adc_rank_map[i]; if(HAL_ADC_ConfigChannel(&adc.hadc1, &sConfig) != HAL_OK) { Error_Handler(); @@ -472,18 +431,17 @@ float AdcHandle::GetMuxFloat(uint8_t chn, uint8_t idx) const static void write_mux_value(uint8_t chn, uint8_t idx, uint8_t num_mux_pins_to_write) { - dsy_gpio *p0, *p1, *p2; - p0 = &adc.pin_cfg[chn].mux_pin_[0]; - dsy_gpio_write(p0, (idx & 0x01) > 0); + GPIO& p0 = adc.pin_cfg[chn].mux_pin_[0]; + p0.Write((idx & 0x01) > 0); if(num_mux_pins_to_write > 1) { - p1 = &adc.pin_cfg[chn].mux_pin_[1]; - dsy_gpio_write(p1, (idx & 0x02) > 0); + GPIO& p1 = adc.pin_cfg[chn].mux_pin_[1]; + p1.Write((idx & 0x02) > 0); } if(num_mux_pins_to_write > 2) { - p2 = &adc.pin_cfg[chn].mux_pin_[2]; - dsy_gpio_write(p2, (idx & 0x04) > 0); + GPIO& p2 = adc.pin_cfg[chn].mux_pin_[2]; + p2.Write((idx & 0x04) > 0); } } @@ -566,9 +524,9 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle) // deinit pins for(size_t i = 0; i < adc.channels; i++) { - dsy_gpio_deinit(&adc.pin_cfg[i].pin_); + adc.pin_cfg[i].pin_.DeInit(); for(size_t muxCh = 0; muxCh < adc.num_mux_pins_required[i]; muxCh++) - dsy_gpio_deinit(&adc.pin_cfg[i].mux_pin_[muxCh]); + adc.pin_cfg[i].mux_pin_[muxCh].DeInit(); } HAL_DMA_DeInit(adcHandle->DMA_Handle); } diff --git a/src/per/adc.h b/src/per/adc.h index 14db03927..d99b6a84e 100644 --- a/src/per/adc.h +++ b/src/per/adc.h @@ -56,7 +56,7 @@ struct AdcChannelConfig \param pin Pin to init. \param speed conversion speed for this pin defaults to 8.5 cycles */ - void InitSingle(dsy_gpio_pin pin, ConversionSpeed speed = SPEED_8CYCLES_5); + void InitSingle(Pin pin, ConversionSpeed speed = SPEED_8CYCLES_5); /** Initializes a single ADC pin as a Multiplexed ADC. @@ -71,15 +71,15 @@ struct AdcChannelConfig \param mux_2 Third mux pin \param speed conversion speed for this pin defaults to 8.5 cycles */ - void InitMux(dsy_gpio_pin adc_pin, + void InitMux(Pin adc_pin, size_t mux_channels, - dsy_gpio_pin mux_0, - dsy_gpio_pin mux_1 = {DSY_GPIOX, 0}, - dsy_gpio_pin mux_2 = {DSY_GPIOX, 0}, + Pin mux_0, + Pin mux_1 = Pin(PORTX, 0), + Pin mux_2 = Pin(PORTX, 0), ConversionSpeed speed = SPEED_8CYCLES_5); - dsy_gpio pin_; /**< & */ - dsy_gpio mux_pin_[MUX_SEL_LAST]; /**< & */ + GPIO pin_; /**< & */ + GPIO mux_pin_[MUX_SEL_LAST]; /**< & */ uint8_t mux_channels_; /**< & */ ConversionSpeed speed_; }; diff --git a/src/per/dac.cpp b/src/per/dac.cpp index 23c34bf20..0a876d29c 100644 --- a/src/per/dac.cpp +++ b/src/per/dac.cpp @@ -3,11 +3,6 @@ #include "per/tim.h" #include "per/dac.h" -extern "C" -{ -#include "util/hal_map.h" -} - namespace daisy { /** Private Implementation class */ @@ -53,6 +48,7 @@ class DacHandle::Impl size_t buff_size_; uint16_t * buff_[2]; DacHandle::DacCallback callback_; + GPIO d1, d2; }; // ================================================================ @@ -238,19 +234,13 @@ void DacHandle::Impl::InitPins() if(ChannelOneActive()) { // Init PA4 as Analog - dsy_gpio d1; - d1.pin = {DSY_GPIOA, 4}; - d1.mode = DSY_GPIO_MODE_ANALOG; - dsy_gpio_init(&d1); + d1.Init(Pin(PORTA, 4), GPIO::Mode::ANALOG); } if(ChannelTwoActive()) { // Init PA5 as Analog - dsy_gpio d2; - d2.pin = {DSY_GPIOA, 5}; - d2.mode = DSY_GPIO_MODE_ANALOG; - dsy_gpio_init(&d2); + d2.Init(Pin(PORTA, 5), GPIO::Mode::ANALOG); } } @@ -309,15 +299,11 @@ void DacHandle::Impl::DeInitPins() { if(ChannelOneActive()) { - dsy_gpio d1; - d1.pin = {DSY_GPIOA, 4}; - dsy_gpio_deinit(&d1); + d1.DeInit(); } if(ChannelTwoActive()) { - dsy_gpio d2; - d2.pin = {DSY_GPIOA, 5}; - dsy_gpio_deinit(&d2); + d2.DeInit(); } } diff --git a/src/per/dac.h b/src/per/dac.h index 01db93d51..60677fa2e 100644 --- a/src/per/dac.h +++ b/src/per/dac.h @@ -1,7 +1,7 @@ #pragma once #ifndef DSY_DAC_H #define DSY_DAC_H - +#include "stm32h7xx_hal.h" #include "daisy_core.h" namespace daisy diff --git a/src/per/gpio.cpp b/src/per/gpio.cpp index d89017103..47f1785bb 100644 --- a/src/per/gpio.cpp +++ b/src/per/gpio.cpp @@ -8,6 +8,11 @@ void GPIO::Init(const Config &cfg) /** Copy Config */ cfg_ = cfg; + Init(); +} + +void GPIO::Init() +{ if(!cfg_.pin.IsValid()) return; @@ -114,93 +119,3 @@ uint32_t *GPIO::GetGPIOBaseRegister() default: return NULL; } } - -// #include "stm32h7xx_hal.h" -// #include "per/gpio.h" - -/** OLD C Stuff */ -extern "C" -{ -#include "util/hal_map.h" - - static void start_clock_for_pin(const dsy_gpio *p) - { - switch(p->pin.port) - { - case DSY_GPIOA: __HAL_RCC_GPIOA_CLK_ENABLE(); break; - case DSY_GPIOB: __HAL_RCC_GPIOB_CLK_ENABLE(); break; - case DSY_GPIOC: __HAL_RCC_GPIOC_CLK_ENABLE(); break; - case DSY_GPIOD: __HAL_RCC_GPIOD_CLK_ENABLE(); break; - case DSY_GPIOE: __HAL_RCC_GPIOE_CLK_ENABLE(); break; - case DSY_GPIOF: __HAL_RCC_GPIOF_CLK_ENABLE(); break; - case DSY_GPIOG: __HAL_RCC_GPIOG_CLK_ENABLE(); break; - case DSY_GPIOH: __HAL_RCC_GPIOH_CLK_ENABLE(); break; - case DSY_GPIOI: __HAL_RCC_GPIOI_CLK_ENABLE(); break; - default: break; - } - } - - void dsy_gpio_init(const dsy_gpio *p) - { - GPIO_TypeDef * port; - GPIO_InitTypeDef ginit; - switch(p->mode) - { - case DSY_GPIO_MODE_INPUT: ginit.Mode = GPIO_MODE_INPUT; break; - case DSY_GPIO_MODE_OUTPUT_PP: - ginit.Mode = GPIO_MODE_OUTPUT_PP; - break; - case DSY_GPIO_MODE_OUTPUT_OD: - ginit.Mode = GPIO_MODE_OUTPUT_OD; - break; - case DSY_GPIO_MODE_ANALOG: ginit.Mode = GPIO_MODE_ANALOG; break; - default: ginit.Mode = GPIO_MODE_INPUT; break; - } - switch(p->pull) - { - case DSY_GPIO_NOPULL: ginit.Pull = GPIO_NOPULL; break; - case DSY_GPIO_PULLUP: ginit.Pull = GPIO_PULLUP; break; - case DSY_GPIO_PULLDOWN: ginit.Pull = GPIO_PULLDOWN; break; - default: ginit.Pull = GPIO_NOPULL; break; - } - ginit.Speed = GPIO_SPEED_LOW; - port = dsy_hal_map_get_port(&p->pin); - ginit.Pin = dsy_hal_map_get_pin(&p->pin); - start_clock_for_pin(p); - HAL_GPIO_Init(port, &ginit); - } - - void dsy_gpio_deinit(const dsy_gpio *p) - { - GPIO_TypeDef *port; - uint16_t pin; - port = dsy_hal_map_get_port(&p->pin); - pin = dsy_hal_map_get_pin(&p->pin); - HAL_GPIO_DeInit(port, pin); - } - - uint8_t dsy_gpio_read(const dsy_gpio *p) - { - return HAL_GPIO_ReadPin(dsy_hal_map_get_port(&p->pin), - dsy_hal_map_get_pin(&p->pin)); - // return HAL_GPIO_ReadPin((GPIO_TypeDef *)gpio_hal_port_map[p->pin.port], - // gpio_hal_pin_map[p->pin.pin]); - } - - void dsy_gpio_write(const dsy_gpio *p, uint8_t state) - { - return HAL_GPIO_WritePin(dsy_hal_map_get_port(&p->pin), - dsy_hal_map_get_pin(&p->pin), - (GPIO_PinState)(state > 0 ? 1 : 0)); - // HAL_GPIO_WritePin((GPIO_TypeDef *)gpio_hal_port_map[p->pin.port], - // gpio_hal_pin_map[p->pin.pin], - // (GPIO_PinState)(state > 0 ? 1 : 0)); - } - void dsy_gpio_toggle(const dsy_gpio *p) - { - return HAL_GPIO_TogglePin(dsy_hal_map_get_port(&p->pin), - dsy_hal_map_get_pin(&p->pin)); - // HAL_GPIO_TogglePin((GPIO_TypeDef *)gpio_hal_port_map[p->pin.port], - // gpio_hal_pin_map[p->pin.pin]); - } -} diff --git a/src/per/gpio.h b/src/per/gpio.h index e23c9e1ac..e37595daa 100644 --- a/src/per/gpio.h +++ b/src/per/gpio.h @@ -73,6 +73,9 @@ class GPIO GPIO() {} + /** @brief Initialize the GPIO using the internal Config struct */ + void Init(); + /** @brief Initialize the GPIO from a Config struct * @param cfg reference to a Config struct populated with the desired settings */ @@ -137,77 +140,5 @@ class GPIO } // namespace daisy - -/** @ingroup peripheral - * @addtogroup DEPRECATED-OLD-GPIO - * - * @brief Deprecated C API for GPIO is staying in place for a - * few versions to support backwards compatibility. - * - * This should not be used for anything new. - * @deprecated These should only be used for casting to configs, and are planned to be reomved in a future version. - * @{ - */ -extern "C" -{ - /** General Purpose IO driver */ - - /** Sets the mode of the GPIO */ - typedef enum - { - DSY_GPIO_MODE_INPUT, /**< & */ - DSY_GPIO_MODE_OUTPUT_PP, /**< Push-Pull */ - DSY_GPIO_MODE_OUTPUT_OD, /**< Open-Drain */ - DSY_GPIO_MODE_ANALOG, /**< & */ - DSY_GPIO_MODE_LAST, /**< & */ - } dsy_gpio_mode; - - /** Configures whether an internal Pull up or Pull down resistor is used */ - typedef enum - { - DSY_GPIO_NOPULL, /**< & */ - DSY_GPIO_PULLUP, /**< & */ - DSY_GPIO_PULLDOWN, /**< & */ - } dsy_gpio_pull; - - /** Struct for holding the pin, and configuration */ - typedef struct - { - dsy_gpio_pin pin; /**< & */ - dsy_gpio_mode mode; /**< & */ - dsy_gpio_pull pull; /**< & */ - } dsy_gpio; - - /** Initializes the gpio with the settings configured. - \param *p Pin pointer - */ - void dsy_gpio_init(const dsy_gpio *p); - - /** Deinitializes the gpio pin - \param *p Pin pointer - */ - void dsy_gpio_deinit(const dsy_gpio *p); - - /** - Reads the state of the gpio pin - \param *p Pin pointer - \return 1 if the pin is HIGH, and 0 if the pin is LOW */ - uint8_t dsy_gpio_read(const dsy_gpio *p); - - /** - Writes the state to the gpio pin - Pin will be set to 3v3 when state is 1, and 0V when state is 0 - \param *p Pin pointer - \param state State to write - */ - void dsy_gpio_write(const dsy_gpio *p, uint8_t state); - - /** Toggles the state of the pin so that it is not at the same state as it was previously. - \param *p Pin pointer - */ - void dsy_gpio_toggle(const dsy_gpio *p); - /**@} */ -} #endif - #endif diff --git a/src/per/i2c.cpp b/src/per/i2c.cpp index 3a8476105..1a7db87c9 100644 --- a/src/per/i2c.cpp +++ b/src/per/i2c.cpp @@ -1,10 +1,6 @@ #include "per/i2c.h" #include "sys/system.h" #include "util/scopedirqblocker.h" -extern "C" -{ -#include "util/hal_map.h" -} namespace daisy { @@ -636,11 +632,11 @@ void I2CHandle::Impl::InitPins() default: break; } - port = dsy_hal_map_get_port(&config_.pin_config.scl); - GPIO_InitStruct.Pin = dsy_hal_map_get_pin(&config_.pin_config.scl); + port = GetHALPort(config_.pin_config.scl); + GPIO_InitStruct.Pin = GetHALPin(config_.pin_config.scl); HAL_GPIO_Init(port, &GPIO_InitStruct); - port = dsy_hal_map_get_port(&config_.pin_config.sda); - GPIO_InitStruct.Pin = dsy_hal_map_get_pin(&config_.pin_config.sda); + port = GetHALPort(config_.pin_config.sda); + GPIO_InitStruct.Pin = GetHALPin(config_.pin_config.sda); HAL_GPIO_Init(port, &GPIO_InitStruct); } @@ -648,11 +644,11 @@ void I2CHandle::Impl::DeinitPins() { GPIO_TypeDef* port; uint16_t pin; - port = dsy_hal_map_get_port(&config_.pin_config.scl); - pin = dsy_hal_map_get_pin(&config_.pin_config.scl); + port = GetHALPort(config_.pin_config.scl); + pin = GetHALPin(config_.pin_config.scl); HAL_GPIO_DeInit(port, pin); - port = dsy_hal_map_get_port(&config_.pin_config.sda); - pin = dsy_hal_map_get_pin(&config_.pin_config.sda); + port = GetHALPort(config_.pin_config.sda); + pin = GetHALPin(config_.pin_config.sda); HAL_GPIO_DeInit(port, pin); } diff --git a/src/per/i2c.h b/src/per/i2c.h index f7d7a89d1..f3903a7d0 100644 --- a/src/per/i2c.h +++ b/src/per/i2c.h @@ -1,4 +1,5 @@ #pragma once +#include "util/hal_map.h" #include "daisy_core.h" namespace daisy @@ -56,9 +57,9 @@ class I2CHandle Peripheral periph; /**< & */ struct { - dsy_gpio_pin scl; /**< & */ - dsy_gpio_pin sda; /**< & */ - } pin_config; /**< & */ + Pin scl; /**< & */ + Pin sda; /**< & */ + } pin_config; /**< & */ Speed speed; /**< & */ Mode mode; /**< & */ diff --git a/src/per/qspi.cpp b/src/per/qspi.cpp index 17727910a..970a6a4f4 100644 --- a/src/per/qspi.cpp +++ b/src/per/qspi.cpp @@ -4,10 +4,6 @@ #include "stm32h7xx_hal.h" #include "dev/flash_IS25LP080D.h" #include "dev/flash_IS25LP064A.h" -extern "C" -{ -#include "util/hal_map.h" -} // TODO: Add handling for alternate device types, // This will be a thing much sooner than anticipated @@ -93,14 +89,14 @@ class QSPIHandle::Impl Status status_; static constexpr size_t pin_count_ - = sizeof(QSPIHandle::Config::pin_config) / sizeof(dsy_gpio_pin); + = sizeof(QSPIHandle::Config::pin_config) / sizeof(Pin); // Data structure for easy hal initialization - dsy_gpio_pin* pin_config_arr_[pin_count_] = {&config_.pin_config.io0, - &config_.pin_config.io1, - &config_.pin_config.io2, - &config_.pin_config.io3, - &config_.pin_config.clk, - &config_.pin_config.ncs}; + Pin* pin_config_arr_[pin_count_] = {&config_.pin_config.io0, + &config_.pin_config.io1, + &config_.pin_config.io2, + &config_.pin_config.io3, + &config_.pin_config.clk, + &config_.pin_config.ncs}; }; @@ -836,15 +832,15 @@ uint8_t QSPIHandle::Impl::GetStatusRegister() uint32_t QSPIHandle::Impl::GetPin(size_t pin) { - dsy_gpio_pin* p = pin_config_arr_[pin]; - return dsy_hal_map_get_pin(p); + Pin* p = pin_config_arr_[pin]; + return GetHALPin(*p); } GPIO_TypeDef* QSPIHandle::Impl::GetPort(size_t pin) { - dsy_gpio_pin* p = pin_config_arr_[pin]; - return dsy_hal_map_get_port(p); + Pin* p = pin_config_arr_[pin]; + return GetHALPort(*p); } diff --git a/src/per/qspi.h b/src/per/qspi.h index b1da9efd4..9dfa54ea3 100644 --- a/src/per/qspi.h +++ b/src/per/qspi.h @@ -4,7 +4,8 @@ #ifndef UNIT_TEST // for unit tests, a dummy implementation #include -#include "daisy_core.h" // Added for dsy_gpio_pin typedef +#include "daisy_core.h" +#include "util/hal_map.h" #define DSY_QSPI_TEXT \ __attribute__((section( \ @@ -81,12 +82,12 @@ class QSPIHandle //SCK, CE# (active low) struct { - dsy_gpio_pin io0; /**< & */ - dsy_gpio_pin io1; /**< & */ - dsy_gpio_pin io2; /**< & */ - dsy_gpio_pin io3; /**< & */ - dsy_gpio_pin clk; /**< & */ - dsy_gpio_pin ncs; /**< & */ + Pin io0; /**< & */ + Pin io1; /**< & */ + Pin io2; /**< & */ + Pin io3; /**< & */ + Pin clk; /**< & */ + Pin ncs; /**< & */ } pin_config; Device device; diff --git a/src/per/rng.cpp b/src/per/rng.cpp index 68f5cbd91..99399aee1 100644 --- a/src/per/rng.cpp +++ b/src/per/rng.cpp @@ -1,5 +1,5 @@ #include "rng.h" -#include "util/hal_map.h" +#include "stm32h7xx_hal.h" #include "sys/system.h" #define RNG_TIMEOUT 100 diff --git a/src/per/sai.cpp b/src/per/sai.cpp index 869111e24..d581f99c5 100644 --- a/src/per/sai.cpp +++ b/src/per/sai.cpp @@ -1,9 +1,5 @@ #include "per/sai.h" #include "daisy_core.h" -extern "C" -{ -#include "util/hal_map.h" -} namespace daisy { @@ -364,14 +360,14 @@ void SaiHandle::Impl::InitPins() bool is_master; GPIO_InitTypeDef GPIO_InitStruct; GPIO_TypeDef* port; - dsy_gpio_pin* cfg[] = {&config_.pin_config.fs, - &config_.pin_config.mclk, - &config_.pin_config.sck, - &config_.pin_config.sa, - &config_.pin_config.sb}; + Pin cfg[] = {config_.pin_config.fs, + config_.pin_config.mclk, + config_.pin_config.sck, + config_.pin_config.sa, + config_.pin_config.sb}; // Special Case checks - dsy_gpio_pin sck_af_pin = {DSY_GPIOA, 2}; - is_master = (config_.a_sync == Config::Sync::MASTER + Pin sck_af_pin = Pin(PORTA, 2); + is_master = (config_.a_sync == Config::Sync::MASTER || config_.b_sync == Config::Sync::MASTER); // Generics GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; @@ -380,7 +376,7 @@ void SaiHandle::Impl::InitPins() for(size_t i = 0; i < 5; i++) { // Skip MCLK if not master. - if(dsy_pin_cmp(&config_.pin_config.mclk, cfg[i]) && !is_master) + if(config_.pin_config.mclk == cfg[i] && !is_master) continue; // Special case for SAI2-sck (should add a map for Alternate Functions at some point..) @@ -390,15 +386,14 @@ void SaiHandle::Impl::InitPins() GPIO_InitStruct.Alternate = GPIO_AF6_SAI1; break; case Config::Peripheral::SAI_2: - GPIO_InitStruct.Alternate = dsy_pin_cmp(cfg[i], &sck_af_pin) - ? GPIO_AF8_SAI2 - : GPIO_AF10_SAI2; + GPIO_InitStruct.Alternate + = cfg[i] == sck_af_pin ? GPIO_AF8_SAI2 : GPIO_AF10_SAI2; break; default: break; } - GPIO_InitStruct.Pin = dsy_hal_map_get_pin(cfg[i]); - port = dsy_hal_map_get_port(cfg[i]); + GPIO_InitStruct.Pin = GetHALPin(cfg[i]); + port = GetHALPort(cfg[i]); HAL_GPIO_Init(port, &GPIO_InitStruct); } } @@ -412,21 +407,21 @@ void SaiHandle::Impl::DeInitPins() || config_.b_sync == Config::Sync::MASTER); if(is_master) { - port = dsy_hal_map_get_port(&config_.pin_config.mclk); - pin = dsy_hal_map_get_pin(&config_.pin_config.mclk); + port = GetHALPort(config_.pin_config.mclk); + pin = GetHALPin(config_.pin_config.mclk); HAL_GPIO_DeInit(port, pin); } - port = dsy_hal_map_get_port(&config_.pin_config.fs); - pin = dsy_hal_map_get_pin(&config_.pin_config.fs); + port = GetHALPort(config_.pin_config.fs); + pin = GetHALPin(config_.pin_config.fs); HAL_GPIO_DeInit(port, pin); - port = dsy_hal_map_get_port(&config_.pin_config.sck); - pin = dsy_hal_map_get_pin(&config_.pin_config.sck); + port = GetHALPort(config_.pin_config.sck); + pin = GetHALPin(config_.pin_config.sck); HAL_GPIO_DeInit(port, pin); - port = dsy_hal_map_get_port(&config_.pin_config.sa); - pin = dsy_hal_map_get_pin(&config_.pin_config.sa); + port = GetHALPort(config_.pin_config.sa); + pin = GetHALPin(config_.pin_config.sa); HAL_GPIO_DeInit(port, pin); - port = dsy_hal_map_get_port(&config_.pin_config.sb); - pin = dsy_hal_map_get_pin(&config_.pin_config.sb); + port = GetHALPort(config_.pin_config.sb); + pin = GetHALPin(config_.pin_config.sb); HAL_GPIO_DeInit(port, pin); } diff --git a/src/per/sai.h b/src/per/sai.h index bec5c06e9..54c0e72a9 100644 --- a/src/per/sai.h +++ b/src/per/sai.h @@ -2,6 +2,7 @@ #ifndef DSY_SAI_H #define DSY_SAI_H +#include "util/hal_map.h" #include "daisy_core.h" namespace daisy @@ -90,7 +91,7 @@ class SaiHandle Peripheral periph; struct { - dsy_gpio_pin mclk, fs, sck, sa, sb; + Pin mclk, fs, sck, sa, sb; } pin_config; SampleRate sr; BitDepth bit_depth; diff --git a/src/per/sdmmc.cpp b/src/per/sdmmc.cpp index 600a10f97..c40af0c01 100644 --- a/src/per/sdmmc.cpp +++ b/src/per/sdmmc.cpp @@ -1,7 +1,4 @@ #include "per/sdmmc.h" -#include "util/hal_map.h" -//#include "fatfs.h" - using namespace daisy; diff --git a/src/per/sdmmc.h b/src/per/sdmmc.h index 625704263..05867f100 100644 --- a/src/per/sdmmc.h +++ b/src/per/sdmmc.h @@ -7,7 +7,7 @@ #define DSY_SDMMC_H /**< macro */ #include - +#include "stm32h7xx_hal.h" namespace daisy { diff --git a/src/per/spi.cpp b/src/per/spi.cpp index 63f8ea61f..2b56034c9 100644 --- a/src/per/spi.cpp +++ b/src/per/spi.cpp @@ -1,16 +1,10 @@ #include "per/spi.h" #include "util/scopedirqblocker.h" -extern "C" -{ -#include "util/hal_map.h" -} - // TODO // - fix up rest of lib so that we can add a spi_handle map to the hal map // - Add configuration for standard spi stuff. - using namespace daisy; static void Error_Handler() @@ -712,60 +706,60 @@ SpiHandle::Result SpiHandle::Impl::BlockingTransmitAndReceive(uint8_t* tx_buff, typedef struct { - dsy_gpio_pin pin; - uint8_t alt; + Pin pin; + uint8_t alt; } pin_alt_spi; -static pin_alt_spi pins_none_spi = {{DSY_GPIOX, 0}, 255}; +static pin_alt_spi pins_none_spi = {Pin(PORTX, 0), 255}; //this is a bit long... /* ============== spi1 ============== */ -static pin_alt_spi spi1_pins_sclk[] = {{{DSY_GPIOG, 11}, GPIO_AF5_SPI1}, - {{DSY_GPIOA, 5}, GPIO_AF5_SPI1}, +static pin_alt_spi spi1_pins_sclk[] = {{Pin(PORTG, 11), GPIO_AF5_SPI1}, + {Pin(PORTA, 5), GPIO_AF5_SPI1}, pins_none_spi}; -static pin_alt_spi spi1_pins_miso[] = {{{DSY_GPIOB, 4}, GPIO_AF5_SPI1}, - {{DSY_GPIOA, 6}, GPIO_AF5_SPI1}, - {{DSY_GPIOG, 9}, GPIO_AF5_SPI1}}; +static pin_alt_spi spi1_pins_miso[] = {{Pin(PORTB, 4), GPIO_AF5_SPI1}, + {Pin(PORTA, 6), GPIO_AF5_SPI1}, + {Pin(PORTG, 9), GPIO_AF5_SPI1}}; -static pin_alt_spi spi1_pins_mosi[] = {{{DSY_GPIOB, 5}, GPIO_AF5_SPI1}, - {{DSY_GPIOA, 7}, GPIO_AF5_SPI1}, +static pin_alt_spi spi1_pins_mosi[] = {{Pin(PORTB, 5), GPIO_AF5_SPI1}, + {Pin(PORTA, 7), GPIO_AF5_SPI1}, pins_none_spi}; -static pin_alt_spi spi1_pins_nss[] = {{{DSY_GPIOG, 10}, GPIO_AF5_SPI1}, - {{DSY_GPIOA, 4}, GPIO_AF5_SPI1}, +static pin_alt_spi spi1_pins_nss[] = {{Pin(PORTG, 10), GPIO_AF5_SPI1}, + {Pin(PORTA, 4), GPIO_AF5_SPI1}, pins_none_spi}; /* ============== spi2 ============== */ static pin_alt_spi spi2_pins_sclk[] - = {{{DSY_GPIOD, 3}, GPIO_AF5_SPI2}, pins_none_spi, pins_none_spi}; + = {{Pin(PORTD, 3), GPIO_AF5_SPI2}, pins_none_spi, pins_none_spi}; -static pin_alt_spi spi2_pins_miso[] = {{{DSY_GPIOB, 14}, GPIO_AF5_SPI2}, - {{DSY_GPIOC, 2}, GPIO_AF5_SPI2}, +static pin_alt_spi spi2_pins_miso[] = {{Pin(PORTB, 14), GPIO_AF5_SPI2}, + {Pin(PORTC, 2), GPIO_AF5_SPI2}, pins_none_spi}; -static pin_alt_spi spi2_pins_mosi[] = {{{DSY_GPIOC, 1}, GPIO_AF5_SPI2}, - {{DSY_GPIOB, 15}, GPIO_AF5_SPI2}, - {{DSY_GPIOC, 3}, GPIO_AF5_SPI2}}; +static pin_alt_spi spi2_pins_mosi[] = {{Pin(PORTC, 1), GPIO_AF5_SPI2}, + {Pin(PORTB, 15), GPIO_AF5_SPI2}, + {Pin(PORTC, 3), GPIO_AF5_SPI2}}; -static pin_alt_spi spi2_pins_nss[] = {{{DSY_GPIOB, 12}, GPIO_AF5_SPI2}, - {{DSY_GPIOB, 4}, GPIO_AF7_SPI2}, - {{DSY_GPIOB, 9}, GPIO_AF5_SPI2}}; +static pin_alt_spi spi2_pins_nss[] = {{Pin(PORTB, 12), GPIO_AF5_SPI2}, + {Pin(PORTB, 4), GPIO_AF7_SPI2}, + {Pin(PORTB, 9), GPIO_AF5_SPI2}}; /* ============== spi3 ============== */ static pin_alt_spi spi3_pins_sclk[] - = {{{DSY_GPIOC, 10}, GPIO_AF6_SPI3}, pins_none_spi, pins_none_spi}; + = {{Pin(PORTC, 10), GPIO_AF6_SPI3}, pins_none_spi, pins_none_spi}; -static pin_alt_spi spi3_pins_miso[] = {{{DSY_GPIOC, 11}, GPIO_AF6_SPI3}, - {{DSY_GPIOB, 4}, GPIO_AF6_SPI3}, +static pin_alt_spi spi3_pins_miso[] = {{Pin(PORTC, 11), GPIO_AF6_SPI3}, + {Pin(PORTB, 4), GPIO_AF6_SPI3}, pins_none_spi}; -static pin_alt_spi spi3_pins_mosi[] = {{{DSY_GPIOC, 12}, GPIO_AF6_SPI3}, - {{DSY_GPIOB, 5}, GPIO_AF7_SPI3}, +static pin_alt_spi spi3_pins_mosi[] = {{Pin(PORTC, 12), GPIO_AF6_SPI3}, + {Pin(PORTB, 5), GPIO_AF7_SPI3}, pins_none_spi}; static pin_alt_spi spi3_pins_nss[] - = {{{DSY_GPIOA, 4}, GPIO_AF6_SPI3}, pins_none_spi, pins_none_spi}; + = {{Pin(PORTA, 4), GPIO_AF6_SPI3}, pins_none_spi, pins_none_spi}; /* ============== spi4 ============== */ static pin_alt_spi spi4_pins_sclk[] @@ -795,18 +789,18 @@ static pin_alt_spi spi5_pins_nss[] /* ============== spi6 ============== */ static pin_alt_spi spi6_pins_sclk[] - = {{{DSY_GPIOA, 5}, GPIO_AF8_SPI6}, pins_none_spi, pins_none_spi}; + = {{Pin(PORTA, 5), GPIO_AF8_SPI6}, pins_none_spi, pins_none_spi}; -static pin_alt_spi spi6_pins_miso[] = {{{DSY_GPIOB, 4}, GPIO_AF8_SPI6}, - {{DSY_GPIOA, 6}, GPIO_AF8_SPI6}, +static pin_alt_spi spi6_pins_miso[] = {{Pin(PORTB, 4), GPIO_AF8_SPI6}, + {Pin(PORTA, 6), GPIO_AF8_SPI6}, pins_none_spi}; -static pin_alt_spi spi6_pins_mosi[] = {{{DSY_GPIOB, 5}, GPIO_AF8_SPI6}, - {{DSY_GPIOA, 7}, GPIO_AF8_SPI6}, +static pin_alt_spi spi6_pins_mosi[] = {{Pin(PORTB, 5), GPIO_AF8_SPI6}, + {Pin(PORTA, 7), GPIO_AF8_SPI6}, pins_none_spi}; static pin_alt_spi spi6_pins_nss[] - = {{{DSY_GPIOA, 4}, GPIO_AF8_SPI6}, pins_none_spi, pins_none_spi}; + = {{Pin(PORTA, 4), GPIO_AF8_SPI6}, pins_none_spi, pins_none_spi}; //an array to hold everything static pin_alt_spi* pins_periphs_spi[] = { @@ -818,17 +812,16 @@ static pin_alt_spi* pins_periphs_spi[] = { spi6_pins_sclk, spi6_pins_miso, spi6_pins_mosi, spi6_pins_nss, }; -SpiHandle::Result -checkPinMatchSpi(GPIO_InitTypeDef* init, dsy_gpio_pin pin, int p_num) +SpiHandle::Result checkPinMatchSpi(GPIO_InitTypeDef* init, Pin pin, int p_num) { for(int i = 0; i < 3; i++) { - if(dsy_pin_cmp(&pins_periphs_spi[p_num][i].pin, &pins_none_spi.pin)) + if(pins_periphs_spi[p_num][i].pin == pins_none_spi.pin) { /* skip */ } - else if(dsy_pin_cmp(&pins_periphs_spi[p_num][i].pin, &pin)) + else if(pins_periphs_spi[p_num][i].pin == pin) { init->Alternate = pins_periphs_spi[p_num][i].alt; return SpiHandle::Result::OK; @@ -886,7 +879,7 @@ SpiHandle::Result SpiHandle::Impl::InitPins() // nss = soft -> ss pin is unused for master and slave bool enable_ss = config_.nss != Config::NSS::SOFT; - if(config_.pin_config.sclk.port != DSY_GPIOX) + if(config_.pin_config.sclk.port != PORTX) { //check sclk against periph if(checkPinMatchSpi(&GPIO_InitStruct, config_.pin_config.sclk, per_num) @@ -896,12 +889,12 @@ SpiHandle::Result SpiHandle::Impl::InitPins() } //setup sclk pin - GPIO_TypeDef* port = dsy_hal_map_get_port(&config_.pin_config.sclk); - GPIO_InitStruct.Pin = dsy_hal_map_get_pin(&config_.pin_config.sclk); + GPIO_TypeDef* port = GetHALPort(config_.pin_config.sclk); + GPIO_InitStruct.Pin = GetHALPin(config_.pin_config.sclk); HAL_GPIO_Init(port, &GPIO_InitStruct); } - if(config_.pin_config.miso.port != DSY_GPIOX && enable_miso) + if(config_.pin_config.miso.port != PORTX && enable_miso) { //check miso against periph if(checkPinMatchSpi( @@ -912,12 +905,12 @@ SpiHandle::Result SpiHandle::Impl::InitPins() } //setup miso pin - GPIO_TypeDef* port = dsy_hal_map_get_port(&config_.pin_config.miso); - GPIO_InitStruct.Pin = dsy_hal_map_get_pin(&config_.pin_config.miso); + GPIO_TypeDef* port = GetHALPort(config_.pin_config.miso); + GPIO_InitStruct.Pin = GetHALPin(config_.pin_config.miso); HAL_GPIO_Init(port, &GPIO_InitStruct); } - if(config_.pin_config.mosi.port != DSY_GPIOX && enable_mosi) + if(config_.pin_config.mosi.port != PORTX && enable_mosi) { //check mosi against periph if(checkPinMatchSpi( @@ -928,12 +921,12 @@ SpiHandle::Result SpiHandle::Impl::InitPins() } //setup mosi pin - GPIO_TypeDef* port = dsy_hal_map_get_port(&config_.pin_config.mosi); - GPIO_InitStruct.Pin = dsy_hal_map_get_pin(&config_.pin_config.mosi); + GPIO_TypeDef* port = GetHALPort(config_.pin_config.mosi); + GPIO_InitStruct.Pin = GetHALPin(config_.pin_config.mosi); HAL_GPIO_Init(port, &GPIO_InitStruct); } - if(config_.pin_config.nss.port != DSY_GPIOX && enable_ss) + if(config_.pin_config.nss.port != PORTX && enable_ss) { //check nss against periph if(checkPinMatchSpi( @@ -944,8 +937,8 @@ SpiHandle::Result SpiHandle::Impl::InitPins() } //setup nss pin - GPIO_TypeDef* port = dsy_hal_map_get_port(&config_.pin_config.nss); - GPIO_InitStruct.Pin = dsy_hal_map_get_pin(&config_.pin_config.nss); + GPIO_TypeDef* port = GetHALPort(config_.pin_config.nss); + GPIO_InitStruct.Pin = GetHALPin(config_.pin_config.nss); HAL_GPIO_Init(port, &GPIO_InitStruct); } @@ -954,20 +947,20 @@ SpiHandle::Result SpiHandle::Impl::InitPins() SpiHandle::Result SpiHandle::Impl::DeInitPins() { - GPIO_TypeDef* port = dsy_hal_map_get_port(&config_.pin_config.sclk); - uint16_t pin = dsy_hal_map_get_pin(&config_.pin_config.sclk); + GPIO_TypeDef* port = GetHALPort(config_.pin_config.sclk); + uint16_t pin = GetHALPin(config_.pin_config.sclk); HAL_GPIO_DeInit(port, pin); - port = dsy_hal_map_get_port(&config_.pin_config.miso); - pin = dsy_hal_map_get_pin(&config_.pin_config.miso); + port = GetHALPort(config_.pin_config.miso); + pin = GetHALPin(config_.pin_config.miso); HAL_GPIO_DeInit(port, pin); - port = dsy_hal_map_get_port(&config_.pin_config.mosi); - pin = dsy_hal_map_get_pin(&config_.pin_config.mosi); + port = GetHALPort(config_.pin_config.mosi); + pin = GetHALPin(config_.pin_config.mosi); HAL_GPIO_DeInit(port, pin); - port = dsy_hal_map_get_port(&config_.pin_config.nss); - pin = dsy_hal_map_get_pin(&config_.pin_config.nss); + port = GetHALPort(config_.pin_config.nss); + pin = GetHALPin(config_.pin_config.nss); HAL_GPIO_DeInit(port, pin); return Result::OK; @@ -985,10 +978,10 @@ void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle) SpiHandle::Impl* handle = MapInstanceToHandle(spiHandle->Instance); //enable clock on all 4 pins - dsy_hal_map_gpio_clk_enable(handle->config_.pin_config.sclk.port); - dsy_hal_map_gpio_clk_enable(handle->config_.pin_config.miso.port); - dsy_hal_map_gpio_clk_enable(handle->config_.pin_config.mosi.port); - dsy_hal_map_gpio_clk_enable(handle->config_.pin_config.nss.port); + GPIOClockEnable(handle->config_.pin_config.sclk); + GPIOClockEnable(handle->config_.pin_config.miso); + GPIOClockEnable(handle->config_.pin_config.mosi); + GPIOClockEnable(handle->config_.pin_config.nss); //enable clock for our peripheral switch(handle->config_.periph) diff --git a/src/per/spi.h b/src/per/spi.h index 30e4e38c9..5e6d2e6cc 100644 --- a/src/per/spi.h +++ b/src/per/spi.h @@ -4,6 +4,10 @@ #include "daisy_core.h" +#if !UNIT_TEST +#include "util/hal_map.h" +#endif + /* TODO: - Add documentation - Add IT @@ -78,10 +82,10 @@ class SpiHandle struct { - dsy_gpio_pin sclk; /**< & */ - dsy_gpio_pin miso; /**< & */ - dsy_gpio_pin mosi; /**< & */ - dsy_gpio_pin nss; /**< & */ + Pin sclk; /**< & */ + Pin miso; /**< & */ + Pin mosi; /**< & */ + Pin nss; /**< & */ } pin_config; Config() diff --git a/src/per/spiMultislave.cpp b/src/per/spiMultislave.cpp index 4d8329ef9..829ac660c 100644 --- a/src/per/spiMultislave.cpp +++ b/src/per/spiMultislave.cpp @@ -11,10 +11,7 @@ SpiHandle::Result MultiSlaveSpiHandle::Init(const Config& config) for(size_t i = 0; i < config_.num_devices; i++) { - nss_pins[i].pin = config_.pin_config.nss[i]; - nss_pins[i].mode = DSY_GPIO_MODE_OUTPUT_PP; - nss_pins[i].pull = DSY_GPIO_NOPULL; - dsy_gpio_init(&nss_pins[i]); + nss_pins[i].Init(config_.pin_config.nss[i], GPIO::Mode::OUTPUT); DisableDevice(i); } @@ -32,7 +29,7 @@ SpiHandle::Result MultiSlaveSpiHandle::Init(const Config& config) spi_config.pin_config.miso = config.pin_config.miso; spi_config.pin_config.mosi = config.pin_config.mosi; spi_config.pin_config.sclk = config.pin_config.sclk; - spi_config.pin_config.nss = {DSY_GPIOX, 0}; // we'll drive this by ourselves + spi_config.pin_config.nss = Pin(PORTX, 0); // we'll drive this by ourselves return spiHandle_.Init(spi_config); } @@ -164,12 +161,12 @@ int MultiSlaveSpiHandle::CheckError() void MultiSlaveSpiHandle::EnableDevice(size_t device_index) { - dsy_gpio_write(&nss_pins[device_index], 0); + nss_pins[device_index].Write(0); } void MultiSlaveSpiHandle::DisableDevice(size_t device_index) { - dsy_gpio_write(&nss_pins[device_index], 1); + nss_pins[device_index].Write(1); } void MultiSlaveSpiHandle::DmaStartCallback(void* context) diff --git a/src/per/spiMultislave.h b/src/per/spiMultislave.h index b6753caee..846c6b977 100644 --- a/src/per/spiMultislave.h +++ b/src/per/spiMultislave.h @@ -25,10 +25,10 @@ class MultiSlaveSpiHandle { struct { - dsy_gpio_pin sclk; /**< & */ - dsy_gpio_pin miso; /**< & */ - dsy_gpio_pin mosi; /**< & */ - dsy_gpio_pin nss[max_num_devices_]; + Pin sclk; /**< & */ + Pin miso; /**< & */ + Pin mosi; /**< & */ + Pin nss[max_num_devices_]; } pin_config; SpiHandle::Config::Peripheral periph; @@ -154,7 +154,7 @@ class MultiSlaveSpiHandle Config config_; SpiHandle spiHandle_; - dsy_gpio nss_pins[max_num_devices_]; + GPIO nss_pins[max_num_devices_]; struct DmaTransfer { diff --git a/src/per/tim.cpp b/src/per/tim.cpp index fbd1a1a84..6c593452b 100644 --- a/src/per/tim.cpp +++ b/src/per/tim.cpp @@ -1,5 +1,4 @@ #include "per/tim.h" -#include "util/hal_map.h" #include "sys/system.h" diff --git a/src/per/tim.h b/src/per/tim.h index c37b25e0c..274b7cfc1 100644 --- a/src/per/tim.h +++ b/src/per/tim.h @@ -3,6 +3,7 @@ #define DSY_TIM_H #include +#include "stm32h7xx_hal.h" namespace daisy { diff --git a/src/per/uart.cpp b/src/per/uart.cpp index 7f9301dc4..4d0658a16 100644 --- a/src/per/uart.cpp +++ b/src/per/uart.cpp @@ -5,11 +5,6 @@ #include "util/ringbuffer.h" #include "util/scopedirqblocker.h" -extern "C" -{ -#include "util/hal_map.h" -} - using namespace daisy; #define UART_RX_BUFF_SIZE 256 @@ -688,59 +683,59 @@ int UartHandler::Impl::CheckError() typedef struct { - dsy_gpio_pin pin; - uint8_t alt; + Pin pin; + uint8_t alt; } pin_alt; -pin_alt pins_none = {{DSY_GPIOX, 0}, 255}; +pin_alt pins_none = {Pin(PORTX, 0), 255}; //valid pins per periph, and the alt they're on -pin_alt usart1_pins_tx[] = {{{DSY_GPIOB, 6}, GPIO_AF7_USART1}, - {{DSY_GPIOB, 14}, GPIO_AF4_USART1}, +pin_alt usart1_pins_tx[] = {{Pin(PORTB, 6), GPIO_AF7_USART1}, + {Pin(PORTB, 14), GPIO_AF4_USART1}, pins_none}; -pin_alt usart1_pins_rx[] = {{{DSY_GPIOB, 7}, GPIO_AF7_USART1}, - {{DSY_GPIOB, 15}, GPIO_AF4_USART1}, +pin_alt usart1_pins_rx[] = {{Pin(PORTB, 7), GPIO_AF7_USART1}, + {Pin(PORTB, 15), GPIO_AF4_USART1}, pins_none}; pin_alt usart2_pins_tx[] - = {{{DSY_GPIOA, 2}, GPIO_AF7_USART2}, pins_none, pins_none}; + = {{Pin(PORTA, 2), GPIO_AF7_USART2}, pins_none, pins_none}; pin_alt usart2_pins_rx[] - = {{{DSY_GPIOA, 3}, GPIO_AF7_USART2}, pins_none, pins_none}; + = {{Pin(PORTA, 3), GPIO_AF7_USART2}, pins_none, pins_none}; pin_alt usart3_pins_tx[] - = {{{DSY_GPIOC, 10}, GPIO_AF7_USART3}, pins_none, pins_none}; + = {{Pin(PORTC, 10), GPIO_AF7_USART3}, pins_none, pins_none}; pin_alt usart3_pins_rx[] - = {{{DSY_GPIOC, 11}, GPIO_AF7_USART3}, pins_none, pins_none}; + = {{Pin(PORTC, 11), GPIO_AF7_USART3}, pins_none, pins_none}; -pin_alt uart4_pins_tx[] = {{{DSY_GPIOB, 9}, GPIO_AF8_UART4}, - {{DSY_GPIOC, 10}, GPIO_AF8_UART4}, - {{DSY_GPIOA, 0}, GPIO_AF8_UART4}}; -pin_alt uart4_pins_rx[] = {{{DSY_GPIOB, 8}, GPIO_AF8_UART4}, - {{DSY_GPIOC, 11}, GPIO_AF8_UART4}, - {{DSY_GPIOA, 1}, GPIO_AF8_UART4}}; +pin_alt uart4_pins_tx[] = {{Pin(PORTB, 9), GPIO_AF8_UART4}, + {Pin(PORTC, 10), GPIO_AF8_UART4}, + {Pin(PORTA, 0), GPIO_AF8_UART4}}; +pin_alt uart4_pins_rx[] = {{Pin(PORTB, 8), GPIO_AF8_UART4}, + {Pin(PORTC, 11), GPIO_AF8_UART4}, + {Pin(PORTA, 1), GPIO_AF8_UART4}}; -pin_alt uart5_pins_tx[] = {{{DSY_GPIOC, 12}, GPIO_AF8_UART5}, - {{DSY_GPIOB, 6}, GPIO_AF14_UART5}, +pin_alt uart5_pins_tx[] = {{Pin(PORTC, 12), GPIO_AF8_UART5}, + {Pin(PORTB, 6), GPIO_AF14_UART5}, pins_none}; -pin_alt uart5_pins_rx[] = {{{DSY_GPIOB, 12}, GPIO_AF14_UART5}, - {{DSY_GPIOD, 2}, GPIO_AF8_UART5}, - {{DSY_GPIOB, 5}, GPIO_AF14_UART5}}; +pin_alt uart5_pins_rx[] = {{Pin(PORTB, 12), GPIO_AF14_UART5}, + {Pin(PORTD, 2), GPIO_AF8_UART5}, + {Pin(PORTB, 5), GPIO_AF14_UART5}}; pin_alt usart6_pins_tx[] = {pins_none, pins_none, pins_none}; pin_alt usart6_pins_rx[] - = {{{DSY_GPIOG, 9}, GPIO_AF7_USART6}, pins_none, pins_none}; + = {{Pin(PORTG, 9), GPIO_AF7_USART6}, pins_none, pins_none}; pin_alt uart7_pins_tx[] - = {{{DSY_GPIOB, 4}, GPIO_AF11_UART7}, pins_none, pins_none}; + = {{Pin(PORTB, 4), GPIO_AF11_UART7}, pins_none, pins_none}; pin_alt uart7_pins_rx[] = {pins_none, pins_none, pins_none}; pin_alt uart8_pins_tx[] = {pins_none, pins_none, pins_none}; pin_alt uart8_pins_rx[] = {pins_none, pins_none, pins_none}; pin_alt lpuart1_pins_tx[] - = {{{DSY_GPIOB, 6}, GPIO_AF8_LPUART}, pins_none, pins_none}; + = {{Pin(PORTB, 6), GPIO_AF8_LPUART}, pins_none, pins_none}; pin_alt lpuart1_pins_rx[] - = {{{DSY_GPIOB, 7}, GPIO_AF8_LPUART}, pins_none, pins_none}; + = {{Pin(PORTB, 7), GPIO_AF8_LPUART}, pins_none, pins_none}; //an array to hold everything pin_alt* pins_periphs[] = {usart1_pins_tx, @@ -762,17 +757,16 @@ pin_alt* pins_periphs[] = {usart1_pins_tx, lpuart1_pins_tx, lpuart1_pins_rx}; -UartHandler::Result -checkPinMatch(GPIO_InitTypeDef* init, dsy_gpio_pin pin, int p_num) +UartHandler::Result checkPinMatch(GPIO_InitTypeDef* init, Pin pin, int p_num) { for(int i = 0; i < 3; i++) { - if(dsy_pin_cmp(&pins_periphs[p_num][i].pin, &pins_none.pin)) + if(pins_periphs[p_num][i].pin == pins_none.pin) { /* skip */ } - else if(dsy_pin_cmp(&pins_periphs[p_num][i].pin, &pin)) + else if(pins_periphs[p_num][i].pin == pin) { init->Alternate = pins_periphs[p_num][i].alt; return UartHandler::Result::OK; @@ -792,7 +786,7 @@ UartHandler::Result UartHandler::Impl::InitPins() int per_num = 2 * (int)(config_.periph); - if(config_.pin_config.tx.port != DSY_GPIOX) + if(config_.pin_config.tx.port != PORTX) { //check tx against periph if(checkPinMatch(&GPIO_InitStruct, config_.pin_config.tx, per_num) @@ -802,12 +796,12 @@ UartHandler::Result UartHandler::Impl::InitPins() } //setup tx pin - GPIO_TypeDef* port = dsy_hal_map_get_port(&config_.pin_config.tx); - GPIO_InitStruct.Pin = dsy_hal_map_get_pin(&config_.pin_config.tx); + GPIO_TypeDef* port = GetHALPort(config_.pin_config.tx); + GPIO_InitStruct.Pin = GetHALPin(config_.pin_config.tx); HAL_GPIO_Init(port, &GPIO_InitStruct); } - if(config_.pin_config.rx.port != DSY_GPIOX) + if(config_.pin_config.rx.port != PORTX) { //check rx against periph if(checkPinMatch(&GPIO_InitStruct, config_.pin_config.rx, per_num + 1) @@ -817,8 +811,8 @@ UartHandler::Result UartHandler::Impl::InitPins() } //setup rx pin - GPIO_TypeDef* port = dsy_hal_map_get_port(&config_.pin_config.rx); - GPIO_InitStruct.Pin = dsy_hal_map_get_pin(&config_.pin_config.rx); + GPIO_TypeDef* port = GetHALPort(config_.pin_config.rx); + GPIO_InitStruct.Pin = GetHALPin(config_.pin_config.rx); HAL_GPIO_Init(port, &GPIO_InitStruct); } @@ -827,12 +821,12 @@ UartHandler::Result UartHandler::Impl::InitPins() UartHandler::Result UartHandler::Impl::DeInitPins() { - GPIO_TypeDef* port = dsy_hal_map_get_port(&config_.pin_config.tx); - uint16_t pin = dsy_hal_map_get_pin(&config_.pin_config.tx); + GPIO_TypeDef* port = GetHALPort(config_.pin_config.tx); + uint16_t pin = GetHALPin(config_.pin_config.tx); HAL_GPIO_DeInit(port, pin); - port = dsy_hal_map_get_port(&config_.pin_config.rx); - pin = dsy_hal_map_get_pin(&config_.pin_config.rx); + port = GetHALPort(config_.pin_config.rx); + pin = GetHALPin(config_.pin_config.rx); HAL_GPIO_DeInit(port, pin); return Result::OK; @@ -849,8 +843,8 @@ void* UartHandler::Impl::next_callback_context_; void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) { UartHandler::Impl* handle = MapInstanceToHandle(uartHandle->Instance); - dsy_hal_map_gpio_clk_enable(handle->config_.pin_config.rx.port); - dsy_hal_map_gpio_clk_enable(handle->config_.pin_config.tx.port); + GPIOClockEnable(handle->config_.pin_config.rx); + GPIOClockEnable(handle->config_.pin_config.tx); switch(handle->config_.periph) { diff --git a/src/per/uart.h b/src/per/uart.h index 8a415fdc8..52314f02a 100644 --- a/src/per/uart.h +++ b/src/per/uart.h @@ -8,6 +8,9 @@ TODO #define DSY_UART_H /**< macro */ #include "daisy_core.h" +#if !UNIT_TEST +#include "util/hal_map.h" +#endif namespace daisy { @@ -69,9 +72,9 @@ class UartHandler struct { - dsy_gpio_pin tx; /**< & */ - dsy_gpio_pin rx; /**< & */ - } pin_config; /**< & */ + Pin tx; /**< & */ + Pin rx; /**< & */ + } pin_config; /**< & */ Config() { diff --git a/src/sys/system.cpp b/src/sys/system.cpp index bdb3fc3f0..6f3ca335a 100644 --- a/src/sys/system.cpp +++ b/src/sys/system.cpp @@ -303,14 +303,13 @@ void System::ResetToBootloader(BootloaderMode mode) if(mode == BootloaderMode::STM) { // Initialize Boot Pin - dsy_gpio_pin bootpin = {DSY_GPIOG, 3}; - dsy_gpio pin; - pin.mode = DSY_GPIO_MODE_OUTPUT_PP; - pin.pin = bootpin; - dsy_gpio_init(&pin); + constexpr Pin bootpin = Pin(PORTG, 3); + + GPIO pin; + pin.Init(bootpin, GPIO::Mode::OUTPUT); // Pull Pin HIGH - dsy_gpio_write(&pin, 1); + pin.Write(1); // wait a few ms for cap to charge HAL_Delay(10); diff --git a/src/util/hal_map.c b/src/util/hal_map.c deleted file mode 100644 index bd095a6f1..000000000 --- a/src/util/hal_map.c +++ /dev/null @@ -1,74 +0,0 @@ -#include "daisy_core.h" - -#include "stm32h7xx_hal.h" - -// Maps Daisy interface to STM32 HAL -- -// I'd like to get all of this stuff tucked away somewhere inbetween the HAL, and User level -// So that I can start to slowly replace HAL stuff over time. -// Also I don't like that this throws a warning for every library file that doesn't use it... - -const static GPIO_TypeDef* gpio_hal_port_map[DSY_GPIO_LAST] = { - GPIOA, - GPIOB, - GPIOC, - GPIOD, - GPIOE, - GPIOF, - GPIOG, - GPIOH, - GPIOI, - GPIOJ, - GPIOK, - NULL, -}; - -const static uint16_t gpio_hal_pin_map[16] = { - GPIO_PIN_0, - GPIO_PIN_1, - GPIO_PIN_2, - GPIO_PIN_3, - GPIO_PIN_4, - GPIO_PIN_5, - GPIO_PIN_6, - GPIO_PIN_7, - GPIO_PIN_8, - GPIO_PIN_9, - GPIO_PIN_10, - GPIO_PIN_11, - GPIO_PIN_12, - GPIO_PIN_13, - GPIO_PIN_14, - GPIO_PIN_15, -}; - -// GPIO FUNCTIONS - -GPIO_TypeDef* dsy_hal_map_get_port(const dsy_gpio_pin* p) -{ - return (GPIO_TypeDef*)gpio_hal_port_map[p->port]; -} -uint16_t dsy_hal_map_get_pin(const dsy_gpio_pin* p) -{ - return (uint16_t)gpio_hal_pin_map[p->pin]; -} - -void dsy_hal_map_gpio_clk_enable(dsy_gpio_port port) -{ - switch(port) - { - case DSY_GPIOA: __HAL_RCC_GPIOA_CLK_ENABLE(); return; - case DSY_GPIOB: __HAL_RCC_GPIOB_CLK_ENABLE(); return; - case DSY_GPIOC: __HAL_RCC_GPIOC_CLK_ENABLE(); return; - case DSY_GPIOD: __HAL_RCC_GPIOD_CLK_ENABLE(); return; - case DSY_GPIOE: __HAL_RCC_GPIOE_CLK_ENABLE(); return; - case DSY_GPIOF: __HAL_RCC_GPIOF_CLK_ENABLE(); return; - case DSY_GPIOG: __HAL_RCC_GPIOG_CLK_ENABLE(); return; - case DSY_GPIOH: __HAL_RCC_GPIOH_CLK_ENABLE(); return; - case DSY_GPIOI: __HAL_RCC_GPIOI_CLK_ENABLE(); return; - case DSY_GPIOJ: __HAL_RCC_GPIOJ_CLK_ENABLE(); return; - case DSY_GPIOK: __HAL_RCC_GPIOK_CLK_ENABLE(); return; - default: return; - } -} - -// ADC FUNCTIONS diff --git a/src/util/hal_map.h b/src/util/hal_map.h index acb7d23e4..6c30be397 100644 --- a/src/util/hal_map.h +++ b/src/util/hal_map.h @@ -7,28 +7,51 @@ /** @addtogroup utility @{ */ +#ifdef __cplusplus +namespace daisy +{ +constexpr uint32_t GetHALPin(Pin p) +{ + return (1 << p.pin); +} +constexpr GPIO_TypeDef *GetHALPort(Pin p) +{ + switch(p.port) + { + case PORTA: return GPIOA; + case PORTB: return GPIOB; + case PORTC: return GPIOC; + case PORTD: return GPIOD; + case PORTE: return GPIOE; + case PORTF: return GPIOF; + case PORTG: return GPIOG; + case PORTH: return GPIOH; + case PORTI: return GPIOI; + case PORTJ: return GPIOJ; + case PORTK: return GPIOK; + default: return NULL; + } +} +inline void GPIOClockEnable(Pin p) +{ + switch(p.port) + { + case PORTA: __HAL_RCC_GPIOA_CLK_ENABLE(); return; + case PORTB: __HAL_RCC_GPIOB_CLK_ENABLE(); return; + case PORTC: __HAL_RCC_GPIOC_CLK_ENABLE(); return; + case PORTD: __HAL_RCC_GPIOD_CLK_ENABLE(); return; + case PORTE: __HAL_RCC_GPIOE_CLK_ENABLE(); return; + case PORTF: __HAL_RCC_GPIOF_CLK_ENABLE(); return; + case PORTG: __HAL_RCC_GPIOG_CLK_ENABLE(); return; + case PORTH: __HAL_RCC_GPIOH_CLK_ENABLE(); return; + case PORTI: __HAL_RCC_GPIOI_CLK_ENABLE(); return; + case PORTJ: __HAL_RCC_GPIOJ_CLK_ENABLE(); return; + case PORTK: __HAL_RCC_GPIOK_CLK_ENABLE(); return; + default: return; + } +} +} // namespace daisy - -/** global structs, and helper functions for interfacing with the stm32 HAL library - while it remains a dependancy. - This file should only be included from source files (c/cpp) - Including it from a header within libdaisy would expose the entire HAL to the users. - This should be an option for users, but should not be required. -*/ - -/** \param *p Pin pin to get - \return HAL GPIO_TypeDef as used in the HAL from a dsy_gpio_pin input. -*/ -GPIO_TypeDef *dsy_hal_map_get_port(const dsy_gpio_pin *p); - -/** \param *p Pin pin to get - \return HAL GPIO Pin as used in the HAL from a dsy_gpio_pin input. -*/ -uint16_t dsy_hal_map_get_pin(const dsy_gpio_pin *p); - -/** \param port port clock to enable -*/ -void dsy_hal_map_gpio_clk_enable(dsy_gpio_port port); - +#endif #endif /** @} */ diff --git a/tests/Pin_gtest.cpp b/tests/Pin_gtest.cpp index 7b10d4ee1..75d6972db 100644 --- a/tests/Pin_gtest.cpp +++ b/tests/Pin_gtest.cpp @@ -25,16 +25,3 @@ TEST(PinTests, c_copy) Pin dest = src; EXPECT_TRUE(src == dest); } - -TEST(PinTests, d_conversion) -{ - dsy_gpio_pin old_pin = {DSY_GPIOA, 5}; - - Pin new_pin = Pin(PORTA, 5); - - /** Using operator dsy_gpio_pin for implicit conversion */ - dsy_gpio_pin converted_pin = new_pin; - - EXPECT_EQ(old_pin.port, converted_pin.port); - EXPECT_EQ(old_pin.pin, converted_pin.pin); -}