implicit declaration of function '_delay_ms' [-Wimplicit-function-declaration]

Go To Last Post
14 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I created a C project for the SAMD10 MCU and have an issue that i did not have before with the same code.

 

I have to add that i uninstalled my previous version of ATMEL studio and installed the latest version  ( 7.0.2397 ).

 

my problem is that i get a warning about implicit declaration when i use the delay function in the "asdf.c" file , but i don't get the warning when i use the delay function in the main.c file.

 

The "implicit declaration of function '_delay_ms' [-Wimplicit-function-declaration]"  warning takes me to the "asdf.c" file , not the delay function in the "main.c" file.

 

How do i properly use the delay function in other files thna the "main.c" file ?

 

 

 

Here are the contents 4 files...

 

main.c

#define F_CPU 8000000UL

#include "sam.h"
#include "delay.h"
#include "asdf.h"

int main(void)
{
    /* Initialize the SAM system */
    SystemInit();

    /* Replace with your application code */
    while (1)
    {
		_delay_ms(11);
    }
}

 

asdf.h

 

void asdf_func(int asdf_var);

 

asdf.c

 

void asdf_func(int asdf_var)
{
	asdf_var+1;
	_delay_ms(11);
}

 

delay.h

 

#ifndef DELAY_H_HEADER
#define DELAY_H_HEADER

#define RAMFUNC __attribute__ ((section(".ramfunc")))
#define OPTIMIZE_HIGH __attribute__((optimize("Os")))

#define cpu_ms_2_cy(ms, f_cpu)  (((uint64_t)(ms) * (f_cpu) + (uint64_t)(7e3-1ul)) / (uint64_t)7e3)
#define cpu_us_2_cy(us, f_cpu)  (((uint64_t)(us) * (f_cpu) + (uint64_t)(7e6-1ul)) / (uint64_t)7e6)
#define delay_cycles            portable_delay_cycles
#define cpu_delay_s(delay)      delay_cycles(cpu_ms_2_cy(1000 * delay, F_CPU))
#define cpu_delay_ms(delay)     delay_cycles(cpu_ms_2_cy(delay, F_CPU))
#define cpu_delay_us(delay)     delay_cycles(cpu_us_2_cy(delay, F_CPU))
#define _delay_us	cpu_delay_us
#define _delay_ms	cpu_delay_ms
#define _delay_s	cpu_delay_s

// Delay loop is put to SRAM so that FWS will not affect delay time
OPTIMIZE_HIGH
RAMFUNC
void portable_delay_cycles(uint32_t n)
{
	__asm (
	"loop: DMB	\n"
	#ifdef __ICCARM__
	"SUBS r0, r0, #1 \n"
	#else
	"SUB r0, r0, #1 \n"
	#endif
	"CMP r0, #0  \n"
	"BNE loop         "
	);
}

 

 

 

 

 

 

 

 

This topic has a solution.
Last Edited: Tue. May 12, 2020 - 09:39 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You need to include delay.h in  asdf.c as well

:: Morten

 

(yes, I work for Microchip, yes, I do this in my spare time, now stop sending PMs)

 

The postings on this site are my own and do not represent Microchip’s positions, strategies, or opinions.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That made it all much worse , now it spits a whole array of other errors and warning

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What is the " whole array of other errors and warning"?

delay.h also requires F_CPU to be defined to work properly.

(You can do that in  "Project/Properties/Project/Properties/Toolchain/Compiler/Symbols" for the whole solution)

David

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Look at the Output window after the compile. Fixing the first error shown there often cleans up the other errors.

David

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So , i did the suggestion you made of adding the F_CPU to symbols and adding  #include "delay.h" to the "asdf.c" file.

 

I also changed the "delay.h" file to "delayx.h" . just in case it interferes with some built in file.

 

i had to add " #include "sam.h" " to the "asdf.c" file as well to get rid of a bunch of errors.

 

now the only issue i seem to have is that it complains about multiple declarations...

 

multiple definition of `portable_delay_cycles'       ...\delayx.h    22

 

 

 

EDIT :

 

So i tried something else.

i copied the "delayx.h" file and named it "delayx2.h" and the function inside of called "portable_delay_cycles" , i renamed to "portable_delay_cycles2"

 

By doing this it seesm to work now

 

 

 

 

 

Last Edited: Mon. Apr 20, 2020 - 02:13 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you include your delayx.h file in multiple files, you are defining it multiple times.

Only the declaration should be in the .h file

void portable_delay_cycles(uint32_t n);

while the definition would be in a .c file

David

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am not really certain what to do here.

 

If i don't include it in the "asdf.c" file i get a warning of implicit declaration , if i do i get an error of multiple declarations.

 

Please see my edited comment on how i "solved"  it.

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here is the ATMEL studio project , if it helps to solve the problem.

Attachment(s): 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Move the definition of portable_delay_cycles to a .c file.

Place a declaration of portable_delay_cycles in the .h file.

 

delayx.c:

// definition of function

#include "sam.h"
#include "delayx.h"

// Delay loop is put to SRAM so that FWS will not affect delay time
OPTIMIZE_HIGH
RAMFUNC
void portable_delay_cycles(uint32_t n)
{
	__asm (
	"loop: DMB	\n"
	#ifdef __ICCARM__
	"SUBS r0, r0, #1 \n"
	#else
	"SUB r0, r0, #1 \n"
	#endif
	"CMP r0, #0  \n"
	"BNE loop         "
	);
}

delayx.h:

#ifndef DELAYX_H_HEADER
#define DELAYX_H_HEADER

#define RAMFUNC __attribute__ ((section(".ramfunc")))
#define OPTIMIZE_HIGH __attribute__((optimize("Os")))

#define cpu_ms_2_cy(ms, f_cpu)  (((uint64_t)(ms) * (f_cpu) + (uint64_t)(7e3-1ul)) / (uint64_t)7e3)
#define cpu_us_2_cy(us, f_cpu)  (((uint64_t)(us) * (f_cpu) + (uint64_t)(7e6-1ul)) / (uint64_t)7e6)
#define delay_cycles            portable_delay_cycles
#define cpu_delay_s(delay)      delay_cycles(cpu_ms_2_cy(1000 * delay, F_CPU))
#define cpu_delay_ms(delay)     delay_cycles(cpu_ms_2_cy(delay, F_CPU))
#define cpu_delay_us(delay)     delay_cycles(cpu_us_2_cy(delay, F_CPU))
#define _delay_us	cpu_delay_us
#define _delay_ms	cpu_delay_ms
#define _delay_s	cpu_delay_s

// declaration of function
void portable_delay_cycles(uint32_t n);

#endif

 

edit: added #include "delayx.h" to delayx.c file

David

Last Edited: Mon. Apr 20, 2020 - 05:40 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


done , but still errors...

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

include the delayx.h file in the delayx.c file

David

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

that worked :)

 

Thanks for your help guys.