SAM4E MCU Tour - 22. Introduction and initialization of GMAC and PHY

Publisher:Mingyue1314Latest update time:2017-01-07 Source: eefocusKeywords:SAM4E Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

There is no need to explain much about the role of network communication. The work carried out this time is to initialize the hardware parts needed in the Ethernet communication process, and also introduce the methods of sending and receiving data.

Due to its complexity, the ASF framework is used, but the implementation of the library functions used will also be introduced.

 

1. MAC, PHY and MII

IEEE 802.3 is a commonly used Ethernet standard, which defines the standards of the physical layer (Physical Layer, PHY) and the media access control layer (Media Access Control, MAC). In addition, in the OSI model, MAC is at the bottom of the data link layer.

In terms of hardware implementation, the GMAC peripheral used by M4 implements the MAC function in 802.3. The development board carries a PHY chip model KSZ8051MNL and an RJ45 interface to implement the physical layer functions:

image

The interface between MAC and PHY is the Media Independent Interface (MII). MII includes a data communication interface and a management interface (MDIO). Since the PHY interface is oriented to MAC, we need to manage PHY and exchange data through MAC.

In addition, the Ethernet II frame developed earlier is the frame format commonly used in Ethernet transmission.

 

2. GMAC DMA Buffer

The GMAC uses a DMA interface. Like the M4's general purpose DMAC, it can also automatically perform multiple transfers, but the method is slightly different. The GMAC's DMA uses different buffer lists for transmit and receive, and the buffer descriptor list is an array, not a linked list like the DMAC uses. The starting position of the array is stored in registers (GMAC_RBQB, GMAC_TBQB), and there is a field in the buffer descriptor (Wrap) to indicate whether it is the last descriptor in the array. For example, for the receive buffer:

image

During operation, DMA accesses each buffer descriptor sequentially, and when the last descriptor is accessed, it restarts the traversal.

For the receive buffer, the length of each buffer in the list is the same, which is specified by the DRBS field in the DMA configuration register (GMAC_DCFGR). When the DMA writes data to the receive buffer, it also sets the corresponding fields of the descriptor to indicate the start and end of each frame; at the same time, it also marks relevant information, such as whether it is a broadcast frame, etc.

For the send buffer, the frame length, whether to add CRC and other control information are also indicated in the descriptor. After the data is prepared, the send operation can be triggered by writing the TSTART field to the GMAC_NCR register.

 

 

 

3. Initialize GMAC using ASF

Since PHY is accessed through MAC, GMAC settings must be completed before setting PHY.

GMAC has about 94 registers, of which about 40 are statistical registers, about 15 registers are related to 1588 and PTP, and about 15 registers are related to special addresses and IDs. In addition, in some status registers, you need to write 1 to a specific bit to clear the status of the bit.

The ASF module used is Ethernet GMAC, and then parameters such as MAC address, IP address, subnet mask, gateway and buffer size can be set in conf_eth.h.

Then call the gmac_dev_init() function to initialize GMAC:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
pmc_enable_periph_clk(ID_GMAC);
//MAC address
uint8_t mac_address[] =
        { ETHERNET_CONF_ETHADDR0, ETHERNET_CONF_ETHADDR1,
            ETHERNET_CONF_ETHADDR2,ETHERNET_CONF_ETHADDR3,
ETHERNET_CONF_ETHADDR4, ETHERNET_CONF_ETHADDR5
};
// GMAC options
gmac_options_t gmac_option;
gmac_option.uc_copy_all_frame = 0; // Do not copy all frames
gmac_option.uc_no_boardcast = 0; // Do not ignore broadcast
memcpy(gmac_option.uc_mac_addr,
mac_address, sizeof(mac_address)); //Copy MAC address
// GMAC driver settings
gmac_device_t gmac_dev;
gs_gmac_dev.p_hw = GMAC; //Specify the GMAC register base address
// Initialize GMAC
gmac_dev_init(GMAC, &gmac_dev, &gmac_option);

 

