Skip to content

Commit 4f1e5dd

Browse files
Mika Leppänenkjbracey
authored andcommitted
Updated K64F ethernet driver to use memory manager
1 parent 16c988c commit 4f1e5dd

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

features/netsocket/emac-drivers/TARGET_Freescale/k64f_emac.cpp

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
#include "cmsis_os.h"
3939

4040
#include "mbed_interface.h"
41-
#include "emac_stack_mem.h"
4241
#include "mbed_assert.h"
4342
#include "netsocket/nsapi_types.h"
4443
#include "mbed_shared_queues.h"
@@ -56,9 +55,9 @@ uint8_t *tx_desc_start_addr;
5655
// RX Buffer descriptors
5756
uint8_t *rx_desc_start_addr;
5857
// RX packet buffer pointers
59-
emac_stack_mem_t *rx_buff[ENET_RX_RING_LEN];
58+
emac_mem_buf_t *rx_buff[ENET_RX_RING_LEN];
6059
// TX packet buffer pointers
61-
emac_stack_mem_t *tx_buff[ENET_RX_RING_LEN];
60+
emac_mem_buf_t *tx_buff[ENET_RX_RING_LEN];
6261
// RX packet payload pointers
6362
uint32_t *rx_ptr[ENET_RX_RING_LEN];
6463

@@ -138,7 +137,7 @@ void K64F_EMAC::tx_reclaim()
138137
// Traverse all descriptors, looking for the ones modified by the uDMA
139138
while((tx_consume_index != tx_produce_index) &&
140139
(!(g_handle.txBdDirty->control & ENET_BUFFDESCRIPTOR_TX_READY_MASK))) {
141-
emac_stack_mem_free(tx_buff[tx_consume_index % ENET_TX_RING_LEN]);
140+
memory_manager->free(tx_buff[tx_consume_index % ENET_TX_RING_LEN]);
142141
if (g_handle.txBdDirty->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK)
143142
g_handle.txBdDirty = g_handle.txBdBase;
144143
else
@@ -212,11 +211,11 @@ bool K64F_EMAC::low_level_init_successful()
212211

213212
/* Create buffers for each receive BD */
214213
for (i = 0; i < ENET_RX_RING_LEN; i++) {
215-
rx_buff[i] = emac_stack_mem_alloc(ENET_ETH_MAX_FLEN, ENET_BUFF_ALIGNMENT);
214+
rx_buff[i] = memory_manager->alloc_heap(ENET_ETH_MAX_FLEN, ENET_BUFF_ALIGNMENT);
216215
if (NULL == rx_buff[i])
217216
return false;
218217

219-
rx_ptr[i] = (uint32_t*)emac_stack_mem_ptr(rx_buff[i]);
218+
rx_ptr[i] = (uint32_t*)memory_manager->get_ptr(rx_buff[i]);
220219
}
221220

222221
tx_consume_index = tx_produce_index = 0;
@@ -277,16 +276,16 @@ bool K64F_EMAC::low_level_init_successful()
277276
}
278277

279278

280-
/** \brief Allocates a emac_stack_mem_t and returns the data from the incoming packet.
279+
/** \brief Allocates a emac_mem_buf_t and returns the data from the incoming packet.
281280
*
282281
* \param[in] idx index of packet to be read
283-
* \return a emac_stack_mem_t filled with the received packet (including MAC header)
282+
* \return a emac_mem_buf_t filled with the received packet (including MAC header)
284283
*/
285-
emac_stack_mem_t *K64F_EMAC::low_level_input(int idx)
284+
emac_mem_buf_t *K64F_EMAC::low_level_input(int idx)
286285
{
287286
volatile enet_rx_bd_struct_t *bdPtr = g_handle.rxBdCurrent;
288-
emac_stack_mem_t *p = NULL;
289-
emac_stack_mem_t *temp_rxbuf = NULL;
287+
emac_mem_buf_t *p = NULL;
288+
emac_mem_buf_t *temp_rxbuf = NULL;
290289
uint32_t length = 0;
291290
const uint16_t err_mask = ENET_BUFFDESCRIPTOR_RX_TRUNC_MASK | ENET_BUFFDESCRIPTOR_RX_CRC_MASK |
292291
ENET_BUFFDESCRIPTOR_RX_NOOCTET_MASK | ENET_BUFFDESCRIPTOR_RX_LENVLIOLATE_MASK;
@@ -306,10 +305,10 @@ emac_stack_mem_t *K64F_EMAC::low_level_input(int idx)
306305

307306
/* Zero-copy */
308307
p = rx_buff[idx];
309-
emac_stack_mem_set_len(p, length);
308+
memory_manager->set_len(p, length);
310309

311310
/* Attempt to queue new buffer */
312-
temp_rxbuf = emac_stack_mem_alloc(ENET_ETH_MAX_FLEN, ENET_BUFF_ALIGNMENT);
311+
temp_rxbuf = memory_manager->alloc_heap(ENET_ETH_MAX_FLEN, ENET_BUFF_ALIGNMENT);
313312
if (NULL == temp_rxbuf) {
314313
/* Re-queue the same buffer */
315314
update_read_buffer(NULL);
@@ -322,12 +321,9 @@ emac_stack_mem_t *K64F_EMAC::low_level_input(int idx)
322321
}
323322

324323
rx_buff[idx] = temp_rxbuf;
325-
rx_ptr[idx] = (uint32_t*)emac_stack_mem_ptr(rx_buff[idx]);
324+
rx_ptr[idx] = (uint32_t*)memory_manager->get_ptr(rx_buff[idx]);
326325

327326
update_read_buffer((uint8_t*)rx_ptr[idx]);
328-
329-
/* Save size */
330-
emac_stack_mem_set_chain_len(p, length);
331327
}
332328

