2 * Copyright (C) 2012-2013 Czech Technical University in Prague
4 * Created on: Aug 23, 2013
7 * - Jan Dolezal <pm.jenik@gmail.com>
9 * This document contains proprietary information belonging to Czech
10 * Technical University in Prague. Passing on and copying of this
11 * document, and communication of its contents is not permitted
12 * without prior written authorization.
14 * File : cmd_netstats.c
17 * This file contains commands for net statistics (ethernet interface and LwIP)
21 #include "cmd_netstats.h"
30 #include "lwip/stats.h"
32 #define INTERFACE_INSTANCE_NUMBER 0
34 int cmd_do_read_linkstat(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
36 if (!isPostInitialized()) {
37 rpp_sci_printf("Eth not initialized run 'ethinit' command first.\n");
40 struct netif *netif = rpp_eth_get_netif(INTERFACE_INSTANCE_NUMBER);
41 if (rpp_eth_phylinkstat(INTERFACE_INSTANCE_NUMBER))
42 rpp_sci_printf("%c%c%d : UP\r\n", netif->name[0], netif->name[1], netif->num);
44 rpp_sci_printf("%c%c%d : DOWN\r\n", netif->name[0], netif->name[1], netif->num);
48 int cmd_do_read_mac(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
50 if (!isPostInitialized()) {
51 rpp_sci_printf("Eth not initialized run 'ethinit' command first.\n");
55 rpp_eth_get_macAddrStr(INTERFACE_INSTANCE_NUMBER,macaddr);
56 rpp_sci_printf((const char *)"%s\n", macaddr);
60 int cmd_do_read_ip(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
62 if (!isPostInitialized()) {
63 rpp_sci_printf("Eth not initialized run 'ethinit' command first.\n");
66 struct netif *netif = rpp_eth_get_netif(INTERFACE_INSTANCE_NUMBER);
68 rpp_eth_getIPDecimalStr(netif->ip_addr, ipString);
69 rpp_sci_printf("Address: %s\r\n", ipString);
70 rpp_eth_getIPDecimalStr(netif->netmask, ipString);
71 rpp_sci_printf("Netmask: %s\r\n", ipString);
72 rpp_eth_getIPDecimalStr(netif->gw, ipString);
73 rpp_sci_printf("Gateway: %s\r\n", ipString);
80 /* --- EMAC DESCRIPTOR FORMAT ---
83 * OFFSET | 31 16 | 15 0
84 * ------------------------------------------------------------------------------------
85 * 0 | *Next Descriptor Pointer
86 * 1 | *Data Buffer Pointer
87 * 2 | Buffer Offset | Buffer Length
88 * 3 | Flags | Packet Length
92 boolean_t bd_SOP(volatile struct emac_tx_bd *bufferDescriptor)
94 return (bufferDescriptor->flags_pktlen & EMAC_DSC_FLAG_SOP);
97 boolean_t bd_EOP(volatile struct emac_tx_bd *bufferDescriptor)
99 return (bufferDescriptor->flags_pktlen & EMAC_DSC_FLAG_EOP);
102 boolean_t bd_OWNER(volatile struct emac_tx_bd *bufferDescriptor)
104 return (bufferDescriptor->flags_pktlen & EMAC_DSC_FLAG_OWNER);
107 boolean_t bd_EOQ(volatile struct emac_tx_bd *bufferDescriptor)
109 return (bufferDescriptor->flags_pktlen & EMAC_DSC_FLAG_EOQ);
112 uint32_t bd_addr(uint16_t bd_num)
114 return EMAC_CTRL_RAM_BASE_m(0) + bd_num*sizeof(struct emac_tx_bd);
117 volatile struct emac_tx_bd *findPreviousBD(volatile struct emac_tx_bd *bufferDescriptor)
119 volatile struct emac_tx_bd *temporary = bufferDescriptor - 1;
121 /* first try space before given bd */
122 if ((uint32_t)temporary >= EMAC_CTRL_RAM_BASE_m(0) && temporary->next == bufferDescriptor)
124 temporary = bufferDescriptor->next;
125 while (temporary != bufferDescriptor && temporary != NULL) {
126 if (temporary->next == bufferDescriptor)
128 temporary = (temporary + 1); /* going through memory, if you want to go through bd chains put here 'temporary = temporary->next;' */
129 if ( (uint32_t)(temporary+1) > EMAC_CTRL_RAM_BASE_m(0)+SIZE_EMAC_CTRL_RAM) /* when out of CPPI RAM */
130 temporary = (struct emac_tx_bd *)EMAC_CTRL_RAM_BASE_m(0); /* continue scanning from the beginning */
132 return (struct emac_tx_bd *)-1; /* not found */
135 void print_bd_content(volatile struct emac_tx_bd *bufferDescriptor, boolean_t humanReadable)
139 rpp_sci_printf("Buffer Descriptor at 0x%08x\n", bufferDescriptor);
140 rpp_sci_printf("Next BD:\t0x%08x\n", bufferDescriptor->next);
141 rpp_sci_printf("Data ptr:\t0x%08x\n", bufferDescriptor->bufptr);
142 rpp_sci_printf("Buf offset:\t%d\tBuf length:\t%d\n", (bufferDescriptor->bufoff_len >> 16), (bufferDescriptor->bufoff_len & 0xffff));
144 rpp_sci_printf("Flags:\t\t0x%04x\t", (bufferDescriptor->flags_pktlen >> 16));
147 data = bufferDescriptor->flags_pktlen;
148 rpp_sci_printf("Flags:\t%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s\n", (data & EMAC_DSC_FLAG_SOP) ? "SOP" : "", (data & EMAC_DSC_FLAG_EOP) ? "EOP" : "", (data & EMAC_DSC_FLAG_OWNER) ? "OWNER" : "", (data & EMAC_DSC_FLAG_EOQ) ? "EOQ" : "", (data & EMAC_DSC_FLAG_TDOWNCMPLT) ? "TDWN" : "", (data & EMAC_DSC_FLAG_PASSCRC) ? "PSCRC" : "", (data & EMAC_DSC_FLAG_JABBER) ? "JBR" : "", (data & EMAC_DSC_FLAG_OVERSIZE) ? "OVSZ" : "", (data & EMAC_DSC_FLAG_FRAGMENT) ? "FRGM" : "", (data & EMAC_DSC_FLAG_UNDERSIZED) ? "UNSZ" : "", (data & EMAC_DSC_FLAG_CONTROL) ? "CTRL" : "", (data & EMAC_DSC_FLAG_OVERRUN) ? "OVRUN" : "", (data & EMAC_DSC_FLAG_CODEERROR) ? "CODEERR" : "", (data & EMAC_DSC_FLAG_ALIGNERROR) ? "ALGNERR" : "", (data & EMAC_DSC_FLAG_CRCERROR) ? "CRCERR" : "", (data & EMAC_DSC_FLAG_NOMATCH) ? "NOMATCH" : "");
150 rpp_sci_printf("Pkt length:\t%d\n", (bufferDescriptor->flags_pktlen & 0xffff));
151 rpp_sci_printf("Pbuf ptr:\t0x%08x\n", bufferDescriptor->pbuf);
155 * mode: 0 flags -> param says which flag (0-15)
156 * 1 bd->next == NULL, no param
158 void print_bds_state(uint8_t mode, uint8_t param)
160 volatile struct emac_tx_bd *bufferDescriptor = (struct emac_tx_bd *)EMAC_CTRL_RAM_BASE_m(0);
163 rpp_sci_printf("TX:");
164 while (index < 409) {
167 if (!(index%50)) rpp_sci_printf("\n");
170 rpp_sci_printf("\nRX: ");
172 if (bufferDescriptor->flags_pktlen & ((1 << 15) >> (param%16)))
177 else if (mode == 1) {
178 if (bufferDescriptor->next == NULL)
187 rpp_sci_printf("\n");
192 unsigned long readNum(uint8_t minamount, uint8_t maxamount, boolean_t hex)
194 unsigned long out = 0;
195 uint8_t input_buffer[BUF_SIZE];
196 uint8_t buf_index = 0;
200 input = rpp_sci_getc();
201 if (input == '\b' && buf_index > 0) {
202 input_buffer[buf_index] = '\0';
208 else if (buf_index >= maxamount)
210 else if (input <= '9' && input >= '0') {
211 input_buffer[buf_index] = input - '0';
215 else if (hex && (input <= 'f' && input >= 'a')) {
216 input_buffer[buf_index] = input - ('a' - 10);
220 else if (hex && (input <= 'F' && input >= 'A')) {
221 input_buffer[buf_index] = input - ('A' - 10);
222 echo((input + ('a' - 'A')));
225 } while ( (input != '\r' && input != '\n' && buf_index < BUF_SIZE) || buf_index < minamount);
226 input = 0; /* use as index */
227 while (input < buf_index) {
229 out = (out << 4) + input_buffer[input];
231 out = (out*10) + input_buffer[input];
237 int cmd_do_bufferdescriptors(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
241 volatile struct emac_tx_bd *bufferDescriptor;
242 boolean_t run = TRUE, interactive = FALSE, overview = FALSE;
245 boolean_t isrRun = FALSE, rb = FALSE, tb = FALSE;
248 /* if eth not initialized return */
249 if (!isPostInitialized()) {
250 rpp_sci_printf("Eth not initialized run 'ethinit' command first.\n");
254 /* examine parameters */
255 for (pindex = 1; param[pindex] != 0; pindex++) {
256 if (strncmp((char *)param[pindex], "-i", 3) == 0) {
260 else if (strncmp((char *)param[pindex], "-o", 3) == 0) {
265 else if (strncmp((char *)param[pindex], "-r", 3) == 0) {
269 else if (strncmp((char *)param[pindex], "-rb", 4) == 0) {
273 else if (strncmp((char *)param[pindex], "-tb", 4) == 0) {
279 struct netif *netif = rpp_eth_get_netif(INTERFACE_INSTANCE_NUMBER);
281 rpp_sci_printf("netif err\n");
287 input = rpp_sci_getc();
289 if (input == 'b') { /* print buffer descriptor at given address */
290 rpp_sci_printf("-? 0x");
291 bufferDescriptor = (struct emac_tx_bd *)readNum(8,8,TRUE);
292 rpp_sci_printf("\n");
293 /* check it fits EMAC CPPI RAM */
294 /*if(bufferDescriptor >= EMAC_CTRL_RAM_BASE_m(INTERFACE_INSTANCE_NUMBER) && (bufferDescriptor + sizeof(struct emac_tx_bd)) <= EMAC_CTRL_RAM_BASE_m(INTERFACE_INSTANCE_NUMBER) + SIZE_EMAC_CTRL_RAM)
296 print_bd_content(bufferDescriptor, FALSE);
300 rpp_sci_printf("address not from BD CPPI RAM range\n");
303 input = rpp_sci_getc();
304 if (input == 'n') { /* next */
305 bufferDescriptor = bufferDescriptor->next;
306 if (bufferDescriptor != NULL)
307 print_bd_content(bufferDescriptor, FALSE);
309 rpp_sci_printf("NULL\n");
313 else if (input == 'p') { /* previous */
314 bufferDescriptor = findPreviousBD(bufferDescriptor);
315 if ((int32_t)bufferDescriptor != -1) {
316 if (bufferDescriptor == NULL) {
317 rpp_sci_printf("NULL\n");
321 print_bd_content(bufferDescriptor, FALSE);
325 rpp_sci_printf("not found\n");
329 else if (input == 'r') { /* reprint */
330 if ((int32_t)bufferDescriptor == -1 || bufferDescriptor == NULL) break;
331 print_bd_content(bufferDescriptor, FALSE);
337 if (input == 'q') { /* quit */
342 else if (input == 's') /* general statistics */
344 else if (input == 't') { /* print tx channel */
345 print_tx_channel_stat();
348 else if (input == 'r') { /* print rx channel */
349 print_rx_channel_stat();
353 else if (input == 'a') { /* autocheck consistency of buffer descriptors */
356 else if (input == 'o') { /* overview */
357 uint8_t mode = rpp_sci_getc() - '0';
358 uint8_t param = rpp_sci_getc() - '0';
359 print_bds_state(mode, param);
361 else if (input == 'c') /* convert */
362 rpp_sci_printf("0x%08x", bd_addr((uint16_t)readNum(1, 3, FALSE)));
367 rpp_sci_printf(" --- OVERVIEWS ---\n");
368 rpp_sci_printf("bd->next points to NULL\n");
369 print_bds_state(1, 0);
370 rpp_sci_printf("bds with EMAC_DSC_FLAG_SOP\n");
371 print_bds_state(0, 0);
372 rpp_sci_printf("bds with EMAC_DSC_FLAG_EOP\n");
373 print_bds_state(0, 1);
374 rpp_sci_printf("bds with EMAC_DSC_FLAG_OWNER\n");
375 print_bds_state(0, 2);
376 rpp_sci_printf("bds with EMAC_DSC_FLAG_EOQ\n");
377 print_bds_state(0, 3);
378 rpp_sci_printf("bds with EMAC_DSC_FLAG_TDOWNCMPLT\n");
379 print_bds_state(0, 4);
380 rpp_sci_printf("bds with EMAC_DSC_FLAG_PASSCRC\n");
381 print_bds_state(0, 5);
382 rpp_sci_printf("\nPress any key ...\r");
384 /* These flags are not so much important */
385 rpp_sci_printf("bds with EMAC_DSC_FLAG_JABBER\n");
386 print_bds_state(0, 6);
387 rpp_sci_printf("bds with EMAC_DSC_FLAG_OVERSIZE\n");
388 print_bds_state(0, 7);
389 rpp_sci_printf("bds with EMAC_DSC_FLAG_FRAGMENT\n");
390 print_bds_state(0, 8);
391 rpp_sci_printf("bds with EMAC_DSC_FLAG_UNDERSIZED\n");
392 print_bds_state(0, 9);
393 rpp_sci_printf("bds with EMAC_DSC_FLAG_CONTROL\n");
394 print_bds_state(0, 10);
395 rpp_sci_printf("bds with EMAC_DSC_FLAG_OVERRUN\n");
396 print_bds_state(0, 11);
397 rpp_sci_printf("bds with EMAC_DSC_FLAG_CODEERROR\n");
398 print_bds_state(0, 12);
399 rpp_sci_printf("bds with EMAC_DSC_FLAG_ALIGNERROR\n");
400 print_bds_state(0, 13);
401 rpp_sci_printf("bds with EMAC_DSC_FLAG_CRCERROR\n");
402 print_bds_state(0, 14);
403 rpp_sci_printf("bds with EMAC_DSC_FLAG_NOMATCH\n");
404 print_bds_state(0, 15);
408 rpp_sci_printf(" --- NUMBER OF ISR RUNS ---\n");
409 rpp_sci_printf("TxIsr: %d", countEMACCore0TxIsr);
410 rpp_sci_printf("RxIsr: %d", countEMACCore0RxIsr);
420 int cmd_do_dhcp(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
422 if (!isPostInitialized()) {
423 rpp_sci_printf("Eth not initialized run 'ethinit' command first.\n");
426 rpp_sci_printf("not implemented yet.");
431 #if LWIP_STATS_DISPLAY
432 int cmd_do_lwip(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
434 if (!isPostInitialized()) {
435 rpp_sci_printf("Eth not initialized run 'ethinit' command first.\n");
439 rpp_sci_printf("\n");
444 int cmd_do_ethinit(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
446 int8_t retVal = rpp_eth_init_postInit(0, NULL); /* post OS startup init of eth (LwIP) for application usage -- waits for a while, for auto-negotiation of ethernet speed and DHCP if used */
450 rpp_sci_printf("already initialized\n");
453 rpp_sci_printf("initialization of physical part of ethernet failed\n");
456 rpp_sci_printf("DHCP couldn't be started due to insufficient memory\n");
459 rpp_sci_printf("cable is not connected\n");
462 rpp_sci_printf("ethinit DONE\n");
469 cmd_des_t const cmd_des_linkstat = {
471 "ethlinkstat","Print current status of ethernet interface",
473 "### Command syntax ###\n"
477 "### Description ###\n"
479 "This command reads PHY link status assigned to ethernet interface and\n"
480 "prints interface name and informs about PHY's status to the output.\n"
489 CMD_HANDLER(cmd_do_read_linkstat), (void *)&cmd_list_netstats
492 cmd_des_t const cmd_des_mac = {
494 "ethmac","Print current MAC address of ethernet interface",
496 "### Command syntax ###\n"
500 "### Description ###\n"
502 "This command obtains MAC address from ethernet interface structure\n"
503 "and prints it to the output.\n"
508 " 12:34:56:78:9a:bc\n",
509 CMD_HANDLER(cmd_do_read_mac), (void *)&cmd_list_netstats
512 cmd_des_t const cmd_des_ip = {
514 "ethip","Print current IP address of network interface",
516 "### Command syntax ###\n"
520 "### Description ###\n"
522 "This command reads current IP address, netmask and gateway of network\n"
523 "interface and prints these to the output.\n"
528 " Address: 192.168.247.1\n"
529 " Netmask: 255.255.255.0\n"
530 " Gateway: 192.168.247.255\n",
531 CMD_HANDLER(cmd_do_read_ip), (void *)&cmd_list_netstats
534 cmd_des_t const cmd_des_bufferdescriptors = {
536 "ethbd","Examine emac buffer descriptors",
538 "### Command syntax ###\n"
542 "### Description ###\n"
543 "After startup you use keys to control what will be done.\n"
546 "- s - general statistics\n"
547 "- t - transmit channel status\n"
548 "- r - receive channel status\n"
549 "- b - after giving address of bd it prints bd content\n"
550 "- a - checks consistency of buffer descriptors\n"
556 CMD_HANDLER(cmd_do_bufferdescriptors), (void *)&cmd_list_netstats
560 cmd_des_t const cmd_des_dhcp = {
562 "dhcp","Prints and controls DHCP",
564 "### Command syntax ###\n"
568 "### Description ###\n"
577 CMD_HANDLER(cmd_do_dhcp), (void *)&cmd_list_netstats
581 #if LWIP_STATS_DISPLAY
582 cmd_des_t const cmd_des_lwip = {
584 "lwip","Prints statistics for the LwIP stack",
586 "### Command syntax ###\n"
590 "### Description ###\n"
592 "Sections printed:\n"
603 "MEM TCP_PCB_LISTEN\n"
607 "MEM TCPIP_MSG_API\n"
608 "MEM TCPIP_MSG_INPKT\n"
614 "Shown sections depends on modules compiled in.\n"
616 CMD_HANDLER(cmd_do_lwip), (void *)&cmd_list_netstats
618 #endif /* if LWIP_STATS_DISPLAY */
620 cmd_des_t const cmd_des_ethinit = {
622 "ethinit","Post OS startup eth initialization",
624 "### Command syntax ###\n"
628 "### Description ###\n"
629 "This command finishes autonegotiation of PHY and initialize LwIP stack.\n"
636 CMD_HANDLER(cmd_do_ethinit), (void *)&cmd_list_netstats
639 /** List of commands for lwip, defined as external */
640 cmd_des_t const *cmd_list_netstats[] = {
644 &cmd_des_bufferdescriptors,
648 #if LWIP_STATS_DISPLAY