📄 uartdebug.c
字号:
/*= uartdebug.c ================================================================
*
* Copyright (C) 2005 Nordic Semiconductor
*
* This file is distributed in the hope that it will be useful, but WITHOUT
* WARRANTY OF ANY KIND.
*
* Author(s): B鴕ge Strand
*
* Description:
*
* Debug information via UART
* General file, no reference to specific hardware
* All functions named "db_..." for Debug
*
* Compiler: Tested with WinAVR, avr-gcc (GCC) 3.4.3
*
* Revision: 1.0
*
*==============================================================================
*/
#include "uartdebug.h"
// Some forward declarations of functions that are not available externally
int db_is_ascii_hex(int c);
char db_ascii_to_hex(int c0, int c1);
char db_gethex(int c0);
// Read and display a single byte from Z1 or RX EEPROM
void db_singleread(char adr) {
// Reading from Z1 ATX register
DB_PRINTF (PSTR ("\r\n(%02X) %02X"), adr, z1_singleread(adr)); // Display result
}
// Write a single byte to Z1 or RX EEPROM, then read back to uart terminal
void db_singlewrite(char adr, char data) {
// Reading from Z1 ATX register
z1_singlewrite(adr, data); // Perform the hardware write access
db_singleread(adr); // Display result
}
// Read multiple bytes from Z1
void db_multiread(char startadr, char endadr) {
extern char slaveinbuf[]; // Global data buffer from Z1 slave to MCU
int n=0; // A local counter
z1_multiread(startadr, endadr); // Perform hardware read access
if (endadr - startadr < SLAVEBUFSIZE) { // Display result
DB_PRINTF (PSTR ("(%02X) "), startadr);
while (startadr <= endadr) {
DB_PRINTF (PSTR ("%02X "), slaveinbuf[n++]);
startadr++;
}
DB_PRINTF (PSTR ("\r\n"));
}
}
// Prompt for multiple bytes to write to Z1
void db_multiwrite(char startadr, char endadr) {
extern char slaveoutbuf[]; // Global data buffer from MCU to Z1 slave
extern char error; // Global error register
int data, temp, n=0;
char c0;
temp = startadr; // We need a little backup of this one
if (endadr - startadr < SLAVEBUFSIZE) {
error = 0;
while ((startadr <= endadr) && (error ==0)) {
DB_PRINTF (PSTR ("\r\n%02X="), startadr++); // Prompt for data
data = db_gethex(getchar()); // Get user input
if (error == 0){
c0 = getchar();
if ((c0 == '\r') || (c0 == '\n'))
slaveoutbuf[n++] = data; // Confirming enter -> write to buffer!
}
}
if (error == 0) {
z1_multiwrite(temp, endadr); // Hardware write
DB_PRINTF (PSTR ("\r\n")); // Present result on new line
db_multiread(temp, endadr); // Display result
}
}
}
// Is c an ascii representation of a hex number?
int db_is_ascii_hex(int c) {
if ((c >= '0') & (c <= '9')) // Numbers 0 through 9
return 1;
if ((c >= 'A') & (c <= 'F')) // A through F
return 1;
if ((c >= 'a') & (c <= 'f') ) // a through f
return 1;
return 0;
}
// Convert the characters c0, c1 into hex number h
char db_ascii_to_hex(int c0, int c1) {
char h;
h = 0;
// Most significant hex number
if ((c0 >= '0') && (c0 <= '9')) // 0-9 -> 0-9
h = c0 - '0';
else if ((c0 >= 'A') && (c0 <= 'F')) // A-F -> 10-15
h = c0 - 'A' + 10;
else if ((c0 >= 'a') && (c0 <= 'f')) // a-f -> 10-15
h = c0 - 'a' + 10;
h <<= 4; // Shift 4 MSBs up
// Least significant hex number
if ((c1 >= '0') && (c1 <= '9')) // 0-9 -> 0-9
h = h + c1 - '0';
else if ((c1 >= 'A') && (c1 <= 'F')) // A-F -> 10-15
h = h + c1 - 'A' + 10;
else if ((c1 >= 'a') && (c1 <= 'f')) // a-f -> 10-15
h = h + c1 - 'a' + 10;
return h;
}
// Get a hex character from the terminal
char db_gethex(int c0) { // c0 already fetched by debug interface
extern char error; // Global error register
int c1; // Input characters from the terminal
error = 0; // NO error detected yet
if (db_is_ascii_hex(c0)) { // If it's an ascii hex number,
c1 = getchar(); // Get second character
if (db_is_ascii_hex(c1)) // If it's an ascii hex number too,
return db_ascii_to_hex(c0, c1); // Give out a hex number
else
error = 2; // Detected an error in 2nd character
}
else
error = 1; // Detected an error in 1st character
return 0; // A fairly boring default value
}
// User interface to read and write raw hex data in Z1
void db_hex(void) {
extern char error; // A global error register for timeouts etc.
int c0;
char db_mode, a0, a1, temp;
#define SINGLEREAD 0 // "> 14" : Read and display contents of address 0x14
#define SINGLEWRITE 1 // "> 14=3b" : Write 0x3b to address 0x14
#define MULTIREAD 2 // "> 14:17" : Read and display addresses 0x14 through 0x17
#define MULTIWRITE 3 // "> 14-17" : Propmpt for new content to 0x14 through 0x17
while (1) { // User interface is infinite loop
db_mode = SINGLEREAD; // Assume first we are doing a single read
DB_PRINTF (PSTR ("\r\n> ")); // Prompt for command
c0 = getchar(); // Get command/address character
if (c0 == 'H') { // "H" for help
DB_PRINTF (PSTR ("\r\nHELP\r\n"));
DB_PRINTF (PSTR ("> 14 : Read and display contents of address 0x14\r\n"));
DB_PRINTF (PSTR ("> 14=3b : Write 0x3b to address 0x14\r\n"));
DB_PRINTF (PSTR ("> 14:17 : Show contents of address 0x14 through 0x17\r\n"));
DB_PRINTF (PSTR ("> 14-17 : Prompt for new content in 0x14 through 0x17\r\n"));
DB_PRINTF (PSTR ("> H : This message\r\n"));
DB_PRINTF (PSTR ("> p : Init DAC\r\n"));
DB_PRINTF (PSTR ("> + : Volume up\r\n"));
DB_PRINTF (PSTR ("> - : Volume down\r\n\r\n"));
}
else if (c0 == 'p') { // "p" for DAC init
temp = dac_init(); // Set up DAC
DB_PRINTF (PSTR ("\r\nDAC init\r\n"));
}
else if (c0 == '+') { // "+" for volume up
temp = dac_volume_up(); // Increase volume
DB_PRINTF (PSTR ("\r\nVolume up\r\n"));
}
else if (c0 == '-') { // "-" for volume down
temp = dac_volume_down(); // Decrease volume
DB_PRINTF (PSTR ("\r\nVolume down\r\n"));
}
else {
a0 = db_gethex(c0); // Get an 8-bit hex number from terminal
if (error == 0) {
c0 = getchar(); // Get third character
if ((c0 == '\r') || (c0 == '\n'))
db_singleread (a0); // If enter, read from the address in a1
else if (c0 == ':')
db_mode = MULTIREAD; // : indicates multiple reads
else if (c0 == '-')
db_mode = MULTIWRITE; // - indicates multiple writes
else if (c0 == '=')
db_mode = SINGLEWRITE; // = indicates a single write
}
}
if (db_mode > SINGLEREAD){ // Must get second address from terminal
a1 = db_gethex(getchar()); // Get an 8-bit hex number from terminal
if (error == 0) {
c0 = getchar(); // Check for a confirming enter
if ((c0 == '\r') || (c0 == '\n')) {
if (db_mode == MULTIREAD) {
DB_PRINTF (PSTR ("\r\n"));
db_multiread (a0, a1);
}
else if (db_mode == MULTIWRITE)
db_multiwrite (a0, a1);
else if (db_mode == SINGLEWRITE)
db_singlewrite (a0, a1);
}
}
}
} // end of infinite for
} // end of main
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -