cancel
Showing results for 
Search instead for 
Did you mean: 

Strange behavior on open-drain output mode for blue pill (STM32F103C8T6)

Luki_Grin
Associate II

Guys, I need to put 5V on a pin. For this, I configured my pin as an open-drain output with a pull-up resistor (10k) between the pin and the 5V line. I checked the 5V-tolerant pins in the Blue Pill datasheet and tested many of them, but the conclusion is always the same: the output is 3.7V when the pin is in high impedance. I used Keil uVision to write my code in C (at startup, I only used CMSIS and DEVICE for pre-configuration). My code is below. I used PB10 as a reference.

#include "stm32f10x.h"

int main(){
  RCC->APB2ENR |= (1 << 3); // Clock for GPIOB

  // Open-drain output and HIGH impedance PB10
  GPIOB->CRH &= ~(0b1111 << 8); // Cleaning bits
  GPIOB->CRH |= (0b0111 << 8); // Setting up bits

  GPIOB->ODR |= (1 << 10); // HIGH impedance
};
 
Could you guys please tell me what I did wrong? 

Edited to apply source code formatting - please see How to insert source code for future reference.

7 REPLIES 7
TDK
Super User

Maybe the overwhelmingly likely counterfeit chip on the board doesn't have 5 V tolerant pins.

If you feel a post has answered your question, please click "Accept as Solution".
Peter BENSCH
ST Employee

You have (at least) made two mistakes:

  • You are using a so-called Blue Pill that has only been used with counterfeits for years (and you are asking for a solution for the counterfeit in the forum of the original manufacturer).

  • You are confusing 5V tolerance (of an input) with 5V capability of an output

Regards
/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Thanks for your comment! I didn't think it was a clone, as the debugger with STM32CubeIDE worked perfectly fine. However, after checking some IDs, I found very strange values, so it's probably fake =(

Thanks for the clarification, Peter. I'm new to the world of microcontrollers, and I'm diving into bare metal to understand how things really work. Didn't know there were clones. I will probably move to a NUCLEO one in the future (to avoid spending many hours on these bugs). 

"You are confusing 5V tolerance (of an input) with 5V capability of an output" --> Could you explain what you mean? I know the output voltage on a pin is normally 3.3V, but if I configure the output as open-drain (high impedance) and connect a pull-up resistor to it, I can get higher values of voltage, right? 

This depends on:

  • whether the specific GPIO has an alternative analogue function
  • and, as mentioned, whether it is an original or a counterfeit.

Details on GPIO behaviour can be found for the original device in the data sheet and in the Reference Manual RM0008.

Hope that helps?

Regard
/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

I know the output voltage on a pin is normally 3.3V, but if I configure the output as open-drain (high impedance) and connect a pull-up resistor to it, I can get higher values of voltage, right? 

The pins will have clamping diodes. Even if the pin is an input or output, It'll protect the pin. In this case, the diodes are preventing the resistor from pulling the pin up to 5V.

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.
CAN Jammer an open source CAN bus hacking tool
CANableV3 Open Source

Not exactly true. In STM32, 5V-tolerant pins have clamping diodes with the cathodes connected to a circuit that provides clamping to 6V or little above. So, for normal 5V-tolerant pins it's perfectly correct to use external pull-up resistor to 5V and with such a resistor the output voltage will be close to 5V.

Chinese clones may behave differently.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice