-1
\$\begingroup\$

I have written my code for STM32F103C8T6 board in Keil using the adequate startup files. I wrote directly to the memory addresses using information from the datasheet but the ST-Link upload seems to upload the hex file to the wrong addresses for example:

GPIOA = 0x4001 0800 

This is according to the STM datasheet. But ST-Link shows the device data range from:

0x0800 0000 to 0x0800 03d4.

My code is a simple program to blink the LED at portA1. I connected the LED across a 10k resistor. Why is STM assigning the wrong addresses when I used pointers to specify the memory locations, or could there be any other mistakes. Code below.

void delay(int a);

int main(void)
{
    unsigned int* GPIO_A;
    GPIO_A = (unsigned int*)0x40010800 ; // Assigning GPIOA to the correct memory location

    unsigned int* GPIO_A_CRL;
    GPIO_A_CRL = GPIO_A + 0x00 ; // Assigning GPIO_A_CRL to the correct memory location

    /*unsigned int* GPIO_A_IDR;
    GPIO_A_IDR = GPIO_A + 0X08 ; // Assigning GPIO_A_IDR to the correct memory location  */

    unsigned int* GPIO_A_BSRR;
    GPIO_A_BSRR = GPIO_A + 0X10 ; // Assigning GPIO_A_BSRR to the correct memory location

    unsigned int* GPIO_A_BRR;
    GPIO_A_BRR = GPIO_A + 0X14 ; // Assigning GPIO_A_BSRR to the correct memory   location

    unsigned int* RCC_APB2ENR;
    RCC_APB2ENR = (unsigned int*)(0x40021000 + 0X18) ; // Assigning                RCC_APB2ENR to the correct memory location

    *RCC_APB2ENR = 0X04; // Set clock for GPIOA

    *GPIO_A_CRL = 0X00008888 ; // Defining pin modes for GPIO_A_CRL

    while(1)  // infinite loop
    {
        *GPIO_A_BSRR = 0X00000002;  // Set bit 1 to 1
        delay(2); // delay
        *GPIO_A_BRR = 0x00000002;  // reset bit 1 to reset value(0)
        delay(2); // delay
    }
}

void delay(int a)
{
    long b = a*1000000;

    for(int i=0;i<b;i++)
    {
        int c = 1;
    }
}
\$\endgroup\$
7
  • 3
    \$\begingroup\$ It could make life a lot easier if you include "stm32f10x.h" which comes with keil. The defines are in there. \$\endgroup\$
    – Tut
    Commented Feb 23, 2017 at 18:51
  • \$\begingroup\$ yeah. But I want to do it the hard way first, could there be anything wrong with the code \$\endgroup\$
    – ched
    Commented Feb 23, 2017 at 19:58
  • 1
    \$\begingroup\$ Code at 0x08000000, GPIO port at 0x40010800. What's the problem with that? \$\endgroup\$ Commented Feb 24, 2017 at 9:21
  • \$\begingroup\$ "but the ST-Link upload seems to upload the hex file to the wrong addresses for example" - No, the program code must be uploaded to FLASH memory. The instructions in the program can then access any memory/IO address they want. It's like asking why your audio player gets installed on the harddisk and not on the soundcard. \$\endgroup\$
    – JimmyB
    Commented Feb 24, 2017 at 11:17
  • \$\begingroup\$ By the way, is your LED blinking as desired? \$\endgroup\$
    – JimmyB
    Commented Feb 24, 2017 at 11:21

2 Answers 2

0
\$\begingroup\$

Check the memory map on the data sheet. Peripherals are at addresses beginning at 0x40000000. 0x08000000 is the beginning of Flash (code) memory.

\$\endgroup\$
7
  • \$\begingroup\$ i have crosschecked and the addresses i selected are right \$\endgroup\$
    – ched
    Commented Feb 23, 2017 at 20:03
  • \$\begingroup\$ So, what exactly is the problem? \$\endgroup\$
    – user28910
    Commented Feb 23, 2017 at 20:45
  • \$\begingroup\$ when trying to debug , i get unable to address target resource, illegal address 0x08000000 . And code doesnt work \$\endgroup\$
    – ched
    Commented Feb 23, 2017 at 20:48
  • \$\begingroup\$ Hmm... 0x08000000 is the location of the vector table; it's not an illegal address. Are you able to connect the debugger and load the program? Can you view memory? I'm not familiar with Keil tools. \$\endgroup\$
    – user28910
    Commented Feb 23, 2017 at 21:19
  • \$\begingroup\$ yes, but the debugger keeps returning unable to access address \$\endgroup\$
    – ched
    Commented Feb 23, 2017 at 21:41
-1
\$\begingroup\$

You could have had the wrong address for gpioa.

\$\endgroup\$
4
  • \$\begingroup\$ I have crosschecked again the addresses are correct . \$\endgroup\$
    – ched
    Commented Feb 23, 2017 at 19:57
  • \$\begingroup\$ Well then a few more possibilities: 1. Thee chip doesn't follow the datasheet. 2. The compiler is faulty. 3. The debuger and software is faulty. 4. Operator error. You can estimate their likely hood and rule them out one at a time. \$\endgroup\$
    – dannyf
    Commented Feb 23, 2017 at 20:06
  • \$\begingroup\$ when tryimng to debug , i get unable to address target resource, illegal address 0x08000000 \$\endgroup\$
    – ched
    Commented Feb 23, 2017 at 20:25
  • \$\begingroup\$ It confirms the issue is operator oriented. Take a loop at the data sheet and check how memory space is organized \$\endgroup\$
    – dannyf
    Commented Feb 23, 2017 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.