333329
#ifdef LOCK_RX_THREAD
@@ -343,7 +339,7 @@ emac_stack_mem_t *K64F_EMAC::low_level_input(int idx)
343339
*/
344340
void K64F_EMAC::input(int idx)
345341
{
346-
emac_stack_mem_t *p;
342+
emac_mem_buf_t *p;
347343

348344
/* move received packet into a new buf */
349345
p = low_level_input(idx);
@@ -412,21 +408,17 @@ void K64F_EMAC::packet_tx()
412408
* \param[in] buf the MAC packet to send (e.g. IP packet including MAC addresses and type)
413409
* \return ERR_OK if the packet could be sent or an err_t value if the packet couldn't be sent
414410
*/
415-
bool K64F_EMAC::link_out(emac_stack_mem_chain_t *chain)
411+
bool K64F_EMAC::link_out(emac_mem_buf_t *buf)
416412
{
417-
emac_stack_mem_t *q;
418-
emac_stack_mem_t *temp_pbuf;
419-
uint8_t *psend = NULL, *dst;
413+
emac_mem_buf_t *temp_pbuf;
420414

421-
temp_pbuf = emac_stack_mem_alloc(emac_stack_mem_chain_len(chain), ENET_BUFF_ALIGNMENT);
415+
temp_pbuf = memory_manager->alloc_heap(memory_manager->get_total_len(buf), ENET_BUFF_ALIGNMENT);
422416
if (NULL == temp_pbuf)
423417
return false;
424418

425-
psend = (uint8_t*)emac_stack_mem_ptr(temp_pbuf);
426-
for (q = emac_stack_mem_chain_dequeue(&chain), dst = psend; q != NULL; q = emac_stack_mem_chain_dequeue(&chain)) {
427-
memcpy(dst, emac_stack_mem_ptr(q), emac_stack_mem_len(q));
428-
dst += emac_stack_mem_len(q);
429-
}
419+
// Copy to new buffer and free original
420+
memory_manager->copy(temp_pbuf, buf);
421+
memory_manager->free(buf);
430422

431423
/* Check if a descriptor is available for the transfer. */
432424
if (xTXDCountSem.wait(0) == 0)
@@ -440,8 +432,8 @@ bool K64F_EMAC::link_out(emac_stack_mem_chain_t *chain)
440432
tx_produce_index += 1;
441433

442434
/* Setup transfers */
443-
g_handle.txBdCurrent->buffer = psend;
444-
g_handle.txBdCurrent->length = emac_stack_mem_len(temp_pbuf);
435+
g_handle.txBdCurrent->buffer = static_cast<uint8_t *>(memory_manager->get_ptr(temp_pbuf));
436+
g_handle.txBdCurrent->length = memory_manager->get_len(temp_pbuf);
445437
g_handle.txBdCurrent->control |= (ENET_BUFFDESCRIPTOR_TX_READY_MASK | ENET_BUFFDESCRIPTOR_TX_LAST_MASK);
446438

447439
/* Increase the buffer descriptor address. */
@@ -545,7 +537,7 @@ uint8_t K64F_EMAC::get_hwaddr_size() const
545537

546538
bool K64F_EMAC::get_hwaddr(uint8_t *addr) const
547539
{
548-
return false;
540+
return false;
549541
}
550542

551543
void K64F_EMAC::set_hwaddr(const uint8_t *addr)
@@ -574,6 +566,11 @@ void K64F_EMAC::power_down()
574566
/* No-op at this stage */
575567
}
576568

569+
void K64F_EMAC::set_memory_manager(EMACMemoryManager &mem_mngr)
570+
{
571+
memory_manager = &mem_mngr;
572+
}
573+
577574

578575
K64F_EMAC &K64F_EMAC::get_instance() {
579576
static K64F_EMAC emac;

features/netsocket/emac-drivers/TARGET_Freescale/k64f_emac.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class K64F_EMAC : public EMAC {
7272
* @param buf Packet to be send
7373
* @return True if the packet was send successfully, False otherwise
7474
*/
75-
virtual bool link_out(emac_stack_mem_chain_t *buf);
75+
virtual bool link_out(emac_mem_buf_t *buf);
7676

7777
/**
7878
* Initializes the HW
@@ -107,6 +107,12 @@ class K64F_EMAC : public EMAC {
107107
*/
108108
virtual void add_multicast_group(uint8_t *address);
109109

110+
/** Sets memory manager that is used to handle memory buffers
111+
*
112+
* @param mem_mngr Pointer to memory manager
113+
*/
114+
virtual void set_memory_manager(EMACMemoryManager &mem_mngr);
115+
110116
private:
111117
bool low_level_init_successful();
112118
void rx_isr();
@@ -115,7 +121,7 @@ class K64F_EMAC : public EMAC {
115121
void packet_tx();
116122
void tx_reclaim();
117123
void input(int idx);
118-
emac_stack_mem_t *low_level_input(int idx);
124+
emac_mem_buf_t *low_level_input(int idx);
119125
static void thread_function(void* pvParameters);
120126
void phy_task();
121127
static void ethernet_callback(ENET_Type *base, enet_handle_t *handle, enet_event_t event, void *param);
@@ -127,6 +133,7 @@ class K64F_EMAC : public EMAC {
127133
uint8_t tx_consume_index, tx_produce_index; /**< TX buffers ring */
128134
emac_link_input_cb_t emac_link_input_cb; /**< Callback for incoming data */
129135
emac_link_state_change_cb_t emac_link_state_cb; /**< Link state change callback */
136+
EMACMemoryManager *memory_manager; /**< Memory manager */
130137
int phy_task_handle; /**< Handle for phy task event */
131138
struct PHY_STATE {
132139
int connected;

0 commit comments

Comments
 (0)