Modularising

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

For a while I have been modularising sections of my programs by adding .h files under the config folder such as Events.h into which I put all the #defines, prototypes, and complete functions. 

 

I am aware that this method is frowned upon, but it does largely work, but I thought I would at this juncture set about doing my modularising  the proper way which is, as I understand it, to create a .h file in the SRC folder into which the defines and prototypes go, and then have a .c file (in the SRC folder) which contains the actual variables, functions as well as an #include "Events.h" and #include <asf.h>  .  And then I have put #include "Events.h" in the main.c file.

 

#include <asf.h>
#include "Events.h"



static volatile uint32_t event_count = 0;

struct events_resource button_event;
struct events_hook hook;


static void configure_event_channel(struct events_resource *resource){
	struct events_config config;
	events_get_config_defaults(&config);
	config.generator      = Button_Event_Generator;
	config.edge_detect    = EVENTS_EDGE_DETECT_NONE;
	config.path           = EVENTS_PATH_ASYNCHRONOUS;
	config.clock_source   = GCLK_GENERATOR_0;
	events_allocate(resource, &config);
	
}
#ifndef EVENTS_H_
#define EVENTS_H_

//Defines

#define Button_Event_Generator  EVSYS_ID_GEN_EIC_EXTINT_7
#define Button_Event_User    EVSYS_ID_USER_NONE


//Prototypes

static void configure_event_channel(struct events_resource *resource);
static void configure_event_user(struct events_resource *resource);
static void configure_event_interrupt(struct events_resource *resource, struct events_hook *hook);
void event_counter(struct events_resource *resource);





#endif /* EVENTS_H_ */

The problem is that when I come to compile, the compiler shows the error "conflicting types for 'configure_event_channel' " .  As I have copied and pasted the actual function to the prototype in the dot h, I am pretty sure there is no actual conflict.  Even when I call the function from main, I get the same error for that.  The tutorial on modularising in AVR Freaks is not using Studio 7, although I think I have followed the principles in it.  

 

What am I doing wrong?  I have used these principles in other libraries I have written and they compile without a problem.  Thanks for any help.

 

Regards Mike

 

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

My guess is you’ve declared the function ‘static’. This limits the scope of that function to the compilation unit (file). Thus putting the prototype in the .h file for other files to access doesn’t gain you much.

So, for local functions - declare them static and put the prototype in the C file. For global functions ( functions that are called from other files) don’ t declare them static and put the prototype in the .h file.

Another way of looking at it is the .h file ‘exposes’ the functions and symbols you want others to see.

BTW. Convention is that #defines are in upper case.

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

Thanks for this.  My understanding of the scope of static is certainly much better.  And this mistake had certainly made a contribution to the error.

 

What I also discovered in the process of uploading files to the forum was that even though I had edited files, Studio 7 has a curious knack of retaining the older (pre-edited) version of the file with the result that what was shown on the screen was not necessarily what the compiler saw.  Even when I completely deleted the .h and .c files and then completely rewrote them under the same name the original version still seemed to be hanging about.  I could only eliminate the error by writing .h and .c files under new names i.e. Event.c and Event.h (as opposed to Events.c and Events.h).

 

I have added this note in case anybody else finds themselves going round in circles over this annoying thing.  Is there a better way of ensuring that the edited file (what you see on the screen ) is actually what the compiler is working with? 

 

Regards

Mike