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 program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * File : cmd_netstats.c
25 * This file contains commands for net statistics (ethernet interface and LwIP)
29 #include "cmd_netstats.h"
38 #include "lwip/stats.h"
40 #define INTERFACE_INSTANCE_NUMBER 0
42 int cmd_do_read_linkstat(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
44 if (!isPostInitialized()) {
45 rpp_sci_printf("Eth not initialized run 'ethinit' command first.\n");
48 struct netif *netif = rpp_eth_get_netif(INTERFACE_INSTANCE_NUMBER);
49 if (rpp_eth_phylinkstat(INTERFACE_INSTANCE_NUMBER))
50 rpp_sci_printf("%c%c%d : UP\r\n", netif->name[0], netif->name[1], netif->num);
52 rpp_sci_printf("%c%c%d : DOWN\r\n", netif->name[0], netif->name[1], netif->num);
56 int cmd_do_read_mac(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
58 if (!isPostInitialized()) {
59 rpp_sci_printf("Eth not initialized run 'ethinit' command first.\n");
63 rpp_eth_get_macAddrStr(INTERFACE_INSTANCE_NUMBER,macaddr);
64 rpp_sci_printf((const char *)"%s\n", macaddr);
68 int cmd_do_read_ip(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
70 if (!isPostInitialized()) {
71 rpp_sci_printf("Eth not initialized run 'ethinit' command first.\n");
74 struct netif *netif = rpp_eth_get_netif(INTERFACE_INSTANCE_NUMBER);
76 rpp_eth_getIPDecimalStr(netif->ip_addr, ipString);
77 rpp_sci_printf("Address: %s\r\n", ipString);
78 rpp_eth_getIPDecimalStr(netif->netmask, ipString);
79 rpp_sci_printf("Netmask: %s\r\n", ipString);
80 rpp_eth_getIPDecimalStr(netif->gw, ipString);
81 rpp_sci_printf("Gateway: %s\r\n", ipString);
88 /* --- EMAC DESCRIPTOR FORMAT ---
91 * OFFSET | 31 16 | 15 0
92 * ------------------------------------------------------------------------------------
93 * 0 | *Next Descriptor Pointer
94 * 1 | *Data Buffer Pointer
95 * 2 | Buffer Offset | Buffer Length
96 * 3 | Flags | Packet Length
100 boolean_t bd_SOP(volatile struct emac_tx_bd *bufferDescriptor)
102 return (bufferDescriptor->flags_pktlen & EMAC_DSC_FLAG_SOP);
105 boolean_t bd_EOP(volatile struct emac_tx_bd *bufferDescriptor)
107 return (bufferDescriptor->flags_pktlen & EMAC_DSC_FLAG_EOP);
110 boolean_t bd_OWNER(volatile struct emac_tx_bd *bufferDescriptor)
112 return (bufferDescriptor->flags_pktlen & EMAC_DSC_FLAG_OWNER);
115 boolean_t bd_EOQ(volatile struct emac_tx_bd *bufferDescriptor)
117 return (bufferDescriptor->flags_pktlen & EMAC_DSC_FLAG_EOQ);
120 uint32_t bd_addr(uint16_t bd_num)
122 return EMAC_CTRL_RAM_BASE_m(0) + bd_num*sizeof(struct emac_tx_bd);
125 volatile struct emac_tx_bd *findPreviousBD(volatile struct emac_tx_bd *bufferDescriptor)
127 volatile struct emac_tx_bd *temporary = bufferDescriptor - 1;
129 /* first try space before given bd */
130 if ((uint32_t)temporary >= EMAC_CTRL_RAM_BASE_m(0) && temporary->next == bufferDescriptor)
132 temporary = bufferDescriptor->next;
133 while (temporary != bufferDescriptor && temporary != NULL) {
134 if (temporary->next == bufferDescriptor)
136 temporary = (temporary + 1); /* going through memory, if you want to go through bd chains put here 'temporary = temporary->next;' */
137 if ( (uint32_t)(temporary+1) > EMAC_CTRL_RAM_BASE_m(0)+SIZE_EMAC_CTRL_RAM) /* when out of CPPI RAM */
138 temporary = (struct emac_tx_bd *)EMAC_CTRL_RAM_BASE_m(0); /* continue scanning from the beginning */
140 return (struct emac_tx_bd *)-1; /* not found */
143 void print_bd_content(volatile struct emac_tx_bd *bufferDescriptor, boolean_t humanReadable)
147 rpp_sci_printf("Buffer Descriptor at 0x%08x\n", bufferDescriptor);
148 rpp_sci_printf("Next BD:\t0x%08x\n", bufferDescriptor->next);
149 rpp_sci_printf("Data ptr:\t0x%08x\n", bufferDescriptor->bufptr);
150 rpp_sci_printf("Buf offset:\t%d\tBuf length:\t%d\n", (bufferDescriptor->bufoff_len >> 16), (bufferDescriptor->bufoff_len & 0xffff));
152 rpp_sci_printf("Flags:\t\t0x%04x\t", (bufferDescriptor->flags_pktlen >> 16));
155 data = bufferDescriptor->flags_pktlen;
156 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" : "");
158 rpp_sci_printf("Pkt length:\t%d\n", (bufferDescriptor->flags_pktlen & 0xffff));
159 rpp_sci_printf("Pbuf ptr:\t0x%08x\n", bufferDescriptor->pbuf);
163 * mode: 0 flags -> param says which flag (0-15)
164 * 1 bd->next == NULL, no param
166 void print_bds_state(uint8_t mode, uint8_t param)
168 volatile struct emac_tx_bd *bufferDescriptor = (struct emac_tx_bd *)EMAC_CTRL_RAM_BASE_m(0);
171 rpp_sci_printf("TX:");
172 while (index < 409) {
175 if (!(index%50)) rpp_sci_printf("\n");
178 rpp_sci_printf("\nRX: ");
180 if (bufferDescriptor->flags_pktlen & ((1 << 15) >> (param%16)))
185 else if (mode == 1) {
186 if (bufferDescriptor->next == NULL)
195 rpp_sci_printf("\n");
200 unsigned long readNum(uint8_t minamount, uint8_t maxamount, boolean_t hex)
202 unsigned long out = 0;
203 uint8_t input_buffer[BUF_SIZE];
204 uint8_t buf_index = 0;
208 input = rpp_sci_getc();
209 if (input == '\b' && buf_index > 0) {
210 input_buffer[buf_index] = '\0';
216 else if (buf_index >= maxamount)
218 else if (input <= '9' && input >= '0') {
219 input_buffer[buf_index] = input - '0';
223 else if (hex && (input <= 'f' && input >= 'a')) {
224 input_buffer[buf_index] = input - ('a' - 10);
228 else if (hex && (input <= 'F' && input >= 'A')) {
229 input_buffer[buf_index] = input - ('A' - 10);
230 echo((input + ('a' - 'A')));
233 } while ( (input != '\r' && input != '\n' && buf_index < BUF_SIZE) || buf_index < minamount);
234 input = 0; /* use as index */
235 while (input < buf_index) {
237 out = (out << 4) + input_buffer[input];
239 out = (out*10) + input_buffer[input];
245 int cmd_do_bufferdescriptors(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
249 volatile struct emac_tx_bd *bufferDescriptor;
250 boolean_t run = TRUE, interactive = FALSE, overview = FALSE;
253 boolean_t isrRun = FALSE, rb = FALSE, tb = FALSE;
256 /* if eth not initialized return */
257 if (!isPostInitialized()) {
258 rpp_sci_printf("Eth not initialized run 'ethinit' command first.\n");
262 /* examine parameters */
263 for (pindex = 1; param[pindex] != 0; pindex++) {
264 if (strncmp((char *)param[pindex], "-i", 3) == 0) {
268 else if (strncmp((char *)param[pindex], "-o", 3) == 0) {
273 else if (strncmp((char *)param[pindex], "-r", 3) == 0) {
277 else if (strncmp((char *)param[pindex], "-rb", 4) == 0) {
281 else if (strncmp((char *)param[pindex], "-tb", 4) == 0) {
287 struct netif *netif = rpp_eth_get_netif(INTERFACE_INSTANCE_NUMBER);
289 rpp_sci_printf("netif err\n");
295 input = rpp_sci_getc();
297 if (input == 'b') { /* print buffer descriptor at given address */
298 rpp_sci_printf("-? 0x");
299 bufferDescriptor = (struct emac_tx_bd *)readNum(8,8,TRUE);
300 rpp_sci_printf("\n");
301 /* check it fits EMAC CPPI RAM */
302 /*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)
304 print_bd_content(bufferDescriptor, FALSE);
308 rpp_sci_printf("address not from BD CPPI RAM range\n");
311 input = rpp_sci_getc();
312 if (input == 'n') { /* next */
313 bufferDescriptor = bufferDescriptor->next;
314 if (bufferDescriptor != NULL)
315 print_bd_content(bufferDescriptor, FALSE);
317 rpp_sci_printf("NULL\n");
321 else if (input == 'p') { /* previous */
322 bufferDescriptor = findPreviousBD(bufferDescriptor);
323 if ((int32_t)bufferDescriptor != -1) {
324 if (bufferDescriptor == NULL) {
325 rpp_sci_printf("NULL\n");
329 print_bd_content(bufferDescriptor, FALSE);
333 rpp_sci_printf("not found\n");
337 else if (input == 'r') { /* reprint */
338 if ((int32_t)bufferDescriptor == -1 || bufferDescriptor == NULL) break;
339 print_bd_content(bufferDescriptor, FALSE);
345 if (input == 'q') { /* quit */
350 else if (input == 's') /* general statistics */
352 else if (input == 't') { /* print tx channel */
353 print_tx_channel_stat();
356 else if (input == 'r') { /* print rx channel */
357 print_rx_channel_stat();
361 else if (input == 'a') { /* autocheck consistency of buffer descriptors */
364 else if (input == 'o') { /* overview */
365 uint8_t mode = rpp_sci_getc() - '0';
366 uint8_t param = rpp_sci_getc() - '0';
367 print_bds_state(mode, param);
369 else if (input == 'c') /* convert */
370 rpp_sci_printf("0x%08x", bd_addr((uint16_t)readNum(1, 3, FALSE)));
375 rpp_sci_printf(" --- OVERVIEWS ---\n");
376 rpp_sci_printf("bd->next points to NULL\n");
377 print_bds_state(1, 0);
378 rpp_sci_printf("bds with EMAC_DSC_FLAG_SOP\n");
379 print_bds_state(0, 0);
380 rpp_sci_printf("bds with EMAC_DSC_FLAG_EOP\n");
381 print_bds_state(0, 1);
382 rpp_sci_printf("bds with EMAC_DSC_FLAG_OWNER\n");
383 print_bds_state(0, 2);
384 rpp_sci_printf("bds with EMAC_DSC_FLAG_EOQ\n");
385 print_bds_state(0, 3);
386 rpp_sci_printf("bds with EMAC_DSC_FLAG_TDOWNCMPLT\n");
387 print_bds_state(0, 4);
388 rpp_sci_printf("bds with EMAC_DSC_FLAG_PASSCRC\n");
389 print_bds_state(0, 5);
390 rpp_sci_printf("\nPress any key ...\r");
392 /* These flags are not so much important */
393 rpp_sci_printf("bds with EMAC_DSC_FLAG_JABBER\n");
394 print_bds_state(0, 6);
395 rpp_sci_printf("bds with EMAC_DSC_FLAG_OVERSIZE\n");
396 print_bds_state(0, 7);
397 rpp_sci_printf("bds with EMAC_DSC_FLAG_FRAGMENT\n");
398 print_bds_state(0, 8);
399 rpp_sci_printf("bds with EMAC_DSC_FLAG_UNDERSIZED\n");
400 print_bds_state(0, 9);
401 rpp_sci_printf("bds with EMAC_DSC_FLAG_CONTROL\n");
402 print_bds_state(0, 10);
403 rpp_sci_printf("bds with EMAC_DSC_FLAG_OVERRUN\n");
404 print_bds_state(0, 11);
405 rpp_sci_printf("bds with EMAC_DSC_FLAG_CODEERROR\n");
406 print_bds_state(0, 12);
407 rpp_sci_printf("bds with EMAC_DSC_FLAG_ALIGNERROR\n");
408 print_bds_state(0, 13);
409 rpp_sci_printf("bds with EMAC_DSC_FLAG_CRCERROR\n");
410 print_bds_state(0, 14);
411 rpp_sci_printf("bds with EMAC_DSC_FLAG_NOMATCH\n");
412 print_bds_state(0, 15);
416 rpp_sci_printf(" --- NUMBER OF ISR RUNS ---\n");
417 rpp_sci_printf("TxIsr: %d", countEMACCore0TxIsr);
418 rpp_sci_printf("RxIsr: %d", countEMACCore0RxIsr);
428 int cmd_do_dhcp(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
430 if (!isPostInitialized()) {
431 rpp_sci_printf("Eth not initialized run 'ethinit' command first.\n");
434 rpp_sci_printf("not implemented yet.");
439 #if LWIP_STATS_DISPLAY
440 int cmd_do_lwip(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
442 if (!isPostInitialized()) {
443 rpp_sci_printf("Eth not initialized run 'ethinit' command first.\n");
447 rpp_sci_printf("\n");
452 int cmd_do_ethinit(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
454 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 */
458 rpp_sci_printf("already initialized\n");
461 rpp_sci_printf("initialization of physical part of ethernet failed\n");
464 rpp_sci_printf("DHCP couldn't be started due to insufficient memory\n");
467 rpp_sci_printf("cable is not connected\n");
470 rpp_sci_printf("ethinit DONE\n");
477 cmd_des_t const cmd_des_linkstat = {
479 "ethlinkstat","Print current status of ethernet interface",
481 "### Command syntax ###\n"
485 "### Description ###\n"
487 "This command reads PHY link status assigned to ethernet interface and\n"
488 "prints interface name and informs about PHY's status to the output.\n"
497 CMD_HANDLER(cmd_do_read_linkstat), (void *)&cmd_list_netstats
500 cmd_des_t const cmd_des_mac = {
502 "ethmac","Print current MAC address of ethernet interface",
504 "### Command syntax ###\n"
508 "### Description ###\n"
510 "This command obtains MAC address from ethernet interface structure\n"
511 "and prints it to the output.\n"
516 " 12:34:56:78:9a:bc\n",
517 CMD_HANDLER(cmd_do_read_mac), (void *)&cmd_list_netstats
520 cmd_des_t const cmd_des_ip = {
522 "ethip","Print current IP address of network interface",
524 "### Command syntax ###\n"
528 "### Description ###\n"
530 "This command reads current IP address, netmask and gateway of network\n"
531 "interface and prints these to the output.\n"
536 " Address: 192.168.247.1\n"
537 " Netmask: 255.255.255.0\n"
538 " Gateway: 192.168.247.255\n",
539 CMD_HANDLER(cmd_do_read_ip), (void *)&cmd_list_netstats
542 cmd_des_t const cmd_des_bufferdescriptors = {
544 "ethbd","Examine emac buffer descriptors",
546 "### Command syntax ###\n"
550 "### Description ###\n"
551 "After startup you use keys to control what will be done.\n"
554 "- s - general statistics\n"
555 "- t - transmit channel status\n"
556 "- r - receive channel status\n"
557 "- b - after giving address of bd it prints bd content\n"
558 "- a - checks consistency of buffer descriptors\n"
564 CMD_HANDLER(cmd_do_bufferdescriptors), (void *)&cmd_list_netstats
568 cmd_des_t const cmd_des_dhcp = {
570 "dhcp","Prints and controls DHCP",
572 "### Command syntax ###\n"
576 "### Description ###\n"
585 CMD_HANDLER(cmd_do_dhcp), (void *)&cmd_list_netstats
589 #if LWIP_STATS_DISPLAY
590 cmd_des_t const cmd_des_lwip = {
592 "lwip","Prints statistics for the LwIP stack",
594 "### Command syntax ###\n"
598 "### Description ###\n"
600 "Sections printed:\n"
611 "MEM TCP_PCB_LISTEN\n"
615 "MEM TCPIP_MSG_API\n"
616 "MEM TCPIP_MSG_INPKT\n"
622 "Shown sections depends on modules compiled in.\n"
624 CMD_HANDLER(cmd_do_lwip), (void *)&cmd_list_netstats
626 #endif /* if LWIP_STATS_DISPLAY */
628 cmd_des_t const cmd_des_ethinit = {
630 "ethinit","Post OS startup eth initialization",
632 "### Command syntax ###\n"
636 "### Description ###\n"
637 "This command finishes autonegotiation of PHY and initialize LwIP stack.\n"
644 CMD_HANDLER(cmd_do_ethinit), (void *)&cmd_list_netstats
647 /** List of commands for lwip, defined as external */
648 cmd_des_t const *cmd_list_netstats[] = {
652 &cmd_des_bufferdescriptors,
656 #if LWIP_STATS_DISPLAY