Unable to turn on pins of stm32F446RE

Status
Not open for further replies.

Prototype21

Junior Member level 1
Joined
Mar 3, 2020
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
121
Hello Guys,
I am new to arm development. I have written the below code for my STM32F446RE Nucleo board. All I am trying to do is turn on Pin PA3, Pin PA2 and onboard LED Pin PA5. The code compiled and flashed successfully. But no success in turning on those pins even after several retries. Am I missing any register?? Need some help guys.

Code:
// Enable clock access to the port.
// Set pin mode
// Set pin as o/p

#include "stm32f4xx.h"                                    // Device header

#define RED                       (1U << 3)              // PIN PA3
#define VIOLET                    (1U << 2)              // PIN PA2 
#define GREEN                     (1U << 2)              // PIN PA5 Onboard LED 

#define GPIOA_CLOCK               (1U << 0)

#define RED_OUT_BIT               (1U << 6)              // General purpose output mode, no alternate function, no analog, no input mode
#define VIOLET_OUT_BIT            (1U << 4)              // General purpose output mode, no alternate function, no analog, no input mode
#define GREEN_OUT_BIT             (1U << 10)             // General purpose output mode, no alternate function, no analog, no input mode

int main() {
    RCC -> AHB1ENR |= GPIOA_CLOCK;
    GPIOA -> MODER |= RED_OUT_BIT | VIOLET_OUT_BIT | GREEN_OUT_BIT;
    
    while(1) {
        GPIOA -> ODR |= RED | VIOLET | GREEN;
    }
}
 

The clock might be running at a very low speed and it is very clear from the code snippet that you haven't set up the processor clock. Initialize the processor clock first and then enable the clock for the peripherals.

C:
void SysClockConfig (void)
{
        /*************>>>>>>> STEPS FOLLOWED <<<<<<<<************
    
    1. ENABLE HSE and wait for the HSE to become Ready
    2. Set the POWER ENABLE CLOCK and VOLTAGE REGULATOR
    3. Configure the FLASH PREFETCH and the LATENCY Related Settings
    4. Configure the PRESCALARS HCLK, PCLK1, PCLK2
    5. Configure the MAIN PLL
    6. Enable the PLL and wait for it to become ready
    7. Select the Clock Source and wait for it to be set
    
    ********************************************************/
    
    
    #define PLL_M     4
    #define PLL_N     180
    #define PLL_P     0  // PLLP = 2

    // 1. ENABLE HSE and wait for the HSE to become Ready
    RCC->CR |= RCC_CR_HSEON; 
    while (!(RCC->CR & RCC_CR_HSERDY));
    
    // 2. Set the POWER ENABLE CLOCK and VOLTAGE REGULATOR
    RCC->APB1ENR |= RCC_APB1ENR_PWREN;
    PWR->CR |= PWR_CR_VOS;
    
    
    // 3. Configure the FLASH PREFETCH and the LATENCY Related Settings
    FLASH->ACR = FLASH_ACR_ICEN | FLASH_ACR_DCEN | FLASH_ACR_PRFTEN | FLASH_ACR_LATENCY_5WS;
    
    // 4. Configure the PRESCALARS HCLK, PCLK1, PCLK2
    // AHB PR
    RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
    
    // APB1 PR
    RCC->CFGR |= RCC_CFGR_PPRE1_DIV4;
    
    // APB2 PR
    RCC->CFGR |= RCC_CFGR_PPRE2_DIV2;
    
    
    // 5. Configure the MAIN PLL
    RCC->PLLCFGR = (PLL_M <<0) | (PLL_N << 6) | (PLL_P <<16) | (RCC_PLLCFGR_PLLSRC_HSE);

    // 6. Enable the PLL and wait for it to become ready
    RCC->CR |= RCC_CR_PLLON;
    while (!(RCC->CR & RCC_CR_PLLRDY));
    
    // 7. Select the Clock Source and wait for it to be set
    RCC->CFGR |= RCC_CFGR_SW_PLL;
    while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL);
}
 
Hi, Thanks for the replay


I am using Nucleo F446RE Board. There is no external oscillator on the board. I am trying to clock my Nucleo board with internal RC Oscillator and by default, it is clocked at 16MHz. The stm32 chips by default should work at 16Mhz without any clock configuration...right??
 

Hi,

Without setting up clock you may be able to process the code,
but maybe if the I/O section clock is not properly set up it it can't change I/O states.
The datasheet should give informations about this.

Klaus
 
but maybe if the I/O section clock is not properly set up it it can't change I/O states.
Klaus

I have already enabled the clock for GPIOA in my code (See my code above). There was a mistake in my code, I corrected it now onboard LED PIN PA5 turn on but other two pins PA2 and PA3 are off.

My new code:

C:
#include "stm32f4xx.h"                                   // Device header

#define RED                       (1U << 3)              // PIN PA3
#define VIOLET                    (1U << 2)              // PIN PA2
#define GREEN                     (1U << 5)              // PIN PA5 Onboard LED <---working

#define GPIOA_CLOCK               (1U << 0)

#define RED_OUT_BIT               (1U << 6)              // General purpose output mode, no alternate function, no analog, no input mode
#define VIOLET_OUT_BIT            (1U << 4)              // General purpose output mode, no alternate function, no analog, no input mode
#define GREEN_OUT_BIT             (1U << 10)             // General purpose output mode, no alternate function, no analog, no input mode

int main() {
    RCC -> AHB1ENR |= GPIOA_CLOCK;
    GPIOA -> MODER |= RED_OUT_BIT | VIOLET_OUT_BIT | GREEN_OUT_BIT;
    
    while(1) {
        GPIOA -> ODR |= RED | VIOLET | GREEN;
    }
}





All the three pins change its sate in debug mode, but only the onboard LED Pin PA5 turns on. Pin PA3 and Pin PA2 are off.
 

Can we see your stm32f4xx.h file?
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…