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++
12612 Discussions

My program get stuck in fopen() while trying to open the LCD HAL mount point.

Altera_Forum
Honored Contributor II
2,576 Views

Hi, I got a problem integrating two parts of a C program with Nios II 9.1 IDE. It's for a university team project and we use a time limited core license of Quartus II 9.1SP1 web edition. We're using Terasic DE2 development board which include a Cyclone II FPGA, 8MB SDRAM, LCD, GPIO and so many other things that we're not using for the project. So, as you can understand, we're loading our program in SDRAM. We've started making our Nios II system with SOPC Builder adding all components we needed for the project based on an Altera tutorial. Our Nios II system is the same from the beginning and all programs tested on it have working well, but now we got a problem integrating two part of the program. 

 

The two part are running fine a part. My team and I tested the first part implementing the main program with LCD display functions and it's working well. We tested the second part in the same project after moving the first part source files away. The major differences of this second part is that it uses the UART core and malloc() function. The test had good results. The second part is running fine with a temporary main() function that I made explicitly to test it. 

 

When building the two parts together in the same Nios II C project, we have no errors nor warnings. The problem is when the program is running. When trying to open the "/dev/lcd_0" mount point the program get stuck within the fopen() function never returning to the caller. However, it's the same exact code that it's executed when the first part program is running appart ; in that way, it's working well, the fopen returns the FILE pointer...  

 

Actually, we traced the problem to the first call to fopen() and I simplified the program to better explain it. Now, the first statement executed in the main() is a test function for the lcd. The code goes like this : 

 

/* FILE : main.c */ # include <stdio.h> void test_lcd(void) { FILE * lcd; fprintf(stderr, "Test_lcd_start\n"); lcd = fopen("/dev/lcd_0", "w"); if(lcd == NULL) { fprintf(stderr, "Failed to open LCD"); exit(EXIT_FAILURE); } fprintf(lcd, "Hello on the LCD\n"); fprintf(stderr, "Test_lcd_end\n"); fclose(lcd); } int main(void) { test_lcd(); }When I debug it with Nios II 9.1 IDE, the program gets in the fopen function. If I hit 'pause', the program is in the fopen subcalls to Hardware Abstraction Layer (HAL) library functions. It is stuck in a infinite loop that I don't have time to totally understand. Anyway, that is not my code, its Altera, and I suppose it works, but there is something somewhere in my code that causes that situation in Altera code. This is where, I'm stuck myself, what goes wrong and where? :confused: Maybe some of you have ideas or knows of related issues to my problem?  

 

As a bonus, I've noticed something. When I run the test_lcd() code directly in main(), it works??? How, I don't know and I don't think it very solves the problem. I mean that I will probably have related bugs harder to find if it works weird like this and not in a sub function. None the less, it's a clue. :rolleyes:That's an example : 

/* FILE : main.c */ # include <stdio.h> int main(void) { FILE * lcd; lcd = fopen("/dev/lcd_0", "w"); if(lcd == NULL) { fprintf(stderr, "Failed to open LCD"); exit(EXIT_FAILURE); } fprintf(stderr, "Hello on the LCD\n"); fclose(lcd); }As I said, the first part and the second part of the program are working well apart. When the two are build together, I got the fopen() problem without ever trying to call a function from the second part source file. 

If somebody could help, my team and I will very appreciate. 

 

Pic.
0 Kudos
4 Replies
Altera_Forum
Honored Contributor II
1,730 Views

It could be a memory corruption or stack overflow problem.... Do you have a function that allocates a lot of variables on the stack? 

Do you use any pointers? Is that case could you double check that they point where they should? 

If you use tables, can you check that the index is always in the allowed range?
0 Kudos
Altera_Forum
Honored Contributor II
1,730 Views

Thank you for your reply. 

 

 

--- Quote Start ---  

It could be a memory corruption or stack overflow problem.... Do you have a function that allocates a lot of variables on the stack? 

Do you use any pointers? Is that case could you double check that they point where they should? 

If you use tables, can you check that the index is always in the allowed range? 

--- Quote End ---  

 

 

Yes, yes and yes. I do all of that and will double check my code, but it could take a while to find the error. 

Curiously, I wonder why it could happen only when adding the second part of the program and not when executing them apart. 

However, I thought that this kind of errors pops out only when the said erroneous function, pointer or array is executed in the code, but this isn't my case. The potentially bugged code is not yet executed at the point when the program get stuck. Could it happens if its for example the function with the error is not called at all? If yes, I will comment out all the definitions of my potentially bugged functions one by one and see which one cause the problem.
0 Kudos
Altera_Forum
Honored Contributor II
1,730 Views

No, the function that causes the error must be called in order to create those kind of problems. But the problems can happen long after the bugged function was called. 

 

The only cases where I had this type of error (merging two projects causes hangs, modifying a function or changing optimisations settings makes the application crash even if said function isn't called) were cases with one form of memory corruption or another... 

 

Could it be a hardware problem, such as a part of memory that can't be accessed?
0 Kudos
Altera_Forum
Honored Contributor II
1,730 Views

Hello to everybody, 

I try to implement 2 × 16 character display Lumex LCM-S01602DSR/C. 

I used Nios II system and added lcd component. 

Now i try to show some characters on lcd, here is my code. 

but unfortunatelly, all i can see is uncoorect characters, they don't show any digits. 

fopen works and it gives right pointer 

How do you think what are the problems? 

 

 

--- Quote Start ---  

#include <stdio.h># include <altera_avalon_lcd_16207_regs.h># include <altera_avalon_lcd_16207.h> 

//#include <altera_avalon_lcd_16207.c> 

int main() 

FILE * lcd;  

lcd = fopen("/dev/lcd", "w"); 

if(lcd == NULL) 

fprintf(stderr, "Failed to open LCD"); 

//exit(1); 

//fprintf(stdout, "Hello on the LCD\n"); 

//fprintf(stdout, "1"); 

int i=0; 

for (i=0; i< 7; i++){ 

fprintf(stdout, "7700"); 

fprintf(stderr, "1133"); 

fprintf(stdin, "4499");  

fprintf(stdout, "\n"); 

fprintf(lcd, "[ 2 J"); 

 

// while (1){ 

// fprintf(lcd, "7700"); 

// }  

fclose(lcd); 

 

return 0; 

--- Quote End ---  

0 Kudos
Reply