) Niosii device management analysis

Source: Internet
Author: User

Address: http://bbs.uconny.com/thread-189-1-2.html

 

Niosii device Analysis
Altera is one of the world's leader in programmable chip system (FPGA) solutions, and niosii is the latest 32-bit embedded soft-core processor launched by Altera, with great flexibility. niosiiDevelopmentThe package contains a set of general peripherals and interface libraries, allowing you to easily integrate the system. We also need to integrate IP addresses with proprietary intellectual property rights into the niosii processor system. Through the user logic interface wizard of the system, we can easily integrate IP addresses into the niosii processor system, provide device drivers at the same timeProgramIn this article, we will make an analysis of the niosii Device System for developers to refer to when writing device drivers.
General description
The Hardware Abstraction Library (hardware action layer system library, hal), applications do not directly control hardware devices, but call the Hal API to drive. Hal API contains the ansi c standard library. application developers can use familiar C library functions to access (Control) devices and files, hal enables the devices in niosii to have relatively consistent development interfaces like those in Unix systems. It also provides UNIX-style library functions, its usage and habits are similar to the familiar Unix (Linux) system, and the device usage is more consistent. This system allows application developers and device-driven developers to work together to improve development efficiency and reduce the difficulty of application development.

Correlation between niosii device and hardware system definition
The development of the system is a customizable and highly flexible process. In the system builder, you can define the processor system and peripheral generation. the PTF file. Based on the PTF file, the system library is generated for the project when the project is created. h Completely defines the hardware system parameters. alt_sys_init.c allocates the peripheral system resources and initializes the device environment. These two files are the entrance for our analysis,

The logical structure of the project is as follows:

