Electron Open-Drain Output GPIO?

I know the F205 can be configured for open-drain output IO. But I can’t seem to find the docs on how to configure that output state. Any one got any hints?

I’ve done it by using pinMode(PIN, INPUT) for the open/high-impedance state and only driving the output low using pinMode(PIN, OUTPUT) and digitalWrite(PIN, LOW). That was on the Core and Photon, but it should be the same on the Electron.

The pin modes are defined in HAL_Pin_Mode in the system firmware.

If you want a custom mode, you can copy-paste this function into your code, delete what you don’t need and customize the rest.

This may work for what you need on the Photon and Electron (but I haven’t tested it).

#include "gpio_hal.h"
#include "pinmap_impl.h"
#include "stm32f2xx.h"
#include <stddef.h>
#include "hw_ticks.h"

void Pin_Mode_Open_Drain(pin_t pin, PinMode setMode)
{
    STM32_Pin_Info* PIN_MAP = HAL_Pin_Map();

    GPIO_TypeDef *gpio_port = PIN_MAP[pin].gpio_peripheral;
    pin_t gpio_pin = PIN_MAP[pin].gpio_pin;

    // Initialize GPIO_InitStructure to fix system wake up from pin function.
    GPIO_InitTypeDef GPIO_InitStructure = {0};

    if (gpio_port == GPIOA)
    {
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    }
    else if (gpio_port == GPIOB)
    {
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    }
    else if (gpio_port == GPIOC)
    {
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
    }
    else if (gpio_port == GPIOD)
    {
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
    }

    GPIO_InitStructure.GPIO_Pin = gpio_pin;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    PIN_MAP[pin].pin_mode = OUTPUT;

    GPIO_Init(gpio_port, &GPIO_InitStructure);
}
2 Likes

jvanier,

I was trying to figure out how I would shim something like that into the HAL, but the function that you have proposed above would be called directly and instead of a " pinMode(SR_OUTPUT_PIN, OUTPUT); " call right, just to make sure I am getting what you are saying… ?

Les

That’s right. Call the custom open drain pin mode function instead of the default pinMode function.