48
48
#include " kinetis_emac.h"
49
49
#include " mbed_power_mgmt.h"
50
50
51
+ using namespace std ::chrono;
52
+
51
53
enet_handle_t g_handle;
52
54
// TX Buffer descriptors
53
55
uint8_t *tx_desc_start_addr;
@@ -75,7 +77,7 @@ extern "C" void kinetis_init_eth_hardware(void);
75
77
/* * \brief Driver thread priority */
76
78
#define THREAD_PRIORITY (osPriorityNormal)
77
79
78
- #define PHY_TASK_PERIOD_MS 200
80
+ #define PHY_TASK_PERIOD 200ms
79
81
80
82
Kinetis_EMAC::Kinetis_EMAC () : xTXDCountSem(ENET_TX_RING_LEN, ENET_TX_RING_LEN), hwaddr()
81
83
{
@@ -125,24 +127,25 @@ static void update_read_buffer(uint8_t *buf)
125
127
*/
126
128
void Kinetis_EMAC::tx_reclaim ()
127
129
{
128
- /* Get exclusive access */
129
- TXLockMutex.lock ();
130
-
131
- // Traverse all descriptors, looking for the ones modified by the uDMA
132
- while ((tx_consume_index != tx_produce_index) &&
133
- (!(g_handle.txBdDirty ->control & ENET_BUFFDESCRIPTOR_TX_READY_MASK))) {
134
- memory_manager->free (tx_buff[tx_consume_index % ENET_TX_RING_LEN]);
135
- if (g_handle.txBdDirty ->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK)
136
- g_handle.txBdDirty = g_handle.txBdBase ;
137
- else
138
- g_handle.txBdDirty ++;
139
-
140
- tx_consume_index += 1 ;
141
- xTXDCountSem.release ();
142
- }
143
-
144
- /* Restore access */
145
- TXLockMutex.unlock ();
130
+ /* Get exclusive access */
131
+ TXLockMutex.lock ();
132
+
133
+ // Traverse all descriptors, looking for the ones modified by the uDMA
134
+ while ((tx_consume_index != tx_produce_index) &&
135
+ (!(g_handle.txBdDirty ->control & ENET_BUFFDESCRIPTOR_TX_READY_MASK))) {
136
+ memory_manager->free (tx_buff[tx_consume_index % ENET_TX_RING_LEN]);
137
+ if (g_handle.txBdDirty ->control & ENET_BUFFDESCRIPTOR_TX_WRAP_MASK) {
138
+ g_handle.txBdDirty = g_handle.txBdBase ;
139
+ } else {
140
+ g_handle.txBdDirty ++;
141
+ }
142
+
143
+ tx_consume_index += 1 ;
144
+ xTXDCountSem.release ();
145
+ }
146
+
147
+ /* Restore access */
148
+ TXLockMutex.unlock ();
146
149
}
147
150
148
151
/* * \brief Ethernet receive interrupt handler
@@ -164,16 +167,15 @@ void Kinetis_EMAC::tx_isr()
164
167
void Kinetis_EMAC::ethernet_callback (ENET_Type *base, enet_handle_t *handle, enet_event_t event, void *param)
165
168
{
166
169
Kinetis_EMAC *enet = static_cast <Kinetis_EMAC *>(param);
167
- switch (event)
168
- {
169
- case kENET_RxEvent :
170
- enet->rx_isr ();
171
- break ;
172
- case kENET_TxEvent :
173
- enet->tx_isr ();
174
- break ;
175
- default :
176
- break ;
170
+ switch (event) {
171
+ case kENET_RxEvent :
172
+ enet->rx_isr ();
173
+ break ;
174
+ case kENET_TxEvent :
175
+ enet->tx_isr ();
176
+ break ;
177
+ default :
178
+ break ;
177
179
}
178
180
}
179
181
@@ -191,24 +193,27 @@ bool Kinetis_EMAC::low_level_init_successful()
191
193
192
194
// Allocate RX descriptors
193
195
rx_desc_start_addr = (uint8_t *)calloc (1 , sizeof (enet_rx_bd_struct_t ) * ENET_RX_RING_LEN + ENET_BUFF_ALIGNMENT);
194
- if (!rx_desc_start_addr)
196
+ if (!rx_desc_start_addr) {
195
197
return false ;
198
+ }
196
199
197
200
// Allocate TX descriptors
198
201
tx_desc_start_addr = (uint8_t *)calloc (1 , sizeof (enet_tx_bd_struct_t ) * ENET_TX_RING_LEN + ENET_BUFF_ALIGNMENT);
199
- if (!tx_desc_start_addr)
202
+ if (!tx_desc_start_addr) {
200
203
return false ;
204
+ }
201
205
202
206
rx_desc_start_addr = (uint8_t *)ENET_ALIGN (rx_desc_start_addr, ENET_BUFF_ALIGNMENT);
203
207
tx_desc_start_addr = (uint8_t *)ENET_ALIGN (tx_desc_start_addr, ENET_BUFF_ALIGNMENT);
204
208
205
209
/* Create buffers for each receive BD */
206
210
for (i = 0 ; i < ENET_RX_RING_LEN; i++) {
207
211
rx_buff[i] = memory_manager->alloc_heap (ENET_ETH_MAX_FLEN, ENET_BUFF_ALIGNMENT);
208
- if (NULL == rx_buff[i])
212
+ if (NULL == rx_buff[i]) {
209
213
return false ;
214
+ }
210
215
211
- rx_ptr[i] = (uint32_t *)memory_manager->get_ptr (rx_buff[i]);
216
+ rx_ptr[i] = (uint32_t *)memory_manager->get_ptr (rx_buff[i]);
212
217
}
213
218
214
219
tx_consume_index = tx_produce_index = 0 ;
@@ -221,7 +226,7 @@ bool Kinetis_EMAC::low_level_init_successful()
221
226
0 ,
222
227
(volatile enet_rx_bd_struct_t *)rx_desc_start_addr,
223
228
(volatile enet_tx_bd_struct_t *)tx_desc_start_addr,
224
- (uint8_t *)&rx_ptr,
229
+ (uint8_t *) &rx_ptr,
225
230
NULL ,
226
231
};
227
232
@@ -291,16 +296,16 @@ emac_mem_buf_t *Kinetis_EMAC::low_level_input(int idx)
291
296
update_read_buffer (NULL );
292
297
293
298
#ifdef LOCK_RX_THREAD
294
- TXLockMutex.unlock ();
299
+ TXLockMutex.unlock ();
295
300
#endif
296
301
297
302
return NULL ;
298
303
}
299
304
300
305
rx_buff[idx] = temp_rxbuf;
301
- rx_ptr[idx] = (uint32_t *)memory_manager->get_ptr (rx_buff[idx]);
306
+ rx_ptr[idx] = (uint32_t *)memory_manager->get_ptr (rx_buff[idx]);
302
307
303
- update_read_buffer ((uint8_t *)rx_ptr[idx]);
308
+ update_read_buffer ((uint8_t *)rx_ptr[idx]);
304
309
}
305
310
306
311
#ifdef LOCK_RX_THREAD
@@ -333,12 +338,12 @@ void Kinetis_EMAC::input(int idx)
333
338
*
334
339
* \param[in] pvParameters pointer to the interface data
335
340
*/
336
- void Kinetis_EMAC::thread_function (void * pvParameters)
341
+ void Kinetis_EMAC::thread_function (void * pvParameters)
337
342
{
338
343
struct Kinetis_EMAC *kinetis_enet = static_cast <Kinetis_EMAC *>(pvParameters);
339
344
340
345
for (;;) {
341
- uint32_t flags = osThreadFlagsWait (FLAG_RX| FLAG_TX, osFlagsWaitAny, osWaitForever);
346
+ uint32_t flags = osThreadFlagsWait (FLAG_RX | FLAG_TX, osFlagsWaitAny, osWaitForever);
342
347
343
348
MBED_ASSERT (!(flags & osFlagsError));
344
349
@@ -389,7 +394,7 @@ bool Kinetis_EMAC::link_out(emac_mem_buf_t *buf)
389
394
{
390
395
// If buffer is chained or not aligned then make a contiguous aligned copy of it
391
396
if (memory_manager->get_next (buf) ||
392
- reinterpret_cast <uint32_t >(memory_manager->get_ptr (buf)) % ENET_BUFF_ALIGNMENT) {
397
+ reinterpret_cast <uint32_t >(memory_manager->get_ptr (buf)) % ENET_BUFF_ALIGNMENT) {
393
398
emac_mem_buf_t *copy_buf;
394
399
copy_buf = memory_manager->alloc_heap (memory_manager->get_total_len (buf), ENET_BUFF_ALIGNMENT);
395
400
if (NULL == copy_buf) {
@@ -507,7 +512,7 @@ bool Kinetis_EMAC::power_up()
507
512
/* Allow the PHY task to detect the initial link state and set up the proper flags */
508
513
osDelay (10 );
509
514
510
- phy_task_handle = mbed::mbed_event_queue ()->call_every (PHY_TASK_PERIOD_MS , mbed::callback (this , &Kinetis_EMAC::phy_task));
515
+ phy_task_handle = mbed::mbed_event_queue ()->call_every (PHY_TASK_PERIOD , mbed::callback (this , &Kinetis_EMAC::phy_task));
511
516
512
517
return true ;
513
518
}
@@ -540,7 +545,7 @@ bool Kinetis_EMAC::get_hwaddr(uint8_t *addr) const
540
545
void Kinetis_EMAC::set_hwaddr (const uint8_t *addr)
541
546
{
542
547
memcpy (hwaddr, addr, sizeof hwaddr);
543
- ENET_SetMacAddr (ENET, const_cast <uint8_t *>(addr));
548
+ ENET_SetMacAddr (ENET, const_cast <uint8_t *>(addr));
544
549
}
545
550
546
551
void Kinetis_EMAC::set_link_input_cb (emac_link_input_cb_t input_cb)
@@ -585,13 +590,15 @@ void Kinetis_EMAC::set_memory_manager(EMACMemoryManager &mem_mngr)
585
590
}
586
591
587
592
588
- Kinetis_EMAC &Kinetis_EMAC::get_instance () {
593
+ Kinetis_EMAC &Kinetis_EMAC::get_instance ()
594
+ {
589
595
static Kinetis_EMAC emac;
590
596
return emac;
591
597
}
592
598
593
599
// Weak so a module can override
594
- MBED_WEAK EMAC &EMAC::get_default_instance () {
600
+ MBED_WEAK EMAC &EMAC::get_default_instance ()
601
+ {
595
602
return Kinetis_EMAC::get_instance ();
596
603
}
597
604
0 commit comments