STM32 F4 max GPIO toggle frequency
In your second code, you have the line GPIO_SetBits(GPIOD,GPIO_Pin_12);, do you actually reset the pin at some point ?

It is faster to set the pin through the register rather than using the library function, as there is plenty of overhead.

Check this tutorial about GPIO.


  To Set PC8
  
  GPIOC->ODR |=  0x00000100;  //( 0b00000000000000000000000100000000)
  
  To clear pin PC8 independent of all other pins on GPIOC (RMW) you could use:
  
  GPIOC->ODR &amp;=  ~(0x00000100);  //( 0b00000000000000000000000100000000)


So you can try

while (1)
{
     GPIOC-&gt;ODR |=  0x00000100; 
     // maybe some delay needed here
     GPIOC-&gt;ODR &amp;=  ~(0x00000100);
}


Of course adjusting to the proper pin / register you need.

Also the pin toggle timing is limited by the hardware.

You might toggle the pin so fast in software side, that the hardware might not have time to follow and thus stay stuck in one state.

It also depends what is connected to that pin, long wire, capacitance, impedance and inductance will slow down the toggle timing as it will be limited by the current the IC is able to draw from that pin, and the internal hardware switching latency.

Hardware latency are defined on the datasheet on page 95, for this example it cannot exceed 50-100Mhz depending the conditions.