⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 debugconsole.c

📁 WWVB receiver using AVR.
💻 C
字号:
/* $Id: debugconsole.c,v 1.20 2006/01/19 20:51:58 simimeie Exp $ * Functions for a serial debug console. If DEBUGCONSOLE is not defined in * the Makefile, then this will not be compiled or linked in. */ /* Unfortunately, an online-help for the debug commands would eat way too * much flash memory, so there is no online help. * Only documentation is here: * List of possible commands. * Each "command" consists of a single character, followed by it's * parameters. _CASE DOES MATTER!_. Numbers usually have to be given in * hexadecimal, and are returned that way too. *  m a        shows memory at address a *  M a v      writes value v to memory address a *  t          shows the current "ticks" value (a 16 bit timestamp) *  d          shows decoded DCF time *  B v        sets LED brightness *  i          show configured IP setting(s) *  n          show net status (MAC, traffic) *  O a v      temporarily overrides LED display. you first have to set the *             digits with a = 0 to 6, a = 7 then activates it for 5 secs */#define BV _BV#include <avr/io.h>#include "debugconsole.h"#include "timers.h"#include "dcf77.h"#include "ledmodule.h"#include "networkstack.h"#include "timeformat.h"/* PD0 is RXD, PD1 is TXD, but we shouldn't need to adress them manually *//* Formula for calculating the value of UBRR from baudrate and cpufreq */#define UBRRCALC ((CPUFREQ / (16 * BAUDRATE)) - 1)#ifdef EXTMEMORY#define INPUTBUFSIZE 20#define OUTPUTBUFSIZE 250#else#define INPUTBUFSIZE 10#define OUTPUTBUFSIZE 32#endifstatic uint8_t inputbuf[INPUTBUFSIZE];static uint8_t outputbuf[OUTPUTBUFSIZE];static uint8_t inputpos;static uint8_t outputsta;static uint8_t outputend;static uint8_t PROMPT[] = "\r\nC:\\>";static void appendchar(uint8_t what) {	uint8_t newpos;	newpos = (outputend + 1) % OUTPUTBUFSIZE;	if (newpos != outputsta) {		outputbuf[outputend] = what;		outputend = newpos;	}}void debugconsole_printtext(uint8_t * what) {	while (*what != 0) {		appendchar(*what);		what++;	}}void debugconsole_init(void) {	/* Set Baud Rate */	UBRRL = UBRRCALC;	/* Enable Send and Receive, but no IRQs */	UCSRB = BV(TXEN) | BV(RXEN);	debugconsole_printtext(PROMPT);	return;}static uint16_t parsehex16(uint8_t start, uint8_t * end) {	uint16_t res = 0;	uint8_t tmp;	while (start < inputpos) {		tmp = inputbuf[start++];		if ((tmp >= '0') && (tmp <= '9')) {			res <<= 4; /* *16 */			res += tmp - '0';		} else if ((tmp >= 'A') && (tmp <= 'F')) {			res <<= 4; /* *16 */			res += tmp - 'A' + 10;		} else if ((tmp >= 'a') && (tmp <= 'f')) {			res <<= 4; /* *16 */			res += tmp - 'a' + 10;		} else {			break;		}	}	*end = start;	return res;}void debugconsole_printhex8(uint8_t what) {	uint8_t buf;	uint8_t i;	for (i=0; i<2; i++) {		buf = (uint8_t)(what & (uint8_t)0xF0) >> 4;		if (buf <= 9) {			buf += '0';		} else {			buf += 'A' - 10;		}		appendchar(buf);		what <<= 4;	}}static void printdec32(uint32_t what) {	uint32_t f = 1000000000;	uint8_t alrpr = 0;	while (f > 0) {		if ((alrpr) || (what / f) || (f == 1)) {			appendchar((what / f) + '0');			alrpr = 1;		}		what = what % f;		f /= 10;	}}static void printdec8b(uint8_t what) {	appendchar((what / 10) + '0');	appendchar((what % 10) + '0');}void debugconsole_work(void) {	uint8_t status = UCSRA; /* Read status */	if (status & BV(RXC)) {		uint8_t val = UDR;		switch (val) {		case 8: /* Backspace? */			if (inputpos > 0) {				inputpos--;				appendchar(val);			}			break;		case '\r':		case '\n':			/* Warning: inputbuf is not null terminated! */			appendchar('\r');			appendchar('\n');			if (inputpos != 0) {				uint8_t parspos = 2;				uint16_t addr;				uint8_t val;				struct tickdata td;				struct brokentime bt;				switch(inputbuf[0]) {#ifdef LEDMODULE				case 'B':					/* B v - sets LED brightness to value					 * v (valid values are 0 - 7) */					led_brightness = parsehex16(parspos, &parspos);					set_led_brightness();				case 'O':					/* O a v - temporarily overwrites LED display */					addr = parsehex16(parspos, &parspos);					val = parsehex16(parspos, &parspos);					if (addr < 7) {						led_dispoverride[addr] = val;					} else {						led_dispovcnt = ((val) ? val : 30);					}					break;#endif				case 'm':					/* m a - shows memory at address a */					addr = parsehex16(parspos, &parspos);					val = *((uint8_t *)addr);					debugconsole_printhex8(addr >> 8);					debugconsole_printhex8(addr & 0x00FF);					appendchar(' ');					appendchar('=');					appendchar(' ');					debugconsole_printhex8(val);					break;				case 'M':					/* M a v - writes value v to memory address a */					addr = parsehex16(parspos, &parspos);					val = parsehex16(parspos, &parspos);					*((uint8_t *)addr) = val;					debugconsole_printhex8(addr >> 8);					debugconsole_printhex8(addr & 0x00FF);					appendchar(' ');					appendchar(':');					appendchar('=');					appendchar(' ');					debugconsole_printhex8(val);					break;				case 't':					/* t - shows "ticks" */					gettickdata(&td);					debugconsole_printhex8(td.seconds >> 8);					debugconsole_printhex8(td.seconds & 0x00FF);					appendchar('s');					appendchar(' ');					debugconsole_printhex8(td.ticks >> 8);					debugconsole_printhex8(td.ticks & 0x00FF);					debugconsole_printtext("t");					break;				case 'd':					/* d - show dcf77 time */					val = dcfvalid;					tstobt(dcftime[val].timestamp						+ ((uint16_t)(getseconds() - dcftime[val].ticksecs)), &bt);					printdec32(bt.day);					appendchar('.');					printdec32(bt.month);					appendchar('.');					printdec8b(bt.year);					appendchar(' ');					printdec8b(bt.hour);					appendchar(':');					printdec8b(bt.min);					appendchar(':');					printdec8b(bt.sec);					appendchar(' ');					printdec32(dcftime[val].timestamp						 + ((uint16_t)(getseconds() - dcftime[val].ticksecs)));					break;#ifdef ISANETWORK				case 'i':					/* i - Show IP settings */					printdec32(net_ip[0]);					appendchar('.');					printdec32(net_ip[1]);					appendchar('.');					printdec32(net_ip[2]);					appendchar('.');					printdec32(net_ip[3]);#if 0					appendchar('/');					printdec32(net_mask[0]);					appendchar('.');					printdec32(net_mask[1]);					appendchar('.');					printdec32(net_mask[2]);					appendchar('.');					printdec32(net_mask[3]);					appendchar(' ');					appendchar('g');					appendchar('w');					appendchar(' ');					printdec32(net_gate[0]);					appendchar('.');					printdec32(net_gate[1]);					appendchar('.');					printdec32(net_gate[2]);					appendchar('.');					printdec32(net_gate[3]);#endif					break;				case 'n':					/* n a - Show net status */					addr = parsehex16(parspos, &parspos);					if (addr == 0) {						for (val = 0; val < 5; val++) {							debugconsole_printhex8(net_mac[val]);							appendchar(':');						}						debugconsole_printhex8(net_mac[5]);#ifdef NETWORKSTATS					} else if (addr == 1) {						appendchar('R');						appendchar('X');						appendchar(' ');						printdec32(net_rxbytes);					} else if (addr == 2) {						appendchar('T');						appendchar('X');						appendchar(' ');						printdec32(net_txbytes);					} else if (addr == 3) {						appendchar('P');						appendchar(' ');						printdec32(net_pings);					} else if (addr == 4) {						appendchar('N');						appendchar('T');						appendchar('P');						appendchar(' ');						printdec32(net_ntpqs);					} else if (addr == 5) {						appendchar('R');						appendchar('C');						appendchar(' ');						printdec32(net_rcons);#endif					}					break;#endif				default:					debugconsole_printtext("Unknown command.");				};			}			debugconsole_printtext(PROMPT);			inputpos = 0;			break;		default:			if (inputpos < INPUTBUFSIZE) {				inputbuf[inputpos++] = val;				/* Echo the character */				appendchar(val);			} else {				appendchar(7); /* Bell */			}		};	}	if (outputend != outputsta) {		/* We have something to send - can we send right now? */		if (status & BV(UDRE)) { /* transmitter is ready. */			UDR = outputbuf[outputsta];			outputsta = (outputsta + 1) % OUTPUTBUFSIZE;		}	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -