50
50
* DEFINE CONSTANTS
51
51
*****************************************************************************/
52
52
#define ETHERNET_TASK_STACK_SIZE 3072
53
- #define ETHERNET_TASK_PRIORITY 12
53
+ #define ETHERNET_TASK_PRIORITY 24 // 12
54
54
#define ETHERNET_CHECK_LINK_PERIOD_MS 2000
55
55
#define ETHERNET_CMD_QUEUE_SIZE 100
56
56
66
66
*****************************************************************************/
67
67
static void TASK_ETHERNET (void * pvParameters );
68
68
static mp_obj_t eth_init_helper (eth_obj_t * self , const mp_arg_val_t * args );
69
- static IRAM_ATTR void ksz8851_evt_callback (uint32_t ksz8851_evt );
69
+ static IRAM_ATTR void ksz8851_evt_callback (uint32_t ksz8851_evt , uint16_t isr );
70
70
static void process_tx (uint8_t * buff , uint16_t len );
71
- static void process_rx (void );
71
+ static uint32_t process_rx (void );
72
72
static void eth_validate_hostname (const char * hostname );
73
73
static esp_err_t modeth_event_handler (void * ctx , system_event_t * event );
74
74
@@ -195,9 +195,10 @@ static void process_tx(uint8_t* buff, uint16_t len)
195
195
ksz8851EndPacketSend ();
196
196
}
197
197
}
198
- static void process_rx (void )
198
+ static uint32_t process_rx (void )
199
199
{
200
200
uint32_t len , frameCnt ;
201
+ uint32_t totalLen = 0 ;
201
202
202
203
frameCnt = (ksz8851_regrd (REG_RX_FRAME_CNT_THRES ) & RX_FRAME_CNT_MASK ) >> 8 ;
203
204
MSG ("TE process_rx f:%u\n" , frameCnt );
@@ -206,16 +207,20 @@ static void process_rx(void)
206
207
ksz8851RetrievePacketData (modeth_rxBuff , & len );
207
208
if (len )
208
209
{
210
+ totalLen += len ;
209
211
tcpip_adapter_eth_input (modeth_rxBuff , len , NULL );
210
212
}
211
213
frameCnt -- ;
212
214
}
213
215
MSG ("TE process_rx len:%u\n" , totalLen );
216
+ return totalLen ;
214
217
}
215
- static IRAM_ATTR void ksz8851_evt_callback (uint32_t ksz8851_evt )
218
+ static IRAM_ATTR void ksz8851_evt_callback (uint32_t ksz8851_evt , uint16_t isr )
216
219
{
217
220
modeth_cmd_ctx_t ctx ;
221
+ ctx .isr = isr ;
218
222
portBASE_TYPE tmp = pdFALSE ;
223
+ bool sent = false;
219
224
220
225
if (ksz8851_evt & KSZ8851_LINK_CHG_INT )
221
226
{
@@ -227,20 +232,29 @@ static IRAM_ATTR void ksz8851_evt_callback(uint32_t ksz8851_evt)
227
232
xTaskNotifyFromISR (ethernetTaskHandle , 0 , eIncrement , NULL );
228
233
}
229
234
xQueueSendToFrontFromISR (eth_cmdQueue , & ctx , & tmp );
235
+ sent = true;
230
236
}
231
237
232
238
if (ksz8851_evt & KSZ8851_RX_INT )
233
239
{
234
240
ctx .cmd = ETH_CMD_RX ;
235
241
ctx .buf = NULL ;
236
242
xQueueSendFromISR (eth_cmdQueue , & ctx , & tmp );
243
+ sent = true;
237
244
}
238
245
239
246
if (ksz8851_evt & KSZ8851_OVERRUN_INT )
240
247
{
241
248
ctx .cmd = ETH_CMD_OVERRUN ;
242
249
ctx .buf = NULL ;
243
250
xQueueSendToFrontFromISR (eth_cmdQueue , & ctx , & tmp );
251
+ sent = true;
252
+ }
253
+
254
+ if ( ! sent ){
255
+ ctx .cmd = ETH_CMD_OTHER ;
256
+ ctx .buf = NULL ;
257
+ xQueueSendFromISR (eth_cmdQueue , & ctx , & tmp );
244
258
}
245
259
246
260
if ( tmp == pdTRUE )
@@ -251,13 +265,22 @@ static IRAM_ATTR void ksz8851_evt_callback(uint32_t ksz8851_evt)
251
265
}
252
266
}
253
267
268
+ // void printTaskStatus(){
269
+ // UBaseType_t numTasks = uxTaskGetNumberOfTasks();
270
+ // char* taskStatusWriteBuffer = (char*) heap_caps_malloc(sizeof(char) * 40 * numTasks, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
271
+ // vTaskGetRunTimeStats(taskStatusWriteBuffer);
272
+ // MSG("TE vTaskGetRunTimeStats:\n%s\n", taskStatusWriteBuffer);
273
+ // heap_caps_free(taskStatusWriteBuffer);
274
+ // }
275
+
254
276
static void TASK_ETHERNET (void * pvParameters ) {
255
277
MSG ("TE\n" );
256
278
257
279
static uint32_t thread_notification ;
258
280
system_event_t evt ;
259
281
modeth_cmd_ctx_t queue_entry ;
260
282
uint8_t timeout = 0 ;
283
+ uint8_t max_timeout = 50 ; // 5
261
284
262
285
// Block task till notification is recieved
263
286
thread_notification = ulTaskNotifyTake (pdTRUE , portMAX_DELAY );
@@ -301,8 +324,69 @@ static void TASK_ETHERNET (void *pvParameters) {
301
324
evt .event_id = SYSTEM_EVENT_ETH_START ;
302
325
esp_event_send (& evt );
303
326
327
+ // MSG("TE for ls=%u 10M=%u 100M=%u\n", get_eth_link_speed(), ETH_SPEED_MODE_10M, ETH_SPEED_MODE_100M);
328
+ UBaseType_t stack_high = uxTaskGetStackHighWaterMark (NULL );
329
+ MSG ("TE stack_high=%u\n" , stack_high );
330
+
331
+ uint16_t ct = 0 ;
332
+ uint16_t ctTX = 0 ;
333
+ uint16_t ctRX = 0 ;
334
+ uint16_t totalTX = 0 ;
335
+ uint16_t totalRX = 0 ;
336
+ bool printStat = false;
337
+ uint16_t interrupt_pin_value = pin_get_value (KSZ8851_INT_PIN );
338
+ //uint16_t interrupt_value = ksz8851_regrd(REG_INT_STATUS);
339
+
304
340
for (;;)
305
341
{
342
+ UBaseType_t stack_high_new = uxTaskGetStackHighWaterMark (NULL );
343
+ uint16_t interrupt_pin_value_new = pin_get_value (KSZ8851_INT_PIN );
344
+ //uint16_t interrupt_value_new = ksz8851_regrd(REG_INT_STATUS);
345
+
346
+ if (stack_high_new > stack_high ) {
347
+ stack_high = stack_high_new ;
348
+ printStat = true;
349
+ }
350
+ if (interrupt_pin_value_new != interrupt_pin_value ) {
351
+ interrupt_pin_value = interrupt_pin_value_new ;
352
+ printStat = true;
353
+ }
354
+ //if ( interrupt_value_new != interrupt_value ) {
355
+ // interrupt_value = interrupt_value_new;
356
+ // printStat = true;
357
+ //}
358
+ if (ct % 100 == 0 ){
359
+ printStat = true;
360
+ }
361
+ if (printStat ){
362
+ MSG ("TE ct=%u stack:%u queue:%u tx:%u/%u rx:%u/%u ksz8851:%u 0x%x\n" ,
363
+ ct , stack_high , uxQueueMessagesWaiting ( eth_cmdQueue ),
364
+ ctTX , totalTX , ctRX , totalRX ,
365
+ ksz8851GetLinkStatus (), interrupt_pin_value );
366
+ // if (ct % 1000 == 0)
367
+ // printTaskStatus();
368
+
369
+ // spi_op
370
+ // ksz8851GetLinkStatus
371
+ // ksz8851PowerDownMode
372
+ // pi_setbits(REG_PORT_LINK_MD, PORT_POWER_SAVE_MODE);
373
+ // ksz8851BeginPacketSend
374
+
375
+ printStat = false;
376
+
377
+ // UBaseType_t numTasks = uxTaskGetNumberOfTasks();
378
+
379
+ // TaskStatus_t* taskStatus = (TaskStatus_t*) heap_caps_malloc(sizeof(TaskStatus_t) * numTasks, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
380
+ // UBaseType_t totalRuntime = 0;
381
+ // UBaseType_t numTasksGotten = uxTaskGetSystemState(taskStatus, numTasks, &totalRuntime);
382
+ // MSG("TE uxTaskGetSystemState: %u, %u, %u\n", numTasks, numTasksGotten, totalRuntime);
383
+ // heap_caps_free(taskStatus);
384
+
385
+ }
386
+
387
+
388
+ ct ++ ;
389
+
306
390
if (!eth_obj .link_status && (xEventGroupGetBits (eth_event_group ) & ETHERNET_EVT_STARTED ))
307
391
{
308
392
// block till link is up again
@@ -326,15 +410,27 @@ static void TASK_ETHERNET (void *pvParameters) {
326
410
327
411
if (xQueueReceive (eth_cmdQueue , & queue_entry , 200 / portTICK_PERIOD_MS ) == pdTRUE )
328
412
{
413
+ // UBaseType_t numWait = uxQueueMessagesWaiting( eth_cmdQueue );
414
+ // if (numWait){
415
+ // MSG("TE msg waiting:%u\n", numWait);
416
+ // //printTaskStatus();
417
+ // }
418
+ MSG ("TE ct=%u cmd:0x%x isr:0x%x queue:%u ksz8851:%u 0x%x\n" ,
419
+ ct , queue_entry .cmd , (queue_entry .cmd == ETH_CMD_TX )?0 :queue_entry .isr , uxQueueMessagesWaiting ( eth_cmdQueue ),
420
+ ksz8851GetLinkStatus (), interrupt_pin_value );
421
+
329
422
switch (queue_entry .cmd )
330
423
{
331
424
case ETH_CMD_TX :
332
425
//MSG("TE TX %u\n", queue_entry.len);
426
+ ctTX ++ ;
427
+ totalTX += queue_entry .len ;
333
428
process_tx (queue_entry .buf , queue_entry .len );
334
429
break ;
335
430
case ETH_CMD_RX :
336
- process_rx ();
337
431
//MSG("TE RX {0x%x}\n", queue_entry.isr);
432
+ ctRX ++ ;
433
+ totalRX += process_rx ();
338
434
// Clear Intr status bits
339
435
ksz8851_regwr (REG_INT_STATUS , INT_MASK );
340
436
break ;
@@ -375,13 +471,14 @@ static void TASK_ETHERNET (void *pvParameters) {
375
471
timeout = 0 ;
376
472
// Checking if interrupt line is locked up in Low state
377
473
//TODO: This workaround should be removed once the lockup is resolved
378
- while ((!pin_get_value (KSZ8851_INT_PIN )) && timeout < 5 )
474
+ while ((!pin_get_value (KSZ8851_INT_PIN )) && timeout < max_timeout )
379
475
{
380
476
MSG ("TE TO %u\n" , timeout );
381
- vTaskDelay (1 / portTICK_PERIOD_MS );
477
+
478
+ vTaskDelay (10 / portTICK_PERIOD_MS );
382
479
timeout ++ ;
383
480
}
384
- if (timeout >= 5 )
481
+ if (timeout >= max_timeout )
385
482
{
386
483
MSG ("TE Force Int\n" );
387
484
xQueueReset (eth_cmdQueue );
0 commit comments