Skip to content

Commit 43e7054

Browse files
Mika Leppänenkjbracey
authored andcommitted
Corrected STM eth driver flagging, memory allocation and thread init
1 parent c38e211 commit 43e7054

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

features/netsocket/emac-drivers/TARGET_STM/stm32xx_emac.cpp

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#if MBED_CONF_LWIP_ETHERNET_ENABLED
2-
3-
#include <stdlib.h>
1+
#include <stdlib.h>
42

53
#include "cmsis_os.h"
64

@@ -9,6 +7,8 @@
97
#include "mbed_shared_queues.h"
108
#include "netsocket/nsapi_types.h"
119

10+
#if DEVICE_EMAC
11+
1212
#include "stm32xx_emac_config.h"
1313
#include "stm32xx_emac.h"
1414

@@ -69,7 +69,9 @@ void ETH_IRQHandler(void);
6969
void HAL_ETH_RxCpltCallback(ETH_HandleTypeDef *heth)
7070
{
7171
STM32_EMAC &emac = STM32_EMAC::get_instance();
72-
osThreadFlagsSet(emac.thread, FLAG_RX);
72+
if (emac.thread) {
73+
osThreadFlagsSet(emac.thread, FLAG_RX);
74+
}
7375
}
7476

7577
/**
@@ -85,6 +87,7 @@ void ETH_IRQHandler(void)
8587
}
8688

8789
STM32_EMAC::STM32_EMAC()
90+
: thread(0)
8891
{
8992
}
9093

@@ -247,8 +250,10 @@ emac_mem_buf_t *STM32_EMAC::low_level_input()
247250
uint16_t len = 0;
248251
uint8_t *buffer;
249252
__IO ETH_DMADescTypeDef *dmarxdesc;
253+
uint32_t bufferoffset = 0;
250254
uint32_t byteslefttocopy = 0;
251255
emac_mem_buf_t *buf = 0;
256+
emac_mem_buf_t *q;
252257
uint32_t payloadoffset = 0;
253258

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

266271
if (len > 0) {
267-
/* Allocate a contiguous memory buffer, if not available drop incoming frame */
268-
buf = memory_manager->alloc_heap(len, 0);
272+
/* Allocate a memory buffer chain from buffer pool */
273+
buf = memory_manager->alloc_pool(len, 0);
269274
}
270275

271-
if (buf) {
272-
/* Check if the length of bytes to copy in current memory buffer is bigger than Rx buffer size*/
273-
while (byteslefttocopy > ETH_RX_BUF_SIZE) {
274-
memcpy(static_cast<uint8_t *>(memory_manager->get_ptr(buf)) + payloadoffset, buffer, ETH_RX_BUF_SIZE);
275-
276-
/* Point to next descriptor */
277-
dmarxdesc = reinterpret_cast<ETH_DMADescTypeDef *>(dmarxdesc->Buffer2NextDescAddr);
278-
buffer = reinterpret_cast<uint8_t *>(dmarxdesc->Buffer1Addr);
279-
280-
byteslefttocopy = byteslefttocopy - ETH_RX_BUF_SIZE;
281-
payloadoffset = payloadoffset + ETH_RX_BUF_SIZE;
276+
if (buf != NULL) {
277+
dmarxdesc = EthHandle.RxFrameInfos.FSRxDesc;
278+
bufferoffset = 0;
279+
for (q = buf; q != NULL; q = memory_manager->get_next(q)) {
280+
byteslefttocopy = memory_manager->get_len(q);
281+
payloadoffset = 0;
282+
283+
/* Check if the length of bytes to copy in current pbuf is bigger than Rx buffer size*/
284+
while ((byteslefttocopy + bufferoffset) > ETH_RX_BUF_SIZE) {
285+
/* Copy data to pbuf */
286+
memcpy(static_cast<uint8_t *>(memory_manager->get_ptr(q)) + payloadoffset, static_cast<uint8_t *>(buffer) + bufferoffset, ETH_RX_BUF_SIZE - bufferoffset);
287+
288+
/* Point to next descriptor */
289+
dmarxdesc = reinterpret_cast<ETH_DMADescTypeDef *>(dmarxdesc->Buffer2NextDescAddr);
290+
buffer = reinterpret_cast<uint8_t *>(dmarxdesc->Buffer1Addr);
291+
292+
byteslefttocopy = byteslefttocopy - (ETH_RX_BUF_SIZE - bufferoffset);
293+
payloadoffset = payloadoffset + (ETH_RX_BUF_SIZE - bufferoffset);
294+
bufferoffset = 0;
295+
}
296+
/* Copy remaining data in pbuf */
297+
memcpy(static_cast<uint8_t *>(memory_manager->get_ptr(q)) + payloadoffset, static_cast<uint8_t *>(buffer) + bufferoffset, byteslefttocopy);
298+
bufferoffset = bufferoffset + byteslefttocopy;
282299
}
283-
memcpy(static_cast<uint8_t *>(memory_manager->get_ptr(buf)) + payloadoffset, buffer, byteslefttocopy);
284300
}
285301

286302
/* Release descriptors to DMA */

0 commit comments

Comments
 (0)