📄 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): Borge Strand
*
* Description:
*
* Debug code used for debugging project on an RS232 terminal emulator.
* Also includes a hex-based user interface that gives access to local
* registers in nRF24Z1, ADC, DAC and ARX-side EEPROPM. These functions
* are not requirred in code compiled for production versions.
*
* Compiler: Tested with WinAVR, avr-gcc (GCC) 3.4.3
*
* Revision: 2.0
*
*==============================================================================
*/
#include "uartdebug.h"
#ifdef DEBUG // All of this file is inside this ifdef!
// 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 nRF24Z1 or RX EEPROM
void db_singleread(char adr) {
db_putenter(); // Reading from Z1 ATX register
mcu_putchar('(');
db_puthex(adr);
mcu_putchar(')');
mcu_putchar(' ');
db_puthex(z1_singleread(adr));
}
// Write a single byte to nRF24Z1 or RX EEPROM, then read back
void db_singlewrite(char adr, char data) {
#ifdef ADCUI
db_putenter(); // Sending data to ADC
mcu_putchar('A'); // There's no readback from ADC, only say "A"
adc_singlewrite(adr, data); // Do the actual write
#endif
#ifdef DACUI
dac_singlewrite(adr, data); // Do the write, DAC code reports RXEXEC status
#endif
#ifdef Z1UI
z1_singlewrite(adr, data); // Perform the hardware write access
db_singleread(adr); // Display result
#endif
}
// 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;
}
// Writes a new line to the terminal
void db_putenter(void) {
mcu_putchar('\r');
mcu_putchar('\n');
}
// Convert lowest nibble to ascii character
void db_puthex(char c) {
char temp = c;
c >>=4; // Shift in most significant hex character
if (c < 10) // 0-9 -> '0' - '9'
mcu_putchar(c + '0');
else // A-F -> 'A' - 'F'
mcu_putchar(c - 0x0A + 'A');
c = temp & 0x0F; // Mask in least significant hex character
if (c < 10) // 0-9 -> '0' - '9'
mcu_putchar(c + '0');
else // A-F -> 'A' - 'F'
mcu_putchar(c - 0x0A + 'A');
}
// 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 = mcu_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 stupid default value
}
#ifdef DEBUGIF
// 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;
char a0, a1;
char run=1; // Keep running hex user interface!
#define SINGLEREAD 0 // "> 14" : Read and display contents of address 0x14
#define SINGLEWRITE 1 // "> 14=3b" : Write 0x3b to address 0x14
while (run==1) { // User interface is infinite loop
db_mode = SINGLEREAD; // Assume first we are doing a single read
db_putenter();
mcu_putchar('>');
c0 = mcu_getchar(); // Get command/address character
if (c0 == 'Q') { // "Q" for quit
db_putenter();
mcu_putchar('b');
mcu_putchar('y');
mcu_putchar('e');
db_putenter();
db_putenter();
run = 0; // Break loop
}
else if (c0 == 'R') { // "R" for random byte
db_putenter();
db_puthex(mcu_randombyte());
db_putenter();
db_putenter();
}
else if (c0 == 'p') // "p" for ATX wakeup pin
mcu_z1wakeup_pin();
else {
a0 = db_gethex(c0); // Get an 8-bit hex number from terminal
if (error == 0) {
c0 = mcu_getchar(); // Get third character
if ((c0 == '\r') || (c0 == '\n'))
db_singleread (a0); // If enter, read from the address in a1
else if (c0 == '=') { // = indicates a single write
a1 = db_gethex(mcu_getchar()); // Get an 8-bit hex number from terminal
if (error == 0) {
c0 = mcu_getchar(); // Check for a confirming enter
if ((c0 == '\r') || (c0 == '\n'))
db_singlewrite (a0, a1);
}
}
}
}
} // end of infinite for
} // end of main
#endif // DEBUGIF
// Print the address configuration currently in TX, ADDR_0, ADDR_1, ADDR_2, ADDR_3, ADDR_4, LINKCSTATE are
// located on consecutive addresses. Thus use a while loop to print them out.
void db_showadr(void) {
int n=ADDR_0;
while (n <= LINKCSTATE) {
db_puthex(z1_singleread(n++));
mcu_putchar(' ');
}
db_putenter();
}
#endif // This whole file is inside a define!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -