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

Problem in creating a custom SOPC device driver

Altera_Forum
Honored Contributor II
1,350 Views

Hi. 

 

I created a SOPC custom component. It's a FIFO. I want to add it to a NIOS project. In doing so, I need to write a driver. I'm reading the paper "Developing Device Drivers for the Hardware Abstraction Layer, Nios II Software Developer’s Handbook", but I can't understand: 

 

1 - How to start developing the driver  

2 - How to add it to de NIOS project. 

 

Thanks a lot for your help.
0 Kudos
7 Replies
Altera_Forum
Honored Contributor II
664 Views

You probably don't need to write anything that is explicitly a 'driver' (unless ou are planning to run linux - but then you need a linux driver). 

In particular you can really ignore anything to do with a HAL - it will just slow down your code! 

 

For a simple project your 'main' function can directly access the io registers of your custom vhdl.
0 Kudos
Altera_Forum
Honored Contributor II
664 Views

I agree with dsl. It doesn't mean you can't have a library. I've done libraries for a custom TSE driver and the lwIP TCP/IP stack. Create new projects that create LIB files and add the LIB files to your base project linker options. Multiple projects can then use the library which supports your SOPC component and the MAKE is faster as there is less code to scan. Note: I do not have my project configured to reference the LIB so MAKE won't scan it on every build, but I add the LIB as a required file for the linker. If I change a LIB (rarely), I right-click it and select 'Build'. 

 

BillA
0 Kudos
Altera_Forum
Honored Contributor II
664 Views

Thanks... but... I keep on misunderstanding. 

 

The guide I mentioned says that each component has to have a *_sw.tcl script (generated by SOPC). However, the script I have is *_hw.tcl. 

The question would be, how can I generate the _sw.tcl script?? 

 

About what dsl says "For a simple project your 'main' function can directly access the io registers of your custom vhdl". Can you tell me what paper I have to read to understand how to do that? 

 

I'm sorry if my questions are so basic. I'm trying to learn. 

 

Thanks a lot again!!! 

 

PS: Is my English not much clear?? Just in case, my apologies for that.
0 Kudos
Altera_Forum
Honored Contributor II
664 Views

Hi. 

 

I found this in system.h : 

 

/* 

* dcfifo_de_top_0 configuration 

*/ 

 

# define DCFIFO_DE_TOP_0_NAME "/dev/dcfifo_de_top_0" 

# define DCFIFO_DE_TOP_0_TYPE "dcfifo_de_top" 

# define DCFIFO_DE_TOP_0_BASE 0x00400000 

# define DCFIFO_DE_TOP_0_SPAN 4 

# define DCFIFO_DE_TOP_0_TERMINATED_PORTS "" 

# define ALT_MODULE_CLASS_dcfifo_de_top_0 dcfifo_de_top 

 

This corresponds to my custom SOPC component. So, I tried this: I modified the DE2_NET project main function (from Altera DE2_NET development board) by adding this following code: 

 

unsigned int i; 

 

 

FILE* fp; 

char buffer[10] = {0}; 

 

fp = fopen("/dev/dcfifo_de_top_0", "r"); 

if (fp == NULL) 

printf ("\nCannot open file"); 

exit (1); 

 

fread (buffer, 10, 1, fp); 

fclose (fp); 

 

 

for(i=0; i<sizeof(buffer); i++) 

printf("\nbuffer[%d] = %d", i, buffer[i]);  

 

 

I build the project, but when I run it, console show this: 

 

Cannot open the file. 

 

So, it seems that fp keeps on pointing to NULL (because cannot open the file). 

 

The question is: Am I doing anything bad??  

 

Thanks again!!!!
0 Kudos
Altera_Forum
Honored Contributor II
664 Views

This will only work if you created a complete HAL driver. 

The easiest way is to actually access the registers with the IORD and IOWR macros, and the DCFIFO_DE_TOP_0_BASE define in system.h to know what address to use.
0 Kudos
Altera_Forum
Honored Contributor II
664 Views

You also probably don't want all the software overhead of the stdio functions.

0 Kudos
Altera_Forum
Honored Contributor II
664 Views

>>The guide I mentioned says that each component has to have a *_sw.tcl script (generated by SOPC). However, the script I have is *_hw.tcl. 

 

I believe you have to create your own *_sw.tcl file. It's been awhile since I've done this (Quartus Version 9.0) . It is documented on the Altera website somewhere. I was able to struggle through it. SOPC builder will copy this to your project, I believe. 

 

And (at least when I was doing it), I recall that I needed to create my own class.ptf file. That was NOT documented. SOPC builder did not create one for me. Perhaps it does now. 

 

Here's my directory structure of a driver I made that worked that I called altera_avalon_hdlc_receiver. 

 

Volume in drive C has no label. 

Volume Serial Number is 6C04-034A 

Directory of C:\altera\IP\altera_avalon_hdlc_receiver 

09/23/2010 03:51 AM <DIR> . 

09/23/2010 03:51 AM <DIR> .. 