The gmac_dev_init(Gmac* p_gmac, gmac_device_t* p_gmac_dev, gmac_options_t* p_opt) function completes the following tasks:

  1. Disable transmission and reception, disable all GMAC interrupts; clear the statistics registers and the transmission and reception status registers.

  2. Set the GMAC_NCFGR register. According to p_opt, determine whether to copy all frames and whether to ignore broadcasts. At the same time, add GMAC_NCFGR_PEN and GMAC_NCFGR_IRXFCS bits. (I think this should be a BUG. Judging from the comments, the GMAC_NCFGR_RFCS bit should be added.)

  3. After setting up the DMA buffer, call gmac_init_mem() to initialize the buffer descriptor, etc. This function also enables sending and receiving, and also enables a series of interrupts. After the setup is complete, the DMA buffer information will be stored in p_gmac_dev.

  4. Write the MAC address into special address register 1.

 

4. PHY Address

During MDIO communication, each PHY will have a 4-bit address. The KSZ8051MNL chip carried by the development board can set the lower 3 bits of the address according to the pin when powered on or reset:

image

In the development board, the value of PHYAD[2:0] is 001 when powered on, that is, its address is 0x1.

In particular, address 0 can be used as the broadcast address of the chip, and the development board has also been configured in this way. In addition, in ASF, the address of PHY is incorrectly defined as 0. The reason why this works correctly is only because 0 is the broadcast address, and the development board has only one PHY chip. For the sake of rigor, modify this address to the correct value:

1
2
3
4
#ifdef BOARD_GMAC_PHY_ADDR
  #undef BOARD_GMAC_PHY_ADDR
#endif
#define BOARD_GMAC_PHY_ADDR 1

 

 

5. Using PHY in ASF

The module used is Ethernet Physical Transceiver. The macro needs to be declared in conf_board.h:

1
2
/* Using ETH PHY: KSZ8051MNL */
#define CONF_BOARD_KSZ8051MNL

 

  1. initialization.

    After the PHY is powered on, you need to wait for a while for it to stabilize. Then you can initialize it:

    1
    2
    3
    4
    5
    if (ethernet_phy_init(GMAC, BOARD_GMAC_PHY_ADDR,sysclk_get_cpu_hz())
            != GMAC_OK) {
        puts("PHY Initialize ERROR!\r");
        return -1;
    }

    In the ethernet_phy_init() function, the following work is done:

    1. Set the MDIO clock MDC.

    2. Send a reset command to the PHY via MDIO.

    3. Check whether the address is correct. The logic of the check is to first read the content of PHYID1 of PHY, and then determine whether the read content is correct. In the KSZ8051MNL chip, the value of this register is 0x22.

    4. If the address is invalid, because there are only 32 valid MDIO addresses, these addresses are iterated, and then the reset command is resent using the new address checked.

    5. If initialization is successful, GMAC_OK is returned.

  2. Self-negotiation.

    Then you need to let PHY negotiate the communication rate and duplex mode:

    1
    2
    3
    4
    5
    6
    ethernet_phy_auto_negotiate(GMAC, BOARD_GMAC_PHY_ADDR);
    if (ethernet_phy_set_link(GMAC, BOARD_GMAC_PHY_ADDR, 0)
            != GMAC_OK) {
        puts("Set link ERROR!\r");
        return -1;
    }

     

    The ethernet_phy_auto_negotiate() function completes the PHY negotiation and sets the GMAC rate and duplex mode based on the negotiation result. The ethernet_phy_set_link() function checks the link status and applies the PHY auto-negotiation result to the GMAC based on the parameter (the third one).

  3. Interrupt handling.

    The GMAC module of ASF needs to obtain relevant interrupts to perform related work: such as updating information related to the send buffer descriptor, or calling user-defined callback functions.

    1
    2
    3
    4
    5
    // Need to enable related interrupts in NVIC
    void GMAC_Handler(void)
    {
        gmac_handler(&gs_gmac_dev);
    }
  4. Data reception.

    Prepare a buffer first, then call gmac_dev_read() to read the contents of the received frame.

    1
    2
    3
    4
    5
    //#define GMAC_FRAME_LENTGH_MAX       1536
    uint8_t eth_buffer[GMAC_FRAME_LENTGH_MAX];
    uint32_t frm_size;
    gmac_dev_read(&gmac_dev, (uint8_t *) eth_buffer,
            sizeof(eth_buffer), &frm_size);
  5. Data is sent.

    1
    gmac_dev_write(&gmac_dev, (uint8_t *)eth_buffer, frm_size, NULL);

     

    This function can be used to send data using GMAC. The fourth parameter is the callback function after the sending is completed. The callback function is called in gmac_handler().


