Firmware: move logging to status.c

Change-Id: Id3fada3d5902ca9e81794020b24f1f480870e986
This commit is contained in:
David Banks 2019-11-09 14:18:47 +00:00
parent 877e075b4d
commit a46db8f641
3 changed files with 116 additions and 98 deletions

View File

@ -481,67 +481,6 @@ uint8_t error_flag = 0;
#define MASK_CLOCK_ERROR 1 #define MASK_CLOCK_ERROR 1
#define MASK_TIMEOUT_ERROR 2 #define MASK_TIMEOUT_ERROR 2
/********************************************************
* Simple string logger, as log0 is expensive
********************************************************/
void logc(char c) {
Serial_TxByte0(c);
if (c == '\n') {
Serial_TxByte0('\r');
}
}
void logs(const char *s) {
while (*s) {
logc(*s++);
}
}
#define logstr(s) logpgmstr(PSTR((s)))
void logpgmstr(const char *s) {
char c;
do {
c = pgm_read_byte(s++);
if (c) {
logc(c);
}
} while (c);
}
void loghex1(uint8_t i) {
i &= 0x0f;
if (i < 10) {
i += '0';
} else {
i += ('A' - 10);
}
logc(i);
}
void loghex2(uint8_t i) {
loghex1(i >> 4);
loghex1(i);
}
void loghex4(uint16_t i) {
loghex2(i >> 8);
loghex2(i);
}
//void loglong(long i) {
// char buffer[16];
// // ltoa adds 176 bytes
// logs(ltoa(i, buffer, 10));
//}
//
//void logint(int i) {
// char buffer[16];
// // itoa adds 176 bytes
// logs(itoa(i, buffer, 10));
//}
/******************************************************** /********************************************************
* User Command Processor * User Command Processor
********************************************************/ ********************************************************/

View File

