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

fail to create output file - simple io problem

Altera_Forum
Honored Contributor II
2,492 Views

Any idea what is wrong with the code below? The code failed to create the sample.txt file. Runtime error. 

 

#include <stdio.h> 

#include <string.h> 

#include <stdlib.h> 

#include <unistd.h> 

#include <fcntl.h> 

#include <sys/types.h> 

#include <sys/stat.h> 

 

int main(void){  

 

const void *str = "hello\n"; 

size_t count;  

 

 

FILE *fp = open("sample.txt", O_WRONLY); 

printf("Opening file sample.txt\n"); 

 

 

if(fp == null) {printf("Open failed");return EXIT_FAILURE; 

 

 

else {printf("Open succeeded\n"); 

 

 

printf("Writing to file ...\n"); 

count = write(fp, str, strlen(str)); // x,  

 

[/INDENT]printf("Writing completed \n"); close(fp); 

 

 

return EXIT_SUCCESS;}
0 Kudos
10 Replies
Altera_Forum
Honored Contributor II
707 Views

Where do you expect the file to be created? What operating system do you run the Nios on? 

If you have installed a writeable filesystem, I guess you'd at least have to give an absolute path.
0 Kudos
Altera_Forum
Honored Contributor II
707 Views

Em. I expect the file to be created in the same location as the source code (the .c file). Currently the Nios II IDE is run on a Windows machine. I am not sure how to give an absolute path in the windows machine. It doesn't seem to be working.  

 

Thanks for replying.
0 Kudos
Altera_Forum
Honored Contributor II
707 Views

It doesn't work that way. The code is executed on the Nios CPU, not on the PC, and the Nios system is not able to create a file on the PC. If you want to transfer data from the Nios system to your PC, you'll have to make your own protocol (or print text on the Jtag UART interface and copy-paste, if you don't have a lot to transfer).

0 Kudos
Altera_Forum
Honored Contributor II
707 Views

Oh. I figured that i will use the zip file system and program it to the flash. To open the file for reading i will use; 

 

fopen("/mnt/rozipfs/sample.txt","r"); 

 

and to write to the sample.txt file i will use something like alt_write_flash_block ... 

 

I am trying this out. Not sure if it will work.
0 Kudos
Altera_Forum
Honored Contributor II
707 Views

I managed to store my file (.txt) using the zip filesystem and then read from it. Apart from that, I also need to write to the file.  

 

My question is: 

1. How to store a .txt file in flash after writing to it? 

 

I am not sure if my understanding on the file operations of nios ii is correct. Thanks for the help.
0 Kudos
Altera_Forum
Honored Contributor II
707 Views

The zip filesystem is read only, and I'm not sure there is a filesystem with write support available for the Altera HAL. You may have to move to ucLinux or eCos, or try to port a filesystem code you can find on the net (such as fatfs (http://code.google.com/p/minini/wiki/fatfs)) to Nios. 

You can also use alt_write_flash_block to write, but it will be written as a bsoc, not as a filesystem entry. You would need to read it back with alt_read_flash_block.
0 Kudos
Altera_Forum
Honored Contributor II
707 Views

Oh. Is ucLinux/ eCos open source? Will look it up now.  

How about MicroC/OS-II? 

 

Thanks for the hint, I was getting stuck and wasting time going around nowhere. Been a few days now.
0 Kudos
Altera_Forum
Honored Contributor II
707 Views

I don't know if uc/OS-II has a filesystem with write. I know that uCLinux and eCos do, but they require more work (and time) before being usable. You need to get familiar with the environment and the building process. 

What type of flash memory are you using and why do you need a file system? Depending on what you want to do, it could be a lot easier to just use the block i/o functions.
0 Kudos
Altera_Forum
Honored Contributor II
707 Views

Actually my project takes in parameters and generates a bmp file. I am utilizing the hardware acceleration to compare the time taken to complete the bmp iterations. 

 

The problem is how do i create the bmp file? Write the bit values to flash? And how do i retrieve it? 

 

I am at the stage where i am still finding out on how to implement it and exploring the options. I do not have a clear picture yet.
0 Kudos
Altera_Forum
Honored Contributor II
707 Views

You'd probably have to create a protocol between your FPGA and a PC to retrieve the file. Anything between serial and Ethernet, depending on the file size and your speed requirements. 

I think you could also use the altera console tools to directly access the Nios CPU's RAM but I've never done it and don't know how this works.
0 Kudos
Reply