Skip to content

Commit 635eb86

Browse files
author
Jamie Smith
authored
EMAC memory manager improvements & fixes (#426)
* EMAC memory manager improvements & fixes * Few fixes * Stop targets from changing TCP settings * Don't use any default network stack in greentea tests * Fix baremetal build issues * Link LwIP for EMW3080B * Use correct section for Nanostack heap on LPC1768 * Use minimal Nanostack by default * Set up link order correctly (I hope) * Fix assert failure * Formatting * Fix build for LPC1768 * Set default test EMAC pool size * AHBSRAM1 -> AHBSRAM * Fix license
1 parent 6184722 commit 635eb86

File tree

57 files changed

+600
-543
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+600
-543
lines changed

.github/workflows/greentea_cmake.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ jobs:
146146
# Note: We have to set a wifi network name and password so that the test will compile on devices that use wifi
147147
run: |
148148
rm -rf __build
149-
cmake -S . -B __build -GNinja -DUPLOAD_METHOD=NONE -DMBED_GREENTEA_WIFI_SECURE_SSID=SomeNetwork -DMBED_GREENTEA_WIFI_SECURE_PASSWORD=SomePassword -DCMAKE_CTEST_ARGUMENTS="--output-on-failure;-V" -DMBED_BUILD_GREENTEA_TESTS=ON -DMBED_GREENTEA_TEST_BAREMETAL=ON -DMBED_GREENTEA_SERIAL_PORT=/dev/ttyDUMMY -DMBED_TARGET=${{ matrix.target }} -DMBED_APP_JSON_PATH=TESTS/configs/greentea_baremetal.json5
149+
cmake -S . -B __build -GNinja -DUPLOAD_METHOD=NONE -DMBED_GREENTEA_WIFI_SECURE_SSID=SomeNetwork -DMBED_GREENTEA_WIFI_SECURE_PASSWORD=SomePassword -DCMAKE_CTEST_ARGUMENTS="--output-on-failure;-V" -DMBED_BUILD_GREENTEA_TESTS=ON -DMBED_GREENTEA_SERIAL_PORT=/dev/ttyDUMMY -DMBED_TARGET=${{ matrix.target }} -DMBED_APP_JSON_PATH=TESTS/configs/greentea_baremetal.json5
150150
cmake --build __build
151151
152152
- name: Build ${{ matrix.target }} with full profile

TESTS/configs/greentea_full.json5

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
"mbed-trace.enable": true,
1919

2020
// Disable colored traces in tests, as the test runner does not like the terminal control chars
21-
"mbed-trace.default-config": "TRACE_ACTIVE_LEVEL_INFO | TRACE_CARRIAGE_RETURN"
21+
"mbed-trace.default-config": "TRACE_ACTIVE_LEVEL_INFO | TRACE_CARRIAGE_RETURN",
22+
23+
// Don't use any network stack as the default, so that we can manually select which one
24+
// is used in the tests.
25+
"nsapi.default-stack": null,
26+
27+
// Configure Nanostack for Ethernet mode only by default. This is sufficient for our tests and is
28+
// needed because not every target has enough flash space for all of Nanostack (it can be more than 400kiB when you include the Mbed TLS deps it pulls in!)
29+
"nanostack.configuration": "ethernet_host"
2230
}
2331
}