@ -1,11 +1,11 @@
/* /*
Status.c Status.c
Functions for logging program status to the serial port, to Functions for logging program status to the serial port, to
be used for debugging pruposes etc. be used for debugging pruposes etc.
2008-03-21, P.Harvey-Smith. 2008-03-21, P.Harvey-Smith.
*/ */
#include <avr/interrupt.h> #include <avr/interrupt.h>
@ -14,6 +14,67 @@
#include "terminalcodes.h" #include "terminalcodes.h"
#include "status.h" #include "status.h"
/********************************************************
* Simple string logger, as log0 is expensive
********************************************************/
void logc(char c) {
Serial_TxByte0(c);
if (c == '\n') {
Serial_TxByte0('\r');
}
}
void logs(const char *s) {
while (*s) {
logc(*s++);
}
}
void logpgmstr(const char *s) {
char c;
do {
c = pgm_read_byte(s++);
if (c) {
logc(c);
}
} while (c);
}
void loghex1(uint8_t i) {
i &= 0x0f;
if (i < 10) {
i += '0';
} else {
i += ('A' - 10);
}
logc(i);
}
void loghex2(uint8_t i) {
loghex1(i >> 4);
loghex1(i);
}
void loghex4(uint16_t i) {
loghex2(i >> 8);
loghex2(i);
}
//void loglong(long i) {
// char buffer[16];
// // ltoa adds 176 bytes
// logs(ltoa(i, buffer, 10));
//}
//
//void logint(int i) {
// char buffer[16];
// // itoa adds 176 bytes
// logs(itoa(i, buffer, 10));
//}
#ifdef SERIAL_STATUS #ifdef SERIAL_STATUS
static int StdioSerial_TxByte0(char DataByte, FILE *Stream); static int StdioSerial_TxByte0(char DataByte, FILE *Stream);
@ -24,7 +85,7 @@ FILE ser1stream = FDEV_SETUP_STREAM(StdioSerial_TxByte1,NULL,_FDEV_SETUP_WRITE);
void StdioSerial_TxByte(char DataByte, uint8_t Port) void StdioSerial_TxByte(char DataByte, uint8_t Port)
{ {
#ifdef COOKED_SERIAL #ifdef COOKED_SERIAL
if((DataByte=='\r') || (DataByte=='\n')) if((DataByte=='\r') || (DataByte=='\n'))
{ {
if(Port==1) if(Port==1)
@ -40,12 +101,12 @@ void StdioSerial_TxByte(char DataByte, uint8_t Port)
} }
else else
#endif #endif
if(Port==1) if(Port==1)
Serial_TxByte1(DataByte); Serial_TxByte1(DataByte);
else else
Serial_TxByte0(DataByte); Serial_TxByte0(DataByte);
} }
int StdioSerial_TxByte0(char DataByte, FILE *Stream) int StdioSerial_TxByte0(char DataByte, FILE *Stream)
@ -81,11 +142,11 @@ void USART_Init0(const uint32_t BaudRate)
UCSR0A = 0; UCSR0A = 0;
UCSR0B = ((1 << RXEN0) | (1 << TXEN0)); UCSR0B = ((1 << RXEN0) | (1 << TXEN0));
UCSR0C = ((1 << UCSZ01) | (1 << UCSZ00)); UCSR0C = ((1 << UCSZ01) | (1 << UCSZ00));
UBRR0 = SERIAL_UBBRVAL(BaudRate); UBRR0 = SERIAL_UBBRVAL(BaudRate);
#else #else
UCR = ((1 << RXEN) | (1 << TXEN)); UCR = ((1 << RXEN) | (1 << TXEN));
UBRR = SERIAL_UBBRVAL(BaudRate); UBRR = SERIAL_UBBRVAL(BaudRate);
#endif #endif
} }
@ -97,7 +158,7 @@ void USART_Init1(const uint32_t BaudRate)
UCSR1A = 0; UCSR1A = 0;
UCSR1B = ((1 << RXEN1) | (1 << TXEN1)); UCSR1B = ((1 << RXEN1) | (1 << TXEN1));
UCSR1C = ((1 << UCSZ11) | (1 << UCSZ10)); UCSR1C = ((1 << UCSZ11) | (1 << UCSZ10));
UBRR1 = SERIAL_UBBRVAL(BaudRate); UBRR1 = SERIAL_UBBRVAL(BaudRate);
#endif #endif
} }
@ -135,7 +196,7 @@ char Serial_RxByte0(void)
#ifdef UCSR0A #ifdef UCSR0A
while (!(USR & (1 << RXC0))) ; while (!(USR & (1 << RXC0))) ;
return UDR0; return UDR0;
#else #else
while (!(USR & (1<<RXC))) ; while (!(USR & (1<<RXC))) ;
return UDR; return UDR;
#endif #endif
@ -181,17 +242,17 @@ void Serial_Init(const uint32_t BaudRate0,
USART_Init1(DefaultBaudRate); USART_Init1(DefaultBaudRate);
else else
USART_Init1(BaudRate1); USART_Init1(BaudRate1);
cls(0); cls(0);
cls(1); cls(1);
// log0("stdio initialised\n"); // log0("stdio initialised\n");
// log0("SerialPort0\n"); // log0("SerialPort0\n");
// log1("SerialPort1\n"); // log1("SerialPort1\n");
} }
#ifdef USE_HEXDUMP #ifdef USE_HEXDUMP
void HexDump(const uint8_t *Buff, void HexDump(const uint8_t *Buff,
uint16_t Length, uint16_t Length,
uint8_t Port) uint8_t Port)
{ {
@ -200,14 +261,14 @@ void HexDump(const uint8_t *Buff,
uint16_t LineOffset; uint16_t LineOffset;
uint16_t CharOffset; uint16_t CharOffset;
const uint8_t *BuffPtr; const uint8_t *BuffPtr;
BuffPtr=Buff; BuffPtr=Buff;
for(LineOffset=0;LineOffset<Length;LineOffset+=16, BuffPtr+=16) for(LineOffset=0;LineOffset<Length;LineOffset+=16, BuffPtr+=16)
{ {
LineBuffPos=LineBuff; LineBuffPos=LineBuff;
LineBuffPos+=sprintf(LineBuffPos,"%4.4X ",LineOffset); LineBuffPos+=sprintf(LineBuffPos,"%4.4X ",LineOffset);
for(CharOffset=0;CharOffset<16;CharOffset++) for(CharOffset=0;CharOffset<16;CharOffset++)
{ {
if((LineOffset+CharOffset)<Length) if((LineOffset+CharOffset)<Length)
@ -215,7 +276,7 @@ void HexDump(const uint8_t *Buff,
else else
LineBuffPos+=sprintf(LineBuffPos," "); LineBuffPos+=sprintf(LineBuffPos," ");
} }
for(CharOffset=0;CharOffset<16;CharOffset++) for(CharOffset=0;CharOffset<16;CharOffset++)
{ {
if((LineOffset+CharOffset)<Length) if((LineOffset+CharOffset)<Length)
@ -236,20 +297,20 @@ void HexDump(const uint8_t *Buff,
} }
} }
void HexDumpHead(const uint8_t *Buff, void HexDumpHead(const uint8_t *Buff,
uint16_t Length, uint16_t Length,
uint8_t Port) uint8_t Port)
{ {
FILE *File; FILE *File;
File=&ser0stream; File=&ser0stream;
switch (Port) switch (Port)
{ {
case 0 : File=&ser0stream; break; case 0 : File=&ser0stream; break;
case 1 : File=&ser1stream; break; case 1 : File=&ser1stream; break;
} }
fprintf_P(File,PSTR("%d\n"),Buff); fprintf_P(File,PSTR("%d\n"),Buff);
fprintf_P(File,PSTR("Addr 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ASCII\n")); fprintf_P(File,PSTR("Addr 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ASCII\n"));
@ -258,13 +319,13 @@ void HexDumpHead(const uint8_t *Buff,
HexDump(Buff,Length,Port); HexDump(Buff,Length,Port);
}; };
#else #else
void HexDump(const uint8_t *Buff, void HexDump(const uint8_t *Buff,
uint16_t Length, uint16_t Length,
uint8_t Port) {}; uint8_t Port) {};
void HexDumpHead(const uint8_t *Buff, void HexDumpHead(const uint8_t *Buff,
uint16_t Length, uint16_t Length,
uint8_t Port) {}; uint8_t Port) {};
#endif #endif
#else #else
@ -283,10 +344,10 @@ void Serial_Init(const uint32_t BaudRate0,
void cls(uint8_t Port) {}; void cls(uint8_t Port) {};
void HexDump(const uint8_t *Buff, void HexDump(const uint8_t *Buff,
uint16_t Length, uint16_t Length,
uint8_t Port) {}; uint8_t Port) {};
void HexDumpHead(const uint8_t *Buff, void HexDumpHead(const uint8_t *Buff,
uint16_t Length, uint16_t Length,
uint8_t Port) {}; uint8_t Port) {};

View File

@ -1,14 +1,14 @@
/* /*
Status.h Status.h
Functions for logging program status to the serial port, to Functions for logging program status to the serial port, to
be used for debugging pruposes etc. be used for debugging pruposes etc.
2008-03-21, P.Harvey-Smith. 2008-03-21, P.Harvey-Smith.
Some functions and macros borrowed from Dean Camera's LURFA Some functions and macros borrowed from Dean Camera's LURFA
USB libraries. USB libraries.
*/ */
#include <avr/io.h> #include <avr/io.h>
@ -20,12 +20,30 @@
#ifndef __STATUS_DEFINES__ #ifndef __STATUS_DEFINES__
#define __STATUS_DEFINES__ #define __STATUS_DEFINES__
/********************************************************
* Simple string logger, as log0 is expensive
********************************************************/
#define logstr(s) logpgmstr(PSTR((s)))
void logc(char c);
void logs(const char *s);
void logpgmstr(const char *s);
void loghex1(uint8_t i);
void loghex2(uint8_t i);
void loghex4(uint16_t i);
//void loglong(long i);
//void logint(int i);
#ifdef SERIAL_STATUS #ifdef SERIAL_STATUS
#define log0(format,...) fprintf_P(&ser0stream,PSTR(format),##__VA_ARGS__) #define log0(format,...) fprintf_P(&ser0stream,PSTR(format),##__VA_ARGS__)
#define log1(format,...) fprintf_P(&ser1stream,PSTR(format),##__VA_ARGS__) #define log1(format,...) fprintf_P(&ser1stream,PSTR(format),##__VA_ARGS__)
#else #else
#define log0(format,...) #define log0(format,...)
#define log1(format,...) #define log1(format,...)
#endif #endif
// //
@ -36,7 +54,7 @@ extern FILE ser0stream;
extern FILE ser1stream; extern FILE ser1stream;
/* Default baud rate if 0 passed to Serial_Init */ /* Default baud rate if 0 passed to Serial_Init */
#define DefaultBaudRate 9600 #define DefaultBaudRate 9600
/** Indicates whether a character has been received through the USART - boolean false if no character /** Indicates whether a character has been received through the USART - boolean false if no character
@ -54,7 +72,7 @@ extern FILE ser1stream;
*/ */
#define SERIAL_2X_UBBRVAL(baud) (((F_CPU / 8) / baud) - 1) #define SERIAL_2X_UBBRVAL(baud) (((F_CPU / 8) / baud) - 1)
#define SerEOL0() { Serial_TxByte0('\r'); Serial_TxByte0('\n'); } #define SerEOL0() { Serial_TxByte0('\r'); Serial_TxByte0('\n'); }
#ifdef NOUSART1 #ifdef NOUSART1
#undef UCSR1A #undef UCSR1A
@ -75,10 +93,10 @@ void Serial_Init(const uint32_t BaudRate0,
void cls(uint8_t Port); void cls(uint8_t Port);
void HexDump(const uint8_t *Buff, void HexDump(const uint8_t *Buff,
uint16_t Length, uint16_t Length,
uint8_t Port); uint8_t Port);
void HexDumpHead(const uint8_t *Buff, void HexDumpHead(const uint8_t *Buff,
uint16_t Length, uint16_t Length,
uint8_t Port); uint8_t Port);