Skip to content

Commit 9095b03

Browse files
author
Mika Leppänen
committed
Updated EMAC test environment for LPCxx boards
Updated EMAC memory manager to use libservice nsdynmemlib for EMAC memory buffers. Located the nsdynmemlib buffer heap to DMA safe memory bank on LPCxx boards. Optimized placement of static variables on EMAC test environment for LPCxx boards to maximize available memory.
1 parent 66e3409 commit 9095b03

File tree

5 files changed

+141
-3
lines changed

5 files changed

+141
-3
lines changed

TESTS/network/emac/emac_TestMemoryManager.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727

2828
#include "rtos/Mutex.h"
2929

30+
extern "C" {
31+
#include "arm_hal_interrupt_private.h"
32+
}
33+
#include "nsdynmemLIB.h"
34+
3035
#include "EMACMemoryManager.h"
3136
#include "emac_TestMemoryManager.h"
3237

@@ -43,12 +48,57 @@
4348

4449
char s_trace_buffer[100] = MEM_MNGR_TRACE;
4550

51+
/* For LPC boards define the heap memory bank ourselves to give us section placement
52+
control */
53+
#ifndef ETHMEM_SECTION
54+
#if defined(TARGET_LPC4088) || defined(TARGET_LPC4088_DM)
55+
# if defined (__ICCARM__)
56+
# define ETHMEM_SECTION
57+
# elif defined(TOOLCHAIN_GCC_CR)
58+
# define ETHMEM_SECTION __attribute__((section(".data.$RamPeriph32")))
59+
# else
60+
# define ETHMEM_SECTION __attribute__((section("AHBSRAM1"),aligned))
61+
# endif
62+
#elif defined(TARGET_LPC1768) || defined(TARGET_LPC1769)
63+
# if defined (__ICCARM__)
64+
# define ETHMEM_SECTION
65+
# elif defined(TOOLCHAIN_GCC_CR)
66+
# define ETHMEM_SECTION __attribute__((section(".data.$RamPeriph32")))
67+
# else
68+
# define ETHMEM_SECTION __attribute__((section("AHBSRAM0"),aligned))
69+
# endif
70+
#endif
71+
#endif
72+
73+
#ifdef ETHMEM_SECTION
74+
// Use nanostack libservice dynamic memory library
75+
#define EMAC_HEAP_SIZE 16300
76+
77+
#if defined (__ICCARM__)
78+
#pragma location = ".ethusbram"
79+
#endif
80+
ETHMEM_SECTION static unsigned char ns_heap[EMAC_HEAP_SIZE];
81+
82+
void emac_heap_error_handler(heap_fail_t event)
83+
{
84+
MBED_ASSERT(0);
85+
}
86+
#endif
87+
4688
EmacTestMemoryManager::EmacTestMemoryManager()
4789
: m_mem_mutex(),
4890
m_mem_buffers(),
4991
m_alloc_unit(BUF_POOL_SIZE),
5092
m_memory_available(true)
5193
{
94+
#ifdef ETHMEM_SECTION
95+
static bool ns_heap_init = false;
96+
if (!ns_heap_init) {
97+
platform_critical_init(); // Create mutex for dynamic memory library
98+
ns_dyn_mem_init(ns_heap, EMAC_HEAP_SIZE, emac_heap_error_handler, NULL);
99+
ns_heap_init = true;
100+
}
101+
#endif
52102
}
53103

54104
emac_mem_buf_t *EmacTestMemoryManager::alloc_heap(uint32_t size, uint32_t align)
@@ -74,7 +124,11 @@ emac_mem_buf_t *EmacTestMemoryManager::alloc_heap(uint32_t size, uint32_t align,
74124

75125
CHECK_ASSERT(buf, "alloc_heap() no memory");
76126

127+
#ifdef ETHMEM_SECTION
128+
buf->buffer = ns_dyn_mem_alloc(BUF_HEAD_SIZE + size + align + BUF_TAIL_SIZE);
129+
#else
77130
buf->buffer = std::malloc(BUF_HEAD_SIZE + size + align + BUF_TAIL_SIZE);
131+
#endif
78132

79133
CHECK_ASSERT(buf->buffer, "alloc_heap() no memory");
80134

@@ -218,7 +272,12 @@ void EmacTestMemoryManager::free(emac_mem_buf_t *buf)
218272
emac_memory_t *next = mem_buf->next;
219273

220274
m_mem_buffers.erase(mem_buf_entry);
275+
276+
#ifdef ETHMEM_SECTION
277+
ns_dyn_mem_free(mem_buf->buffer);
278+
#else
221279
std::free(mem_buf->buffer);
280+
#endif
222281
delete mem_buf;
223282

224283
mem_buf = next;

TESTS/network/emac/emac_TestNetworkStack.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ nsapi_error_t EmacTestNetworkStack::add_dns_server(const SocketAddress &address)
3939
return NSAPI_ERROR_OK;
4040
}
4141