Keywords:SAM4E Reference address:SAM4E MCU Tour - 22. Introduction and initialization of GMAC and PHY

Previous article:SAM4E MCU Tour - 21. DMAC USART Echo
Next article:SAM4E MCU Tour - 23. Using FPU in AS6 (GCC)

Recommended ReadingLatest update time:2024-11-23 06:01

What is the difference between 51 MCU and PIC MCU? What are the differences in their program settings?
 What is 51 single chip microcomputer   51 single-chip microcomputer is a general term for all single-chip microcomputers compatible with Intel 8031 ​​instruction system. The ancestor of this series of single-chip microcomputers is Intel's 8004 single-chip microcomputer. Later, with the development of Flash rom techno
[Microcontroller]
What is the difference between 51 MCU and PIC MCU? What are the differences in their program settings?
AVR microcontroller T0 use
This article introduces the use of T0 of ATmega 16 microcontroller T0 in M16 is an 8-bit timer/counter The following content is extracted from ATmega 16 Datasheet Note that OC0 must be set to output before use, and the pull-up resistor Use CTC method to generate 38K square wave. void timer0_init(void) //CTC
[Microcontroller]
AVR microcontroller T0 use
Development of a wide-range and high-precision optical fiber sensing thermometer based on single-chip microcomputer
    Abstract: The design and development of a large dynamic range, high-precision optical fiber temperature sensor based on a single-chip microcomputer. A dual-optical path, programmable gain amplification system is used to achieve large dynamic range temperature measurement, and a 12-bit ADC is used to convert the m
[Test Measurement]
STM8 series MCU multiplexing pin programming program precautions
When I was burning a program for a recent project, the program did not match the program's function when it was successfully burned and tested. I spent a lot of time looking for the reason and finally figured it out. The program used the pin multiplexing function. But I did not configure it during debugging, so the fu
[Microcontroller]
Implementing rotating LED light alarm based on 51 microcontroller (Proteus simulation)
Specific function implementation: When the switch is pressed, the buzzer alarms and the LED rotates and lights up. Devices used: AT89C51, button, resistor, buzzer (SOUNDER), 8 LED lights Proteus simulation schematic diagram: simulation: Knowledge introduction: Proteus wiring In order to make the entire schematic
[Microcontroller]
Simple STC15F104E MCU Timing Alarm Production
In an extraordinary period, children can't go to school, adults are not at home, and they sleep until 10 o'clock every morning, which is a bit too much, so I made a simple timer alarm as an alarm clock, mainly to cooperate with the network timing function of Xiaomi smart socket. It's just my own small creation for ref
[Microcontroller]
Simple STC15F104E MCU Timing Alarm Production
Proteus simulation of relay controlled lighting equipment based on 51 single chip microcomputer
Simulation circuit diagram: . Part of the source code: /* *Relay controlled lighting equipment* */ #include reg51.h typedef unsigned char uint8; typedef unsigned int uint16; #define K1_DOWN     P1 & 0x01 //K1 button definition #define RELAY_SWITCH() P2 ^= 0x10 //Relay switch control #define LED_Light() P
[Microcontroller]
Proteus simulation of relay controlled lighting equipment based on 51 single chip microcomputer
About the structure and pull-up problem of P0 port of 51 single-chip microcomputer
1. When P0 is used as the address data bus, V1 and V2 work together to form a push-pull structure. When the level is high, V1 is turned on and V2 is turned off; when the level is low, V1 is turned off and V2 is turned on. In this case, no external pull-up resistor is needed. Moreover, when V1 is turned on, V2 is tu
[Microcontroller]
About the structure and pull-up problem of P0 port of 51 single-chip microcomputer
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号