I'm going to start by saying I couldn't find a good data sheet for that Sensor.
The best information I found is Arduino Ultrasonic Ranging
AFAICT, the Sensor needs a 5V signal on its 'Trig' trigger input. So the bad news is the electronics will need to be a tiny bit more complex.
The STM32F4 can not drive a 5V signal directly. The highest voltage its pins can drive is a bit (0.4V) less than its power supply, which is 3.3V.
But, the STM32F4 has plenty of pins which are called '5 Volt Tolerant (FT)'. These pins can't drive 5V directly, but they can be connected to something at 5V without damage (1). You will need to look at ST Micro's STM32F407 datasheet to choose 5V tolerant pins.
So FT pins can connect a signal to ground, i.e. pull the signal low, but not drive it high. This is where a good datasheet would be very helpful. The sensors trigger input wants to be at 5V, but it doesn't say if it needs much current. I will assume it doesn't need much. So I will assume a resistor could be connected between the trigger pin and 5V. That resistor would pull-up the trigger pin, and cause the sensor to trigger. I would guess a 1Kohm resistor would be fine.
So connect a 5V Tolerant output of the STM32F4 to the trigger pin/resistor. The STM32F4 will pull the pin low to disable the sensor, and let it get pulled high by the resistor to enable the sensor (by setting the pin high).
Your software must configure the STM32F4 output pin to be 'Open Drain'. Normally, the software will set the pin low, which will keep the trigger off. Then when the programs wants to trigger the sensor, set the pin high for more than 10useconds, and the sensor should start broadcasting. (As long as the pin has been put into Open Drain output mode, it should work). You can check the trigger signal is working by (bfore connecting the sensor) with a voltmeter.
The other part of the system is connecting the Echo output from the sensor to an input on the STM32F4. That also looks like a 5V signal. I think that a 5V Tolerant pin on the STM32F4 will be okay upto 5.5V. To start with, I'd take make a voltage divider with a couple of resistors in series. Connect the sensors Echo pin to one end of the resistor pair, and the other end to ground. Connect the 'middle' to the STM32F4 input pin. It should work with a couple of 1K resistors, but three in series with two between the STM32F4 input and ground would be even better.
So, for the cost of a few resistors. You should be okay.
Further Suggestions:
If you have access to an oscilloscope, you should be able to see the Echo signal, and check on its voltage.
If the Trigger signal is not enough current to drive the sensor, then it'll need a bipolar transistor or MOSFET to pull the signal high. Cost less than $1, so not a big problem). I would be surprised if this were a problem, but it might be.
1) Note: connecting an STM32F4 pin, which is not 5v Tolerant to 5V may irreparably damage that pin, and maybe worse)
We probably need more information to fully help. However, ...
The 'reset value' of 0x44444444 is the value the registers are set to when the hardware is reset. It is not necessarily the port configuration value which you need.
See RM008 Reference manual ... STM32F103xx ... advanced ARM-based 32-bit MCUs Section 9.2
The code is setting all of the port pins to:
"CNFy: 01: Floating input (reset state)"
"MODEy: 00: Input mode (reset state)"
So all pins except the bottom three are set for input, with no specific pull-up or pull-down. Hence a pin connected to a signal, or holding a random charge could be at either 0 or 1.
It is normal to mask off the data from unused pins of a port so that only the values needed are used. This helps if, for example, your hardware is changed and new pins become active for some other purpose.
I mask the IDR, so that only the value for pins that are active inputs are used, before using the port-input-data-register value.
You might consider setting the pull-down resistor on all other pins to make it easier to see what is happening to the active ins. However, that has some risk; for example if those input pins get connected to a high-signal or Vcc.
If you look at the source code for the standard peripheral library, it will show a sequence of registers accesses which do correctly initialise it.
That might provide enough insight for you to continue your learning.
Possibly easier to read is the Leaflabs libmaple source for the Maple STM32F103 development board.
Or dig through stm32duino.com, who are working on their update to libmample.
Or look at a port of libmaple by an LeafLabs ex-staffer at rambutan.cc
EDIT:
when you set the CRL bits to '8=1000(input pull-down)' did you also set the ODR register to 0? According to table 20 in section 9.1, the ODR determines whether the resitor is a pull-down or a pull-up.
EDIT2:
Well done! A good piece of detective work.
The JTAG pins (PA13, PA14, PA15, PB3, PB4) can be freed up for normal GPIO use by setting a value in AFIO_MAPR, but I would tend to leave those pins as JTAG/SWJ-DP.
AFAIK PA11 and PA12 are ordinary pins, so if CAN, USB, TIM1 and USART1 are not used, they are available for normal GPIO.
Best Answer
Main()
is not where the chip boots.Find the reset interrupt handler. In there you will find it calls
SystemInit
, which does some basic setup and clocks for you.