Understanding the GPIO in STM32F0

Under progress...... 
HAL_TIM_PWM_Stop leaves output pin in undefined state
I'm trying to start/stop a PWM signal to an LED (simple test!!). Using HAL_TIM_PWM_Start() starts the PWM output, HAL_TIM_PWM_Stop() stops the PWM but leaves the output in a random state rather than the (expected) inactive state. This means my LED stays on constantly sometimes without pulsing.
 
Should I expect HAL_TIM_PWM_Stop() to do this or should I be turning a PWM output on/off a different way?
Hi frackers, 
 
 
As mentioned in the timer driver, the function just stop the generation of PWM from the Timer peripheral module and does not change the configuration of the GPIO, in order in case of second recall of PWM start function the generation will be in return and the PWM is on output directly. You can add a GPIO write function then to force the IO state.
 
 
-Hannibal-
I can't just add a GPIO write without changing the Alternate Function of the port from PWM output to PushPull or Open Drain output - to start PWM again then requires re-initialise of the timer with HAL_TIM_PWM_Init() and HAL_TIM_PWM_ConfigChannel().
 
If the Pulse register is set to either 0 or 0xffff does this set the output to a guaranteed steady state and can this be done through the HAL some way other than using  HAL_TIM_PWM_ConfigChannel() ?
Hi frackers, , 
 
 
You can use the ''HAL_TIM_PWM_DeInit()'' function that disable Timer and call the HAL_TIM_PWM_MspDeInit() that you should create and define in the ''STM32Fxx_hal_msp.c'' file. Inside this function, you configure the GPIO as analog for example or other configuration you want. 
 
In case you want to back again just call HAL_TIM_PWM_Init() and your GPIO is reconfigured for PWM output.
 
 
-Hannibal-
Hi frackers,
Use the internal pull-up or down.
It canbe set through the CubeMX TIMx Configuration.

use HAL_TIM_MspPostInit placed inside the MX_TIM1_Init function to pullUp or PullDown the pins

GPIO modes
when a button is connected to the uC pin, and when the button with one end connected to GND and other with uC, is not pressed the uC pin is in floating state. In order to avoid this float problem we use a Pull Up resistor to pull the uC pin to the High level provding one end of the resistor is connected to 5V and the other with the uC pin. We cannot choose the PullUp resistor value randomly because there is a rule for it, will be discussed later. Recommended values for the PullUp resistor are between 1K to 100K ohms.

All the GPIO pins are by default configured as input mode with floating or high impedance state.
input modes : floating and high impedance states.
output modes: default is pushpull, another possible configuration is open drain.

Dealing with Output Buffer:

 

Let us see how to turn on an LED in push pull configuration mode. 

PUSH_PULL configuration


when we write one to the input of inverter it turns the pmos ON and turns the nmos OFF, results in LED glow. in this case the current flow from the VCC to GND through the resistor and Pmos and LED.

in the case 0 is written to the input of inverter the NMOS and PMOS get ON and OFF respectively and hence the LED will be turned off because the point with text drain will be pulled  to the GND.


OPEN DRAIN configuration
In open drain case only one NMOS transistor will be active and can be treated like PMOS transistor is not present. when 0 is applied to the input of inverter the NMOS will be turned on and the point with text drain will be pulled to the GND. but in the case when 1 is applied to the input of inverter the NMOS will be turned OFF and the point with the text drain will become floating meaning it is neither connected to the GND nor with the VCC. hence we can say that in open drain configuration we can acheive either GND or floating drain state, which is not at all usefull in any application.


OPEN_DRAIN with PULLUP/PULLDOWN resistors
In order to use this configuration we use either pull up resistor or pull down resistor . lets take an example of LED using a pull up resistor. when we apply 1-logic at the input of inverter then the NMOS gets OFF and the current in the LED flow from VCC through Internal pull up resistor(built-in inside the uC and can activated/deactivated by means of GPIO registers), results in LED glow.

in the case when we apply 0-logic at the input of the inverter then the NMOS transistor gets ON and the LED will turn off. This is how we can make use of Open drain configuration in conjunction with the pull up or pull down resistors.

Dealing with Input Buffer:

As you have seen, the GPIO is consist of Ouput buffer and input buffer with an enable register bit to select out of these two functionality.
Here, i will be talking about the input buffer of GPIO port meaning that when the Enable Line is high output buffer will be turned off while the input buffer will be activated.


Input Buffer: Input buffer is consist of two CMOS transistor named as NMOS and PMOS. When an external input is applied to it, PMOS and NMOS turns on/off depending upon whether the applied input signal is high or low.

High at applied external input signal, turns the PMOS on and NMOS off, while the Low at the same turns the NMOS on and PMOS off.



by default all the GPIO pins are configured as input, when there is no external input signal is applied then the pin is said to in floating state/Hi-Z state/ High Impedance state. These states may result in inapprperiate behavior in GPIS pin.
 

in case there is no input signal connected at the pin configured in input mode, there is a noise signal preset at the pins with amplitude Variation upto 40% of the VCC. Both the transistor in input buffer gets on because of the noise signal preset at the input, results in leakage current. This leakage current must be avoided to ensure the low consumption of input power especially for the case when the device is battery powered.


In order to avoid this high impedance state problem that causes the leakage current, one must connect the unused pins, configured as input, to the GND or VCC.


In case do you think this post needs a modification, please do let me in comment below.........



Comments