【简介】
在之前的帖子中简单介绍了S32K3 的EMAC 的硬件连接,我们在此基础上通过loopback 模式来验证下MAC 的收发功能,验证通过或后再加上PHY来验证以太网功能,以下是S32K3 EMAC 的loopback 模式说明。

Loopback 模式的配置只要在MAC 的配置寄存器中使能即可
EMAC 的loopback模式的使能在S32DS中选中即可。

本地配置好参数后添加如下的测试代码来验证EMAC的loopback功能。
/* Create gmac loopback Task */
#define GMAC_TASK_PRIO 2
#define GMAC_STK_SIZE 256
TaskHandle_t GmacTask_Handler;
static void gmac_task(void *pvParameters)
{
(void)(pvParameters);
Gmac_Ip_StatusType ret;
Gmac_Ip_TxOptionsType TxOptions = {TRUE, GMAC_CRC_AND_PAD_INSERTION, GMAC_CHECKSUM_INSERTION_DISABLE};
Gmac_Ip_BufferType TxBuffer = {0};
Gmac_Ip_BufferType RxBuffer = {0};
Gmac_Ip_TxInfoType TxInfo = {0};
Gmac_Ip_RxInfoType RxInfo = {0};
uint8_t MacAddr[6U] = {0U};
uint8_t i = 0U;
uint8_t j = 0U;
ret = Gmac_Ip_Init(INST_GMAC_0, &Gmac_0_ConfigPB);
if(ret != GMAC_STATUS_SUCCESS)
{
LOG_E("GMAC init failed %x.", ret);
}
else
{
LOG_I("GMAC init OK.");
}
/* Setup the frame with the Mac address and size */
Gmac_Ip_GetMacAddr(INST_GMAC_0, MacAddr);
LOG_I("MAC Address: %02X:%02X:%02X:%02X:%02X:%02X", MacAddr[0], MacAddr[1], MacAddr[2], MacAddr[3], MacAddr[4], MacAddr[5]);
while(1)
{
/* Request a buffer of at least 64 bytes */
TxBuffer.Length = 64U;
if ((GMAC_STATUS_SUCCESS != Gmac_Ip_GetTxBuff(INST_GMAC_0, 0U, &TxBuffer, NULL_PTR)) || (TxBuffer.Length < 64U))
{
LOG_E("Failed to get TX buffer or buffer size is less than 64 bytes.");
}
else
{
LOG_I("TX buffer acquired successfully.");
}
for (i = 0U; i < 12U; i++)
{
*((uint8 *)(&TxBuffer.Data[0U] + i)) = MacAddr[0 + j];
if (j < 5U)
{
j++;
}
else
{
j = 0U;
}
}
/* Payload = Frame - (DstAddr + SrcAddr + EtherType/Length + FCS) */
*((uint32_t *)(TxBuffer.Data + 13U)) = 64U - (6U + 6U + 2U + 4U);
/* Send the ETH frame */
TxBuffer.Length = 64U - 4U; /* Don't count FCS, because it is automatically inserted by the controller in this example */
ret = Gmac_Ip_SendFrame(INST_GMAC_0, 0U, &TxBuffer, &TxOptions);
if (GMAC_STATUS_SUCCESS != ret)
{
LOG_E("Failed to send ETH frame %d.", ret);
}
else
{
LOG_I("ETH frame sent successfully.");
}
/* Wait for the frame to be transmitted */
do {
ret = Gmac_Ip_GetTransmitStatus(INST_GMAC_0, 0U, &TxBuffer, &TxInfo);
} while (ret == GMAC_STATUS_BUSY);
/* Check the frame status */
if ((GMAC_STATUS_SUCCESS != ret) || (0U != TxInfo.ErrMask))
{
LOG_E("Frame transmission failed %d.", ret);
}
else
{
LOG_I("Frame transmitted successfully.");
}
/* Wait for the frame to be received */
do {
ret = Gmac_Ip_ReadFrame(INST_GMAC_0, 0U, &RxBuffer, &RxInfo);
} while (ret == GMAC_STATUS_RX_QUEUE_EMPTY);
/* Check the frame status */
if ((GMAC_STATUS_SUCCESS != ret) || (0U != RxInfo.ErrMask))
{
LOG_E("Frame reception failed %d.", ret);
}
else
{
LOG_I("Frame received successfully.");
}
Gmac_Ip_ProvideRxBuff(INST_GMAC_0, 0U, &RxBuffer);
trace_byte_stream(RxBuffer.Data, RxBuffer.Length,0);
vTaskDelay(1000);
}
}
static int create_gmac_task(void)
{
xTaskCreate((TaskFunction_t )gmac_task,
(const char* )"gmac",
(uint16_t )GMAC_STK_SIZE,
(void* )NULL,
(UBaseType_t )GMAC_TASK_PRIO,
(TaskHandle_t* )&GmacTask_Handler);
return 1;
}上述测试代码运行后MAC 内部已经可以完成数据的自发自收来验证MAC功能。

我要赚赏金
