Skip to content

Corrected STM eth driver flagging, memory allocation and thread init #6241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 34 additions & 18 deletions features/netsocket/emac-drivers/TARGET_STM/stm32xx_emac.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#if MBED_CONF_LWIP_ETHERNET_ENABLED

#include <stdlib.h>
#include <stdlib.h>

#include "cmsis_os.h"

Expand All @@ -9,6 +7,8 @@
#include "mbed_shared_queues.h"
#include "netsocket/nsapi_types.h"

#if DEVICE_EMAC

#include "stm32xx_emac_config.h"
#include "stm32xx_emac.h"

Expand Down Expand Up @@ -69,7 +69,9 @@ void ETH_IRQHandler(void);
void HAL_ETH_RxCpltCallback(ETH_HandleTypeDef *heth)
{
STM32_EMAC &emac = STM32_EMAC::get_instance();
osThreadFlagsSet(emac.thread, FLAG_RX);
if (emac.thread) {
osThreadFlagsSet(emac.thread, FLAG_RX);
}
}

/**
Expand All @@ -85,6 +87,7 @@ void ETH_IRQHandler(void)
}

STM32_EMAC::STM32_EMAC()
: thread(0)
{
}

Expand Down Expand Up @@ -247,8 +250,10 @@ emac_mem_buf_t *STM32_EMAC::low_level_input()
uint16_t len = 0;
uint8_t *buffer;
__IO ETH_DMADescTypeDef *dmarxdesc;
uint32_t bufferoffset = 0;
uint32_t byteslefttocopy = 0;
emac_mem_buf_t *buf = 0;
emac_mem_buf_t *q;
uint32_t payloadoffset = 0;

/* get received frame */
Expand All @@ -264,23 +269,34 @@ emac_mem_buf_t *STM32_EMAC::low_level_input()
dmarxdesc = EthHandle.RxFrameInfos.FSRxDesc;

if (len > 0) {
/* Allocate a contiguous memory buffer, if not available drop incoming frame */
buf = memory_manager->alloc_heap(len, 0);
/* Allocate a memory buffer chain from buffer pool */
buf = memory_manager->alloc_pool(len, 0);
}

if (buf) {
/* Check if the length of bytes to copy in current memory buffer is bigger than Rx buffer size*/
while (byteslefttocopy > ETH_RX_BUF_SIZE) {
memcpy(static_cast<uint8_t *>(memory_manager->get_ptr(buf)) + payloadoffset, buffer, ETH_RX_BUF_SIZE);

/* Point to next descriptor */
dmarxdesc = reinterpret_cast<ETH_DMADescTypeDef *>(dmarxdesc->Buffer2NextDescAddr);
buffer = reinterpret_cast<uint8_t *>(dmarxdesc->Buffer1Addr);

byteslefttocopy = byteslefttocopy - ETH_RX_BUF_SIZE;
payloadoffset = payloadoffset + ETH_RX_BUF_SIZE;
if (buf != NULL) {
dmarxdesc = EthHandle.RxFrameInfos.FSRxDesc;
bufferoffset = 0;
for (q = buf; q != NULL; q = memory_manager->get_next(q)) {
byteslefttocopy = memory_manager->get_len(q);
payloadoffset = 0;

/* Check if the length of bytes to copy in current pbuf is bigger than Rx buffer size*/
while ((byteslefttocopy + bufferoffset) > ETH_RX_BUF_SIZE) {
/* Copy data to pbuf */
memcpy(static_cast<uint8_t *>(memory_manager->get_ptr(q)) + payloadoffset, static_cast<uint8_t *>(buffer) + bufferoffset, ETH_RX_BUF_SIZE - bufferoffset);

/* Point to next descriptor */
dmarxdesc = reinterpret_cast<ETH_DMADescTypeDef *>(dmarxdesc->Buffer2NextDescAddr);
buffer = reinterpret_cast<uint8_t *>(dmarxdesc->Buffer1Addr);

byteslefttocopy = byteslefttocopy - (ETH_RX_BUF_SIZE - bufferoffset);
payloadoffset = payloadoffset + (ETH_RX_BUF_SIZE - bufferoffset);
bufferoffset = 0;
}
/* Copy remaining data in pbuf */
memcpy(static_cast<uint8_t *>(memory_manager->get_ptr(q)) + payloadoffset, static_cast<uint8_t *>(buffer) + bufferoffset, byteslefttocopy);
bufferoffset = bufferoffset + byteslefttocopy;
}
memcpy(static_cast<uint8_t *>(memory_manager->get_ptr(buf)) + payloadoffset, buffer, byteslefttocopy);
}

/* Release descriptors to DMA */
Expand Down