connectivity/drivers/emac/TARGET_NXP_EMAC/TARGET_LPCTarget/lpc17_emac.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*
1010
* Copyright(C) 2011, NXP Semiconductor
1111
* All rights reserved.
12+
* SPDX-License-Identifier: Apache-2.0
1213
*
1314
***********************************************************************
1415
* Software that is described herein is for illustrative purposes only
@@ -127,7 +128,7 @@ struct lpc_enetdata {
127128

128129
#if defined(TARGET_LPC17XX)
129130
# if defined(TOOLCHAIN_GCC_ARM) || defined(TOOLCHAIN_ARM)
130-
# define ETHMEM_SECTION __attribute__((section("AHBSRAM1"),aligned))
131+
# define ETHMEM_SECTION __attribute__((section("AHBSRAM"),aligned))
131132
# endif
132133
#endif
133134

connectivity/drivers/wifi/TARGET_STM/COMPONENT_EMW3080B/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ target_sources(mbed-wifi
2222
mx_wifi/core/mx_wifi_slip.c
2323
)
2424

25+
# This driver needs utility functions from LwIP
26+
target_link_libraries(mbed-wifi
27+
PRIVATE
28+
mbed-lwipstack
29+
)
30+
2531
# Link override object file coming from static library anyway
2632
#
2733
# NOTE: This linker option is to pretend undefined symbol and won't cause

connectivity/drivers/wifi/TARGET_STM/COMPONENT_EMW3080B/mx_wifi_mbed_os.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void mx_buf_set_size(mx_buf_t *p, int32_t n)
6060

6161
mx_buf_t *mx_buf_alloc(uint32_t len)
6262
{
63-
emac_mem_buf_t *p = emac3080b_global_memory_manager->alloc_pool(len, 0);
63+
emac_mem_buf_t *p = emac3080b_global_memory_manager->alloc_heap(len, 0);
6464
return (mx_buf_t *) p;
6565
}
6666

connectivity/libraries/nanostack-libservice/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ target_sources(mbed-nanostack-libservice
2222
source/nvmHelper/ns_nvm_helper.c
2323
)
2424

25+
target_link_libraries(mbed-nanostack-libservice
26+
PUBLIC
27+
mbed-core-flags)
28+
2529
# The definition, source files and include directories below
2630
# are needed by mbed-trace which is part of the mbed-core CMake target
2731
target_compile_definitions(mbed-core-flags

connectivity/libraries/nanostack-libservice/source/nsdynmemLIB/nsdynmemLIB.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "platform/arm_hal_interrupt.h"
2222
#include <stdlib.h>
2323
#include "ns_list.h"
24+
#include "mbed_toolchain.h"
2425

2526
#ifndef STANDARD_MALLOC
2627
typedef enum mem_stat_update_t {
@@ -598,6 +599,10 @@ static bool pointer_address_validate(ns_mem_book_t *book, ns_mem_word_size_t *pt
598599
return false;
599600
}
600601

602+
// Hook from nanostack to the Mbed EMAC memory manager when memory becomes free.
603+
MBED_WEAK void mbed_ns_heap_free_hook(void)
604+
{}
605+
601606
void ns_mem_free(ns_mem_book_t *book, void *block)
602607
{
603608
#ifndef STANDARD_MALLOC
@@ -633,6 +638,8 @@ void ns_mem_free(ns_mem_book_t *book, void *block)
633638
free(block);
634639
platform_exit_critical();
635640
#endif
641+
642+
mbed_ns_heap_free_hook();
636643
}
637644

638645
void ns_dyn_mem_free(void *block)

connectivity/lwipstack/include/lwipstack/LWIPMemoryManager.h

Lines changed: 10 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -19,151 +19,47 @@
1919

2020
#include "EMACMemoryManager.h"
2121

22+
// Hook called from LwIP whenever a pbuf chain containing at least one pool pbuf has been freed
23+
extern "C" void mbed_lwip_on_pbuf_pool_free_hook();
2224

2325
class LWIPMemoryManager final : public EMACMemoryManager {
2426
public:
2527

26-
/**
27-
* Allocates memory buffer from the heap
28-
*
29-
* Memory buffer allocated from heap is always contiguous and can be arbitrary size.
30-
*
31-
* @param size Size of the memory to allocate in bytes
32-
* @param align Memory alignment requirement in bytes
33-
* @return Allocated memory buffer, or NULL in case of error
34-
*/
3528
net_stack_mem_buf_t *alloc_heap(uint32_t size, uint32_t align) override;
3629

37-
/**
38-
* Allocates memory buffer chain from a pool
39-
*
40-
* Memory allocated from pool is contiguous if size is equal or less than
41-
* (aligned) allocation unit, otherwise may be chained. Will typically come from
42-
* fixed-size packet pool memory.
43-
*
44-
* @param size Total size of the memory to allocate in bytes
45-
* @param align Memory alignment requirement for each buffer in bytes
46-
* @return Allocated memory buffer chain, or NULL in case of error
47-
*/
4830
net_stack_mem_buf_t *alloc_pool(uint32_t size, uint32_t align) override;
4931

50-
/**
51-
* Get memory buffer pool allocation unit
52-
*
53-
* Returns the maximum size of contiguous memory that can be allocated from a pool.
54-
*
55-
* @param align Memory alignment requirement in bytes
56-
* @return Contiguous memory size
57-
*/
5832
uint32_t get_pool_alloc_unit(uint32_t align) const override;
5933

60-
/**
61-
* Free memory buffer chain
62-
*
63-
* If memory buffer is chained must point to the start of the chain. Frees all buffers
64-
* from the chained list.
65-
*
66-
* @param buf Memory buffer chain to be freed.
67-
*/
34+
uint32_t get_pool_size() const override;
35+
6836
void free(net_stack_mem_buf_t *buf) override;
6937

70-
/**
71-
* Return total length of a memory buffer chain
72-
*
73-
* Returns a total length of this buffer and any following buffers in the chain.
74-
*
75-
* @param buf Memory buffer chain
76-
* @return Total length in bytes
77-
*/
7838
uint32_t get_total_len(const net_stack_mem_buf_t *buf) const override;
7939

80-
/**
81-
* Copy a memory buffer chain
82-
*
83-
* Copies data from one buffer chain to another. Copy operation does not adjust the lengths
84-
* of the copied-to memory buffer chain, so chain total lengths must be the same.
85-
*
86-
* @param to_buf Memory buffer chain to copy to
87-
* @param from_buf Memory buffer chain to copy from
88-
*/
8940
void copy(net_stack_mem_buf_t *to_buf, const net_stack_mem_buf_t *from_buf) override;
9041

91-
/**
92-
* Copy to a memory buffer chain
93-
*
94-
* Copies data to a buffer chain. Copy operation does not adjust the lengths
95-
* of the copied-to memory buffer chain, so chain total length must match the
96-
* copied length.
97-
*
98-
* @param to_buf Memory buffer chain to copy to
99-
* @param ptr Pointer to data
100-
* @param len Data length
101-
*/
10242
void copy_to_buf(net_stack_mem_buf_t *to_buf, const void *ptr, uint32_t len) override;
10343

104-
/**
105-
* Copy from a memory buffer chain
106-
*
107-
* Copies data from a memory buffer chain.
108-
*
109-
* @param len Data length
110-
* @param ptr Pointer to data
111-
* @param from_buf Memory buffer chain to copy from
112-
* @return Length of the data that was copied
113-
*/
11444
uint32_t copy_from_buf(void *ptr, uint32_t len, const net_stack_mem_buf_t *from_buf) const override;
11545

116-
/**
117-
* Concatenate two memory buffer chains
118-
*
119-
* Concatenates buffer chain to end of the other buffer chain. Concatenated-to buffer total length
120-
* is adjusted accordingly. cat_buf must point to the start of a the chain. After concatenation
121-
* to_buf's chain now owns those buffers, and they will be freed when the to_buf chain is freed.
122-
*
123-
* @param to_buf Memory buffer chain to concatenate to
124-
* @param cat_buf Memory buffer chain to concatenate
125-
*/
12646
void cat(net_stack_mem_buf_t *to_buf, net_stack_mem_buf_t *cat_buf) override;
12747

128-
/**
129-
* Returns the next buffer
130-
*
131-
* Returns the next buffer from the memory buffer chain.
132-
*
133-
* @param buf Memory buffer
134-
* @return The next memory buffer, or NULL if last
135-
*/
13648
net_stack_mem_buf_t *get_next(const net_stack_mem_buf_t *buf) const override;
13749

138-
/**
139-
* Return pointer to the payload of the buffer
140-
*
141-
* @param buf Memory buffer
142-
* @return Pointer to the payload
143-
*/
14450
void *get_ptr(const net_stack_mem_buf_t *buf) const override;
14551

146-
/**
147-
* Return payload size of the buffer
148-
*
149-
* @param buf Memory buffer
150-
* @return Size in bytes
151-
*/
15252
uint32_t get_len(const net_stack_mem_buf_t *buf) const override;
15353

154-
/**
155-
* Sets the payload size of the buffer
156-
*
157-
* The allocated payload size will not change. It is not permitted
158-
* to change the length of a buffer that is not the first (or only) in a chain.
159-
*
160-
* @param buf Memory buffer
161-
* @param len Payload size, must be less or equal allocated size
162-
*/
16354
void set_len(net_stack_mem_buf_t *buf, uint32_t len) override;
16455

56+
Lifetime get_lifetime(const net_stack_mem_buf_t *buf) const override;
57+
16558
private:
16659

60+
// Allow callback to access private vars
61+
friend void mbed_lwip_on_pbuf_pool_free_hook();
62+
16763
/**
16864
* Returns a total memory alignment size
16965
*
@@ -192,7 +88,7 @@ class LWIPMemoryManager final : public EMACMemoryManager {
19288
* Sets total lengths of a memory buffer chain
19389
*
19490
* Sets total length fields for a memory buffer chain based on buffer
195-
* length fields. All total lengths are calculated again.
91+
* \c len fields. All total lengths are calculated again.
19692
*
19793
* @param pbuf Memory buffer
19894
*/

connectivity/lwipstack/include/lwipstack/LWIPStack.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,12 @@ class LWIP : public OnboardNetworkStack, private mbed::NonCopyable<LWIP> {
344344
*/
345345
void set_default_interface(OnboardNetworkStack::Interface *interface) override;
346346

347+
/// Get the memory manager for the LwIP stack
348+
LWIPMemoryManager &get_memory_manager()
349+
{
350+
return memory_manager;
351+
}
352+
347353
protected:
348354
LWIP();
349355

connectivity/lwipstack/include/lwipstack/lwipopts.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* Copyright (C) 2012 mbed.org, MIT License
2+
* SPDX-License-Identifier: MIT
23
*
34
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
45
* and associated documentation files (the "Software"), to deal in the Software without restriction,
@@ -136,11 +137,11 @@
136137

137138
// Number of pool pbufs.
138139
// Each requires 684 bytes of RAM (if MSS=536 and PBUF_POOL_BUFSIZE defaulting to be based on MSS)
139-
#define PBUF_POOL_SIZE MBED_CONF_LWIP_PBUF_POOL_SIZE
140+
#define PBUF_POOL_SIZE MBED_CONF_NSAPI_EMAC_RX_POOL_NUM_BUFS
140141

141-
#ifdef MBED_CONF_LWIP_PBUF_POOL_BUFSIZE
142+
#ifdef MBED_CONF_NSAPI_EMAC_RX_POOL_BUF_SIZE
142143
#undef PBUF_POOL_BUFSIZE
143-
#define PBUF_POOL_BUFSIZE LWIP_MEM_ALIGN_SIZE(MBED_CONF_LWIP_PBUF_POOL_BUFSIZE)
144+
#define PBUF_POOL_BUFSIZE LWIP_MEM_ALIGN_SIZE(MBED_CONF_NSAPI_EMAC_RX_POOL_BUF_SIZE)
144145
#else
145146
#ifndef PBUF_POOL_BUFSIZE
146147
#if LWIP_IPV6

0 commit comments

Comments
 (0)