Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++

Functions Inclusion

Altera_Forum
Honored Contributor II
1,069 Views

Hello every body; 

I have created macros to deal with my hardware component and put them in a separate .c file, how can I include these macros in my main.c file. 

Thanks.
0 Kudos
5 Replies
Altera_Forum
Honored Contributor II
310 Views

Usually, I put macros in .h file. The file can be included in any .c file requiring the functions.

0 Kudos
Altera_Forum
Honored Contributor II
310 Views

Thanks Mr. tzestan; 

OK, but if what can I do If I want to include a .c file in my main .c file?
0 Kudos
Altera_Forum
Honored Contributor II
310 Views

That defeats the purpose of separating into .h and .c. 

.h is used to keep declarations, macros, constants... but no actual code. 

.c is where definitions are stored. 

 

During compilation, every .c file is compiled individually, with all included files. The step generates an .obj file. Sections of codes (such as functions) in obj are identified using unique symbol names. After compilation, all .obj files are linked into an executable, replacing symbols into actual code addresses. 

 

In your case, if the included .c is also a source file (with function definitions) in the project, there will be multiple .obj files having same symbols. This will cause linker failure unable to resolve symbol address. However, linker can work if the included .c is a file without definitions, but you might as well name it .h to stick to usual convention. 

 

Maybe you want to paste relevant code sections here for us to have a clearer picture on what you like to achieve.
0 Kudos
Altera_Forum
Honored Contributor II
310 Views

Good advice from u Mr.tzestan; 

This is my base functions that i use in my main function 

********************************************************************************************* 

/* 

*Description : AES input Output functions 

*File : "altera_avalon_aes.c" 

* Created on: Jan 26, 2014 

* Author: a4atmel 

*/ 

 

 

 

# include "system.h" 

# include "altera_avalon_aes_regs.h" 

# include <io.h> 

 

 

 

alt_u8 c128=0x0; 

 

void Write_Array(alt_u8 array[4][4]) 

int i,j; 

for (i=1;i<=4;i++) 

for (j=1;j<=4;i++) 

IOWR(InText_BASE,4*(i-1)+j,array[j]); 

void write_key1(alt_u8 array[4][8]) 

int i,j; 

for (i=1;i<=4;i++) 

for (j=1;j<=4;i++) 

iowr(cipherkey1_base,4*(i-1)+j,array[j]); 

void Write_key2(alt_u8 array[4][8]) 

int i,j; 

for (i=1;i<=4;i++) 

for (j=1;j<=4;i++) 

IOWR(Cipherkey2_BASE,4*(i-1)+j,array[4+i][4+j]); 

 

void Read_Array(alt_u8 array[4][4]) 

int i,j; 

for (i=1;i<=4;i++) 

for (j=1;i<=4;i++) 

array[i][j]=IORD(OutText_BASE,4*(i-1)+j); 

 

void Cipher128(alt_u8 InText[4][4],alt_u8 Cipherkey[4][8],alt_u8 OutText[4][4]) 

Write_key1(Cipherkey); 

Write_Array(InText); 

keylen(c128); 

Cipher; 

start; 

Read_Array(OutText); 

stop; 

 

 

**************************************************** 

And this is my main function 

/* 

* Description : AES main function 

* File : main.c" 

* Created on: Jan 26, 2014 

* Author: a4atmel 

*/ 

 

# include "system.h" 

# include "altera_avalon_aes_regs.c" 

# include "altera_avalon_aes.h" 

# include <io.h> 

 

int main() 

// Declarations and Initializations 

 

alt_u8 InText[4][4] = { 

{0xAE, 0x1E, 0x9E, 0x45}, 

{0x2D, 0x03, 0xB7, 0xAF}, 

{0x8A, 0xAC, 0x6F, 0x8E}, 

{0x57, 0x9C, 0xAC, 0x51} 

}; 

 

alt_u8 Cipherkey[4][8] = { 

{0x2B, 0x28, 0xAB, 0x09, 0x0, 0x0, 0x0,0x0}, 

{0x7E, 0xAE, 0xF7, 0xCF, 0x0, 0x0, 0x0,0x0}, 

{0x15, 0xD2, 0x15, 0x4F, 0x0, 0x0, 0x0,0x0}, 

{0x16, 0xA6, 0x88, 0x3C, 0x0, 0x0, 0x0,0x0} 

}; 

alt_u8 OutText[4][4]; 

 

Cipher128(InText,Cipherkey,OutText); 

 

 

return 0; 

}
0 Kudos
Altera_Forum
Honored Contributor II
310 Views

What you have are functions, not macros. 

Create a file "altera_avalon_aes.h" with function declarations similar to the codes at the end of this post. 

Then include this altera_avalon_aes.h at the beginning of main.c and altera_avalon_aes.c. You can leave out the other# includes from altera_avalon_aes.c. 

Make sure altera_avalon_aes.c is a file in your project, but not# include in other files. The project will compile. 

 

//////////////////// 

# ifndef _MY_AESHEADER_ 

# define _MY_AESHEADER_ 

 

# include "system.h" 

# include "altera_avalon_aes_regs.h" 

# include <io.h> 

 

void Write_Array(alt_u8 array[4][4]); 

void Write_key1(alt_u8 array[4][8]); 

void Write_key2(alt_u8 array[4][8]); 

void Read_Array(alt_u8 array[4][4]); 

void Cipher128(alt_u8 InText[4][4],alt_u8 Cipherkey[4][8],alt_u8 OutText[4][4]); 

 

# endif 

/////////////////////////////////////
0 Kudos
Reply