cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F3Discovery and Keil Uvision, how can I write to a register?

jeremy
Associate II
Posted on February 08, 2014 at 21:59

I am trying to understand the stm32 at a low level, and I think I understand how most of the registers I need to modify work, but I am looking for some simple C code to write data to a register. Right now I am trying to do something like this:

volatile unsigned int *peripheral;

 

peripheral=(unsigned int*)GPIO_MODER_MODER9_0; //address I want to write to

 

*peripheral=GPIO_E_start //data I want to put into the register (where I have defined GPIO_e_start as 0x48001000.  

When I run this the code goes to the hard fault catcher, and I'm not sure what I'm doing wrong. Ideally I just want a small simple piece of code that allows me to write data to a register, but I have been stuck for a while now without success.

6 REPLIES 6
Posted on February 08, 2014 at 22:23

Your logic is entirely backward, GPIO_MODER_MODER9_0 is a value not an address

GPIOE->MODER = GPIO_MODER_MODER9_0;

would be what you're looking for.

Want to know the address, look to print out &GPIOE->MODER, your address, at first glance also seems to be wrong, as if it where a bit-banded address or something.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on February 08, 2014 at 22:40

volatile unsigned int *peripheral;

peripheral=(unsigned int*)(&GPIOE->MODER); // 0x48001000, cross checked

*peripheral=GPIO_MODER_MODER9_0;
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
dmirsky
Associate II
Posted on February 08, 2014 at 22:42

Also, make sure the clock for the peripheral is on and turn off optimizations.

This ran fine for me.

    volatile unsigned int *periph = (unsigned int*) 0x40020400;  //GPIOB_MODER

    RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;  // Turn on clock.

    *periph = 0x0f0f0f0f;

jeremy
Associate II
Posted on February 08, 2014 at 23:20

Thanks a lot! I wonder, did you intend to write AHB1? As far as I can tell from looking at the clock diagrams there's only 1 AHB, and I can't find any ahb1 references in my headers.

jeremy
Associate II
Posted on February 08, 2014 at 23:20

Thanks a ton!

Posted on February 08, 2014 at 23:58

Daniel is probably referring to a different series STM32 with a different mix of APB and AHB, and addresses/placement of GPIO peripherals.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..