2023-05-03 14:01:32 +03:00
|
|
|
|
/*
|
|
|
|
|
|
* eth_rx_flow.cpp
|
|
|
|
|
|
*
|
2024-02-21 10:41:56 +03:00
|
|
|
|
* Created on: 7 мар. 2023 г.
|
2023-05-03 14:01:32 +03:00
|
|
|
|
* Author: sychev
|
|
|
|
|
|
*/
|
2023-06-26 18:22:30 +03:00
|
|
|
|
#include "free_rtos/ethernet/eth_rx_flow.hpp"
|
2023-05-03 14:01:32 +03:00
|
|
|
|
|
|
|
|
|
|
#include <networking/enet/core/include/core/enet_soc.h>
|
2023-09-27 15:37:10 +03:00
|
|
|
|
#include <networking/enet/core/src/dma/udma/enet_udma_priv.h> // Yes, it is forbidden !
|
2023-05-03 14:01:32 +03:00
|
|
|
|
#include <networking/enet/utils/include/enet_board.h>
|
|
|
|
|
|
#include <networking/enet/utils/include/enet_appmemutils.h>
|
|
|
|
|
|
#include <networking/enet/utils/include/enet_appmemutils_cfg.h>
|
|
|
|
|
|
#include <networking/enet/utils/include/enet_apputils.h>
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------*/
|
|
|
|
|
|
#include "ti_enet_config.h"
|
|
|
|
|
|
/*----------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
|
|
using namespace free_rtos;
|
|
|
|
|
|
|
|
|
|
|
|
void free_rtos::rxIsrHandler(void *appData)
|
|
|
|
|
|
{
|
|
|
|
|
|
EthRxFlow * rx_flow = (EthRxFlow *)appData;
|
2023-09-26 10:09:59 +03:00
|
|
|
|
|
2023-09-26 16:29:18 +03:00
|
|
|
|
if(rx_flow == nullptr) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2023-09-26 10:09:59 +03:00
|
|
|
|
|
2024-02-16 17:06:59 +03:00
|
|
|
|
rx_flow->retrieveReadyRxPktQ();
|
2023-09-28 14:13:26 +03:00
|
|
|
|
rx_flow->sem_[EthRxFlow::e_signalRxPkt].post();
|
2023-05-03 14:01:32 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void free_rtos::rxTaskHandler(void *appData)
|
|
|
|
|
|
{
|
|
|
|
|
|
EthRxFlow * rx_flow = (EthRxFlow *)appData;
|
|
|
|
|
|
if (rx_flow != nullptr) {
|
|
|
|
|
|
rx_flow->rxProcessPktTask();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-22 10:59:11 +03:00
|
|
|
|
free_rtos::EthRxFlow::EthRxFlow(TEthFrameMacAddr& mac_addr, EthStackIface& eth_stack) :
|
|
|
|
|
|
mac_addr_{mac_addr},
|
|
|
|
|
|
eth_stack_{eth_stack},
|
|
|
|
|
|
passive_mode_{false}
|
|
|
|
|
|
{
|
2024-02-16 17:06:59 +03:00
|
|
|
|
EnetQueue_initQ(&rx_ready_pktq_);
|
2023-09-22 10:59:11 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-27 13:20:33 +03:00
|
|
|
|
void free_rtos::EthRxFlow::initRxFreePktQ(void * appPriv, EnetDma_PktQ * p_packet_queue, uint32_t qCount)
|
2023-05-03 14:01:32 +03:00
|
|
|
|
{
|
|
|
|
|
|
EnetDma_PktQ rxReadyQ;
|
|
|
|
|
|
EnetDma_Pkt *pPktInfo;
|
|
|
|
|
|
uint32_t i;
|
|
|
|
|
|
int32_t status;
|
2024-07-02 14:07:24 +03:00
|
|
|
|
uint32_t scatterSegmentSize[1] = {ENET_MEM_LARGE_POOL_PKT_SIZE};
|
2023-05-03 14:01:32 +03:00
|
|
|
|
|
2023-09-22 10:59:11 +03:00
|
|
|
|
for (i = 0U; i < qCount; ++i)
|
2023-05-03 14:01:32 +03:00
|
|
|
|
{
|
|
|
|
|
|
pPktInfo = EnetMem_allocEthPkt(appPriv,
|
2024-07-02 14:07:24 +03:00
|
|
|
|
ENETDMA_CACHELINE_ALIGNMENT,
|
|
|
|
|
|
1,
|
|
|
|
|
|
scatterSegmentSize);
|
2023-05-03 14:01:32 +03:00
|
|
|
|
EnetAppUtils_assert(pPktInfo != NULL);
|
|
|
|
|
|
|
|
|
|
|
|
ENET_UTILS_SET_PKT_APP_STATE(&pPktInfo->pktState, ENET_PKTSTATE_APP_WITH_FREEQ);
|
|
|
|
|
|
|
2023-10-27 13:20:33 +03:00
|
|
|
|
EnetQueue_enq(p_packet_queue, &pPktInfo->node);
|
2023-05-03 14:01:32 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Retrieve any packets which are ready */
|
|
|
|
|
|
EnetQueue_initQ(&rxReadyQ);
|
2023-10-27 13:20:33 +03:00
|
|
|
|
|
2023-09-22 10:59:11 +03:00
|
|
|
|
status = EnetDma_retrieveRxPktQ(dma_handle_, &rxReadyQ);
|
2023-05-03 14:01:32 +03:00
|
|
|
|
EnetAppUtils_assert(status == ENET_SOK);
|
|
|
|
|
|
|
|
|
|
|
|
/* There should not be any packet with DMA during init */
|
|
|
|
|
|
EnetAppUtils_assert(EnetQueue_getQCount(&rxReadyQ) == 0U);
|
2023-09-22 10:59:11 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-16 17:06:59 +03:00
|
|
|
|
void free_rtos::EthRxFlow::retrieveReadyRxPktQ() {
|
|
|
|
|
|
int32_t status = EnetDma_retrieveRxPktQ(dma_handle_, &rx_ready_pktq_);
|
|
|
|
|
|
EnetAppUtils_assert(status == ENET_SOK);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void free_rtos::EthRxFlow::submitFreeRxPktQ(EnetDma_PktQ * p_packet_queue)
|
2023-09-22 10:59:11 +03:00
|
|
|
|
{
|
2023-09-25 14:58:34 +03:00
|
|
|
|
int32_t status;
|
2024-02-16 17:06:59 +03:00
|
|
|
|
uint32_t qCount;
|
2023-09-22 10:59:11 +03:00
|
|
|
|
|
2024-02-16 17:06:59 +03:00
|
|
|
|
EnetAppUtils_validatePacketState(p_packet_queue,
|
2023-05-03 14:01:32 +03:00
|
|
|
|
ENET_PKTSTATE_APP_WITH_FREEQ,
|
|
|
|
|
|
ENET_PKTSTATE_APP_WITH_DRIVER);
|
|
|
|
|
|
|
2024-02-16 17:06:59 +03:00
|
|
|
|
status = EnetDma_submitRxPktQ(dma_handle_, p_packet_queue);
|
2023-10-27 13:20:33 +03:00
|
|
|
|
EnetAppUtils_assert(status == ENET_SOK);
|
2023-09-25 14:58:34 +03:00
|
|
|
|
|
2024-02-16 17:06:59 +03:00
|
|
|
|
qCount = EnetQueue_getQCount(p_packet_queue);
|
2023-05-03 14:01:32 +03:00
|
|
|
|
/* Assert here, as during init, the number of DMA descriptors should be equal to
|
2023-10-27 13:20:33 +03:00
|
|
|
|
* the number of free Ethernet buffers available with app */
|
|
|
|
|
|
EnetAppUtils_assert(qCount == 0U);
|
2023-05-03 14:01:32 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void free_rtos::EthRxFlow::rxProcessPktTask()
|
|
|
|
|
|
{
|
2024-02-16 17:06:59 +03:00
|
|
|
|
EnetDma_Pkt* rxPktInfo;
|
|
|
|
|
|
EnetDma_PktQ rxFreeQ;
|
2023-05-03 14:01:32 +03:00
|
|
|
|
int32_t status;
|
|
|
|
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
|
|
{
|
2024-02-16 17:06:59 +03:00
|
|
|
|
status = sem_[e_signalRxPkt].pend();
|
2023-11-01 11:48:36 +03:00
|
|
|
|
|
|
|
|
|
|
if(status != SystemP_SUCCESS) {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2023-09-25 14:58:34 +03:00
|
|
|
|
|
2024-02-16 17:06:59 +03:00
|
|
|
|
EnetQueue_initQ(&rxFreeQ);
|
|
|
|
|
|
rxPktInfo = (EnetDma_Pkt *)EnetQueue_deq(&rx_ready_pktq_);
|
2023-05-03 14:01:32 +03:00
|
|
|
|
|
2024-02-16 17:06:59 +03:00
|
|
|
|
while(rxPktInfo != nullptr) {
|
|
|
|
|
|
//if (!passive_mode_) {
|
|
|
|
|
|
for (int j = 0 ; j < rxPktInfo->sgList.numScatterSegments; ++j)
|
|
|
|
|
|
{
|
|
|
|
|
|
eth_stack_.rx_handler(rxPktInfo->sgList.list[j].bufPtr, rxPktInfo->sgList.list[j].segmentFilledLen);
|
|
|
|
|
|
}
|
|
|
|
|
|
//}
|
2023-05-03 14:01:32 +03:00
|
|
|
|
|
2024-02-16 17:06:59 +03:00
|
|
|
|
EnetDma_checkPktState( &rxPktInfo->pktState,
|
|
|
|
|
|
ENET_PKTSTATE_MODULE_APP,
|
|
|
|
|
|
ENET_PKTSTATE_APP_WITH_DRIVER,
|
|
|
|
|
|
ENET_PKTSTATE_APP_WITH_FREEQ);
|
2023-05-03 14:01:32 +03:00
|
|
|
|
|
2024-02-16 17:06:59 +03:00
|
|
|
|
EnetQueue_enq(&rxFreeQ, &rxPktInfo->node);
|
2023-05-03 14:01:32 +03:00
|
|
|
|
|
2024-02-16 17:06:59 +03:00
|
|
|
|
rxPktInfo = (EnetDma_Pkt *)EnetQueue_deq(&rx_ready_pktq_);
|
|
|
|
|
|
}
|
2023-10-27 13:20:33 +03:00
|
|
|
|
|
2024-02-16 17:06:59 +03:00
|
|
|
|
submitFreeRxPktQ(&rxFreeQ);
|
2023-05-03 14:01:32 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-16 17:06:59 +03:00
|
|
|
|
bool free_rtos::EthRxFlow::open(TEthMacPorts port_id, int32_t enetDmaRxChId, UBaseType_t task_priority, UBaseType_t task_stack_size)
|
2023-05-03 14:01:32 +03:00
|
|
|
|
{
|
|
|
|
|
|
EnetApp_GetDmaHandleInArgs rxInArgs;
|
|
|
|
|
|
EnetApp_GetRxDmaHandleOutArgs rxChInfo;
|
|
|
|
|
|
|
2024-01-19 14:57:14 +03:00
|
|
|
|
constexpr uint32_t numPkts[2] = {ENET_DMA_RX_CH0_NUM_PKTS, ENET_DMA_RX_CH1_NUM_PKTS};
|
|
|
|
|
|
|
2024-02-16 17:06:59 +03:00
|
|
|
|
EnetAppUtils_print("rx_flow %u: opening flow...\r\n", port_id);
|
2023-05-03 14:01:32 +03:00
|
|
|
|
|
|
|
|
|
|
if (open_) {
|
2024-02-16 17:06:59 +03:00
|
|
|
|
EnetAppUtils_print("rx_flow %u: rx flow is already open. Do nothing.\r\n", port_id);
|
2023-05-03 14:01:32 +03:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-22 10:59:11 +03:00
|
|
|
|
if (!rx_task.Create("rx_task", task_priority, rxTaskHandler, this, task_stack_size))
|
2023-05-03 14:01:32 +03:00
|
|
|
|
{
|
2024-02-16 17:06:59 +03:00
|
|
|
|
EnetAppUtils_print("rx_flow %u: failed to create rx task.\r\n", port_id);
|
2023-05-03 14:01:32 +03:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-16 17:06:59 +03:00
|
|
|
|
default_port_id_ = port_id;
|
2023-11-01 11:48:36 +03:00
|
|
|
|
enetDmaRxChId_ = enetDmaRxChId;
|
2023-05-03 14:01:32 +03:00
|
|
|
|
|
2024-02-21 10:41:56 +03:00
|
|
|
|
// Регистрация обработчика прерывания модуля DMA
|
|
|
|
|
|
rxInArgs.notifyCb = rxIsrHandler;
|
|
|
|
|
|
rxInArgs.cbArg = this;
|
2023-05-03 14:01:32 +03:00
|
|
|
|
|
|
|
|
|
|
EnetApp_getRxDmaHandle(enetDmaRxChId, &rxInArgs, &rxChInfo);
|
|
|
|
|
|
|
|
|
|
|
|
rx_start_flow_idx_ = rxChInfo.rxFlowStartIdx;
|
|
|
|
|
|
rx_flow_idx_ = rxChInfo.rxFlowIdx;
|
|
|
|
|
|
dma_handle_ = rxChInfo.hRxCh;
|
|
|
|
|
|
|
2024-07-02 14:07:24 +03:00
|
|
|
|
if (rxChInfo.numValidMacAddress != 0)
|
2023-05-03 14:01:32 +03:00
|
|
|
|
{
|
2024-07-02 14:07:24 +03:00
|
|
|
|
EnetUtils_copyMacAddr(mac_addr_.bytes, rxChInfo.macAddr[0]);
|
2023-05-03 14:01:32 +03:00
|
|
|
|
mac_addr_.addr&= ETH_FRAME_MAC_ADDR_MASK;
|
|
|
|
|
|
|
|
|
|
|
|
eth_stack_.set_mac_address(mac_addr_.addr);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EnetAppUtils_assert(rxChInfo.useGlobalEvt == true);
|
|
|
|
|
|
EnetAppUtils_assert(rxChInfo.sizeThreshEn == 0U);
|
2024-01-19 14:57:14 +03:00
|
|
|
|
EnetAppUtils_assert(rxChInfo.maxNumRxPkts >= numPkts[enetDmaRxChId]);
|
2024-02-16 17:06:59 +03:00
|
|
|
|
EnetAppUtils_assert(rxChInfo.chIdx == port_id);
|
2023-05-03 14:01:32 +03:00
|
|
|
|
|
|
|
|
|
|
if (dma_handle_ == nullptr)
|
|
|
|
|
|
{
|
2024-02-16 17:06:59 +03:00
|
|
|
|
EnetAppUtils_print("rx_flow %u: failed to open rx dma flow\r\n", port_id);
|
2023-05-03 14:01:32 +03:00
|
|
|
|
EnetAppUtils_assert(dma_handle_ != nullptr);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-16 17:06:59 +03:00
|
|
|
|
EnetDma_PktQ rxFreeQ;
|
|
|
|
|
|
|
|
|
|
|
|
EnetQueue_initQ(&rxFreeQ);
|
|
|
|
|
|
|
|
|
|
|
|
initRxFreePktQ(this, &rxFreeQ, numPkts[enetDmaRxChId]);
|
|
|
|
|
|
submitFreeRxPktQ(&rxFreeQ);
|
2023-05-03 14:01:32 +03:00
|
|
|
|
|
|
|
|
|
|
open_ = true;
|
|
|
|
|
|
|
2024-02-16 17:06:59 +03:00
|
|
|
|
EnetAppUtils_print("rx_flow %u: rx flow open successfully\r\n", port_id);
|
2023-05-03 14:01:32 +03:00
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|