42+
nsapi_error_t EmacTestNetworkStack::call_in(int delay, mbed::Callback<void()> func)
43+
{
44+
// Implemented as empty to save memory
45+
return NSAPI_ERROR_DEVICE_ERROR;
46+
}
47+
48+
EmacTestNetworkStack::call_in_callback_cb_t EmacTestNetworkStack::get_call_in_callback()
49+
{
50+
call_in_callback_cb_t cb(this, &EmacTestNetworkStack::call_in);
51+
return cb;
52+
}
53+
4254
nsapi_error_t EmacTestNetworkStack::socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto)
4355
{
4456
return NSAPI_ERROR_OK;

TESTS/network/emac/emac_TestNetworkStack.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,38 @@ class EmacTestNetworkStack : public OnboardNetworkStack, private mbed::NonCopyab
356356
int optname, void *optval, unsigned *optlen);
357357

358358
private:
359+
360+
/** Call in callback
361+
*
362+
* Callback is used to call the call in method of the network stack.
363+
*/
364+
typedef mbed::Callback<nsapi_error_t (int delay_ms, mbed::Callback<void()> user_cb)> call_in_callback_cb_t;
365+
366+
/** Get a call in callback
367+
*
368+
* Get a call in callback from the network stack context.
369+
*
370+
* Callback should not take more than 10ms to execute, otherwise it might
371+
* prevent underlying thread processing. A portable user of the callback
372+
* should not make calls to network operations due to stack size limitations.
373+
* The callback should not perform expensive operations such as socket recv/send
374+
* calls or blocking operations.
375+
*
376+
* @return Call in callback
377+
*/
378+
virtual call_in_callback_cb_t get_call_in_callback();
379+
380+
/** Call a callback after a delay
381+
*
382+
* Call a callback from the network stack context after a delay. If function
383+
* returns error callback will not be called.
384+
*
385+
* @param delay Delay in milliseconds
386+
* @param func Callback to be called
387+
* @return 0 on success, negative error code on failure
388+
*/
389+
virtual nsapi_error_t call_in(int delay, mbed::Callback<void()> func);
390+
359391
Interface *m_interface;
360392
};
361393

TESTS/network/emac/emac_util.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,32 @@
3636

3737
using namespace utest::v1;
3838

39+
/* For LPC boards define the memory bank ourselves to give us section placement
40+
control */
41+
#ifndef ETHMEM_SECTION
42+
#if defined(TARGET_LPC4088) || defined(TARGET_LPC4088_DM)
43+
# if defined (__ICCARM__)
44+
# define ETHMEM_SECTION
45+
# elif defined(TOOLCHAIN_GCC_CR)
46+
# define ETHMEM_SECTION __attribute__((section(".data.$RamPeriph32")))
47+
# else
48+
# define ETHMEM_SECTION __attribute__((section("AHBSRAM0"),aligned))
49+
# endif
50+
#elif defined(TARGET_LPC1768) || defined(TARGET_LPC1769)
51+
# if defined (__ICCARM__)
52+
# define ETHMEM_SECTION
53+
# elif defined(TOOLCHAIN_GCC_CR)
54+
# define ETHMEM_SECTION __attribute__((section(".data.$RamPeriph32")))
55+
# else
56+
# define ETHMEM_SECTION __attribute__((section("AHBSRAM1"),aligned))
57+
# endif
58+
#endif
59+
#endif
60+
61+
#ifndef ETHMEM_SECTION
62+
#define ETHMEM_SECTION
63+
#endif
64+
3965
typedef struct {
4066
int length;
4167
int receipt_number;
@@ -63,7 +89,12 @@ static int eth_mtu_size = 0;
6389
// Event queue
6490
static rtos::Semaphore worker_loop_semaphore;
6591
static rtos::Semaphore link_status_semaphore;
66-
static EventQueue worker_loop_event_queue(20 * EVENTS_EVENT_SIZE);
92+
93+
#if defined (__ICCARM__)
94+
#pragma location = ".ethusbram"
95+
#endif
96+
ETHMEM_SECTION static EventQueue worker_loop_event_queue(20 * EVENTS_EVENT_SIZE);
97+
6798
static void worker_loop_event_cb(int event);
6899
static Event<void(int)> worker_loop_event(&worker_loop_event_queue, worker_loop_event_cb);
69100
static void link_input_event_cb(void *buf);
@@ -460,7 +491,11 @@ static void link_input_event_cb(void *buf)
460491
}
461492
}
462493

463-
static unsigned char thread_stack[2048];
494+
495+
#if defined (__ICCARM__)
496+
#pragma location = ".ethusbram"
497+
#endif
498+
ETHMEM_SECTION static unsigned char thread_stack[2048];
464499

465500
void worker_loop(void);
466501

TESTS/network/emac/emac_util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ extern const unsigned char eth_mac_broadcast_addr[];
5858
#define RESET_ERROR_FLAGS(flags) emac_if_reset_error_flags(flags)
5959

6060
#define ETH_FRAME_HEADER_LEN 28
61-
#define ETH_FRAME_MIN_LEN 60
61+
#define ETH_FRAME_MIN_LEN 60 + 4
6262
#define ETH_MAC_ADDR_LEN 6
6363

6464
#define TIMEOUT 1

0 commit comments

Comments
 (0)