2017-08-16 04:53 AM
Hi,
I'm not able to activate the led (green, LD2, PA5) in stepping mode. But the corrosponding register shows that it is activated.
&sharpinclude 'stm32f4xx_rcc.h'
&sharpinclude 'stm32f4xx_gpio.h'int main(){ RCC->AHB1ENR |= RCC_AHB1Periph_GPIOA; GPIOA->MODER |= GPIO_Mode_OUT; GPIOA->OSPEEDR |= GPIO_Speed_25MHz; GPIOA->OTYPER |= GPIO_OType_PP; GPIOA->PUPDR |= GPIO_PuPd_NOPULL; while (1) { GPIOA->BSRRL = GPIO_Pin_5; GPIOA->BSRRH = GPIO_Pin_5; }}What is wrong?
Thank you
#stm32f4 #stm32 #register #nucleoSolved! Go to Solution.
2017-08-16 12:03 PM
>>Why is it with RST not working? Do I need a Time Delay? I wanted to RST to make sure everything is 0.
Because you're holding the device in RESET by writing ONE, you must write ONE, then write ZERO into the bit field
You likely don't want to nuke the registers, for one thing it might result in the loss of debugger connectivity on GPIOA, but more specifically you might be undoing work done by SystemInit() etc in setting up external buses and pin setting that occur prior to execution of main() under the CMSIS model.
2017-08-16 05:37 AM
What is wrong?
Nearly everything
You are mixing CMSIS style programming with SPL style. If you want to operate on registers instead of using a library like SPL (or HAL, or LL) then you should only use the definitions provided by stm32f405xx.h (for example) CMSIS header file.
1. RCC->AHB1ENR |= RCC_AHB1Periph_GPIOA; becomes RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
2. GPIOA->MODER |= GPIO_Mode_OUT; becomes, for example, GPIOA->MODER |= GPIO_MODER_MODE5_0;
3. Other registers also have to be changed accordingly
Read carefully the reference manual of your MCU, and find the correct definitions in the CMSIS header file of your MCU.
2017-08-16 06:01 AM
If you don't understand register level programming consider using the library code until you do.
2017-08-16 07:05 AM
Ok,
sorry for the code above, it is not mine (copied from the internet).
Mine looks like this and doesn't work as well. And I can't understand why... the green LED LD2 is working on the board.
#include 'stm32f4xx.h'
void SystemClock_Config(void); //function prototype for SystemClock configurationvoid GPIO_Config(void); //function frototype for GeneralPurposeInputOutput configurationint main(void){ //initialization SystemClock_Config(); GPIO_Config(); int n; while(1) { GPIOA->ODR |= 0x20; //for(n=0; n<5; n++); GPIOA->ODR &= ~0x20; //for(n=0; n<5; n++); }}//SystemClock configurationvoid SystemClock_Config(void){ RCC->CR |= RCC_CR_CSSON | RCC_CR_HSEBYP | RCC_CR_HSEON; //clock security system enabled, HSE Enable Bypass and HSE Enable //HSE Clock while (!(RCC->CR & RCC_CR_HSERDY)); //wait till HSE oscillator is stable //FLASH Configuration FLASH->ACR |= FLASH_ACR_PRFTEN | FLASH_ACR_ICEN | FLASH_ACR_DCEN | FLASH_ACR_LATENCY_3WS; //FLASH Prefetch, Instruction, Data Cache enable; //FLASH wait states 3 !change to higher value, if problems occur //PLL configuration and activation RCC->PLLCFGR |= RCC_PLLCFGR_PLLSRC_HSE | RCC_PLLCFGR_PLLM_4 | 0x00000078 | RCC_PLLCFGR_PLLP_0; RCC->CR |= RCC_CR_PLLON; //activate PLL while(!(RCC->CR & RCC_CR_PLLRDY)); //wait till PLL is stable //Sysclk activation RCC->CFGR |= RCC_CFGR_HPRE_DIV1; RCC->CFGR |= RCC_CFGR_SW_PLL; while (!(RCC->CFGR & RCC_CFGR_SWS_PLL)); //PPRE1 and PPRE2 Prescaler RCC->CFGR |= RCC_CFGR_PPRE1_DIV4; //HCLK divided by 4 RCC->CFGR |= RCC_CFGR_PPRE2_DIV2; //HCLK divided by 2}//GeneralPurposeInputOutput configurationvoid GPIO_Config(void){ RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; //enable AHB1 GPIO->A and GPIO->C clock RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN; RCC->AHB1RSTR |= RCC_AHB1RSTR_GPIOARST | RCC_AHB1RSTR_GPIOCRST; GPIOA->MODER |= GPIO_MODER_MODER5_0;}Whats wrong with mine?
2017-08-16 08:18 AM
You should also mind that you are blinking the LED at the maximum speed of the MCU (several MHz) so you won't see anything blink.
You should use something like :
int i;
while(1)
{
GPIOA->ODR ^= (1 << 5); //Toogle the 5th pin
for(i = 0; i < 500000; i++)
;
}
And increase/decrease the maximum value for 'i' to see it blink
2017-08-16 08:31 AM
Hi Clive,
now it is working. The problem is as you mentioned:
RCC->AHB1RSTR |= RCC_AHB1RSTR_GPIOARST | RCC_AHB1RSTR_GPIOCRST;
Everything stayed as I posted it.
Why is it with RST not working? Do I need a Time Delay?
I wanted to RST to make sure everything is 0.
Yes I know that I turn on and of at max speed when it is executed in the loop. But I did it step-by-step, so it should have light up.
Thanks
2017-08-16 09:46 AM
Wouldn't do this
RCC->AHB1RSTR |= RCC_AHB1RSTR_GPIOARST | RCC_AHB1RSTR_GPIOCRST;
Use a debugger. Step through the code, review the peripheral level setting/registers. Make sure you are setting/clearing the correct bits in the registers for the specific pin being used.
Using what board?
GPIOA->MODER = (GPIOA->MODER & ~(3 << (5 << 1))) | (1 << (5 << 1)); // Pin 5 Output
GPIOA->TYPER &= ~(1 << 5); // Pin 5 Push-Pull
2017-08-16 11:56 AM
And make it volatile...
The code is being stepped in a debugger, I don't think this is the problem. Operating at 0.0001 Hz
2017-08-16 12:00 PM
Thank you clive for the update!
2017-08-16 12:03 PM
>>Why is it with RST not working? Do I need a Time Delay? I wanted to RST to make sure everything is 0.
Because you're holding the device in RESET by writing ONE, you must write ONE, then write ZERO into the bit field
You likely don't want to nuke the registers, for one thing it might result in the loss of debugger connectivity on GPIOA, but more specifically you might be undoing work done by SystemInit() etc in setting up external buses and pin setting that occur prior to execution of main() under the CMSIS model.