05/07/2009 12:04 PM 1,353 altera_avalon_hdlc_receiver_sw.tcl 

05/07/2009 03:42 PM 161 class.ptf 

06/01/2010 12:19 AM <DIR> HAL 

06/01/2010 12:19 AM <DIR> hdl 

06/01/2010 12:19 AM <DIR> inc 

09/23/2010 03:51 AM 0 output.txt 

3 File(s) 1,514 bytes 

Directory of C:\altera\IP\altera_avalon_hdlc_receiver\HAL 

06/01/2010 12:19 AM <DIR> . 

06/01/2010 12:19 AM <DIR> .. 

06/01/2010 12:19 AM <DIR> inc 

06/01/2010 12:19 AM <DIR> src 

0 File(s) 0 bytes 

Directory of C:\altera\IP\altera_avalon_hdlc_receiver\HAL\inc 

06/01/2010 12:19 AM <DIR> . 

06/01/2010 12:19 AM <DIR> .. 

05/10/2009 11:27 PM 3,031 altera_avalon_hdlc_receiver.h 

1 File(s) 3,031 bytes 

Directory of C:\altera\IP\altera_avalon_hdlc_receiver\HAL\src 

06/01/2010 12:19 AM <DIR> . 

06/01/2010 12:19 AM <DIR> .. 

05/10/2009 11:25 PM 5,723 altera_avalon_hdlc_receiver.c 

05/07/2009 12:39 PM 151 component.mk 

2 File(s) 5,874 bytes 

Directory of C:\altera\IP\altera_avalon_hdlc_receiver\hdl 

06/01/2010 12:19 AM <DIR> . 

06/01/2010 12:19 AM <DIR> .. 

05/14/2009 03:59 AM 4,630 HDLC_Receive.v 

05/14/2009 03:58 AM 4,631 HDLC_Receive.v.bak 

05/14/2009 04:00 AM 4,243 hdlc_receiver_avalon.v 

05/14/2009 12:29 AM 4,317 hdlc_receiver_avalon.v.bak 

05/14/2009 12:30 AM 3,632 hdlc_receiver_avalon_hw.tcl 

05/11/2009 08:31 PM 3,927 hdlc_receiver_avalon_hw.tcl~ 

05/12/2009 03:08 AM 6,265 HDLC_receive_top.v 

05/12/2009 03:01 AM 6,265 HDLC_receive_top.v.bak 

8 File(s) 37,910 bytes 

Directory of C:\altera\IP\altera_avalon_hdlc_receiver\inc 

06/01/2010 12:19 AM <DIR> . 

06/01/2010 12:19 AM <DIR> .. 

05/10/2009 11:26 PM 3,244 altera_avalon_hdlc_receiver_regs.h 

1 File(s) 3,244 bytes 

Total Files Listed: 

15 File(s) 51,573 bytes 

17 Dir(s) 27,238,227,968 bytes free 

 

 

altera_avalon_hdlc_receiver_sw.tcl looks like this:# # altera_avalon_hdlc_receiver_driver.tcl# # Create a new driver 

create_driver altera_avalon_hdlc_receiver_driver# Associate it with some hardware known as "altera_avalon_hdlc_receiver" 

set_sw_property hw_class_name altera_avalon_hdlc_receiver# The version of this driver 

set_sw_property version 9.0# This driver may be incompatible with versions of hardware less# than specified below. Updates to hardware and device drivers# rendering the driver incompatible with older versions of# hardware are noted with this property assignment.# # Multiple-Version compatibility was introduced in version 7.1;# prior versions are therefore excluded. 

set_sw_property min_compatible_hw_version 7.1# Initialize the driver in alt_sys_init() 

set_sw_property auto_initialize true# Location in generated BSP that above sources will be copied into 

set_sw_property bsp_subdirectory drivers# # Source file listings...# # C/C++ source files 

add_sw_property c_source HAL/src/altera_avalon_hdlc_receiver.c# Include files 

add_sw_property include_source HAL/inc/altera_avalon_hdlc_receiver.h 

add_sw_property include_source inc/altera_avalon_hdlc_receiver_regs.h# This driver supports HAL & UCOSII BSP (OS) types 

add_sw_property supported_bsp_type HAL 

add_sw_property supported_bsp_type UCOSII# End of file 

 

class.ptf looks like this: 

CLASS altera_avalon_hdlc_receiver 

MODULE_DEFAULTS 

class = "altera_avalon_hdlc_receiver"; 

class_version = "7.080900"; 

 

 

You will have to add the source path to your driver in NIOS II workspace or your program won't find it when you try to# include the .h files. 

 

In your case, you will need to write the fopen, fwrite, fread routines to deal with your custom device. I remember reading documentation on this subject. 

 

 

That's about all I can help. I haven't done this in awhile. All I can suggest is read until you are blue in the face. Then read some more. It IS possible, and MUCH nicer to abstract your IORD/IOWR commands in your driver. Using these commands in your main code is a programming sin, in my opinion. 

 

Good luck!
0 Kudos
Reply