I am trying to control a mosfet that requires an open drain to switch off the power to peripherals. I am using pin A1 to control the circuit.
Turning it on works using pinMode(pin, OUTPUT)
and digitalWrite(pin, LOW)
.
However, using digitalWrite(pin, HIGH)
results in the switched power initially going off then being switched on again. I understand from the electronics guys that I need to set the A1 pin into open drain mode and the way to do this (from another thread) is to call a custom pinMode()
routine.
void Pin_Mode_Open_Drain(pin_t pin)
{
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;
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);
}
This is not working - any clues as to why? Thanks