Skip to main content
g. lewis
Associate III
February 9, 2017
Question

sw4stm32 basic function and printf not working

  • February 9, 2017
  • 8 replies
  • 4460 views
Posted on February 09, 2017 at 12:12

Hi, I'm beginner and I started by a blink project using SW4STM32 along with STMCubeMx libraries set up for my board which is the STM411RE board.

It  works well using the following lines in the main while loop:

      HAL_GPIO_TogglePin(LD2_GPIO_Port,LD2_Pin);

      for(int i=0;i<14200000;i++);

but It doen't work when I tried to replace the for statment: for(int i=0;i<14200000;i++); by a function declared above the main{}

like:

void tempo(void){

    for(int i=0;i<14200000;i++);

}

This is really simple but I'm stuck here.

I got another problem with the printf statemenent that doesn't do anything. Don't know if this is correlated to it but it seems that there is something wrong on the configuration of Eclipse or missing I guess.

ps: I have include stdio.h on top of the program.

Thanks for your help

#sw4stm32-eclipse
This topic has been closed for replies.

8 replies

parsec
Associate III
February 9, 2017
Posted on February 09, 2017 at 12:48

Can you perhaps post your entire code that doesn't work?

Not sure about your printf problem as I am using a different dev environment. FWIW for printf to work there one has to make sure the correct library is linked, otherwise the underlying functions are just stubs and any calls to printf will result in a big fat nothing. Either that or the redirection of the printf output is wrong.

T J
Senior III
February 9, 2017
Posted on February 09, 2017 at 13:04

I guess the  'for loop' is being compiled out... since it doesn't do anything.

put the 'int i' declaration before the 'for loop'.

and use 'i' after the 'for loop'

maybe you can use

sprintf(string,' i %d\n\r',i);

puts(string);

thats how I got around the printf hardfault issue.

I point my 'puts' to the Usart port...

but I am not an expert, just trying to help...

Olivier GALLIEN
Technical Moderator
February 9, 2017
Posted on February 09, 2017 at 15:26

For compiled out of your 'for loop' you can try to remove optimization. 

For printf you need to either redirect it to an UART or, what I suggest as a first step, use the semihosting feature which will direct your printf to SW4STM32 Console window

For semihosting a tutorial is available in latest version in

Help >> Help Contents >> SystemWorkbench for STM32 User Guide >> Advanced >> Semihosting

Hope it help 

Olivier

Olivier GALLIEN In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.
Sirma Siang
ST Employee
February 9, 2017
Posted on February 09, 2017 at 16:17

Hello

Instead of using a loop, you can use HAL_Delay(time in ms);

i.e.: HAL_Delay(100), wait for 100ms

Kind regards

Sirma

Sirma Siang
ST Employee
February 9, 2017
Posted on February 09, 2017 at 16:23

Hello Kim,

Your code is working on my F769 Discovery board....

The tempo is about 8s.

Are you sure about your GPIO config ?

To drive a led, it should be something like pushpull, with a pull up and high t level.

Kind regards

Sirma

g. lewis
g. lewisAuthor
Associate III
February 9, 2017
Posted on February 09, 2017 at 17:24

Yes Sirma, I used Stm32CubeMx to generate the code and the code works (led is blinking) if I only place the  following statement in the  main{} > while loop:

While(1){

      for(int i=0;i<14200000;i++);

      HAL_GPIO_TogglePin(LD2_GPIO_Port,LD2_Pin);

}

Sirma Siang
ST Employee
February 9, 2017
Posted on February 09, 2017 at 16:28

Hello Kim,

Instead of writting HAL_GPIO_TogglePin(LD2_GPIO_Port,LD2_Pin);

could you try HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); ?

This blink LED2 on my NUCLEO-F411RE board.

Kind regards

Sirma

g. lewis
g. lewisAuthor
Associate III
February 9, 2017
Posted on February 09, 2017 at 16:51

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6rV&d=%2Fa%2F0X0000000bws%2FI7HIzpQ3dNw3pAKdvG7mQMv0ijNdeuP3rF4xg8Kv8S0&asPdf=false
Sirma Siang
ST Employee
February 9, 2017
Posted on February 09, 2017 at 17:30

Hello,

Instead of writting HAL_GPIO_TogglePin(LD2_GPIO_Port,LD2_Pin);

could you try HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); ?

Kind regards

Sirma

g. lewis
g. lewisAuthor
Associate III
February 9, 2017
Posted on February 09, 2017 at 19:35

Yes It's the same...

Andrei Chichak
Lead
February 9, 2017
Posted on February 09, 2017 at 18:42

For the printf problem, you have to add some shim code.

Read this: 

http://embedded.fm/blog/2016/12/13/discovery-uarts-part-3-the-final-step

 

Andrei.

g. lewis
g. lewisAuthor
Associate III
February 9, 2017
Posted on February 09, 2017 at 19:34

Ok. I check that and I'll get back to you soon.

Thanks you all

Mark Shoe
Associate III
May 29, 2017
Posted on May 29, 2017 at 19:41

Your printf goes nowhere. You have to redirect it to UART1:

// redirection of printf

void _write(int file, char *ptr, int len)

{

int i;

for (i = 0; i < len; i++)

{

HAL_UART_Transmit(&huart1, (uint8_t*) &ptr[i], 1, 10);

}

}

The printf has to be closed with a \n

Search youtube for getting started with CubeMX