2014-02-08 12:59 PM
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.
2014-02-08 01:23 PM
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.2014-02-08 01:40 PM
volatile unsigned int *peripheral;
peripheral=(unsigned int*)(&GPIOE->MODER); // 0x48001000, cross checked *peripheral=GPIO_MODER_MODER9_0;2014-02-08 01:42 PM
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;2014-02-08 02:20 PM
2014-02-08 02:20 PM
2014-02-08 02:58 PM
Daniel is probably referring to a different series STM32 with a different mix of APB and AHB, and addresses/placement of GPIO peripherals.