Distribution of peripheral system resources and device environment Initialization
The device drivers, real-time operating systems (ucos ii), and applications in the system are eventually compiled into the same binary file and downloaded to the target system flash for use. Therefore, device resources are defined in the form of global variables. devices of the same type are added to the device two-way linked list and devices are used through Hal APIs. The following uses lan91c111 network peripherals as an example to describe the distribution of peripheral system resources and the initialization of the device environment.
1. Global device linked list definition
In the system library alt_lwip_dev.c, the macro alt_llist_head (alt_lwip_device_list) defines the two-way device linked list of alt_lwip_device_list.
In alt_llist.h, the macro alt_llist_head is defined as follows:
Typedef struct alt_llist_s alt_llist;
Struct alt_llist_s {
Alt_llist * Next;/* pointer to the next element in the list .*/
Alt_llist * Previous;/* pointer to the previous element in the list .*/
};
# Define alt_llist_head (head) alt_llist head ={& head, & head}
It can be seen from the definition that the initial definition is a two-way linked list structure pointing to itself.
2. Global peripheral system resource allocation
L lan91c111 peripherals in system. H, an application project generated by niosii IDE are as follows:
/*
* Lan91c111_0 Configuration
*
*/
# Define lan91c111_0_name "/dev/lan91c111_0"
# Define lan91c111_0_type "altera_avalon_lan91c111"
# Define lan91c111160_base 0x01200000
# Define lan91c111_0_irq 1
# Define lan91c111_0_is_ethernet_mac 1
# Define lan91c111_0_lan91c111_registers_offset 0x0300
# Define lan91c111_0_lan91c111_data_bus_width 32
L allocate peripheral system resources:
Allocate peripheral system resources in alt_sys_init.c
/*
* Allocate the device Storage
*
*/
Altera _ Avalon_cfi_flash_instance (cfi_flash_0, cfi_flash_0 );
Altera_avalon_jtag_uart_instance (jtag_uart_0, jtag_uart_0 );
Altera_avalon_lan91c111_instance (lan91c111_0, lan91c111_0 );
From above Code It can be seen that niosii defines peripheral system resources with a relatively consistent macro.
The altera_avalon_lan91c111_instance macro is defined in altera_avalon_lan_91c111.h:
# Define altera_avalon_lan91c111_instance (name, Dev )\
Alt_avalon_lan91c111_if Dev = \
{\
{\
Alt_llist_entry ,\
{\
0 ,\
Name ##_ name ,\
Alt_avalon_lan91c111_init ,\
Alt_avalon_lan91c111_rx ,\
},\
},\
Name ##_ base + name ##_ lan91c111_registers_offset ,\
Name ##_ IRQ ,\
Name ##_ lan91c111_data_bus_width ,\
0 ,\
0 \
};
There are two key points in syntax to understand this macro. First, the structure variable lan91c111_if of the alt_avalon_lan91c111_if type is defined (result of Dev replacement), and name # is replaced with lan91c111_0 during compilation and preprocessing, for example: name ##_ IRQ is compiled into lan91c111_0_irq. The macro is in system. H is defined and further replaced with 1.
To understand this macro logically, you also need to define the alt_avalon_lan91c111_if type. In altera_avalon_lan_91c111.h, The alt_avalon_lan91c111_if structure is defined as follows:
Typedef struct
{
Alt_lwip_dev_list lwip_dev_list;
Int base_addr;
Int IRQ;
Int bus_width;
Sys_sem_t semaphore;
Alt_u8 tx_packet_no;/* Number of packet allocated for TX */
} Alt_avalon_lan91c111_if;
In the above structure definition, base_addr, IRQ, and bus_width are defined in system. H. sys_sem_t is the semaphore used by the device and will be assigned a value during device initialization. tx_packet_no is allocated in use. The alt_lwip_dev_list structure is defined as follows in alt_lwip_de.h:
Struct alt_lwip_dev
{
/* The netif pointer must be the first element in the Structure */
Struct netif * netif;
Const char * Name;
Err_t (* init_routine) (struct netif *);
Void (* rx_routine )();
};
Typedef struct
{
Alt_llist lList;/* for internal use */
Alt_lwip_dev;
} Alt_lwip_dev_list;
From the code above, we can see that lList in alt_lwip_list is a device linked list, and the macro is assigned a structure with a null pointer. In the dev structure of alt_lwip_dev_list, all regions except netif 0 are set. netif is set during device initialization. The initialization function and read device function of the network device lan91c111 are set, the alt_avalon_lan91c111_init function and the alt_avalon_lan91c111_rx function are defined in the network device driver. If we want to develop another network chip driver Hal, we need to provide similar functions.
1. device environment Initialization
Through the analysis in the previous section, we can clearly see that the device management linked list is defined as a global structure variable, and the device resources are allocated as global variables in the macro definition, these resources are statically defined and allocated. The device environment initializes and dynamically organizes the resource data. In alt_sys_init.c, The alt_sys_ini () function is executed before the main () function, and the related parts of the program are as follows:
/*
* Initialise the devices
*/
Void alt_sys_init (void)
{
Altera_avalon_cfi_flash_init (cfi_flash_0, cfi_flash_0 );
Altera_avalon_jtag_uart_init (jtag_uart_0, jtag_uart_0); altera_avalon_lan91c111_init (lan91c111_0, lan91c111_0 );
}
The environment initialization function of related devices in the alt_sys_init function is called. altera_avalon_lan91c111_init is a macro defined in altera_avalon_lan91c111.h:
# Define altera_avalon_lan91c111_init (name, Dev )\
If (name ##_ IRQ = alt_irq_not_connected )\
{\
Alt_link_error ("error: interrupt not connected for" # Dev "."\
"The Altera aveon lan91c111 driver requires that "\
"Interrupt is connected. Please select an IRQ "\
"This device in FPGA builder .");\
}\
Else if (name ##_ lan91c111_data_bus_width! = 32 )\
{\
Alt_link_error ("error: invalid configuration for" # Dev "."\
"The Altera aveon lan91c111 driver currently only "\
"Supports the configuration of MAC/PHY on "\
"Development Board. Please select this option "\
"In systems builder .");\
}\
Else \
{\
Alt_lwip_dev_reg (Dev );\
}
The above Code clearly states that the alt_lwip_dev_reg (Dev) function is executed, and the macro parameter should be replaced with lan91c111_0, while lan91c111_0 can be defined as the global variable of alt_avalon_lan91c111_if in section 2nd.
The alt_lwip_dev_reg function is defined as the following macro in alt_lwip_dev.h:
# Define alt_lwip_dev_reg (Dev) alt_llist_insert (& alt_lwip_device_list, & Dev. lwip_dev_list.llist)
The function call is converted to alt_llist_insert (& alt_lwip_device_list, & lan91c111_0.lwip_dev_list.llist). The previous parameter is the global linked list structure described in section 1st, the next parameter is the device linked list address described in section 2nd. Through the above analysis, we know that the device environment Initialization is added to the structure of the device's two-way Ring linked list. In actual application development, we can also manage the device using a similar method. The following is a detailed analysis of the alt_llist_insert () function.
The alt_llist_insert () function is defined in alt_llist.h:
Static alt_inline void alt_always_inline alt_llist_insert (alt_llist * List,
Alt_llist * entry)
{
Entry-> previous = List;
Entry-> next = List-> next;

List-> next-> previous = entry;
List-> next = entry;
}
NetworkProtocolStack and network device Initialization
Ucosii and LWIP are integrated in the niosii IDE environment, which must be supported by UCOS. In the nio ii ide, we can select the simple socket server project template to create projects supported by the network. The main function of the Project main file generally has the following program lines:
/*
* Our very first call in an LWIP example design is to start up LWIP TCP/IP
* Stack. we specify a microc/OS-II thread priority for the tcp_ip thread,
* As well as a call-back routine, init_done_func (), which is called once
* The stack is alive and well.
*/
Lwip_stack_init (lwip_tcpip_task_priority, init_done_func, 0 );
......
/*
* As with all microc/OS-II designs, once the initial thread (s) and
* Associated RTOS resources are declared, we start the RTOS. That's it!
*/
Osstart ();
The first line of the statement initializes the network protocol stack and network device initialization. osstart () starts the UCOS real-time multi-task system.
In the previous article, we analyzed that network devices allocated system resources and added them to the linked list of network devices, but the network interface chip and related interface circuit were not initialized, now, we will gradually analyze how the network protocol stack of niosii establishes and initializes underlying devices. Our analysis focuses not on LWIP, but on preparing to write our own network Hal, therefore, our analysis focuses on the combination of network Hal and LWIP.
The lwip_stack_init function is defined in alt_lwip_dev.c. The second parameter is the callback initialization function. The definition of lwip_stack_init is as follows:
Void lwip_stack_init (INT thread_prio, void (* initfunc) (void *), void * Arg)
{
Sys_init ();
# Ifdef stats
Stats_init ();
# Endif/* stats */
Mem_init ();
Memp_init ();
Pbuf_init ();
Netif_init ();
Tcpip_init (thread_prio, initfunc, ARG );
Return;
}
In the tcpip_init () function, the initfunc function is called back when the TCPIP task is started. The init_done_func function is in the user's main program (usually in the same file as the main function ). Initialize the Ethernet device in the init_done_func function:
/*
* At this point LWIP has been initialized, but the Ethernet interface has
* Not; The initialise_lwip_devices () call does so, adding in microc-OS/II
* Threads for low-level Ethernet MAC interface and TCP protocol timer.
*/
If (! Lwip_devices_init (ether_prio ))
Die_with_error ("[tcpip_init_done] fatal: Can't add Ethernet interface! ");

In the lwip_devices_init (ether_prio) function, allocate the netif space and call
Dev_list_ptr-> Dev. netif = netif_add (dev_list_ptr-> Dev. netif,
& Ipaddr, & netmask, & GW,
(Void *) dev_list_ptr, dev_list_ptr-> Dev. init_routine,
Tcpip_input );
}
The dev. init_routine set during device environment Initialization is called, and the network interface circuit and chip register are set to complete network device initialization.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.