This project uses STM32CubeIDE and it's a program created to practice my C habilities during the course 'Microcontroller Embedded C Programming: Absolute Beginners' from FastBit Embedded Brain Academy.
Hardware = NUCLEO-F401RE (differs from the used in course)
First, let's find an avaible LED to control:
Just checked the board and there is a LD2, and jumpers SB21 and SB42 are connected (by a 0R 0805 resistor). This means, that to control this LED, PA5 must be used. This means, GPIO A (port A) and pin 5.
For the firmware, we have 3 important steps: Configure to clock to control GPIOA, configure Mode for pin 5 to work as output, and finally control the signal of the output (0 or 1).
For the clock, we need to find the clock path for the GPIOA. This information is available in the datasheet of STM32F401x. See:
Now, we need to find the register address and the bits that we need to set. For that, I used the RM0368 Reference Manual (STM32F401x). Check that the section that displays the available configurations for the RCC AHB1 register doesn't show the address:
To find the register address, we must sum the 'Address offset' of 0x30 to the initial register boundary addresses, given in the Memory map section of the RM0368 Reference Manual (STM32F401x). Check the ones that we will use:
So, we are using as register address: 0x4002 3800 + 0x30 = 0x4002 3830. Basically, the lines in code that accomplish this mission are:
Now, we can go to the second step of programming the firmware. Setting the pin 5 as output in the mode register.
Let's just clear the bits from MODER5 (10 and 11) of the register GPIOA_MODER. To find the register address, I summed the Address offset to the GPIOA initial boundary address. Being 0x4000 0000 + 0x00 (offset) = 0x4000 0000. Then, I set only the bit 10 of the register, leaving the MODER5 as 01: General purpose output mode. The lines in code that accomplishes this function are:
And finally the third step, setting the pin 5 of port A. The register address that control the pin's output is found by summing the Address offset of 0x14 (from GPIOx_ODR) to the inital boundary value of the memory map for GPIOA. In that case we have 0x40020000 + 0x14 = 0x40020014.
The code lines for this are:
As an option, the following code was written but using the bitwise shift operator.
Final code:
To visualize the register addresses, we need to go in Window -> Show View -> SFRs.
We can reset application to garante that it is in the begin (I), then we can control and follow the instructions by going step by step (II). Just after it passes by the 21 line (and stops at line 26), it is possible to see that the the register RCC - AHB1ENR - GPIOAEN was modified.
Two more steps, passing througt lines 26 and 29 (stopped at line 33), we have cleared and set the register MODER - MODER5.
And finally, the line that is responsible for lighting the LD2. Line 33: