#include #include #include #include #include #include #include #include "libsys.h" #include "emac.h" volatile uint32_t g_rx_pkt_cnt; volatile uint32_t g_rx_byte_cnt; volatile uint32_t g_rx_byte_cnt_last; uint8_t g_buffer[2048]; // ------------------------------------------------------------------- uint8_t mac_dst[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; uint8_t mac_src[6] = {0x00, 0x00, 0x31, 0x10, 0x19, 0x70}; // data 64 .. 1500 uint8_t test_data[] = { 0x08, 0x00, 0x45, 0x00, 0x00, 0x30, 0xB3, 0xFE, 0x00, 0x00, 0x80, 0x11, 0x72, 0xBA, 0x0A, 0x00, 0x00, 0x03, 0x0A, 0x00, 0x00, 0x02, 0x04, 0x00, 0x04, 0x00, 0x00, 0x1C, 0x89, 0x4D, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13 }; uint32_t emac_crc32(uint32_t crc, uint8_t *pData, uint32_t len) { uint32_t n; uint32_t crc_table[] = { 0x4DBDF21C, 0x500AE278, 0x76D3D2D4, 0x6B64C2B0, 0x3B61B38C, 0x26D6A3E8, 0x000F9344, 0x1DB88320, 0xA005713C, 0xBDB26158, 0x9B6B51F4, 0x86DC4190, 0xD6D930AC, 0xCB6E20C8, 0xEDB71064, 0xF0000000 }; for (n=0; n < len; n++) { crc = (crc >> 4) ^ crc_table[(crc ^ (pData[n] >> 0)) & 0x0F]; /* lower nibble */ crc = (crc >> 4) ^ crc_table[(crc ^ (pData[n] >> 4)) & 0x0F]; /* upper nibble */ } // printf("CRC32: "); // for (n=0; n<4; n++) /* display the CRC, lower byte first */ // { // printf("%02X ", (crc >> 8*n) & 0xFF); // } // printf("\n"); return crc; } void handler6(void) { UINT32 i, num_bytes, num_words; volatile UINT32 *pEmac_ctrl = (UINT32*)SYS_EMAC_RX_CTRL; volatile UINT32 *pEmac_size = (UINT32*)SYS_EMAC_RX_SIZE; volatile UINT32 *pEmac_data = (UINT32*)SYS_EMAC_DATA_CACHED; if(EMAC_RX_is_avail()) { num_bytes = EMAC_RX_get_size(); num_words = num_bytes >> 2; if (num_bytes & 3) num_words++; DCACHE_invalidate_all(); EMAC_RX_request(); for (i=0; i < num_words; i++) { ((UINT32*)g_buffer)[i] = *(pEmac_data++); } EMAC_RX_free(); g_rx_pkt_cnt++; g_rx_byte_cnt += num_bytes; g_rx_byte_cnt_last = num_bytes; } } // ------------------------------------------------------------------- int main() { UINT32 res, i, j, crc, num_bytes, num_words, rx_pkt_cnt, tx_pkt_cnt, tx_burst_pkt_cnt, rx_byte_cnt, tx_byte_cnt, pos, size; volatile UINT32 *pEmac_rx_ctrl = (UINT32*)SYS_EMAC_RX_CTRL; volatile UINT32 *pEmac_rx_size = (UINT32*)SYS_EMAC_RX_SIZE; volatile UINT32 *pEmac_tx_ctrl = (UINT32*)SYS_EMAC_TX_CTRL; volatile UINT32 *pEmac_tx_size = (UINT32*)SYS_EMAC_TX_SIZE; volatile UINT32 *pEmac_maddr = (UINT32*)SYS_EMAC_MADDR; volatile UINT8 *pEmac_maddr8 = (UINT8*)SYS_EMAC_MADDR; volatile UINT32 *pEmac_data = (UINT32*)SYS_EMAC_DATA_CACHED; UINT8 buffer[2048]; EMAC_RX_reset(); EMAC_TX_reset(); rx_pkt_cnt = 0; tx_pkt_cnt = 0; rx_byte_cnt = 0; tx_byte_cnt = 0; // Disable promiscious mode // *pEmac_rx_ctrl |= EMAC_RX_CTRL_PROMISCIOUS; *pEmac_rx_ctrl |= EMAC_RX_CTRL_GIGABIT; *pEmac_tx_ctrl |= EMAC_TX_CTRL_GIGABIT; // Set MAC address *(pEmac_maddr+0) = 0x00003110; *(pEmac_maddr+1) = 0x19700000; printf("Own MAC address = "); for (i=0; i < 6; i++) { printf("%02X", pEmac_maddr8[i]); if (i < 5) printf(":"); } printf("\n"); interrupt_register(6, (void*)handler6); interrupt_enable(6); *pEmac_rx_ctrl |= EMAC_RX_CTRL_INTEN; printf("SYS_EMAC_RX_STAT = %08X\n", *pEmac_rx_ctrl); printf("SYS_EMAC_MADDR_0 = %08X\n", *(pEmac_maddr+0)); printf("SYS_EMAC_MADDR_1 = %08X\n", *(pEmac_maddr+1)); rx_pkt_cnt = 0; tx_pkt_cnt = 0; rx_byte_cnt = 0; tx_byte_cnt = 0; g_rx_pkt_cnt = rx_pkt_cnt; g_rx_byte_cnt = rx_byte_cnt; g_rx_byte_cnt_last = 0; while(1) { if (g_rx_pkt_cnt != rx_pkt_cnt) { rx_pkt_cnt = g_rx_pkt_cnt; rx_byte_cnt = g_rx_byte_cnt; printf("Received packet with %d bytes\n", g_rx_byte_cnt_last); printf("%d packets received (%d bytes)\n", rx_pkt_cnt, rx_byte_cnt); printf("\n"); } pos = 0; size = sizeof(mac_dst); memcpy(&buffer[pos], mac_dst, size); pos += size; size = sizeof(mac_src); memcpy(&buffer[pos], mac_src, size); pos += size; size = sizeof(test_data); memcpy(&buffer[pos], test_data, size); pos += size; while(!EMAC_TX_is_idle()); num_bytes = pos; tx_burst_pkt_cnt = 0; for (j=0; j < 100; j++) { EMAC_TX_alloc(num_bytes); num_words = num_bytes >> 2; if (num_bytes & 3) num_words++; if (!EMAC_TX_is_avail()) break; for (i=0; i < num_words; i++) { *pEmac_data = ((UINT32*)buffer)[i]; } EMAC_TX_commit(); tx_pkt_cnt++; tx_byte_cnt += num_bytes; tx_burst_pkt_cnt++; } printf("TX number of burst packets : %d\n", tx_burst_pkt_cnt); // printf("TX data (%d bytes):\n", num_bytes); // PrintBuffer8((UINT8*)buffer, 16, num_bytes); printf("%d packets transmitted (%d bytes)\n\n", tx_pkt_cnt, tx_byte_cnt); } return res; }